avc.h 4.5 KB

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