status.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mmap based event notifications for SELinux
  4. *
  5. * Author: KaiGai Kohei <kaigai@ak.jp.nec.com>
  6. *
  7. * Copyright (C) 2010 NEC corporation
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/gfp.h>
  11. #include <linux/mm.h>
  12. #include <linux/mutex.h>
  13. #include "avc.h"
  14. #include "security.h"
  15. /*
  16. * The selinux_status_page shall be exposed to userspace applications
  17. * using mmap interface on /selinux/status.
  18. * It enables to notify applications a few events that will cause reset
  19. * of userspace access vector without context switching.
  20. *
  21. * The selinux_kernel_status structure on the head of status page is
  22. * protected from concurrent accesses using seqlock logic, so userspace
  23. * application should reference the status page according to the seqlock
  24. * logic.
  25. *
  26. * Typically, application checks status->sequence at the head of access
  27. * control routine. If it is odd-number, kernel is updating the status,
  28. * so please wait for a moment. If it is changed from the last sequence
  29. * number, it means something happen, so application will reset userspace
  30. * avc, if needed.
  31. * In most cases, application shall confirm the kernel status is not
  32. * changed without any system call invocations.
  33. */
  34. /*
  35. * selinux_kernel_status_page
  36. *
  37. * It returns a reference to selinux_status_page. If the status page is
  38. * not allocated yet, it also tries to allocate it at the first time.
  39. */
  40. struct page *selinux_kernel_status_page(void)
  41. {
  42. struct selinux_kernel_status *status;
  43. struct page *result = NULL;
  44. mutex_lock(&selinux_state.status_lock);
  45. if (!selinux_state.status_page) {
  46. selinux_state.status_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  47. if (selinux_state.status_page) {
  48. status = page_address(selinux_state.status_page);
  49. status->version = SELINUX_KERNEL_STATUS_VERSION;
  50. status->sequence = 0;
  51. status->enforcing = enforcing_enabled();
  52. /*
  53. * NOTE: the next policyload event shall set
  54. * a positive value on the status->policyload,
  55. * although it may not be 1, but never zero.
  56. * So, application can know it was updated.
  57. */
  58. status->policyload = 0;
  59. status->deny_unknown =
  60. !security_get_allow_unknown();
  61. }
  62. }
  63. result = selinux_state.status_page;
  64. mutex_unlock(&selinux_state.status_lock);
  65. return result;
  66. }
  67. /*
  68. * selinux_status_update_setenforce
  69. *
  70. * It updates status of the current enforcing/permissive mode.
  71. */
  72. void selinux_status_update_setenforce(bool enforcing)
  73. {
  74. struct selinux_kernel_status *status;
  75. mutex_lock(&selinux_state.status_lock);
  76. if (selinux_state.status_page) {
  77. status = page_address(selinux_state.status_page);
  78. status->sequence++;
  79. smp_wmb();
  80. status->enforcing = enforcing ? 1 : 0;
  81. smp_wmb();
  82. status->sequence++;
  83. }
  84. mutex_unlock(&selinux_state.status_lock);
  85. }
  86. /*
  87. * selinux_status_update_policyload
  88. *
  89. * It updates status of the times of policy reloaded, and current
  90. * setting of deny_unknown.
  91. */
  92. void selinux_status_update_policyload(u32 seqno)
  93. {
  94. struct selinux_kernel_status *status;
  95. mutex_lock(&selinux_state.status_lock);
  96. if (selinux_state.status_page) {
  97. status = page_address(selinux_state.status_page);
  98. status->sequence++;
  99. smp_wmb();
  100. status->policyload = seqno;
  101. status->deny_unknown = !security_get_allow_unknown();
  102. smp_wmb();
  103. status->sequence++;
  104. }
  105. mutex_unlock(&selinux_state.status_lock);
  106. }