xref: /openbmc/linux/ipc/syscall.c (revision 65749e0bb5e7de876ee43d3f601e32afe17e9248)
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:
73baed7fc9SChristoph Hellwig 		return sys_msgctl(first, second, (struct msqid_ds __user *)ptr);
74baed7fc9SChristoph Hellwig 
75baed7fc9SChristoph Hellwig 	case SHMAT:
76baed7fc9SChristoph Hellwig 		switch (version) {
77baed7fc9SChristoph Hellwig 		default: {
78baed7fc9SChristoph Hellwig 			unsigned long raddr;
79baed7fc9SChristoph Hellwig 			ret = do_shmat(first, (char __user *)ptr,
80079a96aeSWill Deacon 				       second, &raddr, SHMLBA);
81baed7fc9SChristoph Hellwig 			if (ret)
82baed7fc9SChristoph Hellwig 				return ret;
83baed7fc9SChristoph Hellwig 			return put_user(raddr, (unsigned long __user *) third);
84baed7fc9SChristoph Hellwig 		}
85baed7fc9SChristoph Hellwig 		case 1:
86baed7fc9SChristoph Hellwig 			/*
87baed7fc9SChristoph Hellwig 			 * This was the entry point for kernel-originating calls
88baed7fc9SChristoph Hellwig 			 * from iBCS2 in 2.2 days.
89baed7fc9SChristoph Hellwig 			 */
90baed7fc9SChristoph Hellwig 			return -EINVAL;
91baed7fc9SChristoph Hellwig 		}
92baed7fc9SChristoph Hellwig 	case SHMDT:
93baed7fc9SChristoph Hellwig 		return sys_shmdt((char __user *)ptr);
94baed7fc9SChristoph Hellwig 	case SHMGET:
95*65749e0bSDominik Brodowski 		return ksys_shmget(first, second, third);
96baed7fc9SChristoph Hellwig 	case SHMCTL:
97baed7fc9SChristoph Hellwig 		return sys_shmctl(first, second,
98baed7fc9SChristoph Hellwig 				   (struct shmid_ds __user *) ptr);
99baed7fc9SChristoph Hellwig 	default:
100baed7fc9SChristoph Hellwig 		return -ENOSYS;
101baed7fc9SChristoph Hellwig 	}
102baed7fc9SChristoph Hellwig }
103baed7fc9SChristoph Hellwig #endif
10420bc2a3aSAl Viro 
10520bc2a3aSAl Viro #ifdef CONFIG_COMPAT
10620bc2a3aSAl Viro #include <linux/compat.h>
10720bc2a3aSAl Viro 
10820bc2a3aSAl Viro #ifndef COMPAT_SHMLBA
10920bc2a3aSAl Viro #define COMPAT_SHMLBA	SHMLBA
11020bc2a3aSAl Viro #endif
11120bc2a3aSAl Viro 
11220bc2a3aSAl Viro struct compat_ipc_kludge {
11320bc2a3aSAl Viro 	compat_uptr_t msgp;
11420bc2a3aSAl Viro 	compat_long_t msgtyp;
11520bc2a3aSAl Viro };
11620bc2a3aSAl Viro 
11720bc2a3aSAl Viro #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
11820bc2a3aSAl Viro COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second,
11920bc2a3aSAl Viro 	u32, third, compat_uptr_t, ptr, u32, fifth)
12020bc2a3aSAl Viro {
12120bc2a3aSAl Viro 	int version;
12220bc2a3aSAl Viro 	u32 pad;
12320bc2a3aSAl Viro 
12420bc2a3aSAl Viro 	version = call >> 16; /* hack for backward compatibility */
12520bc2a3aSAl Viro 	call &= 0xffff;
12620bc2a3aSAl Viro 
12720bc2a3aSAl Viro 	switch (call) {
12820bc2a3aSAl Viro 	case SEMOP:
12920bc2a3aSAl Viro 		/* struct sembuf is the same on 32 and 64bit :)) */
13041f4f0e2SDominik Brodowski 		return ksys_semtimedop(first, compat_ptr(ptr), second, NULL);
13120bc2a3aSAl Viro 	case SEMTIMEDOP:
13241f4f0e2SDominik Brodowski 		return compat_ksys_semtimedop(first, compat_ptr(ptr), second,
13320bc2a3aSAl Viro 						compat_ptr(fifth));
13420bc2a3aSAl Viro 	case SEMGET:
13569894718SDominik Brodowski 		return ksys_semget(first, second, third);
13620bc2a3aSAl Viro 	case SEMCTL:
13720bc2a3aSAl Viro 		if (!ptr)
13820bc2a3aSAl Viro 			return -EINVAL;
13920bc2a3aSAl Viro 		if (get_user(pad, (u32 __user *) compat_ptr(ptr)))
14020bc2a3aSAl Viro 			return -EFAULT;
141d969c6faSDominik Brodowski 		return compat_ksys_semctl(first, second, third, pad);
14220bc2a3aSAl Viro 
14320bc2a3aSAl Viro 	case MSGSND:
14420bc2a3aSAl Viro 		return compat_sys_msgsnd(first, ptr, second, third);
14520bc2a3aSAl Viro 
14620bc2a3aSAl Viro 	case MSGRCV: {
14720bc2a3aSAl Viro 		void __user *uptr = compat_ptr(ptr);
14820bc2a3aSAl Viro 
14920bc2a3aSAl Viro 		if (first < 0 || second < 0)
15020bc2a3aSAl Viro 			return -EINVAL;
15120bc2a3aSAl Viro 
15220bc2a3aSAl Viro 		if (!version) {
15320bc2a3aSAl Viro 			struct compat_ipc_kludge ipck;
15420bc2a3aSAl Viro 			if (!uptr)
15520bc2a3aSAl Viro 				return -EINVAL;
15620bc2a3aSAl Viro 			if (copy_from_user(&ipck, uptr, sizeof(ipck)))
15720bc2a3aSAl Viro 				return -EFAULT;
15820bc2a3aSAl Viro 			return compat_sys_msgrcv(first, ipck.msgp, second,
15920bc2a3aSAl Viro 						 ipck.msgtyp, third);
16020bc2a3aSAl Viro 		}
16120bc2a3aSAl Viro 		return compat_sys_msgrcv(first, ptr, second, fifth, third);
16220bc2a3aSAl Viro 	}
16320bc2a3aSAl Viro 	case MSGGET:
1643d65661aSDominik Brodowski 		return ksys_msgget(first, second);
16520bc2a3aSAl Viro 	case MSGCTL:
16620bc2a3aSAl Viro 		return compat_sys_msgctl(first, second, compat_ptr(ptr));
16720bc2a3aSAl Viro 
16820bc2a3aSAl Viro 	case SHMAT: {
16920bc2a3aSAl Viro 		int err;
17020bc2a3aSAl Viro 		unsigned long raddr;
17120bc2a3aSAl Viro 
17220bc2a3aSAl Viro 		if (version == 1)
17320bc2a3aSAl Viro 			return -EINVAL;
17420bc2a3aSAl Viro 		err = do_shmat(first, compat_ptr(ptr), second, &raddr,
17520bc2a3aSAl Viro 			       COMPAT_SHMLBA);
17620bc2a3aSAl Viro 		if (err < 0)
17720bc2a3aSAl Viro 			return err;
1786aa211e8SLinus Torvalds 		return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third));
17920bc2a3aSAl Viro 	}
18020bc2a3aSAl Viro 	case SHMDT:
18120bc2a3aSAl Viro 		return sys_shmdt(compat_ptr(ptr));
18220bc2a3aSAl Viro 	case SHMGET:
183*65749e0bSDominik Brodowski 		return ksys_shmget(first, (unsigned int)second, third);
18420bc2a3aSAl Viro 	case SHMCTL:
18520bc2a3aSAl Viro 		return compat_sys_shmctl(first, second, compat_ptr(ptr));
18620bc2a3aSAl Viro 	}
18720bc2a3aSAl Viro 
18820bc2a3aSAl Viro 	return -ENOSYS;
18920bc2a3aSAl Viro }
19020bc2a3aSAl Viro #endif
19120bc2a3aSAl Viro #endif
192