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 15 #ifndef __AA_FILE_H 16 #define __AA_FILE_H 17 18 #include "domain.h" 19 #include "match.h" 20 #include "perms.h" 21 22 struct aa_profile; 23 struct path; 24 25 26 #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\ 27 AA_MAY_CREATE | AA_MAY_DELETE | \ 28 AA_MAY_META_READ | AA_MAY_META_WRITE | \ 29 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \ 30 AA_EXEC_MMAP | AA_MAY_LINK) 31 32 /* struct aa_file_ctx - the AppArmor context the file was opened in 33 * @perms: the permission the file was opened with 34 * 35 * The file_ctx could currently be directly stored in file->f_security 36 * as the profile reference is now stored in the f_cred. However the 37 * ctx struct will expand in the future so we keep the struct. 38 */ 39 struct aa_file_ctx { 40 u16 allow; 41 }; 42 43 /** 44 * aa_alloc_file_context - allocate file_ctx 45 * @gfp: gfp flags for allocation 46 * 47 * Returns: file_ctx or NULL on failure 48 */ 49 static inline struct aa_file_ctx *aa_alloc_file_context(gfp_t gfp) 50 { 51 return kzalloc(sizeof(struct aa_file_ctx), gfp); 52 } 53 54 /** 55 * aa_free_file_context - free a file_ctx 56 * @ctx: file_ctx to free (MAYBE_NULL) 57 */ 58 static inline void aa_free_file_context(struct aa_file_ctx *ctx) 59 { 60 if (ctx) 61 kzfree(ctx); 62 } 63 64 /* 65 * The xindex is broken into 3 parts 66 * - index - an index into either the exec name table or the variable table 67 * - exec type - which determines how the executable name and index are used 68 * - flags - which modify how the destination name is applied 69 */ 70 #define AA_X_INDEX_MASK 0x03ff 71 72 #define AA_X_TYPE_MASK 0x0c00 73 #define AA_X_TYPE_SHIFT 10 74 #define AA_X_NONE 0x0000 75 #define AA_X_NAME 0x0400 /* use executable name px */ 76 #define AA_X_TABLE 0x0800 /* use a specified name ->n# */ 77 78 #define AA_X_UNSAFE 0x1000 79 #define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */ 80 #define AA_X_INHERIT 0x4000 81 #define AA_X_UNCONFINED 0x8000 82 83 /* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */ 84 #define AA_SECURE_X_NEEDED 0x8000 85 86 /* need to make conditional which ones are being set */ 87 struct path_cond { 88 kuid_t uid; 89 umode_t mode; 90 }; 91 92 /* struct file_perms - file permission 93 * @allow: mask of permissions that are allowed 94 * @audit: mask of permissions to force an audit message for 95 * @quiet: mask of permissions to quiet audit messages for 96 * @kill: mask of permissions that when matched will kill the task 97 * @xindex: exec transition index if @allow contains MAY_EXEC 98 * 99 * The @audit and @queit mask should be mutually exclusive. 100 */ 101 struct file_perms { 102 u32 allow; 103 u32 audit; 104 u32 quiet; 105 u32 kill; 106 u16 xindex; 107 }; 108 109 extern struct file_perms nullperms; 110 111 #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill) 112 113 /* FIXME: split perms from dfa and match this to description 114 * also add delegation info. 115 */ 116 static inline u16 dfa_map_xindex(u16 mask) 117 { 118 u16 old_index = (mask >> 10) & 0xf; 119 u16 index = 0; 120 121 if (mask & 0x100) 122 index |= AA_X_UNSAFE; 123 if (mask & 0x200) 124 index |= AA_X_INHERIT; 125 if (mask & 0x80) 126 index |= AA_X_UNCONFINED; 127 128 if (old_index == 1) { 129 index |= AA_X_UNCONFINED; 130 } else if (old_index == 2) { 131 index |= AA_X_NAME; 132 } else if (old_index == 3) { 133 index |= AA_X_NAME | AA_X_CHILD; 134 } else if (old_index) { 135 index |= AA_X_TABLE; 136 index |= old_index - 4; 137 } 138 139 return index; 140 } 141 142 /* 143 * map old dfa inline permissions to new format 144 */ 145 #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \ 146 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 147 #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f) 148 #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f) 149 #define dfa_user_xindex(dfa, state) \ 150 (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff)) 151 152 #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \ 153 0x7f) | \ 154 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 155 #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f) 156 #define dfa_other_quiet(dfa, state) \ 157 ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f) 158 #define dfa_other_xindex(dfa, state) \ 159 dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff) 160 161 int aa_audit_file(struct aa_profile *profile, struct file_perms *perms, 162 const char *op, u32 request, const char *name, 163 const char *target, kuid_t ouid, const char *info, int error); 164 165 /** 166 * struct aa_file_rules - components used for file rule permissions 167 * @dfa: dfa to match path names and conditionals against 168 * @perms: permission table indexed by the matched state accept entry of @dfa 169 * @trans: transition table for indexed by named x transitions 170 * 171 * File permission are determined by matching a path against @dfa and then 172 * then using the value of the accept entry for the matching state as 173 * an index into @perms. If a named exec transition is required it is 174 * looked up in the transition table. 175 */ 176 struct aa_file_rules { 177 unsigned int start; 178 struct aa_dfa *dfa; 179 /* struct perms perms; */ 180 struct aa_domain trans; 181 /* TODO: add delegate table */ 182 }; 183 184 unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start, 185 const char *name, struct path_cond *cond, 186 struct file_perms *perms); 187 188 int aa_path_perm(const char *op, struct aa_profile *profile, 189 const struct path *path, int flags, u32 request, 190 struct path_cond *cond); 191 192 int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry, 193 const struct path *new_dir, struct dentry *new_dentry); 194 195 int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file, 196 u32 request); 197 198 static inline void aa_free_file_rules(struct aa_file_rules *rules) 199 { 200 aa_put_dfa(rules->dfa); 201 aa_free_domain_entries(&rules->trans); 202 } 203 204 /** 205 * aa_map_file_perms - map file flags to AppArmor permissions 206 * @file: open file to map flags to AppArmor permissions 207 * 208 * Returns: apparmor permission set for the file 209 */ 210 static inline u32 aa_map_file_to_perms(struct file *file) 211 { 212 int flags = file->f_flags; 213 u32 perms = 0; 214 215 if (file->f_mode & FMODE_WRITE) 216 perms |= MAY_WRITE; 217 if (file->f_mode & FMODE_READ) 218 perms |= MAY_READ; 219 220 if ((flags & O_APPEND) && (perms & MAY_WRITE)) 221 perms = (perms & ~MAY_WRITE) | MAY_APPEND; 222 /* trunc implies write permission */ 223 if (flags & O_TRUNC) 224 perms |= MAY_WRITE; 225 if (flags & O_CREAT) 226 perms |= AA_MAY_CREATE; 227 228 return perms; 229 } 230 231 #endif /* __AA_FILE_H */ 232