Blame view

net/core/scm.c 7.62 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
  /* scm.c - Socket level control messages processing.
   *
   * Author:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
   *              Alignment and value checking mods by Craig Metz
   *
   *		This program is free software; you can redistribute it and/or
   *		modify it under the terms of the GNU General Public License
   *		as published by the Free Software Foundation; either version
   *		2 of the License, or (at your option) any later version.
   */
  
  #include <linux/module.h>
  #include <linux/signal.h>
4fc268d24   Randy Dunlap   [PATCH] capable/c...
14
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
  #include <linux/errno.h>
  #include <linux/sched.h>
  #include <linux/mm.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
25
26
  #include <linux/stat.h>
  #include <linux/socket.h>
  #include <linux/file.h>
  #include <linux/fcntl.h>
  #include <linux/net.h>
  #include <linux/interrupt.h>
  #include <linux/netdevice.h>
  #include <linux/security.h>
92f28d973   Eric W. Biederman   scm: Require CAP_...
27
  #include <linux/pid_namespace.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
28
29
  #include <linux/pid.h>
  #include <linux/nsproxy.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
30
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
37
38
  #include <asm/uaccess.h>
  
  #include <net/protocol.h>
  #include <linux/skbuff.h>
  #include <net/sock.h>
  #include <net/compat.h>
  #include <net/scm.h>
