Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 2 | /* |
| 3 | * security/tomoyo/domain.c |
| 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "common.h" |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 9 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 10 | #include <linux/binfmts.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 12 | #include <linux/rculist.h> |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 13 | |
| 14 | /* Variables definitions.*/ |
| 15 | |
| 16 | /* The initial domain. */ |
| 17 | struct tomoyo_domain_info tomoyo_kernel_domain; |
| 18 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 19 | /** |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 20 | * tomoyo_update_policy - Update an entry for exception policy. |
| 21 | * |
| 22 | * @new_entry: Pointer to "struct tomoyo_acl_info". |
| 23 | * @size: Size of @new_entry in bytes. |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 24 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 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 | */ |
| 31 | int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 32 | struct tomoyo_acl_param *param, |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 33 | bool (*check_duplicate)(const struct tomoyo_acl_head |
| 34 | *, |
| 35 | const struct tomoyo_acl_head |
| 36 | *)) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 37 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 38 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 39 | struct tomoyo_acl_head *entry; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 40 | struct list_head *list = param->list; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 41 | |
| 42 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 43 | return -ENOMEM; |
Tetsuo Handa | 6bd5ce6 | 2019-12-16 19:16:48 +0900 | [diff] [blame] | 44 | list_for_each_entry_rcu(entry, list, list, |
| 45 | srcu_read_lock_held(&tomoyo_ss)) { |
Tetsuo Handa | f9732ea | 2011-09-25 17:50:23 +0900 | [diff] [blame] | 46 | if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS) |
| 47 | continue; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 48 | if (!check_duplicate(entry, new_entry)) |
| 49 | continue; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 50 | entry->is_deleted = param->is_delete; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 51 | error = 0; |
| 52 | break; |
| 53 | } |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 54 | if (error && !param->is_delete) { |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 55 | entry = tomoyo_commit_ok(new_entry, size); |
| 56 | if (entry) { |
| 57 | list_add_tail_rcu(&entry->list, list); |
| 58 | error = 0; |
| 59 | } |
| 60 | } |
| 61 | mutex_unlock(&tomoyo_policy_lock); |
| 62 | return error; |
| 63 | } |
| 64 | |
| 65 | /** |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 66 | * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry. |
| 67 | * |
| 68 | * @a: Pointer to "struct tomoyo_acl_info". |
| 69 | * @b: Pointer to "struct tomoyo_acl_info". |
| 70 | * |
| 71 | * Returns true if @a == @b, false otherwise. |
| 72 | */ |
| 73 | static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a, |
| 74 | const struct tomoyo_acl_info *b) |
| 75 | { |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 76 | return a->type == b->type && a->cond == b->cond; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 80 | * tomoyo_update_domain - Update an entry for domain policy. |
| 81 | * |
| 82 | * @new_entry: Pointer to "struct tomoyo_acl_info". |
| 83 | * @size: Size of @new_entry in bytes. |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 84 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 85 | * @check_duplicate: Callback function to find duplicated entry. |
| 86 | * @merge_duplicate: Callback function to merge duplicated entry. |
| 87 | * |
| 88 | * Returns 0 on success, negative value otherwise. |
| 89 | * |
| 90 | * Caller holds tomoyo_read_lock(). |
| 91 | */ |
| 92 | int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 93 | struct tomoyo_acl_param *param, |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 94 | bool (*check_duplicate)(const struct tomoyo_acl_info |
| 95 | *, |
| 96 | const struct tomoyo_acl_info |
| 97 | *), |
| 98 | bool (*merge_duplicate)(struct tomoyo_acl_info *, |
| 99 | struct tomoyo_acl_info *, |
| 100 | const bool)) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 101 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 102 | const bool is_delete = param->is_delete; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 103 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 104 | struct tomoyo_acl_info *entry; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 105 | struct list_head * const list = param->list; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 106 | |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 107 | if (param->data[0]) { |
| 108 | new_entry->cond = tomoyo_get_condition(param); |
| 109 | if (!new_entry->cond) |
| 110 | return -EINVAL; |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 111 | /* |
| 112 | * Domain transition preference is allowed for only |
| 113 | * "file execute" entries. |
| 114 | */ |
| 115 | if (new_entry->cond->transit && |
| 116 | !(new_entry->type == TOMOYO_TYPE_PATH_ACL && |
| 117 | container_of(new_entry, struct tomoyo_path_acl, head) |
| 118 | ->perm == 1 << TOMOYO_TYPE_EXECUTE)) |
| 119 | goto out; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 120 | } |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 121 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 122 | goto out; |
Tetsuo Handa | 6bd5ce6 | 2019-12-16 19:16:48 +0900 | [diff] [blame] | 123 | list_for_each_entry_rcu(entry, list, list, |
| 124 | srcu_read_lock_held(&tomoyo_ss)) { |
Tetsuo Handa | f9732ea | 2011-09-25 17:50:23 +0900 | [diff] [blame] | 125 | if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS) |
| 126 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 127 | if (!tomoyo_same_acl_head(entry, new_entry) || |
| 128 | !check_duplicate(entry, new_entry)) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 129 | continue; |
| 130 | if (merge_duplicate) |
| 131 | entry->is_deleted = merge_duplicate(entry, new_entry, |
| 132 | is_delete); |
| 133 | else |
| 134 | entry->is_deleted = is_delete; |
| 135 | error = 0; |
| 136 | break; |
| 137 | } |
| 138 | if (error && !is_delete) { |
| 139 | entry = tomoyo_commit_ok(new_entry, size); |
| 140 | if (entry) { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 141 | list_add_tail_rcu(&entry->list, list); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 142 | error = 0; |
| 143 | } |
| 144 | } |
| 145 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 146 | out: |
| 147 | tomoyo_put_condition(new_entry->cond); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 148 | return error; |
| 149 | } |
| 150 | |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 151 | /** |
| 152 | * tomoyo_check_acl - Do permission check. |
| 153 | * |
| 154 | * @r: Pointer to "struct tomoyo_request_info". |
| 155 | * @check_entry: Callback function to check type specific parameters. |
| 156 | * |
| 157 | * Returns 0 on success, negative value otherwise. |
| 158 | * |
| 159 | * Caller holds tomoyo_read_lock(). |
| 160 | */ |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 161 | void tomoyo_check_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 162 | bool (*check_entry)(struct tomoyo_request_info *, |
| 163 | const struct tomoyo_acl_info *)) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 164 | { |
| 165 | const struct tomoyo_domain_info *domain = r->domain; |
| 166 | struct tomoyo_acl_info *ptr; |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 167 | const struct list_head *list = &domain->acl_info_list; |
Tetsuo Handa | 4b42564 | 2019-01-24 18:37:36 +0900 | [diff] [blame] | 168 | u16 i = 0; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 169 | |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 170 | retry: |
Tetsuo Handa | 6bd5ce6 | 2019-12-16 19:16:48 +0900 | [diff] [blame] | 171 | list_for_each_entry_rcu(ptr, list, list, |
| 172 | srcu_read_lock_held(&tomoyo_ss)) { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 173 | if (ptr->is_deleted || ptr->type != r->param_type) |
| 174 | continue; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 175 | if (!check_entry(r, ptr)) |
| 176 | continue; |
| 177 | if (!tomoyo_condition(r, ptr->cond)) |
| 178 | continue; |
Tetsuo Handa | 1f067a6 | 2011-09-10 15:24:56 +0900 | [diff] [blame] | 179 | r->matched_acl = ptr; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 180 | r->granted = true; |
| 181 | return; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 182 | } |
Tetsuo Handa | 4b42564 | 2019-01-24 18:37:36 +0900 | [diff] [blame] | 183 | for (; i < TOMOYO_MAX_ACL_GROUPS; i++) { |
| 184 | if (!test_bit(i, domain->group)) |
| 185 | continue; |
| 186 | list = &domain->ns->acl_group[i++]; |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 187 | goto retry; |
| 188 | } |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 189 | r->granted = false; |
| 190 | } |
| 191 | |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 192 | /* The list for "struct tomoyo_domain_info". */ |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 193 | LIST_HEAD(tomoyo_domain_list); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 194 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 195 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 196 | * tomoyo_last_word - Get last component of a domainname. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 197 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 198 | * @name: Domainname to check. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 199 | * |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 200 | * Returns the last word of @domainname. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 201 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 202 | static const char *tomoyo_last_word(const char *name) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 203 | { |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 204 | const char *cp = strrchr(name, ' '); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 205 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 206 | if (cp) |
| 207 | return cp + 1; |
| 208 | return name; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 211 | /** |
| 212 | * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry. |
| 213 | * |
| 214 | * @a: Pointer to "struct tomoyo_acl_head". |
| 215 | * @b: Pointer to "struct tomoyo_acl_head". |
| 216 | * |
| 217 | * Returns true if @a == @b, false otherwise. |
| 218 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 219 | static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a, |
| 220 | const struct tomoyo_acl_head *b) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 221 | { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 222 | const struct tomoyo_transition_control *p1 = container_of(a, |
| 223 | typeof(*p1), |
| 224 | head); |
| 225 | const struct tomoyo_transition_control *p2 = container_of(b, |
| 226 | typeof(*p2), |
| 227 | head); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 228 | |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 229 | return p1->type == p2->type && p1->is_last_name == p2->is_last_name |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 230 | && p1->domainname == p2->domainname |
| 231 | && p1->program == p2->program; |
| 232 | } |
| 233 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 234 | /** |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 235 | * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 236 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 237 | * @param: Pointer to "struct tomoyo_acl_param". |
| 238 | * @type: Type of this entry. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 239 | * |
| 240 | * Returns 0 on success, negative value otherwise. |
| 241 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 242 | int tomoyo_write_transition_control(struct tomoyo_acl_param *param, |
| 243 | const u8 type) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 244 | { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 245 | struct tomoyo_transition_control e = { .type = type }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 246 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
| 247 | char *program = param->data; |
| 248 | char *domainname = strstr(program, " from "); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 249 | |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 250 | if (domainname) { |
| 251 | *domainname = '\0'; |
| 252 | domainname += 6; |
| 253 | } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP || |
| 254 | type == TOMOYO_TRANSITION_CONTROL_KEEP) { |
| 255 | domainname = program; |
| 256 | program = NULL; |
| 257 | } |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 258 | if (program && strcmp(program, "any")) { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 259 | if (!tomoyo_correct_path(program)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 260 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 261 | e.program = tomoyo_get_name(program); |
| 262 | if (!e.program) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 263 | goto out; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 264 | } |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 265 | if (domainname && strcmp(domainname, "any")) { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 266 | if (!tomoyo_correct_domain(domainname)) { |
| 267 | if (!tomoyo_correct_path(domainname)) |
| 268 | goto out; |
| 269 | e.is_last_name = true; |
| 270 | } |
| 271 | e.domainname = tomoyo_get_name(domainname); |
| 272 | if (!e.domainname) |
| 273 | goto out; |
| 274 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 275 | param->list = ¶m->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL]; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 276 | error = tomoyo_update_policy(&e.head, sizeof(e), param, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 277 | tomoyo_same_transition_control); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 278 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 279 | tomoyo_put_name(e.domainname); |
| 280 | tomoyo_put_name(e.program); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 281 | return error; |
| 282 | } |
| 283 | |
| 284 | /** |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 285 | * tomoyo_scan_transition - Try to find specific domain transition type. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 286 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 287 | * @list: Pointer to "struct list_head". |
| 288 | * @domainname: The name of current domain. |
| 289 | * @program: The name of requested program. |
| 290 | * @last_name: The last component of @domainname. |
| 291 | * @type: One of values in "enum tomoyo_transition_type". |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 292 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 293 | * Returns true if found one, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 294 | * |
| 295 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 296 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 297 | static inline bool tomoyo_scan_transition |
| 298 | (const struct list_head *list, const struct tomoyo_path_info *domainname, |
| 299 | const struct tomoyo_path_info *program, const char *last_name, |
| 300 | const enum tomoyo_transition_type type) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 301 | { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 302 | const struct tomoyo_transition_control *ptr; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 303 | |
Tetsuo Handa | 6bd5ce6 | 2019-12-16 19:16:48 +0900 | [diff] [blame] | 304 | list_for_each_entry_rcu(ptr, list, head.list, |
| 305 | srcu_read_lock_held(&tomoyo_ss)) { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 306 | if (ptr->head.is_deleted || ptr->type != type) |
| 307 | continue; |
| 308 | if (ptr->domainname) { |
| 309 | if (!ptr->is_last_name) { |
| 310 | if (ptr->domainname != domainname) |
| 311 | continue; |
| 312 | } else { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 313 | /* |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 314 | * Use direct strcmp() since this is |
| 315 | * unlikely used. |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 316 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 317 | if (strcmp(ptr->domainname->name, last_name)) |
| 318 | continue; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 319 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 320 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 321 | if (ptr->program && tomoyo_pathcmp(ptr->program, program)) |
| 322 | continue; |
| 323 | return true; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 324 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * tomoyo_transition_type - Get domain transition type. |
| 330 | * |
| 331 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
| 332 | * @domainname: The name of current domain. |
| 333 | * @program: The name of requested program. |
| 334 | * |
| 335 | * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes |
| 336 | * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if |
| 337 | * executing @program reinitializes domain transition within that namespace, |
| 338 | * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname , |
| 339 | * others otherwise. |
| 340 | * |
| 341 | * Caller holds tomoyo_read_lock(). |
| 342 | */ |
| 343 | static enum tomoyo_transition_type tomoyo_transition_type |
| 344 | (const struct tomoyo_policy_namespace *ns, |
| 345 | const struct tomoyo_path_info *domainname, |
| 346 | const struct tomoyo_path_info *program) |
| 347 | { |
| 348 | const char *last_name = tomoyo_last_word(domainname->name); |
| 349 | enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 350 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 351 | while (type < TOMOYO_MAX_TRANSITION_TYPE) { |
| 352 | const struct list_head * const list = |
| 353 | &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL]; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 354 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 355 | if (!tomoyo_scan_transition(list, domainname, program, |
| 356 | last_name, type)) { |
| 357 | type++; |
| 358 | continue; |
| 359 | } |
| 360 | if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET && |
| 361 | type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) |
| 362 | break; |
| 363 | /* |
| 364 | * Do not check for reset_domain if no_reset_domain matched. |
| 365 | * Do not check for initialize_domain if no_initialize_domain |
| 366 | * matched. |
| 367 | */ |
| 368 | type++; |
| 369 | type++; |
| 370 | } |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 371 | return type; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 372 | } |
| 373 | |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 374 | /** |
| 375 | * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry. |
| 376 | * |
| 377 | * @a: Pointer to "struct tomoyo_acl_head". |
| 378 | * @b: Pointer to "struct tomoyo_acl_head". |
| 379 | * |
| 380 | * Returns true if @a == @b, false otherwise. |
| 381 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 382 | static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a, |
| 383 | const struct tomoyo_acl_head *b) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 384 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 385 | const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1), |
| 386 | head); |
| 387 | const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2), |
| 388 | head); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 389 | |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 390 | return p1->original_name == p2->original_name && |
| 391 | p1->aggregated_name == p2->aggregated_name; |
| 392 | } |
| 393 | |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 394 | /** |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 395 | * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list. |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 396 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 397 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 398 | * |
| 399 | * Returns 0 on success, negative value otherwise. |
| 400 | * |
| 401 | * Caller holds tomoyo_read_lock(). |
| 402 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 403 | int tomoyo_write_aggregator(struct tomoyo_acl_param *param) |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 404 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 405 | struct tomoyo_aggregator e = { }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 406 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
| 407 | const char *original_name = tomoyo_read_token(param); |
| 408 | const char *aggregated_name = tomoyo_read_token(param); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 409 | |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 410 | if (!tomoyo_correct_word(original_name) || |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 411 | !tomoyo_correct_path(aggregated_name)) |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 412 | return -EINVAL; |
| 413 | e.original_name = tomoyo_get_name(original_name); |
| 414 | e.aggregated_name = tomoyo_get_name(aggregated_name); |
| 415 | if (!e.original_name || !e.aggregated_name || |
| 416 | e.aggregated_name->is_patterned) /* No patterns allowed. */ |
| 417 | goto out; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 418 | param->list = ¶m->ns->policy_list[TOMOYO_ID_AGGREGATOR]; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 419 | error = tomoyo_update_policy(&e.head, sizeof(e), param, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 420 | tomoyo_same_aggregator); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 421 | out: |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 422 | tomoyo_put_name(e.original_name); |
| 423 | tomoyo_put_name(e.aggregated_name); |
| 424 | return error; |
| 425 | } |
| 426 | |
| 427 | /** |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 428 | * tomoyo_find_namespace - Find specified namespace. |
| 429 | * |
| 430 | * @name: Name of namespace to find. |
| 431 | * @len: Length of @name. |
| 432 | * |
| 433 | * Returns pointer to "struct tomoyo_policy_namespace" if found, |
| 434 | * NULL otherwise. |
| 435 | * |
| 436 | * Caller holds tomoyo_read_lock(). |
| 437 | */ |
| 438 | static struct tomoyo_policy_namespace *tomoyo_find_namespace |
| 439 | (const char *name, const unsigned int len) |
| 440 | { |
| 441 | struct tomoyo_policy_namespace *ns; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 442 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 443 | list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) { |
| 444 | if (strncmp(name, ns->name, len) || |
| 445 | (name[len] && name[len] != ' ')) |
| 446 | continue; |
| 447 | return ns; |
| 448 | } |
| 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * tomoyo_assign_namespace - Create a new namespace. |
| 454 | * |
| 455 | * @domainname: Name of namespace to create. |
| 456 | * |
| 457 | * Returns pointer to "struct tomoyo_policy_namespace" on success, |
| 458 | * NULL otherwise. |
| 459 | * |
| 460 | * Caller holds tomoyo_read_lock(). |
| 461 | */ |
| 462 | struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname) |
| 463 | { |
| 464 | struct tomoyo_policy_namespace *ptr; |
| 465 | struct tomoyo_policy_namespace *entry; |
| 466 | const char *cp = domainname; |
| 467 | unsigned int len = 0; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 468 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 469 | while (*cp && *cp++ != ' ') |
| 470 | len++; |
| 471 | ptr = tomoyo_find_namespace(domainname, len); |
| 472 | if (ptr) |
| 473 | return ptr; |
| 474 | if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname)) |
| 475 | return NULL; |
Zheng Zengkai | 1b6b924 | 2020-11-26 22:38:15 +0800 | [diff] [blame] | 476 | entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS | __GFP_NOWARN); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 477 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 478 | goto out; |
| 479 | ptr = tomoyo_find_namespace(domainname, len); |
| 480 | if (!ptr && tomoyo_memory_ok(entry)) { |
| 481 | char *name = (char *) (entry + 1); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 482 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 483 | ptr = entry; |
| 484 | memmove(name, domainname, len); |
| 485 | name[len] = '\0'; |
| 486 | entry->name = name; |
| 487 | tomoyo_init_policy_namespace(entry); |
| 488 | entry = NULL; |
| 489 | } |
| 490 | mutex_unlock(&tomoyo_policy_lock); |
| 491 | out: |
| 492 | kfree(entry); |
| 493 | return ptr; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * tomoyo_namespace_jump - Check for namespace jump. |
| 498 | * |
| 499 | * @domainname: Name of domain. |
| 500 | * |
| 501 | * Returns true if namespace differs, false otherwise. |
| 502 | */ |
| 503 | static bool tomoyo_namespace_jump(const char *domainname) |
| 504 | { |
| 505 | const char *namespace = tomoyo_current_namespace()->name; |
| 506 | const int len = strlen(namespace); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 507 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 508 | return strncmp(domainname, namespace, len) || |
| 509 | (domainname[len] && domainname[len] != ' '); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * tomoyo_assign_domain - Create a domain or a namespace. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 514 | * |
| 515 | * @domainname: The name of domain. |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 516 | * @transit: True if transit to domain found or created. |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 517 | * |
| 518 | * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 519 | * |
| 520 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 521 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 522 | struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 523 | const bool transit) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 524 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 525 | struct tomoyo_domain_info e = { }; |
| 526 | struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname); |
| 527 | bool created = false; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 528 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 529 | if (entry) { |
| 530 | if (transit) { |
| 531 | /* |
| 532 | * Since namespace is created at runtime, profiles may |
| 533 | * not be created by the moment the process transits to |
| 534 | * that domain. Do not perform domain transition if |
| 535 | * profile for that domain is not yet created. |
| 536 | */ |
Tetsuo Handa | e00fb3f | 2011-09-27 11:48:53 +0900 | [diff] [blame] | 537 | if (tomoyo_policy_loaded && |
| 538 | !entry->ns->profile_ptr[entry->profile]) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 539 | return NULL; |
| 540 | } |
| 541 | return entry; |
| 542 | } |
| 543 | /* Requested domain does not exist. */ |
| 544 | /* Don't create requested domain if domainname is invalid. */ |
| 545 | if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 || |
| 546 | !tomoyo_correct_domain(domainname)) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 547 | return NULL; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 548 | /* |
| 549 | * Since definition of profiles and acl_groups may differ across |
| 550 | * namespaces, do not inherit "use_profile" and "use_group" settings |
| 551 | * by automatically creating requested domain upon domain transition. |
| 552 | */ |
| 553 | if (transit && tomoyo_namespace_jump(domainname)) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 554 | return NULL; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 555 | e.ns = tomoyo_assign_namespace(domainname); |
| 556 | if (!e.ns) |
| 557 | return NULL; |
| 558 | /* |
| 559 | * "use_profile" and "use_group" settings for automatically created |
| 560 | * domains are inherited from current domain. These are 0 for manually |
| 561 | * created domains. |
| 562 | */ |
| 563 | if (transit) { |
| 564 | const struct tomoyo_domain_info *domain = tomoyo_domain(); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 565 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 566 | e.profile = domain->profile; |
Tetsuo Handa | 4b42564 | 2019-01-24 18:37:36 +0900 | [diff] [blame] | 567 | memcpy(e.group, domain->group, sizeof(e.group)); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 568 | } |
| 569 | e.domainname = tomoyo_get_name(domainname); |
| 570 | if (!e.domainname) |
| 571 | return NULL; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 572 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 573 | goto out; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 574 | entry = tomoyo_find_domain(domainname); |
| 575 | if (!entry) { |
| 576 | entry = tomoyo_commit_ok(&e, sizeof(e)); |
| 577 | if (entry) { |
| 578 | INIT_LIST_HEAD(&entry->acl_info_list); |
| 579 | list_add_tail_rcu(&entry->list, &tomoyo_domain_list); |
| 580 | created = true; |
| 581 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 582 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 583 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 584 | out: |
| 585 | tomoyo_put_name(e.domainname); |
| 586 | if (entry && transit) { |
| 587 | if (created) { |
| 588 | struct tomoyo_request_info r; |
Tetsuo Handa | 4b42564 | 2019-01-24 18:37:36 +0900 | [diff] [blame] | 589 | int i; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 590 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 591 | tomoyo_init_request_info(&r, entry, |
| 592 | TOMOYO_MAC_FILE_EXECUTE); |
| 593 | r.granted = false; |
| 594 | tomoyo_write_log(&r, "use_profile %u\n", |
| 595 | entry->profile); |
Tetsuo Handa | 4b42564 | 2019-01-24 18:37:36 +0900 | [diff] [blame] | 596 | for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++) |
| 597 | if (test_bit(i, entry->group)) |
| 598 | tomoyo_write_log(&r, "use_group %u\n", |
| 599 | i); |
Tetsuo Handa | 778c4a4 | 2011-09-25 17:49:09 +0900 | [diff] [blame] | 600 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | return entry; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /** |
Tetsuo Handa | d58e0da | 2011-09-10 15:22:48 +0900 | [diff] [blame] | 607 | * tomoyo_environ - Check permission for environment variable names. |
| 608 | * |
| 609 | * @ee: Pointer to "struct tomoyo_execve". |
| 610 | * |
| 611 | * Returns 0 on success, negative value otherwise. |
| 612 | */ |
| 613 | static int tomoyo_environ(struct tomoyo_execve *ee) |
| 614 | { |
| 615 | struct tomoyo_request_info *r = &ee->r; |
| 616 | struct linux_binprm *bprm = ee->bprm; |
| 617 | /* env_page.data is allocated by tomoyo_dump_page(). */ |
| 618 | struct tomoyo_page_dump env_page = { }; |
| 619 | char *arg_ptr; /* Size is TOMOYO_EXEC_TMPSIZE bytes */ |
| 620 | int arg_len = 0; |
| 621 | unsigned long pos = bprm->p; |
| 622 | int offset = pos % PAGE_SIZE; |
| 623 | int argv_count = bprm->argc; |
| 624 | int envp_count = bprm->envc; |
| 625 | int error = -ENOMEM; |
| 626 | |
| 627 | ee->r.type = TOMOYO_MAC_ENVIRON; |
| 628 | ee->r.profile = r->domain->profile; |
| 629 | ee->r.mode = tomoyo_get_mode(r->domain->ns, ee->r.profile, |
| 630 | TOMOYO_MAC_ENVIRON); |
| 631 | if (!r->mode || !envp_count) |
| 632 | return 0; |
| 633 | arg_ptr = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS); |
| 634 | if (!arg_ptr) |
| 635 | goto out; |
| 636 | while (error == -ENOMEM) { |
| 637 | if (!tomoyo_dump_page(bprm, pos, &env_page)) |
| 638 | goto out; |
| 639 | pos += PAGE_SIZE - offset; |
| 640 | /* Read. */ |
| 641 | while (argv_count && offset < PAGE_SIZE) { |
| 642 | if (!env_page.data[offset++]) |
| 643 | argv_count--; |
| 644 | } |
| 645 | if (argv_count) { |
| 646 | offset = 0; |
| 647 | continue; |
| 648 | } |
| 649 | while (offset < PAGE_SIZE) { |
| 650 | const unsigned char c = env_page.data[offset++]; |
| 651 | |
| 652 | if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) { |
| 653 | if (c == '=') { |
| 654 | arg_ptr[arg_len++] = '\0'; |
| 655 | } else if (c == '\\') { |
| 656 | arg_ptr[arg_len++] = '\\'; |
| 657 | arg_ptr[arg_len++] = '\\'; |
| 658 | } else if (c > ' ' && c < 127) { |
| 659 | arg_ptr[arg_len++] = c; |
| 660 | } else { |
| 661 | arg_ptr[arg_len++] = '\\'; |
| 662 | arg_ptr[arg_len++] = (c >> 6) + '0'; |
| 663 | arg_ptr[arg_len++] |
| 664 | = ((c >> 3) & 7) + '0'; |
| 665 | arg_ptr[arg_len++] = (c & 7) + '0'; |
| 666 | } |
| 667 | } else { |
| 668 | arg_ptr[arg_len] = '\0'; |
| 669 | } |
| 670 | if (c) |
| 671 | continue; |
| 672 | if (tomoyo_env_perm(r, arg_ptr)) { |
| 673 | error = -EPERM; |
| 674 | break; |
| 675 | } |
| 676 | if (!--envp_count) { |
| 677 | error = 0; |
| 678 | break; |
| 679 | } |
| 680 | arg_len = 0; |
| 681 | } |
| 682 | offset = 0; |
| 683 | } |
| 684 | out: |
| 685 | if (r->mode != TOMOYO_CONFIG_ENFORCING) |
| 686 | error = 0; |
| 687 | kfree(env_page.data); |
| 688 | kfree(arg_ptr); |
| 689 | return error; |
| 690 | } |
| 691 | |
| 692 | /** |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 693 | * tomoyo_find_next_domain - Find a domain. |
| 694 | * |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 695 | * @bprm: Pointer to "struct linux_binprm". |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 696 | * |
| 697 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 698 | * |
| 699 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 700 | */ |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 701 | int tomoyo_find_next_domain(struct linux_binprm *bprm) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 702 | { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 703 | struct tomoyo_domain_info *old_domain = tomoyo_domain(); |
| 704 | struct tomoyo_domain_info *domain = NULL; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 705 | const char *original_name = bprm->filename; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 706 | int retval = -ENOMEM; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 707 | bool reject_on_transition_failure = false; |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 708 | const struct tomoyo_path_info *candidate; |
| 709 | struct tomoyo_path_info exename; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 710 | struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS); |
Tetsuo Handa | d58e0da | 2011-09-10 15:22:48 +0900 | [diff] [blame] | 711 | |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 712 | if (!ee) |
| 713 | return -ENOMEM; |
| 714 | ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS); |
| 715 | if (!ee->tmp) { |
| 716 | kfree(ee); |
| 717 | return -ENOMEM; |
| 718 | } |
| 719 | /* ee->dump->data is allocated by tomoyo_dump_page(). */ |
| 720 | tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE); |
| 721 | ee->r.ee = ee; |
| 722 | ee->bprm = bprm; |
| 723 | ee->r.obj = &ee->obj; |
| 724 | ee->obj.path1 = bprm->file->f_path; |
Tetsuo Handa | 0617c7f | 2010-06-21 09:58:53 +0900 | [diff] [blame] | 725 | /* Get symlink's pathname of program. */ |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 726 | retval = -ENOENT; |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 727 | exename.name = tomoyo_realpath_nofollow(original_name); |
| 728 | if (!exename.name) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 729 | goto out; |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 730 | tomoyo_fill_path_info(&exename); |
| 731 | retry: |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 732 | /* Check 'aggregator' directive. */ |
| 733 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 734 | struct tomoyo_aggregator *ptr; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 735 | struct list_head *list = |
| 736 | &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR]; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 737 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 738 | /* Check 'aggregator' directive. */ |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 739 | candidate = &exename; |
Tetsuo Handa | 6bd5ce6 | 2019-12-16 19:16:48 +0900 | [diff] [blame] | 740 | list_for_each_entry_rcu(ptr, list, head.list, |
| 741 | srcu_read_lock_held(&tomoyo_ss)) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 742 | if (ptr->head.is_deleted || |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 743 | !tomoyo_path_matches_pattern(&exename, |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 744 | ptr->original_name)) |
| 745 | continue; |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 746 | candidate = ptr->aggregated_name; |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 751 | /* Check execute permission. */ |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 752 | retval = tomoyo_execute_permission(&ee->r, candidate); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 753 | if (retval == TOMOYO_RETRY_REQUEST) |
| 754 | goto retry; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 755 | if (retval < 0) |
| 756 | goto out; |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 757 | /* |
| 758 | * To be able to specify domainnames with wildcards, use the |
| 759 | * pathname specified in the policy (which may contain |
| 760 | * wildcard) rather than the pathname passed to execve() |
| 761 | * (which never contains wildcard). |
| 762 | */ |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 763 | if (ee->r.param.path.matched_path) |
| 764 | candidate = ee->r.param.path.matched_path; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 765 | |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 766 | /* |
| 767 | * Check for domain transition preference if "file execute" matched. |
Eric W. Biederman | be619f7 | 2020-07-13 12:06:48 -0500 | [diff] [blame] | 768 | * If preference is given, make execve() fail if domain transition |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 769 | * has failed, for domain transition preference should be used with |
| 770 | * destination domain defined. |
| 771 | */ |
| 772 | if (ee->transition) { |
| 773 | const char *domainname = ee->transition->name; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 774 | |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 775 | reject_on_transition_failure = true; |
| 776 | if (!strcmp(domainname, "keep")) |
| 777 | goto force_keep_domain; |
| 778 | if (!strcmp(domainname, "child")) |
| 779 | goto force_child_domain; |
| 780 | if (!strcmp(domainname, "reset")) |
| 781 | goto force_reset_domain; |
| 782 | if (!strcmp(domainname, "initialize")) |
| 783 | goto force_initialize_domain; |
| 784 | if (!strcmp(domainname, "parent")) { |
| 785 | char *cp; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 786 | |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 787 | strncpy(ee->tmp, old_domain->domainname->name, |
| 788 | TOMOYO_EXEC_TMPSIZE - 1); |
| 789 | cp = strrchr(ee->tmp, ' '); |
| 790 | if (cp) |
| 791 | *cp = '\0'; |
| 792 | } else if (*domainname == '<') |
| 793 | strncpy(ee->tmp, domainname, TOMOYO_EXEC_TMPSIZE - 1); |
| 794 | else |
| 795 | snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s", |
| 796 | old_domain->domainname->name, domainname); |
| 797 | goto force_jump_domain; |
| 798 | } |
| 799 | /* |
| 800 | * No domain transition preference specified. |
| 801 | * Calculate domain to transit to. |
| 802 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 803 | switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname, |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 804 | candidate)) { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 805 | case TOMOYO_TRANSITION_CONTROL_RESET: |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 806 | force_reset_domain: |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 807 | /* Transit to the root of specified namespace. */ |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 808 | snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", |
| 809 | candidate->name); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 810 | /* |
Eric W. Biederman | be619f7 | 2020-07-13 12:06:48 -0500 | [diff] [blame] | 811 | * Make execve() fail if domain transition across namespaces |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 812 | * has failed. |
| 813 | */ |
| 814 | reject_on_transition_failure = true; |
| 815 | break; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 816 | case TOMOYO_TRANSITION_CONTROL_INITIALIZE: |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 817 | force_initialize_domain: |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 818 | /* Transit to the child of current namespace's root. */ |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 819 | snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s", |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 820 | old_domain->ns->name, candidate->name); |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 821 | break; |
| 822 | case TOMOYO_TRANSITION_CONTROL_KEEP: |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 823 | force_keep_domain: |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 824 | /* Keep current domain. */ |
| 825 | domain = old_domain; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 826 | break; |
| 827 | default: |
| 828 | if (old_domain == &tomoyo_kernel_domain && |
| 829 | !tomoyo_policy_loaded) { |
| 830 | /* |
| 831 | * Needn't to transit from kernel domain before |
| 832 | * starting /sbin/init. But transit from kernel domain |
| 833 | * if executing initializers because they might start |
| 834 | * before /sbin/init. |
| 835 | */ |
| 836 | domain = old_domain; |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 837 | break; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 838 | } |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 839 | force_child_domain: |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 840 | /* Normal domain transition. */ |
| 841 | snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s", |
| 842 | old_domain->domainname->name, candidate->name); |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 843 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 844 | } |
Tetsuo Handa | 6bce98e | 2011-09-16 22:54:25 +0900 | [diff] [blame] | 845 | force_jump_domain: |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 846 | if (!domain) |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 847 | domain = tomoyo_assign_domain(ee->tmp, true); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 848 | if (domain) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 849 | retval = 0; |
| 850 | else if (reject_on_transition_failure) { |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 851 | pr_warn("ERROR: Domain '%s' not ready.\n", ee->tmp); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 852 | retval = -ENOMEM; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 853 | } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 854 | retval = -ENOMEM; |
| 855 | else { |
| 856 | retval = 0; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 857 | if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) { |
| 858 | old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 859 | ee->r.granted = false; |
| 860 | tomoyo_write_log(&ee->r, "%s", tomoyo_dif |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 861 | [TOMOYO_DIF_TRANSITION_FAILED]); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 862 | pr_warn("ERROR: Domain '%s' not defined.\n", ee->tmp); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 863 | } |
| 864 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 865 | out: |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 866 | if (!domain) |
| 867 | domain = old_domain; |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 868 | /* Update reference count on "struct tomoyo_domain_info". */ |
Tetsuo Handa | 8c6cb98 | 2019-01-19 23:11:40 +0900 | [diff] [blame] | 869 | { |
| 870 | struct tomoyo_task *s = tomoyo_task(current); |
| 871 | |
| 872 | s->old_domain_info = s->domain_info; |
| 873 | s->domain_info = domain; |
| 874 | atomic_inc(&domain->users); |
| 875 | } |
Tetsuo Handa | a8f7640 | 2011-09-10 15:27:12 +0900 | [diff] [blame] | 876 | kfree(exename.name); |
Tetsuo Handa | d58e0da | 2011-09-10 15:22:48 +0900 | [diff] [blame] | 877 | if (!retval) { |
| 878 | ee->r.domain = domain; |
| 879 | retval = tomoyo_environ(ee); |
| 880 | } |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 881 | kfree(ee->tmp); |
| 882 | kfree(ee->dump.data); |
| 883 | kfree(ee); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 884 | return retval; |
| 885 | } |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 886 | |
| 887 | /** |
| 888 | * tomoyo_dump_page - Dump a page to buffer. |
| 889 | * |
| 890 | * @bprm: Pointer to "struct linux_binprm". |
| 891 | * @pos: Location to dump. |
Tetsuo Handa | 15269fb | 2020-12-06 13:44:57 +0900 | [diff] [blame] | 892 | * @dump: Pointer to "struct tomoyo_page_dump". |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 893 | * |
| 894 | * Returns true on success, false otherwise. |
| 895 | */ |
| 896 | bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos, |
| 897 | struct tomoyo_page_dump *dump) |
| 898 | { |
| 899 | struct page *page; |
Tetsuo Handa | d58e0da | 2011-09-10 15:22:48 +0900 | [diff] [blame] | 900 | |
| 901 | /* dump->data is released by tomoyo_find_next_domain(). */ |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 902 | if (!dump->data) { |
| 903 | dump->data = kzalloc(PAGE_SIZE, GFP_NOFS); |
| 904 | if (!dump->data) |
| 905 | return false; |
| 906 | } |
| 907 | /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */ |
| 908 | #ifdef CONFIG_MMU |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 909 | /* |
| 910 | * This is called at execve() time in order to dig around |
| 911 | * in the argv/environment of the new proceess |
| 912 | * (represented by bprm). 'current' is the process doing |
| 913 | * the execve(). |
| 914 | */ |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 915 | if (get_user_pages_remote(bprm->mm, pos, 1, |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 916 | FOLL_FORCE, &page, NULL, NULL) <= 0) |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 917 | return false; |
| 918 | #else |
| 919 | page = bprm->page[pos / PAGE_SIZE]; |
| 920 | #endif |
| 921 | if (page != dump->page) { |
| 922 | const unsigned int offset = pos % PAGE_SIZE; |
| 923 | /* |
| 924 | * Maybe kmap()/kunmap() should be used here. |
| 925 | * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic(). |
| 926 | * So do I. |
| 927 | */ |
Cong Wang | c58e037 | 2011-11-25 23:26:35 +0800 | [diff] [blame] | 928 | char *kaddr = kmap_atomic(page); |
Tetsuo Handa | d58e0da | 2011-09-10 15:22:48 +0900 | [diff] [blame] | 929 | |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 930 | dump->page = page; |
| 931 | memcpy(dump->data + offset, kaddr + offset, |
| 932 | PAGE_SIZE - offset); |
Cong Wang | c58e037 | 2011-11-25 23:26:35 +0800 | [diff] [blame] | 933 | kunmap_atomic(kaddr); |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 934 | } |
| 935 | /* Same with put_arg_page(page) in fs/exec.c */ |
| 936 | #ifdef CONFIG_MMU |
| 937 | put_page(page); |
| 938 | #endif |
| 939 | return true; |
| 940 | } |