Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/module.h>
0003 #include <linux/kernel.h>
0004 #include <linux/string.h>
0005 #include <linux/socket.h>
0006 #include <linux/net.h>
0007 #include <linux/fs.h>
0008 #include <net/af_unix.h>
0009 #include <net/scm.h>
0010 #include <linux/init.h>
0011 #include <linux/io_uring.h>
0012 
0013 #include "scm.h"
0014 
0015 unsigned int unix_tot_inflight;
0016 EXPORT_SYMBOL(unix_tot_inflight);
0017 
0018 LIST_HEAD(gc_inflight_list);
0019 EXPORT_SYMBOL(gc_inflight_list);
0020 
0021 DEFINE_SPINLOCK(unix_gc_lock);
0022 EXPORT_SYMBOL(unix_gc_lock);
0023 
0024 struct sock *unix_get_socket(struct file *filp)
0025 {
0026     struct sock *u_sock = NULL;
0027     struct inode *inode = file_inode(filp);
0028 
0029     /* Socket ? */
0030     if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
0031         struct socket *sock = SOCKET_I(inode);
0032         struct sock *s = sock->sk;
0033 
0034         /* PF_UNIX ? */
0035         if (s && sock->ops && sock->ops->family == PF_UNIX)
0036             u_sock = s;
0037     } else {
0038         /* Could be an io_uring instance */
0039         u_sock = io_uring_get_socket(filp);
0040     }
0041     return u_sock;
0042 }
0043 EXPORT_SYMBOL(unix_get_socket);
0044 
0045 /* Keep the number of times in flight count for the file
0046  * descriptor if it is for an AF_UNIX socket.
0047  */
0048 void unix_inflight(struct user_struct *user, struct file *fp)
0049 {
0050     struct sock *s = unix_get_socket(fp);
0051 
0052     spin_lock(&unix_gc_lock);
0053 
0054     if (s) {
0055         struct unix_sock *u = unix_sk(s);
0056 
0057         if (atomic_long_inc_return(&u->inflight) == 1) {
0058             BUG_ON(!list_empty(&u->link));
0059             list_add_tail(&u->link, &gc_inflight_list);
0060         } else {
0061             BUG_ON(list_empty(&u->link));
0062         }
0063         /* Paired with READ_ONCE() in wait_for_unix_gc() */
0064         WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
0065     }
0066     user->unix_inflight++;
0067     spin_unlock(&unix_gc_lock);
0068 }
0069 
0070 void unix_notinflight(struct user_struct *user, struct file *fp)
0071 {
0072     struct sock *s = unix_get_socket(fp);
0073 
0074     spin_lock(&unix_gc_lock);
0075 
0076     if (s) {
0077         struct unix_sock *u = unix_sk(s);
0078 
0079         BUG_ON(!atomic_long_read(&u->inflight));
0080         BUG_ON(list_empty(&u->link));
0081 
0082         if (atomic_long_dec_and_test(&u->inflight))
0083             list_del_init(&u->link);
0084         /* Paired with READ_ONCE() in wait_for_unix_gc() */
0085         WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
0086     }
0087     user->unix_inflight--;
0088     spin_unlock(&unix_gc_lock);
0089 }
0090 
0091 /*
0092  * The "user->unix_inflight" variable is protected by the garbage
0093  * collection lock, and we just read it locklessly here. If you go
0094  * over the limit, there might be a tiny race in actually noticing
0095  * it across threads. Tough.
0096  */
0097 static inline bool too_many_unix_fds(struct task_struct *p)
0098 {
0099     struct user_struct *user = current_user();
0100 
0101     if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
0102         return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
0103     return false;
0104 }
0105 
0106 int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
0107 {
0108     int i;
0109 
0110     if (too_many_unix_fds(current))
0111         return -ETOOMANYREFS;
0112 
0113     /*
0114      * Need to duplicate file references for the sake of garbage
0115      * collection.  Otherwise a socket in the fps might become a
0116      * candidate for GC while the skb is not yet queued.
0117      */
0118     UNIXCB(skb).fp = scm_fp_dup(scm->fp);
0119     if (!UNIXCB(skb).fp)
0120         return -ENOMEM;
0121 
0122     for (i = scm->fp->count - 1; i >= 0; i--)
0123         unix_inflight(scm->fp->user, scm->fp->fp[i]);
0124     return 0;
0125 }
0126 EXPORT_SYMBOL(unix_attach_fds);
0127 
0128 void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
0129 {
0130     int i;
0131 
0132     scm->fp = UNIXCB(skb).fp;
0133     UNIXCB(skb).fp = NULL;
0134 
0135     for (i = scm->fp->count-1; i >= 0; i--)
0136         unix_notinflight(scm->fp->user, scm->fp->fp[i]);
0137 }
0138 EXPORT_SYMBOL(unix_detach_fds);
0139 
0140 void unix_destruct_scm(struct sk_buff *skb)
0141 {
0142     struct scm_cookie scm;
0143 
0144     memset(&scm, 0, sizeof(scm));
0145     scm.pid  = UNIXCB(skb).pid;
0146     if (UNIXCB(skb).fp)
0147         unix_detach_fds(&scm, skb);
0148 
0149     /* Alas, it calls VFS */
0150     /* So fscking what? fput() had been SMP-safe since the last Summer */
0151     scm_destroy(&scm);
0152     sock_wfree(skb);
0153 }
0154 EXPORT_SYMBOL(unix_destruct_scm);