2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (michal.wronski@gmail.com)
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
11 * Audit: George Wilson (ltcgcw@us.ibm.com)
13 * This file is released under the GPL.
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>
46 struct mqueue_fs_context {
47 struct ipc_namespace *ipc_ns;
48 bool newns; /* Set if newly created ipc namespace */
51 #define MQUEUE_MAGIC 0x19800202
52 #define DIRENT_SIZE 20
53 #define FILENT_SIZE 80
61 struct posix_msg_tree_node {
62 struct rb_node rb_node;
63 struct list_head msg_list;
67 struct 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 */
74 struct mqueue_inode_info {
76 struct inode vfs_inode;
77 wait_queue_head_t wait_q;
79 struct rb_root msg_tree;
80 struct rb_node *msg_tree_rightmost;
81 struct posix_msg_tree_node *node_cache;
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;
92 /* for tasks waiting for free space and messages, respectively */
93 struct ext_wait_queue e_wait_q[2];
95 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
98 static struct file_system_type mqueue_fs_type;
99 static const struct inode_operations mqueue_dir_inode_operations;
100 static const struct file_operations mqueue_file_operations;
101 static const struct super_operations mqueue_super_ops;
102 static const struct fs_context_operations mqueue_fs_context_ops;
103 static void remove_notification(struct mqueue_inode_info *info);
105 static struct kmem_cache *mqueue_inode_cachep;
107 static struct ctl_table_header *mq_sysctl_table;
109 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
111 return container_of(inode, struct mqueue_inode_info, vfs_inode);
115 * This routine should be called with the mq_lock held.
117 static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
119 return get_ipc_ns(inode->i_sb->s_fs_info);
122 static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
124 struct ipc_namespace *ns;
127 ns = __get_ns_from_inode(inode);
128 spin_unlock(&mq_lock);
132 /* Auxiliary functions to manipulate messages' list */
133 static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
135 struct rb_node **p, *parent = NULL;
136 struct posix_msg_tree_node *leaf;
137 bool rightmost = true;
139 p = &info->msg_tree.rb_node;
142 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
144 if (likely(leaf->priority == msg->m_type))
146 else if (msg->m_type < leaf->priority) {
152 if (info->node_cache) {
153 leaf = info->node_cache;
154 info->node_cache = NULL;
156 leaf = kmalloc(sizeof(*leaf), GFP_ATOMIC);
159 INIT_LIST_HEAD(&leaf->msg_list);
161 leaf->priority = msg->m_type;
164 info->msg_tree_rightmost = &leaf->rb_node;
166 rb_link_node(&leaf->rb_node, parent, p);
167 rb_insert_color(&leaf->rb_node, &info->msg_tree);
169 info->attr.mq_curmsgs++;
170 info->qsize += msg->m_ts;
171 list_add_tail(&msg->m_list, &leaf->msg_list);
175 static inline void msg_tree_erase(struct posix_msg_tree_node *leaf,
176 struct mqueue_inode_info *info)
178 struct rb_node *node = &leaf->rb_node;
180 if (info->msg_tree_rightmost == node)
181 info->msg_tree_rightmost = rb_prev(node);
183 rb_erase(node, &info->msg_tree);
184 if (info->node_cache) {
187 info->node_cache = leaf;
191 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
193 struct rb_node *parent = NULL;
194 struct posix_msg_tree_node *leaf;
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.
203 parent = info->msg_tree_rightmost;
205 if (info->attr.mq_curmsgs) {
206 pr_warn_once("Inconsistency in POSIX message queue, "
207 "no tree element, but supposedly messages "
209 info->attr.mq_curmsgs = 0;
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);
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);
228 info->attr.mq_curmsgs--;
229 info->qsize -= msg->m_ts;
233 static struct inode *mqueue_get_inode(struct super_block *sb,
234 struct ipc_namespace *ipc_ns, umode_t mode,
235 struct mq_attr *attr)
237 struct user_struct *u = current_user();
241 inode = new_inode(sb);
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);
252 struct mqueue_inode_info *info;
253 unsigned long mq_bytes, mq_treesize;
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;
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);
276 info->attr.mq_maxmsg = attr->mq_maxmsg;
277 info->attr.mq_msgsize = attr->mq_msgsize;
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.
294 if (info->attr.mq_maxmsg <= 0 || info->attr.mq_msgsize <= 0)
296 if (capable(CAP_SYS_RESOURCE)) {
297 if (info->attr.mq_maxmsg > HARD_MSGMAX ||
298 info->attr.mq_msgsize > HARD_MSGSIZEMAX)
301 if (info->attr.mq_maxmsg > ipc_ns->mq_msg_max ||
302 info->attr.mq_msgsize > ipc_ns->mq_msgsize_max)
306 /* check for overflow */
307 if (info->attr.mq_msgsize > ULONG_MAX/info->attr.mq_maxmsg)
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)
315 mq_bytes += mq_treesize;
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 */
324 u->mq_bytes += mq_bytes;
325 spin_unlock(&mq_lock);
328 info->user = get_uid(u);
329 } else if (S_ISDIR(mode)) {
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;
344 static int mqueue_fill_super(struct super_block *sb, struct fs_context *fc)
347 struct ipc_namespace *ns = sb->s_fs_info;
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;
355 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
357 return PTR_ERR(inode);
359 sb->s_root = d_make_root(inode);
365 static int mqueue_get_tree(struct fs_context *fc)
367 struct mqueue_fs_context *ctx = fc->fs_private;
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.
374 fc->s_fs_info = ctx->ipc_ns;
375 return get_tree_nodev(fc, mqueue_fill_super);
377 return get_tree_keyed(fc, mqueue_fill_super, ctx->ipc_ns);
380 static void mqueue_fs_context_free(struct fs_context *fc)
382 struct mqueue_fs_context *ctx = fc->fs_private;
384 put_ipc_ns(ctx->ipc_ns);
388 static int mqueue_init_fs_context(struct fs_context *fc)
390 struct mqueue_fs_context *ctx;
392 ctx = kzalloc(sizeof(struct mqueue_fs_context), GFP_KERNEL);
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;
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.
408 static struct vfsmount *mq_create_mount(struct ipc_namespace *ns)
410 struct mqueue_fs_context *ctx;
411 struct fs_context *fc;
412 struct vfsmount *mnt;
414 fc = fs_context_for_mount(&mqueue_fs_type, SB_KERNMOUNT);
418 ctx = fc->fs_private;
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);
430 static void init_once(void *foo)
432 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
434 inode_init_once(&p->vfs_inode);
437 static struct inode *mqueue_alloc_inode(struct super_block *sb)
439 struct mqueue_inode_info *ei;
441 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
444 return &ei->vfs_inode;
447 static void mqueue_free_inode(struct inode *inode)
449 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
452 static void mqueue_evict_inode(struct inode *inode)
454 struct mqueue_inode_info *info;
455 struct user_struct *user;
456 struct ipc_namespace *ipc_ns;
457 struct msg_msg *msg, *nmsg;
462 if (S_ISDIR(inode->i_mode))
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);
473 list_for_each_entry_safe(msg, nmsg, &tmp_msg, m_list) {
474 list_del(&msg->m_list);
480 unsigned long mq_bytes, mq_treesize;
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);
487 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
488 info->attr.mq_msgsize);
491 user->mq_bytes -= mq_bytes;
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.
499 ipc_ns->mq_queues_count--;
500 spin_unlock(&mq_lock);
507 static int mqueue_create_attr(struct dentry *dentry, umode_t mode, void *arg)
509 struct inode *dir = dentry->d_parent->d_inode;
511 struct mq_attr *attr = arg;
513 struct ipc_namespace *ipc_ns;
516 ipc_ns = __get_ns_from_inode(dir);
522 if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
523 !capable(CAP_SYS_RESOURCE)) {
527 ipc_ns->mq_queues_count++;
528 spin_unlock(&mq_lock);
530 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
532 error = PTR_ERR(inode);
534 ipc_ns->mq_queues_count--;
539 dir->i_size += DIRENT_SIZE;
540 dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
542 d_instantiate(dentry, inode);
546 spin_unlock(&mq_lock);
552 static int mqueue_create(struct inode *dir, struct dentry *dentry,
553 umode_t mode, bool excl)
555 return mqueue_create_attr(dentry, mode, NULL);
558 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
560 struct inode *inode = d_inode(dentry);
562 dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
563 dir->i_size -= DIRENT_SIZE;
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)
576 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
577 size_t count, loff_t *off)
579 struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
580 char buffer[FILENT_SIZE];
583 spin_lock(&info->lock);
584 snprintf(buffer, sizeof(buffer),
585 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
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';
595 ret = simple_read_from_buffer(u_data, count, off, buffer,
600 file_inode(filp)->i_atime = file_inode(filp)->i_ctime = current_time(file_inode(filp));
604 static int mqueue_flush_file(struct file *filp, fl_owner_t id)
606 struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
608 spin_lock(&info->lock);
609 if (task_tgid(current) == info->notify_owner)
610 remove_notification(info);
612 spin_unlock(&info->lock);
616 static __poll_t mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
618 struct mqueue_inode_info *info = MQUEUE_I(file_inode(filp));
621 poll_wait(filp, &info->wait_q, poll_tab);
623 spin_lock(&info->lock);
624 if (info->attr.mq_curmsgs)
625 retval = EPOLLIN | EPOLLRDNORM;
627 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
628 retval |= EPOLLOUT | EPOLLWRNORM;
629 spin_unlock(&info->lock);
634 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
635 static void wq_add(struct mqueue_inode_info *info, int sr,
636 struct ext_wait_queue *ewp)
638 struct ext_wait_queue *walk;
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);
646 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
650 * Puts current task to sleep. Caller must hold queue lock. After return
654 static int wq_sleep(struct mqueue_inode_info *info, int sr,
655 ktime_t *timeout, struct ext_wait_queue *ewp)
656 __releases(&info->lock)
661 wq_add(info, sr, ewp);
664 __set_current_state(TASK_INTERRUPTIBLE);
666 spin_unlock(&info->lock);
667 time = schedule_hrtimeout_range_clock(timeout, 0,
668 HRTIMER_MODE_ABS, CLOCK_REALTIME);
670 if (ewp->state == STATE_READY) {
674 spin_lock(&info->lock);
675 if (ewp->state == STATE_READY) {
679 if (signal_pending(current)) {
680 retval = -ERESTARTSYS;
688 list_del(&ewp->list);
690 spin_unlock(&info->lock);
696 * Returns waiting task that should be serviced first or NULL if none exists
698 static struct ext_wait_queue *wq_get_first_waiter(
699 struct mqueue_inode_info *info, int sr)
701 struct list_head *ptr;
703 ptr = info->e_wait_q[sr].list.prev;
704 if (ptr == &info->e_wait_q[sr].list)
706 return list_entry(ptr, struct ext_wait_queue, list);
710 static inline void set_cookie(struct sk_buff *skb, char code)
712 ((char *)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
716 * The next function is only to split too long sys_mq_timedsend
718 static void __do_notify(struct mqueue_inode_info *info)
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
725 if (info->notify_owner &&
726 info->attr.mq_curmsgs == 1) {
727 switch (info->notify.sigev_notify) {
731 struct kernel_siginfo sig_i;
732 struct task_struct *task;
734 /* do_mq_notify() accepts sigev_signo == 0, why?? */
735 if (!info->notify.sigev_signo)
738 clear_siginfo(&sig_i);
739 sig_i.si_signo = info->notify.sigev_signo;
741 sig_i.si_code = SI_MESGQ;
742 sig_i.si_value = info->notify.sigev_value;
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,
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.
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);
766 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
767 netlink_sendskb(info->notify_sock, info->notify_cookie);
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;
776 wake_up(&info->wait_q);
779 static int prepare_timeout(const struct __kernel_timespec __user *u_abs_timeout,
780 struct timespec64 *ts)
782 if (get_timespec64(ts, u_abs_timeout))
784 if (!timespec64_valid(ts))
789 static void remove_notification(struct mqueue_inode_info *info)
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);
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;
802 static int prepare_open(struct dentry *dentry, int oflag, int ro,
803 umode_t mode, struct filename *name,
804 struct mq_attr *attr)
806 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
807 MAY_READ | MAY_WRITE };
810 if (d_really_is_negative(dentry)) {
811 if (!(oflag & O_CREAT))
815 audit_inode_parent_hidden(name, dentry->d_parent);
816 return vfs_mkobj(dentry, mode & ~current_umask(),
817 mqueue_create_attr, attr);
819 /* it already existed */
820 audit_inode(name, dentry, 0);
821 if ((oflag & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
823 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
825 acc = oflag2acc[oflag & O_ACCMODE];
826 return inode_permission(d_inode(dentry), acc);
829 static int do_mq_open(const char __user *u_name, int oflag, umode_t mode,
830 struct mq_attr *attr)
832 struct vfsmount *mnt = current->nsproxy->ipc_ns->mq_mnt;
833 struct dentry *root = mnt->mnt_root;
834 struct filename *name;
839 audit_mq_open(oflag, mode, attr);
841 if (IS_ERR(name = getname(u_name)))
842 return PTR_ERR(name);
844 fd = get_unused_fd_flags(O_CLOEXEC);
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);
855 path.mnt = mntget(mnt);
856 error = prepare_open(path.dentry, oflag, ro, mode, name, attr);
858 struct file *file = dentry_open(&path, oflag, current_cred());
860 fd_install(fd, file);
862 error = PTR_ERR(file);
870 inode_unlock(d_inode(root));
878 SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
879 struct mq_attr __user *, u_attr)
882 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
885 return do_mq_open(u_name, oflag, mode, u_attr ? &attr : NULL);
888 SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
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;
897 name = getname(u_name);
899 return PTR_ERR(name);
901 audit_inode_parent_hidden(name, mnt->mnt_root);
902 err = mnt_want_write(mnt);
905 inode_lock_nested(d_inode(mnt->mnt_root), I_MUTEX_PARENT);
906 dentry = lookup_one_len(name->name, mnt->mnt_root,
908 if (IS_ERR(dentry)) {
909 err = PTR_ERR(dentry);
913 inode = d_inode(dentry);
918 err = vfs_unlink(d_inode(dentry->d_parent), dentry, NULL);
923 inode_unlock(d_inode(mnt->mnt_root));
933 /* Pipelined send and receive functions.
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
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.
949 * The same algorithm is used for senders.
952 /* pipelined_send() - send a message directly to the task waiting in
953 * sys_mq_timedreceive() (without inserting message into a queue).
955 static 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)
960 receiver->msg = message;
961 list_del(&receiver->list);
962 wake_q_add(wake_q, receiver->task);
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.
971 receiver->state = STATE_READY;
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). */
976 static inline void pipelined_receive(struct wake_q_head *wake_q,
977 struct mqueue_inode_info *info)
979 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
983 wake_up_interruptible(&info->wait_q);
986 if (msg_insert(sender->msg, info))
989 list_del(&sender->list);
990 wake_q_add(wake_q, sender->task);
991 sender->state = STATE_READY;
994 static 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)
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;
1007 DEFINE_WAKE_Q(wake_q);
1009 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
1013 expires = timespec64_to_ktime(*ts);
1017 audit_mq_sendrecv(mqdes, msg_len, msg_prio, ts);
1020 if (unlikely(!f.file)) {
1025 inode = file_inode(f.file);
1026 if (unlikely(f.file->f_op != &mqueue_file_operations)) {
1030 info = MQUEUE_I(inode);
1033 if (unlikely(!(f.file->f_mode & FMODE_WRITE))) {
1038 if (unlikely(msg_len > info->attr.mq_msgsize)) {
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);
1050 msg_ptr->m_ts = msg_len;
1051 msg_ptr->m_type = msg_prio;
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.
1058 if (!info->node_cache)
1059 new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
1061 spin_lock(&info->lock);
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;
1072 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
1073 if (f.file->f_flags & O_NONBLOCK) {
1076 wait.task = current;
1077 wait.msg = (void *) msg_ptr;
1078 wait.state = STATE_NONE;
1079 ret = wq_sleep(info, SEND, timeout, &wait);
1081 * wq_sleep must be called with info->lock held, and
1082 * returns with the lock released
1087 receiver = wq_get_first_waiter(info, RECV);
1089 pipelined_send(&wake_q, info, msg_ptr, receiver);
1091 /* adds message to the queue */
1092 ret = msg_insert(msg_ptr, info);
1097 inode->i_atime = inode->i_mtime = inode->i_ctime =
1098 current_time(inode);
1101 spin_unlock(&info->lock);
1112 static 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)
1117 struct msg_msg *msg_ptr;
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;
1126 expires = timespec64_to_ktime(*ts);
1130 audit_mq_sendrecv(mqdes, msg_len, 0, ts);
1133 if (unlikely(!f.file)) {
1138 inode = file_inode(f.file);
1139 if (unlikely(f.file->f_op != &mqueue_file_operations)) {
1143 info = MQUEUE_I(inode);
1146 if (unlikely(!(f.file->f_mode & FMODE_READ))) {
1151 /* checks if buffer is big enough */
1152 if (unlikely(msg_len < info->attr.mq_msgsize)) {
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.
1162 if (!info->node_cache)
1163 new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
1165 spin_lock(&info->lock);
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;
1175 if (info->attr.mq_curmsgs == 0) {
1176 if (f.file->f_flags & O_NONBLOCK) {
1177 spin_unlock(&info->lock);
1180 wait.task = current;
1181 wait.state = STATE_NONE;
1182 ret = wq_sleep(info, RECV, timeout, &wait);
1186 DEFINE_WAKE_Q(wake_q);
1188 msg_ptr = msg_get(info);
1190 inode->i_atime = inode->i_mtime = inode->i_ctime =
1191 current_time(inode);
1193 /* There is now free space in queue. */
1194 pipelined_receive(&wake_q, info);
1195 spin_unlock(&info->lock);
1200 ret = msg_ptr->m_ts;
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)) {
1214 SYSCALL_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)
1218 struct timespec64 ts, *p = NULL;
1219 if (u_abs_timeout) {
1220 int res = prepare_timeout(u_abs_timeout, &ts);
1225 return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
1228 SYSCALL_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)
1232 struct timespec64 ts, *p = NULL;
1233 if (u_abs_timeout) {
1234 int res = prepare_timeout(u_abs_timeout, &ts);
1239 return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
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.
1247 static int do_mq_notify(mqd_t mqdes, const struct sigevent *notification)
1252 struct inode *inode;
1253 struct mqueue_inode_info *info;
1256 audit_mq_notify(mqdes, notification);
1260 if (notification != NULL) {
1261 if (unlikely(notification->sigev_notify != SIGEV_NONE &&
1262 notification->sigev_notify != SIGEV_SIGNAL &&
1263 notification->sigev_notify != SIGEV_THREAD))
1265 if (notification->sigev_notify == SIGEV_SIGNAL &&
1266 !valid_signal(notification->sigev_signo)) {
1269 if (notification->sigev_notify == SIGEV_THREAD) {
1272 /* create the notify skb */
1273 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1277 if (copy_from_user(nc->data,
1278 notification->sigev_value.sival_ptr,
1279 NOTIFY_COOKIE_LEN)) {
1284 /* TODO: add a header? */
1285 skb_put(nc, NOTIFY_COOKIE_LEN);
1286 /* and attach it to the socket */
1288 f = fdget(notification->sigev_signo);
1293 sock = netlink_getsockbyfilp(f.file);
1296 ret = PTR_ERR(sock);
1300 timeo = MAX_SCHEDULE_TIMEOUT;
1301 ret = netlink_attachskb(sock, nc, &timeo, NULL);
1317 inode = file_inode(f.file);
1318 if (unlikely(f.file->f_op != &mqueue_file_operations)) {
1322 info = MQUEUE_I(inode);
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);
1331 } else if (info->notify_owner != NULL) {
1334 switch (notification->sigev_notify) {
1336 info->notify.sigev_notify = SIGEV_NONE;
1339 info->notify_sock = sock;
1340 info->notify_cookie = nc;
1343 info->notify.sigev_notify = SIGEV_THREAD;
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;
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);
1357 spin_unlock(&info->lock);
1362 netlink_detachskb(sock, nc);
1370 SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1371 const struct sigevent __user *, u_notification)
1373 struct sigevent n, *p = NULL;
1374 if (u_notification) {
1375 if (copy_from_user(&n, u_notification, sizeof(struct sigevent)))
1379 return do_mq_notify(mqdes, p);
1382 static int do_mq_getsetattr(int mqdes, struct mq_attr *new, struct mq_attr *old)
1385 struct inode *inode;
1386 struct mqueue_inode_info *info;
1388 if (new && (new->mq_flags & (~O_NONBLOCK)))
1395 if (unlikely(f.file->f_op != &mqueue_file_operations)) {
1400 inode = file_inode(f.file);
1401 info = MQUEUE_I(inode);
1403 spin_lock(&info->lock);
1407 old->mq_flags = f.file->f_flags & O_NONBLOCK;
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;
1415 f.file->f_flags &= ~O_NONBLOCK;
1416 spin_unlock(&f.file->f_lock);
1418 inode->i_atime = inode->i_ctime = current_time(inode);
1421 spin_unlock(&info->lock);
1426 SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1427 const struct mq_attr __user *, u_mqstat,
1428 struct mq_attr __user *, u_omqstat)
1431 struct mq_attr mqstat, omqstat;
1432 struct mq_attr *new = NULL, *old = NULL;
1436 if (copy_from_user(new, u_mqstat, sizeof(struct mq_attr)))
1442 ret = do_mq_getsetattr(mqdes, new, old);
1446 if (copy_to_user(u_omqstat, old, sizeof(struct mq_attr)))
1451 #ifdef CONFIG_COMPAT
1453 struct 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 */
1461 static inline int get_compat_mq_attr(struct mq_attr *attr,
1462 const struct compat_mq_attr __user *uattr)
1464 struct compat_mq_attr v;
1466 if (copy_from_user(&v, uattr, sizeof(*uattr)))
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;
1477 static inline int put_compat_mq_attr(const struct mq_attr *attr,
1478 struct compat_mq_attr __user *uattr)
1480 struct compat_mq_attr v;
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)))
1492 COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
1493 int, oflag, compat_mode_t, mode,
1494 struct compat_mq_attr __user *, u_attr)
1496 struct mq_attr attr, *p = NULL;
1497 if (u_attr && oflag & O_CREAT) {
1499 if (get_compat_mq_attr(&attr, u_attr))
1502 return do_mq_open(u_name, oflag, mode, p);
1505 COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1506 const struct compat_sigevent __user *, u_notification)
1508 struct sigevent n, *p = NULL;
1509 if (u_notification) {
1510 if (get_compat_sigevent(&n, u_notification))
1512 if (n.sigev_notify == SIGEV_THREAD)
1513 n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
1516 return do_mq_notify(mqdes, p);
1519 COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1520 const struct compat_mq_attr __user *, u_mqstat,
1521 struct compat_mq_attr __user *, u_omqstat)
1524 struct mq_attr mqstat, omqstat;
1525 struct mq_attr *new = NULL, *old = NULL;
1529 if (get_compat_mq_attr(new, u_mqstat))
1535 ret = do_mq_getsetattr(mqdes, new, old);
1539 if (put_compat_mq_attr(old, u_omqstat))
1545 #ifdef CONFIG_COMPAT_32BIT_TIME
1546 static int compat_prepare_timeout(const struct old_timespec32 __user *p,
1547 struct timespec64 *ts)
1549 if (get_old_timespec32(ts, p))
1551 if (!timespec64_valid(ts))
1556 SYSCALL_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)
1561 struct timespec64 ts, *p = NULL;
1562 if (u_abs_timeout) {
1563 int res = compat_prepare_timeout(u_abs_timeout, &ts);
1568 return do_mq_timedsend(mqdes, u_msg_ptr, msg_len, msg_prio, p);
1571 SYSCALL_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)
1576 struct timespec64 ts, *p = NULL;
1577 if (u_abs_timeout) {
1578 int res = compat_prepare_timeout(u_abs_timeout, &ts);
1583 return do_mq_timedreceive(mqdes, u_msg_ptr, msg_len, u_msg_prio, p);
1587 static const struct inode_operations mqueue_dir_inode_operations = {
1588 .lookup = simple_lookup,
1589 .create = mqueue_create,
1590 .unlink = mqueue_unlink,
1593 static 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,
1600 static 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,
1607 static const struct fs_context_operations mqueue_fs_context_ops = {
1608 .free = mqueue_fs_context_free,
1609 .get_tree = mqueue_get_tree,
1612 static struct file_system_type mqueue_fs_type = {
1614 .init_fs_context = mqueue_init_fs_context,
1615 .kill_sb = kill_litter_super,
1616 .fs_flags = FS_USERNS_MOUNT,
1619 int mq_init_ns(struct ipc_namespace *ns)
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;
1630 m = mq_create_mount(ns);
1637 void mq_clear_sbinfo(struct ipc_namespace *ns)
1639 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1642 void mq_put_mnt(struct ipc_namespace *ns)
1644 kern_unmount(ns->mq_mnt);
1647 static int __init init_mqueue_fs(void)
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)
1657 /* ignore failures - they are not fatal */
1658 mq_sysctl_table = mq_register_sysctl_table();
1660 error = register_filesystem(&mqueue_fs_type);
1664 spin_lock_init(&mq_lock);
1666 error = mq_init_ns(&init_ipc_ns);
1668 goto out_filesystem;
1673 unregister_filesystem(&mqueue_fs_type);
1675 if (mq_sysctl_table)
1676 unregister_sysctl_table(mq_sysctl_table);
1677 kmem_cache_destroy(mqueue_inode_cachep);
1681 device_initcall(init_mqueue_fs);