blob: 05450b17c57fa71fe14827ac2c2e11ed1229c772 [file] [log] [blame]
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09001/*
2 * security/tomoyo/domain.c
3 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09004 * Domain transition functions for TOMOYO.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09005 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09007 */
8
9#include "common.h"
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090010#include <linux/binfmts.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090012
13/* Variables definitions.*/
14
15/* The initial domain. */
16struct tomoyo_domain_info tomoyo_kernel_domain;
17
Tetsuo Handa237ab452010-06-12 20:46:22 +090018/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090019 * tomoyo_update_policy - Update an entry for exception policy.
20 *
21 * @new_entry: Pointer to "struct tomoyo_acl_info".
22 * @size: Size of @new_entry in bytes.
23 * @is_delete: True if it is a delete request.
24 * @list: Pointer to "struct list_head".
25 * @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,
32 bool is_delete, struct list_head *list,
33 bool (*check_duplicate) (const struct tomoyo_acl_head
34 *,
35 const struct tomoyo_acl_head
36 *))
37{
38 int error = is_delete ? -ENOENT : -ENOMEM;
39 struct tomoyo_acl_head *entry;
40
41 if (mutex_lock_interruptible(&tomoyo_policy_lock))
42 return -ENOMEM;
43 list_for_each_entry_rcu(entry, list, list) {
44 if (!check_duplicate(entry, new_entry))
45 continue;
46 entry->is_deleted = is_delete;
47 error = 0;
48 break;
49 }
50 if (error && !is_delete) {
51 entry = tomoyo_commit_ok(new_entry, size);
52 if (entry) {
53 list_add_tail_rcu(&entry->list, list);
54 error = 0;
55 }
56 }
57 mutex_unlock(&tomoyo_policy_lock);
58 return error;
59}
60
61/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090062 * tomoyo_update_domain - Update an entry for domain policy.
63 *
64 * @new_entry: Pointer to "struct tomoyo_acl_info".
65 * @size: Size of @new_entry in bytes.
66 * @is_delete: True if it is a delete request.
67 * @domain: Pointer to "struct tomoyo_domain_info".
68 * @check_duplicate: Callback function to find duplicated entry.
69 * @merge_duplicate: Callback function to merge duplicated entry.
70 *
71 * Returns 0 on success, negative value otherwise.
72 *
73 * Caller holds tomoyo_read_lock().
74 */
75int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
76 bool is_delete, struct tomoyo_domain_info *domain,
77 bool (*check_duplicate) (const struct tomoyo_acl_info
78 *,
79 const struct tomoyo_acl_info
80 *),
81 bool (*merge_duplicate) (struct tomoyo_acl_info *,
82 struct tomoyo_acl_info *,
83 const bool))
84{
85 int error = is_delete ? -ENOENT : -ENOMEM;
86 struct tomoyo_acl_info *entry;
87
88 if (mutex_lock_interruptible(&tomoyo_policy_lock))
89 return error;
90 list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
91 if (!check_duplicate(entry, new_entry))
92 continue;
93 if (merge_duplicate)
94 entry->is_deleted = merge_duplicate(entry, new_entry,
95 is_delete);
96 else
97 entry->is_deleted = is_delete;
98 error = 0;
99 break;
100 }
101 if (error && !is_delete) {
102 entry = tomoyo_commit_ok(new_entry, size);
103 if (entry) {
104 list_add_tail_rcu(&entry->list, &domain->acl_info_list);
105 error = 0;
106 }
107 }
108 mutex_unlock(&tomoyo_policy_lock);
109 return error;
110}
111
Tetsuo Handa99a85252010-06-16 16:22:51 +0900112void tomoyo_check_acl(struct tomoyo_request_info *r,
113 bool (*check_entry) (const struct tomoyo_request_info *,
114 const struct tomoyo_acl_info *))
115{
116 const struct tomoyo_domain_info *domain = r->domain;
117 struct tomoyo_acl_info *ptr;
118
119 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
120 if (ptr->is_deleted || ptr->type != r->param_type)
121 continue;
122 if (check_entry(r, ptr)) {
123 r->granted = true;
124 return;
125 }
126 }
127 r->granted = false;
128}
129
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900130/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900131LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900132
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900133struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
134struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
135
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900136/**
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900137 * tomoyo_get_last_name - Get last component of a domainname.
138 *
139 * @domain: Pointer to "struct tomoyo_domain_info".
140 *
141 * Returns the last component of the domainname.
142 */
143const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
144{
145 const char *cp0 = domain->domainname->name;
146 const char *cp1 = strrchr(cp0, ' ');
147
148 if (cp1)
149 return cp1 + 1;
150 return cp0;
151}
152
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900153static bool tomoyo_same_transition_control_entry(const struct tomoyo_acl_head *
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900154 a,
155 const struct tomoyo_acl_head *
156 b)
157{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900158 const struct tomoyo_transition_control *p1 = container_of(a,
159 typeof(*p1),
160 head);
161 const struct tomoyo_transition_control *p2 = container_of(b,
162 typeof(*p2),
163 head);
164 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900165 && p1->domainname == p2->domainname
166 && p1->program == p2->program;
167}
168
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900169/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900170 * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900171 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900172 * @domainname: The name of domain. Maybe NULL.
173 * @program: The name of program. Maybe NULL.
174 * @type: Type of transition.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900175 * @is_delete: True if it is a delete request.
176 *
177 * Returns 0 on success, negative value otherwise.
178 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900179static int tomoyo_update_transition_control_entry(const char *domainname,
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900180 const char *program,
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900181 const u8 type,
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900182 const bool is_delete)
183{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900184 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900185 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900186 if (program) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900187 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900188 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900189 e.program = tomoyo_get_name(program);
190 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900191 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900192 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900193 if (domainname) {
194 if (!tomoyo_correct_domain(domainname)) {
195 if (!tomoyo_correct_path(domainname))
196 goto out;
197 e.is_last_name = true;
198 }
199 e.domainname = tomoyo_get_name(domainname);
200 if (!e.domainname)
201 goto out;
202 }
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900203 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900204 &tomoyo_policy_list
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900205 [TOMOYO_ID_TRANSITION_CONTROL],
206 tomoyo_same_transition_control_entry);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900207 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900208 tomoyo_put_name(e.domainname);
209 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900210 return error;
211}
212
213/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900214 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900215 *
216 * @data: String to parse.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900217 * @is_delete: True if it is a delete request.
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900218 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900219 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900220 * Returns 0 on success, negative value otherwise.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900221 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900222int tomoyo_write_transition_control(char *data, const bool is_delete,
223 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900224{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900225 char *domainname = strstr(data, " from ");
226 if (domainname) {
227 *domainname = '\0';
228 domainname += 6;
229 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
230 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
231 domainname = data;
232 data = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900233 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900234 return tomoyo_update_transition_control_entry(domainname, data, type,
235 is_delete);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900236}
237
238/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900239 * tomoyo_transition_type - Get domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900240 *
241 * @domainname: The name of domain.
242 * @program: The name of program.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900243 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900244 * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
245 * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
246 * @program suppresses domain transition, others otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900247 *
248 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900249 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900250static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
251 const struct tomoyo_path_info *program)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900252{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900253 const struct tomoyo_transition_control *ptr;
254 const char *last_name = tomoyo_last_word(domainname->name);
255 u8 type;
256 for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
257 next:
258 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
259 [TOMOYO_ID_TRANSITION_CONTROL],
260 head.list) {
261 if (ptr->head.is_deleted || ptr->type != type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900262 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900263 if (ptr->domainname) {
264 if (!ptr->is_last_name) {
265 if (ptr->domainname != domainname)
266 continue;
267 } else {
268 /*
269 * Use direct strcmp() since this is
270 * unlikely used.
271 */
272 if (strcmp(ptr->domainname->name,
273 last_name))
274 continue;
275 }
276 }
277 if (ptr->program &&
278 tomoyo_pathcmp(ptr->program, program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900279 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900280 if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
281 /*
282 * Do not check for initialize_domain if
283 * no_initialize_domain matched.
284 */
285 type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
286 goto next;
287 }
288 goto done;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900289 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900290 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900291 done:
292 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900293}
294
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900295static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
296 const struct tomoyo_acl_head *b)
297{
298 const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
299 head);
300 const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
301 head);
302 return p1->original_name == p2->original_name &&
303 p1->aggregated_name == p2->aggregated_name;
304}
305
Tetsuo Handa10843072010-06-03 20:38:03 +0900306/**
307 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
308 *
309 * @original_name: The original program's name.
310 * @aggregated_name: The program name to use.
311 * @is_delete: True if it is a delete request.
312 *
313 * Returns 0 on success, negative value otherwise.
314 *
315 * Caller holds tomoyo_read_lock().
316 */
317static int tomoyo_update_aggregator_entry(const char *original_name,
318 const char *aggregated_name,
319 const bool is_delete)
320{
Tetsuo Handa10843072010-06-03 20:38:03 +0900321 struct tomoyo_aggregator_entry e = { };
322 int error = is_delete ? -ENOENT : -ENOMEM;
323
Tetsuo Handa75093152010-06-16 16:23:55 +0900324 if (!tomoyo_correct_path(original_name) ||
325 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900326 return -EINVAL;
327 e.original_name = tomoyo_get_name(original_name);
328 e.aggregated_name = tomoyo_get_name(aggregated_name);
329 if (!e.original_name || !e.aggregated_name ||
330 e.aggregated_name->is_patterned) /* No patterns allowed. */
331 goto out;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900332 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900333 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900334 tomoyo_same_aggregator_entry);
Tetsuo Handa10843072010-06-03 20:38:03 +0900335 out:
336 tomoyo_put_name(e.original_name);
337 tomoyo_put_name(e.aggregated_name);
338 return error;
339}
340
341/**
Tetsuo Handa10843072010-06-03 20:38:03 +0900342 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
343 *
344 * @data: String to parse.
345 * @is_delete: True if it is a delete request.
346 *
347 * Returns 0 on success, negative value otherwise.
348 *
349 * Caller holds tomoyo_read_lock().
350 */
351int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
352{
353 char *cp = strchr(data, ' ');
354
355 if (!cp)
356 return -EINVAL;
357 *cp++ = '\0';
358 return tomoyo_update_aggregator_entry(data, cp, is_delete);
359}
360
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900361/**
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900362 * tomoyo_find_or_assign_new_domain - Create a domain.
363 *
364 * @domainname: The name of domain.
365 * @profile: Profile number to assign if the domain was newly created.
366 *
367 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900368 *
369 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900370 */
371struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
372 domainname,
373 const u8 profile)
374{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900375 struct tomoyo_domain_info *entry;
Tetsuo Handa29282382010-05-06 00:18:15 +0900376 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900377 const struct tomoyo_path_info *saved_domainname;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900378 bool found = false;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900379
Tetsuo Handa75093152010-06-16 16:23:55 +0900380 if (!tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900381 return NULL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900382 saved_domainname = tomoyo_get_name(domainname);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900383 if (!saved_domainname)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900384 return NULL;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900385 entry = kzalloc(sizeof(*entry), GFP_NOFS);
Tetsuo Handa29282382010-05-06 00:18:15 +0900386 if (mutex_lock_interruptible(&tomoyo_policy_lock))
387 goto out;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900388 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
389 if (domain->is_deleted ||
390 tomoyo_pathcmp(saved_domainname, domain->domainname))
391 continue;
392 found = true;
393 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900394 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900395 if (!found && tomoyo_memory_ok(entry)) {
396 INIT_LIST_HEAD(&entry->acl_info_list);
397 entry->domainname = saved_domainname;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900398 saved_domainname = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900399 entry->profile = profile;
400 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
401 domain = entry;
402 entry = NULL;
403 found = true;
404 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900405 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900406 out:
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900407 tomoyo_put_name(saved_domainname);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900408 kfree(entry);
409 return found ? domain : NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900410}
411
412/**
413 * tomoyo_find_next_domain - Find a domain.
414 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900415 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900416 *
417 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900418 *
419 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900420 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900421int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900422{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900423 struct tomoyo_request_info r;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900424 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900425 struct tomoyo_domain_info *old_domain = tomoyo_domain();
426 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900427 const char *original_name = bprm->filename;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900428 u8 mode;
429 bool is_enforce;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900430 int retval = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900431 bool need_kfree = false;
432 struct tomoyo_path_info rn = { }; /* real name */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900433 struct tomoyo_path_info ln; /* last name */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900434
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900435 ln.name = tomoyo_get_last_name(old_domain);
436 tomoyo_fill_path_info(&ln);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900437 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
438 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900439 if (!tmp)
440 goto out;
441
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900442 retry:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900443 if (need_kfree) {
444 kfree(rn.name);
445 need_kfree = false;
446 }
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900447 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900448 retval = -ENOENT;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900449 rn.name = tomoyo_realpath_nofollow(original_name);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900450 if (!rn.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900451 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900452 tomoyo_fill_path_info(&rn);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900453 need_kfree = true;
454
Tetsuo Handa10843072010-06-03 20:38:03 +0900455 /* Check 'aggregator' directive. */
456 {
457 struct tomoyo_aggregator_entry *ptr;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900458 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
459 [TOMOYO_ID_AGGREGATOR], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900460 if (ptr->head.is_deleted ||
Tetsuo Handa10843072010-06-03 20:38:03 +0900461 !tomoyo_path_matches_pattern(&rn,
462 ptr->original_name))
463 continue;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900464 kfree(rn.name);
Tetsuo Handa10843072010-06-03 20:38:03 +0900465 need_kfree = false;
466 /* This is OK because it is read only. */
467 rn = *ptr->aggregated_name;
468 break;
469 }
470 }
471
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900472 /* Check execute permission. */
Tetsuo Handa05336de2010-06-16 16:20:24 +0900473 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900474 if (retval == TOMOYO_RETRY_REQUEST)
475 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900476 if (retval < 0)
477 goto out;
478
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900479 /* Calculate domain to transit to. */
480 switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
481 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900482 /* Transit to the child of tomoyo_kernel_domain domain. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900483 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
484 "%s", rn.name);
485 break;
486 case TOMOYO_TRANSITION_CONTROL_KEEP:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900487 /* Keep current domain. */
488 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900489 break;
490 default:
491 if (old_domain == &tomoyo_kernel_domain &&
492 !tomoyo_policy_loaded) {
493 /*
494 * Needn't to transit from kernel domain before
495 * starting /sbin/init. But transit from kernel domain
496 * if executing initializers because they might start
497 * before /sbin/init.
498 */
499 domain = old_domain;
500 } else {
501 /* Normal domain transition. */
502 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
503 old_domain->domainname->name, rn.name);
504 }
505 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900506 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900507 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900508 goto done;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900509 domain = tomoyo_find_domain(tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900510 if (domain)
511 goto done;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900512 if (is_enforce) {
513 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900514 "%s\n", tmp);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900515 if (error == TOMOYO_RETRY_REQUEST)
516 goto retry;
517 if (error < 0)
518 goto done;
519 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900520 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900521 done:
522 if (domain)
523 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900524 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900525 if (is_enforce)
526 retval = -EPERM;
527 else
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900528 old_domain->transition_failed = true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900529 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900530 if (!domain)
531 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900532 /* Update reference count on "struct tomoyo_domain_info". */
533 atomic_inc(&domain->users);
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900534 bprm->cred->security = domain;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900535 if (need_kfree)
536 kfree(rn.name);
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900537 kfree(tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900538 return retval;
539}