0003.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. diff -rcNP linux/Documentation/admin-guide/sysctl/kernel.rst ipc/Documentation/admin-guide/sysctl/kernel.rst
  2. *** linux/Documentation/admin-guide/sysctl/kernel.rst 2021-02-24 11:30:42.253000000 -0500
  3. --- ipc/Documentation/admin-guide/sysctl/kernel.rst 2021-02-24 15:56:41.302000000 -0500
  4. ***************
  5. *** 283,288 ****
  6. --- 283,311 ----
  7. default value of dmesg_restrict.
  8. + harden_ipc
  9. + ==========
  10. +
  11. + This toggle indicates whether access to overly-permissive IPC objects
  12. + is disallowed.
  13. +
  14. + If harden_ipc is set to (0), there are no restrictions. If harden_ipc
  15. + is set to (1), access to overly-permissive IPC objects (shared
  16. + memory, message queues, and semaphores) will be denied for processes
  17. + given the following criteria beyond normal permission checks:
  18. + 1) If the IPC object is world-accessible and the euid doesn't match
  19. + that of the creator or current uid for the IPC object
  20. + 2) If the IPC object is group-accessible and the egid doesn't
  21. + match that of the creator or current gid for the IPC object
  22. + It's a common error to grant too much permission to these objects,
  23. + with impact ranging from denial of service and information leaking to
  24. + privilege escalation. This feature was developed in response to
  25. + research by Tim Brown:
  26. + http://labs.portcullis.co.uk/whitepapers/memory-squatting-attacks-on-system-v-shared-memory/
  27. + who found hundreds of such insecure usages. Processes with
  28. + CAP_IPC_OWNER are still permitted to access these IPC objects.
  29. +
  30. +
  31. domainname & hostname:
  32. ======================
  33. diff -rcNP linux/include/linux/ipc.h ipc/include/linux/ipc.h
  34. *** linux/include/linux/ipc.h 2021-02-23 09:02:26.000000000 -0500
  35. --- ipc/include/linux/ipc.h 2021-02-24 15:57:28.815000000 -0500
  36. ***************
  37. *** 8,13 ****
  38. --- 8,15 ----
  39. #include <uapi/linux/ipc.h>
  40. #include <linux/refcount.h>
  41. + extern int harden_ipc;
  42. +
  43. /* used by in-kernel data structures */
  44. struct kern_ipc_perm {
  45. spinlock_t lock;
  46. diff -rcNP linux/ipc/harden_ipc.c ipc/ipc/harden_ipc.c
  47. *** linux/ipc/harden_ipc.c 1969-12-31 19:00:00.000000000 -0500
  48. --- ipc/ipc/harden_ipc.c 2021-02-24 15:59:45.029000000 -0500
  49. ***************
  50. *** 0 ****
  51. --- 1,46 ----
  52. + #include <linux/kernel.h>
  53. + #include <linux/mm.h>
  54. + #include <linux/sched.h>
  55. + #include <linux/file.h>
  56. + #include <linux/ipc.h>
  57. + #include <linux/ipc_namespace.h>
  58. + #include <linux/cred.h>
  59. +
  60. + int harden_ipc __read_mostly = IS_ENABLED(CONFIG_SECURITY_HARDEN_IPC);
  61. +
  62. + int
  63. + ipc_permitted(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, int requested_mode, int granted_mode)
  64. + {
  65. + int write;
  66. + int orig_granted_mode;
  67. + kuid_t euid;
  68. + kgid_t egid;
  69. +
  70. + if (!harden_ipc)
  71. + return 1;
  72. +
  73. + euid = current_euid();
  74. + egid = current_egid();
  75. +
  76. + write = requested_mode & 00002;
  77. + orig_granted_mode = ipcp->mode;
  78. +
  79. + if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid))
  80. + orig_granted_mode >>= 6;
  81. + else {
  82. + /* if likely wrong permissions, lock to user */
  83. + if (orig_granted_mode & 0007)
  84. + orig_granted_mode = 0;
  85. + /* otherwise do a egid-only check */
  86. + else if (gid_eq(egid, ipcp->cgid) || gid_eq(egid, ipcp->gid))
  87. + orig_granted_mode >>= 3;
  88. + /* otherwise, no access */
  89. + else
  90. + orig_granted_mode = 0;
  91. + }
  92. + if (!(requested_mode & ~granted_mode & 0007) && (requested_mode & ~orig_granted_mode & 0007) &&
  93. + !ns_capable_noaudit(ns->user_ns, CAP_IPC_OWNER)) {
  94. + return 0;
  95. + }
  96. + return 1;
  97. + }
  98. diff -rcNP linux/ipc/Makefile ipc/ipc/Makefile
  99. *** linux/ipc/Makefile 2021-02-23 09:02:26.000000000 -0500
  100. --- ipc/ipc/Makefile 2021-02-24 16:01:14.313000000 -0500
  101. ***************
  102. *** 4,12 ****
  103. #
  104. obj-$(CONFIG_SYSVIPC_COMPAT) += compat.o
  105. ! obj-$(CONFIG_SYSVIPC) += util.o msgutil.o msg.o sem.o shm.o syscall.o
  106. obj-$(CONFIG_SYSVIPC_SYSCTL) += ipc_sysctl.o
  107. obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o
  108. obj-$(CONFIG_IPC_NS) += namespace.o
  109. obj-$(CONFIG_POSIX_MQUEUE_SYSCTL) += mq_sysctl.o
  110. -
  111. --- 4,11 ----
  112. #
  113. obj-$(CONFIG_SYSVIPC_COMPAT) += compat.o
  114. ! obj-$(CONFIG_SYSVIPC) += util.o msgutil.o msg.o sem.o shm.o syscall.o harden_ipc.o
  115. obj-$(CONFIG_SYSVIPC_SYSCTL) += ipc_sysctl.o
  116. obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o
  117. obj-$(CONFIG_IPC_NS) += namespace.o
  118. obj-$(CONFIG_POSIX_MQUEUE_SYSCTL) += mq_sysctl.o
  119. diff -rcNP linux/ipc/util.c ipc/ipc/util.c
  120. *** linux/ipc/util.c 2021-02-23 09:02:26.000000000 -0500
  121. --- ipc/ipc/util.c 2021-02-24 16:02:24.916000000 -0500
  122. ***************
  123. *** 76,81 ****
  124. --- 76,83 ----
  125. int (*show)(struct seq_file *, void *);
  126. };
  127. + extern int ipc_permitted(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, int requested_mode, int granted_mode);
  128. +
  129. /**
  130. * ipc_init - initialise ipc subsystem
  131. *
  132. ***************
  133. *** 529,534 ****
  134. --- 531,540 ----
  135. granted_mode >>= 6;
  136. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  137. granted_mode >>= 3;
  138. +
  139. + if (!ipc_permitted(ns, ipcp, requested_mode, granted_mode))
  140. + return -1;
  141. +
  142. /* is there some bit set in requested_mode but not in granted_mode? */
  143. if ((requested_mode & ~granted_mode & 0007) &&
  144. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  145. diff -rcNP linux/kernel/sysctl.c ipc/kernel/sysctl.c
  146. *** linux/kernel/sysctl.c 2021-02-24 11:30:42.279000000 -0500
  147. --- ipc/kernel/sysctl.c 2021-02-24 16:03:28.137000000 -0500
  148. ***************
  149. *** 68,73 ****
  150. --- 68,74 ----
  151. #include <linux/bpf.h>
  152. #include <linux/mount.h>
  153. #include <linux/userfaultfd_k.h>
  154. + #include <linux/ipc.h>
  155. #include "../lib/kstrtox.h"
  156. ***************
  157. *** 935,940 ****
  158. --- 936,952 ----
  159. .extra1 = SYSCTL_ZERO,
  160. .extra2 = SYSCTL_ONE,
  161. },
  162. + #ifdef CONFIG_SYSVIPC
  163. + {
  164. + .procname = "harden_ipc",
  165. + .data = &harden_ipc,
  166. + .maxlen = sizeof(int),
  167. + .mode = 0644,
  168. + .proc_handler = &proc_dointvec_minmax_sysadmin,
  169. + .extra1 = SYSCTL_ZERO,
  170. + .extra2 = SYSCTL_ONE,
  171. + },
  172. + #endif
  173. {
  174. .procname = "ngroups_max",
  175. .data = &ngroups_max,
  176. diff -rcNP linux/security/Kconfig ipc/security/Kconfig
  177. *** linux/security/Kconfig 2021-02-24 11:30:42.289000000 -0500
  178. --- ipc/security/Kconfig 2021-02-24 16:05:10.104000000 -0500
  179. ***************
  180. *** 61,66 ****
  181. --- 61,91 ----
  182. bool
  183. default n
  184. + config SECURITY_HARDEN_IPC
  185. + bool "Disallow access to overly-permissive IPC objects"
  186. + default y
  187. + depends on SYSVIPC
  188. + help
  189. + If you say Y here, access to overly-permissive IPC objects (shared
  190. + memory, message queues, and semaphores) will be denied for processes
  191. + given the following criteria beyond normal permission checks:
  192. + 1) If the IPC object is world-accessible and the euid doesn't match
  193. + that of the creator or current uid for the IPC object
  194. + 2) If the IPC object is group-accessible and the egid doesn't
  195. + match that of the creator or current gid for the IPC object
  196. + It's a common error to grant too much permission to these objects,
  197. + with impact ranging from denial of service and information leaking to
  198. + privilege escalation. This feature was developed in response to
  199. + research by Tim Brown:
  200. + http://labs.portcullis.co.uk/whitepapers/memory-squatting-attacks-on-system-v-shared-memory/
  201. + who found hundreds of such insecure usages. Processes with
  202. + CAP_IPC_OWNER are still permitted to access these IPC objects.
  203. +
  204. + This setting can be overridden at runtime via the kernel.harden_ipc
  205. + sysctl.
  206. +
  207. + If unsure, say Y.
  208. +
  209. config SECURITYFS
  210. bool "Enable the securityfs filesystem"
  211. help