blob: cd0f92d88bb43a35c99230c52effb311da0d5213 [file] [log] [blame]
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09001/*
2 * security/tomoyo/domain.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09005 */
6
7#include "common.h"
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09008#include <linux/binfmts.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090010
11/* Variables definitions.*/
12
13/* The initial domain. */
14struct tomoyo_domain_info tomoyo_kernel_domain;
15
Tetsuo Handa237ab452010-06-12 20:46:22 +090016/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090017 * tomoyo_update_policy - Update an entry for exception policy.
18 *
19 * @new_entry: Pointer to "struct tomoyo_acl_info".
20 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090021 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090022 * @check_duplicate: Callback function to find duplicated entry.
23 *
24 * Returns 0 on success, negative value otherwise.
25 *
26 * Caller holds tomoyo_read_lock().
27 */
28int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090029 struct tomoyo_acl_param *param,
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090030 bool (*check_duplicate) (const struct tomoyo_acl_head
31 *,
32 const struct tomoyo_acl_head
33 *))
34{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090035 int error = param->is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090036 struct tomoyo_acl_head *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090037 struct list_head *list = param->list;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090038
39 if (mutex_lock_interruptible(&tomoyo_policy_lock))
40 return -ENOMEM;
41 list_for_each_entry_rcu(entry, list, list) {
42 if (!check_duplicate(entry, new_entry))
43 continue;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090044 entry->is_deleted = param->is_delete;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090045 error = 0;
46 break;
47 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090048 if (error && !param->is_delete) {
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090049 entry = tomoyo_commit_ok(new_entry, size);
50 if (entry) {
51 list_add_tail_rcu(&entry->list, list);
52 error = 0;
53 }
54 }
55 mutex_unlock(&tomoyo_policy_lock);
56 return error;
57}
58
59/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090060 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
61 *
62 * @a: Pointer to "struct tomoyo_acl_info".
63 * @b: Pointer to "struct tomoyo_acl_info".
64 *
65 * Returns true if @a == @b, false otherwise.
66 */
67static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
68 const struct tomoyo_acl_info *b)
69{
Tetsuo Handa2066a362011-07-08 13:21:37 +090070 return a->type == b->type && a->cond == b->cond;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090071}
72
73/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090074 * tomoyo_update_domain - Update an entry for domain policy.
75 *
76 * @new_entry: Pointer to "struct tomoyo_acl_info".
77 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090078 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa237ab452010-06-12 20:46:22 +090079 * @check_duplicate: Callback function to find duplicated entry.
80 * @merge_duplicate: Callback function to merge duplicated entry.
81 *
82 * Returns 0 on success, negative value otherwise.
83 *
84 * Caller holds tomoyo_read_lock().
85 */
86int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090087 struct tomoyo_acl_param *param,
Tetsuo Handa237ab452010-06-12 20:46:22 +090088 bool (*check_duplicate) (const struct tomoyo_acl_info
89 *,
90 const struct tomoyo_acl_info
91 *),
92 bool (*merge_duplicate) (struct tomoyo_acl_info *,
93 struct tomoyo_acl_info *,
94 const bool))
95{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090096 const bool is_delete = param->is_delete;
Tetsuo Handa237ab452010-06-12 20:46:22 +090097 int error = is_delete ? -ENOENT : -ENOMEM;
98 struct tomoyo_acl_info *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090099 struct list_head * const list = param->list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900100
Tetsuo Handa2066a362011-07-08 13:21:37 +0900101 if (param->data[0]) {
102 new_entry->cond = tomoyo_get_condition(param);
103 if (!new_entry->cond)
104 return -EINVAL;
105 }
Tetsuo Handa237ab452010-06-12 20:46:22 +0900106 if (mutex_lock_interruptible(&tomoyo_policy_lock))
Tetsuo Handa2066a362011-07-08 13:21:37 +0900107 goto out;
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);
Tetsuo Handa2066a362011-07-08 13:21:37 +0900128out:
129 tomoyo_put_condition(new_entry->cond);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900130 return error;
131}
132
Tetsuo Handa32997142011-06-26 23:19:28 +0900133/**
134 * tomoyo_check_acl - Do permission check.
135 *
136 * @r: Pointer to "struct tomoyo_request_info".
137 * @check_entry: Callback function to check type specific parameters.
138 *
139 * Returns 0 on success, negative value otherwise.
140 *
141 * Caller holds tomoyo_read_lock().
142 */
Tetsuo Handa99a85252010-06-16 16:22:51 +0900143void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900144 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900145 const struct tomoyo_acl_info *))
146{
147 const struct tomoyo_domain_info *domain = r->domain;
148 struct tomoyo_acl_info *ptr;
Tetsuo Handa32997142011-06-26 23:19:28 +0900149 bool retried = false;
150 const struct list_head *list = &domain->acl_info_list;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900151
Tetsuo Handa32997142011-06-26 23:19:28 +0900152retry:
153 list_for_each_entry_rcu(ptr, list, list) {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900154 if (ptr->is_deleted || ptr->type != r->param_type)
155 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900156 if (!check_entry(r, ptr))
157 continue;
158 if (!tomoyo_condition(r, ptr->cond))
159 continue;
160 r->granted = true;
161 return;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900162 }
Tetsuo Handa32997142011-06-26 23:19:28 +0900163 if (!retried) {
164 retried = true;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900165 list = &domain->ns->acl_group[domain->group];
Tetsuo Handa32997142011-06-26 23:19:28 +0900166 goto retry;
167 }
Tetsuo Handa99a85252010-06-16 16:22:51 +0900168 r->granted = false;
169}
170
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900171/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900172LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900173
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900174/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900175 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900176 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900177 * @name: Domainname to check.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900178 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900179 * Returns the last word of @domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900180 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900181static const char *tomoyo_last_word(const char *name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900182{
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900183 const char *cp = strrchr(name, ' ');
184 if (cp)
185 return cp + 1;
186 return name;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900187}
188
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900189/**
190 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
191 *
192 * @a: Pointer to "struct tomoyo_acl_head".
193 * @b: Pointer to "struct tomoyo_acl_head".
194 *
195 * Returns true if @a == @b, false otherwise.
196 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900197static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
198 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900199{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900200 const struct tomoyo_transition_control *p1 = container_of(a,
201 typeof(*p1),
202 head);
203 const struct tomoyo_transition_control *p2 = container_of(b,
204 typeof(*p2),
205 head);
206 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900207 && p1->domainname == p2->domainname
208 && p1->program == p2->program;
209}
210
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900211/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900212 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900213 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900214 * @param: Pointer to "struct tomoyo_acl_param".
215 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900216 *
217 * Returns 0 on success, negative value otherwise.
218 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900219int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
220 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900221{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900222 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900223 int error = param->is_delete ? -ENOENT : -ENOMEM;
224 char *program = param->data;
225 char *domainname = strstr(program, " from ");
226 if (domainname) {
227 *domainname = '\0';
228 domainname += 6;
229 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
230 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
231 domainname = program;
232 program = NULL;
233 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900234 if (program && strcmp(program, "any")) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900235 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900236 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900237 e.program = tomoyo_get_name(program);
238 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900239 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900240 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900241 if (domainname && strcmp(domainname, "any")) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900242 if (!tomoyo_correct_domain(domainname)) {
243 if (!tomoyo_correct_path(domainname))
244 goto out;
245 e.is_last_name = true;
246 }
247 e.domainname = tomoyo_get_name(domainname);
248 if (!e.domainname)
249 goto out;
250 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900251 param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900252 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900253 tomoyo_same_transition_control);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900254out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900255 tomoyo_put_name(e.domainname);
256 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900257 return error;
258}
259
260/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900261 * tomoyo_scan_transition - Try to find specific domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900262 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900263 * @list: Pointer to "struct list_head".
264 * @domainname: The name of current domain.
265 * @program: The name of requested program.
266 * @last_name: The last component of @domainname.
267 * @type: One of values in "enum tomoyo_transition_type".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900268 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900269 * Returns true if found one, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900270 *
271 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900272 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900273static inline bool tomoyo_scan_transition
274(const struct list_head *list, const struct tomoyo_path_info *domainname,
275 const struct tomoyo_path_info *program, const char *last_name,
276 const enum tomoyo_transition_type type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900277{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900278 const struct tomoyo_transition_control *ptr;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900279 list_for_each_entry_rcu(ptr, list, head.list) {
280 if (ptr->head.is_deleted || ptr->type != type)
281 continue;
282 if (ptr->domainname) {
283 if (!ptr->is_last_name) {
284 if (ptr->domainname != domainname)
285 continue;
286 } else {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900287 /*
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900288 * Use direct strcmp() since this is
289 * unlikely used.
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900290 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900291 if (strcmp(ptr->domainname->name, last_name))
292 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900293 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900294 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900295 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
296 continue;
297 return true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900298 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900299 return false;
300}
301
302/**
303 * tomoyo_transition_type - Get domain transition type.
304 *
305 * @ns: Pointer to "struct tomoyo_policy_namespace".
306 * @domainname: The name of current domain.
307 * @program: The name of requested program.
308 *
309 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
310 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
311 * executing @program reinitializes domain transition within that namespace,
312 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
313 * others otherwise.
314 *
315 * Caller holds tomoyo_read_lock().
316 */
317static enum tomoyo_transition_type tomoyo_transition_type
318(const struct tomoyo_policy_namespace *ns,
319 const struct tomoyo_path_info *domainname,
320 const struct tomoyo_path_info *program)
321{
322 const char *last_name = tomoyo_last_word(domainname->name);
323 enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
324 while (type < TOMOYO_MAX_TRANSITION_TYPE) {
325 const struct list_head * const list =
326 &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
327 if (!tomoyo_scan_transition(list, domainname, program,
328 last_name, type)) {
329 type++;
330 continue;
331 }
332 if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
333 type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
334 break;
335 /*
336 * Do not check for reset_domain if no_reset_domain matched.
337 * Do not check for initialize_domain if no_initialize_domain
338 * matched.
339 */
340 type++;
341 type++;
342 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900343 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900344}
345
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900346/**
347 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
348 *
349 * @a: Pointer to "struct tomoyo_acl_head".
350 * @b: Pointer to "struct tomoyo_acl_head".
351 *
352 * Returns true if @a == @b, false otherwise.
353 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900354static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
355 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900356{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900357 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
358 head);
359 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
360 head);
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900361 return p1->original_name == p2->original_name &&
362 p1->aggregated_name == p2->aggregated_name;
363}
364
Tetsuo Handa10843072010-06-03 20:38:03 +0900365/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900366 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900367 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900368 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa10843072010-06-03 20:38:03 +0900369 *
370 * Returns 0 on success, negative value otherwise.
371 *
372 * Caller holds tomoyo_read_lock().
373 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900374int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
Tetsuo Handa10843072010-06-03 20:38:03 +0900375{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900376 struct tomoyo_aggregator e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900377 int error = param->is_delete ? -ENOENT : -ENOMEM;
378 const char *original_name = tomoyo_read_token(param);
379 const char *aggregated_name = tomoyo_read_token(param);
380 if (!tomoyo_correct_word(original_name) ||
Tetsuo Handa75093152010-06-16 16:23:55 +0900381 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900382 return -EINVAL;
383 e.original_name = tomoyo_get_name(original_name);
384 e.aggregated_name = tomoyo_get_name(aggregated_name);
385 if (!e.original_name || !e.aggregated_name ||
386 e.aggregated_name->is_patterned) /* No patterns allowed. */
387 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900388 param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900389 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900390 tomoyo_same_aggregator);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900391out:
Tetsuo Handa10843072010-06-03 20:38:03 +0900392 tomoyo_put_name(e.original_name);
393 tomoyo_put_name(e.aggregated_name);
394 return error;
395}
396
397/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900398 * tomoyo_find_namespace - Find specified namespace.
399 *
400 * @name: Name of namespace to find.
401 * @len: Length of @name.
402 *
403 * Returns pointer to "struct tomoyo_policy_namespace" if found,
404 * NULL otherwise.
405 *
406 * Caller holds tomoyo_read_lock().
407 */
408static struct tomoyo_policy_namespace *tomoyo_find_namespace
409(const char *name, const unsigned int len)
410{
411 struct tomoyo_policy_namespace *ns;
412 list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
413 if (strncmp(name, ns->name, len) ||
414 (name[len] && name[len] != ' '))
415 continue;
416 return ns;
417 }
418 return NULL;
419}
420
421/**
422 * tomoyo_assign_namespace - Create a new namespace.
423 *
424 * @domainname: Name of namespace to create.
425 *
426 * Returns pointer to "struct tomoyo_policy_namespace" on success,
427 * NULL otherwise.
428 *
429 * Caller holds tomoyo_read_lock().
430 */
431struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
432{
433 struct tomoyo_policy_namespace *ptr;
434 struct tomoyo_policy_namespace *entry;
435 const char *cp = domainname;
436 unsigned int len = 0;
437 while (*cp && *cp++ != ' ')
438 len++;
439 ptr = tomoyo_find_namespace(domainname, len);
440 if (ptr)
441 return ptr;
442 if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
443 return NULL;
444 entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
445 if (!entry)
446 return NULL;
447 if (mutex_lock_interruptible(&tomoyo_policy_lock))
448 goto out;
449 ptr = tomoyo_find_namespace(domainname, len);
450 if (!ptr && tomoyo_memory_ok(entry)) {
451 char *name = (char *) (entry + 1);
452 ptr = entry;
453 memmove(name, domainname, len);
454 name[len] = '\0';
455 entry->name = name;
456 tomoyo_init_policy_namespace(entry);
457 entry = NULL;
458 }
459 mutex_unlock(&tomoyo_policy_lock);
460out:
461 kfree(entry);
462 return ptr;
463}
464
465/**
466 * tomoyo_namespace_jump - Check for namespace jump.
467 *
468 * @domainname: Name of domain.
469 *
470 * Returns true if namespace differs, false otherwise.
471 */
472static bool tomoyo_namespace_jump(const char *domainname)
473{
474 const char *namespace = tomoyo_current_namespace()->name;
475 const int len = strlen(namespace);
476 return strncmp(domainname, namespace, len) ||
477 (domainname[len] && domainname[len] != ' ');
478}
479
480/**
481 * tomoyo_assign_domain - Create a domain or a namespace.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900482 *
483 * @domainname: The name of domain.
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900484 * @transit: True if transit to domain found or created.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900485 *
486 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900487 *
488 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900489 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900490struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900491 const bool transit)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900492{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900493 struct tomoyo_domain_info e = { };
494 struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
495 bool created = false;
496 if (entry) {
497 if (transit) {
498 /*
499 * Since namespace is created at runtime, profiles may
500 * not be created by the moment the process transits to
501 * that domain. Do not perform domain transition if
502 * profile for that domain is not yet created.
503 */
504 if (!entry->ns->profile_ptr[entry->profile])
505 return NULL;
506 }
507 return entry;
508 }
509 /* Requested domain does not exist. */
510 /* Don't create requested domain if domainname is invalid. */
511 if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
512 !tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900513 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900514 /*
515 * Since definition of profiles and acl_groups may differ across
516 * namespaces, do not inherit "use_profile" and "use_group" settings
517 * by automatically creating requested domain upon domain transition.
518 */
519 if (transit && tomoyo_namespace_jump(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900520 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900521 e.ns = tomoyo_assign_namespace(domainname);
522 if (!e.ns)
523 return NULL;
524 /*
525 * "use_profile" and "use_group" settings for automatically created
526 * domains are inherited from current domain. These are 0 for manually
527 * created domains.
528 */
529 if (transit) {
530 const struct tomoyo_domain_info *domain = tomoyo_domain();
531 e.profile = domain->profile;
532 e.group = domain->group;
533 }
534 e.domainname = tomoyo_get_name(domainname);
535 if (!e.domainname)
536 return NULL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900537 if (mutex_lock_interruptible(&tomoyo_policy_lock))
538 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900539 entry = tomoyo_find_domain(domainname);
540 if (!entry) {
541 entry = tomoyo_commit_ok(&e, sizeof(e));
542 if (entry) {
543 INIT_LIST_HEAD(&entry->acl_info_list);
544 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
545 created = true;
546 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900547 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900548 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900549out:
550 tomoyo_put_name(e.domainname);
551 if (entry && transit) {
552 if (created) {
553 struct tomoyo_request_info r;
554 tomoyo_init_request_info(&r, entry,
555 TOMOYO_MAC_FILE_EXECUTE);
556 r.granted = false;
557 tomoyo_write_log(&r, "use_profile %u\n",
558 entry->profile);
559 tomoyo_write_log(&r, "use_group %u\n", entry->group);
560 }
561 }
562 return entry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900563}
564
565/**
566 * tomoyo_find_next_domain - Find a domain.
567 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900568 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900569 *
570 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900571 *
572 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900573 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900574int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900575{
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900576 struct tomoyo_domain_info *old_domain = tomoyo_domain();
577 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900578 const char *original_name = bprm->filename;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900579 int retval = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900580 bool need_kfree = false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900581 bool reject_on_transition_failure = false;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900582 struct tomoyo_path_info rn = { }; /* real name */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900583 struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
584 if (!ee)
585 return -ENOMEM;
586 ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
587 if (!ee->tmp) {
588 kfree(ee);
589 return -ENOMEM;
590 }
591 /* ee->dump->data is allocated by tomoyo_dump_page(). */
592 tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
593 ee->r.ee = ee;
594 ee->bprm = bprm;
595 ee->r.obj = &ee->obj;
596 ee->obj.path1 = bprm->file->f_path;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900597 retry:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900598 if (need_kfree) {
599 kfree(rn.name);
600 need_kfree = false;
601 }
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900602 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900603 retval = -ENOENT;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900604 rn.name = tomoyo_realpath_nofollow(original_name);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900605 if (!rn.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900606 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900607 tomoyo_fill_path_info(&rn);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900608 need_kfree = true;
609
Tetsuo Handa10843072010-06-03 20:38:03 +0900610 /* Check 'aggregator' directive. */
611 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900612 struct tomoyo_aggregator *ptr;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900613 struct list_head *list =
614 &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
615 /* Check 'aggregator' directive. */
616 list_for_each_entry_rcu(ptr, list, head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900617 if (ptr->head.is_deleted ||
Tetsuo Handa10843072010-06-03 20:38:03 +0900618 !tomoyo_path_matches_pattern(&rn,
619 ptr->original_name))
620 continue;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900621 kfree(rn.name);
Tetsuo Handa10843072010-06-03 20:38:03 +0900622 need_kfree = false;
623 /* This is OK because it is read only. */
624 rn = *ptr->aggregated_name;
625 break;
626 }
627 }
628
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900629 /* Check execute permission. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900630 retval = tomoyo_path_permission(&ee->r, TOMOYO_TYPE_EXECUTE, &rn);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900631 if (retval == TOMOYO_RETRY_REQUEST)
632 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900633 if (retval < 0)
634 goto out;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900635 /*
636 * To be able to specify domainnames with wildcards, use the
637 * pathname specified in the policy (which may contain
638 * wildcard) rather than the pathname passed to execve()
639 * (which never contains wildcard).
640 */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900641 if (ee->r.param.path.matched_path) {
Tetsuo Handa484ca792010-07-29 14:29:55 +0900642 if (need_kfree)
643 kfree(rn.name);
644 need_kfree = false;
645 /* This is OK because it is read only. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900646 rn = *ee->r.param.path.matched_path;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900647 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900648
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900649 /* Calculate domain to transit to. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900650 switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
651 &rn)) {
652 case TOMOYO_TRANSITION_CONTROL_RESET:
653 /* Transit to the root of specified namespace. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900654 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900655 /*
656 * Make do_execve() fail if domain transition across namespaces
657 * has failed.
658 */
659 reject_on_transition_failure = true;
660 break;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900661 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900662 /* Transit to the child of current namespace's root. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900663 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900664 old_domain->ns->name, rn.name);
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900665 break;
666 case TOMOYO_TRANSITION_CONTROL_KEEP:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900667 /* Keep current domain. */
668 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900669 break;
670 default:
671 if (old_domain == &tomoyo_kernel_domain &&
672 !tomoyo_policy_loaded) {
673 /*
674 * Needn't to transit from kernel domain before
675 * starting /sbin/init. But transit from kernel domain
676 * if executing initializers because they might start
677 * before /sbin/init.
678 */
679 domain = old_domain;
680 } else {
681 /* Normal domain transition. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900682 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900683 old_domain->domainname->name, rn.name);
684 }
685 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900686 }
Tetsuo Handa7c759642011-06-26 23:15:31 +0900687 if (!domain)
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900688 domain = tomoyo_assign_domain(ee->tmp, true);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900689 if (domain)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900690 retval = 0;
691 else if (reject_on_transition_failure) {
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900692 printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
693 ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900694 retval = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900695 } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900696 retval = -ENOMEM;
697 else {
698 retval = 0;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900699 if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
700 old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900701 ee->r.granted = false;
702 tomoyo_write_log(&ee->r, "%s", tomoyo_dif
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900703 [TOMOYO_DIF_TRANSITION_FAILED]);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900704 printk(KERN_WARNING
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900705 "ERROR: Domain '%s' not defined.\n", ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900706 }
707 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900708 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900709 if (!domain)
710 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900711 /* Update reference count on "struct tomoyo_domain_info". */
712 atomic_inc(&domain->users);
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900713 bprm->cred->security = domain;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900714 if (need_kfree)
715 kfree(rn.name);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900716 kfree(ee->tmp);
717 kfree(ee->dump.data);
718 kfree(ee);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900719 return retval;
720}
Tetsuo Handa5b636852011-07-08 13:24:54 +0900721
722/**
723 * tomoyo_dump_page - Dump a page to buffer.
724 *
725 * @bprm: Pointer to "struct linux_binprm".
726 * @pos: Location to dump.
727 * @dump: Poiner to "struct tomoyo_page_dump".
728 *
729 * Returns true on success, false otherwise.
730 */
731bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
732 struct tomoyo_page_dump *dump)
733{
734 struct page *page;
735 /* dump->data is released by tomoyo_finish_execve(). */
736 if (!dump->data) {
737 dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
738 if (!dump->data)
739 return false;
740 }
741 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
742#ifdef CONFIG_MMU
743 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
744 return false;
745#else
746 page = bprm->page[pos / PAGE_SIZE];
747#endif
748 if (page != dump->page) {
749 const unsigned int offset = pos % PAGE_SIZE;
750 /*
751 * Maybe kmap()/kunmap() should be used here.
752 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
753 * So do I.
754 */
755 char *kaddr = kmap_atomic(page, KM_USER0);
756 dump->page = page;
757 memcpy(dump->data + offset, kaddr + offset,
758 PAGE_SIZE - offset);
759 kunmap_atomic(kaddr, KM_USER0);
760 }
761 /* Same with put_arg_page(page) in fs/exec.c */
762#ifdef CONFIG_MMU
763 put_page(page);
764#endif
765 return true;
766}