2 * AppArmor security module
4 * This file contains AppArmor lib definitions
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
17 #include <linux/slab.h>
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
28 #define write_is_locked(X) !write_can_lock(X)
30 #define write_is_locked(X) (1)
31 #endif /* CONFIG_SMP */
34 * DEBUG remains global (no per profile flag) since it is mostly used in sysctl
35 * which is not related to profile accesses.
38 #define DEBUG_ON (aa_g_debug)
39 #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
40 #define AA_DEBUG(fmt, args...) \
43 pr_debug_ratelimited("AppArmor: " fmt, ##args); \
46 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
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)
53 #define AA_BUG_FMT(X, fmt, args...)
56 #define AA_ERROR(fmt, args...) \
57 pr_err_ratelimited("AppArmor: " fmt, ##args)
59 /* Flag indicating whether initialization completed */
60 extern int apparmor_initialized;
63 const char *skipn_spaces(const char *str, size_t n);
64 char *aa_split_fqname(char *args, char **ns_name);
65 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
67 void aa_info_message(const char *str);
70 * aa_strneq - compare null terminated @str to a non null terminated substring
71 * @str: a null terminated string
72 * @sub: a substring, not necessarily null terminated
73 * @len: length of @sub to compare
75 * The @str string must be full consumed for this to be considered a match
77 static inline bool aa_strneq(const char *str, const char *sub, int len)
79 return !strncmp(str, sub, len) && !str[len];
83 * aa_dfa_null_transition - step to next state after null character
84 * @dfa: the dfa to match against
85 * @start: the state of the dfa to start matching in
87 * aa_dfa_null_transition transitions to the next state after a null
88 * character which is not used in standard matching and is only
89 * used to separate pairs.
91 static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa,
94 /* the null transition only needs the string's null terminator byte */
95 return aa_dfa_next(dfa, start, 0);
98 static inline bool path_mediated_fs(struct dentry *dentry)
100 return !(dentry->d_sb->s_flags & MS_NOUSER);
109 #define str_to_counted(str) \
110 ((struct counted_str *)(str - offsetof(struct counted_str, name)))
112 #define __counted /* atm just a notation */
114 void aa_str_kref(struct kref *kref);
115 char *aa_str_alloc(int size, gfp_t gfp);
118 static inline __counted char *aa_get_str(__counted char *str)
121 kref_get(&(str_to_counted(str)->count));
126 static inline void aa_put_str(__counted char *str)
129 kref_put(&str_to_counted(str)->count, aa_str_kref);
133 /* struct aa_policy - common part of both namespaces and profiles
134 * @name: name of the object
135 * @hname - The hierarchical name
136 * @list: list policy object is on
137 * @profiles: head of the profiles list contained in the object
141 __counted char *hname;
142 struct list_head list;
143 struct list_head profiles;
147 * basename - find the last component of an hname
148 * @name: hname to find the base profile name component of (NOT NULL)
150 * Returns: the tail (base profile name) name component of an hname
152 static inline const char *basename(const char *hname)
156 hname = strim((char *)hname);
157 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
164 * __policy_find - find a policy by @name on a policy list
165 * @head: list to search (NOT NULL)
166 * @name: name to search for (NOT NULL)
168 * Requires: rcu_read_lock be held
170 * Returns: unrefcounted policy that match @name or NULL if not found
172 static inline struct aa_policy *__policy_find(struct list_head *head,
175 struct aa_policy *policy;
177 list_for_each_entry_rcu(policy, head, list) {
178 if (!strcmp(policy->name, name))
185 * __policy_strn_find - find a policy that's name matches @len chars of @str
186 * @head: list to search (NOT NULL)
187 * @str: string to search for (NOT NULL)
188 * @len: length of match required
190 * Requires: rcu_read_lock be held
192 * Returns: unrefcounted policy that match @str or NULL if not found
194 * if @len == strlen(@strlen) then this is equiv to __policy_find
195 * other wise it allows searching for policy by a partial match of name
197 static inline struct aa_policy *__policy_strn_find(struct list_head *head,
198 const char *str, int len)
200 struct aa_policy *policy;
202 list_for_each_entry_rcu(policy, head, list) {
203 if (aa_strneq(policy->name, str, len))
210 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
211 const char *name, gfp_t gfp);
212 void aa_policy_destroy(struct aa_policy *policy);
214 #endif /* AA_LIB_H */