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 * @str: 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);
48 * aa_split_fqname - split a fqname into a profile and namespace name
49 * @fqname: a full qualified name in namespace profile format (NOT NULL)
50 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
52 * Returns: profile name or NULL if one is not specified
54 * Split a namespace name from a profile name (see policy.c for naming
55 * description). If a portion of the name is missing it returns NULL for
58 * NOTE: may modify the @fqname string. The pointers returned point
59 * into the @fqname string.
61 char *aa_split_fqname(char *fqname, char **ns_name)
63 char *name = strim(fqname);
67 char *split = strchr(&name[1], ':');
68 *ns_name = skip_spaces(&name[1]);
70 /* overwrite ':' with \0 */
72 if (strncmp(split, "//", 2) == 0)
74 name = skip_spaces(split);
76 /* a ns name without a following profile is allowed */
79 if (name && *name == 0)
86 * skipn_spaces - Removes leading whitespace from @str.
87 * @str: The string to be stripped.
89 * Returns a pointer to the first non-whitespace character in @str.
90 * if all whitespace will return NULL
93 const char *skipn_spaces(const char *str, size_t n)
95 for (; n && isspace(*str); --n)
102 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
105 const char *end = fqname + n;
106 const char *name = skipn_spaces(fqname, n);
114 if (name[0] == ':') {
115 char *split = strnchr(&name[1], end - &name[1], ':');
116 *ns_name = skipn_spaces(&name[1], end - &name[1]);
120 *ns_len = split - *ns_name;
124 if (end - split > 1 && strncmp(split, "//", 2) == 0)
126 name = skipn_spaces(split, end - split);
128 /* a ns name without a following profile is allowed */
130 *ns_len = end - *ns_name;
133 if (name && *name == 0)
140 * aa_info_message - log a none profile related status message
141 * @str: message to log
143 void aa_info_message(const char *str)
146 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, NULL);
148 aad(&sa)->info = str;
149 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
151 printk(KERN_INFO "AppArmor: %s\n", str);
154 __counted char *aa_str_alloc(int size, gfp_t gfp)
156 struct counted_str *str;
158 str = kmalloc(struct_size(str, name, size), gfp);
162 kref_init(&str->count);
166 void aa_str_kref(struct kref *kref)
168 kfree(container_of(kref, struct counted_str, count));
172 const char aa_file_perm_chrs[] = "xwracd km l ";
173 const char *aa_file_perm_names[] = {
216 * aa_perm_mask_to_str - convert a perm mask to its short string
217 * @str: character buffer to store string in (at least 10 characters)
218 * @str_size: size of the @str buffer
219 * @chrs: NUL-terminated character buffer of permission characters
220 * @mask: permission mask to convert
222 void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs, u32 mask)
224 unsigned int i, perm = 1;
225 size_t num_chrs = strlen(chrs);
227 for (i = 0; i < num_chrs; perm <<= 1, i++) {
229 /* Ensure that one byte is left for NUL-termination */
230 if (WARN_ON_ONCE(str_size <= 1))
240 void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
243 const char *fmt = "%s";
244 unsigned int i, perm = 1;
247 for (i = 0; i < 32; perm <<= 1, i++) {
249 audit_log_format(ab, fmt, names[i]);
258 void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
259 u32 chrsmask, const char * const *names, u32 namesmask)
263 audit_log_format(ab, "\"");
264 if ((mask & chrsmask) && chrs) {
265 aa_perm_mask_to_str(str, sizeof(str), chrs, mask & chrsmask);
267 audit_log_format(ab, "%s", str);
268 if (mask & namesmask)
269 audit_log_format(ab, " ");
271 if ((mask & namesmask) && names)
272 aa_audit_perm_names(ab, names, mask & namesmask);
273 audit_log_format(ab, "\"");
277 * aa_audit_perms_cb - generic callback fn for auditing perms
278 * @ab: audit buffer (NOT NULL)
279 * @va: audit struct to audit values of (NOT NULL)
281 static void aa_audit_perms_cb(struct audit_buffer *ab, void *va)
283 struct common_audit_data *sa = va;
285 if (aad(sa)->request) {
286 audit_log_format(ab, " requested_mask=");
287 aa_audit_perm_mask(ab, aad(sa)->request, aa_file_perm_chrs,
288 PERMS_CHRS_MASK, aa_file_perm_names,
291 if (aad(sa)->denied) {
292 audit_log_format(ab, "denied_mask=");
293 aa_audit_perm_mask(ab, aad(sa)->denied, aa_file_perm_chrs,
294 PERMS_CHRS_MASK, aa_file_perm_names,
297 audit_log_format(ab, " peer=");
298 aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
299 FLAGS_NONE, GFP_ATOMIC);
303 * aa_apply_modes_to_perms - apply namespace and profile flags to perms
304 * @profile: that perms where computed from
305 * @perms: perms to apply mode modifiers to
307 * TODO: split into profile and ns based flags for when accumulating perms
309 void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
311 switch (AUDIT_MODE(profile)) {
313 perms->audit = ALL_PERMS_MASK;
321 case AUDIT_QUIET_DENIED:
322 perms->quiet = ALL_PERMS_MASK;
326 if (KILL_MODE(profile))
327 perms->kill = ALL_PERMS_MASK;
328 else if (COMPLAIN_MODE(profile))
329 perms->complain = ALL_PERMS_MASK;
330 else if (USER_MODE(profile))
331 perms->prompt = ALL_PERMS_MASK;
334 void aa_profile_match_label(struct aa_profile *profile,
335 struct aa_ruleset *rules,
336 struct aa_label *label,
337 int type, u32 request, struct aa_perms *perms)
339 /* TODO: doesn't yet handle extended types */
342 state = aa_dfa_next(rules->policy.dfa,
343 rules->policy.start[AA_CLASS_LABEL],
345 aa_label_match(profile, rules, label, state, false, request, perms);
349 /* currently unused */
350 int aa_profile_label_perm(struct aa_profile *profile, struct aa_profile *target,
351 u32 request, int type, u32 *deny,
352 struct common_audit_data *sa)
354 struct aa_ruleset *rules = list_first_entry(&profile->rules,
355 typeof(*rules), list);
356 struct aa_perms perms;
358 aad(sa)->label = &profile->label;
359 aad(sa)->peer = &target->label;
360 aad(sa)->request = request;
362 aa_profile_match_label(profile, rules, &target->label, type, request,
364 aa_apply_modes_to_perms(profile, &perms);
365 *deny |= request & perms.deny;
366 return aa_check_perms(profile, &perms, request, sa, aa_audit_perms_cb);
370 * aa_check_perms - do audit mode selection based on perms set
371 * @profile: profile being checked
372 * @perms: perms computed for the request
373 * @request: requested perms
374 * @deny: Returns: explicit deny set
375 * @sa: initialized audit structure (MAY BE NULL if not auditing)
376 * @cb: callback fn for type specific fields (MAY BE NULL)
378 * Returns: 0 if permission else error code
380 * Note: profile audit modes need to be set before calling by setting the
381 * perm masks appropriately.
383 * If not auditing then complain mode is not enabled and the
384 * error code will indicate whether there was an explicit deny
385 * with a positive value.
387 int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
388 u32 request, struct common_audit_data *sa,
389 void (*cb)(struct audit_buffer *, void *))
392 u32 denied = request & (~perms->allow | perms->deny);
394 if (likely(!denied)) {
395 /* mask off perms that are not being force audited */
396 request &= perms->audit;
400 type = AUDIT_APPARMOR_AUDIT;
405 if (denied & perms->kill)
406 type = AUDIT_APPARMOR_KILL;
407 else if (denied == (denied & perms->complain))
408 type = AUDIT_APPARMOR_ALLOWED;
410 type = AUDIT_APPARMOR_DENIED;
412 if (denied == (denied & perms->hide))
415 denied &= ~perms->quiet;
421 aad(sa)->label = &profile->label;
422 aad(sa)->request = request;
423 aad(sa)->denied = denied;
424 aad(sa)->error = error;
425 aa_audit_msg(type, sa, cb);
428 if (type == AUDIT_APPARMOR_ALLOWED)
436 * aa_policy_init - initialize a policy structure
437 * @policy: policy to initialize (NOT NULL)
438 * @prefix: prefix name if any is required. (MAYBE NULL)
439 * @name: name of the policy, init will make a copy of it (NOT NULL)
440 * @gfp: allocation mode
442 * Note: this fn creates a copy of strings passed in
444 * Returns: true if policy init successful
446 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
447 const char *name, gfp_t gfp)
451 /* freed by policy_free */
453 hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
455 sprintf(hname, "%s//%s", prefix, name);
457 hname = aa_str_alloc(strlen(name) + 1, gfp);
463 policy->hname = hname;
464 /* base.name is a substring of fqname */
465 policy->name = basename(policy->hname);
466 INIT_LIST_HEAD(&policy->list);
467 INIT_LIST_HEAD(&policy->profiles);
473 * aa_policy_destroy - free the elements referenced by @policy
474 * @policy: policy that is to have its elements freed (NOT NULL)
476 void aa_policy_destroy(struct aa_policy *policy)
478 AA_BUG(on_list_rcu(&policy->profiles));
479 AA_BUG(on_list_rcu(&policy->list));
481 /* don't free name as its a subset of hname */
482 aa_put_str(policy->hname);