TOMOYO: Small cleanup.
[deliverable/linux.git] / security / tomoyo / domain.c
CommitLineData
26a2a1c9
KT
1/*
2 * security/tomoyo/domain.c
3 *
c3ef1500 4 * Domain transition functions for TOMOYO.
26a2a1c9 5 *
c3ef1500 6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
26a2a1c9
KT
7 */
8
9#include "common.h"
26a2a1c9 10#include <linux/binfmts.h>
5a0e3ad6 11#include <linux/slab.h>
26a2a1c9
KT
12
13/* Variables definitions.*/
14
15/* The initial domain. */
16struct tomoyo_domain_info tomoyo_kernel_domain;
17
36f5e1ff
TH
18/**
19 * 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
237ab459
TH
61/**
62 * 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
99a85259
TH
112void 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
a230f9e7 130/* The list for "struct tomoyo_domain_info". */
26a2a1c9 131LIST_HEAD(tomoyo_domain_list);
26a2a1c9 132
a230f9e7
TH
133struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
134struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
135
26a2a1c9
KT
136/**
137 * 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
5448ec4f 153static bool tomoyo_same_transition_control_entry(const struct tomoyo_acl_head *
36f5e1ff
TH
154 a,
155 const struct tomoyo_acl_head *
156 b)
157{
5448ec4f
TH
158 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
36f5e1ff
TH
165 && p1->domainname == p2->domainname
166 && p1->program == p2->program;
167}
168
26a2a1c9 169/**
5448ec4f 170 * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
26a2a1c9 171 *
5448ec4f
TH
172 * @domainname: The name of domain. Maybe NULL.
173 * @program: The name of program. Maybe NULL.
174 * @type: Type of transition.
26a2a1c9
KT
175 * @is_delete: True if it is a delete request.
176 *
177 * Returns 0 on success, negative value otherwise.
178 */
5448ec4f 179static int tomoyo_update_transition_control_entry(const char *domainname,
26a2a1c9 180 const char *program,
5448ec4f 181 const u8 type,
26a2a1c9
KT
182 const bool is_delete)
183{
5448ec4f 184 struct tomoyo_transition_control e = { .type = type };
ca0b7df3 185 int error = is_delete ? -ENOENT : -ENOMEM;
5448ec4f
TH
186 if (program) {
187 if (!tomoyo_correct_path(program))
188 return -EINVAL;
189 e.program = tomoyo_get_name(program);
190 if (!e.program)
191 goto out;
192 }
26a2a1c9 193 if (domainname) {
5448ec4f
TH
194 if (!tomoyo_correct_domain(domainname)) {
195 if (!tomoyo_correct_path(domainname))
196 goto out;
9e4b50e9 197 e.is_last_name = true;
5448ec4f 198 }
9e4b50e9
TH
199 e.domainname = tomoyo_get_name(domainname);
200 if (!e.domainname)
ca0b7df3 201 goto out;
26a2a1c9 202 }
36f5e1ff 203 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7 204 &tomoyo_policy_list
5448ec4f
TH
205 [TOMOYO_ID_TRANSITION_CONTROL],
206 tomoyo_same_transition_control_entry);
ca0b7df3 207 out:
9e4b50e9
TH
208 tomoyo_put_name(e.domainname);
209 tomoyo_put_name(e.program);
26a2a1c9
KT
210 return error;
211}
212
26a2a1c9 213/**
5448ec4f 214 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
26a2a1c9
KT
215 *
216 * @data: String to parse.
26a2a1c9 217 * @is_delete: True if it is a delete request.
5448ec4f 218 * @type: Type of this entry.
26a2a1c9
KT
219 *
220 * Returns 0 on success, negative value otherwise.
221 */
5448ec4f
TH
222int tomoyo_write_transition_control(char *data, const bool is_delete,
223 const u8 type)
26a2a1c9 224{
5448ec4f
TH
225 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;
26a2a1c9 233 }
5448ec4f 234 return tomoyo_update_transition_control_entry(domainname, data, type,
26a2a1c9
KT
235 is_delete);
236}
237
238/**
5448ec4f 239 * tomoyo_transition_type - Get domain transition type.
26a2a1c9
KT
240 *
241 * @domainname: The name of domain.
242 * @program: The name of program.
26a2a1c9 243 *
5448ec4f
TH
244 * 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.
fdb8ebb7
TH
247 *
248 * Caller holds tomoyo_read_lock().
26a2a1c9 249 */
5448ec4f
TH
250static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
251 const struct tomoyo_path_info *program)
26a2a1c9 252{
5448ec4f
TH
253 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)
26a2a1c9 262 continue;
5448ec4f
TH
263 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))
26a2a1c9 279 continue;
5448ec4f
TH
280 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;
26a2a1c9 289 }
26a2a1c9 290 }
5448ec4f
TH
291 done:
292 return type;
26a2a1c9
KT
293}
294
36f5e1ff
TH
295static 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
1084307c
TH
306/**
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{
1084307c
TH
321 struct tomoyo_aggregator_entry e = { };
322 int error = is_delete ? -ENOENT : -ENOMEM;
323
75093152
TH
324 if (!tomoyo_correct_path(original_name) ||
325 !tomoyo_correct_path(aggregated_name))
1084307c
TH
326 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;
36f5e1ff 332 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7 333 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
36f5e1ff 334 tomoyo_same_aggregator_entry);
1084307c
TH
335 out:
336 tomoyo_put_name(e.original_name);
337 tomoyo_put_name(e.aggregated_name);
338 return error;
339}
340
1084307c
TH
341/**
342 * 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
26a2a1c9
KT
361/**
362 * 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.
fdb8ebb7
TH
368 *
369 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
370 */
371struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
372 domainname,
373 const u8 profile)
374{
ca0b7df3 375 struct tomoyo_domain_info *entry;
29282381 376 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 377 const struct tomoyo_path_info *saved_domainname;
ca0b7df3 378 bool found = false;
26a2a1c9 379
75093152 380 if (!tomoyo_correct_domain(domainname))
ca0b7df3 381 return NULL;
bf24fb01 382 saved_domainname = tomoyo_get_name(domainname);
26a2a1c9 383 if (!saved_domainname)
ca0b7df3 384 return NULL;
4e5d6f7e 385 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29282381
TH
386 if (mutex_lock_interruptible(&tomoyo_policy_lock))
387 goto out;
ca0b7df3
TH
388 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;
394 }
395 if (!found && tomoyo_memory_ok(entry)) {
396 INIT_LIST_HEAD(&entry->acl_info_list);
397 entry->domainname = saved_domainname;
bf24fb01 398 saved_domainname = NULL;
ca0b7df3
TH
399 entry->profile = profile;
400 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
401 domain = entry;
402 entry = NULL;
403 found = true;
26a2a1c9 404 }
f737d95d 405 mutex_unlock(&tomoyo_policy_lock);
29282381 406 out:
bf24fb01 407 tomoyo_put_name(saved_domainname);
ca0b7df3
TH
408 kfree(entry);
409 return found ? domain : NULL;
26a2a1c9
KT
410}
411
412/**
413 * tomoyo_find_next_domain - Find a domain.
414 *
56f8c9bc 415 * @bprm: Pointer to "struct linux_binprm".
26a2a1c9
KT
416 *
417 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
418 *
419 * Caller holds tomoyo_read_lock().
26a2a1c9 420 */
56f8c9bc 421int tomoyo_find_next_domain(struct linux_binprm *bprm)
26a2a1c9 422{
17fcfbd9 423 struct tomoyo_request_info r;
c8c57e84 424 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
26a2a1c9
KT
425 struct tomoyo_domain_info *old_domain = tomoyo_domain();
426 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 427 const char *original_name = bprm->filename;
57c2590f
TH
428 u8 mode;
429 bool is_enforce;
26a2a1c9 430 int retval = -ENOMEM;
c8c57e84
TH
431 bool need_kfree = false;
432 struct tomoyo_path_info rn = { }; /* real name */
17fcfbd9 433 struct tomoyo_path_info ln; /* last name */
26a2a1c9 434
c8c57e84
TH
435 ln.name = tomoyo_get_last_name(old_domain);
436 tomoyo_fill_path_info(&ln);
57c2590f
TH
437 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
438 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
26a2a1c9
KT
439 if (!tmp)
440 goto out;
441
17fcfbd9 442 retry:
c8c57e84
TH
443 if (need_kfree) {
444 kfree(rn.name);
445 need_kfree = false;
446 }
0617c7ff 447 /* Get symlink's pathname of program. */
26a2a1c9 448 retval = -ENOENT;
0617c7ff 449 rn.name = tomoyo_realpath_nofollow(original_name);
c8c57e84 450 if (!rn.name)
26a2a1c9 451 goto out;
c8c57e84
TH
452 tomoyo_fill_path_info(&rn);
453 need_kfree = true;
454
1084307c
TH
455 /* Check 'aggregator' directive. */
456 {
457 struct tomoyo_aggregator_entry *ptr;
a230f9e7
TH
458 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
459 [TOMOYO_ID_AGGREGATOR], head.list) {
82e0f001 460 if (ptr->head.is_deleted ||
1084307c
TH
461 !tomoyo_path_matches_pattern(&rn,
462 ptr->original_name))
463 continue;
0617c7ff 464 kfree(rn.name);
1084307c
TH
465 need_kfree = false;
466 /* This is OK because it is read only. */
467 rn = *ptr->aggregated_name;
468 break;
469 }
470 }
471
26a2a1c9 472 /* Check execute permission. */
05336dee 473 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
17fcfbd9
TH
474 if (retval == TOMOYO_RETRY_REQUEST)
475 goto retry;
26a2a1c9
KT
476 if (retval < 0)
477 goto out;
478
5448ec4f
TH
479 /* Calculate domain to transit to. */
480 switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
481 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
26a2a1c9 482 /* Transit to the child of tomoyo_kernel_domain domain. */
5448ec4f
TH
483 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
484 "%s", rn.name);
485 break;
486 case TOMOYO_TRANSITION_CONTROL_KEEP:
26a2a1c9
KT
487 /* Keep current domain. */
488 domain = old_domain;
5448ec4f
TH
489 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;
26a2a1c9 506 }
c8c57e84 507 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
26a2a1c9 508 goto done;
c8c57e84 509 domain = tomoyo_find_domain(tmp);
26a2a1c9
KT
510 if (domain)
511 goto done;
17fcfbd9
TH
512 if (is_enforce) {
513 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
c8c57e84 514 "%s\n", tmp);
17fcfbd9
TH
515 if (error == TOMOYO_RETRY_REQUEST)
516 goto retry;
517 if (error < 0)
518 goto done;
519 }
c8c57e84 520 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
26a2a1c9
KT
521 done:
522 if (domain)
523 goto out;
c8c57e84 524 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
26a2a1c9
KT
525 if (is_enforce)
526 retval = -EPERM;
527 else
ea13ddba 528 old_domain->transition_failed = true;
26a2a1c9 529 out:
56f8c9bc
TH
530 if (!domain)
531 domain = old_domain;
ec8e6a4e
TH
532 /* Update reference count on "struct tomoyo_domain_info". */
533 atomic_inc(&domain->users);
56f8c9bc 534 bprm->cred->security = domain;
c8c57e84
TH
535 if (need_kfree)
536 kfree(rn.name);
8e2d39a1 537 kfree(tmp);
26a2a1c9
KT
538 return retval;
539}
This page took 0.158668 seconds and 5 git commands to generate.