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 enum reftype { 106 REF_NS, 107 REF_PROXY, 108 REF_RAWDATA, 109 }; 110 111 /* common reference count used by data the shows up in aafs */ 112 struct aa_common_ref { 113 struct kref count; 114 enum reftype reftype; 115 }; 116 117 /** 118 * aa_strneq - compare null terminated @str to a non null terminated substring 119 * @str: a null terminated string 120 * @sub: a substring, not necessarily null terminated 121 * @len: length of @sub to compare 122 * 123 * The @str string must be full consumed for this to be considered a match 124 */ 125 static inline bool aa_strneq(const char *str, const char *sub, int len) 126 { 127 return !strncmp(str, sub, len) && !str[len]; 128 } 129 130 /** 131 * aa_dfa_null_transition - step to next state after null character 132 * @dfa: the dfa to match against 133 * @start: the state of the dfa to start matching in 134 * 135 * aa_dfa_null_transition transitions to the next state after a null 136 * character which is not used in standard matching and is only 137 * used to separate pairs. 138 */ 139 static inline aa_state_t aa_dfa_null_transition(struct aa_dfa *dfa, 140 aa_state_t start) 141 { 142 /* the null transition only needs the string's null terminator byte */ 143 return aa_dfa_next(dfa, start, 0); 144 } 145 146 static inline bool path_mediated_fs(struct dentry *dentry) 147 { 148 return !(dentry->d_sb->s_flags & SB_NOUSER); 149 } 150 151 struct aa_str_table_ent { 152 int count; 153 int size; 154 char *strs; 155 }; 156 157 struct aa_str_table { 158 int size; 159 struct aa_str_table_ent *table; 160 }; 161 162 bool aa_resize_str_table(struct aa_str_table *t, int newsize, gfp_t gfp); 163 void aa_destroy_str_table(struct aa_str_table *table); 164 165 struct counted_str { 166 struct kref count; 167 char name[]; 168 }; 169 170 #define str_to_counted(str) \ 171 ((struct counted_str *)(str - offsetof(struct counted_str, name))) 172 173 #define __counted /* atm just a notation */ 174 175 void aa_str_kref(struct kref *kref); 176 char *aa_str_alloc(int size, gfp_t gfp); 177 178 179 static inline __counted char *aa_get_str(__counted char *str) 180 { 181 if (str) 182 kref_get(&(str_to_counted(str)->count)); 183 184 return str; 185 } 186 187 static inline void aa_put_str(__counted char *str) 188 { 189 if (str) 190 kref_put(&str_to_counted(str)->count, aa_str_kref); 191 } 192 193 194 /* struct aa_policy - common part of both namespaces and profiles 195 * @name: name of the object 196 * @hname - The hierarchical name 197 * @list: list policy object is on 198 * @profiles: head of the profiles list contained in the object 199 */ 200 struct aa_policy { 201 const char *name; 202 __counted char *hname; 203 struct list_head list; 204 struct list_head profiles; 205 }; 206 207 /** 208 * basename - find the last component of an hname 209 * @hname: hname to find the base profile name component of (NOT NULL) 210 * 211 * Returns: the tail (base profile name) name component of an hname 212 */ 213 static inline const char *basename(const char *hname) 214 { 215 char *split; 216 217 hname = strim((char *)hname); 218 for (split = strstr(hname, "//"); split; split = strstr(hname, "//")) 219 hname = split + 2; 220 221 return hname; 222 } 223 224 /** 225 * __policy_find - find a policy by @name on a policy list 226 * @head: list to search (NOT NULL) 227 * @name: name to search for (NOT NULL) 228 * 229 * Requires: rcu_read_lock be held 230 * 231 * Returns: unrefcounted policy that match @name or NULL if not found 232 */ 233 static inline struct aa_policy *__policy_find(struct list_head *head, 234 const char *name) 235 { 236 struct aa_policy *policy; 237 238 list_for_each_entry_rcu(policy, head, list) { 239 if (!strcmp(policy->name, name)) 240 return policy; 241 } 242 return NULL; 243 } 244 245 /** 246 * __policy_strn_find - find a policy that's name matches @len chars of @str 247 * @head: list to search (NOT NULL) 248 * @str: string to search for (NOT NULL) 249 * @len: length of match required 250 * 251 * Requires: rcu_read_lock be held 252 * 253 * Returns: unrefcounted policy that match @str or NULL if not found 254 * 255 * if @len == strlen(@strlen) then this is equiv to __policy_find 256 * other wise it allows searching for policy by a partial match of name 257 */ 258 static inline struct aa_policy *__policy_strn_find(struct list_head *head, 259 const char *str, int len) 260 { 261 struct aa_policy *policy; 262 263 list_for_each_entry_rcu(policy, head, list) { 264 if (aa_strneq(policy->name, str, len)) 265 return policy; 266 } 267 268 return NULL; 269 } 270 271 bool aa_policy_init(struct aa_policy *policy, const char *prefix, 272 const char *name, gfp_t gfp); 273 void aa_policy_destroy(struct aa_policy *policy); 274 275 276 /* 277 * fn_label_build - abstract out the build of a label transition 278 * @L: label the transition is being computed for 279 * @P: profile parameter derived from L by this macro, can be passed to FN 280 * @GFP: memory allocation type to use 281 * @FN: fn to call for each profile transition. @P is set to the profile 282 * 283 * Returns: new label on success 284 * ERR_PTR if build @FN fails 285 * NULL if label_build fails due to low memory conditions 286 * 287 * @FN must return a label or ERR_PTR on failure. NULL is not allowed 288 */ 289 #define fn_label_build(L, P, GFP, FN) \ 290 ({ \ 291 __label__ __do_cleanup, __done; \ 292 struct aa_label *__new_; \ 293 \ 294 if ((L)->size > 1) { \ 295 /* TODO: add cache of transitions already done */ \ 296 struct label_it __i; \ 297 int __j, __k, __count; \ 298 DEFINE_VEC(label, __lvec); \ 299 DEFINE_VEC(profile, __pvec); \ 300 if (vec_setup(label, __lvec, (L)->size, (GFP))) { \ 301 __new_ = NULL; \ 302 goto __done; \ 303 } \ 304 __j = 0; \ 305 label_for_each(__i, (L), (P)) { \ 306 __new_ = (FN); \ 307 AA_BUG(!__new_); \ 308 if (IS_ERR(__new_)) \ 309 goto __do_cleanup; \ 310 __lvec[__j++] = __new_; \ 311 } \ 312 for (__j = __count = 0; __j < (L)->size; __j++) \ 313 __count += __lvec[__j]->size; \ 314 if (!vec_setup(profile, __pvec, __count, (GFP))) { \ 315 for (__j = __k = 0; __j < (L)->size; __j++) { \ 316 label_for_each(__i, __lvec[__j], (P)) \ 317 __pvec[__k++] = aa_get_profile(P); \ 318 } \ 319 __count -= aa_vec_unique(__pvec, __count, 0); \ 320 if (__count > 1) { \ 321 __new_ = aa_vec_find_or_create_label(__pvec,\ 322 __count, (GFP)); \ 323 /* only fails if out of Mem */ \ 324 if (!__new_) \ 325 __new_ = NULL; \ 326 } else \ 327 __new_ = aa_get_label(&__pvec[0]->label); \ 328 vec_cleanup(profile, __pvec, __count); \ 329 } else \ 330 __new_ = NULL; \ 331 __do_cleanup: \ 332 vec_cleanup(label, __lvec, (L)->size); \ 333 } else { \ 334 (P) = labels_profile(L); \ 335 __new_ = (FN); \ 336 } \ 337 __done: \ 338 if (!__new_) \ 339 AA_DEBUG(DEBUG_LABEL, "label build failed\n"); \ 340 (__new_); \ 341 }) 342 343 344 #define __fn_build_in_scope(NS, P, NS_FN, OTHER_FN) \ 345 ({ \ 346 struct aa_label *__new; \ 347 if ((P)->ns != (NS)) \ 348 __new = (OTHER_FN); \ 349 else \ 350 __new = (NS_FN); \ 351 (__new); \ 352 }) 353 354 #define fn_label_build_in_scope(L, P, GFP, NS_FN, OTHER_FN) \ 355 ({ \ 356 fn_label_build((L), (P), (GFP), \ 357 __fn_build_in_scope(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \ 358 }) 359 360 #endif /* __AA_LIB_H */ 361