msg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 <linux/uaccess.h>
  41. #include "util.h"
  42. /* one msg_receiver structure for each sleeping receiver */
  43. struct msg_receiver {
  44. struct list_head r_list;
  45. struct task_struct *r_tsk;
  46. int r_mode;
  47. long r_msgtype;
  48. long r_maxsize;
  49. struct msg_msg *r_msg;
  50. };
  51. /* one msg_sender for each sleeping sender */
  52. struct msg_sender {
  53. struct list_head list;
  54. struct task_struct *tsk;
  55. size_t msgsz;
  56. };
  57. #define SEARCH_ANY 1
  58. #define SEARCH_EQUAL 2
  59. #define SEARCH_NOTEQUAL 3
  60. #define SEARCH_LESSEQUAL 4
  61. #define SEARCH_NUMBER 5
  62. #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
  63. static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
  64. {
  65. struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
  66. if (IS_ERR(ipcp))
  67. return ERR_CAST(ipcp);
  68. return container_of(ipcp, struct msg_queue, q_perm);
  69. }
  70. static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns,
  71. int id)
  72. {
  73. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id);
  74. if (IS_ERR(ipcp))
  75. return ERR_CAST(ipcp);
  76. return container_of(ipcp, struct msg_queue, q_perm);
  77. }
  78. static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
  79. {
  80. ipc_rmid(&msg_ids(ns), &s->q_perm);
  81. }
  82. static void msg_rcu_free(struct rcu_head *head)
  83. {
  84. struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
  85. struct msg_queue *msq = ipc_rcu_to_struct(p);
  86. security_msg_queue_free(msq);
  87. ipc_rcu_free(head);
  88. }
  89. /**
  90. * newque - Create a new msg queue
  91. * @ns: namespace
  92. * @params: ptr to the structure that contains the key and msgflg
  93. *
  94. * Called with msg_ids.rwsem held (writer)
  95. */
  96. static int newque(struct ipc_namespace *ns, struct ipc_params *params)
  97. {
  98. struct msg_queue *msq;
  99. int id, retval;
  100. key_t key = params->key;
  101. int msgflg = params->flg;
  102. msq = ipc_rcu_alloc(sizeof(*msq));
  103. if (!msq)
  104. return -ENOMEM;
  105. msq->q_perm.mode = msgflg & S_IRWXUGO;
  106. msq->q_perm.key = key;
  107. msq->q_perm.security = NULL;
  108. retval = security_msg_queue_alloc(msq);
  109. if (retval) {
  110. ipc_rcu_putref(msq, ipc_rcu_free);
  111. return retval;
  112. }
  113. msq->q_stime = msq->q_rtime = 0;
  114. msq->q_ctime = get_seconds();
  115. msq->q_cbytes = msq->q_qnum = 0;
  116. msq->q_qbytes = ns->msg_ctlmnb;
  117. msq->q_lspid = msq->q_lrpid = 0;
  118. INIT_LIST_HEAD(&msq->q_messages);
  119. INIT_LIST_HEAD(&msq->q_receivers);
  120. INIT_LIST_HEAD(&msq->q_senders);
  121. /* ipc_addid() locks msq upon success. */
  122. id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
  123. if (id < 0) {
  124. ipc_rcu_putref(msq, msg_rcu_free);
  125. return id;
  126. }
  127. ipc_unlock_object(&msq->q_perm);
  128. rcu_read_unlock();
  129. return msq->q_perm.id;
  130. }
  131. static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz)
  132. {
  133. return msgsz + msq->q_cbytes <= msq->q_qbytes &&
  134. 1 + msq->q_qnum <= msq->q_qbytes;
  135. }
  136. static inline void ss_add(struct msg_queue *msq,
  137. struct msg_sender *mss, size_t msgsz)
  138. {
  139. mss->tsk = current;
  140. mss->msgsz = msgsz;
  141. __set_current_state(TASK_INTERRUPTIBLE);
  142. list_add_tail(&mss->list, &msq->q_senders);
  143. }
  144. static inline void ss_del(struct msg_sender *mss)
  145. {
  146. if (mss->list.next)
  147. list_del(&mss->list);
  148. }
  149. static void ss_wakeup(struct msg_queue *msq,
  150. struct wake_q_head *wake_q, bool kill)
  151. {
  152. struct msg_sender *mss, *t;
  153. struct task_struct *stop_tsk = NULL;
  154. struct list_head *h = &msq->q_senders;
  155. list_for_each_entry_safe(mss, t, h, list) {
  156. if (kill)
  157. mss->list.next = NULL;
  158. /*
  159. * Stop at the first task we don't wakeup,
  160. * we've already iterated the original
  161. * sender queue.
  162. */
  163. else if (stop_tsk == mss->tsk)
  164. break;
  165. /*
  166. * We are not in an EIDRM scenario here, therefore
  167. * verify that we really need to wakeup the task.
  168. * To maintain current semantics and wakeup order,
  169. * move the sender to the tail on behalf of the
  170. * blocked task.
  171. */
  172. else if (!msg_fits_inqueue(msq, mss->msgsz)) {
  173. if (!stop_tsk)
  174. stop_tsk = mss->tsk;
  175. list_move_tail(&mss->list, &msq->q_senders);
  176. continue;
  177. }
  178. wake_q_add(wake_q, mss->tsk);
  179. }
  180. }
  181. static void expunge_all(struct msg_queue *msq, int res,
  182. struct wake_q_head *wake_q)
  183. {
  184. struct msg_receiver *msr, *t;
  185. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  186. wake_q_add(wake_q, msr->r_tsk);
  187. WRITE_ONCE(msr->r_msg, ERR_PTR(res));
  188. }
  189. }
  190. /*
  191. * freeque() wakes up waiters on the sender and receiver waiting queue,
  192. * removes the message queue from message queue ID IDR, and cleans up all the
  193. * messages associated with this queue.
  194. *
  195. * msg_ids.rwsem (writer) and the spinlock for this message queue are held
  196. * before freeque() is called. msg_ids.rwsem remains locked on exit.
  197. */
  198. static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  199. {
  200. struct msg_msg *msg, *t;
  201. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  202. WAKE_Q(wake_q);
  203. expunge_all(msq, -EIDRM, &wake_q);
  204. ss_wakeup(msq, &wake_q, true);
  205. msg_rmid(ns, msq);
  206. ipc_unlock_object(&msq->q_perm);
  207. wake_up_q(&wake_q);
  208. rcu_read_unlock();
  209. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  210. atomic_dec(&ns->msg_hdrs);
  211. free_msg(msg);
  212. }
  213. atomic_sub(msq->q_cbytes, &ns->msg_bytes);
  214. ipc_rcu_putref(msq, msg_rcu_free);
  215. }
  216. /*
  217. * Called with msg_ids.rwsem and ipcp locked.
  218. */
  219. static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
  220. {
  221. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  222. return security_msg_queue_associate(msq, msgflg);
  223. }
  224. SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
  225. {
  226. struct ipc_namespace *ns;
  227. static const struct ipc_ops msg_ops = {
  228. .getnew = newque,
  229. .associate = msg_security,
  230. };
  231. struct ipc_params msg_params;
  232. ns = current->nsproxy->ipc_ns;
  233. msg_params.key = key;
  234. msg_params.flg = msgflg;
  235. return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
  236. }
  237. static inline unsigned long
  238. copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
  239. {
  240. switch (version) {
  241. case IPC_64:
  242. return copy_to_user(buf, in, sizeof(*in));
  243. case IPC_OLD:
  244. {
  245. struct msqid_ds out;
  246. memset(&out, 0, sizeof(out));
  247. ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
  248. out.msg_stime = in->msg_stime;
  249. out.msg_rtime = in->msg_rtime;
  250. out.msg_ctime = in->msg_ctime;
  251. if (in->msg_cbytes > USHRT_MAX)
  252. out.msg_cbytes = USHRT_MAX;
  253. else
  254. out.msg_cbytes = in->msg_cbytes;
  255. out.msg_lcbytes = in->msg_cbytes;
  256. if (in->msg_qnum > USHRT_MAX)
  257. out.msg_qnum = USHRT_MAX;
  258. else
  259. out.msg_qnum = in->msg_qnum;
  260. if (in->msg_qbytes > USHRT_MAX)
  261. out.msg_qbytes = USHRT_MAX;
  262. else
  263. out.msg_qbytes = in->msg_qbytes;
  264. out.msg_lqbytes = in->msg_qbytes;
  265. out.msg_lspid = in->msg_lspid;
  266. out.msg_lrpid = in->msg_lrpid;
  267. return copy_to_user(buf, &out, sizeof(out));
  268. }
  269. default:
  270. return -EINVAL;
  271. }
  272. }
  273. static inline unsigned long
  274. copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
  275. {
  276. switch (version) {
  277. case IPC_64:
  278. if (copy_from_user(out, buf, sizeof(*out)))
  279. return -EFAULT;
  280. return 0;
  281. case IPC_OLD:
  282. {
  283. struct msqid_ds tbuf_old;
  284. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  285. return -EFAULT;
  286. out->msg_perm.uid = tbuf_old.msg_perm.uid;
  287. out->msg_perm.gid = tbuf_old.msg_perm.gid;
  288. out->msg_perm.mode = tbuf_old.msg_perm.mode;
  289. if (tbuf_old.msg_qbytes == 0)
  290. out->msg_qbytes = tbuf_old.msg_lqbytes;
  291. else
  292. out->msg_qbytes = tbuf_old.msg_qbytes;
  293. return 0;
  294. }
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. /*
  300. * This function handles some msgctl commands which require the rwsem
  301. * to be held in write mode.
  302. * NOTE: no locks must be held, the rwsem is taken inside this function.
  303. */
  304. static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  305. struct msqid_ds __user *buf, int version)
  306. {
  307. struct kern_ipc_perm *ipcp;
  308. struct msqid64_ds uninitialized_var(msqid64);
  309. struct msg_queue *msq;
  310. int err;
  311. if (cmd == IPC_SET) {
  312. if (copy_msqid_from_user(&msqid64, buf, version))
  313. return -EFAULT;
  314. }
  315. down_write(&msg_ids(ns).rwsem);
  316. rcu_read_lock();
  317. ipcp = ipcctl_pre_down_nolock(ns, &msg_ids(ns), msqid, cmd,
  318. &msqid64.msg_perm, msqid64.msg_qbytes);
  319. if (IS_ERR(ipcp)) {
  320. err = PTR_ERR(ipcp);
  321. goto out_unlock1;
  322. }
  323. msq = container_of(ipcp, struct msg_queue, q_perm);
  324. err = security_msg_queue_msgctl(msq, cmd);
  325. if (err)
  326. goto out_unlock1;
  327. switch (cmd) {
  328. case IPC_RMID:
  329. ipc_lock_object(&msq->q_perm);
  330. /* freeque unlocks the ipc object and rcu */
  331. freeque(ns, ipcp);
  332. goto out_up;
  333. case IPC_SET:
  334. {
  335. WAKE_Q(wake_q);
  336. if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
  337. !capable(CAP_SYS_RESOURCE)) {
  338. err = -EPERM;
  339. goto out_unlock1;
  340. }
  341. ipc_lock_object(&msq->q_perm);
  342. err = ipc_update_perm(&msqid64.msg_perm, ipcp);
  343. if (err)
  344. goto out_unlock0;
  345. msq->q_qbytes = msqid64.msg_qbytes;
  346. msq->q_ctime = get_seconds();
  347. /*
  348. * Sleeping receivers might be excluded by
  349. * stricter permissions.
  350. */
  351. expunge_all(msq, -EAGAIN, &wake_q);
  352. /*
  353. * Sleeping senders might be able to send
  354. * due to a larger queue size.
  355. */
  356. ss_wakeup(msq, &wake_q, false);
  357. ipc_unlock_object(&msq->q_perm);
  358. wake_up_q(&wake_q);
  359. goto out_unlock1;
  360. }
  361. default:
  362. err = -EINVAL;
  363. goto out_unlock1;
  364. }
  365. out_unlock0:
  366. ipc_unlock_object(&msq->q_perm);
  367. out_unlock1:
  368. rcu_read_unlock();
  369. out_up:
  370. up_write(&msg_ids(ns).rwsem);
  371. return err;
  372. }
  373. static int msgctl_nolock(struct ipc_namespace *ns, int msqid,
  374. int cmd, int version, void __user *buf)
  375. {
  376. int err;
  377. struct msg_queue *msq;
  378. switch (cmd) {
  379. case IPC_INFO:
  380. case MSG_INFO:
  381. {
  382. struct msginfo msginfo;
  383. int max_id;
  384. if (!buf)
  385. return -EFAULT;
  386. /*
  387. * We must not return kernel stack data.
  388. * due to padding, it's not enough
  389. * to set all member fields.
  390. */
  391. err = security_msg_queue_msgctl(NULL, cmd);
  392. if (err)
  393. return err;
  394. memset(&msginfo, 0, sizeof(msginfo));
  395. msginfo.msgmni = ns->msg_ctlmni;
  396. msginfo.msgmax = ns->msg_ctlmax;
  397. msginfo.msgmnb = ns->msg_ctlmnb;
  398. msginfo.msgssz = MSGSSZ;
  399. msginfo.msgseg = MSGSEG;
  400. down_read(&msg_ids(ns).rwsem);
  401. if (cmd == MSG_INFO) {
  402. msginfo.msgpool = msg_ids(ns).in_use;
  403. msginfo.msgmap = atomic_read(&ns->msg_hdrs);
  404. msginfo.msgtql = atomic_read(&ns->msg_bytes);
  405. } else {
  406. msginfo.msgmap = MSGMAP;
  407. msginfo.msgpool = MSGPOOL;
  408. msginfo.msgtql = MSGTQL;
  409. }
  410. max_id = ipc_get_maxid(&msg_ids(ns));
  411. up_read(&msg_ids(ns).rwsem);
  412. if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
  413. return -EFAULT;
  414. return (max_id < 0) ? 0 : max_id;
  415. }
  416. case MSG_STAT:
  417. case IPC_STAT:
  418. {
  419. struct msqid64_ds tbuf;
  420. int success_return;
  421. if (!buf)
  422. return -EFAULT;
  423. memset(&tbuf, 0, sizeof(tbuf));
  424. rcu_read_lock();
  425. if (cmd == MSG_STAT) {
  426. msq = msq_obtain_object(ns, msqid);
  427. if (IS_ERR(msq)) {
  428. err = PTR_ERR(msq);
  429. goto out_unlock;
  430. }
  431. success_return = msq->q_perm.id;
  432. } else {
  433. msq = msq_obtain_object_check(ns, msqid);
  434. if (IS_ERR(msq)) {
  435. err = PTR_ERR(msq);
  436. goto out_unlock;
  437. }
  438. success_return = 0;
  439. }
  440. err = -EACCES;
  441. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  442. goto out_unlock;
  443. err = security_msg_queue_msgctl(msq, cmd);
  444. if (err)
  445. goto out_unlock;
  446. kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
  447. tbuf.msg_stime = msq->q_stime;
  448. tbuf.msg_rtime = msq->q_rtime;
  449. tbuf.msg_ctime = msq->q_ctime;
  450. tbuf.msg_cbytes = msq->q_cbytes;
  451. tbuf.msg_qnum = msq->q_qnum;
  452. tbuf.msg_qbytes = msq->q_qbytes;
  453. tbuf.msg_lspid = msq->q_lspid;
  454. tbuf.msg_lrpid = msq->q_lrpid;
  455. rcu_read_unlock();
  456. if (copy_msqid_to_user(buf, &tbuf, version))
  457. return -EFAULT;
  458. return success_return;
  459. }
  460. default:
  461. return -EINVAL;
  462. }
  463. return err;
  464. out_unlock:
  465. rcu_read_unlock();
  466. return err;
  467. }
  468. SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  469. {
  470. int version;
  471. struct ipc_namespace *ns;
  472. if (msqid < 0 || cmd < 0)
  473. return -EINVAL;
  474. version = ipc_parse_version(&cmd);
  475. ns = current->nsproxy->ipc_ns;
  476. switch (cmd) {
  477. case IPC_INFO:
  478. case MSG_INFO:
  479. case MSG_STAT: /* msqid is an index rather than a msg queue id */
  480. case IPC_STAT:
  481. return msgctl_nolock(ns, msqid, cmd, version, buf);
  482. case IPC_SET:
  483. case IPC_RMID:
  484. return msgctl_down(ns, msqid, cmd, buf, version);
  485. default:
  486. return -EINVAL;
  487. }
  488. }
  489. static int testmsg(struct msg_msg *msg, long type, int mode)
  490. {
  491. switch (mode) {
  492. case SEARCH_ANY:
  493. case SEARCH_NUMBER:
  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. struct wake_q_head *wake_q)
  512. {
  513. struct msg_receiver *msr, *t;
  514. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  515. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  516. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  517. msr->r_msgtype, msr->r_mode)) {
  518. list_del(&msr->r_list);
  519. if (msr->r_maxsize < msg->m_ts) {
  520. wake_q_add(wake_q, msr->r_tsk);
  521. WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG));
  522. } else {
  523. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  524. msq->q_rtime = get_seconds();
  525. wake_q_add(wake_q, msr->r_tsk);
  526. WRITE_ONCE(msr->r_msg, msg);
  527. return 1;
  528. }
  529. }
  530. }
  531. return 0;
  532. }
  533. long do_msgsnd(int msqid, long mtype, void __user *mtext,
  534. size_t msgsz, int msgflg)
  535. {
  536. struct msg_queue *msq;
  537. struct msg_msg *msg;
  538. int err;
  539. struct ipc_namespace *ns;
  540. WAKE_Q(wake_q);
  541. ns = current->nsproxy->ipc_ns;
  542. if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
  543. return -EINVAL;
  544. if (mtype < 1)
  545. return -EINVAL;
  546. msg = load_msg(mtext, msgsz);
  547. if (IS_ERR(msg))
  548. return PTR_ERR(msg);
  549. msg->m_type = mtype;
  550. msg->m_ts = msgsz;
  551. rcu_read_lock();
  552. msq = msq_obtain_object_check(ns, msqid);
  553. if (IS_ERR(msq)) {
  554. err = PTR_ERR(msq);
  555. goto out_unlock1;
  556. }
  557. ipc_lock_object(&msq->q_perm);
  558. for (;;) {
  559. struct msg_sender s;
  560. err = -EACCES;
  561. if (ipcperms(ns, &msq->q_perm, S_IWUGO))
  562. goto out_unlock0;
  563. /* raced with RMID? */
  564. if (!ipc_valid_object(&msq->q_perm)) {
  565. err = -EIDRM;
  566. goto out_unlock0;
  567. }
  568. err = security_msg_queue_msgsnd(msq, msg, msgflg);
  569. if (err)
  570. goto out_unlock0;
  571. if (msg_fits_inqueue(msq, msgsz))
  572. break;
  573. /* queue full, wait: */
  574. if (msgflg & IPC_NOWAIT) {
  575. err = -EAGAIN;
  576. goto out_unlock0;
  577. }
  578. /* enqueue the sender and prepare to block */
  579. ss_add(msq, &s, msgsz);
  580. if (!ipc_rcu_getref(msq)) {
  581. err = -EIDRM;
  582. goto out_unlock0;
  583. }
  584. ipc_unlock_object(&msq->q_perm);
  585. rcu_read_unlock();
  586. schedule();
  587. rcu_read_lock();
  588. ipc_lock_object(&msq->q_perm);
  589. ipc_rcu_putref(msq, msg_rcu_free);
  590. /* raced with RMID? */
  591. if (!ipc_valid_object(&msq->q_perm)) {
  592. err = -EIDRM;
  593. goto out_unlock0;
  594. }
  595. ss_del(&s);
  596. if (signal_pending(current)) {
  597. err = -ERESTARTNOHAND;
  598. goto out_unlock0;
  599. }
  600. }
  601. msq->q_lspid = task_tgid_vnr(current);
  602. msq->q_stime = get_seconds();
  603. if (!pipelined_send(msq, msg, &wake_q)) {
  604. /* no one is waiting for this message, enqueue it */
  605. list_add_tail(&msg->m_list, &msq->q_messages);
  606. msq->q_cbytes += msgsz;
  607. msq->q_qnum++;
  608. atomic_add(msgsz, &ns->msg_bytes);
  609. atomic_inc(&ns->msg_hdrs);
  610. }
  611. err = 0;
  612. msg = NULL;
  613. out_unlock0:
  614. ipc_unlock_object(&msq->q_perm);
  615. wake_up_q(&wake_q);
  616. out_unlock1:
  617. rcu_read_unlock();
  618. if (msg != NULL)
  619. free_msg(msg);
  620. return err;
  621. }
  622. SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  623. int, msgflg)
  624. {
  625. long mtype;
  626. if (get_user(mtype, &msgp->mtype))
  627. return -EFAULT;
  628. return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
  629. }
  630. static inline int convert_mode(long *msgtyp, int msgflg)
  631. {
  632. if (msgflg & MSG_COPY)
  633. return SEARCH_NUMBER;
  634. /*
  635. * find message of correct type.
  636. * msgtyp = 0 => get first.
  637. * msgtyp > 0 => get first message of matching type.
  638. * msgtyp < 0 => get message with least type must be < abs(msgtype).
  639. */
  640. if (*msgtyp == 0)
  641. return SEARCH_ANY;
  642. if (*msgtyp < 0) {
  643. if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */
  644. *msgtyp = LONG_MAX;
  645. else
  646. *msgtyp = -*msgtyp;
  647. return SEARCH_LESSEQUAL;
  648. }
  649. if (msgflg & MSG_EXCEPT)
  650. return SEARCH_NOTEQUAL;
  651. return SEARCH_EQUAL;
  652. }
  653. static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
  654. {
  655. struct msgbuf __user *msgp = dest;
  656. size_t msgsz;
  657. if (put_user(msg->m_type, &msgp->mtype))
  658. return -EFAULT;
  659. msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
  660. if (store_msg(msgp->mtext, msg, msgsz))
  661. return -EFAULT;
  662. return msgsz;
  663. }
  664. #ifdef CONFIG_CHECKPOINT_RESTORE
  665. /*
  666. * This function creates new kernel message structure, large enough to store
  667. * bufsz message bytes.
  668. */
  669. static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
  670. {
  671. struct msg_msg *copy;
  672. /*
  673. * Create dummy message to copy real message to.
  674. */
  675. copy = load_msg(buf, bufsz);
  676. if (!IS_ERR(copy))
  677. copy->m_ts = bufsz;
  678. return copy;
  679. }
  680. static inline void free_copy(struct msg_msg *copy)
  681. {
  682. if (copy)
  683. free_msg(copy);
  684. }
  685. #else
  686. static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
  687. {
  688. return ERR_PTR(-ENOSYS);
  689. }
  690. static inline void free_copy(struct msg_msg *copy)
  691. {
  692. }
  693. #endif
  694. static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
  695. {
  696. struct msg_msg *msg, *found = NULL;
  697. long count = 0;
  698. list_for_each_entry(msg, &msq->q_messages, m_list) {
  699. if (testmsg(msg, *msgtyp, mode) &&
  700. !security_msg_queue_msgrcv(msq, msg, current,
  701. *msgtyp, mode)) {
  702. if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
  703. *msgtyp = msg->m_type - 1;
  704. found = msg;
  705. } else if (mode == SEARCH_NUMBER) {
  706. if (*msgtyp == count)
  707. return msg;
  708. } else
  709. return msg;
  710. count++;
  711. }
  712. }
  713. return found ?: ERR_PTR(-EAGAIN);
  714. }
  715. long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
  716. long (*msg_handler)(void __user *, struct msg_msg *, size_t))
  717. {
  718. int mode;
  719. struct msg_queue *msq;
  720. struct ipc_namespace *ns;
  721. struct msg_msg *msg, *copy = NULL;
  722. WAKE_Q(wake_q);
  723. ns = current->nsproxy->ipc_ns;
  724. if (msqid < 0 || (long) bufsz < 0)
  725. return -EINVAL;
  726. if (msgflg & MSG_COPY) {
  727. if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
  728. return -EINVAL;
  729. copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
  730. if (IS_ERR(copy))
  731. return PTR_ERR(copy);
  732. }
  733. mode = convert_mode(&msgtyp, msgflg);
  734. rcu_read_lock();
  735. msq = msq_obtain_object_check(ns, msqid);
  736. if (IS_ERR(msq)) {
  737. rcu_read_unlock();
  738. free_copy(copy);
  739. return PTR_ERR(msq);
  740. }
  741. for (;;) {
  742. struct msg_receiver msr_d;
  743. msg = ERR_PTR(-EACCES);
  744. if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  745. goto out_unlock1;
  746. ipc_lock_object(&msq->q_perm);
  747. /* raced with RMID? */
  748. if (!ipc_valid_object(&msq->q_perm)) {
  749. msg = ERR_PTR(-EIDRM);
  750. goto out_unlock0;
  751. }
  752. msg = find_msg(msq, &msgtyp, mode);
  753. if (!IS_ERR(msg)) {
  754. /*
  755. * Found a suitable message.
  756. * Unlink it from the queue.
  757. */
  758. if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
  759. msg = ERR_PTR(-E2BIG);
  760. goto out_unlock0;
  761. }
  762. /*
  763. * If we are copying, then do not unlink message and do
  764. * not update queue parameters.
  765. */
  766. if (msgflg & MSG_COPY) {
  767. msg = copy_msg(msg, copy);
  768. goto out_unlock0;
  769. }
  770. list_del(&msg->m_list);
  771. msq->q_qnum--;
  772. msq->q_rtime = get_seconds();
  773. msq->q_lrpid = task_tgid_vnr(current);
  774. msq->q_cbytes -= msg->m_ts;
  775. atomic_sub(msg->m_ts, &ns->msg_bytes);
  776. atomic_dec(&ns->msg_hdrs);
  777. ss_wakeup(msq, &wake_q, false);
  778. goto out_unlock0;
  779. }
  780. /* No message waiting. Wait for a message */
  781. if (msgflg & IPC_NOWAIT) {
  782. msg = ERR_PTR(-ENOMSG);
  783. goto out_unlock0;
  784. }
  785. list_add_tail(&msr_d.r_list, &msq->q_receivers);
  786. msr_d.r_tsk = current;
  787. msr_d.r_msgtype = msgtyp;
  788. msr_d.r_mode = mode;
  789. if (msgflg & MSG_NOERROR)
  790. msr_d.r_maxsize = INT_MAX;
  791. else
  792. msr_d.r_maxsize = bufsz;
  793. msr_d.r_msg = ERR_PTR(-EAGAIN);
  794. __set_current_state(TASK_INTERRUPTIBLE);
  795. ipc_unlock_object(&msq->q_perm);
  796. rcu_read_unlock();
  797. schedule();
  798. /*
  799. * Lockless receive, part 1:
  800. * We don't hold a reference to the queue and getting a
  801. * reference would defeat the idea of a lockless operation,
  802. * thus the code relies on rcu to guarantee the existence of
  803. * msq:
  804. * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
  805. * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
  806. */
  807. rcu_read_lock();
  808. /*
  809. * Lockless receive, part 2:
  810. * The work in pipelined_send() and expunge_all():
  811. * - Set pointer to message
  812. * - Queue the receiver task for later wakeup
  813. * - Wake up the process after the lock is dropped.
  814. *
  815. * Should the process wake up before this wakeup (due to a
  816. * signal) it will either see the message and continue ...
  817. */
  818. msg = READ_ONCE(msr_d.r_msg);
  819. if (msg != ERR_PTR(-EAGAIN))
  820. goto out_unlock1;
  821. /*
  822. * ... or see -EAGAIN, acquire the lock to check the message
  823. * again.
  824. */
  825. ipc_lock_object(&msq->q_perm);
  826. msg = msr_d.r_msg;
  827. if (msg != ERR_PTR(-EAGAIN))
  828. goto out_unlock0;
  829. list_del(&msr_d.r_list);
  830. if (signal_pending(current)) {
  831. msg = ERR_PTR(-ERESTARTNOHAND);
  832. goto out_unlock0;
  833. }
  834. ipc_unlock_object(&msq->q_perm);
  835. }
  836. out_unlock0:
  837. ipc_unlock_object(&msq->q_perm);
  838. wake_up_q(&wake_q);
  839. out_unlock1:
  840. rcu_read_unlock();
  841. if (IS_ERR(msg)) {
  842. free_copy(copy);
  843. return PTR_ERR(msg);
  844. }
  845. bufsz = msg_handler(buf, msg, bufsz);
  846. free_msg(msg);
  847. return bufsz;
  848. }
  849. SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  850. long, msgtyp, int, msgflg)
  851. {
  852. return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
  853. }
  854. void msg_init_ns(struct ipc_namespace *ns)
  855. {
  856. ns->msg_ctlmax = MSGMAX;
  857. ns->msg_ctlmnb = MSGMNB;
  858. ns->msg_ctlmni = MSGMNI;
  859. atomic_set(&ns->msg_bytes, 0);
  860. atomic_set(&ns->msg_hdrs, 0);
  861. ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
  862. }
  863. #ifdef CONFIG_IPC_NS
  864. void msg_exit_ns(struct ipc_namespace *ns)
  865. {
  866. free_ipcs(ns, &msg_ids(ns), freeque);
  867. idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
  868. }
  869. #endif
  870. #ifdef CONFIG_PROC_FS
  871. static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
  872. {
  873. struct user_namespace *user_ns = seq_user_ns(s);
  874. struct msg_queue *msq = it;
  875. seq_printf(s,
  876. "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
  877. msq->q_perm.key,
  878. msq->q_perm.id,
  879. msq->q_perm.mode,
  880. msq->q_cbytes,
  881. msq->q_qnum,
  882. msq->q_lspid,
  883. msq->q_lrpid,
  884. from_kuid_munged(user_ns, msq->q_perm.uid),
  885. from_kgid_munged(user_ns, msq->q_perm.gid),
  886. from_kuid_munged(user_ns, msq->q_perm.cuid),
  887. from_kgid_munged(user_ns, msq->q_perm.cgid),
  888. msq->q_stime,
  889. msq->q_rtime,
  890. msq->q_ctime);
  891. return 0;
  892. }
  893. #endif
  894. void __init msg_init(void)
  895. {
  896. msg_init_ns(&init_ipc_ns);
  897. ipc_init_proc_interface("sysvipc/msg",
  898. " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
  899. IPC_MSG_IDS, sysvipc_msg_proc_show);
  900. }