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