1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/socket.h>
8 #include <net/af_unix.h>
10 #include <linux/init.h>
11 #include <linux/io_uring.h>
15 unsigned int unix_tot_inflight;
16 EXPORT_SYMBOL(unix_tot_inflight);
18 LIST_HEAD(gc_inflight_list);
19 EXPORT_SYMBOL(gc_inflight_list);
21 DEFINE_SPINLOCK(unix_gc_lock);
22 EXPORT_SYMBOL(unix_gc_lock);
24 struct sock *unix_get_socket(struct file *filp)
26 struct sock *u_sock = NULL;
27 struct inode *inode = file_inode(filp);
30 if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
31 struct socket *sock = SOCKET_I(inode);
32 struct sock *s = sock->sk;
35 if (s && sock->ops && sock->ops->family == PF_UNIX)
38 /* Could be an io_uring instance */
39 u_sock = io_uring_get_socket(filp);
43 EXPORT_SYMBOL(unix_get_socket);
45 /* Keep the number of times in flight count for the file
46 * descriptor if it is for an AF_UNIX socket.
48 void unix_inflight(struct user_struct *user, struct file *fp)
50 struct sock *s = unix_get_socket(fp);
52 spin_lock(&unix_gc_lock);
55 struct unix_sock *u = unix_sk(s);
57 if (atomic_long_inc_return(&u->inflight) == 1) {
58 BUG_ON(!list_empty(&u->link));
59 list_add_tail(&u->link, &gc_inflight_list);
61 BUG_ON(list_empty(&u->link));
63 /* Paired with READ_ONCE() in wait_for_unix_gc() */
64 WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
66 user->unix_inflight++;
67 spin_unlock(&unix_gc_lock);
70 void unix_notinflight(struct user_struct *user, struct file *fp)
72 struct sock *s = unix_get_socket(fp);
74 spin_lock(&unix_gc_lock);
77 struct unix_sock *u = unix_sk(s);
79 BUG_ON(!atomic_long_read(&u->inflight));
80 BUG_ON(list_empty(&u->link));
82 if (atomic_long_dec_and_test(&u->inflight))
83 list_del_init(&u->link);
84 /* Paired with READ_ONCE() in wait_for_unix_gc() */
85 WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
87 user->unix_inflight--;
88 spin_unlock(&unix_gc_lock);
92 * The "user->unix_inflight" variable is protected by the garbage
93 * collection lock, and we just read it locklessly here. If you go
94 * over the limit, there might be a tiny race in actually noticing
95 * it across threads. Tough.
97 static inline bool too_many_unix_fds(struct task_struct *p)
99 struct user_struct *user = current_user();
101 if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
102 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
106 int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
110 if (too_many_unix_fds(current))
111 return -ETOOMANYREFS;
114 * Need to duplicate file references for the sake of garbage
115 * collection. Otherwise a socket in the fps might become a
116 * candidate for GC while the skb is not yet queued.
118 UNIXCB(skb).fp = scm_fp_dup(scm->fp);
122 for (i = scm->fp->count - 1; i >= 0; i--)
123 unix_inflight(scm->fp->user, scm->fp->fp[i]);
126 EXPORT_SYMBOL(unix_attach_fds);
128 void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
132 scm->fp = UNIXCB(skb).fp;
133 UNIXCB(skb).fp = NULL;
135 for (i = scm->fp->count-1; i >= 0; i--)
136 unix_notinflight(scm->fp->user, scm->fp->fp[i]);
138 EXPORT_SYMBOL(unix_detach_fds);
140 void unix_destruct_scm(struct sk_buff *skb)
142 struct scm_cookie scm;
144 memset(&scm, 0, sizeof(scm));
145 scm.pid = UNIXCB(skb).pid;
147 unix_detach_fds(&scm, skb);
149 /* Alas, it calls VFS */
150 /* So fscking what? fput() had been SMP-safe since the last Summer */
154 EXPORT_SYMBOL(unix_destruct_scm);
156 void io_uring_destruct_scm(struct sk_buff *skb)
158 unix_destruct_scm(skb);
160 EXPORT_SYMBOL(io_uring_destruct_scm);