sysv_msg.c revision 163606
1/*-
2 * Implementation of SVID messages
3 *
4 * Author:  Daniel Boulet
5 *
6 * Copyright 1993 Daniel Boulet and RTMX Inc.
7 *
8 * This system call was implemented by Daniel Boulet under contract from RTMX.
9 *
10 * Redistribution and use in source forms, with and without modification,
11 * are permitted provided that this entire comment appears intact.
12 *
13 * Redistribution in binary form may occur without any restrictions.
14 * Obviously, it would be nice if you gave credit where credit is due
15 * but requiring it would be too onerous.
16 *
17 * This software is provided ``AS IS'' without any warranties of any kind.
18 */
19/*-
20 * Copyright (c) 2003-2005 McAfee, Inc.
21 * All rights reserved.
22 *
23 * This software was developed for the FreeBSD Project in part by McAfee
24 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
25 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
26 * program.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 *    notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 *    notice, this list of conditions and the following disclaimer in the
35 *    documentation and/or other materials provided with the distribution.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 */
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: head/sys/kern/sysv_msg.c 163606 2006-10-22 11:52:19Z rwatson $");
52
53#include "opt_sysvipc.h"
54#include "opt_mac.h"
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/sysproto.h>
59#include <sys/kernel.h>
60#include <sys/proc.h>
61#include <sys/lock.h>
62#include <sys/mutex.h>
63#include <sys/module.h>
64#include <sys/msg.h>
65#include <sys/syscall.h>
66#include <sys/syscallsubr.h>
67#include <sys/sysent.h>
68#include <sys/sysctl.h>
69#include <sys/malloc.h>
70#include <sys/jail.h>
71
72#include <security/mac/mac_framework.h>
73
74static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
75
76static void msginit(void);
77static int msgunload(void);
78static int sysvmsg_modload(struct module *, int, void *);
79
80#ifdef MSG_DEBUG
81#define DPRINTF(a)	printf a
82#else
83#define DPRINTF(a)
84#endif
85
86static void msg_freehdr(struct msg *msghdr);
87
88/* XXX casting to (sy_call_t *) is bogus, as usual. */
89static sy_call_t *msgcalls[] = {
90	(sy_call_t *)msgctl, (sy_call_t *)msgget,
91	(sy_call_t *)msgsnd, (sy_call_t *)msgrcv
92};
93
94#ifndef MSGSSZ
95#define MSGSSZ	8		/* Each segment must be 2^N long */
96#endif
97#ifndef MSGSEG
98#define MSGSEG	2048		/* must be less than 32767 */
99#endif
100#define MSGMAX	(MSGSSZ*MSGSEG)
101#ifndef MSGMNB
102#define MSGMNB	2048		/* max # of bytes in a queue */
103#endif
104#ifndef MSGMNI
105#define MSGMNI	40
106#endif
107#ifndef MSGTQL
108#define MSGTQL	40
109#endif
110
111/*
112 * Based on the configuration parameters described in an SVR2 (yes, two)
113 * config(1m) man page.
114 *
115 * Each message is broken up and stored in segments that are msgssz bytes
116 * long.  For efficiency reasons, this should be a power of two.  Also,
117 * it doesn't make sense if it is less than 8 or greater than about 256.
118 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
119 * two between 8 and 1024 inclusive (and panic's if it isn't).
120 */
121struct msginfo msginfo = {
122                MSGMAX,         /* max chars in a message */
123                MSGMNI,         /* # of message queue identifiers */
124                MSGMNB,         /* max chars in a queue */
125                MSGTQL,         /* max messages in system */
126                MSGSSZ,         /* size of a message segment */
127                		/* (must be small power of 2 greater than 4) */
128                MSGSEG          /* number of message segments */
129};
130
131/*
132 * macros to convert between msqid_ds's and msqid's.
133 * (specific to this implementation)
134 */
135#define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
136#define MSQID_IX(id)	((id) & 0xffff)
137#define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
138
139/*
140 * The rest of this file is specific to this particular implementation.
141 */
142
143struct msgmap {
144	short	next;		/* next segment in buffer */
145    				/* -1 -> available */
146    				/* 0..(MSGSEG-1) -> index of next segment */
147};
148
149#define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
150
151static int nfree_msgmaps;	/* # of free map entries */
152static short free_msgmaps;	/* head of linked list of free map entries */
153static struct msg *free_msghdrs;/* list of free msg headers */
154static char *msgpool;		/* MSGMAX byte long msg buffer pool */
155static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
156static struct msg *msghdrs;	/* MSGTQL msg headers */
157static struct msqid_kernel *msqids;	/* MSGMNI msqid_kernel struct's */
158static struct mtx msq_mtx;	/* global mutex for message queues. */
159
160static void
161msginit()
162{
163	register int i;
164
165	TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
166	TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
167	msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
168	TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
169	TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
170	TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
171
172	msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
173	if (msgpool == NULL)
174		panic("msgpool is NULL");
175	msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
176	if (msgmaps == NULL)
177		panic("msgmaps is NULL");
178	msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
179	if (msghdrs == NULL)
180		panic("msghdrs is NULL");
181	msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
182	    M_WAITOK);
183	if (msqids == NULL)
184		panic("msqids is NULL");
185
186	/*
187	 * msginfo.msgssz should be a power of two for efficiency reasons.
188	 * It is also pretty silly if msginfo.msgssz is less than 8
189	 * or greater than about 256 so ...
190	 */
191
192	i = 8;
193	while (i < 1024 && i != msginfo.msgssz)
194		i <<= 1;
195    	if (i != msginfo.msgssz) {
196		DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
197		    msginfo.msgssz));
198		panic("msginfo.msgssz not a small power of 2");
199	}
200
201	if (msginfo.msgseg > 32767) {
202		DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
203		panic("msginfo.msgseg > 32767");
204	}
205
206	if (msgmaps == NULL)
207		panic("msgmaps is NULL");
208
209	for (i = 0; i < msginfo.msgseg; i++) {
210		if (i > 0)
211			msgmaps[i-1].next = i;
212		msgmaps[i].next = -1;	/* implies entry is available */
213	}
214	free_msgmaps = 0;
215	nfree_msgmaps = msginfo.msgseg;
216
217	if (msghdrs == NULL)
218		panic("msghdrs is NULL");
219
220	for (i = 0; i < msginfo.msgtql; i++) {
221		msghdrs[i].msg_type = 0;
222		if (i > 0)
223			msghdrs[i-1].msg_next = &msghdrs[i];
224		msghdrs[i].msg_next = NULL;
225#ifdef MAC
226		mac_init_sysv_msgmsg(&msghdrs[i]);
227#endif
228    	}
229	free_msghdrs = &msghdrs[0];
230
231	if (msqids == NULL)
232		panic("msqids is NULL");
233
234	for (i = 0; i < msginfo.msgmni; i++) {
235		msqids[i].u.msg_qbytes = 0;	/* implies entry is available */
236		msqids[i].u.msg_perm.seq = 0;	/* reset to a known value */
237		msqids[i].u.msg_perm.mode = 0;
238#ifdef MAC
239		mac_init_sysv_msgqueue(&msqids[i]);
240#endif
241	}
242	mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
243}
244
245static int
246msgunload()
247{
248	struct msqid_kernel *msqkptr;
249	int msqid;
250#ifdef MAC
251	int i;
252#endif
253
254	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
255		/*
256		 * Look for an unallocated and unlocked msqid_ds.
257		 * msqid_ds's can be locked by msgsnd or msgrcv while
258		 * they are copying the message in/out.  We can't
259		 * re-use the entry until they release it.
260		 */
261		msqkptr = &msqids[msqid];
262		if (msqkptr->u.msg_qbytes != 0 ||
263		    (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
264			break;
265	}
266	if (msqid != msginfo.msgmni)
267		return (EBUSY);
268
269#ifdef MAC
270	for (i = 0; i < msginfo.msgtql; i++)
271		mac_destroy_sysv_msgmsg(&msghdrs[i]);
272	for (msqid = 0; msqid < msginfo.msgmni; msqid++)
273		mac_destroy_sysv_msgqueue(&msqids[msqid]);
274#endif
275	free(msgpool, M_MSG);
276	free(msgmaps, M_MSG);
277	free(msghdrs, M_MSG);
278	free(msqids, M_MSG);
279	mtx_destroy(&msq_mtx);
280	return (0);
281}
282
283
284static int
285sysvmsg_modload(struct module *module, int cmd, void *arg)
286{
287	int error = 0;
288
289	switch (cmd) {
290	case MOD_LOAD:
291		msginit();
292		break;
293	case MOD_UNLOAD:
294		error = msgunload();
295		break;
296	case MOD_SHUTDOWN:
297		break;
298	default:
299		error = EINVAL;
300		break;
301	}
302	return (error);
303}
304
305static moduledata_t sysvmsg_mod = {
306	"sysvmsg",
307	&sysvmsg_modload,
308	NULL
309};
310
311SYSCALL_MODULE_HELPER(msgsys);
312SYSCALL_MODULE_HELPER(msgctl);
313SYSCALL_MODULE_HELPER(msgget);
314SYSCALL_MODULE_HELPER(msgsnd);
315SYSCALL_MODULE_HELPER(msgrcv);
316
317DECLARE_MODULE(sysvmsg, sysvmsg_mod,
318	SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
319MODULE_VERSION(sysvmsg, 1);
320
321/*
322 * Entry point for all MSG calls
323 *
324 * MPSAFE
325 */
326int
327msgsys(td, uap)
328	struct thread *td;
329	/* XXX actually varargs. */
330	struct msgsys_args /* {
331		int	which;
332		int	a2;
333		int	a3;
334		int	a4;
335		int	a5;
336		int	a6;
337	} */ *uap;
338{
339	int error;
340
341	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
342		return (ENOSYS);
343	if (uap->which < 0 ||
344	    uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
345		return (EINVAL);
346	error = (*msgcalls[uap->which])(td, &uap->a2);
347	return (error);
348}
349
350static void
351msg_freehdr(msghdr)
352	struct msg *msghdr;
353{
354	while (msghdr->msg_ts > 0) {
355		short next;
356		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
357			panic("msghdr->msg_spot out of range");
358		next = msgmaps[msghdr->msg_spot].next;
359		msgmaps[msghdr->msg_spot].next = free_msgmaps;
360		free_msgmaps = msghdr->msg_spot;
361		nfree_msgmaps++;
362		msghdr->msg_spot = next;
363		if (msghdr->msg_ts >= msginfo.msgssz)
364			msghdr->msg_ts -= msginfo.msgssz;
365		else
366			msghdr->msg_ts = 0;
367	}
368	if (msghdr->msg_spot != -1)
369		panic("msghdr->msg_spot != -1");
370	msghdr->msg_next = free_msghdrs;
371	free_msghdrs = msghdr;
372#ifdef MAC
373	mac_cleanup_sysv_msgmsg(msghdr);
374#endif
375}
376
377#ifndef _SYS_SYSPROTO_H_
378struct msgctl_args {
379	int	msqid;
380	int	cmd;
381	struct	msqid_ds *buf;
382};
383#endif
384
385/*
386 * MPSAFE
387 */
388int
389msgctl(td, uap)
390	struct thread *td;
391	register struct msgctl_args *uap;
392{
393	int msqid = uap->msqid;
394	int cmd = uap->cmd;
395	struct msqid_ds msqbuf;
396	int error;
397
398	DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, uap->buf));
399	if (cmd == IPC_SET &&
400	    (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
401		return (error);
402	error = kern_msgctl(td, msqid, cmd, &msqbuf);
403	if (cmd == IPC_STAT && error == 0)
404		error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
405	return (error);
406}
407
408int
409kern_msgctl(td, msqid, cmd, msqbuf)
410	struct thread *td;
411	int msqid;
412	int cmd;
413	struct msqid_ds *msqbuf;
414{
415	int rval, error, msqix;
416	register struct msqid_kernel *msqkptr;
417
418	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
419		return (ENOSYS);
420
421	msqix = IPCID_TO_IX(msqid);
422
423	if (msqix < 0 || msqix >= msginfo.msgmni) {
424		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
425		    msginfo.msgmni));
426		return (EINVAL);
427	}
428
429	msqkptr = &msqids[msqix];
430
431	mtx_lock(&msq_mtx);
432	if (msqkptr->u.msg_qbytes == 0) {
433		DPRINTF(("no such msqid\n"));
434		error = EINVAL;
435		goto done2;
436	}
437	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
438		DPRINTF(("wrong sequence number\n"));
439		error = EINVAL;
440		goto done2;
441	}
442#ifdef MAC
443	error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd);
444	if (error != 0)
445		goto done2;
446#endif
447
448	error = 0;
449	rval = 0;
450
451	switch (cmd) {
452
453	case IPC_RMID:
454	{
455		struct msg *msghdr;
456		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
457			goto done2;
458
459#ifdef MAC
460		/*
461		 * Check that the thread has MAC access permissions to
462		 * individual msghdrs.  Note: We need to do this in a
463		 * separate loop because the actual loop alters the
464		 * msq/msghdr info as it progresses, and there is no going
465		 * back if half the way through we discover that the
466		 * thread cannot free a certain msghdr.  The msq will get
467		 * into an inconsistent state.
468		 */
469		for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
470		    msghdr = msghdr->msg_next) {
471			error = mac_check_sysv_msgrmid(td->td_ucred, msghdr);
472			if (error != 0)
473				goto done2;
474		}
475#endif
476
477		/* Free the message headers */
478		msghdr = msqkptr->u.msg_first;
479		while (msghdr != NULL) {
480			struct msg *msghdr_tmp;
481
482			/* Free the segments of each message */
483			msqkptr->u.msg_cbytes -= msghdr->msg_ts;
484			msqkptr->u.msg_qnum--;
485			msghdr_tmp = msghdr;
486			msghdr = msghdr->msg_next;
487			msg_freehdr(msghdr_tmp);
488		}
489
490		if (msqkptr->u.msg_cbytes != 0)
491			panic("msg_cbytes is screwed up");
492		if (msqkptr->u.msg_qnum != 0)
493			panic("msg_qnum is screwed up");
494
495		msqkptr->u.msg_qbytes = 0;	/* Mark it as free */
496
497#ifdef MAC
498		mac_cleanup_sysv_msgqueue(msqkptr);
499#endif
500
501		wakeup(msqkptr);
502	}
503
504		break;
505
506	case IPC_SET:
507		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
508			goto done2;
509		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
510			error = suser(td);
511			if (error)
512				goto done2;
513		}
514		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
515			DPRINTF(("can't increase msg_qbytes beyond %d"
516			    "(truncating)\n", msginfo.msgmnb));
517			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
518		}
519		if (msqbuf->msg_qbytes == 0) {
520			DPRINTF(("can't reduce msg_qbytes to 0\n"));
521			error = EINVAL;		/* non-standard errno! */
522			goto done2;
523		}
524		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
525		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
526		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
527		    (msqbuf->msg_perm.mode & 0777);
528		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
529		msqkptr->u.msg_ctime = time_second;
530		break;
531
532	case IPC_STAT:
533		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
534			DPRINTF(("requester doesn't have read access\n"));
535			goto done2;
536		}
537		*msqbuf = msqkptr->u;
538		break;
539
540	default:
541		DPRINTF(("invalid command %d\n", cmd));
542		error = EINVAL;
543		goto done2;
544	}
545
546	if (error == 0)
547		td->td_retval[0] = rval;
548done2:
549	mtx_unlock(&msq_mtx);
550	return (error);
551}
552
553#ifndef _SYS_SYSPROTO_H_
554struct msgget_args {
555	key_t	key;
556	int	msgflg;
557};
558#endif
559
560/*
561 * MPSAFE
562 */
563int
564msgget(td, uap)
565	struct thread *td;
566	register struct msgget_args *uap;
567{
568	int msqid, error = 0;
569	int key = uap->key;
570	int msgflg = uap->msgflg;
571	struct ucred *cred = td->td_ucred;
572	register struct msqid_kernel *msqkptr = NULL;
573
574	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
575
576	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
577		return (ENOSYS);
578
579	mtx_lock(&msq_mtx);
580	if (key != IPC_PRIVATE) {
581		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
582			msqkptr = &msqids[msqid];
583			if (msqkptr->u.msg_qbytes != 0 &&
584			    msqkptr->u.msg_perm.key == key)
585				break;
586		}
587		if (msqid < msginfo.msgmni) {
588			DPRINTF(("found public key\n"));
589			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
590				DPRINTF(("not exclusive\n"));
591				error = EEXIST;
592				goto done2;
593			}
594			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
595			    msgflg & 0700))) {
596				DPRINTF(("requester doesn't have 0%o access\n",
597				    msgflg & 0700));
598				goto done2;
599			}
600#ifdef MAC
601			error = mac_check_sysv_msqget(cred, msqkptr);
602			if (error != 0)
603				goto done2;
604#endif
605			goto found;
606		}
607	}
608
609	DPRINTF(("need to allocate the msqid_ds\n"));
610	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
611		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
612			/*
613			 * Look for an unallocated and unlocked msqid_ds.
614			 * msqid_ds's can be locked by msgsnd or msgrcv while
615			 * they are copying the message in/out.  We can't
616			 * re-use the entry until they release it.
617			 */
618			msqkptr = &msqids[msqid];
619			if (msqkptr->u.msg_qbytes == 0 &&
620			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
621				break;
622		}
623		if (msqid == msginfo.msgmni) {
624			DPRINTF(("no more msqid_ds's available\n"));
625			error = ENOSPC;
626			goto done2;
627		}
628		DPRINTF(("msqid %d is available\n", msqid));
629		msqkptr->u.msg_perm.key = key;
630		msqkptr->u.msg_perm.cuid = cred->cr_uid;
631		msqkptr->u.msg_perm.uid = cred->cr_uid;
632		msqkptr->u.msg_perm.cgid = cred->cr_gid;
633		msqkptr->u.msg_perm.gid = cred->cr_gid;
634		msqkptr->u.msg_perm.mode = (msgflg & 0777);
635		/* Make sure that the returned msqid is unique */
636		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
637		msqkptr->u.msg_first = NULL;
638		msqkptr->u.msg_last = NULL;
639		msqkptr->u.msg_cbytes = 0;
640		msqkptr->u.msg_qnum = 0;
641		msqkptr->u.msg_qbytes = msginfo.msgmnb;
642		msqkptr->u.msg_lspid = 0;
643		msqkptr->u.msg_lrpid = 0;
644		msqkptr->u.msg_stime = 0;
645		msqkptr->u.msg_rtime = 0;
646		msqkptr->u.msg_ctime = time_second;
647#ifdef MAC
648		mac_create_sysv_msgqueue(cred, msqkptr);
649#endif
650	} else {
651		DPRINTF(("didn't find it and wasn't asked to create it\n"));
652		error = ENOENT;
653		goto done2;
654	}
655
656found:
657	/* Construct the unique msqid */
658	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
659done2:
660	mtx_unlock(&msq_mtx);
661	return (error);
662}
663
664#ifndef _SYS_SYSPROTO_H_
665struct msgsnd_args {
666	int	msqid;
667	const void	*msgp;
668	size_t	msgsz;
669	int	msgflg;
670};
671#endif
672
673/*
674 * MPSAFE
675 */
676int
677msgsnd(td, uap)
678	struct thread *td;
679	register struct msgsnd_args *uap;
680{
681	int msqid = uap->msqid;
682	const void *user_msgp = uap->msgp;
683	size_t msgsz = uap->msgsz;
684	int msgflg = uap->msgflg;
685	int segs_needed, error = 0;
686	register struct msqid_kernel *msqkptr;
687	register struct msg *msghdr;
688	short next;
689
690	DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
691	    msgflg));
692	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
693		return (ENOSYS);
694
695	mtx_lock(&msq_mtx);
696	msqid = IPCID_TO_IX(msqid);
697
698	if (msqid < 0 || msqid >= msginfo.msgmni) {
699		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
700		    msginfo.msgmni));
701		error = EINVAL;
702		goto done2;
703	}
704
705	msqkptr = &msqids[msqid];
706	if (msqkptr->u.msg_qbytes == 0) {
707		DPRINTF(("no such message queue id\n"));
708		error = EINVAL;
709		goto done2;
710	}
711	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
712		DPRINTF(("wrong sequence number\n"));
713		error = EINVAL;
714		goto done2;
715	}
716
717	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
718		DPRINTF(("requester doesn't have write access\n"));
719		goto done2;
720	}
721
722#ifdef MAC
723	error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
724	if (error != 0)
725		goto done2;
726#endif
727
728	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
729	DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
730	    segs_needed));
731	for (;;) {
732		int need_more_resources = 0;
733
734		/*
735		 * check msgsz
736		 * (inside this loop in case msg_qbytes changes while we sleep)
737		 */
738
739		if (msgsz > msqkptr->u.msg_qbytes) {
740			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
741			error = EINVAL;
742			goto done2;
743		}
744
745		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
746			DPRINTF(("msqid is locked\n"));
747			need_more_resources = 1;
748		}
749		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
750			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
751			need_more_resources = 1;
752		}
753		if (segs_needed > nfree_msgmaps) {
754			DPRINTF(("segs_needed > nfree_msgmaps\n"));
755			need_more_resources = 1;
756		}
757		if (free_msghdrs == NULL) {
758			DPRINTF(("no more msghdrs\n"));
759			need_more_resources = 1;
760		}
761
762		if (need_more_resources) {
763			int we_own_it;
764
765			if ((msgflg & IPC_NOWAIT) != 0) {
766				DPRINTF(("need more resources but caller "
767				    "doesn't want to wait\n"));
768				error = EAGAIN;
769				goto done2;
770			}
771
772			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
773				DPRINTF(("we don't own the msqid_ds\n"));
774				we_own_it = 0;
775			} else {
776				/* Force later arrivals to wait for our
777				   request */
778				DPRINTF(("we own the msqid_ds\n"));
779				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
780				we_own_it = 1;
781			}
782			DPRINTF(("goodnight\n"));
783			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
784			    "msgwait", 0);
785			DPRINTF(("good morning, error=%d\n", error));
786			if (we_own_it)
787				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
788			if (error != 0) {
789				DPRINTF(("msgsnd:  interrupted system call\n"));
790				error = EINTR;
791				goto done2;
792			}
793
794			/*
795			 * Make sure that the msq queue still exists
796			 */
797
798			if (msqkptr->u.msg_qbytes == 0) {
799				DPRINTF(("msqid deleted\n"));
800				error = EIDRM;
801				goto done2;
802			}
803
804		} else {
805			DPRINTF(("got all the resources that we need\n"));
806			break;
807		}
808	}
809
810	/*
811	 * We have the resources that we need.
812	 * Make sure!
813	 */
814
815	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
816		panic("msg_perm.mode & MSG_LOCKED");
817	if (segs_needed > nfree_msgmaps)
818		panic("segs_needed > nfree_msgmaps");
819	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
820		panic("msgsz + msg_cbytes > msg_qbytes");
821	if (free_msghdrs == NULL)
822		panic("no more msghdrs");
823
824	/*
825	 * Re-lock the msqid_ds in case we page-fault when copying in the
826	 * message
827	 */
828
829	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
830		panic("msqid_ds is already locked");
831	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
832
833	/*
834	 * Allocate a message header
835	 */
836
837	msghdr = free_msghdrs;
838	free_msghdrs = msghdr->msg_next;
839	msghdr->msg_spot = -1;
840	msghdr->msg_ts = msgsz;
841#ifdef MAC
842	/*
843	 * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
844	 * immediately?  Or, should it be checked just before the msg is
845	 * enqueued in the msgq (as it is done now)?
846	 */
847	mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
848#endif
849
850	/*
851	 * Allocate space for the message
852	 */
853
854	while (segs_needed > 0) {
855		if (nfree_msgmaps <= 0)
856			panic("not enough msgmaps");
857		if (free_msgmaps == -1)
858			panic("nil free_msgmaps");
859		next = free_msgmaps;
860		if (next <= -1)
861			panic("next too low #1");
862		if (next >= msginfo.msgseg)
863			panic("next out of range #1");
864		DPRINTF(("allocating segment %d to message\n", next));
865		free_msgmaps = msgmaps[next].next;
866		nfree_msgmaps--;
867		msgmaps[next].next = msghdr->msg_spot;
868		msghdr->msg_spot = next;
869		segs_needed--;
870	}
871
872	/*
873	 * Copy in the message type
874	 */
875
876	mtx_unlock(&msq_mtx);
877	if ((error = copyin(user_msgp, &msghdr->msg_type,
878	    sizeof(msghdr->msg_type))) != 0) {
879		mtx_lock(&msq_mtx);
880		DPRINTF(("error %d copying the message type\n", error));
881		msg_freehdr(msghdr);
882		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
883		wakeup(msqkptr);
884		goto done2;
885	}
886	mtx_lock(&msq_mtx);
887	user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
888
889	/*
890	 * Validate the message type
891	 */
892
893	if (msghdr->msg_type < 1) {
894		msg_freehdr(msghdr);
895		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
896		wakeup(msqkptr);
897		DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
898		error = EINVAL;
899		goto done2;
900	}
901
902	/*
903	 * Copy in the message body
904	 */
905
906	next = msghdr->msg_spot;
907	while (msgsz > 0) {
908		size_t tlen;
909		if (msgsz > msginfo.msgssz)
910			tlen = msginfo.msgssz;
911		else
912			tlen = msgsz;
913		if (next <= -1)
914			panic("next too low #2");
915		if (next >= msginfo.msgseg)
916			panic("next out of range #2");
917		mtx_unlock(&msq_mtx);
918		if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
919		    tlen)) != 0) {
920			mtx_lock(&msq_mtx);
921			DPRINTF(("error %d copying in message segment\n",
922			    error));
923			msg_freehdr(msghdr);
924			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
925			wakeup(msqkptr);
926			goto done2;
927		}
928		mtx_lock(&msq_mtx);
929		msgsz -= tlen;
930		user_msgp = (const char *)user_msgp + tlen;
931		next = msgmaps[next].next;
932	}
933	if (next != -1)
934		panic("didn't use all the msg segments");
935
936	/*
937	 * We've got the message.  Unlock the msqid_ds.
938	 */
939
940	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
941
942	/*
943	 * Make sure that the msqid_ds is still allocated.
944	 */
945
946	if (msqkptr->u.msg_qbytes == 0) {
947		msg_freehdr(msghdr);
948		wakeup(msqkptr);
949		error = EIDRM;
950		goto done2;
951	}
952
953#ifdef MAC
954	/*
955	 * Note: Since the task/thread allocates the msghdr and usually
956	 * primes it with its own MAC label, for a majority of policies, it
957	 * won't be necessary to check whether the msghdr has access
958	 * permissions to the msgq.  The mac_check_sysv_msqsnd check would
959	 * suffice in that case.  However, this hook may be required where
960	 * individual policies derive a non-identical label for the msghdr
961	 * from the current thread label and may want to check the msghdr
962	 * enqueue permissions, along with read/write permissions to the
963	 * msgq.
964	 */
965	error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
966	if (error != 0) {
967		msg_freehdr(msghdr);
968		wakeup(msqkptr);
969		goto done2;
970	}
971#endif
972
973	/*
974	 * Put the message into the queue
975	 */
976	if (msqkptr->u.msg_first == NULL) {
977		msqkptr->u.msg_first = msghdr;
978		msqkptr->u.msg_last = msghdr;
979	} else {
980		msqkptr->u.msg_last->msg_next = msghdr;
981		msqkptr->u.msg_last = msghdr;
982	}
983	msqkptr->u.msg_last->msg_next = NULL;
984
985	msqkptr->u.msg_cbytes += msghdr->msg_ts;
986	msqkptr->u.msg_qnum++;
987	msqkptr->u.msg_lspid = td->td_proc->p_pid;
988	msqkptr->u.msg_stime = time_second;
989
990	wakeup(msqkptr);
991	td->td_retval[0] = 0;
992done2:
993	mtx_unlock(&msq_mtx);
994	return (error);
995}
996
997#ifndef _SYS_SYSPROTO_H_
998struct msgrcv_args {
999	int	msqid;
1000	void	*msgp;
1001	size_t	msgsz;
1002	long	msgtyp;
1003	int	msgflg;
1004};
1005#endif
1006
1007/*
1008 * MPSAFE
1009 */
1010int
1011msgrcv(td, uap)
1012	struct thread *td;
1013	register struct msgrcv_args *uap;
1014{
1015	int msqid = uap->msqid;
1016	void *user_msgp = uap->msgp;
1017	size_t msgsz = uap->msgsz;
1018	long msgtyp = uap->msgtyp;
1019	int msgflg = uap->msgflg;
1020	size_t len;
1021	register struct msqid_kernel *msqkptr;
1022	register struct msg *msghdr;
1023	int error = 0;
1024	short next;
1025
1026	DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
1027	    msgsz, msgtyp, msgflg));
1028
1029	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1030		return (ENOSYS);
1031
1032	msqid = IPCID_TO_IX(msqid);
1033
1034	if (msqid < 0 || msqid >= msginfo.msgmni) {
1035		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
1036		    msginfo.msgmni));
1037		return (EINVAL);
1038	}
1039
1040	msqkptr = &msqids[msqid];
1041	mtx_lock(&msq_mtx);
1042	if (msqkptr->u.msg_qbytes == 0) {
1043		DPRINTF(("no such message queue id\n"));
1044		error = EINVAL;
1045		goto done2;
1046	}
1047	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1048		DPRINTF(("wrong sequence number\n"));
1049		error = EINVAL;
1050		goto done2;
1051	}
1052
1053	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1054		DPRINTF(("requester doesn't have read access\n"));
1055		goto done2;
1056	}
1057
1058#ifdef MAC
1059	error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
1060	if (error != 0)
1061		goto done2;
1062#endif
1063
1064	msghdr = NULL;
1065	while (msghdr == NULL) {
1066		if (msgtyp == 0) {
1067			msghdr = msqkptr->u.msg_first;
1068			if (msghdr != NULL) {
1069				if (msgsz < msghdr->msg_ts &&
1070				    (msgflg & MSG_NOERROR) == 0) {
1071					DPRINTF(("first message on the queue "
1072					    "is too big (want %d, got %d)\n",
1073					    msgsz, msghdr->msg_ts));
1074					error = E2BIG;
1075					goto done2;
1076				}
1077#ifdef MAC
1078				error = mac_check_sysv_msgrcv(td->td_ucred,
1079				    msghdr);
1080				if (error != 0)
1081					goto done2;
1082#endif
1083				if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1084					msqkptr->u.msg_first = NULL;
1085					msqkptr->u.msg_last = NULL;
1086				} else {
1087					msqkptr->u.msg_first = msghdr->msg_next;
1088					if (msqkptr->u.msg_first == NULL)
1089						panic("msg_first/last screwed up #1");
1090				}
1091			}
1092		} else {
1093			struct msg *previous;
1094			struct msg **prev;
1095
1096			previous = NULL;
1097			prev = &(msqkptr->u.msg_first);
1098			while ((msghdr = *prev) != NULL) {
1099				/*
1100				 * Is this message's type an exact match or is
1101				 * this message's type less than or equal to
1102				 * the absolute value of a negative msgtyp?
1103				 * Note that the second half of this test can
1104				 * NEVER be true if msgtyp is positive since
1105				 * msg_type is always positive!
1106				 */
1107
1108				if (msgtyp == msghdr->msg_type ||
1109				    msghdr->msg_type <= -msgtyp) {
1110					DPRINTF(("found message type %d, "
1111					    "requested %d\n",
1112					    msghdr->msg_type, msgtyp));
1113					if (msgsz < msghdr->msg_ts &&
1114					    (msgflg & MSG_NOERROR) == 0) {
1115						DPRINTF(("requested message "
1116						    "on the queue is too big "
1117						    "(want %d, got %d)\n",
1118						    msgsz, msghdr->msg_ts));
1119						error = E2BIG;
1120						goto done2;
1121					}
1122#ifdef MAC
1123					error = mac_check_sysv_msgrcv(
1124					    td->td_ucred, msghdr);
1125					if (error != 0)
1126						goto done2;
1127#endif
1128					*prev = msghdr->msg_next;
1129					if (msghdr == msqkptr->u.msg_last) {
1130						if (previous == NULL) {
1131							if (prev !=
1132							    &msqkptr->u.msg_first)
1133								panic("msg_first/last screwed up #2");
1134							msqkptr->u.msg_first =
1135							    NULL;
1136							msqkptr->u.msg_last =
1137							    NULL;
1138						} else {
1139							if (prev ==
1140							    &msqkptr->u.msg_first)
1141								panic("msg_first/last screwed up #3");
1142							msqkptr->u.msg_last =
1143							    previous;
1144						}
1145					}
1146					break;
1147				}
1148				previous = msghdr;
1149				prev = &(msghdr->msg_next);
1150			}
1151		}
1152
1153		/*
1154		 * We've either extracted the msghdr for the appropriate
1155		 * message or there isn't one.
1156		 * If there is one then bail out of this loop.
1157		 */
1158
1159		if (msghdr != NULL)
1160			break;
1161
1162		/*
1163		 * Hmph!  No message found.  Does the user want to wait?
1164		 */
1165
1166		if ((msgflg & IPC_NOWAIT) != 0) {
1167			DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1168			    msgtyp));
1169			/* The SVID says to return ENOMSG. */
1170			error = ENOMSG;
1171			goto done2;
1172		}
1173
1174		/*
1175		 * Wait for something to happen
1176		 */
1177
1178		DPRINTF(("msgrcv:  goodnight\n"));
1179		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1180		    "msgwait", 0);
1181		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1182
1183		if (error != 0) {
1184			DPRINTF(("msgsnd:  interrupted system call\n"));
1185			error = EINTR;
1186			goto done2;
1187		}
1188
1189		/*
1190		 * Make sure that the msq queue still exists
1191		 */
1192
1193		if (msqkptr->u.msg_qbytes == 0 ||
1194		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1195			DPRINTF(("msqid deleted\n"));
1196			error = EIDRM;
1197			goto done2;
1198		}
1199	}
1200
1201	/*
1202	 * Return the message to the user.
1203	 *
1204	 * First, do the bookkeeping (before we risk being interrupted).
1205	 */
1206
1207	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1208	msqkptr->u.msg_qnum--;
1209	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1210	msqkptr->u.msg_rtime = time_second;
1211
1212	/*
1213	 * Make msgsz the actual amount that we'll be returning.
1214	 * Note that this effectively truncates the message if it is too long
1215	 * (since msgsz is never increased).
1216	 */
1217
1218	DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1219	    msghdr->msg_ts));
1220	if (msgsz > msghdr->msg_ts)
1221		msgsz = msghdr->msg_ts;
1222
1223	/*
1224	 * Return the type to the user.
1225	 */
1226
1227	mtx_unlock(&msq_mtx);
1228	error = copyout(&(msghdr->msg_type), user_msgp,
1229	    sizeof(msghdr->msg_type));
1230	mtx_lock(&msq_mtx);
1231	if (error != 0) {
1232		DPRINTF(("error (%d) copying out message type\n", error));
1233		msg_freehdr(msghdr);
1234		wakeup(msqkptr);
1235		goto done2;
1236	}
1237	user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1238
1239	/*
1240	 * Return the segments to the user
1241	 */
1242
1243	next = msghdr->msg_spot;
1244	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1245		size_t tlen;
1246
1247		if (msgsz - len > msginfo.msgssz)
1248			tlen = msginfo.msgssz;
1249		else
1250			tlen = msgsz - len;
1251		if (next <= -1)
1252			panic("next too low #3");
1253		if (next >= msginfo.msgseg)
1254			panic("next out of range #3");
1255		mtx_unlock(&msq_mtx);
1256		error = copyout(&msgpool[next * msginfo.msgssz],
1257		    user_msgp, tlen);
1258		mtx_lock(&msq_mtx);
1259		if (error != 0) {
1260			DPRINTF(("error (%d) copying out message segment\n",
1261			    error));
1262			msg_freehdr(msghdr);
1263			wakeup(msqkptr);
1264			goto done2;
1265		}
1266		user_msgp = (char *)user_msgp + tlen;
1267		next = msgmaps[next].next;
1268	}
1269
1270	/*
1271	 * Done, return the actual number of bytes copied out.
1272	 */
1273
1274	msg_freehdr(msghdr);
1275	wakeup(msqkptr);
1276	td->td_retval[0] = msgsz;
1277done2:
1278	mtx_unlock(&msq_mtx);
1279	return (error);
1280}
1281
1282static int
1283sysctl_msqids(SYSCTL_HANDLER_ARGS)
1284{
1285
1286	return (SYSCTL_OUT(req, msqids,
1287	    sizeof(struct msqid_kernel) * msginfo.msgmni));
1288}
1289
1290SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1291    "Maximum message size");
1292SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1293    "Number of message queue identifiers");
1294SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1295    "Maximum number of bytes in a queue");
1296SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1297    "Maximum number of messages in the system");
1298SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1299    "Size of a message segment");
1300SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1301    "Number of message segments");
1302SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1303    NULL, 0, sysctl_msqids, "", "Message queue IDs");
1304