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, 0x%x)\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 /* 675 * MPSAFE 676 */ 677 int 678 msgsnd(td, uap) 679 struct thread *td; 680 register struct msgsnd_args *uap; 681 { 682 int msqid = uap->msqid; 683 const void *user_msgp = uap->msgp; 684 size_t msgsz = uap->msgsz; 685 int msgflg = uap->msgflg; 686 int segs_needed, error = 0; 687 register struct msqid_kernel *msqkptr; 688 register struct msg *msghdr; 689 short next; 690 691 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz, 692 msgflg)); 693 if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 694 return (ENOSYS); 695 696 mtx_lock(&msq_mtx); 697 msqid = IPCID_TO_IX(msqid); 698 699 if (msqid < 0 || msqid >= msginfo.msgmni) { 700 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 701 msginfo.msgmni)); 702 error = EINVAL; 703 goto done2; 704 } 705 706 msqkptr = &msqids[msqid]; 707 if (msqkptr->u.msg_qbytes == 0) { 708 DPRINTF(("no such message queue id\n")); 709 error = EINVAL; 710 goto done2; 711 } 712 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 713 DPRINTF(("wrong sequence number\n")); 714 error = EINVAL; 715 goto done2; 716 } 717 718 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) { 719 DPRINTF(("requester doesn't have write access\n")); 720 goto done2; 721 } 722 723 #ifdef MAC 724 error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr); 725 if (error != 0) 726 goto done2; 727 #endif 728 729 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 730 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, 731 segs_needed)); 732 for (;;) { 733 int need_more_resources = 0; 734 735 /* 736 * check msgsz 737 * (inside this loop in case msg_qbytes changes while we sleep) 738 */ 739 740 if (msgsz > msqkptr->u.msg_qbytes) { 741 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n")); 742 error = EINVAL; 743 goto done2; 744 } 745 746 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) { 747 DPRINTF(("msqid is locked\n")); 748 need_more_resources = 1; 749 } 750 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) { 751 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n")); 752 need_more_resources = 1; 753 } 754 if (segs_needed > nfree_msgmaps) { 755 DPRINTF(("segs_needed > nfree_msgmaps\n")); 756 need_more_resources = 1; 757 } 758 if (free_msghdrs == NULL) { 759 DPRINTF(("no more msghdrs\n")); 760 need_more_resources = 1; 761 } 762 763 if (need_more_resources) { 764 int we_own_it; 765 766 if ((msgflg & IPC_NOWAIT) != 0) { 767 DPRINTF(("need more resources but caller " 768 "doesn't want to wait\n")); 769 error = EAGAIN; 770 goto done2; 771 } 772 773 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) { 774 DPRINTF(("we don't own the msqid_ds\n")); 775 we_own_it = 0; 776 } else { 777 /* Force later arrivals to wait for our 778 request */ 779 DPRINTF(("we own the msqid_ds\n")); 780 msqkptr->u.msg_perm.mode |= MSG_LOCKED; 781 we_own_it = 1; 782 } 783 DPRINTF(("goodnight\n")); 784 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH, 785 "msgwait", 0); 786 DPRINTF(("good morning, error=%d\n", error)); 787 if (we_own_it) 788 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 789 if (error != 0) { 790 DPRINTF(("msgsnd: interrupted system call\n")); 791 error = EINTR; 792 goto done2; 793 } 794 795 /* 796 * Make sure that the msq queue still exists 797 */ 798 799 if (msqkptr->u.msg_qbytes == 0) { 800 DPRINTF(("msqid deleted\n")); 801 error = EIDRM; 802 goto done2; 803 } 804 805 } else { 806 DPRINTF(("got all the resources that we need\n")); 807 break; 808 } 809 } 810 811 /* 812 * We have the resources that we need. 813 * Make sure! 814 */ 815 816 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) 817 panic("msg_perm.mode & MSG_LOCKED"); 818 if (segs_needed > nfree_msgmaps) 819 panic("segs_needed > nfree_msgmaps"); 820 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) 821 panic("msgsz + msg_cbytes > msg_qbytes"); 822 if (free_msghdrs == NULL) 823 panic("no more msghdrs"); 824 825 /* 826 * Re-lock the msqid_ds in case we page-fault when copying in the 827 * message 828 */ 829 830 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) 831 panic("msqid_ds is already locked"); 832 msqkptr->u.msg_perm.mode |= MSG_LOCKED; 833 834 /* 835 * Allocate a message header 836 */ 837 838 msghdr = free_msghdrs; 839 free_msghdrs = msghdr->msg_next; 840 msghdr->msg_spot = -1; 841 msghdr->msg_ts = msgsz; 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 * Copy in the message type 875 */ 876 877 mtx_unlock(&msq_mtx); 878 if ((error = copyin(user_msgp, &msghdr->msg_type, 879 sizeof(msghdr->msg_type))) != 0) { 880 mtx_lock(&msq_mtx); 881 DPRINTF(("error %d copying the message type\n", error)); 882 msg_freehdr(msghdr); 883 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 884 wakeup(msqkptr); 885 goto done2; 886 } 887 mtx_lock(&msq_mtx); 888 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type); 889 890 /* 891 * Validate the message type 892 */ 893 894 if (msghdr->msg_type < 1) { 895 msg_freehdr(msghdr); 896 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 897 wakeup(msqkptr); 898 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type)); 899 error = EINVAL; 900 goto done2; 901 } 902 903 /* 904 * Copy in the message body 905 */ 906 907 next = msghdr->msg_spot; 908 while (msgsz > 0) { 909 size_t tlen; 910 if (msgsz > msginfo.msgssz) 911 tlen = msginfo.msgssz; 912 else 913 tlen = msgsz; 914 if (next <= -1) 915 panic("next too low #2"); 916 if (next >= msginfo.msgseg) 917 panic("next out of range #2"); 918 mtx_unlock(&msq_mtx); 919 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], 920 tlen)) != 0) { 921 mtx_lock(&msq_mtx); 922 DPRINTF(("error %d copying in message segment\n", 923 error)); 924 msg_freehdr(msghdr); 925 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 926 wakeup(msqkptr); 927 goto done2; 928 } 929 mtx_lock(&msq_mtx); 930 msgsz -= tlen; 931 user_msgp = (const char *)user_msgp + tlen; 932 next = msgmaps[next].next; 933 } 934 if (next != -1) 935 panic("didn't use all the msg segments"); 936 937 /* 938 * We've got the message. Unlock the msqid_ds. 939 */ 940 941 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; 942 943 /* 944 * Make sure that the msqid_ds is still allocated. 945 */ 946 947 if (msqkptr->u.msg_qbytes == 0) { 948 msg_freehdr(msghdr); 949 wakeup(msqkptr); 950 error = EIDRM; 951 goto done2; 952 } 953 954 #ifdef MAC 955 /* 956 * Note: Since the task/thread allocates the msghdr and usually 957 * primes it with its own MAC label, for a majority of policies, it 958 * won't be necessary to check whether the msghdr has access 959 * permissions to the msgq. The mac_check_sysv_msqsnd check would 960 * suffice in that case. However, this hook may be required where 961 * individual policies derive a non-identical label for the msghdr 962 * from the current thread label and may want to check the msghdr 963 * enqueue permissions, along with read/write permissions to the 964 * msgq. 965 */ 966 error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr); 967 if (error != 0) { 968 msg_freehdr(msghdr); 969 wakeup(msqkptr); 970 goto done2; 971 } 972 #endif 973 974 /* 975 * Put the message into the queue 976 */ 977 if (msqkptr->u.msg_first == NULL) { 978 msqkptr->u.msg_first = msghdr; 979 msqkptr->u.msg_last = msghdr; 980 } else { 981 msqkptr->u.msg_last->msg_next = msghdr; 982 msqkptr->u.msg_last = msghdr; 983 } 984 msqkptr->u.msg_last->msg_next = NULL; 985 986 msqkptr->u.msg_cbytes += msghdr->msg_ts; 987 msqkptr->u.msg_qnum++; 988 msqkptr->u.msg_lspid = td->td_proc->p_pid; 989 msqkptr->u.msg_stime = time_second; 990 991 wakeup(msqkptr); 992 td->td_retval[0] = 0; 993 done2: 994 mtx_unlock(&msq_mtx); 995 return (error); 996 } 997 998 #ifndef _SYS_SYSPROTO_H_ 999 struct msgrcv_args { 1000 int msqid; 1001 void *msgp; 1002 size_t msgsz; 1003 long msgtyp; 1004 int msgflg; 1005 }; 1006 #endif 1007 1008 /* 1009 * MPSAFE 1010 */ 1011 int 1012 msgrcv(td, uap) 1013 struct thread *td; 1014 register struct msgrcv_args *uap; 1015 { 1016 int msqid = uap->msqid; 1017 void *user_msgp = uap->msgp; 1018 size_t msgsz = uap->msgsz; 1019 long msgtyp = uap->msgtyp; 1020 int msgflg = uap->msgflg; 1021 size_t len; 1022 register struct msqid_kernel *msqkptr; 1023 register struct msg *msghdr; 1024 int error = 0; 1025 short next; 1026 1027 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp, 1028 msgsz, msgtyp, msgflg)); 1029 1030 if (!jail_sysvipc_allowed && jailed(td->td_ucred)) 1031 return (ENOSYS); 1032 1033 msqid = IPCID_TO_IX(msqid); 1034 1035 if (msqid < 0 || msqid >= msginfo.msgmni) { 1036 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 1037 msginfo.msgmni)); 1038 return (EINVAL); 1039 } 1040 1041 msqkptr = &msqids[msqid]; 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(uap->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 %d, 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 %d, " 1112 "requested %d\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 %d, got %d)\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=%d)\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 "msgwait", 0); 1182 DPRINTF(("msgrcv: good morning (error=%d)\n", error)); 1183 1184 if (error != 0) { 1185 DPRINTF(("msgsnd: 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(uap->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=%d, msg_ts=%d\n", msgsz, 1220 msghdr->msg_ts)); 1221 if (msgsz > msghdr->msg_ts) 1222 msgsz = msghdr->msg_ts; 1223 1224 /* 1225 * Return the type to the user. 1226 */ 1227 1228 mtx_unlock(&msq_mtx); 1229 error = copyout(&(msghdr->msg_type), user_msgp, 1230 sizeof(msghdr->msg_type)); 1231 mtx_lock(&msq_mtx); 1232 if (error != 0) { 1233 DPRINTF(("error (%d) copying out message type\n", error)); 1234 msg_freehdr(msghdr); 1235 wakeup(msqkptr); 1236 goto done2; 1237 } 1238 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 1239 1240 /* 1241 * Return the segments to the user 1242 */ 1243 1244 next = msghdr->msg_spot; 1245 for (len = 0; len < msgsz; len += msginfo.msgssz) { 1246 size_t tlen; 1247 1248 if (msgsz - len > msginfo.msgssz) 1249 tlen = msginfo.msgssz; 1250 else 1251 tlen = msgsz - len; 1252 if (next <= -1) 1253 panic("next too low #3"); 1254 if (next >= msginfo.msgseg) 1255 panic("next out of range #3"); 1256 mtx_unlock(&msq_mtx); 1257 error = copyout(&msgpool[next * msginfo.msgssz], 1258 user_msgp, tlen); 1259 mtx_lock(&msq_mtx); 1260 if (error != 0) { 1261 DPRINTF(("error (%d) copying out message segment\n", 1262 error)); 1263 msg_freehdr(msghdr); 1264 wakeup(msqkptr); 1265 goto done2; 1266 } 1267 user_msgp = (char *)user_msgp + tlen; 1268 next = msgmaps[next].next; 1269 } 1270 1271 /* 1272 * Done, return the actual number of bytes copied out. 1273 */ 1274 1275 msg_freehdr(msghdr); 1276 wakeup(msqkptr); 1277 td->td_retval[0] = msgsz; 1278 done2: 1279 mtx_unlock(&msq_mtx); 1280 return (error); 1281 } 1282 1283 static int 1284 sysctl_msqids(SYSCTL_HANDLER_ARGS) 1285 { 1286 1287 return (SYSCTL_OUT(req, msqids, 1288 sizeof(struct msqid_kernel) * msginfo.msgmni)); 1289 } 1290 1291 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, 1292 "Maximum message size"); 1293 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0, 1294 "Number of message queue identifiers"); 1295 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0, 1296 "Maximum number of bytes in a queue"); 1297 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0, 1298 "Maximum number of messages in the system"); 1299 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0, 1300 "Size of a message segment"); 1301 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0, 1302 "Number of message segments"); 1303 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD, 1304 NULL, 0, sysctl_msqids, "", "Message queue IDs"); 1305