2 * AppArmor security module
4 * This file contains basic common functions used in AppArmor
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #include <linux/ctype.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/vmalloc.h>
21 #include "include/audit.h"
22 #include "include/apparmor.h"
23 #include "include/lib.h"
24 #include "include/policy.h"
27 * aa_split_fqname - split a fqname into a profile and namespace name
28 * @fqname: a full qualified name in namespace profile format (NOT NULL)
29 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
31 * Returns: profile name or NULL if one is not specified
33 * Split a namespace name from a profile name (see policy.c for naming
34 * description). If a portion of the name is missing it returns NULL for
37 * NOTE: may modify the @fqname string. The pointers returned point
38 * into the @fqname string.
40 char *aa_split_fqname(char *fqname, char **ns_name)
42 char *name = strim(fqname);
46 char *split = strchr(&name[1], ':');
47 *ns_name = skip_spaces(&name[1]);
49 /* overwrite ':' with \0 */
51 if (strncmp(split, "//", 2) == 0)
53 name = skip_spaces(split);
55 /* a ns name without a following profile is allowed */
58 if (name && *name == 0)
65 * skipn_spaces - Removes leading whitespace from @str.
66 * @str: The string to be stripped.
68 * Returns a pointer to the first non-whitespace character in @str.
69 * if all whitespace will return NULL
72 static const char *skipn_spaces(const char *str, size_t n)
74 for (; n && isspace(*str); --n)
81 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
84 const char *end = fqname + n;
85 const char *name = skipn_spaces(fqname, n);
92 char *split = strnchr(&name[1], end - &name[1], ':');
93 *ns_name = skipn_spaces(&name[1], end - &name[1]);
97 *ns_len = split - *ns_name;
101 if (end - split > 1 && strncmp(split, "//", 2) == 0)
103 name = skipn_spaces(split, end - split);
105 /* a ns name without a following profile is allowed */
107 *ns_len = end - *ns_name;
110 if (name && *name == 0)
117 * aa_info_message - log a none profile related status message
118 * @str: message to log
120 void aa_info_message(const char *str)
123 struct common_audit_data sa;
124 struct apparmor_audit_data aad = {0,};
125 sa.type = LSM_AUDIT_DATA_NONE;
128 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
130 printk(KERN_INFO "AppArmor: %s\n", str);
134 * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
135 * @size: how many bytes of memory are required
136 * @flags: the type of memory to allocate (see kmalloc).
138 * Return: allocated buffer or NULL if failed
140 * It is possible that policy being loaded from the user is larger than
141 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
143 void *__aa_kvmalloc(size_t size, gfp_t flags)
150 /* do not attempt kmalloc if we need more than 16 pages at once */
151 if (size <= (16*PAGE_SIZE))
152 buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
155 if (flags & __GFP_ZERO)
156 buffer = vzalloc(size);
158 buffer = vmalloc(size);
164 * aa_policy_init - initialize a policy structure
165 * @policy: policy to initialize (NOT NULL)
166 * @prefix: prefix name if any is required. (MAYBE NULL)
167 * @name: name of the policy, init will make a copy of it (NOT NULL)
169 * Note: this fn creates a copy of strings passed in
171 * Returns: true if policy init successful
173 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
176 /* freed by policy_free */
178 policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
181 sprintf((char *)policy->hname, "%s//%s", prefix, name);
183 policy->hname = kstrdup(name, GFP_KERNEL);
186 /* base.name is a substring of fqname */
187 policy->name = (char *)basename(policy->hname);
188 INIT_LIST_HEAD(&policy->list);
189 INIT_LIST_HEAD(&policy->profiles);
195 * aa_policy_destroy - free the elements referenced by @policy
196 * @policy: policy that is to have its elements freed (NOT NULL)
198 void aa_policy_destroy(struct aa_policy *policy)
200 /* still contains profiles -- invalid */
201 if (on_list_rcu(&policy->profiles)) {
202 AA_ERROR("%s: internal error, policy '%s' contains profiles\n",
203 __func__, policy->name);
205 if (on_list_rcu(&policy->list)) {
206 AA_ERROR("%s: internal error, policy '%s' still on list\n",
207 __func__, policy->name);
210 /* don't free name as its a subset of hname */
211 kzfree(policy->hname);