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 AA_DEBUG(fmt, args...) \
41 pr_debug_ratelimited("AppArmor: " fmt, ##args); \
44 #define AA_ERROR(fmt, args...) \
45 pr_err_ratelimited("AppArmor: " fmt, ##args)
47 /* Flag indicating whether initialization completed */
48 extern int apparmor_initialized __initdata;
51 char *aa_split_fqname(char *args, char **ns_name);
52 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
54 void aa_info_message(const char *str);
55 void *__aa_kvmalloc(size_t size, gfp_t flags);
57 static inline void *kvmalloc(size_t size)
59 return __aa_kvmalloc(size, 0);
62 static inline void *kvzalloc(size_t size)
64 return __aa_kvmalloc(size, __GFP_ZERO);
67 /* returns 0 if kref not incremented */
68 static inline int kref_get_not0(struct kref *kref)
70 return atomic_inc_not_zero(&kref->refcount);
74 * aa_strneq - compare null terminated @str to a non null terminated substring
75 * @str: a null terminated string
76 * @sub: a substring, not necessarily null terminated
77 * @len: length of @sub to compare
79 * The @str string must be full consumed for this to be considered a match
81 static inline bool aa_strneq(const char *str, const char *sub, int len)
83 return !strncmp(str, sub, len) && !str[len];
87 * aa_dfa_null_transition - step to next state after null character
88 * @dfa: the dfa to match against
89 * @start: the state of the dfa to start matching in
91 * aa_dfa_null_transition transitions to the next state after a null
92 * character which is not used in standard matching and is only
93 * used to separate pairs.
95 static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa,
98 /* the null transition only needs the string's null terminator byte */
99 return aa_dfa_next(dfa, start, 0);
102 static inline bool mediated_filesystem(struct dentry *dentry)
104 return !(dentry->d_sb->s_flags & MS_NOUSER);
107 /* struct aa_policy - common part of both namespaces and profiles
108 * @name: name of the object
109 * @hname - The hierarchical name
110 * @list: list policy object is on
111 * @profiles: head of the profiles list contained in the object
116 struct list_head list;
117 struct list_head profiles;
121 * hname_tail - find the last component of an hname
122 * @name: hname to find the base profile name component of (NOT NULL)
124 * Returns: the tail (base profile name) name component of an hname
126 static inline const char *hname_tail(const char *hname)
130 hname = strim((char *)hname);
131 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
138 * __policy_find - find a policy by @name on a policy list
139 * @head: list to search (NOT NULL)
140 * @name: name to search for (NOT NULL)
142 * Requires: rcu_read_lock be held
144 * Returns: unrefcounted policy that match @name or NULL if not found
146 static inline struct aa_policy *__policy_find(struct list_head *head,
149 struct aa_policy *policy;
151 list_for_each_entry_rcu(policy, head, list) {
152 if (!strcmp(policy->name, name))
159 * __policy_strn_find - find a policy that's name matches @len chars of @str
160 * @head: list to search (NOT NULL)
161 * @str: string to search for (NOT NULL)
162 * @len: length of match required
164 * Requires: rcu_read_lock be held
166 * Returns: unrefcounted policy that match @str or NULL if not found
168 * if @len == strlen(@strlen) then this is equiv to __policy_find
169 * other wise it allows searching for policy by a partial match of name
171 static inline struct aa_policy *__policy_strn_find(struct list_head *head,
172 const char *str, int len)
174 struct aa_policy *policy;
176 list_for_each_entry_rcu(policy, head, list) {
177 if (aa_strneq(policy->name, str, len))
184 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
186 void aa_policy_destroy(struct aa_policy *policy);
188 #endif /* AA_LIB_H */