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