1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains basic common functions used in AppArmor
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #include <linux/ctype.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/vmalloc.h>
17 #include "include/audit.h"
18 #include "include/apparmor.h"
19 #include "include/lib.h"
20 #include "include/perms.h"
21 #include "include/policy.h"
23 struct aa_perms nullperms;
24 struct aa_perms allperms = { .allow = ALL_PERMS_MASK,
25 .quiet = ALL_PERMS_MASK,
26 .hide = ALL_PERMS_MASK };
29 * aa_free_str_table - free entries str table
30 * @t: the string table to free (MAYBE NULL)
32 void aa_free_str_table(struct aa_str_table *t)
40 for (i = 0; i < t->size; i++)
41 kfree_sensitive(t->table[i]);
42 kfree_sensitive(t->table);
49 * skipn_spaces - Removes leading whitespace from @str.
50 * @str: The string to be stripped.
51 * @n: length of str to parse, will stop at \0 if encountered before n
53 * Returns a pointer to the first non-whitespace character in @str.
54 * if all whitespace will return NULL
57 const char *skipn_spaces(const char *str, size_t n)
59 for (; n && isspace(*str); --n)
66 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
69 const char *end = fqname + n;
70 const char *name = skipn_spaces(fqname, n);
79 char *split = strnchr(&name[1], end - &name[1], ':');
80 *ns_name = skipn_spaces(&name[1], end - &name[1]);
84 *ns_len = split - *ns_name;
88 if (end - split > 1 && strncmp(split, "//", 2) == 0)
90 name = skipn_spaces(split, end - split);
92 /* a ns name without a following profile is allowed */
94 *ns_len = end - *ns_name;
97 if (name && *name == 0)
104 * aa_info_message - log a none profile related status message
105 * @str: message to log
107 void aa_info_message(const char *str)
110 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, NULL);
113 aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, NULL);
115 printk(KERN_INFO "AppArmor: %s\n", str);
118 __counted char *aa_str_alloc(int size, gfp_t gfp)
120 struct counted_str *str;
122 str = kmalloc(struct_size(str, name, size), gfp);
126 kref_init(&str->count);
130 void aa_str_kref(struct kref *kref)
132 kfree(container_of(kref, struct counted_str, count));
136 const char aa_file_perm_chrs[] = "xwracd km l ";
137 const char *aa_file_perm_names[] = {
180 * aa_perm_mask_to_str - convert a perm mask to its short string
181 * @str: character buffer to store string in (at least 10 characters)
182 * @str_size: size of the @str buffer
183 * @chrs: NUL-terminated character buffer of permission characters
184 * @mask: permission mask to convert
186 void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs, u32 mask)
188 unsigned int i, perm = 1;
189 size_t num_chrs = strlen(chrs);
191 for (i = 0; i < num_chrs; perm <<= 1, i++) {
193 /* Ensure that one byte is left for NUL-termination */
194 if (WARN_ON_ONCE(str_size <= 1))
204 void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
207 const char *fmt = "%s";
208 unsigned int i, perm = 1;
211 for (i = 0; i < 32; perm <<= 1, i++) {
213 audit_log_format(ab, fmt, names[i]);
222 void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
223 u32 chrsmask, const char * const *names, u32 namesmask)
227 audit_log_format(ab, "\"");
228 if ((mask & chrsmask) && chrs) {
229 aa_perm_mask_to_str(str, sizeof(str), chrs, mask & chrsmask);
231 audit_log_format(ab, "%s", str);
232 if (mask & namesmask)
233 audit_log_format(ab, " ");
235 if ((mask & namesmask) && names)
236 aa_audit_perm_names(ab, names, mask & namesmask);
237 audit_log_format(ab, "\"");
241 * aa_apply_modes_to_perms - apply namespace and profile flags to perms
242 * @profile: that perms where computed from
243 * @perms: perms to apply mode modifiers to
245 * TODO: split into profile and ns based flags for when accumulating perms
247 void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
249 switch (AUDIT_MODE(profile)) {
251 perms->audit = ALL_PERMS_MASK;
259 case AUDIT_QUIET_DENIED:
260 perms->quiet = ALL_PERMS_MASK;
264 if (KILL_MODE(profile))
265 perms->kill = ALL_PERMS_MASK;
266 else if (COMPLAIN_MODE(profile))
267 perms->complain = ALL_PERMS_MASK;
268 else if (USER_MODE(profile))
269 perms->prompt = ALL_PERMS_MASK;
272 void aa_profile_match_label(struct aa_profile *profile,
273 struct aa_ruleset *rules,
274 struct aa_label *label,
275 int type, u32 request, struct aa_perms *perms)
277 /* TODO: doesn't yet handle extended types */
280 state = aa_dfa_next(rules->policy->dfa,
281 rules->policy->start[AA_CLASS_LABEL],
283 aa_label_match(profile, rules, label, state, false, request, perms);
288 * aa_check_perms - do audit mode selection based on perms set
289 * @profile: profile being checked
290 * @perms: perms computed for the request
291 * @request: requested perms
292 * @ad: initialized audit structure (MAY BE NULL if not auditing)
293 * @cb: callback fn for type specific fields (MAY BE NULL)
295 * Returns: 0 if permission else error code
297 * Note: profile audit modes need to be set before calling by setting the
298 * perm masks appropriately.
300 * If not auditing then complain mode is not enabled and the
301 * error code will indicate whether there was an explicit deny
302 * with a positive value.
304 int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
305 u32 request, struct apparmor_audit_data *ad,
306 void (*cb)(struct audit_buffer *, void *))
309 u32 denied = request & (~perms->allow | perms->deny);
311 if (likely(!denied)) {
312 /* mask off perms that are not being force audited */
313 request &= perms->audit;
317 type = AUDIT_APPARMOR_AUDIT;
322 if (denied & perms->kill)
323 type = AUDIT_APPARMOR_KILL;
324 else if (denied == (denied & perms->complain))
325 type = AUDIT_APPARMOR_ALLOWED;
327 type = AUDIT_APPARMOR_DENIED;
329 if (denied == (denied & perms->hide))
332 denied &= ~perms->quiet;
338 ad->subj_label = &profile->label;
339 ad->request = request;
342 aa_audit_msg(type, ad, cb);
345 if (type == AUDIT_APPARMOR_ALLOWED)
353 * aa_policy_init - initialize a policy structure
354 * @policy: policy to initialize (NOT NULL)
355 * @prefix: prefix name if any is required. (MAYBE NULL)
356 * @name: name of the policy, init will make a copy of it (NOT NULL)
357 * @gfp: allocation mode
359 * Note: this fn creates a copy of strings passed in
361 * Returns: true if policy init successful
363 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
364 const char *name, gfp_t gfp)
368 /* freed by policy_free */
370 hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
372 sprintf(hname, "%s//%s", prefix, name);
374 hname = aa_str_alloc(strlen(name) + 1, gfp);
380 policy->hname = hname;
381 /* base.name is a substring of fqname */
382 policy->name = basename(policy->hname);
383 INIT_LIST_HEAD(&policy->list);
384 INIT_LIST_HEAD(&policy->profiles);
390 * aa_policy_destroy - free the elements referenced by @policy
391 * @policy: policy that is to have its elements freed (NOT NULL)
393 void aa_policy_destroy(struct aa_policy *policy)
395 AA_BUG(on_list_rcu(&policy->profiles));
396 AA_BUG(on_list_rcu(&policy->list));
398 /* don't free name as its a subset of hname */
399 aa_put_str(policy->hname);