1 /* scm.c - Socket level control messages processing.
2  *
3  * Author:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
4  *              Alignment and value checking mods by Craig Metz
5  *
6  *		This program is free software; you can redistribute it and/or
7  *		modify it under the terms of the GNU General Public License
8  *		as published by the Free Software Foundation; either version
9  *		2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/stat.h>
19 #include <linux/socket.h>
20 #include <linux/file.h>
21 #include <linux/fcntl.h>
22 #include <linux/net.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 
29 #include <net/protocol.h>
30 #include <linux/skbuff.h>
31 #include <net/sock.h>
32 #include <net/scm.h>
33 
34 
35 /*
36  *	Only allow a user to send credentials, that they could set with
37  *	setu(g)id.
38  */
39 
scm_check_creds(struct ucred * creds)40 static __inline__ int scm_check_creds(struct ucred *creds)
41 {
42 	if ((creds->pid == current->pid || capable(CAP_SYS_ADMIN)) &&
43 	    ((creds->uid == current->uid || creds->uid == current->euid ||
44 	      creds->uid == current->suid) || capable(CAP_SETUID)) &&
45 	    ((creds->gid == current->gid || creds->gid == current->egid ||
46 	      creds->gid == current->sgid) || capable(CAP_SETGID))) {
47 	       return 0;
48 	}
49 	return -EPERM;
50 }
51 
scm_fp_copy(struct cmsghdr * cmsg,struct scm_fp_list ** fplp)52 static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
53 {
54 	int *fdp = (int*)CMSG_DATA(cmsg);
55 	struct scm_fp_list *fpl = *fplp;
56 	struct file **fpp;
57 	int i, num;
58 
59 	num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
60 
61 	if (num <= 0)
62 		return 0;
63 
64 	if (num > SCM_MAX_FD)
65 		return -EINVAL;
66 
67 	if (!fpl)
68 	{
69 		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
70 		if (!fpl)
71 			return -ENOMEM;
72 		*fplp = fpl;
73 		INIT_LIST_HEAD(&fpl->list);
74 		fpl->count = 0;
75 	}
76 	fpp = &fpl->fp[fpl->count];
77 
78 	if (fpl->count + num > SCM_MAX_FD)
79 		return -EINVAL;
80 
81 	/*
82 	 *	Verify the descriptors and increment the usage count.
83 	 */
84 
85 	for (i=0; i< num; i++)
86 	{
87 		int fd = fdp[i];
88 		struct file *file;
89 
90 		if (fd < 0 || !(file = fget(fd)))
91 			return -EBADF;
92 		*fpp++ = file;
93 		fpl->count++;
94 	}
95 	return num;
96 }
97 
__scm_destroy(struct scm_cookie * scm)98 void __scm_destroy(struct scm_cookie *scm)
99 {
100 	struct scm_fp_list *fpl = scm->fp;
101 	int i;
102 
103 	if (fpl) {
104 		scm->fp = NULL;
105 		if (current->scm_work_list) {
106 			list_add_tail(&fpl->list, current->scm_work_list);
107 		} else {
108 			LIST_HEAD(work_list);
109 
110 			current->scm_work_list = &work_list;
111 
112 			list_add(&fpl->list, &work_list);
113 			while (!list_empty(&work_list)) {
114 				fpl = list_entry(work_list.next, struct scm_fp_list, list);
115 
116 				list_del(&fpl->list);
117 				for (i=fpl->count-1; i>=0; i--)
118 					fput(fpl->fp[i]);
119 				kfree(fpl);
120 			}
121 
122 			current->scm_work_list = NULL;
123 		}
124 	}
125 }
126 
__scm_send(struct socket * sock,struct msghdr * msg,struct scm_cookie * p)127 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
128 {
129 	struct cmsghdr *cmsg;
130 	int err;
131 
132 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
133 	{
134 		err = -EINVAL;
135 
136 		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
137 		/* The first check was omitted in <= 2.2.5. The reasoning was
138 		   that parser checks cmsg_len in any case, so that
139 		   additional check would be work duplication.
140 		   But if cmsg_level is not SOL_SOCKET, we do not check
141 		   for too short ancillary data object at all! Oops.
142 		   OK, let's add it...
143 		 */
144 		if (!CMSG_OK(msg, cmsg))
145 			goto error;
146 
147 		if (cmsg->cmsg_level != SOL_SOCKET)
148 			continue;
149 
150 		switch (cmsg->cmsg_type)
151 		{
152 		case SCM_RIGHTS:
153 			err=scm_fp_copy(cmsg, &p->fp);
154 			if (err<0)
155 				goto error;
156 			break;
157 		case SCM_CREDENTIALS:
158 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
159 				goto error;
160 			memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
161 			err = scm_check_creds(&p->creds);
162 			if (err)
163 				goto error;
164 			break;
165 		default:
166 			goto error;
167 		}
168 	}
169 
170 	if (p->fp && !p->fp->count)
171 	{
172 		kfree(p->fp);
173 		p->fp = NULL;
174 	}
175 	return 0;
176 
177 error:
178 	scm_destroy(p);
179 	return err;
180 }
181 
put_cmsg(struct msghdr * msg,int level,int type,int len,void * data)182 int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
183 {
184 	struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
185 	struct cmsghdr cmhdr;
186 	int cmlen = CMSG_LEN(len);
187 	int err;
188 
189 	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
190 		msg->msg_flags |= MSG_CTRUNC;
191 		return 0; /* XXX: return error? check spec. */
192 	}
193 	if (msg->msg_controllen < cmlen) {
194 		msg->msg_flags |= MSG_CTRUNC;
195 		cmlen = msg->msg_controllen;
196 	}
197 	cmhdr.cmsg_level = level;
198 	cmhdr.cmsg_type = type;
199 	cmhdr.cmsg_len = cmlen;
200 
201 	err = -EFAULT;
202 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
203 		goto out;
204 	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
205 		goto out;
206 	cmlen = CMSG_SPACE(len);
207 	msg->msg_control += cmlen;
208 	msg->msg_controllen -= cmlen;
209 	err = 0;
210 out:
211 	return err;
212 }
213 
scm_detach_fds(struct msghdr * msg,struct scm_cookie * scm)214 void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
215 {
216 	struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
217 
218 	int fdmax = 0;
219 	int fdnum = scm->fp->count;
220 	struct file **fp = scm->fp->fp;
221 	int *cmfptr;
222 	int err = 0, i;
223 
224 	if (msg->msg_controllen > sizeof(struct cmsghdr))
225 		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
226 			 / sizeof(int));
227 
228 	if (fdnum < fdmax)
229 		fdmax = fdnum;
230 
231 	for (i=0, cmfptr=(int*)CMSG_DATA(cm); i<fdmax; i++, cmfptr++)
232 	{
233 		int new_fd;
234 		err = get_unused_fd();
235 		if (err < 0)
236 			break;
237 		new_fd = err;
238 		err = put_user(new_fd, cmfptr);
239 		if (err) {
240 			put_unused_fd(new_fd);
241 			break;
242 		}
243 		/* Bump the usage count and install the file. */
244 		get_file(fp[i]);
245 		fd_install(new_fd, fp[i]);
246 	}
247 
248 	if (i > 0)
249 	{
250 		int cmlen = CMSG_LEN(i*sizeof(int));
251 		if (!err)
252 			err = put_user(SOL_SOCKET, &cm->cmsg_level);
253 		if (!err)
254 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
255 		if (!err)
256 			err = put_user(cmlen, &cm->cmsg_len);
257 		if (!err) {
258 			cmlen = CMSG_SPACE(i*sizeof(int));
259 			msg->msg_control += cmlen;
260 			msg->msg_controllen -= cmlen;
261 		}
262 	}
263 	if (i < fdnum || (fdnum && fdmax <= 0))
264 		msg->msg_flags |= MSG_CTRUNC;
265 
266 	/*
267 	 * All of the files that fit in the message have had their
268 	 * usage counts incremented, so we just free the list.
269 	 */
270 	__scm_destroy(scm);
271 }
272 
scm_fp_dup(struct scm_fp_list * fpl)273 struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
274 {
275 	struct scm_fp_list *new_fpl;
276 	int i;
277 
278 	if (!fpl)
279 		return NULL;
280 
281 	new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
282 	if (new_fpl) {
283 		INIT_LIST_HEAD(&new_fpl->list);
284 		for (i=fpl->count-1; i>=0; i--)
285 			get_file(fpl->fp[i]);
286 		memcpy(new_fpl, fpl, sizeof(*fpl));
287 	}
288 	return new_fpl;
289 }
290