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
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/vmalloc.h>
20 #include "include/audit.h"
21 #include "include/apparmor.h"
22 #include "include/lib.h"
23 #include "include/policy.h"
26 * aa_split_fqname - split a fqname into a profile and namespace name
27 * @fqname: a full qualified name in namespace profile format (NOT NULL)
28 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
30 * Returns: profile name or NULL if one is not specified
32 * Split a namespace name from a profile name (see policy.c for naming
33 * description). If a portion of the name is missing it returns NULL for
36 * NOTE: may modify the @fqname string. The pointers returned point
37 * into the @fqname string.
39 char *aa_split_fqname(char *fqname, char **ns_name)
41 char *name = strim(fqname);
45 char *split = strchr(&name[1], ':');
46 *ns_name = skip_spaces(&name[1]);
48 /* overwrite ':' with \0 */
50 if (strncmp(split, "//", 2) == 0)
52 name = skip_spaces(split);
54 /* a ns name without a following profile is allowed */
57 if (name && *name == 0)
64 * aa_info_message - log a none profile related status message
65 * @str: message to log
67 void aa_info_message(const char *str)
70 struct common_audit_data sa;
71 struct apparmor_audit_data aad = {0,};
72 sa.type = LSM_AUDIT_DATA_NONE;
75 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
77 printk(KERN_INFO "AppArmor: %s\n", str);
81 * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
82 * @size: how many bytes of memory are required
83 * @flags: the type of memory to allocate (see kmalloc).
85 * Return: allocated buffer or NULL if failed
87 * It is possible that policy being loaded from the user is larger than
88 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
90 void *__aa_kvmalloc(size_t size, gfp_t flags)
97 /* do not attempt kmalloc if we need more than 16 pages at once */
98 if (size <= (16*PAGE_SIZE))
99 buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
102 if (flags & __GFP_ZERO)
103 buffer = vzalloc(size);
105 buffer = vmalloc(size);
111 * aa_policy_init - initialize a policy structure
112 * @policy: policy to initialize (NOT NULL)
113 * @prefix: prefix name if any is required. (MAYBE NULL)
114 * @name: name of the policy, init will make a copy of it (NOT NULL)
116 * Note: this fn creates a copy of strings passed in
118 * Returns: true if policy init successful
120 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
123 /* freed by policy_free */
125 policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
128 sprintf(policy->hname, "%s//%s", prefix, name);
130 policy->hname = kstrdup(name, GFP_KERNEL);
133 /* base.name is a substring of fqname */
134 policy->name = (char *)hname_tail(policy->hname);
135 INIT_LIST_HEAD(&policy->list);
136 INIT_LIST_HEAD(&policy->profiles);
142 * aa_policy_destroy - free the elements referenced by @policy
143 * @policy: policy that is to have its elements freed (NOT NULL)
145 void aa_policy_destroy(struct aa_policy *policy)
147 /* still contains profiles -- invalid */
148 if (on_list_rcu(&policy->profiles)) {
149 AA_ERROR("%s: internal error, policy '%s' contains profiles\n",
150 __func__, policy->name);
152 if (on_list_rcu(&policy->list)) {
153 AA_ERROR("%s: internal error, policy '%s' still on list\n",
154 __func__, policy->name);
157 /* don't free name as its a subset of hname */
158 kzfree(policy->hname);