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