status.c 3.4 KB

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