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