status.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mmap based event notifications for SELinux
  4. *
  5. * Author: KaiGai Kohei <[email protected]>
  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(struct selinux_state *state)
  41. {
  42. struct selinux_kernel_status *status;
  43. struct page *result = NULL;
  44. mutex_lock(&state->status_lock);
  45. if (!state->status_page) {
  46. state->status_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  47. if (state->status_page) {
  48. status = page_address(state->status_page);
  49. status->version = SELINUX_KERNEL_STATUS_VERSION;
  50. status->sequence = 0;
  51. status->enforcing = enforcing_enabled(state);
  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(state);
  61. }
  62. }
  63. result = state->status_page;
  64. mutex_unlock(&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(struct selinux_state *state,
  73. int enforcing)
  74. {
  75. struct selinux_kernel_status *status;
  76. mutex_lock(&state->status_lock);
  77. if (state->status_page) {
  78. status = page_address(state->status_page);
  79. status->sequence++;
  80. smp_wmb();
  81. status->enforcing = enforcing;
  82. smp_wmb();
  83. status->sequence++;
  84. }
  85. mutex_unlock(&state->status_lock);
  86. }
  87. /*
  88. * selinux_status_update_policyload
  89. *
  90. * It updates status of the times of policy reloaded, and current
  91. * setting of deny_unknown.
  92. */
  93. void selinux_status_update_policyload(struct selinux_state *state,
  94. int seqno)
  95. {
  96. struct selinux_kernel_status *status;
  97. mutex_lock(&state->status_lock);
  98. if (state->status_page) {
  99. status = page_address(state->status_page);
  100. status->sequence++;
  101. smp_wmb();
  102. status->policyload = seqno;
  103. status->deny_unknown = !security_get_allow_unknown(state);
  104. smp_wmb();
  105. status->sequence++;
  106. }
  107. mutex_unlock(&state->status_lock);
  108. }