blob: 90d29f59cac6e525f1904bdb7a1d623e34ae4fe2 [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
Heiko Carstens8eee9092014-03-04 16:19:16 +010067COMPAT_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct timespec __user *u_ts;
73
H. Peter Anvin81993e82014-02-01 18:54:11 -080074 if (compat_convert_timespec(&u_ts, u_abs_timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EFAULT;
76
77 return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
78 msg_prio, u_ts);
79}
80
Heiko Carstens8eee9092014-03-04 16:19:16 +010081COMPAT_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct timespec __user *u_ts;
H. Peter Anvin81993e82014-02-01 18:54:11 -080087
88 if (compat_convert_timespec(&u_ts, u_abs_timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return -EFAULT;
90
91 return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
92 u_msg_prio, u_ts);
93}
94
Heiko Carstens5d70a592014-03-04 11:17:50 +010095COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
96 const struct compat_sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
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
Heiko Carstens5d70a592014-03-04 11:17:50 +0100112COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
113 const struct compat_mq_attr __user *, u_mqstat,
114 struct compat_mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct mq_attr mqstat;
117 struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
118 long ret;
119
Dan Rosenberg03145be2010-10-27 15:34:17 -0700120 memset(&mqstat, 0, sizeof(mqstat));
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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}