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