min_addr.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/mm.h>
  4. #include <linux/security.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/minmax.h>
  7. /* amount of vm to protect from userspace access by both DAC and the LSM*/
  8. unsigned long mmap_min_addr;
  9. /* amount of vm to protect from userspace using CAP_SYS_RAWIO (DAC) */
  10. unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  11. /* amount of vm to protect from userspace using the LSM = CONFIG_LSM_MMAP_MIN_ADDR */
  12. /*
  13. * Update mmap_min_addr = max(dac_mmap_min_addr, CONFIG_LSM_MMAP_MIN_ADDR)
  14. */
  15. static void update_mmap_min_addr(void)
  16. {
  17. #ifdef CONFIG_LSM_MMAP_MIN_ADDR
  18. mmap_min_addr = umax(dac_mmap_min_addr, CONFIG_LSM_MMAP_MIN_ADDR);
  19. #else
  20. mmap_min_addr = dac_mmap_min_addr;
  21. #endif
  22. }
  23. /*
  24. * sysctl handler which just sets dac_mmap_min_addr = the new value and then
  25. * calls update_mmap_min_addr() so non MAP_FIXED hints get rounded properly
  26. */
  27. int mmap_min_addr_handler(const struct ctl_table *table, int write,
  28. void *buffer, size_t *lenp, loff_t *ppos)
  29. {
  30. int ret;
  31. if (write && !capable(CAP_SYS_RAWIO))
  32. return -EPERM;
  33. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  34. update_mmap_min_addr();
  35. return ret;
  36. }
  37. static const struct ctl_table min_addr_sysctl_table[] = {
  38. {
  39. .procname = "mmap_min_addr",
  40. .data = &dac_mmap_min_addr,
  41. .maxlen = sizeof(unsigned long),
  42. .mode = 0644,
  43. .proc_handler = mmap_min_addr_handler,
  44. },
  45. };
  46. static int __init mmap_min_addr_init(void)
  47. {
  48. register_sysctl_init("vm", min_addr_sysctl_table);
  49. update_mmap_min_addr();
  50. return 0;
  51. }
  52. pure_initcall(mmap_min_addr_init);