syscall.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0
  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>
  9. #include <linux/syscalls.h>
  10. #include <linux/security.h>
  11. #include <linux/ipc_namespace.h>
  12. #include "util.h"
  13. #ifdef __ARCH_WANT_SYS_IPC
  14. #include <linux/errno.h>
  15. #include <linux/ipc.h>
  16. #include <linux/shm.h>
  17. #include <linux/uaccess.h>
  18. SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
  19. unsigned long, third, void __user *, ptr, long, fifth)
  20. {
  21. int version, ret;
  22. version = call >> 16; /* hack for backward compatibility */
  23. call &= 0xffff;
  24. switch (call) {
  25. case SEMOP:
  26. return ksys_semtimedop(first, (struct sembuf __user *)ptr,
  27. second, NULL);
  28. case SEMTIMEDOP:
  29. return ksys_semtimedop(first, (struct sembuf __user *)ptr,
  30. second,
  31. (const struct timespec __user *)fifth);
  32. case SEMGET:
  33. return ksys_semget(first, second, third);
  34. case SEMCTL: {
  35. unsigned long arg;
  36. if (!ptr)
  37. return -EINVAL;
  38. if (get_user(arg, (unsigned long __user *) ptr))
  39. return -EFAULT;
  40. return ksys_semctl(first, second, third, arg);
  41. }
  42. case MSGSND:
  43. return ksys_msgsnd(first, (struct msgbuf __user *) ptr,
  44. second, third);
  45. case MSGRCV:
  46. switch (version) {
  47. case 0: {
  48. struct ipc_kludge tmp;
  49. if (!ptr)
  50. return -EINVAL;
  51. if (copy_from_user(&tmp,
  52. (struct ipc_kludge __user *) ptr,
  53. sizeof(tmp)))
  54. return -EFAULT;
  55. return ksys_msgrcv(first, tmp.msgp, second,
  56. tmp.msgtyp, third);
  57. }
  58. default:
  59. return ksys_msgrcv(first,
  60. (struct msgbuf __user *) ptr,
  61. second, fifth, third);
  62. }
  63. case MSGGET:
  64. return ksys_msgget((key_t) first, second);
  65. case MSGCTL:
  66. return ksys_msgctl(first, second,
  67. (struct msqid_ds __user *)ptr);
  68. case SHMAT:
  69. switch (version) {
  70. default: {
  71. unsigned long raddr;
  72. ret = do_shmat(first, (char __user *)ptr,
  73. second, &raddr, SHMLBA);
  74. if (ret)
  75. return ret;
  76. return put_user(raddr, (unsigned long __user *) third);
  77. }
  78. case 1:
  79. /*
  80. * This was the entry point for kernel-originating calls
  81. * from iBCS2 in 2.2 days.
  82. */
  83. return -EINVAL;
  84. }
  85. case SHMDT:
  86. return ksys_shmdt((char __user *)ptr);
  87. case SHMGET:
  88. return ksys_shmget(first, second, third);
  89. case SHMCTL:
  90. return ksys_shmctl(first, second,
  91. (struct shmid_ds __user *) ptr);
  92. default:
  93. return -ENOSYS;
  94. }
  95. }
  96. #endif
  97. #ifdef CONFIG_COMPAT
  98. #include <linux/compat.h>
  99. #ifndef COMPAT_SHMLBA
  100. #define COMPAT_SHMLBA SHMLBA
  101. #endif
  102. struct compat_ipc_kludge {
  103. compat_uptr_t msgp;
  104. compat_long_t msgtyp;
  105. };
  106. #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
  107. COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second,
  108. u32, third, compat_uptr_t, ptr, u32, fifth)
  109. {
  110. int version;
  111. u32 pad;
  112. version = call >> 16; /* hack for backward compatibility */
  113. call &= 0xffff;
  114. switch (call) {
  115. case SEMOP:
  116. /* struct sembuf is the same on 32 and 64bit :)) */
  117. return ksys_semtimedop(first, compat_ptr(ptr), second, NULL);
  118. case SEMTIMEDOP:
  119. return compat_ksys_semtimedop(first, compat_ptr(ptr), second,
  120. compat_ptr(fifth));
  121. case SEMGET:
  122. return ksys_semget(first, second, third);
  123. case SEMCTL:
  124. if (!ptr)
  125. return -EINVAL;
  126. if (get_user(pad, (u32 __user *) compat_ptr(ptr)))
  127. return -EFAULT;
  128. return compat_ksys_semctl(first, second, third, pad);
  129. case MSGSND:
  130. return compat_ksys_msgsnd(first, ptr, second, third);
  131. case MSGRCV: {
  132. void __user *uptr = compat_ptr(ptr);
  133. if (first < 0 || second < 0)
  134. return -EINVAL;
  135. if (!version) {
  136. struct compat_ipc_kludge ipck;
  137. if (!uptr)
  138. return -EINVAL;
  139. if (copy_from_user(&ipck, uptr, sizeof(ipck)))
  140. return -EFAULT;
  141. return compat_ksys_msgrcv(first, ipck.msgp, second,
  142. ipck.msgtyp, third);
  143. }
  144. return compat_ksys_msgrcv(first, ptr, second, fifth, third);
  145. }
  146. case MSGGET:
  147. return ksys_msgget(first, second);
  148. case MSGCTL:
  149. return compat_ksys_msgctl(first, second, compat_ptr(ptr));
  150. case SHMAT: {
  151. int err;
  152. unsigned long raddr;
  153. if (version == 1)
  154. return -EINVAL;
  155. err = do_shmat(first, compat_ptr(ptr), second, &raddr,
  156. COMPAT_SHMLBA);
  157. if (err < 0)
  158. return err;
  159. return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third));
  160. }
  161. case SHMDT:
  162. return ksys_shmdt(compat_ptr(ptr));
  163. case SHMGET:
  164. return ksys_shmget(first, (unsigned int)second, third);
  165. case SHMCTL:
  166. return compat_ksys_shmctl(first, second, compat_ptr(ptr));
  167. }
  168. return -ENOSYS;
  169. }
  170. #endif
  171. #endif