sysfs.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. diff -rcNP og/Documentation/admin-guide/sysctl/fs.rst linux-5.4.116/Documentation/admin-guide/sysctl/fs.rst
  2. *** og/Documentation/admin-guide/sysctl/fs.rst 2021-05-02 12:05:04.000000000 +0300
  3. --- linux-5.4.116/Documentation/admin-guide/sysctl/fs.rst 2021-05-04 14:44:03.000000000 +0300
  4. ***************
  5. *** 48,53 ****
  6. --- 48,54 ----
  7. - suid_dumpable
  8. - super-max
  9. - super-nr
  10. + - sysfs_restrict
  11. aio-nr & aio-max-nr
  12. ***************
  13. *** 272,277 ****
  14. --- 273,303 ----
  15. This protection is based on the restrictions in Openwall and grsecurity.
  16. + sysfs_restrict
  17. + --------------
  18. +
  19. + This toggle controls the permissions of sysfs (the pseudo-filesystem
  20. + mounted at /sys).
  21. +
  22. + When sysfs_restrict is set to (0), there are no restrictions and
  23. + unprivileged users are permitted to access sysfs. When sysfs_restrict
  24. + is set to (1), sysfs and any filesystem normally mounted under
  25. + it (e.g. debugfs) will be accessible only by root.
  26. +
  27. + These filesystems generally provide access to hardware and debug information
  28. + that isn't appropriate for unprivileged users of the system. Sysfs and
  29. + debugfs have also become a large source of new vulnerabilities, ranging
  30. + from infoleaks to local compromise. There has been very little oversight with
  31. + an eye toward security involved in adding new exporters of information to these
  32. + filesystems, so their use is discouraged.
  33. +
  34. + This is disabled by default as many programs (e.g. Xorg or debugging tools)
  35. + require access to sysfs/debugfs.
  36. +
  37. + The kernel config option CONFIG_SECURITY_SYSFS_RESTRICT sets the default value
  38. + of sysfs_restrict.
  39. +
  40. +
  41. suid_dumpable:
  42. --------------
  43. diff -rcNP og/fs/debugfs/inode.c linux-5.4.116/fs/debugfs/inode.c
  44. *** og/fs/debugfs/inode.c 2021-05-02 12:05:04.000000000 +0300
  45. --- linux-5.4.116/fs/debugfs/inode.c 2021-05-04 14:47:28.000000000 +0300
  46. ***************
  47. *** 27,32 ****
  48. --- 27,33 ----
  49. #include <linux/magic.h>
  50. #include <linux/slab.h>
  51. #include <linux/security.h>
  52. + #include <linux/sysfs.h>
  53. #include "internal.h"
  54. ***************
  55. *** 562,568 ****
  56. return failed_creating(dentry);
  57. }
  58. ! inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  59. inode->i_op = &debugfs_dir_inode_operations;
  60. inode->i_fop = &simple_dir_operations;
  61. --- 563,572 ----
  62. return failed_creating(dentry);
  63. }
  64. ! inode->i_mode = S_IRWXU;
  65. ! if (!sysfs_restrict)
  66. ! inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  67. !
  68. inode->i_op = &debugfs_dir_inode_operations;
  69. inode->i_fop = &simple_dir_operations;
  70. ***************
  71. *** 913,916 ****
  72. return retval;
  73. }
  74. core_initcall(debugfs_init);
  75. -
  76. --- 917,919 ----
  77. diff -rcNP og/fs/sysfs/dir.c linux-5.4.116/fs/sysfs/dir.c
  78. *** og/fs/sysfs/dir.c 2021-05-02 12:05:04.000000000 +0300
  79. --- linux-5.4.116/fs/sysfs/dir.c 2021-05-04 14:45:18.000000000 +0300
  80. ***************
  81. *** 32,37 ****
  82. --- 32,39 ----
  83. kfree(buf);
  84. }
  85. + int sysfs_restrict = IS_ENABLED(CONFIG_SECURITY_SYSFS_RESTRICT);
  86. +
  87. /**
  88. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  89. * @kobj: object we're creating directory for
  90. ***************
  91. *** 40,45 ****
  92. --- 42,48 ----
  93. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  94. {
  95. struct kernfs_node *parent, *kn;
  96. + umode_t *mode = S_IRWXU;
  97. kuid_t uid;
  98. kgid_t gid;
  99. ***************
  100. *** 56,63 ****
  101. kobject_get_ownership(kobj, &uid, &gid);
  102. kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
  103. ! S_IRWXU | S_IRUGO | S_IXUGO, uid, gid,
  104. kobj, ns);
  105. if (IS_ERR(kn)) {
  106. if (PTR_ERR(kn) == -EEXIST)
  107. --- 59,69 ----
  108. kobject_get_ownership(kobj, &uid, &gid);
  109. + if (!sysfs_restrict)
  110. + mode = S_IRWXU | S_IRUGO | S_IXUGO;
  111. +
  112. kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
  113. ! mode, uid, gid,
  114. kobj, ns);
  115. if (IS_ERR(kn)) {
  116. if (PTR_ERR(kn) == -EEXIST)
  117. diff -rcNP og/include/linux/sysfs.h linux-5.4.116/include/linux/sysfs.h
  118. *** og/include/linux/sysfs.h 2021-05-02 12:05:04.000000000 +0300
  119. --- linux-5.4.116/include/linux/sysfs.h 2021-05-04 14:36:37.000000000 +0300
  120. ***************
  121. *** 305,310 ****
  122. --- 305,312 ----
  123. int __must_check sysfs_init(void);
  124. + extern int sysfs_restrict;
  125. +
  126. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  127. {
  128. return kernfs_enable_ns(kn);
  129. diff -rcNP og/kernel/sysctl.c linux-5.4.116/kernel/sysctl.c
  130. *** og/kernel/sysctl.c 2021-05-04 14:23:10.000000000 +0300
  131. --- linux-5.4.116/kernel/sysctl.c 2021-05-04 14:45:52.000000000 +0300
  132. ***************
  133. *** 69,74 ****
  134. --- 69,75 ----
  135. #include <linux/mount.h>
  136. #include <linux/userfaultfd_k.h>
  137. #include <linux/ipc.h>
  138. + #include <linux/sysfs.h>
  139. #include "../lib/kstrtox.h"
  140. ***************
  141. *** 1395,1401 ****
  142. .proc_handler = overcommit_kbytes_handler,
  143. },
  144. {
  145. ! .procname = "page-cluster",
  146. .data = &page_cluster,
  147. .maxlen = sizeof(int),
  148. .mode = 0644,
  149. --- 1396,1402 ----
  150. .proc_handler = overcommit_kbytes_handler,
  151. },
  152. {
  153. ! .procname = "page-cluster",
  154. .data = &page_cluster,
  155. .maxlen = sizeof(int),
  156. .mode = 0644,
  157. ***************
  158. *** 1913,1919 ****
  159. .mode = 0555,
  160. .child = inotify_table,
  161. },
  162. ! #endif
  163. #ifdef CONFIG_EPOLL
  164. {
  165. .procname = "epoll",
  166. --- 1914,1920 ----
  167. .mode = 0555,
  168. .child = inotify_table,
  169. },
  170. ! #endif
  171. #ifdef CONFIG_EPOLL
  172. {
  173. .procname = "epoll",
  174. ***************
  175. *** 1967,1972 ****
  176. --- 1968,1984 ----
  177. .extra1 = SYSCTL_ZERO,
  178. .extra2 = &two,
  179. },
  180. + #ifdef CONFIG_SYSFS
  181. + {
  182. + .procname = "sysfs_restrict",
  183. + .data = &sysfs_restrict,
  184. + .maxlen = sizeof(int),
  185. + .mode = 0600,
  186. + .proc_handler = proc_dointvec_minmax_sysadmin,
  187. + .extra1 = SYSCTL_ZERO,
  188. + .extra2 = SYSCTL_ONE,
  189. + },
  190. + #endif
  191. #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
  192. {
  193. .procname = "binfmt_misc",
  194. ***************
  195. *** 2394,2405 ****
  196. int *i, vleft, first = 1, err = 0;
  197. size_t left;
  198. char *kbuf = NULL, *p;
  199. !
  200. if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
  201. *lenp = 0;
  202. return 0;
  203. }
  204. !
  205. i = (int *) tbl_data;
  206. vleft = table->maxlen / sizeof(*i);
  207. left = *lenp;
  208. --- 2406,2417 ----
  209. int *i, vleft, first = 1, err = 0;
  210. size_t left;
  211. char *kbuf = NULL, *p;
  212. !
  213. if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
  214. *lenp = 0;
  215. return 0;
  216. }
  217. !
  218. i = (int *) tbl_data;
  219. vleft = table->maxlen / sizeof(*i);
  220. left = *lenp;
  221. ***************
  222. *** 2625,2631 ****
  223. * @ppos: file position
  224. *
  225. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  226. ! * values from/to the user buffer, treated as an ASCII string.
  227. *
  228. * Returns 0 on success.
  229. */
  230. --- 2637,2643 ----
  231. * @ppos: file position
  232. *
  233. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  234. ! * values from/to the user buffer, treated as an ASCII string.
  235. *
  236. * Returns 0 on success.
  237. */
  238. ***************
  239. *** 3138,3144 ****
  240. * @ppos: file position
  241. *
  242. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  243. ! * values from/to the user buffer, treated as an ASCII string.
  244. * The values read are assumed to be in seconds, and are converted into
  245. * jiffies.
  246. *
  247. --- 3150,3156 ----
  248. * @ppos: file position
  249. *
  250. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  251. ! * values from/to the user buffer, treated as an ASCII string.
  252. * The values read are assumed to be in seconds, and are converted into
  253. * jiffies.
  254. *
  255. ***************
  256. *** 3160,3167 ****
  257. * @ppos: pointer to the file position
  258. *
  259. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  260. ! * values from/to the user buffer, treated as an ASCII string.
  261. ! * The values read are assumed to be in 1/USER_HZ seconds, and
  262. * are converted into jiffies.
  263. *
  264. * Returns 0 on success.
  265. --- 3172,3179 ----
  266. * @ppos: pointer to the file position
  267. *
  268. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  269. ! * values from/to the user buffer, treated as an ASCII string.
  270. ! * The values read are assumed to be in 1/USER_HZ seconds, and
  271. * are converted into jiffies.
  272. *
  273. * Returns 0 on success.
  274. ***************
  275. *** 3183,3190 ****
  276. * @ppos: the current position in the file
  277. *
  278. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  279. ! * values from/to the user buffer, treated as an ASCII string.
  280. ! * The values read are assumed to be in 1/1000 seconds, and
  281. * are converted into jiffies.
  282. *
  283. * Returns 0 on success.
  284. --- 3195,3202 ----
  285. * @ppos: the current position in the file
  286. *
  287. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  288. ! * values from/to the user buffer, treated as an ASCII string.
  289. ! * The values read are assumed to be in 1/1000 seconds, and
  290. * are converted into jiffies.
  291. *
  292. * Returns 0 on success.
  293. diff -rcNP og/security/Kconfig linux-5.4.116/security/Kconfig
  294. *** og/security/Kconfig 2021-05-04 14:23:10.000000000 +0300
  295. --- linux-5.4.116/security/Kconfig 2021-05-04 14:43:04.000000000 +0300
  296. ***************
  297. *** 64,69 ****
  298. --- 64,92 ----
  299. If unsure say N.
  300. + config SECURITY_SYSFS_RESTRICT
  301. + bool "Sysfs/debugfs restriction"
  302. + default n
  303. + depends on SYSFS
  304. + help
  305. + If you say Y here, sysfs (the pseudo-filesystem mounted at /sys) and
  306. + any filesystem normally mounted under it (e.g. debugfs) will be
  307. + accessible only by root. These filesystems generally provide access
  308. + to hardware and debug information that isn't appropriate for unprivileged
  309. + users of the system. Sysfs and debugfs have also become a large source
  310. + of new vulnerabilities, ranging from infoleaks to local compromise.
  311. + There has been very little oversight with an eye toward security involved
  312. + in adding new exporters of information to these filesystems, so their
  313. + use is discouraged.
  314. +
  315. + This is disabled by default as many programs (e.g. Xorg or debugging tools)
  316. + require access to sysfs/debugfs.
  317. +
  318. + This setting can be overridden at runtime via the
  319. + fs.sysfs_restrict sysctl.
  320. +
  321. + If unsure say N.
  322. +
  323. config SECURITY
  324. bool "Enable different security models"
  325. depends on SYSFS