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