Back to home page

LXR

 
 

    


0001 /*
0002  * linux/ipc/msg.c
0003  * Copyright (C) 1992 Krishna Balasubramanian
0004  *
0005  * Removed all the remaining kerneld mess
0006  * Catch the -EFAULT stuff properly
0007  * Use GFP_KERNEL for messages as in 1.2
0008  * Fixed up the unchecked user space derefs
0009  * Copyright (C) 1998 Alan Cox & Andi Kleen
0010  *
0011  * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
0012  *
0013  * mostly rewritten, threaded and wake-one semantics added
0014  * MSGMAX limit removed, sysctl's added
0015  * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
0016  *
0017  * support for audit of ipc object properties and permission changes
0018  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
0019  *
0020  * namespaces support
0021  * OpenVZ, SWsoft Inc.
0022  * Pavel Emelianov <xemul@openvz.org>
0023  */
0024 
0025 #include <linux/capability.h>
0026 #include <linux/msg.h>
0027 #include <linux/spinlock.h>
0028 #include <linux/init.h>
0029 #include <linux/mm.h>
0030 #include <linux/proc_fs.h>
0031 #include <linux/list.h>
0032 #include <linux/security.h>
0033 #include <linux/sched.h>
0034 #include <linux/syscalls.h>
0035 #include <linux/audit.h>
0036 #include <linux/seq_file.h>
0037 #include <linux/rwsem.h>
0038 #include <linux/nsproxy.h>
0039 #include <linux/ipc_namespace.h>
0040 
0041 #include <asm/current.h>
0042 #include <linux/uaccess.h>
0043 #include "util.h"
0044 
0045 /* one msg_receiver structure for each sleeping receiver */
0046 struct msg_receiver {
0047     struct list_head    r_list;
0048     struct task_struct  *r_tsk;
0049 
0050     int         r_mode;
0051     long            r_msgtype;
0052     long            r_maxsize;
0053 
0054     struct msg_msg      *r_msg;
0055 };
0056 
0057 /* one msg_sender for each sleeping sender */
0058 struct msg_sender {
0059     struct list_head    list;
0060     struct task_struct  *tsk;
0061     size_t                  msgsz;
0062 };
0063 
0064 #define SEARCH_ANY      1
0065 #define SEARCH_EQUAL        2
0066 #define SEARCH_NOTEQUAL     3
0067 #define SEARCH_LESSEQUAL    4
0068 #define SEARCH_NUMBER       5
0069 
0070 #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
0071 
0072 static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
0073 {
0074     struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
0075 
0076     if (IS_ERR(ipcp))
0077         return ERR_CAST(ipcp);
0078 
0079     return container_of(ipcp, struct msg_queue, q_perm);
0080 }
0081 
0082 static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns,
0083                             int id)
0084 {
0085     struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id);
0086 
0087     if (IS_ERR(ipcp))
0088         return ERR_CAST(ipcp);
0089 
0090     return container_of(ipcp, struct msg_queue, q_perm);
0091 }
0092 
0093 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
0094 {
0095     ipc_rmid(&msg_ids(ns), &s->q_perm);
0096 }
0097 
0098 static void msg_rcu_free(struct rcu_head *head)
0099 {
0100     struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
0101     struct msg_queue *msq = ipc_rcu_to_struct(p);
0102 
0103     security_msg_queue_free(msq);
0104     ipc_rcu_free(head);
0105 }
0106 
0107 /**
0108  * newque - Create a new msg queue
0109  * @ns: namespace
0110  * @params: ptr to the structure that contains the key and msgflg
0111  *
0112  * Called with msg_ids.rwsem held (writer)
0113  */
0114 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
0115 {
0116     struct msg_queue *msq;
0117     int id, retval;
0118     key_t key = params->key;
0119     int msgflg = params->flg;
0120 
0121     msq = ipc_rcu_alloc(sizeof(*msq));
0122     if (!msq)
0123         return -ENOMEM;
0124 
0125     msq->q_perm.mode = msgflg & S_IRWXUGO;
0126     msq->q_perm.key = key;
0127 
0128     msq->q_perm.security = NULL;
0129     retval = security_msg_queue_alloc(msq);
0130     if (retval) {
0131         ipc_rcu_putref(msq, ipc_rcu_free);
0132         return retval;
0133     }
0134 
0135     msq->q_stime = msq->q_rtime = 0;
0136     msq->q_ctime = get_seconds();
0137     msq->q_cbytes = msq->q_qnum = 0;
0138     msq->q_qbytes = ns->msg_ctlmnb;
0139     msq->q_lspid = msq->q_lrpid = 0;
0140     INIT_LIST_HEAD(&msq->q_messages);
0141     INIT_LIST_HEAD(&msq->q_receivers);
0142     INIT_LIST_HEAD(&msq->q_senders);
0143 
0144     /* ipc_addid() locks msq upon success. */
0145     id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
0146     if (id < 0) {
0147         ipc_rcu_putref(msq, msg_rcu_free);
0148         return id;
0149     }
0150 
0151     ipc_unlock_object(&msq->q_perm);
0152     rcu_read_unlock();
0153 
0154     return msq->q_perm.id;
0155 }
0156 
0157 static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz)
0158 {
0159     return msgsz + msq->q_cbytes <= msq->q_qbytes &&
0160         1 + msq->q_qnum <= msq->q_qbytes;
0161 }
0162 
0163 static inline void ss_add(struct msg_queue *msq,
0164               struct msg_sender *mss, size_t msgsz)
0165 {
0166     mss->tsk = current;
0167     mss->msgsz = msgsz;
0168     __set_current_state(TASK_INTERRUPTIBLE);
0169     list_add_tail(&mss->list, &msq->q_senders);
0170 }
0171 
0172 static inline void ss_del(struct msg_sender *mss)
0173 {
0174     if (mss->list.next)
0175         list_del(&mss->list);
0176 }
0177 
0178 static void ss_wakeup(struct msg_queue *msq,
0179               struct wake_q_head *wake_q, bool kill)
0180 {
0181     struct msg_sender *mss, *t;
0182     struct task_struct *stop_tsk = NULL;
0183     struct list_head *h = &msq->q_senders;
0184 
0185     list_for_each_entry_safe(mss, t, h, list) {
0186         if (kill)
0187             mss->list.next = NULL;
0188 
0189         /*
0190          * Stop at the first task we don't wakeup,
0191          * we've already iterated the original
0192          * sender queue.
0193          */
0194         else if (stop_tsk == mss->tsk)
0195             break;
0196         /*
0197          * We are not in an EIDRM scenario here, therefore
0198          * verify that we really need to wakeup the task.
0199          * To maintain current semantics and wakeup order,
0200          * move the sender to the tail on behalf of the
0201          * blocked task.
0202          */
0203         else if (!msg_fits_inqueue(msq, mss->msgsz)) {
0204             if (!stop_tsk)
0205                 stop_tsk = mss->tsk;
0206 
0207             list_move_tail(&mss->list, &msq->q_senders);
0208             continue;
0209         }
0210 
0211         wake_q_add(wake_q, mss->tsk);
0212     }
0213 }
0214 
0215 static void expunge_all(struct msg_queue *msq, int res,
0216             struct wake_q_head *wake_q)
0217 {
0218     struct msg_receiver *msr, *t;
0219 
0220     list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
0221         wake_q_add(wake_q, msr->r_tsk);
0222         WRITE_ONCE(msr->r_msg, ERR_PTR(res));
0223     }
0224 }
0225 
0226 /*
0227  * freeque() wakes up waiters on the sender and receiver waiting queue,
0228  * removes the message queue from message queue ID IDR, and cleans up all the
0229  * messages associated with this queue.
0230  *
0231  * msg_ids.rwsem (writer) and the spinlock for this message queue are held
0232  * before freeque() is called. msg_ids.rwsem remains locked on exit.
0233  */
0234 static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
0235 {
0236     struct msg_msg *msg, *t;
0237     struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
0238     DEFINE_WAKE_Q(wake_q);
0239 
0240     expunge_all(msq, -EIDRM, &wake_q);
0241     ss_wakeup(msq, &wake_q, true);
0242     msg_rmid(ns, msq);
0243     ipc_unlock_object(&msq->q_perm);
0244     wake_up_q(&wake_q);
0245     rcu_read_unlock();
0246 
0247     list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
0248         atomic_dec(&ns->msg_hdrs);
0249         free_msg(msg);
0250     }
0251     atomic_sub(msq->q_cbytes, &ns->msg_bytes);
0252     ipc_rcu_putref(msq, msg_rcu_free);
0253 }
0254 
0255 /*
0256  * Called with msg_ids.rwsem and ipcp locked.
0257  */
0258 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
0259 {
0260     struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
0261 
0262     return security_msg_queue_associate(msq, msgflg);
0263 }
0264 
0265 SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
0266 {
0267     struct ipc_namespace *ns;
0268     static const struct ipc_ops msg_ops = {
0269         .getnew = newque,
0270         .associate = msg_security,
0271     };
0272     struct ipc_params msg_params;
0273 
0274     ns = current->nsproxy->ipc_ns;
0275 
0276     msg_params.key = key;
0277     msg_params.flg = msgflg;
0278 
0279     return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
0280 }
0281 
0282 static inline unsigned long
0283 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
0284 {
0285     switch (version) {
0286     case IPC_64:
0287         return copy_to_user(buf, in, sizeof(*in));
0288     case IPC_OLD:
0289     {
0290         struct msqid_ds out;
0291 
0292         memset(&out, 0, sizeof(out));
0293 
0294         ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
0295 
0296         out.msg_stime       = in->msg_stime;
0297         out.msg_rtime       = in->msg_rtime;
0298         out.msg_ctime       = in->msg_ctime;
0299 
0300         if (in->msg_cbytes > USHRT_MAX)
0301             out.msg_cbytes  = USHRT_MAX;
0302         else
0303             out.msg_cbytes  = in->msg_cbytes;
0304         out.msg_lcbytes     = in->msg_cbytes;
0305 
0306         if (in->msg_qnum > USHRT_MAX)
0307             out.msg_qnum    = USHRT_MAX;
0308         else
0309             out.msg_qnum    = in->msg_qnum;
0310 
0311         if (in->msg_qbytes > USHRT_MAX)
0312             out.msg_qbytes  = USHRT_MAX;
0313         else
0314             out.msg_qbytes  = in->msg_qbytes;
0315         out.msg_lqbytes     = in->msg_qbytes;
0316 
0317         out.msg_lspid       = in->msg_lspid;
0318         out.msg_lrpid       = in->msg_lrpid;
0319 
0320         return copy_to_user(buf, &out, sizeof(out));
0321     }
0322     default:
0323         return -EINVAL;
0324     }
0325 }
0326 
0327 static inline unsigned long
0328 copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
0329 {
0330     switch (version) {
0331     case IPC_64:
0332         if (copy_from_user(out, buf, sizeof(*out)))
0333             return -EFAULT;
0334         return 0;
0335     case IPC_OLD:
0336     {
0337         struct msqid_ds tbuf_old;
0338 
0339         if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
0340             return -EFAULT;
0341 
0342         out->msg_perm.uid   = tbuf_old.msg_perm.uid;
0343         out->msg_perm.gid   = tbuf_old.msg_perm.gid;
0344         out->msg_perm.mode  = tbuf_old.msg_perm.mode;
0345 
0346         if (tbuf_old.msg_qbytes == 0)
0347             out->msg_qbytes = tbuf_old.msg_lqbytes;
0348         else
0349             out->msg_qbytes = tbuf_old.msg_qbytes;
0350 
0351         return 0;
0352     }
0353     default:
0354         return -EINVAL;
0355     }
0356 }
0357 
0358 /*
0359  * This function handles some msgctl commands which require the rwsem
0360  * to be held in write mode.
0361  * NOTE: no locks must be held, the rwsem is taken inside this function.
0362  */
0363 static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
0364                struct msqid_ds __user *buf, int version)
0365 {
0366     struct kern_ipc_perm *ipcp;
0367     struct msqid64_ds uninitialized_var(msqid64);
0368     struct msg_queue *msq;
0369     int err;
0370 
0371     if (cmd == IPC_SET) {
0372         if (copy_msqid_from_user(&msqid64, buf, version))
0373             return -EFAULT;
0374     }
0375 
0376     down_write(&msg_ids(ns).rwsem);
0377     rcu_read_lock();
0378 
0379     ipcp = ipcctl_pre_down_nolock(ns, &msg_ids(ns), msqid, cmd,
0380                       &msqid64.msg_perm, msqid64.msg_qbytes);
0381     if (IS_ERR(ipcp)) {
0382         err = PTR_ERR(ipcp);
0383         goto out_unlock1;
0384     }
0385 
0386     msq = container_of(ipcp, struct msg_queue, q_perm);
0387 
0388     err = security_msg_queue_msgctl(msq, cmd);
0389     if (err)
0390         goto out_unlock1;
0391 
0392     switch (cmd) {
0393     case IPC_RMID:
0394         ipc_lock_object(&msq->q_perm);
0395         /* freeque unlocks the ipc object and rcu */
0396         freeque(ns, ipcp);
0397         goto out_up;
0398     case IPC_SET:
0399     {
0400         DEFINE_WAKE_Q(wake_q);
0401 
0402         if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
0403             !capable(CAP_SYS_RESOURCE)) {
0404             err = -EPERM;
0405             goto out_unlock1;
0406         }
0407 
0408         ipc_lock_object(&msq->q_perm);
0409         err = ipc_update_perm(&msqid64.msg_perm, ipcp);
0410         if (err)
0411             goto out_unlock0;
0412 
0413         msq->q_qbytes = msqid64.msg_qbytes;
0414 
0415         msq->q_ctime = get_seconds();
0416         /*
0417          * Sleeping receivers might be excluded by
0418          * stricter permissions.
0419          */
0420         expunge_all(msq, -EAGAIN, &wake_q);
0421         /*
0422          * Sleeping senders might be able to send
0423          * due to a larger queue size.
0424          */
0425         ss_wakeup(msq, &wake_q, false);
0426         ipc_unlock_object(&msq->q_perm);
0427         wake_up_q(&wake_q);
0428 
0429         goto out_unlock1;
0430     }
0431     default:
0432         err = -EINVAL;
0433         goto out_unlock1;
0434     }
0435 
0436 out_unlock0:
0437     ipc_unlock_object(&msq->q_perm);
0438 out_unlock1:
0439     rcu_read_unlock();
0440 out_up:
0441     up_write(&msg_ids(ns).rwsem);
0442     return err;
0443 }
0444 
0445 static int msgctl_nolock(struct ipc_namespace *ns, int msqid,
0446              int cmd, int version, void __user *buf)
0447 {
0448     int err;
0449     struct msg_queue *msq;
0450 
0451     switch (cmd) {
0452     case IPC_INFO:
0453     case MSG_INFO:
0454     {
0455         struct msginfo msginfo;
0456         int max_id;
0457 
0458         if (!buf)
0459             return -EFAULT;
0460 
0461         /*
0462          * We must not return kernel stack data.
0463          * due to padding, it's not enough
0464          * to set all member fields.
0465          */
0466         err = security_msg_queue_msgctl(NULL, cmd);
0467         if (err)
0468             return err;
0469 
0470         memset(&msginfo, 0, sizeof(msginfo));
0471         msginfo.msgmni = ns->msg_ctlmni;
0472         msginfo.msgmax = ns->msg_ctlmax;
0473         msginfo.msgmnb = ns->msg_ctlmnb;
0474         msginfo.msgssz = MSGSSZ;
0475         msginfo.msgseg = MSGSEG;
0476         down_read(&msg_ids(ns).rwsem);
0477         if (cmd == MSG_INFO) {
0478             msginfo.msgpool = msg_ids(ns).in_use;
0479             msginfo.msgmap = atomic_read(&ns->msg_hdrs);
0480             msginfo.msgtql = atomic_read(&ns->msg_bytes);
0481         } else {
0482             msginfo.msgmap = MSGMAP;
0483             msginfo.msgpool = MSGPOOL;
0484             msginfo.msgtql = MSGTQL;
0485         }
0486         max_id = ipc_get_maxid(&msg_ids(ns));
0487         up_read(&msg_ids(ns).rwsem);
0488         if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
0489             return -EFAULT;
0490         return (max_id < 0) ? 0 : max_id;
0491     }
0492 
0493     case MSG_STAT:
0494     case IPC_STAT:
0495     {
0496         struct msqid64_ds tbuf;
0497         int success_return;
0498 
0499         if (!buf)
0500             return -EFAULT;
0501 
0502         memset(&tbuf, 0, sizeof(tbuf));
0503 
0504         rcu_read_lock();
0505         if (cmd == MSG_STAT) {
0506             msq = msq_obtain_object(ns, msqid);
0507             if (IS_ERR(msq)) {
0508                 err = PTR_ERR(msq);
0509                 goto out_unlock;
0510             }
0511             success_return = msq->q_perm.id;
0512         } else {
0513             msq = msq_obtain_object_check(ns, msqid);
0514             if (IS_ERR(msq)) {
0515                 err = PTR_ERR(msq);
0516                 goto out_unlock;
0517             }
0518             success_return = 0;
0519         }
0520 
0521         err = -EACCES;
0522         if (ipcperms(ns, &msq->q_perm, S_IRUGO))
0523             goto out_unlock;
0524 
0525         err = security_msg_queue_msgctl(msq, cmd);
0526         if (err)
0527             goto out_unlock;
0528 
0529         kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
0530         tbuf.msg_stime  = msq->q_stime;
0531         tbuf.msg_rtime  = msq->q_rtime;
0532         tbuf.msg_ctime  = msq->q_ctime;
0533         tbuf.msg_cbytes = msq->q_cbytes;
0534         tbuf.msg_qnum   = msq->q_qnum;
0535         tbuf.msg_qbytes = msq->q_qbytes;
0536         tbuf.msg_lspid  = msq->q_lspid;
0537         tbuf.msg_lrpid  = msq->q_lrpid;
0538         rcu_read_unlock();
0539 
0540         if (copy_msqid_to_user(buf, &tbuf, version))
0541             return -EFAULT;
0542         return success_return;
0543     }
0544 
0545     default:
0546         return -EINVAL;
0547     }
0548 
0549     return err;
0550 out_unlock:
0551     rcu_read_unlock();
0552     return err;
0553 }
0554 
0555 SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
0556 {
0557     int version;
0558     struct ipc_namespace *ns;
0559 
0560     if (msqid < 0 || cmd < 0)
0561         return -EINVAL;
0562 
0563     version = ipc_parse_version(&cmd);
0564     ns = current->nsproxy->ipc_ns;
0565 
0566     switch (cmd) {
0567     case IPC_INFO:
0568     case MSG_INFO:
0569     case MSG_STAT:  /* msqid is an index rather than a msg queue id */
0570     case IPC_STAT:
0571         return msgctl_nolock(ns, msqid, cmd, version, buf);
0572     case IPC_SET:
0573     case IPC_RMID:
0574         return msgctl_down(ns, msqid, cmd, buf, version);
0575     default:
0576         return  -EINVAL;
0577     }
0578 }
0579 
0580 static int testmsg(struct msg_msg *msg, long type, int mode)
0581 {
0582     switch (mode) {
0583     case SEARCH_ANY:
0584     case SEARCH_NUMBER:
0585         return 1;
0586     case SEARCH_LESSEQUAL:
0587         if (msg->m_type <= type)
0588             return 1;
0589         break;
0590     case SEARCH_EQUAL:
0591         if (msg->m_type == type)
0592             return 1;
0593         break;
0594     case SEARCH_NOTEQUAL:
0595         if (msg->m_type != type)
0596             return 1;
0597         break;
0598     }
0599     return 0;
0600 }
0601 
0602 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
0603                  struct wake_q_head *wake_q)
0604 {
0605     struct msg_receiver *msr, *t;
0606 
0607     list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
0608         if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
0609             !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
0610                            msr->r_msgtype, msr->r_mode)) {
0611 
0612             list_del(&msr->r_list);
0613             if (msr->r_maxsize < msg->m_ts) {
0614                 wake_q_add(wake_q, msr->r_tsk);
0615                 WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG));
0616             } else {
0617                 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
0618                 msq->q_rtime = get_seconds();
0619 
0620                 wake_q_add(wake_q, msr->r_tsk);
0621                 WRITE_ONCE(msr->r_msg, msg);
0622                 return 1;
0623             }
0624         }
0625     }
0626 
0627     return 0;
0628 }
0629 
0630 long do_msgsnd(int msqid, long mtype, void __user *mtext,
0631         size_t msgsz, int msgflg)
0632 {
0633     struct msg_queue *msq;
0634     struct msg_msg *msg;
0635     int err;
0636     struct ipc_namespace *ns;
0637     DEFINE_WAKE_Q(wake_q);
0638 
0639     ns = current->nsproxy->ipc_ns;
0640 
0641     if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
0642         return -EINVAL;
0643     if (mtype < 1)
0644         return -EINVAL;
0645 
0646     msg = load_msg(mtext, msgsz);
0647     if (IS_ERR(msg))
0648         return PTR_ERR(msg);
0649 
0650     msg->m_type = mtype;
0651     msg->m_ts = msgsz;
0652 
0653     rcu_read_lock();
0654     msq = msq_obtain_object_check(ns, msqid);
0655     if (IS_ERR(msq)) {
0656         err = PTR_ERR(msq);
0657         goto out_unlock1;
0658     }
0659 
0660     ipc_lock_object(&msq->q_perm);
0661 
0662     for (;;) {
0663         struct msg_sender s;
0664 
0665         err = -EACCES;
0666         if (ipcperms(ns, &msq->q_perm, S_IWUGO))
0667             goto out_unlock0;
0668 
0669         /* raced with RMID? */
0670         if (!ipc_valid_object(&msq->q_perm)) {
0671             err = -EIDRM;
0672             goto out_unlock0;
0673         }
0674 
0675         err = security_msg_queue_msgsnd(msq, msg, msgflg);
0676         if (err)
0677             goto out_unlock0;
0678 
0679         if (msg_fits_inqueue(msq, msgsz))
0680             break;
0681 
0682         /* queue full, wait: */
0683         if (msgflg & IPC_NOWAIT) {
0684             err = -EAGAIN;
0685             goto out_unlock0;
0686         }
0687 
0688         /* enqueue the sender and prepare to block */
0689         ss_add(msq, &s, msgsz);
0690 
0691         if (!ipc_rcu_getref(msq)) {
0692             err = -EIDRM;
0693             goto out_unlock0;
0694         }
0695 
0696         ipc_unlock_object(&msq->q_perm);
0697         rcu_read_unlock();
0698         schedule();
0699 
0700         rcu_read_lock();
0701         ipc_lock_object(&msq->q_perm);
0702 
0703         ipc_rcu_putref(msq, msg_rcu_free);
0704         /* raced with RMID? */
0705         if (!ipc_valid_object(&msq->q_perm)) {
0706             err = -EIDRM;
0707             goto out_unlock0;
0708         }
0709         ss_del(&s);
0710 
0711         if (signal_pending(current)) {
0712             err = -ERESTARTNOHAND;
0713             goto out_unlock0;
0714         }
0715 
0716     }
0717 
0718     msq->q_lspid = task_tgid_vnr(current);
0719     msq->q_stime = get_seconds();
0720 
0721     if (!pipelined_send(msq, msg, &wake_q)) {
0722         /* no one is waiting for this message, enqueue it */
0723         list_add_tail(&msg->m_list, &msq->q_messages);
0724         msq->q_cbytes += msgsz;
0725         msq->q_qnum++;
0726         atomic_add(msgsz, &ns->msg_bytes);
0727         atomic_inc(&ns->msg_hdrs);
0728     }
0729 
0730     err = 0;
0731     msg = NULL;
0732 
0733 out_unlock0:
0734     ipc_unlock_object(&msq->q_perm);
0735     wake_up_q(&wake_q);
0736 out_unlock1:
0737     rcu_read_unlock();
0738     if (msg != NULL)
0739         free_msg(msg);
0740     return err;
0741 }
0742 
0743 SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
0744         int, msgflg)
0745 {
0746     long mtype;
0747 
0748     if (get_user(mtype, &msgp->mtype))
0749         return -EFAULT;
0750     return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
0751 }
0752 
0753 static inline int convert_mode(long *msgtyp, int msgflg)
0754 {
0755     if (msgflg & MSG_COPY)
0756         return SEARCH_NUMBER;
0757     /*
0758      *  find message of correct type.
0759      *  msgtyp = 0 => get first.
0760      *  msgtyp > 0 => get first message of matching type.
0761      *  msgtyp < 0 => get message with least type must be < abs(msgtype).
0762      */
0763     if (*msgtyp == 0)
0764         return SEARCH_ANY;
0765     if (*msgtyp < 0) {
0766         if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */
0767             *msgtyp = LONG_MAX;
0768         else
0769             *msgtyp = -*msgtyp;
0770         return SEARCH_LESSEQUAL;
0771     }
0772     if (msgflg & MSG_EXCEPT)
0773         return SEARCH_NOTEQUAL;
0774     return SEARCH_EQUAL;
0775 }
0776 
0777 static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
0778 {
0779     struct msgbuf __user *msgp = dest;
0780     size_t msgsz;
0781 
0782     if (put_user(msg->m_type, &msgp->mtype))
0783         return -EFAULT;
0784 
0785     msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
0786     if (store_msg(msgp->mtext, msg, msgsz))
0787         return -EFAULT;
0788     return msgsz;
0789 }
0790 
0791 #ifdef CONFIG_CHECKPOINT_RESTORE
0792 /*
0793  * This function creates new kernel message structure, large enough to store
0794  * bufsz message bytes.
0795  */
0796 static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
0797 {
0798     struct msg_msg *copy;
0799 
0800     /*
0801      * Create dummy message to copy real message to.
0802      */
0803     copy = load_msg(buf, bufsz);
0804     if (!IS_ERR(copy))
0805         copy->m_ts = bufsz;
0806     return copy;
0807 }
0808 
0809 static inline void free_copy(struct msg_msg *copy)
0810 {
0811     if (copy)
0812         free_msg(copy);
0813 }
0814 #else
0815 static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
0816 {
0817     return ERR_PTR(-ENOSYS);
0818 }
0819 
0820 static inline void free_copy(struct msg_msg *copy)
0821 {
0822 }
0823 #endif
0824 
0825 static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
0826 {
0827     struct msg_msg *msg, *found = NULL;
0828     long count = 0;
0829 
0830     list_for_each_entry(msg, &msq->q_messages, m_list) {
0831         if (testmsg(msg, *msgtyp, mode) &&
0832             !security_msg_queue_msgrcv(msq, msg, current,
0833                            *msgtyp, mode)) {
0834             if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
0835                 *msgtyp = msg->m_type - 1;
0836                 found = msg;
0837             } else if (mode == SEARCH_NUMBER) {
0838                 if (*msgtyp == count)
0839                     return msg;
0840             } else
0841                 return msg;
0842             count++;
0843         }
0844     }
0845 
0846     return found ?: ERR_PTR(-EAGAIN);
0847 }
0848 
0849 long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
0850            long (*msg_handler)(void __user *, struct msg_msg *, size_t))
0851 {
0852     int mode;
0853     struct msg_queue *msq;
0854     struct ipc_namespace *ns;
0855     struct msg_msg *msg, *copy = NULL;
0856     DEFINE_WAKE_Q(wake_q);
0857 
0858     ns = current->nsproxy->ipc_ns;
0859 
0860     if (msqid < 0 || (long) bufsz < 0)
0861         return -EINVAL;
0862 
0863     if (msgflg & MSG_COPY) {
0864         if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
0865             return -EINVAL;
0866         copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
0867         if (IS_ERR(copy))
0868             return PTR_ERR(copy);
0869     }
0870     mode = convert_mode(&msgtyp, msgflg);
0871 
0872     rcu_read_lock();
0873     msq = msq_obtain_object_check(ns, msqid);
0874     if (IS_ERR(msq)) {
0875         rcu_read_unlock();
0876         free_copy(copy);
0877         return PTR_ERR(msq);
0878     }
0879 
0880     for (;;) {
0881         struct msg_receiver msr_d;
0882 
0883         msg = ERR_PTR(-EACCES);
0884         if (ipcperms(ns, &msq->q_perm, S_IRUGO))
0885             goto out_unlock1;
0886 
0887         ipc_lock_object(&msq->q_perm);
0888 
0889         /* raced with RMID? */
0890         if (!ipc_valid_object(&msq->q_perm)) {
0891             msg = ERR_PTR(-EIDRM);
0892             goto out_unlock0;
0893         }
0894 
0895         msg = find_msg(msq, &msgtyp, mode);
0896         if (!IS_ERR(msg)) {
0897             /*
0898              * Found a suitable message.
0899              * Unlink it from the queue.
0900              */
0901             if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
0902                 msg = ERR_PTR(-E2BIG);
0903                 goto out_unlock0;
0904             }
0905             /*
0906              * If we are copying, then do not unlink message and do
0907              * not update queue parameters.
0908              */
0909             if (msgflg & MSG_COPY) {
0910                 msg = copy_msg(msg, copy);
0911                 goto out_unlock0;
0912             }
0913 
0914             list_del(&msg->m_list);
0915             msq->q_qnum--;
0916             msq->q_rtime = get_seconds();
0917             msq->q_lrpid = task_tgid_vnr(current);
0918             msq->q_cbytes -= msg->m_ts;
0919             atomic_sub(msg->m_ts, &ns->msg_bytes);
0920             atomic_dec(&ns->msg_hdrs);
0921             ss_wakeup(msq, &wake_q, false);
0922 
0923             goto out_unlock0;
0924         }
0925 
0926         /* No message waiting. Wait for a message */
0927         if (msgflg & IPC_NOWAIT) {
0928             msg = ERR_PTR(-ENOMSG);
0929             goto out_unlock0;
0930         }
0931 
0932         list_add_tail(&msr_d.r_list, &msq->q_receivers);
0933         msr_d.r_tsk = current;
0934         msr_d.r_msgtype = msgtyp;
0935         msr_d.r_mode = mode;
0936         if (msgflg & MSG_NOERROR)
0937             msr_d.r_maxsize = INT_MAX;
0938         else
0939             msr_d.r_maxsize = bufsz;
0940         msr_d.r_msg = ERR_PTR(-EAGAIN);
0941         __set_current_state(TASK_INTERRUPTIBLE);
0942 
0943         ipc_unlock_object(&msq->q_perm);
0944         rcu_read_unlock();
0945         schedule();
0946 
0947         /*
0948          * Lockless receive, part 1:
0949          * We don't hold a reference to the queue and getting a
0950          * reference would defeat the idea of a lockless operation,
0951          * thus the code relies on rcu to guarantee the existence of
0952          * msq:
0953          * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
0954          * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
0955          */
0956         rcu_read_lock();
0957 
0958         /*
0959          * Lockless receive, part 2:
0960          * The work in pipelined_send() and expunge_all():
0961          * - Set pointer to message
0962          * - Queue the receiver task for later wakeup
0963          * - Wake up the process after the lock is dropped.
0964          *
0965          * Should the process wake up before this wakeup (due to a
0966          * signal) it will either see the message and continue ...
0967          */
0968         msg = READ_ONCE(msr_d.r_msg);
0969         if (msg != ERR_PTR(-EAGAIN))
0970             goto out_unlock1;
0971 
0972          /*
0973           * ... or see -EAGAIN, acquire the lock to check the message
0974           * again.
0975           */
0976         ipc_lock_object(&msq->q_perm);
0977 
0978         msg = msr_d.r_msg;
0979         if (msg != ERR_PTR(-EAGAIN))
0980             goto out_unlock0;
0981 
0982         list_del(&msr_d.r_list);
0983         if (signal_pending(current)) {
0984             msg = ERR_PTR(-ERESTARTNOHAND);
0985             goto out_unlock0;
0986         }
0987 
0988         ipc_unlock_object(&msq->q_perm);
0989     }
0990 
0991 out_unlock0:
0992     ipc_unlock_object(&msq->q_perm);
0993     wake_up_q(&wake_q);
0994 out_unlock1:
0995     rcu_read_unlock();
0996     if (IS_ERR(msg)) {
0997         free_copy(copy);
0998         return PTR_ERR(msg);
0999     }
1000 
1001     bufsz = msg_handler(buf, msg, bufsz);
1002     free_msg(msg);
1003 
1004     return bufsz;
1005 }
1006 
1007 SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
1008         long, msgtyp, int, msgflg)
1009 {
1010     return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
1011 }
1012 
1013 
1014 void msg_init_ns(struct ipc_namespace *ns)
1015 {
1016     ns->msg_ctlmax = MSGMAX;
1017     ns->msg_ctlmnb = MSGMNB;
1018     ns->msg_ctlmni = MSGMNI;
1019 
1020     atomic_set(&ns->msg_bytes, 0);
1021     atomic_set(&ns->msg_hdrs, 0);
1022     ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1023 }
1024 
1025 #ifdef CONFIG_IPC_NS
1026 void msg_exit_ns(struct ipc_namespace *ns)
1027 {
1028     free_ipcs(ns, &msg_ids(ns), freeque);
1029     idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
1030 }
1031 #endif
1032 
1033 #ifdef CONFIG_PROC_FS
1034 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1035 {
1036     struct user_namespace *user_ns = seq_user_ns(s);
1037     struct msg_queue *msq = it;
1038 
1039     seq_printf(s,
1040            "%10d %10d  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
1041            msq->q_perm.key,
1042            msq->q_perm.id,
1043            msq->q_perm.mode,
1044            msq->q_cbytes,
1045            msq->q_qnum,
1046            msq->q_lspid,
1047            msq->q_lrpid,
1048            from_kuid_munged(user_ns, msq->q_perm.uid),
1049            from_kgid_munged(user_ns, msq->q_perm.gid),
1050            from_kuid_munged(user_ns, msq->q_perm.cuid),
1051            from_kgid_munged(user_ns, msq->q_perm.cgid),
1052            msq->q_stime,
1053            msq->q_rtime,
1054            msq->q_ctime);
1055 
1056     return 0;
1057 }
1058 #endif
1059 
1060 void __init msg_init(void)
1061 {
1062     msg_init_ns(&init_ipc_ns);
1063 
1064     ipc_init_proc_interface("sysvipc/msg",
1065                 "       key      msqid perms      cbytes       qnum lspid lrpid   uid   gid  cuid  cgid      stime      rtime      ctime\n",
1066                 IPC_MSG_IDS, sysvipc_msg_proc_show);
1067 }