blob: 1b570bde7a3b2f56a672772cbb710d3b2ad16041 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002/*
3 * security/tomoyo/memory.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 */
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 */
17void 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;
Tetsuo Handacdcf6722019-01-24 18:37:35 +090022
Tetsuo Handac3ef1502010-05-17 10:12:46 +090023 if (tomoyo_last_pid != pid) {
Tetsuo Handacdcf6722019-01-24 18:37:35 +090024 pr_warn("ERROR: Out of memory at %s.\n", function);
Tetsuo Handac3ef1502010-05-17 10:12:46 +090025 tomoyo_last_pid = pid;
26 }
27 if (!tomoyo_policy_loaded)
28 panic("MAC Initialization failed.\n");
29}
30
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090031/* Memoy currently used by policy/audit log/query. */
32unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
33/* Memory quota for "policy"/"audit log"/"query". */
34unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
35
Tetsuo Handac3ef1502010-05-17 10:12:46 +090036/**
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 Handaa427fd12011-09-25 17:51:06 +090044 *
45 * Caller holds tomoyo_policy_lock mutex.
Tetsuo Handac3ef1502010-05-17 10:12:46 +090046 */
47bool tomoyo_memory_ok(void *ptr)
48{
Tetsuo Handab22b8b92011-06-26 23:21:50 +090049 if (ptr) {
50 const size_t s = ksize(ptr);
Tetsuo Handacdcf6722019-01-24 18:37:35 +090051
Tetsuo Handab22b8b92011-06-26 23:21:50 +090052 tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s;
Tetsuo Handaa427fd12011-09-25 17:51:06 +090053 if (!tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] ||
54 tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <=
55 tomoyo_memory_quota[TOMOYO_MEMORY_POLICY])
Tetsuo Handab22b8b92011-06-26 23:21:50 +090056 return true;
Tetsuo Handaa427fd12011-09-25 17:51:06 +090057 tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s;
Tetsuo Handac3ef1502010-05-17 10:12:46 +090058 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +090059 tomoyo_warn_oom(__func__);
60 return false;
61}
62
63/**
64 * tomoyo_commit_ok - Check memory quota.
65 *
66 * @data: Data to copy from.
67 * @size: Size in byte.
68 *
69 * Returns pointer to allocated memory on success, NULL otherwise.
70 * @data is zero-cleared on success.
Tetsuo Handaa427fd12011-09-25 17:51:06 +090071 *
72 * Caller holds tomoyo_policy_lock mutex.
Tetsuo Handac3ef1502010-05-17 10:12:46 +090073 */
74void *tomoyo_commit_ok(void *data, const unsigned int size)
75{
Zheng Zengkai1b6b9242020-11-26 22:38:15 +080076 void *ptr = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
Tetsuo Handacdcf6722019-01-24 18:37:35 +090077
Tetsuo Handac3ef1502010-05-17 10:12:46 +090078 if (tomoyo_memory_ok(ptr)) {
79 memmove(ptr, data, size);
80 memset(data, 0, size);
81 return ptr;
82 }
Xiaochen Wangcfc64fd2011-03-31 00:27:32 +090083 kfree(ptr);
Tetsuo Handac3ef1502010-05-17 10:12:46 +090084 return NULL;
85}
86
87/**
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090088 * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
89 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +090090 * @param: Pointer to "struct tomoyo_acl_param".
91 * @idx: Index number.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090092 *
93 * Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
94 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +090095struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
96 const u8 idx)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090097{
98 struct tomoyo_group e = { };
99 struct tomoyo_group *group = NULL;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900100 struct list_head *list;
101 const char *group_name = tomoyo_read_token(param);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900102 bool found = false;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900103
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900104 if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
105 return NULL;
106 e.group_name = tomoyo_get_name(group_name);
107 if (!e.group_name)
108 return NULL;
109 if (mutex_lock_interruptible(&tomoyo_policy_lock))
110 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900111 list = &param->ns->group_list[idx];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900112 list_for_each_entry(group, list, head.list) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +0900113 if (e.group_name != group->group_name ||
114 atomic_read(&group->head.users) == TOMOYO_GC_IN_PROGRESS)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900115 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900116 atomic_inc(&group->head.users);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900117 found = true;
118 break;
119 }
120 if (!found) {
121 struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900122
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900123 if (entry) {
124 INIT_LIST_HEAD(&entry->member_list);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900125 atomic_set(&entry->head.users, 1);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900126 list_add_tail_rcu(&entry->head.list, list);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900127 group = entry;
128 found = true;
129 }
130 }
131 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900132out:
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900133 tomoyo_put_name(e.group_name);
134 return found ? group : NULL;
135}
136
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900137/*
138 * tomoyo_name_list is used for holding string data used by TOMOYO.
139 * Since same string data is likely used for multiple times (e.g.
140 * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
141 * "const struct tomoyo_path_info *".
142 */
143struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
144
145/**
146 * tomoyo_get_name - Allocate permanent memory for string data.
147 *
148 * @name: The string to store into the permernent memory.
149 *
150 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
151 */
152const struct tomoyo_path_info *tomoyo_get_name(const char *name)
153{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900154 struct tomoyo_name *ptr;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900155 unsigned int hash;
156 int len;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900157 struct list_head *head;
158
159 if (!name)
160 return NULL;
161 len = strlen(name) + 1;
Linus Torvalds8387ff22016-06-10 07:51:30 -0700162 hash = full_name_hash(NULL, (const unsigned char *) name, len - 1);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900163 head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
164 if (mutex_lock_interruptible(&tomoyo_policy_lock))
165 return NULL;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900166 list_for_each_entry(ptr, head, head.list) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +0900167 if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name) ||
168 atomic_read(&ptr->head.users) == TOMOYO_GC_IN_PROGRESS)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900169 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900170 atomic_inc(&ptr->head.users);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900171 goto out;
172 }
Zheng Zengkai1b6b9242020-11-26 22:38:15 +0800173 ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS | __GFP_NOWARN);
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900174 if (tomoyo_memory_ok(ptr)) {
175 ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
176 memmove((char *) ptr->entry.name, name, len);
177 atomic_set(&ptr->head.users, 1);
178 tomoyo_fill_path_info(&ptr->entry);
179 list_add_tail(&ptr->head.list, head);
180 } else {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900181 kfree(ptr);
182 ptr = NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900183 }
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900184out:
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900185 mutex_unlock(&tomoyo_policy_lock);
186 return ptr ? &ptr->entry : NULL;
187}
188
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900189/* Initial namespace.*/
190struct tomoyo_policy_namespace tomoyo_kernel_namespace;
191
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900192/**
193 * tomoyo_mm_init - Initialize mm related code.
194 */
195void __init tomoyo_mm_init(void)
196{
197 int idx;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900198
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900199 for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
200 INIT_LIST_HEAD(&tomoyo_name_list[idx]);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900201 tomoyo_kernel_namespace.name = "<kernel>";
202 tomoyo_init_policy_namespace(&tomoyo_kernel_namespace);
203 tomoyo_kernel_domain.ns = &tomoyo_kernel_namespace;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900204 INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900205 tomoyo_kernel_domain.domainname = tomoyo_get_name("<kernel>");
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900206 list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900207}