summaryrefslogtreecommitdiffstats
path: root/net/core/scm.c
blob: e5fa793a705206483edfa136fa2bcf24652e6ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* 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/signal.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/stat.h>
#include <linux/socket.h>
#include <linux/fcntl.h>
#include <linux/net.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>

#include <asm/system.h>
#include <asm/uaccess.h>

#include <linux/inet.h>
#include <linux/netdevice.h>
#include <net/ip.h>
#include <net/protocol.h>
#include <net/rarp.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/scm.h>


/*
 *	Only allow a user to send credentials, that they could set with 
 *	setu(g)id.
 */

static __inline__ int scm_check_creds(struct ucred *creds)
{
	if (suser())
		return 0;
	if (creds->pid != current->pid ||
	    (creds->uid != current->uid && creds->uid != current->euid &&
	     creds->uid != current->suid) ||
	    (creds->gid != current->gid && creds->gid != current->egid &&
	     creds->gid != current->sgid))
		return -EPERM;
	return 0;
}


static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
{
	int num;
	struct scm_fp_list *fpl = *fplp;
	struct file **fpp;
	int *fdp = (int*)CMSG_DATA(cmsg);
	int i;

	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;
	}
	fpp = &fpl->fp[fpl->count];

	if (fpl->count + num > SCM_MAX_FD)
		return -EINVAL;
	
	/*
	 *	Verify the descriptors.
	 */
	 
	for (i=0; i< num; i++)
	{
		int fd;
		
		fd = fdp[i];
		if (fd < 0 || fd >= NR_OPEN)
			return -EBADF;
		if (current->files->fd[fd]==NULL)
			return -EBADF;
		fpp[i] = current->files->fd[fd];
	}
	
        /* add another reference to these files */
	for (i=0; i< num; i++, fpp++)
		(*fpp)->f_count++;
	fpl->count += num;
	
	return num;
}

void __scm_destroy(struct scm_cookie *scm)
{
	int i;
	struct scm_fp_list *fpl = scm->fp;

	if (!fpl)
		return;

	for (i=fpl->count-1; i>=0; i--)
		close_fp(fpl->fp[i]);

	kfree(fpl);
}



extern __inline__ int not_one_bit(unsigned val)
{
	return (val-1) & val;
}


int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
{
	int err;
	struct cmsghdr *cmsg;
	struct file *file;
	int acc_fd;
	unsigned scm_flags=0;

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
	{
		if (cmsg->cmsg_level != SOL_SOCKET)
			continue;

		err = -EINVAL;

		switch (cmsg->cmsg_type)
		{
		case SCM_RIGHTS:
			err=scm_fp_copy(cmsg, &p->fp);
			if (err<0)
				goto error;
			break;
		case SCM_CREDENTIALS:
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
				goto error;
			memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
			err = scm_check_creds(&p->creds);
			if (err)
				goto error;
			break;
		case SCM_CONNECT:
			if (scm_flags)
				goto error;
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
				goto error;
			memcpy(&acc_fd, CMSG_DATA(cmsg), sizeof(int));
			p->sock = NULL;
			if (acc_fd != -1) {
				if (acc_fd < 0 || acc_fd >= NR_OPEN ||
				    (file=current->files->fd[acc_fd])==NULL)
					return -EBADF;
				if (!file->f_dentry->d_inode || !file->f_dentry->d_inode->i_sock)
					return -ENOTSOCK;
				p->sock = &file->f_dentry->d_inode->u.socket_i;
				if (p->sock->state != SS_UNCONNECTED) 
					return -EINVAL;
			}
			scm_flags |= MSG_SYN;
			break;
		default:
			goto error;
		}
	}

	if (p->fp && !p->fp->count)
	{
		kfree(p->fp);
		p->fp = NULL;
	}
	
	err = -EINVAL;
	msg->msg_flags |= scm_flags;
	scm_flags = msg->msg_flags&MSG_CTLFLAGS;
	if (not_one_bit(scm_flags))
		goto error;

	if (!(scm_flags && p->fp))
		return 0;

error:
	scm_destroy(p);
	return err;
}

void put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
	struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;
	int cmlen = CMSG_LEN(len);
	int err;

	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
		msg->msg_flags |= MSG_CTRUNC;
		return;
	}
	if (msg->msg_controllen < cmlen) {
		msg->msg_flags |= MSG_CTRUNC;
		cmlen = msg->msg_controllen;
	}
	err = put_user(level, &cm->cmsg_level);
	if (!err)
		err = put_user(type, &cm->cmsg_type);
	if (!err)
		err = put_user(cmlen, &cm->cmsg_len);
	if (!err)
		err = copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr));
	if (!err) {
		cmlen = CMSG_SPACE(len);
		msg->msg_control += cmlen;
		msg->msg_controllen -= cmlen;
	}
}

void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
{
	struct cmsghdr *cm = (struct cmsghdr*)msg->msg_control;

	int fdmax = (msg->msg_controllen - sizeof(struct cmsghdr))/sizeof(int);
	int fdnum = scm->fp->count;
	int *cmfptr;
	int err = 0;
	int i;
	struct file **fp = scm->fp->fp;

	if (fdnum < fdmax)
		fdmax = fdnum;

	for (i=0, cmfptr=(int*)CMSG_DATA(cm); i<fdmax; i++, cmfptr++)
	{
		int new_fd = get_unused_fd();
		if (new_fd < 0)
			break;
		current->files->fd[new_fd] = fp[i];
		err = put_user(new_fd, cmfptr);
	}

	if (i > 0)
	{
		int cmlen = CMSG_LEN(i*sizeof(int));
		if (!err)
			err = put_user(SOL_SOCKET, &cm->cmsg_level);
		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 (err)
		i = 0;

	/*
	 *	Dump those that don't fit.
	 */
	for ( ; i < fdnum; i++)	{
		msg->msg_flags |= MSG_CTRUNC;
		close_fp(fp[i]);
	}

	kfree (scm->fp);
	scm->fp = NULL;
}

struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
{
	int i;
	struct scm_fp_list *new_fpl;

	if (!fpl)
		return NULL;

	new_fpl = kmalloc(fpl->count*sizeof(int) + sizeof(*fpl), GFP_KERNEL);
	if (!new_fpl)
		return NULL;

	memcpy(new_fpl, fpl, fpl->count*sizeof(int) + sizeof(*fpl));

	for (i=fpl->count-1; i>=0; i--)
		fpl->fp[i]->f_count++;

	return new_fpl;
}