1 /* 2 * AppArmor security module 3 * 4 * This file contains AppArmor lib definitions 5 * 6 * 2017 Canonical Ltd. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation, version 2 of the 11 * License. 12 */ 13 14 #ifndef __AA_LIB_H 15 #define __AA_LIB_H 16 17 #include <linux/slab.h> 18 #include <linux/fs.h> 19 20 #include "match.h" 21 22 /* Provide our own test for whether a write lock is held for asserts 23 * this is because on none SMP systems write_can_lock will always 24 * resolve to true, which is what you want for code making decisions 25 * based on it, but wrong for asserts checking that the lock is held 26 */ 27 #ifdef CONFIG_SMP 28 #define write_is_locked(X) !write_can_lock(X) 29 #else 30 #define write_is_locked(X) (1) 31 #endif /* CONFIG_SMP */ 32 33 /* 34 * DEBUG remains global (no per profile flag) since it is mostly used in sysctl 35 * which is not related to profile accesses. 36 */ 37 38 #define DEBUG_ON (aa_g_debug) 39 #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args) 40 #define AA_DEBUG(fmt, args...) \ 41 do { \ 42 if (DEBUG_ON) \ 43 pr_debug_ratelimited("AppArmor: " fmt, ##args); \ 44 } while (0) 45 46 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X) 47 48 #define AA_BUG(X, args...) AA_BUG_FMT((X), "" args) 49 #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS 50 #define AA_BUG_FMT(X, fmt, args...) \ 51 WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args) 52 #else 53 #define AA_BUG_FMT(X, fmt, args...) 54 #endif 55 56 #define AA_ERROR(fmt, args...) \ 57 pr_err_ratelimited("AppArmor: " fmt, ##args) 58 59 /* Flag indicating whether initialization completed */ 60 extern int apparmor_initialized __initdata; 61 62 /* fn's in lib */ 63 char *aa_split_fqname(char *args, char **ns_name); 64 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, 65 size_t *ns_len); 66 void aa_info_message(const char *str); 67 void *__aa_kvmalloc(size_t size, gfp_t flags); 68 69 static inline void *kvmalloc(size_t size) 70 { 71 return __aa_kvmalloc(size, 0); 72 } 73 74 static inline void *kvzalloc(size_t size) 75 { 76 return __aa_kvmalloc(size, __GFP_ZERO); 77 } 78 79 /* returns 0 if kref not incremented */ 80 static inline int kref_get_not0(struct kref *kref) 81 { 82 return atomic_inc_not_zero(&kref->refcount); 83 } 84 85 /** 86 * aa_strneq - compare null terminated @str to a non null terminated substring 87 * @str: a null terminated string 88 * @sub: a substring, not necessarily null terminated 89 * @len: length of @sub to compare 90 * 91 * The @str string must be full consumed for this to be considered a match 92 */ 93 static inline bool aa_strneq(const char *str, const char *sub, int len) 94 { 95 return !strncmp(str, sub, len) && !str[len]; 96 } 97 98 /** 99 * aa_dfa_null_transition - step to next state after null character 100 * @dfa: the dfa to match against 101 * @start: the state of the dfa to start matching in 102 * 103 * aa_dfa_null_transition transitions to the next state after a null 104 * character which is not used in standard matching and is only 105 * used to separate pairs. 106 */ 107 static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa, 108 unsigned int start) 109 { 110 /* the null transition only needs the string's null terminator byte */ 111 return aa_dfa_next(dfa, start, 0); 112 } 113 114 static inline bool path_mediated_fs(struct dentry *dentry) 115 { 116 return !(dentry->d_sb->s_flags & MS_NOUSER); 117 } 118 119 /* struct aa_policy - common part of both namespaces and profiles 120 * @name: name of the object 121 * @hname - The hierarchical name 122 * @list: list policy object is on 123 * @profiles: head of the profiles list contained in the object 124 */ 125 struct aa_policy { 126 char *name; 127 char *hname; 128 struct list_head list; 129 struct list_head profiles; 130 }; 131 132 /** 133 * hname_tail - find the last component of an hname 134 * @name: hname to find the base profile name component of (NOT NULL) 135 * 136 * Returns: the tail (base profile name) name component of an hname 137 */ 138 static inline const char *hname_tail(const char *hname) 139 { 140 char *split; 141 142 hname = strim((char *)hname); 143 for (split = strstr(hname, "//"); split; split = strstr(hname, "//")) 144 hname = split + 2; 145 146 return hname; 147 } 148 149 /** 150 * __policy_find - find a policy by @name on a policy list 151 * @head: list to search (NOT NULL) 152 * @name: name to search for (NOT NULL) 153 * 154 * Requires: rcu_read_lock be held 155 * 156 * Returns: unrefcounted policy that match @name or NULL if not found 157 */ 158 static inline struct aa_policy *__policy_find(struct list_head *head, 159 const char *name) 160 { 161 struct aa_policy *policy; 162 163 list_for_each_entry_rcu(policy, head, list) { 164 if (!strcmp(policy->name, name)) 165 return policy; 166 } 167 return NULL; 168 } 169 170 /** 171 * __policy_strn_find - find a policy that's name matches @len chars of @str 172 * @head: list to search (NOT NULL) 173 * @str: string to search for (NOT NULL) 174 * @len: length of match required 175 * 176 * Requires: rcu_read_lock be held 177 * 178 * Returns: unrefcounted policy that match @str or NULL if not found 179 * 180 * if @len == strlen(@strlen) then this is equiv to __policy_find 181 * other wise it allows searching for policy by a partial match of name 182 */ 183 static inline struct aa_policy *__policy_strn_find(struct list_head *head, 184 const char *str, int len) 185 { 186 struct aa_policy *policy; 187 188 list_for_each_entry_rcu(policy, head, list) { 189 if (aa_strneq(policy->name, str, len)) 190 return policy; 191 } 192 193 return NULL; 194 } 195 196 bool aa_policy_init(struct aa_policy *policy, const char *prefix, 197 const char *name); 198 void aa_policy_destroy(struct aa_policy *policy); 199 200 #endif /* AA_LIB_H */ 201