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