blob: 7869d6a9980bfc4e7c0cf70a826d7b5661aaef67 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09002/*
3 * security/tomoyo/domain.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09006 */
7
8#include "common.h"
Ingo Molnarb2d09102017-02-04 01:27:20 +01009
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090010#include <linux/binfmts.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010012#include <linux/rculist.h>
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090013
14/* Variables definitions.*/
15
16/* The initial domain. */
17struct tomoyo_domain_info tomoyo_kernel_domain;
18
Tetsuo Handa237ab452010-06-12 20:46:22 +090019/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090020 * tomoyo_update_policy - Update an entry for exception policy.
21 *
22 * @new_entry: Pointer to "struct tomoyo_acl_info".
23 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090024 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090025 * @check_duplicate: Callback function to find duplicated entry.
26 *
27 * Returns 0 on success, negative value otherwise.
28 *
29 * Caller holds tomoyo_read_lock().
30 */
31int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090032 struct tomoyo_acl_param *param,
Tetsuo Handacdcf6722019-01-24 18:37:35 +090033 bool (*check_duplicate)(const struct tomoyo_acl_head
34 *,
35 const struct tomoyo_acl_head
36 *))
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090037{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090038 int error = param->is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090039 struct tomoyo_acl_head *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090040 struct list_head *list = param->list;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090041
42 if (mutex_lock_interruptible(&tomoyo_policy_lock))
43 return -ENOMEM;
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +090044 list_for_each_entry_rcu(entry, list, list,
45 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +090046 if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS)
47 continue;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090048 if (!check_duplicate(entry, new_entry))
49 continue;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090050 entry->is_deleted = param->is_delete;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090051 error = 0;
52 break;
53 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090054 if (error && !param->is_delete) {
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090055 entry = tomoyo_commit_ok(new_entry, size);
56 if (entry) {
57 list_add_tail_rcu(&entry->list, list);
58 error = 0;
59 }
60 }
61 mutex_unlock(&tomoyo_policy_lock);
62 return error;
63}
64
65/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090066 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
67 *
68 * @a: Pointer to "struct tomoyo_acl_info".
69 * @b: Pointer to "struct tomoyo_acl_info".
70 *
71 * Returns true if @a == @b, false otherwise.
72 */
73static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
74 const struct tomoyo_acl_info *b)
75{
Tetsuo Handa2066a362011-07-08 13:21:37 +090076 return a->type == b->type && a->cond == b->cond;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090077}
78
79/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090080 * tomoyo_update_domain - Update an entry for domain policy.
81 *
82 * @new_entry: Pointer to "struct tomoyo_acl_info".
83 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090084 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa237ab452010-06-12 20:46:22 +090085 * @check_duplicate: Callback function to find duplicated entry.
86 * @merge_duplicate: Callback function to merge duplicated entry.
87 *
88 * Returns 0 on success, negative value otherwise.
89 *
90 * Caller holds tomoyo_read_lock().
91 */
92int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090093 struct tomoyo_acl_param *param,
Tetsuo Handacdcf6722019-01-24 18:37:35 +090094 bool (*check_duplicate)(const struct tomoyo_acl_info
95 *,
96 const struct tomoyo_acl_info
97 *),
98 bool (*merge_duplicate)(struct tomoyo_acl_info *,
99 struct tomoyo_acl_info *,
100 const bool))
Tetsuo Handa237ab452010-06-12 20:46:22 +0900101{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900102 const bool is_delete = param->is_delete;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900103 int error = is_delete ? -ENOENT : -ENOMEM;
104 struct tomoyo_acl_info *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900105 struct list_head * const list = param->list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900106
Tetsuo Handa2066a362011-07-08 13:21:37 +0900107 if (param->data[0]) {
108 new_entry->cond = tomoyo_get_condition(param);
109 if (!new_entry->cond)
110 return -EINVAL;
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900111 /*
112 * Domain transition preference is allowed for only
113 * "file execute" entries.
114 */
115 if (new_entry->cond->transit &&
116 !(new_entry->type == TOMOYO_TYPE_PATH_ACL &&
117 container_of(new_entry, struct tomoyo_path_acl, head)
118 ->perm == 1 << TOMOYO_TYPE_EXECUTE))
119 goto out;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900120 }
Tetsuo Handa237ab452010-06-12 20:46:22 +0900121 if (mutex_lock_interruptible(&tomoyo_policy_lock))
Tetsuo Handa2066a362011-07-08 13:21:37 +0900122 goto out;
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +0900123 list_for_each_entry_rcu(entry, list, list,
124 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +0900125 if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS)
126 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900127 if (!tomoyo_same_acl_head(entry, new_entry) ||
128 !check_duplicate(entry, new_entry))
Tetsuo Handa237ab452010-06-12 20:46:22 +0900129 continue;
130 if (merge_duplicate)
131 entry->is_deleted = merge_duplicate(entry, new_entry,
132 is_delete);
133 else
134 entry->is_deleted = is_delete;
135 error = 0;
136 break;
137 }
138 if (error && !is_delete) {
139 entry = tomoyo_commit_ok(new_entry, size);
140 if (entry) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900141 list_add_tail_rcu(&entry->list, list);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900142 error = 0;
143 }
144 }
145 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa2066a362011-07-08 13:21:37 +0900146out:
147 tomoyo_put_condition(new_entry->cond);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900148 return error;
149}
150
Tetsuo Handa32997142011-06-26 23:19:28 +0900151/**
152 * tomoyo_check_acl - Do permission check.
153 *
154 * @r: Pointer to "struct tomoyo_request_info".
155 * @check_entry: Callback function to check type specific parameters.
156 *
157 * Returns 0 on success, negative value otherwise.
158 *
159 * Caller holds tomoyo_read_lock().
160 */
Tetsuo Handa99a85252010-06-16 16:22:51 +0900161void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900162 bool (*check_entry)(struct tomoyo_request_info *,
163 const struct tomoyo_acl_info *))
Tetsuo Handa99a85252010-06-16 16:22:51 +0900164{
165 const struct tomoyo_domain_info *domain = r->domain;
166 struct tomoyo_acl_info *ptr;
Tetsuo Handa32997142011-06-26 23:19:28 +0900167 const struct list_head *list = &domain->acl_info_list;
Tetsuo Handa4b425642019-01-24 18:37:36 +0900168 u16 i = 0;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900169
Tetsuo Handa32997142011-06-26 23:19:28 +0900170retry:
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +0900171 list_for_each_entry_rcu(ptr, list, list,
172 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900173 if (ptr->is_deleted || ptr->type != r->param_type)
174 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900175 if (!check_entry(r, ptr))
176 continue;
177 if (!tomoyo_condition(r, ptr->cond))
178 continue;
Tetsuo Handa1f067a62011-09-10 15:24:56 +0900179 r->matched_acl = ptr;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900180 r->granted = true;
181 return;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900182 }
Tetsuo Handa4b425642019-01-24 18:37:36 +0900183 for (; i < TOMOYO_MAX_ACL_GROUPS; i++) {
184 if (!test_bit(i, domain->group))
185 continue;
186 list = &domain->ns->acl_group[i++];
Tetsuo Handa32997142011-06-26 23:19:28 +0900187 goto retry;
188 }
Tetsuo Handa99a85252010-06-16 16:22:51 +0900189 r->granted = false;
190}
191
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900192/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900193LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900194
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900195/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900196 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900197 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900198 * @name: Domainname to check.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900199 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900200 * Returns the last word of @domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900201 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900202static const char *tomoyo_last_word(const char *name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900203{
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900204 const char *cp = strrchr(name, ' ');
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900205
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900206 if (cp)
207 return cp + 1;
208 return name;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900209}
210
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900211/**
212 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
213 *
214 * @a: Pointer to "struct tomoyo_acl_head".
215 * @b: Pointer to "struct tomoyo_acl_head".
216 *
217 * Returns true if @a == @b, false otherwise.
218 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900219static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
220 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900221{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900222 const struct tomoyo_transition_control *p1 = container_of(a,
223 typeof(*p1),
224 head);
225 const struct tomoyo_transition_control *p2 = container_of(b,
226 typeof(*p2),
227 head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900228
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900229 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900230 && p1->domainname == p2->domainname
231 && p1->program == p2->program;
232}
233
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900234/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900235 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900236 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900237 * @param: Pointer to "struct tomoyo_acl_param".
238 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900239 *
240 * Returns 0 on success, negative value otherwise.
241 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900242int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
243 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900244{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900245 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900246 int error = param->is_delete ? -ENOENT : -ENOMEM;
247 char *program = param->data;
248 char *domainname = strstr(program, " from ");
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900249
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900250 if (domainname) {
251 *domainname = '\0';
252 domainname += 6;
253 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
254 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
255 domainname = program;
256 program = NULL;
257 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900258 if (program && strcmp(program, "any")) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900259 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900260 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900261 e.program = tomoyo_get_name(program);
262 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900263 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900264 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900265 if (domainname && strcmp(domainname, "any")) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900266 if (!tomoyo_correct_domain(domainname)) {
267 if (!tomoyo_correct_path(domainname))
268 goto out;
269 e.is_last_name = true;
270 }
271 e.domainname = tomoyo_get_name(domainname);
272 if (!e.domainname)
273 goto out;
274 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900275 param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900276 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900277 tomoyo_same_transition_control);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900278out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900279 tomoyo_put_name(e.domainname);
280 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900281 return error;
282}
283
284/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900285 * tomoyo_scan_transition - Try to find specific domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900286 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900287 * @list: Pointer to "struct list_head".
288 * @domainname: The name of current domain.
289 * @program: The name of requested program.
290 * @last_name: The last component of @domainname.
291 * @type: One of values in "enum tomoyo_transition_type".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900292 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900293 * Returns true if found one, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900294 *
295 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900296 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900297static inline bool tomoyo_scan_transition
298(const struct list_head *list, const struct tomoyo_path_info *domainname,
299 const struct tomoyo_path_info *program, const char *last_name,
300 const enum tomoyo_transition_type type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900301{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900302 const struct tomoyo_transition_control *ptr;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900303
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +0900304 list_for_each_entry_rcu(ptr, list, head.list,
305 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900306 if (ptr->head.is_deleted || ptr->type != type)
307 continue;
308 if (ptr->domainname) {
309 if (!ptr->is_last_name) {
310 if (ptr->domainname != domainname)
311 continue;
312 } else {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900313 /*
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900314 * Use direct strcmp() since this is
315 * unlikely used.
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900316 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900317 if (strcmp(ptr->domainname->name, last_name))
318 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900319 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900320 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900321 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
322 continue;
323 return true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900324 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900325 return false;
326}
327
328/**
329 * tomoyo_transition_type - Get domain transition type.
330 *
331 * @ns: Pointer to "struct tomoyo_policy_namespace".
332 * @domainname: The name of current domain.
333 * @program: The name of requested program.
334 *
335 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
336 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
337 * executing @program reinitializes domain transition within that namespace,
338 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
339 * others otherwise.
340 *
341 * Caller holds tomoyo_read_lock().
342 */
343static enum tomoyo_transition_type tomoyo_transition_type
344(const struct tomoyo_policy_namespace *ns,
345 const struct tomoyo_path_info *domainname,
346 const struct tomoyo_path_info *program)
347{
348 const char *last_name = tomoyo_last_word(domainname->name);
349 enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900350
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900351 while (type < TOMOYO_MAX_TRANSITION_TYPE) {
352 const struct list_head * const list =
353 &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900354
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900355 if (!tomoyo_scan_transition(list, domainname, program,
356 last_name, type)) {
357 type++;
358 continue;
359 }
360 if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
361 type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
362 break;
363 /*
364 * Do not check for reset_domain if no_reset_domain matched.
365 * Do not check for initialize_domain if no_initialize_domain
366 * matched.
367 */
368 type++;
369 type++;
370 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900371 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900372}
373
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900374/**
375 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
376 *
377 * @a: Pointer to "struct tomoyo_acl_head".
378 * @b: Pointer to "struct tomoyo_acl_head".
379 *
380 * Returns true if @a == @b, false otherwise.
381 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900382static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
383 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900384{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900385 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
386 head);
387 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
388 head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900389
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900390 return p1->original_name == p2->original_name &&
391 p1->aggregated_name == p2->aggregated_name;
392}
393
Tetsuo Handa10843072010-06-03 20:38:03 +0900394/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900395 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900396 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900397 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa10843072010-06-03 20:38:03 +0900398 *
399 * Returns 0 on success, negative value otherwise.
400 *
401 * Caller holds tomoyo_read_lock().
402 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900403int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
Tetsuo Handa10843072010-06-03 20:38:03 +0900404{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900405 struct tomoyo_aggregator e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900406 int error = param->is_delete ? -ENOENT : -ENOMEM;
407 const char *original_name = tomoyo_read_token(param);
408 const char *aggregated_name = tomoyo_read_token(param);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900409
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900410 if (!tomoyo_correct_word(original_name) ||
Tetsuo Handa75093152010-06-16 16:23:55 +0900411 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900412 return -EINVAL;
413 e.original_name = tomoyo_get_name(original_name);
414 e.aggregated_name = tomoyo_get_name(aggregated_name);
415 if (!e.original_name || !e.aggregated_name ||
416 e.aggregated_name->is_patterned) /* No patterns allowed. */
417 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900418 param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900419 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900420 tomoyo_same_aggregator);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900421out:
Tetsuo Handa10843072010-06-03 20:38:03 +0900422 tomoyo_put_name(e.original_name);
423 tomoyo_put_name(e.aggregated_name);
424 return error;
425}
426
427/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900428 * tomoyo_find_namespace - Find specified namespace.
429 *
430 * @name: Name of namespace to find.
431 * @len: Length of @name.
432 *
433 * Returns pointer to "struct tomoyo_policy_namespace" if found,
434 * NULL otherwise.
435 *
436 * Caller holds tomoyo_read_lock().
437 */
438static struct tomoyo_policy_namespace *tomoyo_find_namespace
439(const char *name, const unsigned int len)
440{
441 struct tomoyo_policy_namespace *ns;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900442
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900443 list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
444 if (strncmp(name, ns->name, len) ||
445 (name[len] && name[len] != ' '))
446 continue;
447 return ns;
448 }
449 return NULL;
450}
451
452/**
453 * tomoyo_assign_namespace - Create a new namespace.
454 *
455 * @domainname: Name of namespace to create.
456 *
457 * Returns pointer to "struct tomoyo_policy_namespace" on success,
458 * NULL otherwise.
459 *
460 * Caller holds tomoyo_read_lock().
461 */
462struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
463{
464 struct tomoyo_policy_namespace *ptr;
465 struct tomoyo_policy_namespace *entry;
466 const char *cp = domainname;
467 unsigned int len = 0;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900468
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900469 while (*cp && *cp++ != ' ')
470 len++;
471 ptr = tomoyo_find_namespace(domainname, len);
472 if (ptr)
473 return ptr;
474 if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
475 return NULL;
476 entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
477 if (!entry)
478 return NULL;
479 if (mutex_lock_interruptible(&tomoyo_policy_lock))
480 goto out;
481 ptr = tomoyo_find_namespace(domainname, len);
482 if (!ptr && tomoyo_memory_ok(entry)) {
483 char *name = (char *) (entry + 1);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900484
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900485 ptr = entry;
486 memmove(name, domainname, len);
487 name[len] = '\0';
488 entry->name = name;
489 tomoyo_init_policy_namespace(entry);
490 entry = NULL;
491 }
492 mutex_unlock(&tomoyo_policy_lock);
493out:
494 kfree(entry);
495 return ptr;
496}
497
498/**
499 * tomoyo_namespace_jump - Check for namespace jump.
500 *
501 * @domainname: Name of domain.
502 *
503 * Returns true if namespace differs, false otherwise.
504 */
505static bool tomoyo_namespace_jump(const char *domainname)
506{
507 const char *namespace = tomoyo_current_namespace()->name;
508 const int len = strlen(namespace);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900509
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900510 return strncmp(domainname, namespace, len) ||
511 (domainname[len] && domainname[len] != ' ');
512}
513
514/**
515 * tomoyo_assign_domain - Create a domain or a namespace.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900516 *
517 * @domainname: The name of domain.
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900518 * @transit: True if transit to domain found or created.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900519 *
520 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900521 *
522 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900523 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900524struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900525 const bool transit)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900526{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900527 struct tomoyo_domain_info e = { };
528 struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
529 bool created = false;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900530
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900531 if (entry) {
532 if (transit) {
533 /*
534 * Since namespace is created at runtime, profiles may
535 * not be created by the moment the process transits to
536 * that domain. Do not perform domain transition if
537 * profile for that domain is not yet created.
538 */
Tetsuo Handae00fb3f2011-09-27 11:48:53 +0900539 if (tomoyo_policy_loaded &&
540 !entry->ns->profile_ptr[entry->profile])
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900541 return NULL;
542 }
543 return entry;
544 }
545 /* Requested domain does not exist. */
546 /* Don't create requested domain if domainname is invalid. */
547 if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
548 !tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900549 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900550 /*
551 * Since definition of profiles and acl_groups may differ across
552 * namespaces, do not inherit "use_profile" and "use_group" settings
553 * by automatically creating requested domain upon domain transition.
554 */
555 if (transit && tomoyo_namespace_jump(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900556 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900557 e.ns = tomoyo_assign_namespace(domainname);
558 if (!e.ns)
559 return NULL;
560 /*
561 * "use_profile" and "use_group" settings for automatically created
562 * domains are inherited from current domain. These are 0 for manually
563 * created domains.
564 */
565 if (transit) {
566 const struct tomoyo_domain_info *domain = tomoyo_domain();
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900567
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900568 e.profile = domain->profile;
Tetsuo Handa4b425642019-01-24 18:37:36 +0900569 memcpy(e.group, domain->group, sizeof(e.group));
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900570 }
571 e.domainname = tomoyo_get_name(domainname);
572 if (!e.domainname)
573 return NULL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900574 if (mutex_lock_interruptible(&tomoyo_policy_lock))
575 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900576 entry = tomoyo_find_domain(domainname);
577 if (!entry) {
578 entry = tomoyo_commit_ok(&e, sizeof(e));
579 if (entry) {
580 INIT_LIST_HEAD(&entry->acl_info_list);
581 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
582 created = true;
583 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900584 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900585 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900586out:
587 tomoyo_put_name(e.domainname);
588 if (entry && transit) {
589 if (created) {
590 struct tomoyo_request_info r;
Tetsuo Handa4b425642019-01-24 18:37:36 +0900591 int i;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900592
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900593 tomoyo_init_request_info(&r, entry,
594 TOMOYO_MAC_FILE_EXECUTE);
595 r.granted = false;
596 tomoyo_write_log(&r, "use_profile %u\n",
597 entry->profile);
Tetsuo Handa4b425642019-01-24 18:37:36 +0900598 for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
599 if (test_bit(i, entry->group))
600 tomoyo_write_log(&r, "use_group %u\n",
601 i);
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900602 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900603 }
604 }
605 return entry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900606}
607
608/**
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900609 * tomoyo_environ - Check permission for environment variable names.
610 *
611 * @ee: Pointer to "struct tomoyo_execve".
612 *
613 * Returns 0 on success, negative value otherwise.
614 */
615static int tomoyo_environ(struct tomoyo_execve *ee)
616{
617 struct tomoyo_request_info *r = &ee->r;
618 struct linux_binprm *bprm = ee->bprm;
619 /* env_page.data is allocated by tomoyo_dump_page(). */
620 struct tomoyo_page_dump env_page = { };
621 char *arg_ptr; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
622 int arg_len = 0;
623 unsigned long pos = bprm->p;
624 int offset = pos % PAGE_SIZE;
625 int argv_count = bprm->argc;
626 int envp_count = bprm->envc;
627 int error = -ENOMEM;
628
629 ee->r.type = TOMOYO_MAC_ENVIRON;
630 ee->r.profile = r->domain->profile;
631 ee->r.mode = tomoyo_get_mode(r->domain->ns, ee->r.profile,
632 TOMOYO_MAC_ENVIRON);
633 if (!r->mode || !envp_count)
634 return 0;
635 arg_ptr = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
636 if (!arg_ptr)
637 goto out;
638 while (error == -ENOMEM) {
639 if (!tomoyo_dump_page(bprm, pos, &env_page))
640 goto out;
641 pos += PAGE_SIZE - offset;
642 /* Read. */
643 while (argv_count && offset < PAGE_SIZE) {
644 if (!env_page.data[offset++])
645 argv_count--;
646 }
647 if (argv_count) {
648 offset = 0;
649 continue;
650 }
651 while (offset < PAGE_SIZE) {
652 const unsigned char c = env_page.data[offset++];
653
654 if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
655 if (c == '=') {
656 arg_ptr[arg_len++] = '\0';
657 } else if (c == '\\') {
658 arg_ptr[arg_len++] = '\\';
659 arg_ptr[arg_len++] = '\\';
660 } else if (c > ' ' && c < 127) {
661 arg_ptr[arg_len++] = c;
662 } else {
663 arg_ptr[arg_len++] = '\\';
664 arg_ptr[arg_len++] = (c >> 6) + '0';
665 arg_ptr[arg_len++]
666 = ((c >> 3) & 7) + '0';
667 arg_ptr[arg_len++] = (c & 7) + '0';
668 }
669 } else {
670 arg_ptr[arg_len] = '\0';
671 }
672 if (c)
673 continue;
674 if (tomoyo_env_perm(r, arg_ptr)) {
675 error = -EPERM;
676 break;
677 }
678 if (!--envp_count) {
679 error = 0;
680 break;
681 }
682 arg_len = 0;
683 }
684 offset = 0;
685 }
686out:
687 if (r->mode != TOMOYO_CONFIG_ENFORCING)
688 error = 0;
689 kfree(env_page.data);
690 kfree(arg_ptr);
691 return error;
692}
693
694/**
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900695 * tomoyo_find_next_domain - Find a domain.
696 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900697 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900698 *
699 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900700 *
701 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900702 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900703int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900704{
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900705 struct tomoyo_domain_info *old_domain = tomoyo_domain();
706 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900707 const char *original_name = bprm->filename;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900708 int retval = -ENOMEM;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900709 bool reject_on_transition_failure = false;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900710 const struct tomoyo_path_info *candidate;
711 struct tomoyo_path_info exename;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900712 struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900713
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900714 if (!ee)
715 return -ENOMEM;
716 ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
717 if (!ee->tmp) {
718 kfree(ee);
719 return -ENOMEM;
720 }
721 /* ee->dump->data is allocated by tomoyo_dump_page(). */
722 tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
723 ee->r.ee = ee;
724 ee->bprm = bprm;
725 ee->r.obj = &ee->obj;
726 ee->obj.path1 = bprm->file->f_path;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900727 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900728 retval = -ENOENT;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900729 exename.name = tomoyo_realpath_nofollow(original_name);
730 if (!exename.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900731 goto out;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900732 tomoyo_fill_path_info(&exename);
733retry:
Tetsuo Handa10843072010-06-03 20:38:03 +0900734 /* Check 'aggregator' directive. */
735 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900736 struct tomoyo_aggregator *ptr;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900737 struct list_head *list =
738 &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900739
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900740 /* Check 'aggregator' directive. */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900741 candidate = &exename;
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +0900742 list_for_each_entry_rcu(ptr, list, head.list,
743 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900744 if (ptr->head.is_deleted ||
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900745 !tomoyo_path_matches_pattern(&exename,
Tetsuo Handa10843072010-06-03 20:38:03 +0900746 ptr->original_name))
747 continue;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900748 candidate = ptr->aggregated_name;
Tetsuo Handa10843072010-06-03 20:38:03 +0900749 break;
750 }
751 }
752
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900753 /* Check execute permission. */
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900754 retval = tomoyo_execute_permission(&ee->r, candidate);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900755 if (retval == TOMOYO_RETRY_REQUEST)
756 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900757 if (retval < 0)
758 goto out;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900759 /*
760 * To be able to specify domainnames with wildcards, use the
761 * pathname specified in the policy (which may contain
762 * wildcard) rather than the pathname passed to execve()
763 * (which never contains wildcard).
764 */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900765 if (ee->r.param.path.matched_path)
766 candidate = ee->r.param.path.matched_path;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900767
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900768 /*
769 * Check for domain transition preference if "file execute" matched.
770 * If preference is given, make do_execve() fail if domain transition
771 * has failed, for domain transition preference should be used with
772 * destination domain defined.
773 */
774 if (ee->transition) {
775 const char *domainname = ee->transition->name;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900776
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900777 reject_on_transition_failure = true;
778 if (!strcmp(domainname, "keep"))
779 goto force_keep_domain;
780 if (!strcmp(domainname, "child"))
781 goto force_child_domain;
782 if (!strcmp(domainname, "reset"))
783 goto force_reset_domain;
784 if (!strcmp(domainname, "initialize"))
785 goto force_initialize_domain;
786 if (!strcmp(domainname, "parent")) {
787 char *cp;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900788
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900789 strncpy(ee->tmp, old_domain->domainname->name,
790 TOMOYO_EXEC_TMPSIZE - 1);
791 cp = strrchr(ee->tmp, ' ');
792 if (cp)
793 *cp = '\0';
794 } else if (*domainname == '<')
795 strncpy(ee->tmp, domainname, TOMOYO_EXEC_TMPSIZE - 1);
796 else
797 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
798 old_domain->domainname->name, domainname);
799 goto force_jump_domain;
800 }
801 /*
802 * No domain transition preference specified.
803 * Calculate domain to transit to.
804 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900805 switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900806 candidate)) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900807 case TOMOYO_TRANSITION_CONTROL_RESET:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900808force_reset_domain:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900809 /* Transit to the root of specified namespace. */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900810 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>",
811 candidate->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900812 /*
813 * Make do_execve() fail if domain transition across namespaces
814 * has failed.
815 */
816 reject_on_transition_failure = true;
817 break;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900818 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900819force_initialize_domain:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900820 /* Transit to the child of current namespace's root. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900821 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900822 old_domain->ns->name, candidate->name);
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900823 break;
824 case TOMOYO_TRANSITION_CONTROL_KEEP:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900825force_keep_domain:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900826 /* Keep current domain. */
827 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900828 break;
829 default:
830 if (old_domain == &tomoyo_kernel_domain &&
831 !tomoyo_policy_loaded) {
832 /*
833 * Needn't to transit from kernel domain before
834 * starting /sbin/init. But transit from kernel domain
835 * if executing initializers because they might start
836 * before /sbin/init.
837 */
838 domain = old_domain;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900839 break;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900840 }
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900841force_child_domain:
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900842 /* Normal domain transition. */
843 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
844 old_domain->domainname->name, candidate->name);
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900845 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900846 }
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900847force_jump_domain:
Tetsuo Handa7c759642011-06-26 23:15:31 +0900848 if (!domain)
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900849 domain = tomoyo_assign_domain(ee->tmp, true);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900850 if (domain)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900851 retval = 0;
852 else if (reject_on_transition_failure) {
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900853 pr_warn("ERROR: Domain '%s' not ready.\n", ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900854 retval = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900855 } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900856 retval = -ENOMEM;
857 else {
858 retval = 0;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900859 if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
860 old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900861 ee->r.granted = false;
862 tomoyo_write_log(&ee->r, "%s", tomoyo_dif
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900863 [TOMOYO_DIF_TRANSITION_FAILED]);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900864 pr_warn("ERROR: Domain '%s' not defined.\n", ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900865 }
866 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900867 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900868 if (!domain)
869 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900870 /* Update reference count on "struct tomoyo_domain_info". */
Tetsuo Handa8c6cb982019-01-19 23:11:40 +0900871 {
872 struct tomoyo_task *s = tomoyo_task(current);
873
874 s->old_domain_info = s->domain_info;
875 s->domain_info = domain;
876 atomic_inc(&domain->users);
877 }
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900878 kfree(exename.name);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900879 if (!retval) {
880 ee->r.domain = domain;
881 retval = tomoyo_environ(ee);
882 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900883 kfree(ee->tmp);
884 kfree(ee->dump.data);
885 kfree(ee);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900886 return retval;
887}
Tetsuo Handa5b636852011-07-08 13:24:54 +0900888
889/**
890 * tomoyo_dump_page - Dump a page to buffer.
891 *
892 * @bprm: Pointer to "struct linux_binprm".
893 * @pos: Location to dump.
894 * @dump: Poiner to "struct tomoyo_page_dump".
895 *
896 * Returns true on success, false otherwise.
897 */
898bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
899 struct tomoyo_page_dump *dump)
900{
901 struct page *page;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900902
903 /* dump->data is released by tomoyo_find_next_domain(). */
Tetsuo Handa5b636852011-07-08 13:24:54 +0900904 if (!dump->data) {
905 dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
906 if (!dump->data)
907 return false;
908 }
909 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
910#ifdef CONFIG_MMU
Dave Hansen1e987792016-02-12 13:01:54 -0800911 /*
912 * This is called at execve() time in order to dig around
913 * in the argv/environment of the new proceess
914 * (represented by bprm). 'current' is the process doing
915 * the execve().
916 */
917 if (get_user_pages_remote(current, bprm->mm, pos, 1,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -0800918 FOLL_FORCE, &page, NULL, NULL) <= 0)
Tetsuo Handa5b636852011-07-08 13:24:54 +0900919 return false;
920#else
921 page = bprm->page[pos / PAGE_SIZE];
922#endif
923 if (page != dump->page) {
924 const unsigned int offset = pos % PAGE_SIZE;
925 /*
926 * Maybe kmap()/kunmap() should be used here.
927 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
928 * So do I.
929 */
Cong Wangc58e0372011-11-25 23:26:35 +0800930 char *kaddr = kmap_atomic(page);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900931
Tetsuo Handa5b636852011-07-08 13:24:54 +0900932 dump->page = page;
933 memcpy(dump->data + offset, kaddr + offset,
934 PAGE_SIZE - offset);
Cong Wangc58e0372011-11-25 23:26:35 +0800935 kunmap_atomic(kaddr);
Tetsuo Handa5b636852011-07-08 13:24:54 +0900936 }
937 /* Same with put_arg_page(page) in fs/exec.c */
938#ifdef CONFIG_MMU
939 put_page(page);
940#endif
941 return true;
942}