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