mqueue.c 39 KB

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