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