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