xref: /freebsd/sys/kern/sysv_msg.c (revision 46117e1f0c7bc108569bebd031ced36d9d262d22)
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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
21  *
22  * Copyright (c) 2003-2005 McAfee, Inc.
23  * Copyright (c) 2016-2017 Robert N. M. Watson
24  * All rights reserved.
25  *
26  * This software was developed for the FreeBSD Project in part by McAfee
27  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
28  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
29  * program.
30  *
31  * Portions of this software were developed by BAE Systems, the University of
32  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
33  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
34  * Computing (TC) research program.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include "opt_sysvipc.h"
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sysproto.h>
66 #include <sys/kernel.h>
67 #include <sys/priv.h>
68 #include <sys/proc.h>
69 #include <sys/lock.h>
70 #include <sys/mutex.h>
71 #include <sys/module.h>
72 #include <sys/mount.h>
73 #include <sys/msg.h>
74 #include <sys/racct.h>
75 #include <sys/sx.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysent.h>
79 #include <sys/sysctl.h>
80 #include <sys/malloc.h>
81 #include <sys/jail.h>
82 
83 #include <security/audit/audit.h>
84 #include <security/mac/mac_framework.h>
85 
86 FEATURE(sysv_msg, "System V message queues support");
87 
88 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
89 
90 static int msginit(void);
91 static int msgunload(void);
92 static int sysvmsg_modload(struct module *, int, void *);
93 static void msq_remove(struct msqid_kernel *);
94 static struct prison *msg_find_prison(struct ucred *);
95 static int msq_prison_cansee(struct prison *, struct msqid_kernel *);
96 static int msg_prison_check(void *, void *);
97 static int msg_prison_set(void *, void *);
98 static int msg_prison_get(void *, void *);
99 static int msg_prison_remove(void *, void *);
100 static void msg_prison_cleanup(struct prison *);
101 
102 
103 #ifdef MSG_DEBUG
104 #define DPRINTF(a)	printf a
105 #else
106 #define DPRINTF(a)	(void)0
107 #endif
108 
109 static void msg_freehdr(struct msg *msghdr);
110 
111 #ifndef MSGSSZ
112 #define MSGSSZ	8		/* Each segment must be 2^N long */
113 #endif
114 #ifndef MSGSEG
115 #define MSGSEG	2048		/* must be less than 32767 */
116 #endif
117 #define MSGMAX	(MSGSSZ*MSGSEG)
118 #ifndef MSGMNB
119 #define MSGMNB	2048		/* max # of bytes in a queue */
120 #endif
121 #ifndef MSGMNI
122 #define MSGMNI	40
123 #endif
124 #ifndef MSGTQL
125 #define MSGTQL	40
126 #endif
127 
128 /*
129  * Based on the configuration parameters described in an SVR2 (yes, two)
130  * config(1m) man page.
131  *
132  * Each message is broken up and stored in segments that are msgssz bytes
133  * long.  For efficiency reasons, this should be a power of two.  Also,
134  * it doesn't make sense if it is less than 8 or greater than about 256.
135  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
136  * two between 8 and 1024 inclusive (and panic's if it isn't).
137  */
138 struct msginfo msginfo = {
139                 MSGMAX,         /* max chars in a message */
140                 MSGMNI,         /* # of message queue identifiers */
141                 MSGMNB,         /* max chars in a queue */
142                 MSGTQL,         /* max messages in system */
143                 MSGSSZ,         /* size of a message segment */
144                 		/* (must be small power of 2 greater than 4) */
145                 MSGSEG          /* number of message segments */
146 };
147 
148 /*
149  * macros to convert between msqid_ds's and msqid's.
150  * (specific to this implementation)
151  */
152 #define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
153 #define MSQID_IX(id)	((id) & 0xffff)
154 #define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
155 
156 /*
157  * The rest of this file is specific to this particular implementation.
158  */
159 
160 struct msgmap {
161 	short	next;		/* next segment in buffer */
162     				/* -1 -> available */
163     				/* 0..(MSGSEG-1) -> index of next segment */
164 };
165 
166 #define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
167 
168 static int nfree_msgmaps;	/* # of free map entries */
169 static short free_msgmaps;	/* head of linked list of free map entries */
170 static struct msg *free_msghdrs;/* list of free msg headers */
171 static char *msgpool;		/* MSGMAX byte long msg buffer pool */
172 static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
173 static struct msg *msghdrs;	/* MSGTQL msg headers */
174 static struct msqid_kernel *msqids;	/* MSGMNI msqid_kernel struct's */
175 static struct mtx msq_mtx;	/* global mutex for message queues. */
176 static unsigned msg_prison_slot;/* prison OSD slot */
177 
178 static struct syscall_helper_data msg_syscalls[] = {
179 	SYSCALL_INIT_HELPER(msgctl),
180 	SYSCALL_INIT_HELPER(msgget),
181 	SYSCALL_INIT_HELPER(msgsnd),
182 	SYSCALL_INIT_HELPER(msgrcv),
183 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
184     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
185 	SYSCALL_INIT_HELPER(msgsys),
186 	SYSCALL_INIT_HELPER_COMPAT(freebsd7_msgctl),
187 #endif
188 	SYSCALL_INIT_LAST
189 };
190 
191 #ifdef COMPAT_FREEBSD32
192 #include <compat/freebsd32/freebsd32.h>
193 #include <compat/freebsd32/freebsd32_ipc.h>
194 #include <compat/freebsd32/freebsd32_proto.h>
195 #include <compat/freebsd32/freebsd32_signal.h>
196 #include <compat/freebsd32/freebsd32_syscall.h>
197 #include <compat/freebsd32/freebsd32_util.h>
198 
199 static struct syscall_helper_data msg32_syscalls[] = {
200 	SYSCALL32_INIT_HELPER(freebsd32_msgctl),
201 	SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
202 	SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
203 	SYSCALL32_INIT_HELPER_COMPAT(msgget),
204 	SYSCALL32_INIT_HELPER(freebsd32_msgsys),
205 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
206     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
207 	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
208 #endif
209 	SYSCALL_INIT_LAST
210 };
211 #endif
212 
213 static int
214 msginit()
215 {
216 	struct prison *pr;
217 	void **rsv;
218 	int i, error;
219 	osd_method_t methods[PR_MAXMETHOD] = {
220 	    [PR_METHOD_CHECK] =		msg_prison_check,
221 	    [PR_METHOD_SET] =		msg_prison_set,
222 	    [PR_METHOD_GET] =		msg_prison_get,
223 	    [PR_METHOD_REMOVE] =	msg_prison_remove,
224 	};
225 
226 	msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
227 	msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
228 	msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
229 	msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
230 	msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
231 	    M_WAITOK | M_ZERO);
232 
233 	/*
234 	 * msginfo.msgssz should be a power of two for efficiency reasons.
235 	 * It is also pretty silly if msginfo.msgssz is less than 8
236 	 * or greater than about 256 so ...
237 	 */
238 
239 	i = 8;
240 	while (i < 1024 && i != msginfo.msgssz)
241 		i <<= 1;
242     	if (i != msginfo.msgssz) {
243 		DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
244 		    msginfo.msgssz));
245 		panic("msginfo.msgssz not a small power of 2");
246 	}
247 
248 	if (msginfo.msgseg > 32767) {
249 		DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
250 		panic("msginfo.msgseg > 32767");
251 	}
252 
253 	for (i = 0; i < msginfo.msgseg; i++) {
254 		if (i > 0)
255 			msgmaps[i-1].next = i;
256 		msgmaps[i].next = -1;	/* implies entry is available */
257 	}
258 	free_msgmaps = 0;
259 	nfree_msgmaps = msginfo.msgseg;
260 
261 	for (i = 0; i < msginfo.msgtql; i++) {
262 		msghdrs[i].msg_type = 0;
263 		if (i > 0)
264 			msghdrs[i-1].msg_next = &msghdrs[i];
265 		msghdrs[i].msg_next = NULL;
266 #ifdef MAC
267 		mac_sysvmsg_init(&msghdrs[i]);
268 #endif
269     	}
270 	free_msghdrs = &msghdrs[0];
271 
272 	for (i = 0; i < msginfo.msgmni; i++) {
273 		msqids[i].u.msg_qbytes = 0;	/* implies entry is available */
274 		msqids[i].u.msg_perm.seq = 0;	/* reset to a known value */
275 		msqids[i].u.msg_perm.mode = 0;
276 #ifdef MAC
277 		mac_sysvmsq_init(&msqids[i]);
278 #endif
279 	}
280 	mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
281 
282 	/* Set current prisons according to their allow.sysvipc. */
283 	msg_prison_slot = osd_jail_register(NULL, methods);
284 	rsv = osd_reserve(msg_prison_slot);
285 	prison_lock(&prison0);
286 	(void)osd_jail_set_reserved(&prison0, msg_prison_slot, rsv, &prison0);
287 	prison_unlock(&prison0);
288 	rsv = NULL;
289 	sx_slock(&allprison_lock);
290 	TAILQ_FOREACH(pr, &allprison, pr_list) {
291 		if (rsv == NULL)
292 			rsv = osd_reserve(msg_prison_slot);
293 		prison_lock(pr);
294 		if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
295 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
296 			    &prison0);
297 			rsv = NULL;
298 		}
299 		prison_unlock(pr);
300 	}
301 	if (rsv != NULL)
302 		osd_free_reserved(rsv);
303 	sx_sunlock(&allprison_lock);
304 
305 	error = syscall_helper_register(msg_syscalls, SY_THR_STATIC_KLD);
306 	if (error != 0)
307 		return (error);
308 #ifdef COMPAT_FREEBSD32
309 	error = syscall32_helper_register(msg32_syscalls, SY_THR_STATIC_KLD);
310 	if (error != 0)
311 		return (error);
312 #endif
313 	return (0);
314 }
315 
316 static int
317 msgunload()
318 {
319 	struct msqid_kernel *msqkptr;
320 	int msqid;
321 #ifdef MAC
322 	int i;
323 #endif
324 
325 	syscall_helper_unregister(msg_syscalls);
326 #ifdef COMPAT_FREEBSD32
327 	syscall32_helper_unregister(msg32_syscalls);
328 #endif
329 
330 	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
331 		msqkptr = &msqids[msqid];
332 		if (msqkptr->u.msg_qbytes != 0 ||
333 		    (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
334 			break;
335 	}
336 	if (msqid != msginfo.msgmni)
337 		return (EBUSY);
338 
339 	if (msg_prison_slot != 0)
340 		osd_jail_deregister(msg_prison_slot);
341 #ifdef MAC
342 	for (i = 0; i < msginfo.msgtql; i++)
343 		mac_sysvmsg_destroy(&msghdrs[i]);
344 	for (msqid = 0; msqid < msginfo.msgmni; msqid++)
345 		mac_sysvmsq_destroy(&msqids[msqid]);
346 #endif
347 	free(msgpool, M_MSG);
348 	free(msgmaps, M_MSG);
349 	free(msghdrs, M_MSG);
350 	free(msqids, M_MSG);
351 	mtx_destroy(&msq_mtx);
352 	return (0);
353 }
354 
355 
356 static int
357 sysvmsg_modload(struct module *module, int cmd, void *arg)
358 {
359 	int error = 0;
360 
361 	switch (cmd) {
362 	case MOD_LOAD:
363 		error = msginit();
364 		if (error != 0)
365 			msgunload();
366 		break;
367 	case MOD_UNLOAD:
368 		error = msgunload();
369 		break;
370 	case MOD_SHUTDOWN:
371 		break;
372 	default:
373 		error = EINVAL;
374 		break;
375 	}
376 	return (error);
377 }
378 
379 static moduledata_t sysvmsg_mod = {
380 	"sysvmsg",
381 	&sysvmsg_modload,
382 	NULL
383 };
384 
385 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
386 MODULE_VERSION(sysvmsg, 1);
387 
388 static void
389 msg_freehdr(msghdr)
390 	struct msg *msghdr;
391 {
392 	while (msghdr->msg_ts > 0) {
393 		short next;
394 		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
395 			panic("msghdr->msg_spot out of range");
396 		next = msgmaps[msghdr->msg_spot].next;
397 		msgmaps[msghdr->msg_spot].next = free_msgmaps;
398 		free_msgmaps = msghdr->msg_spot;
399 		nfree_msgmaps++;
400 		msghdr->msg_spot = next;
401 		if (msghdr->msg_ts >= msginfo.msgssz)
402 			msghdr->msg_ts -= msginfo.msgssz;
403 		else
404 			msghdr->msg_ts = 0;
405 	}
406 	if (msghdr->msg_spot != -1)
407 		panic("msghdr->msg_spot != -1");
408 	msghdr->msg_next = free_msghdrs;
409 	free_msghdrs = msghdr;
410 #ifdef MAC
411 	mac_sysvmsg_cleanup(msghdr);
412 #endif
413 }
414 
415 static void
416 msq_remove(struct msqid_kernel *msqkptr)
417 {
418 	struct msg *msghdr;
419 
420 	racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
421 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
422 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
423 	crfree(msqkptr->cred);
424 	msqkptr->cred = NULL;
425 
426 	/* Free the message headers */
427 	msghdr = msqkptr->u.__msg_first;
428 	while (msghdr != NULL) {
429 		struct msg *msghdr_tmp;
430 
431 		/* Free the segments of each message */
432 		msqkptr->u.msg_cbytes -= msghdr->msg_ts;
433 		msqkptr->u.msg_qnum--;
434 		msghdr_tmp = msghdr;
435 		msghdr = msghdr->msg_next;
436 		msg_freehdr(msghdr_tmp);
437 	}
438 
439 	if (msqkptr->u.msg_cbytes != 0)
440 		panic("msg_cbytes is screwed up");
441 	if (msqkptr->u.msg_qnum != 0)
442 		panic("msg_qnum is screwed up");
443 
444 	msqkptr->u.msg_qbytes = 0;	/* Mark it as free */
445 
446 #ifdef MAC
447 	mac_sysvmsq_cleanup(msqkptr);
448 #endif
449 
450 	wakeup(msqkptr);
451 }
452 
453 static struct prison *
454 msg_find_prison(struct ucred *cred)
455 {
456 	struct prison *pr, *rpr;
457 
458 	pr = cred->cr_prison;
459 	prison_lock(pr);
460 	rpr = osd_jail_get(pr, msg_prison_slot);
461 	prison_unlock(pr);
462 	return rpr;
463 }
464 
465 static int
466 msq_prison_cansee(struct prison *rpr, struct msqid_kernel *msqkptr)
467 {
468 
469 	if (msqkptr->cred == NULL ||
470 	    !(rpr == msqkptr->cred->cr_prison ||
471 	      prison_ischild(rpr, msqkptr->cred->cr_prison)))
472 		return (EINVAL);
473 	return (0);
474 }
475 
476 #ifndef _SYS_SYSPROTO_H_
477 struct msgctl_args {
478 	int	msqid;
479 	int	cmd;
480 	struct	msqid_ds *buf;
481 };
482 #endif
483 int
484 sys_msgctl(struct thread *td, struct msgctl_args *uap)
485 {
486 	int msqid = uap->msqid;
487 	int cmd = uap->cmd;
488 	struct msqid_ds msqbuf;
489 	int error;
490 
491 	DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
492 	if (cmd == IPC_SET &&
493 	    (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
494 		return (error);
495 	error = kern_msgctl(td, msqid, cmd, &msqbuf);
496 	if (cmd == IPC_STAT && error == 0)
497 		error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
498 	return (error);
499 }
500 
501 int
502 kern_msgctl(td, msqid, cmd, msqbuf)
503 	struct thread *td;
504 	int msqid;
505 	int cmd;
506 	struct msqid_ds *msqbuf;
507 {
508 	int rval, error, msqix;
509 	struct msqid_kernel *msqkptr;
510 	struct prison *rpr;
511 
512 	rpr = msg_find_prison(td->td_ucred);
513 	if (rpr == NULL)
514 		return (ENOSYS);
515 
516 	AUDIT_ARG_SVIPC_CMD(cmd);
517 	AUDIT_ARG_SVIPC_ID(msqid);
518 	msqix = IPCID_TO_IX(msqid);
519 
520 	if (msqix < 0 || msqix >= msginfo.msgmni) {
521 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
522 		    msginfo.msgmni));
523 		return (EINVAL);
524 	}
525 
526 	msqkptr = &msqids[msqix];
527 
528 	mtx_lock(&msq_mtx);
529 	if (msqkptr->u.msg_qbytes == 0) {
530 		DPRINTF(("no such msqid\n"));
531 		error = EINVAL;
532 		goto done2;
533 	}
534 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
535 		DPRINTF(("wrong sequence number\n"));
536 		error = EINVAL;
537 		goto done2;
538 	}
539 
540 	error = msq_prison_cansee(rpr, msqkptr);
541 	if (error != 0) {
542 		DPRINTF(("requester can't see prison\n"));
543 		goto done2;
544 	}
545 
546 #ifdef MAC
547 	error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
548 	if (error != 0)
549 		goto done2;
550 #endif
551 
552 	error = 0;
553 	rval = 0;
554 
555 	switch (cmd) {
556 
557 	case IPC_RMID:
558 	{
559 #ifdef MAC
560 		struct msg *msghdr;
561 #endif
562 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
563 			goto done2;
564 
565 #ifdef MAC
566 		/*
567 		 * Check that the thread has MAC access permissions to
568 		 * individual msghdrs.  Note: We need to do this in a
569 		 * separate loop because the actual loop alters the
570 		 * msq/msghdr info as it progresses, and there is no going
571 		 * back if half the way through we discover that the
572 		 * thread cannot free a certain msghdr.  The msq will get
573 		 * into an inconsistent state.
574 		 */
575 		for (msghdr = msqkptr->u.__msg_first; msghdr != NULL;
576 		    msghdr = msghdr->msg_next) {
577 			error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
578 			if (error != 0)
579 				goto done2;
580 		}
581 #endif
582 
583 		msq_remove(msqkptr);
584 	}
585 
586 		break;
587 
588 	case IPC_SET:
589 		AUDIT_ARG_SVIPC_PERM(&msqbuf->msg_perm);
590 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
591 			goto done2;
592 		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
593 			error = priv_check(td, PRIV_IPC_MSGSIZE);
594 			if (error)
595 				goto done2;
596 		}
597 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
598 			DPRINTF(("can't increase msg_qbytes beyond %d"
599 			    "(truncating)\n", msginfo.msgmnb));
600 			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
601 		}
602 		if (msqbuf->msg_qbytes == 0) {
603 			DPRINTF(("can't reduce msg_qbytes to 0\n"));
604 			error = EINVAL;		/* non-standard errno! */
605 			goto done2;
606 		}
607 		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
608 		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
609 		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
610 		    (msqbuf->msg_perm.mode & 0777);
611 		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
612 		msqkptr->u.msg_ctime = time_second;
613 		break;
614 
615 	case IPC_STAT:
616 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
617 			DPRINTF(("requester doesn't have read access\n"));
618 			goto done2;
619 		}
620 		*msqbuf = msqkptr->u;
621 		if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
622 			msqbuf->msg_perm.key = IPC_PRIVATE;
623 		break;
624 
625 	default:
626 		DPRINTF(("invalid command %d\n", cmd));
627 		error = EINVAL;
628 		goto done2;
629 	}
630 
631 	if (error == 0)
632 		td->td_retval[0] = rval;
633 done2:
634 	mtx_unlock(&msq_mtx);
635 	return (error);
636 }
637 
638 #ifndef _SYS_SYSPROTO_H_
639 struct msgget_args {
640 	key_t	key;
641 	int	msgflg;
642 };
643 #endif
644 
645 int
646 sys_msgget(struct thread *td, struct msgget_args *uap)
647 {
648 	int msqid, error = 0;
649 	int key = uap->key;
650 	int msgflg = uap->msgflg;
651 	struct ucred *cred = td->td_ucred;
652 	struct msqid_kernel *msqkptr = NULL;
653 
654 	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
655 
656 	if (msg_find_prison(cred) == NULL)
657 		return (ENOSYS);
658 
659 	mtx_lock(&msq_mtx);
660 	if (key != IPC_PRIVATE) {
661 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
662 			msqkptr = &msqids[msqid];
663 			if (msqkptr->u.msg_qbytes != 0 &&
664 			    msqkptr->cred != NULL &&
665 			    msqkptr->cred->cr_prison == cred->cr_prison &&
666 			    msqkptr->u.msg_perm.key == key)
667 				break;
668 		}
669 		if (msqid < msginfo.msgmni) {
670 			DPRINTF(("found public key\n"));
671 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
672 				DPRINTF(("not exclusive\n"));
673 				error = EEXIST;
674 				goto done2;
675 			}
676 			AUDIT_ARG_SVIPC_ID(IXSEQ_TO_IPCID(msqid,
677 			    msqkptr->u.msg_perm));
678 			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
679 			    msgflg & 0700))) {
680 				DPRINTF(("requester doesn't have 0%o access\n",
681 				    msgflg & 0700));
682 				goto done2;
683 			}
684 #ifdef MAC
685 			error = mac_sysvmsq_check_msqget(cred, msqkptr);
686 			if (error != 0)
687 				goto done2;
688 #endif
689 			goto found;
690 		}
691 	}
692 
693 	DPRINTF(("need to allocate the msqid_ds\n"));
694 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
695 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
696 			/*
697 			 * Look for an unallocated and unlocked msqid_ds.
698 			 * msqid_ds's can be locked by msgsnd or msgrcv while
699 			 * they are copying the message in/out.  We can't
700 			 * re-use the entry until they release it.
701 			 */
702 			msqkptr = &msqids[msqid];
703 			if (msqkptr->u.msg_qbytes == 0 &&
704 			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
705 				break;
706 		}
707 		if (msqid == msginfo.msgmni) {
708 			DPRINTF(("no more msqid_ds's available\n"));
709 			error = ENOSPC;
710 			goto done2;
711 		}
712 #ifdef RACCT
713 		if (racct_enable) {
714 			PROC_LOCK(td->td_proc);
715 			error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
716 			PROC_UNLOCK(td->td_proc);
717 			if (error != 0) {
718 				error = ENOSPC;
719 				goto done2;
720 			}
721 		}
722 #endif
723 		DPRINTF(("msqid %d is available\n", msqid));
724 		msqkptr->u.msg_perm.key = key;
725 		msqkptr->u.msg_perm.cuid = cred->cr_uid;
726 		msqkptr->u.msg_perm.uid = cred->cr_uid;
727 		msqkptr->u.msg_perm.cgid = cred->cr_gid;
728 		msqkptr->u.msg_perm.gid = cred->cr_gid;
729 		msqkptr->u.msg_perm.mode = (msgflg & 0777);
730 		msqkptr->cred = crhold(cred);
731 		/* Make sure that the returned msqid is unique */
732 		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
733 		msqkptr->u.__msg_first = NULL;
734 		msqkptr->u.__msg_last = NULL;
735 		msqkptr->u.msg_cbytes = 0;
736 		msqkptr->u.msg_qnum = 0;
737 		msqkptr->u.msg_qbytes = msginfo.msgmnb;
738 		msqkptr->u.msg_lspid = 0;
739 		msqkptr->u.msg_lrpid = 0;
740 		msqkptr->u.msg_stime = 0;
741 		msqkptr->u.msg_rtime = 0;
742 		msqkptr->u.msg_ctime = time_second;
743 #ifdef MAC
744 		mac_sysvmsq_create(cred, msqkptr);
745 #endif
746 		AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
747 	} else {
748 		DPRINTF(("didn't find it and wasn't asked to create it\n"));
749 		error = ENOENT;
750 		goto done2;
751 	}
752 
753 found:
754 	/* Construct the unique msqid */
755 	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
756 done2:
757 	mtx_unlock(&msq_mtx);
758 	return (error);
759 }
760 
761 #ifndef _SYS_SYSPROTO_H_
762 struct msgsnd_args {
763 	int	msqid;
764 	const void	*msgp;	/* XXX msgp is actually mtext. */
765 	size_t	msgsz;
766 	int	msgflg;
767 };
768 #endif
769 int
770 kern_msgsnd(struct thread *td, int msqid, const void *msgp,
771     size_t msgsz, int msgflg, long mtype)
772 {
773 	int msqix, segs_needed, error = 0;
774 	struct msqid_kernel *msqkptr;
775 	struct msg *msghdr;
776 	struct prison *rpr;
777 	short next;
778 #ifdef RACCT
779 	size_t saved_msgsz = 0;
780 #endif
781 
782 	rpr = msg_find_prison(td->td_ucred);
783 	if (rpr == NULL)
784 		return (ENOSYS);
785 
786 	mtx_lock(&msq_mtx);
787 	AUDIT_ARG_SVIPC_ID(msqid);
788 	msqix = IPCID_TO_IX(msqid);
789 
790 	if (msqix < 0 || msqix >= msginfo.msgmni) {
791 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
792 		    msginfo.msgmni));
793 		error = EINVAL;
794 		goto done2;
795 	}
796 
797 	msqkptr = &msqids[msqix];
798 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
799 	if (msqkptr->u.msg_qbytes == 0) {
800 		DPRINTF(("no such message queue id\n"));
801 		error = EINVAL;
802 		goto done2;
803 	}
804 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
805 		DPRINTF(("wrong sequence number\n"));
806 		error = EINVAL;
807 		goto done2;
808 	}
809 
810 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
811 		DPRINTF(("requester can't see prison\n"));
812 		goto done2;
813 	}
814 
815 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
816 		DPRINTF(("requester doesn't have write access\n"));
817 		goto done2;
818 	}
819 
820 #ifdef MAC
821 	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
822 	if (error != 0)
823 		goto done2;
824 #endif
825 
826 #ifdef RACCT
827 	if (racct_enable) {
828 		PROC_LOCK(td->td_proc);
829 		if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
830 			PROC_UNLOCK(td->td_proc);
831 			error = EAGAIN;
832 			goto done2;
833 		}
834 		saved_msgsz = msgsz;
835 		if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
836 			racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
837 			PROC_UNLOCK(td->td_proc);
838 			error = EAGAIN;
839 			goto done2;
840 		}
841 		PROC_UNLOCK(td->td_proc);
842 	}
843 #endif
844 
845 	segs_needed = howmany(msgsz, msginfo.msgssz);
846 	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
847 	    msginfo.msgssz, segs_needed));
848 	for (;;) {
849 		int need_more_resources = 0;
850 
851 		/*
852 		 * check msgsz
853 		 * (inside this loop in case msg_qbytes changes while we sleep)
854 		 */
855 
856 		if (msgsz > msqkptr->u.msg_qbytes) {
857 			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
858 			error = EINVAL;
859 			goto done3;
860 		}
861 
862 		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
863 			DPRINTF(("msqid is locked\n"));
864 			need_more_resources = 1;
865 		}
866 		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
867 			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
868 			need_more_resources = 1;
869 		}
870 		if (segs_needed > nfree_msgmaps) {
871 			DPRINTF(("segs_needed > nfree_msgmaps\n"));
872 			need_more_resources = 1;
873 		}
874 		if (free_msghdrs == NULL) {
875 			DPRINTF(("no more msghdrs\n"));
876 			need_more_resources = 1;
877 		}
878 
879 		if (need_more_resources) {
880 			int we_own_it;
881 
882 			if ((msgflg & IPC_NOWAIT) != 0) {
883 				DPRINTF(("need more resources but caller "
884 				    "doesn't want to wait\n"));
885 				error = EAGAIN;
886 				goto done3;
887 			}
888 
889 			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
890 				DPRINTF(("we don't own the msqid_ds\n"));
891 				we_own_it = 0;
892 			} else {
893 				/* Force later arrivals to wait for our
894 				   request */
895 				DPRINTF(("we own the msqid_ds\n"));
896 				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
897 				we_own_it = 1;
898 			}
899 			DPRINTF(("msgsnd:  goodnight\n"));
900 			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
901 			    "msgsnd", hz);
902 			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
903 			if (we_own_it)
904 				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
905 			if (error == EWOULDBLOCK) {
906 				DPRINTF(("msgsnd:  timed out\n"));
907 				continue;
908 			}
909 			if (error != 0) {
910 				DPRINTF(("msgsnd:  interrupted system call\n"));
911 				error = EINTR;
912 				goto done3;
913 			}
914 
915 			/*
916 			 * Make sure that the msq queue still exists
917 			 */
918 
919 			if (msqkptr->u.msg_qbytes == 0) {
920 				DPRINTF(("msqid deleted\n"));
921 				error = EIDRM;
922 				goto done3;
923 			}
924 
925 		} else {
926 			DPRINTF(("got all the resources that we need\n"));
927 			break;
928 		}
929 	}
930 
931 	/*
932 	 * We have the resources that we need.
933 	 * Make sure!
934 	 */
935 
936 	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
937 		panic("msg_perm.mode & MSG_LOCKED");
938 	if (segs_needed > nfree_msgmaps)
939 		panic("segs_needed > nfree_msgmaps");
940 	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
941 		panic("msgsz + msg_cbytes > msg_qbytes");
942 	if (free_msghdrs == NULL)
943 		panic("no more msghdrs");
944 
945 	/*
946 	 * Re-lock the msqid_ds in case we page-fault when copying in the
947 	 * message
948 	 */
949 
950 	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
951 		panic("msqid_ds is already locked");
952 	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
953 
954 	/*
955 	 * Allocate a message header
956 	 */
957 
958 	msghdr = free_msghdrs;
959 	free_msghdrs = msghdr->msg_next;
960 	msghdr->msg_spot = -1;
961 	msghdr->msg_ts = msgsz;
962 	msghdr->msg_type = mtype;
963 #ifdef MAC
964 	/*
965 	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
966 	 * immediately?  Or, should it be checked just before the msg is
967 	 * enqueued in the msgq (as it is done now)?
968 	 */
969 	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
970 #endif
971 
972 	/*
973 	 * Allocate space for the message
974 	 */
975 
976 	while (segs_needed > 0) {
977 		if (nfree_msgmaps <= 0)
978 			panic("not enough msgmaps");
979 		if (free_msgmaps == -1)
980 			panic("nil free_msgmaps");
981 		next = free_msgmaps;
982 		if (next <= -1)
983 			panic("next too low #1");
984 		if (next >= msginfo.msgseg)
985 			panic("next out of range #1");
986 		DPRINTF(("allocating segment %d to message\n", next));
987 		free_msgmaps = msgmaps[next].next;
988 		nfree_msgmaps--;
989 		msgmaps[next].next = msghdr->msg_spot;
990 		msghdr->msg_spot = next;
991 		segs_needed--;
992 	}
993 
994 	/*
995 	 * Validate the message type
996 	 */
997 
998 	if (msghdr->msg_type < 1) {
999 		msg_freehdr(msghdr);
1000 		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1001 		wakeup(msqkptr);
1002 		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
1003 		error = EINVAL;
1004 		goto done3;
1005 	}
1006 
1007 	/*
1008 	 * Copy in the message body
1009 	 */
1010 
1011 	next = msghdr->msg_spot;
1012 	while (msgsz > 0) {
1013 		size_t tlen;
1014 		if (msgsz > msginfo.msgssz)
1015 			tlen = msginfo.msgssz;
1016 		else
1017 			tlen = msgsz;
1018 		if (next <= -1)
1019 			panic("next too low #2");
1020 		if (next >= msginfo.msgseg)
1021 			panic("next out of range #2");
1022 		mtx_unlock(&msq_mtx);
1023 		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1024 		    tlen)) != 0) {
1025 			mtx_lock(&msq_mtx);
1026 			DPRINTF(("error %d copying in message segment\n",
1027 			    error));
1028 			msg_freehdr(msghdr);
1029 			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1030 			wakeup(msqkptr);
1031 			goto done3;
1032 		}
1033 		mtx_lock(&msq_mtx);
1034 		msgsz -= tlen;
1035 		msgp = (const char *)msgp + tlen;
1036 		next = msgmaps[next].next;
1037 	}
1038 	if (next != -1)
1039 		panic("didn't use all the msg segments");
1040 
1041 	/*
1042 	 * We've got the message.  Unlock the msqid_ds.
1043 	 */
1044 
1045 	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1046 
1047 	/*
1048 	 * Make sure that the msqid_ds is still allocated.
1049 	 */
1050 
1051 	if (msqkptr->u.msg_qbytes == 0) {
1052 		msg_freehdr(msghdr);
1053 		wakeup(msqkptr);
1054 		error = EIDRM;
1055 		goto done3;
1056 	}
1057 
1058 #ifdef MAC
1059 	/*
1060 	 * Note: Since the task/thread allocates the msghdr and usually
1061 	 * primes it with its own MAC label, for a majority of policies, it
1062 	 * won't be necessary to check whether the msghdr has access
1063 	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
1064 	 * suffice in that case.  However, this hook may be required where
1065 	 * individual policies derive a non-identical label for the msghdr
1066 	 * from the current thread label and may want to check the msghdr
1067 	 * enqueue permissions, along with read/write permissions to the
1068 	 * msgq.
1069 	 */
1070 	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1071 	if (error != 0) {
1072 		msg_freehdr(msghdr);
1073 		wakeup(msqkptr);
1074 		goto done3;
1075 	}
1076 #endif
1077 
1078 	/*
1079 	 * Put the message into the queue
1080 	 */
1081 	if (msqkptr->u.__msg_first == NULL) {
1082 		msqkptr->u.__msg_first = msghdr;
1083 		msqkptr->u.__msg_last = msghdr;
1084 	} else {
1085 		msqkptr->u.__msg_last->msg_next = msghdr;
1086 		msqkptr->u.__msg_last = msghdr;
1087 	}
1088 	msqkptr->u.__msg_last->msg_next = NULL;
1089 
1090 	msqkptr->u.msg_cbytes += msghdr->msg_ts;
1091 	msqkptr->u.msg_qnum++;
1092 	msqkptr->u.msg_lspid = td->td_proc->p_pid;
1093 	msqkptr->u.msg_stime = time_second;
1094 
1095 	wakeup(msqkptr);
1096 	td->td_retval[0] = 0;
1097 done3:
1098 #ifdef RACCT
1099 	if (racct_enable && error != 0) {
1100 		PROC_LOCK(td->td_proc);
1101 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1102 		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1103 		PROC_UNLOCK(td->td_proc);
1104 	}
1105 #endif
1106 done2:
1107 	mtx_unlock(&msq_mtx);
1108 	return (error);
1109 }
1110 
1111 int
1112 sys_msgsnd(struct thread *td, struct msgsnd_args *uap)
1113 {
1114 	int error;
1115 	long mtype;
1116 
1117 	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1118 	    uap->msgsz, uap->msgflg));
1119 
1120 	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1121 		DPRINTF(("error %d copying the message type\n", error));
1122 		return (error);
1123 	}
1124 	return (kern_msgsnd(td, uap->msqid,
1125 	    (const char *)uap->msgp + sizeof(mtype),
1126 	    uap->msgsz, uap->msgflg, mtype));
1127 }
1128 
1129 #ifndef _SYS_SYSPROTO_H_
1130 struct msgrcv_args {
1131 	int	msqid;
1132 	void	*msgp;
1133 	size_t	msgsz;
1134 	long	msgtyp;
1135 	int	msgflg;
1136 };
1137 #endif
1138 /* XXX msgp is actually mtext. */
1139 int
1140 kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp,
1141     int msgflg, long *mtype)
1142 {
1143 	size_t len;
1144 	struct msqid_kernel *msqkptr;
1145 	struct msg *msghdr;
1146 	struct prison *rpr;
1147 	int msqix, error = 0;
1148 	short next;
1149 
1150 	rpr = msg_find_prison(td->td_ucred);
1151 	if (rpr == NULL)
1152 		return (ENOSYS);
1153 
1154 	AUDIT_ARG_SVIPC_ID(msqid);
1155 	msqix = IPCID_TO_IX(msqid);
1156 
1157 	if (msqix < 0 || msqix >= msginfo.msgmni) {
1158 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1159 		    msginfo.msgmni));
1160 		return (EINVAL);
1161 	}
1162 
1163 	msqkptr = &msqids[msqix];
1164 	mtx_lock(&msq_mtx);
1165 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
1166 	if (msqkptr->u.msg_qbytes == 0) {
1167 		DPRINTF(("no such message queue id\n"));
1168 		error = EINVAL;
1169 		goto done2;
1170 	}
1171 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1172 		DPRINTF(("wrong sequence number\n"));
1173 		error = EINVAL;
1174 		goto done2;
1175 	}
1176 
1177 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
1178 		DPRINTF(("requester can't see prison\n"));
1179 		goto done2;
1180 	}
1181 
1182 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1183 		DPRINTF(("requester doesn't have read access\n"));
1184 		goto done2;
1185 	}
1186 
1187 #ifdef MAC
1188 	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1189 	if (error != 0)
1190 		goto done2;
1191 #endif
1192 
1193 	msghdr = NULL;
1194 	while (msghdr == NULL) {
1195 		if (msgtyp == 0) {
1196 			msghdr = msqkptr->u.__msg_first;
1197 			if (msghdr != NULL) {
1198 				if (msgsz < msghdr->msg_ts &&
1199 				    (msgflg & MSG_NOERROR) == 0) {
1200 					DPRINTF(("first message on the queue "
1201 					    "is too big (want %zu, got %d)\n",
1202 					    msgsz, msghdr->msg_ts));
1203 					error = E2BIG;
1204 					goto done2;
1205 				}
1206 #ifdef MAC
1207 				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1208 				    msghdr);
1209 				if (error != 0)
1210 					goto done2;
1211 #endif
1212 				if (msqkptr->u.__msg_first ==
1213 				    msqkptr->u.__msg_last) {
1214 					msqkptr->u.__msg_first = NULL;
1215 					msqkptr->u.__msg_last = NULL;
1216 				} else {
1217 					msqkptr->u.__msg_first = msghdr->msg_next;
1218 					if (msqkptr->u.__msg_first == NULL)
1219 						panic("msg_first/last screwed up #1");
1220 				}
1221 			}
1222 		} else {
1223 			struct msg *previous;
1224 			struct msg **prev;
1225 
1226 			previous = NULL;
1227 			prev = &(msqkptr->u.__msg_first);
1228 			while ((msghdr = *prev) != NULL) {
1229 				/*
1230 				 * Is this message's type an exact match or is
1231 				 * this message's type less than or equal to
1232 				 * the absolute value of a negative msgtyp?
1233 				 * Note that the second half of this test can
1234 				 * NEVER be true if msgtyp is positive since
1235 				 * msg_type is always positive!
1236 				 */
1237 
1238 				if (msgtyp == msghdr->msg_type ||
1239 				    msghdr->msg_type <= -msgtyp) {
1240 					DPRINTF(("found message type %ld, "
1241 					    "requested %ld\n",
1242 					    msghdr->msg_type, msgtyp));
1243 					if (msgsz < msghdr->msg_ts &&
1244 					    (msgflg & MSG_NOERROR) == 0) {
1245 						DPRINTF(("requested message "
1246 						    "on the queue is too big "
1247 						    "(want %zu, got %hu)\n",
1248 						    msgsz, msghdr->msg_ts));
1249 						error = E2BIG;
1250 						goto done2;
1251 					}
1252 #ifdef MAC
1253 					error = mac_sysvmsq_check_msgrcv(
1254 					    td->td_ucred, msghdr);
1255 					if (error != 0)
1256 						goto done2;
1257 #endif
1258 					*prev = msghdr->msg_next;
1259 					if (msghdr == msqkptr->u.__msg_last) {
1260 						if (previous == NULL) {
1261 							if (prev !=
1262 							    &msqkptr->u.__msg_first)
1263 								panic("__msg_first/last screwed up #2");
1264 							msqkptr->u.__msg_first =
1265 							    NULL;
1266 							msqkptr->u.__msg_last =
1267 							    NULL;
1268 						} else {
1269 							if (prev ==
1270 							    &msqkptr->u.__msg_first)
1271 								panic("__msg_first/last screwed up #3");
1272 							msqkptr->u.__msg_last =
1273 							    previous;
1274 						}
1275 					}
1276 					break;
1277 				}
1278 				previous = msghdr;
1279 				prev = &(msghdr->msg_next);
1280 			}
1281 		}
1282 
1283 		/*
1284 		 * We've either extracted the msghdr for the appropriate
1285 		 * message or there isn't one.
1286 		 * If there is one then bail out of this loop.
1287 		 */
1288 
1289 		if (msghdr != NULL)
1290 			break;
1291 
1292 		/*
1293 		 * Hmph!  No message found.  Does the user want to wait?
1294 		 */
1295 
1296 		if ((msgflg & IPC_NOWAIT) != 0) {
1297 			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1298 			    msgtyp));
1299 			/* The SVID says to return ENOMSG. */
1300 			error = ENOMSG;
1301 			goto done2;
1302 		}
1303 
1304 		/*
1305 		 * Wait for something to happen
1306 		 */
1307 
1308 		DPRINTF(("msgrcv:  goodnight\n"));
1309 		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1310 		    "msgrcv", 0);
1311 		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1312 
1313 		if (error != 0) {
1314 			DPRINTF(("msgrcv:  interrupted system call\n"));
1315 			error = EINTR;
1316 			goto done2;
1317 		}
1318 
1319 		/*
1320 		 * Make sure that the msq queue still exists
1321 		 */
1322 
1323 		if (msqkptr->u.msg_qbytes == 0 ||
1324 		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1325 			DPRINTF(("msqid deleted\n"));
1326 			error = EIDRM;
1327 			goto done2;
1328 		}
1329 	}
1330 
1331 	/*
1332 	 * Return the message to the user.
1333 	 *
1334 	 * First, do the bookkeeping (before we risk being interrupted).
1335 	 */
1336 
1337 	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1338 	msqkptr->u.msg_qnum--;
1339 	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1340 	msqkptr->u.msg_rtime = time_second;
1341 
1342 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1343 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1344 
1345 	/*
1346 	 * Make msgsz the actual amount that we'll be returning.
1347 	 * Note that this effectively truncates the message if it is too long
1348 	 * (since msgsz is never increased).
1349 	 */
1350 
1351 	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1352 	    msghdr->msg_ts));
1353 	if (msgsz > msghdr->msg_ts)
1354 		msgsz = msghdr->msg_ts;
1355 	*mtype = msghdr->msg_type;
1356 
1357 	/*
1358 	 * Return the segments to the user
1359 	 */
1360 
1361 	next = msghdr->msg_spot;
1362 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1363 		size_t tlen;
1364 
1365 		if (msgsz - len > msginfo.msgssz)
1366 			tlen = msginfo.msgssz;
1367 		else
1368 			tlen = msgsz - len;
1369 		if (next <= -1)
1370 			panic("next too low #3");
1371 		if (next >= msginfo.msgseg)
1372 			panic("next out of range #3");
1373 		mtx_unlock(&msq_mtx);
1374 		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1375 		mtx_lock(&msq_mtx);
1376 		if (error != 0) {
1377 			DPRINTF(("error (%d) copying out message segment\n",
1378 			    error));
1379 			msg_freehdr(msghdr);
1380 			wakeup(msqkptr);
1381 			goto done2;
1382 		}
1383 		msgp = (char *)msgp + tlen;
1384 		next = msgmaps[next].next;
1385 	}
1386 
1387 	/*
1388 	 * Done, return the actual number of bytes copied out.
1389 	 */
1390 
1391 	msg_freehdr(msghdr);
1392 	wakeup(msqkptr);
1393 	td->td_retval[0] = msgsz;
1394 done2:
1395 	mtx_unlock(&msq_mtx);
1396 	return (error);
1397 }
1398 
1399 int
1400 sys_msgrcv(struct thread *td, struct msgrcv_args *uap)
1401 {
1402 	int error;
1403 	long mtype;
1404 
1405 	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1406 	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1407 
1408 	if ((error = kern_msgrcv(td, uap->msqid,
1409 	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1410 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1411 		return (error);
1412 	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1413 		DPRINTF(("error %d copying the message type\n", error));
1414 	return (error);
1415 }
1416 
1417 static int
1418 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1419 {
1420 	struct msqid_kernel tmsqk;
1421 #ifdef COMPAT_FREEBSD32
1422 	struct msqid_kernel32 tmsqk32;
1423 #endif
1424 	struct prison *pr, *rpr;
1425 	void *outaddr;
1426 	size_t outsize;
1427 	int error, i;
1428 
1429 	pr = req->td->td_ucred->cr_prison;
1430 	rpr = msg_find_prison(req->td->td_ucred);
1431 	error = 0;
1432 	for (i = 0; i < msginfo.msgmni; i++) {
1433 		mtx_lock(&msq_mtx);
1434 		if (msqids[i].u.msg_qbytes == 0 || rpr == NULL ||
1435 		    msq_prison_cansee(rpr, &msqids[i]) != 0)
1436 			bzero(&tmsqk, sizeof(tmsqk));
1437 		else {
1438 			tmsqk = msqids[i];
1439 			if (tmsqk.cred->cr_prison != pr)
1440 				tmsqk.u.msg_perm.key = IPC_PRIVATE;
1441 		}
1442 		mtx_unlock(&msq_mtx);
1443 #ifdef COMPAT_FREEBSD32
1444 		if (SV_CURPROC_FLAG(SV_ILP32)) {
1445 			bzero(&tmsqk32, sizeof(tmsqk32));
1446 			freebsd32_ipcperm_out(&tmsqk.u.msg_perm,
1447 			    &tmsqk32.u.msg_perm);
1448 			/* Don't copy u.msg_first or u.msg_last */
1449 			CP(tmsqk, tmsqk32, u.msg_cbytes);
1450 			CP(tmsqk, tmsqk32, u.msg_qnum);
1451 			CP(tmsqk, tmsqk32, u.msg_qbytes);
1452 			CP(tmsqk, tmsqk32, u.msg_lspid);
1453 			CP(tmsqk, tmsqk32, u.msg_lrpid);
1454 			CP(tmsqk, tmsqk32, u.msg_stime);
1455 			CP(tmsqk, tmsqk32, u.msg_rtime);
1456 			CP(tmsqk, tmsqk32, u.msg_ctime);
1457 			/* Don't copy label or cred */
1458 			outaddr = &tmsqk32;
1459 			outsize = sizeof(tmsqk32);
1460 		} else
1461 #endif
1462 		{
1463 			/* Don't leak kernel pointers */
1464 			tmsqk.u.__msg_first = NULL;
1465 			tmsqk.u.__msg_last = NULL;
1466 			tmsqk.label = NULL;
1467 			tmsqk.cred = NULL;
1468 			/*
1469 			 * XXX: some padding also exists, but we take care to
1470 			 * allocate our pool of msqid_kernel structs with
1471 			 * zeroed memory so this should be OK.
1472 			 */
1473 			outaddr = &tmsqk;
1474 			outsize = sizeof(tmsqk);
1475 		}
1476 		error = SYSCTL_OUT(req, outaddr, outsize);
1477 		if (error != 0)
1478 			break;
1479 	}
1480 	return (error);
1481 }
1482 
1483 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1484     "Maximum message size");
1485 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1486     "Number of message queue identifiers");
1487 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1488     "Maximum number of bytes in a queue");
1489 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1490     "Maximum number of messages in the system");
1491 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1492     "Size of a message segment");
1493 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1494     "Number of message segments");
1495 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids,
1496     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
1497     NULL, 0, sysctl_msqids, "",
1498     "Array of struct msqid_kernel for each potential message queue");
1499 
1500 static int
1501 msg_prison_check(void *obj, void *data)
1502 {
1503 	struct prison *pr = obj;
1504 	struct prison *prpr;
1505 	struct vfsoptlist *opts = data;
1506 	int error, jsys;
1507 
1508 	/*
1509 	 * sysvmsg is a jailsys integer.
1510 	 * It must be "disable" if the parent jail is disabled.
1511 	 */
1512 	error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1513 	if (error != ENOENT) {
1514 		if (error != 0)
1515 			return (error);
1516 		switch (jsys) {
1517 		case JAIL_SYS_DISABLE:
1518 			break;
1519 		case JAIL_SYS_NEW:
1520 		case JAIL_SYS_INHERIT:
1521 			prison_lock(pr->pr_parent);
1522 			prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1523 			prison_unlock(pr->pr_parent);
1524 			if (prpr == NULL)
1525 				return (EPERM);
1526 			break;
1527 		default:
1528 			return (EINVAL);
1529 		}
1530 	}
1531 
1532 	return (0);
1533 }
1534 
1535 static int
1536 msg_prison_set(void *obj, void *data)
1537 {
1538 	struct prison *pr = obj;
1539 	struct prison *tpr, *orpr, *nrpr, *trpr;
1540 	struct vfsoptlist *opts = data;
1541 	void *rsv;
1542 	int jsys, descend;
1543 
1544 	/*
1545 	 * sysvmsg controls which jail is the root of the associated msgs (this
1546 	 * jail or same as the parent), or if the feature is available at all.
1547 	 */
1548 	if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1549 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1550 		    ? JAIL_SYS_INHERIT
1551 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1552 		    ? JAIL_SYS_DISABLE
1553 		    : -1;
1554 	if (jsys == JAIL_SYS_DISABLE) {
1555 		prison_lock(pr);
1556 		orpr = osd_jail_get(pr, msg_prison_slot);
1557 		if (orpr != NULL)
1558 			osd_jail_del(pr, msg_prison_slot);
1559 		prison_unlock(pr);
1560 		if (orpr != NULL) {
1561 			if (orpr == pr)
1562 				msg_prison_cleanup(pr);
1563 			/* Disable all child jails as well. */
1564 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1565 				prison_lock(tpr);
1566 				trpr = osd_jail_get(tpr, msg_prison_slot);
1567 				if (trpr != NULL) {
1568 					osd_jail_del(tpr, msg_prison_slot);
1569 					prison_unlock(tpr);
1570 					if (trpr == tpr)
1571 						msg_prison_cleanup(tpr);
1572 				} else {
1573 					prison_unlock(tpr);
1574 					descend = 0;
1575 				}
1576 			}
1577 		}
1578 	} else if (jsys != -1) {
1579 		if (jsys == JAIL_SYS_NEW)
1580 			nrpr = pr;
1581 		else {
1582 			prison_lock(pr->pr_parent);
1583 			nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1584 			prison_unlock(pr->pr_parent);
1585 		}
1586 		rsv = osd_reserve(msg_prison_slot);
1587 		prison_lock(pr);
1588 		orpr = osd_jail_get(pr, msg_prison_slot);
1589 		if (orpr != nrpr)
1590 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1591 			    nrpr);
1592 		else
1593 			osd_free_reserved(rsv);
1594 		prison_unlock(pr);
1595 		if (orpr != nrpr) {
1596 			if (orpr == pr)
1597 				msg_prison_cleanup(pr);
1598 			if (orpr != NULL) {
1599 				/* Change child jails matching the old root, */
1600 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1601 					prison_lock(tpr);
1602 					trpr = osd_jail_get(tpr,
1603 					    msg_prison_slot);
1604 					if (trpr == orpr) {
1605 						(void)osd_jail_set(tpr,
1606 						    msg_prison_slot, nrpr);
1607 						prison_unlock(tpr);
1608 						if (trpr == tpr)
1609 							msg_prison_cleanup(tpr);
1610 					} else {
1611 						prison_unlock(tpr);
1612 						descend = 0;
1613 					}
1614 				}
1615 			}
1616 		}
1617 	}
1618 
1619 	return (0);
1620 }
1621 
1622 static int
1623 msg_prison_get(void *obj, void *data)
1624 {
1625 	struct prison *pr = obj;
1626 	struct prison *rpr;
1627 	struct vfsoptlist *opts = data;
1628 	int error, jsys;
1629 
1630 	/* Set sysvmsg based on the jail's root prison. */
1631 	prison_lock(pr);
1632 	rpr = osd_jail_get(pr, msg_prison_slot);
1633 	prison_unlock(pr);
1634 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1635 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1636 	error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1637 	if (error == ENOENT)
1638 		error = 0;
1639 	return (error);
1640 }
1641 
1642 static int
1643 msg_prison_remove(void *obj, void *data __unused)
1644 {
1645 	struct prison *pr = obj;
1646 	struct prison *rpr;
1647 
1648 	prison_lock(pr);
1649 	rpr = osd_jail_get(pr, msg_prison_slot);
1650 	prison_unlock(pr);
1651 	if (rpr == pr)
1652 		msg_prison_cleanup(pr);
1653 	return (0);
1654 }
1655 
1656 static void
1657 msg_prison_cleanup(struct prison *pr)
1658 {
1659 	struct msqid_kernel *msqkptr;
1660 	int i;
1661 
1662 	/* Remove any msqs that belong to this jail. */
1663 	mtx_lock(&msq_mtx);
1664 	for (i = 0; i < msginfo.msgmni; i++) {
1665 		msqkptr = &msqids[i];
1666 		if (msqkptr->u.msg_qbytes != 0 &&
1667 		    msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1668 			msq_remove(msqkptr);
1669 	}
1670 	mtx_unlock(&msq_mtx);
1671 }
1672 
1673 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1674 
1675 #ifdef COMPAT_FREEBSD32
1676 int
1677 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1678 {
1679 
1680 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1681     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1682 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1683 	switch (uap->which) {
1684 	case 0:
1685 		return (freebsd7_freebsd32_msgctl(td,
1686 		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1687 	case 2:
1688 		return (freebsd32_msgsnd(td,
1689 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1690 	case 3:
1691 		return (freebsd32_msgrcv(td,
1692 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1693 	default:
1694 		return (sys_msgsys(td, (struct msgsys_args *)uap));
1695 	}
1696 #else
1697 	return (nosys(td, NULL));
1698 #endif
1699 }
1700 
1701 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1702     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1703 int
1704 freebsd7_freebsd32_msgctl(struct thread *td,
1705     struct freebsd7_freebsd32_msgctl_args *uap)
1706 {
1707 	struct msqid_ds msqbuf;
1708 	struct msqid_ds32_old msqbuf32;
1709 	int error;
1710 
1711 	if (uap->cmd == IPC_SET) {
1712 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1713 		if (error)
1714 			return (error);
1715 		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1716 		PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1717 		PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1718 		CP(msqbuf32, msqbuf, msg_cbytes);
1719 		CP(msqbuf32, msqbuf, msg_qnum);
1720 		CP(msqbuf32, msqbuf, msg_qbytes);
1721 		CP(msqbuf32, msqbuf, msg_lspid);
1722 		CP(msqbuf32, msqbuf, msg_lrpid);
1723 		CP(msqbuf32, msqbuf, msg_stime);
1724 		CP(msqbuf32, msqbuf, msg_rtime);
1725 		CP(msqbuf32, msqbuf, msg_ctime);
1726 	}
1727 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1728 	if (error)
1729 		return (error);
1730 	if (uap->cmd == IPC_STAT) {
1731 		bzero(&msqbuf32, sizeof(msqbuf32));
1732 		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1733 		PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1734 		PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1735 		CP(msqbuf, msqbuf32, msg_cbytes);
1736 		CP(msqbuf, msqbuf32, msg_qnum);
1737 		CP(msqbuf, msqbuf32, msg_qbytes);
1738 		CP(msqbuf, msqbuf32, msg_lspid);
1739 		CP(msqbuf, msqbuf32, msg_lrpid);
1740 		CP(msqbuf, msqbuf32, msg_stime);
1741 		CP(msqbuf, msqbuf32, msg_rtime);
1742 		CP(msqbuf, msqbuf32, msg_ctime);
1743 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1744 	}
1745 	return (error);
1746 }
1747 #endif
1748 
1749 int
1750 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1751 {
1752 	struct msqid_ds msqbuf;
1753 	struct msqid_ds32 msqbuf32;
1754 	int error;
1755 
1756 	if (uap->cmd == IPC_SET) {
1757 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1758 		if (error)
1759 			return (error);
1760 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1761 		PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1762 		PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1763 		CP(msqbuf32, msqbuf, msg_cbytes);
1764 		CP(msqbuf32, msqbuf, msg_qnum);
1765 		CP(msqbuf32, msqbuf, msg_qbytes);
1766 		CP(msqbuf32, msqbuf, msg_lspid);
1767 		CP(msqbuf32, msqbuf, msg_lrpid);
1768 		CP(msqbuf32, msqbuf, msg_stime);
1769 		CP(msqbuf32, msqbuf, msg_rtime);
1770 		CP(msqbuf32, msqbuf, msg_ctime);
1771 	}
1772 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1773 	if (error)
1774 		return (error);
1775 	if (uap->cmd == IPC_STAT) {
1776 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1777 		PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1778 		PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1779 		CP(msqbuf, msqbuf32, msg_cbytes);
1780 		CP(msqbuf, msqbuf32, msg_qnum);
1781 		CP(msqbuf, msqbuf32, msg_qbytes);
1782 		CP(msqbuf, msqbuf32, msg_lspid);
1783 		CP(msqbuf, msqbuf32, msg_lrpid);
1784 		CP(msqbuf, msqbuf32, msg_stime);
1785 		CP(msqbuf, msqbuf32, msg_rtime);
1786 		CP(msqbuf, msqbuf32, msg_ctime);
1787 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1788 	}
1789 	return (error);
1790 }
1791 
1792 int
1793 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1794 {
1795 	const void *msgp;
1796 	long mtype;
1797 	int32_t mtype32;
1798 	int error;
1799 
1800 	msgp = PTRIN(uap->msgp);
1801 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1802 		return (error);
1803 	mtype = mtype32;
1804 	return (kern_msgsnd(td, uap->msqid,
1805 	    (const char *)msgp + sizeof(mtype32),
1806 	    uap->msgsz, uap->msgflg, mtype));
1807 }
1808 
1809 int
1810 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1811 {
1812 	void *msgp;
1813 	long mtype;
1814 	int32_t mtype32;
1815 	int error;
1816 
1817 	msgp = PTRIN(uap->msgp);
1818 	if ((error = kern_msgrcv(td, uap->msqid,
1819 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1820 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1821 		return (error);
1822 	mtype32 = (int32_t)mtype;
1823 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1824 }
1825 #endif
1826 
1827 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1828     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1829 
1830 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1831 static sy_call_t *msgcalls[] = {
1832 	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1833 	(sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1834 };
1835 
1836 /*
1837  * Entry point for all MSG calls.
1838  *
1839  * XXX actually varargs.
1840  * struct msgsys_args {
1841  *		int	which;
1842  *		int	a2;
1843  *		int	a3;
1844  *		int	a4;
1845  *		int	a5;
1846  *		int	a6;
1847  *	} *uap;
1848  */
1849 int
1850 sys_msgsys(struct thread *td, struct msgsys_args *uap)
1851 {
1852 	int error;
1853 
1854 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1855 	if (uap->which < 0 || uap->which >= nitems(msgcalls))
1856 		return (EINVAL);
1857 	error = (*msgcalls[uap->which])(td, &uap->a2);
1858 	return (error);
1859 }
1860 
1861 #ifndef CP
1862 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1863 #endif
1864 
1865 #ifndef _SYS_SYSPROTO_H_
1866 struct freebsd7_msgctl_args {
1867 	int	msqid;
1868 	int	cmd;
1869 	struct	msqid_ds_old *buf;
1870 };
1871 #endif
1872 int
1873 freebsd7_msgctl(struct thread *td, struct freebsd7_msgctl_args *uap)
1874 {
1875 	struct msqid_ds_old msqold;
1876 	struct msqid_ds msqbuf;
1877 	int error;
1878 
1879 	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1880 	    uap->buf));
1881 	if (uap->cmd == IPC_SET) {
1882 		error = copyin(uap->buf, &msqold, sizeof(msqold));
1883 		if (error)
1884 			return (error);
1885 		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1886 		CP(msqold, msqbuf, __msg_first);
1887 		CP(msqold, msqbuf, __msg_last);
1888 		CP(msqold, msqbuf, msg_cbytes);
1889 		CP(msqold, msqbuf, msg_qnum);
1890 		CP(msqold, msqbuf, msg_qbytes);
1891 		CP(msqold, msqbuf, msg_lspid);
1892 		CP(msqold, msqbuf, msg_lrpid);
1893 		CP(msqold, msqbuf, msg_stime);
1894 		CP(msqold, msqbuf, msg_rtime);
1895 		CP(msqold, msqbuf, msg_ctime);
1896 	}
1897 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1898 	if (error)
1899 		return (error);
1900 	if (uap->cmd == IPC_STAT) {
1901 		bzero(&msqold, sizeof(msqold));
1902 		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1903 		CP(msqbuf, msqold, __msg_first);
1904 		CP(msqbuf, msqold, __msg_last);
1905 		CP(msqbuf, msqold, msg_cbytes);
1906 		CP(msqbuf, msqold, msg_qnum);
1907 		CP(msqbuf, msqold, msg_qbytes);
1908 		CP(msqbuf, msqold, msg_lspid);
1909 		CP(msqbuf, msqold, msg_lrpid);
1910 		CP(msqbuf, msqold, msg_stime);
1911 		CP(msqbuf, msqold, msg_rtime);
1912 		CP(msqbuf, msqold, msg_ctime);
1913 		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1914 	}
1915 	return (error);
1916 }
1917 
1918 #undef CP
1919 
1920 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1921 	   COMPAT_FREEBSD7 */
1922