mqueue.c 44 KB

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