1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor lib definitions 6 * 7 * 2017 Canonical Ltd. 8 */ 9 10 #ifndef __AA_LIB_H 11 #define __AA_LIB_H 12 13 #include <linux/slab.h> 14 #include <linux/fs.h> 15 #include <linux/lsm_hooks.h> 16 17 #include "match.h" 18 19 extern struct aa_dfa *stacksplitdfa; 20 21 /* 22 * split individual debug cases out in preparation for finer grained 23 * debug controls in the future. 24 */ 25 #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args) 26 27 #define DEBUG_NONE 0 28 #define DEBUG_LABEL_ABS_ROOT 1 29 #define DEBUG_LABEL 2 30 #define DEBUG_DOMAIN 4 31 #define DEBUG_POLICY 8 32 #define DEBUG_INTERFACE 0x10 33 #define DEBUG_UNPACK 0x20 34 #define DEBUG_TAGS 0x40 35 36 #define DEBUG_ALL 0x7f /* update if new DEBUG_X added */ 37 #define DEBUG_PARSE_ERROR (-1) 38 39 #define DEBUG_ON (aa_g_debug != DEBUG_NONE) 40 #define DEBUG_ABS_ROOT (aa_g_debug & DEBUG_LABEL_ABS_ROOT) 41 42 #define AA_DEBUG(opt, fmt, args...) \ 43 do { \ 44 if (aa_g_debug & opt) \ 45 pr_warn_ratelimited("%s: " fmt, __func__, ##args); \ 46 } while (0) 47 #define AA_DEBUG_LABEL(LAB, X, fmt, args...) \ 48 do { \ 49 if ((LAB)->flags & FLAG_DEBUG1) \ 50 AA_DEBUG(X, fmt, ##args); \ 51 } while (0) 52 53 #define AA_DEBUG_PROFILE(PROF, X, fmt...) AA_DEBUG_LABEL(&(PROF)->label, X, ##fmt) 54 55 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X) 56 57 #define AA_BUG(X, args...) \ 58 do { \ 59 _Pragma("GCC diagnostic ignored \"-Wformat-zero-length\""); \ 60 AA_BUG_FMT((X), "" args); \ 61 _Pragma("GCC diagnostic warning \"-Wformat-zero-length\""); \ 62 } while (0) 63 #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS 64 #define AA_BUG_FMT(X, fmt, args...) \ 65 WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args) 66 #else 67 #define AA_BUG_FMT(X, fmt, args...) \ 68 do { \ 69 BUILD_BUG_ON_INVALID(X); \ 70 no_printk(fmt, ##args); \ 71 } while (0) 72 #endif 73 74 int aa_parse_debug_params(const char *str); 75 int aa_print_debug_params(char *buffer); 76 77 #define AA_ERROR(fmt, args...) \ 78 pr_err_ratelimited("AppArmor: " fmt, ##args) 79 80 /* Flag indicating whether initialization completed */ 81 extern int apparmor_initialized; 82 83 /* semantic split of scope and view */ 84 #define aa_in_scope(SUBJ, OBJ) \ 85 aa_ns_visible(SUBJ, OBJ, false) 86 87 #define aa_in_view(SUBJ, OBJ) \ 88 aa_ns_visible(SUBJ, OBJ, true) 89 90 #define label_for_each_in_scope(I, NS, L, P) \ 91 label_for_each_in_ns(I, NS, L, P) 92 93 #define fn_for_each_in_scope(L, P, FN) \ 94 fn_for_each_in_ns(L, P, FN) 95 96 /* fn's in lib */ 97 const char *skipn_spaces(const char *str, size_t n); 98 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, 99 size_t *ns_len); 100 void aa_info_message(const char *str); 101 102 /* Security blob offsets */ 103 extern struct lsm_blob_sizes apparmor_blob_sizes; 104 105 /** 106 * aa_strneq - compare null terminated @str to a non null terminated substring 107 * @str: a null terminated string 108 * @sub: a substring, not necessarily null terminated 109 * @len: length of @sub to compare 110 * 111 * The @str string must be full consumed for this to be considered a match 112 */ 113 static inline bool aa_strneq(const char *str, const char *sub, int len) 114 { 115 return !strncmp(str, sub, len) && !str[len]; 116 } 117 118 /** 119 * aa_dfa_null_transition - step to next state after null character 120 * @dfa: the dfa to match against 121 * @start: the state of the dfa to start matching in 122 * 123 * aa_dfa_null_transition transitions to the next state after a null 124 * character which is not used in standard matching and is only 125 * used to separate pairs. 126 */ 127 static inline aa_state_t aa_dfa_null_transition(struct aa_dfa *dfa, 128 aa_state_t start) 129 { 130 /* the null transition only needs the string's null terminator byte */ 131 return aa_dfa_next(dfa, start, 0); 132 } 133 134 static inline bool path_mediated_fs(struct dentry *dentry) 135 { 136 return !(dentry->d_sb->s_flags & SB_NOUSER); 137 } 138 139 struct aa_str_table_ent { 140 int count; 141 int size; 142 char *strs; 143 }; 144 145 struct aa_str_table { 146 int size; 147 struct aa_str_table_ent *table; 148 }; 149 150 bool aa_resize_str_table(struct aa_str_table *t, int newsize, gfp_t gfp); 151 void aa_destroy_str_table(struct aa_str_table *table); 152 153 struct counted_str { 154 struct kref count; 155 char name[]; 156 }; 157 158 #define str_to_counted(str) \ 159 ((struct counted_str *)(str - offsetof(struct counted_str, name))) 160 161 #define __counted /* atm just a notation */ 162 163 void aa_str_kref(struct kref *kref); 164 char *aa_str_alloc(int size, gfp_t gfp); 165 166 167 static inline __counted char *aa_get_str(__counted char *str) 168 { 169 if (str) 170 kref_get(&(str_to_counted(str)->count)); 171 172 return str; 173 } 174 175 static inline void aa_put_str(__counted char *str) 176 { 177 if (str) 178 kref_put(&str_to_counted(str)->count, aa_str_kref); 179 } 180 181 182 /* struct aa_policy - common part of both namespaces and profiles 183 * @name: name of the object 184 * @hname - The hierarchical name 185 * @list: list policy object is on 186 * @profiles: head of the profiles list contained in the object 187 */ 188 struct aa_policy { 189 const char *name; 190 __counted char *hname; 191 struct list_head list; 192 struct list_head profiles; 193 }; 194 195 /** 196 * basename - find the last component of an hname 197 * @hname: hname to find the base profile name component of (NOT NULL) 198 * 199 * Returns: the tail (base profile name) name component of an hname 200 */ 201 static inline const char *basename(const char *hname) 202 { 203 char *split; 204 205 hname = strim((char *)hname); 206 for (split = strstr(hname, "//"); split; split = strstr(hname, "//")) 207 hname = split + 2; 208 209 return hname; 210 } 211 212 /** 213 * __policy_find - find a policy by @name on a policy list 214 * @head: list to search (NOT NULL) 215 * @name: name to search for (NOT NULL) 216 * 217 * Requires: rcu_read_lock be held 218 * 219 * Returns: unrefcounted policy that match @name or NULL if not found 220 */ 221 static inline struct aa_policy *__policy_find(struct list_head *head, 222 const char *name) 223 { 224 struct aa_policy *policy; 225 226 list_for_each_entry_rcu(policy, head, list) { 227 if (!strcmp(policy->name, name)) 228 return policy; 229 } 230 return NULL; 231 } 232 233 /** 234 * __policy_strn_find - find a policy that's name matches @len chars of @str 235 * @head: list to search (NOT NULL) 236 * @str: string to search for (NOT NULL) 237 * @len: length of match required 238 * 239 * Requires: rcu_read_lock be held 240 * 241 * Returns: unrefcounted policy that match @str or NULL if not found 242 * 243 * if @len == strlen(@strlen) then this is equiv to __policy_find 244 * other wise it allows searching for policy by a partial match of name 245 */ 246 static inline struct aa_policy *__policy_strn_find(struct list_head *head, 247 const char *str, int len) 248 { 249 struct aa_policy *policy; 250 251 list_for_each_entry_rcu(policy, head, list) { 252 if (aa_strneq(policy->name, str, len)) 253 return policy; 254 } 255 256 return NULL; 257 } 258 259 bool aa_policy_init(struct aa_policy *policy, const char *prefix, 260 const char *name, gfp_t gfp); 261 void aa_policy_destroy(struct aa_policy *policy); 262 263 264 /* 265 * fn_label_build - abstract out the build of a label transition 266 * @L: label the transition is being computed for 267 * @P: profile parameter derived from L by this macro, can be passed to FN 268 * @GFP: memory allocation type to use 269 * @FN: fn to call for each profile transition. @P is set to the profile 270 * 271 * Returns: new label on success 272 * ERR_PTR if build @FN fails 273 * NULL if label_build fails due to low memory conditions 274 * 275 * @FN must return a label or ERR_PTR on failure. NULL is not allowed 276 */ 277 #define fn_label_build(L, P, GFP, FN) \ 278 ({ \ 279 __label__ __do_cleanup, __done; \ 280 struct aa_label *__new_; \ 281 \ 282 if ((L)->size > 1) { \ 283 /* TODO: add cache of transitions already done */ \ 284 struct label_it __i; \ 285 int __j, __k, __count; \ 286 DEFINE_VEC(label, __lvec); \ 287 DEFINE_VEC(profile, __pvec); \ 288 if (vec_setup(label, __lvec, (L)->size, (GFP))) { \ 289 __new_ = NULL; \ 290 goto __done; \ 291 } \ 292 __j = 0; \ 293 label_for_each(__i, (L), (P)) { \ 294 __new_ = (FN); \ 295 AA_BUG(!__new_); \ 296 if (IS_ERR(__new_)) \ 297 goto __do_cleanup; \ 298 __lvec[__j++] = __new_; \ 299 } \ 300 for (__j = __count = 0; __j < (L)->size; __j++) \ 301 __count += __lvec[__j]->size; \ 302 if (!vec_setup(profile, __pvec, __count, (GFP))) { \ 303 for (__j = __k = 0; __j < (L)->size; __j++) { \ 304 label_for_each(__i, __lvec[__j], (P)) \ 305 __pvec[__k++] = aa_get_profile(P); \ 306 } \ 307 __count -= aa_vec_unique(__pvec, __count, 0); \ 308 if (__count > 1) { \ 309 __new_ = aa_vec_find_or_create_label(__pvec,\ 310 __count, (GFP)); \ 311 /* only fails if out of Mem */ \ 312 if (!__new_) \ 313 __new_ = NULL; \ 314 } else \ 315 __new_ = aa_get_label(&__pvec[0]->label); \ 316 vec_cleanup(profile, __pvec, __count); \ 317 } else \ 318 __new_ = NULL; \ 319 __do_cleanup: \ 320 vec_cleanup(label, __lvec, (L)->size); \ 321 } else { \ 322 (P) = labels_profile(L); \ 323 __new_ = (FN); \ 324 } \ 325 __done: \ 326 if (!__new_) \ 327 AA_DEBUG(DEBUG_LABEL, "label build failed\n"); \ 328 (__new_); \ 329 }) 330 331 332 #define __fn_build_in_scope(NS, P, NS_FN, OTHER_FN) \ 333 ({ \ 334 struct aa_label *__new; \ 335 if ((P)->ns != (NS)) \ 336 __new = (OTHER_FN); \ 337 else \ 338 __new = (NS_FN); \ 339 (__new); \ 340 }) 341 342 #define fn_label_build_in_scope(L, P, GFP, NS_FN, OTHER_FN) \ 343 ({ \ 344 fn_label_build((L), (P), (GFP), \ 345 __fn_build_in_scope(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \ 346 }) 347 348 #endif /* __AA_LIB_H */ 349