Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/memory.c |
| 3 | * |
| 4 | * Memory management functions for TOMOYO. |
| 5 | * |
| 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
| 7 | */ |
| 8 | |
| 9 | #include <linux/hash.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include "common.h" |
| 12 | |
| 13 | /** |
| 14 | * tomoyo_warn_oom - Print out of memory warning message. |
| 15 | * |
| 16 | * @function: Function's name. |
| 17 | */ |
| 18 | void tomoyo_warn_oom(const char *function) |
| 19 | { |
| 20 | /* Reduce error messages. */ |
| 21 | static pid_t tomoyo_last_pid; |
| 22 | const pid_t pid = current->pid; |
| 23 | if (tomoyo_last_pid != pid) { |
| 24 | printk(KERN_WARNING "ERROR: Out of memory at %s.\n", |
| 25 | function); |
| 26 | tomoyo_last_pid = pid; |
| 27 | } |
| 28 | if (!tomoyo_policy_loaded) |
| 29 | panic("MAC Initialization failed.\n"); |
| 30 | } |
| 31 | |
| 32 | /* Memory allocated for policy. */ |
| 33 | static atomic_t tomoyo_policy_memory_size; |
| 34 | /* Quota for holding policy. */ |
| 35 | static unsigned int tomoyo_quota_for_policy; |
| 36 | |
| 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 | { |
| 48 | size_t s = ptr ? ksize(ptr) : 0; |
| 49 | atomic_add(s, &tomoyo_policy_memory_size); |
| 50 | if (ptr && (!tomoyo_quota_for_policy || |
| 51 | atomic_read(&tomoyo_policy_memory_size) |
| 52 | <= tomoyo_quota_for_policy)) { |
| 53 | memset(ptr, 0, s); |
| 54 | return true; |
| 55 | } |
| 56 | atomic_sub(s, &tomoyo_policy_memory_size); |
| 57 | tomoyo_warn_oom(__func__); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * tomoyo_commit_ok - Check memory quota. |
| 63 | * |
| 64 | * @data: Data to copy from. |
| 65 | * @size: Size in byte. |
| 66 | * |
| 67 | * Returns pointer to allocated memory on success, NULL otherwise. |
| 68 | * @data is zero-cleared on success. |
| 69 | */ |
| 70 | void *tomoyo_commit_ok(void *data, const unsigned int size) |
| 71 | { |
| 72 | void *ptr = kzalloc(size, GFP_NOFS); |
| 73 | if (tomoyo_memory_ok(ptr)) { |
| 74 | memmove(ptr, data, size); |
| 75 | memset(data, 0, size); |
| 76 | return ptr; |
| 77 | } |
Xiaochen Wang | cfc64fd | 2011-03-31 00:27:32 +0900 | [diff] [blame] | 78 | kfree(ptr); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * tomoyo_memory_free - Free memory for elements. |
| 84 | * |
| 85 | * @ptr: Pointer to allocated memory. |
| 86 | */ |
| 87 | void tomoyo_memory_free(void *ptr) |
| 88 | { |
| 89 | atomic_sub(ksize(ptr), &tomoyo_policy_memory_size); |
| 90 | kfree(ptr); |
| 91 | } |
| 92 | |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 93 | /** |
| 94 | * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group". |
| 95 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 96 | * @param: Pointer to "struct tomoyo_acl_param". |
| 97 | * @idx: Index number. |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 98 | * |
| 99 | * Returns pointer to "struct tomoyo_group" on success, NULL otherwise. |
| 100 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 101 | struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, |
| 102 | const u8 idx) |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 103 | { |
| 104 | struct tomoyo_group e = { }; |
| 105 | struct tomoyo_group *group = NULL; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 106 | struct list_head *list; |
| 107 | const char *group_name = tomoyo_read_token(param); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 108 | bool found = false; |
| 109 | if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP) |
| 110 | return NULL; |
| 111 | e.group_name = tomoyo_get_name(group_name); |
| 112 | if (!e.group_name) |
| 113 | return NULL; |
| 114 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 115 | goto out; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 116 | list = &tomoyo_group_list[idx]; |
| 117 | list_for_each_entry(group, list, head.list) { |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 118 | if (e.group_name != group->group_name) |
| 119 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 120 | atomic_inc(&group->head.users); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 121 | found = true; |
| 122 | break; |
| 123 | } |
| 124 | if (!found) { |
| 125 | struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e)); |
| 126 | if (entry) { |
| 127 | INIT_LIST_HEAD(&entry->member_list); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 128 | atomic_set(&entry->head.users, 1); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 129 | list_add_tail_rcu(&entry->head.list, list); |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 130 | group = entry; |
| 131 | found = true; |
| 132 | } |
| 133 | } |
| 134 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 135 | out: |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 136 | tomoyo_put_name(e.group_name); |
| 137 | return found ? group : NULL; |
| 138 | } |
| 139 | |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 140 | /* |
| 141 | * tomoyo_name_list is used for holding string data used by TOMOYO. |
| 142 | * Since same string data is likely used for multiple times (e.g. |
| 143 | * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of |
| 144 | * "const struct tomoyo_path_info *". |
| 145 | */ |
| 146 | struct list_head tomoyo_name_list[TOMOYO_MAX_HASH]; |
| 147 | |
| 148 | /** |
| 149 | * tomoyo_get_name - Allocate permanent memory for string data. |
| 150 | * |
| 151 | * @name: The string to store into the permernent memory. |
| 152 | * |
| 153 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. |
| 154 | */ |
| 155 | const struct tomoyo_path_info *tomoyo_get_name(const char *name) |
| 156 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 157 | struct tomoyo_name *ptr; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 158 | unsigned int hash; |
| 159 | int len; |
| 160 | int allocated_len; |
| 161 | struct list_head *head; |
| 162 | |
| 163 | if (!name) |
| 164 | return NULL; |
| 165 | len = strlen(name) + 1; |
| 166 | hash = full_name_hash((const unsigned char *) name, len - 1); |
| 167 | head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)]; |
| 168 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 169 | return NULL; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 170 | list_for_each_entry(ptr, head, head.list) { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 171 | if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name)) |
| 172 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 173 | atomic_inc(&ptr->head.users); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 174 | goto out; |
| 175 | } |
| 176 | ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS); |
| 177 | allocated_len = ptr ? ksize(ptr) : 0; |
| 178 | if (!ptr || (tomoyo_quota_for_policy && |
| 179 | atomic_read(&tomoyo_policy_memory_size) + allocated_len |
| 180 | > tomoyo_quota_for_policy)) { |
| 181 | kfree(ptr); |
| 182 | ptr = NULL; |
| 183 | tomoyo_warn_oom(__func__); |
| 184 | goto out; |
| 185 | } |
| 186 | atomic_add(allocated_len, &tomoyo_policy_memory_size); |
| 187 | ptr->entry.name = ((char *) ptr) + sizeof(*ptr); |
| 188 | memmove((char *) ptr->entry.name, name, len); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 189 | atomic_set(&ptr->head.users, 1); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 190 | tomoyo_fill_path_info(&ptr->entry); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 191 | list_add_tail(&ptr->head.list, head); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 192 | out: |
| 193 | mutex_unlock(&tomoyo_policy_lock); |
| 194 | return ptr ? &ptr->entry : NULL; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * tomoyo_mm_init - Initialize mm related code. |
| 199 | */ |
| 200 | void __init tomoyo_mm_init(void) |
| 201 | { |
| 202 | int idx; |
| 203 | |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 204 | for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++) |
| 205 | INIT_LIST_HEAD(&tomoyo_policy_list[idx]); |
| 206 | for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++) |
| 207 | INIT_LIST_HEAD(&tomoyo_group_list[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]); |
| 210 | INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list); |
| 211 | tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME); |
| 212 | list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list); |
| 213 | idx = tomoyo_read_lock(); |
| 214 | if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain) |
| 215 | panic("Can't register tomoyo_kernel_domain"); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 216 | #if 0 |
| 217 | /* Will be replaced with tomoyo_load_builtin_policy(). */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 218 | { |
| 219 | /* Load built-in policy. */ |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 220 | tomoyo_write_transition_control("/sbin/hotplug", false, |
| 221 | TOMOYO_TRANSITION_CONTROL_INITIALIZE); |
| 222 | tomoyo_write_transition_control("/sbin/modprobe", false, |
| 223 | TOMOYO_TRANSITION_CONTROL_INITIALIZE); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 224 | } |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 225 | #endif |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 226 | tomoyo_read_unlock(idx); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* Memory allocated for query lists. */ |
| 231 | unsigned int tomoyo_query_memory_size; |
| 232 | /* Quota for holding query lists. */ |
| 233 | unsigned int tomoyo_quota_for_query; |
| 234 | |
| 235 | /** |
| 236 | * tomoyo_read_memory_counter - Check for memory usage in bytes. |
| 237 | * |
| 238 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 239 | * |
| 240 | * Returns memory usage. |
| 241 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 242 | void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 243 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 244 | if (!head->r.eof) { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 245 | const unsigned int policy |
| 246 | = atomic_read(&tomoyo_policy_memory_size); |
| 247 | const unsigned int query = tomoyo_query_memory_size; |
| 248 | char buffer[64]; |
| 249 | |
| 250 | memset(buffer, 0, sizeof(buffer)); |
| 251 | if (tomoyo_quota_for_policy) |
| 252 | snprintf(buffer, sizeof(buffer) - 1, |
| 253 | " (Quota: %10u)", |
| 254 | tomoyo_quota_for_policy); |
| 255 | else |
| 256 | buffer[0] = '\0'; |
| 257 | tomoyo_io_printf(head, "Policy: %10u%s\n", policy, |
| 258 | buffer); |
| 259 | if (tomoyo_quota_for_query) |
| 260 | snprintf(buffer, sizeof(buffer) - 1, |
| 261 | " (Quota: %10u)", |
| 262 | tomoyo_quota_for_query); |
| 263 | else |
| 264 | buffer[0] = '\0'; |
| 265 | tomoyo_io_printf(head, "Query lists: %10u%s\n", query, |
| 266 | buffer); |
| 267 | tomoyo_io_printf(head, "Total: %10u\n", policy + query); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 268 | head->r.eof = true; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 269 | } |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * tomoyo_write_memory_quota - Set memory quota. |
| 274 | * |
| 275 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 276 | * |
| 277 | * Returns 0. |
| 278 | */ |
| 279 | int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head) |
| 280 | { |
| 281 | char *data = head->write_buf; |
| 282 | unsigned int size; |
| 283 | |
| 284 | if (sscanf(data, "Policy: %u", &size) == 1) |
| 285 | tomoyo_quota_for_policy = size; |
| 286 | else if (sscanf(data, "Query lists: %u", &size) == 1) |
| 287 | tomoyo_quota_for_query = size; |
| 288 | return 0; |
| 289 | } |