1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2baed7fc9SChristoph Hellwig /* 3baed7fc9SChristoph Hellwig * sys_ipc() is the old de-multiplexer for the SysV IPC calls. 4baed7fc9SChristoph Hellwig * 5baed7fc9SChristoph Hellwig * This is really horribly ugly, and new architectures should just wire up 6baed7fc9SChristoph Hellwig * the individual syscalls instead. 7baed7fc9SChristoph Hellwig */ 8baed7fc9SChristoph Hellwig #include <linux/unistd.h> 920bc2a3aSAl Viro #include <linux/syscalls.h> 10baed7fc9SChristoph Hellwig 11baed7fc9SChristoph Hellwig #ifdef __ARCH_WANT_SYS_IPC 12baed7fc9SChristoph Hellwig #include <linux/errno.h> 13baed7fc9SChristoph Hellwig #include <linux/ipc.h> 14baed7fc9SChristoph Hellwig #include <linux/shm.h> 15baed7fc9SChristoph Hellwig #include <linux/uaccess.h> 16baed7fc9SChristoph Hellwig 1745575f5aSAnton Blanchard SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second, 18baed7fc9SChristoph Hellwig unsigned long, third, void __user *, ptr, long, fifth) 19baed7fc9SChristoph Hellwig { 20baed7fc9SChristoph Hellwig int version, ret; 21baed7fc9SChristoph Hellwig 22baed7fc9SChristoph Hellwig version = call >> 16; /* hack for backward compatibility */ 23baed7fc9SChristoph Hellwig call &= 0xffff; 24baed7fc9SChristoph Hellwig 25baed7fc9SChristoph Hellwig switch (call) { 26baed7fc9SChristoph Hellwig case SEMOP: 27baed7fc9SChristoph Hellwig return sys_semtimedop(first, (struct sembuf __user *)ptr, 28baed7fc9SChristoph Hellwig second, NULL); 29baed7fc9SChristoph Hellwig case SEMTIMEDOP: 30baed7fc9SChristoph Hellwig return sys_semtimedop(first, (struct sembuf __user *)ptr, 31baed7fc9SChristoph Hellwig second, 32baed7fc9SChristoph Hellwig (const struct timespec __user *)fifth); 33baed7fc9SChristoph Hellwig 34baed7fc9SChristoph Hellwig case SEMGET: 35baed7fc9SChristoph Hellwig return sys_semget(first, second, third); 36baed7fc9SChristoph Hellwig case SEMCTL: { 37e1fd1f49SAl Viro unsigned long arg; 38baed7fc9SChristoph Hellwig if (!ptr) 39baed7fc9SChristoph Hellwig return -EINVAL; 40e1fd1f49SAl Viro if (get_user(arg, (unsigned long __user *) ptr)) 41baed7fc9SChristoph Hellwig return -EFAULT; 42e1fd1f49SAl Viro return sys_semctl(first, second, third, arg); 43baed7fc9SChristoph Hellwig } 44baed7fc9SChristoph Hellwig 45baed7fc9SChristoph Hellwig case MSGSND: 46baed7fc9SChristoph Hellwig return sys_msgsnd(first, (struct msgbuf __user *) ptr, 47baed7fc9SChristoph Hellwig second, third); 48baed7fc9SChristoph Hellwig case MSGRCV: 49baed7fc9SChristoph Hellwig switch (version) { 50baed7fc9SChristoph Hellwig case 0: { 51baed7fc9SChristoph Hellwig struct ipc_kludge tmp; 52baed7fc9SChristoph Hellwig if (!ptr) 53baed7fc9SChristoph Hellwig return -EINVAL; 54baed7fc9SChristoph Hellwig 55baed7fc9SChristoph Hellwig if (copy_from_user(&tmp, 56baed7fc9SChristoph Hellwig (struct ipc_kludge __user *) ptr, 57baed7fc9SChristoph Hellwig sizeof(tmp))) 58baed7fc9SChristoph Hellwig return -EFAULT; 59baed7fc9SChristoph Hellwig return sys_msgrcv(first, tmp.msgp, second, 60baed7fc9SChristoph Hellwig tmp.msgtyp, third); 61baed7fc9SChristoph Hellwig } 62baed7fc9SChristoph Hellwig default: 63baed7fc9SChristoph Hellwig return sys_msgrcv(first, 64baed7fc9SChristoph Hellwig (struct msgbuf __user *) ptr, 65baed7fc9SChristoph Hellwig second, fifth, third); 66baed7fc9SChristoph Hellwig } 67baed7fc9SChristoph Hellwig case MSGGET: 68baed7fc9SChristoph Hellwig return sys_msgget((key_t) first, second); 69baed7fc9SChristoph Hellwig case MSGCTL: 70baed7fc9SChristoph Hellwig return sys_msgctl(first, second, (struct msqid_ds __user *)ptr); 71baed7fc9SChristoph Hellwig 72baed7fc9SChristoph Hellwig case SHMAT: 73baed7fc9SChristoph Hellwig switch (version) { 74baed7fc9SChristoph Hellwig default: { 75baed7fc9SChristoph Hellwig unsigned long raddr; 76baed7fc9SChristoph Hellwig ret = do_shmat(first, (char __user *)ptr, 77079a96aeSWill Deacon second, &raddr, SHMLBA); 78baed7fc9SChristoph Hellwig if (ret) 79baed7fc9SChristoph Hellwig return ret; 80baed7fc9SChristoph Hellwig return put_user(raddr, (unsigned long __user *) third); 81baed7fc9SChristoph Hellwig } 82baed7fc9SChristoph Hellwig case 1: 83baed7fc9SChristoph Hellwig /* 84baed7fc9SChristoph Hellwig * This was the entry point for kernel-originating calls 85baed7fc9SChristoph Hellwig * from iBCS2 in 2.2 days. 86baed7fc9SChristoph Hellwig */ 87baed7fc9SChristoph Hellwig return -EINVAL; 88baed7fc9SChristoph Hellwig } 89baed7fc9SChristoph Hellwig case SHMDT: 90baed7fc9SChristoph Hellwig return sys_shmdt((char __user *)ptr); 91baed7fc9SChristoph Hellwig case SHMGET: 92baed7fc9SChristoph Hellwig return sys_shmget(first, second, third); 93baed7fc9SChristoph Hellwig case SHMCTL: 94baed7fc9SChristoph Hellwig return sys_shmctl(first, second, 95baed7fc9SChristoph Hellwig (struct shmid_ds __user *) ptr); 96baed7fc9SChristoph Hellwig default: 97baed7fc9SChristoph Hellwig return -ENOSYS; 98baed7fc9SChristoph Hellwig } 99baed7fc9SChristoph Hellwig } 100baed7fc9SChristoph Hellwig #endif 10120bc2a3aSAl Viro 10220bc2a3aSAl Viro #ifdef CONFIG_COMPAT 10320bc2a3aSAl Viro #include <linux/compat.h> 10420bc2a3aSAl Viro 10520bc2a3aSAl Viro #ifndef COMPAT_SHMLBA 10620bc2a3aSAl Viro #define COMPAT_SHMLBA SHMLBA 10720bc2a3aSAl Viro #endif 10820bc2a3aSAl Viro 10920bc2a3aSAl Viro struct compat_ipc_kludge { 11020bc2a3aSAl Viro compat_uptr_t msgp; 11120bc2a3aSAl Viro compat_long_t msgtyp; 11220bc2a3aSAl Viro }; 11320bc2a3aSAl Viro 11420bc2a3aSAl Viro #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC 11520bc2a3aSAl Viro COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second, 11620bc2a3aSAl Viro u32, third, compat_uptr_t, ptr, u32, fifth) 11720bc2a3aSAl Viro { 11820bc2a3aSAl Viro int version; 11920bc2a3aSAl Viro u32 pad; 12020bc2a3aSAl Viro 12120bc2a3aSAl Viro version = call >> 16; /* hack for backward compatibility */ 12220bc2a3aSAl Viro call &= 0xffff; 12320bc2a3aSAl Viro 12420bc2a3aSAl Viro switch (call) { 12520bc2a3aSAl Viro case SEMOP: 12620bc2a3aSAl Viro /* struct sembuf is the same on 32 and 64bit :)) */ 12720bc2a3aSAl Viro return sys_semtimedop(first, compat_ptr(ptr), second, NULL); 12820bc2a3aSAl Viro case SEMTIMEDOP: 12920bc2a3aSAl Viro return compat_sys_semtimedop(first, compat_ptr(ptr), second, 13020bc2a3aSAl Viro compat_ptr(fifth)); 13120bc2a3aSAl Viro case SEMGET: 13220bc2a3aSAl Viro return sys_semget(first, second, third); 13320bc2a3aSAl Viro case SEMCTL: 13420bc2a3aSAl Viro if (!ptr) 13520bc2a3aSAl Viro return -EINVAL; 13620bc2a3aSAl Viro if (get_user(pad, (u32 __user *) compat_ptr(ptr))) 13720bc2a3aSAl Viro return -EFAULT; 13820bc2a3aSAl Viro return compat_sys_semctl(first, second, third, pad); 13920bc2a3aSAl Viro 14020bc2a3aSAl Viro case MSGSND: 14120bc2a3aSAl Viro return compat_sys_msgsnd(first, ptr, second, third); 14220bc2a3aSAl Viro 14320bc2a3aSAl Viro case MSGRCV: { 14420bc2a3aSAl Viro void __user *uptr = compat_ptr(ptr); 14520bc2a3aSAl Viro 14620bc2a3aSAl Viro if (first < 0 || second < 0) 14720bc2a3aSAl Viro return -EINVAL; 14820bc2a3aSAl Viro 14920bc2a3aSAl Viro if (!version) { 15020bc2a3aSAl Viro struct compat_ipc_kludge ipck; 15120bc2a3aSAl Viro if (!uptr) 15220bc2a3aSAl Viro return -EINVAL; 15320bc2a3aSAl Viro if (copy_from_user(&ipck, uptr, sizeof(ipck))) 15420bc2a3aSAl Viro return -EFAULT; 15520bc2a3aSAl Viro return compat_sys_msgrcv(first, ipck.msgp, second, 15620bc2a3aSAl Viro ipck.msgtyp, third); 15720bc2a3aSAl Viro } 15820bc2a3aSAl Viro return compat_sys_msgrcv(first, ptr, second, fifth, third); 15920bc2a3aSAl Viro } 16020bc2a3aSAl Viro case MSGGET: 16120bc2a3aSAl Viro return sys_msgget(first, second); 16220bc2a3aSAl Viro case MSGCTL: 16320bc2a3aSAl Viro return compat_sys_msgctl(first, second, compat_ptr(ptr)); 16420bc2a3aSAl Viro 16520bc2a3aSAl Viro case SHMAT: { 16620bc2a3aSAl Viro int err; 16720bc2a3aSAl Viro unsigned long raddr; 16820bc2a3aSAl Viro 16920bc2a3aSAl Viro if (version == 1) 17020bc2a3aSAl Viro return -EINVAL; 17120bc2a3aSAl Viro err = do_shmat(first, compat_ptr(ptr), second, &raddr, 17220bc2a3aSAl Viro COMPAT_SHMLBA); 17320bc2a3aSAl Viro if (err < 0) 17420bc2a3aSAl Viro return err; 17520bc2a3aSAl Viro return put_user(raddr, (compat_ulong_t *)compat_ptr(third)); 17620bc2a3aSAl Viro } 17720bc2a3aSAl Viro case SHMDT: 17820bc2a3aSAl Viro return sys_shmdt(compat_ptr(ptr)); 17920bc2a3aSAl Viro case SHMGET: 18020bc2a3aSAl Viro return sys_shmget(first, (unsigned)second, third); 18120bc2a3aSAl Viro case SHMCTL: 18220bc2a3aSAl Viro return compat_sys_shmctl(first, second, compat_ptr(ptr)); 18320bc2a3aSAl Viro } 18420bc2a3aSAl Viro 18520bc2a3aSAl Viro return -ENOSYS; 18620bc2a3aSAl Viro } 18720bc2a3aSAl Viro #endif 18820bc2a3aSAl Viro #endif 189