status.c 3.5 KB

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