1f4e65870SJens Axboe // SPDX-License-Identifier: GPL-2.0
2f4e65870SJens Axboe #include <linux/module.h>
3f4e65870SJens Axboe #include <linux/kernel.h>
4f4e65870SJens Axboe #include <linux/string.h>
5f4e65870SJens Axboe #include <linux/socket.h>
6f4e65870SJens Axboe #include <linux/net.h>
7f4e65870SJens Axboe #include <linux/fs.h>
8f4e65870SJens Axboe #include <net/af_unix.h>
9f4e65870SJens Axboe #include <net/scm.h>
10f4e65870SJens Axboe #include <linux/init.h>
11a3ec6005SJens Axboe #include <linux/io_uring.h>
12f4e65870SJens Axboe
13f4e65870SJens Axboe #include "scm.h"
14f4e65870SJens Axboe
15f4e65870SJens Axboe unsigned int unix_tot_inflight;
16f4e65870SJens Axboe EXPORT_SYMBOL(unix_tot_inflight);
17f4e65870SJens Axboe
18f4e65870SJens Axboe LIST_HEAD(gc_inflight_list);
19f4e65870SJens Axboe EXPORT_SYMBOL(gc_inflight_list);
20f4e65870SJens Axboe
21f4e65870SJens Axboe DEFINE_SPINLOCK(unix_gc_lock);
22f4e65870SJens Axboe EXPORT_SYMBOL(unix_gc_lock);
23f4e65870SJens Axboe
unix_get_socket(struct file * filp)24f4e65870SJens Axboe struct sock *unix_get_socket(struct file *filp)
25f4e65870SJens Axboe {
26f4e65870SJens Axboe struct sock *u_sock = NULL;
27f4e65870SJens Axboe struct inode *inode = file_inode(filp);
28f4e65870SJens Axboe
29f4e65870SJens Axboe /* Socket ? */
30f4e65870SJens Axboe if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
31f4e65870SJens Axboe struct socket *sock = SOCKET_I(inode);
321ded5e5aSEric Dumazet const struct proto_ops *ops = READ_ONCE(sock->ops);
33f4e65870SJens Axboe struct sock *s = sock->sk;
34f4e65870SJens Axboe
35f4e65870SJens Axboe /* PF_UNIX ? */
361ded5e5aSEric Dumazet if (s && ops && ops->family == PF_UNIX)
37f4e65870SJens Axboe u_sock = s;
38f4e65870SJens Axboe }
39303c0a13SJens Axboe
40f4e65870SJens Axboe return u_sock;
41f4e65870SJens Axboe }
42f4e65870SJens Axboe EXPORT_SYMBOL(unix_get_socket);
43f4e65870SJens Axboe
44f4e65870SJens Axboe /* Keep the number of times in flight count for the file
45f4e65870SJens Axboe * descriptor if it is for an AF_UNIX socket.
46f4e65870SJens Axboe */
unix_inflight(struct user_struct * user,struct file * fp)47f4e65870SJens Axboe void unix_inflight(struct user_struct *user, struct file *fp)
48f4e65870SJens Axboe {
49f4e65870SJens Axboe struct sock *s = unix_get_socket(fp);
50f4e65870SJens Axboe
51f4e65870SJens Axboe spin_lock(&unix_gc_lock);
52f4e65870SJens Axboe
53f4e65870SJens Axboe if (s) {
54f4e65870SJens Axboe struct unix_sock *u = unix_sk(s);
55f4e65870SJens Axboe
56*301fdbaaSKuniyuki Iwashima if (!u->inflight) {
57f4e65870SJens Axboe BUG_ON(!list_empty(&u->link));
58f4e65870SJens Axboe list_add_tail(&u->link, &gc_inflight_list);
59f4e65870SJens Axboe } else {
60f4e65870SJens Axboe BUG_ON(list_empty(&u->link));
61f4e65870SJens Axboe }
62*301fdbaaSKuniyuki Iwashima u->inflight++;
639d6d7f1cSEric Dumazet /* Paired with READ_ONCE() in wait_for_unix_gc() */
649d6d7f1cSEric Dumazet WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
65f4e65870SJens Axboe }
660bc36c06SKuniyuki Iwashima WRITE_ONCE(user->unix_inflight, user->unix_inflight + 1);
67f4e65870SJens Axboe spin_unlock(&unix_gc_lock);
68f4e65870SJens Axboe }
69f4e65870SJens Axboe
unix_notinflight(struct user_struct * user,struct file * fp)70f4e65870SJens Axboe void unix_notinflight(struct user_struct *user, struct file *fp)
71f4e65870SJens Axboe {
72f4e65870SJens Axboe struct sock *s = unix_get_socket(fp);
73f4e65870SJens Axboe
74f4e65870SJens Axboe spin_lock(&unix_gc_lock);
75f4e65870SJens Axboe
76f4e65870SJens Axboe if (s) {
77f4e65870SJens Axboe struct unix_sock *u = unix_sk(s);
78f4e65870SJens Axboe
79*301fdbaaSKuniyuki Iwashima BUG_ON(!u->inflight);
80f4e65870SJens Axboe BUG_ON(list_empty(&u->link));
81f4e65870SJens Axboe
82*301fdbaaSKuniyuki Iwashima u->inflight--;
83*301fdbaaSKuniyuki Iwashima if (!u->inflight)
84f4e65870SJens Axboe list_del_init(&u->link);
859d6d7f1cSEric Dumazet /* Paired with READ_ONCE() in wait_for_unix_gc() */
869d6d7f1cSEric Dumazet WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
87f4e65870SJens Axboe }
880bc36c06SKuniyuki Iwashima WRITE_ONCE(user->unix_inflight, user->unix_inflight - 1);
89f4e65870SJens Axboe spin_unlock(&unix_gc_lock);
90f4e65870SJens Axboe }
91f4e65870SJens Axboe
92f4e65870SJens Axboe /*
93f4e65870SJens Axboe * The "user->unix_inflight" variable is protected by the garbage
94f4e65870SJens Axboe * collection lock, and we just read it locklessly here. If you go
95f4e65870SJens Axboe * over the limit, there might be a tiny race in actually noticing
96f4e65870SJens Axboe * it across threads. Tough.
97f4e65870SJens Axboe */
too_many_unix_fds(struct task_struct * p)98f4e65870SJens Axboe static inline bool too_many_unix_fds(struct task_struct *p)
99f4e65870SJens Axboe {
100f4e65870SJens Axboe struct user_struct *user = current_user();
101f4e65870SJens Axboe
1020bc36c06SKuniyuki Iwashima if (unlikely(READ_ONCE(user->unix_inflight) > task_rlimit(p, RLIMIT_NOFILE)))
103f4e65870SJens Axboe return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
104f4e65870SJens Axboe return false;
105f4e65870SJens Axboe }
106f4e65870SJens Axboe
unix_attach_fds(struct scm_cookie * scm,struct sk_buff * skb)107f4e65870SJens Axboe int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
108f4e65870SJens Axboe {
109f4e65870SJens Axboe int i;
110f4e65870SJens Axboe
111f4e65870SJens Axboe if (too_many_unix_fds(current))
112f4e65870SJens Axboe return -ETOOMANYREFS;
113f4e65870SJens Axboe
114f4e65870SJens Axboe /*
115f4e65870SJens Axboe * Need to duplicate file references for the sake of garbage
116f4e65870SJens Axboe * collection. Otherwise a socket in the fps might become a
117f4e65870SJens Axboe * candidate for GC while the skb is not yet queued.
118f4e65870SJens Axboe */
119f4e65870SJens Axboe UNIXCB(skb).fp = scm_fp_dup(scm->fp);
120f4e65870SJens Axboe if (!UNIXCB(skb).fp)
121f4e65870SJens Axboe return -ENOMEM;
122f4e65870SJens Axboe
123f4e65870SJens Axboe for (i = scm->fp->count - 1; i >= 0; i--)
124f4e65870SJens Axboe unix_inflight(scm->fp->user, scm->fp->fp[i]);
125f4e65870SJens Axboe return 0;
126f4e65870SJens Axboe }
127f4e65870SJens Axboe EXPORT_SYMBOL(unix_attach_fds);
128f4e65870SJens Axboe
unix_detach_fds(struct scm_cookie * scm,struct sk_buff * skb)129f4e65870SJens Axboe void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
130f4e65870SJens Axboe {
131f4e65870SJens Axboe int i;
132f4e65870SJens Axboe
133f4e65870SJens Axboe scm->fp = UNIXCB(skb).fp;
134f4e65870SJens Axboe UNIXCB(skb).fp = NULL;
135f4e65870SJens Axboe
136f4e65870SJens Axboe for (i = scm->fp->count-1; i >= 0; i--)
137f4e65870SJens Axboe unix_notinflight(scm->fp->user, scm->fp->fp[i]);
138f4e65870SJens Axboe }
139f4e65870SJens Axboe EXPORT_SYMBOL(unix_detach_fds);
140f4e65870SJens Axboe
unix_destruct_scm(struct sk_buff * skb)141f4e65870SJens Axboe void unix_destruct_scm(struct sk_buff *skb)
142f4e65870SJens Axboe {
143f4e65870SJens Axboe struct scm_cookie scm;
144f4e65870SJens Axboe
145f4e65870SJens Axboe memset(&scm, 0, sizeof(scm));
146f4e65870SJens Axboe scm.pid = UNIXCB(skb).pid;
147f4e65870SJens Axboe if (UNIXCB(skb).fp)
148f4e65870SJens Axboe unix_detach_fds(&scm, skb);
149f4e65870SJens Axboe
150f4e65870SJens Axboe /* Alas, it calls VFS */
151f4e65870SJens Axboe /* So fscking what? fput() had been SMP-safe since the last Summer */
152f4e65870SJens Axboe scm_destroy(&scm);
153f4e65870SJens Axboe sock_wfree(skb);
154f4e65870SJens Axboe }
155f4e65870SJens Axboe EXPORT_SYMBOL(unix_destruct_scm);
15610369080SEric Dumazet
io_uring_destruct_scm(struct sk_buff * skb)15710369080SEric Dumazet void io_uring_destruct_scm(struct sk_buff *skb)
15810369080SEric Dumazet {
15910369080SEric Dumazet unix_destruct_scm(skb);
16010369080SEric Dumazet }
16110369080SEric Dumazet EXPORT_SYMBOL(io_uring_destruct_scm);
162