perms.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor basic permission sets definitions.
  5. *
  6. * Copyright 2017 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. */
  13. #ifndef __AA_PERM_H
  14. #define __AA_PERM_H
  15. #include <linux/fs.h>
  16. #include "label.h"
  17. #define AA_MAY_EXEC MAY_EXEC
  18. #define AA_MAY_WRITE MAY_WRITE
  19. #define AA_MAY_READ MAY_READ
  20. #define AA_MAY_APPEND MAY_APPEND
  21. #define AA_MAY_CREATE 0x0010
  22. #define AA_MAY_DELETE 0x0020
  23. #define AA_MAY_OPEN 0x0040
  24. #define AA_MAY_RENAME 0x0080 /* pair */
  25. #define AA_MAY_SETATTR 0x0100 /* meta write */
  26. #define AA_MAY_GETATTR 0x0200 /* meta read */
  27. #define AA_MAY_SETCRED 0x0400 /* security cred/attr */
  28. #define AA_MAY_GETCRED 0x0800
  29. #define AA_MAY_CHMOD 0x1000 /* pair */
  30. #define AA_MAY_CHOWN 0x2000 /* pair */
  31. #define AA_MAY_CHGRP 0x4000 /* pair */
  32. #define AA_MAY_LOCK 0x8000 /* LINK_SUBSET overlaid */
  33. #define AA_EXEC_MMAP 0x00010000
  34. #define AA_MAY_MPROT 0x00020000 /* extend conditions */
  35. #define AA_MAY_LINK 0x00040000 /* pair */
  36. #define AA_MAY_SNAPSHOT 0x00080000 /* pair */
  37. #define AA_MAY_DELEGATE
  38. #define AA_CONT_MATCH 0x08000000
  39. #define AA_MAY_STACK 0x10000000
  40. #define AA_MAY_ONEXEC 0x20000000 /* either stack or change_profile */
  41. #define AA_MAY_CHANGE_PROFILE 0x40000000
  42. #define AA_MAY_CHANGEHAT 0x80000000
  43. #define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */
  44. #define PERMS_CHRS_MASK (MAY_READ | MAY_WRITE | AA_MAY_CREATE | \
  45. AA_MAY_DELETE | AA_MAY_LINK | AA_MAY_LOCK | \
  46. AA_MAY_EXEC | AA_EXEC_MMAP | AA_MAY_APPEND)
  47. #define PERMS_NAMES_MASK (PERMS_CHRS_MASK | AA_MAY_OPEN | AA_MAY_RENAME | \
  48. AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_SETCRED | \
  49. AA_MAY_GETCRED | AA_MAY_CHMOD | AA_MAY_CHOWN | \
  50. AA_MAY_CHGRP | AA_MAY_MPROT | AA_MAY_SNAPSHOT | \
  51. AA_MAY_STACK | AA_MAY_ONEXEC | \
  52. AA_MAY_CHANGE_PROFILE | AA_MAY_CHANGEHAT)
  53. extern const char aa_file_perm_chrs[];
  54. extern const char *aa_file_perm_names[];
  55. struct aa_perms {
  56. u32 allow;
  57. u32 audit; /* set only when allow is set */
  58. u32 deny; /* explicit deny, or conflict if allow also set */
  59. u32 quiet; /* set only when ~allow | deny */
  60. u32 kill; /* set only when ~allow | deny */
  61. u32 stop; /* set only when ~allow | deny */
  62. u32 complain; /* accumulates only used when ~allow & ~deny */
  63. u32 cond; /* set only when ~allow and ~deny */
  64. u32 hide; /* set only when ~allow | deny */
  65. u32 prompt; /* accumulates only used when ~allow & ~deny */
  66. /* Reserved:
  67. * u32 subtree; / * set only when allow is set * /
  68. */
  69. u16 xindex;
  70. };
  71. #define ALL_PERMS_MASK 0xffffffff
  72. extern struct aa_perms nullperms;
  73. extern struct aa_perms allperms;
  74. #define xcheck(FN1, FN2) \
  75. ({ \
  76. int e, error = FN1; \
  77. e = FN2; \
  78. if (e) \
  79. error = e; \
  80. error; \
  81. })
  82. /*
  83. * TODO: update for labels pointing to labels instead of profiles
  84. * TODO: optimize the walk, currently does subwalk of L2 for each P in L1
  85. * gah this doesn't allow for label compound check!!!!
  86. */
  87. #define xcheck_ns_profile_profile(P1, P2, FN, args...) \
  88. ({ \
  89. int ____e = 0; \
  90. if (P1->ns == P2->ns) \
  91. ____e = FN((P1), (P2), args); \
  92. (____e); \
  93. })
  94. #define xcheck_ns_profile_label(P, L, FN, args...) \
  95. ({ \
  96. struct aa_profile *__p2; \
  97. fn_for_each((L), __p2, \
  98. xcheck_ns_profile_profile((P), __p2, (FN), args)); \
  99. })
  100. #define xcheck_ns_labels(L1, L2, FN, args...) \
  101. ({ \
  102. struct aa_profile *__p1; \
  103. fn_for_each((L1), __p1, FN(__p1, (L2), args)); \
  104. })
  105. /* Do the cross check but applying FN at the profiles level */
  106. #define xcheck_labels_profiles(L1, L2, FN, args...) \
  107. xcheck_ns_labels((L1), (L2), xcheck_ns_profile_label, (FN), args)
  108. #define xcheck_labels(L1, L2, P, FN1, FN2) \
  109. xcheck(fn_for_each((L1), (P), (FN1)), fn_for_each((L2), (P), (FN2)))
  110. void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs,
  111. u32 mask);
  112. void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
  113. u32 mask);
  114. void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
  115. u32 chrsmask, const char * const *names, u32 namesmask);
  116. void aa_apply_modes_to_perms(struct aa_profile *profile,
  117. struct aa_perms *perms);
  118. void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
  119. struct aa_perms *perms);
  120. void aa_perms_accum(struct aa_perms *accum, struct aa_perms *addend);
  121. void aa_perms_accum_raw(struct aa_perms *accum, struct aa_perms *addend);
  122. void aa_profile_match_label(struct aa_profile *profile, struct aa_label *label,
  123. int type, u32 request, struct aa_perms *perms);
  124. int aa_profile_label_perm(struct aa_profile *profile, struct aa_profile *target,
  125. u32 request, int type, u32 *deny,
  126. struct common_audit_data *sa);
  127. int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
  128. u32 request, struct common_audit_data *sa,
  129. void (*cb)(struct audit_buffer *, void *));
  130. #endif /* __AA_PERM_H */