avc.h 5.2 KB

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