blob: 5d8ef31a60f1c5858ad8f2ce537ef7826f352994 [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 Johansenfe6bb312017-01-16 00:42:14 -080024#include "include/policy.h"
John Johansencdff2642010-07-29 14:47:57 -070025
26/**
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)
30 *
31 * Returns: profile name or NULL if one is not specified
32 *
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
35 * that portion.
36 *
37 * NOTE: may modify the @fqname string. The pointers returned point
38 * into the @fqname string.
39 */
40char *aa_split_fqname(char *fqname, char **ns_name)
41{
42 char *name = strim(fqname);
43
44 *ns_name = NULL;
45 if (name[0] == ':') {
46 char *split = strchr(&name[1], ':');
John Johansen04ccd532010-08-27 18:33:28 -070047 *ns_name = skip_spaces(&name[1]);
John Johansencdff2642010-07-29 14:47:57 -070048 if (split) {
49 /* overwrite ':' with \0 */
John Johansen2654bfb2013-02-27 03:45:05 -080050 *split++ = 0;
51 if (strncmp(split, "//", 2) == 0)
52 split += 2;
53 name = skip_spaces(split);
John Johansencdff2642010-07-29 14:47:57 -070054 } else
55 /* a ns name without a following profile is allowed */
56 name = NULL;
John Johansencdff2642010-07-29 14:47:57 -070057 }
58 if (name && *name == 0)
59 name = NULL;
60
61 return name;
62}
63
64/**
John Johansen3b0aaf52017-01-16 00:42:23 -080065 * skipn_spaces - Removes leading whitespace from @str.
66 * @str: The string to be stripped.
67 *
68 * Returns a pointer to the first non-whitespace character in @str.
69 * if all whitespace will return NULL
70 */
71
72static const char *skipn_spaces(const char *str, size_t n)
73{
74 for (; n && isspace(*str); --n)
75 ++str;
76 if (n)
77 return (char *)str;
78 return NULL;
79}
80
81const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
82 size_t *ns_len)
83{
84 const char *end = fqname + n;
85 const char *name = skipn_spaces(fqname, n);
86
87 if (!name)
88 return NULL;
89 *ns_name = NULL;
90 *ns_len = 0;
91 if (name[0] == ':') {
92 char *split = strnchr(&name[1], end - &name[1], ':');
93 *ns_name = skipn_spaces(&name[1], end - &name[1]);
94 if (!*ns_name)
95 return NULL;
96 if (split) {
97 *ns_len = split - *ns_name;
98 if (*ns_len == 0)
99 *ns_name = NULL;
100 split++;
101 if (end - split > 1 && strncmp(split, "//", 2) == 0)
102 split += 2;
103 name = skipn_spaces(split, end - split);
104 } else {
105 /* a ns name without a following profile is allowed */
106 name = NULL;
107 *ns_len = end - *ns_name;
108 }
109 }
110 if (name && *name == 0)
111 name = NULL;
112
113 return name;
114}
115
116/**
John Johansencdff2642010-07-29 14:47:57 -0700117 * aa_info_message - log a none profile related status message
118 * @str: message to log
119 */
120void aa_info_message(const char *str)
121{
122 if (audit_enabled) {
123 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700124 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -0400125 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700126 sa.aad = &aad;
127 aad.info = str;
John Johansencdff2642010-07-29 14:47:57 -0700128 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
129 }
130 printk(KERN_INFO "AppArmor: %s\n", str);
131}
132
133/**
John Johansen0ca554b2013-02-18 16:04:34 -0800134 * __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).
John Johansencdff2642010-07-29 14:47:57 -0700137 *
138 * Return: allocated buffer or NULL if failed
139 *
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.
142 */
John Johansen0ca554b2013-02-18 16:04:34 -0800143void *__aa_kvmalloc(size_t size, gfp_t flags)
John Johansencdff2642010-07-29 14:47:57 -0700144{
145 void *buffer = NULL;
146
147 if (size == 0)
148 return NULL;
149
150 /* do not attempt kmalloc if we need more than 16 pages at once */
151 if (size <= (16*PAGE_SIZE))
Tetsuo Handaa7f6c1b2016-11-14 20:11:52 +0900152 buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
153 __GFP_NOWARN);
John Johansencdff2642010-07-29 14:47:57 -0700154 if (!buffer) {
John Johansen0ca554b2013-02-18 16:04:34 -0800155 if (flags & __GFP_ZERO)
156 buffer = vzalloc(size);
157 else
158 buffer = vmalloc(size);
John Johansencdff2642010-07-29 14:47:57 -0700159 }
160 return buffer;
161}
John Johansenfe6bb312017-01-16 00:42:14 -0800162
163/**
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)
168 *
169 * Note: this fn creates a copy of strings passed in
170 *
171 * Returns: true if policy init successful
172 */
173bool aa_policy_init(struct aa_policy *policy, const char *prefix,
John Johansend102d892017-01-16 00:42:31 -0800174 const char *name, gfp_t gfp)
John Johansenfe6bb312017-01-16 00:42:14 -0800175{
176 /* freed by policy_free */
177 if (prefix) {
178 policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
John Johansend102d892017-01-16 00:42:31 -0800179 gfp);
John Johansenfe6bb312017-01-16 00:42:14 -0800180 if (policy->hname)
John Johansenbbe4a7c2017-01-16 00:42:30 -0800181 sprintf((char *)policy->hname, "%s//%s", prefix, name);
John Johansenfe6bb312017-01-16 00:42:14 -0800182 } else
John Johansend102d892017-01-16 00:42:31 -0800183 policy->hname = kstrdup(name, gfp);
John Johansenfe6bb312017-01-16 00:42:14 -0800184 if (!policy->hname)
185 return 0;
186 /* base.name is a substring of fqname */
John Johansend102d892017-01-16 00:42:31 -0800187 policy->name = basename(policy->hname);
John Johansenfe6bb312017-01-16 00:42:14 -0800188 INIT_LIST_HEAD(&policy->list);
189 INIT_LIST_HEAD(&policy->profiles);
190
191 return 1;
192}
193
194/**
195 * aa_policy_destroy - free the elements referenced by @policy
196 * @policy: policy that is to have its elements freed (NOT NULL)
197 */
198void aa_policy_destroy(struct aa_policy *policy)
199{
John Johansen5fd1b952017-01-16 00:42:32 -0800200 AA_BUG(on_list_rcu(&policy->profiles));
201 AA_BUG(on_list_rcu(&policy->list));
John Johansenfe6bb312017-01-16 00:42:14 -0800202
203 /* don't free name as its a subset of hname */
204 kzfree(policy->hname);
205}