xref: /openbmc/linux/security/apparmor/lib.c (revision fc7e0b26b8d26e680bb2f252e9521385e0092e4c)
1cdff2642SJohn Johansen /*
2cdff2642SJohn Johansen  * AppArmor security module
3cdff2642SJohn Johansen  *
4cdff2642SJohn Johansen  * This file contains basic common functions used in AppArmor
5cdff2642SJohn Johansen  *
6cdff2642SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
7cdff2642SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
8cdff2642SJohn Johansen  *
9cdff2642SJohn Johansen  * This program is free software; you can redistribute it and/or
10cdff2642SJohn Johansen  * modify it under the terms of the GNU General Public License as
11cdff2642SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12cdff2642SJohn Johansen  * License.
13cdff2642SJohn Johansen  */
14cdff2642SJohn Johansen 
153b0aaf58SJohn Johansen #include <linux/ctype.h>
16b7f080cfSAlexey Dobriyan #include <linux/mm.h>
17cdff2642SJohn Johansen #include <linux/slab.h>
18cdff2642SJohn Johansen #include <linux/string.h>
19cdff2642SJohn Johansen #include <linux/vmalloc.h>
20cdff2642SJohn Johansen 
21cdff2642SJohn Johansen #include "include/audit.h"
2232c3df63SJames Morris #include "include/apparmor.h"
2312557dcbSJohn Johansen #include "include/lib.h"
24*fc7e0b26SJohn Johansen #include "include/perms.h"
25fe6bb31fSJohn Johansen #include "include/policy.h"
26cdff2642SJohn Johansen 
27cdff2642SJohn Johansen /**
28cdff2642SJohn Johansen  * aa_split_fqname - split a fqname into a profile and namespace name
29cdff2642SJohn Johansen  * @fqname: a full qualified name in namespace profile format (NOT NULL)
30cdff2642SJohn Johansen  * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
31cdff2642SJohn Johansen  *
32cdff2642SJohn Johansen  * Returns: profile name or NULL if one is not specified
33cdff2642SJohn Johansen  *
34cdff2642SJohn Johansen  * Split a namespace name from a profile name (see policy.c for naming
35cdff2642SJohn Johansen  * description).  If a portion of the name is missing it returns NULL for
36cdff2642SJohn Johansen  * that portion.
37cdff2642SJohn Johansen  *
38cdff2642SJohn Johansen  * NOTE: may modify the @fqname string.  The pointers returned point
39cdff2642SJohn Johansen  *       into the @fqname string.
40cdff2642SJohn Johansen  */
41cdff2642SJohn Johansen char *aa_split_fqname(char *fqname, char **ns_name)
42cdff2642SJohn Johansen {
43cdff2642SJohn Johansen 	char *name = strim(fqname);
44cdff2642SJohn Johansen 
45cdff2642SJohn Johansen 	*ns_name = NULL;
46cdff2642SJohn Johansen 	if (name[0] == ':') {
47cdff2642SJohn Johansen 		char *split = strchr(&name[1], ':');
4804ccd53fSJohn Johansen 		*ns_name = skip_spaces(&name[1]);
49cdff2642SJohn Johansen 		if (split) {
50cdff2642SJohn Johansen 			/* overwrite ':' with \0 */
512654bfbcSJohn Johansen 			*split++ = 0;
522654bfbcSJohn Johansen 			if (strncmp(split, "//", 2) == 0)
532654bfbcSJohn Johansen 				split += 2;
542654bfbcSJohn Johansen 			name = skip_spaces(split);
55cdff2642SJohn Johansen 		} else
56cdff2642SJohn Johansen 			/* a ns name without a following profile is allowed */
57cdff2642SJohn Johansen 			name = NULL;
58cdff2642SJohn Johansen 	}
59cdff2642SJohn Johansen 	if (name && *name == 0)
60cdff2642SJohn Johansen 		name = NULL;
61cdff2642SJohn Johansen 
62cdff2642SJohn Johansen 	return name;
63cdff2642SJohn Johansen }
64cdff2642SJohn Johansen 
65cdff2642SJohn Johansen /**
663b0aaf58SJohn Johansen  * skipn_spaces - Removes leading whitespace from @str.
673b0aaf58SJohn Johansen  * @str: The string to be stripped.
683b0aaf58SJohn Johansen  *
693b0aaf58SJohn Johansen  * Returns a pointer to the first non-whitespace character in @str.
703b0aaf58SJohn Johansen  * if all whitespace will return NULL
713b0aaf58SJohn Johansen  */
723b0aaf58SJohn Johansen 
73b91deb9dSJohn Johansen const char *skipn_spaces(const char *str, size_t n)
743b0aaf58SJohn Johansen {
753b0aaf58SJohn Johansen 	for (; n && isspace(*str); --n)
763b0aaf58SJohn Johansen 		++str;
773b0aaf58SJohn Johansen 	if (n)
783b0aaf58SJohn Johansen 		return (char *)str;
793b0aaf58SJohn Johansen 	return NULL;
803b0aaf58SJohn Johansen }
813b0aaf58SJohn Johansen 
823b0aaf58SJohn Johansen const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
833b0aaf58SJohn Johansen 			     size_t *ns_len)
843b0aaf58SJohn Johansen {
853b0aaf58SJohn Johansen 	const char *end = fqname + n;
863b0aaf58SJohn Johansen 	const char *name = skipn_spaces(fqname, n);
873b0aaf58SJohn Johansen 
883b0aaf58SJohn Johansen 	if (!name)
893b0aaf58SJohn Johansen 		return NULL;
903b0aaf58SJohn Johansen 	*ns_name = NULL;
913b0aaf58SJohn Johansen 	*ns_len = 0;
923b0aaf58SJohn Johansen 	if (name[0] == ':') {
933b0aaf58SJohn Johansen 		char *split = strnchr(&name[1], end - &name[1], ':');
943b0aaf58SJohn Johansen 		*ns_name = skipn_spaces(&name[1], end - &name[1]);
953b0aaf58SJohn Johansen 		if (!*ns_name)
963b0aaf58SJohn Johansen 			return NULL;
973b0aaf58SJohn Johansen 		if (split) {
983b0aaf58SJohn Johansen 			*ns_len = split - *ns_name;
993b0aaf58SJohn Johansen 			if (*ns_len == 0)
1003b0aaf58SJohn Johansen 				*ns_name = NULL;
1013b0aaf58SJohn Johansen 			split++;
1023b0aaf58SJohn Johansen 			if (end - split > 1 && strncmp(split, "//", 2) == 0)
1033b0aaf58SJohn Johansen 				split += 2;
1043b0aaf58SJohn Johansen 			name = skipn_spaces(split, end - split);
1053b0aaf58SJohn Johansen 		} else {
1063b0aaf58SJohn Johansen 			/* a ns name without a following profile is allowed */
1073b0aaf58SJohn Johansen 			name = NULL;
1083b0aaf58SJohn Johansen 			*ns_len = end - *ns_name;
1093b0aaf58SJohn Johansen 		}
1103b0aaf58SJohn Johansen 	}
1113b0aaf58SJohn Johansen 	if (name && *name == 0)
1123b0aaf58SJohn Johansen 		name = NULL;
1133b0aaf58SJohn Johansen 
1143b0aaf58SJohn Johansen 	return name;
1153b0aaf58SJohn Johansen }
1163b0aaf58SJohn Johansen 
1173b0aaf58SJohn Johansen /**
118cdff2642SJohn Johansen  * aa_info_message - log a none profile related status message
119cdff2642SJohn Johansen  * @str: message to log
120cdff2642SJohn Johansen  */
121cdff2642SJohn Johansen void aa_info_message(const char *str)
122cdff2642SJohn Johansen {
123cdff2642SJohn Johansen 	if (audit_enabled) {
124ef88a7acSJohn Johansen 		DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
125ef88a7acSJohn Johansen 
126ef88a7acSJohn Johansen 		aad(&sa)->info = str;
127cdff2642SJohn Johansen 		aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
128cdff2642SJohn Johansen 	}
129cdff2642SJohn Johansen 	printk(KERN_INFO "AppArmor: %s\n", str);
130cdff2642SJohn Johansen }
131cdff2642SJohn Johansen 
132cdff2642SJohn Johansen /**
133fe6bb31fSJohn Johansen  * aa_policy_init - initialize a policy structure
134fe6bb31fSJohn Johansen  * @policy: policy to initialize  (NOT NULL)
135fe6bb31fSJohn Johansen  * @prefix: prefix name if any is required.  (MAYBE NULL)
136fe6bb31fSJohn Johansen  * @name: name of the policy, init will make a copy of it  (NOT NULL)
137fe6bb31fSJohn Johansen  *
138fe6bb31fSJohn Johansen  * Note: this fn creates a copy of strings passed in
139fe6bb31fSJohn Johansen  *
140fe6bb31fSJohn Johansen  * Returns: true if policy init successful
141fe6bb31fSJohn Johansen  */
142fe6bb31fSJohn Johansen bool aa_policy_init(struct aa_policy *policy, const char *prefix,
143d102d895SJohn Johansen 		    const char *name, gfp_t gfp)
144fe6bb31fSJohn Johansen {
145fe6bb31fSJohn Johansen 	/* freed by policy_free */
146fe6bb31fSJohn Johansen 	if (prefix) {
147fe6bb31fSJohn Johansen 		policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
148d102d895SJohn Johansen 					gfp);
149fe6bb31fSJohn Johansen 		if (policy->hname)
150bbe4a7c8SJohn Johansen 			sprintf((char *)policy->hname, "%s//%s", prefix, name);
151fe6bb31fSJohn Johansen 	} else
152d102d895SJohn Johansen 		policy->hname = kstrdup(name, gfp);
153fe6bb31fSJohn Johansen 	if (!policy->hname)
154b9c42ac7Skbuild test robot 		return false;
155fe6bb31fSJohn Johansen 	/* base.name is a substring of fqname */
156d102d895SJohn Johansen 	policy->name = basename(policy->hname);
157fe6bb31fSJohn Johansen 	INIT_LIST_HEAD(&policy->list);
158fe6bb31fSJohn Johansen 	INIT_LIST_HEAD(&policy->profiles);
159fe6bb31fSJohn Johansen 
160b9c42ac7Skbuild test robot 	return true;
161fe6bb31fSJohn Johansen }
162fe6bb31fSJohn Johansen 
163fe6bb31fSJohn Johansen /**
164fe6bb31fSJohn Johansen  * aa_policy_destroy - free the elements referenced by @policy
165fe6bb31fSJohn Johansen  * @policy: policy that is to have its elements freed  (NOT NULL)
166fe6bb31fSJohn Johansen  */
167fe6bb31fSJohn Johansen void aa_policy_destroy(struct aa_policy *policy)
168fe6bb31fSJohn Johansen {
1695fd1b95fSJohn Johansen 	AA_BUG(on_list_rcu(&policy->profiles));
1705fd1b95fSJohn Johansen 	AA_BUG(on_list_rcu(&policy->list));
171fe6bb31fSJohn Johansen 
172fe6bb31fSJohn Johansen 	/* don't free name as its a subset of hname */
173fe6bb31fSJohn Johansen 	kzfree(policy->hname);
174fe6bb31fSJohn Johansen }
175