msg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. msq->q_qbytes = msqid64.msg_qbytes;
  372. ipc_update_perm(&msqid64.msg_perm, ipcp);
  373. msq->q_ctime = get_seconds();
  374. /* sleeping receivers might be excluded by
  375. * stricter permissions.
  376. */
  377. expunge_all(msq, -EAGAIN);
  378. /* sleeping senders might be able to send
  379. * due to a larger queue size.
  380. */
  381. ss_wakeup(&msq->q_senders, 0);
  382. break;
  383. default:
  384. err = -EINVAL;
  385. }
  386. out_unlock:
  387. msg_unlock(msq);
  388. out_up:
  389. up_write(&msg_ids(ns).rw_mutex);
  390. return err;
  391. }
  392. SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  393. {
  394. struct msg_queue *msq;
  395. int err, version;
  396. struct ipc_namespace *ns;
  397. if (msqid < 0 || cmd < 0)
  398. return -EINVAL;
  399. version = ipc_parse_version(&cmd);
  400. ns = current->nsproxy->ipc_ns;
  401. switch (cmd) {
  402. case IPC_INFO:
  403. case MSG_INFO:
  404. {
  405. struct msginfo msginfo;
  406. int max_id;
  407. if (!buf)
  408. return -EFAULT;
  409. /*
  410. * We must not return kernel stack data.
  411. * due to padding, it's not enough
  412. * to set all member fields.
  413. */
  414. err = security_msg_queue_msgctl(NULL, cmd);
  415. if (err)
  416. return err;
  417. memset(&msginfo, 0, sizeof(msginfo));
  418. msginfo.msgmni = ns->msg_ctlmni;
  419. msginfo.msgmax = ns->msg_ctlmax;
  420. msginfo.msgmnb = ns->msg_ctlmnb;
  421. msginfo.msgssz = MSGSSZ;
  422. msginfo.msgseg = MSGSEG;
  423. down_read(&msg_ids(ns).rw_mutex);
  424. if (cmd == MSG_INFO) {
  425. msginfo.msgpool = msg_ids(ns).in_use;
  426. msginfo.msgmap = atomic_read(&ns->msg_hdrs);
  427. msginfo.msgtql = atomic_read(&ns->msg_bytes);
  428. } else {
  429. msginfo.msgmap = MSGMAP;
  430. msginfo.msgpool = MSGPOOL;
  431. msginfo.msgtql = MSGTQL;
  432. }
  433. max_id = ipc_get_maxid(&msg_ids(ns));
  434. up_read(&msg_ids(ns).rw_mutex);
  435. if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
  436. return -EFAULT;
  437. return (max_id < 0) ? 0 : max_id;
  438. }
  439. case MSG_STAT: /* msqid is an index rather than a msg queue id */
  440. case IPC_STAT:
  441. {
  442. struct msqid64_ds tbuf;
  443. int success_return;
  444. if (!buf)
  445. return -EFAULT;
  446. if (cmd == MSG_STAT) {
  447. msq = msg_lock(ns, msqid);
  448. if (IS_ERR(msq))
  449. return PTR_ERR(msq);
  450. success_return = msq->q_perm.id;
  451. } else {
  452. msq = msg_lock_check(ns, msqid);
  453. if (IS_ERR(msq))
  454. return PTR_ERR(msq);
  455. success_return = 0;
  456. }
  457. err = -EACCES;
  458. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  459. goto out_unlock;
  460. err = security_msg_queue_msgctl(msq, cmd);
  461. if (err)
  462. goto out_unlock;
  463. memset(&tbuf, 0, sizeof(tbuf));
  464. kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
  465. tbuf.msg_stime = msq->q_stime;
  466. tbuf.msg_rtime = msq->q_rtime;
  467. tbuf.msg_ctime = msq->q_ctime;
  468. tbuf.msg_cbytes = msq->q_cbytes;
  469. tbuf.msg_qnum = msq->q_qnum;
  470. tbuf.msg_qbytes = msq->q_qbytes;
  471. tbuf.msg_lspid = msq->q_lspid;
  472. tbuf.msg_lrpid = msq->q_lrpid;
  473. msg_unlock(msq);
  474. if (copy_msqid_to_user(buf, &tbuf, version))
  475. return -EFAULT;
  476. return success_return;
  477. }
  478. case IPC_SET:
  479. case IPC_RMID:
  480. err = msgctl_down(ns, msqid, cmd, buf, version);
  481. return err;
  482. default:
  483. return -EINVAL;
  484. }
  485. out_unlock:
  486. msg_unlock(msq);
  487. return err;
  488. }
  489. static int testmsg(struct msg_msg *msg, long type, int mode)
  490. {
  491. switch(mode)
  492. {
  493. case SEARCH_ANY:
  494. return 1;
  495. case SEARCH_LESSEQUAL:
  496. if (msg->m_type <=type)
  497. return 1;
  498. break;
  499. case SEARCH_EQUAL:
  500. if (msg->m_type == type)
  501. return 1;
  502. break;
  503. case SEARCH_NOTEQUAL:
  504. if (msg->m_type != type)
  505. return 1;
  506. break;
  507. }
  508. return 0;
  509. }
  510. static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  511. {
  512. struct list_head *tmp;
  513. tmp = msq->q_receivers.next;
  514. while (tmp != &msq->q_receivers) {
  515. struct msg_receiver *msr;
  516. msr = list_entry(tmp, struct msg_receiver, r_list);
  517. tmp = tmp->next;
  518. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  519. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  520. msr->r_msgtype, msr->r_mode)) {
  521. list_del(&msr->r_list);
  522. if (msr->r_maxsize < msg->m_ts) {
  523. msr->r_msg = NULL;
  524. wake_up_process(msr->r_tsk);
  525. smp_mb();
  526. msr->r_msg = ERR_PTR(-E2BIG);
  527. } else {
  528. msr->r_msg = NULL;
  529. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  530. msq->q_rtime = get_seconds();
  531. wake_up_process(msr->r_tsk);
  532. smp_mb();
  533. msr->r_msg = msg;
  534. return 1;
  535. }
  536. }
  537. }
  538. return 0;
  539. }
  540. long do_msgsnd(int msqid, long mtype, void __user *mtext,
  541. size_t msgsz, int msgflg)
  542. {
  543. struct msg_queue *msq;
  544. struct msg_msg *msg;
  545. int err;
  546. struct ipc_namespace *ns;
  547. ns = current->nsproxy->ipc_ns;
  548. if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
  549. return -EINVAL;
  550. if (mtype < 1)
  551. return -EINVAL;
  552. msg = load_msg(mtext, msgsz);
  553. if (IS_ERR(msg))
  554. return PTR_ERR(msg);
  555. msg->m_type = mtype;
  556. msg->m_ts = msgsz;
  557. msq = msg_lock_check(ns, msqid);
  558. if (IS_ERR(msq)) {
  559. err = PTR_ERR(msq);
  560. goto out_free;
  561. }
  562. for (;;) {
  563. struct msg_sender s;
  564. err = -EACCES;
  565. if (ipcperms(ns, &msq->q_perm, S_IWUGO))
  566. goto out_unlock_free;
  567. err = security_msg_queue_msgsnd(msq, msg, msgflg);
  568. if (err)
  569. goto out_unlock_free;
  570. if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
  571. 1 + msq->q_qnum <= msq->q_qbytes) {
  572. break;
  573. }
  574. /* queue full, wait: */
  575. if (msgflg & IPC_NOWAIT) {
  576. err = -EAGAIN;
  577. goto out_unlock_free;
  578. }
  579. ss_add(msq, &s);
  580. ipc_rcu_getref(msq);
  581. msg_unlock(msq);
  582. schedule();
  583. ipc_lock_by_ptr(&msq->q_perm);
  584. ipc_rcu_putref(msq);
  585. if (msq->q_perm.deleted) {
  586. err = -EIDRM;
  587. goto out_unlock_free;
  588. }
  589. ss_del(&s);
  590. if (signal_pending(current)) {
  591. err = -ERESTARTNOHAND;
  592. goto out_unlock_free;
  593. }
  594. }
  595. msq->q_lspid = task_tgid_vnr(current);
  596. msq->q_stime = get_seconds();
  597. if (!pipelined_send(msq, msg)) {
  598. /* no one is waiting for this message, enqueue it */
  599. list_add_tail(&msg->m_list, &msq->q_messages);
  600. msq->q_cbytes += msgsz;
  601. msq->q_qnum++;
  602. atomic_add(msgsz, &ns->msg_bytes);
  603. atomic_inc(&ns->msg_hdrs);
  604. }
  605. err = 0;
  606. msg = NULL;
  607. out_unlock_free:
  608. msg_unlock(msq);
  609. out_free:
  610. if (msg != NULL)
  611. free_msg(msg);
  612. return err;
  613. }
  614. SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  615. int, msgflg)
  616. {
  617. long mtype;
  618. if (get_user(mtype, &msgp->mtype))
  619. return -EFAULT;
  620. return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
  621. }
  622. static inline int convert_mode(long *msgtyp, int msgflg)
  623. {
  624. /*
  625. * find message of correct type.
  626. * msgtyp = 0 => get first.
  627. * msgtyp > 0 => get first message of matching type.
  628. * msgtyp < 0 => get message with least type must be < abs(msgtype).
  629. */
  630. if (*msgtyp == 0)
  631. return SEARCH_ANY;
  632. if (*msgtyp < 0) {
  633. *msgtyp = -*msgtyp;
  634. return SEARCH_LESSEQUAL;
  635. }
  636. if (msgflg & MSG_EXCEPT)
  637. return SEARCH_NOTEQUAL;
  638. return SEARCH_EQUAL;
  639. }
  640. long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
  641. size_t msgsz, long msgtyp, int msgflg)
  642. {
  643. struct msg_queue *msq;
  644. struct msg_msg *msg;
  645. int mode;
  646. struct ipc_namespace *ns;
  647. if (msqid < 0 || (long) msgsz < 0)
  648. return -EINVAL;
  649. mode = convert_mode(&msgtyp, msgflg);
  650. ns = current->nsproxy->ipc_ns;
  651. msq = msg_lock_check(ns, msqid);
  652. if (IS_ERR(msq))
  653. return PTR_ERR(msq);
  654. for (;;) {
  655. struct msg_receiver msr_d;
  656. struct list_head *tmp;
  657. msg = ERR_PTR(-EACCES);
  658. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  659. goto out_unlock;
  660. msg = ERR_PTR(-EAGAIN);
  661. tmp = msq->q_messages.next;
  662. while (tmp != &msq->q_messages) {
  663. struct msg_msg *walk_msg;
  664. walk_msg = list_entry(tmp, struct msg_msg, m_list);
  665. if (testmsg(walk_msg, msgtyp, mode) &&
  666. !security_msg_queue_msgrcv(msq, walk_msg, current,
  667. msgtyp, mode)) {
  668. msg = walk_msg;
  669. if (mode == SEARCH_LESSEQUAL &&
  670. walk_msg->m_type != 1) {
  671. msg = walk_msg;
  672. msgtyp = walk_msg->m_type - 1;
  673. } else {
  674. msg = walk_msg;
  675. break;
  676. }
  677. }
  678. tmp = tmp->next;
  679. }
  680. if (!IS_ERR(msg)) {
  681. /*
  682. * Found a suitable message.
  683. * Unlink it from the queue.
  684. */
  685. if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
  686. msg = ERR_PTR(-E2BIG);
  687. goto out_unlock;
  688. }
  689. list_del(&msg->m_list);
  690. msq->q_qnum--;
  691. msq->q_rtime = get_seconds();
  692. msq->q_lrpid = task_tgid_vnr(current);
  693. msq->q_cbytes -= msg->m_ts;
  694. atomic_sub(msg->m_ts, &ns->msg_bytes);
  695. atomic_dec(&ns->msg_hdrs);
  696. ss_wakeup(&msq->q_senders, 0);
  697. msg_unlock(msq);
  698. break;
  699. }
  700. /* No message waiting. Wait for a message */
  701. if (msgflg & IPC_NOWAIT) {
  702. msg = ERR_PTR(-ENOMSG);
  703. goto out_unlock;
  704. }
  705. list_add_tail(&msr_d.r_list, &msq->q_receivers);
  706. msr_d.r_tsk = current;
  707. msr_d.r_msgtype = msgtyp;
  708. msr_d.r_mode = mode;
  709. if (msgflg & MSG_NOERROR)
  710. msr_d.r_maxsize = INT_MAX;
  711. else
  712. msr_d.r_maxsize = msgsz;
  713. msr_d.r_msg = ERR_PTR(-EAGAIN);
  714. current->state = TASK_INTERRUPTIBLE;
  715. msg_unlock(msq);
  716. schedule();
  717. /* Lockless receive, part 1:
  718. * Disable preemption. We don't hold a reference to the queue
  719. * and getting a reference would defeat the idea of a lockless
  720. * operation, thus the code relies on rcu to guarantee the
  721. * existence of msq:
  722. * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
  723. * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
  724. * rcu_read_lock() prevents preemption between reading r_msg
  725. * and the spin_lock() inside ipc_lock_by_ptr().
  726. */
  727. rcu_read_lock();
  728. /* Lockless receive, part 2:
  729. * Wait until pipelined_send or expunge_all are outside of
  730. * wake_up_process(). There is a race with exit(), see
  731. * ipc/mqueue.c for the details.
  732. */
  733. msg = (struct msg_msg*)msr_d.r_msg;
  734. while (msg == NULL) {
  735. cpu_relax();
  736. msg = (struct msg_msg *)msr_d.r_msg;
  737. }
  738. /* Lockless receive, part 3:
  739. * If there is a message or an error then accept it without
  740. * locking.
  741. */
  742. if (msg != ERR_PTR(-EAGAIN)) {
  743. rcu_read_unlock();
  744. break;
  745. }
  746. /* Lockless receive, part 3:
  747. * Acquire the queue spinlock.
  748. */
  749. ipc_lock_by_ptr(&msq->q_perm);
  750. rcu_read_unlock();
  751. /* Lockless receive, part 4:
  752. * Repeat test after acquiring the spinlock.
  753. */
  754. msg = (struct msg_msg*)msr_d.r_msg;
  755. if (msg != ERR_PTR(-EAGAIN))
  756. goto out_unlock;
  757. list_del(&msr_d.r_list);
  758. if (signal_pending(current)) {
  759. msg = ERR_PTR(-ERESTARTNOHAND);
  760. out_unlock:
  761. msg_unlock(msq);
  762. break;
  763. }
  764. }
  765. if (IS_ERR(msg))
  766. return PTR_ERR(msg);
  767. msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
  768. *pmtype = msg->m_type;
  769. if (store_msg(mtext, msg, msgsz))
  770. msgsz = -EFAULT;
  771. free_msg(msg);
  772. return msgsz;
  773. }
  774. SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  775. long, msgtyp, int, msgflg)
  776. {
  777. long err, mtype;
  778. err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
  779. if (err < 0)
  780. goto out;
  781. if (put_user(mtype, &msgp->mtype))
  782. err = -EFAULT;
  783. out:
  784. return err;
  785. }
  786. #ifdef CONFIG_PROC_FS
  787. static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
  788. {
  789. struct msg_queue *msq = it;
  790. return seq_printf(s,
  791. "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
  792. msq->q_perm.key,
  793. msq->q_perm.id,
  794. msq->q_perm.mode,
  795. msq->q_cbytes,
  796. msq->q_qnum,
  797. msq->q_lspid,
  798. msq->q_lrpid,
  799. msq->q_perm.uid,
  800. msq->q_perm.gid,
  801. msq->q_perm.cuid,
  802. msq->q_perm.cgid,
  803. msq->q_stime,
  804. msq->q_rtime,
  805. msq->q_ctime);
  806. }
  807. #endif