1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/ipc/msg.c 4 * Copyright (C) 1992 Krishna Balasubramanian 5 * 6 * Removed all the remaining kerneld mess 7 * Catch the -EFAULT stuff properly 8 * Use GFP_KERNEL for messages as in 1.2 9 * Fixed up the unchecked user space derefs 10 * Copyright (C) 1998 Alan Cox & Andi Kleen 11 * 12 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <[email protected]> 13 * 14 * mostly rewritten, threaded and wake-one semantics added 15 * MSGMAX limit removed, sysctl's added 16 * (c) 1999 Manfred Spraul <[email protected]> 17 * 18 * support for audit of ipc object properties and permission changes 19 * Dustin Kirkland <[email protected]> 20 * 21 * namespaces support 22 * OpenVZ, SWsoft Inc. 23 * Pavel Emelianov <[email protected]> 24 */ 25 26 #include <linux/capability.h> 27 #include <linux/msg.h> 28 #include <linux/spinlock.h> 29 #include <linux/init.h> 30 #include <linux/mm.h> 31 #include <linux/proc_fs.h> 32 #include <linux/list.h> 33 #include <linux/security.h> 34 #include <linux/sched/wake_q.h> 35 #include <linux/syscalls.h> 36 #include <linux/audit.h> 37 #include <linux/seq_file.h> 38 #include <linux/rwsem.h> 39 #include <linux/nsproxy.h> 40 #include <linux/ipc_namespace.h> 41 42 #include <asm/current.h> 43 #include <linux/uaccess.h> 44 #include "util.h" 45 46 /* one msq_queue structure for each present queue on the system */ 47 struct msg_queue { 48 struct kern_ipc_perm q_perm; 49 time64_t q_stime; /* last msgsnd time */ 50 time64_t q_rtime; /* last msgrcv time */ 51 time64_t q_ctime; /* last change time */ 52 unsigned long q_cbytes; /* current number of bytes on queue */ 53 unsigned long q_qnum; /* number of messages in queue */ 54 unsigned long q_qbytes; /* max number of bytes on queue */ 55 struct pid *q_lspid; /* pid of last msgsnd */ 56 struct pid *q_lrpid; /* last receive pid */ 57 58 struct list_head q_messages; 59 struct list_head q_receivers; 60 struct list_head q_senders; 61 } __randomize_layout; 62 63 /* one msg_receiver structure for each sleeping receiver */ 64 struct msg_receiver { 65 struct list_head r_list; 66 struct task_struct *r_tsk; 67 68 int r_mode; 69 long r_msgtype; 70 long r_maxsize; 71 72 struct msg_msg *r_msg; 73 }; 74 75 /* one msg_sender for each sleeping sender */ 76 struct msg_sender { 77 struct list_head list; 78 struct task_struct *tsk; 79 size_t msgsz; 80 }; 81 82 #define SEARCH_ANY 1 83 #define SEARCH_EQUAL 2 84 #define SEARCH_NOTEQUAL 3 85 #define SEARCH_LESSEQUAL 4 86 #define SEARCH_NUMBER 5 87 88 #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS]) 89 90 static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id) 91 { 92 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id); 93 94 if (IS_ERR(ipcp)) 95 return ERR_CAST(ipcp); 96 97 return container_of(ipcp, struct msg_queue, q_perm); 98 } 99 100 static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns, 101 int id) 102 { 103 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id); 104 105 if (IS_ERR(ipcp)) 106 return ERR_CAST(ipcp); 107 108 return container_of(ipcp, struct msg_queue, q_perm); 109 } 110 111 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s) 112 { 113 ipc_rmid(&msg_ids(ns), &s->q_perm); 114 } 115 116 static void msg_rcu_free(struct rcu_head *head) 117 { 118 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu); 119 struct msg_queue *msq = container_of(p, struct msg_queue, q_perm); 120 121 security_msg_queue_free(&msq->q_perm); 122 kvfree(msq); 123 } 124 125 /** 126 * newque - Create a new msg queue 127 * @ns: namespace 128 * @params: ptr to the structure that contains the key and msgflg 129 * 130 * Called with msg_ids.rwsem held (writer) 131 */ 132 static int newque(struct ipc_namespace *ns, struct ipc_params *params) 133 { 134 struct msg_queue *msq; 135 int retval; 136 key_t key = params->key; 137 int msgflg = params->flg; 138 139 msq = kvmalloc(sizeof(*msq), GFP_KERNEL); 140 if (unlikely(!msq)) 141 return -ENOMEM; 142 143 msq->q_perm.mode = msgflg & S_IRWXUGO; 144 msq->q_perm.key = key; 145 146 msq->q_perm.security = NULL; 147 retval = security_msg_queue_alloc(&msq->q_perm); 148 if (retval) { 149 kvfree(msq); 150 return retval; 151 } 152 153 msq->q_stime = msq->q_rtime = 0; 154 msq->q_ctime = ktime_get_real_seconds(); 155 msq->q_cbytes = msq->q_qnum = 0; 156 msq->q_qbytes = ns->msg_ctlmnb; 157 msq->q_lspid = msq->q_lrpid = NULL; 158 INIT_LIST_HEAD(&msq->q_messages); 159 INIT_LIST_HEAD(&msq->q_receivers); 160 INIT_LIST_HEAD(&msq->q_senders); 161 162 /* ipc_addid() locks msq upon success. */ 163 retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni); 164 if (retval < 0) { 165 call_rcu(&msq->q_perm.rcu, msg_rcu_free); 166 return retval; 167 } 168 169 ipc_unlock_object(&msq->q_perm); 170 rcu_read_unlock(); 171 172 return msq->q_perm.id; 173 } 174 175 static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz) 176 { 177 return msgsz + msq->q_cbytes <= msq->q_qbytes && 178 1 + msq->q_qnum <= msq->q_qbytes; 179 } 180 181 static inline void ss_add(struct msg_queue *msq, 182 struct msg_sender *mss, size_t msgsz) 183 { 184 mss->tsk = current; 185 mss->msgsz = msgsz; 186 __set_current_state(TASK_INTERRUPTIBLE); 187 list_add_tail(&mss->list, &msq->q_senders); 188 } 189 190 static inline void ss_del(struct msg_sender *mss) 191 { 192 if (mss->list.next) 193 list_del(&mss->list); 194 } 195 196 static void ss_wakeup(struct msg_queue *msq, 197 struct wake_q_head *wake_q, bool kill) 198 { 199 struct msg_sender *mss, *t; 200 struct task_struct *stop_tsk = NULL; 201 struct list_head *h = &msq->q_senders; 202 203 list_for_each_entry_safe(mss, t, h, list) { 204 if (kill) 205 mss->list.next = NULL; 206 207 /* 208 * Stop at the first task we don't wakeup, 209 * we've already iterated the original 210 * sender queue. 211 */ 212 else if (stop_tsk == mss->tsk) 213 break; 214 /* 215 * We are not in an EIDRM scenario here, therefore 216 * verify that we really need to wakeup the task. 217 * To maintain current semantics and wakeup order, 218 * move the sender to the tail on behalf of the 219 * blocked task. 220 */ 221 else if (!msg_fits_inqueue(msq, mss->msgsz)) { 222 if (!stop_tsk) 223 stop_tsk = mss->tsk; 224 225 list_move_tail(&mss->list, &msq->q_senders); 226 continue; 227 } 228 229 wake_q_add(wake_q, mss->tsk); 230 } 231 } 232 233 static void expunge_all(struct msg_queue *msq, int res, 234 struct wake_q_head *wake_q) 235 { 236 struct msg_receiver *msr, *t; 237 238 list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) { 239 wake_q_add(wake_q, msr->r_tsk); 240 WRITE_ONCE(msr->r_msg, ERR_PTR(res)); 241 } 242 } 243 244 /* 245 * freeque() wakes up waiters on the sender and receiver waiting queue, 246 * removes the message queue from message queue ID IDR, and cleans up all the 247 * messages associated with this queue. 248 * 249 * msg_ids.rwsem (writer) and the spinlock for this message queue are held 250 * before freeque() is called. msg_ids.rwsem remains locked on exit. 251 */ 252 static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) 253 { 254 struct msg_msg *msg, *t; 255 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm); 256 DEFINE_WAKE_Q(wake_q); 257 258 expunge_all(msq, -EIDRM, &wake_q); 259 ss_wakeup(msq, &wake_q, true); 260 msg_rmid(ns, msq); 261 ipc_unlock_object(&msq->q_perm); 262 wake_up_q(&wake_q); 263 rcu_read_unlock(); 264 265 list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) { 266 atomic_dec(&ns->msg_hdrs); 267 free_msg(msg); 268 } 269 atomic_sub(msq->q_cbytes, &ns->msg_bytes); 270 ipc_update_pid(&msq->q_lspid, NULL); 271 ipc_update_pid(&msq->q_lrpid, NULL); 272 ipc_rcu_putref(&msq->q_perm, msg_rcu_free); 273 } 274 275 SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) 276 { 277 struct ipc_namespace *ns; 278 static const struct ipc_ops msg_ops = { 279 .getnew = newque, 280 .associate = security_msg_queue_associate, 281 }; 282 struct ipc_params msg_params; 283 284 ns = current->nsproxy->ipc_ns; 285 286 msg_params.key = key; 287 msg_params.flg = msgflg; 288 289 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params); 290 } 291 292 static inline unsigned long 293 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version) 294 { 295 switch (version) { 296 case IPC_64: 297 return copy_to_user(buf, in, sizeof(*in)); 298 case IPC_OLD: 299 { 300 struct msqid_ds out; 301 302 memset(&out, 0, sizeof(out)); 303 304 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm); 305 306 out.msg_stime = in->msg_stime; 307 out.msg_rtime = in->msg_rtime; 308 out.msg_ctime = in->msg_ctime; 309 310 if (in->msg_cbytes > USHRT_MAX) 311 out.msg_cbytes = USHRT_MAX; 312 else 313 out.msg_cbytes = in->msg_cbytes; 314 out.msg_lcbytes = in->msg_cbytes; 315 316 if (in->msg_qnum > USHRT_MAX) 317 out.msg_qnum = USHRT_MAX; 318 else 319 out.msg_qnum = in->msg_qnum; 320 321 if (in->msg_qbytes > USHRT_MAX) 322 out.msg_qbytes = USHRT_MAX; 323 else 324 out.msg_qbytes = in->msg_qbytes; 325 out.msg_lqbytes = in->msg_qbytes; 326 327 out.msg_lspid = in->msg_lspid; 328 out.msg_lrpid = in->msg_lrpid; 329 330 return copy_to_user(buf, &out, sizeof(out)); 331 } 332 default: 333 return -EINVAL; 334 } 335 } 336 337 static inline unsigned long 338 copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version) 339 { 340 switch (version) { 341 case IPC_64: 342 if (copy_from_user(out, buf, sizeof(*out))) 343 return -EFAULT; 344 return 0; 345 case IPC_OLD: 346 { 347 struct msqid_ds tbuf_old; 348 349 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) 350 return -EFAULT; 351 352 out->msg_perm.uid = tbuf_old.msg_perm.uid; 353 out->msg_perm.gid = tbuf_old.msg_perm.gid; 354 out->msg_perm.mode = tbuf_old.msg_perm.mode; 355 356 if (tbuf_old.msg_qbytes == 0) 357 out->msg_qbytes = tbuf_old.msg_lqbytes; 358 else 359 out->msg_qbytes = tbuf_old.msg_qbytes; 360 361 return 0; 362 } 363 default: 364 return -EINVAL; 365 } 366 } 367 368 /* 369 * This function handles some msgctl commands which require the rwsem 370 * to be held in write mode. 371 * NOTE: no locks must be held, the rwsem is taken inside this function. 372 */ 373 static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd, 374 struct msqid64_ds *msqid64) 375 { 376 struct kern_ipc_perm *ipcp; 377 struct msg_queue *msq; 378 int err; 379 380 down_write(&msg_ids(ns).rwsem); 381 rcu_read_lock(); 382 383 ipcp = ipcctl_pre_down_nolock(ns, &msg_ids(ns), msqid, cmd, 384 &msqid64->msg_perm, msqid64->msg_qbytes); 385 if (IS_ERR(ipcp)) { 386 err = PTR_ERR(ipcp); 387 goto out_unlock1; 388 } 389 390 msq = container_of(ipcp, struct msg_queue, q_perm); 391 392 err = security_msg_queue_msgctl(&msq->q_perm, cmd); 393 if (err) 394 goto out_unlock1; 395 396 switch (cmd) { 397 case IPC_RMID: 398 ipc_lock_object(&msq->q_perm); 399 /* freeque unlocks the ipc object and rcu */ 400 freeque(ns, ipcp); 401 goto out_up; 402 case IPC_SET: 403 { 404 DEFINE_WAKE_Q(wake_q); 405 406 if (msqid64->msg_qbytes > ns->msg_ctlmnb && 407 !capable(CAP_SYS_RESOURCE)) { 408 err = -EPERM; 409 goto out_unlock1; 410 } 411 412 ipc_lock_object(&msq->q_perm); 413 err = ipc_update_perm(&msqid64->msg_perm, ipcp); 414 if (err) 415 goto out_unlock0; 416 417 msq->q_qbytes = msqid64->msg_qbytes; 418 419 msq->q_ctime = ktime_get_real_seconds(); 420 /* 421 * Sleeping receivers might be excluded by 422 * stricter permissions. 423 */ 424 expunge_all(msq, -EAGAIN, &wake_q); 425 /* 426 * Sleeping senders might be able to send 427 * due to a larger queue size. 428 */ 429 ss_wakeup(msq, &wake_q, false); 430 ipc_unlock_object(&msq->q_perm); 431 wake_up_q(&wake_q); 432 433 goto out_unlock1; 434 } 435 default: 436 err = -EINVAL; 437 goto out_unlock1; 438 } 439 440 out_unlock0: 441 ipc_unlock_object(&msq->q_perm); 442 out_unlock1: 443 rcu_read_unlock(); 444 out_up: 445 up_write(&msg_ids(ns).rwsem); 446 return err; 447 } 448 449 static int msgctl_info(struct ipc_namespace *ns, int msqid, 450 int cmd, struct msginfo *msginfo) 451 { 452 int err; 453 int max_id; 454 455 /* 456 * We must not return kernel stack data. 457 * due to padding, it's not enough 458 * to set all member fields. 459 */ 460 err = security_msg_queue_msgctl(NULL, cmd); 461 if (err) 462 return err; 463 464 memset(msginfo, 0, sizeof(*msginfo)); 465 msginfo->msgmni = ns->msg_ctlmni; 466 msginfo->msgmax = ns->msg_ctlmax; 467 msginfo->msgmnb = ns->msg_ctlmnb; 468 msginfo->msgssz = MSGSSZ; 469 msginfo->msgseg = MSGSEG; 470 down_read(&msg_ids(ns).rwsem); 471 if (cmd == MSG_INFO) { 472 msginfo->msgpool = msg_ids(ns).in_use; 473 msginfo->msgmap = atomic_read(&ns->msg_hdrs); 474 msginfo->msgtql = atomic_read(&ns->msg_bytes); 475 } else { 476 msginfo->msgmap = MSGMAP; 477 msginfo->msgpool = MSGPOOL; 478 msginfo->msgtql = MSGTQL; 479 } 480 max_id = ipc_get_maxid(&msg_ids(ns)); 481 up_read(&msg_ids(ns).rwsem); 482 return (max_id < 0) ? 0 : max_id; 483 } 484 485 static int msgctl_stat(struct ipc_namespace *ns, int msqid, 486 int cmd, struct msqid64_ds *p) 487 { 488 struct msg_queue *msq; 489 int id = 0; 490 int err; 491 492 memset(p, 0, sizeof(*p)); 493 494 rcu_read_lock(); 495 if (cmd == MSG_STAT) { 496 msq = msq_obtain_object(ns, msqid); 497 if (IS_ERR(msq)) { 498 err = PTR_ERR(msq); 499 goto out_unlock; 500 } 501 id = msq->q_perm.id; 502 } else { 503 msq = msq_obtain_object_check(ns, msqid); 504 if (IS_ERR(msq)) { 505 err = PTR_ERR(msq); 506 goto out_unlock; 507 } 508 } 509 510 err = -EACCES; 511 if (ipcperms(ns, &msq->q_perm, S_IRUGO)) 512 goto out_unlock; 513 514 err = security_msg_queue_msgctl(&msq->q_perm, cmd); 515 if (err) 516 goto out_unlock; 517 518 ipc_lock_object(&msq->q_perm); 519 520 if (!ipc_valid_object(&msq->q_perm)) { 521 ipc_unlock_object(&msq->q_perm); 522 err = -EIDRM; 523 goto out_unlock; 524 } 525 526 kernel_to_ipc64_perm(&msq->q_perm, &p->msg_perm); 527 p->msg_stime = msq->q_stime; 528 p->msg_rtime = msq->q_rtime; 529 p->msg_ctime = msq->q_ctime; 530 p->msg_cbytes = msq->q_cbytes; 531 p->msg_qnum = msq->q_qnum; 532 p->msg_qbytes = msq->q_qbytes; 533 p->msg_lspid = pid_vnr(msq->q_lspid); 534 p->msg_lrpid = pid_vnr(msq->q_lrpid); 535 536 ipc_unlock_object(&msq->q_perm); 537 rcu_read_unlock(); 538 return id; 539 540 out_unlock: 541 rcu_read_unlock(); 542 return err; 543 } 544 545 SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf) 546 { 547 int version; 548 struct ipc_namespace *ns; 549 struct msqid64_ds msqid64; 550 int err; 551 552 if (msqid < 0 || cmd < 0) 553 return -EINVAL; 554 555 version = ipc_parse_version(&cmd); 556 ns = current->nsproxy->ipc_ns; 557 558 switch (cmd) { 559 case IPC_INFO: 560 case MSG_INFO: { 561 struct msginfo msginfo; 562 err = msgctl_info(ns, msqid, cmd, &msginfo); 563 if (err < 0) 564 return err; 565 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo))) 566 err = -EFAULT; 567 return err; 568 } 569 case MSG_STAT: /* msqid is an index rather than a msg queue id */ 570 case IPC_STAT: 571 err = msgctl_stat(ns, msqid, cmd, &msqid64); 572 if (err < 0) 573 return err; 574 if (copy_msqid_to_user(buf, &msqid64, version)) 575 err = -EFAULT; 576 return err; 577 case IPC_SET: 578 if (copy_msqid_from_user(&msqid64, buf, version)) 579 return -EFAULT; 580 /* fallthru */ 581 case IPC_RMID: 582 return msgctl_down(ns, msqid, cmd, &msqid64); 583 default: 584 return -EINVAL; 585 } 586 } 587 588 #ifdef CONFIG_COMPAT 589 590 struct compat_msqid_ds { 591 struct compat_ipc_perm msg_perm; 592 compat_uptr_t msg_first; 593 compat_uptr_t msg_last; 594 compat_time_t msg_stime; 595 compat_time_t msg_rtime; 596 compat_time_t msg_ctime; 597 compat_ulong_t msg_lcbytes; 598 compat_ulong_t msg_lqbytes; 599 unsigned short msg_cbytes; 600 unsigned short msg_qnum; 601 unsigned short msg_qbytes; 602 compat_ipc_pid_t msg_lspid; 603 compat_ipc_pid_t msg_lrpid; 604 }; 605 606 static int copy_compat_msqid_from_user(struct msqid64_ds *out, void __user *buf, 607 int version) 608 { 609 memset(out, 0, sizeof(*out)); 610 if (version == IPC_64) { 611 struct compat_msqid64_ds __user *p = buf; 612 if (get_compat_ipc64_perm(&out->msg_perm, &p->msg_perm)) 613 return -EFAULT; 614 if (get_user(out->msg_qbytes, &p->msg_qbytes)) 615 return -EFAULT; 616 } else { 617 struct compat_msqid_ds __user *p = buf; 618 if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm)) 619 return -EFAULT; 620 if (get_user(out->msg_qbytes, &p->msg_qbytes)) 621 return -EFAULT; 622 } 623 return 0; 624 } 625 626 static int copy_compat_msqid_to_user(void __user *buf, struct msqid64_ds *in, 627 int version) 628 { 629 if (version == IPC_64) { 630 struct compat_msqid64_ds v; 631 memset(&v, 0, sizeof(v)); 632 to_compat_ipc64_perm(&v.msg_perm, &in->msg_perm); 633 v.msg_stime = in->msg_stime; 634 v.msg_rtime = in->msg_rtime; 635 v.msg_ctime = in->msg_ctime; 636 v.msg_cbytes = in->msg_cbytes; 637 v.msg_qnum = in->msg_qnum; 638 v.msg_qbytes = in->msg_qbytes; 639 v.msg_lspid = in->msg_lspid; 640 v.msg_lrpid = in->msg_lrpid; 641 return copy_to_user(buf, &v, sizeof(v)); 642 } else { 643 struct compat_msqid_ds v; 644 memset(&v, 0, sizeof(v)); 645 to_compat_ipc_perm(&v.msg_perm, &in->msg_perm); 646 v.msg_stime = in->msg_stime; 647 v.msg_rtime = in->msg_rtime; 648 v.msg_ctime = in->msg_ctime; 649 v.msg_cbytes = in->msg_cbytes; 650 v.msg_qnum = in->msg_qnum; 651 v.msg_qbytes = in->msg_qbytes; 652 v.msg_lspid = in->msg_lspid; 653 v.msg_lrpid = in->msg_lrpid; 654 return copy_to_user(buf, &v, sizeof(v)); 655 } 656 } 657 658 COMPAT_SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, void __user *, uptr) 659 { 660 struct ipc_namespace *ns; 661 int err; 662 struct msqid64_ds msqid64; 663 int version = compat_ipc_parse_version(&cmd); 664 665 ns = current->nsproxy->ipc_ns; 666 667 if (msqid < 0 || cmd < 0) 668 return -EINVAL; 669 670 switch (cmd & (~IPC_64)) { 671 case IPC_INFO: 672 case MSG_INFO: { 673 struct msginfo msginfo; 674 err = msgctl_info(ns, msqid, cmd, &msginfo); 675 if (err < 0) 676 return err; 677 if (copy_to_user(uptr, &msginfo, sizeof(struct msginfo))) 678 err = -EFAULT; 679 return err; 680 } 681 case IPC_STAT: 682 case MSG_STAT: 683 err = msgctl_stat(ns, msqid, cmd, &msqid64); 684 if (err < 0) 685 return err; 686 if (copy_compat_msqid_to_user(uptr, &msqid64, version)) 687 err = -EFAULT; 688 return err; 689 case IPC_SET: 690 if (copy_compat_msqid_from_user(&msqid64, uptr, version)) 691 return -EFAULT; 692 /* fallthru */ 693 case IPC_RMID: 694 return msgctl_down(ns, msqid, cmd, &msqid64); 695 default: 696 return -EINVAL; 697 } 698 } 699 #endif 700 701 static int testmsg(struct msg_msg *msg, long type, int mode) 702 { 703 switch (mode) { 704 case SEARCH_ANY: 705 case SEARCH_NUMBER: 706 return 1; 707 case SEARCH_LESSEQUAL: 708 if (msg->m_type <= type) 709 return 1; 710 break; 711 case SEARCH_EQUAL: 712 if (msg->m_type == type) 713 return 1; 714 break; 715 case SEARCH_NOTEQUAL: 716 if (msg->m_type != type) 717 return 1; 718 break; 719 } 720 return 0; 721 } 722 723 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg, 724 struct wake_q_head *wake_q) 725 { 726 struct msg_receiver *msr, *t; 727 728 list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) { 729 if (testmsg(msg, msr->r_msgtype, msr->r_mode) && 730 !security_msg_queue_msgrcv(&msq->q_perm, msg, msr->r_tsk, 731 msr->r_msgtype, msr->r_mode)) { 732 733 list_del(&msr->r_list); 734 if (msr->r_maxsize < msg->m_ts) { 735 wake_q_add(wake_q, msr->r_tsk); 736 WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG)); 737 } else { 738 ipc_update_pid(&msq->q_lrpid, task_pid(msr->r_tsk)); 739 msq->q_rtime = get_seconds(); 740 741 wake_q_add(wake_q, msr->r_tsk); 742 WRITE_ONCE(msr->r_msg, msg); 743 return 1; 744 } 745 } 746 } 747 748 return 0; 749 } 750 751 static long do_msgsnd(int msqid, long mtype, void __user *mtext, 752 size_t msgsz, int msgflg) 753 { 754 struct msg_queue *msq; 755 struct msg_msg *msg; 756 int err; 757 struct ipc_namespace *ns; 758 DEFINE_WAKE_Q(wake_q); 759 760 ns = current->nsproxy->ipc_ns; 761 762 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0) 763 return -EINVAL; 764 if (mtype < 1) 765 return -EINVAL; 766 767 msg = load_msg(mtext, msgsz); 768 if (IS_ERR(msg)) 769 return PTR_ERR(msg); 770 771 msg->m_type = mtype; 772 msg->m_ts = msgsz; 773 774 rcu_read_lock(); 775 msq = msq_obtain_object_check(ns, msqid); 776 if (IS_ERR(msq)) { 777 err = PTR_ERR(msq); 778 goto out_unlock1; 779 } 780 781 ipc_lock_object(&msq->q_perm); 782 783 for (;;) { 784 struct msg_sender s; 785 786 err = -EACCES; 787 if (ipcperms(ns, &msq->q_perm, S_IWUGO)) 788 goto out_unlock0; 789 790 /* raced with RMID? */ 791 if (!ipc_valid_object(&msq->q_perm)) { 792 err = -EIDRM; 793 goto out_unlock0; 794 } 795 796 err = security_msg_queue_msgsnd(&msq->q_perm, msg, msgflg); 797 if (err) 798 goto out_unlock0; 799 800 if (msg_fits_inqueue(msq, msgsz)) 801 break; 802 803 /* queue full, wait: */ 804 if (msgflg & IPC_NOWAIT) { 805 err = -EAGAIN; 806 goto out_unlock0; 807 } 808 809 /* enqueue the sender and prepare to block */ 810 ss_add(msq, &s, msgsz); 811 812 if (!ipc_rcu_getref(&msq->q_perm)) { 813 err = -EIDRM; 814 goto out_unlock0; 815 } 816 817 ipc_unlock_object(&msq->q_perm); 818 rcu_read_unlock(); 819 schedule(); 820 821 rcu_read_lock(); 822 ipc_lock_object(&msq->q_perm); 823 824 ipc_rcu_putref(&msq->q_perm, msg_rcu_free); 825 /* raced with RMID? */ 826 if (!ipc_valid_object(&msq->q_perm)) { 827 err = -EIDRM; 828 goto out_unlock0; 829 } 830 ss_del(&s); 831 832 if (signal_pending(current)) { 833 err = -ERESTARTNOHAND; 834 goto out_unlock0; 835 } 836 837 } 838 839 ipc_update_pid(&msq->q_lspid, task_tgid(current)); 840 msq->q_stime = get_seconds(); 841 842 if (!pipelined_send(msq, msg, &wake_q)) { 843 /* no one is waiting for this message, enqueue it */ 844 list_add_tail(&msg->m_list, &msq->q_messages); 845 msq->q_cbytes += msgsz; 846 msq->q_qnum++; 847 atomic_add(msgsz, &ns->msg_bytes); 848 atomic_inc(&ns->msg_hdrs); 849 } 850 851 err = 0; 852 msg = NULL; 853 854 out_unlock0: 855 ipc_unlock_object(&msq->q_perm); 856 wake_up_q(&wake_q); 857 out_unlock1: 858 rcu_read_unlock(); 859 if (msg != NULL) 860 free_msg(msg); 861 return err; 862 } 863 864 SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, 865 int, msgflg) 866 { 867 long mtype; 868 869 if (get_user(mtype, &msgp->mtype)) 870 return -EFAULT; 871 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg); 872 } 873 874 #ifdef CONFIG_COMPAT 875 876 struct compat_msgbuf { 877 compat_long_t mtype; 878 char mtext[1]; 879 }; 880 881 COMPAT_SYSCALL_DEFINE4(msgsnd, int, msqid, compat_uptr_t, msgp, 882 compat_ssize_t, msgsz, int, msgflg) 883 { 884 struct compat_msgbuf __user *up = compat_ptr(msgp); 885 compat_long_t mtype; 886 887 if (get_user(mtype, &up->mtype)) 888 return -EFAULT; 889 return do_msgsnd(msqid, mtype, up->mtext, (ssize_t)msgsz, msgflg); 890 } 891 #endif 892 893 static inline int convert_mode(long *msgtyp, int msgflg) 894 { 895 if (msgflg & MSG_COPY) 896 return SEARCH_NUMBER; 897 /* 898 * find message of correct type. 899 * msgtyp = 0 => get first. 900 * msgtyp > 0 => get first message of matching type. 901 * msgtyp < 0 => get message with least type must be < abs(msgtype). 902 */ 903 if (*msgtyp == 0) 904 return SEARCH_ANY; 905 if (*msgtyp < 0) { 906 if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */ 907 *msgtyp = LONG_MAX; 908 else 909 *msgtyp = -*msgtyp; 910 return SEARCH_LESSEQUAL; 911 } 912 if (msgflg & MSG_EXCEPT) 913 return SEARCH_NOTEQUAL; 914 return SEARCH_EQUAL; 915 } 916 917 static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz) 918 { 919 struct msgbuf __user *msgp = dest; 920 size_t msgsz; 921 922 if (put_user(msg->m_type, &msgp->mtype)) 923 return -EFAULT; 924 925 msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz; 926 if (store_msg(msgp->mtext, msg, msgsz)) 927 return -EFAULT; 928 return msgsz; 929 } 930 931 #ifdef CONFIG_CHECKPOINT_RESTORE 932 /* 933 * This function creates new kernel message structure, large enough to store 934 * bufsz message bytes. 935 */ 936 static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz) 937 { 938 struct msg_msg *copy; 939 940 /* 941 * Create dummy message to copy real message to. 942 */ 943 copy = load_msg(buf, bufsz); 944 if (!IS_ERR(copy)) 945 copy->m_ts = bufsz; 946 return copy; 947 } 948 949 static inline void free_copy(struct msg_msg *copy) 950 { 951 if (copy) 952 free_msg(copy); 953 } 954 #else 955 static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz) 956 { 957 return ERR_PTR(-ENOSYS); 958 } 959 960 static inline void free_copy(struct msg_msg *copy) 961 { 962 } 963 #endif 964 965 static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode) 966 { 967 struct msg_msg *msg, *found = NULL; 968 long count = 0; 969 970 list_for_each_entry(msg, &msq->q_messages, m_list) { 971 if (testmsg(msg, *msgtyp, mode) && 972 !security_msg_queue_msgrcv(&msq->q_perm, msg, current, 973 *msgtyp, mode)) { 974 if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) { 975 *msgtyp = msg->m_type - 1; 976 found = msg; 977 } else if (mode == SEARCH_NUMBER) { 978 if (*msgtyp == count) 979 return msg; 980 } else 981 return msg; 982 count++; 983 } 984 } 985 986 return found ?: ERR_PTR(-EAGAIN); 987 } 988 989 static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg, 990 long (*msg_handler)(void __user *, struct msg_msg *, size_t)) 991 { 992 int mode; 993 struct msg_queue *msq; 994 struct ipc_namespace *ns; 995 struct msg_msg *msg, *copy = NULL; 996 DEFINE_WAKE_Q(wake_q); 997 998 ns = current->nsproxy->ipc_ns; 999 1000 if (msqid < 0 || (long) bufsz < 0) 1001 return -EINVAL; 1002 1003 if (msgflg & MSG_COPY) { 1004 if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT)) 1005 return -EINVAL; 1006 copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax)); 1007 if (IS_ERR(copy)) 1008 return PTR_ERR(copy); 1009 } 1010 mode = convert_mode(&msgtyp, msgflg); 1011 1012 rcu_read_lock(); 1013 msq = msq_obtain_object_check(ns, msqid); 1014 if (IS_ERR(msq)) { 1015 rcu_read_unlock(); 1016 free_copy(copy); 1017 return PTR_ERR(msq); 1018 } 1019 1020 for (;;) { 1021 struct msg_receiver msr_d; 1022 1023 msg = ERR_PTR(-EACCES); 1024 if (ipcperms(ns, &msq->q_perm, S_IRUGO)) 1025 goto out_unlock1; 1026 1027 ipc_lock_object(&msq->q_perm); 1028 1029 /* raced with RMID? */ 1030 if (!ipc_valid_object(&msq->q_perm)) { 1031 msg = ERR_PTR(-EIDRM); 1032 goto out_unlock0; 1033 } 1034 1035 msg = find_msg(msq, &msgtyp, mode); 1036 if (!IS_ERR(msg)) { 1037 /* 1038 * Found a suitable message. 1039 * Unlink it from the queue. 1040 */ 1041 if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) { 1042 msg = ERR_PTR(-E2BIG); 1043 goto out_unlock0; 1044 } 1045 /* 1046 * If we are copying, then do not unlink message and do 1047 * not update queue parameters. 1048 */ 1049 if (msgflg & MSG_COPY) { 1050 msg = copy_msg(msg, copy); 1051 goto out_unlock0; 1052 } 1053 1054 list_del(&msg->m_list); 1055 msq->q_qnum--; 1056 msq->q_rtime = get_seconds(); 1057 ipc_update_pid(&msq->q_lrpid, task_tgid(current)); 1058 msq->q_cbytes -= msg->m_ts; 1059 atomic_sub(msg->m_ts, &ns->msg_bytes); 1060 atomic_dec(&ns->msg_hdrs); 1061 ss_wakeup(msq, &wake_q, false); 1062 1063 goto out_unlock0; 1064 } 1065 1066 /* No message waiting. Wait for a message */ 1067 if (msgflg & IPC_NOWAIT) { 1068 msg = ERR_PTR(-ENOMSG); 1069 goto out_unlock0; 1070 } 1071 1072 list_add_tail(&msr_d.r_list, &msq->q_receivers); 1073 msr_d.r_tsk = current; 1074 msr_d.r_msgtype = msgtyp; 1075 msr_d.r_mode = mode; 1076 if (msgflg & MSG_NOERROR) 1077 msr_d.r_maxsize = INT_MAX; 1078 else 1079 msr_d.r_maxsize = bufsz; 1080 msr_d.r_msg = ERR_PTR(-EAGAIN); 1081 __set_current_state(TASK_INTERRUPTIBLE); 1082 1083 ipc_unlock_object(&msq->q_perm); 1084 rcu_read_unlock(); 1085 schedule(); 1086 1087 /* 1088 * Lockless receive, part 1: 1089 * We don't hold a reference to the queue and getting a 1090 * reference would defeat the idea of a lockless operation, 1091 * thus the code relies on rcu to guarantee the existence of 1092 * msq: 1093 * Prior to destruction, expunge_all(-EIRDM) changes r_msg. 1094 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed. 1095 */ 1096 rcu_read_lock(); 1097 1098 /* 1099 * Lockless receive, part 2: 1100 * The work in pipelined_send() and expunge_all(): 1101 * - Set pointer to message 1102 * - Queue the receiver task for later wakeup 1103 * - Wake up the process after the lock is dropped. 1104 * 1105 * Should the process wake up before this wakeup (due to a 1106 * signal) it will either see the message and continue ... 1107 */ 1108 msg = READ_ONCE(msr_d.r_msg); 1109 if (msg != ERR_PTR(-EAGAIN)) 1110 goto out_unlock1; 1111 1112 /* 1113 * ... or see -EAGAIN, acquire the lock to check the message 1114 * again. 1115 */ 1116 ipc_lock_object(&msq->q_perm); 1117 1118 msg = msr_d.r_msg; 1119 if (msg != ERR_PTR(-EAGAIN)) 1120 goto out_unlock0; 1121 1122 list_del(&msr_d.r_list); 1123 if (signal_pending(current)) { 1124 msg = ERR_PTR(-ERESTARTNOHAND); 1125 goto out_unlock0; 1126 } 1127 1128 ipc_unlock_object(&msq->q_perm); 1129 } 1130 1131 out_unlock0: 1132 ipc_unlock_object(&msq->q_perm); 1133 wake_up_q(&wake_q); 1134 out_unlock1: 1135 rcu_read_unlock(); 1136 if (IS_ERR(msg)) { 1137 free_copy(copy); 1138 return PTR_ERR(msg); 1139 } 1140 1141 bufsz = msg_handler(buf, msg, bufsz); 1142 free_msg(msg); 1143 1144 return bufsz; 1145 } 1146 1147 SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, 1148 long, msgtyp, int, msgflg) 1149 { 1150 return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill); 1151 } 1152 1153 #ifdef CONFIG_COMPAT 1154 static long compat_do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz) 1155 { 1156 struct compat_msgbuf __user *msgp = dest; 1157 size_t msgsz; 1158 1159 if (put_user(msg->m_type, &msgp->mtype)) 1160 return -EFAULT; 1161 1162 msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz; 1163 if (store_msg(msgp->mtext, msg, msgsz)) 1164 return -EFAULT; 1165 return msgsz; 1166 } 1167 1168 COMPAT_SYSCALL_DEFINE5(msgrcv, int, msqid, compat_uptr_t, msgp, 1169 compat_ssize_t, msgsz, compat_long_t, msgtyp, int, msgflg) 1170 { 1171 return do_msgrcv(msqid, compat_ptr(msgp), (ssize_t)msgsz, (long)msgtyp, 1172 msgflg, compat_do_msg_fill); 1173 } 1174 #endif 1175 1176 int msg_init_ns(struct ipc_namespace *ns) 1177 { 1178 ns->msg_ctlmax = MSGMAX; 1179 ns->msg_ctlmnb = MSGMNB; 1180 ns->msg_ctlmni = MSGMNI; 1181 1182 atomic_set(&ns->msg_bytes, 0); 1183 atomic_set(&ns->msg_hdrs, 0); 1184 return ipc_init_ids(&ns->ids[IPC_MSG_IDS]); 1185 } 1186 1187 #ifdef CONFIG_IPC_NS 1188 void msg_exit_ns(struct ipc_namespace *ns) 1189 { 1190 free_ipcs(ns, &msg_ids(ns), freeque); 1191 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr); 1192 rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht); 1193 } 1194 #endif 1195 1196 #ifdef CONFIG_PROC_FS 1197 static int sysvipc_msg_proc_show(struct seq_file *s, void *it) 1198 { 1199 struct pid_namespace *pid_ns = ipc_seq_pid_ns(s); 1200 struct user_namespace *user_ns = seq_user_ns(s); 1201 struct kern_ipc_perm *ipcp = it; 1202 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm); 1203 1204 seq_printf(s, 1205 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10llu %10llu %10llu\n", 1206 msq->q_perm.key, 1207 msq->q_perm.id, 1208 msq->q_perm.mode, 1209 msq->q_cbytes, 1210 msq->q_qnum, 1211 pid_nr_ns(msq->q_lspid, pid_ns), 1212 pid_nr_ns(msq->q_lrpid, pid_ns), 1213 from_kuid_munged(user_ns, msq->q_perm.uid), 1214 from_kgid_munged(user_ns, msq->q_perm.gid), 1215 from_kuid_munged(user_ns, msq->q_perm.cuid), 1216 from_kgid_munged(user_ns, msq->q_perm.cgid), 1217 msq->q_stime, 1218 msq->q_rtime, 1219 msq->q_ctime); 1220 1221 return 0; 1222 } 1223 #endif 1224 1225 int __init msg_init(void) 1226 { 1227 const int err = msg_init_ns(&init_ipc_ns); 1228 1229 ipc_init_proc_interface("sysvipc/msg", 1230 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n", 1231 IPC_MSG_IDS, sysvipc_msg_proc_show); 1232 return err; 1233 } 1234