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