status.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /*
  38. * selinux_kernel_status_page
  39. *
  40. * It returns a reference to selinux_status_page. If the status page is
  41. * not allocated yet, it also tries to allocate it at the first time.
  42. */
  43. struct page *selinux_kernel_status_page(struct selinux_state *state)
  44. {
  45. struct selinux_kernel_status *status;
  46. struct page *result = NULL;
  47. mutex_lock(&state->ss->status_lock);
  48. if (!state->ss->status_page) {
  49. state->ss->status_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  50. if (state->ss->status_page) {
  51. status = page_address(state->ss->status_page);
  52. status->version = SELINUX_KERNEL_STATUS_VERSION;
  53. status->sequence = 0;
  54. status->enforcing = enforcing_enabled(state);
  55. /*
  56. * NOTE: the next policyload event shall set
  57. * a positive value on the status->policyload,
  58. * although it may not be 1, but never zero.
  59. * So, application can know it was updated.
  60. */
  61. status->policyload = 0;
  62. status->deny_unknown =
  63. !security_get_allow_unknown(state);
  64. }
  65. }
  66. result = state->ss->status_page;
  67. mutex_unlock(&state->ss->status_lock);
  68. return result;
  69. }
  70. /*
  71. * selinux_status_update_setenforce
  72. *
  73. * It updates status of the current enforcing/permissive mode.
  74. */
  75. void selinux_status_update_setenforce(struct selinux_state *state,
  76. int enforcing)
  77. {
  78. struct selinux_kernel_status *status;
  79. mutex_lock(&state->ss->status_lock);
  80. if (state->ss->status_page) {
  81. status = page_address(state->ss->status_page);
  82. status->sequence++;
  83. smp_wmb();
  84. status->enforcing = enforcing;
  85. smp_wmb();
  86. status->sequence++;
  87. }
  88. mutex_unlock(&state->ss->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(struct selinux_state *state,
  97. int seqno)
  98. {
  99. struct selinux_kernel_status *status;
  100. mutex_lock(&state->ss->status_lock);
  101. if (state->ss->status_page) {
  102. status = page_address(state->ss->status_page);
  103. status->sequence++;
  104. smp_wmb();
  105. status->policyload = seqno;
  106. status->deny_unknown = !security_get_allow_unknown(state);
  107. smp_wmb();
  108. status->sequence++;
  109. }
  110. mutex_unlock(&state->ss->status_lock);
  111. }