avc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Access vector cache interface for object managers.
  4. *
  5. * Author : Stephen Smalley, <[email protected]>
  6. */
  7. #ifndef _SELINUX_AVC_H_
  8. #define _SELINUX_AVC_H_
  9. #include <linux/stddef.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/init.h>
  15. #include <linux/audit.h>
  16. #include <linux/lsm_audit.h>
  17. #include <linux/in6.h>
  18. #include "flask.h"
  19. #include "av_permissions.h"
  20. #include "security.h"
  21. /*
  22. * An entry in the AVC.
  23. */
  24. struct avc_entry;
  25. struct task_struct;
  26. struct inode;
  27. struct sock;
  28. struct sk_buff;
  29. /*
  30. * AVC statistics
  31. */
  32. struct avc_cache_stats {
  33. unsigned int lookups;
  34. unsigned int misses;
  35. unsigned int allocations;
  36. unsigned int reclaims;
  37. unsigned int frees;
  38. };
  39. /*
  40. * We only need this data after we have decided to send an audit message.
  41. */
  42. struct selinux_audit_data {
  43. u32 ssid;
  44. u32 tsid;
  45. u16 tclass;
  46. u32 requested;
  47. u32 audited;
  48. u32 denied;
  49. int result;
  50. struct selinux_state *state;
  51. } __randomize_layout;
  52. /*
  53. * AVC operations
  54. */
  55. void __init avc_init(void);
  56. static inline u32 avc_audit_required(u32 requested,
  57. struct av_decision *avd,
  58. int result,
  59. u32 auditdeny,
  60. u32 *deniedp)
  61. {
  62. u32 denied, audited;
  63. denied = requested & ~avd->allowed;
  64. if (unlikely(denied)) {
  65. audited = denied & avd->auditdeny;
  66. /*
  67. * auditdeny is TRICKY! Setting a bit in
  68. * this field means that ANY denials should NOT be audited if
  69. * the policy contains an explicit dontaudit rule for that
  70. * permission. Take notice that this is unrelated to the
  71. * actual permissions that were denied. As an example lets
  72. * assume:
  73. *
  74. * denied == READ
  75. * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
  76. * auditdeny & ACCESS == 1
  77. *
  78. * We will NOT audit the denial even though the denied
  79. * permission was READ and the auditdeny checks were for
  80. * ACCESS
  81. */
  82. if (auditdeny && !(auditdeny & avd->auditdeny))
  83. audited = 0;
  84. } else if (result)
  85. audited = denied = requested;
  86. else
  87. audited = requested & avd->auditallow;
  88. *deniedp = denied;
  89. return audited;
  90. }
  91. int slow_avc_audit(struct selinux_state *state,
  92. u32 ssid, u32 tsid, u16 tclass,
  93. u32 requested, u32 audited, u32 denied, int result,
  94. struct common_audit_data *a);
  95. /**
  96. * avc_audit - Audit the granting or denial of permissions.
  97. * @state: SELinux state
  98. * @ssid: source security identifier
  99. * @tsid: target security identifier
  100. * @tclass: target security class
  101. * @requested: requested permissions
  102. * @avd: access vector decisions
  103. * @result: result from avc_has_perm_noaudit
  104. * @a: auxiliary audit data
  105. *
  106. * Audit the granting or denial of permissions in accordance
  107. * with the policy. This function is typically called by
  108. * avc_has_perm() after a permission check, but can also be
  109. * called directly by callers who use avc_has_perm_noaudit()
  110. * in order to separate the permission check from the auditing.
  111. * For example, this separation is useful when the permission check must
  112. * be performed under a lock, to allow the lock to be released
  113. * before calling the auditing code.
  114. */
  115. static inline int avc_audit(struct selinux_state *state,
  116. u32 ssid, u32 tsid,
  117. u16 tclass, u32 requested,
  118. struct av_decision *avd,
  119. int result,
  120. struct common_audit_data *a)
  121. {
  122. u32 audited, denied;
  123. audited = avc_audit_required(requested, avd, result, 0, &denied);
  124. if (likely(!audited))
  125. return 0;
  126. return slow_avc_audit(state, ssid, tsid, tclass,
  127. requested, audited, denied, result,
  128. a);
  129. }
  130. #define AVC_STRICT 1 /* Ignore permissive mode. */
  131. #define AVC_EXTENDED_PERMS 2 /* update extended permissions */
  132. int avc_has_perm_noaudit(struct selinux_state *state,
  133. u32 ssid, u32 tsid,
  134. u16 tclass, u32 requested,
  135. unsigned flags,
  136. struct av_decision *avd);
  137. int avc_has_perm(struct selinux_state *state,
  138. u32 ssid, u32 tsid,
  139. u16 tclass, u32 requested,
  140. struct common_audit_data *auditdata);
  141. int avc_has_extended_perms(struct selinux_state *state,
  142. u32 ssid, u32 tsid, u16 tclass, u32 requested,
  143. u8 driver, u8 perm, struct common_audit_data *ad);
  144. u32 avc_policy_seqno(struct selinux_state *state);
  145. #define AVC_CALLBACK_GRANT 1
  146. #define AVC_CALLBACK_TRY_REVOKE 2
  147. #define AVC_CALLBACK_REVOKE 4
  148. #define AVC_CALLBACK_RESET 8
  149. #define AVC_CALLBACK_AUDITALLOW_ENABLE 16
  150. #define AVC_CALLBACK_AUDITALLOW_DISABLE 32
  151. #define AVC_CALLBACK_AUDITDENY_ENABLE 64
  152. #define AVC_CALLBACK_AUDITDENY_DISABLE 128
  153. #define AVC_CALLBACK_ADD_XPERMS 256
  154. int avc_add_callback(int (*callback)(u32 event), u32 events);
  155. /* Exported to selinuxfs */
  156. struct selinux_avc;
  157. int avc_get_hash_stats(struct selinux_avc *avc, char *page);
  158. unsigned int avc_get_cache_threshold(struct selinux_avc *avc);
  159. void avc_set_cache_threshold(struct selinux_avc *avc,
  160. unsigned int cache_threshold);
  161. /* Attempt to free avc node cache */
  162. void avc_disable(void);
  163. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  164. DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats);
  165. #endif
  166. #endif /* _SELINUX_AVC_H_ */