blob: af5f325e2f33127605a7a58222c7ca49ccde2a53 [file] [log] [blame]
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09001/*
2 * security/tomoyo/domain.c
3 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09004 * Domain transition functions for TOMOYO.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09005 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09007 */
8
9#include "common.h"
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090010#include <linux/binfmts.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090012
13/* Variables definitions.*/
14
Tetsuo Handa32997142011-06-26 23:19:28 +090015/* The global ACL referred by "use_group" keyword. */
16struct list_head tomoyo_acl_group[TOMOYO_MAX_ACL_GROUPS];
17
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090018/* The initial domain. */
19struct tomoyo_domain_info tomoyo_kernel_domain;
20
Tetsuo Handa237ab452010-06-12 20:46:22 +090021/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090022 * tomoyo_update_policy - Update an entry for exception policy.
23 *
24 * @new_entry: Pointer to "struct tomoyo_acl_info".
25 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090026 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090027 * @check_duplicate: Callback function to find duplicated entry.
28 *
29 * Returns 0 on success, negative value otherwise.
30 *
31 * Caller holds tomoyo_read_lock().
32 */
33int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090034 struct tomoyo_acl_param *param,
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090035 bool (*check_duplicate) (const struct tomoyo_acl_head
36 *,
37 const struct tomoyo_acl_head
38 *))
39{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090040 int error = param->is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090041 struct tomoyo_acl_head *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090042 struct list_head *list = param->list;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090043
44 if (mutex_lock_interruptible(&tomoyo_policy_lock))
45 return -ENOMEM;
46 list_for_each_entry_rcu(entry, list, list) {
47 if (!check_duplicate(entry, new_entry))
48 continue;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090049 entry->is_deleted = param->is_delete;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090050 error = 0;
51 break;
52 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090053 if (error && !param->is_delete) {
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090054 entry = tomoyo_commit_ok(new_entry, size);
55 if (entry) {
56 list_add_tail_rcu(&entry->list, list);
57 error = 0;
58 }
59 }
60 mutex_unlock(&tomoyo_policy_lock);
61 return error;
62}
63
64/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090065 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
66 *
67 * @a: Pointer to "struct tomoyo_acl_info".
68 * @b: Pointer to "struct tomoyo_acl_info".
69 *
70 * Returns true if @a == @b, false otherwise.
71 */
72static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
73 const struct tomoyo_acl_info *b)
74{
75 return a->type == b->type;
76}
77
78/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090079 * tomoyo_update_domain - Update an entry for domain policy.
80 *
81 * @new_entry: Pointer to "struct tomoyo_acl_info".
82 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090083 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa237ab452010-06-12 20:46:22 +090084 * @check_duplicate: Callback function to find duplicated entry.
85 * @merge_duplicate: Callback function to merge duplicated entry.
86 *
87 * Returns 0 on success, negative value otherwise.
88 *
89 * Caller holds tomoyo_read_lock().
90 */
91int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090092 struct tomoyo_acl_param *param,
Tetsuo Handa237ab452010-06-12 20:46:22 +090093 bool (*check_duplicate) (const struct tomoyo_acl_info
94 *,
95 const struct tomoyo_acl_info
96 *),
97 bool (*merge_duplicate) (struct tomoyo_acl_info *,
98 struct tomoyo_acl_info *,
99 const bool))
100{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900101 const bool is_delete = param->is_delete;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900102 int error = is_delete ? -ENOENT : -ENOMEM;
103 struct tomoyo_acl_info *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900104 struct list_head * const list = param->list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900105
106 if (mutex_lock_interruptible(&tomoyo_policy_lock))
107 return error;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900108 list_for_each_entry_rcu(entry, list, list) {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900109 if (!tomoyo_same_acl_head(entry, new_entry) ||
110 !check_duplicate(entry, new_entry))
Tetsuo Handa237ab452010-06-12 20:46:22 +0900111 continue;
112 if (merge_duplicate)
113 entry->is_deleted = merge_duplicate(entry, new_entry,
114 is_delete);
115 else
116 entry->is_deleted = is_delete;
117 error = 0;
118 break;
119 }
120 if (error && !is_delete) {
121 entry = tomoyo_commit_ok(new_entry, size);
122 if (entry) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900123 list_add_tail_rcu(&entry->list, list);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900124 error = 0;
125 }
126 }
127 mutex_unlock(&tomoyo_policy_lock);
128 return error;
129}
130
Tetsuo Handa32997142011-06-26 23:19:28 +0900131/**
132 * tomoyo_check_acl - Do permission check.
133 *
134 * @r: Pointer to "struct tomoyo_request_info".
135 * @check_entry: Callback function to check type specific parameters.
136 *
137 * Returns 0 on success, negative value otherwise.
138 *
139 * Caller holds tomoyo_read_lock().
140 */
Tetsuo Handa99a85252010-06-16 16:22:51 +0900141void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900142 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900143 const struct tomoyo_acl_info *))
144{
145 const struct tomoyo_domain_info *domain = r->domain;
146 struct tomoyo_acl_info *ptr;
Tetsuo Handa32997142011-06-26 23:19:28 +0900147 bool retried = false;
148 const struct list_head *list = &domain->acl_info_list;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900149
Tetsuo Handa32997142011-06-26 23:19:28 +0900150retry:
151 list_for_each_entry_rcu(ptr, list, list) {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900152 if (ptr->is_deleted || ptr->type != r->param_type)
153 continue;
154 if (check_entry(r, ptr)) {
155 r->granted = true;
156 return;
157 }
158 }
Tetsuo Handa32997142011-06-26 23:19:28 +0900159 if (!retried) {
160 retried = true;
161 list = &tomoyo_acl_group[domain->group];
162 goto retry;
163 }
Tetsuo Handa99a85252010-06-16 16:22:51 +0900164 r->granted = false;
165}
166
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900167/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900168LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900169
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900170struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
171struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
172
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900173/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900174 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900175 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900176 * @domainname: Domainname to check.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900177 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900178 * Returns the last word of @domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900179 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900180static const char *tomoyo_last_word(const char *name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900181{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900182 const char *cp = strrchr(name, ' ');
183 if (cp)
184 return cp + 1;
185 return name;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900186}
187
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900188/**
189 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
190 *
191 * @a: Pointer to "struct tomoyo_acl_head".
192 * @b: Pointer to "struct tomoyo_acl_head".
193 *
194 * Returns true if @a == @b, false otherwise.
195 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900196static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
197 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900198{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900199 const struct tomoyo_transition_control *p1 = container_of(a,
200 typeof(*p1),
201 head);
202 const struct tomoyo_transition_control *p2 = container_of(b,
203 typeof(*p2),
204 head);
205 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900206 && p1->domainname == p2->domainname
207 && p1->program == p2->program;
208}
209
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900210/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900211 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900212 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900213 * @param: Pointer to "struct tomoyo_acl_param".
214 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900215 *
216 * Returns 0 on success, negative value otherwise.
217 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900218int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
219 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900220{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900221 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900222 int error = param->is_delete ? -ENOENT : -ENOMEM;
223 char *program = param->data;
224 char *domainname = strstr(program, " from ");
225 if (domainname) {
226 *domainname = '\0';
227 domainname += 6;
228 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
229 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
230 domainname = program;
231 program = NULL;
232 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900233 if (program && strcmp(program, "any")) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900234 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900235 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900236 e.program = tomoyo_get_name(program);
237 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900238 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900239 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900240 if (domainname && strcmp(domainname, "any")) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900241 if (!tomoyo_correct_domain(domainname)) {
242 if (!tomoyo_correct_path(domainname))
243 goto out;
244 e.is_last_name = true;
245 }
246 e.domainname = tomoyo_get_name(domainname);
247 if (!e.domainname)
248 goto out;
249 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900250 param->list = &tomoyo_policy_list[TOMOYO_ID_TRANSITION_CONTROL];
251 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900252 tomoyo_same_transition_control);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900253out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900254 tomoyo_put_name(e.domainname);
255 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900256 return error;
257}
258
259/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900260 * tomoyo_transition_type - Get domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900261 *
262 * @domainname: The name of domain.
263 * @program: The name of program.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900264 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900265 * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
266 * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
267 * @program suppresses domain transition, others otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900268 *
269 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900270 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900271static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
272 const struct tomoyo_path_info *program)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900273{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900274 const struct tomoyo_transition_control *ptr;
275 const char *last_name = tomoyo_last_word(domainname->name);
276 u8 type;
277 for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
278 next:
279 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
280 [TOMOYO_ID_TRANSITION_CONTROL],
281 head.list) {
282 if (ptr->head.is_deleted || ptr->type != type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900283 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900284 if (ptr->domainname) {
285 if (!ptr->is_last_name) {
286 if (ptr->domainname != domainname)
287 continue;
288 } else {
289 /*
290 * Use direct strcmp() since this is
291 * unlikely used.
292 */
293 if (strcmp(ptr->domainname->name,
294 last_name))
295 continue;
296 }
297 }
298 if (ptr->program &&
299 tomoyo_pathcmp(ptr->program, program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900300 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900301 if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
302 /*
303 * Do not check for initialize_domain if
304 * no_initialize_domain matched.
305 */
306 type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
307 goto next;
308 }
309 goto done;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900310 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900311 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900312 done:
313 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900314}
315
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900316/**
317 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
318 *
319 * @a: Pointer to "struct tomoyo_acl_head".
320 * @b: Pointer to "struct tomoyo_acl_head".
321 *
322 * Returns true if @a == @b, false otherwise.
323 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900324static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
325 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900326{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900327 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
328 head);
329 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
330 head);
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900331 return p1->original_name == p2->original_name &&
332 p1->aggregated_name == p2->aggregated_name;
333}
334
Tetsuo Handa10843072010-06-03 20:38:03 +0900335/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900336 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900337 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900338 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa10843072010-06-03 20:38:03 +0900339 *
340 * Returns 0 on success, negative value otherwise.
341 *
342 * Caller holds tomoyo_read_lock().
343 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900344int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
Tetsuo Handa10843072010-06-03 20:38:03 +0900345{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900346 struct tomoyo_aggregator e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900347 int error = param->is_delete ? -ENOENT : -ENOMEM;
348 const char *original_name = tomoyo_read_token(param);
349 const char *aggregated_name = tomoyo_read_token(param);
350 if (!tomoyo_correct_word(original_name) ||
Tetsuo Handa75093152010-06-16 16:23:55 +0900351 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900352 return -EINVAL;
353 e.original_name = tomoyo_get_name(original_name);
354 e.aggregated_name = tomoyo_get_name(aggregated_name);
355 if (!e.original_name || !e.aggregated_name ||
356 e.aggregated_name->is_patterned) /* No patterns allowed. */
357 goto out;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900358 param->list = &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR];
359 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900360 tomoyo_same_aggregator);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900361out:
Tetsuo Handa10843072010-06-03 20:38:03 +0900362 tomoyo_put_name(e.original_name);
363 tomoyo_put_name(e.aggregated_name);
364 return error;
365}
366
367/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900368 * tomoyo_assign_domain - Create a domain.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900369 *
370 * @domainname: The name of domain.
371 * @profile: Profile number to assign if the domain was newly created.
372 *
373 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900374 *
375 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900376 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900377struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
378 const u8 profile)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900379{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900380 struct tomoyo_domain_info *entry;
Tetsuo Handa29282382010-05-06 00:18:15 +0900381 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900382 const struct tomoyo_path_info *saved_domainname;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900383 bool found = false;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900384
Tetsuo Handa75093152010-06-16 16:23:55 +0900385 if (!tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900386 return NULL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900387 saved_domainname = tomoyo_get_name(domainname);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900388 if (!saved_domainname)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900389 return NULL;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900390 entry = kzalloc(sizeof(*entry), GFP_NOFS);
Tetsuo Handa29282382010-05-06 00:18:15 +0900391 if (mutex_lock_interruptible(&tomoyo_policy_lock))
392 goto out;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900393 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
394 if (domain->is_deleted ||
395 tomoyo_pathcmp(saved_domainname, domain->domainname))
396 continue;
397 found = true;
398 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900399 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900400 if (!found && tomoyo_memory_ok(entry)) {
401 INIT_LIST_HEAD(&entry->acl_info_list);
402 entry->domainname = saved_domainname;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900403 saved_domainname = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900404 entry->profile = profile;
405 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
406 domain = entry;
407 entry = NULL;
408 found = true;
409 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900410 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900411 out:
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900412 tomoyo_put_name(saved_domainname);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900413 kfree(entry);
414 return found ? domain : NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900415}
416
417/**
418 * tomoyo_find_next_domain - Find a domain.
419 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900420 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900421 *
422 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900423 *
424 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900425 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900426int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900427{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900428 struct tomoyo_request_info r;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900429 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900430 struct tomoyo_domain_info *old_domain = tomoyo_domain();
431 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900432 const char *original_name = bprm->filename;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900433 u8 mode;
434 bool is_enforce;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900435 int retval = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900436 bool need_kfree = false;
437 struct tomoyo_path_info rn = { }; /* real name */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900438
Tetsuo Handa57c25902010-06-03 20:38:44 +0900439 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
440 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900441 if (!tmp)
442 goto out;
443
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900444 retry:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900445 if (need_kfree) {
446 kfree(rn.name);
447 need_kfree = false;
448 }
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900449 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900450 retval = -ENOENT;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900451 rn.name = tomoyo_realpath_nofollow(original_name);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900452 if (!rn.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900453 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900454 tomoyo_fill_path_info(&rn);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900455 need_kfree = true;
456
Tetsuo Handa10843072010-06-03 20:38:03 +0900457 /* Check 'aggregator' directive. */
458 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900459 struct tomoyo_aggregator *ptr;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900460 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
461 [TOMOYO_ID_AGGREGATOR], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900462 if (ptr->head.is_deleted ||
Tetsuo Handa10843072010-06-03 20:38:03 +0900463 !tomoyo_path_matches_pattern(&rn,
464 ptr->original_name))
465 continue;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900466 kfree(rn.name);
Tetsuo Handa10843072010-06-03 20:38:03 +0900467 need_kfree = false;
468 /* This is OK because it is read only. */
469 rn = *ptr->aggregated_name;
470 break;
471 }
472 }
473
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900474 /* Check execute permission. */
Tetsuo Handa05336de2010-06-16 16:20:24 +0900475 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900476 if (retval == TOMOYO_RETRY_REQUEST)
477 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900478 if (retval < 0)
479 goto out;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900480 /*
481 * To be able to specify domainnames with wildcards, use the
482 * pathname specified in the policy (which may contain
483 * wildcard) rather than the pathname passed to execve()
484 * (which never contains wildcard).
485 */
486 if (r.param.path.matched_path) {
487 if (need_kfree)
488 kfree(rn.name);
489 need_kfree = false;
490 /* This is OK because it is read only. */
491 rn = *r.param.path.matched_path;
492 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900493
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900494 /* Calculate domain to transit to. */
495 switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
496 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900497 /* Transit to the child of tomoyo_kernel_domain domain. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900498 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
499 "%s", rn.name);
500 break;
501 case TOMOYO_TRANSITION_CONTROL_KEEP:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900502 /* Keep current domain. */
503 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900504 break;
505 default:
506 if (old_domain == &tomoyo_kernel_domain &&
507 !tomoyo_policy_loaded) {
508 /*
509 * Needn't to transit from kernel domain before
510 * starting /sbin/init. But transit from kernel domain
511 * if executing initializers because they might start
512 * before /sbin/init.
513 */
514 domain = old_domain;
515 } else {
516 /* Normal domain transition. */
517 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
518 old_domain->domainname->name, rn.name);
519 }
520 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900521 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900522 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900523 goto done;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900524 domain = tomoyo_find_domain(tmp);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900525 if (!domain)
526 domain = tomoyo_assign_domain(tmp, old_domain->profile);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900527 done:
528 if (domain)
529 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900530 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900531 if (is_enforce)
532 retval = -EPERM;
533 else
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900534 old_domain->transition_failed = true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900535 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900536 if (!domain)
537 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900538 /* Update reference count on "struct tomoyo_domain_info". */
539 atomic_inc(&domain->users);
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900540 bprm->cred->security = domain;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900541 if (need_kfree)
542 kfree(rn.name);
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900543 kfree(tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900544 return retval;
545}