xref: /freebsd/sys/kern/sysv_msg.c (revision f5ef5f675d9d9eb6e35ed9142d70ecf774456ad4)
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(struct msg *msghdr)
390 {
391 	while (msghdr->msg_ts > 0) {
392 		short next;
393 		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
394 			panic("msghdr->msg_spot out of range");
395 		next = msgmaps[msghdr->msg_spot].next;
396 		msgmaps[msghdr->msg_spot].next = free_msgmaps;
397 		free_msgmaps = msghdr->msg_spot;
398 		nfree_msgmaps++;
399 		msghdr->msg_spot = next;
400 		if (msghdr->msg_ts >= msginfo.msgssz)
401 			msghdr->msg_ts -= msginfo.msgssz;
402 		else
403 			msghdr->msg_ts = 0;
404 	}
405 	if (msghdr->msg_spot != -1)
406 		panic("msghdr->msg_spot != -1");
407 	msghdr->msg_next = free_msghdrs;
408 	free_msghdrs = msghdr;
409 #ifdef MAC
410 	mac_sysvmsg_cleanup(msghdr);
411 #endif
412 }
413 
414 static void
415 msq_remove(struct msqid_kernel *msqkptr)
416 {
417 	struct msg *msghdr;
418 
419 	racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
420 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
421 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
422 	crfree(msqkptr->cred);
423 	msqkptr->cred = NULL;
424 
425 	/* Free the message headers */
426 	msghdr = msqkptr->u.__msg_first;
427 	while (msghdr != NULL) {
428 		struct msg *msghdr_tmp;
429 
430 		/* Free the segments of each message */
431 		msqkptr->u.msg_cbytes -= msghdr->msg_ts;
432 		msqkptr->u.msg_qnum--;
433 		msghdr_tmp = msghdr;
434 		msghdr = msghdr->msg_next;
435 		msg_freehdr(msghdr_tmp);
436 	}
437 
438 	if (msqkptr->u.msg_cbytes != 0)
439 		panic("msg_cbytes is screwed up");
440 	if (msqkptr->u.msg_qnum != 0)
441 		panic("msg_qnum is screwed up");
442 
443 	msqkptr->u.msg_qbytes = 0;	/* Mark it as free */
444 
445 #ifdef MAC
446 	mac_sysvmsq_cleanup(msqkptr);
447 #endif
448 
449 	wakeup(msqkptr);
450 }
451 
452 static struct prison *
453 msg_find_prison(struct ucred *cred)
454 {
455 	struct prison *pr, *rpr;
456 
457 	pr = cred->cr_prison;
458 	prison_lock(pr);
459 	rpr = osd_jail_get(pr, msg_prison_slot);
460 	prison_unlock(pr);
461 	return rpr;
462 }
463 
464 static int
465 msq_prison_cansee(struct prison *rpr, struct msqid_kernel *msqkptr)
466 {
467 
468 	if (msqkptr->cred == NULL ||
469 	    !(rpr == msqkptr->cred->cr_prison ||
470 	      prison_ischild(rpr, msqkptr->cred->cr_prison)))
471 		return (EINVAL);
472 	return (0);
473 }
474 
475 #ifndef _SYS_SYSPROTO_H_
476 struct msgctl_args {
477 	int	msqid;
478 	int	cmd;
479 	struct	msqid_ds *buf;
480 };
481 #endif
482 int
483 sys_msgctl(struct thread *td, struct msgctl_args *uap)
484 {
485 	int msqid = uap->msqid;
486 	int cmd = uap->cmd;
487 	struct msqid_ds msqbuf;
488 	int error;
489 
490 	DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
491 	if (cmd == IPC_SET &&
492 	    (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
493 		return (error);
494 	error = kern_msgctl(td, msqid, cmd, &msqbuf);
495 	if (cmd == IPC_STAT && error == 0)
496 		error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
497 	return (error);
498 }
499 
500 int
501 kern_msgctl(struct thread *td, int msqid, int cmd, struct msqid_ds *msqbuf)
502 {
503 	int rval, error, msqix;
504 	struct msqid_kernel *msqkptr;
505 	struct prison *rpr;
506 
507 	rpr = msg_find_prison(td->td_ucred);
508 	if (rpr == NULL)
509 		return (ENOSYS);
510 
511 	AUDIT_ARG_SVIPC_CMD(cmd);
512 	AUDIT_ARG_SVIPC_ID(msqid);
513 	msqix = IPCID_TO_IX(msqid);
514 
515 	if (msqix < 0 || msqix >= msginfo.msgmni) {
516 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
517 		    msginfo.msgmni));
518 		return (EINVAL);
519 	}
520 
521 	msqkptr = &msqids[msqix];
522 
523 	mtx_lock(&msq_mtx);
524 	if (msqkptr->u.msg_qbytes == 0) {
525 		DPRINTF(("no such msqid\n"));
526 		error = EINVAL;
527 		goto done2;
528 	}
529 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
530 		DPRINTF(("wrong sequence number\n"));
531 		error = EINVAL;
532 		goto done2;
533 	}
534 
535 	error = msq_prison_cansee(rpr, msqkptr);
536 	if (error != 0) {
537 		DPRINTF(("requester can't see prison\n"));
538 		goto done2;
539 	}
540 
541 #ifdef MAC
542 	error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
543 	if (error != 0)
544 		goto done2;
545 #endif
546 
547 	error = 0;
548 	rval = 0;
549 
550 	switch (cmd) {
551 
552 	case IPC_RMID:
553 	{
554 #ifdef MAC
555 		struct msg *msghdr;
556 #endif
557 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
558 			goto done2;
559 
560 #ifdef MAC
561 		/*
562 		 * Check that the thread has MAC access permissions to
563 		 * individual msghdrs.  Note: We need to do this in a
564 		 * separate loop because the actual loop alters the
565 		 * msq/msghdr info as it progresses, and there is no going
566 		 * back if half the way through we discover that the
567 		 * thread cannot free a certain msghdr.  The msq will get
568 		 * into an inconsistent state.
569 		 */
570 		for (msghdr = msqkptr->u.__msg_first; msghdr != NULL;
571 		    msghdr = msghdr->msg_next) {
572 			error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
573 			if (error != 0)
574 				goto done2;
575 		}
576 #endif
577 
578 		msq_remove(msqkptr);
579 	}
580 
581 		break;
582 
583 	case IPC_SET:
584 		AUDIT_ARG_SVIPC_PERM(&msqbuf->msg_perm);
585 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
586 			goto done2;
587 		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
588 			error = priv_check(td, PRIV_IPC_MSGSIZE);
589 			if (error)
590 				goto done2;
591 		}
592 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
593 			DPRINTF(("can't increase msg_qbytes beyond %d"
594 			    "(truncating)\n", msginfo.msgmnb));
595 			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
596 		}
597 		if (msqbuf->msg_qbytes == 0) {
598 			DPRINTF(("can't reduce msg_qbytes to 0\n"));
599 			error = EINVAL;		/* non-standard errno! */
600 			goto done2;
601 		}
602 		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
603 		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
604 		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
605 		    (msqbuf->msg_perm.mode & 0777);
606 		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
607 		msqkptr->u.msg_ctime = time_second;
608 		break;
609 
610 	case IPC_STAT:
611 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
612 			DPRINTF(("requester doesn't have read access\n"));
613 			goto done2;
614 		}
615 		*msqbuf = msqkptr->u;
616 		if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
617 			msqbuf->msg_perm.key = IPC_PRIVATE;
618 		break;
619 
620 	default:
621 		DPRINTF(("invalid command %d\n", cmd));
622 		error = EINVAL;
623 		goto done2;
624 	}
625 
626 	if (error == 0)
627 		td->td_retval[0] = rval;
628 done2:
629 	mtx_unlock(&msq_mtx);
630 	return (error);
631 }
632 
633 #ifndef _SYS_SYSPROTO_H_
634 struct msgget_args {
635 	key_t	key;
636 	int	msgflg;
637 };
638 #endif
639 
640 int
641 sys_msgget(struct thread *td, struct msgget_args *uap)
642 {
643 	int msqid, error = 0;
644 	int key = uap->key;
645 	int msgflg = uap->msgflg;
646 	struct ucred *cred = td->td_ucred;
647 	struct msqid_kernel *msqkptr = NULL;
648 
649 	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
650 
651 	if (msg_find_prison(cred) == NULL)
652 		return (ENOSYS);
653 
654 	mtx_lock(&msq_mtx);
655 	if (key != IPC_PRIVATE) {
656 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
657 			msqkptr = &msqids[msqid];
658 			if (msqkptr->u.msg_qbytes != 0 &&
659 			    msqkptr->cred != NULL &&
660 			    msqkptr->cred->cr_prison == cred->cr_prison &&
661 			    msqkptr->u.msg_perm.key == key)
662 				break;
663 		}
664 		if (msqid < msginfo.msgmni) {
665 			DPRINTF(("found public key\n"));
666 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
667 				DPRINTF(("not exclusive\n"));
668 				error = EEXIST;
669 				goto done2;
670 			}
671 			AUDIT_ARG_SVIPC_ID(IXSEQ_TO_IPCID(msqid,
672 			    msqkptr->u.msg_perm));
673 			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
674 			    msgflg & 0700))) {
675 				DPRINTF(("requester doesn't have 0%o access\n",
676 				    msgflg & 0700));
677 				goto done2;
678 			}
679 #ifdef MAC
680 			error = mac_sysvmsq_check_msqget(cred, msqkptr);
681 			if (error != 0)
682 				goto done2;
683 #endif
684 			goto found;
685 		}
686 	}
687 
688 	DPRINTF(("need to allocate the msqid_ds\n"));
689 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
690 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
691 			/*
692 			 * Look for an unallocated and unlocked msqid_ds.
693 			 * msqid_ds's can be locked by msgsnd or msgrcv while
694 			 * they are copying the message in/out.  We can't
695 			 * re-use the entry until they release it.
696 			 */
697 			msqkptr = &msqids[msqid];
698 			if (msqkptr->u.msg_qbytes == 0 &&
699 			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
700 				break;
701 		}
702 		if (msqid == msginfo.msgmni) {
703 			DPRINTF(("no more msqid_ds's available\n"));
704 			error = ENOSPC;
705 			goto done2;
706 		}
707 #ifdef RACCT
708 		if (racct_enable) {
709 			PROC_LOCK(td->td_proc);
710 			error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
711 			PROC_UNLOCK(td->td_proc);
712 			if (error != 0) {
713 				error = ENOSPC;
714 				goto done2;
715 			}
716 		}
717 #endif
718 		DPRINTF(("msqid %d is available\n", msqid));
719 		msqkptr->u.msg_perm.key = key;
720 		msqkptr->u.msg_perm.cuid = cred->cr_uid;
721 		msqkptr->u.msg_perm.uid = cred->cr_uid;
722 		msqkptr->u.msg_perm.cgid = cred->cr_gid;
723 		msqkptr->u.msg_perm.gid = cred->cr_gid;
724 		msqkptr->u.msg_perm.mode = (msgflg & 0777);
725 		msqkptr->cred = crhold(cred);
726 		/* Make sure that the returned msqid is unique */
727 		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
728 		msqkptr->u.__msg_first = NULL;
729 		msqkptr->u.__msg_last = NULL;
730 		msqkptr->u.msg_cbytes = 0;
731 		msqkptr->u.msg_qnum = 0;
732 		msqkptr->u.msg_qbytes = msginfo.msgmnb;
733 		msqkptr->u.msg_lspid = 0;
734 		msqkptr->u.msg_lrpid = 0;
735 		msqkptr->u.msg_stime = 0;
736 		msqkptr->u.msg_rtime = 0;
737 		msqkptr->u.msg_ctime = time_second;
738 #ifdef MAC
739 		mac_sysvmsq_create(cred, msqkptr);
740 #endif
741 		AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
742 	} else {
743 		DPRINTF(("didn't find it and wasn't asked to create it\n"));
744 		error = ENOENT;
745 		goto done2;
746 	}
747 
748 found:
749 	/* Construct the unique msqid */
750 	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
751 done2:
752 	mtx_unlock(&msq_mtx);
753 	return (error);
754 }
755 
756 #ifndef _SYS_SYSPROTO_H_
757 struct msgsnd_args {
758 	int	msqid;
759 	const void	*msgp;	/* XXX msgp is actually mtext. */
760 	size_t	msgsz;
761 	int	msgflg;
762 };
763 #endif
764 int
765 kern_msgsnd(struct thread *td, int msqid, const void *msgp,
766     size_t msgsz, int msgflg, long mtype)
767 {
768 	int msqix, segs_needed, error = 0;
769 	struct msqid_kernel *msqkptr;
770 	struct msg *msghdr;
771 	struct prison *rpr;
772 	short next;
773 #ifdef RACCT
774 	size_t saved_msgsz = 0;
775 #endif
776 
777 	rpr = msg_find_prison(td->td_ucred);
778 	if (rpr == NULL)
779 		return (ENOSYS);
780 
781 	mtx_lock(&msq_mtx);
782 	AUDIT_ARG_SVIPC_ID(msqid);
783 	msqix = IPCID_TO_IX(msqid);
784 
785 	if (msqix < 0 || msqix >= msginfo.msgmni) {
786 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
787 		    msginfo.msgmni));
788 		error = EINVAL;
789 		goto done2;
790 	}
791 
792 	msqkptr = &msqids[msqix];
793 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
794 	if (msqkptr->u.msg_qbytes == 0) {
795 		DPRINTF(("no such message queue id\n"));
796 		error = EINVAL;
797 		goto done2;
798 	}
799 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
800 		DPRINTF(("wrong sequence number\n"));
801 		error = EINVAL;
802 		goto done2;
803 	}
804 
805 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
806 		DPRINTF(("requester can't see prison\n"));
807 		goto done2;
808 	}
809 
810 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
811 		DPRINTF(("requester doesn't have write access\n"));
812 		goto done2;
813 	}
814 
815 #ifdef MAC
816 	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
817 	if (error != 0)
818 		goto done2;
819 #endif
820 
821 #ifdef RACCT
822 	if (racct_enable) {
823 		PROC_LOCK(td->td_proc);
824 		if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
825 			PROC_UNLOCK(td->td_proc);
826 			error = EAGAIN;
827 			goto done2;
828 		}
829 		saved_msgsz = msgsz;
830 		if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
831 			racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
832 			PROC_UNLOCK(td->td_proc);
833 			error = EAGAIN;
834 			goto done2;
835 		}
836 		PROC_UNLOCK(td->td_proc);
837 	}
838 #endif
839 
840 	segs_needed = howmany(msgsz, msginfo.msgssz);
841 	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
842 	    msginfo.msgssz, segs_needed));
843 	for (;;) {
844 		int need_more_resources = 0;
845 
846 		/*
847 		 * check msgsz
848 		 * (inside this loop in case msg_qbytes changes while we sleep)
849 		 */
850 
851 		if (msgsz > msqkptr->u.msg_qbytes) {
852 			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
853 			error = EINVAL;
854 			goto done3;
855 		}
856 
857 		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
858 			DPRINTF(("msqid is locked\n"));
859 			need_more_resources = 1;
860 		}
861 		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
862 			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
863 			need_more_resources = 1;
864 		}
865 		if (segs_needed > nfree_msgmaps) {
866 			DPRINTF(("segs_needed > nfree_msgmaps\n"));
867 			need_more_resources = 1;
868 		}
869 		if (free_msghdrs == NULL) {
870 			DPRINTF(("no more msghdrs\n"));
871 			need_more_resources = 1;
872 		}
873 
874 		if (need_more_resources) {
875 			int we_own_it;
876 
877 			if ((msgflg & IPC_NOWAIT) != 0) {
878 				DPRINTF(("need more resources but caller "
879 				    "doesn't want to wait\n"));
880 				error = EAGAIN;
881 				goto done3;
882 			}
883 
884 			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
885 				DPRINTF(("we don't own the msqid_ds\n"));
886 				we_own_it = 0;
887 			} else {
888 				/* Force later arrivals to wait for our
889 				   request */
890 				DPRINTF(("we own the msqid_ds\n"));
891 				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
892 				we_own_it = 1;
893 			}
894 			DPRINTF(("msgsnd:  goodnight\n"));
895 			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
896 			    "msgsnd", hz);
897 			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
898 			if (we_own_it)
899 				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
900 			if (error == EWOULDBLOCK) {
901 				DPRINTF(("msgsnd:  timed out\n"));
902 				continue;
903 			}
904 			if (error != 0) {
905 				DPRINTF(("msgsnd:  interrupted system call\n"));
906 				error = EINTR;
907 				goto done3;
908 			}
909 
910 			/*
911 			 * Make sure that the msq queue still exists
912 			 */
913 
914 			if (msqkptr->u.msg_qbytes == 0) {
915 				DPRINTF(("msqid deleted\n"));
916 				error = EIDRM;
917 				goto done3;
918 			}
919 
920 		} else {
921 			DPRINTF(("got all the resources that we need\n"));
922 			break;
923 		}
924 	}
925 
926 	/*
927 	 * We have the resources that we need.
928 	 * Make sure!
929 	 */
930 
931 	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
932 		panic("msg_perm.mode & MSG_LOCKED");
933 	if (segs_needed > nfree_msgmaps)
934 		panic("segs_needed > nfree_msgmaps");
935 	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
936 		panic("msgsz + msg_cbytes > msg_qbytes");
937 	if (free_msghdrs == NULL)
938 		panic("no more msghdrs");
939 
940 	/*
941 	 * Re-lock the msqid_ds in case we page-fault when copying in the
942 	 * message
943 	 */
944 
945 	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
946 		panic("msqid_ds is already locked");
947 	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
948 
949 	/*
950 	 * Allocate a message header
951 	 */
952 
953 	msghdr = free_msghdrs;
954 	free_msghdrs = msghdr->msg_next;
955 	msghdr->msg_spot = -1;
956 	msghdr->msg_ts = msgsz;
957 	msghdr->msg_type = mtype;
958 #ifdef MAC
959 	/*
960 	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
961 	 * immediately?  Or, should it be checked just before the msg is
962 	 * enqueued in the msgq (as it is done now)?
963 	 */
964 	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
965 #endif
966 
967 	/*
968 	 * Allocate space for the message
969 	 */
970 
971 	while (segs_needed > 0) {
972 		if (nfree_msgmaps <= 0)
973 			panic("not enough msgmaps");
974 		if (free_msgmaps == -1)
975 			panic("nil free_msgmaps");
976 		next = free_msgmaps;
977 		if (next <= -1)
978 			panic("next too low #1");
979 		if (next >= msginfo.msgseg)
980 			panic("next out of range #1");
981 		DPRINTF(("allocating segment %d to message\n", next));
982 		free_msgmaps = msgmaps[next].next;
983 		nfree_msgmaps--;
984 		msgmaps[next].next = msghdr->msg_spot;
985 		msghdr->msg_spot = next;
986 		segs_needed--;
987 	}
988 
989 	/*
990 	 * Validate the message type
991 	 */
992 
993 	if (msghdr->msg_type < 1) {
994 		msg_freehdr(msghdr);
995 		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
996 		wakeup(msqkptr);
997 		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
998 		error = EINVAL;
999 		goto done3;
1000 	}
1001 
1002 	/*
1003 	 * Copy in the message body
1004 	 */
1005 
1006 	next = msghdr->msg_spot;
1007 	while (msgsz > 0) {
1008 		size_t tlen;
1009 		if (msgsz > msginfo.msgssz)
1010 			tlen = msginfo.msgssz;
1011 		else
1012 			tlen = msgsz;
1013 		if (next <= -1)
1014 			panic("next too low #2");
1015 		if (next >= msginfo.msgseg)
1016 			panic("next out of range #2");
1017 		mtx_unlock(&msq_mtx);
1018 		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1019 		    tlen)) != 0) {
1020 			mtx_lock(&msq_mtx);
1021 			DPRINTF(("error %d copying in message segment\n",
1022 			    error));
1023 			msg_freehdr(msghdr);
1024 			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1025 			wakeup(msqkptr);
1026 			goto done3;
1027 		}
1028 		mtx_lock(&msq_mtx);
1029 		msgsz -= tlen;
1030 		msgp = (const char *)msgp + tlen;
1031 		next = msgmaps[next].next;
1032 	}
1033 	if (next != -1)
1034 		panic("didn't use all the msg segments");
1035 
1036 	/*
1037 	 * We've got the message.  Unlock the msqid_ds.
1038 	 */
1039 
1040 	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1041 
1042 	/*
1043 	 * Make sure that the msqid_ds is still allocated.
1044 	 */
1045 
1046 	if (msqkptr->u.msg_qbytes == 0) {
1047 		msg_freehdr(msghdr);
1048 		wakeup(msqkptr);
1049 		error = EIDRM;
1050 		goto done3;
1051 	}
1052 
1053 #ifdef MAC
1054 	/*
1055 	 * Note: Since the task/thread allocates the msghdr and usually
1056 	 * primes it with its own MAC label, for a majority of policies, it
1057 	 * won't be necessary to check whether the msghdr has access
1058 	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
1059 	 * suffice in that case.  However, this hook may be required where
1060 	 * individual policies derive a non-identical label for the msghdr
1061 	 * from the current thread label and may want to check the msghdr
1062 	 * enqueue permissions, along with read/write permissions to the
1063 	 * msgq.
1064 	 */
1065 	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1066 	if (error != 0) {
1067 		msg_freehdr(msghdr);
1068 		wakeup(msqkptr);
1069 		goto done3;
1070 	}
1071 #endif
1072 
1073 	/*
1074 	 * Put the message into the queue
1075 	 */
1076 	if (msqkptr->u.__msg_first == NULL) {
1077 		msqkptr->u.__msg_first = msghdr;
1078 		msqkptr->u.__msg_last = msghdr;
1079 	} else {
1080 		msqkptr->u.__msg_last->msg_next = msghdr;
1081 		msqkptr->u.__msg_last = msghdr;
1082 	}
1083 	msqkptr->u.__msg_last->msg_next = NULL;
1084 
1085 	msqkptr->u.msg_cbytes += msghdr->msg_ts;
1086 	msqkptr->u.msg_qnum++;
1087 	msqkptr->u.msg_lspid = td->td_proc->p_pid;
1088 	msqkptr->u.msg_stime = time_second;
1089 
1090 	wakeup(msqkptr);
1091 	td->td_retval[0] = 0;
1092 done3:
1093 #ifdef RACCT
1094 	if (racct_enable && error != 0) {
1095 		PROC_LOCK(td->td_proc);
1096 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1097 		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1098 		PROC_UNLOCK(td->td_proc);
1099 	}
1100 #endif
1101 done2:
1102 	mtx_unlock(&msq_mtx);
1103 	return (error);
1104 }
1105 
1106 int
1107 sys_msgsnd(struct thread *td, struct msgsnd_args *uap)
1108 {
1109 	int error;
1110 	long mtype;
1111 
1112 	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1113 	    uap->msgsz, uap->msgflg));
1114 
1115 	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1116 		DPRINTF(("error %d copying the message type\n", error));
1117 		return (error);
1118 	}
1119 	return (kern_msgsnd(td, uap->msqid,
1120 	    (const char *)uap->msgp + sizeof(mtype),
1121 	    uap->msgsz, uap->msgflg, mtype));
1122 }
1123 
1124 #ifndef _SYS_SYSPROTO_H_
1125 struct msgrcv_args {
1126 	int	msqid;
1127 	void	*msgp;
1128 	size_t	msgsz;
1129 	long	msgtyp;
1130 	int	msgflg;
1131 };
1132 #endif
1133 /* XXX msgp is actually mtext. */
1134 int
1135 kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp,
1136     int msgflg, long *mtype)
1137 {
1138 	size_t len;
1139 	struct msqid_kernel *msqkptr;
1140 	struct msg *msghdr;
1141 	struct prison *rpr;
1142 	int msqix, error = 0;
1143 	short next;
1144 
1145 	rpr = msg_find_prison(td->td_ucred);
1146 	if (rpr == NULL)
1147 		return (ENOSYS);
1148 
1149 	AUDIT_ARG_SVIPC_ID(msqid);
1150 	msqix = IPCID_TO_IX(msqid);
1151 
1152 	if (msqix < 0 || msqix >= msginfo.msgmni) {
1153 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1154 		    msginfo.msgmni));
1155 		return (EINVAL);
1156 	}
1157 
1158 	msqkptr = &msqids[msqix];
1159 	mtx_lock(&msq_mtx);
1160 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
1161 	if (msqkptr->u.msg_qbytes == 0) {
1162 		DPRINTF(("no such message queue id\n"));
1163 		error = EINVAL;
1164 		goto done2;
1165 	}
1166 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1167 		DPRINTF(("wrong sequence number\n"));
1168 		error = EINVAL;
1169 		goto done2;
1170 	}
1171 
1172 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
1173 		DPRINTF(("requester can't see prison\n"));
1174 		goto done2;
1175 	}
1176 
1177 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1178 		DPRINTF(("requester doesn't have read access\n"));
1179 		goto done2;
1180 	}
1181 
1182 #ifdef MAC
1183 	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1184 	if (error != 0)
1185 		goto done2;
1186 #endif
1187 
1188 	msghdr = NULL;
1189 	while (msghdr == NULL) {
1190 		if (msgtyp == 0) {
1191 			msghdr = msqkptr->u.__msg_first;
1192 			if (msghdr != NULL) {
1193 				if (msgsz < msghdr->msg_ts &&
1194 				    (msgflg & MSG_NOERROR) == 0) {
1195 					DPRINTF(("first message on the queue "
1196 					    "is too big (want %zu, got %d)\n",
1197 					    msgsz, msghdr->msg_ts));
1198 					error = E2BIG;
1199 					goto done2;
1200 				}
1201 #ifdef MAC
1202 				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1203 				    msghdr);
1204 				if (error != 0)
1205 					goto done2;
1206 #endif
1207 				if (msqkptr->u.__msg_first ==
1208 				    msqkptr->u.__msg_last) {
1209 					msqkptr->u.__msg_first = NULL;
1210 					msqkptr->u.__msg_last = NULL;
1211 				} else {
1212 					msqkptr->u.__msg_first = msghdr->msg_next;
1213 					if (msqkptr->u.__msg_first == NULL)
1214 						panic("msg_first/last screwed up #1");
1215 				}
1216 			}
1217 		} else {
1218 			struct msg *previous;
1219 			struct msg **prev;
1220 
1221 			previous = NULL;
1222 			prev = &(msqkptr->u.__msg_first);
1223 			while ((msghdr = *prev) != NULL) {
1224 				/*
1225 				 * Is this message's type an exact match or is
1226 				 * this message's type less than or equal to
1227 				 * the absolute value of a negative msgtyp?
1228 				 * Note that the second half of this test can
1229 				 * NEVER be true if msgtyp is positive since
1230 				 * msg_type is always positive!
1231 				 */
1232 
1233 				if (msgtyp == msghdr->msg_type ||
1234 				    msghdr->msg_type <= -msgtyp) {
1235 					DPRINTF(("found message type %ld, "
1236 					    "requested %ld\n",
1237 					    msghdr->msg_type, msgtyp));
1238 					if (msgsz < msghdr->msg_ts &&
1239 					    (msgflg & MSG_NOERROR) == 0) {
1240 						DPRINTF(("requested message "
1241 						    "on the queue is too big "
1242 						    "(want %zu, got %hu)\n",
1243 						    msgsz, msghdr->msg_ts));
1244 						error = E2BIG;
1245 						goto done2;
1246 					}
1247 #ifdef MAC
1248 					error = mac_sysvmsq_check_msgrcv(
1249 					    td->td_ucred, msghdr);
1250 					if (error != 0)
1251 						goto done2;
1252 #endif
1253 					*prev = msghdr->msg_next;
1254 					if (msghdr == msqkptr->u.__msg_last) {
1255 						if (previous == NULL) {
1256 							if (prev !=
1257 							    &msqkptr->u.__msg_first)
1258 								panic("__msg_first/last screwed up #2");
1259 							msqkptr->u.__msg_first =
1260 							    NULL;
1261 							msqkptr->u.__msg_last =
1262 							    NULL;
1263 						} else {
1264 							if (prev ==
1265 							    &msqkptr->u.__msg_first)
1266 								panic("__msg_first/last screwed up #3");
1267 							msqkptr->u.__msg_last =
1268 							    previous;
1269 						}
1270 					}
1271 					break;
1272 				}
1273 				previous = msghdr;
1274 				prev = &(msghdr->msg_next);
1275 			}
1276 		}
1277 
1278 		/*
1279 		 * We've either extracted the msghdr for the appropriate
1280 		 * message or there isn't one.
1281 		 * If there is one then bail out of this loop.
1282 		 */
1283 
1284 		if (msghdr != NULL)
1285 			break;
1286 
1287 		/*
1288 		 * Hmph!  No message found.  Does the user want to wait?
1289 		 */
1290 
1291 		if ((msgflg & IPC_NOWAIT) != 0) {
1292 			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1293 			    msgtyp));
1294 			/* The SVID says to return ENOMSG. */
1295 			error = ENOMSG;
1296 			goto done2;
1297 		}
1298 
1299 		/*
1300 		 * Wait for something to happen
1301 		 */
1302 
1303 		DPRINTF(("msgrcv:  goodnight\n"));
1304 		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1305 		    "msgrcv", 0);
1306 		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1307 
1308 		if (error != 0) {
1309 			DPRINTF(("msgrcv:  interrupted system call\n"));
1310 			error = EINTR;
1311 			goto done2;
1312 		}
1313 
1314 		/*
1315 		 * Make sure that the msq queue still exists
1316 		 */
1317 
1318 		if (msqkptr->u.msg_qbytes == 0 ||
1319 		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1320 			DPRINTF(("msqid deleted\n"));
1321 			error = EIDRM;
1322 			goto done2;
1323 		}
1324 	}
1325 
1326 	/*
1327 	 * Return the message to the user.
1328 	 *
1329 	 * First, do the bookkeeping (before we risk being interrupted).
1330 	 */
1331 
1332 	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1333 	msqkptr->u.msg_qnum--;
1334 	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1335 	msqkptr->u.msg_rtime = time_second;
1336 
1337 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1338 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1339 
1340 	/*
1341 	 * Make msgsz the actual amount that we'll be returning.
1342 	 * Note that this effectively truncates the message if it is too long
1343 	 * (since msgsz is never increased).
1344 	 */
1345 
1346 	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1347 	    msghdr->msg_ts));
1348 	if (msgsz > msghdr->msg_ts)
1349 		msgsz = msghdr->msg_ts;
1350 	*mtype = msghdr->msg_type;
1351 
1352 	/*
1353 	 * Return the segments to the user
1354 	 */
1355 
1356 	next = msghdr->msg_spot;
1357 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1358 		size_t tlen;
1359 
1360 		if (msgsz - len > msginfo.msgssz)
1361 			tlen = msginfo.msgssz;
1362 		else
1363 			tlen = msgsz - len;
1364 		if (next <= -1)
1365 			panic("next too low #3");
1366 		if (next >= msginfo.msgseg)
1367 			panic("next out of range #3");
1368 		mtx_unlock(&msq_mtx);
1369 		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1370 		mtx_lock(&msq_mtx);
1371 		if (error != 0) {
1372 			DPRINTF(("error (%d) copying out message segment\n",
1373 			    error));
1374 			msg_freehdr(msghdr);
1375 			wakeup(msqkptr);
1376 			goto done2;
1377 		}
1378 		msgp = (char *)msgp + tlen;
1379 		next = msgmaps[next].next;
1380 	}
1381 
1382 	/*
1383 	 * Done, return the actual number of bytes copied out.
1384 	 */
1385 
1386 	msg_freehdr(msghdr);
1387 	wakeup(msqkptr);
1388 	td->td_retval[0] = msgsz;
1389 done2:
1390 	mtx_unlock(&msq_mtx);
1391 	return (error);
1392 }
1393 
1394 int
1395 sys_msgrcv(struct thread *td, struct msgrcv_args *uap)
1396 {
1397 	int error;
1398 	long mtype;
1399 
1400 	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1401 	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1402 
1403 	if ((error = kern_msgrcv(td, uap->msqid,
1404 	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1405 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1406 		return (error);
1407 	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1408 		DPRINTF(("error %d copying the message type\n", error));
1409 	return (error);
1410 }
1411 
1412 static int
1413 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1414 {
1415 	struct msqid_kernel tmsqk;
1416 #ifdef COMPAT_FREEBSD32
1417 	struct msqid_kernel32 tmsqk32;
1418 #endif
1419 	struct prison *pr, *rpr;
1420 	void *outaddr;
1421 	size_t outsize;
1422 	int error, i;
1423 
1424 	pr = req->td->td_ucred->cr_prison;
1425 	rpr = msg_find_prison(req->td->td_ucred);
1426 	error = 0;
1427 	for (i = 0; i < msginfo.msgmni; i++) {
1428 		mtx_lock(&msq_mtx);
1429 		if (msqids[i].u.msg_qbytes == 0 || rpr == NULL ||
1430 		    msq_prison_cansee(rpr, &msqids[i]) != 0)
1431 			bzero(&tmsqk, sizeof(tmsqk));
1432 		else {
1433 			tmsqk = msqids[i];
1434 			if (tmsqk.cred->cr_prison != pr)
1435 				tmsqk.u.msg_perm.key = IPC_PRIVATE;
1436 		}
1437 		mtx_unlock(&msq_mtx);
1438 #ifdef COMPAT_FREEBSD32
1439 		if (SV_CURPROC_FLAG(SV_ILP32)) {
1440 			bzero(&tmsqk32, sizeof(tmsqk32));
1441 			freebsd32_ipcperm_out(&tmsqk.u.msg_perm,
1442 			    &tmsqk32.u.msg_perm);
1443 			/* Don't copy u.msg_first or u.msg_last */
1444 			CP(tmsqk, tmsqk32, u.msg_cbytes);
1445 			CP(tmsqk, tmsqk32, u.msg_qnum);
1446 			CP(tmsqk, tmsqk32, u.msg_qbytes);
1447 			CP(tmsqk, tmsqk32, u.msg_lspid);
1448 			CP(tmsqk, tmsqk32, u.msg_lrpid);
1449 			CP(tmsqk, tmsqk32, u.msg_stime);
1450 			CP(tmsqk, tmsqk32, u.msg_rtime);
1451 			CP(tmsqk, tmsqk32, u.msg_ctime);
1452 			/* Don't copy label or cred */
1453 			outaddr = &tmsqk32;
1454 			outsize = sizeof(tmsqk32);
1455 		} else
1456 #endif
1457 		{
1458 			/* Don't leak kernel pointers */
1459 			tmsqk.u.__msg_first = NULL;
1460 			tmsqk.u.__msg_last = NULL;
1461 			tmsqk.label = NULL;
1462 			tmsqk.cred = NULL;
1463 			/*
1464 			 * XXX: some padding also exists, but we take care to
1465 			 * allocate our pool of msqid_kernel structs with
1466 			 * zeroed memory so this should be OK.
1467 			 */
1468 			outaddr = &tmsqk;
1469 			outsize = sizeof(tmsqk);
1470 		}
1471 		error = SYSCTL_OUT(req, outaddr, outsize);
1472 		if (error != 0)
1473 			break;
1474 	}
1475 	return (error);
1476 }
1477 
1478 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1479     "Maximum message size");
1480 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1481     "Number of message queue identifiers");
1482 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1483     "Maximum number of bytes in a queue");
1484 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1485     "Maximum number of messages in the system");
1486 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1487     "Size of a message segment");
1488 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1489     "Number of message segments");
1490 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids,
1491     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
1492     NULL, 0, sysctl_msqids, "",
1493     "Array of struct msqid_kernel for each potential message queue");
1494 
1495 static int
1496 msg_prison_check(void *obj, void *data)
1497 {
1498 	struct prison *pr = obj;
1499 	struct prison *prpr;
1500 	struct vfsoptlist *opts = data;
1501 	int error, jsys;
1502 
1503 	/*
1504 	 * sysvmsg is a jailsys integer.
1505 	 * It must be "disable" if the parent jail is disabled.
1506 	 */
1507 	error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1508 	if (error != ENOENT) {
1509 		if (error != 0)
1510 			return (error);
1511 		switch (jsys) {
1512 		case JAIL_SYS_DISABLE:
1513 			break;
1514 		case JAIL_SYS_NEW:
1515 		case JAIL_SYS_INHERIT:
1516 			prison_lock(pr->pr_parent);
1517 			prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1518 			prison_unlock(pr->pr_parent);
1519 			if (prpr == NULL)
1520 				return (EPERM);
1521 			break;
1522 		default:
1523 			return (EINVAL);
1524 		}
1525 	}
1526 
1527 	return (0);
1528 }
1529 
1530 static int
1531 msg_prison_set(void *obj, void *data)
1532 {
1533 	struct prison *pr = obj;
1534 	struct prison *tpr, *orpr, *nrpr, *trpr;
1535 	struct vfsoptlist *opts = data;
1536 	void *rsv;
1537 	int jsys, descend;
1538 
1539 	/*
1540 	 * sysvmsg controls which jail is the root of the associated msgs (this
1541 	 * jail or same as the parent), or if the feature is available at all.
1542 	 */
1543 	if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1544 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1545 		    ? JAIL_SYS_INHERIT
1546 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1547 		    ? JAIL_SYS_DISABLE
1548 		    : -1;
1549 	if (jsys == JAIL_SYS_DISABLE) {
1550 		prison_lock(pr);
1551 		orpr = osd_jail_get(pr, msg_prison_slot);
1552 		if (orpr != NULL)
1553 			osd_jail_del(pr, msg_prison_slot);
1554 		prison_unlock(pr);
1555 		if (orpr != NULL) {
1556 			if (orpr == pr)
1557 				msg_prison_cleanup(pr);
1558 			/* Disable all child jails as well. */
1559 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1560 				prison_lock(tpr);
1561 				trpr = osd_jail_get(tpr, msg_prison_slot);
1562 				if (trpr != NULL) {
1563 					osd_jail_del(tpr, msg_prison_slot);
1564 					prison_unlock(tpr);
1565 					if (trpr == tpr)
1566 						msg_prison_cleanup(tpr);
1567 				} else {
1568 					prison_unlock(tpr);
1569 					descend = 0;
1570 				}
1571 			}
1572 		}
1573 	} else if (jsys != -1) {
1574 		if (jsys == JAIL_SYS_NEW)
1575 			nrpr = pr;
1576 		else {
1577 			prison_lock(pr->pr_parent);
1578 			nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1579 			prison_unlock(pr->pr_parent);
1580 		}
1581 		rsv = osd_reserve(msg_prison_slot);
1582 		prison_lock(pr);
1583 		orpr = osd_jail_get(pr, msg_prison_slot);
1584 		if (orpr != nrpr)
1585 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1586 			    nrpr);
1587 		else
1588 			osd_free_reserved(rsv);
1589 		prison_unlock(pr);
1590 		if (orpr != nrpr) {
1591 			if (orpr == pr)
1592 				msg_prison_cleanup(pr);
1593 			if (orpr != NULL) {
1594 				/* Change child jails matching the old root, */
1595 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1596 					prison_lock(tpr);
1597 					trpr = osd_jail_get(tpr,
1598 					    msg_prison_slot);
1599 					if (trpr == orpr) {
1600 						(void)osd_jail_set(tpr,
1601 						    msg_prison_slot, nrpr);
1602 						prison_unlock(tpr);
1603 						if (trpr == tpr)
1604 							msg_prison_cleanup(tpr);
1605 					} else {
1606 						prison_unlock(tpr);
1607 						descend = 0;
1608 					}
1609 				}
1610 			}
1611 		}
1612 	}
1613 
1614 	return (0);
1615 }
1616 
1617 static int
1618 msg_prison_get(void *obj, void *data)
1619 {
1620 	struct prison *pr = obj;
1621 	struct prison *rpr;
1622 	struct vfsoptlist *opts = data;
1623 	int error, jsys;
1624 
1625 	/* Set sysvmsg based on the jail's root prison. */
1626 	prison_lock(pr);
1627 	rpr = osd_jail_get(pr, msg_prison_slot);
1628 	prison_unlock(pr);
1629 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1630 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1631 	error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1632 	if (error == ENOENT)
1633 		error = 0;
1634 	return (error);
1635 }
1636 
1637 static int
1638 msg_prison_remove(void *obj, void *data __unused)
1639 {
1640 	struct prison *pr = obj;
1641 	struct prison *rpr;
1642 
1643 	prison_lock(pr);
1644 	rpr = osd_jail_get(pr, msg_prison_slot);
1645 	prison_unlock(pr);
1646 	if (rpr == pr)
1647 		msg_prison_cleanup(pr);
1648 	return (0);
1649 }
1650 
1651 static void
1652 msg_prison_cleanup(struct prison *pr)
1653 {
1654 	struct msqid_kernel *msqkptr;
1655 	int i;
1656 
1657 	/* Remove any msqs that belong to this jail. */
1658 	mtx_lock(&msq_mtx);
1659 	for (i = 0; i < msginfo.msgmni; i++) {
1660 		msqkptr = &msqids[i];
1661 		if (msqkptr->u.msg_qbytes != 0 &&
1662 		    msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1663 			msq_remove(msqkptr);
1664 	}
1665 	mtx_unlock(&msq_mtx);
1666 }
1667 
1668 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1669 
1670 #ifdef COMPAT_FREEBSD32
1671 int
1672 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1673 {
1674 
1675 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1676     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1677 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1678 	switch (uap->which) {
1679 	case 0:
1680 		return (freebsd7_freebsd32_msgctl(td,
1681 		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1682 	case 2:
1683 		return (freebsd32_msgsnd(td,
1684 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1685 	case 3:
1686 		return (freebsd32_msgrcv(td,
1687 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1688 	default:
1689 		return (sys_msgsys(td, (struct msgsys_args *)uap));
1690 	}
1691 #else
1692 	return (nosys(td, NULL));
1693 #endif
1694 }
1695 
1696 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1697     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1698 int
1699 freebsd7_freebsd32_msgctl(struct thread *td,
1700     struct freebsd7_freebsd32_msgctl_args *uap)
1701 {
1702 	struct msqid_ds msqbuf;
1703 	struct msqid_ds32_old msqbuf32;
1704 	int error;
1705 
1706 	if (uap->cmd == IPC_SET) {
1707 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1708 		if (error)
1709 			return (error);
1710 		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1711 		PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1712 		PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1713 		CP(msqbuf32, msqbuf, msg_cbytes);
1714 		CP(msqbuf32, msqbuf, msg_qnum);
1715 		CP(msqbuf32, msqbuf, msg_qbytes);
1716 		CP(msqbuf32, msqbuf, msg_lspid);
1717 		CP(msqbuf32, msqbuf, msg_lrpid);
1718 		CP(msqbuf32, msqbuf, msg_stime);
1719 		CP(msqbuf32, msqbuf, msg_rtime);
1720 		CP(msqbuf32, msqbuf, msg_ctime);
1721 	}
1722 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1723 	if (error)
1724 		return (error);
1725 	if (uap->cmd == IPC_STAT) {
1726 		bzero(&msqbuf32, sizeof(msqbuf32));
1727 		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1728 		PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1729 		PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1730 		CP(msqbuf, msqbuf32, msg_cbytes);
1731 		CP(msqbuf, msqbuf32, msg_qnum);
1732 		CP(msqbuf, msqbuf32, msg_qbytes);
1733 		CP(msqbuf, msqbuf32, msg_lspid);
1734 		CP(msqbuf, msqbuf32, msg_lrpid);
1735 		CP(msqbuf, msqbuf32, msg_stime);
1736 		CP(msqbuf, msqbuf32, msg_rtime);
1737 		CP(msqbuf, msqbuf32, msg_ctime);
1738 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1739 	}
1740 	return (error);
1741 }
1742 #endif
1743 
1744 int
1745 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1746 {
1747 	struct msqid_ds msqbuf;
1748 	struct msqid_ds32 msqbuf32;
1749 	int error;
1750 
1751 	if (uap->cmd == IPC_SET) {
1752 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1753 		if (error)
1754 			return (error);
1755 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1756 		PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1757 		PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1758 		CP(msqbuf32, msqbuf, msg_cbytes);
1759 		CP(msqbuf32, msqbuf, msg_qnum);
1760 		CP(msqbuf32, msqbuf, msg_qbytes);
1761 		CP(msqbuf32, msqbuf, msg_lspid);
1762 		CP(msqbuf32, msqbuf, msg_lrpid);
1763 		CP(msqbuf32, msqbuf, msg_stime);
1764 		CP(msqbuf32, msqbuf, msg_rtime);
1765 		CP(msqbuf32, msqbuf, msg_ctime);
1766 	}
1767 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1768 	if (error)
1769 		return (error);
1770 	if (uap->cmd == IPC_STAT) {
1771 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1772 		PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1773 		PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1774 		CP(msqbuf, msqbuf32, msg_cbytes);
1775 		CP(msqbuf, msqbuf32, msg_qnum);
1776 		CP(msqbuf, msqbuf32, msg_qbytes);
1777 		CP(msqbuf, msqbuf32, msg_lspid);
1778 		CP(msqbuf, msqbuf32, msg_lrpid);
1779 		CP(msqbuf, msqbuf32, msg_stime);
1780 		CP(msqbuf, msqbuf32, msg_rtime);
1781 		CP(msqbuf, msqbuf32, msg_ctime);
1782 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1783 	}
1784 	return (error);
1785 }
1786 
1787 int
1788 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1789 {
1790 	const void *msgp;
1791 	long mtype;
1792 	int32_t mtype32;
1793 	int error;
1794 
1795 	msgp = PTRIN(uap->msgp);
1796 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1797 		return (error);
1798 	mtype = mtype32;
1799 	return (kern_msgsnd(td, uap->msqid,
1800 	    (const char *)msgp + sizeof(mtype32),
1801 	    uap->msgsz, uap->msgflg, mtype));
1802 }
1803 
1804 int
1805 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1806 {
1807 	void *msgp;
1808 	long mtype;
1809 	int32_t mtype32;
1810 	int error;
1811 
1812 	msgp = PTRIN(uap->msgp);
1813 	if ((error = kern_msgrcv(td, uap->msqid,
1814 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1815 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1816 		return (error);
1817 	mtype32 = (int32_t)mtype;
1818 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1819 }
1820 #endif
1821 
1822 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1823     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1824 
1825 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1826 static sy_call_t *msgcalls[] = {
1827 	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1828 	(sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1829 };
1830 
1831 /*
1832  * Entry point for all MSG calls.
1833  *
1834  * XXX actually varargs.
1835  * struct msgsys_args {
1836  *		int	which;
1837  *		int	a2;
1838  *		int	a3;
1839  *		int	a4;
1840  *		int	a5;
1841  *		int	a6;
1842  *	} *uap;
1843  */
1844 int
1845 sys_msgsys(struct thread *td, struct msgsys_args *uap)
1846 {
1847 	int error;
1848 
1849 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1850 	if (uap->which < 0 || uap->which >= nitems(msgcalls))
1851 		return (EINVAL);
1852 	error = (*msgcalls[uap->which])(td, &uap->a2);
1853 	return (error);
1854 }
1855 
1856 #ifndef CP
1857 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1858 #endif
1859 
1860 #ifndef _SYS_SYSPROTO_H_
1861 struct freebsd7_msgctl_args {
1862 	int	msqid;
1863 	int	cmd;
1864 	struct	msqid_ds_old *buf;
1865 };
1866 #endif
1867 int
1868 freebsd7_msgctl(struct thread *td, struct freebsd7_msgctl_args *uap)
1869 {
1870 	struct msqid_ds_old msqold;
1871 	struct msqid_ds msqbuf;
1872 	int error;
1873 
1874 	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1875 	    uap->buf));
1876 	if (uap->cmd == IPC_SET) {
1877 		error = copyin(uap->buf, &msqold, sizeof(msqold));
1878 		if (error)
1879 			return (error);
1880 		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1881 		CP(msqold, msqbuf, __msg_first);
1882 		CP(msqold, msqbuf, __msg_last);
1883 		CP(msqold, msqbuf, msg_cbytes);
1884 		CP(msqold, msqbuf, msg_qnum);
1885 		CP(msqold, msqbuf, msg_qbytes);
1886 		CP(msqold, msqbuf, msg_lspid);
1887 		CP(msqold, msqbuf, msg_lrpid);
1888 		CP(msqold, msqbuf, msg_stime);
1889 		CP(msqold, msqbuf, msg_rtime);
1890 		CP(msqold, msqbuf, msg_ctime);
1891 	}
1892 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1893 	if (error)
1894 		return (error);
1895 	if (uap->cmd == IPC_STAT) {
1896 		bzero(&msqold, sizeof(msqold));
1897 		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1898 		CP(msqbuf, msqold, __msg_first);
1899 		CP(msqbuf, msqold, __msg_last);
1900 		CP(msqbuf, msqold, msg_cbytes);
1901 		CP(msqbuf, msqold, msg_qnum);
1902 		CP(msqbuf, msqold, msg_qbytes);
1903 		CP(msqbuf, msqold, msg_lspid);
1904 		CP(msqbuf, msqold, msg_lrpid);
1905 		CP(msqbuf, msqold, msg_stime);
1906 		CP(msqbuf, msqold, msg_rtime);
1907 		CP(msqbuf, msqold, msg_ctime);
1908 		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1909 	}
1910 	return (error);
1911 }
1912 
1913 #undef CP
1914 
1915 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1916 	   COMPAT_FREEBSD7 */
1917