mqueue.c 40 KB

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