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