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