blob: dfb0e988d542b2c3e713af21ef0451c0c35b9a74 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -08002/*
3 * sys_ipc() is the old de-multiplexer for the SysV IPC calls.
4 *
5 * This is really horribly ugly, and new architectures should just wire up
6 * the individual syscalls instead.
7 */
8#include <linux/unistd.h>
Al Viro20bc2a32017-07-09 10:27:22 -04009#include <linux/syscalls.h>
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +010010#include <linux/security.h>
11#include <linux/ipc_namespace.h>
12#include "util.h"
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080013
14#ifdef __ARCH_WANT_SYS_IPC
15#include <linux/errno.h>
16#include <linux/ipc.h>
17#include <linux/shm.h>
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080018#include <linux/uaccess.h>
19
Arnd Bergmann58fa4a42019-01-16 14:15:20 +010020int ksys_ipc(unsigned int call, int first, unsigned long second,
21 unsigned long third, void __user * ptr, long fifth)
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080022{
23 int version, ret;
24
25 version = call >> 16; /* hack for backward compatibility */
26 call &= 0xffff;
27
28 switch (call) {
29 case SEMOP:
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +010030 return ksys_semtimedop(first, (struct sembuf __user *)ptr,
31 second, NULL);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080032 case SEMTIMEDOP:
Arnd Bergmann3ca47e92019-04-23 17:43:50 +020033 if (IS_ENABLED(CONFIG_64BIT))
Arnd Bergmann5dc0b152018-04-13 17:03:29 +020034 return ksys_semtimedop(first, ptr, second,
35 (const struct __kernel_timespec __user *)fifth);
36 else if (IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
37 return compat_ksys_semtimedop(first, ptr, second,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +020038 (const struct old_timespec32 __user *)fifth);
Arnd Bergmann5dc0b152018-04-13 17:03:29 +020039 else
40 return -ENOSYS;
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080041
42 case SEMGET:
Dominik Brodowski69894712018-03-20 19:53:58 +010043 return ksys_semget(first, second, third);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080044 case SEMCTL: {
Al Viroe1fd1f42013-03-05 15:04:55 -050045 unsigned long arg;
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080046 if (!ptr)
47 return -EINVAL;
Al Viroe1fd1f42013-03-05 15:04:55 -050048 if (get_user(arg, (unsigned long __user *) ptr))
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080049 return -EFAULT;
Arnd Bergmann275f2212018-12-31 22:22:40 +010050 return ksys_old_semctl(first, second, third, arg);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080051 }
52
53 case MSGSND:
Dominik Brodowski31c213f2018-03-20 21:29:00 +010054 return ksys_msgsnd(first, (struct msgbuf __user *) ptr,
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080055 second, third);
56 case MSGRCV:
57 switch (version) {
58 case 0: {
59 struct ipc_kludge tmp;
60 if (!ptr)
61 return -EINVAL;
62
63 if (copy_from_user(&tmp,
64 (struct ipc_kludge __user *) ptr,
65 sizeof(tmp)))
66 return -EFAULT;
Dominik Brodowski078faac2018-03-20 21:25:57 +010067 return ksys_msgrcv(first, tmp.msgp, second,
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080068 tmp.msgtyp, third);
69 }
70 default:
Dominik Brodowski078faac2018-03-20 21:25:57 +010071 return ksys_msgrcv(first,
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080072 (struct msgbuf __user *) ptr,
73 second, fifth, third);
74 }
75 case MSGGET:
Dominik Brodowski3d656612018-03-20 20:06:04 +010076 return ksys_msgget((key_t) first, second);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080077 case MSGCTL:
Arnd Bergmann275f2212018-12-31 22:22:40 +010078 return ksys_old_msgctl(first, second,
Dominik Brodowskie340db52018-03-20 20:15:28 +010079 (struct msqid_ds __user *)ptr);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080080
81 case SHMAT:
82 switch (version) {
83 default: {
84 unsigned long raddr;
85 ret = do_shmat(first, (char __user *)ptr,
Will Deacon079a96a2012-07-30 14:42:38 -070086 second, &raddr, SHMLBA);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -080087 if (ret)
88 return ret;
89 return put_user(raddr, (unsigned long __user *) third);
90 }
91 case 1:
92 /*
93 * This was the entry point for kernel-originating calls
94 * from iBCS2 in 2.2 days.
95 */
96 return -EINVAL;
97 }
98 case SHMDT:
Dominik Brodowskida1e27442018-03-20 20:09:48 +010099 return ksys_shmdt((char __user *)ptr);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -0800100 case SHMGET:
Dominik Brodowski65749e02018-03-20 20:07:53 +0100101 return ksys_shmget(first, second, third);
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -0800102 case SHMCTL:
Arnd Bergmann275f2212018-12-31 22:22:40 +0100103 return ksys_old_shmctl(first, second,
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -0800104 (struct shmid_ds __user *) ptr);
105 default:
106 return -ENOSYS;
107 }
108}
Arnd Bergmann58fa4a42019-01-16 14:15:20 +0100109
110SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
111 unsigned long, third, void __user *, ptr, long, fifth)
112{
113 return ksys_ipc(call, first, second, third, ptr, fifth);
114}
Christoph Hellwigbaed7fc2010-03-10 15:21:18 -0800115#endif
Al Viro20bc2a32017-07-09 10:27:22 -0400116
117#ifdef CONFIG_COMPAT
118#include <linux/compat.h>
119
120#ifndef COMPAT_SHMLBA
121#define COMPAT_SHMLBA SHMLBA
122#endif
123
124struct compat_ipc_kludge {
125 compat_uptr_t msgp;
126 compat_long_t msgtyp;
127};
128
129#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
Arnd Bergmann58fa4a42019-01-16 14:15:20 +0100130int compat_ksys_ipc(u32 call, int first, int second,
131 u32 third, compat_uptr_t ptr, u32 fifth)
Al Viro20bc2a32017-07-09 10:27:22 -0400132{
133 int version;
134 u32 pad;
135
136 version = call >> 16; /* hack for backward compatibility */
137 call &= 0xffff;
138
139 switch (call) {
140 case SEMOP:
141 /* struct sembuf is the same on 32 and 64bit :)) */
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100142 return ksys_semtimedop(first, compat_ptr(ptr), second, NULL);
Al Viro20bc2a32017-07-09 10:27:22 -0400143 case SEMTIMEDOP:
Arnd Bergmann5dc0b152018-04-13 17:03:29 +0200144 if (!IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
145 return -ENOSYS;
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100146 return compat_ksys_semtimedop(first, compat_ptr(ptr), second,
Al Viro20bc2a32017-07-09 10:27:22 -0400147 compat_ptr(fifth));
148 case SEMGET:
Dominik Brodowski69894712018-03-20 19:53:58 +0100149 return ksys_semget(first, second, third);
Al Viro20bc2a32017-07-09 10:27:22 -0400150 case SEMCTL:
151 if (!ptr)
152 return -EINVAL;
153 if (get_user(pad, (u32 __user *) compat_ptr(ptr)))
154 return -EFAULT;
Arnd Bergmann275f2212018-12-31 22:22:40 +0100155 return compat_ksys_old_semctl(first, second, third, pad);
Al Viro20bc2a32017-07-09 10:27:22 -0400156
157 case MSGSND:
Dominik Brodowski31c213f2018-03-20 21:29:00 +0100158 return compat_ksys_msgsnd(first, ptr, second, third);
Al Viro20bc2a32017-07-09 10:27:22 -0400159
160 case MSGRCV: {
161 void __user *uptr = compat_ptr(ptr);
162
163 if (first < 0 || second < 0)
164 return -EINVAL;
165
166 if (!version) {
167 struct compat_ipc_kludge ipck;
168 if (!uptr)
169 return -EINVAL;
170 if (copy_from_user(&ipck, uptr, sizeof(ipck)))
171 return -EFAULT;
Dominik Brodowski078faac2018-03-20 21:25:57 +0100172 return compat_ksys_msgrcv(first, ipck.msgp, second,
Al Viro20bc2a32017-07-09 10:27:22 -0400173 ipck.msgtyp, third);
174 }
Dominik Brodowski078faac2018-03-20 21:25:57 +0100175 return compat_ksys_msgrcv(first, ptr, second, fifth, third);
Al Viro20bc2a32017-07-09 10:27:22 -0400176 }
177 case MSGGET:
Dominik Brodowski3d656612018-03-20 20:06:04 +0100178 return ksys_msgget(first, second);
Al Viro20bc2a32017-07-09 10:27:22 -0400179 case MSGCTL:
Arnd Bergmann275f2212018-12-31 22:22:40 +0100180 return compat_ksys_old_msgctl(first, second, compat_ptr(ptr));
Al Viro20bc2a32017-07-09 10:27:22 -0400181
182 case SHMAT: {
183 int err;
184 unsigned long raddr;
185
186 if (version == 1)
187 return -EINVAL;
188 err = do_shmat(first, compat_ptr(ptr), second, &raddr,
189 COMPAT_SHMLBA);
190 if (err < 0)
191 return err;
Linus Torvalds6aa211e2017-09-25 18:37:28 -0700192 return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third));
Al Viro20bc2a32017-07-09 10:27:22 -0400193 }
194 case SHMDT:
Dominik Brodowskida1e27442018-03-20 20:09:48 +0100195 return ksys_shmdt(compat_ptr(ptr));
Al Viro20bc2a32017-07-09 10:27:22 -0400196 case SHMGET:
Dominik Brodowski65749e02018-03-20 20:07:53 +0100197 return ksys_shmget(first, (unsigned int)second, third);
Al Viro20bc2a32017-07-09 10:27:22 -0400198 case SHMCTL:
Arnd Bergmann275f2212018-12-31 22:22:40 +0100199 return compat_ksys_old_shmctl(first, second, compat_ptr(ptr));
Al Viro20bc2a32017-07-09 10:27:22 -0400200 }
201
202 return -ENOSYS;
203}
Arnd Bergmann58fa4a42019-01-16 14:15:20 +0100204
205COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second,
206 u32, third, compat_uptr_t, ptr, u32, fifth)
207{
208 return compat_ksys_ipc(call, first, second, third, ptr, fifth);
209}
Al Viro20bc2a32017-07-09 10:27:22 -0400210#endif
211#endif