blob: 0ceecdbb4658b35ea476bf84620fc1505ac23ec6 [file] [log] [blame]
John Johansencdff2642010-07-29 14:47:57 -07001/*
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
John Johansen3b0aaf52017-01-16 00:42:23 -080015#include <linux/ctype.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000016#include <linux/mm.h>
John Johansencdff2642010-07-29 14:47:57 -070017#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/vmalloc.h>
20
21#include "include/audit.h"
James Morris32c3df62011-08-29 11:15:25 +100022#include "include/apparmor.h"
John Johansen12557dc2017-01-16 00:42:13 -080023#include "include/lib.h"
John Johansenfc7e0b22017-05-26 01:57:09 -070024#include "include/perms.h"
John Johansenfe6bb312017-01-16 00:42:14 -080025#include "include/policy.h"
John Johansencdff2642010-07-29 14:47:57 -070026
John Johansen2d679f32017-05-29 12:19:39 -070027struct aa_perms nullperms;
John Johansenaa9aeea2017-05-29 12:16:04 -070028struct aa_perms allperms = { .allow = ALL_PERMS_MASK,
29 .quiet = ALL_PERMS_MASK,
30 .hide = ALL_PERMS_MASK };
31
John Johansencdff2642010-07-29 14:47:57 -070032/**
33 * aa_split_fqname - split a fqname into a profile and namespace name
34 * @fqname: a full qualified name in namespace profile format (NOT NULL)
35 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
36 *
37 * Returns: profile name or NULL if one is not specified
38 *
39 * Split a namespace name from a profile name (see policy.c for naming
40 * description). If a portion of the name is missing it returns NULL for
41 * that portion.
42 *
43 * NOTE: may modify the @fqname string. The pointers returned point
44 * into the @fqname string.
45 */
46char *aa_split_fqname(char *fqname, char **ns_name)
47{
48 char *name = strim(fqname);
49
50 *ns_name = NULL;
51 if (name[0] == ':') {
52 char *split = strchr(&name[1], ':');
John Johansen04ccd532010-08-27 18:33:28 -070053 *ns_name = skip_spaces(&name[1]);
John Johansencdff2642010-07-29 14:47:57 -070054 if (split) {
55 /* overwrite ':' with \0 */
John Johansen2654bfb2013-02-27 03:45:05 -080056 *split++ = 0;
57 if (strncmp(split, "//", 2) == 0)
58 split += 2;
59 name = skip_spaces(split);
John Johansencdff2642010-07-29 14:47:57 -070060 } else
61 /* a ns name without a following profile is allowed */
62 name = NULL;
John Johansencdff2642010-07-29 14:47:57 -070063 }
64 if (name && *name == 0)
65 name = NULL;
66
67 return name;
68}
69
70/**
John Johansen3b0aaf52017-01-16 00:42:23 -080071 * skipn_spaces - Removes leading whitespace from @str.
72 * @str: The string to be stripped.
73 *
74 * Returns a pointer to the first non-whitespace character in @str.
75 * if all whitespace will return NULL
76 */
77
John Johansenb91deb92017-05-22 02:47:22 -070078const char *skipn_spaces(const char *str, size_t n)
John Johansen3b0aaf52017-01-16 00:42:23 -080079{
80 for (; n && isspace(*str); --n)
81 ++str;
82 if (n)
83 return (char *)str;
84 return NULL;
85}
86
87const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
88 size_t *ns_len)
89{
90 const char *end = fqname + n;
91 const char *name = skipn_spaces(fqname, n);
92
93 if (!name)
94 return NULL;
95 *ns_name = NULL;
96 *ns_len = 0;
97 if (name[0] == ':') {
98 char *split = strnchr(&name[1], end - &name[1], ':');
99 *ns_name = skipn_spaces(&name[1], end - &name[1]);
100 if (!*ns_name)
101 return NULL;
102 if (split) {
103 *ns_len = split - *ns_name;
104 if (*ns_len == 0)
105 *ns_name = NULL;
106 split++;
107 if (end - split > 1 && strncmp(split, "//", 2) == 0)
108 split += 2;
109 name = skipn_spaces(split, end - split);
110 } else {
111 /* a ns name without a following profile is allowed */
112 name = NULL;
113 *ns_len = end - *ns_name;
114 }
115 }
116 if (name && *name == 0)
117 name = NULL;
118
119 return name;
120}
121
122/**
John Johansencdff2642010-07-29 14:47:57 -0700123 * aa_info_message - log a none profile related status message
124 * @str: message to log
125 */
126void aa_info_message(const char *str)
127{
128 if (audit_enabled) {
John Johansenef88a7a2017-01-16 00:43:02 -0800129 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
130
131 aad(&sa)->info = str;
John Johansencdff2642010-07-29 14:47:57 -0700132 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
133 }
134 printk(KERN_INFO "AppArmor: %s\n", str);
135}
136
John Johansena1bd6272017-06-09 07:09:05 -0700137__counted char *aa_str_alloc(int size, gfp_t gfp)
138{
139 struct counted_str *str;
140
141 str = kmalloc(sizeof(struct counted_str) + size, gfp);
142 if (!str)
143 return NULL;
144
145 kref_init(&str->count);
146 return str->name;
147}
148
149void aa_str_kref(struct kref *kref)
150{
151 kfree(container_of(kref, struct counted_str, count));
152}
153
154
John Johansene53cfe62017-05-26 15:07:22 -0700155const char aa_file_perm_chrs[] = "xwracd km l ";
156const char *aa_file_perm_names[] = {
157 "exec",
158 "write",
159 "read",
160 "append",
161
162 "create",
163 "delete",
164 "open",
165 "rename",
166
167 "setattr",
168 "getattr",
169 "setcred",
170 "getcred",
171
172 "chmod",
173 "chown",
174 "chgrp",
175 "lock",
176
177 "mmap",
178 "mprot",
179 "link",
180 "snapshot",
181
182 "unknown",
183 "unknown",
184 "unknown",
185 "unknown",
186
187 "unknown",
188 "unknown",
189 "unknown",
190 "unknown",
191
192 "stack",
193 "change_onexec",
194 "change_profile",
195 "change_hat",
196};
197
198/**
199 * aa_perm_mask_to_str - convert a perm mask to its short string
200 * @str: character buffer to store string in (at least 10 characters)
201 * @mask: permission mask to convert
202 */
203void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask)
204{
205 unsigned int i, perm = 1;
206
207 for (i = 0; i < 32; perm <<= 1, i++) {
208 if (mask & perm)
209 *str++ = chrs[i];
210 }
211 *str = '\0';
212}
213
John Johansenaa9aeea2017-05-29 12:16:04 -0700214void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask)
215{
216 const char *fmt = "%s";
217 unsigned int i, perm = 1;
218 bool prev = false;
219
220 for (i = 0; i < 32; perm <<= 1, i++) {
221 if (mask & perm) {
222 audit_log_format(ab, fmt, names[i]);
223 if (!prev) {
224 prev = true;
225 fmt = " %s";
226 }
227 }
228 }
229}
230
231void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
232 u32 chrsmask, const char **names, u32 namesmask)
233{
234 char str[33];
235
236 audit_log_format(ab, "\"");
237 if ((mask & chrsmask) && chrs) {
238 aa_perm_mask_to_str(str, chrs, mask & chrsmask);
239 mask &= ~chrsmask;
240 audit_log_format(ab, "%s", str);
241 if (mask & namesmask)
242 audit_log_format(ab, " ");
243 }
244 if ((mask & namesmask) && names)
245 aa_audit_perm_names(ab, names, mask & namesmask);
246 audit_log_format(ab, "\"");
247}
248
249/**
250 * aa_apply_modes_to_perms - apply namespace and profile flags to perms
251 * @profile: that perms where computed from
252 * @perms: perms to apply mode modifiers to
253 *
254 * TODO: split into profile and ns based flags for when accumulating perms
255 */
256void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
257{
258 switch (AUDIT_MODE(profile)) {
259 case AUDIT_ALL:
260 perms->audit = ALL_PERMS_MASK;
261 /* fall through */
262 case AUDIT_NOQUIET:
263 perms->quiet = 0;
264 break;
265 case AUDIT_QUIET:
266 perms->audit = 0;
267 /* fall through */
268 case AUDIT_QUIET_DENIED:
269 perms->quiet = ALL_PERMS_MASK;
270 break;
271 }
272
273 if (KILL_MODE(profile))
274 perms->kill = ALL_PERMS_MASK;
275 else if (COMPLAIN_MODE(profile))
276 perms->complain = ALL_PERMS_MASK;
277/*
278 * TODO:
279 * else if (PROMPT_MODE(profile))
280 * perms->prompt = ALL_PERMS_MASK;
281 */
282}
283
284static u32 map_other(u32 x)
285{
286 return ((x & 0x3) << 8) | /* SETATTR/GETATTR */
287 ((x & 0x1c) << 18) | /* ACCEPT/BIND/LISTEN */
288 ((x & 0x60) << 19); /* SETOPT/GETOPT */
289}
290
291void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
292 struct aa_perms *perms)
293{
294 perms->deny = 0;
295 perms->kill = perms->stop = 0;
296 perms->complain = perms->cond = 0;
297 perms->hide = 0;
298 perms->prompt = 0;
299 perms->allow = dfa_user_allow(dfa, state);
300 perms->audit = dfa_user_audit(dfa, state);
301 perms->quiet = dfa_user_quiet(dfa, state);
302
303 /* for v5 perm mapping in the policydb, the other set is used
304 * to extend the general perm set
305 */
306 perms->allow |= map_other(dfa_other_allow(dfa, state));
307 perms->audit |= map_other(dfa_other_audit(dfa, state));
308 perms->quiet |= map_other(dfa_other_quiet(dfa, state));
309// perms->xindex = dfa_user_xindex(dfa, state);
310}
311
John Johansencdff2642010-07-29 14:47:57 -0700312/**
John Johansenfe6bb312017-01-16 00:42:14 -0800313 * aa_policy_init - initialize a policy structure
314 * @policy: policy to initialize (NOT NULL)
315 * @prefix: prefix name if any is required. (MAYBE NULL)
316 * @name: name of the policy, init will make a copy of it (NOT NULL)
John Johansena1bd6272017-06-09 07:09:05 -0700317 * @gfp: allocation mode
John Johansenfe6bb312017-01-16 00:42:14 -0800318 *
319 * Note: this fn creates a copy of strings passed in
320 *
321 * Returns: true if policy init successful
322 */
323bool aa_policy_init(struct aa_policy *policy, const char *prefix,
John Johansend102d892017-01-16 00:42:31 -0800324 const char *name, gfp_t gfp)
John Johansenfe6bb312017-01-16 00:42:14 -0800325{
John Johansena1bd6272017-06-09 07:09:05 -0700326 char *hname;
327
John Johansenfe6bb312017-01-16 00:42:14 -0800328 /* freed by policy_free */
329 if (prefix) {
John Johansena1bd6272017-06-09 07:09:05 -0700330 hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
331 if (hname)
332 sprintf(hname, "%s//%s", prefix, name);
333 } else {
334 hname = aa_str_alloc(strlen(name) + 1, gfp);
335 if (hname)
336 strcpy(hname, name);
337 }
338 if (!hname)
kbuild test robotb9c42ac2017-04-06 06:55:19 -0700339 return false;
John Johansena1bd6272017-06-09 07:09:05 -0700340 policy->hname = hname;
John Johansenfe6bb312017-01-16 00:42:14 -0800341 /* base.name is a substring of fqname */
John Johansend102d892017-01-16 00:42:31 -0800342 policy->name = basename(policy->hname);
John Johansenfe6bb312017-01-16 00:42:14 -0800343 INIT_LIST_HEAD(&policy->list);
344 INIT_LIST_HEAD(&policy->profiles);
345
kbuild test robotb9c42ac2017-04-06 06:55:19 -0700346 return true;
John Johansenfe6bb312017-01-16 00:42:14 -0800347}
348
349/**
350 * aa_policy_destroy - free the elements referenced by @policy
351 * @policy: policy that is to have its elements freed (NOT NULL)
352 */
353void aa_policy_destroy(struct aa_policy *policy)
354{
John Johansen5fd1b952017-01-16 00:42:32 -0800355 AA_BUG(on_list_rcu(&policy->profiles));
356 AA_BUG(on_list_rcu(&policy->list));
John Johansenfe6bb312017-01-16 00:42:14 -0800357
358 /* don't free name as its a subset of hname */
John Johansena1bd6272017-06-09 07:09:05 -0700359 aa_put_str(policy->hname);
John Johansenfe6bb312017-01-16 00:42:14 -0800360}