status.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = NULL;
  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. {
  52. selinux_status_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  53. if (selinux_status_page)
  54. {
  55. status = page_address(selinux_status_page);
  56. status->version = SELINUX_KERNEL_STATUS_VERSION;
  57. status->sequence = 0;
  58. status->enforcing = selinux_enforcing;
  59. /*
  60. * NOTE: the next policyload event shall set
  61. * a positive value on the status->policyload,
  62. * although it may not be 1, but never zero.
  63. * So, application can know it was updated.
  64. */
  65. status->policyload = 0;
  66. status->deny_unknown = !security_get_allow_unknown();
  67. }
  68. }
  69. result = selinux_status_page;
  70. mutex_unlock(&selinux_status_lock);
  71. return result;
  72. }
  73. /*
  74. * selinux_status_update_setenforce
  75. *
  76. * It updates status of the current enforcing/permissive mode.
  77. */
  78. void selinux_status_update_setenforce(int enforcing)
  79. {
  80. struct selinux_kernel_status *status;
  81. mutex_lock(&selinux_status_lock);
  82. if (selinux_status_page)
  83. {
  84. status = page_address(selinux_status_page);
  85. status->sequence++;
  86. smp_wmb();
  87. status->enforcing = enforcing;
  88. smp_wmb();
  89. status->sequence++;
  90. }
  91. mutex_unlock(&selinux_status_lock);
  92. }
  93. /*
  94. * selinux_status_update_policyload
  95. *
  96. * It updates status of the times of policy reloaded, and current
  97. * setting of deny_unknown.
  98. */
  99. void selinux_status_update_policyload(int seqno)
  100. {
  101. struct selinux_kernel_status *status;
  102. mutex_lock(&selinux_status_lock);
  103. if (selinux_status_page)
  104. {
  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. }