| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* security/tomoyo/environ.c |
| 4 |
* |
| 5 |
* Copyright (C) 2005-2011 NTT DATA CORPORATION |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "common.h" |
| 9 |
|
| 10 |
/** |
| 11 |
* tomoyo_check_env_acl - Check permission for environment variable's name. |
| 12 |
* |
| 13 |
* @r: Pointer to "struct tomoyo_request_info". |
| 14 |
* @ptr: Pointer to "struct tomoyo_acl_info". |
| 15 |
* |
| 16 |
* Returns true if granted, false otherwise. |
| 17 |
*/ |
| 18 |
static bool tomoyo_check_env_acl(struct tomoyo_request_info *r, |
| 19 |
const struct tomoyo_acl_info *ptr) |
| 20 |
{ |
| 21 |
const struct tomoyo_env_acl *acl = |
| 22 |
container_of(ptr, typeof(*acl), head); |
| 23 |
|
| 24 |
return tomoyo_path_matches_pattern(r->param.environ.name, acl->env); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* tomoyo_audit_env_log - Audit environment variable name log. |
| 29 |
* |
| 30 |
* @r: Pointer to "struct tomoyo_request_info". |
| 31 |
* |
| 32 |
* Returns 0 on success, negative value otherwise. |
| 33 |
*/ |
| 34 |
static int tomoyo_audit_env_log(struct tomoyo_request_info *r) |
| 35 |
__must_hold_shared(&tomoyo_ss) |
| 36 |
{ |
| 37 |
return tomoyo_supervisor(r, "misc env %s\n", |
| 38 |
r->param.environ.name->name); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* tomoyo_env_perm - Check permission for environment variable's name. |
| 43 |
* |
| 44 |
* @r: Pointer to "struct tomoyo_request_info". |
| 45 |
* @env: The name of environment variable. |
| 46 |
* |
| 47 |
* Returns 0 on success, negative value otherwise. |
| 48 |
* |
| 49 |
* Caller holds tomoyo_read_lock(). |
| 50 |
*/ |
| 51 |
int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env) |
| 52 |
{ |
| 53 |
struct tomoyo_path_info environ; |
| 54 |
int error; |
| 55 |
|
| 56 |
if (!env || !*env) |
| 57 |
return 0; |
| 58 |
environ.name = env; |
| 59 |
tomoyo_fill_path_info(&environ); |
| 60 |
r->param_type = TOMOYO_TYPE_ENV_ACL; |
| 61 |
r->param.environ.name = &environ; |
| 62 |
do { |
| 63 |
tomoyo_check_acl(r, tomoyo_check_env_acl); |
| 64 |
error = tomoyo_audit_env_log(r); |
| 65 |
} while (error == TOMOYO_RETRY_REQUEST); |
| 66 |
return error; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* tomoyo_same_env_acl - Check for duplicated "struct tomoyo_env_acl" entry. |
| 71 |
* |
| 72 |
* @a: Pointer to "struct tomoyo_acl_info". |
| 73 |
* @b: Pointer to "struct tomoyo_acl_info". |
| 74 |
* |
| 75 |
* Returns true if @a == @b, false otherwise. |
| 76 |
*/ |
| 77 |
static bool tomoyo_same_env_acl(const struct tomoyo_acl_info *a, |
| 78 |
const struct tomoyo_acl_info *b) |
| 79 |
{ |
| 80 |
const struct tomoyo_env_acl *p1 = container_of(a, typeof(*p1), head); |
| 81 |
const struct tomoyo_env_acl *p2 = container_of(b, typeof(*p2), head); |
| 82 |
|
| 83 |
return p1->env == p2->env; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* tomoyo_write_env - Write "struct tomoyo_env_acl" list. |
| 88 |
* |
| 89 |
* @param: Pointer to "struct tomoyo_acl_param". |
| 90 |
* |
| 91 |
* Returns 0 on success, negative value otherwise. |
| 92 |
* |
| 93 |
* Caller holds tomoyo_read_lock(). |
| 94 |
*/ |
| 95 |
static int tomoyo_write_env(struct tomoyo_acl_param *param) |
| 96 |
{ |
| 97 |
struct tomoyo_env_acl e = { .head.type = TOMOYO_TYPE_ENV_ACL }; |
| 98 |
int error = -ENOMEM; |
| 99 |
const char *data = tomoyo_read_token(param); |
| 100 |
|
| 101 |
if (!tomoyo_correct_word(data) || strchr(data, '=')) |
| 102 |
return -EINVAL; |
| 103 |
e.env = tomoyo_get_name(data); |
| 104 |
if (!e.env) |
| 105 |
return error; |
| 106 |
error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 107 |
tomoyo_same_env_acl, NULL); |
| 108 |
tomoyo_put_name(e.env); |
| 109 |
return error; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* tomoyo_write_misc - Update environment variable list. |
| 114 |
* |
| 115 |
* @param: Pointer to "struct tomoyo_acl_param". |
| 116 |
* |
| 117 |
* Returns 0 on success, negative value otherwise. |
| 118 |
*/ |
| 119 |
int tomoyo_write_misc(struct tomoyo_acl_param *param) |
| 120 |
{ |
| 121 |
if (tomoyo_str_starts(¶m->data, "env ")) |
| 122 |
return tomoyo_write_env(param); |
| 123 |
return -EINVAL; |
| 124 |
} |