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
2058fa4a41SArnd Bergmannint ksys_ipc(unsigned int call, int first, unsigned long second,
2158fa4a41SArnd Bergmann	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,
3141f4f0e2SDominik Brodowski				       second, NULL);
32baed7fc9SChristoph Hellwig	case SEMTIMEDOP:
333ca47e95SArnd Bergmann		if (IS_ENABLED(CONFIG_64BIT))
345dc0b152SArnd Bergmann			return ksys_semtimedop(first, ptr, second,
355dc0b152SArnd Bergmann			        (const struct __kernel_timespec __user *)fifth);
365dc0b152SArnd Bergmann		else if (IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
375dc0b152SArnd Bergmann			return compat_ksys_semtimedop(first, ptr, second,
389afc5eeeSArnd Bergmann			        (const struct old_timespec32 __user *)fifth);
395dc0b152SArnd Bergmann		else
405dc0b152SArnd Bergmann			return -ENOSYS;
41baed7fc9SChristoph Hellwig
42baed7fc9SChristoph Hellwig	case SEMGET:
4369894718SDominik Brodowski		return ksys_semget(first, second, third);
44baed7fc9SChristoph Hellwig	case SEMCTL: {
45e1fd1f49SAl Viro		unsigned long arg;
46baed7fc9SChristoph Hellwig		if (!ptr)
47baed7fc9SChristoph Hellwig			return -EINVAL;
48e1fd1f49SAl Viro		if (get_user(arg, (unsigned long __user *) ptr))
49baed7fc9SChristoph Hellwig			return -EFAULT;
50275f2214SArnd Bergmann		return ksys_old_semctl(first, second, third, arg);
51baed7fc9SChristoph Hellwig	}
52baed7fc9SChristoph Hellwig
53baed7fc9SChristoph Hellwig	case MSGSND:
5431c213f2SDominik Brodowski		return ksys_msgsnd(first, (struct msgbuf __user *) ptr,
55baed7fc9SChristoph Hellwig				  second, third);
56baed7fc9SChristoph Hellwig	case MSGRCV:
57baed7fc9SChristoph Hellwig		switch (version) {
58baed7fc9SChristoph Hellwig		case 0: {
59baed7fc9SChristoph Hellwig			struct ipc_kludge tmp;
60baed7fc9SChristoph Hellwig			if (!ptr)
61baed7fc9SChristoph Hellwig				return -EINVAL;
62baed7fc9SChristoph Hellwig
63baed7fc9SChristoph Hellwig			if (copy_from_user(&tmp,
64baed7fc9SChristoph Hellwig					   (struct ipc_kludge __user *) ptr,
65baed7fc9SChristoph Hellwig					   sizeof(tmp)))
66baed7fc9SChristoph Hellwig				return -EFAULT;
67078faac9SDominik Brodowski			return ksys_msgrcv(first, tmp.msgp, second,
68baed7fc9SChristoph Hellwig					   tmp.msgtyp, third);
69baed7fc9SChristoph Hellwig		}
70baed7fc9SChristoph Hellwig		default:
71078faac9SDominik Brodowski			return ksys_msgrcv(first,
72baed7fc9SChristoph Hellwig					   (struct msgbuf __user *) ptr,
73baed7fc9SChristoph Hellwig					   second, fifth, third);
74baed7fc9SChristoph Hellwig		}
75baed7fc9SChristoph Hellwig	case MSGGET:
763d65661aSDominik Brodowski		return ksys_msgget((key_t) first, second);
77baed7fc9SChristoph Hellwig	case MSGCTL:
78275f2214SArnd Bergmann		return ksys_old_msgctl(first, second,
79e340db56SDominik Brodowski				   (struct msqid_ds __user *)ptr);
80baed7fc9SChristoph Hellwig
81baed7fc9SChristoph Hellwig	case SHMAT:
82baed7fc9SChristoph Hellwig		switch (version) {
83baed7fc9SChristoph Hellwig		default: {
84baed7fc9SChristoph Hellwig			unsigned long raddr;
85baed7fc9SChristoph Hellwig			ret = do_shmat(first, (char __user *)ptr,
86079a96aeSWill Deacon				       second, &raddr, SHMLBA);
87baed7fc9SChristoph Hellwig			if (ret)
88baed7fc9SChristoph Hellwig				return ret;
89baed7fc9SChristoph Hellwig			return put_user(raddr, (unsigned long __user *) third);
90baed7fc9SChristoph Hellwig		}
91baed7fc9SChristoph Hellwig		case 1:
92baed7fc9SChristoph Hellwig			/*
93baed7fc9SChristoph Hellwig			 * This was the entry point for kernel-originating calls
94baed7fc9SChristoph Hellwig			 * from iBCS2 in 2.2 days.
95baed7fc9SChristoph Hellwig			 */
96baed7fc9SChristoph Hellwig			return -EINVAL;
97baed7fc9SChristoph Hellwig		}
98baed7fc9SChristoph Hellwig	case SHMDT:
99da1e2744SDominik Brodowski		return ksys_shmdt((char __user *)ptr);
100baed7fc9SChristoph Hellwig	case SHMGET:
10165749e0bSDominik Brodowski		return ksys_shmget(first, second, third);
102baed7fc9SChristoph Hellwig	case SHMCTL:
103275f2214SArnd Bergmann		return ksys_old_shmctl(first, second,
104baed7fc9SChristoph Hellwig				   (struct shmid_ds __user *) ptr);
105baed7fc9SChristoph Hellwig	default:
106baed7fc9SChristoph Hellwig		return -ENOSYS;
107baed7fc9SChristoph Hellwig	}
108baed7fc9SChristoph Hellwig}
10958fa4a41SArnd Bergmann
11058fa4a41SArnd BergmannSYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
11158fa4a41SArnd Bergmann		unsigned long, third, void __user *, ptr, long, fifth)
11258fa4a41SArnd Bergmann{
11358fa4a41SArnd Bergmann	return ksys_ipc(call, first, second, third, ptr, fifth);
11458fa4a41SArnd Bergmann}
115baed7fc9SChristoph Hellwig#endif
11620bc2a3aSAl Viro
11720bc2a3aSAl Viro#ifdef CONFIG_COMPAT
11820bc2a3aSAl Viro#include <linux/compat.h>
11920bc2a3aSAl Viro
12020bc2a3aSAl Viro#ifndef COMPAT_SHMLBA
12120bc2a3aSAl Viro#define COMPAT_SHMLBA	SHMLBA
12220bc2a3aSAl Viro#endif
12320bc2a3aSAl Viro
12420bc2a3aSAl Virostruct compat_ipc_kludge {
12520bc2a3aSAl Viro	compat_uptr_t msgp;
12620bc2a3aSAl Viro	compat_long_t msgtyp;
12720bc2a3aSAl Viro};
12820bc2a3aSAl Viro
13058fa4a41SArnd Bergmannint compat_ksys_ipc(u32 call, int first, int second,
13158fa4a41SArnd Bergmann	u32 third, compat_uptr_t ptr, u32 fifth)
13220bc2a3aSAl Viro{
13320bc2a3aSAl Viro	int version;
13420bc2a3aSAl Viro	u32 pad;
13520bc2a3aSAl Viro
13620bc2a3aSAl Viro	version = call >> 16; /* hack for backward compatibility */
13720bc2a3aSAl Viro	call &= 0xffff;
13820bc2a3aSAl Viro
13920bc2a3aSAl Viro	switch (call) {
14020bc2a3aSAl Viro	case SEMOP:
14120bc2a3aSAl Viro		/* struct sembuf is the same on 32 and 64bit :)) */
14241f4f0e2SDominik Brodowski		return ksys_semtimedop(first, compat_ptr(ptr), second, NULL);
14320bc2a3aSAl Viro	case SEMTIMEDOP:
1445dc0b152SArnd Bergmann		if (!IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
1455dc0b152SArnd Bergmann			return -ENOSYS;
14641f4f0e2SDominik Brodowski		return compat_ksys_semtimedop(first, compat_ptr(ptr), second,
14720bc2a3aSAl Viro						compat_ptr(fifth));
14820bc2a3aSAl Viro	case SEMGET:
14969894718SDominik Brodowski		return ksys_semget(first, second, third);
15020bc2a3aSAl Viro	case SEMCTL:
15120bc2a3aSAl Viro		if (!ptr)
15220bc2a3aSAl Viro			return -EINVAL;
15320bc2a3aSAl Viro		if (get_user(pad, (u32 __user *) compat_ptr(ptr)))
15420bc2a3aSAl Viro			return -EFAULT;
155275f2214SArnd Bergmann		return compat_ksys_old_semctl(first, second, third, pad);
15620bc2a3aSAl Viro
15720bc2a3aSAl Viro	case MSGSND:
15831c213f2SDominik Brodowski		return compat_ksys_msgsnd(first, ptr, second, third);
15920bc2a3aSAl Viro
16020bc2a3aSAl Viro	case MSGRCV: {
16120bc2a3aSAl Viro		void __user *uptr = compat_ptr(ptr);
16220bc2a3aSAl Viro
16320bc2a3aSAl Viro		if (first < 0 || second < 0)
16420bc2a3aSAl Viro			return -EINVAL;
16520bc2a3aSAl Viro
16620bc2a3aSAl Viro		if (!version) {
16720bc2a3aSAl Viro			struct compat_ipc_kludge ipck;
16820bc2a3aSAl Viro			if (!uptr)
16920bc2a3aSAl Viro				return -EINVAL;
17020bc2a3aSAl Viro			if (copy_from_user(&ipck, uptr, sizeof(ipck)))
17120bc2a3aSAl Viro				return -EFAULT;
172078faac9SDominik Brodowski			return compat_ksys_msgrcv(first, ipck.msgp, second,
17320bc2a3aSAl Viro						 ipck.msgtyp, third);
17420bc2a3aSAl Viro		}
175078faac9SDominik Brodowski		return compat_ksys_msgrcv(first, ptr, second, fifth, third);
17620bc2a3aSAl Viro	}
17720bc2a3aSAl Viro	case MSGGET:
1783d65661aSDominik Brodowski		return ksys_msgget(first, second);
17920bc2a3aSAl Viro	case MSGCTL:
180275f2214SArnd Bergmann		return compat_ksys_old_msgctl(first, second, compat_ptr(ptr));
18120bc2a3aSAl Viro
18220bc2a3aSAl Viro	case SHMAT: {
18320bc2a3aSAl Viro		int err;
18420bc2a3aSAl Viro		unsigned long raddr;
18520bc2a3aSAl Viro
18620bc2a3aSAl Viro		if (version == 1)
18720bc2a3aSAl Viro			return -EINVAL;
18820bc2a3aSAl Viro		err = do_shmat(first, compat_ptr(ptr), second, &raddr,
18920bc2a3aSAl Viro			       COMPAT_SHMLBA);
19020bc2a3aSAl Viro		if (err < 0)
19120bc2a3aSAl Viro			return err;
1926aa211e8SLinus Torvalds		return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third));
19320bc2a3aSAl Viro	}
19420bc2a3aSAl Viro	case SHMDT:
195da1e2744SDominik Brodowski		return ksys_shmdt(compat_ptr(ptr));
19620bc2a3aSAl Viro	case SHMGET:
19765749e0bSDominik Brodowski		return ksys_shmget(first, (unsigned int)second, third);
19820bc2a3aSAl Viro	case SHMCTL:
199275f2214SArnd Bergmann		return compat_ksys_old_shmctl(first, second, compat_ptr(ptr));
20020bc2a3aSAl Viro	}
20120bc2a3aSAl Viro
20220bc2a3aSAl Viro	return -ENOSYS;
20320bc2a3aSAl Viro}
20458fa4a41SArnd Bergmann
20558fa4a41SArnd BergmannCOMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second,
20658fa4a41SArnd Bergmann	u32, third, compat_uptr_t, ptr, u32, fifth)
20758fa4a41SArnd Bergmann{
20858fa4a41SArnd Bergmann	return compat_ksys_ipc(call, first, second, third, ptr, fifth);
20958fa4a41SArnd Bergmann}
21020bc2a3aSAl Viro#endif
21120bc2a3aSAl Viro#endif