mqueue.c 43 KB

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