mqueue.c 44 KB

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