mqueue.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. unsigned long mq_bytes, mq_treesize;
  349. struct ipc_namespace *ipc_ns;
  350. struct msg_msg *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. free_msg(msg);
  359. kfree(info->node_cache);
  360. spin_unlock(&info->lock);
  361. /* Total amount of bytes accounted for the mqueue */
  362. mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
  363. min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
  364. sizeof(struct posix_msg_tree_node);
  365. mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
  366. info->attr.mq_msgsize);
  367. user = info->user;
  368. if (user) {
  369. spin_lock(&mq_lock);
  370. user->mq_bytes -= mq_bytes;
  371. /*
  372. * get_ns_from_inode() ensures that the
  373. * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
  374. * to which we now hold a reference, or it is NULL.
  375. * We can't put it here under mq_lock, though.
  376. */
  377. if (ipc_ns)
  378. ipc_ns->mq_queues_count--;
  379. spin_unlock(&mq_lock);
  380. free_uid(user);
  381. }
  382. if (ipc_ns)
  383. put_ipc_ns(ipc_ns);
  384. }
  385. static int mqueue_create_attr(struct dentry *dentry, umode_t mode, void *arg)
  386. {
  387. struct inode *dir = dentry->d_parent->d_inode;
  388. struct inode *inode;
  389. struct mq_attr *attr = arg;
  390. int error;
  391. struct ipc_namespace *ipc_ns;
  392. spin_lock(&mq_lock);
  393. ipc_ns = __get_ns_from_inode(dir);
  394. if (!ipc_ns) {
  395. error = -EACCES;
  396. goto out_unlock;
  397. }
  398. if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
  399. !capable(CAP_SYS_RESOURCE)) {
  400. error = -ENOSPC;
  401. goto out_unlock;
  402. }
  403. ipc_ns->mq_queues_count++;
  404. spin_unlock(&mq_lock);
  405. inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
  406. if (IS_ERR(inode)) {
  407. error = PTR_ERR(inode);
  408. spin_lock(&mq_lock);
  409. ipc_ns->mq_queues_count--;
  410. goto out_unlock;
  411. }
  412. put_ipc_ns(ipc_ns);
  413. dir->i_size += DIRENT_SIZE;
  414. dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
  415. d_instantiate(dentry, inode);
  416. dget(dentry);
  417. return 0;
  418. out_unlock:
  419. spin_unlock(&mq_lock);
  420. if (ipc_ns)
  421. put_ipc_ns(ipc_ns);
  422. return error;
  423. }
  424. static int mqueue_create(struct inode *dir, struct dentry *dentry,
  425. umode_t mode, bool excl)
  426. {
  427. return mqueue_create_attr(dentry, mode, NULL);
  428. }
  429. static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
  430. {
  431. struct inode *inode = d_inode(dentry);
  432. dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
  433. dir->i_size -= DIRENT_SIZE;
  434. drop_nlink(inode);
  435. dput(dentry);
  436. return 0;
  437. }
  438. /*
  439. * This is routine for system read from queue file.
  440. * To avoid mess with doing here some sort of mq_receive we allow
  441. * to read only queue size & notification info (the only values
  442. * that are interesting from user point of view and aren't accessible
  443. * through std routines)
  444. */
  445. static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
  446. size_t count, loff_t *off)
  447. {
  448. struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
  449. char buffer[FILENT_SIZE];
  450. ssize_t ret;
  451. spin_lock(&info->lock);
  452. snprintf(buffer, sizeof(buffer),
  453. "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
  454. info->qsize,
  455. info->notify_owner ? info->notify.sigev_notify : 0,
  456. (info->notify_owner &&
  457. info->notify.sigev_notify == SIGEV_SIGNAL) ?
  458. info->notify.sigev_signo : 0,
  459. pid_vnr(info->notify_owner));
  460. spin_unlock(&info->lock);
  461. buffer[sizeof(buffer)-1] = '\0';
  462. ret = simple_read_from_buffer(u_data, count, off, buffer,
  463. strlen(buffer));
  464. if (ret <= 0)
  465. return ret;
  466. file_inode(filp)->i_atime = file_inode(filp)->i_ctime = current_time(file_inode(filp));
  467. return ret;
  468. }
  469. static int mqueue_flush_file(struct file *filp, fl_owner_t id)
  470. {
  471. struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
  472. spin_lock(&info->lock);
  473. if (task_tgid(current) == info->notify_owner)
  474. remove_notification(info);
  475. spin_unlock(&info->lock);
  476. return 0;
  477. }
  478. static __poll_t mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
  479. {
  480. struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
  481. __poll_t retval = 0;
  482. poll_wait(filp, &info->wait_q, poll_tab);
  483. spin_lock(&info->lock);
  484. if (info->attr.mq_curmsgs)
  485. retval = EPOLLIN | EPOLLRDNORM;
  486. if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
  487. retval |= EPOLLOUT | EPOLLWRNORM;
  488. spin_unlock(&info->lock);
  489. return retval;
  490. }
  491. /* Adds current to info->e_wait_q[sr] before element with smaller prio */
  492. static void wq_add(struct mqueue_inode_info *info, int sr,
  493. struct ext_wait_queue *ewp)
  494. {
  495. struct ext_wait_queue *walk;
  496. ewp->task = current;
  497. list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
  498. if (walk->task->prio <= current->prio) {
  499. list_add_tail(&ewp->list, &walk->list);
  500. return;
  501. }
  502. }
  503. list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
  504. }
  505. /*
  506. * Puts current task to sleep. Caller must hold queue lock. After return
  507. * lock isn't held.
  508. * sr: SEND or RECV
  509. */
  510. static int wq_sleep(struct mqueue_inode_info *info, int sr,
  511. ktime_t *timeout, struct ext_wait_queue *ewp)
  512. __releases(&info->lock)
  513. {
  514. int retval;
  515. signed long time;
  516. wq_add(info, sr, ewp);
  517. for (;;) {
  518. __set_current_state(TASK_INTERRUPTIBLE);
  519. spin_unlock(&info->lock);
  520. time = schedule_hrtimeout_range_clock(timeout, 0,
  521. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  522. if (ewp->state == STATE_READY) {
  523. retval = 0;
  524. goto out;
  525. }
  526. spin_lock(&info->lock);
  527. if (ewp->state == STATE_READY) {
  528. retval = 0;
  529. goto out_unlock;
  530. }
  531. if (signal_pending(current)) {
  532. retval = -ERESTARTSYS;
  533. break;
  534. }
  535. if (time == 0) {
  536. retval = -ETIMEDOUT;
  537. break;
  538. }
  539. }
  540. list_del(&ewp->list);
  541. out_unlock:
  542. spin_unlock(&info->lock);
  543. out:
  544. return retval;
  545. }
  546. /*
  547. * Returns waiting task that should be serviced first or NULL if none exists
  548. */
  549. static struct ext_wait_queue *wq_get_first_waiter(
  550. struct mqueue_inode_info *info, int sr)
  551. {
  552. struct list_head *ptr;
  553. ptr = info->e_wait_q[sr].list.prev;
  554. if (ptr == &info->e_wait_q[sr].list)
  555. return NULL;
  556. return list_entry(ptr, struct ext_wait_queue, list);
  557. }
  558. static inline void set_cookie(struct sk_buff *skb, char code)
  559. {
  560. ((char *)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
  561. }
  562. /*
  563. * The next function is only to split too long sys_mq_timedsend
  564. */
  565. static void __do_notify(struct mqueue_inode_info *info)
  566. {
  567. /* notification
  568. * invoked when there is registered process and there isn't process
  569. * waiting synchronously for message AND state of queue changed from
  570. * empty to not empty. Here we are sure that no one is waiting
  571. * synchronously. */
  572. if (info->notify_owner &&
  573. info->attr.mq_curmsgs == 1) {
  574. struct siginfo sig_i;
  575. switch (info->notify.sigev_notify) {
  576. case SIGEV_NONE:
  577. break;
  578. case SIGEV_SIGNAL:
  579. /* sends signal */
  580. clear_siginfo(&sig_i);
  581. sig_i.si_signo = info->notify.sigev_signo;
  582. sig_i.si_errno = 0;
  583. sig_i.si_code = SI_MESGQ;
  584. sig_i.si_value = info->notify.sigev_value;
  585. /* map current pid/uid into info->owner's namespaces */
  586. rcu_read_lock();
  587. sig_i.si_pid = task_tgid_nr_ns(current,
  588. ns_of_pid(info->notify_owner));
  589. sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid());
  590. rcu_read_unlock();
  591. kill_pid_info(info->notify.sigev_signo,
  592. &sig_i, info->notify_owner);
  593. break;
  594. case SIGEV_THREAD:
  595. set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
  596. netlink_sendskb(info->notify_sock, info->notify_cookie);
  597. break;
  598. }
  599. /* after notification unregisters process */
  600. put_pid(info->notify_owner);
  601. put_user_ns(info->notify_user_ns);
  602. info->notify_owner = NULL;
  603. info->notify_user_ns = NULL;
  604. }
  605. wake_up(&info->wait_q);
  606. }
  607. static int prepare_timeout(const struct __kernel_timespec __user *u_abs_timeout,
  608. struct timespec64 *ts)
  609. {
  610. if (get_timespec64(ts, u_abs_timeout))
  611. return -EFAULT;
  612. if (!timespec64_valid(ts))
  613. return -EINVAL;
  614. return 0;
  615. }
  616. static void remove_notification(struct mqueue_inode_info *info)
  617. {
  618. if (info->notify_owner != NULL &&
  619. info->notify.sigev_notify == SIGEV_THREAD) {
  620. set_cookie(info->notify_cookie, NOTIFY_REMOVED);
  621. netlink_sendskb(info->notify_sock, info->notify_cookie);
  622. }
  623. put_pid(info->notify_owner);
  624. put_user_ns(info->notify_user_ns);
  625. info->notify_owner = NULL;
  626. info->notify_user_ns = NULL;
  627. }
  628. static int prepare_open(struct dentry *dentry, int oflag, int ro,
  629. umode_t mode, struct filename *name,
  630. struct mq_attr *attr)
  631. {
  632. static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  633. MAY_READ | MAY_WRITE };
  634. int acc;
  635. if (d_really_is_negative(dentry)) {
  636. if (!(oflag & O_CREAT))
  637. return -ENOENT;
  638. if (ro)
  639. return ro;
  640. audit_inode_parent_hidden(name, dentry->d_parent);
  641. return vfs_mkobj(dentry, mode & ~current_umask(),
  642. mqueue_create_attr, attr);
  643. }
  644. /* it already existed */
  645. audit_inode(name, dentry, 0);
  646. if ((oflag & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  647. return -EEXIST;
  648. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
  649. return -EINVAL;
  650. acc = oflag2acc[oflag & O_ACCMODE];
  651. return inode_permission(d_inode(dentry), acc);
  652. }
  653. static int do_mq_open(const char __user *u_name, int oflag, umode_t mode,
  654. struct mq_attr *attr)
  655. {
  656. struct vfsmount *mnt = current->nsproxy->ipc_ns->mq_mnt;
  657. struct dentry *root = mnt->mnt_root;
  658. struct filename *name;
  659. struct path path;
  660. int fd, error;
  661. int ro;
  662. audit_mq_open(oflag, mode, attr);
  663. if (IS_ERR(name = getname(u_name)))
  664. return PTR_ERR(name);
  665. fd = get_unused_fd_flags(O_CLOEXEC);
  666. if (fd < 0)
  667. goto out_putname;
  668. ro = mnt_want_write(mnt); /* we'll drop it in any case */
  669. inode_lock(d_inode(root));
  670. path.dentry = lookup_one_len(name->name, root, strlen(name->name));
  671. if (IS_ERR(path.dentry)) {
  672. error = PTR_ERR(path.dentry);
  673. goto out_putfd;
  674. }
  675. path.mnt = mntget(mnt);
  676. error = prepare_open(path.dentry, oflag, ro, mode, name, attr);
  677. if (!error) {
  678. struct file *file = dentry_open(&path, oflag, current_cred());
  679. if (!IS_ERR(file))
  680. fd_install(fd, file);
  681. else
  682. error = PTR_ERR(file);
  683. }
  684. path_put(&path);
  685. out_putfd:
  686. if (error) {
  687. put_unused_fd(fd);
  688. fd = error;
  689. }
  690. inode_unlock(d_inode(root));
  691. if (!ro)
  692. mnt_drop_write(mnt);
  693. out_putname:
  694. putname(name);
  695. return fd;
  696. }
  697. SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
  698. struct mq_attr __user *, u_attr)
  699. {
  700. struct mq_attr attr;
  701. if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
  702. return -EFAULT;
  703. return do_mq_open(u_name, oflag, mode, u_attr ? &attr : NULL);
  704. }
  705. SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
  706. {
  707. int err;
  708. struct filename *name;
  709. struct dentry *dentry;
  710. struct inode *inode = NULL;
  711. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  712. struct vfsmount *mnt = ipc_ns->mq_mnt;
  713. name = getname(u_name);
  714. if (IS_ERR(name))
  715. return PTR_ERR(name);
  716. audit_inode_parent_hidden(name, mnt->mnt_root);
  717. err = mnt_want_write(mnt);
  718. if (err)
  719. goto out_name;
  720. inode_lock_nested(d_inode(mnt->mnt_root), I_MUTEX_PARENT);
  721. dentry = lookup_one_len(name->name, mnt->mnt_root,
  722. strlen(name->name));
  723. if (IS_ERR(dentry)) {
  724. err = PTR_ERR(dentry);
  725. goto out_unlock;
  726. }
  727. inode = d_inode(dentry);
  728. if (!inode) {
  729. err = -ENOENT;
  730. } else {
  731. ihold(inode);
  732. err = vfs_unlink(d_inode(dentry->d_parent), dentry, NULL);
  733. }
  734. dput(dentry);
  735. out_unlock:
  736. inode_unlock(d_inode(mnt->mnt_root));
  737. if (inode)
  738. iput(inode);
  739. mnt_drop_write(mnt);
  740. out_name:
  741. putname(name);
  742. return err;
  743. }
  744. /* Pipelined send and receive functions.
  745. *
  746. * If a receiver finds no waiting message, then it registers itself in the
  747. * list of waiting receivers. A sender checks that list before adding the new
  748. * message into the message array. If there is a waiting receiver, then it
  749. * bypasses the message array and directly hands the message over to the
  750. * receiver. The receiver accepts the message and returns without grabbing the
  751. * queue spinlock:
  752. *
  753. * - Set pointer to message.
  754. * - Queue the receiver task for later wakeup (without the info->lock).
  755. * - Update its state to STATE_READY. Now the receiver can continue.
  756. * - Wake up the process after the lock is dropped. Should the process wake up
  757. * before this wakeup (due to a timeout or a signal) it will either see
  758. * STATE_READY and continue or acquire the lock to check the state again.
  759. *
  760. * The same algorithm is used for senders.
  761. */
  762. /* pipelined_send() - send a message directly to the task waiting in
  763. * sys_mq_timedreceive() (without inserting message into a queue).
  764. */
  765. static inline void pipelined_send(struct wake_q_head *wake_q,
  766. struct mqueue_inode_info *info,
  767. struct msg_msg *message,
  768. struct ext_wait_queue *receiver)
  769. {
  770. receiver->msg = message;
  771. list_del(&receiver->list);
  772. wake_q_add(wake_q, receiver->task);
  773. /*
  774. * Rely on the implicit cmpxchg barrier from wake_q_add such
  775. * that we can ensure that updating receiver->state is the last
  776. * write operation: As once set, the receiver can continue,
  777. * and if we don't have the reference count from the wake_q,
  778. * yet, at that point we can later have a use-after-free
  779. * condition and bogus wakeup.
  780. */
  781. receiver->state = STATE_READY;
  782. }
  783. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  784. * gets its message and put to the queue (we have one free place for sure). */
  785. static inline void pipelined_receive(struct wake_q_head *wake_q,
  786. struct mqueue_inode_info *info)
  787. {
  788. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  789. if (!sender) {
  790. /* for poll */
  791. wake_up_interruptible(&info->wait_q);
  792. return;
  793. }
  794. if (msg_insert(sender->msg, info))
  795. return;
  796. list_del(&sender->list);
  797. wake_q_add(wake_q, sender->task);
  798. sender->state = STATE_READY;
  799. }
  800. static int do_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
  801. size_t msg_len, unsigned int msg_prio,
  802. struct timespec64 *ts)
  803. {
  804. struct fd f;
  805. struct inode *inode;
  806. struct ext_wait_queue wait;
  807. struct ext_wait_queue *receiver;
  808. struct msg_msg *msg_ptr;
  809. struct mqueue_inode_info *info;
  810. ktime_t expires, *timeout = NULL;
  811. struct posix_msg_tree_node *new_leaf = NULL;
  812. int ret = 0;
  813. DEFINE_WAKE_Q(wake_q);
  814. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  815. return -EINVAL;
  816. if (ts) {
  817. expires = timespec64_to_ktime(*ts);
  818. timeout = &expires;
  819. }
  820. audit_mq_sendrecv(mqdes, msg_len, msg_prio, ts);
  821. f = fdget(mqdes);
  822. if (unlikely(!f.file)) {
  823. ret = -EBADF;
  824. goto out;
  825. }
  826. inode = file_inode(f.file);
  827. if (unlikely(f.file->f_op != &mqueue_file_operations)) {
  828. ret = -EBADF;
  829. goto out_fput;
  830. }
  831. info = MQUEUE_I(inode);
  832. audit_file(f.file);
  833. if (unlikely(!(f.file->f_mode & FMODE_WRITE))) {
  834. ret = -EBADF;
  835. goto out_fput;
  836. }
  837. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  838. ret = -EMSGSIZE;
  839. goto out_fput;
  840. }
  841. /* First try to allocate memory, before doing anything with
  842. * existing queues. */
  843. msg_ptr = load_msg(u_msg_ptr, msg_len);
  844. if (IS_ERR(msg_ptr)) {
  845. ret = PTR_ERR(msg_ptr);
  846. goto out_fput;
  847. }
  848. msg_ptr->m_ts = msg_len;
  849. msg_ptr->m_type = msg_prio;
  850. /*
  851. * msg_insert really wants us to have a valid, spare node struct so
  852. * it doesn't have to kmalloc a GFP_ATOMIC allocation, but it will
  853. * fall back to that if necessary.
  854. */
  855. if (!info->node_cache)
  856. new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
  857. spin_lock(&info->lock);
  858. if (!info->node_cache && new_leaf) {
  859. /* Save our speculative allocation into the cache */
  860. INIT_LIST_HEAD(&new_leaf->msg_list);
  861. info->node_cache = new_leaf;
  862. new_leaf = NULL;
  863. } else {
  864. kfree(new_leaf);
  865. }
  866. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  867. if (f.file->f_flags & O_NONBLOCK) {
  868. ret = -EAGAIN;
  869. } else {
  870. wait.task = current;
  871. wait.msg = (void *) msg_ptr;
  872. wait.state = STATE_NONE;
  873. ret = wq_sleep(info, SEND, timeout, &wait);
  874. /*
  875. * wq_sleep must be called with info->lock held, and
  876. * returns with the lock released
  877. */
  878. goto out_free;
  879. }
  880. } else {
  881. receiver = wq_get_first_waiter(info, RECV);
  882. if (receiver) {
  883. pipelined_send(&wake_q, info, msg_ptr, receiver);
  884. } else {
  885. /* adds message to the queue */
  886. ret = msg_insert(msg_ptr, info);
  887. if (ret)
  888. goto out_unlock;
  889. __do_notify(info);
  890. }
  891. inode->i_atime = inode->i_mtime = inode->i_ctime =
  892. current_time(inode);
  893. }
  894. out_unlock:
  895. spin_unlock(&info->lock);
  896. wake_up_q(&wake_q);
  897. out_free:
  898. if (ret)
  899. free_msg(msg_ptr);
  900. out_fput:
  901. fdput(f);
  902. out:
  903. return ret;
  904. }
  905. static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
  906. size_t msg_len, unsigned int __user *u_msg_prio,
  907. struct timespec64 *ts)
  908. {
  909. ssize_t ret;
  910. struct msg_msg *msg_ptr;
  911. struct fd f;
  912. struct inode *inode;
  913. struct mqueue_inode_info *info;
  914. struct ext_wait_queue wait;
  915. ktime_t expires, *timeout = NULL;
  916. struct posix_msg_tree_node *new_leaf = NULL;
  917. if (ts) {
  918. expires = timespec64_to_ktime(*ts);
  919. timeout = &expires;
  920. }
  921. audit_mq_sendrecv(mqdes, msg_len, 0, ts);
  922. f = fdget(mqdes);
  923. if (unlikely(!f.file)) {
  924. ret = -EBADF;
  925. goto out;
  926. }
  927. inode = file_inode(f.file);
  928. if (unlikely(f.file->f_op != &mqueue_file_operations)) {
  929. ret = -EBADF;
  930. goto out_fput;
  931. }
  932. info = MQUEUE_I(inode);
  933. audit_file(f.file);
  934. if (unlikely(!(f.file->f_mode & FMODE_READ))) {
  935. ret = -EBADF;
  936. goto out_fput;
  937. }
  938. /* checks if buffer is big enough */
  939. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  940. ret = -EMSGSIZE;
  941. goto out_fput;
  942. }
  943. /*
  944. * msg_insert really wants us to have a valid, spare node struct so
  945. * it doesn't have to kmalloc a GFP_ATOMIC allocation, but it will
  946. * fall back to that if necessary.
  947. */
  948. if (!info->node_cache)
  949. new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
  950. spin_lock(&info->lock);
  951. if (!info->node_cache && new_leaf) {
  952. /* Save our speculative allocation into the cache */
  953. INIT_LIST_HEAD(&new_leaf->msg_list);
  954. info->node_cache = new_leaf;
  955. } else {
  956. kfree(new_leaf);
  957. }
  958. if (info->attr.mq_curmsgs == 0) {
  959. if (f.file->f_flags & O_NONBLOCK) {
  960. spin_unlock(&info->lock);
  961. ret = -EAGAIN;
  962. } else {
  963. wait.task = current;
  964. wait.state = STATE_NONE;
  965. ret = wq_sleep(info, RECV, timeout, &wait);
  966. msg_ptr = wait.msg;
  967. }
  968. } else {
  969. DEFINE_WAKE_Q(wake_q);
  970. msg_ptr = msg_get(info);
  971. inode->i_atime = inode->i_mtime = inode->i_ctime =
  972. current_time(inode);
  973. /* There is now free space in queue. */
  974. pipelined_receive(&wake_q, info);
  975. spin_unlock(&info->lock);
  976. wake_up_q(&wake_q);
  977. ret = 0;
  978. }
  979. if (ret == 0) {
  980. ret = msg_ptr->m_ts;
  981. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  982. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  983. ret = -EFAULT;
  984. }
  985. free_msg(msg_ptr);
  986. }
  987. out_fput:
  988. fdput(f);
  989. out:
  990. return ret;
  991. }
  992. SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
  993. size_t, msg_len, unsigned int, msg_prio,
  994. const struct __kernel_timespec __user *, u_abs_timeout)
  995. {
  996. struct timespec64 ts, *p = NULL;
  997. if (u_abs_timeout) {
  998. int res = prepare_timeout(u_abs_timeout, &ts);
  999. if (res)
  1000. return res;
  1001. p = &ts;
  1002. }
  1003. return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
  1004. }
  1005. SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
  1006. size_t, msg_len, unsigned int __user *, u_msg_prio,
  1007. const struct __kernel_timespec __user *, u_abs_timeout)
  1008. {
  1009. struct timespec64 ts, *p = NULL;
  1010. if (u_abs_timeout) {
  1011. int res = prepare_timeout(u_abs_timeout, &ts);
  1012. if (res)
  1013. return res;
  1014. p = &ts;
  1015. }
  1016. return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
  1017. }
  1018. /*
  1019. * Notes: the case when user wants us to deregister (with NULL as pointer)
  1020. * and he isn't currently owner of notification, will be silently discarded.
  1021. * It isn't explicitly defined in the POSIX.
  1022. */
  1023. static int do_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  1024. {
  1025. int ret;
  1026. struct fd f;
  1027. struct sock *sock;
  1028. struct inode *inode;
  1029. struct mqueue_inode_info *info;
  1030. struct sk_buff *nc;
  1031. audit_mq_notify(mqdes, notification);
  1032. nc = NULL;
  1033. sock = NULL;
  1034. if (notification != NULL) {
  1035. if (unlikely(notification->sigev_notify != SIGEV_NONE &&
  1036. notification->sigev_notify != SIGEV_SIGNAL &&
  1037. notification->sigev_notify != SIGEV_THREAD))
  1038. return -EINVAL;
  1039. if (notification->sigev_notify == SIGEV_SIGNAL &&
  1040. !valid_signal(notification->sigev_signo)) {
  1041. return -EINVAL;
  1042. }
  1043. if (notification->sigev_notify == SIGEV_THREAD) {
  1044. long timeo;
  1045. /* create the notify skb */
  1046. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  1047. if (!nc) {
  1048. ret = -ENOMEM;
  1049. goto out;
  1050. }
  1051. if (copy_from_user(nc->data,
  1052. notification->sigev_value.sival_ptr,
  1053. NOTIFY_COOKIE_LEN)) {
  1054. ret = -EFAULT;
  1055. goto out;
  1056. }
  1057. /* TODO: add a header? */
  1058. skb_put(nc, NOTIFY_COOKIE_LEN);
  1059. /* and attach it to the socket */
  1060. retry:
  1061. f = fdget(notification->sigev_signo);
  1062. if (!f.file) {
  1063. ret = -EBADF;
  1064. goto out;
  1065. }
  1066. sock = netlink_getsockbyfilp(f.file);
  1067. fdput(f);
  1068. if (IS_ERR(sock)) {
  1069. ret = PTR_ERR(sock);
  1070. sock = NULL;
  1071. goto out;
  1072. }
  1073. timeo = MAX_SCHEDULE_TIMEOUT;
  1074. ret = netlink_attachskb(sock, nc, &timeo, NULL);
  1075. if (ret == 1) {
  1076. sock = NULL;
  1077. goto retry;
  1078. }
  1079. if (ret) {
  1080. sock = NULL;
  1081. nc = NULL;
  1082. goto out;
  1083. }
  1084. }
  1085. }
  1086. f = fdget(mqdes);
  1087. if (!f.file) {
  1088. ret = -EBADF;
  1089. goto out;
  1090. }
  1091. inode = file_inode(f.file);
  1092. if (unlikely(f.file->f_op != &mqueue_file_operations)) {
  1093. ret = -EBADF;
  1094. goto out_fput;
  1095. }
  1096. info = MQUEUE_I(inode);
  1097. ret = 0;
  1098. spin_lock(&info->lock);
  1099. if (notification == NULL) {
  1100. if (info->notify_owner == task_tgid(current)) {
  1101. remove_notification(info);
  1102. inode->i_atime = inode->i_ctime = current_time(inode);
  1103. }
  1104. } else if (info->notify_owner != NULL) {
  1105. ret = -EBUSY;
  1106. } else {
  1107. switch (notification->sigev_notify) {
  1108. case SIGEV_NONE:
  1109. info->notify.sigev_notify = SIGEV_NONE;
  1110. break;
  1111. case SIGEV_THREAD:
  1112. info->notify_sock = sock;
  1113. info->notify_cookie = nc;
  1114. sock = NULL;
  1115. nc = NULL;
  1116. info->notify.sigev_notify = SIGEV_THREAD;
  1117. break;
  1118. case SIGEV_SIGNAL:
  1119. info->notify.sigev_signo = notification->sigev_signo;
  1120. info->notify.sigev_value = notification->sigev_value;
  1121. info->notify.sigev_notify = SIGEV_SIGNAL;
  1122. break;
  1123. }
  1124. info->notify_owner = get_pid(task_tgid(current));
  1125. info->notify_user_ns = get_user_ns(current_user_ns());
  1126. inode->i_atime = inode->i_ctime = current_time(inode);
  1127. }
  1128. spin_unlock(&info->lock);
  1129. out_fput:
  1130. fdput(f);
  1131. out:
  1132. if (sock)
  1133. netlink_detachskb(sock, nc);
  1134. else if (nc)
  1135. dev_kfree_skb(nc);
  1136. return ret;
  1137. }
  1138. SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  1139. const struct sigevent __user *, u_notification)
  1140. {
  1141. struct sigevent n, *p = NULL;
  1142. if (u_notification) {
  1143. if (copy_from_user(&n, u_notification, sizeof(struct sigevent)))
  1144. return -EFAULT;
  1145. p = &n;
  1146. }
  1147. return do_mq_notify(mqdes, p);
  1148. }
  1149. static int do_mq_getsetattr(int mqdes, struct mq_attr *new, struct mq_attr *old)
  1150. {
  1151. struct fd f;
  1152. struct inode *inode;
  1153. struct mqueue_inode_info *info;
  1154. if (new && (new->mq_flags & (~O_NONBLOCK)))
  1155. return -EINVAL;
  1156. f = fdget(mqdes);
  1157. if (!f.file)
  1158. return -EBADF;
  1159. if (unlikely(f.file->f_op != &mqueue_file_operations)) {
  1160. fdput(f);
  1161. return -EBADF;
  1162. }
  1163. inode = file_inode(f.file);
  1164. info = MQUEUE_I(inode);
  1165. spin_lock(&info->lock);
  1166. if (old) {
  1167. *old = info->attr;
  1168. old->mq_flags = f.file->f_flags & O_NONBLOCK;
  1169. }
  1170. if (new) {
  1171. audit_mq_getsetattr(mqdes, new);
  1172. spin_lock(&f.file->f_lock);
  1173. if (new->mq_flags & O_NONBLOCK)
  1174. f.file->f_flags |= O_NONBLOCK;
  1175. else
  1176. f.file->f_flags &= ~O_NONBLOCK;
  1177. spin_unlock(&f.file->f_lock);
  1178. inode->i_atime = inode->i_ctime = current_time(inode);
  1179. }
  1180. spin_unlock(&info->lock);
  1181. fdput(f);
  1182. return 0;
  1183. }
  1184. SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1185. const struct mq_attr __user *, u_mqstat,
  1186. struct mq_attr __user *, u_omqstat)
  1187. {
  1188. int ret;
  1189. struct mq_attr mqstat, omqstat;
  1190. struct mq_attr *new = NULL, *old = NULL;
  1191. if (u_mqstat) {
  1192. new = &mqstat;
  1193. if (copy_from_user(new, u_mqstat, sizeof(struct mq_attr)))
  1194. return -EFAULT;
  1195. }
  1196. if (u_omqstat)
  1197. old = &omqstat;
  1198. ret = do_mq_getsetattr(mqdes, new, old);
  1199. if (ret || !old)
  1200. return ret;
  1201. if (copy_to_user(u_omqstat, old, sizeof(struct mq_attr)))
  1202. return -EFAULT;
  1203. return 0;
  1204. }
  1205. #ifdef CONFIG_COMPAT
  1206. struct compat_mq_attr {
  1207. compat_long_t mq_flags; /* message queue flags */
  1208. compat_long_t mq_maxmsg; /* maximum number of messages */
  1209. compat_long_t mq_msgsize; /* maximum message size */
  1210. compat_long_t mq_curmsgs; /* number of messages currently queued */
  1211. compat_long_t __reserved[4]; /* ignored for input, zeroed for output */
  1212. };
  1213. static inline int get_compat_mq_attr(struct mq_attr *attr,
  1214. const struct compat_mq_attr __user *uattr)
  1215. {
  1216. struct compat_mq_attr v;
  1217. if (copy_from_user(&v, uattr, sizeof(*uattr)))
  1218. return -EFAULT;
  1219. memset(attr, 0, sizeof(*attr));
  1220. attr->mq_flags = v.mq_flags;
  1221. attr->mq_maxmsg = v.mq_maxmsg;
  1222. attr->mq_msgsize = v.mq_msgsize;
  1223. attr->mq_curmsgs = v.mq_curmsgs;
  1224. return 0;
  1225. }
  1226. static inline int put_compat_mq_attr(const struct mq_attr *attr,
  1227. struct compat_mq_attr __user *uattr)
  1228. {
  1229. struct compat_mq_attr v;
  1230. memset(&v, 0, sizeof(v));
  1231. v.mq_flags = attr->mq_flags;
  1232. v.mq_maxmsg = attr->mq_maxmsg;
  1233. v.mq_msgsize = attr->mq_msgsize;
  1234. v.mq_curmsgs = attr->mq_curmsgs;
  1235. if (copy_to_user(uattr, &v, sizeof(*uattr)))
  1236. return -EFAULT;
  1237. return 0;
  1238. }
  1239. COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
  1240. int, oflag, compat_mode_t, mode,
  1241. struct compat_mq_attr __user *, u_attr)
  1242. {
  1243. struct mq_attr attr, *p = NULL;
  1244. if (u_attr && oflag & O_CREAT) {
  1245. p = &attr;
  1246. if (get_compat_mq_attr(&attr, u_attr))
  1247. return -EFAULT;
  1248. }
  1249. return do_mq_open(u_name, oflag, mode, p);
  1250. }
  1251. COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  1252. const struct compat_sigevent __user *, u_notification)
  1253. {
  1254. struct sigevent n, *p = NULL;
  1255. if (u_notification) {
  1256. if (get_compat_sigevent(&n, u_notification))
  1257. return -EFAULT;
  1258. if (n.sigev_notify == SIGEV_THREAD)
  1259. n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
  1260. p = &n;
  1261. }
  1262. return do_mq_notify(mqdes, p);
  1263. }
  1264. COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1265. const struct compat_mq_attr __user *, u_mqstat,
  1266. struct compat_mq_attr __user *, u_omqstat)
  1267. {
  1268. int ret;
  1269. struct mq_attr mqstat, omqstat;
  1270. struct mq_attr *new = NULL, *old = NULL;
  1271. if (u_mqstat) {
  1272. new = &mqstat;
  1273. if (get_compat_mq_attr(new, u_mqstat))
  1274. return -EFAULT;
  1275. }
  1276. if (u_omqstat)
  1277. old = &omqstat;
  1278. ret = do_mq_getsetattr(mqdes, new, old);
  1279. if (ret || !old)
  1280. return ret;
  1281. if (put_compat_mq_attr(old, u_omqstat))
  1282. return -EFAULT;
  1283. return 0;
  1284. }
  1285. #endif
  1286. #ifdef CONFIG_COMPAT_32BIT_TIME
  1287. static int compat_prepare_timeout(const struct old_timespec32 __user *p,
  1288. struct timespec64 *ts)
  1289. {
  1290. if (get_old_timespec32(ts, p))
  1291. return -EFAULT;
  1292. if (!timespec64_valid(ts))
  1293. return -EINVAL;
  1294. return 0;
  1295. }
  1296. COMPAT_SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes,
  1297. const char __user *, u_msg_ptr,
  1298. compat_size_t, msg_len, unsigned int, msg_prio,
  1299. const struct old_timespec32 __user *, u_abs_timeout)
  1300. {
  1301. struct timespec64 ts, *p = NULL;
  1302. if (u_abs_timeout) {
  1303. int res = compat_prepare_timeout(u_abs_timeout, &ts);
  1304. if (res)
  1305. return res;
  1306. p = &ts;
  1307. }
  1308. return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
  1309. }
  1310. COMPAT_SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes,
  1311. char __user *, u_msg_ptr,
  1312. compat_size_t, msg_len, unsigned int __user *, u_msg_prio,
  1313. const struct old_timespec32 __user *, u_abs_timeout)
  1314. {
  1315. struct timespec64 ts, *p = NULL;
  1316. if (u_abs_timeout) {
  1317. int res = compat_prepare_timeout(u_abs_timeout, &ts);
  1318. if (res)
  1319. return res;
  1320. p = &ts;
  1321. }
  1322. return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
  1323. }
  1324. #endif
  1325. static const struct inode_operations mqueue_dir_inode_operations = {
  1326. .lookup = simple_lookup,
  1327. .create = mqueue_create,
  1328. .unlink = mqueue_unlink,
  1329. };
  1330. static const struct file_operations mqueue_file_operations = {
  1331. .flush = mqueue_flush_file,
  1332. .poll = mqueue_poll_file,
  1333. .read = mqueue_read_file,
  1334. .llseek = default_llseek,
  1335. };
  1336. static const struct super_operations mqueue_super_ops = {
  1337. .alloc_inode = mqueue_alloc_inode,
  1338. .destroy_inode = mqueue_destroy_inode,
  1339. .evict_inode = mqueue_evict_inode,
  1340. .statfs = simple_statfs,
  1341. };
  1342. static struct file_system_type mqueue_fs_type = {
  1343. .name = "mqueue",
  1344. .mount = mqueue_mount,
  1345. .kill_sb = kill_litter_super,
  1346. .fs_flags = FS_USERNS_MOUNT,
  1347. };
  1348. int mq_init_ns(struct ipc_namespace *ns)
  1349. {
  1350. ns->mq_queues_count = 0;
  1351. ns->mq_queues_max = DFLT_QUEUESMAX;
  1352. ns->mq_msg_max = DFLT_MSGMAX;
  1353. ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
  1354. ns->mq_msg_default = DFLT_MSG;
  1355. ns->mq_msgsize_default = DFLT_MSGSIZE;
  1356. ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
  1357. if (IS_ERR(ns->mq_mnt)) {
  1358. int err = PTR_ERR(ns->mq_mnt);
  1359. ns->mq_mnt = NULL;
  1360. return err;
  1361. }
  1362. return 0;
  1363. }
  1364. void mq_clear_sbinfo(struct ipc_namespace *ns)
  1365. {
  1366. ns->mq_mnt->mnt_sb->s_fs_info = NULL;
  1367. }
  1368. void mq_put_mnt(struct ipc_namespace *ns)
  1369. {
  1370. kern_unmount(ns->mq_mnt);
  1371. }
  1372. static int __init init_mqueue_fs(void)
  1373. {
  1374. int error;
  1375. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1376. sizeof(struct mqueue_inode_info), 0,
  1377. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, init_once);
  1378. if (mqueue_inode_cachep == NULL)
  1379. return -ENOMEM;
  1380. /* ignore failures - they are not fatal */
  1381. mq_sysctl_table = mq_register_sysctl_table();
  1382. error = register_filesystem(&mqueue_fs_type);
  1383. if (error)
  1384. goto out_sysctl;
  1385. spin_lock_init(&mq_lock);
  1386. error = mq_init_ns(&init_ipc_ns);
  1387. if (error)
  1388. goto out_filesystem;
  1389. return 0;
  1390. out_filesystem:
  1391. unregister_filesystem(&mqueue_fs_type);
  1392. out_sysctl:
  1393. if (mq_sysctl_table)
  1394. unregister_sysctl_table(mq_sysctl_table);
  1395. kmem_cache_destroy(mqueue_inode_cachep);
  1396. return error;
  1397. }
  1398. device_initcall(init_mqueue_fs);