]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/domain.c
TOMOYO: Use callback for updating entries.
[net-next-2.6.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
c3fa109a
TH
112/*
113 * tomoyo_domain_list is used for holding list of domains.
114 * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
115 * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
116 *
117 * An entry is added by
118 *
119 * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
120 * /sys/kernel/security/tomoyo/domain_policy
121 *
122 * and is deleted by
123 *
124 * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
125 * /sys/kernel/security/tomoyo/domain_policy
126 *
127 * and all entries are retrieved by
128 *
129 * # cat /sys/kernel/security/tomoyo/domain_policy
130 *
131 * A domain is added by
132 *
133 * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
134 *
135 * and is deleted by
136 *
137 * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
138 *
139 * and all domains are retrieved by
140 *
141 * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
142 *
143 * Normally, a domainname is monotonically getting longer because a domainname
144 * which the process will belong to if an execve() operation succeeds is
145 * defined as a concatenation of "current domainname" + "pathname passed to
146 * execve()".
147 * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
148 * exceptions.
149 */
26a2a1c9 150LIST_HEAD(tomoyo_domain_list);
26a2a1c9 151
26a2a1c9
KT
152/**
153 * tomoyo_get_last_name - Get last component of a domainname.
154 *
155 * @domain: Pointer to "struct tomoyo_domain_info".
156 *
157 * Returns the last component of the domainname.
158 */
159const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
160{
161 const char *cp0 = domain->domainname->name;
162 const char *cp1 = strrchr(cp0, ' ');
163
164 if (cp1)
165 return cp1 + 1;
166 return cp0;
167}
168
c3fa109a
TH
169/*
170 * tomoyo_domain_initializer_list is used for holding list of programs which
171 * triggers reinitialization of domainname. Normally, a domainname is
172 * monotonically getting longer. But sometimes, we restart daemon programs.
173 * It would be convenient for us that "a daemon started upon system boot" and
174 * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
175 * provides a way to shorten domainnames.
176 *
177 * An entry is added by
178 *
179 * # echo 'initialize_domain /usr/sbin/httpd' > \
180 * /sys/kernel/security/tomoyo/exception_policy
181 *
182 * and is deleted by
183 *
184 * # echo 'delete initialize_domain /usr/sbin/httpd' > \
185 * /sys/kernel/security/tomoyo/exception_policy
186 *
187 * and all entries are retrieved by
188 *
189 * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
190 *
191 * In the example above, /usr/sbin/httpd will belong to
192 * "<kernel> /usr/sbin/httpd" domain.
193 *
194 * You may specify a domainname using "from" keyword.
195 * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
196 * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
197 * domain to belong to "<kernel> /usr/sbin/httpd" domain.
198 *
199 * You may add "no_" prefix to "initialize_domain".
200 * "initialize_domain /usr/sbin/httpd" and
201 * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
202 * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
203 * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
204 */
847b173e 205LIST_HEAD(tomoyo_domain_initializer_list);
26a2a1c9 206
36f5e1ff
TH
207static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
208 a,
209 const struct tomoyo_acl_head *
210 b)
211{
212 const struct tomoyo_domain_initializer_entry *p1 =
213 container_of(a, typeof(*p1), head);
214 const struct tomoyo_domain_initializer_entry *p2 =
215 container_of(b, typeof(*p2), head);
216 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
217 && p1->domainname == p2->domainname
218 && p1->program == p2->program;
219}
220
26a2a1c9
KT
221/**
222 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
223 *
224 * @domainname: The name of domain. May be NULL.
225 * @program: The name of program.
226 * @is_not: True if it is "no_initialize_domain" entry.
227 * @is_delete: True if it is a delete request.
228 *
229 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
230 *
231 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
232 */
233static int tomoyo_update_domain_initializer_entry(const char *domainname,
234 const char *program,
235 const bool is_not,
236 const bool is_delete)
237{
9e4b50e9 238 struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
ca0b7df3 239 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9 240
3f629636
TH
241 if (!tomoyo_is_correct_path(program))
242 return -EINVAL;
26a2a1c9
KT
243 if (domainname) {
244 if (!tomoyo_is_domain_def(domainname) &&
3f629636 245 tomoyo_is_correct_path(domainname))
9e4b50e9 246 e.is_last_name = true;
17080008 247 else if (!tomoyo_is_correct_domain(domainname))
26a2a1c9 248 return -EINVAL;
9e4b50e9
TH
249 e.domainname = tomoyo_get_name(domainname);
250 if (!e.domainname)
ca0b7df3 251 goto out;
26a2a1c9 252 }
9e4b50e9
TH
253 e.program = tomoyo_get_name(program);
254 if (!e.program)
ca0b7df3 255 goto out;
36f5e1ff
TH
256 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
257 &tomoyo_domain_initializer_list,
258 tomoyo_same_domain_initializer_entry);
ca0b7df3 259 out:
9e4b50e9
TH
260 tomoyo_put_name(e.domainname);
261 tomoyo_put_name(e.program);
26a2a1c9
KT
262 return error;
263}
264
265/**
266 * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
267 *
268 * @head: Pointer to "struct tomoyo_io_buffer".
269 *
270 * Returns true on success, false otherwise.
fdb8ebb7
TH
271 *
272 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
273 */
274bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
275{
276 struct list_head *pos;
277 bool done = true;
278
26a2a1c9
KT
279 list_for_each_cookie(pos, head->read_var2,
280 &tomoyo_domain_initializer_list) {
281 const char *no;
282 const char *from = "";
283 const char *domain = "";
284 struct tomoyo_domain_initializer_entry *ptr;
285 ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
82e0f001
TH
286 head.list);
287 if (ptr->head.is_deleted)
26a2a1c9
KT
288 continue;
289 no = ptr->is_not ? "no_" : "";
290 if (ptr->domainname) {
291 from = " from ";
292 domain = ptr->domainname->name;
293 }
7d2948b1
TH
294 done = tomoyo_io_printf(head,
295 "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
296 "%s%s%s\n", no, ptr->program->name,
297 from, domain);
298 if (!done)
26a2a1c9 299 break;
26a2a1c9 300 }
26a2a1c9
KT
301 return done;
302}
303
304/**
305 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
306 *
307 * @data: String to parse.
308 * @is_not: True if it is "no_initialize_domain" entry.
309 * @is_delete: True if it is a delete request.
310 *
311 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
312 *
313 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
314 */
315int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
316 const bool is_delete)
317{
318 char *cp = strstr(data, " from ");
319
320 if (cp) {
321 *cp = '\0';
322 return tomoyo_update_domain_initializer_entry(cp + 6, data,
323 is_not,
324 is_delete);
325 }
326 return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
327 is_delete);
328}
329
330/**
331 * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
332 *
333 * @domainname: The name of domain.
334 * @program: The name of program.
335 * @last_name: The last component of @domainname.
336 *
337 * Returns true if executing @program reinitializes domain transition,
338 * false otherwise.
fdb8ebb7
TH
339 *
340 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
341 */
342static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
343 domainname,
344 const struct tomoyo_path_info *program,
345 const struct tomoyo_path_info *
346 last_name)
347{
348 struct tomoyo_domain_initializer_entry *ptr;
349 bool flag = false;
350
82e0f001
TH
351 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
352 head.list) {
353 if (ptr->head.is_deleted)
26a2a1c9
KT
354 continue;
355 if (ptr->domainname) {
356 if (!ptr->is_last_name) {
357 if (ptr->domainname != domainname)
358 continue;
359 } else {
360 if (tomoyo_pathcmp(ptr->domainname, last_name))
361 continue;
362 }
363 }
364 if (tomoyo_pathcmp(ptr->program, program))
365 continue;
366 if (ptr->is_not) {
367 flag = false;
368 break;
369 }
370 flag = true;
371 }
26a2a1c9
KT
372 return flag;
373}
374
c3fa109a
TH
375/*
376 * tomoyo_domain_keeper_list is used for holding list of domainnames which
377 * suppresses domain transition. Normally, a domainname is monotonically
378 * getting longer. But sometimes, we want to suppress domain transition.
379 * It would be convenient for us that programs executed from a login session
380 * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
381 * transition.
382 *
383 * An entry is added by
384 *
385 * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
386 * /sys/kernel/security/tomoyo/exception_policy
387 *
388 * and is deleted by
389 *
390 * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
391 * /sys/kernel/security/tomoyo/exception_policy
392 *
393 * and all entries are retrieved by
394 *
395 * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
396 *
397 * In the example above, any process which belongs to
398 * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
399 * unless explicitly specified by "initialize_domain" or "no_keep_domain".
400 *
401 * You may specify a program using "from" keyword.
402 * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
403 * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
404 * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
405 *
406 * You may add "no_" prefix to "keep_domain".
407 * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
408 * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
409 * cause "/usr/bin/passwd" to belong to
410 * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
411 * explicitly specified by "initialize_domain".
412 */
847b173e 413LIST_HEAD(tomoyo_domain_keeper_list);
26a2a1c9 414
36f5e1ff
TH
415static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
416 const struct tomoyo_acl_head *b)
417{
418 const struct tomoyo_domain_keeper_entry *p1 =
419 container_of(a, typeof(*p1), head);
420 const struct tomoyo_domain_keeper_entry *p2 =
421 container_of(b, typeof(*p2), head);
422 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
423 && p1->domainname == p2->domainname
424 && p1->program == p2->program;
425}
426
26a2a1c9
KT
427/**
428 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
429 *
430 * @domainname: The name of domain.
431 * @program: The name of program. May be NULL.
432 * @is_not: True if it is "no_keep_domain" entry.
433 * @is_delete: True if it is a delete request.
434 *
435 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
436 *
437 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
438 */
439static int tomoyo_update_domain_keeper_entry(const char *domainname,
440 const char *program,
441 const bool is_not,
442 const bool is_delete)
443{
9e4b50e9 444 struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
ca0b7df3 445 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9
KT
446
447 if (!tomoyo_is_domain_def(domainname) &&
3f629636 448 tomoyo_is_correct_path(domainname))
9e4b50e9 449 e.is_last_name = true;
17080008 450 else if (!tomoyo_is_correct_domain(domainname))
26a2a1c9
KT
451 return -EINVAL;
452 if (program) {
3f629636 453 if (!tomoyo_is_correct_path(program))
26a2a1c9 454 return -EINVAL;
9e4b50e9
TH
455 e.program = tomoyo_get_name(program);
456 if (!e.program)
ca0b7df3 457 goto out;
26a2a1c9 458 }
9e4b50e9
TH
459 e.domainname = tomoyo_get_name(domainname);
460 if (!e.domainname)
ca0b7df3 461 goto out;
36f5e1ff
TH
462 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
463 &tomoyo_domain_keeper_list,
464 tomoyo_same_domain_keeper_entry);
ca0b7df3 465 out:
9e4b50e9
TH
466 tomoyo_put_name(e.domainname);
467 tomoyo_put_name(e.program);
26a2a1c9
KT
468 return error;
469}
470
471/**
472 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
473 *
474 * @data: String to parse.
475 * @is_not: True if it is "no_keep_domain" entry.
476 * @is_delete: True if it is a delete request.
477 *
fdb8ebb7 478 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
479 */
480int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
481 const bool is_delete)
482{
483 char *cp = strstr(data, " from ");
484
485 if (cp) {
486 *cp = '\0';
487 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
488 is_delete);
489 }
490 return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
491}
492
493/**
494 * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
495 *
496 * @head: Pointer to "struct tomoyo_io_buffer".
497 *
498 * Returns true on success, false otherwise.
fdb8ebb7
TH
499 *
500 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
501 */
502bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
503{
504 struct list_head *pos;
33043cbb 505 bool done = true;
26a2a1c9 506
26a2a1c9
KT
507 list_for_each_cookie(pos, head->read_var2,
508 &tomoyo_domain_keeper_list) {
509 struct tomoyo_domain_keeper_entry *ptr;
510 const char *no;
511 const char *from = "";
512 const char *program = "";
513
82e0f001
TH
514 ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
515 head.list);
516 if (ptr->head.is_deleted)
26a2a1c9
KT
517 continue;
518 no = ptr->is_not ? "no_" : "";
519 if (ptr->program) {
520 from = " from ";
521 program = ptr->program->name;
522 }
7d2948b1
TH
523 done = tomoyo_io_printf(head,
524 "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
525 "%s%s%s\n", no, program, from,
526 ptr->domainname->name);
527 if (!done)
26a2a1c9 528 break;
26a2a1c9 529 }
26a2a1c9
KT
530 return done;
531}
532
533/**
534 * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
535 *
536 * @domainname: The name of domain.
537 * @program: The name of program.
538 * @last_name: The last component of @domainname.
539 *
540 * Returns true if executing @program supresses domain transition,
541 * false otherwise.
fdb8ebb7
TH
542 *
543 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
544 */
545static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
546 const struct tomoyo_path_info *program,
547 const struct tomoyo_path_info *last_name)
548{
549 struct tomoyo_domain_keeper_entry *ptr;
550 bool flag = false;
551
82e0f001
TH
552 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
553 if (ptr->head.is_deleted)
26a2a1c9
KT
554 continue;
555 if (!ptr->is_last_name) {
556 if (ptr->domainname != domainname)
557 continue;
558 } else {
559 if (tomoyo_pathcmp(ptr->domainname, last_name))
560 continue;
561 }
562 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
563 continue;
564 if (ptr->is_not) {
565 flag = false;
566 break;
567 }
568 flag = true;
569 }
26a2a1c9
KT
570 return flag;
571}
572
1084307c
TH
573/*
574 * tomoyo_aggregator_list is used for holding list of rewrite table for
575 * execve() request. Some programs provides similar functionality. This keyword
576 * allows users to aggregate such programs.
577 *
578 * Entries are added by
579 *
580 * # echo 'aggregator /usr/bin/vi /./editor' > \
581 * /sys/kernel/security/tomoyo/exception_policy
582 * # echo 'aggregator /usr/bin/emacs /./editor' > \
583 * /sys/kernel/security/tomoyo/exception_policy
584 *
585 * and are deleted by
586 *
587 * # echo 'delete aggregator /usr/bin/vi /./editor' > \
588 * /sys/kernel/security/tomoyo/exception_policy
589 * # echo 'delete aggregator /usr/bin/emacs /./editor' > \
590 * /sys/kernel/security/tomoyo/exception_policy
591 *
592 * and all entries are retrieved by
593 *
594 * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy
595 *
596 * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed,
597 * permission is checked for /./editor and domainname which the current process
598 * will belong to after execve() succeeds is calculated using /./editor .
599 */
600LIST_HEAD(tomoyo_aggregator_list);
601
36f5e1ff
TH
602static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
603 const struct tomoyo_acl_head *b)
604{
605 const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
606 head);
607 const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
608 head);
609 return p1->original_name == p2->original_name &&
610 p1->aggregated_name == p2->aggregated_name;
611}
612
1084307c
TH
613/**
614 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
615 *
616 * @original_name: The original program's name.
617 * @aggregated_name: The program name to use.
618 * @is_delete: True if it is a delete request.
619 *
620 * Returns 0 on success, negative value otherwise.
621 *
622 * Caller holds tomoyo_read_lock().
623 */
624static int tomoyo_update_aggregator_entry(const char *original_name,
625 const char *aggregated_name,
626 const bool is_delete)
627{
1084307c
TH
628 struct tomoyo_aggregator_entry e = { };
629 int error = is_delete ? -ENOENT : -ENOMEM;
630
631 if (!tomoyo_is_correct_path(original_name) ||
632 !tomoyo_is_correct_path(aggregated_name))
633 return -EINVAL;
634 e.original_name = tomoyo_get_name(original_name);
635 e.aggregated_name = tomoyo_get_name(aggregated_name);
636 if (!e.original_name || !e.aggregated_name ||
637 e.aggregated_name->is_patterned) /* No patterns allowed. */
638 goto out;
36f5e1ff
TH
639 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
640 &tomoyo_aggregator_list,
641 tomoyo_same_aggregator_entry);
1084307c
TH
642 out:
643 tomoyo_put_name(e.original_name);
644 tomoyo_put_name(e.aggregated_name);
645 return error;
646}
647
648/**
649 * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
650 *
651 * @head: Pointer to "struct tomoyo_io_buffer".
652 *
653 * Returns true on success, false otherwise.
654 *
655 * Caller holds tomoyo_read_lock().
656 */
657bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
658{
659 struct list_head *pos;
660 bool done = true;
661
662 list_for_each_cookie(pos, head->read_var2, &tomoyo_aggregator_list) {
663 struct tomoyo_aggregator_entry *ptr;
664
82e0f001
TH
665 ptr = list_entry(pos, struct tomoyo_aggregator_entry,
666 head.list);
667 if (ptr->head.is_deleted)
1084307c
TH
668 continue;
669 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
670 "%s %s\n", ptr->original_name->name,
671 ptr->aggregated_name->name);
672 if (!done)
673 break;
674 }
675 return done;
676}
677
678/**
679 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
680 *
681 * @data: String to parse.
682 * @is_delete: True if it is a delete request.
683 *
684 * Returns 0 on success, negative value otherwise.
685 *
686 * Caller holds tomoyo_read_lock().
687 */
688int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
689{
690 char *cp = strchr(data, ' ');
691
692 if (!cp)
693 return -EINVAL;
694 *cp++ = '\0';
695 return tomoyo_update_aggregator_entry(data, cp, is_delete);
696}
697
c3fa109a
TH
698/*
699 * tomoyo_alias_list is used for holding list of symlink's pathnames which are
700 * allowed to be passed to an execve() request. Normally, the domainname which
701 * the current process will belong to after execve() succeeds is calculated
702 * using dereferenced pathnames. But some programs behave differently depending
703 * on the name passed to argv[0]. For busybox, calculating domainname using
704 * dereferenced pathnames will cause all programs in the busybox to belong to
705 * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
706 * pathname for checking execve()'s permission and calculating domainname which
707 * the current process will belong to after execve() succeeds.
708 *
709 * An entry is added by
710 *
711 * # echo 'alias /bin/busybox /bin/cat' > \
712 * /sys/kernel/security/tomoyo/exception_policy
713 *
714 * and is deleted by
715 *
716 * # echo 'delete alias /bin/busybox /bin/cat' > \
717 * /sys/kernel/security/tomoyo/exception_policy
718 *
719 * and all entries are retrieved by
720 *
721 * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
722 *
723 * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
724 * of /bin/cat is requested, permission is checked for /bin/cat rather than
725 * /bin/busybox and domainname which the current process will belong to after
726 * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
727 */
847b173e 728LIST_HEAD(tomoyo_alias_list);
26a2a1c9 729
36f5e1ff
TH
730static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
731 const struct tomoyo_acl_head *b)
732{
733 const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
734 head);
735 const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
736 head);
737 return p1->original_name == p2->original_name &&
738 p1->aliased_name == p2->aliased_name;
739}
740
26a2a1c9
KT
741/**
742 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
743 *
744 * @original_name: The original program's real name.
745 * @aliased_name: The symbolic program's symbolic link's name.
746 * @is_delete: True if it is a delete request.
747 *
748 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
749 *
750 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
751 */
752static int tomoyo_update_alias_entry(const char *original_name,
753 const char *aliased_name,
754 const bool is_delete)
755{
9e4b50e9 756 struct tomoyo_alias_entry e = { };
ca0b7df3 757 int error = is_delete ? -ENOENT : -ENOMEM;
26a2a1c9 758
3f629636
TH
759 if (!tomoyo_is_correct_path(original_name) ||
760 !tomoyo_is_correct_path(aliased_name))
761 return -EINVAL;
9e4b50e9
TH
762 e.original_name = tomoyo_get_name(original_name);
763 e.aliased_name = tomoyo_get_name(aliased_name);
3f629636
TH
764 if (!e.original_name || !e.aliased_name ||
765 e.original_name->is_patterned || e.aliased_name->is_patterned)
766 goto out; /* No patterns allowed. */
36f5e1ff
TH
767 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
768 &tomoyo_alias_list,
769 tomoyo_same_alias_entry);
ca0b7df3 770 out:
9e4b50e9
TH
771 tomoyo_put_name(e.original_name);
772 tomoyo_put_name(e.aliased_name);
26a2a1c9
KT
773 return error;
774}
775
776/**
777 * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
778 *
779 * @head: Pointer to "struct tomoyo_io_buffer".
780 *
781 * Returns true on success, false otherwise.
fdb8ebb7
TH
782 *
783 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
784 */
785bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
786{
787 struct list_head *pos;
788 bool done = true;
789
26a2a1c9
KT
790 list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
791 struct tomoyo_alias_entry *ptr;
792
82e0f001
TH
793 ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
794 if (ptr->head.is_deleted)
26a2a1c9 795 continue;
7d2948b1
TH
796 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
797 ptr->original_name->name,
798 ptr->aliased_name->name);
799 if (!done)
26a2a1c9 800 break;
26a2a1c9 801 }
26a2a1c9
KT
802 return done;
803}
804
805/**
806 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
807 *
808 * @data: String to parse.
809 * @is_delete: True if it is a delete request.
810 *
811 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
812 *
813 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
814 */
815int tomoyo_write_alias_policy(char *data, const bool is_delete)
816{
817 char *cp = strchr(data, ' ');
818
819 if (!cp)
820 return -EINVAL;
821 *cp++ = '\0';
822 return tomoyo_update_alias_entry(data, cp, is_delete);
823}
824
26a2a1c9
KT
825/**
826 * tomoyo_find_or_assign_new_domain - Create a domain.
827 *
828 * @domainname: The name of domain.
829 * @profile: Profile number to assign if the domain was newly created.
830 *
831 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
fdb8ebb7
TH
832 *
833 * Caller holds tomoyo_read_lock().
26a2a1c9
KT
834 */
835struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
836 domainname,
837 const u8 profile)
838{
ca0b7df3 839 struct tomoyo_domain_info *entry;
29282381 840 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 841 const struct tomoyo_path_info *saved_domainname;
ca0b7df3 842 bool found = false;
26a2a1c9 843
17080008 844 if (!tomoyo_is_correct_domain(domainname))
ca0b7df3 845 return NULL;
bf24fb01 846 saved_domainname = tomoyo_get_name(domainname);
26a2a1c9 847 if (!saved_domainname)
ca0b7df3 848 return NULL;
4e5d6f7e 849 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29282381
TH
850 if (mutex_lock_interruptible(&tomoyo_policy_lock))
851 goto out;
ca0b7df3
TH
852 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
853 if (domain->is_deleted ||
854 tomoyo_pathcmp(saved_domainname, domain->domainname))
855 continue;
856 found = true;
857 break;
858 }
859 if (!found && tomoyo_memory_ok(entry)) {
860 INIT_LIST_HEAD(&entry->acl_info_list);
861 entry->domainname = saved_domainname;
bf24fb01 862 saved_domainname = NULL;
ca0b7df3
TH
863 entry->profile = profile;
864 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
865 domain = entry;
866 entry = NULL;
867 found = true;
26a2a1c9 868 }
f737d95d 869 mutex_unlock(&tomoyo_policy_lock);
29282381 870 out:
bf24fb01 871 tomoyo_put_name(saved_domainname);
ca0b7df3
TH
872 kfree(entry);
873 return found ? domain : NULL;
26a2a1c9
KT
874}
875
876/**
877 * tomoyo_find_next_domain - Find a domain.
878 *
56f8c9bc 879 * @bprm: Pointer to "struct linux_binprm".
26a2a1c9
KT
880 *
881 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
882 *
883 * Caller holds tomoyo_read_lock().
26a2a1c9 884 */
56f8c9bc 885int tomoyo_find_next_domain(struct linux_binprm *bprm)
26a2a1c9 886{
17fcfbd9 887 struct tomoyo_request_info r;
c8c57e84 888 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
26a2a1c9
KT
889 struct tomoyo_domain_info *old_domain = tomoyo_domain();
890 struct tomoyo_domain_info *domain = NULL;
891 const char *old_domain_name = old_domain->domainname->name;
892 const char *original_name = bprm->filename;
57c2590f
TH
893 u8 mode;
894 bool is_enforce;
26a2a1c9 895 int retval = -ENOMEM;
c8c57e84
TH
896 bool need_kfree = false;
897 struct tomoyo_path_info rn = { }; /* real name */
898 struct tomoyo_path_info sn = { }; /* symlink name */
17fcfbd9 899 struct tomoyo_path_info ln; /* last name */
26a2a1c9 900
c8c57e84
TH
901 ln.name = tomoyo_get_last_name(old_domain);
902 tomoyo_fill_path_info(&ln);
57c2590f
TH
903 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
904 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
26a2a1c9
KT
905 if (!tmp)
906 goto out;
907
17fcfbd9 908 retry:
c8c57e84
TH
909 if (need_kfree) {
910 kfree(rn.name);
911 need_kfree = false;
912 }
26a2a1c9
KT
913 /* Get tomoyo_realpath of program. */
914 retval = -ENOENT;
c8c57e84
TH
915 rn.name = tomoyo_realpath(original_name);
916 if (!rn.name)
26a2a1c9 917 goto out;
c8c57e84
TH
918 tomoyo_fill_path_info(&rn);
919 need_kfree = true;
920
26a2a1c9 921 /* Get tomoyo_realpath of symbolic link. */
c8c57e84
TH
922 sn.name = tomoyo_realpath_nofollow(original_name);
923 if (!sn.name)
26a2a1c9 924 goto out;
17fcfbd9 925 tomoyo_fill_path_info(&sn);
26a2a1c9
KT
926
927 /* Check 'alias' directive. */
17fcfbd9 928 if (tomoyo_pathcmp(&rn, &sn)) {
26a2a1c9
KT
929 struct tomoyo_alias_entry *ptr;
930 /* Is this program allowed to be called via symbolic links? */
82e0f001
TH
931 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
932 if (ptr->head.is_deleted ||
17fcfbd9
TH
933 tomoyo_pathcmp(&rn, ptr->original_name) ||
934 tomoyo_pathcmp(&sn, ptr->aliased_name))
26a2a1c9 935 continue;
c8c57e84
TH
936 kfree(rn.name);
937 need_kfree = false;
938 /* This is OK because it is read only. */
939 rn = *ptr->aliased_name;
26a2a1c9
KT
940 break;
941 }
26a2a1c9
KT
942 }
943
1084307c
TH
944 /* Check 'aggregator' directive. */
945 {
946 struct tomoyo_aggregator_entry *ptr;
82e0f001
TH
947 list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list,
948 head.list) {
949 if (ptr->head.is_deleted ||
1084307c
TH
950 !tomoyo_path_matches_pattern(&rn,
951 ptr->original_name))
952 continue;
953 if (need_kfree)
954 kfree(rn.name);
955 need_kfree = false;
956 /* This is OK because it is read only. */
957 rn = *ptr->aggregated_name;
958 break;
959 }
960 }
961
26a2a1c9 962 /* Check execute permission. */
57c2590f 963 retval = tomoyo_check_exec_perm(&r, &rn);
17fcfbd9
TH
964 if (retval == TOMOYO_RETRY_REQUEST)
965 goto retry;
26a2a1c9
KT
966 if (retval < 0)
967 goto out;
968
17fcfbd9 969 if (tomoyo_is_domain_initializer(old_domain->domainname, &rn, &ln)) {
26a2a1c9 970 /* Transit to the child of tomoyo_kernel_domain domain. */
c8c57e84
TH
971 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
972 TOMOYO_ROOT_NAME " " "%s", rn.name);
26a2a1c9
KT
973 } else if (old_domain == &tomoyo_kernel_domain &&
974 !tomoyo_policy_loaded) {
975 /*
976 * Needn't to transit from kernel domain before starting
977 * /sbin/init. But transit from kernel domain if executing
978 * initializers because they might start before /sbin/init.
979 */
980 domain = old_domain;
17fcfbd9 981 } else if (tomoyo_is_domain_keeper(old_domain->domainname, &rn, &ln)) {
26a2a1c9
KT
982 /* Keep current domain. */
983 domain = old_domain;
984 } else {
985 /* Normal domain transition. */
c8c57e84
TH
986 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
987 "%s %s", old_domain_name, rn.name);
26a2a1c9 988 }
c8c57e84 989 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
26a2a1c9 990 goto done;
c8c57e84 991 domain = tomoyo_find_domain(tmp);
26a2a1c9
KT
992 if (domain)
993 goto done;
17fcfbd9
TH
994 if (is_enforce) {
995 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
c8c57e84 996 "%s\n", tmp);
17fcfbd9
TH
997 if (error == TOMOYO_RETRY_REQUEST)
998 goto retry;
999 if (error < 0)
1000 goto done;
1001 }
c8c57e84 1002 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
26a2a1c9
KT
1003 done:
1004 if (domain)
1005 goto out;
c8c57e84 1006 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
26a2a1c9
KT
1007 if (is_enforce)
1008 retval = -EPERM;
1009 else
ea13ddba 1010 old_domain->transition_failed = true;
26a2a1c9 1011 out:
56f8c9bc
TH
1012 if (!domain)
1013 domain = old_domain;
ec8e6a4e
TH
1014 /* Update reference count on "struct tomoyo_domain_info". */
1015 atomic_inc(&domain->users);
56f8c9bc 1016 bprm->cred->security = domain;
c8c57e84
TH
1017 if (need_kfree)
1018 kfree(rn.name);
1019 kfree(sn.name);
8e2d39a1 1020 kfree(tmp);
26a2a1c9
KT
1021 return retval;
1022}