perms.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor basic permission sets definitions.
  6. *
  7. * Copyright 2017 Canonical Ltd.
  8. */
  9. #ifndef __AA_PERM_H
  10. #define __AA_PERM_H
  11. #include <linux/fs.h>
  12. #include "label.h"
  13. #define AA_MAY_EXEC MAY_EXEC
  14. #define AA_MAY_WRITE MAY_WRITE
  15. #define AA_MAY_READ MAY_READ
  16. #define AA_MAY_APPEND MAY_APPEND
  17. #define AA_MAY_CREATE 0x0010
  18. #define AA_MAY_DELETE 0x0020
  19. #define AA_MAY_OPEN 0x0040
  20. #define AA_MAY_RENAME 0x0080 /* pair */
  21. #define AA_MAY_SETATTR 0x0100 /* meta write */
  22. #define AA_MAY_GETATTR 0x0200 /* meta read */
  23. #define AA_MAY_SETCRED 0x0400 /* security cred/attr */
  24. #define AA_MAY_GETCRED 0x0800
  25. #define AA_MAY_CHMOD 0x1000 /* pair */
  26. #define AA_MAY_CHOWN 0x2000 /* pair */
  27. #define AA_MAY_CHGRP 0x4000 /* pair */
  28. #define AA_MAY_LOCK 0x8000 /* LINK_SUBSET overlaid */
  29. #define AA_EXEC_MMAP 0x00010000
  30. #define AA_MAY_MPROT 0x00020000 /* extend conditions */
  31. #define AA_MAY_LINK 0x00040000 /* pair */
  32. #define AA_MAY_SNAPSHOT 0x00080000 /* pair */
  33. #define AA_MAY_DELEGATE
  34. #define AA_CONT_MATCH 0x08000000
  35. #define AA_MAY_STACK 0x10000000
  36. #define AA_MAY_ONEXEC 0x20000000 /* either stack or change_profile */
  37. #define AA_MAY_CHANGE_PROFILE 0x40000000
  38. #define AA_MAY_CHANGEHAT 0x80000000
  39. #define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */
  40. #define AA_MAY_CREATE_SQPOLL AA_MAY_CREATE
  41. #define AA_MAY_OVERRIDE_CRED AA_MAY_APPEND
  42. #define AA_URING_PERM_MASK (AA_MAY_OVERRIDE_CRED | AA_MAY_CREATE_SQPOLL)
  43. #define PERMS_CHRS_MASK (MAY_READ | MAY_WRITE | AA_MAY_CREATE | \
  44. AA_MAY_DELETE | AA_MAY_LINK | AA_MAY_LOCK | \
  45. AA_MAY_EXEC | AA_EXEC_MMAP | AA_MAY_APPEND)
  46. #define PERMS_NAMES_MASK (PERMS_CHRS_MASK | AA_MAY_OPEN | AA_MAY_RENAME | \
  47. AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_SETCRED | \
  48. AA_MAY_GETCRED | AA_MAY_CHMOD | AA_MAY_CHOWN | \
  49. AA_MAY_CHGRP | AA_MAY_MPROT | AA_MAY_SNAPSHOT | \
  50. AA_MAY_STACK | AA_MAY_ONEXEC | \
  51. AA_MAY_CHANGE_PROFILE | AA_MAY_CHANGEHAT)
  52. extern const char aa_file_perm_chrs[];
  53. extern const char *aa_file_perm_names[];
  54. struct aa_perms {
  55. u32 allow;
  56. u32 deny; /* explicit deny, or conflict if allow also set */
  57. u32 subtree; /* allow perm on full subtree only when allow is set */
  58. u32 cond; /* set only when ~allow and ~deny */
  59. u32 kill; /* set only when ~allow | deny */
  60. u32 complain; /* accumulates only used when ~allow & ~deny */
  61. u32 prompt; /* accumulates only used when ~allow & ~deny */
  62. u32 audit; /* set only when allow is set */
  63. u32 quiet; /* set only when ~allow | deny */
  64. u32 hide; /* set only when ~allow | deny */
  65. u32 xindex;
  66. u32 tag; /* tag string index, if present */
  67. u32 label; /* label string index, if present */
  68. };
  69. /*
  70. * Indexes are broken into a 24 bit index and 8 bit flag.
  71. * For the index to be valid there must be a value in the flag
  72. */
  73. #define AA_INDEX_MASK 0x00ffffff
  74. #define AA_INDEX_FLAG_MASK 0xff000000
  75. #define AA_INDEX_NONE 0
  76. #define ALL_PERMS_MASK 0xffffffff
  77. extern struct aa_perms nullperms;
  78. extern struct aa_perms allperms;
  79. /**
  80. * aa_perms_accum_raw - accumulate perms with out masking off overlapping perms
  81. * @accum: perms struct to accumulate into
  82. * @addend: perms struct to add to @accum
  83. */
  84. static inline void aa_perms_accum_raw(struct aa_perms *accum,
  85. struct aa_perms *addend)
  86. {
  87. accum->deny |= addend->deny;
  88. accum->allow &= addend->allow & ~addend->deny;
  89. accum->audit |= addend->audit & addend->allow;
  90. accum->quiet &= addend->quiet & ~addend->allow;
  91. accum->kill |= addend->kill & ~addend->allow;
  92. accum->complain |= addend->complain & ~addend->allow & ~addend->deny;
  93. accum->cond |= addend->cond & ~addend->allow & ~addend->deny;
  94. accum->hide &= addend->hide & ~addend->allow;
  95. accum->prompt |= addend->prompt & ~addend->allow & ~addend->deny;
  96. accum->subtree |= addend->subtree & ~addend->deny;
  97. if (!accum->xindex)
  98. accum->xindex = addend->xindex;
  99. if (!accum->tag)
  100. accum->tag = addend->tag;
  101. if (!accum->label)
  102. accum->label = addend->label;
  103. }
  104. /**
  105. * aa_perms_accum - accumulate perms, masking off overlapping perms
  106. * @accum: perms struct to accumulate into
  107. * @addend: perms struct to add to @accum
  108. */
  109. static inline void aa_perms_accum(struct aa_perms *accum,
  110. struct aa_perms *addend)
  111. {
  112. accum->deny |= addend->deny;
  113. accum->allow &= addend->allow & ~accum->deny;
  114. accum->audit |= addend->audit & accum->allow;
  115. accum->quiet &= addend->quiet & ~accum->allow;
  116. accum->kill |= addend->kill & ~accum->allow;
  117. accum->complain |= addend->complain & ~accum->allow & ~accum->deny;
  118. accum->cond |= addend->cond & ~accum->allow & ~accum->deny;
  119. accum->hide &= addend->hide & ~accum->allow;
  120. accum->prompt |= addend->prompt & ~accum->allow & ~accum->deny;
  121. accum->subtree &= addend->subtree & ~accum->deny;
  122. if (!accum->xindex)
  123. accum->xindex = addend->xindex;
  124. if (!accum->tag)
  125. accum->tag = addend->tag;
  126. if (!accum->label)
  127. accum->label = addend->label;
  128. }
  129. #define xcheck(FN1, FN2) \
  130. ({ \
  131. int e, error = FN1; \
  132. e = FN2; \
  133. if (e) \
  134. error = e; \
  135. error; \
  136. })
  137. /*
  138. * TODO: update for labels pointing to labels instead of profiles
  139. * TODO: optimize the walk, currently does subwalk of L2 for each P in L1
  140. * gah this doesn't allow for label compound check!!!!
  141. */
  142. #define xcheck_ns_profile_profile(P1, P2, FN, args...) \
  143. ({ \
  144. int ____e = 0; \
  145. if (P1->ns == P2->ns) \
  146. ____e = FN((P1), (P2), args); \
  147. (____e); \
  148. })
  149. #define xcheck_ns_profile_label(P, L, FN, args...) \
  150. ({ \
  151. struct aa_profile *__p2; \
  152. fn_for_each((L), __p2, \
  153. xcheck_ns_profile_profile((P), __p2, (FN), args)); \
  154. })
  155. #define xcheck_ns_labels(L1, L2, FN, args...) \
  156. ({ \
  157. struct aa_profile *__p1; \
  158. fn_for_each((L1), __p1, FN(__p1, (L2), args)); \
  159. })
  160. /* Do the cross check but applying FN at the profiles level */
  161. #define xcheck_labels_profiles(L1, L2, FN, args...) \
  162. xcheck_ns_labels((L1), (L2), xcheck_ns_profile_label, (FN), args)
  163. #define xcheck_labels(L1, L2, P, FN1, FN2) \
  164. xcheck(fn_for_each((L1), (P), (FN1)), fn_for_each((L2), (P), (FN2)))
  165. extern struct aa_perms default_perms;
  166. void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs,
  167. u32 mask);
  168. void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
  169. u32 mask);
  170. void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
  171. u32 chrsmask, const char * const *names, u32 namesmask);
  172. void aa_apply_modes_to_perms(struct aa_profile *profile,
  173. struct aa_perms *perms);
  174. void aa_perms_accum(struct aa_perms *accum, struct aa_perms *addend);
  175. void aa_perms_accum_raw(struct aa_perms *accum, struct aa_perms *addend);
  176. void aa_profile_match_label(struct aa_profile *profile,
  177. struct aa_ruleset *rules, struct aa_label *label,
  178. int type, u32 request, struct aa_perms *perms);
  179. int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
  180. u32 request, struct apparmor_audit_data *ad,
  181. void (*cb)(struct audit_buffer *, void *));
  182. #endif /* __AA_PERM_H */