]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/apparmor/lib.c
apparmor: split out shared policy_XXX fns to lib
[karo-tx-linux.git] / security / apparmor / lib.c
1 /*
2  * AppArmor security module
3  *
4  * This file contains basic common functions used in AppArmor
5  *
6  * Copyright (C) 1998-2008 Novell/SUSE
7  * Copyright 2009-2010 Canonical Ltd.
8  *
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
12  * License.
13  */
14
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/vmalloc.h>
19
20 #include "include/audit.h"
21 #include "include/apparmor.h"
22 #include "include/lib.h"
23 #include "include/policy.h"
24
25 /**
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)
29  *
30  * Returns: profile name or NULL if one is not specified
31  *
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
34  * that portion.
35  *
36  * NOTE: may modify the @fqname string.  The pointers returned point
37  *       into the @fqname string.
38  */
39 char *aa_split_fqname(char *fqname, char **ns_name)
40 {
41         char *name = strim(fqname);
42
43         *ns_name = NULL;
44         if (name[0] == ':') {
45                 char *split = strchr(&name[1], ':');
46                 *ns_name = skip_spaces(&name[1]);
47                 if (split) {
48                         /* overwrite ':' with \0 */
49                         *split++ = 0;
50                         if (strncmp(split, "//", 2) == 0)
51                                 split += 2;
52                         name = skip_spaces(split);
53                 } else
54                         /* a ns name without a following profile is allowed */
55                         name = NULL;
56         }
57         if (name && *name == 0)
58                 name = NULL;
59
60         return name;
61 }
62
63 /**
64  * aa_info_message - log a none profile related status message
65  * @str: message to log
66  */
67 void aa_info_message(const char *str)
68 {
69         if (audit_enabled) {
70                 struct common_audit_data sa;
71                 struct apparmor_audit_data aad = {0,};
72                 sa.type = LSM_AUDIT_DATA_NONE;
73                 sa.aad = &aad;
74                 aad.info = str;
75                 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
76         }
77         printk(KERN_INFO "AppArmor: %s\n", str);
78 }
79
80 /**
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).
84  *
85  * Return: allocated buffer or NULL if failed
86  *
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.
89  */
90 void *__aa_kvmalloc(size_t size, gfp_t flags)
91 {
92         void *buffer = NULL;
93
94         if (size == 0)
95                 return NULL;
96
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 |
100                                  __GFP_NOWARN);
101         if (!buffer) {
102                 if (flags & __GFP_ZERO)
103                         buffer = vzalloc(size);
104                 else
105                         buffer = vmalloc(size);
106         }
107         return buffer;
108 }
109
110 /**
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)
115  *
116  * Note: this fn creates a copy of strings passed in
117  *
118  * Returns: true if policy init successful
119  */
120 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
121                     const char *name)
122 {
123         /* freed by policy_free */
124         if (prefix) {
125                 policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
126                                         GFP_KERNEL);
127                 if (policy->hname)
128                         sprintf(policy->hname, "%s//%s", prefix, name);
129         } else
130                 policy->hname = kstrdup(name, GFP_KERNEL);
131         if (!policy->hname)
132                 return 0;
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);
137
138         return 1;
139 }
140
141 /**
142  * aa_policy_destroy - free the elements referenced by @policy
143  * @policy: policy that is to have its elements freed  (NOT NULL)
144  */
145 void aa_policy_destroy(struct aa_policy *policy)
146 {
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);
151         }
152         if (on_list_rcu(&policy->list)) {
153                 AA_ERROR("%s: internal error, policy '%s' still on list\n",
154                          __func__, policy->name);
155         }
156
157         /* don't free name as its a subset of hname */
158         kzfree(policy->hname);
159 }