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