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