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