mqueue.c 40 KB

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