Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 2 | /* |
| 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 Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 9 | #include <linux/syscalls.h> |
Dominik Brodowski | 41f4f0e | 2018-03-20 19:48:14 +0100 | [diff] [blame] | 10 | #include <linux/security.h> |
| 11 | #include <linux/ipc_namespace.h> |
| 12 | #include "util.h" |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 13 | |
| 14 | #ifdef __ARCH_WANT_SYS_IPC |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/ipc.h> |
| 17 | #include <linux/shm.h> |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
| 19 | |
Anton Blanchard | 45575f5 | 2010-03-22 17:47:59 +1100 | [diff] [blame] | 20 | SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second, |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 21 | unsigned long, third, void __user *, ptr, long, fifth) |
| 22 | { |
| 23 | int version, ret; |
| 24 | |
| 25 | version = call >> 16; /* hack for backward compatibility */ |
| 26 | call &= 0xffff; |
| 27 | |
| 28 | switch (call) { |
| 29 | case SEMOP: |
Dominik Brodowski | 41f4f0e | 2018-03-20 19:48:14 +0100 | [diff] [blame] | 30 | return ksys_semtimedop(first, (struct sembuf __user *)ptr, |
| 31 | second, NULL); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 32 | case SEMTIMEDOP: |
Arnd Bergmann | 5dc0b15 | 2018-04-13 17:03:29 +0200 | [diff] [blame] | 33 | if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME)) |
| 34 | 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, |
| 38 | (const struct compat_timespec __user *)fifth); |
| 39 | else |
| 40 | return -ENOSYS; |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 41 | |
| 42 | case SEMGET: |
Dominik Brodowski | 6989471 | 2018-03-20 19:53:58 +0100 | [diff] [blame] | 43 | return ksys_semget(first, second, third); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 44 | case SEMCTL: { |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 45 | unsigned long arg; |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 46 | if (!ptr) |
| 47 | return -EINVAL; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 48 | if (get_user(arg, (unsigned long __user *) ptr)) |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 49 | return -EFAULT; |
Dominik Brodowski | d969c6f | 2018-03-20 20:00:39 +0100 | [diff] [blame] | 50 | return ksys_semctl(first, second, third, arg); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | case MSGSND: |
Dominik Brodowski | 31c213f | 2018-03-20 21:29:00 +0100 | [diff] [blame] | 54 | return ksys_msgsnd(first, (struct msgbuf __user *) ptr, |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 55 | 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 Brodowski | 078faac | 2018-03-20 21:25:57 +0100 | [diff] [blame] | 67 | return ksys_msgrcv(first, tmp.msgp, second, |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 68 | tmp.msgtyp, third); |
| 69 | } |
| 70 | default: |
Dominik Brodowski | 078faac | 2018-03-20 21:25:57 +0100 | [diff] [blame] | 71 | return ksys_msgrcv(first, |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 72 | (struct msgbuf __user *) ptr, |
| 73 | second, fifth, third); |
| 74 | } |
| 75 | case MSGGET: |
Dominik Brodowski | 3d65661 | 2018-03-20 20:06:04 +0100 | [diff] [blame] | 76 | return ksys_msgget((key_t) first, second); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 77 | case MSGCTL: |
Dominik Brodowski | e340db5 | 2018-03-20 20:15:28 +0100 | [diff] [blame] | 78 | return ksys_msgctl(first, second, |
| 79 | (struct msqid_ds __user *)ptr); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 80 | |
| 81 | case SHMAT: |
| 82 | switch (version) { |
| 83 | default: { |
| 84 | unsigned long raddr; |
| 85 | ret = do_shmat(first, (char __user *)ptr, |
Will Deacon | 079a96a | 2012-07-30 14:42:38 -0700 | [diff] [blame] | 86 | second, &raddr, SHMLBA); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 87 | 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 Brodowski | da1e2744 | 2018-03-20 20:09:48 +0100 | [diff] [blame] | 99 | return ksys_shmdt((char __user *)ptr); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 100 | case SHMGET: |
Dominik Brodowski | 65749e0 | 2018-03-20 20:07:53 +0100 | [diff] [blame] | 101 | return ksys_shmget(first, second, third); |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 102 | case SHMCTL: |
Dominik Brodowski | c84d079 | 2018-03-20 20:12:33 +0100 | [diff] [blame] | 103 | return ksys_shmctl(first, second, |
Christoph Hellwig | baed7fc | 2010-03-10 15:21:18 -0800 | [diff] [blame] | 104 | (struct shmid_ds __user *) ptr); |
| 105 | default: |
| 106 | return -ENOSYS; |
| 107 | } |
| 108 | } |
| 109 | #endif |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 110 | |
| 111 | #ifdef CONFIG_COMPAT |
| 112 | #include <linux/compat.h> |
| 113 | |
| 114 | #ifndef COMPAT_SHMLBA |
| 115 | #define COMPAT_SHMLBA SHMLBA |
| 116 | #endif |
| 117 | |
| 118 | struct compat_ipc_kludge { |
| 119 | compat_uptr_t msgp; |
| 120 | compat_long_t msgtyp; |
| 121 | }; |
| 122 | |
| 123 | #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC |
| 124 | COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second, |
| 125 | u32, third, compat_uptr_t, ptr, u32, fifth) |
| 126 | { |
| 127 | int version; |
| 128 | u32 pad; |
| 129 | |
| 130 | version = call >> 16; /* hack for backward compatibility */ |
| 131 | call &= 0xffff; |
| 132 | |
| 133 | switch (call) { |
| 134 | case SEMOP: |
| 135 | /* struct sembuf is the same on 32 and 64bit :)) */ |
Dominik Brodowski | 41f4f0e | 2018-03-20 19:48:14 +0100 | [diff] [blame] | 136 | return ksys_semtimedop(first, compat_ptr(ptr), second, NULL); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 137 | case SEMTIMEDOP: |
Arnd Bergmann | 5dc0b15 | 2018-04-13 17:03:29 +0200 | [diff] [blame] | 138 | if (!IS_ENABLED(CONFIG_COMPAT_32BIT_TIME)) |
| 139 | return -ENOSYS; |
Dominik Brodowski | 41f4f0e | 2018-03-20 19:48:14 +0100 | [diff] [blame] | 140 | return compat_ksys_semtimedop(first, compat_ptr(ptr), second, |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 141 | compat_ptr(fifth)); |
| 142 | case SEMGET: |
Dominik Brodowski | 6989471 | 2018-03-20 19:53:58 +0100 | [diff] [blame] | 143 | return ksys_semget(first, second, third); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 144 | case SEMCTL: |
| 145 | if (!ptr) |
| 146 | return -EINVAL; |
| 147 | if (get_user(pad, (u32 __user *) compat_ptr(ptr))) |
| 148 | return -EFAULT; |
Dominik Brodowski | d969c6f | 2018-03-20 20:00:39 +0100 | [diff] [blame] | 149 | return compat_ksys_semctl(first, second, third, pad); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 150 | |
| 151 | case MSGSND: |
Dominik Brodowski | 31c213f | 2018-03-20 21:29:00 +0100 | [diff] [blame] | 152 | return compat_ksys_msgsnd(first, ptr, second, third); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 153 | |
| 154 | case MSGRCV: { |
| 155 | void __user *uptr = compat_ptr(ptr); |
| 156 | |
| 157 | if (first < 0 || second < 0) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | if (!version) { |
| 161 | struct compat_ipc_kludge ipck; |
| 162 | if (!uptr) |
| 163 | return -EINVAL; |
| 164 | if (copy_from_user(&ipck, uptr, sizeof(ipck))) |
| 165 | return -EFAULT; |
Dominik Brodowski | 078faac | 2018-03-20 21:25:57 +0100 | [diff] [blame] | 166 | return compat_ksys_msgrcv(first, ipck.msgp, second, |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 167 | ipck.msgtyp, third); |
| 168 | } |
Dominik Brodowski | 078faac | 2018-03-20 21:25:57 +0100 | [diff] [blame] | 169 | return compat_ksys_msgrcv(first, ptr, second, fifth, third); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 170 | } |
| 171 | case MSGGET: |
Dominik Brodowski | 3d65661 | 2018-03-20 20:06:04 +0100 | [diff] [blame] | 172 | return ksys_msgget(first, second); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 173 | case MSGCTL: |
Dominik Brodowski | e340db5 | 2018-03-20 20:15:28 +0100 | [diff] [blame] | 174 | return compat_ksys_msgctl(first, second, compat_ptr(ptr)); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 175 | |
| 176 | case SHMAT: { |
| 177 | int err; |
| 178 | unsigned long raddr; |
| 179 | |
| 180 | if (version == 1) |
| 181 | return -EINVAL; |
| 182 | err = do_shmat(first, compat_ptr(ptr), second, &raddr, |
| 183 | COMPAT_SHMLBA); |
| 184 | if (err < 0) |
| 185 | return err; |
Linus Torvalds | 6aa211e | 2017-09-25 18:37:28 -0700 | [diff] [blame] | 186 | return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third)); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 187 | } |
| 188 | case SHMDT: |
Dominik Brodowski | da1e2744 | 2018-03-20 20:09:48 +0100 | [diff] [blame] | 189 | return ksys_shmdt(compat_ptr(ptr)); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 190 | case SHMGET: |
Dominik Brodowski | 65749e0 | 2018-03-20 20:07:53 +0100 | [diff] [blame] | 191 | return ksys_shmget(first, (unsigned int)second, third); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 192 | case SHMCTL: |
Dominik Brodowski | c84d079 | 2018-03-20 20:12:33 +0100 | [diff] [blame] | 193 | return compat_ksys_shmctl(first, second, compat_ptr(ptr)); |
Al Viro | 20bc2a3 | 2017-07-09 10:27:22 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return -ENOSYS; |
| 197 | } |
| 198 | #endif |
| 199 | #endif |