avc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_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. * AVC operations
  57. */
  58. void __init avc_init(void);
  59. static inline u32 avc_audit_required(u32 requested,
  60. struct av_decision *avd,
  61. int result,
  62. u32 auditdeny,
  63. u32 *deniedp)
  64. {
  65. u32 denied, audited;
  66. denied = requested & ~avd->allowed;
  67. if (unlikely(denied)) {
  68. audited = denied & avd->auditdeny;
  69. /*
  70. * auditdeny is TRICKY! Setting a bit in
  71. * this field means that ANY denials should NOT be audited if
  72. * the policy contains an explicit dontaudit rule for that
  73. * permission. Take notice that this is unrelated to the
  74. * actual permissions that were denied. As an example lets
  75. * assume:
  76. *
  77. * denied == READ
  78. * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
  79. * auditdeny & ACCESS == 1
  80. *
  81. * We will NOT audit the denial even though the denied
  82. * permission was READ and the auditdeny checks were for
  83. * ACCESS
  84. */
  85. if (auditdeny && !(auditdeny & avd->auditdeny))
  86. audited = 0;
  87. } else if (result)
  88. audited = denied = requested;
  89. else
  90. audited = requested & avd->auditallow;
  91. *deniedp = denied;
  92. return audited;
  93. }
  94. int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
  95. u32 requested, u32 audited, u32 denied, int result,
  96. struct common_audit_data *a,
  97. unsigned flags);
  98. /**
  99. * avc_audit - Audit the granting or denial of permissions.
  100. * @ssid: source security identifier
  101. * @tsid: target security identifier
  102. * @tclass: target security class
  103. * @requested: requested permissions
  104. * @avd: access vector decisions
  105. * @result: result from avc_has_perm_noaudit
  106. * @a: auxiliary audit data
  107. * @flags: VFS walk flags
  108. *
  109. * Audit the granting or denial of permissions in accordance
  110. * with the policy. This function is typically called by
  111. * avc_has_perm() after a permission check, but can also be
  112. * called directly by callers who use avc_has_perm_noaudit()
  113. * in order to separate the permission check from the auditing.
  114. * For example, this separation is useful when the permission check must
  115. * be performed under a lock, to allow the lock to be released
  116. * before calling the auditing code.
  117. */
  118. static inline int avc_audit(u32 ssid, u32 tsid,
  119. u16 tclass, u32 requested,
  120. struct av_decision *avd,
  121. int result,
  122. struct common_audit_data *a,
  123. int flags)
  124. {
  125. u32 audited, denied;
  126. audited = avc_audit_required(requested, avd, result, 0, &denied);
  127. if (likely(!audited))
  128. return 0;
  129. return slow_avc_audit(ssid, tsid, tclass,
  130. requested, audited, denied, result,
  131. a, flags);
  132. }
  133. #define AVC_STRICT 1 /* Ignore permissive mode. */
  134. #define AVC_EXTENDED_PERMS 2 /* update extended permissions */
  135. int avc_has_perm_noaudit(u32 ssid, u32 tsid,
  136. u16 tclass, u32 requested,
  137. unsigned flags,
  138. struct av_decision *avd);
  139. int avc_has_perm(u32 ssid, u32 tsid,
  140. u16 tclass, u32 requested,
  141. struct common_audit_data *auditdata);
  142. int avc_has_perm_flags(u32 ssid, u32 tsid,
  143. u16 tclass, u32 requested,
  144. struct common_audit_data *auditdata,
  145. int flags);
  146. int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
  147. u8 driver, u8 perm, struct common_audit_data *ad);
  148. u32 avc_policy_seqno(void);
  149. #define AVC_CALLBACK_GRANT 1
  150. #define AVC_CALLBACK_TRY_REVOKE 2
  151. #define AVC_CALLBACK_REVOKE 4
  152. #define AVC_CALLBACK_RESET 8
  153. #define AVC_CALLBACK_AUDITALLOW_ENABLE 16
  154. #define AVC_CALLBACK_AUDITALLOW_DISABLE 32
  155. #define AVC_CALLBACK_AUDITDENY_ENABLE 64
  156. #define AVC_CALLBACK_AUDITDENY_DISABLE 128
  157. #define AVC_CALLBACK_ADD_XPERMS 256
  158. int avc_add_callback(int (*callback)(u32 event), u32 events);
  159. /* Exported to selinuxfs */
  160. int avc_get_hash_stats(char *page);
  161. extern unsigned int avc_cache_threshold;
  162. /* Attempt to free avc node cache */
  163. void avc_disable(void);
  164. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  165. DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats);
  166. #endif
  167. #endif /* _SELINUX_AVC_H_ */