TOMOYO: Remove unused mutex.
[linux-2.6] / security / tomoyo / domain.c
1 /*
2  * security/tomoyo/domain.c
3  *
4  * Implementation of the Domain-Based Mandatory Access Control.
5  *
6  * Copyright (C) 2005-2009  NTT DATA CORPORATION
7  *
8  * Version: 2.2.0   2009/04/01
9  *
10  */
11
12 #include "common.h"
13 #include "tomoyo.h"
14 #include "realpath.h"
15 #include <linux/binfmts.h>
16
17 /* Variables definitions.*/
18
19 /* The initial domain. */
20 struct tomoyo_domain_info tomoyo_kernel_domain;
21
22 /* The list for "struct tomoyo_domain_info". */
23 LIST_HEAD(tomoyo_domain_list);
24 DECLARE_RWSEM(tomoyo_domain_list_lock);
25
26 /* Structure for "initialize_domain" and "no_initialize_domain" keyword. */
27 struct tomoyo_domain_initializer_entry {
28         struct list_head list;
29         const struct tomoyo_path_info *domainname;    /* This may be NULL */
30         const struct tomoyo_path_info *program;
31         bool is_deleted;
32         bool is_not;       /* True if this entry is "no_initialize_domain".  */
33         /* True if the domainname is tomoyo_get_last_name(). */
34         bool is_last_name;
35 };
36
37 /* Structure for "keep_domain" and "no_keep_domain" keyword. */
38 struct tomoyo_domain_keeper_entry {
39         struct list_head list;
40         const struct tomoyo_path_info *domainname;
41         const struct tomoyo_path_info *program;       /* This may be NULL */
42         bool is_deleted;
43         bool is_not;       /* True if this entry is "no_keep_domain".        */
44         /* True if the domainname is tomoyo_get_last_name(). */
45         bool is_last_name;
46 };
47
48 /* Structure for "alias" keyword. */
49 struct tomoyo_alias_entry {
50         struct list_head list;
51         const struct tomoyo_path_info *original_name;
52         const struct tomoyo_path_info *aliased_name;
53         bool is_deleted;
54 };
55
56 /**
57  * tomoyo_set_domain_flag - Set or clear domain's attribute flags.
58  *
59  * @domain:    Pointer to "struct tomoyo_domain_info".
60  * @is_delete: True if it is a delete request.
61  * @flags:     Flags to set or clear.
62  *
63  * Returns nothing.
64  */
65 void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
66                             const bool is_delete, const u8 flags)
67 {
68         /* We need to serialize because this is bitfield operation. */
69         static DEFINE_SPINLOCK(lock);
70         /***** CRITICAL SECTION START *****/
71         spin_lock(&lock);
72         if (!is_delete)
73                 domain->flags |= flags;
74         else
75                 domain->flags &= ~flags;
76         spin_unlock(&lock);
77         /***** CRITICAL SECTION END *****/
78 }
79
80 /**
81  * tomoyo_get_last_name - Get last component of a domainname.
82  *
83  * @domain: Pointer to "struct tomoyo_domain_info".
84  *
85  * Returns the last component of the domainname.
86  */
87 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
88 {
89         const char *cp0 = domain->domainname->name;
90         const char *cp1 = strrchr(cp0, ' ');
91
92         if (cp1)
93                 return cp1 + 1;
94         return cp0;
95 }
96
97 /* The list for "struct tomoyo_domain_initializer_entry". */
98 static LIST_HEAD(tomoyo_domain_initializer_list);
99 static DECLARE_RWSEM(tomoyo_domain_initializer_list_lock);
100
101 /**
102  * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
103  *
104  * @domainname: The name of domain. May be NULL.
105  * @program:    The name of program.
106  * @is_not:     True if it is "no_initialize_domain" entry.
107  * @is_delete:  True if it is a delete request.
108  *
109  * Returns 0 on success, negative value otherwise.
110  */
111 static int tomoyo_update_domain_initializer_entry(const char *domainname,
112                                                   const char *program,
113                                                   const bool is_not,
114                                                   const bool is_delete)
115 {
116         struct tomoyo_domain_initializer_entry *new_entry;
117         struct tomoyo_domain_initializer_entry *ptr;
118         const struct tomoyo_path_info *saved_program;
119         const struct tomoyo_path_info *saved_domainname = NULL;
120         int error = -ENOMEM;
121         bool is_last_name = false;
122
123         if (!tomoyo_is_correct_path(program, 1, -1, -1, __func__))
124                 return -EINVAL; /* No patterns allowed. */
125         if (domainname) {
126                 if (!tomoyo_is_domain_def(domainname) &&
127                     tomoyo_is_correct_path(domainname, 1, -1, -1, __func__))
128                         is_last_name = true;
129                 else if (!tomoyo_is_correct_domain(domainname, __func__))
130                         return -EINVAL;
131                 saved_domainname = tomoyo_save_name(domainname);
132                 if (!saved_domainname)
133                         return -ENOMEM;
134         }
135         saved_program = tomoyo_save_name(program);
136         if (!saved_program)
137                 return -ENOMEM;
138         /***** EXCLUSIVE SECTION START *****/
139         down_write(&tomoyo_domain_initializer_list_lock);
140         list_for_each_entry(ptr, &tomoyo_domain_initializer_list, list) {
141                 if (ptr->is_not != is_not ||
142                     ptr->domainname != saved_domainname ||
143                     ptr->program != saved_program)
144                         continue;
145                 ptr->is_deleted = is_delete;
146                 error = 0;
147                 goto out;
148         }
149         if (is_delete) {
150                 error = -ENOENT;
151                 goto out;
152         }
153         new_entry = tomoyo_alloc_element(sizeof(*new_entry));
154         if (!new_entry)
155                 goto out;
156         new_entry->domainname = saved_domainname;
157         new_entry->program = saved_program;
158         new_entry->is_not = is_not;
159         new_entry->is_last_name = is_last_name;
160         list_add_tail(&new_entry->list, &tomoyo_domain_initializer_list);
161         error = 0;
162  out:
163         up_write(&tomoyo_domain_initializer_list_lock);
164         /***** EXCLUSIVE SECTION END *****/
165         return error;
166 }
167
168 /**
169  * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
170  *
171  * @head: Pointer to "struct tomoyo_io_buffer".
172  *
173  * Returns true on success, false otherwise.
174  */
175 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
176 {
177         struct list_head *pos;
178         bool done = true;
179
180         down_read(&tomoyo_domain_initializer_list_lock);
181         list_for_each_cookie(pos, head->read_var2,
182                              &tomoyo_domain_initializer_list) {
183                 const char *no;
184                 const char *from = "";
185                 const char *domain = "";
186                 struct tomoyo_domain_initializer_entry *ptr;
187                 ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
188                                   list);
189                 if (ptr->is_deleted)
190                         continue;
191                 no = ptr->is_not ? "no_" : "";
192                 if (ptr->domainname) {
193                         from = " from ";
194                         domain = ptr->domainname->name;
195                 }
196                 if (!tomoyo_io_printf(head,
197                                       "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
198                                       "%s%s%s\n", no, ptr->program->name, from,
199                                       domain)) {
200                         done = false;
201                         break;
202                 }
203         }
204         up_read(&tomoyo_domain_initializer_list_lock);
205         return done;
206 }
207
208 /**
209  * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
210  *
211  * @data:      String to parse.
212  * @is_not:    True if it is "no_initialize_domain" entry.
213  * @is_delete: True if it is a delete request.
214  *
215  * Returns 0 on success, negative value otherwise.
216  */
217 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
218                                            const bool is_delete)
219 {
220         char *cp = strstr(data, " from ");
221
222         if (cp) {
223                 *cp = '\0';
224                 return tomoyo_update_domain_initializer_entry(cp + 6, data,
225                                                               is_not,
226                                                               is_delete);
227         }
228         return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
229                                                       is_delete);
230 }
231
232 /**
233  * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
234  *
235  * @domainname: The name of domain.
236  * @program:    The name of program.
237  * @last_name:  The last component of @domainname.
238  *
239  * Returns true if executing @program reinitializes domain transition,
240  * false otherwise.
241  */
242 static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
243                                          domainname,
244                                          const struct tomoyo_path_info *program,
245                                          const struct tomoyo_path_info *
246                                          last_name)
247 {
248         struct tomoyo_domain_initializer_entry *ptr;
249         bool flag = false;
250
251         down_read(&tomoyo_domain_initializer_list_lock);
252         list_for_each_entry(ptr,  &tomoyo_domain_initializer_list, list) {
253                 if (ptr->is_deleted)
254                         continue;
255                 if (ptr->domainname) {
256                         if (!ptr->is_last_name) {
257                                 if (ptr->domainname != domainname)
258                                         continue;
259                         } else {
260                                 if (tomoyo_pathcmp(ptr->domainname, last_name))
261                                         continue;
262                         }
263                 }
264                 if (tomoyo_pathcmp(ptr->program, program))
265                         continue;
266                 if (ptr->is_not) {
267                         flag = false;
268                         break;
269                 }
270                 flag = true;
271         }
272         up_read(&tomoyo_domain_initializer_list_lock);
273         return flag;
274 }
275
276 /* The list for "struct tomoyo_domain_keeper_entry". */
277 static LIST_HEAD(tomoyo_domain_keeper_list);
278 static DECLARE_RWSEM(tomoyo_domain_keeper_list_lock);
279
280 /**
281  * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
282  *
283  * @domainname: The name of domain.
284  * @program:    The name of program. May be NULL.
285  * @is_not:     True if it is "no_keep_domain" entry.
286  * @is_delete:  True if it is a delete request.
287  *
288  * Returns 0 on success, negative value otherwise.
289  */
290 static int tomoyo_update_domain_keeper_entry(const char *domainname,
291                                              const char *program,
292                                              const bool is_not,
293                                              const bool is_delete)
294 {
295         struct tomoyo_domain_keeper_entry *new_entry;
296         struct tomoyo_domain_keeper_entry *ptr;
297         const struct tomoyo_path_info *saved_domainname;
298         const struct tomoyo_path_info *saved_program = NULL;
299         int error = -ENOMEM;
300         bool is_last_name = false;
301
302         if (!tomoyo_is_domain_def(domainname) &&
303             tomoyo_is_correct_path(domainname, 1, -1, -1, __func__))
304                 is_last_name = true;
305         else if (!tomoyo_is_correct_domain(domainname, __func__))
306                 return -EINVAL;
307         if (program) {
308                 if (!tomoyo_is_correct_path(program, 1, -1, -1, __func__))
309                         return -EINVAL;
310                 saved_program = tomoyo_save_name(program);
311                 if (!saved_program)
312                         return -ENOMEM;
313         }
314         saved_domainname = tomoyo_save_name(domainname);
315         if (!saved_domainname)
316                 return -ENOMEM;
317         /***** EXCLUSIVE SECTION START *****/
318         down_write(&tomoyo_domain_keeper_list_lock);
319         list_for_each_entry(ptr, &tomoyo_domain_keeper_list, list) {
320                 if (ptr->is_not != is_not ||
321                     ptr->domainname != saved_domainname ||
322                     ptr->program != saved_program)
323                         continue;
324                 ptr->is_deleted = is_delete;
325                 error = 0;
326                 goto out;
327         }
328         if (is_delete) {
329                 error = -ENOENT;
330                 goto out;
331         }
332         new_entry = tomoyo_alloc_element(sizeof(*new_entry));
333         if (!new_entry)
334                 goto out;
335         new_entry->domainname = saved_domainname;
336         new_entry->program = saved_program;
337         new_entry->is_not = is_not;
338         new_entry->is_last_name = is_last_name;
339         list_add_tail(&new_entry->list, &tomoyo_domain_keeper_list);
340         error = 0;
341  out:
342         up_write(&tomoyo_domain_keeper_list_lock);
343         /***** EXCLUSIVE SECTION END *****/
344         return error;
345 }
346
347 /**
348  * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
349  *
350  * @data:      String to parse.
351  * @is_not:    True if it is "no_keep_domain" entry.
352  * @is_delete: True if it is a delete request.
353  *
354  */
355 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
356                                       const bool is_delete)
357 {
358         char *cp = strstr(data, " from ");
359
360         if (cp) {
361                 *cp = '\0';
362                 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
363                                                          is_delete);
364         }
365         return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
366 }
367
368 /**
369  * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
370  *
371  * @head: Pointer to "struct tomoyo_io_buffer".
372  *
373  * Returns true on success, false otherwise.
374  */
375 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
376 {
377         struct list_head *pos;
378         bool done = true;
379
380         down_read(&tomoyo_domain_keeper_list_lock);
381         list_for_each_cookie(pos, head->read_var2,
382                              &tomoyo_domain_keeper_list) {
383                 struct tomoyo_domain_keeper_entry *ptr;
384                 const char *no;
385                 const char *from = "";
386                 const char *program = "";
387
388                 ptr = list_entry(pos, struct tomoyo_domain_keeper_entry, list);
389                 if (ptr->is_deleted)
390                         continue;
391                 no = ptr->is_not ? "no_" : "";
392                 if (ptr->program) {
393                         from = " from ";
394                         program = ptr->program->name;
395                 }
396                 if (!tomoyo_io_printf(head,
397                                       "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
398                                       "%s%s%s\n", no, program, from,
399                                       ptr->domainname->name)) {
400                         done = false;
401                         break;
402                 }
403         }
404         up_read(&tomoyo_domain_keeper_list_lock);
405         return done;
406 }
407
408 /**
409  * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
410  *
411  * @domainname: The name of domain.
412  * @program:    The name of program.
413  * @last_name:  The last component of @domainname.
414  *
415  * Returns true if executing @program supresses domain transition,
416  * false otherwise.
417  */
418 static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
419                                     const struct tomoyo_path_info *program,
420                                     const struct tomoyo_path_info *last_name)
421 {
422         struct tomoyo_domain_keeper_entry *ptr;
423         bool flag = false;
424
425         down_read(&tomoyo_domain_keeper_list_lock);
426         list_for_each_entry(ptr, &tomoyo_domain_keeper_list, list) {
427                 if (ptr->is_deleted)
428                         continue;
429                 if (!ptr->is_last_name) {
430                         if (ptr->domainname != domainname)
431                                 continue;
432                 } else {
433                         if (tomoyo_pathcmp(ptr->domainname, last_name))
434                                 continue;
435                 }
436                 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
437                         continue;
438                 if (ptr->is_not) {
439                         flag = false;
440                         break;
441                 }
442                 flag = true;
443         }
444         up_read(&tomoyo_domain_keeper_list_lock);
445         return flag;
446 }
447
448 /* The list for "struct tomoyo_alias_entry". */
449 static LIST_HEAD(tomoyo_alias_list);
450 static DECLARE_RWSEM(tomoyo_alias_list_lock);
451
452 /**
453  * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
454  *
455  * @original_name: The original program's real name.
456  * @aliased_name:  The symbolic program's symbolic link's name.
457  * @is_delete:     True if it is a delete request.
458  *
459  * Returns 0 on success, negative value otherwise.
460  */
461 static int tomoyo_update_alias_entry(const char *original_name,
462                                      const char *aliased_name,
463                                      const bool is_delete)
464 {
465         struct tomoyo_alias_entry *new_entry;
466         struct tomoyo_alias_entry *ptr;
467         const struct tomoyo_path_info *saved_original_name;
468         const struct tomoyo_path_info *saved_aliased_name;
469         int error = -ENOMEM;
470
471         if (!tomoyo_is_correct_path(original_name, 1, -1, -1, __func__) ||
472             !tomoyo_is_correct_path(aliased_name, 1, -1, -1, __func__))
473                 return -EINVAL; /* No patterns allowed. */
474         saved_original_name = tomoyo_save_name(original_name);
475         saved_aliased_name = tomoyo_save_name(aliased_name);
476         if (!saved_original_name || !saved_aliased_name)
477                 return -ENOMEM;
478         /***** EXCLUSIVE SECTION START *****/
479         down_write(&tomoyo_alias_list_lock);
480         list_for_each_entry(ptr, &tomoyo_alias_list, list) {
481                 if (ptr->original_name != saved_original_name ||
482                     ptr->aliased_name != saved_aliased_name)
483                         continue;
484                 ptr->is_deleted = is_delete;
485                 error = 0;
486                 goto out;
487         }
488         if (is_delete) {
489                 error = -ENOENT;
490                 goto out;
491         }
492         new_entry = tomoyo_alloc_element(sizeof(*new_entry));
493         if (!new_entry)
494                 goto out;
495         new_entry->original_name = saved_original_name;
496         new_entry->aliased_name = saved_aliased_name;
497         list_add_tail(&new_entry->list, &tomoyo_alias_list);
498         error = 0;
499  out:
500         up_write(&tomoyo_alias_list_lock);
501         /***** EXCLUSIVE SECTION END *****/
502         return error;
503 }
504
505 /**
506  * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
507  *
508  * @head: Pointer to "struct tomoyo_io_buffer".
509  *
510  * Returns true on success, false otherwise.
511  */
512 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
513 {
514         struct list_head *pos;
515         bool done = true;
516
517         down_read(&tomoyo_alias_list_lock);
518         list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
519                 struct tomoyo_alias_entry *ptr;
520
521                 ptr = list_entry(pos, struct tomoyo_alias_entry, list);
522                 if (ptr->is_deleted)
523                         continue;
524                 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
525                                       ptr->original_name->name,
526                                       ptr->aliased_name->name)) {
527                         done = false;
528                         break;
529                 }
530         }
531         up_read(&tomoyo_alias_list_lock);
532         return done;
533 }
534
535 /**
536  * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
537  *
538  * @data:      String to parse.
539  * @is_delete: True if it is a delete request.
540  *
541  * Returns 0 on success, negative value otherwise.
542  */
543 int tomoyo_write_alias_policy(char *data, const bool is_delete)
544 {
545         char *cp = strchr(data, ' ');
546
547         if (!cp)
548                 return -EINVAL;
549         *cp++ = '\0';
550         return tomoyo_update_alias_entry(data, cp, is_delete);
551 }
552
553 /* Domain create/delete handler. */
554
555 /**
556  * tomoyo_delete_domain - Delete a domain.
557  *
558  * @domainname: The name of domain.
559  *
560  * Returns 0.
561  */
562 int tomoyo_delete_domain(char *domainname)
563 {
564         struct tomoyo_domain_info *domain;
565         struct tomoyo_path_info name;
566
567         name.name = domainname;
568         tomoyo_fill_path_info(&name);
569         /***** EXCLUSIVE SECTION START *****/
570         down_write(&tomoyo_domain_list_lock);
571         /* Is there an active domain? */
572         list_for_each_entry(domain, &tomoyo_domain_list, list) {
573                 /* Never delete tomoyo_kernel_domain */
574                 if (domain == &tomoyo_kernel_domain)
575                         continue;
576                 if (domain->is_deleted ||
577                     tomoyo_pathcmp(domain->domainname, &name))
578                         continue;
579                 domain->is_deleted = true;
580                 break;
581         }
582         up_write(&tomoyo_domain_list_lock);
583         /***** EXCLUSIVE SECTION END *****/
584         return 0;
585 }
586
587 /**
588  * tomoyo_find_or_assign_new_domain - Create a domain.
589  *
590  * @domainname: The name of domain.
591  * @profile:    Profile number to assign if the domain was newly created.
592  *
593  * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
594  */
595 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
596                                                             domainname,
597                                                             const u8 profile)
598 {
599         struct tomoyo_domain_info *domain = NULL;
600         const struct tomoyo_path_info *saved_domainname;
601
602         /***** EXCLUSIVE SECTION START *****/
603         down_write(&tomoyo_domain_list_lock);
604         domain = tomoyo_find_domain(domainname);
605         if (domain)
606                 goto out;
607         if (!tomoyo_is_correct_domain(domainname, __func__))
608                 goto out;
609         saved_domainname = tomoyo_save_name(domainname);
610         if (!saved_domainname)
611                 goto out;
612         /* Can I reuse memory of deleted domain? */
613         list_for_each_entry(domain, &tomoyo_domain_list, list) {
614                 struct task_struct *p;
615                 struct tomoyo_acl_info *ptr;
616                 bool flag;
617                 if (!domain->is_deleted ||
618                     domain->domainname != saved_domainname)
619                         continue;
620                 flag = false;
621                 /***** CRITICAL SECTION START *****/
622                 read_lock(&tasklist_lock);
623                 for_each_process(p) {
624                         if (tomoyo_real_domain(p) != domain)
625                                 continue;
626                         flag = true;
627                         break;
628                 }
629                 read_unlock(&tasklist_lock);
630                 /***** CRITICAL SECTION END *****/
631                 if (flag)
632                         continue;
633                 list_for_each_entry(ptr, &domain->acl_info_list, list) {
634                         ptr->type |= TOMOYO_ACL_DELETED;
635                 }
636                 tomoyo_set_domain_flag(domain, true, domain->flags);
637                 domain->profile = profile;
638                 domain->quota_warned = false;
639                 mb(); /* Avoid out-of-order execution. */
640                 domain->is_deleted = false;
641                 goto out;
642         }
643         /* No memory reusable. Create using new memory. */
644         domain = tomoyo_alloc_element(sizeof(*domain));
645         if (domain) {
646                 INIT_LIST_HEAD(&domain->acl_info_list);
647                 domain->domainname = saved_domainname;
648                 domain->profile = profile;
649                 list_add_tail(&domain->list, &tomoyo_domain_list);
650         }
651  out:
652         up_write(&tomoyo_domain_list_lock);
653         /***** EXCLUSIVE SECTION END *****/
654         return domain;
655 }
656
657 /**
658  * tomoyo_find_next_domain - Find a domain.
659  *
660  * @bprm:           Pointer to "struct linux_binprm".
661  * @next_domain:    Pointer to pointer to "struct tomoyo_domain_info".
662  *
663  * Returns 0 on success, negative value otherwise.
664  */
665 int tomoyo_find_next_domain(struct linux_binprm *bprm,
666                             struct tomoyo_domain_info **next_domain)
667 {
668         /*
669          * This function assumes that the size of buffer returned by
670          * tomoyo_realpath() = TOMOYO_MAX_PATHNAME_LEN.
671          */
672         struct tomoyo_page_buffer *tmp = tomoyo_alloc(sizeof(*tmp));
673         struct tomoyo_domain_info *old_domain = tomoyo_domain();
674         struct tomoyo_domain_info *domain = NULL;
675         const char *old_domain_name = old_domain->domainname->name;
676         const char *original_name = bprm->filename;
677         char *new_domain_name = NULL;
678         char *real_program_name = NULL;
679         char *symlink_program_name = NULL;
680         const u8 mode = tomoyo_check_flags(old_domain, TOMOYO_MAC_FOR_FILE);
681         const bool is_enforce = (mode == 3);
682         int retval = -ENOMEM;
683         struct tomoyo_path_info r; /* real name */
684         struct tomoyo_path_info s; /* symlink name */
685         struct tomoyo_path_info l; /* last name */
686         static bool initialized;
687
688         if (!tmp)
689                 goto out;
690
691         if (!initialized) {
692                 /*
693                  * Built-in initializers. This is needed because policies are
694                  * not loaded until starting /sbin/init.
695                  */
696                 tomoyo_update_domain_initializer_entry(NULL, "/sbin/hotplug",
697                                                        false, false);
698                 tomoyo_update_domain_initializer_entry(NULL, "/sbin/modprobe",
699                                                        false, false);
700                 initialized = true;
701         }
702
703         /* Get tomoyo_realpath of program. */
704         retval = -ENOENT;
705         /* I hope tomoyo_realpath() won't fail with -ENOMEM. */
706         real_program_name = tomoyo_realpath(original_name);
707         if (!real_program_name)
708                 goto out;
709         /* Get tomoyo_realpath of symbolic link. */
710         symlink_program_name = tomoyo_realpath_nofollow(original_name);
711         if (!symlink_program_name)
712                 goto out;
713
714         r.name = real_program_name;
715         tomoyo_fill_path_info(&r);
716         s.name = symlink_program_name;
717         tomoyo_fill_path_info(&s);
718         l.name = tomoyo_get_last_name(old_domain);
719         tomoyo_fill_path_info(&l);
720
721         /* Check 'alias' directive. */
722         if (tomoyo_pathcmp(&r, &s)) {
723                 struct tomoyo_alias_entry *ptr;
724                 /* Is this program allowed to be called via symbolic links? */
725                 down_read(&tomoyo_alias_list_lock);
726                 list_for_each_entry(ptr, &tomoyo_alias_list, list) {
727                         if (ptr->is_deleted ||
728                             tomoyo_pathcmp(&r, ptr->original_name) ||
729                             tomoyo_pathcmp(&s, ptr->aliased_name))
730                                 continue;
731                         memset(real_program_name, 0, TOMOYO_MAX_PATHNAME_LEN);
732                         strncpy(real_program_name, ptr->aliased_name->name,
733                                 TOMOYO_MAX_PATHNAME_LEN - 1);
734                         tomoyo_fill_path_info(&r);
735                         break;
736                 }
737                 up_read(&tomoyo_alias_list_lock);
738         }
739
740         /* Check execute permission. */
741         retval = tomoyo_check_exec_perm(old_domain, &r, tmp);
742         if (retval < 0)
743                 goto out;
744
745         new_domain_name = tmp->buffer;
746         if (tomoyo_is_domain_initializer(old_domain->domainname, &r, &l)) {
747                 /* Transit to the child of tomoyo_kernel_domain domain. */
748                 snprintf(new_domain_name, TOMOYO_MAX_PATHNAME_LEN + 1,
749                          TOMOYO_ROOT_NAME " " "%s", real_program_name);
750         } else if (old_domain == &tomoyo_kernel_domain &&
751                    !tomoyo_policy_loaded) {
752                 /*
753                  * Needn't to transit from kernel domain before starting
754                  * /sbin/init. But transit from kernel domain if executing
755                  * initializers because they might start before /sbin/init.
756                  */
757                 domain = old_domain;
758         } else if (tomoyo_is_domain_keeper(old_domain->domainname, &r, &l)) {
759                 /* Keep current domain. */
760                 domain = old_domain;
761         } else {
762                 /* Normal domain transition. */
763                 snprintf(new_domain_name, TOMOYO_MAX_PATHNAME_LEN + 1,
764                          "%s %s", old_domain_name, real_program_name);
765         }
766         if (domain || strlen(new_domain_name) >= TOMOYO_MAX_PATHNAME_LEN)
767                 goto done;
768         down_read(&tomoyo_domain_list_lock);
769         domain = tomoyo_find_domain(new_domain_name);
770         up_read(&tomoyo_domain_list_lock);
771         if (domain)
772                 goto done;
773         if (is_enforce)
774                 goto done;
775         domain = tomoyo_find_or_assign_new_domain(new_domain_name,
776                                                   old_domain->profile);
777  done:
778         if (domain)
779                 goto out;
780         printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",
781                new_domain_name);
782         if (is_enforce)
783                 retval = -EPERM;
784         else
785                 tomoyo_set_domain_flag(old_domain, false,
786                                        TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED);
787  out:
788         tomoyo_free(real_program_name);
789         tomoyo_free(symlink_program_name);
790         *next_domain = domain ? domain : old_domain;
791         tomoyo_free(tmp);
792         return retval;
793 }