avc.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Access vector cache interface for object managers.
  4. *
  5. * Author : Stephen Smalley, <sds@tycho.nsa.gov>
  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. };
  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. unsigned flags);
  96. /**
  97. * avc_audit - Audit the granting or denial of permissions.
  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. * @flags: VFS walk flags
  106. *
  107. * Audit the granting or denial of permissions in accordance
  108. * with the policy. This function is typically called by
  109. * avc_has_perm() after a permission check, but can also be
  110. * called directly by callers who use avc_has_perm_noaudit()
  111. * in order to separate the permission check from the auditing.
  112. * For example, this separation is useful when the permission check must
  113. * be performed under a lock, to allow the lock to be released
  114. * before calling the auditing code.
  115. */
  116. static inline int avc_audit(struct selinux_state *state,
  117. u32 ssid, u32 tsid,
  118. u16 tclass, u32 requested,
  119. struct av_decision *avd,
  120. int result,
  121. struct common_audit_data *a,
  122. int flags)
  123. {
  124. u32 audited, denied;
  125. audited = avc_audit_required(requested, avd, result, 0, &denied);
  126. if (likely(!audited))
  127. return 0;
  128. return slow_avc_audit(state, ssid, tsid, tclass,
  129. requested, audited, denied, result,
  130. a, flags);
  131. }
  132. #define AVC_STRICT 1 /* Ignore permissive mode. */
  133. #define AVC_EXTENDED_PERMS 2 /* update extended permissions */
  134. int avc_has_perm_noaudit(struct selinux_state *state,
  135. u32 ssid, u32 tsid,
  136. u16 tclass, u32 requested,
  137. unsigned flags,
  138. struct av_decision *avd);
  139. int avc_has_perm(struct selinux_state *state,
  140. u32 ssid, u32 tsid,
  141. u16 tclass, u32 requested,
  142. struct common_audit_data *auditdata);
  143. int avc_has_perm_flags(struct selinux_state *state,
  144. u32 ssid, u32 tsid,
  145. u16 tclass, u32 requested,
  146. struct common_audit_data *auditdata,
  147. int flags);
  148. int avc_has_extended_perms(struct selinux_state *state,
  149. u32 ssid, u32 tsid, u16 tclass, u32 requested,
  150. u8 driver, u8 perm, struct common_audit_data *ad);
  151. u32 avc_policy_seqno(struct selinux_state *state);
  152. #define AVC_CALLBACK_GRANT 1
  153. #define AVC_CALLBACK_TRY_REVOKE 2
  154. #define AVC_CALLBACK_REVOKE 4
  155. #define AVC_CALLBACK_RESET 8
  156. #define AVC_CALLBACK_AUDITALLOW_ENABLE 16
  157. #define AVC_CALLBACK_AUDITALLOW_DISABLE 32
  158. #define AVC_CALLBACK_AUDITDENY_ENABLE 64
  159. #define AVC_CALLBACK_AUDITDENY_DISABLE 128
  160. #define AVC_CALLBACK_ADD_XPERMS 256
  161. int avc_add_callback(int (*callback)(u32 event), u32 events);
  162. /* Exported to selinuxfs */
  163. struct selinux_avc;
  164. int avc_get_hash_stats(struct selinux_avc *avc, char *page);
  165. unsigned int avc_get_cache_threshold(struct selinux_avc *avc);
  166. void avc_set_cache_threshold(struct selinux_avc *avc,
  167. unsigned int cache_threshold);
  168. /* Attempt to free avc node cache */
  169. void avc_disable(void);
  170. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  171. DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats);
  172. #endif
  173. #ifdef CONFIG_MTK_SELINUX_AEE_WARNING
  174. extern struct sk_buff *audit_get_skb(struct audit_buffer *ab);
  175. extern void __attribute__((weak)) mtk_audit_hook(char *data);
  176. #endif
  177. #endif /* _SELINUX_AVC_H_ */