0001-ZEN-Add-sysctl-and-CONFIG-to-disallow-unprivileged-C.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. From 164d2dd36ff81a5a4b4d6440317438cf6009cd59 Mon Sep 17 00:00:00 2001
  2. From: "Jan Alexander Steffens (heftig)" <jan.steffens@gmail.com>
  3. Date: Mon, 16 Sep 2019 04:53:20 +0200
  4. Subject: [PATCH 1/4] ZEN: Add sysctl and CONFIG to disallow unprivileged
  5. CLONE_NEWUSER
  6. Our default behavior continues to match the vanilla kernel.
  7. ---
  8. init/Kconfig | 16 ++++++++++++++++
  9. kernel/fork.c | 15 +++++++++++++++
  10. kernel/sysctl.c | 12 ++++++++++++
  11. kernel/user_namespace.c | 7 +++++++
  12. 4 files changed, 50 insertions(+)
  13. diff --git a/init/Kconfig b/init/Kconfig
  14. index 74a5ac65644f..965a628556e8 100644
  15. --- a/init/Kconfig
  16. +++ b/init/Kconfig
  17. @@ -1102,6 +1102,22 @@ config USER_NS
  18. If unsure, say N.
  19. +config USER_NS_UNPRIVILEGED
  20. + bool "Allow unprivileged users to create namespaces"
  21. + default y
  22. + depends on USER_NS
  23. + help
  24. + When disabled, unprivileged users will not be able to create
  25. + new namespaces. Allowing users to create their own namespaces
  26. + has been part of several recent local privilege escalation
  27. + exploits, so if you need user namespaces but are
  28. + paranoid^Wsecurity-conscious you want to disable this.
  29. +
  30. + This setting can be overridden at runtime via the
  31. + kernel.unprivileged_userns_clone sysctl.
  32. +
  33. + If unsure, say Y.
  34. +
  35. config PID_NS
  36. bool "PID Namespaces"
  37. default y
  38. diff --git a/kernel/fork.c b/kernel/fork.c
  39. index 48ed22774efa..ec61454a18d5 100644
  40. --- a/kernel/fork.c
  41. +++ b/kernel/fork.c
  42. @@ -106,6 +106,11 @@
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/task.h>
  45. +#ifdef CONFIG_USER_NS
  46. +extern int unprivileged_userns_clone;
  47. +#else
  48. +#define unprivileged_userns_clone 0
  49. +#endif
  50. /*
  51. * Minimum number of threads to boot the kernel
  52. @@ -1848,6 +1853,10 @@ static __latent_entropy struct task_struct *copy_process(
  53. if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
  54. return ERR_PTR(-EINVAL);
  55. + if ((clone_flags & CLONE_NEWUSER) && !unprivileged_userns_clone)
  56. + if (!capable(CAP_SYS_ADMIN))
  57. + return ERR_PTR(-EPERM);
  58. +
  59. /*
  60. * Thread groups must share signals as well, and detached threads
  61. * can only be started up within the thread group.
  62. @@ -2948,6 +2957,12 @@ int ksys_unshare(unsigned long unshare_flags)
  63. if (unshare_flags & CLONE_NEWNS)
  64. unshare_flags |= CLONE_FS;
  65. + if ((unshare_flags & CLONE_NEWUSER) && !unprivileged_userns_clone) {
  66. + err = -EPERM;
  67. + if (!capable(CAP_SYS_ADMIN))
  68. + goto bad_unshare_out;
  69. + }
  70. +
  71. err = check_unshare_flags(unshare_flags);
  72. if (err)
  73. goto bad_unshare_out;
  74. diff --git a/kernel/sysctl.c b/kernel/sysctl.c
  75. index 8a176d8727a3..9500597739a2 100644
  76. --- a/kernel/sysctl.c
  77. +++ b/kernel/sysctl.c
  78. @@ -110,6 +110,9 @@ extern int core_uses_pid;
  79. extern char core_pattern[];
  80. extern unsigned int core_pipe_limit;
  81. #endif
  82. +#ifdef CONFIG_USER_NS
  83. +extern int unprivileged_userns_clone;
  84. +#endif
  85. extern int pid_max;
  86. extern int pid_max_min, pid_max_max;
  87. extern int percpu_pagelist_fraction;
  88. @@ -534,6 +537,15 @@ static struct ctl_table kern_table[] = {
  89. .proc_handler = proc_dointvec,
  90. },
  91. #endif
  92. +#ifdef CONFIG_USER_NS
  93. + {
  94. + .procname = "unprivileged_userns_clone",
  95. + .data = &unprivileged_userns_clone,
  96. + .maxlen = sizeof(int),
  97. + .mode = 0644,
  98. + .proc_handler = proc_dointvec,
  99. + },
  100. +#endif
  101. #ifdef CONFIG_PROC_SYSCTL
  102. {
  103. .procname = "tainted",
  104. diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
  105. index 8eadadc478f9..c36ecd19562c 100644
  106. --- a/kernel/user_namespace.c
  107. +++ b/kernel/user_namespace.c
  108. @@ -21,6 +21,13 @@
  109. #include <linux/bsearch.h>
  110. #include <linux/sort.h>
  111. +/* sysctl */
  112. +#ifdef CONFIG_USER_NS_UNPRIVILEGED
  113. +int unprivileged_userns_clone = 1;
  114. +#else
  115. +int unprivileged_userns_clone;
  116. +#endif
  117. +
  118. static struct kmem_cache *user_ns_cachep __read_mostly;
  119. static DEFINE_MUTEX(userns_state_mutex);
  120. --
  121. 2.27.0