msg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * linux/ipc/msg.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. *
  5. * Removed all the remaining kerneld mess
  6. * Catch the -EFAULT stuff properly
  7. * Use GFP_KERNEL for messages as in 1.2
  8. * Fixed up the unchecked user space derefs
  9. * Copyright (C) 1998 Alan Cox & Andi Kleen
  10. *
  11. * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  12. *
  13. * mostly rewritten, threaded and wake-one semantics added
  14. * MSGMAX limit removed, sysctl's added
  15. * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  16. *
  17. * support for audit of ipc object properties and permission changes
  18. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  19. *
  20. * namespaces support
  21. * OpenVZ, SWsoft Inc.
  22. * Pavel Emelianov <xemul@openvz.org>
  23. */
  24. #include <linux/capability.h>
  25. #include <linux/msg.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/init.h>
  28. #include <linux/mm.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/list.h>
  31. #include <linux/security.h>
  32. #include <linux/sched.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/audit.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/rwsem.h>
  37. #include <linux/nsproxy.h>
  38. #include <linux/ipc_namespace.h>
  39. #include <asm/current.h>
  40. #include <asm/uaccess.h>
  41. #include "util.h"
  42. /*
  43. * one msg_receiver structure for each sleeping receiver:
  44. */
  45. struct msg_receiver {
  46. struct list_head r_list;
  47. struct task_struct *r_tsk;
  48. int r_mode;
  49. long r_msgtype;
  50. long r_maxsize;
  51. struct msg_msg *volatile r_msg;
  52. };
  53. /* one msg_sender for each sleeping sender */
  54. struct msg_sender {
  55. struct list_head list;
  56. struct task_struct *tsk;
  57. };
  58. #define SEARCH_ANY 1
  59. #define SEARCH_EQUAL 2
  60. #define SEARCH_NOTEQUAL 3
  61. #define SEARCH_LESSEQUAL 4
  62. #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
  63. #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
  64. static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
  65. static int newque(struct ipc_namespace *, struct ipc_params *);
  66. #ifdef CONFIG_PROC_FS
  67. static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
  68. #endif
  69. /*
  70. * Scale msgmni with the available lowmem size: the memory dedicated to msg
  71. * queues should occupy at most 1/MSG_MEM_SCALE of lowmem.
  72. * Also take into account the number of nsproxies created so far.
  73. * This should be done staying within the (MSGMNI , IPCMNI/nr_ipc_ns) range.
  74. */
  75. void recompute_msgmni(struct ipc_namespace *ns)
  76. {
  77. struct sysinfo i;
  78. unsigned long allowed;
  79. int nb_ns;
  80. si_meminfo(&i);
  81. allowed = (((i.totalram - i.totalhigh) / MSG_MEM_SCALE) * i.mem_unit)
  82. / MSGMNB;
  83. nb_ns = atomic_read(&nr_ipc_ns);
  84. allowed /= nb_ns;
  85. if (allowed < MSGMNI) {
  86. ns->msg_ctlmni = MSGMNI;
  87. return;
  88. }
  89. if (allowed > IPCMNI / nb_ns) {
  90. ns->msg_ctlmni = IPCMNI / nb_ns;
  91. return;
  92. }
  93. ns->msg_ctlmni = allowed;
  94. }
  95. void msg_init_ns(struct ipc_namespace *ns)
  96. {
  97. ns->msg_ctlmax = MSGMAX;
  98. ns->msg_ctlmnb = MSGMNB;
  99. recompute_msgmni(ns);
  100. atomic_set(&ns->msg_bytes, 0);
  101. atomic_set(&ns->msg_hdrs, 0);
  102. ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
  103. }
  104. #ifdef CONFIG_IPC_NS
  105. void msg_exit_ns(struct ipc_namespace *ns)
  106. {
  107. free_ipcs(ns, &msg_ids(ns), freeque);
  108. idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
  109. }
  110. #endif
  111. void __init msg_init(void)
  112. {
  113. msg_init_ns(&init_ipc_ns);
  114. printk(KERN_INFO "msgmni has been set to %d\n",
  115. init_ipc_ns.msg_ctlmni);
  116. ipc_init_proc_interface("sysvipc/msg",
  117. " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
  118. IPC_MSG_IDS, sysvipc_msg_proc_show);
  119. }
  120. /*
  121. * msg_lock_(check_) routines are called in the paths where the rw_mutex
  122. * is not held.
  123. */
  124. static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
  125. {
  126. struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
  127. if (IS_ERR(ipcp))
  128. return (struct msg_queue *)ipcp;
  129. return container_of(ipcp, struct msg_queue, q_perm);
  130. }
  131. static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
  132. int id)
  133. {
  134. struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
  135. if (IS_ERR(ipcp))
  136. return (struct msg_queue *)ipcp;
  137. return container_of(ipcp, struct msg_queue, q_perm);
  138. }
  139. static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
  140. {
  141. ipc_rmid(&msg_ids(ns), &s->q_perm);
  142. }
  143. /**
  144. * newque - Create a new msg queue
  145. * @ns: namespace
  146. * @params: ptr to the structure that contains the key and msgflg
  147. *
  148. * Called with msg_ids.rw_mutex held (writer)
  149. */
  150. static int newque(struct ipc_namespace *ns, struct ipc_params *params)
  151. {
  152. struct msg_queue *msq;
  153. int id, retval;
  154. key_t key = params->key;
  155. int msgflg = params->flg;
  156. msq = ipc_rcu_alloc(sizeof(*msq));
  157. if (!msq)
  158. return -ENOMEM;
  159. msq->q_perm.mode = msgflg & S_IRWXUGO;
  160. msq->q_perm.key = key;
  161. msq->q_perm.security = NULL;
  162. retval = security_msg_queue_alloc(msq);
  163. if (retval) {
  164. ipc_rcu_putref(msq);
  165. return retval;
  166. }
  167. msq->q_stime = msq->q_rtime = 0;
  168. msq->q_ctime = get_seconds();
  169. msq->q_cbytes = msq->q_qnum = 0;
  170. msq->q_qbytes = ns->msg_ctlmnb;
  171. msq->q_lspid = msq->q_lrpid = 0;
  172. INIT_LIST_HEAD(&msq->q_messages);
  173. INIT_LIST_HEAD(&msq->q_receivers);
  174. INIT_LIST_HEAD(&msq->q_senders);
  175. /*
  176. * ipc_addid() locks msq
  177. */
  178. id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
  179. if (id < 0) {
  180. security_msg_queue_free(msq);
  181. ipc_rcu_putref(msq);
  182. return id;
  183. }
  184. msg_unlock(msq);
  185. return msq->q_perm.id;
  186. }
  187. static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
  188. {
  189. mss->tsk = current;
  190. current->state = TASK_INTERRUPTIBLE;
  191. list_add_tail(&mss->list, &msq->q_senders);
  192. }
  193. static inline void ss_del(struct msg_sender *mss)
  194. {
  195. if (mss->list.next != NULL)
  196. list_del(&mss->list);
  197. }
  198. static void ss_wakeup(struct list_head *h, int kill)
  199. {
  200. struct list_head *tmp;
  201. tmp = h->next;
  202. while (tmp != h) {
  203. struct msg_sender *mss;
  204. mss = list_entry(tmp, struct msg_sender, list);
  205. tmp = tmp->next;
  206. if (kill)
  207. mss->list.next = NULL;
  208. wake_up_process(mss->tsk);
  209. }
  210. }
  211. static void expunge_all(struct msg_queue *msq, int res)
  212. {
  213. struct list_head *tmp;
  214. tmp = msq->q_receivers.next;
  215. while (tmp != &msq->q_receivers) {
  216. struct msg_receiver *msr;
  217. msr = list_entry(tmp, struct msg_receiver, r_list);
  218. tmp = tmp->next;
  219. msr->r_msg = NULL;
  220. wake_up_process(msr->r_tsk);
  221. smp_mb();
  222. msr->r_msg = ERR_PTR(res);
  223. }
  224. }
  225. /*
  226. * freeque() wakes up waiters on the sender and receiver waiting queue,
  227. * removes the message queue from message queue ID IDR, and cleans up all the
  228. * messages associated with this queue.
  229. *
  230. * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
  231. * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
  232. */
  233. static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  234. {
  235. struct list_head *tmp;
  236. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  237. expunge_all(msq, -EIDRM);
  238. ss_wakeup(&msq->q_senders, 1);
  239. msg_rmid(ns, msq);
  240. msg_unlock(msq);
  241. tmp = msq->q_messages.next;
  242. while (tmp != &msq->q_messages) {
  243. struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
  244. tmp = tmp->next;
  245. atomic_dec(&ns->msg_hdrs);
  246. free_msg(msg);
  247. }
  248. atomic_sub(msq->q_cbytes, &ns->msg_bytes);
  249. security_msg_queue_free(msq);
  250. ipc_lock_by_ptr(&msq->q_perm);
  251. ipc_rcu_putref(msq);
  252. ipc_unlock(&msq->q_perm);
  253. }
  254. /*
  255. * Called with msg_ids.rw_mutex and ipcp locked.
  256. */
  257. static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
  258. {
  259. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  260. return security_msg_queue_associate(msq, msgflg);
  261. }
  262. SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
  263. {
  264. struct ipc_namespace *ns;
  265. struct ipc_ops msg_ops;
  266. struct ipc_params msg_params;
  267. ns = current->nsproxy->ipc_ns;
  268. msg_ops.getnew = newque;
  269. msg_ops.associate = msg_security;
  270. msg_ops.more_checks = NULL;
  271. msg_params.key = key;
  272. msg_params.flg = msgflg;
  273. return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
  274. }
  275. static inline unsigned long
  276. copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
  277. {
  278. switch(version) {
  279. case IPC_64:
  280. return copy_to_user(buf, in, sizeof(*in));
  281. case IPC_OLD:
  282. {
  283. struct msqid_ds out;
  284. memset(&out, 0, sizeof(out));
  285. ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
  286. out.msg_stime = in->msg_stime;
  287. out.msg_rtime = in->msg_rtime;
  288. out.msg_ctime = in->msg_ctime;
  289. if (in->msg_cbytes > USHRT_MAX)
  290. out.msg_cbytes = USHRT_MAX;
  291. else
  292. out.msg_cbytes = in->msg_cbytes;
  293. out.msg_lcbytes = in->msg_cbytes;
  294. if (in->msg_qnum > USHRT_MAX)
  295. out.msg_qnum = USHRT_MAX;
  296. else
  297. out.msg_qnum = in->msg_qnum;
  298. if (in->msg_qbytes > USHRT_MAX)
  299. out.msg_qbytes = USHRT_MAX;
  300. else
  301. out.msg_qbytes = in->msg_qbytes;
  302. out.msg_lqbytes = in->msg_qbytes;
  303. out.msg_lspid = in->msg_lspid;
  304. out.msg_lrpid = in->msg_lrpid;
  305. return copy_to_user(buf, &out, sizeof(out));
  306. }
  307. default:
  308. return -EINVAL;
  309. }
  310. }
  311. static inline unsigned long
  312. copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
  313. {
  314. switch(version) {
  315. case IPC_64:
  316. if (copy_from_user(out, buf, sizeof(*out)))
  317. return -EFAULT;
  318. return 0;
  319. case IPC_OLD:
  320. {
  321. struct msqid_ds tbuf_old;
  322. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  323. return -EFAULT;
  324. out->msg_perm.uid = tbuf_old.msg_perm.uid;
  325. out->msg_perm.gid = tbuf_old.msg_perm.gid;
  326. out->msg_perm.mode = tbuf_old.msg_perm.mode;
  327. if (tbuf_old.msg_qbytes == 0)
  328. out->msg_qbytes = tbuf_old.msg_lqbytes;
  329. else
  330. out->msg_qbytes = tbuf_old.msg_qbytes;
  331. return 0;
  332. }
  333. default:
  334. return -EINVAL;
  335. }
  336. }
  337. /*
  338. * This function handles some msgctl commands which require the rw_mutex
  339. * to be held in write mode.
  340. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  341. */
  342. static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  343. struct msqid_ds __user *buf, int version)
  344. {
  345. struct kern_ipc_perm *ipcp;
  346. struct msqid64_ds uninitialized_var(msqid64);
  347. struct msg_queue *msq;
  348. int err;
  349. if (cmd == IPC_SET) {
  350. if (copy_msqid_from_user(&msqid64, buf, version))
  351. return -EFAULT;
  352. }
  353. ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
  354. &msqid64.msg_perm, msqid64.msg_qbytes);
  355. if (IS_ERR(ipcp))
  356. return PTR_ERR(ipcp);
  357. msq = container_of(ipcp, struct msg_queue, q_perm);
  358. err = security_msg_queue_msgctl(msq, cmd);
  359. if (err)
  360. goto out_unlock;
  361. switch (cmd) {
  362. case IPC_RMID:
  363. freeque(ns, ipcp);
  364. goto out_up;
  365. case IPC_SET:
  366. if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
  367. !capable(CAP_SYS_RESOURCE)) {
  368. err = -EPERM;
  369. goto out_unlock;
  370. }
  371. err = ipc_update_perm(&msqid64.msg_perm, ipcp);
  372. if (err)
  373. goto out_unlock;
  374. msq->q_qbytes = msqid64.msg_qbytes;
  375. msq->q_ctime = get_seconds();
  376. /* sleeping receivers might be excluded by
  377. * stricter permissions.
  378. */
  379. expunge_all(msq, -EAGAIN);
  380. /* sleeping senders might be able to send
  381. * due to a larger queue size.
  382. */
  383. ss_wakeup(&msq->q_senders, 0);
  384. break;
  385. default:
  386. err = -EINVAL;
  387. }
  388. out_unlock:
  389. msg_unlock(msq);
  390. out_up:
  391. up_write(&msg_ids(ns).rw_mutex);
  392. return err;
  393. }
  394. SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  395. {
  396. struct msg_queue *msq;
  397. int err, version;
  398. struct ipc_namespace *ns;
  399. if (msqid < 0 || cmd < 0)
  400. return -EINVAL;
  401. version = ipc_parse_version(&cmd);
  402. ns = current->nsproxy->ipc_ns;
  403. switch (cmd) {
  404. case IPC_INFO:
  405. case MSG_INFO:
  406. {
  407. struct msginfo msginfo;
  408. int max_id;
  409. if (!buf)
  410. return -EFAULT;
  411. /*
  412. * We must not return kernel stack data.
  413. * due to padding, it's not enough
  414. * to set all member fields.
  415. */
  416. err = security_msg_queue_msgctl(NULL, cmd);
  417. if (err)
  418. return err;
  419. memset(&msginfo, 0, sizeof(msginfo));
  420. msginfo.msgmni = ns->msg_ctlmni;
  421. msginfo.msgmax = ns->msg_ctlmax;
  422. msginfo.msgmnb = ns->msg_ctlmnb;
  423. msginfo.msgssz = MSGSSZ;
  424. msginfo.msgseg = MSGSEG;
  425. down_read(&msg_ids(ns).rw_mutex);
  426. if (cmd == MSG_INFO) {
  427. msginfo.msgpool = msg_ids(ns).in_use;
  428. msginfo.msgmap = atomic_read(&ns->msg_hdrs);
  429. msginfo.msgtql = atomic_read(&ns->msg_bytes);
  430. } else {
  431. msginfo.msgmap = MSGMAP;
  432. msginfo.msgpool = MSGPOOL;
  433. msginfo.msgtql = MSGTQL;
  434. }
  435. max_id = ipc_get_maxid(&msg_ids(ns));
  436. up_read(&msg_ids(ns).rw_mutex);
  437. if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
  438. return -EFAULT;
  439. return (max_id < 0) ? 0 : max_id;
  440. }
  441. case MSG_STAT: /* msqid is an index rather than a msg queue id */
  442. case IPC_STAT:
  443. {
  444. struct msqid64_ds tbuf;
  445. int success_return;
  446. if (!buf)
  447. return -EFAULT;
  448. if (cmd == MSG_STAT) {
  449. msq = msg_lock(ns, msqid);
  450. if (IS_ERR(msq))
  451. return PTR_ERR(msq);
  452. success_return = msq->q_perm.id;
  453. } else {
  454. msq = msg_lock_check(ns, msqid);
  455. if (IS_ERR(msq))
  456. return PTR_ERR(msq);
  457. success_return = 0;
  458. }
  459. err = -EACCES;
  460. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  461. goto out_unlock;
  462. err = security_msg_queue_msgctl(msq, cmd);
  463. if (err)
  464. goto out_unlock;
  465. memset(&tbuf, 0, sizeof(tbuf));
  466. kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
  467. tbuf.msg_stime = msq->q_stime;
  468. tbuf.msg_rtime = msq->q_rtime;
  469. tbuf.msg_ctime = msq->q_ctime;
  470. tbuf.msg_cbytes = msq->q_cbytes;
  471. tbuf.msg_qnum = msq->q_qnum;
  472. tbuf.msg_qbytes = msq->q_qbytes;
  473. tbuf.msg_lspid = msq->q_lspid;
  474. tbuf.msg_lrpid = msq->q_lrpid;
  475. msg_unlock(msq);
  476. if (copy_msqid_to_user(buf, &tbuf, version))
  477. return -EFAULT;
  478. return success_return;
  479. }
  480. case IPC_SET:
  481. case IPC_RMID:
  482. err = msgctl_down(ns, msqid, cmd, buf, version);
  483. return err;
  484. default:
  485. return -EINVAL;
  486. }
  487. out_unlock:
  488. msg_unlock(msq);
  489. return err;
  490. }
  491. static int testmsg(struct msg_msg *msg, long type, int mode)
  492. {
  493. switch(mode)
  494. {
  495. case SEARCH_ANY:
  496. return 1;
  497. case SEARCH_LESSEQUAL:
  498. if (msg->m_type <=type)
  499. return 1;
  500. break;
  501. case SEARCH_EQUAL:
  502. if (msg->m_type == type)
  503. return 1;
  504. break;
  505. case SEARCH_NOTEQUAL:
  506. if (msg->m_type != type)
  507. return 1;
  508. break;
  509. }
  510. return 0;
  511. }
  512. static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  513. {
  514. struct list_head *tmp;
  515. tmp = msq->q_receivers.next;
  516. while (tmp != &msq->q_receivers) {
  517. struct msg_receiver *msr;
  518. msr = list_entry(tmp, struct msg_receiver, r_list);
  519. tmp = tmp->next;
  520. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  521. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  522. msr->r_msgtype, msr->r_mode)) {
  523. list_del(&msr->r_list);
  524. if (msr->r_maxsize < msg->m_ts) {
  525. msr->r_msg = NULL;
  526. wake_up_process(msr->r_tsk);
  527. smp_mb();
  528. msr->r_msg = ERR_PTR(-E2BIG);
  529. } else {
  530. msr->r_msg = NULL;
  531. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  532. msq->q_rtime = get_seconds();
  533. wake_up_process(msr->r_tsk);
  534. smp_mb();
  535. msr->r_msg = msg;
  536. return 1;
  537. }
  538. }
  539. }
  540. return 0;
  541. }
  542. long do_msgsnd(int msqid, long mtype, void __user *mtext,
  543. size_t msgsz, int msgflg)
  544. {
  545. struct msg_queue *msq;
  546. struct msg_msg *msg;
  547. int err;
  548. struct ipc_namespace *ns;
  549. ns = current->nsproxy->ipc_ns;
  550. if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
  551. return -EINVAL;
  552. if (mtype < 1)
  553. return -EINVAL;
  554. msg = load_msg(mtext, msgsz);
  555. if (IS_ERR(msg))
  556. return PTR_ERR(msg);
  557. msg->m_type = mtype;
  558. msg->m_ts = msgsz;
  559. msq = msg_lock_check(ns, msqid);
  560. if (IS_ERR(msq)) {
  561. err = PTR_ERR(msq);
  562. goto out_free;
  563. }
  564. for (;;) {
  565. struct msg_sender s;
  566. err = -EACCES;
  567. if (ipcperms(ns, &msq->q_perm, S_IWUGO))
  568. goto out_unlock_free;
  569. err = security_msg_queue_msgsnd(msq, msg, msgflg);
  570. if (err)
  571. goto out_unlock_free;
  572. if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
  573. 1 + msq->q_qnum <= msq->q_qbytes) {
  574. break;
  575. }
  576. /* queue full, wait: */
  577. if (msgflg & IPC_NOWAIT) {
  578. err = -EAGAIN;
  579. goto out_unlock_free;
  580. }
  581. ss_add(msq, &s);
  582. ipc_rcu_getref(msq);
  583. msg_unlock(msq);
  584. schedule();
  585. ipc_lock_by_ptr(&msq->q_perm);
  586. ipc_rcu_putref(msq);
  587. if (msq->q_perm.deleted) {
  588. err = -EIDRM;
  589. goto out_unlock_free;
  590. }
  591. ss_del(&s);
  592. if (signal_pending(current)) {
  593. err = -ERESTARTNOHAND;
  594. goto out_unlock_free;
  595. }
  596. }
  597. msq->q_lspid = task_tgid_vnr(current);
  598. msq->q_stime = get_seconds();
  599. if (!pipelined_send(msq, msg)) {
  600. /* no one is waiting for this message, enqueue it */
  601. list_add_tail(&msg->m_list, &msq->q_messages);
  602. msq->q_cbytes += msgsz;
  603. msq->q_qnum++;
  604. atomic_add(msgsz, &ns->msg_bytes);
  605. atomic_inc(&ns->msg_hdrs);
  606. }
  607. err = 0;
  608. msg = NULL;
  609. out_unlock_free:
  610. msg_unlock(msq);
  611. out_free:
  612. if (msg != NULL)
  613. free_msg(msg);
  614. return err;
  615. }
  616. SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  617. int, msgflg)
  618. {
  619. long mtype;
  620. if (get_user(mtype, &msgp->mtype))
  621. return -EFAULT;
  622. return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
  623. }
  624. static inline int convert_mode(long *msgtyp, int msgflg)
  625. {
  626. /*
  627. * find message of correct type.
  628. * msgtyp = 0 => get first.
  629. * msgtyp > 0 => get first message of matching type.
  630. * msgtyp < 0 => get message with least type must be < abs(msgtype).
  631. */
  632. if (*msgtyp == 0)
  633. return SEARCH_ANY;
  634. if (*msgtyp < 0) {
  635. *msgtyp = -*msgtyp;
  636. return SEARCH_LESSEQUAL;
  637. }
  638. if (msgflg & MSG_EXCEPT)
  639. return SEARCH_NOTEQUAL;
  640. return SEARCH_EQUAL;
  641. }
  642. long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
  643. size_t msgsz, long msgtyp, int msgflg)
  644. {
  645. struct msg_queue *msq;
  646. struct msg_msg *msg;
  647. int mode;
  648. struct ipc_namespace *ns;
  649. if (msqid < 0 || (long) msgsz < 0)
  650. return -EINVAL;
  651. mode = convert_mode(&msgtyp, msgflg);
  652. ns = current->nsproxy->ipc_ns;
  653. msq = msg_lock_check(ns, msqid);
  654. if (IS_ERR(msq))
  655. return PTR_ERR(msq);
  656. for (;;) {
  657. struct msg_receiver msr_d;
  658. struct list_head *tmp;
  659. msg = ERR_PTR(-EACCES);
  660. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  661. goto out_unlock;
  662. msg = ERR_PTR(-EAGAIN);
  663. tmp = msq->q_messages.next;
  664. while (tmp != &msq->q_messages) {
  665. struct msg_msg *walk_msg;
  666. walk_msg = list_entry(tmp, struct msg_msg, m_list);
  667. if (testmsg(walk_msg, msgtyp, mode) &&
  668. !security_msg_queue_msgrcv(msq, walk_msg, current,
  669. msgtyp, mode)) {
  670. msg = walk_msg;
  671. if (mode == SEARCH_LESSEQUAL &&
  672. walk_msg->m_type != 1) {
  673. msg = walk_msg;
  674. msgtyp = walk_msg->m_type - 1;
  675. } else {
  676. msg = walk_msg;
  677. break;
  678. }
  679. }
  680. tmp = tmp->next;
  681. }
  682. if (!IS_ERR(msg)) {
  683. /*
  684. * Found a suitable message.
  685. * Unlink it from the queue.
  686. */
  687. if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
  688. msg = ERR_PTR(-E2BIG);
  689. goto out_unlock;
  690. }
  691. list_del(&msg->m_list);
  692. msq->q_qnum--;
  693. msq->q_rtime = get_seconds();
  694. msq->q_lrpid = task_tgid_vnr(current);
  695. msq->q_cbytes -= msg->m_ts;
  696. atomic_sub(msg->m_ts, &ns->msg_bytes);
  697. atomic_dec(&ns->msg_hdrs);
  698. ss_wakeup(&msq->q_senders, 0);
  699. msg_unlock(msq);
  700. break;
  701. }
  702. /* No message waiting. Wait for a message */
  703. if (msgflg & IPC_NOWAIT) {
  704. msg = ERR_PTR(-ENOMSG);
  705. goto out_unlock;
  706. }
  707. list_add_tail(&msr_d.r_list, &msq->q_receivers);
  708. msr_d.r_tsk = current;
  709. msr_d.r_msgtype = msgtyp;
  710. msr_d.r_mode = mode;
  711. if (msgflg & MSG_NOERROR)
  712. msr_d.r_maxsize = INT_MAX;
  713. else
  714. msr_d.r_maxsize = msgsz;
  715. msr_d.r_msg = ERR_PTR(-EAGAIN);
  716. current->state = TASK_INTERRUPTIBLE;
  717. msg_unlock(msq);
  718. schedule();
  719. /* Lockless receive, part 1:
  720. * Disable preemption. We don't hold a reference to the queue
  721. * and getting a reference would defeat the idea of a lockless
  722. * operation, thus the code relies on rcu to guarantee the
  723. * existence of msq:
  724. * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
  725. * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
  726. * rcu_read_lock() prevents preemption between reading r_msg
  727. * and the spin_lock() inside ipc_lock_by_ptr().
  728. */
  729. rcu_read_lock();
  730. /* Lockless receive, part 2:
  731. * Wait until pipelined_send or expunge_all are outside of
  732. * wake_up_process(). There is a race with exit(), see
  733. * ipc/mqueue.c for the details.
  734. */
  735. msg = (struct msg_msg*)msr_d.r_msg;
  736. while (msg == NULL) {
  737. cpu_relax();
  738. msg = (struct msg_msg *)msr_d.r_msg;
  739. }
  740. /* Lockless receive, part 3:
  741. * If there is a message or an error then accept it without
  742. * locking.
  743. */
  744. if (msg != ERR_PTR(-EAGAIN)) {
  745. rcu_read_unlock();
  746. break;
  747. }
  748. /* Lockless receive, part 3:
  749. * Acquire the queue spinlock.
  750. */
  751. ipc_lock_by_ptr(&msq->q_perm);
  752. rcu_read_unlock();
  753. /* Lockless receive, part 4:
  754. * Repeat test after acquiring the spinlock.
  755. */
  756. msg = (struct msg_msg*)msr_d.r_msg;
  757. if (msg != ERR_PTR(-EAGAIN))
  758. goto out_unlock;
  759. list_del(&msr_d.r_list);
  760. if (signal_pending(current)) {
  761. msg = ERR_PTR(-ERESTARTNOHAND);
  762. out_unlock:
  763. msg_unlock(msq);
  764. break;
  765. }
  766. }
  767. if (IS_ERR(msg))
  768. return PTR_ERR(msg);
  769. msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
  770. *pmtype = msg->m_type;
  771. if (store_msg(mtext, msg, msgsz))
  772. msgsz = -EFAULT;
  773. free_msg(msg);
  774. return msgsz;
  775. }
  776. SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  777. long, msgtyp, int, msgflg)
  778. {
  779. long err, mtype;
  780. err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
  781. if (err < 0)
  782. goto out;
  783. if (put_user(mtype, &msgp->mtype))
  784. err = -EFAULT;
  785. out:
  786. return err;
  787. }
  788. #ifdef CONFIG_PROC_FS
  789. static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
  790. {
  791. struct user_namespace *user_ns = seq_user_ns(s);
  792. struct msg_queue *msq = it;
  793. return seq_printf(s,
  794. "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
  795. msq->q_perm.key,
  796. msq->q_perm.id,
  797. msq->q_perm.mode,
  798. msq->q_cbytes,
  799. msq->q_qnum,
  800. msq->q_lspid,
  801. msq->q_lrpid,
  802. from_kuid_munged(user_ns, msq->q_perm.uid),
  803. from_kgid_munged(user_ns, msq->q_perm.gid),
  804. from_kuid_munged(user_ns, msq->q_perm.cuid),
  805. from_kgid_munged(user_ns, msq->q_perm.cgid),
  806. msq->q_stime,
  807. msq->q_rtime,
  808. msq->q_ctime);
  809. }
  810. #endif