GNU Linux-libre 4.14.251-gnu1
[releases.git] / net / unix / scm.c
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>
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 #include <linux/sched/signal.h>
12
13 #include "scm.h"
14
15 unsigned int unix_tot_inflight;
16 EXPORT_SYMBOL(unix_tot_inflight);
17
18 LIST_HEAD(gc_inflight_list);
19 EXPORT_SYMBOL(gc_inflight_list);
20
21 DEFINE_SPINLOCK(unix_gc_lock);
22 EXPORT_SYMBOL(unix_gc_lock);
23
24 struct sock *unix_get_socket(struct file *filp)
25 {
26         struct sock *u_sock = NULL;
27         struct inode *inode = file_inode(filp);
28
29         /* Socket ? */
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;
33
34                 /* PF_UNIX ? */
35                 if (s && sock->ops && sock->ops->family == PF_UNIX)
36                         u_sock = s;
37         }
38         return u_sock;
39 }
40 EXPORT_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  */
45 void 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 (atomic_long_inc_return(&u->inflight) == 1) {
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                 unix_tot_inflight++;
61         }
62         user->unix_inflight++;
63         spin_unlock(&unix_gc_lock);
64 }
65
66 void unix_notinflight(struct user_struct *user, struct file *fp)
67 {
68         struct sock *s = unix_get_socket(fp);
69
70         spin_lock(&unix_gc_lock);
71
72         if (s) {
73                 struct unix_sock *u = unix_sk(s);
74
75                 BUG_ON(!atomic_long_read(&u->inflight));
76                 BUG_ON(list_empty(&u->link));
77
78                 if (atomic_long_dec_and_test(&u->inflight))
79                         list_del_init(&u->link);
80                 unix_tot_inflight--;
81         }
82         user->unix_inflight--;
83         spin_unlock(&unix_gc_lock);
84 }
85
86 /*
87  * The "user->unix_inflight" variable is protected by the garbage
88  * collection lock, and we just read it locklessly here. If you go
89  * over the limit, there might be a tiny race in actually noticing
90  * it across threads. Tough.
91  */
92 static inline bool too_many_unix_fds(struct task_struct *p)
93 {
94         struct user_struct *user = current_user();
95
96         if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
97                 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
98         return false;
99 }
100
101 int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
102 {
103         int i;
104
105         if (too_many_unix_fds(current))
106                 return -ETOOMANYREFS;
107
108         /*
109          * Need to duplicate file references for the sake of garbage
110          * collection.  Otherwise a socket in the fps might become a
111          * candidate for GC while the skb is not yet queued.
112          */
113         UNIXCB(skb).fp = scm_fp_dup(scm->fp);
114         if (!UNIXCB(skb).fp)
115                 return -ENOMEM;
116
117         for (i = scm->fp->count - 1; i >= 0; i--)
118                 unix_inflight(scm->fp->user, scm->fp->fp[i]);
119         return 0;
120 }
121 EXPORT_SYMBOL(unix_attach_fds);
122
123 void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
124 {
125         int i;
126
127         scm->fp = UNIXCB(skb).fp;
128         UNIXCB(skb).fp = NULL;
129
130         for (i = scm->fp->count-1; i >= 0; i--)
131                 unix_notinflight(scm->fp->user, scm->fp->fp[i]);
132 }
133 EXPORT_SYMBOL(unix_detach_fds);
134
135 void unix_destruct_scm(struct sk_buff *skb)
136 {
137         struct scm_cookie scm;
138
139         memset(&scm, 0, sizeof(scm));
140         scm.pid  = UNIXCB(skb).pid;
141         if (UNIXCB(skb).fp)
142                 unix_detach_fds(&scm, skb);
143
144         /* Alas, it calls VFS */
145         /* So fscking what? fput() had been SMP-safe since the last Summer */
146         scm_destroy(&scm);
147         sock_wfree(skb);
148 }
149 EXPORT_SYMBOL(unix_destruct_scm);