file.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor file mediation function definitions.
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #ifndef __AA_FILE_H
  15. #define __AA_FILE_H
  16. #include "domain.h"
  17. #include "match.h"
  18. struct aa_profile;
  19. struct path;
  20. /*
  21. * We use MAY_EXEC, MAY_WRITE, MAY_READ, MAY_APPEND and the following flags
  22. * for profile permissions
  23. */
  24. #define AA_MAY_CREATE 0x0010
  25. #define AA_MAY_DELETE 0x0020
  26. #define AA_MAY_META_WRITE 0x0040
  27. #define AA_MAY_META_READ 0x0080
  28. #define AA_MAY_CHMOD 0x0100
  29. #define AA_MAY_CHOWN 0x0200
  30. #define AA_MAY_LOCK 0x0400
  31. #define AA_EXEC_MMAP 0x0800
  32. #define AA_MAY_LINK 0x1000
  33. #define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */
  34. #define AA_MAY_ONEXEC 0x40000000 /* exec allows onexec */
  35. #define AA_MAY_CHANGE_PROFILE 0x80000000
  36. #define AA_MAY_CHANGEHAT 0x80000000 /* ctrl auditing only */
  37. #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
  38. AA_MAY_CREATE | AA_MAY_DELETE | \
  39. AA_MAY_META_READ | AA_MAY_META_WRITE | \
  40. AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
  41. AA_EXEC_MMAP | AA_MAY_LINK)
  42. /*
  43. * The xindex is broken into 3 parts
  44. * - index - an index into either the exec name table or the variable table
  45. * - exec type - which determines how the executable name and index are used
  46. * - flags - which modify how the destination name is applied
  47. */
  48. #define AA_X_INDEX_MASK 0x03ff
  49. #define AA_X_TYPE_MASK 0x0c00
  50. #define AA_X_TYPE_SHIFT 10
  51. #define AA_X_NONE 0x0000
  52. #define AA_X_NAME 0x0400 /* use executable name px */
  53. #define AA_X_TABLE 0x0800 /* use a specified name ->n# */
  54. #define AA_X_UNSAFE 0x1000
  55. #define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */
  56. #define AA_X_INHERIT 0x4000
  57. #define AA_X_UNCONFINED 0x8000
  58. /* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */
  59. #define AA_SECURE_X_NEEDED 0x8000
  60. /* need to make conditional which ones are being set */
  61. struct path_cond {
  62. kuid_t uid;
  63. umode_t mode;
  64. };
  65. /* struct file_perms - file permission
  66. * @allow: mask of permissions that are allowed
  67. * @audit: mask of permissions to force an audit message for
  68. * @quiet: mask of permissions to quiet audit messages for
  69. * @kill: mask of permissions that when matched will kill the task
  70. * @xindex: exec transition index if @allow contains MAY_EXEC
  71. *
  72. * The @audit and @queit mask should be mutually exclusive.
  73. */
  74. struct file_perms {
  75. u32 allow;
  76. u32 audit;
  77. u32 quiet;
  78. u32 kill;
  79. u16 xindex;
  80. };
  81. extern struct file_perms nullperms;
  82. #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
  83. /* FIXME: split perms from dfa and match this to description
  84. * also add delegation info.
  85. */
  86. static inline u16 dfa_map_xindex(u16 mask)
  87. {
  88. u16 old_index = (mask >> 10) & 0xf;
  89. u16 index = 0;
  90. if (mask & 0x100)
  91. index |= AA_X_UNSAFE;
  92. if (mask & 0x200)
  93. index |= AA_X_INHERIT;
  94. if (mask & 0x80)
  95. index |= AA_X_UNCONFINED;
  96. if (old_index == 1) {
  97. index |= AA_X_UNCONFINED;
  98. } else if (old_index == 2) {
  99. index |= AA_X_NAME;
  100. } else if (old_index == 3) {
  101. index |= AA_X_NAME | AA_X_CHILD;
  102. } else if (old_index) {
  103. index |= AA_X_TABLE;
  104. index |= old_index - 4;
  105. }
  106. return index;
  107. }
  108. /*
  109. * map old dfa inline permissions to new format
  110. */
  111. #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
  112. ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
  113. #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
  114. #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
  115. #define dfa_user_xindex(dfa, state) \
  116. (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
  117. #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
  118. 0x7f) | \
  119. ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
  120. #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
  121. #define dfa_other_quiet(dfa, state) \
  122. ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
  123. #define dfa_other_xindex(dfa, state) \
  124. dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
  125. int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
  126. gfp_t gfp, int op, u32 request, const char *name,
  127. const char *target, kuid_t ouid, const char *info, int error);
  128. /**
  129. * struct aa_file_rules - components used for file rule permissions
  130. * @dfa: dfa to match path names and conditionals against
  131. * @perms: permission table indexed by the matched state accept entry of @dfa
  132. * @trans: transition table for indexed by named x transitions
  133. *
  134. * File permission are determined by matching a path against @dfa and then
  135. * then using the value of the accept entry for the matching state as
  136. * an index into @perms. If a named exec transition is required it is
  137. * looked up in the transition table.
  138. */
  139. struct aa_file_rules {
  140. unsigned int start;
  141. struct aa_dfa *dfa;
  142. /* struct perms perms; */
  143. struct aa_domain trans;
  144. /* TODO: add delegate table */
  145. };
  146. unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
  147. const char *name, struct path_cond *cond,
  148. struct file_perms *perms);
  149. int aa_path_perm(int op, struct aa_profile *profile, struct path *path,
  150. int flags, u32 request, struct path_cond *cond);
  151. int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
  152. struct path *new_dir, struct dentry *new_dentry);
  153. int aa_file_perm(int op, struct aa_profile *profile, struct file *file,
  154. u32 request);
  155. static inline void aa_free_file_rules(struct aa_file_rules *rules)
  156. {
  157. aa_put_dfa(rules->dfa);
  158. aa_free_domain_entries(&rules->trans);
  159. }
  160. /**
  161. * aa_map_file_perms - map file flags to AppArmor permissions
  162. * @file: open file to map flags to AppArmor permissions
  163. *
  164. * Returns: apparmor permission set for the file
  165. */
  166. static inline u32 aa_map_file_to_perms(struct file *file)
  167. {
  168. int flags = file->f_flags;
  169. u32 perms = 0;
  170. if (file->f_mode & FMODE_WRITE)
  171. perms |= MAY_WRITE;
  172. if (file->f_mode & FMODE_READ)
  173. perms |= MAY_READ;
  174. if ((flags & O_APPEND) && (perms & MAY_WRITE))
  175. perms = (perms & ~MAY_WRITE) | MAY_APPEND;
  176. /* trunc implies write permission */
  177. if (flags & O_TRUNC)
  178. perms |= MAY_WRITE;
  179. if (flags & O_CREAT)
  180. perms |= AA_MAY_CREATE;
  181. return perms;
  182. }
  183. #endif /* __AA_FILE_H */