blob: af087fb40bf7406ec1cb7f4b64616544c00b56a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 <asm/uaccess.h>
16
17struct 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
25static 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
37static 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
Heiko Carstens5d70a592014-03-04 11:17:50 +010049COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
50 int, oflag, compat_mode_t, mode,
51 struct compat_mq_attr __user *, u_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 void __user *p = NULL;
54 if (u_attr && oflag & O_CREAT) {
55 struct mq_attr attr;
Dan Rosenberg03145be2010-10-27 15:34:17 -070056
57 memset(&attr, 0, sizeof(attr));
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
Manfred Spraul239521f2014-01-27 17:07:04 -080067static int compat_prepare_timeout(struct timespec __user **p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 const struct compat_timespec __user *u)
69{
70 struct timespec ts;
71 if (!u) {
72 *p = NULL;
73 return 0;
74 }
75 *p = compat_alloc_user_space(sizeof(ts));
76 if (get_compat_timespec(&ts, u) || copy_to_user(*p, &ts, sizeof(ts)))
77 return -EFAULT;
78 return 0;
79}
80
81asmlinkage long compat_sys_mq_timedsend(mqd_t mqdes,
82 const char __user *u_msg_ptr,
83 size_t msg_len, unsigned int msg_prio,
84 const struct compat_timespec __user *u_abs_timeout)
85{
86 struct timespec __user *u_ts;
87
88 if (compat_prepare_timeout(&u_ts, u_abs_timeout))
89 return -EFAULT;
90
91 return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
92 msg_prio, u_ts);
93}
94
95asmlinkage ssize_t compat_sys_mq_timedreceive(mqd_t mqdes,
96 char __user *u_msg_ptr,
97 size_t msg_len, unsigned int __user *u_msg_prio,
98 const struct compat_timespec __user *u_abs_timeout)
99{
100 struct timespec __user *u_ts;
101 if (compat_prepare_timeout(&u_ts, u_abs_timeout))
102 return -EFAULT;
103
104 return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
105 u_msg_prio, u_ts);
106}
107
Heiko Carstens5d70a592014-03-04 11:17:50 +0100108COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
109 const struct compat_sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct sigevent __user *p = NULL;
112 if (u_notification) {
113 struct sigevent n;
114 p = compat_alloc_user_space(sizeof(*p));
115 if (get_compat_sigevent(&n, u_notification))
116 return -EFAULT;
117 if (n.sigev_notify == SIGEV_THREAD)
118 n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
119 if (copy_to_user(p, &n, sizeof(*p)))
120 return -EFAULT;
121 }
122 return sys_mq_notify(mqdes, p);
123}
124
Heiko Carstens5d70a592014-03-04 11:17:50 +0100125COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
126 const struct compat_mq_attr __user *, u_mqstat,
127 struct compat_mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct mq_attr mqstat;
130 struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
131 long ret;
132
Dan Rosenberg03145be2010-10-27 15:34:17 -0700133 memset(&mqstat, 0, sizeof(mqstat));
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (u_mqstat) {
136 if (get_compat_mq_attr(&mqstat, u_mqstat) ||
137 copy_to_user(p, &mqstat, sizeof(mqstat)))
138 return -EFAULT;
139 }
140 ret = sys_mq_getsetattr(mqdes,
141 u_mqstat ? p : NULL,
142 u_omqstat ? p + 1 : NULL);
143 if (ret)
144 return ret;
145 if (u_omqstat) {
146 if (copy_from_user(&mqstat, p + 1, sizeof(mqstat)) ||
147 put_compat_mq_attr(&mqstat, u_omqstat))
148 return -EFAULT;
149 }
150 return 0;
151}