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