]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/tomoyo/domain.c
TOMOYO: Allow wildcard for execute permission.
[mirror_ubuntu-bionic-kernel.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
c3fa109a
TH
18/*
19 * tomoyo_domain_list is used for holding list of domains.
20 * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
21 * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
22 *
23 * An entry is added by
24 *
25 * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
26 * /sys/kernel/security/tomoyo/domain_policy
27 *
28 * and is deleted by
29 *
30 * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
31 * /sys/kernel/security/tomoyo/domain_policy
32 *
33 * and all entries are retrieved by
34 *
35 * # cat /sys/kernel/security/tomoyo/domain_policy
36 *
37 * A domain is added by
38 *
39 * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
40 *
41 * and is deleted by
42 *
43 * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
44 *
45 * and all domains are retrieved by
46 *
47 * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
48 *
49 * Normally, a domainname is monotonically getting longer because a domainname
50 * which the process will belong to if an execve() operation succeeds is
51 * defined as a concatenation of "current domainname" + "pathname passed to
52 * execve()".
53 * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
54 * exceptions.
55 */
26a2a1c9 56LIST_HEAD(tomoyo_domain_list);
26a2a1c9 57
26a2a1c9
KT
58/**
59 * tomoyo_get_last_name - Get last component of a domainname.
60 *
61 * @domain: Pointer to "struct tomoyo_domain_info".
62 *
63 * Returns the last component of the domainname.
64 */
65const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
66{
67 const char *cp0 = domain->domainname->name;
68 const char *cp1 = strrchr(cp0, ' ');
69
70 if (cp1)
71 return cp1 + 1;
72 return cp0;
73}
74
c3fa109a
TH
75/*
76 * tomoyo_domain_initializer_list is used for holding list of programs which
77 * triggers reinitialization of domainname. Normally, a domainname is
78 * monotonically getting longer. But sometimes, we restart daemon programs.
79 * It would be convenient for us that "a daemon started upon system boot" and
80 * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
81 * provides a way to shorten domainnames.
82 *
83 * An entry is added by
84 *
85 * # echo 'initialize_domain /usr/sbin/httpd' > \
86 * /sys/kernel/security/tomoyo/exception_policy
87 *
88 * and is deleted by
89 *
90 * # echo 'delete initialize_domain /usr/sbin/httpd' > \
91 * /sys/kernel/security/tomoyo/exception_policy
92 *
93 * and all entries are retrieved by
94 *
95 * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
96 *
97 * In the example above, /usr/sbin/httpd will belong to
98 * "<kernel> /usr/sbin/httpd" domain.
99 *
100 * You may specify a domainname using "from" keyword.
101 * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
102 * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
103 * domain to belong to "<kernel> /usr/sbin/httpd" domain.
104 *
105 * You may add "no_" prefix to "initialize_domain".
106 * "initialize_domain /usr/sbin/httpd" and
107 * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
108 * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
109 * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
110 */
847b173e 111LIST_HEAD(tomoyo_domain_initializer_list);
26a2a1c9
KT
112
113/**
114 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
115 *
116 * @domainname: The name of domain. May be NULL.
117 * @program: The name of program.
118 * @is_not: True if it is "no_initialize_domain" entry.
119 * @is_delete: True if it is a delete request.
120 *
121 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
122 *
123 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
124 */
125static int tomoyo_update_domain_initializer_entry(const char *domainname,
126 const char *program,
127 const bool is_not,
128 const bool is_delete)
129{
26a2a1c9 130 struct tomoyo_domain_initializer_entry *ptr;
9e4b50e9 131 struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
ca0b7df3 132 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9 133
3f629636
TH
134 if (!tomoyo_is_correct_path(program))
135 return -EINVAL;
26a2a1c9
KT
136 if (domainname) {
137 if (!tomoyo_is_domain_def(domainname) &&
3f629636 138 tomoyo_is_correct_path(domainname))
9e4b50e9 139 e.is_last_name = true;
17080008 140 else if (!tomoyo_is_correct_domain(domainname))
26a2a1c9 141 return -EINVAL;
9e4b50e9
TH
142 e.domainname = tomoyo_get_name(domainname);
143 if (!e.domainname)
ca0b7df3 144 goto out;
26a2a1c9 145 }
9e4b50e9
TH
146 e.program = tomoyo_get_name(program);
147 if (!e.program)
ca0b7df3 148 goto out;
29282381
TH
149 if (mutex_lock_interruptible(&tomoyo_policy_lock))
150 goto out;
fdb8ebb7 151 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
9e4b50e9 152 if (!tomoyo_is_same_domain_initializer_entry(ptr, &e))
26a2a1c9
KT
153 continue;
154 ptr->is_deleted = is_delete;
155 error = 0;
ca0b7df3 156 break;
26a2a1c9 157 }
9e4b50e9
TH
158 if (!is_delete && error) {
159 struct tomoyo_domain_initializer_entry *entry =
160 tomoyo_commit_ok(&e, sizeof(e));
161 if (entry) {
162 list_add_tail_rcu(&entry->list,
163 &tomoyo_domain_initializer_list);
164 error = 0;
165 }
26a2a1c9 166 }
f737d95d 167 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 168 out:
9e4b50e9
TH
169 tomoyo_put_name(e.domainname);
170 tomoyo_put_name(e.program);
26a2a1c9
KT
171 return error;
172}
173
174/**
175 * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
176 *
177 * @head: Pointer to "struct tomoyo_io_buffer".
178 *
179 * Returns true on success, false otherwise.
fdb8ebb7
TH
180 *
181 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
182 */
183bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
184{
185 struct list_head *pos;
186 bool done = true;
187
26a2a1c9
KT
188 list_for_each_cookie(pos, head->read_var2,
189 &tomoyo_domain_initializer_list) {
190 const char *no;
191 const char *from = "";
192 const char *domain = "";
193 struct tomoyo_domain_initializer_entry *ptr;
194 ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
195 list);
196 if (ptr->is_deleted)
197 continue;
198 no = ptr->is_not ? "no_" : "";
199 if (ptr->domainname) {
200 from = " from ";
201 domain = ptr->domainname->name;
202 }
7d2948b1
TH
203 done = tomoyo_io_printf(head,
204 "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
205 "%s%s%s\n", no, ptr->program->name,
206 from, domain);
207 if (!done)
26a2a1c9 208 break;
26a2a1c9 209 }
26a2a1c9
KT
210 return done;
211}
212
213/**
214 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
215 *
216 * @data: String to parse.
217 * @is_not: True if it is "no_initialize_domain" entry.
218 * @is_delete: True if it is a delete request.
219 *
220 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
221 *
222 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
223 */
224int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
225 const bool is_delete)
226{
227 char *cp = strstr(data, " from ");
228
229 if (cp) {
230 *cp = '\0';
231 return tomoyo_update_domain_initializer_entry(cp + 6, data,
232 is_not,
233 is_delete);
234 }
235 return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
236 is_delete);
237}
238
239/**
240 * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
241 *
242 * @domainname: The name of domain.
243 * @program: The name of program.
244 * @last_name: The last component of @domainname.
245 *
246 * Returns true if executing @program reinitializes domain transition,
247 * false otherwise.
fdb8ebb7
TH
248 *
249 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
250 */
251static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
252 domainname,
253 const struct tomoyo_path_info *program,
254 const struct tomoyo_path_info *
255 last_name)
256{
257 struct tomoyo_domain_initializer_entry *ptr;
258 bool flag = false;
259
fdb8ebb7 260 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
26a2a1c9
KT
261 if (ptr->is_deleted)
262 continue;
263 if (ptr->domainname) {
264 if (!ptr->is_last_name) {
265 if (ptr->domainname != domainname)
266 continue;
267 } else {
268 if (tomoyo_pathcmp(ptr->domainname, last_name))
269 continue;
270 }
271 }
272 if (tomoyo_pathcmp(ptr->program, program))
273 continue;
274 if (ptr->is_not) {
275 flag = false;
276 break;
277 }
278 flag = true;
279 }
26a2a1c9
KT
280 return flag;
281}
282
c3fa109a
TH
283/*
284 * tomoyo_domain_keeper_list is used for holding list of domainnames which
285 * suppresses domain transition. Normally, a domainname is monotonically
286 * getting longer. But sometimes, we want to suppress domain transition.
287 * It would be convenient for us that programs executed from a login session
288 * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
289 * transition.
290 *
291 * An entry is added by
292 *
293 * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
294 * /sys/kernel/security/tomoyo/exception_policy
295 *
296 * and is deleted by
297 *
298 * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
299 * /sys/kernel/security/tomoyo/exception_policy
300 *
301 * and all entries are retrieved by
302 *
303 * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
304 *
305 * In the example above, any process which belongs to
306 * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
307 * unless explicitly specified by "initialize_domain" or "no_keep_domain".
308 *
309 * You may specify a program using "from" keyword.
310 * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
311 * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
312 * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
313 *
314 * You may add "no_" prefix to "keep_domain".
315 * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
316 * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
317 * cause "/usr/bin/passwd" to belong to
318 * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
319 * explicitly specified by "initialize_domain".
320 */
847b173e 321LIST_HEAD(tomoyo_domain_keeper_list);
26a2a1c9
KT
322
323/**
324 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
325 *
326 * @domainname: The name of domain.
327 * @program: The name of program. May be NULL.
328 * @is_not: True if it is "no_keep_domain" entry.
329 * @is_delete: True if it is a delete request.
330 *
331 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
332 *
333 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
334 */
335static int tomoyo_update_domain_keeper_entry(const char *domainname,
336 const char *program,
337 const bool is_not,
338 const bool is_delete)
339{
26a2a1c9 340 struct tomoyo_domain_keeper_entry *ptr;
9e4b50e9 341 struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
ca0b7df3 342 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9
KT
343
344 if (!tomoyo_is_domain_def(domainname) &&
3f629636 345 tomoyo_is_correct_path(domainname))
9e4b50e9 346 e.is_last_name = true;
17080008 347 else if (!tomoyo_is_correct_domain(domainname))
26a2a1c9
KT
348 return -EINVAL;
349 if (program) {
3f629636 350 if (!tomoyo_is_correct_path(program))
26a2a1c9 351 return -EINVAL;
9e4b50e9
TH
352 e.program = tomoyo_get_name(program);
353 if (!e.program)
ca0b7df3 354 goto out;
26a2a1c9 355 }
9e4b50e9
TH
356 e.domainname = tomoyo_get_name(domainname);
357 if (!e.domainname)
ca0b7df3 358 goto out;
29282381
TH
359 if (mutex_lock_interruptible(&tomoyo_policy_lock))
360 goto out;
fdb8ebb7 361 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
9e4b50e9 362 if (!tomoyo_is_same_domain_keeper_entry(ptr, &e))
26a2a1c9
KT
363 continue;
364 ptr->is_deleted = is_delete;
365 error = 0;
ca0b7df3 366 break;
26a2a1c9 367 }
9e4b50e9
TH
368 if (!is_delete && error) {
369 struct tomoyo_domain_keeper_entry *entry =
370 tomoyo_commit_ok(&e, sizeof(e));
371 if (entry) {
372 list_add_tail_rcu(&entry->list,
373 &tomoyo_domain_keeper_list);
374 error = 0;
375 }
26a2a1c9 376 }
f737d95d 377 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 378 out:
9e4b50e9
TH
379 tomoyo_put_name(e.domainname);
380 tomoyo_put_name(e.program);
26a2a1c9
KT
381 return error;
382}
383
384/**
385 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
386 *
387 * @data: String to parse.
388 * @is_not: True if it is "no_keep_domain" entry.
389 * @is_delete: True if it is a delete request.
390 *
fdb8ebb7 391 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
392 */
393int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
394 const bool is_delete)
395{
396 char *cp = strstr(data, " from ");
397
398 if (cp) {
399 *cp = '\0';
400 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
401 is_delete);
402 }
403 return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
404}
405
406/**
407 * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
408 *
409 * @head: Pointer to "struct tomoyo_io_buffer".
410 *
411 * Returns true on success, false otherwise.
fdb8ebb7
TH
412 *
413 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
414 */
415bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
416{
417 struct list_head *pos;
33043cbb 418 bool done = true;
26a2a1c9 419
26a2a1c9
KT
420 list_for_each_cookie(pos, head->read_var2,
421 &tomoyo_domain_keeper_list) {
422 struct tomoyo_domain_keeper_entry *ptr;
423 const char *no;
424 const char *from = "";
425 const char *program = "";
426
427 ptr = list_entry(pos, struct tomoyo_domain_keeper_entry, list);
428 if (ptr->is_deleted)
429 continue;
430 no = ptr->is_not ? "no_" : "";
431 if (ptr->program) {
432 from = " from ";
433 program = ptr->program->name;
434 }
7d2948b1
TH
435 done = tomoyo_io_printf(head,
436 "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
437 "%s%s%s\n", no, program, from,
438 ptr->domainname->name);
439 if (!done)
26a2a1c9 440 break;
26a2a1c9 441 }
26a2a1c9
KT
442 return done;
443}
444
445/**
446 * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
447 *
448 * @domainname: The name of domain.
449 * @program: The name of program.
450 * @last_name: The last component of @domainname.
451 *
452 * Returns true if executing @program supresses domain transition,
453 * false otherwise.
fdb8ebb7
TH
454 *
455 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
456 */
457static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
458 const struct tomoyo_path_info *program,
459 const struct tomoyo_path_info *last_name)
460{
461 struct tomoyo_domain_keeper_entry *ptr;
462 bool flag = false;
463
fdb8ebb7 464 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
26a2a1c9
KT
465 if (ptr->is_deleted)
466 continue;
467 if (!ptr->is_last_name) {
468 if (ptr->domainname != domainname)
469 continue;
470 } else {
471 if (tomoyo_pathcmp(ptr->domainname, last_name))
472 continue;
473 }
474 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
475 continue;
476 if (ptr->is_not) {
477 flag = false;
478 break;
479 }
480 flag = true;
481 }
26a2a1c9
KT
482 return flag;
483}
484
c3fa109a
TH
485/*
486 * tomoyo_alias_list is used for holding list of symlink's pathnames which are
487 * allowed to be passed to an execve() request. Normally, the domainname which
488 * the current process will belong to after execve() succeeds is calculated
489 * using dereferenced pathnames. But some programs behave differently depending
490 * on the name passed to argv[0]. For busybox, calculating domainname using
491 * dereferenced pathnames will cause all programs in the busybox to belong to
492 * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
493 * pathname for checking execve()'s permission and calculating domainname which
494 * the current process will belong to after execve() succeeds.
495 *
496 * An entry is added by
497 *
498 * # echo 'alias /bin/busybox /bin/cat' > \
499 * /sys/kernel/security/tomoyo/exception_policy
500 *
501 * and is deleted by
502 *
503 * # echo 'delete alias /bin/busybox /bin/cat' > \
504 * /sys/kernel/security/tomoyo/exception_policy
505 *
506 * and all entries are retrieved by
507 *
508 * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
509 *
510 * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
511 * of /bin/cat is requested, permission is checked for /bin/cat rather than
512 * /bin/busybox and domainname which the current process will belong to after
513 * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
514 */
847b173e 515LIST_HEAD(tomoyo_alias_list);
26a2a1c9
KT
516
517/**
518 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
519 *
520 * @original_name: The original program's real name.
521 * @aliased_name: The symbolic program's symbolic link's name.
522 * @is_delete: True if it is a delete request.
523 *
524 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
525 *
526 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
527 */
528static int tomoyo_update_alias_entry(const char *original_name,
529 const char *aliased_name,
530 const bool is_delete)
531{
26a2a1c9 532 struct tomoyo_alias_entry *ptr;
9e4b50e9 533 struct tomoyo_alias_entry e = { };
ca0b7df3 534 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9 535
3f629636
TH
536 if (!tomoyo_is_correct_path(original_name) ||
537 !tomoyo_is_correct_path(aliased_name))
538 return -EINVAL;
9e4b50e9
TH
539 e.original_name = tomoyo_get_name(original_name);
540 e.aliased_name = tomoyo_get_name(aliased_name);
3f629636
TH
541 if (!e.original_name || !e.aliased_name ||
542 e.original_name->is_patterned || e.aliased_name->is_patterned)
543 goto out; /* No patterns allowed. */
29282381
TH
544 if (mutex_lock_interruptible(&tomoyo_policy_lock))
545 goto out;
fdb8ebb7 546 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
9e4b50e9 547 if (!tomoyo_is_same_alias_entry(ptr, &e))
26a2a1c9
KT
548 continue;
549 ptr->is_deleted = is_delete;
550 error = 0;
ca0b7df3 551 break;
26a2a1c9 552 }
9e4b50e9
TH
553 if (!is_delete && error) {
554 struct tomoyo_alias_entry *entry =
555 tomoyo_commit_ok(&e, sizeof(e));
556 if (entry) {
557 list_add_tail_rcu(&entry->list, &tomoyo_alias_list);
558 error = 0;
559 }
26a2a1c9 560 }
f737d95d 561 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 562 out:
9e4b50e9
TH
563 tomoyo_put_name(e.original_name);
564 tomoyo_put_name(e.aliased_name);
26a2a1c9
KT
565 return error;
566}
567
568/**
569 * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
570 *
571 * @head: Pointer to "struct tomoyo_io_buffer".
572 *
573 * Returns true on success, false otherwise.
fdb8ebb7
TH
574 *
575 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
576 */
577bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
578{
579 struct list_head *pos;
580 bool done = true;
581
26a2a1c9
KT
582 list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
583 struct tomoyo_alias_entry *ptr;
584
585 ptr = list_entry(pos, struct tomoyo_alias_entry, list);
586 if (ptr->is_deleted)
587 continue;
7d2948b1
TH
588 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
589 ptr->original_name->name,
590 ptr->aliased_name->name);
591 if (!done)
26a2a1c9 592 break;
26a2a1c9 593 }
26a2a1c9
KT
594 return done;
595}
596
597/**
598 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
599 *
600 * @data: String to parse.
601 * @is_delete: True if it is a delete request.
602 *
603 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
604 *
605 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
606 */
607int tomoyo_write_alias_policy(char *data, const bool is_delete)
608{
609 char *cp = strchr(data, ' ');
610
611 if (!cp)
612 return -EINVAL;
613 *cp++ = '\0';
614 return tomoyo_update_alias_entry(data, cp, is_delete);
615}
616
26a2a1c9
KT
617/**
618 * tomoyo_find_or_assign_new_domain - Create a domain.
619 *
620 * @domainname: The name of domain.
621 * @profile: Profile number to assign if the domain was newly created.
622 *
623 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
fdb8ebb7
TH
624 *
625 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
626 */
627struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
628 domainname,
629 const u8 profile)
630{
ca0b7df3 631 struct tomoyo_domain_info *entry;
29282381 632 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 633 const struct tomoyo_path_info *saved_domainname;
ca0b7df3 634 bool found = false;
26a2a1c9 635
17080008 636 if (!tomoyo_is_correct_domain(domainname))
ca0b7df3 637 return NULL;
bf24fb01 638 saved_domainname = tomoyo_get_name(domainname);
26a2a1c9 639 if (!saved_domainname)
ca0b7df3 640 return NULL;
4e5d6f7e 641 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29282381
TH
642 if (mutex_lock_interruptible(&tomoyo_policy_lock))
643 goto out;
ca0b7df3
TH
644 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
645 if (domain->is_deleted ||
646 tomoyo_pathcmp(saved_domainname, domain->domainname))
647 continue;
648 found = true;
649 break;
650 }
651 if (!found && tomoyo_memory_ok(entry)) {
652 INIT_LIST_HEAD(&entry->acl_info_list);
653 entry->domainname = saved_domainname;
bf24fb01 654 saved_domainname = NULL;
ca0b7df3
TH
655 entry->profile = profile;
656 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
657 domain = entry;
658 entry = NULL;
659 found = true;
26a2a1c9 660 }
f737d95d 661 mutex_unlock(&tomoyo_policy_lock);
29282381 662 out:
bf24fb01 663 tomoyo_put_name(saved_domainname);
ca0b7df3
TH
664 kfree(entry);
665 return found ? domain : NULL;
26a2a1c9
KT
666}
667
668/**
669 * tomoyo_find_next_domain - Find a domain.
670 *
56f8c9bc 671 * @bprm: Pointer to "struct linux_binprm".
26a2a1c9
KT
672 *
673 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
674 *
675 * Caller holds tomoyo_read_lock().
26a2a1c9 676 */
56f8c9bc 677int tomoyo_find_next_domain(struct linux_binprm *bprm)
26a2a1c9 678{
17fcfbd9 679 struct tomoyo_request_info r;
c8c57e84 680 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
26a2a1c9
KT
681 struct tomoyo_domain_info *old_domain = tomoyo_domain();
682 struct tomoyo_domain_info *domain = NULL;
683 const char *old_domain_name = old_domain->domainname->name;
684 const char *original_name = bprm->filename;
26a2a1c9 685 const u8 mode = tomoyo_check_flags(old_domain, TOMOYO_MAC_FOR_FILE);
cb0abe6a 686 const bool is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
26a2a1c9 687 int retval = -ENOMEM;
c8c57e84
TH
688 bool need_kfree = false;
689 struct tomoyo_path_info rn = { }; /* real name */
690 struct tomoyo_path_info sn = { }; /* symlink name */
17fcfbd9 691 struct tomoyo_path_info ln; /* last name */
26a2a1c9 692
c8c57e84
TH
693 ln.name = tomoyo_get_last_name(old_domain);
694 tomoyo_fill_path_info(&ln);
17fcfbd9 695 tomoyo_init_request_info(&r, NULL);
26a2a1c9
KT
696 if (!tmp)
697 goto out;
698
17fcfbd9 699 retry:
c8c57e84
TH
700 if (need_kfree) {
701 kfree(rn.name);
702 need_kfree = false;
703 }
26a2a1c9
KT
704 /* Get tomoyo_realpath of program. */
705 retval = -ENOENT;
c8c57e84
TH
706 rn.name = tomoyo_realpath(original_name);
707 if (!rn.name)
26a2a1c9 708 goto out;
c8c57e84
TH
709 tomoyo_fill_path_info(&rn);
710 need_kfree = true;
711
26a2a1c9 712 /* Get tomoyo_realpath of symbolic link. */
c8c57e84
TH
713 sn.name = tomoyo_realpath_nofollow(original_name);
714 if (!sn.name)
26a2a1c9 715 goto out;
17fcfbd9 716 tomoyo_fill_path_info(&sn);
26a2a1c9
KT
717
718 /* Check 'alias' directive. */
17fcfbd9 719 if (tomoyo_pathcmp(&rn, &sn)) {
26a2a1c9
KT
720 struct tomoyo_alias_entry *ptr;
721 /* Is this program allowed to be called via symbolic links? */
fdb8ebb7 722 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
26a2a1c9 723 if (ptr->is_deleted ||
17fcfbd9
TH
724 tomoyo_pathcmp(&rn, ptr->original_name) ||
725 tomoyo_pathcmp(&sn, ptr->aliased_name))
26a2a1c9 726 continue;
c8c57e84
TH
727 kfree(rn.name);
728 need_kfree = false;
729 /* This is OK because it is read only. */
730 rn = *ptr->aliased_name;
26a2a1c9
KT
731 break;
732 }
26a2a1c9
KT
733 }
734
735 /* Check execute permission. */
17fcfbd9
TH
736 retval = tomoyo_check_exec_perm(old_domain, &rn);
737 if (retval == TOMOYO_RETRY_REQUEST)
738 goto retry;
26a2a1c9
KT
739 if (retval < 0)
740 goto out;
741
17fcfbd9 742 if (tomoyo_is_domain_initializer(old_domain->domainname, &rn, &ln)) {
26a2a1c9 743 /* Transit to the child of tomoyo_kernel_domain domain. */
c8c57e84
TH
744 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
745 TOMOYO_ROOT_NAME " " "%s", rn.name);
26a2a1c9
KT
746 } else if (old_domain == &tomoyo_kernel_domain &&
747 !tomoyo_policy_loaded) {
748 /*
749 * Needn't to transit from kernel domain before starting
750 * /sbin/init. But transit from kernel domain if executing
751 * initializers because they might start before /sbin/init.
752 */
753 domain = old_domain;
17fcfbd9 754 } else if (tomoyo_is_domain_keeper(old_domain->domainname, &rn, &ln)) {
26a2a1c9
KT
755 /* Keep current domain. */
756 domain = old_domain;
757 } else {
758 /* Normal domain transition. */
c8c57e84
TH
759 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
760 "%s %s", old_domain_name, rn.name);
26a2a1c9 761 }
c8c57e84 762 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
26a2a1c9 763 goto done;
c8c57e84 764 domain = tomoyo_find_domain(tmp);
26a2a1c9
KT
765 if (domain)
766 goto done;
17fcfbd9
TH
767 if (is_enforce) {
768 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
c8c57e84 769 "%s\n", tmp);
17fcfbd9
TH
770 if (error == TOMOYO_RETRY_REQUEST)
771 goto retry;
772 if (error < 0)
773 goto done;
774 }
c8c57e84 775 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
26a2a1c9
KT
776 done:
777 if (domain)
778 goto out;
c8c57e84 779 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
26a2a1c9
KT
780 if (is_enforce)
781 retval = -EPERM;
782 else
ea13ddba 783 old_domain->transition_failed = true;
26a2a1c9 784 out:
56f8c9bc
TH
785 if (!domain)
786 domain = old_domain;
ec8e6a4e
TH
787 /* Update reference count on "struct tomoyo_domain_info". */
788 atomic_inc(&domain->users);
56f8c9bc 789 bprm->cred->security = domain;
c8c57e84
TH
790 if (need_kfree)
791 kfree(rn.name);
792 kfree(sn.name);
8e2d39a1 793 kfree(tmp);
26a2a1c9
KT
794 return retval;
795}