blob: 43977083254b4c18e4bbda1e14624085cb7ab9e7 [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
15/* The initial domain. */
16struct tomoyo_domain_info tomoyo_kernel_domain;
17
Tetsuo Handa237ab452010-06-12 20:46:22 +090018/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090019 * 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
61/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090062 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
63 *
64 * @a: Pointer to "struct tomoyo_acl_info".
65 * @b: Pointer to "struct tomoyo_acl_info".
66 *
67 * Returns true if @a == @b, false otherwise.
68 */
69static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
70 const struct tomoyo_acl_info *b)
71{
72 return a->type == b->type;
73}
74
75/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090076 * tomoyo_update_domain - Update an entry for domain policy.
77 *
78 * @new_entry: Pointer to "struct tomoyo_acl_info".
79 * @size: Size of @new_entry in bytes.
80 * @is_delete: True if it is a delete request.
81 * @domain: Pointer to "struct tomoyo_domain_info".
82 * @check_duplicate: Callback function to find duplicated entry.
83 * @merge_duplicate: Callback function to merge duplicated entry.
84 *
85 * Returns 0 on success, negative value otherwise.
86 *
87 * Caller holds tomoyo_read_lock().
88 */
89int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
90 bool is_delete, struct tomoyo_domain_info *domain,
91 bool (*check_duplicate) (const struct tomoyo_acl_info
92 *,
93 const struct tomoyo_acl_info
94 *),
95 bool (*merge_duplicate) (struct tomoyo_acl_info *,
96 struct tomoyo_acl_info *,
97 const bool))
98{
99 int error = is_delete ? -ENOENT : -ENOMEM;
100 struct tomoyo_acl_info *entry;
101
102 if (mutex_lock_interruptible(&tomoyo_policy_lock))
103 return error;
104 list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900105 if (!tomoyo_same_acl_head(entry, new_entry) ||
106 !check_duplicate(entry, new_entry))
Tetsuo Handa237ab452010-06-12 20:46:22 +0900107 continue;
108 if (merge_duplicate)
109 entry->is_deleted = merge_duplicate(entry, new_entry,
110 is_delete);
111 else
112 entry->is_deleted = is_delete;
113 error = 0;
114 break;
115 }
116 if (error && !is_delete) {
117 entry = tomoyo_commit_ok(new_entry, size);
118 if (entry) {
119 list_add_tail_rcu(&entry->list, &domain->acl_info_list);
120 error = 0;
121 }
122 }
123 mutex_unlock(&tomoyo_policy_lock);
124 return error;
125}
126
Tetsuo Handa99a85252010-06-16 16:22:51 +0900127void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900128 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900129 const struct tomoyo_acl_info *))
130{
131 const struct tomoyo_domain_info *domain = r->domain;
132 struct tomoyo_acl_info *ptr;
133
134 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
135 if (ptr->is_deleted || ptr->type != r->param_type)
136 continue;
137 if (check_entry(r, ptr)) {
138 r->granted = true;
139 return;
140 }
141 }
142 r->granted = false;
143}
144
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900145/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900146LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900147
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900148struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
149struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
150
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900151/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900152 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900153 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900154 * @domainname: Domainname to check.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900155 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900156 * Returns the last word of @domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900157 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900158static const char *tomoyo_last_word(const char *name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900159{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900160 const char *cp = strrchr(name, ' ');
161 if (cp)
162 return cp + 1;
163 return name;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900164}
165
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900166static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
167 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900168{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900169 const struct tomoyo_transition_control *p1 = container_of(a,
170 typeof(*p1),
171 head);
172 const struct tomoyo_transition_control *p2 = container_of(b,
173 typeof(*p2),
174 head);
175 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900176 && p1->domainname == p2->domainname
177 && p1->program == p2->program;
178}
179
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900180/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900181 * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900182 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900183 * @domainname: The name of domain. Maybe NULL.
184 * @program: The name of program. Maybe NULL.
185 * @type: Type of transition.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900186 * @is_delete: True if it is a delete request.
187 *
188 * Returns 0 on success, negative value otherwise.
189 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900190static int tomoyo_update_transition_control_entry(const char *domainname,
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900191 const char *program,
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900192 const u8 type,
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900193 const bool is_delete)
194{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900195 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900196 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900197 if (program) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900198 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900199 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900200 e.program = tomoyo_get_name(program);
201 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900202 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900203 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900204 if (domainname) {
205 if (!tomoyo_correct_domain(domainname)) {
206 if (!tomoyo_correct_path(domainname))
207 goto out;
208 e.is_last_name = true;
209 }
210 e.domainname = tomoyo_get_name(domainname);
211 if (!e.domainname)
212 goto out;
213 }
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900214 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900215 &tomoyo_policy_list
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900216 [TOMOYO_ID_TRANSITION_CONTROL],
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900217 tomoyo_same_transition_control);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900218 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900219 tomoyo_put_name(e.domainname);
220 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900221 return error;
222}
223
224/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900225 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900226 *
227 * @data: String to parse.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900228 * @is_delete: True if it is a delete request.
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900229 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900230 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900231 * Returns 0 on success, negative value otherwise.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900232 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900233int tomoyo_write_transition_control(char *data, const bool is_delete,
234 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900235{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900236 char *domainname = strstr(data, " from ");
237 if (domainname) {
238 *domainname = '\0';
239 domainname += 6;
240 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
241 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
242 domainname = data;
243 data = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900244 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900245 return tomoyo_update_transition_control_entry(domainname, data, type,
246 is_delete);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900247}
248
249/**
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900250 * tomoyo_transition_type - Get domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900251 *
252 * @domainname: The name of domain.
253 * @program: The name of program.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900254 *
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900255 * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
256 * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
257 * @program suppresses domain transition, others otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900258 *
259 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900260 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900261static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
262 const struct tomoyo_path_info *program)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900263{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900264 const struct tomoyo_transition_control *ptr;
265 const char *last_name = tomoyo_last_word(domainname->name);
266 u8 type;
267 for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
268 next:
269 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
270 [TOMOYO_ID_TRANSITION_CONTROL],
271 head.list) {
272 if (ptr->head.is_deleted || ptr->type != type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900273 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900274 if (ptr->domainname) {
275 if (!ptr->is_last_name) {
276 if (ptr->domainname != domainname)
277 continue;
278 } else {
279 /*
280 * Use direct strcmp() since this is
281 * unlikely used.
282 */
283 if (strcmp(ptr->domainname->name,
284 last_name))
285 continue;
286 }
287 }
288 if (ptr->program &&
289 tomoyo_pathcmp(ptr->program, program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900290 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900291 if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
292 /*
293 * Do not check for initialize_domain if
294 * no_initialize_domain matched.
295 */
296 type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
297 goto next;
298 }
299 goto done;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900300 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900301 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900302 done:
303 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900304}
305
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900306static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
307 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900308{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900309 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1), head);
310 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900311 return p1->original_name == p2->original_name &&
312 p1->aggregated_name == p2->aggregated_name;
313}
314
Tetsuo Handa10843072010-06-03 20:38:03 +0900315/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900316 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900317 *
318 * @original_name: The original program's name.
319 * @aggregated_name: The program name to use.
320 * @is_delete: True if it is a delete request.
321 *
322 * Returns 0 on success, negative value otherwise.
323 *
324 * Caller holds tomoyo_read_lock().
325 */
326static int tomoyo_update_aggregator_entry(const char *original_name,
327 const char *aggregated_name,
328 const bool is_delete)
329{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900330 struct tomoyo_aggregator e = { };
Tetsuo Handa10843072010-06-03 20:38:03 +0900331 int error = is_delete ? -ENOENT : -ENOMEM;
332
Tetsuo Handa75093152010-06-16 16:23:55 +0900333 if (!tomoyo_correct_path(original_name) ||
334 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900335 return -EINVAL;
336 e.original_name = tomoyo_get_name(original_name);
337 e.aggregated_name = tomoyo_get_name(aggregated_name);
338 if (!e.original_name || !e.aggregated_name ||
339 e.aggregated_name->is_patterned) /* No patterns allowed. */
340 goto out;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900341 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900342 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900343 tomoyo_same_aggregator);
Tetsuo Handa10843072010-06-03 20:38:03 +0900344 out:
345 tomoyo_put_name(e.original_name);
346 tomoyo_put_name(e.aggregated_name);
347 return error;
348}
349
350/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900351 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900352 *
353 * @data: String to parse.
354 * @is_delete: True if it is a delete request.
355 *
356 * Returns 0 on success, negative value otherwise.
357 *
358 * Caller holds tomoyo_read_lock().
359 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900360int tomoyo_write_aggregator(char *data, const bool is_delete)
Tetsuo Handa10843072010-06-03 20:38:03 +0900361{
362 char *cp = strchr(data, ' ');
363
364 if (!cp)
365 return -EINVAL;
366 *cp++ = '\0';
367 return tomoyo_update_aggregator_entry(data, cp, is_delete);
368}
369
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900370/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900371 * tomoyo_assign_domain - Create a domain.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900372 *
373 * @domainname: The name of domain.
374 * @profile: Profile number to assign if the domain was newly created.
375 *
376 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900377 *
378 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900379 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900380struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
381 const u8 profile)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900382{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900383 struct tomoyo_domain_info *entry;
Tetsuo Handa29282382010-05-06 00:18:15 +0900384 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900385 const struct tomoyo_path_info *saved_domainname;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900386 bool found = false;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900387
Tetsuo Handa75093152010-06-16 16:23:55 +0900388 if (!tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900389 return NULL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900390 saved_domainname = tomoyo_get_name(domainname);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900391 if (!saved_domainname)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900392 return NULL;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900393 entry = kzalloc(sizeof(*entry), GFP_NOFS);
Tetsuo Handa29282382010-05-06 00:18:15 +0900394 if (mutex_lock_interruptible(&tomoyo_policy_lock))
395 goto out;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900396 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
397 if (domain->is_deleted ||
398 tomoyo_pathcmp(saved_domainname, domain->domainname))
399 continue;
400 found = true;
401 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900402 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900403 if (!found && tomoyo_memory_ok(entry)) {
404 INIT_LIST_HEAD(&entry->acl_info_list);
405 entry->domainname = saved_domainname;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900406 saved_domainname = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900407 entry->profile = profile;
408 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
409 domain = entry;
410 entry = NULL;
411 found = true;
412 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900413 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900414 out:
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900415 tomoyo_put_name(saved_domainname);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900416 kfree(entry);
417 return found ? domain : NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900418}
419
420/**
421 * tomoyo_find_next_domain - Find a domain.
422 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900423 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900424 *
425 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900426 *
427 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900428 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900429int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900430{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900431 struct tomoyo_request_info r;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900432 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900433 struct tomoyo_domain_info *old_domain = tomoyo_domain();
434 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900435 const char *original_name = bprm->filename;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900436 u8 mode;
437 bool is_enforce;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900438 int retval = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900439 bool need_kfree = false;
440 struct tomoyo_path_info rn = { }; /* real name */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900441
Tetsuo Handa57c25902010-06-03 20:38:44 +0900442 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
443 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900444 if (!tmp)
445 goto out;
446
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900447 retry:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900448 if (need_kfree) {
449 kfree(rn.name);
450 need_kfree = false;
451 }
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900452 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900453 retval = -ENOENT;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900454 rn.name = tomoyo_realpath_nofollow(original_name);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900455 if (!rn.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900456 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900457 tomoyo_fill_path_info(&rn);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900458 need_kfree = true;
459
Tetsuo Handa10843072010-06-03 20:38:03 +0900460 /* Check 'aggregator' directive. */
461 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900462 struct tomoyo_aggregator *ptr;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900463 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
464 [TOMOYO_ID_AGGREGATOR], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900465 if (ptr->head.is_deleted ||
Tetsuo Handa10843072010-06-03 20:38:03 +0900466 !tomoyo_path_matches_pattern(&rn,
467 ptr->original_name))
468 continue;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900469 kfree(rn.name);
Tetsuo Handa10843072010-06-03 20:38:03 +0900470 need_kfree = false;
471 /* This is OK because it is read only. */
472 rn = *ptr->aggregated_name;
473 break;
474 }
475 }
476
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900477 /* Check execute permission. */
Tetsuo Handa05336de2010-06-16 16:20:24 +0900478 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900479 if (retval == TOMOYO_RETRY_REQUEST)
480 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900481 if (retval < 0)
482 goto out;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900483 /*
484 * To be able to specify domainnames with wildcards, use the
485 * pathname specified in the policy (which may contain
486 * wildcard) rather than the pathname passed to execve()
487 * (which never contains wildcard).
488 */
489 if (r.param.path.matched_path) {
490 if (need_kfree)
491 kfree(rn.name);
492 need_kfree = false;
493 /* This is OK because it is read only. */
494 rn = *r.param.path.matched_path;
495 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900496
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900497 /* Calculate domain to transit to. */
498 switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
499 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900500 /* Transit to the child of tomoyo_kernel_domain domain. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900501 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
502 "%s", rn.name);
503 break;
504 case TOMOYO_TRANSITION_CONTROL_KEEP:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900505 /* Keep current domain. */
506 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900507 break;
508 default:
509 if (old_domain == &tomoyo_kernel_domain &&
510 !tomoyo_policy_loaded) {
511 /*
512 * Needn't to transit from kernel domain before
513 * starting /sbin/init. But transit from kernel domain
514 * if executing initializers because they might start
515 * before /sbin/init.
516 */
517 domain = old_domain;
518 } else {
519 /* Normal domain transition. */
520 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
521 old_domain->domainname->name, rn.name);
522 }
523 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900524 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900525 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900526 goto done;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900527 domain = tomoyo_find_domain(tmp);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900528 if (!domain)
529 domain = tomoyo_assign_domain(tmp, old_domain->profile);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900530 done:
531 if (domain)
532 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900533 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900534 if (is_enforce)
535 retval = -EPERM;
536 else
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900537 old_domain->transition_failed = true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900538 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900539 if (!domain)
540 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900541 /* Update reference count on "struct tomoyo_domain_info". */
542 atomic_inc(&domain->users);
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900543 bprm->cred->security = domain;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900544 if (need_kfree)
545 kfree(rn.name);
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900546 kfree(tmp);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900547 return retval;
548}