d84295067   Daniel Wagner   net: net_cls: fd ...
39
  #include <net/cls_cgroup.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
  
  
  /*
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
43
   *	Only allow a user to send credentials, that they could set with
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
48
   *	setu(g)id.
   */
  
  static __inline__ int scm_check_creds(struct ucred *creds)
  {
86a264abe   David Howells   CRED: Wrap curren...
49
  	const struct cred *cred = current_cred();
b2e4f544f   Eric W. Biederman   userns: Convert n...
50
51
52
53
54
  	kuid_t uid = make_kuid(cred->user_ns, creds->uid);
  	kgid_t gid = make_kgid(cred->user_ns, creds->gid);
  
  	if (!uid_valid(uid) || !gid_valid(gid))
  		return -EINVAL;
b6dff3ec5   David Howells   CRED: Separate ta...
55

92f28d973   Eric W. Biederman   scm: Require CAP_...
56
  	if ((creds->pid == task_tgid_vnr(current) ||
979ad974d   Andy Lutomirski   net: Check the co...
57
  	     ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
b2e4f544f   Eric W. Biederman   userns: Convert n...
58
  	    ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
00f70de09   Eric W. Biederman   net: Allow userns...
59
  	      uid_eq(uid, cred->suid)) || nsown_capable(CAP_SETUID)) &&
b2e4f544f   Eric W. Biederman   userns: Convert n...
60
  	    ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
00f70de09   Eric W. Biederman   net: Allow userns...
61
  	      gid_eq(gid, cred->sgid)) || nsown_capable(CAP_SETGID))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  	       return 0;
  	}
  	return -EPERM;
  }
  
  static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
  {
  	int *fdp = (int*)CMSG_DATA(cmsg);
  	struct scm_fp_list *fpl = *fplp;
  	struct file **fpp;
  	int i, num;
  
  	num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
  
  	if (num <= 0)
  		return 0;
  
  	if (num > SCM_MAX_FD)
  		return -EINVAL;
  
  	if (!fpl)
  	{
  		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
  		if (!fpl)
  			return -ENOMEM;
  		*fplp = fpl;
  		fpl->count = 0;
bba14de98   Eric Dumazet   scm: lower SCM_MA...
89
  		fpl->max = SCM_MAX_FD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
  	}
  	fpp = &fpl->fp[fpl->count];
bba14de98   Eric Dumazet   scm: lower SCM_MA...
92
  	if (fpl->count + num > fpl->max)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  		return -EINVAL;
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
94

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
  	/*
  	 *	Verify the descriptors and increment the usage count.
  	 */
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
98

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
  	for (i=0; i< num; i++)
  	{
  		int fd = fdp[i];
  		struct file *file;
326be7b48   Al Viro   Allow passing O_P...
103
  		if (fd < 0 || !(file = fget_raw(fd)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  			return -EBADF;
  		*fpp++ = file;
  		fpl->count++;
  	}
  	return num;
  }
  
  void __scm_destroy(struct scm_cookie *scm)
  {
  	struct scm_fp_list *fpl = scm->fp;
  	int i;
  
  	if (fpl) {
  		scm->fp = NULL;
6120d3dbb   Al Viro   get rid of ->scm_...
118
119
120
  		for (i=fpl->count-1; i>=0; i--)
  			fput(fpl->fp[i]);
  		kfree(fpl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
  	}
  }
9e34a5b51   Eric Dumazet   net/core: EXPORT_...
123
  EXPORT_SYMBOL(__scm_destroy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  
  int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  {
  	struct cmsghdr *cmsg;
  	int err;
  
  	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
  	{
  		err = -EINVAL;
  
  		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  		/* The first check was omitted in <= 2.2.5. The reasoning was
  		   that parser checks cmsg_len in any case, so that
  		   additional check would be work duplication.
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
138
  		   But if cmsg_level is not SOL_SOCKET, we do not check
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
149
150
  		   for too short ancillary data object at all! Oops.
  		   OK, let's add it...
  		 */
  		if (!CMSG_OK(msg, cmsg))
  			goto error;
  
  		if (cmsg->cmsg_level != SOL_SOCKET)
  			continue;
  
  		switch (cmsg->cmsg_type)
  		{
  		case SCM_RIGHTS:
76dadd76c   Eric W. Biederman   scm: Only support...
151
152
  			if (!sock->ops || sock->ops->family != PF_UNIX)
  				goto error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
155
156
157
  			err=scm_fp_copy(cmsg, &p->fp);
  			if (err<0)
  				goto error;
  			break;
  		case SCM_CREDENTIALS:
b2e4f544f   Eric W. Biederman   userns: Convert n...
158
  		{
dbe9a4173   Eric W. Biederman   scm: Don't use st...
159
  			struct ucred creds;
b2e4f544f   Eric W. Biederman   userns: Convert n...
160
161
  			kuid_t uid;
  			kgid_t gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  				goto error;
dbe9a4173   Eric W. Biederman   scm: Don't use st...
164
165
  			memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  			err = scm_check_creds(&creds);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
  			if (err)
  				goto error;
257b5358b   Eric W. Biederman   scm: Capture the ...
168

dbe9a4173   Eric W. Biederman   scm: Don't use st...
169
170
  			p->creds.pid = creds.pid;
  			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
257b5358b   Eric W. Biederman   scm: Capture the ...
171
172
  				struct pid *pid;
  				err = -ESRCH;
dbe9a4173   Eric W. Biederman   scm: Don't use st...
173
  				pid = find_get_pid(creds.pid);
257b5358b   Eric W. Biederman   scm: Capture the ...
174
175
176
177
178
  				if (!pid)
  					goto error;
  				put_pid(p->pid);
  				p->pid = pid;
  			}
b2e4f544f   Eric W. Biederman   userns: Convert n...
179
  			err = -EINVAL;
dbe9a4173   Eric W. Biederman   scm: Don't use st...
180
181
  			uid = make_kuid(current_user_ns(), creds.uid);
  			gid = make_kgid(current_user_ns(), creds.gid);
b2e4f544f   Eric W. Biederman   userns: Convert n...
182
183
  			if (!uid_valid(uid) || !gid_valid(gid))
  				goto error;
dbe9a4173   Eric W. Biederman   scm: Don't use st...
184
185
  			p->creds.uid = uid;
  			p->creds.gid = gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  			break;
b2e4f544f   Eric W. Biederman   userns: Convert n...
187
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
192
193
194
195
196
197
198
  		default:
  			goto error;
  		}
  	}
  
  	if (p->fp && !p->fp->count)
  	{
  		kfree(p->fp);
  		p->fp = NULL;
  	}
  	return 0;
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
199

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
  error:
  	scm_destroy(p);
  	return err;
  }
9e34a5b51   Eric Dumazet   net/core: EXPORT_...
204
  EXPORT_SYMBOL(__scm_send);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
  
  int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  {
cfcabdcc2   Stephen Hemminger   [NET]: sparse war...
208
209
  	struct cmsghdr __user *cm
  		= (__force struct cmsghdr __user *)msg->msg_control;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  	struct cmsghdr cmhdr;
  	int cmlen = CMSG_LEN(len);
  	int err;
  
  	if (MSG_CMSG_COMPAT & msg->msg_flags)
  		return put_cmsg_compat(msg, level, type, len, data);
  
  	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
  		msg->msg_flags |= MSG_CTRUNC;
  		return 0; /* XXX: return error? check spec. */
  	}
  	if (msg->msg_controllen < cmlen) {
  		msg->msg_flags |= MSG_CTRUNC;
  		cmlen = msg->msg_controllen;
  	}
  	cmhdr.cmsg_level = level;
  	cmhdr.cmsg_type = type;
  	cmhdr.cmsg_len = cmlen;
  
  	err = -EFAULT;
  	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
4ec93edb1   YOSHIFUJI Hideaki   [NET] CORE: Fix w...
231
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
  	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
  		goto out;
  	cmlen = CMSG_SPACE(len);
1ac70e7ad   Wei Yongjun   [NET]: Fix functi...
235
236
  	if (msg->msg_controllen < cmlen)
  		cmlen = msg->msg_controllen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
242
  	msg->msg_control += cmlen;
  	msg->msg_controllen -= cmlen;
  	err = 0;
  out:
  	return err;
  }
9e34a5b51   Eric Dumazet   net/core: EXPORT_...
243
  EXPORT_SYMBOL(put_cmsg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
  
  void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  {
cfcabdcc2   Stephen Hemminger   [NET]: sparse war...
247
248
  	struct cmsghdr __user *cm
  		= (__force struct cmsghdr __user*)msg->msg_control;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  
  	int fdmax = 0;
  	int fdnum = scm->fp->count;
  	struct file **fp = scm->fp->fp;
  	int __user *cmfptr;
  	int err = 0, i;
  
  	if (MSG_CMSG_COMPAT & msg->msg_flags) {
  		scm_detach_fds_compat(msg, scm);
  		return;
  	}
  
  	if (msg->msg_controllen > sizeof(struct cmsghdr))
  		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
  			 / sizeof(int));
  
  	if (fdnum < fdmax)
  		fdmax = fdnum;
cfcabdcc2   Stephen Hemminger   [NET]: sparse war...
267
268
  	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
  	     i++, cmfptr++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
  	{
48a87cc26   John Fastabend   net: netprio: fd ...
270
  		struct socket *sock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
  		int new_fd;
  		err = security_file_receive(fp[i]);
  		if (err)
  			break;
4a19542e5   Ulrich Drepper   O_CLOEXEC for SCM...
275
276
  		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
  					  ? O_CLOEXEC : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
279
280
281
282
283
284
285
  		if (err < 0)
  			break;
  		new_fd = err;
  		err = put_user(new_fd, cmfptr);
  		if (err) {
  			put_unused_fd(new_fd);
  			break;
  		}
  		/* Bump the usage count and install the file. */
48a87cc26   John Fastabend   net: netprio: fd ...
286
  		sock = sock_from_file(fp[i], &err);
d84295067   Daniel Wagner   net: net_cls: fd ...
287
  		if (sock) {
6ffd46410   Zefan Li   netprio_cgroup: r...
288
  			sock_update_netprioidx(sock->sk);
211d2f97e   Zefan Li   cls_cgroup: remov...
289
  			sock_update_classid(sock->sk);
d84295067   Daniel Wagner   net: net_cls: fd ...
290
  		}
cb0942b81   Al Viro   make get_file() r...
291
  		fd_install(new_fd, get_file(fp[i]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
296
  	}
  
  	if (i > 0)
  	{
  		int cmlen = CMSG_LEN(i*sizeof(int));
effee6a00   Miklos Szeredi   [NET]: File descr...
297
  		err = put_user(SOL_SOCKET, &cm->cmsg_level);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  		if (!err)
  			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  		if (!err)
  			err = put_user(cmlen, &cm->cmsg_len);
  		if (!err) {
  			cmlen = CMSG_SPACE(i*sizeof(int));
  			msg->msg_control += cmlen;
  			msg->msg_controllen -= cmlen;
  		}
  	}
  	if (i < fdnum || (fdnum && fdmax <= 0))
  		msg->msg_flags |= MSG_CTRUNC;
  
  	/*
  	 * All of the files that fit in the message have had their
  	 * usage counts incremented, so we just free the list.
  	 */
  	__scm_destroy(scm);
  }
9e34a5b51   Eric Dumazet   net/core: EXPORT_...
317
  EXPORT_SYMBOL(scm_detach_fds);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
319
320
321
322
323
324
325
  
  struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  {
  	struct scm_fp_list *new_fpl;
  	int i;
  
  	if (!fpl)
  		return NULL;
bba14de98   Eric Dumazet   scm: lower SCM_MA...
326
327
  	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  			  GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  	if (new_fpl) {
bba14de98   Eric Dumazet   scm: lower SCM_MA...
329
  		for (i = 0; i < fpl->count; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  			get_file(fpl->fp[i]);
bba14de98   Eric Dumazet   scm: lower SCM_MA...
331
  		new_fpl->max = new_fpl->count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
333
334
  	}
  	return new_fpl;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
  EXPORT_SYMBOL(scm_fp_dup);