]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/tomoyo/domain.c
TOMOYO: Use struct for passing ACL line.
[mirror_ubuntu-bionic-kernel.git] / security / tomoyo / domain.c
CommitLineData
26a2a1c9
KT
1/*
2 * security/tomoyo/domain.c
3 *
c3ef1500 4 * Domain transition functions for TOMOYO.
26a2a1c9 5 *
c3ef1500 6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
26a2a1c9
KT
7 */
8
9#include "common.h"
26a2a1c9 10#include <linux/binfmts.h>
5a0e3ad6 11#include <linux/slab.h>
26a2a1c9
KT
12
13/* Variables definitions.*/
14
15/* The initial domain. */
16struct tomoyo_domain_info tomoyo_kernel_domain;
17
36f5e1ff
TH
18/**
19 * tomoyo_update_policy - Update an entry for exception policy.
20 *
21 * @new_entry: Pointer to "struct tomoyo_acl_info".
22 * @size: Size of @new_entry in bytes.
a238cf5b 23 * @param: Pointer to "struct tomoyo_acl_param".
36f5e1ff
TH
24 * @check_duplicate: Callback function to find duplicated entry.
25 *
26 * Returns 0 on success, negative value otherwise.
27 *
28 * Caller holds tomoyo_read_lock().
29 */
30int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
a238cf5b 31 struct tomoyo_acl_param *param,
36f5e1ff
TH
32 bool (*check_duplicate) (const struct tomoyo_acl_head
33 *,
34 const struct tomoyo_acl_head
35 *))
36{
a238cf5b 37 int error = param->is_delete ? -ENOENT : -ENOMEM;
36f5e1ff 38 struct tomoyo_acl_head *entry;
a238cf5b 39 struct list_head *list = param->list;
36f5e1ff
TH
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;
a238cf5b 46 entry->is_deleted = param->is_delete;
36f5e1ff
TH
47 error = 0;
48 break;
49 }
a238cf5b 50 if (error && !param->is_delete) {
36f5e1ff
TH
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
0df7e8b8
TH
61/**
62 * 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
237ab459
TH
75/**
76 * 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.
a238cf5b 80 * @param: Pointer to "struct tomoyo_acl_param".
237ab459
TH
81 * @check_duplicate: Callback function to find duplicated entry.
82 * @merge_duplicate: Callback function to merge duplicated entry.
83 *
84 * Returns 0 on success, negative value otherwise.
85 *
86 * Caller holds tomoyo_read_lock().
87 */
88int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
a238cf5b 89 struct tomoyo_acl_param *param,
237ab459
TH
90 bool (*check_duplicate) (const struct tomoyo_acl_info
91 *,
92 const struct tomoyo_acl_info
93 *),
94 bool (*merge_duplicate) (struct tomoyo_acl_info *,
95 struct tomoyo_acl_info *,
96 const bool))
97{
a238cf5b 98 const bool is_delete = param->is_delete;
237ab459
TH
99 int error = is_delete ? -ENOENT : -ENOMEM;
100 struct tomoyo_acl_info *entry;
a238cf5b 101 struct list_head * const list = param->list;
237ab459
TH
102
103 if (mutex_lock_interruptible(&tomoyo_policy_lock))
104 return error;
a238cf5b 105 list_for_each_entry_rcu(entry, list, list) {
0df7e8b8
TH
106 if (!tomoyo_same_acl_head(entry, new_entry) ||
107 !check_duplicate(entry, new_entry))
237ab459
TH
108 continue;
109 if (merge_duplicate)
110 entry->is_deleted = merge_duplicate(entry, new_entry,
111 is_delete);
112 else
113 entry->is_deleted = is_delete;
114 error = 0;
115 break;
116 }
117 if (error && !is_delete) {
118 entry = tomoyo_commit_ok(new_entry, size);
119 if (entry) {
a238cf5b 120 list_add_tail_rcu(&entry->list, list);
237ab459
TH
121 error = 0;
122 }
123 }
124 mutex_unlock(&tomoyo_policy_lock);
125 return error;
126}
127
99a85259 128void tomoyo_check_acl(struct tomoyo_request_info *r,
484ca79c 129 bool (*check_entry) (struct tomoyo_request_info *,
99a85259
TH
130 const struct tomoyo_acl_info *))
131{
132 const struct tomoyo_domain_info *domain = r->domain;
133 struct tomoyo_acl_info *ptr;
134
135 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
136 if (ptr->is_deleted || ptr->type != r->param_type)
137 continue;
138 if (check_entry(r, ptr)) {
139 r->granted = true;
140 return;
141 }
142 }
143 r->granted = false;
144}
145
a230f9e7 146/* The list for "struct tomoyo_domain_info". */
26a2a1c9 147LIST_HEAD(tomoyo_domain_list);
26a2a1c9 148
a230f9e7
TH
149struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
150struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
151
26a2a1c9 152/**
e2bf6907 153 * tomoyo_last_word - Get last component of a domainname.
26a2a1c9 154 *
e2bf6907 155 * @domainname: Domainname to check.
26a2a1c9 156 *
e2bf6907 157 * Returns the last word of @domainname.
26a2a1c9 158 */
e2bf6907 159static const char *tomoyo_last_word(const char *name)
26a2a1c9 160{
e2bf6907
TH
161 const char *cp = strrchr(name, ' ');
162 if (cp)
163 return cp + 1;
164 return name;
26a2a1c9
KT
165}
166
a238cf5b
TH
167/**
168 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
169 *
170 * @a: Pointer to "struct tomoyo_acl_head".
171 * @b: Pointer to "struct tomoyo_acl_head".
172 *
173 * Returns true if @a == @b, false otherwise.
174 */
e2bf6907
TH
175static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
176 const struct tomoyo_acl_head *b)
36f5e1ff 177{
5448ec4f
TH
178 const struct tomoyo_transition_control *p1 = container_of(a,
179 typeof(*p1),
180 head);
181 const struct tomoyo_transition_control *p2 = container_of(b,
182 typeof(*p2),
183 head);
184 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
36f5e1ff
TH
185 && p1->domainname == p2->domainname
186 && p1->program == p2->program;
187}
188
26a2a1c9 189/**
a238cf5b 190 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
26a2a1c9 191 *
a238cf5b
TH
192 * @param: Pointer to "struct tomoyo_acl_param".
193 * @type: Type of this entry.
26a2a1c9
KT
194 *
195 * Returns 0 on success, negative value otherwise.
196 */
a238cf5b
TH
197int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
198 const u8 type)
26a2a1c9 199{
5448ec4f 200 struct tomoyo_transition_control e = { .type = type };
a238cf5b
TH
201 int error = param->is_delete ? -ENOENT : -ENOMEM;
202 char *program = param->data;
203 char *domainname = strstr(program, " from ");
204 if (domainname) {
205 *domainname = '\0';
206 domainname += 6;
207 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
208 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
209 domainname = program;
210 program = NULL;
211 }
5448ec4f
TH
212 if (program) {
213 if (!tomoyo_correct_path(program))
214 return -EINVAL;
215 e.program = tomoyo_get_name(program);
216 if (!e.program)
217 goto out;
218 }
26a2a1c9 219 if (domainname) {
5448ec4f
TH
220 if (!tomoyo_correct_domain(domainname)) {
221 if (!tomoyo_correct_path(domainname))
222 goto out;
9e4b50e9 223 e.is_last_name = true;
5448ec4f 224 }
9e4b50e9
TH
225 e.domainname = tomoyo_get_name(domainname);
226 if (!e.domainname)
ca0b7df3 227 goto out;
26a2a1c9 228 }
a238cf5b
TH
229 param->list = &tomoyo_policy_list[TOMOYO_ID_TRANSITION_CONTROL];
230 error = tomoyo_update_policy(&e.head, sizeof(e), param,
e2bf6907 231 tomoyo_same_transition_control);
a238cf5b 232out:
9e4b50e9
TH
233 tomoyo_put_name(e.domainname);
234 tomoyo_put_name(e.program);
26a2a1c9
KT
235 return error;
236}
237
26a2a1c9 238/**
5448ec4f 239 * tomoyo_transition_type - Get domain transition type.
26a2a1c9
KT
240 *
241 * @domainname: The name of domain.
242 * @program: The name of program.
26a2a1c9 243 *
5448ec4f
TH
244 * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
245 * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
246 * @program suppresses domain transition, others otherwise.
fdb8ebb7
TH
247 *
248 * Caller holds tomoyo_read_lock().
26a2a1c9 249 */
5448ec4f
TH
250static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
251 const struct tomoyo_path_info *program)
26a2a1c9 252{
5448ec4f
TH
253 const struct tomoyo_transition_control *ptr;
254 const char *last_name = tomoyo_last_word(domainname->name);
255 u8 type;
256 for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
257 next:
258 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
259 [TOMOYO_ID_TRANSITION_CONTROL],
260 head.list) {
261 if (ptr->head.is_deleted || ptr->type != type)
26a2a1c9 262 continue;
5448ec4f
TH
263 if (ptr->domainname) {
264 if (!ptr->is_last_name) {
265 if (ptr->domainname != domainname)
266 continue;
267 } else {
268 /*
269 * Use direct strcmp() since this is
270 * unlikely used.
271 */
272 if (strcmp(ptr->domainname->name,
273 last_name))
274 continue;
275 }
276 }
277 if (ptr->program &&
278 tomoyo_pathcmp(ptr->program, program))
26a2a1c9 279 continue;
5448ec4f
TH
280 if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
281 /*
282 * Do not check for initialize_domain if
283 * no_initialize_domain matched.
284 */
285 type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
286 goto next;
287 }
288 goto done;
26a2a1c9 289 }
26a2a1c9 290 }
5448ec4f
TH
291 done:
292 return type;
26a2a1c9
KT
293}
294
a238cf5b
TH
295/**
296 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
297 *
298 * @a: Pointer to "struct tomoyo_acl_head".
299 * @b: Pointer to "struct tomoyo_acl_head".
300 *
301 * Returns true if @a == @b, false otherwise.
302 */
e2bf6907
TH
303static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
304 const struct tomoyo_acl_head *b)
36f5e1ff 305{
a238cf5b
TH
306 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
307 head);
308 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
309 head);
36f5e1ff
TH
310 return p1->original_name == p2->original_name &&
311 p1->aggregated_name == p2->aggregated_name;
312}
313
1084307c 314/**
a238cf5b 315 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
1084307c 316 *
a238cf5b 317 * @param: Pointer to "struct tomoyo_acl_param".
1084307c
TH
318 *
319 * Returns 0 on success, negative value otherwise.
320 *
321 * Caller holds tomoyo_read_lock().
322 */
a238cf5b 323int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
1084307c 324{
e2bf6907 325 struct tomoyo_aggregator e = { };
a238cf5b
TH
326 int error = param->is_delete ? -ENOENT : -ENOMEM;
327 const char *original_name = tomoyo_read_token(param);
328 const char *aggregated_name = tomoyo_read_token(param);
329 if (!tomoyo_correct_word(original_name) ||
75093152 330 !tomoyo_correct_path(aggregated_name))
1084307c
TH
331 return -EINVAL;
332 e.original_name = tomoyo_get_name(original_name);
333 e.aggregated_name = tomoyo_get_name(aggregated_name);
334 if (!e.original_name || !e.aggregated_name ||
335 e.aggregated_name->is_patterned) /* No patterns allowed. */
336 goto out;
a238cf5b
TH
337 param->list = &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR];
338 error = tomoyo_update_policy(&e.head, sizeof(e), param,
e2bf6907 339 tomoyo_same_aggregator);
a238cf5b 340out:
1084307c
TH
341 tomoyo_put_name(e.original_name);
342 tomoyo_put_name(e.aggregated_name);
343 return error;
344}
345
26a2a1c9 346/**
e2bf6907 347 * tomoyo_assign_domain - Create a domain.
26a2a1c9
KT
348 *
349 * @domainname: The name of domain.
350 * @profile: Profile number to assign if the domain was newly created.
351 *
352 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
fdb8ebb7
TH
353 *
354 * Caller holds tomoyo_read_lock().
26a2a1c9 355 */
e2bf6907
TH
356struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
357 const u8 profile)
26a2a1c9 358{
ca0b7df3 359 struct tomoyo_domain_info *entry;
29282381 360 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 361 const struct tomoyo_path_info *saved_domainname;
ca0b7df3 362 bool found = false;
26a2a1c9 363
75093152 364 if (!tomoyo_correct_domain(domainname))
ca0b7df3 365 return NULL;
bf24fb01 366 saved_domainname = tomoyo_get_name(domainname);
26a2a1c9 367 if (!saved_domainname)
ca0b7df3 368 return NULL;
4e5d6f7e 369 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29282381
TH
370 if (mutex_lock_interruptible(&tomoyo_policy_lock))
371 goto out;
ca0b7df3
TH
372 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
373 if (domain->is_deleted ||
374 tomoyo_pathcmp(saved_domainname, domain->domainname))
375 continue;
376 found = true;
377 break;
378 }
379 if (!found && tomoyo_memory_ok(entry)) {
380 INIT_LIST_HEAD(&entry->acl_info_list);
381 entry->domainname = saved_domainname;
bf24fb01 382 saved_domainname = NULL;
ca0b7df3
TH
383 entry->profile = profile;
384 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
385 domain = entry;
386 entry = NULL;
387 found = true;
26a2a1c9 388 }
f737d95d 389 mutex_unlock(&tomoyo_policy_lock);
29282381 390 out:
bf24fb01 391 tomoyo_put_name(saved_domainname);
ca0b7df3
TH
392 kfree(entry);
393 return found ? domain : NULL;
26a2a1c9
KT
394}
395
396/**
397 * tomoyo_find_next_domain - Find a domain.
398 *
56f8c9bc 399 * @bprm: Pointer to "struct linux_binprm".
26a2a1c9
KT
400 *
401 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
402 *
403 * Caller holds tomoyo_read_lock().
26a2a1c9 404 */
56f8c9bc 405int tomoyo_find_next_domain(struct linux_binprm *bprm)
26a2a1c9 406{
17fcfbd9 407 struct tomoyo_request_info r;
c8c57e84 408 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
26a2a1c9
KT
409 struct tomoyo_domain_info *old_domain = tomoyo_domain();
410 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 411 const char *original_name = bprm->filename;
57c2590f
TH
412 u8 mode;
413 bool is_enforce;
26a2a1c9 414 int retval = -ENOMEM;
c8c57e84
TH
415 bool need_kfree = false;
416 struct tomoyo_path_info rn = { }; /* real name */
26a2a1c9 417
57c2590f
TH
418 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
419 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
26a2a1c9
KT
420 if (!tmp)
421 goto out;
422
17fcfbd9 423 retry:
c8c57e84
TH
424 if (need_kfree) {
425 kfree(rn.name);
426 need_kfree = false;
427 }
0617c7ff 428 /* Get symlink's pathname of program. */
26a2a1c9 429 retval = -ENOENT;
0617c7ff 430 rn.name = tomoyo_realpath_nofollow(original_name);
c8c57e84 431 if (!rn.name)
26a2a1c9 432 goto out;
c8c57e84
TH
433 tomoyo_fill_path_info(&rn);
434 need_kfree = true;
435
1084307c
TH
436 /* Check 'aggregator' directive. */
437 {
e2bf6907 438 struct tomoyo_aggregator *ptr;
a230f9e7
TH
439 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
440 [TOMOYO_ID_AGGREGATOR], head.list) {
82e0f001 441 if (ptr->head.is_deleted ||
1084307c
TH
442 !tomoyo_path_matches_pattern(&rn,
443 ptr->original_name))
444 continue;
0617c7ff 445 kfree(rn.name);
1084307c
TH
446 need_kfree = false;
447 /* This is OK because it is read only. */
448 rn = *ptr->aggregated_name;
449 break;
450 }
451 }
452
26a2a1c9 453 /* Check execute permission. */
05336dee 454 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
17fcfbd9
TH
455 if (retval == TOMOYO_RETRY_REQUEST)
456 goto retry;
26a2a1c9
KT
457 if (retval < 0)
458 goto out;
484ca79c
TH
459 /*
460 * To be able to specify domainnames with wildcards, use the
461 * pathname specified in the policy (which may contain
462 * wildcard) rather than the pathname passed to execve()
463 * (which never contains wildcard).
464 */
465 if (r.param.path.matched_path) {
466 if (need_kfree)
467 kfree(rn.name);
468 need_kfree = false;
469 /* This is OK because it is read only. */
470 rn = *r.param.path.matched_path;
471 }
26a2a1c9 472
5448ec4f
TH
473 /* Calculate domain to transit to. */
474 switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
475 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
26a2a1c9 476 /* Transit to the child of tomoyo_kernel_domain domain. */
5448ec4f
TH
477 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
478 "%s", rn.name);
479 break;
480 case TOMOYO_TRANSITION_CONTROL_KEEP:
26a2a1c9
KT
481 /* Keep current domain. */
482 domain = old_domain;
5448ec4f
TH
483 break;
484 default:
485 if (old_domain == &tomoyo_kernel_domain &&
486 !tomoyo_policy_loaded) {
487 /*
488 * Needn't to transit from kernel domain before
489 * starting /sbin/init. But transit from kernel domain
490 * if executing initializers because they might start
491 * before /sbin/init.
492 */
493 domain = old_domain;
494 } else {
495 /* Normal domain transition. */
496 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
497 old_domain->domainname->name, rn.name);
498 }
499 break;
26a2a1c9 500 }
c8c57e84 501 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
26a2a1c9 502 goto done;
c8c57e84 503 domain = tomoyo_find_domain(tmp);
7c75964f
TH
504 if (!domain)
505 domain = tomoyo_assign_domain(tmp, old_domain->profile);
26a2a1c9
KT
506 done:
507 if (domain)
508 goto out;
c8c57e84 509 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
26a2a1c9
KT
510 if (is_enforce)
511 retval = -EPERM;
512 else
ea13ddba 513 old_domain->transition_failed = true;
26a2a1c9 514 out:
56f8c9bc
TH
515 if (!domain)
516 domain = old_domain;
ec8e6a4e
TH
517 /* Update reference count on "struct tomoyo_domain_info". */
518 atomic_inc(&domain->users);
56f8c9bc 519 bprm->cred->security = domain;
c8c57e84
TH
520 if (need_kfree)
521 kfree(rn.name);
8e2d39a1 522 kfree(tmp);
26a2a1c9
KT
523 return retval;
524}