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