Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2 | /* |
| 3 | * security/tomoyo/memory.c |
| 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/hash.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include "common.h" |
| 11 | |
| 12 | /** |
| 13 | * tomoyo_warn_oom - Print out of memory warning message. |
| 14 | * |
| 15 | * @function: Function's name. |
| 16 | */ |
| 17 | void tomoyo_warn_oom(const char *function) |
| 18 | { |
| 19 | /* Reduce error messages. */ |
| 20 | static pid_t tomoyo_last_pid; |
| 21 | const pid_t pid = current->pid; |
| 22 | if (tomoyo_last_pid != pid) { |
| 23 | printk(KERN_WARNING "ERROR: Out of memory at %s.\n", |
| 24 | function); |
| 25 | tomoyo_last_pid = pid; |
| 26 | } |
| 27 | if (!tomoyo_policy_loaded) |
| 28 | panic("MAC Initialization failed.\n"); |
| 29 | } |
| 30 | |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 31 | /* Memoy currently used by policy/audit log/query. */ |
| 32 | unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT]; |
| 33 | /* Memory quota for "policy"/"audit log"/"query". */ |
| 34 | unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT]; |
| 35 | |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 36 | /** |
| 37 | * tomoyo_memory_ok - Check memory quota. |
| 38 | * |
| 39 | * @ptr: Pointer to allocated memory. |
| 40 | * |
| 41 | * Returns true on success, false otherwise. |
| 42 | * |
| 43 | * Returns true if @ptr is not NULL and quota not exceeded, false otherwise. |
Tetsuo Handa | a427fd1 | 2011-09-25 17:51:06 +0900 | [diff] [blame] | 44 | * |
| 45 | * Caller holds tomoyo_policy_lock mutex. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 46 | */ |
| 47 | bool tomoyo_memory_ok(void *ptr) |
| 48 | { |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 49 | if (ptr) { |
| 50 | const size_t s = ksize(ptr); |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 51 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s; |
Tetsuo Handa | a427fd1 | 2011-09-25 17:51:06 +0900 | [diff] [blame] | 52 | if (!tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] || |
| 53 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <= |
| 54 | tomoyo_memory_quota[TOMOYO_MEMORY_POLICY]) |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 55 | return true; |
Tetsuo Handa | a427fd1 | 2011-09-25 17:51:06 +0900 | [diff] [blame] | 56 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 57 | } |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 58 | tomoyo_warn_oom(__func__); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * tomoyo_commit_ok - Check memory quota. |
| 64 | * |
| 65 | * @data: Data to copy from. |
| 66 | * @size: Size in byte. |
| 67 | * |
| 68 | * Returns pointer to allocated memory on success, NULL otherwise. |
| 69 | * @data is zero-cleared on success. |
Tetsuo Handa | a427fd1 | 2011-09-25 17:51:06 +0900 | [diff] [blame] | 70 | * |
| 71 | * Caller holds tomoyo_policy_lock mutex. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 72 | */ |
| 73 | void *tomoyo_commit_ok(void *data, const unsigned int size) |
| 74 | { |
| 75 | void *ptr = kzalloc(size, GFP_NOFS); |
| 76 | if (tomoyo_memory_ok(ptr)) { |
| 77 | memmove(ptr, data, size); |
| 78 | memset(data, 0, size); |
| 79 | return ptr; |
| 80 | } |
Xiaochen Wang | cfc64fd | 2011-03-31 00:27:32 +0900 | [diff] [blame] | 81 | kfree(ptr); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | /** |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 86 | * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group". |
| 87 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 88 | * @param: Pointer to "struct tomoyo_acl_param". |
| 89 | * @idx: Index number. |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 90 | * |
| 91 | * Returns pointer to "struct tomoyo_group" on success, NULL otherwise. |
| 92 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 93 | struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, |
| 94 | const u8 idx) |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 95 | { |
| 96 | struct tomoyo_group e = { }; |
| 97 | struct tomoyo_group *group = NULL; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 98 | struct list_head *list; |
| 99 | const char *group_name = tomoyo_read_token(param); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 100 | bool found = false; |
| 101 | if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP) |
| 102 | return NULL; |
| 103 | e.group_name = tomoyo_get_name(group_name); |
| 104 | if (!e.group_name) |
| 105 | return NULL; |
| 106 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 107 | goto out; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 108 | list = ¶m->ns->group_list[idx]; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 109 | list_for_each_entry(group, list, head.list) { |
Tetsuo Handa | f9732ea | 2011-09-25 17:50:23 +0900 | [diff] [blame] | 110 | if (e.group_name != group->group_name || |
| 111 | atomic_read(&group->head.users) == TOMOYO_GC_IN_PROGRESS) |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 112 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 113 | atomic_inc(&group->head.users); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 114 | found = true; |
| 115 | break; |
| 116 | } |
| 117 | if (!found) { |
| 118 | struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e)); |
| 119 | if (entry) { |
| 120 | INIT_LIST_HEAD(&entry->member_list); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 121 | atomic_set(&entry->head.users, 1); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 122 | list_add_tail_rcu(&entry->head.list, list); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 123 | group = entry; |
| 124 | found = true; |
| 125 | } |
| 126 | } |
| 127 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 128 | out: |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 129 | tomoyo_put_name(e.group_name); |
| 130 | return found ? group : NULL; |
| 131 | } |
| 132 | |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 133 | /* |
| 134 | * tomoyo_name_list is used for holding string data used by TOMOYO. |
| 135 | * Since same string data is likely used for multiple times (e.g. |
| 136 | * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of |
| 137 | * "const struct tomoyo_path_info *". |
| 138 | */ |
| 139 | struct list_head tomoyo_name_list[TOMOYO_MAX_HASH]; |
| 140 | |
| 141 | /** |
| 142 | * tomoyo_get_name - Allocate permanent memory for string data. |
| 143 | * |
| 144 | * @name: The string to store into the permernent memory. |
| 145 | * |
| 146 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. |
| 147 | */ |
| 148 | const struct tomoyo_path_info *tomoyo_get_name(const char *name) |
| 149 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 150 | struct tomoyo_name *ptr; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 151 | unsigned int hash; |
| 152 | int len; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 153 | struct list_head *head; |
| 154 | |
| 155 | if (!name) |
| 156 | return NULL; |
| 157 | len = strlen(name) + 1; |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 158 | hash = full_name_hash(NULL, (const unsigned char *) name, len - 1); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 159 | head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)]; |
| 160 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 161 | return NULL; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 162 | list_for_each_entry(ptr, head, head.list) { |
Tetsuo Handa | f9732ea | 2011-09-25 17:50:23 +0900 | [diff] [blame] | 163 | if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name) || |
| 164 | atomic_read(&ptr->head.users) == TOMOYO_GC_IN_PROGRESS) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 165 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 166 | atomic_inc(&ptr->head.users); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 167 | goto out; |
| 168 | } |
| 169 | ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS); |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 170 | if (tomoyo_memory_ok(ptr)) { |
| 171 | ptr->entry.name = ((char *) ptr) + sizeof(*ptr); |
| 172 | memmove((char *) ptr->entry.name, name, len); |
| 173 | atomic_set(&ptr->head.users, 1); |
| 174 | tomoyo_fill_path_info(&ptr->entry); |
| 175 | list_add_tail(&ptr->head.list, head); |
| 176 | } else { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 177 | kfree(ptr); |
| 178 | ptr = NULL; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 179 | } |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 180 | out: |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 181 | mutex_unlock(&tomoyo_policy_lock); |
| 182 | return ptr ? &ptr->entry : NULL; |
| 183 | } |
| 184 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 185 | /* Initial namespace.*/ |
| 186 | struct tomoyo_policy_namespace tomoyo_kernel_namespace; |
| 187 | |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 188 | /** |
| 189 | * tomoyo_mm_init - Initialize mm related code. |
| 190 | */ |
| 191 | void __init tomoyo_mm_init(void) |
| 192 | { |
| 193 | int idx; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 194 | for (idx = 0; idx < TOMOYO_MAX_HASH; idx++) |
| 195 | INIT_LIST_HEAD(&tomoyo_name_list[idx]); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 196 | tomoyo_kernel_namespace.name = "<kernel>"; |
| 197 | tomoyo_init_policy_namespace(&tomoyo_kernel_namespace); |
| 198 | tomoyo_kernel_domain.ns = &tomoyo_kernel_namespace; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 199 | INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 200 | tomoyo_kernel_domain.domainname = tomoyo_get_name("<kernel>"); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 201 | list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 202 | } |