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 | /** |
| 19 | * tomoyo_update_domain - Update an entry for domain 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 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 25 | * @check_duplicate: Callback function to find duplicated entry. |
| 26 | * @merge_duplicate: Callback function to merge duplicated entry. |
| 27 | * |
| 28 | * Returns 0 on success, negative value otherwise. |
| 29 | * |
| 30 | * Caller holds tomoyo_read_lock(). |
| 31 | */ |
| 32 | int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size, |
| 33 | bool is_delete, struct tomoyo_domain_info *domain, |
| 34 | bool (*check_duplicate) (const struct tomoyo_acl_info |
| 35 | *, |
| 36 | const struct tomoyo_acl_info |
| 37 | *), |
| 38 | bool (*merge_duplicate) (struct tomoyo_acl_info *, |
| 39 | struct tomoyo_acl_info *, |
| 40 | const bool)) |
| 41 | { |
| 42 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 43 | struct tomoyo_acl_info *entry; |
| 44 | |
| 45 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 46 | return error; |
| 47 | list_for_each_entry_rcu(entry, &domain->acl_info_list, list) { |
| 48 | if (!check_duplicate(entry, new_entry)) |
| 49 | continue; |
| 50 | if (merge_duplicate) |
| 51 | entry->is_deleted = merge_duplicate(entry, new_entry, |
| 52 | is_delete); |
| 53 | else |
| 54 | entry->is_deleted = is_delete; |
| 55 | error = 0; |
| 56 | break; |
| 57 | } |
| 58 | if (error && !is_delete) { |
| 59 | entry = tomoyo_commit_ok(new_entry, size); |
| 60 | if (entry) { |
| 61 | list_add_tail_rcu(&entry->list, &domain->acl_info_list); |
| 62 | error = 0; |
| 63 | } |
| 64 | } |
| 65 | mutex_unlock(&tomoyo_policy_lock); |
| 66 | return error; |
| 67 | } |
| 68 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 69 | /* |
| 70 | * tomoyo_domain_list is used for holding list of domains. |
| 71 | * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding |
| 72 | * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain. |
| 73 | * |
| 74 | * An entry is added by |
| 75 | * |
| 76 | * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \ |
| 77 | * /sys/kernel/security/tomoyo/domain_policy |
| 78 | * |
| 79 | * and is deleted by |
| 80 | * |
| 81 | * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \ |
| 82 | * /sys/kernel/security/tomoyo/domain_policy |
| 83 | * |
| 84 | * and all entries are retrieved by |
| 85 | * |
| 86 | * # cat /sys/kernel/security/tomoyo/domain_policy |
| 87 | * |
| 88 | * A domain is added by |
| 89 | * |
| 90 | * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy |
| 91 | * |
| 92 | * and is deleted by |
| 93 | * |
| 94 | * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy |
| 95 | * |
| 96 | * and all domains are retrieved by |
| 97 | * |
| 98 | * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
| 99 | * |
| 100 | * Normally, a domainname is monotonically getting longer because a domainname |
| 101 | * which the process will belong to if an execve() operation succeeds is |
| 102 | * defined as a concatenation of "current domainname" + "pathname passed to |
| 103 | * execve()". |
| 104 | * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for |
| 105 | * exceptions. |
| 106 | */ |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 107 | LIST_HEAD(tomoyo_domain_list); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 108 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 109 | /** |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 110 | * tomoyo_get_last_name - Get last component of a domainname. |
| 111 | * |
| 112 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 113 | * |
| 114 | * Returns the last component of the domainname. |
| 115 | */ |
| 116 | const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain) |
| 117 | { |
| 118 | const char *cp0 = domain->domainname->name; |
| 119 | const char *cp1 = strrchr(cp0, ' '); |
| 120 | |
| 121 | if (cp1) |
| 122 | return cp1 + 1; |
| 123 | return cp0; |
| 124 | } |
| 125 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 126 | /* |
| 127 | * tomoyo_domain_initializer_list is used for holding list of programs which |
| 128 | * triggers reinitialization of domainname. Normally, a domainname is |
| 129 | * monotonically getting longer. But sometimes, we restart daemon programs. |
| 130 | * It would be convenient for us that "a daemon started upon system boot" and |
| 131 | * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO |
| 132 | * provides a way to shorten domainnames. |
| 133 | * |
| 134 | * An entry is added by |
| 135 | * |
| 136 | * # echo 'initialize_domain /usr/sbin/httpd' > \ |
| 137 | * /sys/kernel/security/tomoyo/exception_policy |
| 138 | * |
| 139 | * and is deleted by |
| 140 | * |
| 141 | * # echo 'delete initialize_domain /usr/sbin/httpd' > \ |
| 142 | * /sys/kernel/security/tomoyo/exception_policy |
| 143 | * |
| 144 | * and all entries are retrieved by |
| 145 | * |
| 146 | * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy |
| 147 | * |
| 148 | * In the example above, /usr/sbin/httpd will belong to |
| 149 | * "<kernel> /usr/sbin/httpd" domain. |
| 150 | * |
| 151 | * You may specify a domainname using "from" keyword. |
| 152 | * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd" |
| 153 | * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd" |
| 154 | * domain to belong to "<kernel> /usr/sbin/httpd" domain. |
| 155 | * |
| 156 | * You may add "no_" prefix to "initialize_domain". |
| 157 | * "initialize_domain /usr/sbin/httpd" and |
| 158 | * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd" |
| 159 | * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain |
| 160 | * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain. |
| 161 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 162 | LIST_HEAD(tomoyo_domain_initializer_list); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list. |
| 166 | * |
| 167 | * @domainname: The name of domain. May be NULL. |
| 168 | * @program: The name of program. |
| 169 | * @is_not: True if it is "no_initialize_domain" entry. |
| 170 | * @is_delete: True if it is a delete request. |
| 171 | * |
| 172 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 173 | * |
| 174 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 175 | */ |
| 176 | static int tomoyo_update_domain_initializer_entry(const char *domainname, |
| 177 | const char *program, |
| 178 | const bool is_not, |
| 179 | const bool is_delete) |
| 180 | { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 181 | struct tomoyo_domain_initializer_entry *ptr; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 182 | struct tomoyo_domain_initializer_entry e = { .is_not = is_not }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 183 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 184 | |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 185 | if (!tomoyo_is_correct_path(program)) |
| 186 | return -EINVAL; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 187 | if (domainname) { |
| 188 | if (!tomoyo_is_domain_def(domainname) && |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 189 | tomoyo_is_correct_path(domainname)) |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 190 | e.is_last_name = true; |
Tetsuo Handa | 1708000 | 2010-02-16 21:14:48 +0900 | [diff] [blame] | 191 | else if (!tomoyo_is_correct_domain(domainname)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 192 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 193 | e.domainname = tomoyo_get_name(domainname); |
| 194 | if (!e.domainname) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 195 | goto out; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 196 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 197 | e.program = tomoyo_get_name(program); |
| 198 | if (!e.program) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 199 | goto out; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 200 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 201 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 202 | list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 203 | if (!tomoyo_is_same_domain_initializer_entry(ptr, &e)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 204 | continue; |
| 205 | ptr->is_deleted = is_delete; |
| 206 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 207 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 208 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 209 | if (!is_delete && error) { |
| 210 | struct tomoyo_domain_initializer_entry *entry = |
| 211 | tomoyo_commit_ok(&e, sizeof(e)); |
| 212 | if (entry) { |
| 213 | list_add_tail_rcu(&entry->list, |
| 214 | &tomoyo_domain_initializer_list); |
| 215 | error = 0; |
| 216 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 217 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 218 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 219 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 220 | tomoyo_put_name(e.domainname); |
| 221 | tomoyo_put_name(e.program); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 222 | return error; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list. |
| 227 | * |
| 228 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 229 | * |
| 230 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 231 | * |
| 232 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 233 | */ |
| 234 | bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head) |
| 235 | { |
| 236 | struct list_head *pos; |
| 237 | bool done = true; |
| 238 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 239 | list_for_each_cookie(pos, head->read_var2, |
| 240 | &tomoyo_domain_initializer_list) { |
| 241 | const char *no; |
| 242 | const char *from = ""; |
| 243 | const char *domain = ""; |
| 244 | struct tomoyo_domain_initializer_entry *ptr; |
| 245 | ptr = list_entry(pos, struct tomoyo_domain_initializer_entry, |
| 246 | list); |
| 247 | if (ptr->is_deleted) |
| 248 | continue; |
| 249 | no = ptr->is_not ? "no_" : ""; |
| 250 | if (ptr->domainname) { |
| 251 | from = " from "; |
| 252 | domain = ptr->domainname->name; |
| 253 | } |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 254 | done = tomoyo_io_printf(head, |
| 255 | "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN |
| 256 | "%s%s%s\n", no, ptr->program->name, |
| 257 | from, domain); |
| 258 | if (!done) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 259 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 260 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 261 | return done; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list. |
| 266 | * |
| 267 | * @data: String to parse. |
| 268 | * @is_not: True if it is "no_initialize_domain" entry. |
| 269 | * @is_delete: True if it is a delete request. |
| 270 | * |
| 271 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 272 | * |
| 273 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 274 | */ |
| 275 | int tomoyo_write_domain_initializer_policy(char *data, const bool is_not, |
| 276 | const bool is_delete) |
| 277 | { |
| 278 | char *cp = strstr(data, " from "); |
| 279 | |
| 280 | if (cp) { |
| 281 | *cp = '\0'; |
| 282 | return tomoyo_update_domain_initializer_entry(cp + 6, data, |
| 283 | is_not, |
| 284 | is_delete); |
| 285 | } |
| 286 | return tomoyo_update_domain_initializer_entry(NULL, data, is_not, |
| 287 | is_delete); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization. |
| 292 | * |
| 293 | * @domainname: The name of domain. |
| 294 | * @program: The name of program. |
| 295 | * @last_name: The last component of @domainname. |
| 296 | * |
| 297 | * Returns true if executing @program reinitializes domain transition, |
| 298 | * false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 299 | * |
| 300 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 301 | */ |
| 302 | static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info * |
| 303 | domainname, |
| 304 | const struct tomoyo_path_info *program, |
| 305 | const struct tomoyo_path_info * |
| 306 | last_name) |
| 307 | { |
| 308 | struct tomoyo_domain_initializer_entry *ptr; |
| 309 | bool flag = false; |
| 310 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 311 | list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 312 | if (ptr->is_deleted) |
| 313 | continue; |
| 314 | if (ptr->domainname) { |
| 315 | if (!ptr->is_last_name) { |
| 316 | if (ptr->domainname != domainname) |
| 317 | continue; |
| 318 | } else { |
| 319 | if (tomoyo_pathcmp(ptr->domainname, last_name)) |
| 320 | continue; |
| 321 | } |
| 322 | } |
| 323 | if (tomoyo_pathcmp(ptr->program, program)) |
| 324 | continue; |
| 325 | if (ptr->is_not) { |
| 326 | flag = false; |
| 327 | break; |
| 328 | } |
| 329 | flag = true; |
| 330 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 331 | return flag; |
| 332 | } |
| 333 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 334 | /* |
| 335 | * tomoyo_domain_keeper_list is used for holding list of domainnames which |
| 336 | * suppresses domain transition. Normally, a domainname is monotonically |
| 337 | * getting longer. But sometimes, we want to suppress domain transition. |
| 338 | * It would be convenient for us that programs executed from a login session |
| 339 | * belong to the same domain. Thus, TOMOYO provides a way to suppress domain |
| 340 | * transition. |
| 341 | * |
| 342 | * An entry is added by |
| 343 | * |
| 344 | * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \ |
| 345 | * /sys/kernel/security/tomoyo/exception_policy |
| 346 | * |
| 347 | * and is deleted by |
| 348 | * |
| 349 | * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \ |
| 350 | * /sys/kernel/security/tomoyo/exception_policy |
| 351 | * |
| 352 | * and all entries are retrieved by |
| 353 | * |
| 354 | * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy |
| 355 | * |
| 356 | * In the example above, any process which belongs to |
| 357 | * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain, |
| 358 | * unless explicitly specified by "initialize_domain" or "no_keep_domain". |
| 359 | * |
| 360 | * You may specify a program using "from" keyword. |
| 361 | * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash" |
| 362 | * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash" |
| 363 | * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain. |
| 364 | * |
| 365 | * You may add "no_" prefix to "keep_domain". |
| 366 | * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and |
| 367 | * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will |
| 368 | * cause "/usr/bin/passwd" to belong to |
| 369 | * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless |
| 370 | * explicitly specified by "initialize_domain". |
| 371 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 372 | LIST_HEAD(tomoyo_domain_keeper_list); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list. |
| 376 | * |
| 377 | * @domainname: The name of domain. |
| 378 | * @program: The name of program. May be NULL. |
| 379 | * @is_not: True if it is "no_keep_domain" entry. |
| 380 | * @is_delete: True if it is a delete request. |
| 381 | * |
| 382 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 383 | * |
| 384 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 385 | */ |
| 386 | static int tomoyo_update_domain_keeper_entry(const char *domainname, |
| 387 | const char *program, |
| 388 | const bool is_not, |
| 389 | const bool is_delete) |
| 390 | { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 391 | struct tomoyo_domain_keeper_entry *ptr; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 392 | struct tomoyo_domain_keeper_entry e = { .is_not = is_not }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 393 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 394 | |
| 395 | if (!tomoyo_is_domain_def(domainname) && |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 396 | tomoyo_is_correct_path(domainname)) |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 397 | e.is_last_name = true; |
Tetsuo Handa | 1708000 | 2010-02-16 21:14:48 +0900 | [diff] [blame] | 398 | else if (!tomoyo_is_correct_domain(domainname)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | if (program) { |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 401 | if (!tomoyo_is_correct_path(program)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 402 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 403 | e.program = tomoyo_get_name(program); |
| 404 | if (!e.program) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 405 | goto out; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 406 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 407 | e.domainname = tomoyo_get_name(domainname); |
| 408 | if (!e.domainname) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 409 | goto out; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 410 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 411 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 412 | list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 413 | if (!tomoyo_is_same_domain_keeper_entry(ptr, &e)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 414 | continue; |
| 415 | ptr->is_deleted = is_delete; |
| 416 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 417 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 418 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 419 | if (!is_delete && error) { |
| 420 | struct tomoyo_domain_keeper_entry *entry = |
| 421 | tomoyo_commit_ok(&e, sizeof(e)); |
| 422 | if (entry) { |
| 423 | list_add_tail_rcu(&entry->list, |
| 424 | &tomoyo_domain_keeper_list); |
| 425 | error = 0; |
| 426 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 427 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 428 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 429 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 430 | tomoyo_put_name(e.domainname); |
| 431 | tomoyo_put_name(e.program); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 432 | return error; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list. |
| 437 | * |
| 438 | * @data: String to parse. |
| 439 | * @is_not: True if it is "no_keep_domain" entry. |
| 440 | * @is_delete: True if it is a delete request. |
| 441 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 442 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 443 | */ |
| 444 | int tomoyo_write_domain_keeper_policy(char *data, const bool is_not, |
| 445 | const bool is_delete) |
| 446 | { |
| 447 | char *cp = strstr(data, " from "); |
| 448 | |
| 449 | if (cp) { |
| 450 | *cp = '\0'; |
| 451 | return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not, |
| 452 | is_delete); |
| 453 | } |
| 454 | return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list. |
| 459 | * |
| 460 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 461 | * |
| 462 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 463 | * |
| 464 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 465 | */ |
| 466 | bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head) |
| 467 | { |
| 468 | struct list_head *pos; |
Tetsuo Handa | 33043cb | 2009-02-13 16:00:58 +0900 | [diff] [blame] | 469 | bool done = true; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 470 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 471 | list_for_each_cookie(pos, head->read_var2, |
| 472 | &tomoyo_domain_keeper_list) { |
| 473 | struct tomoyo_domain_keeper_entry *ptr; |
| 474 | const char *no; |
| 475 | const char *from = ""; |
| 476 | const char *program = ""; |
| 477 | |
| 478 | ptr = list_entry(pos, struct tomoyo_domain_keeper_entry, list); |
| 479 | if (ptr->is_deleted) |
| 480 | continue; |
| 481 | no = ptr->is_not ? "no_" : ""; |
| 482 | if (ptr->program) { |
| 483 | from = " from "; |
| 484 | program = ptr->program->name; |
| 485 | } |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 486 | done = tomoyo_io_printf(head, |
| 487 | "%s" TOMOYO_KEYWORD_KEEP_DOMAIN |
| 488 | "%s%s%s\n", no, program, from, |
| 489 | ptr->domainname->name); |
| 490 | if (!done) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 491 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 492 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 493 | return done; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression. |
| 498 | * |
| 499 | * @domainname: The name of domain. |
| 500 | * @program: The name of program. |
| 501 | * @last_name: The last component of @domainname. |
| 502 | * |
| 503 | * Returns true if executing @program supresses domain transition, |
| 504 | * false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 505 | * |
| 506 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 507 | */ |
| 508 | static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname, |
| 509 | const struct tomoyo_path_info *program, |
| 510 | const struct tomoyo_path_info *last_name) |
| 511 | { |
| 512 | struct tomoyo_domain_keeper_entry *ptr; |
| 513 | bool flag = false; |
| 514 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 515 | list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 516 | if (ptr->is_deleted) |
| 517 | continue; |
| 518 | if (!ptr->is_last_name) { |
| 519 | if (ptr->domainname != domainname) |
| 520 | continue; |
| 521 | } else { |
| 522 | if (tomoyo_pathcmp(ptr->domainname, last_name)) |
| 523 | continue; |
| 524 | } |
| 525 | if (ptr->program && tomoyo_pathcmp(ptr->program, program)) |
| 526 | continue; |
| 527 | if (ptr->is_not) { |
| 528 | flag = false; |
| 529 | break; |
| 530 | } |
| 531 | flag = true; |
| 532 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 533 | return flag; |
| 534 | } |
| 535 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 536 | /* |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 537 | * tomoyo_aggregator_list is used for holding list of rewrite table for |
| 538 | * execve() request. Some programs provides similar functionality. This keyword |
| 539 | * allows users to aggregate such programs. |
| 540 | * |
| 541 | * Entries are added by |
| 542 | * |
| 543 | * # echo 'aggregator /usr/bin/vi /./editor' > \ |
| 544 | * /sys/kernel/security/tomoyo/exception_policy |
| 545 | * # echo 'aggregator /usr/bin/emacs /./editor' > \ |
| 546 | * /sys/kernel/security/tomoyo/exception_policy |
| 547 | * |
| 548 | * and are deleted by |
| 549 | * |
| 550 | * # echo 'delete aggregator /usr/bin/vi /./editor' > \ |
| 551 | * /sys/kernel/security/tomoyo/exception_policy |
| 552 | * # echo 'delete aggregator /usr/bin/emacs /./editor' > \ |
| 553 | * /sys/kernel/security/tomoyo/exception_policy |
| 554 | * |
| 555 | * and all entries are retrieved by |
| 556 | * |
| 557 | * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy |
| 558 | * |
| 559 | * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed, |
| 560 | * permission is checked for /./editor and domainname which the current process |
| 561 | * will belong to after execve() succeeds is calculated using /./editor . |
| 562 | */ |
| 563 | LIST_HEAD(tomoyo_aggregator_list); |
| 564 | |
| 565 | /** |
| 566 | * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list. |
| 567 | * |
| 568 | * @original_name: The original program's name. |
| 569 | * @aggregated_name: The program name to use. |
| 570 | * @is_delete: True if it is a delete request. |
| 571 | * |
| 572 | * Returns 0 on success, negative value otherwise. |
| 573 | * |
| 574 | * Caller holds tomoyo_read_lock(). |
| 575 | */ |
| 576 | static int tomoyo_update_aggregator_entry(const char *original_name, |
| 577 | const char *aggregated_name, |
| 578 | const bool is_delete) |
| 579 | { |
| 580 | struct tomoyo_aggregator_entry *ptr; |
| 581 | struct tomoyo_aggregator_entry e = { }; |
| 582 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 583 | |
| 584 | if (!tomoyo_is_correct_path(original_name) || |
| 585 | !tomoyo_is_correct_path(aggregated_name)) |
| 586 | return -EINVAL; |
| 587 | e.original_name = tomoyo_get_name(original_name); |
| 588 | e.aggregated_name = tomoyo_get_name(aggregated_name); |
| 589 | if (!e.original_name || !e.aggregated_name || |
| 590 | e.aggregated_name->is_patterned) /* No patterns allowed. */ |
| 591 | goto out; |
| 592 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 593 | goto out; |
| 594 | list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list, list) { |
| 595 | if (!tomoyo_is_same_aggregator_entry(ptr, &e)) |
| 596 | continue; |
| 597 | ptr->is_deleted = is_delete; |
| 598 | error = 0; |
| 599 | break; |
| 600 | } |
| 601 | if (!is_delete && error) { |
| 602 | struct tomoyo_aggregator_entry *entry = |
| 603 | tomoyo_commit_ok(&e, sizeof(e)); |
| 604 | if (entry) { |
| 605 | list_add_tail_rcu(&entry->list, |
| 606 | &tomoyo_aggregator_list); |
| 607 | error = 0; |
| 608 | } |
| 609 | } |
| 610 | mutex_unlock(&tomoyo_policy_lock); |
| 611 | out: |
| 612 | tomoyo_put_name(e.original_name); |
| 613 | tomoyo_put_name(e.aggregated_name); |
| 614 | return error; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list. |
| 619 | * |
| 620 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 621 | * |
| 622 | * Returns true on success, false otherwise. |
| 623 | * |
| 624 | * Caller holds tomoyo_read_lock(). |
| 625 | */ |
| 626 | bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head) |
| 627 | { |
| 628 | struct list_head *pos; |
| 629 | bool done = true; |
| 630 | |
| 631 | list_for_each_cookie(pos, head->read_var2, &tomoyo_aggregator_list) { |
| 632 | struct tomoyo_aggregator_entry *ptr; |
| 633 | |
| 634 | ptr = list_entry(pos, struct tomoyo_aggregator_entry, list); |
| 635 | if (ptr->is_deleted) |
| 636 | continue; |
| 637 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR |
| 638 | "%s %s\n", ptr->original_name->name, |
| 639 | ptr->aggregated_name->name); |
| 640 | if (!done) |
| 641 | break; |
| 642 | } |
| 643 | return done; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list. |
| 648 | * |
| 649 | * @data: String to parse. |
| 650 | * @is_delete: True if it is a delete request. |
| 651 | * |
| 652 | * Returns 0 on success, negative value otherwise. |
| 653 | * |
| 654 | * Caller holds tomoyo_read_lock(). |
| 655 | */ |
| 656 | int tomoyo_write_aggregator_policy(char *data, const bool is_delete) |
| 657 | { |
| 658 | char *cp = strchr(data, ' '); |
| 659 | |
| 660 | if (!cp) |
| 661 | return -EINVAL; |
| 662 | *cp++ = '\0'; |
| 663 | return tomoyo_update_aggregator_entry(data, cp, is_delete); |
| 664 | } |
| 665 | |
| 666 | /* |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 667 | * tomoyo_alias_list is used for holding list of symlink's pathnames which are |
| 668 | * allowed to be passed to an execve() request. Normally, the domainname which |
| 669 | * the current process will belong to after execve() succeeds is calculated |
| 670 | * using dereferenced pathnames. But some programs behave differently depending |
| 671 | * on the name passed to argv[0]. For busybox, calculating domainname using |
| 672 | * dereferenced pathnames will cause all programs in the busybox to belong to |
| 673 | * the same domain. Thus, TOMOYO provides a way to allow use of symlink's |
| 674 | * pathname for checking execve()'s permission and calculating domainname which |
| 675 | * the current process will belong to after execve() succeeds. |
| 676 | * |
| 677 | * An entry is added by |
| 678 | * |
| 679 | * # echo 'alias /bin/busybox /bin/cat' > \ |
| 680 | * /sys/kernel/security/tomoyo/exception_policy |
| 681 | * |
| 682 | * and is deleted by |
| 683 | * |
| 684 | * # echo 'delete alias /bin/busybox /bin/cat' > \ |
| 685 | * /sys/kernel/security/tomoyo/exception_policy |
| 686 | * |
| 687 | * and all entries are retrieved by |
| 688 | * |
| 689 | * # grep ^alias /sys/kernel/security/tomoyo/exception_policy |
| 690 | * |
| 691 | * In the example above, if /bin/cat is a symlink to /bin/busybox and execution |
| 692 | * of /bin/cat is requested, permission is checked for /bin/cat rather than |
| 693 | * /bin/busybox and domainname which the current process will belong to after |
| 694 | * execve() succeeds is calculated using /bin/cat rather than /bin/busybox . |
| 695 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 696 | LIST_HEAD(tomoyo_alias_list); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 697 | |
| 698 | /** |
| 699 | * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list. |
| 700 | * |
| 701 | * @original_name: The original program's real name. |
| 702 | * @aliased_name: The symbolic program's symbolic link's name. |
| 703 | * @is_delete: True if it is a delete request. |
| 704 | * |
| 705 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 706 | * |
| 707 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 708 | */ |
| 709 | static int tomoyo_update_alias_entry(const char *original_name, |
| 710 | const char *aliased_name, |
| 711 | const bool is_delete) |
| 712 | { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 713 | struct tomoyo_alias_entry *ptr; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 714 | struct tomoyo_alias_entry e = { }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 715 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 716 | |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 717 | if (!tomoyo_is_correct_path(original_name) || |
| 718 | !tomoyo_is_correct_path(aliased_name)) |
| 719 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 720 | e.original_name = tomoyo_get_name(original_name); |
| 721 | e.aliased_name = tomoyo_get_name(aliased_name); |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 722 | if (!e.original_name || !e.aliased_name || |
| 723 | e.original_name->is_patterned || e.aliased_name->is_patterned) |
| 724 | goto out; /* No patterns allowed. */ |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 725 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 726 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 727 | list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 728 | if (!tomoyo_is_same_alias_entry(ptr, &e)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 729 | continue; |
| 730 | ptr->is_deleted = is_delete; |
| 731 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 732 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 733 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 734 | if (!is_delete && error) { |
| 735 | struct tomoyo_alias_entry *entry = |
| 736 | tomoyo_commit_ok(&e, sizeof(e)); |
| 737 | if (entry) { |
| 738 | list_add_tail_rcu(&entry->list, &tomoyo_alias_list); |
| 739 | error = 0; |
| 740 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 741 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 742 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 743 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 744 | tomoyo_put_name(e.original_name); |
| 745 | tomoyo_put_name(e.aliased_name); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 746 | return error; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list. |
| 751 | * |
| 752 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 753 | * |
| 754 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 755 | * |
| 756 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 757 | */ |
| 758 | bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head) |
| 759 | { |
| 760 | struct list_head *pos; |
| 761 | bool done = true; |
| 762 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 763 | list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) { |
| 764 | struct tomoyo_alias_entry *ptr; |
| 765 | |
| 766 | ptr = list_entry(pos, struct tomoyo_alias_entry, list); |
| 767 | if (ptr->is_deleted) |
| 768 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 769 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n", |
| 770 | ptr->original_name->name, |
| 771 | ptr->aliased_name->name); |
| 772 | if (!done) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 773 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 774 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 775 | return done; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list. |
| 780 | * |
| 781 | * @data: String to parse. |
| 782 | * @is_delete: True if it is a delete request. |
| 783 | * |
| 784 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 785 | * |
| 786 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 787 | */ |
| 788 | int tomoyo_write_alias_policy(char *data, const bool is_delete) |
| 789 | { |
| 790 | char *cp = strchr(data, ' '); |
| 791 | |
| 792 | if (!cp) |
| 793 | return -EINVAL; |
| 794 | *cp++ = '\0'; |
| 795 | return tomoyo_update_alias_entry(data, cp, is_delete); |
| 796 | } |
| 797 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 798 | /** |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 799 | * tomoyo_find_or_assign_new_domain - Create a domain. |
| 800 | * |
| 801 | * @domainname: The name of domain. |
| 802 | * @profile: Profile number to assign if the domain was newly created. |
| 803 | * |
| 804 | * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 805 | * |
| 806 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 807 | */ |
| 808 | struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char * |
| 809 | domainname, |
| 810 | const u8 profile) |
| 811 | { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 812 | struct tomoyo_domain_info *entry; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 813 | struct tomoyo_domain_info *domain = NULL; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 814 | const struct tomoyo_path_info *saved_domainname; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 815 | bool found = false; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 816 | |
Tetsuo Handa | 1708000 | 2010-02-16 21:14:48 +0900 | [diff] [blame] | 817 | if (!tomoyo_is_correct_domain(domainname)) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 818 | return NULL; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 819 | saved_domainname = tomoyo_get_name(domainname); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 820 | if (!saved_domainname) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 821 | return NULL; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 822 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 823 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 824 | goto out; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 825 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 826 | if (domain->is_deleted || |
| 827 | tomoyo_pathcmp(saved_domainname, domain->domainname)) |
| 828 | continue; |
| 829 | found = true; |
| 830 | break; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 831 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 832 | if (!found && tomoyo_memory_ok(entry)) { |
| 833 | INIT_LIST_HEAD(&entry->acl_info_list); |
| 834 | entry->domainname = saved_domainname; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 835 | saved_domainname = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 836 | entry->profile = profile; |
| 837 | list_add_tail_rcu(&entry->list, &tomoyo_domain_list); |
| 838 | domain = entry; |
| 839 | entry = NULL; |
| 840 | found = true; |
| 841 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 842 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 843 | out: |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 844 | tomoyo_put_name(saved_domainname); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 845 | kfree(entry); |
| 846 | return found ? domain : NULL; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * tomoyo_find_next_domain - Find a domain. |
| 851 | * |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 852 | * @bprm: Pointer to "struct linux_binprm". |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 853 | * |
| 854 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 855 | * |
| 856 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 857 | */ |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 858 | int tomoyo_find_next_domain(struct linux_binprm *bprm) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 859 | { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 860 | struct tomoyo_request_info r; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 861 | char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 862 | struct tomoyo_domain_info *old_domain = tomoyo_domain(); |
| 863 | struct tomoyo_domain_info *domain = NULL; |
| 864 | const char *old_domain_name = old_domain->domainname->name; |
| 865 | const char *original_name = bprm->filename; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 866 | u8 mode; |
| 867 | bool is_enforce; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 868 | int retval = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 869 | bool need_kfree = false; |
| 870 | struct tomoyo_path_info rn = { }; /* real name */ |
| 871 | struct tomoyo_path_info sn = { }; /* symlink name */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 872 | struct tomoyo_path_info ln; /* last name */ |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 873 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 874 | ln.name = tomoyo_get_last_name(old_domain); |
| 875 | tomoyo_fill_path_info(&ln); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 876 | mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE); |
| 877 | is_enforce = (mode == TOMOYO_CONFIG_ENFORCING); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 878 | if (!tmp) |
| 879 | goto out; |
| 880 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 881 | retry: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 882 | if (need_kfree) { |
| 883 | kfree(rn.name); |
| 884 | need_kfree = false; |
| 885 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 886 | /* Get tomoyo_realpath of program. */ |
| 887 | retval = -ENOENT; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 888 | rn.name = tomoyo_realpath(original_name); |
| 889 | if (!rn.name) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 890 | goto out; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 891 | tomoyo_fill_path_info(&rn); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 892 | need_kfree = true; |
| 893 | |
| 894 | /* Get tomoyo_realpath of symbolic link. */ |
| 895 | sn.name = tomoyo_realpath_nofollow(original_name); |
| 896 | if (!sn.name) |
| 897 | goto out; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 898 | tomoyo_fill_path_info(&sn); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 899 | |
| 900 | /* Check 'alias' directive. */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 901 | if (tomoyo_pathcmp(&rn, &sn)) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 902 | struct tomoyo_alias_entry *ptr; |
| 903 | /* Is this program allowed to be called via symbolic links? */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 904 | list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 905 | if (ptr->is_deleted || |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 906 | tomoyo_pathcmp(&rn, ptr->original_name) || |
| 907 | tomoyo_pathcmp(&sn, ptr->aliased_name)) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 908 | continue; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 909 | kfree(rn.name); |
| 910 | need_kfree = false; |
| 911 | /* This is OK because it is read only. */ |
| 912 | rn = *ptr->aliased_name; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 913 | break; |
| 914 | } |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 915 | } |
| 916 | |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 917 | /* Check 'aggregator' directive. */ |
| 918 | { |
| 919 | struct tomoyo_aggregator_entry *ptr; |
| 920 | list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list, list) { |
| 921 | if (ptr->is_deleted || |
| 922 | !tomoyo_path_matches_pattern(&rn, |
| 923 | ptr->original_name)) |
| 924 | continue; |
| 925 | if (need_kfree) |
| 926 | kfree(rn.name); |
| 927 | need_kfree = false; |
| 928 | /* This is OK because it is read only. */ |
| 929 | rn = *ptr->aggregated_name; |
| 930 | break; |
| 931 | } |
| 932 | } |
| 933 | |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 934 | /* Check execute permission. */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 935 | retval = tomoyo_check_exec_perm(&r, &rn); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 936 | if (retval == TOMOYO_RETRY_REQUEST) |
| 937 | goto retry; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 938 | if (retval < 0) |
| 939 | goto out; |
| 940 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 941 | if (tomoyo_is_domain_initializer(old_domain->domainname, &rn, &ln)) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 942 | /* Transit to the child of tomoyo_kernel_domain domain. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 943 | snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, |
| 944 | TOMOYO_ROOT_NAME " " "%s", rn.name); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 945 | } else if (old_domain == &tomoyo_kernel_domain && |
| 946 | !tomoyo_policy_loaded) { |
| 947 | /* |
| 948 | * Needn't to transit from kernel domain before starting |
| 949 | * /sbin/init. But transit from kernel domain if executing |
| 950 | * initializers because they might start before /sbin/init. |
| 951 | */ |
| 952 | domain = old_domain; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 953 | } else if (tomoyo_is_domain_keeper(old_domain->domainname, &rn, &ln)) { |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 954 | /* Keep current domain. */ |
| 955 | domain = old_domain; |
| 956 | } else { |
| 957 | /* Normal domain transition. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 958 | snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, |
| 959 | "%s %s", old_domain_name, rn.name); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 960 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 961 | if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10) |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 962 | goto done; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 963 | domain = tomoyo_find_domain(tmp); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 964 | if (domain) |
| 965 | goto done; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 966 | if (is_enforce) { |
| 967 | int error = tomoyo_supervisor(&r, "# wants to create domain\n" |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 968 | "%s\n", tmp); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 969 | if (error == TOMOYO_RETRY_REQUEST) |
| 970 | goto retry; |
| 971 | if (error < 0) |
| 972 | goto done; |
| 973 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 974 | domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 975 | done: |
| 976 | if (domain) |
| 977 | goto out; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 978 | printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 979 | if (is_enforce) |
| 980 | retval = -EPERM; |
| 981 | else |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 982 | old_domain->transition_failed = true; |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 983 | out: |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 984 | if (!domain) |
| 985 | domain = old_domain; |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 986 | /* Update reference count on "struct tomoyo_domain_info". */ |
| 987 | atomic_inc(&domain->users); |
Tetsuo Handa | 56f8c9bc | 2009-06-19 14:13:27 +0900 | [diff] [blame] | 988 | bprm->cred->security = domain; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 989 | if (need_kfree) |
| 990 | kfree(rn.name); |
| 991 | kfree(sn.name); |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 992 | kfree(tmp); |
Kentaro Takeda | 26a2a1c | 2009-02-05 17:18:15 +0900 | [diff] [blame] | 993 | return retval; |
| 994 | } |