Merge branch 'linux-4.4' of git://github.com/skeggsb/linux into drm-fixes
[deliverable/linux.git] / ipc / compat_mq.c
CommitLineData
1da177e4
LT
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
7153e402 15#include <linux/uaccess.h>
1da177e4
LT
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
5d70a596
HC
49COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name,
50 int, oflag, compat_mode_t, mode,
51 struct compat_mq_attr __user *, u_attr)
1da177e4
LT
52{
53 void __user *p = NULL;
54 if (u_attr && oflag & O_CREAT) {
55 struct mq_attr attr;
03145beb
DR
56
57 memset(&attr, 0, sizeof(attr));
58
1da177e4
LT
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
8eee9093
HC
67COMPAT_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)
1da177e4
LT
71{
72 struct timespec __user *u_ts;
73
81993e81 74 if (compat_convert_timespec(&u_ts, u_abs_timeout))
1da177e4
LT
75 return -EFAULT;
76
77 return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
78 msg_prio, u_ts);
79}
80
8eee9093
HC
81COMPAT_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)
1da177e4
LT
85{
86 struct timespec __user *u_ts;
81993e81
PA
87
88 if (compat_convert_timespec(&u_ts, u_abs_timeout))
1da177e4
LT
89 return -EFAULT;
90
91 return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
92 u_msg_prio, u_ts);
93}
94
5d70a596
HC
95COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
96 const struct compat_sigevent __user *, u_notification)
1da177e4
LT
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
5d70a596
HC
112COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
113 const struct compat_mq_attr __user *, u_mqstat,
114 struct compat_mq_attr __user *, u_omqstat)
1da177e4
LT
115{
116 struct mq_attr mqstat;
117 struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
118 long ret;
119
03145beb
DR
120 memset(&mqstat, 0, sizeof(mqstat));
121
1da177e4
LT
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}
This page took 0.689962 seconds and 5 git commands to generate.