1b2441318SGreg 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> 1041f4f0e2SDominik Brodowski #include <linux/security.h> 1141f4f0e2SDominik Brodowski #include <linux/ipc_namespace.h> 1241f4f0e2SDominik Brodowski #include "util.h" 13baed7fc9SChristoph Hellwig 14baed7fc9SChristoph Hellwig #ifdef __ARCH_WANT_SYS_IPC 15baed7fc9SChristoph Hellwig #include <linux/errno.h> 16baed7fc9SChristoph Hellwig #include <linux/ipc.h> 17baed7fc9SChristoph Hellwig #include <linux/shm.h> 18baed7fc9SChristoph Hellwig #include <linux/uaccess.h> 19baed7fc9SChristoph Hellwig 2045575f5aSAnton Blanchard SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second, 21baed7fc9SChristoph Hellwig unsigned long, third, void __user *, ptr, long, fifth) 22baed7fc9SChristoph Hellwig { 23baed7fc9SChristoph Hellwig int version, ret; 24baed7fc9SChristoph Hellwig 25baed7fc9SChristoph Hellwig version = call >> 16; /* hack for backward compatibility */ 26baed7fc9SChristoph Hellwig call &= 0xffff; 27baed7fc9SChristoph Hellwig 28baed7fc9SChristoph Hellwig switch (call) { 29baed7fc9SChristoph Hellwig case SEMOP: 3041f4f0e2SDominik Brodowski return ksys_semtimedop(first, (struct sembuf __user *)ptr, 31baed7fc9SChristoph Hellwig second, NULL); 32baed7fc9SChristoph Hellwig case SEMTIMEDOP: 3341f4f0e2SDominik Brodowski return ksys_semtimedop(first, (struct sembuf __user *)ptr, 34baed7fc9SChristoph Hellwig second, 35baed7fc9SChristoph Hellwig (const struct timespec __user *)fifth); 36baed7fc9SChristoph Hellwig 37baed7fc9SChristoph Hellwig case SEMGET: 3869894718SDominik Brodowski return ksys_semget(first, second, third); 39baed7fc9SChristoph Hellwig case SEMCTL: { 40e1fd1f49SAl Viro unsigned long arg; 41baed7fc9SChristoph Hellwig if (!ptr) 42baed7fc9SChristoph Hellwig return -EINVAL; 43e1fd1f49SAl Viro if (get_user(arg, (unsigned long __user *) ptr)) 44baed7fc9SChristoph Hellwig return -EFAULT; 45d969c6faSDominik Brodowski return ksys_semctl(first, second, third, arg); 46baed7fc9SChristoph Hellwig } 47baed7fc9SChristoph Hellwig 48baed7fc9SChristoph Hellwig case MSGSND: 49baed7fc9SChristoph Hellwig return sys_msgsnd(first, (struct msgbuf __user *) ptr, 50baed7fc9SChristoph Hellwig second, third); 51baed7fc9SChristoph Hellwig case MSGRCV: 52baed7fc9SChristoph Hellwig switch (version) { 53baed7fc9SChristoph Hellwig case 0: { 54baed7fc9SChristoph Hellwig struct ipc_kludge tmp; 55baed7fc9SChristoph Hellwig if (!ptr) 56baed7fc9SChristoph Hellwig return -EINVAL; 57baed7fc9SChristoph Hellwig 58baed7fc9SChristoph Hellwig if (copy_from_user(&tmp, 59baed7fc9SChristoph Hellwig (struct ipc_kludge __user *) ptr, 60baed7fc9SChristoph Hellwig sizeof(tmp))) 61baed7fc9SChristoph Hellwig return -EFAULT; 62baed7fc9SChristoph Hellwig return sys_msgrcv(first, tmp.msgp, second, 63baed7fc9SChristoph Hellwig tmp.msgtyp, third); 64baed7fc9SChristoph Hellwig } 65baed7fc9SChristoph Hellwig default: 66baed7fc9SChristoph Hellwig return sys_msgrcv(first, 67baed7fc9SChristoph Hellwig (struct msgbuf __user *) ptr, 68baed7fc9SChristoph Hellwig second, fifth, third); 69baed7fc9SChristoph Hellwig } 70baed7fc9SChristoph Hellwig case MSGGET: 713d65661aSDominik Brodowski return ksys_msgget((key_t) first, second); 72baed7fc9SChristoph Hellwig case MSGCTL: 73*e340db56SDominik Brodowski return ksys_msgctl(first, second, 74*e340db56SDominik Brodowski (struct msqid_ds __user *)ptr); 75baed7fc9SChristoph Hellwig 76baed7fc9SChristoph Hellwig case SHMAT: 77baed7fc9SChristoph Hellwig switch (version) { 78baed7fc9SChristoph Hellwig default: { 79baed7fc9SChristoph Hellwig unsigned long raddr; 80baed7fc9SChristoph Hellwig ret = do_shmat(first, (char __user *)ptr, 81079a96aeSWill Deacon second, &raddr, SHMLBA); 82baed7fc9SChristoph Hellwig if (ret) 83baed7fc9SChristoph Hellwig return ret; 84baed7fc9SChristoph Hellwig return put_user(raddr, (unsigned long __user *) third); 85baed7fc9SChristoph Hellwig } 86baed7fc9SChristoph Hellwig case 1: 87baed7fc9SChristoph Hellwig /* 88baed7fc9SChristoph Hellwig * This was the entry point for kernel-originating calls 89baed7fc9SChristoph Hellwig * from iBCS2 in 2.2 days. 90baed7fc9SChristoph Hellwig */ 91baed7fc9SChristoph Hellwig return -EINVAL; 92baed7fc9SChristoph Hellwig } 93baed7fc9SChristoph Hellwig case SHMDT: 94da1e2744SDominik Brodowski return ksys_shmdt((char __user *)ptr); 95baed7fc9SChristoph Hellwig case SHMGET: 9665749e0bSDominik Brodowski return ksys_shmget(first, second, third); 97baed7fc9SChristoph Hellwig case SHMCTL: 98c84d0791SDominik Brodowski return ksys_shmctl(first, second, 99baed7fc9SChristoph Hellwig (struct shmid_ds __user *) ptr); 100baed7fc9SChristoph Hellwig default: 101baed7fc9SChristoph Hellwig return -ENOSYS; 102baed7fc9SChristoph Hellwig } 103baed7fc9SChristoph Hellwig } 104baed7fc9SChristoph Hellwig #endif 10520bc2a3aSAl Viro 10620bc2a3aSAl Viro #ifdef CONFIG_COMPAT 10720bc2a3aSAl Viro #include <linux/compat.h> 10820bc2a3aSAl Viro 10920bc2a3aSAl Viro #ifndef COMPAT_SHMLBA 11020bc2a3aSAl Viro #define COMPAT_SHMLBA SHMLBA 11120bc2a3aSAl Viro #endif 11220bc2a3aSAl Viro 11320bc2a3aSAl Viro struct compat_ipc_kludge { 11420bc2a3aSAl Viro compat_uptr_t msgp; 11520bc2a3aSAl Viro compat_long_t msgtyp; 11620bc2a3aSAl Viro }; 11720bc2a3aSAl Viro 11820bc2a3aSAl Viro #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC 11920bc2a3aSAl Viro COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second, 12020bc2a3aSAl Viro u32, third, compat_uptr_t, ptr, u32, fifth) 12120bc2a3aSAl Viro { 12220bc2a3aSAl Viro int version; 12320bc2a3aSAl Viro u32 pad; 12420bc2a3aSAl Viro 12520bc2a3aSAl Viro version = call >> 16; /* hack for backward compatibility */ 12620bc2a3aSAl Viro call &= 0xffff; 12720bc2a3aSAl Viro 12820bc2a3aSAl Viro switch (call) { 12920bc2a3aSAl Viro case SEMOP: 13020bc2a3aSAl Viro /* struct sembuf is the same on 32 and 64bit :)) */ 13141f4f0e2SDominik Brodowski return ksys_semtimedop(first, compat_ptr(ptr), second, NULL); 13220bc2a3aSAl Viro case SEMTIMEDOP: 13341f4f0e2SDominik Brodowski return compat_ksys_semtimedop(first, compat_ptr(ptr), second, 13420bc2a3aSAl Viro compat_ptr(fifth)); 13520bc2a3aSAl Viro case SEMGET: 13669894718SDominik Brodowski return ksys_semget(first, second, third); 13720bc2a3aSAl Viro case SEMCTL: 13820bc2a3aSAl Viro if (!ptr) 13920bc2a3aSAl Viro return -EINVAL; 14020bc2a3aSAl Viro if (get_user(pad, (u32 __user *) compat_ptr(ptr))) 14120bc2a3aSAl Viro return -EFAULT; 142d969c6faSDominik Brodowski return compat_ksys_semctl(first, second, third, pad); 14320bc2a3aSAl Viro 14420bc2a3aSAl Viro case MSGSND: 14520bc2a3aSAl Viro return compat_sys_msgsnd(first, ptr, second, third); 14620bc2a3aSAl Viro 14720bc2a3aSAl Viro case MSGRCV: { 14820bc2a3aSAl Viro void __user *uptr = compat_ptr(ptr); 14920bc2a3aSAl Viro 15020bc2a3aSAl Viro if (first < 0 || second < 0) 15120bc2a3aSAl Viro return -EINVAL; 15220bc2a3aSAl Viro 15320bc2a3aSAl Viro if (!version) { 15420bc2a3aSAl Viro struct compat_ipc_kludge ipck; 15520bc2a3aSAl Viro if (!uptr) 15620bc2a3aSAl Viro return -EINVAL; 15720bc2a3aSAl Viro if (copy_from_user(&ipck, uptr, sizeof(ipck))) 15820bc2a3aSAl Viro return -EFAULT; 15920bc2a3aSAl Viro return compat_sys_msgrcv(first, ipck.msgp, second, 16020bc2a3aSAl Viro ipck.msgtyp, third); 16120bc2a3aSAl Viro } 16220bc2a3aSAl Viro return compat_sys_msgrcv(first, ptr, second, fifth, third); 16320bc2a3aSAl Viro } 16420bc2a3aSAl Viro case MSGGET: 1653d65661aSDominik Brodowski return ksys_msgget(first, second); 16620bc2a3aSAl Viro case MSGCTL: 167*e340db56SDominik Brodowski return compat_ksys_msgctl(first, second, compat_ptr(ptr)); 16820bc2a3aSAl Viro 16920bc2a3aSAl Viro case SHMAT: { 17020bc2a3aSAl Viro int err; 17120bc2a3aSAl Viro unsigned long raddr; 17220bc2a3aSAl Viro 17320bc2a3aSAl Viro if (version == 1) 17420bc2a3aSAl Viro return -EINVAL; 17520bc2a3aSAl Viro err = do_shmat(first, compat_ptr(ptr), second, &raddr, 17620bc2a3aSAl Viro COMPAT_SHMLBA); 17720bc2a3aSAl Viro if (err < 0) 17820bc2a3aSAl Viro return err; 1796aa211e8SLinus Torvalds return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third)); 18020bc2a3aSAl Viro } 18120bc2a3aSAl Viro case SHMDT: 182da1e2744SDominik Brodowski return ksys_shmdt(compat_ptr(ptr)); 18320bc2a3aSAl Viro case SHMGET: 18465749e0bSDominik Brodowski return ksys_shmget(first, (unsigned int)second, third); 18520bc2a3aSAl Viro case SHMCTL: 186c84d0791SDominik Brodowski return compat_ksys_shmctl(first, second, compat_ptr(ptr)); 18720bc2a3aSAl Viro } 18820bc2a3aSAl Viro 18920bc2a3aSAl Viro return -ENOSYS; 19020bc2a3aSAl Viro } 19120bc2a3aSAl Viro #endif 19220bc2a3aSAl Viro #endif 193