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>
14 unsigned int unix_tot_inflight;
15 EXPORT_SYMBOL(unix_tot_inflight);
17 LIST_HEAD(gc_inflight_list);
18 EXPORT_SYMBOL(gc_inflight_list);
20 DEFINE_SPINLOCK(unix_gc_lock);
21 EXPORT_SYMBOL(unix_gc_lock);
23 struct sock *unix_get_socket(struct file *filp)
25 struct sock *u_sock = NULL;
26 struct inode *inode = file_inode(filp);
29 if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
30 struct socket *sock = SOCKET_I(inode);
31 struct sock *s = sock->sk;
34 if (s && sock->ops && sock->ops->family == PF_UNIX)
39 EXPORT_SYMBOL(unix_get_socket);
41 /* Keep the number of times in flight count for the file
42 * descriptor if it is for an AF_UNIX socket.
44 void unix_inflight(struct user_struct *user, struct file *fp)
46 struct sock *s = unix_get_socket(fp);
48 spin_lock(&unix_gc_lock);
51 struct unix_sock *u = unix_sk(s);
53 if (atomic_long_inc_return(&u->inflight) == 1) {
54 BUG_ON(!list_empty(&u->link));
55 list_add_tail(&u->link, &gc_inflight_list);
57 BUG_ON(list_empty(&u->link));
61 user->unix_inflight++;
62 spin_unlock(&unix_gc_lock);
65 void unix_notinflight(struct user_struct *user, struct file *fp)
67 struct sock *s = unix_get_socket(fp);
69 spin_lock(&unix_gc_lock);
72 struct unix_sock *u = unix_sk(s);
74 BUG_ON(!atomic_long_read(&u->inflight));
75 BUG_ON(list_empty(&u->link));
77 if (atomic_long_dec_and_test(&u->inflight))
78 list_del_init(&u->link);
81 user->unix_inflight--;
82 spin_unlock(&unix_gc_lock);
86 * The "user->unix_inflight" variable is protected by the garbage
87 * collection lock, and we just read it locklessly here. If you go
88 * over the limit, there might be a tiny race in actually noticing
89 * it across threads. Tough.
91 static inline bool too_many_unix_fds(struct task_struct *p)
93 struct user_struct *user = current_user();
95 if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
96 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
100 #define MAX_RECURSION_LEVEL 4
102 int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
105 unsigned char max_level = 0;
107 if (too_many_unix_fds(current))
108 return -ETOOMANYREFS;
110 for (i = scm->fp->count - 1; i >= 0; i--) {
111 struct sock *sk = unix_get_socket(scm->fp->fp[i]);
114 max_level = max(max_level,
115 unix_sk(sk)->recursion_level);
117 if (unlikely(max_level > MAX_RECURSION_LEVEL))
118 return -ETOOMANYREFS;
121 * Need to duplicate file references for the sake of garbage
122 * collection. Otherwise a socket in the fps might become a
123 * candidate for GC while the skb is not yet queued.
125 UNIXCB(skb).fp = scm_fp_dup(scm->fp);
129 for (i = scm->fp->count - 1; i >= 0; i--)
130 unix_inflight(scm->fp->user, scm->fp->fp[i]);
133 EXPORT_SYMBOL(unix_attach_fds);
135 void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
139 scm->fp = UNIXCB(skb).fp;
140 UNIXCB(skb).fp = NULL;
142 for (i = scm->fp->count-1; i >= 0; i--)
143 unix_notinflight(scm->fp->user, scm->fp->fp[i]);
145 EXPORT_SYMBOL(unix_detach_fds);
147 void unix_destruct_scm(struct sk_buff *skb)
149 struct scm_cookie scm;
151 memset(&scm, 0, sizeof(scm));
152 scm.pid = UNIXCB(skb).pid;
154 unix_detach_fds(&scm, skb);
156 /* Alas, it calls VFS */
157 /* So fscking what? fput() had been SMP-safe since the last Summer */
161 EXPORT_SYMBOL(unix_destruct_scm);