1 /*
2 * ipc/compat_mq.c
3 * 32 bit emulation for POSIX message queue system calls
4 *
5 * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author: Arnd Bergmann <arnd@arndb.de>
7 */
8
9 #include <linux/compat.h>
10 #include <linux/fs.h>
11 #include <linux/kernel.h>
12 #include <linux/mqueue.h>
13 #include <linux/syscalls.h>
14
15 #include <linux/uaccess.h>
16
17 struct compat_mq_attr {
18 compat_long_t mq_flags; /* message queue flags */
19 compat_long_t mq_maxmsg; /* maximum number of messages */
20 compat_long_t mq_msgsize; /* maximum message size */
21 compat_long_t mq_curmsgs; /* number of messages currently queued */
22 compat_long_t __reserved[4]; /* ignored for input, zeroed for output */
23 };
24
get_compat_mq_attr(struct mq_attr * attr,const struct compat_mq_attr __user * uattr)25 static inline int get_compat_mq_attr(struct mq_attr *attr,
26 const struct compat_mq_attr __user *uattr)
27 {
28 if (!access_ok(VERIFY_READ, uattr, sizeof *uattr))
29 return -EFAULT;
30
31 return __get_user(attr->mq_flags, &uattr->mq_flags)
32 | __get_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
33 | __get_user(attr->mq_msgsize, &uattr->mq_msgsize)
34 | __get_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
35 }
36
put_compat_mq_attr(const struct mq_attr * attr,struct compat_mq_attr __user * uattr)37 static inline int put_compat_mq_attr(const struct mq_attr *attr,
38 struct compat_mq_attr __user *uattr)
39 {
40 if (clear_user(uattr, sizeof *uattr))
41 return -EFAULT;
42
43 return __put_user(attr->mq_flags, &uattr->mq_flags)
44 | __put_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
45 | __put_user(attr->mq_msgsize, &uattr->mq_msgsize)
46 | __put_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
47 }
48
COMPAT_SYSCALL_DEFINE4(mq_open,const char __user *,u_name,int,oflag,compat_mode_t,mode,struct compat_mq_attr __user *,u_attr)49 COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
50 int, oflag, compat_mode_t, mode,
51 struct compat_mq_attr __user *, u_attr)
52 {
53 void __user *p = NULL;
54 if (u_attr && oflag & O_CREAT) {
55 struct mq_attr attr;
56
57 memset(&attr, 0, sizeof(attr));
58
59 p = compat_alloc_user_space(sizeof(attr));
60 if (get_compat_mq_attr(&attr, u_attr) ||
61 copy_to_user(p, &attr, sizeof(attr)))
62 return -EFAULT;
63 }
64 return sys_mq_open(u_name, oflag, mode, p);
65 }
66
COMPAT_SYSCALL_DEFINE5(mq_timedsend,mqd_t,mqdes,const char __user *,u_msg_ptr,compat_size_t,msg_len,unsigned int,msg_prio,const struct compat_timespec __user *,u_abs_timeout)67 COMPAT_SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes,
68 const char __user *, u_msg_ptr,
69 compat_size_t, msg_len, unsigned int, msg_prio,
70 const struct compat_timespec __user *, u_abs_timeout)
71 {
72 struct timespec __user *u_ts;
73
74 if (compat_convert_timespec(&u_ts, u_abs_timeout))
75 return -EFAULT;
76
77 return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
78 msg_prio, u_ts);
79 }
80
COMPAT_SYSCALL_DEFINE5(mq_timedreceive,mqd_t,mqdes,char __user *,u_msg_ptr,compat_size_t,msg_len,unsigned int __user *,u_msg_prio,const struct compat_timespec __user *,u_abs_timeout)81 COMPAT_SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes,
82 char __user *, u_msg_ptr,
83 compat_size_t, msg_len, unsigned int __user *, u_msg_prio,
84 const struct compat_timespec __user *, u_abs_timeout)
85 {
86 struct timespec __user *u_ts;
87
88 if (compat_convert_timespec(&u_ts, u_abs_timeout))
89 return -EFAULT;
90
91 return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
92 u_msg_prio, u_ts);
93 }
94
COMPAT_SYSCALL_DEFINE2(mq_notify,mqd_t,mqdes,const struct compat_sigevent __user *,u_notification)95 COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
96 const struct compat_sigevent __user *, u_notification)
97 {
98 struct sigevent __user *p = NULL;
99 if (u_notification) {
100 struct sigevent n;
101 p = compat_alloc_user_space(sizeof(*p));
102 if (get_compat_sigevent(&n, u_notification))
103 return -EFAULT;
104 if (n.sigev_notify == SIGEV_THREAD)
105 n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
106 if (copy_to_user(p, &n, sizeof(*p)))
107 return -EFAULT;
108 }
109 return sys_mq_notify(mqdes, p);
110 }
111
COMPAT_SYSCALL_DEFINE3(mq_getsetattr,mqd_t,mqdes,const struct compat_mq_attr __user *,u_mqstat,struct compat_mq_attr __user *,u_omqstat)112 COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
113 const struct compat_mq_attr __user *, u_mqstat,
114 struct compat_mq_attr __user *, u_omqstat)
115 {
116 struct mq_attr mqstat;
117 struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
118 long ret;
119
120 memset(&mqstat, 0, sizeof(mqstat));
121
122 if (u_mqstat) {
123 if (get_compat_mq_attr(&mqstat, u_mqstat) ||
124 copy_to_user(p, &mqstat, sizeof(mqstat)))
125 return -EFAULT;
126 }
127 ret = sys_mq_getsetattr(mqdes,
128 u_mqstat ? p : NULL,
129 u_omqstat ? p + 1 : NULL);
130 if (ret)
131 return ret;
132 if (u_omqstat) {
133 if (copy_from_user(&mqstat, p + 1, sizeof(mqstat)) ||
134 put_compat_mq_attr(&mqstat, u_omqstat))
135 return -EFAULT;
136 }
137 return 0;
138 }
139