file.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor file mediation function definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #ifndef __AA_FILE_H
  11. #define __AA_FILE_H
  12. #include <linux/spinlock.h>
  13. #include "domain.h"
  14. #include "match.h"
  15. #include "perms.h"
  16. struct aa_profile;
  17. struct path;
  18. #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
  19. #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
  20. AA_MAY_CREATE | AA_MAY_DELETE | \
  21. AA_MAY_GETATTR | AA_MAY_SETATTR | \
  22. AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
  23. AA_EXEC_MMAP | AA_MAY_LINK)
  24. static inline struct aa_file_ctx *file_ctx(struct file *file)
  25. {
  26. return file->f_security + apparmor_blob_sizes.lbs_file;
  27. }
  28. /* struct aa_file_ctx - the AppArmor context the file was opened in
  29. * @lock: lock to update the ctx
  30. * @label: label currently cached on the ctx
  31. * @perms: the permission the file was opened with
  32. */
  33. struct aa_file_ctx {
  34. spinlock_t lock;
  35. struct aa_label __rcu *label;
  36. u32 allow;
  37. };
  38. /**
  39. * aa_alloc_file_ctx - allocate file_ctx
  40. * @label: initial label of task creating the file
  41. * @gfp: gfp flags for allocation
  42. *
  43. * Returns: file_ctx or NULL on failure
  44. */
  45. static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
  46. gfp_t gfp)
  47. {
  48. struct aa_file_ctx *ctx;
  49. ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
  50. if (ctx) {
  51. spin_lock_init(&ctx->lock);
  52. rcu_assign_pointer(ctx->label, aa_get_label(label));
  53. }
  54. return ctx;
  55. }
  56. /**
  57. * aa_free_file_ctx - free a file_ctx
  58. * @ctx: file_ctx to free (MAYBE_NULL)
  59. */
  60. static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
  61. {
  62. if (ctx) {
  63. aa_put_label(rcu_access_pointer(ctx->label));
  64. kfree_sensitive(ctx);
  65. }
  66. }
  67. static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
  68. {
  69. return aa_get_label_rcu(&ctx->label);
  70. }
  71. /*
  72. * The xindex is broken into 3 parts
  73. * - index - an index into either the exec name table or the variable table
  74. * - exec type - which determines how the executable name and index are used
  75. * - flags - which modify how the destination name is applied
  76. */
  77. #define AA_X_INDEX_MASK 0x03ff
  78. #define AA_X_TYPE_MASK 0x0c00
  79. #define AA_X_TYPE_SHIFT 10
  80. #define AA_X_NONE 0x0000
  81. #define AA_X_NAME 0x0400 /* use executable name px */
  82. #define AA_X_TABLE 0x0800 /* use a specified name ->n# */
  83. #define AA_X_UNSAFE 0x1000
  84. #define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */
  85. #define AA_X_INHERIT 0x4000
  86. #define AA_X_UNCONFINED 0x8000
  87. /* need to make conditional which ones are being set */
  88. struct path_cond {
  89. kuid_t uid;
  90. umode_t mode;
  91. };
  92. #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
  93. /* FIXME: split perms from dfa and match this to description
  94. * also add delegation info.
  95. */
  96. static inline u16 dfa_map_xindex(u16 mask)
  97. {
  98. u16 old_index = (mask >> 10) & 0xf;
  99. u16 index = 0;
  100. if (mask & 0x100)
  101. index |= AA_X_UNSAFE;
  102. if (mask & 0x200)
  103. index |= AA_X_INHERIT;
  104. if (mask & 0x80)
  105. index |= AA_X_UNCONFINED;
  106. if (old_index == 1) {
  107. index |= AA_X_UNCONFINED;
  108. } else if (old_index == 2) {
  109. index |= AA_X_NAME;
  110. } else if (old_index == 3) {
  111. index |= AA_X_NAME | AA_X_CHILD;
  112. } else if (old_index) {
  113. index |= AA_X_TABLE;
  114. index |= old_index - 4;
  115. }
  116. return index;
  117. }
  118. /*
  119. * map old dfa inline permissions to new format
  120. */
  121. #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
  122. ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
  123. #define dfa_user_xbits(dfa, state) (((ACCEPT_TABLE(dfa)[state]) >> 7) & 0x7f)
  124. #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
  125. #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
  126. #define dfa_user_xindex(dfa, state) \
  127. (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
  128. #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
  129. 0x7f) | \
  130. ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
  131. #define dfa_other_xbits(dfa, state) \
  132. ((((ACCEPT_TABLE(dfa)[state]) >> 7) >> 14) & 0x7f)
  133. #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
  134. #define dfa_other_quiet(dfa, state) \
  135. ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
  136. #define dfa_other_xindex(dfa, state) \
  137. dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
  138. int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
  139. const char *op, u32 request, const char *name,
  140. const char *target, struct aa_label *tlabel, kuid_t ouid,
  141. const char *info, int error);
  142. /**
  143. * struct aa_file_rules - components used for file rule permissions
  144. * @dfa: dfa to match path names and conditionals against
  145. * @perms: permission table indexed by the matched state accept entry of @dfa
  146. * @trans: transition table for indexed by named x transitions
  147. *
  148. * File permission are determined by matching a path against @dfa and
  149. * then using the value of the accept entry for the matching state as
  150. * an index into @perms. If a named exec transition is required it is
  151. * looked up in the transition table.
  152. */
  153. struct aa_file_rules {
  154. unsigned int start;
  155. struct aa_dfa *dfa;
  156. /* struct perms perms; */
  157. struct aa_domain trans;
  158. /* TODO: add delegate table */
  159. };
  160. struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
  161. struct path_cond *cond);
  162. unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
  163. const char *name, struct path_cond *cond,
  164. struct aa_perms *perms);
  165. int __aa_path_perm(const char *op, struct aa_profile *profile,
  166. const char *name, u32 request, struct path_cond *cond,
  167. int flags, struct aa_perms *perms);
  168. int aa_path_perm(const char *op, struct aa_label *label,
  169. const struct path *path, int flags, u32 request,
  170. struct path_cond *cond);
  171. int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
  172. const struct path *new_dir, struct dentry *new_dentry);
  173. int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
  174. u32 request, bool in_atomic);
  175. void aa_inherit_files(const struct cred *cred, struct files_struct *files);
  176. static inline void aa_free_file_rules(struct aa_file_rules *rules)
  177. {
  178. aa_put_dfa(rules->dfa);
  179. aa_free_domain_entries(&rules->trans);
  180. }
  181. /**
  182. * aa_map_file_perms - map file flags to AppArmor permissions
  183. * @file: open file to map flags to AppArmor permissions
  184. *
  185. * Returns: apparmor permission set for the file
  186. */
  187. static inline u32 aa_map_file_to_perms(struct file *file)
  188. {
  189. int flags = file->f_flags;
  190. u32 perms = 0;
  191. if (file->f_mode & FMODE_WRITE)
  192. perms |= MAY_WRITE;
  193. if (file->f_mode & FMODE_READ)
  194. perms |= MAY_READ;
  195. if ((flags & O_APPEND) && (perms & MAY_WRITE))
  196. perms = (perms & ~MAY_WRITE) | MAY_APPEND;
  197. /* trunc implies write permission */
  198. if (flags & O_TRUNC)
  199. perms |= MAY_WRITE;
  200. if (flags & O_CREAT)
  201. perms |= AA_MAY_CREATE;
  202. return perms;
  203. }
  204. #endif /* __AA_FILE_H */