0005.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. diff -rcNP void-hardened-kernel 2/Documentation/admin-guide/kernel-parameters.txt void-hardened-kernel/Documentation/admin-guide/kernel-parameters.txt
  2. *** void-hardened-kernel 2/Documentation/admin-guide/kernel-parameters.txt 2021-04-08 00:01:58.000000000 +0300
  3. --- void-hardened-kernel/Documentation/admin-guide/kernel-parameters.txt 2021-04-11 22:39:31.000000000 +0300
  4. ***************
  5. *** 2689,2694 ****
  6. --- 2689,2698 ----
  7. log everything. Information is printed at KERN_DEBUG
  8. so loglevel=8 may also need to be specified.
  9. + modharden= [SECURITY]
  10. + on - Restrict module auto-loading to CAP_SYS_MODULE
  11. + off - Don't restrict module auto-loading
  12. +
  13. module.sig_enforce
  14. [KNL] When CONFIG_MODULE_SIG is set, this means that
  15. modules without (valid) signatures will fail to load.
  16. diff -rcNP void-hardened-kernel 2/Documentation/admin-guide/sysctl/kernel.rst void-hardened-kernel/Documentation/admin-guide/sysctl/kernel.rst
  17. *** void-hardened-kernel 2/Documentation/admin-guide/sysctl/kernel.rst 2021-04-08 00:01:58.000000000 +0300
  18. --- void-hardened-kernel/Documentation/admin-guide/sysctl/kernel.rst 2021-04-11 22:39:31.000000000 +0300
  19. ***************
  20. *** 469,474 ****
  21. --- 469,488 ----
  22. 0, the cache is disabled. Enabled if nonzero.
  23. + modharden:
  24. + ==========
  25. +
  26. + This toggle indicates whether unprivileged users are allowed to
  27. + auto-load kernel modules.
  28. +
  29. + When modharden is set to (0) there are no restrictions. When
  30. + modharden is set to (1), only users with ``CAP_SYS_MODULE`` are
  31. + permitted to load kernel modules
  32. +
  33. + The kernel config option ``CONFIG_SECURITY_MODHARDEN`` sets the
  34. + default value of modharden.
  35. +
  36. +
  37. modules_disabled:
  38. =================
  39. diff -rcNP void-hardened-kernel 2/include/linux/kmod.h void-hardened-kernel/include/linux/kmod.h
  40. *** void-hardened-kernel 2/include/linux/kmod.h 2021-04-08 00:01:58.000000000 +0300
  41. --- void-hardened-kernel/include/linux/kmod.h 2021-04-11 22:39:31.000000000 +0300
  42. ***************
  43. *** 22,27 ****
  44. --- 22,28 ----
  45. * usually useless though. */
  46. extern __printf(2, 3)
  47. int __request_module(bool wait, const char *name, ...);
  48. + extern int modharden;
  49. #define request_module(mod...) __request_module(true, mod)
  50. #define request_module_nowait(mod...) __request_module(false, mod)
  51. #define try_then_request_module(x, mod...) \
  52. diff -rcNP void-hardened-kernel 2/kernel/kmod.c void-hardened-kernel/kernel/kmod.c
  53. *** void-hardened-kernel 2/kernel/kmod.c 2021-04-08 00:01:58.000000000 +0300
  54. --- void-hardened-kernel/kernel/kmod.c 2021-04-11 22:39:31.000000000 +0300
  55. ***************
  56. *** 106,111 ****
  57. --- 106,128 ----
  58. return -ENOMEM;
  59. }
  60. + int modharden __read_mostly = IS_ENABLED(CONFIG_SECURITY_MODHARDEN);
  61. +
  62. + static int __init enable_modharden(char *level)
  63. + {
  64. + if (!level)
  65. + return -EINVAL;
  66. +
  67. + if (strcmp(level, "on") == 0)
  68. + modharden = 1;
  69. + else if (strcmp(level, "off") == 0)
  70. + modharden = 0;
  71. + else
  72. + return -EINVAL;
  73. +
  74. + return 0;
  75. + }
  76. + early_param("modharden", enable_modharden);
  77. /**
  78. * __request_module - try to load a kernel module
  79. * @wait: wait (or not) for the operation to complete
  80. ***************
  81. *** 149,154 ****
  82. --- 166,176 ----
  83. if (ret)
  84. return ret;
  85. + if (modharden && !capable(CAP_SYS_MODULE)) {
  86. + printk(KERN_ALERT "denied attempt to auto-load module %s\n", module_name);
  87. + return -EPERM;
  88. + }
  89. +
  90. if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
  91. pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
  92. atomic_read(&kmod_concurrent_max),
  93. diff -rcNP void-hardened-kernel 2/kernel/sysctl.c void-hardened-kernel/kernel/sysctl.c
  94. *** void-hardened-kernel 2/kernel/sysctl.c 2021-04-08 00:01:58.000000000 +0300
  95. --- void-hardened-kernel/kernel/sysctl.c 2021-04-11 22:39:31.000000000 +0300
  96. ***************
  97. *** 730,735 ****
  98. --- 730,744 ----
  99. .extra1 = SYSCTL_ONE,
  100. .extra2 = SYSCTL_ONE,
  101. },
  102. + {
  103. + .procname = "modharden",
  104. + .data = &modharden,
  105. + .maxlen = sizeof(int),
  106. + .mode = 0644,
  107. + .proc_handler = proc_dointvec_minmax,
  108. + .extra1 = SYSCTL_ZERO,
  109. + .extra2 = SYSCTL_ONE,
  110. + },
  111. #endif
  112. #ifdef CONFIG_UEVENT_HELPER
  113. {
  114. diff -rcNP void-hardened-kernel 2/security/Kconfig void-hardened-kernel/security/Kconfig
  115. *** void-hardened-kernel 2/security/Kconfig 2021-04-08 00:01:58.000000000 +0300
  116. --- void-hardened-kernel/security/Kconfig 2021-04-11 22:43:45.000000000 +0300
  117. ***************
  118. *** 42,47 ****
  119. --- 42,69 ----
  120. If you are unsure how to answer this question, answer N.
  121. + config SECURITY_MODHARDEN
  122. + bool "Harden module auto-loading"
  123. + default n
  124. + depends on MODULES
  125. + help
  126. + If you say Y here, module auto-loading in response to use of some
  127. + feature implemented by an unloaded module will be restricted to
  128. + CAP_SYS_MODULE. Enabling this option helps defend against attacks
  129. + by unprivileged users who abuse the auto-loading behavior to
  130. + cause a vulnerable module to load that is then exploited.
  131. +
  132. + If this option prevents a legitimate use of auto-loading for a
  133. + non-root user, the administrator can execute modprobe manually
  134. + with the exact name of the module mentioned in the alert log.
  135. + Alternatively, the administrator can add the module to the list
  136. + of modules loaded at boot by modifying init scripts.
  137. +
  138. + This setting can be overridden at runtime via the
  139. + kernel.modharden sysctl.
  140. +
  141. + If unsure say N.
  142. +
  143. config SECURITY
  144. bool "Enable different security models"
  145. depends on SYSFS
  146. ***************
  147. *** 353,356 ****
  148. source "security/Kconfig.hardening"
  149. endmenu
  150. -
  151. --- 375,377 ----