blob: e29ccdb0309a1e842fa5683103d0bfdcc5d7ebec [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
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000015#include <linux/mm.h>
John Johansencdff2642010-07-29 14:47:57 -070016#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/vmalloc.h>
19
20#include "include/audit.h"
James Morris32c3df62011-08-29 11:15:25 +100021#include "include/apparmor.h"
John Johansen12557dc2017-01-16 00:42:13 -080022#include "include/lib.h"
John Johansenfe6bb312017-01-16 00:42:14 -080023#include "include/policy.h"
John Johansencdff2642010-07-29 14:47:57 -070024
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 */
39char *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], ':');
John Johansen04ccd532010-08-27 18:33:28 -070046 *ns_name = skip_spaces(&name[1]);
John Johansencdff2642010-07-29 14:47:57 -070047 if (split) {
48 /* overwrite ':' with \0 */
John Johansen2654bfb2013-02-27 03:45:05 -080049 *split++ = 0;
50 if (strncmp(split, "//", 2) == 0)
51 split += 2;
52 name = skip_spaces(split);
John Johansencdff2642010-07-29 14:47:57 -070053 } else
54 /* a ns name without a following profile is allowed */
55 name = NULL;
John Johansencdff2642010-07-29 14:47:57 -070056 }
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 */
67void aa_info_message(const char *str)
68{
69 if (audit_enabled) {
70 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -070071 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -040072 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -070073 sa.aad = &aad;
74 aad.info = str;
John Johansencdff2642010-07-29 14:47:57 -070075 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
76 }
77 printk(KERN_INFO "AppArmor: %s\n", str);
78}
79
80/**
John Johansen0ca554b2013-02-18 16:04:34 -080081 * __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).
John Johansencdff2642010-07-29 14:47:57 -070084 *
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 */
John Johansen0ca554b2013-02-18 16:04:34 -080090void *__aa_kvmalloc(size_t size, gfp_t flags)
John Johansencdff2642010-07-29 14:47:57 -070091{
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))
Tetsuo Handaa7f6c1b2016-11-14 20:11:52 +090099 buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
100 __GFP_NOWARN);
John Johansencdff2642010-07-29 14:47:57 -0700101 if (!buffer) {
John Johansen0ca554b2013-02-18 16:04:34 -0800102 if (flags & __GFP_ZERO)
103 buffer = vzalloc(size);
104 else
105 buffer = vmalloc(size);
John Johansencdff2642010-07-29 14:47:57 -0700106 }
107 return buffer;
108}
John Johansenfe6bb312017-01-16 00:42:14 -0800109
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 */
120bool 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 */
145void 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}