John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 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 | |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 15 | #include <linux/mm.h> |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | |
| 20 | #include "include/audit.h" |
James Morris | 32c3df6 | 2011-08-29 11:15:25 +1000 | [diff] [blame] | 21 | #include "include/apparmor.h" |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 22 | #include "include/lib.h" |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 23 | #include "include/policy.h" |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 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], ':'); |
John Johansen | 04ccd53 | 2010-08-27 18:33:28 -0700 | [diff] [blame] | 46 | *ns_name = skip_spaces(&name[1]); |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 47 | if (split) { |
| 48 | /* overwrite ':' with \0 */ |
John Johansen | 2654bfb | 2013-02-27 03:45:05 -0800 | [diff] [blame] | 49 | *split++ = 0; |
| 50 | if (strncmp(split, "//", 2) == 0) |
| 51 | split += 2; |
| 52 | name = skip_spaces(split); |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 53 | } else |
| 54 | /* a ns name without a following profile is allowed */ |
| 55 | name = NULL; |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 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; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 71 | struct apparmor_audit_data aad = {0,}; |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 72 | sa.type = LSM_AUDIT_DATA_NONE; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 73 | sa.aad = &aad; |
| 74 | aad.info = str; |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 75 | aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL); |
| 76 | } |
| 77 | printk(KERN_INFO "AppArmor: %s\n", str); |
| 78 | } |
| 79 | |
| 80 | /** |
John Johansen | 0ca554b | 2013-02-18 16:04:34 -0800 | [diff] [blame] | 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). |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 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 | */ |
John Johansen | 0ca554b | 2013-02-18 16:04:34 -0800 | [diff] [blame] | 90 | void *__aa_kvmalloc(size_t size, gfp_t flags) |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 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)) |
Tetsuo Handa | a7f6c1b | 2016-11-14 20:11:52 +0900 | [diff] [blame] | 99 | buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY | |
| 100 | __GFP_NOWARN); |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 101 | if (!buffer) { |
John Johansen | 0ca554b | 2013-02-18 16:04:34 -0800 | [diff] [blame] | 102 | if (flags & __GFP_ZERO) |
| 103 | buffer = vzalloc(size); |
| 104 | else |
| 105 | buffer = vmalloc(size); |
John Johansen | cdff264 | 2010-07-29 14:47:57 -0700 | [diff] [blame] | 106 | } |
| 107 | return buffer; |
| 108 | } |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 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 | } |