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