mqueue.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /*
  2. * POSIX message queues filesystem for Linux.
  3. *
  4. * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
  5. * Michal Wronski (michal.wronski@gmail.com)
  6. *
  7. * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
  8. * Lockless receive & send, fd based notify:
  9. * Manfred Spraul (manfred@colorfullife.com)
  10. *
  11. * Audit: George Wilson (ltcgcw@us.ibm.com)
  12. *
  13. * This file is released under the GPL.
  14. */
  15. #include <linux/capability.h>
  16. #include <linux/init.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/file.h>
  19. #include <linux/mount.h>
  20. #include <linux/fs_context.h>
  21. #include <linux/namei.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/poll.h>
  24. #include <linux/mqueue.h>
  25. #include <linux/msg.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/netlink.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/audit.h>
  31. #include <linux/signal.h>
  32. #include <linux/mutex.h>
  33. #include <linux/nsproxy.h>
  34. #include <linux/pid.h>
  35. #include <linux/ipc_namespace.h>
  36. #include <linux/user_namespace.h>
  37. #include <linux/slab.h>
  38. #include <linux/sched/wake_q.h>
  39. #include <linux/sched/signal.h>
  40. #include <linux/sched/user.h>
  41. #include <net/sock.h>
  42. #include "util.h"
  43. struct mqueue_fs_context {
  44. struct ipc_namespace *ipc_ns;
  45. bool newns; /* Set if newly created ipc namespace */
  46. };
  47. #define MQUEUE_MAGIC 0x19800202
  48. #define DIRENT_SIZE 20
  49. #define FILENT_SIZE 80
  50. #define SEND 0
  51. #define RECV 1
  52. #define STATE_NONE 0
  53. #define STATE_READY 1
  54. struct posix_msg_tree_node {
  55. struct rb_node rb_node;
  56. struct list_head msg_list;
  57. int priority;
  58. };
  59. /*
  60. * Locking:
  61. *
  62. * Accesses to a message queue are synchronized by acquiring info->lock.
  63. *
  64. * There are two notable exceptions:
  65. * - The actual wakeup of a sleeping task is performed using the wake_q
  66. * framework. info->lock is already released when wake_up_q is called.
  67. * - The exit codepaths after sleeping check ext_wait_queue->state without
  68. * any locks. If it is STATE_READY, then the syscall is completed without
  69. * acquiring info->lock.
  70. *
  71. * MQ_BARRIER:
  72. * To achieve proper release/acquire memory barrier pairing, the state is set to
  73. * STATE_READY with smp_store_release(), and it is read with READ_ONCE followed
  74. * by smp_acquire__after_ctrl_dep(). In addition, wake_q_add_safe() is used.
  75. *
  76. * This prevents the following races:
  77. *
  78. * 1) With the simple wake_q_add(), the task could be gone already before
  79. * the increase of the reference happens
  80. * Thread A
  81. * Thread B
  82. * WRITE_ONCE(wait.state, STATE_NONE);
  83. * schedule_hrtimeout()
  84. * wake_q_add(A)
  85. * if (cmpxchg()) // success
  86. * ->state = STATE_READY (reordered)
  87. * <timeout returns>
  88. * if (wait.state == STATE_READY) return;
  89. * sysret to user space
  90. * sys_exit()
  91. * get_task_struct() // UaF
  92. *
  93. * Solution: Use wake_q_add_safe() and perform the get_task_struct() before
  94. * the smp_store_release() that does ->state = STATE_READY.
  95. *
  96. * 2) Without proper _release/_acquire barriers, the woken up task
  97. * could read stale data
  98. *
  99. * Thread A
  100. * Thread B
  101. * do_mq_timedreceive
  102. * WRITE_ONCE(wait.state, STATE_NONE);
  103. * schedule_hrtimeout()
  104. * state = STATE_READY;
  105. * <timeout returns>
  106. * if (wait.state == STATE_READY) return;
  107. * msg_ptr = wait.msg; // Access to stale data!
  108. * receiver->msg = message; (reordered)
  109. *
  110. * Solution: use _release and _acquire barriers.
  111. *
  112. * 3) There is intentionally no barrier when setting current->state
  113. * to TASK_INTERRUPTIBLE: spin_unlock(&info->lock) provides the
  114. * release memory barrier, and the wakeup is triggered when holding
  115. * info->lock, i.e. spin_lock(&info->lock) provided a pairing
  116. * acquire memory barrier.
  117. */
  118. struct ext_wait_queue { /* queue of sleeping tasks */
  119. struct task_struct *task;
  120. struct list_head list;
  121. struct msg_msg *msg; /* ptr of loaded message */
  122. int state; /* one of STATE_* values */
  123. };
  124. struct mqueue_inode_info {
  125. spinlock_t lock;
  126. struct inode vfs_inode;
  127. wait_queue_head_t wait_q;
  128. struct rb_root msg_tree;
  129. struct rb_node *msg_tree_rightmost;
  130. struct posix_msg_tree_node *node_cache;
  131. struct mq_attr attr;
  132. struct sigevent notify;
  133. struct pid *notify_owner;
  134. u32 notify_self_exec_id;
  135. struct user_namespace *notify_user_ns;
  136. struct ucounts *ucounts; /* user who created, for accounting */
  137. struct sock *notify_sock;
  138. struct sk_buff *notify_cookie;
  139. /* for tasks waiting for free space and messages, respectively */
  140. struct ext_wait_queue e_wait_q[2];
  141. unsigned long qsize; /* size of queue in memory (sum of all msgs) */
  142. };
  143. static struct file_system_type mqueue_fs_type;
  144. static const struct inode_operations mqueue_dir_inode_operations;
  145. static const struct file_operations mqueue_file_operations;
  146. static const struct super_operations mqueue_super_ops;
  147. static const struct fs_context_operations mqueue_fs_context_ops;
  148. static void remove_notification(struct mqueue_inode_info *info);
  149. static struct kmem_cache *mqueue_inode_cachep;
  150. static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
  151. {
  152. return container_of(inode, struct mqueue_inode_info, vfs_inode);
  153. }
  154. /*
  155. * This routine should be called with the mq_lock held.
  156. */
  157. static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
  158. {
  159. return get_ipc_ns(inode->i_sb->s_fs_info);
  160. }
  161. static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
  162. {
  163. struct ipc_namespace *ns;
  164. spin_lock(&mq_lock);
  165. ns = __get_ns_from_inode(inode);
  166. spin_unlock(&mq_lock);
  167. return ns;
  168. }
  169. /* Auxiliary functions to manipulate messages' list */
  170. static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
  171. {
  172. struct rb_node **p, *parent = NULL;
  173. struct posix_msg_tree_node *leaf;
  174. bool rightmost = true;
  175. p = &info->msg_tree.rb_node;
  176. while (*p) {
  177. parent = *p;
  178. leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
  179. if (likely(leaf->priority == msg->m_type))
  180. goto insert_msg;
  181. else if (msg->m_type < leaf->priority) {
  182. p = &(*p)->rb_left;
  183. rightmost = false;
  184. } else
  185. p = &(*p)->rb_right;
  186. }
  187. if (info->node_cache) {
  188. leaf = info->node_cache;
  189. info->node_cache = NULL;
  190. } else {
  191. leaf = kmalloc(sizeof(*leaf), GFP_ATOMIC);
  192. if (!leaf)
  193. return -ENOMEM;
  194. INIT_LIST_HEAD(&leaf->msg_list);
  195. }
  196. leaf->priority = msg->m_type;
  197. if (rightmost)
  198. info->msg_tree_rightmost = &leaf->rb_node;
  199. rb_link_node(&leaf->rb_node, parent, p);
  200. rb_insert_color(&leaf->rb_node, &info->msg_tree);
  201. insert_msg:
  202. info->attr.mq_curmsgs++;
  203. info->qsize += msg->m_ts;
  204. list_add_tail(&msg->m_list, &leaf->msg_list);
  205. return 0;
  206. }
  207. static inline void msg_tree_erase(struct posix_msg_tree_node *leaf,
  208. struct mqueue_inode_info *info)
  209. {
  210. struct rb_node *node = &leaf->rb_node;
  211. if (info->msg_tree_rightmost == node)
  212. info->msg_tree_rightmost = rb_prev(node);
  213. rb_erase(node, &info->msg_tree);
  214. if (info->node_cache)
  215. kfree(leaf);
  216. else
  217. info->node_cache = leaf;
  218. }
  219. static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
  220. {
  221. struct rb_node *parent = NULL;
  222. struct posix_msg_tree_node *leaf;
  223. struct msg_msg *msg;
  224. try_again:
  225. /*
  226. * During insert, low priorities go to the left and high to the
  227. * right. On receive, we want the highest priorities first, so
  228. * walk all the way to the right.
  229. */
  230. parent = info->msg_tree_rightmost;
  231. if (!parent) {
  232. if (info->attr.mq_curmsgs) {
  233. pr_warn_once("Inconsistency in POSIX message queue, "
  234. "no tree element, but supposedly messages "
  235. "should exist!\n");
  236. info->attr.mq_curmsgs = 0;
  237. }
  238. return NULL;
  239. }
  240. leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
  241. if (unlikely(list_empty(&leaf->msg_list))) {
  242. pr_warn_once("Inconsistency in POSIX message queue, "
  243. "empty leaf node but we haven't implemented "
  244. "lazy leaf delete!\n");
  245. msg_tree_erase(leaf, info);
  246. goto try_again;
  247. } else {
  248. msg = list_first_entry(&leaf->msg_list,
  249. struct msg_msg, m_list);
  250. list_del(&msg->m_list);
  251. if (list_empty(&leaf->msg_list)) {
  252. msg_tree_erase(leaf, info);
  253. }
  254. }
  255. info->attr.mq_curmsgs--;
  256. info->qsize -= msg->m_ts;
  257. return msg;
  258. }
  259. static struct inode *mqueue_get_inode(struct super_block *sb,
  260. struct ipc_namespace *ipc_ns, umode_t mode,
  261. struct mq_attr *attr)
  262. {
  263. struct inode *inode;
  264. int ret = -ENOMEM;
  265. inode = new_inode(sb);
  266. if (!inode)
  267. goto err;
  268. inode->i_ino = get_next_ino();
  269. inode->i_mode = mode;
  270. inode->i_uid = current_fsuid();
  271. inode->i_gid = current_fsgid();
  272. simple_inode_init_ts(inode);
  273. if (S_ISREG(mode)) {
  274. struct mqueue_inode_info *info;
  275. unsigned long mq_bytes, mq_treesize;
  276. inode->i_fop = &mqueue_file_operations;
  277. inode->i_size = FILENT_SIZE;
  278. /* mqueue specific info */
  279. info = MQUEUE_I(inode);
  280. spin_lock_init(&info->lock);
  281. init_waitqueue_head(&info->wait_q);
  282. INIT_LIST_HEAD(&info->e_wait_q[0].list);
  283. INIT_LIST_HEAD(&info->e_wait_q[1].list);
  284. info->notify_owner = NULL;
  285. info->notify_user_ns = NULL;
  286. info->qsize = 0;
  287. info->ucounts = NULL; /* set when all is ok */
  288. info->msg_tree = RB_ROOT;
  289. info->msg_tree_rightmost = NULL;
  290. info->node_cache = NULL;
  291. memset(&info->attr, 0, sizeof(info->attr));
  292. info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
  293. ipc_ns->mq_msg_default);
  294. info->attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
  295. ipc_ns->mq_msgsize_default);
  296. if (attr) {
  297. info->attr.mq_maxmsg = attr->mq_maxmsg;
  298. info->attr.mq_msgsize = attr->mq_msgsize;
  299. }
  300. /*
  301. * We used to allocate a static array of pointers and account
  302. * the size of that array as well as one msg_msg struct per
  303. * possible message into the queue size. That's no longer
  304. * accurate as the queue is now an rbtree and will grow and
  305. * shrink depending on usage patterns. We can, however, still
  306. * account one msg_msg struct per message, but the nodes are
  307. * allocated depending on priority usage, and most programs
  308. * only use one, or a handful, of priorities. However, since
  309. * this is pinned memory, we need to assume worst case, so
  310. * that means the min(mq_maxmsg, max_priorities) * struct
  311. * posix_msg_tree_node.
  312. */
  313. ret = -EINVAL;
  314. if (info->attr.mq_maxmsg <= 0 || info->attr.mq_msgsize <= 0)
  315. goto out_inode;
  316. if (capable(CAP_SYS_RESOURCE)) {
  317. if (info->attr.mq_maxmsg > HARD_MSGMAX ||
  318. info->attr.mq_msgsize > HARD_MSGSIZEMAX)
  319. goto out_inode;
  320. } else {
  321. if (info->attr.mq_maxmsg > ipc_ns->mq_msg_max ||
  322. info->attr.mq_msgsize > ipc_ns->mq_msgsize_max)
  323. goto out_inode;
  324. }
  325. ret = -EOVERFLOW;
  326. /* check for overflow */
  327. if (info->attr.mq_msgsize > ULONG_MAX/info->attr.mq_maxmsg)
  328. goto out_inode;
  329. mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
  330. min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
  331. sizeof(struct posix_msg_tree_node);
  332. mq_bytes = info->attr.mq_maxmsg * info->attr.mq_msgsize;
  333. if (mq_bytes + mq_treesize < mq_bytes)
  334. goto out_inode;
  335. mq_bytes += mq_treesize;
  336. info->ucounts = get_ucounts(current_ucounts());
  337. if (info->ucounts) {
  338. long msgqueue;
  339. spin_lock(&mq_lock);
  340. msgqueue = inc_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
  341. if (msgqueue == LONG_MAX || msgqueue > rlimit(RLIMIT_MSGQUEUE)) {
  342. dec_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
  343. spin_unlock(&mq_lock);
  344. put_ucounts(info->ucounts);
  345. info->ucounts = NULL;
  346. /* mqueue_evict_inode() releases info->messages */
  347. ret = -EMFILE;
  348. goto out_inode;
  349. }
  350. spin_unlock(&mq_lock);
  351. }
  352. } else if (S_ISDIR(mode)) {
  353. inc_nlink(inode);
  354. /* Some things misbehave if size == 0 on a directory */
  355. inode->i_size = 2 * DIRENT_SIZE;
  356. inode->i_op = &mqueue_dir_inode_operations;
  357. inode->i_fop = &simple_dir_operations;
  358. }
  359. return inode;
  360. out_inode:
  361. iput(inode);
  362. err:
  363. return ERR_PTR(ret);
  364. }
  365. static int mqueue_fill_super(struct super_block *sb, struct fs_context *fc)
  366. {
  367. struct inode *inode;
  368. struct ipc_namespace *ns = sb->s_fs_info;
  369. sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
  370. sb->s_blocksize = PAGE_SIZE;
  371. sb->s_blocksize_bits = PAGE_SHIFT;
  372. sb->s_magic = MQUEUE_MAGIC;
  373. sb->s_op = &mqueue_super_ops;
  374. inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
  375. if (IS_ERR(inode))
  376. return PTR_ERR(inode);
  377. sb->s_root = d_make_root(inode);
  378. if (!sb->s_root)
  379. return -ENOMEM;
  380. return 0;
  381. }
  382. static int mqueue_get_tree(struct fs_context *fc)
  383. {
  384. struct mqueue_fs_context *ctx = fc->fs_private;
  385. /*
  386. * With a newly created ipc namespace, we don't need to do a search
  387. * for an ipc namespace match, but we still need to set s_fs_info.
  388. */
  389. if (ctx->newns) {
  390. fc->s_fs_info = ctx->ipc_ns;
  391. return get_tree_nodev(fc, mqueue_fill_super);
  392. }
  393. return get_tree_keyed(fc, mqueue_fill_super, ctx->ipc_ns);
  394. }
  395. static void mqueue_fs_context_free(struct fs_context *fc)
  396. {
  397. struct mqueue_fs_context *ctx = fc->fs_private;
  398. put_ipc_ns(ctx->ipc_ns);
  399. kfree(ctx);
  400. }
  401. static int mqueue_init_fs_context(struct fs_context *fc)
  402. {
  403. struct mqueue_fs_context *ctx;
  404. ctx = kzalloc(sizeof(struct mqueue_fs_context), GFP_KERNEL);
  405. if (!ctx)
  406. return -ENOMEM;
  407. ctx->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
  408. put_user_ns(fc->user_ns);
  409. fc->user_ns = get_user_ns(ctx->ipc_ns->user_ns);
  410. fc->fs_private = ctx;
  411. fc->ops = &mqueue_fs_context_ops;
  412. return 0;
  413. }
  414. /*
  415. * mq_init_ns() is currently the only caller of mq_create_mount().
  416. * So the ns parameter is always a newly created ipc namespace.
  417. */
  418. static struct vfsmount *mq_create_mount(struct ipc_namespace *ns)
  419. {
  420. struct mqueue_fs_context *ctx;
  421. struct fs_context *fc;
  422. struct vfsmount *mnt;
  423. fc = fs_context_for_mount(&mqueue_fs_type, SB_KERNMOUNT);
  424. if (IS_ERR(fc))
  425. return ERR_CAST(fc);
  426. ctx = fc->fs_private;
  427. ctx->newns = true;
  428. put_ipc_ns(ctx->ipc_ns);
  429. ctx->ipc_ns = get_ipc_ns(ns);
  430. put_user_ns(fc->user_ns);
  431. fc->user_ns = get_user_ns(ctx->ipc_ns->user_ns);
  432. mnt = fc_mount(fc);
  433. put_fs_context(fc);
  434. return mnt;
  435. }
  436. static void init_once(void *foo)
  437. {
  438. struct mqueue_inode_info *p = foo;
  439. inode_init_once(&p->vfs_inode);
  440. }
  441. static struct inode *mqueue_alloc_inode(struct super_block *sb)
  442. {
  443. struct mqueue_inode_info *ei;
  444. ei = alloc_inode_sb(sb, mqueue_inode_cachep, GFP_KERNEL);
  445. if (!ei)
  446. return NULL;
  447. return &ei->vfs_inode;
  448. }
  449. static void mqueue_free_inode(struct inode *inode)
  450. {
  451. kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
  452. }
  453. static void mqueue_evict_inode(struct inode *inode)
  454. {
  455. struct mqueue_inode_info *info;
  456. struct ipc_namespace *ipc_ns;
  457. struct msg_msg *msg, *nmsg;
  458. LIST_HEAD(tmp_msg);
  459. clear_inode(inode);
  460. if (S_ISDIR(inode->i_mode))
  461. return;
  462. ipc_ns = get_ns_from_inode(inode);
  463. info = MQUEUE_I(inode);
  464. spin_lock(&info->lock);
  465. while ((msg = msg_get(info)) != NULL)
  466. list_add_tail(&msg->m_list, &tmp_msg);
  467. kfree(info->node_cache);
  468. spin_unlock(&info->lock);
  469. list_for_each_entry_safe(msg, nmsg, &tmp_msg, m_list) {
  470. list_del(&msg->m_list);
  471. free_msg(msg);
  472. }
  473. if (info->ucounts) {
  474. unsigned long mq_bytes, mq_treesize;
  475. /* Total amount of bytes accounted for the mqueue */
  476. mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
  477. min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
  478. sizeof(struct posix_msg_tree_node);
  479. mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
  480. info->attr.mq_msgsize);
  481. spin_lock(&mq_lock);
  482. dec_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
  483. /*
  484. * get_ns_from_inode() ensures that the
  485. * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
  486. * to which we now hold a reference, or it is NULL.
  487. * We can't put it here under mq_lock, though.
  488. */
  489. if (ipc_ns)
  490. ipc_ns->mq_queues_count--;
  491. spin_unlock(&mq_lock);
  492. put_ucounts(info->ucounts);
  493. info->ucounts = NULL;
  494. }
  495. if (ipc_ns)
  496. put_ipc_ns(ipc_ns);
  497. }
  498. static int mqueue_create_attr(struct dentry *dentry, umode_t mode, void *arg)
  499. {
  500. struct inode *dir = dentry->d_parent->d_inode;
  501. struct inode *inode;
  502. struct mq_attr *attr = arg;
  503. int error;
  504. struct ipc_namespace *ipc_ns;
  505. spin_lock(&mq_lock);
  506. ipc_ns = __get_ns_from_inode(dir);
  507. if (!ipc_ns) {
  508. error = -EACCES;
  509. goto out_unlock;
  510. }
  511. if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
  512. !capable(CAP_SYS_RESOURCE)) {
  513. error = -ENOSPC;
  514. goto out_unlock;
  515. }
  516. ipc_ns->mq_queues_count++;
  517. spin_unlock(&mq_lock);
  518. inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
  519. if (IS_ERR(inode)) {
  520. error = PTR_ERR(inode);
  521. spin_lock(&mq_lock);
  522. ipc_ns->mq_queues_count--;
  523. goto out_unlock;
  524. }
  525. put_ipc_ns(ipc_ns);
  526. dir->i_size += DIRENT_SIZE;
  527. simple_inode_init_ts(dir);
  528. d_instantiate(dentry, inode);
  529. dget(dentry);
  530. return 0;
  531. out_unlock:
  532. spin_unlock(&mq_lock);
  533. if (ipc_ns)
  534. put_ipc_ns(ipc_ns);
  535. return error;
  536. }
  537. static int mqueue_create(struct mnt_idmap *idmap, struct inode *dir,
  538. struct dentry *dentry, umode_t mode, bool excl)
  539. {
  540. return mqueue_create_attr(dentry, mode, NULL);
  541. }
  542. static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
  543. {
  544. struct inode *inode = d_inode(dentry);
  545. simple_inode_init_ts(dir);
  546. dir->i_size -= DIRENT_SIZE;
  547. drop_nlink(inode);
  548. dput(dentry);
  549. return 0;
  550. }
  551. /*
  552. * This is routine for system read from queue file.
  553. * To avoid mess with doing here some sort of mq_receive we allow
  554. * to read only queue size & notification info (the only values
  555. * that are interesting from user point of view and aren't accessible
  556. * through std routines)
  557. */
  558. static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
  559. size_t count, loff_t *off)
  560. {
  561. struct inode *inode = file_inode(filp);
  562. struct mqueue_inode_info *info = MQUEUE_I(inode);
  563. char buffer[FILENT_SIZE];
  564. ssize_t ret;
  565. spin_lock(&info->lock);
  566. snprintf(buffer, sizeof(buffer),
  567. "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
  568. info->qsize,
  569. info->notify_owner ? info->notify.sigev_notify : 0,
  570. (info->notify_owner &&
  571. info->notify.sigev_notify == SIGEV_SIGNAL) ?
  572. info->notify.sigev_signo : 0,
  573. pid_vnr(info->notify_owner));
  574. spin_unlock(&info->lock);
  575. buffer[sizeof(buffer)-1] = '\0';
  576. ret = simple_read_from_buffer(u_data, count, off, buffer,
  577. strlen(buffer));
  578. if (ret <= 0)
  579. return ret;
  580. inode_set_atime_to_ts(inode, inode_set_ctime_current(inode));
  581. return ret;
  582. }
  583. static int mqueue_flush_file(struct file *filp, fl_owner_t id)
  584. {
  585. struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
  586. spin_lock(&info->lock);
  587. if (task_tgid(current) == info->notify_owner)
  588. remove_notification(info);
  589. spin_unlock(&info->lock);
  590. return 0;
  591. }
  592. static __poll_t mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
  593. {
  594. struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
  595. __poll_t retval = 0;
  596. poll_wait(filp, &info->wait_q, poll_tab);
  597. spin_lock(&info->lock);
  598. if (info->attr.mq_curmsgs)
  599. retval = EPOLLIN | EPOLLRDNORM;
  600. if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
  601. retval |= EPOLLOUT | EPOLLWRNORM;
  602. spin_unlock(&info->lock);
  603. return retval;
  604. }
  605. /* Adds current to info->e_wait_q[sr] before element with smaller prio */
  606. static void wq_add(struct mqueue_inode_info *info, int sr,
  607. struct ext_wait_queue *ewp)
  608. {
  609. struct ext_wait_queue *walk;
  610. list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
  611. if (walk->task->prio <= current->prio) {
  612. list_add_tail(&ewp->list, &walk->list);
  613. return;
  614. }
  615. }
  616. list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
  617. }
  618. /*
  619. * Puts current task to sleep. Caller must hold queue lock. After return
  620. * lock isn't held.
  621. * sr: SEND or RECV
  622. */
  623. static int wq_sleep(struct mqueue_inode_info *info, int sr,
  624. ktime_t *timeout, struct ext_wait_queue *ewp)
  625. __releases(&info->lock)
  626. {
  627. int retval;
  628. signed long time;
  629. wq_add(info, sr, ewp);
  630. for (;;) {
  631. /* memory barrier not required, we hold info->lock */
  632. __set_current_state(TASK_INTERRUPTIBLE);
  633. spin_unlock(&info->lock);
  634. time = schedule_hrtimeout_range_clock(timeout, 0,
  635. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  636. if (READ_ONCE(ewp->state) == STATE_READY) {
  637. /* see MQ_BARRIER for purpose/pairing */
  638. smp_acquire__after_ctrl_dep();
  639. retval = 0;
  640. goto out;
  641. }
  642. spin_lock(&info->lock);
  643. /* we hold info->lock, so no memory barrier required */
  644. if (READ_ONCE(ewp->state) == STATE_READY) {
  645. retval = 0;
  646. goto out_unlock;
  647. }
  648. if (signal_pending(current)) {
  649. retval = -ERESTARTSYS;
  650. break;
  651. }
  652. if (time == 0) {
  653. retval = -ETIMEDOUT;
  654. break;
  655. }
  656. }
  657. list_del(&ewp->list);
  658. out_unlock:
  659. spin_unlock(&info->lock);
  660. out:
  661. return retval;
  662. }
  663. /*
  664. * Returns waiting task that should be serviced first or NULL if none exists
  665. */
  666. static struct ext_wait_queue *wq_get_first_waiter(
  667. struct mqueue_inode_info *info, int sr)
  668. {
  669. struct list_head *ptr;
  670. ptr = info->e_wait_q[sr].list.prev;
  671. if (ptr == &info->e_wait_q[sr].list)
  672. return NULL;
  673. return list_entry(ptr, struct ext_wait_queue, list);
  674. }
  675. static inline void set_cookie(struct sk_buff *skb, char code)
  676. {
  677. ((char *)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
  678. }
  679. /*
  680. * The next function is only to split too long sys_mq_timedsend
  681. */
  682. static void __do_notify(struct mqueue_inode_info *info)
  683. {
  684. /* notification
  685. * invoked when there is registered process and there isn't process
  686. * waiting synchronously for message AND state of queue changed from
  687. * empty to not empty. Here we are sure that no one is waiting
  688. * synchronously. */
  689. if (info->notify_owner &&
  690. info->attr.mq_curmsgs == 1) {
  691. switch (info->notify.sigev_notify) {
  692. case SIGEV_NONE:
  693. break;
  694. case SIGEV_SIGNAL: {
  695. struct kernel_siginfo sig_i;
  696. struct task_struct *task;
  697. /* do_mq_notify() accepts sigev_signo == 0, why?? */
  698. if (!info->notify.sigev_signo)
  699. break;
  700. clear_siginfo(&sig_i);
  701. sig_i.si_signo = info->notify.sigev_signo;
  702. sig_i.si_errno = 0;
  703. sig_i.si_code = SI_MESGQ;
  704. sig_i.si_value = info->notify.sigev_value;
  705. rcu_read_lock();
  706. /* map current pid/uid into info->owner's namespaces */
  707. sig_i.si_pid = task_tgid_nr_ns(current,
  708. ns_of_pid(info->notify_owner));
  709. sig_i.si_uid = from_kuid_munged(info->notify_user_ns,
  710. current_uid());
  711. /*
  712. * We can't use kill_pid_info(), this signal should
  713. * bypass check_kill_permission(). It is from kernel
  714. * but si_fromuser() can't know this.
  715. * We do check the self_exec_id, to avoid sending
  716. * signals to programs that don't expect them.
  717. */
  718. task = pid_task(info->notify_owner, PIDTYPE_TGID);
  719. if (task && task->self_exec_id ==
  720. info->notify_self_exec_id) {
  721. do_send_sig_info(info->notify.sigev_signo,
  722. &sig_i, task, PIDTYPE_TGID);
  723. }
  724. rcu_read_unlock();
  725. break;
  726. }
  727. case SIGEV_THREAD:
  728. set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
  729. netlink_sendskb(info->notify_sock, info->notify_cookie);
  730. break;
  731. }
  732. /* after notification unregisters process */
  733. put_pid(info->notify_owner);
  734. put_user_ns(info->notify_user_ns);
  735. info->notify_owner = NULL;
  736. info->notify_user_ns = NULL;
  737. }
  738. wake_up(&info->wait_q);
  739. }
  740. static int prepare_timeout(const struct __kernel_timespec __user *u_abs_timeout,
  741. struct timespec64 *ts)
  742. {
  743. if (get_timespec64(ts, u_abs_timeout))
  744. return -EFAULT;
  745. if (!timespec64_valid(ts))
  746. return -EINVAL;
  747. return 0;
  748. }
  749. static void remove_notification(struct mqueue_inode_info *info)
  750. {
  751. if (info->notify_owner != NULL &&
  752. info->notify.sigev_notify == SIGEV_THREAD) {
  753. set_cookie(info->notify_cookie, NOTIFY_REMOVED);
  754. netlink_sendskb(info->notify_sock, info->notify_cookie);
  755. }
  756. put_pid(info->notify_owner);
  757. put_user_ns(info->notify_user_ns);
  758. info->notify_owner = NULL;
  759. info->notify_user_ns = NULL;
  760. }
  761. static int prepare_open(struct dentry *dentry, int oflag, int ro,
  762. umode_t mode, struct filename *name,
  763. struct mq_attr *attr)
  764. {
  765. static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  766. MAY_READ | MAY_WRITE };
  767. int acc;
  768. if (d_really_is_negative(dentry)) {
  769. if (!(oflag & O_CREAT))
  770. return -ENOENT;
  771. if (ro)
  772. return ro;
  773. audit_inode_parent_hidden(name, dentry->d_parent);
  774. return vfs_mkobj(dentry, mode & ~current_umask(),
  775. mqueue_create_attr, attr);
  776. }
  777. /* it already existed */
  778. audit_inode(name, dentry, 0);
  779. if ((oflag & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  780. return -EEXIST;
  781. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
  782. return -EINVAL;
  783. acc = oflag2acc[oflag & O_ACCMODE];
  784. return inode_permission(&nop_mnt_idmap, d_inode(dentry), acc);
  785. }
  786. static int do_mq_open(const char __user *u_name, int oflag, umode_t mode,
  787. struct mq_attr *attr)
  788. {
  789. struct vfsmount *mnt = current->nsproxy->ipc_ns->mq_mnt;
  790. struct dentry *root = mnt->mnt_root;
  791. struct filename *name;
  792. struct path path;
  793. int fd, error;
  794. int ro;
  795. audit_mq_open(oflag, mode, attr);
  796. name = getname(u_name);
  797. if (IS_ERR(name))
  798. return PTR_ERR(name);
  799. fd = get_unused_fd_flags(O_CLOEXEC);
  800. if (fd < 0)
  801. goto out_putname;
  802. ro = mnt_want_write(mnt); /* we'll drop it in any case */
  803. inode_lock(d_inode(root));
  804. path.dentry = lookup_one_len(name->name, root, strlen(name->name));
  805. if (IS_ERR(path.dentry)) {
  806. error = PTR_ERR(path.dentry);
  807. goto out_putfd;
  808. }
  809. path.mnt = mntget(mnt);
  810. error = prepare_open(path.dentry, oflag, ro, mode, name, attr);
  811. if (!error) {
  812. struct file *file = dentry_open(&path, oflag, current_cred());
  813. if (!IS_ERR(file))
  814. fd_install(fd, file);
  815. else
  816. error = PTR_ERR(file);
  817. }
  818. path_put(&path);
  819. out_putfd:
  820. if (error) {
  821. put_unused_fd(fd);
  822. fd = error;
  823. }
  824. inode_unlock(d_inode(root));
  825. if (!ro)
  826. mnt_drop_write(mnt);
  827. out_putname:
  828. putname(name);
  829. return fd;
  830. }
  831. SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
  832. struct mq_attr __user *, u_attr)
  833. {
  834. struct mq_attr attr;
  835. if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
  836. return -EFAULT;
  837. return do_mq_open(u_name, oflag, mode, u_attr ? &attr : NULL);
  838. }
  839. SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
  840. {
  841. int err;
  842. struct filename *name;
  843. struct dentry *dentry;
  844. struct inode *inode = NULL;
  845. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  846. struct vfsmount *mnt = ipc_ns->mq_mnt;
  847. name = getname(u_name);
  848. if (IS_ERR(name))
  849. return PTR_ERR(name);
  850. audit_inode_parent_hidden(name, mnt->mnt_root);
  851. err = mnt_want_write(mnt);
  852. if (err)
  853. goto out_name;
  854. inode_lock_nested(d_inode(mnt->mnt_root), I_MUTEX_PARENT);
  855. dentry = lookup_one_len(name->name, mnt->mnt_root,
  856. strlen(name->name));
  857. if (IS_ERR(dentry)) {
  858. err = PTR_ERR(dentry);
  859. goto out_unlock;
  860. }
  861. inode = d_inode(dentry);
  862. if (!inode) {
  863. err = -ENOENT;
  864. } else {
  865. ihold(inode);
  866. err = vfs_unlink(&nop_mnt_idmap, d_inode(dentry->d_parent),
  867. dentry, NULL);
  868. }
  869. dput(dentry);
  870. out_unlock:
  871. inode_unlock(d_inode(mnt->mnt_root));
  872. iput(inode);
  873. mnt_drop_write(mnt);
  874. out_name:
  875. putname(name);
  876. return err;
  877. }
  878. /* Pipelined send and receive functions.
  879. *
  880. * If a receiver finds no waiting message, then it registers itself in the
  881. * list of waiting receivers. A sender checks that list before adding the new
  882. * message into the message array. If there is a waiting receiver, then it
  883. * bypasses the message array and directly hands the message over to the
  884. * receiver. The receiver accepts the message and returns without grabbing the
  885. * queue spinlock:
  886. *
  887. * - Set pointer to message.
  888. * - Queue the receiver task for later wakeup (without the info->lock).
  889. * - Update its state to STATE_READY. Now the receiver can continue.
  890. * - Wake up the process after the lock is dropped. Should the process wake up
  891. * before this wakeup (due to a timeout or a signal) it will either see
  892. * STATE_READY and continue or acquire the lock to check the state again.
  893. *
  894. * The same algorithm is used for senders.
  895. */
  896. static inline void __pipelined_op(struct wake_q_head *wake_q,
  897. struct mqueue_inode_info *info,
  898. struct ext_wait_queue *this)
  899. {
  900. struct task_struct *task;
  901. list_del(&this->list);
  902. task = get_task_struct(this->task);
  903. /* see MQ_BARRIER for purpose/pairing */
  904. smp_store_release(&this->state, STATE_READY);
  905. wake_q_add_safe(wake_q, task);
  906. }
  907. /* pipelined_send() - send a message directly to the task waiting in
  908. * sys_mq_timedreceive() (without inserting message into a queue).
  909. */
  910. static inline void pipelined_send(struct wake_q_head *wake_q,
  911. struct mqueue_inode_info *info,
  912. struct msg_msg *message,
  913. struct ext_wait_queue *receiver)
  914. {
  915. receiver->msg = message;
  916. __pipelined_op(wake_q, info, receiver);
  917. }
  918. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  919. * gets its message and put to the queue (we have one free place for sure). */
  920. static inline void pipelined_receive(struct wake_q_head *wake_q,
  921. struct mqueue_inode_info *info)
  922. {
  923. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  924. if (!sender) {
  925. /* for poll */
  926. wake_up_interruptible(&info->wait_q);
  927. return;
  928. }
  929. if (msg_insert(sender->msg, info))
  930. return;
  931. __pipelined_op(wake_q, info, sender);
  932. }
  933. static int do_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
  934. size_t msg_len, unsigned int msg_prio,
  935. struct timespec64 *ts)
  936. {
  937. struct fd f;
  938. struct inode *inode;
  939. struct ext_wait_queue wait;
  940. struct ext_wait_queue *receiver;
  941. struct msg_msg *msg_ptr;
  942. struct mqueue_inode_info *info;
  943. ktime_t expires, *timeout = NULL;
  944. struct posix_msg_tree_node *new_leaf = NULL;
  945. int ret = 0;
  946. DEFINE_WAKE_Q(wake_q);
  947. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  948. return -EINVAL;
  949. if (ts) {
  950. expires = timespec64_to_ktime(*ts);
  951. timeout = &expires;
  952. }
  953. audit_mq_sendrecv(mqdes, msg_len, msg_prio, ts);
  954. f = fdget(mqdes);
  955. if (unlikely(!fd_file(f))) {
  956. ret = -EBADF;
  957. goto out;
  958. }
  959. inode = file_inode(fd_file(f));
  960. if (unlikely(fd_file(f)->f_op != &mqueue_file_operations)) {
  961. ret = -EBADF;
  962. goto out_fput;
  963. }
  964. info = MQUEUE_I(inode);
  965. audit_file(fd_file(f));
  966. if (unlikely(!(fd_file(f)->f_mode & FMODE_WRITE))) {
  967. ret = -EBADF;
  968. goto out_fput;
  969. }
  970. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  971. ret = -EMSGSIZE;
  972. goto out_fput;
  973. }
  974. /* First try to allocate memory, before doing anything with
  975. * existing queues. */
  976. msg_ptr = load_msg(u_msg_ptr, msg_len);
  977. if (IS_ERR(msg_ptr)) {
  978. ret = PTR_ERR(msg_ptr);
  979. goto out_fput;
  980. }
  981. msg_ptr->m_ts = msg_len;
  982. msg_ptr->m_type = msg_prio;
  983. /*
  984. * msg_insert really wants us to have a valid, spare node struct so
  985. * it doesn't have to kmalloc a GFP_ATOMIC allocation, but it will
  986. * fall back to that if necessary.
  987. */
  988. if (!info->node_cache)
  989. new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
  990. spin_lock(&info->lock);
  991. if (!info->node_cache && new_leaf) {
  992. /* Save our speculative allocation into the cache */
  993. INIT_LIST_HEAD(&new_leaf->msg_list);
  994. info->node_cache = new_leaf;
  995. new_leaf = NULL;
  996. } else {
  997. kfree(new_leaf);
  998. }
  999. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  1000. if (fd_file(f)->f_flags & O_NONBLOCK) {
  1001. ret = -EAGAIN;
  1002. } else {
  1003. wait.task = current;
  1004. wait.msg = (void *) msg_ptr;
  1005. /* memory barrier not required, we hold info->lock */
  1006. WRITE_ONCE(wait.state, STATE_NONE);
  1007. ret = wq_sleep(info, SEND, timeout, &wait);
  1008. /*
  1009. * wq_sleep must be called with info->lock held, and
  1010. * returns with the lock released
  1011. */
  1012. goto out_free;
  1013. }
  1014. } else {
  1015. receiver = wq_get_first_waiter(info, RECV);
  1016. if (receiver) {
  1017. pipelined_send(&wake_q, info, msg_ptr, receiver);
  1018. } else {
  1019. /* adds message to the queue */
  1020. ret = msg_insert(msg_ptr, info);
  1021. if (ret)
  1022. goto out_unlock;
  1023. __do_notify(info);
  1024. }
  1025. simple_inode_init_ts(inode);
  1026. }
  1027. out_unlock:
  1028. spin_unlock(&info->lock);
  1029. wake_up_q(&wake_q);
  1030. out_free:
  1031. if (ret)
  1032. free_msg(msg_ptr);
  1033. out_fput:
  1034. fdput(f);
  1035. out:
  1036. return ret;
  1037. }
  1038. static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
  1039. size_t msg_len, unsigned int __user *u_msg_prio,
  1040. struct timespec64 *ts)
  1041. {
  1042. ssize_t ret;
  1043. struct msg_msg *msg_ptr;
  1044. struct fd f;
  1045. struct inode *inode;
  1046. struct mqueue_inode_info *info;
  1047. struct ext_wait_queue wait;
  1048. ktime_t expires, *timeout = NULL;
  1049. struct posix_msg_tree_node *new_leaf = NULL;
  1050. if (ts) {
  1051. expires = timespec64_to_ktime(*ts);
  1052. timeout = &expires;
  1053. }
  1054. audit_mq_sendrecv(mqdes, msg_len, 0, ts);
  1055. f = fdget(mqdes);
  1056. if (unlikely(!fd_file(f))) {
  1057. ret = -EBADF;
  1058. goto out;
  1059. }
  1060. inode = file_inode(fd_file(f));
  1061. if (unlikely(fd_file(f)->f_op != &mqueue_file_operations)) {
  1062. ret = -EBADF;
  1063. goto out_fput;
  1064. }
  1065. info = MQUEUE_I(inode);
  1066. audit_file(fd_file(f));
  1067. if (unlikely(!(fd_file(f)->f_mode & FMODE_READ))) {
  1068. ret = -EBADF;
  1069. goto out_fput;
  1070. }
  1071. /* checks if buffer is big enough */
  1072. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  1073. ret = -EMSGSIZE;
  1074. goto out_fput;
  1075. }
  1076. /*
  1077. * msg_insert really wants us to have a valid, spare node struct so
  1078. * it doesn't have to kmalloc a GFP_ATOMIC allocation, but it will
  1079. * fall back to that if necessary.
  1080. */
  1081. if (!info->node_cache)
  1082. new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
  1083. spin_lock(&info->lock);
  1084. if (!info->node_cache && new_leaf) {
  1085. /* Save our speculative allocation into the cache */
  1086. INIT_LIST_HEAD(&new_leaf->msg_list);
  1087. info->node_cache = new_leaf;
  1088. } else {
  1089. kfree(new_leaf);
  1090. }
  1091. if (info->attr.mq_curmsgs == 0) {
  1092. if (fd_file(f)->f_flags & O_NONBLOCK) {
  1093. spin_unlock(&info->lock);
  1094. ret = -EAGAIN;
  1095. } else {
  1096. wait.task = current;
  1097. /* memory barrier not required, we hold info->lock */
  1098. WRITE_ONCE(wait.state, STATE_NONE);
  1099. ret = wq_sleep(info, RECV, timeout, &wait);
  1100. msg_ptr = wait.msg;
  1101. }
  1102. } else {
  1103. DEFINE_WAKE_Q(wake_q);
  1104. msg_ptr = msg_get(info);
  1105. simple_inode_init_ts(inode);
  1106. /* There is now free space in queue. */
  1107. pipelined_receive(&wake_q, info);
  1108. spin_unlock(&info->lock);
  1109. wake_up_q(&wake_q);
  1110. ret = 0;
  1111. }
  1112. if (ret == 0) {
  1113. ret = msg_ptr->m_ts;
  1114. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  1115. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  1116. ret = -EFAULT;
  1117. }
  1118. free_msg(msg_ptr);
  1119. }
  1120. out_fput:
  1121. fdput(f);
  1122. out:
  1123. return ret;
  1124. }
  1125. SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
  1126. size_t, msg_len, unsigned int, msg_prio,
  1127. const struct __kernel_timespec __user *, u_abs_timeout)
  1128. {
  1129. struct timespec64 ts, *p = NULL;
  1130. if (u_abs_timeout) {
  1131. int res = prepare_timeout(u_abs_timeout, &ts);
  1132. if (res)
  1133. return res;
  1134. p = &ts;
  1135. }
  1136. return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
  1137. }
  1138. SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
  1139. size_t, msg_len, unsigned int __user *, u_msg_prio,
  1140. const struct __kernel_timespec __user *, u_abs_timeout)
  1141. {
  1142. struct timespec64 ts, *p = NULL;
  1143. if (u_abs_timeout) {
  1144. int res = prepare_timeout(u_abs_timeout, &ts);
  1145. if (res)
  1146. return res;
  1147. p = &ts;
  1148. }
  1149. return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
  1150. }
  1151. /*
  1152. * Notes: the case when user wants us to deregister (with NULL as pointer)
  1153. * and he isn't currently owner of notification, will be silently discarded.
  1154. * It isn't explicitly defined in the POSIX.
  1155. */
  1156. static int do_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  1157. {
  1158. int ret;
  1159. struct fd f;
  1160. struct sock *sock;
  1161. struct inode *inode;
  1162. struct mqueue_inode_info *info;
  1163. struct sk_buff *nc;
  1164. audit_mq_notify(mqdes, notification);
  1165. nc = NULL;
  1166. sock = NULL;
  1167. if (notification != NULL) {
  1168. if (unlikely(notification->sigev_notify != SIGEV_NONE &&
  1169. notification->sigev_notify != SIGEV_SIGNAL &&
  1170. notification->sigev_notify != SIGEV_THREAD))
  1171. return -EINVAL;
  1172. if (notification->sigev_notify == SIGEV_SIGNAL &&
  1173. !valid_signal(notification->sigev_signo)) {
  1174. return -EINVAL;
  1175. }
  1176. if (notification->sigev_notify == SIGEV_THREAD) {
  1177. long timeo;
  1178. /* create the notify skb */
  1179. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  1180. if (!nc)
  1181. return -ENOMEM;
  1182. if (copy_from_user(nc->data,
  1183. notification->sigev_value.sival_ptr,
  1184. NOTIFY_COOKIE_LEN)) {
  1185. ret = -EFAULT;
  1186. goto free_skb;
  1187. }
  1188. /* TODO: add a header? */
  1189. skb_put(nc, NOTIFY_COOKIE_LEN);
  1190. /* and attach it to the socket */
  1191. retry:
  1192. f = fdget(notification->sigev_signo);
  1193. if (!fd_file(f)) {
  1194. ret = -EBADF;
  1195. goto out;
  1196. }
  1197. sock = netlink_getsockbyfilp(fd_file(f));
  1198. fdput(f);
  1199. if (IS_ERR(sock)) {
  1200. ret = PTR_ERR(sock);
  1201. goto free_skb;
  1202. }
  1203. timeo = MAX_SCHEDULE_TIMEOUT;
  1204. ret = netlink_attachskb(sock, nc, &timeo, NULL);
  1205. if (ret == 1) {
  1206. sock = NULL;
  1207. goto retry;
  1208. }
  1209. if (ret)
  1210. return ret;
  1211. }
  1212. }
  1213. f = fdget(mqdes);
  1214. if (!fd_file(f)) {
  1215. ret = -EBADF;
  1216. goto out;
  1217. }
  1218. inode = file_inode(fd_file(f));
  1219. if (unlikely(fd_file(f)->f_op != &mqueue_file_operations)) {
  1220. ret = -EBADF;
  1221. goto out_fput;
  1222. }
  1223. info = MQUEUE_I(inode);
  1224. ret = 0;
  1225. spin_lock(&info->lock);
  1226. if (notification == NULL) {
  1227. if (info->notify_owner == task_tgid(current)) {
  1228. remove_notification(info);
  1229. inode_set_atime_to_ts(inode,
  1230. inode_set_ctime_current(inode));
  1231. }
  1232. } else if (info->notify_owner != NULL) {
  1233. ret = -EBUSY;
  1234. } else {
  1235. switch (notification->sigev_notify) {
  1236. case SIGEV_NONE:
  1237. info->notify.sigev_notify = SIGEV_NONE;
  1238. break;
  1239. case SIGEV_THREAD:
  1240. info->notify_sock = sock;
  1241. info->notify_cookie = nc;
  1242. sock = NULL;
  1243. nc = NULL;
  1244. info->notify.sigev_notify = SIGEV_THREAD;
  1245. break;
  1246. case SIGEV_SIGNAL:
  1247. info->notify.sigev_signo = notification->sigev_signo;
  1248. info->notify.sigev_value = notification->sigev_value;
  1249. info->notify.sigev_notify = SIGEV_SIGNAL;
  1250. info->notify_self_exec_id = current->self_exec_id;
  1251. break;
  1252. }
  1253. info->notify_owner = get_pid(task_tgid(current));
  1254. info->notify_user_ns = get_user_ns(current_user_ns());
  1255. inode_set_atime_to_ts(inode, inode_set_ctime_current(inode));
  1256. }
  1257. spin_unlock(&info->lock);
  1258. out_fput:
  1259. fdput(f);
  1260. out:
  1261. if (sock)
  1262. netlink_detachskb(sock, nc);
  1263. else
  1264. free_skb:
  1265. dev_kfree_skb(nc);
  1266. return ret;
  1267. }
  1268. SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  1269. const struct sigevent __user *, u_notification)
  1270. {
  1271. struct sigevent n, *p = NULL;
  1272. if (u_notification) {
  1273. if (copy_from_user(&n, u_notification, sizeof(struct sigevent)))
  1274. return -EFAULT;
  1275. p = &n;
  1276. }
  1277. return do_mq_notify(mqdes, p);
  1278. }
  1279. static int do_mq_getsetattr(int mqdes, struct mq_attr *new, struct mq_attr *old)
  1280. {
  1281. struct fd f;
  1282. struct inode *inode;
  1283. struct mqueue_inode_info *info;
  1284. if (new && (new->mq_flags & (~O_NONBLOCK)))
  1285. return -EINVAL;
  1286. f = fdget(mqdes);
  1287. if (!fd_file(f))
  1288. return -EBADF;
  1289. if (unlikely(fd_file(f)->f_op != &mqueue_file_operations)) {
  1290. fdput(f);
  1291. return -EBADF;
  1292. }
  1293. inode = file_inode(fd_file(f));
  1294. info = MQUEUE_I(inode);
  1295. spin_lock(&info->lock);
  1296. if (old) {
  1297. *old = info->attr;
  1298. old->mq_flags = fd_file(f)->f_flags & O_NONBLOCK;
  1299. }
  1300. if (new) {
  1301. audit_mq_getsetattr(mqdes, new);
  1302. spin_lock(&fd_file(f)->f_lock);
  1303. if (new->mq_flags & O_NONBLOCK)
  1304. fd_file(f)->f_flags |= O_NONBLOCK;
  1305. else
  1306. fd_file(f)->f_flags &= ~O_NONBLOCK;
  1307. spin_unlock(&fd_file(f)->f_lock);
  1308. inode_set_atime_to_ts(inode, inode_set_ctime_current(inode));
  1309. }
  1310. spin_unlock(&info->lock);
  1311. fdput(f);
  1312. return 0;
  1313. }
  1314. SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1315. const struct mq_attr __user *, u_mqstat,
  1316. struct mq_attr __user *, u_omqstat)
  1317. {
  1318. int ret;
  1319. struct mq_attr mqstat, omqstat;
  1320. struct mq_attr *new = NULL, *old = NULL;
  1321. if (u_mqstat) {
  1322. new = &mqstat;
  1323. if (copy_from_user(new, u_mqstat, sizeof(struct mq_attr)))
  1324. return -EFAULT;
  1325. }
  1326. if (u_omqstat)
  1327. old = &omqstat;
  1328. ret = do_mq_getsetattr(mqdes, new, old);
  1329. if (ret || !old)
  1330. return ret;
  1331. if (copy_to_user(u_omqstat, old, sizeof(struct mq_attr)))
  1332. return -EFAULT;
  1333. return 0;
  1334. }
  1335. #ifdef CONFIG_COMPAT
  1336. struct compat_mq_attr {
  1337. compat_long_t mq_flags; /* message queue flags */
  1338. compat_long_t mq_maxmsg; /* maximum number of messages */
  1339. compat_long_t mq_msgsize; /* maximum message size */
  1340. compat_long_t mq_curmsgs; /* number of messages currently queued */
  1341. compat_long_t __reserved[4]; /* ignored for input, zeroed for output */
  1342. };
  1343. static inline int get_compat_mq_attr(struct mq_attr *attr,
  1344. const struct compat_mq_attr __user *uattr)
  1345. {
  1346. struct compat_mq_attr v;
  1347. if (copy_from_user(&v, uattr, sizeof(*uattr)))
  1348. return -EFAULT;
  1349. memset(attr, 0, sizeof(*attr));
  1350. attr->mq_flags = v.mq_flags;
  1351. attr->mq_maxmsg = v.mq_maxmsg;
  1352. attr->mq_msgsize = v.mq_msgsize;
  1353. attr->mq_curmsgs = v.mq_curmsgs;
  1354. return 0;
  1355. }
  1356. static inline int put_compat_mq_attr(const struct mq_attr *attr,
  1357. struct compat_mq_attr __user *uattr)
  1358. {
  1359. struct compat_mq_attr v;
  1360. memset(&v, 0, sizeof(v));
  1361. v.mq_flags = attr->mq_flags;
  1362. v.mq_maxmsg = attr->mq_maxmsg;
  1363. v.mq_msgsize = attr->mq_msgsize;
  1364. v.mq_curmsgs = attr->mq_curmsgs;
  1365. if (copy_to_user(uattr, &v, sizeof(*uattr)))
  1366. return -EFAULT;
  1367. return 0;
  1368. }
  1369. COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
  1370. int, oflag, compat_mode_t, mode,
  1371. struct compat_mq_attr __user *, u_attr)
  1372. {
  1373. struct mq_attr attr, *p = NULL;
  1374. if (u_attr && oflag & O_CREAT) {
  1375. p = &attr;
  1376. if (get_compat_mq_attr(&attr, u_attr))
  1377. return -EFAULT;
  1378. }
  1379. return do_mq_open(u_name, oflag, mode, p);
  1380. }
  1381. COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  1382. const struct compat_sigevent __user *, u_notification)
  1383. {
  1384. struct sigevent n, *p = NULL;
  1385. if (u_notification) {
  1386. if (get_compat_sigevent(&n, u_notification))
  1387. return -EFAULT;
  1388. if (n.sigev_notify == SIGEV_THREAD)
  1389. n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
  1390. p = &n;
  1391. }
  1392. return do_mq_notify(mqdes, p);
  1393. }
  1394. COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1395. const struct compat_mq_attr __user *, u_mqstat,
  1396. struct compat_mq_attr __user *, u_omqstat)
  1397. {
  1398. int ret;
  1399. struct mq_attr mqstat, omqstat;
  1400. struct mq_attr *new = NULL, *old = NULL;
  1401. if (u_mqstat) {
  1402. new = &mqstat;
  1403. if (get_compat_mq_attr(new, u_mqstat))
  1404. return -EFAULT;
  1405. }
  1406. if (u_omqstat)
  1407. old = &omqstat;
  1408. ret = do_mq_getsetattr(mqdes, new, old);
  1409. if (ret || !old)
  1410. return ret;
  1411. if (put_compat_mq_attr(old, u_omqstat))
  1412. return -EFAULT;
  1413. return 0;
  1414. }
  1415. #endif
  1416. #ifdef CONFIG_COMPAT_32BIT_TIME
  1417. static int compat_prepare_timeout(const struct old_timespec32 __user *p,
  1418. struct timespec64 *ts)
  1419. {
  1420. if (get_old_timespec32(ts, p))
  1421. return -EFAULT;
  1422. if (!timespec64_valid(ts))
  1423. return -EINVAL;
  1424. return 0;
  1425. }
  1426. SYSCALL_DEFINE5(mq_timedsend_time32, mqd_t, mqdes,
  1427. const char __user *, u_msg_ptr,
  1428. unsigned int, msg_len, unsigned int, msg_prio,
  1429. const struct old_timespec32 __user *, u_abs_timeout)
  1430. {
  1431. struct timespec64 ts, *p = NULL;
  1432. if (u_abs_timeout) {
  1433. int res = compat_prepare_timeout(u_abs_timeout, &ts);
  1434. if (res)
  1435. return res;
  1436. p = &ts;
  1437. }
  1438. return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
  1439. }
  1440. SYSCALL_DEFINE5(mq_timedreceive_time32, mqd_t, mqdes,
  1441. char __user *, u_msg_ptr,
  1442. unsigned int, msg_len, unsigned int __user *, u_msg_prio,
  1443. const struct old_timespec32 __user *, u_abs_timeout)
  1444. {
  1445. struct timespec64 ts, *p = NULL;
  1446. if (u_abs_timeout) {
  1447. int res = compat_prepare_timeout(u_abs_timeout, &ts);
  1448. if (res)
  1449. return res;
  1450. p = &ts;
  1451. }
  1452. return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
  1453. }
  1454. #endif
  1455. static const struct inode_operations mqueue_dir_inode_operations = {
  1456. .lookup = simple_lookup,
  1457. .create = mqueue_create,
  1458. .unlink = mqueue_unlink,
  1459. };
  1460. static const struct file_operations mqueue_file_operations = {
  1461. .flush = mqueue_flush_file,
  1462. .poll = mqueue_poll_file,
  1463. .read = mqueue_read_file,
  1464. .llseek = default_llseek,
  1465. };
  1466. static const struct super_operations mqueue_super_ops = {
  1467. .alloc_inode = mqueue_alloc_inode,
  1468. .free_inode = mqueue_free_inode,
  1469. .evict_inode = mqueue_evict_inode,
  1470. .statfs = simple_statfs,
  1471. };
  1472. static const struct fs_context_operations mqueue_fs_context_ops = {
  1473. .free = mqueue_fs_context_free,
  1474. .get_tree = mqueue_get_tree,
  1475. };
  1476. static struct file_system_type mqueue_fs_type = {
  1477. .name = "mqueue",
  1478. .init_fs_context = mqueue_init_fs_context,
  1479. .kill_sb = kill_litter_super,
  1480. .fs_flags = FS_USERNS_MOUNT,
  1481. };
  1482. int mq_init_ns(struct ipc_namespace *ns)
  1483. {
  1484. struct vfsmount *m;
  1485. ns->mq_queues_count = 0;
  1486. ns->mq_queues_max = DFLT_QUEUESMAX;
  1487. ns->mq_msg_max = DFLT_MSGMAX;
  1488. ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
  1489. ns->mq_msg_default = DFLT_MSG;
  1490. ns->mq_msgsize_default = DFLT_MSGSIZE;
  1491. m = mq_create_mount(ns);
  1492. if (IS_ERR(m))
  1493. return PTR_ERR(m);
  1494. ns->mq_mnt = m;
  1495. return 0;
  1496. }
  1497. void mq_clear_sbinfo(struct ipc_namespace *ns)
  1498. {
  1499. ns->mq_mnt->mnt_sb->s_fs_info = NULL;
  1500. }
  1501. static int __init init_mqueue_fs(void)
  1502. {
  1503. int error;
  1504. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1505. sizeof(struct mqueue_inode_info), 0,
  1506. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, init_once);
  1507. if (mqueue_inode_cachep == NULL)
  1508. return -ENOMEM;
  1509. if (!setup_mq_sysctls(&init_ipc_ns)) {
  1510. pr_warn("sysctl registration failed\n");
  1511. error = -ENOMEM;
  1512. goto out_kmem;
  1513. }
  1514. error = register_filesystem(&mqueue_fs_type);
  1515. if (error)
  1516. goto out_sysctl;
  1517. spin_lock_init(&mq_lock);
  1518. error = mq_init_ns(&init_ipc_ns);
  1519. if (error)
  1520. goto out_filesystem;
  1521. return 0;
  1522. out_filesystem:
  1523. unregister_filesystem(&mqueue_fs_type);
  1524. out_sysctl:
  1525. retire_mq_sysctls(&init_ipc_ns);
  1526. out_kmem:
  1527. kmem_cache_destroy(mqueue_inode_cachep);
  1528. return error;
  1529. }
  1530. device_initcall(init_mqueue_fs);