Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/file.c |
| 3 | * |
| 4 | * Implementation of the Domain-Based Mandatory Access Control. |
| 5 | * |
| 6 | * Copyright (C) 2005-2009 NTT DATA CORPORATION |
| 7 | * |
Tetsuo Handa | 39826a1 | 2009-04-08 22:31:28 +0900 | [diff] [blame] | 8 | * Version: 2.2.0 2009/04/01 |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "tomoyo.h" |
| 14 | #include "realpath.h" |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 15 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 16 | /* |
| 17 | * tomoyo_globally_readable_file_entry is a structure which is used for holding |
| 18 | * "allow_read" entries. |
| 19 | * It has following fields. |
| 20 | * |
| 21 | * (1) "list" which is linked to tomoyo_globally_readable_list . |
| 22 | * (2) "filename" is a pathname which is allowed to open(O_RDONLY). |
| 23 | * (3) "is_deleted" is a bool which is true if marked as deleted, false |
| 24 | * otherwise. |
| 25 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 26 | struct tomoyo_globally_readable_file_entry { |
| 27 | struct list_head list; |
| 28 | const struct tomoyo_path_info *filename; |
| 29 | bool is_deleted; |
| 30 | }; |
| 31 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 32 | /* |
| 33 | * tomoyo_pattern_entry is a structure which is used for holding |
| 34 | * "tomoyo_pattern_list" entries. |
| 35 | * It has following fields. |
| 36 | * |
| 37 | * (1) "list" which is linked to tomoyo_pattern_list . |
| 38 | * (2) "pattern" is a pathname pattern which is used for converting pathnames |
| 39 | * to pathname patterns during learning mode. |
| 40 | * (3) "is_deleted" is a bool which is true if marked as deleted, false |
| 41 | * otherwise. |
| 42 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 43 | struct tomoyo_pattern_entry { |
| 44 | struct list_head list; |
| 45 | const struct tomoyo_path_info *pattern; |
| 46 | bool is_deleted; |
| 47 | }; |
| 48 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 49 | /* |
| 50 | * tomoyo_no_rewrite_entry is a structure which is used for holding |
| 51 | * "deny_rewrite" entries. |
| 52 | * It has following fields. |
| 53 | * |
| 54 | * (1) "list" which is linked to tomoyo_no_rewrite_list . |
| 55 | * (2) "pattern" is a pathname which is by default not permitted to modify |
| 56 | * already existing content. |
| 57 | * (3) "is_deleted" is a bool which is true if marked as deleted, false |
| 58 | * otherwise. |
| 59 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 60 | struct tomoyo_no_rewrite_entry { |
| 61 | struct list_head list; |
| 62 | const struct tomoyo_path_info *pattern; |
| 63 | bool is_deleted; |
| 64 | }; |
| 65 | |
| 66 | /* Keyword array for single path operations. */ |
| 67 | static const char *tomoyo_sp_keyword[TOMOYO_MAX_SINGLE_PATH_OPERATION] = { |
| 68 | [TOMOYO_TYPE_READ_WRITE_ACL] = "read/write", |
| 69 | [TOMOYO_TYPE_EXECUTE_ACL] = "execute", |
| 70 | [TOMOYO_TYPE_READ_ACL] = "read", |
| 71 | [TOMOYO_TYPE_WRITE_ACL] = "write", |
| 72 | [TOMOYO_TYPE_CREATE_ACL] = "create", |
| 73 | [TOMOYO_TYPE_UNLINK_ACL] = "unlink", |
| 74 | [TOMOYO_TYPE_MKDIR_ACL] = "mkdir", |
| 75 | [TOMOYO_TYPE_RMDIR_ACL] = "rmdir", |
| 76 | [TOMOYO_TYPE_MKFIFO_ACL] = "mkfifo", |
| 77 | [TOMOYO_TYPE_MKSOCK_ACL] = "mksock", |
| 78 | [TOMOYO_TYPE_MKBLOCK_ACL] = "mkblock", |
| 79 | [TOMOYO_TYPE_MKCHAR_ACL] = "mkchar", |
| 80 | [TOMOYO_TYPE_TRUNCATE_ACL] = "truncate", |
| 81 | [TOMOYO_TYPE_SYMLINK_ACL] = "symlink", |
| 82 | [TOMOYO_TYPE_REWRITE_ACL] = "rewrite", |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 83 | [TOMOYO_TYPE_IOCTL_ACL] = "ioctl", |
| 84 | [TOMOYO_TYPE_CHMOD_ACL] = "chmod", |
| 85 | [TOMOYO_TYPE_CHOWN_ACL] = "chown", |
| 86 | [TOMOYO_TYPE_CHGRP_ACL] = "chgrp", |
| 87 | [TOMOYO_TYPE_CHROOT_ACL] = "chroot", |
| 88 | [TOMOYO_TYPE_MOUNT_ACL] = "mount", |
| 89 | [TOMOYO_TYPE_UMOUNT_ACL] = "unmount", |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /* Keyword array for double path operations. */ |
| 93 | static const char *tomoyo_dp_keyword[TOMOYO_MAX_DOUBLE_PATH_OPERATION] = { |
| 94 | [TOMOYO_TYPE_LINK_ACL] = "link", |
| 95 | [TOMOYO_TYPE_RENAME_ACL] = "rename", |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 96 | [TOMOYO_TYPE_PIVOT_ROOT_ACL] = "pivot_root", |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * tomoyo_sp2keyword - Get the name of single path operation. |
| 101 | * |
| 102 | * @operation: Type of operation. |
| 103 | * |
| 104 | * Returns the name of single path operation. |
| 105 | */ |
| 106 | const char *tomoyo_sp2keyword(const u8 operation) |
| 107 | { |
| 108 | return (operation < TOMOYO_MAX_SINGLE_PATH_OPERATION) |
| 109 | ? tomoyo_sp_keyword[operation] : NULL; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * tomoyo_dp2keyword - Get the name of double path operation. |
| 114 | * |
| 115 | * @operation: Type of operation. |
| 116 | * |
| 117 | * Returns the name of double path operation. |
| 118 | */ |
| 119 | const char *tomoyo_dp2keyword(const u8 operation) |
| 120 | { |
| 121 | return (operation < TOMOYO_MAX_DOUBLE_PATH_OPERATION) |
| 122 | ? tomoyo_dp_keyword[operation] : NULL; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * tomoyo_strendswith - Check whether the token ends with the given token. |
| 127 | * |
| 128 | * @name: The token to check. |
| 129 | * @tail: The token to find. |
| 130 | * |
| 131 | * Returns true if @name ends with @tail, false otherwise. |
| 132 | */ |
| 133 | static bool tomoyo_strendswith(const char *name, const char *tail) |
| 134 | { |
| 135 | int len; |
| 136 | |
| 137 | if (!name || !tail) |
| 138 | return false; |
| 139 | len = strlen(name) - strlen(tail); |
| 140 | return len >= 0 && !strcmp(name + len, tail); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * tomoyo_get_path - Get realpath. |
| 145 | * |
| 146 | * @path: Pointer to "struct path". |
| 147 | * |
| 148 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. |
| 149 | */ |
| 150 | static struct tomoyo_path_info *tomoyo_get_path(struct path *path) |
| 151 | { |
| 152 | int error; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 153 | struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf), |
| 154 | GFP_KERNEL); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 155 | |
| 156 | if (!buf) |
| 157 | return NULL; |
| 158 | /* Reserve one byte for appending "/". */ |
| 159 | error = tomoyo_realpath_from_path2(path, buf->body, |
| 160 | sizeof(buf->body) - 2); |
| 161 | if (!error) { |
| 162 | buf->head.name = buf->body; |
| 163 | tomoyo_fill_path_info(&buf->head); |
| 164 | return &buf->head; |
| 165 | } |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 166 | kfree(buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 167 | return NULL; |
| 168 | } |
| 169 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 170 | static int tomoyo_update_double_path_acl(const u8 type, const char *filename1, |
| 171 | const char *filename2, |
| 172 | struct tomoyo_domain_info * |
| 173 | const domain, const bool is_delete); |
| 174 | static int tomoyo_update_single_path_acl(const u8 type, const char *filename, |
| 175 | struct tomoyo_domain_info * |
| 176 | const domain, const bool is_delete); |
| 177 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 178 | /* |
| 179 | * tomoyo_globally_readable_list is used for holding list of pathnames which |
| 180 | * are by default allowed to be open()ed for reading by any process. |
| 181 | * |
| 182 | * An entry is added by |
| 183 | * |
| 184 | * # echo 'allow_read /lib/libc-2.5.so' > \ |
| 185 | * /sys/kernel/security/tomoyo/exception_policy |
| 186 | * |
| 187 | * and is deleted by |
| 188 | * |
| 189 | * # echo 'delete allow_read /lib/libc-2.5.so' > \ |
| 190 | * /sys/kernel/security/tomoyo/exception_policy |
| 191 | * |
| 192 | * and all entries are retrieved by |
| 193 | * |
| 194 | * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy |
| 195 | * |
| 196 | * In the example above, any process is allowed to |
| 197 | * open("/lib/libc-2.5.so", O_RDONLY). |
| 198 | * One exception is, if the domain which current process belongs to is marked |
| 199 | * as "ignore_global_allow_read", current process can't do so unless explicitly |
| 200 | * given "allow_read /lib/libc-2.5.so" to the domain which current process |
| 201 | * belongs to. |
| 202 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 203 | static LIST_HEAD(tomoyo_globally_readable_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list. |
| 207 | * |
| 208 | * @filename: Filename unconditionally permitted to open() for reading. |
| 209 | * @is_delete: True if it is a delete request. |
| 210 | * |
| 211 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 212 | * |
| 213 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 214 | */ |
| 215 | static int tomoyo_update_globally_readable_entry(const char *filename, |
| 216 | const bool is_delete) |
| 217 | { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 218 | struct tomoyo_globally_readable_file_entry *entry = NULL; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 219 | struct tomoyo_globally_readable_file_entry *ptr; |
| 220 | const struct tomoyo_path_info *saved_filename; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 221 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 222 | |
| 223 | if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__)) |
| 224 | return -EINVAL; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 225 | saved_filename = tomoyo_get_name(filename); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 226 | if (!saved_filename) |
| 227 | return -ENOMEM; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 228 | if (!is_delete) |
| 229 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 230 | mutex_lock(&tomoyo_policy_lock); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 231 | list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 232 | if (ptr->filename != saved_filename) |
| 233 | continue; |
| 234 | ptr->is_deleted = is_delete; |
| 235 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 236 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 237 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 238 | if (!is_delete && error && tomoyo_memory_ok(entry)) { |
| 239 | entry->filename = saved_filename; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 240 | saved_filename = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 241 | list_add_tail_rcu(&entry->list, &tomoyo_globally_readable_list); |
| 242 | entry = NULL; |
| 243 | error = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 244 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 245 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 246 | tomoyo_put_name(saved_filename); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 247 | kfree(entry); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 248 | return error; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading. |
| 253 | * |
| 254 | * @filename: The filename to check. |
| 255 | * |
| 256 | * Returns true if any domain can open @filename for reading, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 257 | * |
| 258 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 259 | */ |
| 260 | static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info * |
| 261 | filename) |
| 262 | { |
| 263 | struct tomoyo_globally_readable_file_entry *ptr; |
| 264 | bool found = false; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 265 | |
| 266 | list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 267 | if (!ptr->is_deleted && |
| 268 | tomoyo_path_matches_pattern(filename, ptr->filename)) { |
| 269 | found = true; |
| 270 | break; |
| 271 | } |
| 272 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 273 | return found; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list. |
| 278 | * |
| 279 | * @data: String to parse. |
| 280 | * @is_delete: True if it is a delete request. |
| 281 | * |
| 282 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 283 | * |
| 284 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 285 | */ |
| 286 | int tomoyo_write_globally_readable_policy(char *data, const bool is_delete) |
| 287 | { |
| 288 | return tomoyo_update_globally_readable_entry(data, is_delete); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list. |
| 293 | * |
| 294 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 295 | * |
| 296 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 297 | * |
| 298 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 299 | */ |
| 300 | bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head) |
| 301 | { |
| 302 | struct list_head *pos; |
| 303 | bool done = true; |
| 304 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 305 | list_for_each_cookie(pos, head->read_var2, |
| 306 | &tomoyo_globally_readable_list) { |
| 307 | struct tomoyo_globally_readable_file_entry *ptr; |
| 308 | ptr = list_entry(pos, |
| 309 | struct tomoyo_globally_readable_file_entry, |
| 310 | list); |
| 311 | if (ptr->is_deleted) |
| 312 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 313 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n", |
| 314 | ptr->filename->name); |
| 315 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 316 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 317 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 318 | return done; |
| 319 | } |
| 320 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 321 | /* tomoyo_pattern_list is used for holding list of pathnames which are used for |
| 322 | * converting pathnames to pathname patterns during learning mode. |
| 323 | * |
| 324 | * An entry is added by |
| 325 | * |
| 326 | * # echo 'file_pattern /proc/\$/mounts' > \ |
| 327 | * /sys/kernel/security/tomoyo/exception_policy |
| 328 | * |
| 329 | * and is deleted by |
| 330 | * |
| 331 | * # echo 'delete file_pattern /proc/\$/mounts' > \ |
| 332 | * /sys/kernel/security/tomoyo/exception_policy |
| 333 | * |
| 334 | * and all entries are retrieved by |
| 335 | * |
| 336 | * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy |
| 337 | * |
| 338 | * In the example above, if a process which belongs to a domain which is in |
| 339 | * learning mode requested open("/proc/1/mounts", O_RDONLY), |
| 340 | * "allow_read /proc/\$/mounts" is automatically added to the domain which that |
| 341 | * process belongs to. |
| 342 | * |
| 343 | * It is not a desirable behavior that we have to use /proc/\$/ instead of |
| 344 | * /proc/self/ when current process needs to access only current process's |
| 345 | * information. As of now, LSM version of TOMOYO is using __d_path() for |
| 346 | * calculating pathname. Non LSM version of TOMOYO is using its own function |
| 347 | * which pretends as if /proc/self/ is not a symlink; so that we can forbid |
| 348 | * current process from accessing other process's information. |
| 349 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 350 | static LIST_HEAD(tomoyo_pattern_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list. |
| 354 | * |
| 355 | * @pattern: Pathname pattern. |
| 356 | * @is_delete: True if it is a delete request. |
| 357 | * |
| 358 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 359 | * |
| 360 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 361 | */ |
| 362 | static int tomoyo_update_file_pattern_entry(const char *pattern, |
| 363 | const bool is_delete) |
| 364 | { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 365 | struct tomoyo_pattern_entry *entry = NULL; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 366 | struct tomoyo_pattern_entry *ptr; |
| 367 | const struct tomoyo_path_info *saved_pattern; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 368 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 369 | |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 370 | saved_pattern = tomoyo_get_name(pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 371 | if (!saved_pattern) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 372 | return error; |
| 373 | if (!saved_pattern->is_patterned) |
| 374 | goto out; |
| 375 | if (!is_delete) |
| 376 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 377 | mutex_lock(&tomoyo_policy_lock); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 378 | list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 379 | if (saved_pattern != ptr->pattern) |
| 380 | continue; |
| 381 | ptr->is_deleted = is_delete; |
| 382 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 383 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 384 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 385 | if (!is_delete && error && tomoyo_memory_ok(entry)) { |
| 386 | entry->pattern = saved_pattern; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 387 | saved_pattern = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 388 | list_add_tail_rcu(&entry->list, &tomoyo_pattern_list); |
| 389 | entry = NULL; |
| 390 | error = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 391 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 392 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 393 | out: |
| 394 | kfree(entry); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 395 | tomoyo_put_name(saved_pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 396 | return error; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * tomoyo_get_file_pattern - Get patterned pathname. |
| 401 | * |
| 402 | * @filename: The filename to find patterned pathname. |
| 403 | * |
| 404 | * Returns pointer to pathname pattern if matched, @filename otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 405 | * |
| 406 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 407 | */ |
| 408 | static const struct tomoyo_path_info * |
| 409 | tomoyo_get_file_pattern(const struct tomoyo_path_info *filename) |
| 410 | { |
| 411 | struct tomoyo_pattern_entry *ptr; |
| 412 | const struct tomoyo_path_info *pattern = NULL; |
| 413 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 414 | list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 415 | if (ptr->is_deleted) |
| 416 | continue; |
| 417 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 418 | continue; |
| 419 | pattern = ptr->pattern; |
| 420 | if (tomoyo_strendswith(pattern->name, "/\\*")) { |
| 421 | /* Do nothing. Try to find the better match. */ |
| 422 | } else { |
| 423 | /* This would be the better match. Use this. */ |
| 424 | break; |
| 425 | } |
| 426 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 427 | if (pattern) |
| 428 | filename = pattern; |
| 429 | return filename; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list. |
| 434 | * |
| 435 | * @data: String to parse. |
| 436 | * @is_delete: True if it is a delete request. |
| 437 | * |
| 438 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 439 | * |
| 440 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 441 | */ |
| 442 | int tomoyo_write_pattern_policy(char *data, const bool is_delete) |
| 443 | { |
| 444 | return tomoyo_update_file_pattern_entry(data, is_delete); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list. |
| 449 | * |
| 450 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 451 | * |
| 452 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 453 | * |
| 454 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 455 | */ |
| 456 | bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head) |
| 457 | { |
| 458 | struct list_head *pos; |
| 459 | bool done = true; |
| 460 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 461 | list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) { |
| 462 | struct tomoyo_pattern_entry *ptr; |
| 463 | ptr = list_entry(pos, struct tomoyo_pattern_entry, list); |
| 464 | if (ptr->is_deleted) |
| 465 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 466 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN |
| 467 | "%s\n", ptr->pattern->name); |
| 468 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 469 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 470 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 471 | return done; |
| 472 | } |
| 473 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 474 | /* |
| 475 | * tomoyo_no_rewrite_list is used for holding list of pathnames which are by |
| 476 | * default forbidden to modify already written content of a file. |
| 477 | * |
| 478 | * An entry is added by |
| 479 | * |
| 480 | * # echo 'deny_rewrite /var/log/messages' > \ |
| 481 | * /sys/kernel/security/tomoyo/exception_policy |
| 482 | * |
| 483 | * and is deleted by |
| 484 | * |
| 485 | * # echo 'delete deny_rewrite /var/log/messages' > \ |
| 486 | * /sys/kernel/security/tomoyo/exception_policy |
| 487 | * |
| 488 | * and all entries are retrieved by |
| 489 | * |
| 490 | * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy |
| 491 | * |
| 492 | * In the example above, if a process requested to rewrite /var/log/messages , |
| 493 | * the process can't rewrite unless the domain which that process belongs to |
| 494 | * has "allow_rewrite /var/log/messages" entry. |
| 495 | * |
| 496 | * It is not a desirable behavior that we have to add "\040(deleted)" suffix |
| 497 | * when we want to allow rewriting already unlink()ed file. As of now, |
| 498 | * LSM version of TOMOYO is using __d_path() for calculating pathname. |
| 499 | * Non LSM version of TOMOYO is using its own function which doesn't append |
| 500 | * " (deleted)" suffix if the file is already unlink()ed; so that we don't |
| 501 | * need to worry whether the file is already unlink()ed or not. |
| 502 | */ |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 503 | static LIST_HEAD(tomoyo_no_rewrite_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 504 | |
| 505 | /** |
| 506 | * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list. |
| 507 | * |
| 508 | * @pattern: Pathname pattern that are not rewritable by default. |
| 509 | * @is_delete: True if it is a delete request. |
| 510 | * |
| 511 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 512 | * |
| 513 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 514 | */ |
| 515 | static int tomoyo_update_no_rewrite_entry(const char *pattern, |
| 516 | const bool is_delete) |
| 517 | { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 518 | struct tomoyo_no_rewrite_entry *entry = NULL; |
| 519 | struct tomoyo_no_rewrite_entry *ptr; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 520 | const struct tomoyo_path_info *saved_pattern; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 521 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 522 | |
| 523 | if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__)) |
| 524 | return -EINVAL; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 525 | saved_pattern = tomoyo_get_name(pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 526 | if (!saved_pattern) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 527 | return error; |
| 528 | if (!is_delete) |
| 529 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 530 | mutex_lock(&tomoyo_policy_lock); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 531 | list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 532 | if (ptr->pattern != saved_pattern) |
| 533 | continue; |
| 534 | ptr->is_deleted = is_delete; |
| 535 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 536 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 537 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 538 | if (!is_delete && error && tomoyo_memory_ok(entry)) { |
| 539 | entry->pattern = saved_pattern; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 540 | saved_pattern = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 541 | list_add_tail_rcu(&entry->list, &tomoyo_no_rewrite_list); |
| 542 | entry = NULL; |
| 543 | error = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 544 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 545 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 546 | tomoyo_put_name(saved_pattern); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 547 | kfree(entry); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 548 | return error; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited. |
| 553 | * |
| 554 | * @filename: Filename to check. |
| 555 | * |
| 556 | * Returns true if @filename is specified by "deny_rewrite" directive, |
| 557 | * false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 558 | * |
| 559 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 560 | */ |
| 561 | static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename) |
| 562 | { |
| 563 | struct tomoyo_no_rewrite_entry *ptr; |
| 564 | bool found = false; |
| 565 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 566 | list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 567 | if (ptr->is_deleted) |
| 568 | continue; |
| 569 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 570 | continue; |
| 571 | found = true; |
| 572 | break; |
| 573 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 574 | return found; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list. |
| 579 | * |
| 580 | * @data: String to parse. |
| 581 | * @is_delete: True if it is a delete request. |
| 582 | * |
| 583 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 584 | * |
| 585 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 586 | */ |
| 587 | int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete) |
| 588 | { |
| 589 | return tomoyo_update_no_rewrite_entry(data, is_delete); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list. |
| 594 | * |
| 595 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 596 | * |
| 597 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 598 | * |
| 599 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 600 | */ |
| 601 | bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head) |
| 602 | { |
| 603 | struct list_head *pos; |
| 604 | bool done = true; |
| 605 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 606 | list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) { |
| 607 | struct tomoyo_no_rewrite_entry *ptr; |
| 608 | ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list); |
| 609 | if (ptr->is_deleted) |
| 610 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 611 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE |
| 612 | "%s\n", ptr->pattern->name); |
| 613 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 614 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 615 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 616 | return done; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * tomoyo_update_file_acl - Update file's read/write/execute ACL. |
| 621 | * |
| 622 | * @filename: Filename. |
| 623 | * @perm: Permission (between 1 to 7). |
| 624 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 625 | * @is_delete: True if it is a delete request. |
| 626 | * |
| 627 | * Returns 0 on success, negative value otherwise. |
| 628 | * |
| 629 | * This is legacy support interface for older policy syntax. |
| 630 | * Current policy syntax uses "allow_read/write" instead of "6", |
| 631 | * "allow_read" instead of "4", "allow_write" instead of "2", |
| 632 | * "allow_execute" instead of "1". |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 633 | * |
| 634 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 635 | */ |
| 636 | static int tomoyo_update_file_acl(const char *filename, u8 perm, |
| 637 | struct tomoyo_domain_info * const domain, |
| 638 | const bool is_delete) |
| 639 | { |
| 640 | if (perm > 7 || !perm) { |
| 641 | printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n", |
| 642 | __func__, perm, filename); |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | if (filename[0] != '@' && tomoyo_strendswith(filename, "/")) |
| 646 | /* |
| 647 | * Only 'allow_mkdir' and 'allow_rmdir' are valid for |
| 648 | * directory permissions. |
| 649 | */ |
| 650 | return 0; |
| 651 | if (perm & 4) |
| 652 | tomoyo_update_single_path_acl(TOMOYO_TYPE_READ_ACL, filename, |
| 653 | domain, is_delete); |
| 654 | if (perm & 2) |
| 655 | tomoyo_update_single_path_acl(TOMOYO_TYPE_WRITE_ACL, filename, |
| 656 | domain, is_delete); |
| 657 | if (perm & 1) |
| 658 | tomoyo_update_single_path_acl(TOMOYO_TYPE_EXECUTE_ACL, |
| 659 | filename, domain, is_delete); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * tomoyo_check_single_path_acl2 - Check permission for single path operation. |
| 665 | * |
| 666 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 667 | * @filename: Filename to check. |
| 668 | * @perm: Permission. |
| 669 | * @may_use_pattern: True if patterned ACL is permitted. |
| 670 | * |
| 671 | * Returns 0 on success, -EPERM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 672 | * |
| 673 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 674 | */ |
| 675 | static int tomoyo_check_single_path_acl2(const struct tomoyo_domain_info * |
| 676 | domain, |
| 677 | const struct tomoyo_path_info * |
| 678 | filename, |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 679 | const u32 perm, |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 680 | const bool may_use_pattern) |
| 681 | { |
| 682 | struct tomoyo_acl_info *ptr; |
| 683 | int error = -EPERM; |
| 684 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 685 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 686 | struct tomoyo_single_path_acl_record *acl; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 687 | if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 688 | continue; |
| 689 | acl = container_of(ptr, struct tomoyo_single_path_acl_record, |
| 690 | head); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 691 | if (perm <= 0xFFFF) { |
| 692 | if (!(acl->perm & perm)) |
| 693 | continue; |
| 694 | } else { |
| 695 | if (!(acl->perm_high & (perm >> 16))) |
| 696 | continue; |
| 697 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 698 | if (may_use_pattern || !acl->filename->is_patterned) { |
| 699 | if (!tomoyo_path_matches_pattern(filename, |
| 700 | acl->filename)) |
| 701 | continue; |
| 702 | } else { |
| 703 | continue; |
| 704 | } |
| 705 | error = 0; |
| 706 | break; |
| 707 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 708 | return error; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * tomoyo_check_file_acl - Check permission for opening files. |
| 713 | * |
| 714 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 715 | * @filename: Filename to check. |
| 716 | * @operation: Mode ("read" or "write" or "read/write" or "execute"). |
| 717 | * |
| 718 | * Returns 0 on success, -EPERM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 719 | * |
| 720 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 721 | */ |
| 722 | static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain, |
| 723 | const struct tomoyo_path_info *filename, |
| 724 | const u8 operation) |
| 725 | { |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 726 | u32 perm = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 727 | |
| 728 | if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE)) |
| 729 | return 0; |
| 730 | if (operation == 6) |
| 731 | perm = 1 << TOMOYO_TYPE_READ_WRITE_ACL; |
| 732 | else if (operation == 4) |
| 733 | perm = 1 << TOMOYO_TYPE_READ_ACL; |
| 734 | else if (operation == 2) |
| 735 | perm = 1 << TOMOYO_TYPE_WRITE_ACL; |
| 736 | else if (operation == 1) |
| 737 | perm = 1 << TOMOYO_TYPE_EXECUTE_ACL; |
| 738 | else |
| 739 | BUG(); |
| 740 | return tomoyo_check_single_path_acl2(domain, filename, perm, |
| 741 | operation != 1); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * tomoyo_check_file_perm2 - Check permission for opening files. |
| 746 | * |
| 747 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 748 | * @filename: Filename to check. |
| 749 | * @perm: Mode ("read" or "write" or "read/write" or "execute"). |
| 750 | * @operation: Operation name passed used for verbose mode. |
| 751 | * @mode: Access control mode. |
| 752 | * |
| 753 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 754 | * |
| 755 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 756 | */ |
| 757 | static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain, |
| 758 | const struct tomoyo_path_info *filename, |
| 759 | const u8 perm, const char *operation, |
| 760 | const u8 mode) |
| 761 | { |
| 762 | const bool is_enforce = (mode == 3); |
| 763 | const char *msg = "<unknown>"; |
| 764 | int error = 0; |
| 765 | |
| 766 | if (!filename) |
| 767 | return 0; |
| 768 | error = tomoyo_check_file_acl(domain, filename, perm); |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 769 | if (error && perm == 4 && !domain->ignore_global_allow_read |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 770 | && tomoyo_is_globally_readable_file(filename)) |
| 771 | error = 0; |
| 772 | if (perm == 6) |
| 773 | msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_WRITE_ACL); |
| 774 | else if (perm == 4) |
| 775 | msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_ACL); |
| 776 | else if (perm == 2) |
| 777 | msg = tomoyo_sp2keyword(TOMOYO_TYPE_WRITE_ACL); |
| 778 | else if (perm == 1) |
| 779 | msg = tomoyo_sp2keyword(TOMOYO_TYPE_EXECUTE_ACL); |
| 780 | else |
| 781 | BUG(); |
| 782 | if (!error) |
| 783 | return 0; |
| 784 | if (tomoyo_verbose_mode(domain)) |
| 785 | printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied " |
| 786 | "for %s\n", tomoyo_get_msg(is_enforce), msg, operation, |
| 787 | filename->name, tomoyo_get_last_name(domain)); |
| 788 | if (is_enforce) |
| 789 | return error; |
| 790 | if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) { |
| 791 | /* Don't use patterns for execute permission. */ |
| 792 | const struct tomoyo_path_info *patterned_file = (perm != 1) ? |
| 793 | tomoyo_get_file_pattern(filename) : filename; |
| 794 | tomoyo_update_file_acl(patterned_file->name, perm, |
| 795 | domain, false); |
| 796 | } |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * tomoyo_write_file_policy - Update file related list. |
| 802 | * |
| 803 | * @data: String to parse. |
| 804 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 805 | * @is_delete: True if it is a delete request. |
| 806 | * |
| 807 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 808 | * |
| 809 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 810 | */ |
| 811 | int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain, |
| 812 | const bool is_delete) |
| 813 | { |
| 814 | char *filename = strchr(data, ' '); |
| 815 | char *filename2; |
| 816 | unsigned int perm; |
| 817 | u8 type; |
| 818 | |
| 819 | if (!filename) |
| 820 | return -EINVAL; |
| 821 | *filename++ = '\0'; |
| 822 | if (sscanf(data, "%u", &perm) == 1) |
| 823 | return tomoyo_update_file_acl(filename, (u8) perm, domain, |
| 824 | is_delete); |
| 825 | if (strncmp(data, "allow_", 6)) |
| 826 | goto out; |
| 827 | data += 6; |
| 828 | for (type = 0; type < TOMOYO_MAX_SINGLE_PATH_OPERATION; type++) { |
| 829 | if (strcmp(data, tomoyo_sp_keyword[type])) |
| 830 | continue; |
| 831 | return tomoyo_update_single_path_acl(type, filename, |
| 832 | domain, is_delete); |
| 833 | } |
| 834 | filename2 = strchr(filename, ' '); |
| 835 | if (!filename2) |
| 836 | goto out; |
| 837 | *filename2++ = '\0'; |
| 838 | for (type = 0; type < TOMOYO_MAX_DOUBLE_PATH_OPERATION; type++) { |
| 839 | if (strcmp(data, tomoyo_dp_keyword[type])) |
| 840 | continue; |
| 841 | return tomoyo_update_double_path_acl(type, filename, filename2, |
| 842 | domain, is_delete); |
| 843 | } |
| 844 | out: |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * tomoyo_update_single_path_acl - Update "struct tomoyo_single_path_acl_record" list. |
| 850 | * |
| 851 | * @type: Type of operation. |
| 852 | * @filename: Filename. |
| 853 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 854 | * @is_delete: True if it is a delete request. |
| 855 | * |
| 856 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 857 | * |
| 858 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 859 | */ |
| 860 | static int tomoyo_update_single_path_acl(const u8 type, const char *filename, |
| 861 | struct tomoyo_domain_info * |
| 862 | const domain, const bool is_delete) |
| 863 | { |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 864 | static const u32 rw_mask = |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 865 | (1 << TOMOYO_TYPE_READ_ACL) | (1 << TOMOYO_TYPE_WRITE_ACL); |
| 866 | const struct tomoyo_path_info *saved_filename; |
| 867 | struct tomoyo_acl_info *ptr; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 868 | struct tomoyo_single_path_acl_record *entry = NULL; |
| 869 | int error = is_delete ? -ENOENT : -ENOMEM; |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 870 | const u32 perm = 1 << type; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 871 | |
| 872 | if (!domain) |
| 873 | return -EINVAL; |
| 874 | if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__)) |
| 875 | return -EINVAL; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 876 | saved_filename = tomoyo_get_name(filename); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 877 | if (!saved_filename) |
| 878 | return -ENOMEM; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 879 | if (!is_delete) |
| 880 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 881 | mutex_lock(&tomoyo_policy_lock); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 882 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 883 | struct tomoyo_single_path_acl_record *acl = |
| 884 | container_of(ptr, struct tomoyo_single_path_acl_record, |
| 885 | head); |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 886 | if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 887 | continue; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 888 | if (acl->filename != saved_filename) |
| 889 | continue; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 890 | if (is_delete) { |
| 891 | if (perm <= 0xFFFF) |
| 892 | acl->perm &= ~perm; |
| 893 | else |
| 894 | acl->perm_high &= ~(perm >> 16); |
| 895 | if ((acl->perm & rw_mask) != rw_mask) |
| 896 | acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE_ACL); |
| 897 | else if (!(acl->perm & |
| 898 | (1 << TOMOYO_TYPE_READ_WRITE_ACL))) |
| 899 | acl->perm &= ~rw_mask; |
| 900 | } else { |
| 901 | if (perm <= 0xFFFF) |
| 902 | acl->perm |= perm; |
| 903 | else |
| 904 | acl->perm_high |= (perm >> 16); |
| 905 | if ((acl->perm & rw_mask) == rw_mask) |
| 906 | acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE_ACL; |
| 907 | else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)) |
| 908 | acl->perm |= rw_mask; |
| 909 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 910 | error = 0; |
| 911 | break; |
| 912 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 913 | if (!is_delete && error && tomoyo_memory_ok(entry)) { |
| 914 | entry->head.type = TOMOYO_TYPE_SINGLE_PATH_ACL; |
| 915 | if (perm <= 0xFFFF) |
| 916 | entry->perm = perm; |
| 917 | else |
| 918 | entry->perm_high = (perm >> 16); |
| 919 | if (perm == (1 << TOMOYO_TYPE_READ_WRITE_ACL)) |
| 920 | entry->perm |= rw_mask; |
| 921 | entry->filename = saved_filename; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 922 | saved_filename = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 923 | list_add_tail_rcu(&entry->head.list, &domain->acl_info_list); |
| 924 | entry = NULL; |
| 925 | error = 0; |
| 926 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 927 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 928 | kfree(entry); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 929 | tomoyo_put_name(saved_filename); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 930 | return error; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * tomoyo_update_double_path_acl - Update "struct tomoyo_double_path_acl_record" list. |
| 935 | * |
| 936 | * @type: Type of operation. |
| 937 | * @filename1: First filename. |
| 938 | * @filename2: Second filename. |
| 939 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 940 | * @is_delete: True if it is a delete request. |
| 941 | * |
| 942 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 943 | * |
| 944 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 945 | */ |
| 946 | static int tomoyo_update_double_path_acl(const u8 type, const char *filename1, |
| 947 | const char *filename2, |
| 948 | struct tomoyo_domain_info * |
| 949 | const domain, const bool is_delete) |
| 950 | { |
| 951 | const struct tomoyo_path_info *saved_filename1; |
| 952 | const struct tomoyo_path_info *saved_filename2; |
| 953 | struct tomoyo_acl_info *ptr; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 954 | struct tomoyo_double_path_acl_record *entry = NULL; |
| 955 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 956 | const u8 perm = 1 << type; |
| 957 | |
| 958 | if (!domain) |
| 959 | return -EINVAL; |
| 960 | if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) || |
| 961 | !tomoyo_is_correct_path(filename2, 0, 0, 0, __func__)) |
| 962 | return -EINVAL; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 963 | saved_filename1 = tomoyo_get_name(filename1); |
| 964 | saved_filename2 = tomoyo_get_name(filename2); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 965 | if (!saved_filename1 || !saved_filename2) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 966 | goto out; |
| 967 | if (!is_delete) |
| 968 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 969 | mutex_lock(&tomoyo_policy_lock); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 970 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 971 | struct tomoyo_double_path_acl_record *acl = |
| 972 | container_of(ptr, struct tomoyo_double_path_acl_record, |
| 973 | head); |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 974 | if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 975 | continue; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 976 | if (acl->filename1 != saved_filename1 || |
| 977 | acl->filename2 != saved_filename2) |
| 978 | continue; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 979 | if (is_delete) |
| 980 | acl->perm &= ~perm; |
| 981 | else |
| 982 | acl->perm |= perm; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 983 | error = 0; |
| 984 | break; |
| 985 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 986 | if (!is_delete && error && tomoyo_memory_ok(entry)) { |
| 987 | entry->head.type = TOMOYO_TYPE_DOUBLE_PATH_ACL; |
| 988 | entry->perm = perm; |
| 989 | entry->filename1 = saved_filename1; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 990 | saved_filename1 = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 991 | entry->filename2 = saved_filename2; |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 992 | saved_filename2 = NULL; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 993 | list_add_tail_rcu(&entry->head.list, &domain->acl_info_list); |
| 994 | entry = NULL; |
| 995 | error = 0; |
| 996 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 997 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 998 | out: |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame^] | 999 | tomoyo_put_name(saved_filename1); |
| 1000 | tomoyo_put_name(saved_filename2); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 1001 | kfree(entry); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1002 | return error; |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * tomoyo_check_single_path_acl - Check permission for single path operation. |
| 1007 | * |
| 1008 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1009 | * @type: Type of operation. |
| 1010 | * @filename: Filename to check. |
| 1011 | * |
| 1012 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1013 | * |
| 1014 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1015 | */ |
| 1016 | static int tomoyo_check_single_path_acl(struct tomoyo_domain_info *domain, |
| 1017 | const u8 type, |
| 1018 | const struct tomoyo_path_info *filename) |
| 1019 | { |
| 1020 | if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE)) |
| 1021 | return 0; |
| 1022 | return tomoyo_check_single_path_acl2(domain, filename, 1 << type, 1); |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * tomoyo_check_double_path_acl - Check permission for double path operation. |
| 1027 | * |
| 1028 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1029 | * @type: Type of operation. |
| 1030 | * @filename1: First filename to check. |
| 1031 | * @filename2: Second filename to check. |
| 1032 | * |
| 1033 | * Returns 0 on success, -EPERM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1034 | * |
| 1035 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1036 | */ |
| 1037 | static int tomoyo_check_double_path_acl(const struct tomoyo_domain_info *domain, |
| 1038 | const u8 type, |
| 1039 | const struct tomoyo_path_info * |
| 1040 | filename1, |
| 1041 | const struct tomoyo_path_info * |
| 1042 | filename2) |
| 1043 | { |
| 1044 | struct tomoyo_acl_info *ptr; |
| 1045 | const u8 perm = 1 << type; |
| 1046 | int error = -EPERM; |
| 1047 | |
| 1048 | if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE)) |
| 1049 | return 0; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1050 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1051 | struct tomoyo_double_path_acl_record *acl; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 1052 | if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1053 | continue; |
| 1054 | acl = container_of(ptr, struct tomoyo_double_path_acl_record, |
| 1055 | head); |
| 1056 | if (!(acl->perm & perm)) |
| 1057 | continue; |
| 1058 | if (!tomoyo_path_matches_pattern(filename1, acl->filename1)) |
| 1059 | continue; |
| 1060 | if (!tomoyo_path_matches_pattern(filename2, acl->filename2)) |
| 1061 | continue; |
| 1062 | error = 0; |
| 1063 | break; |
| 1064 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1065 | return error; |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * tomoyo_check_single_path_permission2 - Check permission for single path operation. |
| 1070 | * |
| 1071 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1072 | * @operation: Type of operation. |
| 1073 | * @filename: Filename to check. |
| 1074 | * @mode: Access control mode. |
| 1075 | * |
| 1076 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1077 | * |
| 1078 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1079 | */ |
| 1080 | static int tomoyo_check_single_path_permission2(struct tomoyo_domain_info * |
| 1081 | const domain, u8 operation, |
| 1082 | const struct tomoyo_path_info * |
| 1083 | filename, const u8 mode) |
| 1084 | { |
| 1085 | const char *msg; |
| 1086 | int error; |
| 1087 | const bool is_enforce = (mode == 3); |
| 1088 | |
| 1089 | if (!mode) |
| 1090 | return 0; |
| 1091 | next: |
| 1092 | error = tomoyo_check_single_path_acl(domain, operation, filename); |
| 1093 | msg = tomoyo_sp2keyword(operation); |
| 1094 | if (!error) |
| 1095 | goto ok; |
| 1096 | if (tomoyo_verbose_mode(domain)) |
| 1097 | printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n", |
| 1098 | tomoyo_get_msg(is_enforce), msg, filename->name, |
| 1099 | tomoyo_get_last_name(domain)); |
| 1100 | if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) { |
| 1101 | const char *name = tomoyo_get_file_pattern(filename)->name; |
| 1102 | tomoyo_update_single_path_acl(operation, name, domain, false); |
| 1103 | } |
| 1104 | if (!is_enforce) |
| 1105 | error = 0; |
| 1106 | ok: |
| 1107 | /* |
| 1108 | * Since "allow_truncate" doesn't imply "allow_rewrite" permission, |
| 1109 | * we need to check "allow_rewrite" permission if the filename is |
| 1110 | * specified by "deny_rewrite" keyword. |
| 1111 | */ |
| 1112 | if (!error && operation == TOMOYO_TYPE_TRUNCATE_ACL && |
| 1113 | tomoyo_is_no_rewrite_file(filename)) { |
| 1114 | operation = TOMOYO_TYPE_REWRITE_ACL; |
| 1115 | goto next; |
| 1116 | } |
| 1117 | return error; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1121 | * tomoyo_check_exec_perm - Check permission for "execute". |
| 1122 | * |
| 1123 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1124 | * @filename: Check permission for "execute". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1125 | * |
| 1126 | * Returns 0 on success, negativevalue otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1127 | * |
| 1128 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1129 | */ |
| 1130 | int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain, |
Tetsuo Handa | bcb8697 | 2009-06-04 15:14:34 +0900 | [diff] [blame] | 1131 | const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1132 | { |
| 1133 | const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE); |
| 1134 | |
| 1135 | if (!mode) |
| 1136 | return 0; |
| 1137 | return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * tomoyo_check_open_permission - Check permission for "read" and "write". |
| 1142 | * |
| 1143 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1144 | * @path: Pointer to "struct path". |
| 1145 | * @flag: Flags for open(). |
| 1146 | * |
| 1147 | * Returns 0 on success, negative value otherwise. |
| 1148 | */ |
| 1149 | int tomoyo_check_open_permission(struct tomoyo_domain_info *domain, |
| 1150 | struct path *path, const int flag) |
| 1151 | { |
| 1152 | const u8 acc_mode = ACC_MODE(flag); |
| 1153 | int error = -ENOMEM; |
| 1154 | struct tomoyo_path_info *buf; |
| 1155 | const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE); |
| 1156 | const bool is_enforce = (mode == 3); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1157 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1158 | |
| 1159 | if (!mode || !path->mnt) |
| 1160 | return 0; |
| 1161 | if (acc_mode == 0) |
| 1162 | return 0; |
| 1163 | if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)) |
| 1164 | /* |
| 1165 | * I don't check directories here because mkdir() and rmdir() |
| 1166 | * don't call me. |
| 1167 | */ |
| 1168 | return 0; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1169 | idx = tomoyo_read_lock(); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1170 | buf = tomoyo_get_path(path); |
| 1171 | if (!buf) |
| 1172 | goto out; |
| 1173 | error = 0; |
| 1174 | /* |
| 1175 | * If the filename is specified by "deny_rewrite" keyword, |
| 1176 | * we need to check "allow_rewrite" permission when the filename is not |
| 1177 | * opened for append mode or the filename is truncated at open time. |
| 1178 | */ |
| 1179 | if ((acc_mode & MAY_WRITE) && |
| 1180 | ((flag & O_TRUNC) || !(flag & O_APPEND)) && |
| 1181 | (tomoyo_is_no_rewrite_file(buf))) { |
| 1182 | error = tomoyo_check_single_path_permission2(domain, |
| 1183 | TOMOYO_TYPE_REWRITE_ACL, |
| 1184 | buf, mode); |
| 1185 | } |
| 1186 | if (!error) |
| 1187 | error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open", |
| 1188 | mode); |
| 1189 | if (!error && (flag & O_TRUNC)) |
| 1190 | error = tomoyo_check_single_path_permission2(domain, |
| 1191 | TOMOYO_TYPE_TRUNCATE_ACL, |
| 1192 | buf, mode); |
| 1193 | out: |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1194 | kfree(buf); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1195 | tomoyo_read_unlock(idx); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1196 | if (!is_enforce) |
| 1197 | error = 0; |
| 1198 | return error; |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 1202 | * tomoyo_check_1path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1203 | * |
| 1204 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1205 | * @operation: Type of operation. |
| 1206 | * @path: Pointer to "struct path". |
| 1207 | * |
| 1208 | * Returns 0 on success, negative value otherwise. |
| 1209 | */ |
| 1210 | int tomoyo_check_1path_perm(struct tomoyo_domain_info *domain, |
| 1211 | const u8 operation, struct path *path) |
| 1212 | { |
| 1213 | int error = -ENOMEM; |
| 1214 | struct tomoyo_path_info *buf; |
| 1215 | const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE); |
| 1216 | const bool is_enforce = (mode == 3); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1217 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1218 | |
| 1219 | if (!mode || !path->mnt) |
| 1220 | return 0; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1221 | idx = tomoyo_read_lock(); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1222 | buf = tomoyo_get_path(path); |
| 1223 | if (!buf) |
| 1224 | goto out; |
| 1225 | switch (operation) { |
| 1226 | case TOMOYO_TYPE_MKDIR_ACL: |
| 1227 | case TOMOYO_TYPE_RMDIR_ACL: |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 1228 | case TOMOYO_TYPE_CHROOT_ACL: |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1229 | if (!buf->is_dir) { |
| 1230 | /* |
| 1231 | * tomoyo_get_path() reserves space for appending "/." |
| 1232 | */ |
| 1233 | strcat((char *) buf->name, "/"); |
| 1234 | tomoyo_fill_path_info(buf); |
| 1235 | } |
| 1236 | } |
| 1237 | error = tomoyo_check_single_path_permission2(domain, operation, buf, |
| 1238 | mode); |
| 1239 | out: |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1240 | kfree(buf); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1241 | tomoyo_read_unlock(idx); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1242 | if (!is_enforce) |
| 1243 | error = 0; |
| 1244 | return error; |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * tomoyo_check_rewrite_permission - Check permission for "rewrite". |
| 1249 | * |
| 1250 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1251 | * @filp: Pointer to "struct file". |
| 1252 | * |
| 1253 | * Returns 0 on success, negative value otherwise. |
| 1254 | */ |
| 1255 | int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain, |
| 1256 | struct file *filp) |
| 1257 | { |
| 1258 | int error = -ENOMEM; |
| 1259 | const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE); |
| 1260 | const bool is_enforce = (mode == 3); |
| 1261 | struct tomoyo_path_info *buf; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1262 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1263 | |
| 1264 | if (!mode || !filp->f_path.mnt) |
| 1265 | return 0; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1266 | |
| 1267 | idx = tomoyo_read_lock(); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1268 | buf = tomoyo_get_path(&filp->f_path); |
| 1269 | if (!buf) |
| 1270 | goto out; |
| 1271 | if (!tomoyo_is_no_rewrite_file(buf)) { |
| 1272 | error = 0; |
| 1273 | goto out; |
| 1274 | } |
| 1275 | error = tomoyo_check_single_path_permission2(domain, |
| 1276 | TOMOYO_TYPE_REWRITE_ACL, |
| 1277 | buf, mode); |
| 1278 | out: |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1279 | kfree(buf); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1280 | tomoyo_read_unlock(idx); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1281 | if (!is_enforce) |
| 1282 | error = 0; |
| 1283 | return error; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 1287 | * tomoyo_check_2path_perm - Check permission for "rename", "link" and "pivot_root". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1288 | * |
| 1289 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1290 | * @operation: Type of operation. |
| 1291 | * @path1: Pointer to "struct path". |
| 1292 | * @path2: Pointer to "struct path". |
| 1293 | * |
| 1294 | * Returns 0 on success, negative value otherwise. |
| 1295 | */ |
| 1296 | int tomoyo_check_2path_perm(struct tomoyo_domain_info * const domain, |
| 1297 | const u8 operation, struct path *path1, |
| 1298 | struct path *path2) |
| 1299 | { |
| 1300 | int error = -ENOMEM; |
| 1301 | struct tomoyo_path_info *buf1, *buf2; |
| 1302 | const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE); |
| 1303 | const bool is_enforce = (mode == 3); |
| 1304 | const char *msg; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1305 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1306 | |
| 1307 | if (!mode || !path1->mnt || !path2->mnt) |
| 1308 | return 0; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1309 | idx = tomoyo_read_lock(); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1310 | buf1 = tomoyo_get_path(path1); |
| 1311 | buf2 = tomoyo_get_path(path2); |
| 1312 | if (!buf1 || !buf2) |
| 1313 | goto out; |
| 1314 | { |
| 1315 | struct dentry *dentry = path1->dentry; |
| 1316 | if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) { |
| 1317 | /* |
| 1318 | * tomoyo_get_path() reserves space for appending "/." |
| 1319 | */ |
| 1320 | if (!buf1->is_dir) { |
| 1321 | strcat((char *) buf1->name, "/"); |
| 1322 | tomoyo_fill_path_info(buf1); |
| 1323 | } |
| 1324 | if (!buf2->is_dir) { |
| 1325 | strcat((char *) buf2->name, "/"); |
| 1326 | tomoyo_fill_path_info(buf2); |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | error = tomoyo_check_double_path_acl(domain, operation, buf1, buf2); |
| 1331 | msg = tomoyo_dp2keyword(operation); |
| 1332 | if (!error) |
| 1333 | goto out; |
| 1334 | if (tomoyo_verbose_mode(domain)) |
| 1335 | printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' " |
| 1336 | "denied for %s\n", tomoyo_get_msg(is_enforce), |
| 1337 | msg, buf1->name, buf2->name, |
| 1338 | tomoyo_get_last_name(domain)); |
| 1339 | if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) { |
| 1340 | const char *name1 = tomoyo_get_file_pattern(buf1)->name; |
| 1341 | const char *name2 = tomoyo_get_file_pattern(buf2)->name; |
| 1342 | tomoyo_update_double_path_acl(operation, name1, name2, domain, |
| 1343 | false); |
| 1344 | } |
| 1345 | out: |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1346 | kfree(buf1); |
| 1347 | kfree(buf2); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1348 | tomoyo_read_unlock(idx); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1349 | if (!is_enforce) |
| 1350 | error = 0; |
| 1351 | return error; |
| 1352 | } |