Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/file.c |
| 3 | * |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 4 | * Pathname restriction functions. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 5 | * |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "common.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 11 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 12 | /* Keyword array for operations with one pathname. */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 13 | static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = { |
| 14 | [TOMOYO_TYPE_READ_WRITE] = "read/write", |
| 15 | [TOMOYO_TYPE_EXECUTE] = "execute", |
| 16 | [TOMOYO_TYPE_READ] = "read", |
| 17 | [TOMOYO_TYPE_WRITE] = "write", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 18 | [TOMOYO_TYPE_UNLINK] = "unlink", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 19 | [TOMOYO_TYPE_RMDIR] = "rmdir", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 20 | [TOMOYO_TYPE_TRUNCATE] = "truncate", |
| 21 | [TOMOYO_TYPE_SYMLINK] = "symlink", |
| 22 | [TOMOYO_TYPE_REWRITE] = "rewrite", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 23 | [TOMOYO_TYPE_CHROOT] = "chroot", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 24 | [TOMOYO_TYPE_UMOUNT] = "unmount", |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 25 | }; |
| 26 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 27 | /* Keyword array for operations with one pathname and three numbers. */ |
| 28 | static const char *tomoyo_path_number3_keyword |
| 29 | [TOMOYO_MAX_PATH_NUMBER3_OPERATION] = { |
| 30 | [TOMOYO_TYPE_MKBLOCK] = "mkblock", |
| 31 | [TOMOYO_TYPE_MKCHAR] = "mkchar", |
| 32 | }; |
| 33 | |
| 34 | /* Keyword array for operations with two pathnames. */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 35 | static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 36 | [TOMOYO_TYPE_LINK] = "link", |
| 37 | [TOMOYO_TYPE_RENAME] = "rename", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 38 | [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root", |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 41 | /* Keyword array for operations with one pathname and one number. */ |
| 42 | static const char *tomoyo_path_number_keyword |
| 43 | [TOMOYO_MAX_PATH_NUMBER_OPERATION] = { |
| 44 | [TOMOYO_TYPE_CREATE] = "create", |
| 45 | [TOMOYO_TYPE_MKDIR] = "mkdir", |
| 46 | [TOMOYO_TYPE_MKFIFO] = "mkfifo", |
| 47 | [TOMOYO_TYPE_MKSOCK] = "mksock", |
| 48 | [TOMOYO_TYPE_IOCTL] = "ioctl", |
| 49 | [TOMOYO_TYPE_CHMOD] = "chmod", |
| 50 | [TOMOYO_TYPE_CHOWN] = "chown", |
| 51 | [TOMOYO_TYPE_CHGRP] = "chgrp", |
| 52 | }; |
| 53 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 54 | static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = { |
| 55 | [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN, |
| 56 | [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE, |
| 57 | [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN, |
| 58 | [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN, |
| 59 | [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK, |
| 60 | [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR, |
| 61 | [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE, |
| 62 | [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK, |
| 63 | [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE, |
| 64 | [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT, |
| 65 | [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT, |
| 66 | }; |
| 67 | |
| 68 | static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = { |
| 69 | [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK, |
| 70 | [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR, |
| 71 | }; |
| 72 | |
| 73 | static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = { |
| 74 | [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK, |
| 75 | [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME, |
| 76 | [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT, |
| 77 | }; |
| 78 | |
| 79 | static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = { |
| 80 | [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE, |
| 81 | [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR, |
| 82 | [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO, |
| 83 | [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK, |
| 84 | [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL, |
| 85 | [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD, |
| 86 | [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN, |
| 87 | [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP, |
| 88 | }; |
| 89 | |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 90 | void tomoyo_put_name_union(struct tomoyo_name_union *ptr) |
| 91 | { |
| 92 | if (!ptr) |
| 93 | return; |
| 94 | if (ptr->is_group) |
| 95 | tomoyo_put_path_group(ptr->group); |
| 96 | else |
| 97 | tomoyo_put_name(ptr->filename); |
| 98 | } |
| 99 | |
| 100 | bool tomoyo_compare_name_union(const struct tomoyo_path_info *name, |
| 101 | const struct tomoyo_name_union *ptr) |
| 102 | { |
| 103 | if (ptr->is_group) |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 104 | return tomoyo_path_matches_group(name, ptr->group); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 105 | return tomoyo_path_matches_pattern(name, ptr->filename); |
| 106 | } |
| 107 | |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 108 | void tomoyo_put_number_union(struct tomoyo_number_union *ptr) |
| 109 | { |
| 110 | if (ptr && ptr->is_group) |
| 111 | tomoyo_put_number_group(ptr->group); |
| 112 | } |
| 113 | |
| 114 | bool tomoyo_compare_number_union(const unsigned long value, |
| 115 | const struct tomoyo_number_union *ptr) |
| 116 | { |
| 117 | if (ptr->is_group) |
| 118 | return tomoyo_number_matches_group(value, value, ptr->group); |
| 119 | return value >= ptr->values[0] && value <= ptr->values[1]; |
| 120 | } |
| 121 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 122 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 123 | * tomoyo_path2keyword - Get the name of single path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 124 | * |
| 125 | * @operation: Type of operation. |
| 126 | * |
| 127 | * Returns the name of single path operation. |
| 128 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 129 | const char *tomoyo_path2keyword(const u8 operation) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 130 | { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 131 | return (operation < TOMOYO_MAX_PATH_OPERATION) |
| 132 | ? tomoyo_path_keyword[operation] : NULL; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 136 | * tomoyo_path_number32keyword - Get the name of path/number/number/number operations. |
| 137 | * |
| 138 | * @operation: Type of operation. |
| 139 | * |
| 140 | * Returns the name of path/number/number/number operation. |
| 141 | */ |
| 142 | const char *tomoyo_path_number32keyword(const u8 operation) |
| 143 | { |
| 144 | return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION) |
| 145 | ? tomoyo_path_number3_keyword[operation] : NULL; |
| 146 | } |
| 147 | |
| 148 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 149 | * tomoyo_path22keyword - Get the name of double path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 150 | * |
| 151 | * @operation: Type of operation. |
| 152 | * |
| 153 | * Returns the name of double path operation. |
| 154 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 155 | const char *tomoyo_path22keyword(const u8 operation) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 156 | { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 157 | return (operation < TOMOYO_MAX_PATH2_OPERATION) |
| 158 | ? tomoyo_path2_keyword[operation] : NULL; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 162 | * tomoyo_path_number2keyword - Get the name of path/number operations. |
| 163 | * |
| 164 | * @operation: Type of operation. |
| 165 | * |
| 166 | * Returns the name of path/number operation. |
| 167 | */ |
| 168 | const char *tomoyo_path_number2keyword(const u8 operation) |
| 169 | { |
| 170 | return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION) |
| 171 | ? tomoyo_path_number_keyword[operation] : NULL; |
| 172 | } |
| 173 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 174 | static void tomoyo_add_slash(struct tomoyo_path_info *buf) |
| 175 | { |
| 176 | if (buf->is_dir) |
| 177 | return; |
| 178 | /* |
| 179 | * This is OK because tomoyo_encode() reserves space for appending "/". |
| 180 | */ |
| 181 | strcat((char *) buf->name, "/"); |
| 182 | tomoyo_fill_path_info(buf); |
| 183 | } |
| 184 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 185 | /** |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 186 | * tomoyo_strendswith - Check whether the token ends with the given token. |
| 187 | * |
| 188 | * @name: The token to check. |
| 189 | * @tail: The token to find. |
| 190 | * |
| 191 | * Returns true if @name ends with @tail, false otherwise. |
| 192 | */ |
| 193 | static bool tomoyo_strendswith(const char *name, const char *tail) |
| 194 | { |
| 195 | int len; |
| 196 | |
| 197 | if (!name || !tail) |
| 198 | return false; |
| 199 | len = strlen(name) - strlen(tail); |
| 200 | return len >= 0 && !strcmp(name + len, tail); |
| 201 | } |
| 202 | |
| 203 | /** |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 204 | * tomoyo_get_realpath - Get realpath. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 205 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 206 | * @buf: Pointer to "struct tomoyo_path_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 207 | * @path: Pointer to "struct path". |
| 208 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 209 | * Returns true on success, false otherwise. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 210 | */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 211 | static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 212 | { |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 213 | buf->name = tomoyo_realpath_from_path(path); |
| 214 | if (buf->name) { |
| 215 | tomoyo_fill_path_info(buf); |
| 216 | return true; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 217 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 218 | return false; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 221 | static int tomoyo_update_path2_acl(const u8 type, const char *filename1, |
| 222 | const char *filename2, |
| 223 | struct tomoyo_domain_info *const domain, |
| 224 | const bool is_delete); |
| 225 | static int tomoyo_update_path_acl(const u8 type, const char *filename, |
| 226 | struct tomoyo_domain_info *const domain, |
| 227 | const bool is_delete); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 228 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 229 | /* |
| 230 | * tomoyo_globally_readable_list is used for holding list of pathnames which |
| 231 | * are by default allowed to be open()ed for reading by any process. |
| 232 | * |
| 233 | * An entry is added by |
| 234 | * |
| 235 | * # echo 'allow_read /lib/libc-2.5.so' > \ |
| 236 | * /sys/kernel/security/tomoyo/exception_policy |
| 237 | * |
| 238 | * and is deleted by |
| 239 | * |
| 240 | * # echo 'delete allow_read /lib/libc-2.5.so' > \ |
| 241 | * /sys/kernel/security/tomoyo/exception_policy |
| 242 | * |
| 243 | * and all entries are retrieved by |
| 244 | * |
| 245 | * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy |
| 246 | * |
| 247 | * In the example above, any process is allowed to |
| 248 | * open("/lib/libc-2.5.so", O_RDONLY). |
| 249 | * One exception is, if the domain which current process belongs to is marked |
| 250 | * as "ignore_global_allow_read", current process can't do so unless explicitly |
| 251 | * given "allow_read /lib/libc-2.5.so" to the domain which current process |
| 252 | * belongs to. |
| 253 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 254 | LIST_HEAD(tomoyo_globally_readable_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list. |
| 258 | * |
| 259 | * @filename: Filename unconditionally permitted to open() for reading. |
| 260 | * @is_delete: True if it is a delete request. |
| 261 | * |
| 262 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 263 | * |
| 264 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 265 | */ |
| 266 | static int tomoyo_update_globally_readable_entry(const char *filename, |
| 267 | const bool is_delete) |
| 268 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 269 | struct tomoyo_globally_readable_file_entry *ptr; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 270 | struct tomoyo_globally_readable_file_entry e = { }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 271 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 272 | |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 273 | if (!tomoyo_is_correct_word(filename)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 274 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 275 | e.filename = tomoyo_get_name(filename); |
| 276 | if (!e.filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 277 | return -ENOMEM; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 278 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 279 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 280 | list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 281 | if (ptr->filename != e.filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 282 | continue; |
| 283 | ptr->is_deleted = is_delete; |
| 284 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 285 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 286 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 287 | if (!is_delete && error) { |
| 288 | struct tomoyo_globally_readable_file_entry *entry = |
| 289 | tomoyo_commit_ok(&e, sizeof(e)); |
| 290 | if (entry) { |
| 291 | list_add_tail_rcu(&entry->list, |
| 292 | &tomoyo_globally_readable_list); |
| 293 | error = 0; |
| 294 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 295 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 296 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 297 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 298 | tomoyo_put_name(e.filename); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 299 | return error; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading. |
| 304 | * |
| 305 | * @filename: The filename to check. |
| 306 | * |
| 307 | * Returns true if any domain can open @filename for reading, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 308 | * |
| 309 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 310 | */ |
| 311 | static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info * |
| 312 | filename) |
| 313 | { |
| 314 | struct tomoyo_globally_readable_file_entry *ptr; |
| 315 | bool found = false; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 316 | |
| 317 | list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 318 | if (!ptr->is_deleted && |
| 319 | tomoyo_path_matches_pattern(filename, ptr->filename)) { |
| 320 | found = true; |
| 321 | break; |
| 322 | } |
| 323 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 324 | return found; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list. |
| 329 | * |
| 330 | * @data: String to parse. |
| 331 | * @is_delete: True if it is a delete request. |
| 332 | * |
| 333 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 334 | * |
| 335 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 336 | */ |
| 337 | int tomoyo_write_globally_readable_policy(char *data, const bool is_delete) |
| 338 | { |
| 339 | return tomoyo_update_globally_readable_entry(data, is_delete); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list. |
| 344 | * |
| 345 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 346 | * |
| 347 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 348 | * |
| 349 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 350 | */ |
| 351 | bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head) |
| 352 | { |
| 353 | struct list_head *pos; |
| 354 | bool done = true; |
| 355 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 356 | list_for_each_cookie(pos, head->read_var2, |
| 357 | &tomoyo_globally_readable_list) { |
| 358 | struct tomoyo_globally_readable_file_entry *ptr; |
| 359 | ptr = list_entry(pos, |
| 360 | struct tomoyo_globally_readable_file_entry, |
| 361 | list); |
| 362 | if (ptr->is_deleted) |
| 363 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 364 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n", |
| 365 | ptr->filename->name); |
| 366 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 367 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 368 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 369 | return done; |
| 370 | } |
| 371 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 372 | /* tomoyo_pattern_list is used for holding list of pathnames which are used for |
| 373 | * converting pathnames to pathname patterns during learning mode. |
| 374 | * |
| 375 | * An entry is added by |
| 376 | * |
| 377 | * # echo 'file_pattern /proc/\$/mounts' > \ |
| 378 | * /sys/kernel/security/tomoyo/exception_policy |
| 379 | * |
| 380 | * and is deleted by |
| 381 | * |
| 382 | * # echo 'delete file_pattern /proc/\$/mounts' > \ |
| 383 | * /sys/kernel/security/tomoyo/exception_policy |
| 384 | * |
| 385 | * and all entries are retrieved by |
| 386 | * |
| 387 | * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy |
| 388 | * |
| 389 | * In the example above, if a process which belongs to a domain which is in |
| 390 | * learning mode requested open("/proc/1/mounts", O_RDONLY), |
| 391 | * "allow_read /proc/\$/mounts" is automatically added to the domain which that |
| 392 | * process belongs to. |
| 393 | * |
| 394 | * It is not a desirable behavior that we have to use /proc/\$/ instead of |
| 395 | * /proc/self/ when current process needs to access only current process's |
| 396 | * information. As of now, LSM version of TOMOYO is using __d_path() for |
| 397 | * calculating pathname. Non LSM version of TOMOYO is using its own function |
| 398 | * which pretends as if /proc/self/ is not a symlink; so that we can forbid |
| 399 | * current process from accessing other process's information. |
| 400 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 401 | LIST_HEAD(tomoyo_pattern_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list. |
| 405 | * |
| 406 | * @pattern: Pathname pattern. |
| 407 | * @is_delete: True if it is a delete request. |
| 408 | * |
| 409 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 410 | * |
| 411 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 412 | */ |
| 413 | static int tomoyo_update_file_pattern_entry(const char *pattern, |
| 414 | const bool is_delete) |
| 415 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 416 | struct tomoyo_pattern_entry *ptr; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 417 | struct tomoyo_pattern_entry e = { }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 418 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 419 | |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 420 | if (!tomoyo_is_correct_word(pattern)) |
| 421 | return -EINVAL; |
| 422 | e.pattern = tomoyo_get_name(pattern); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 423 | if (!e.pattern) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 424 | return error; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 425 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 426 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 427 | list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 428 | if (e.pattern != ptr->pattern) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 429 | continue; |
| 430 | ptr->is_deleted = is_delete; |
| 431 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 432 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 433 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 434 | if (!is_delete && error) { |
| 435 | struct tomoyo_pattern_entry *entry = |
| 436 | tomoyo_commit_ok(&e, sizeof(e)); |
| 437 | if (entry) { |
| 438 | list_add_tail_rcu(&entry->list, &tomoyo_pattern_list); |
| 439 | error = 0; |
| 440 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 441 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 442 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 443 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 444 | tomoyo_put_name(e.pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 445 | return error; |
| 446 | } |
| 447 | |
| 448 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 449 | * tomoyo_file_pattern - Get patterned pathname. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 450 | * |
| 451 | * @filename: The filename to find patterned pathname. |
| 452 | * |
| 453 | * Returns pointer to pathname pattern if matched, @filename otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 454 | * |
| 455 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 456 | */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 457 | const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 458 | { |
| 459 | struct tomoyo_pattern_entry *ptr; |
| 460 | const struct tomoyo_path_info *pattern = NULL; |
| 461 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 462 | list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 463 | if (ptr->is_deleted) |
| 464 | continue; |
| 465 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 466 | continue; |
| 467 | pattern = ptr->pattern; |
| 468 | if (tomoyo_strendswith(pattern->name, "/\\*")) { |
| 469 | /* Do nothing. Try to find the better match. */ |
| 470 | } else { |
| 471 | /* This would be the better match. Use this. */ |
| 472 | break; |
| 473 | } |
| 474 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 475 | if (pattern) |
| 476 | filename = pattern; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 477 | return filename->name; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
| 481 | * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list. |
| 482 | * |
| 483 | * @data: String to parse. |
| 484 | * @is_delete: True if it is a delete request. |
| 485 | * |
| 486 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 487 | * |
| 488 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 489 | */ |
| 490 | int tomoyo_write_pattern_policy(char *data, const bool is_delete) |
| 491 | { |
| 492 | return tomoyo_update_file_pattern_entry(data, is_delete); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list. |
| 497 | * |
| 498 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 499 | * |
| 500 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 501 | * |
| 502 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 503 | */ |
| 504 | bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head) |
| 505 | { |
| 506 | struct list_head *pos; |
| 507 | bool done = true; |
| 508 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 509 | list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) { |
| 510 | struct tomoyo_pattern_entry *ptr; |
| 511 | ptr = list_entry(pos, struct tomoyo_pattern_entry, list); |
| 512 | if (ptr->is_deleted) |
| 513 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 514 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN |
| 515 | "%s\n", ptr->pattern->name); |
| 516 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 517 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 518 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 519 | return done; |
| 520 | } |
| 521 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 522 | /* |
| 523 | * tomoyo_no_rewrite_list is used for holding list of pathnames which are by |
| 524 | * default forbidden to modify already written content of a file. |
| 525 | * |
| 526 | * An entry is added by |
| 527 | * |
| 528 | * # echo 'deny_rewrite /var/log/messages' > \ |
| 529 | * /sys/kernel/security/tomoyo/exception_policy |
| 530 | * |
| 531 | * and is deleted by |
| 532 | * |
| 533 | * # echo 'delete deny_rewrite /var/log/messages' > \ |
| 534 | * /sys/kernel/security/tomoyo/exception_policy |
| 535 | * |
| 536 | * and all entries are retrieved by |
| 537 | * |
| 538 | * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy |
| 539 | * |
| 540 | * In the example above, if a process requested to rewrite /var/log/messages , |
| 541 | * the process can't rewrite unless the domain which that process belongs to |
| 542 | * has "allow_rewrite /var/log/messages" entry. |
| 543 | * |
| 544 | * It is not a desirable behavior that we have to add "\040(deleted)" suffix |
| 545 | * when we want to allow rewriting already unlink()ed file. As of now, |
| 546 | * LSM version of TOMOYO is using __d_path() for calculating pathname. |
| 547 | * Non LSM version of TOMOYO is using its own function which doesn't append |
| 548 | * " (deleted)" suffix if the file is already unlink()ed; so that we don't |
| 549 | * need to worry whether the file is already unlink()ed or not. |
| 550 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 551 | LIST_HEAD(tomoyo_no_rewrite_list); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 552 | |
| 553 | /** |
| 554 | * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list. |
| 555 | * |
| 556 | * @pattern: Pathname pattern that are not rewritable by default. |
| 557 | * @is_delete: True if it is a delete request. |
| 558 | * |
| 559 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 560 | * |
| 561 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 562 | */ |
| 563 | static int tomoyo_update_no_rewrite_entry(const char *pattern, |
| 564 | const bool is_delete) |
| 565 | { |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 566 | struct tomoyo_no_rewrite_entry *ptr; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 567 | struct tomoyo_no_rewrite_entry e = { }; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 568 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 569 | |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 570 | if (!tomoyo_is_correct_word(pattern)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 571 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 572 | e.pattern = tomoyo_get_name(pattern); |
| 573 | if (!e.pattern) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 574 | return error; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 575 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 576 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 577 | list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 578 | if (ptr->pattern != e.pattern) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 579 | continue; |
| 580 | ptr->is_deleted = is_delete; |
| 581 | error = 0; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 582 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 583 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 584 | if (!is_delete && error) { |
| 585 | struct tomoyo_no_rewrite_entry *entry = |
| 586 | tomoyo_commit_ok(&e, sizeof(e)); |
| 587 | if (entry) { |
| 588 | list_add_tail_rcu(&entry->list, |
| 589 | &tomoyo_no_rewrite_list); |
| 590 | error = 0; |
| 591 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 592 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 593 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 594 | out: |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 595 | tomoyo_put_name(e.pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 596 | return error; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited. |
| 601 | * |
| 602 | * @filename: Filename to check. |
| 603 | * |
| 604 | * Returns true if @filename is specified by "deny_rewrite" directive, |
| 605 | * false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 606 | * |
| 607 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 608 | */ |
| 609 | static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename) |
| 610 | { |
| 611 | struct tomoyo_no_rewrite_entry *ptr; |
| 612 | bool found = false; |
| 613 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 614 | list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 615 | if (ptr->is_deleted) |
| 616 | continue; |
| 617 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 618 | continue; |
| 619 | found = true; |
| 620 | break; |
| 621 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 622 | return found; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list. |
| 627 | * |
| 628 | * @data: String to parse. |
| 629 | * @is_delete: True if it is a delete request. |
| 630 | * |
| 631 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 632 | * |
| 633 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 634 | */ |
| 635 | int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete) |
| 636 | { |
| 637 | return tomoyo_update_no_rewrite_entry(data, is_delete); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list. |
| 642 | * |
| 643 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 644 | * |
| 645 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 646 | * |
| 647 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 648 | */ |
| 649 | bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head) |
| 650 | { |
| 651 | struct list_head *pos; |
| 652 | bool done = true; |
| 653 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 654 | list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) { |
| 655 | struct tomoyo_no_rewrite_entry *ptr; |
| 656 | ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list); |
| 657 | if (ptr->is_deleted) |
| 658 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 659 | done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE |
| 660 | "%s\n", ptr->pattern->name); |
| 661 | if (!done) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 662 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 663 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 664 | return done; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * tomoyo_update_file_acl - Update file's read/write/execute ACL. |
| 669 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 670 | * @perm: Permission (between 1 to 7). |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 671 | * @filename: Filename. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 672 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 673 | * @is_delete: True if it is a delete request. |
| 674 | * |
| 675 | * Returns 0 on success, negative value otherwise. |
| 676 | * |
| 677 | * This is legacy support interface for older policy syntax. |
| 678 | * Current policy syntax uses "allow_read/write" instead of "6", |
| 679 | * "allow_read" instead of "4", "allow_write" instead of "2", |
| 680 | * "allow_execute" instead of "1". |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 681 | * |
| 682 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 683 | */ |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 684 | static int tomoyo_update_file_acl(u8 perm, const char *filename, |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 685 | struct tomoyo_domain_info * const domain, |
| 686 | const bool is_delete) |
| 687 | { |
| 688 | if (perm > 7 || !perm) { |
| 689 | printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n", |
| 690 | __func__, perm, filename); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | if (filename[0] != '@' && tomoyo_strendswith(filename, "/")) |
| 694 | /* |
| 695 | * Only 'allow_mkdir' and 'allow_rmdir' are valid for |
| 696 | * directory permissions. |
| 697 | */ |
| 698 | return 0; |
| 699 | if (perm & 4) |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 700 | tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain, |
| 701 | is_delete); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 702 | if (perm & 2) |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 703 | tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain, |
| 704 | is_delete); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 705 | if (perm & 1) |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 706 | tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain, |
| 707 | is_delete); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | /** |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 712 | * tomoyo_path_acl - Check permission for single path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 713 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 714 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 715 | * @filename: Filename to check. |
| 716 | * @perm: Permission. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 722 | static int tomoyo_path_acl(const struct tomoyo_request_info *r, |
| 723 | const struct tomoyo_path_info *filename, |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 724 | const u32 perm) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 725 | { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 726 | struct tomoyo_domain_info *domain = r->domain; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 727 | struct tomoyo_acl_info *ptr; |
| 728 | int error = -EPERM; |
| 729 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 730 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 731 | struct tomoyo_path_acl *acl; |
| 732 | if (ptr->type != TOMOYO_TYPE_PATH_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 733 | continue; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 734 | acl = container_of(ptr, struct tomoyo_path_acl, head); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 735 | if (!(acl->perm & perm) || |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 736 | !tomoyo_compare_name_union(filename, &acl->name)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 737 | continue; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 738 | error = 0; |
| 739 | break; |
| 740 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 741 | return error; |
| 742 | } |
| 743 | |
| 744 | /** |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 745 | * tomoyo_file_perm - Check permission for opening files. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 746 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 747 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 748 | * @filename: Filename to check. |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 749 | * @mode: Mode ("read" or "write" or "read/write" or "execute"). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 750 | * |
| 751 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 752 | * |
| 753 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 754 | */ |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 755 | static int tomoyo_file_perm(struct tomoyo_request_info *r, |
| 756 | const struct tomoyo_path_info *filename, |
| 757 | const u8 mode) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 758 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 759 | const char *msg = "<unknown>"; |
| 760 | int error = 0; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 761 | u32 perm = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 762 | |
| 763 | if (!filename) |
| 764 | return 0; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 765 | |
| 766 | if (mode == 6) { |
| 767 | msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE); |
| 768 | perm = 1 << TOMOYO_TYPE_READ_WRITE; |
| 769 | } else if (mode == 4) { |
| 770 | msg = tomoyo_path2keyword(TOMOYO_TYPE_READ); |
| 771 | perm = 1 << TOMOYO_TYPE_READ; |
| 772 | } else if (mode == 2) { |
| 773 | msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE); |
| 774 | perm = 1 << TOMOYO_TYPE_WRITE; |
| 775 | } else if (mode == 1) { |
| 776 | msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE); |
| 777 | perm = 1 << TOMOYO_TYPE_EXECUTE; |
| 778 | } else |
| 779 | BUG(); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 780 | do { |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 781 | error = tomoyo_path_acl(r, filename, perm); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 782 | if (error && mode == 4 && !r->domain->ignore_global_allow_read |
| 783 | && tomoyo_is_globally_readable_file(filename)) |
| 784 | error = 0; |
| 785 | if (!error) |
| 786 | break; |
| 787 | tomoyo_warn_log(r, "%s %s", msg, filename->name); |
| 788 | error = tomoyo_supervisor(r, "allow_%s %s\n", msg, |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 789 | tomoyo_file_pattern(filename)); |
| 790 | /* |
| 791 | * Do not retry for execute request, for alias may have |
| 792 | * changed. |
| 793 | */ |
| 794 | } while (error == TOMOYO_RETRY_REQUEST && mode != 1); |
| 795 | if (r->mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 796 | error = 0; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 797 | return error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 801 | * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 802 | * |
| 803 | * @type: Type of operation. |
| 804 | * @filename: Filename. |
| 805 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 806 | * @is_delete: True if it is a delete request. |
| 807 | * |
| 808 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 809 | * |
| 810 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 811 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 812 | static int tomoyo_update_path_acl(const u8 type, const char *filename, |
| 813 | struct tomoyo_domain_info *const domain, |
| 814 | const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 815 | { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 816 | static const u16 tomoyo_rw_mask = |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 817 | (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 818 | const u16 perm = 1 << type; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 819 | struct tomoyo_acl_info *ptr; |
| 820 | struct tomoyo_path_acl e = { |
| 821 | .head.type = TOMOYO_TYPE_PATH_ACL, |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 822 | .perm = perm |
| 823 | }; |
| 824 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 825 | |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 826 | if (type == TOMOYO_TYPE_READ_WRITE) |
| 827 | e.perm |= tomoyo_rw_mask; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 828 | if (!domain) |
| 829 | return -EINVAL; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 830 | if (!tomoyo_parse_name_union(filename, &e.name)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 831 | return -EINVAL; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 832 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 833 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 834 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 835 | struct tomoyo_path_acl *acl = |
| 836 | container_of(ptr, struct tomoyo_path_acl, head); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 837 | if (!tomoyo_is_same_path_acl(acl, &e)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 838 | continue; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 839 | if (is_delete) { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 840 | acl->perm &= ~perm; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 841 | if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask) |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 842 | acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE); |
| 843 | else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))) |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 844 | acl->perm &= ~tomoyo_rw_mask; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 845 | } else { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 846 | acl->perm |= perm; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 847 | if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask) |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 848 | acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE; |
| 849 | else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)) |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 850 | acl->perm |= tomoyo_rw_mask; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 851 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 852 | error = 0; |
| 853 | break; |
| 854 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 855 | if (!is_delete && error) { |
| 856 | struct tomoyo_path_acl *entry = |
| 857 | tomoyo_commit_ok(&e, sizeof(e)); |
| 858 | if (entry) { |
| 859 | list_add_tail_rcu(&entry->head.list, |
| 860 | &domain->acl_info_list); |
| 861 | error = 0; |
| 862 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 863 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 864 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 865 | out: |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 866 | tomoyo_put_name_union(&e.name); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 867 | return error; |
| 868 | } |
| 869 | |
| 870 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 871 | * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list. |
| 872 | * |
| 873 | * @type: Type of operation. |
| 874 | * @filename: Filename. |
| 875 | * @mode: Create mode. |
| 876 | * @major: Device major number. |
| 877 | * @minor: Device minor number. |
| 878 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 879 | * @is_delete: True if it is a delete request. |
| 880 | * |
| 881 | * Returns 0 on success, negative value otherwise. |
| 882 | */ |
| 883 | static inline int tomoyo_update_path_number3_acl(const u8 type, |
| 884 | const char *filename, |
| 885 | char *mode, |
| 886 | char *major, char *minor, |
| 887 | struct tomoyo_domain_info * |
| 888 | const domain, |
| 889 | const bool is_delete) |
| 890 | { |
| 891 | const u8 perm = 1 << type; |
| 892 | struct tomoyo_acl_info *ptr; |
| 893 | struct tomoyo_path_number3_acl e = { |
| 894 | .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL, |
| 895 | .perm = perm |
| 896 | }; |
| 897 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 898 | if (!tomoyo_parse_name_union(filename, &e.name) || |
| 899 | !tomoyo_parse_number_union(mode, &e.mode) || |
| 900 | !tomoyo_parse_number_union(major, &e.major) || |
| 901 | !tomoyo_parse_number_union(minor, &e.minor)) |
| 902 | goto out; |
| 903 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 904 | goto out; |
| 905 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
| 906 | struct tomoyo_path_number3_acl *acl = |
| 907 | container_of(ptr, struct tomoyo_path_number3_acl, head); |
| 908 | if (!tomoyo_is_same_path_number3_acl(acl, &e)) |
| 909 | continue; |
| 910 | if (is_delete) |
| 911 | acl->perm &= ~perm; |
| 912 | else |
| 913 | acl->perm |= perm; |
| 914 | error = 0; |
| 915 | break; |
| 916 | } |
| 917 | if (!is_delete && error) { |
| 918 | struct tomoyo_path_number3_acl *entry = |
| 919 | tomoyo_commit_ok(&e, sizeof(e)); |
| 920 | if (entry) { |
| 921 | list_add_tail_rcu(&entry->head.list, |
| 922 | &domain->acl_info_list); |
| 923 | error = 0; |
| 924 | } |
| 925 | } |
| 926 | mutex_unlock(&tomoyo_policy_lock); |
| 927 | out: |
| 928 | tomoyo_put_name_union(&e.name); |
| 929 | tomoyo_put_number_union(&e.mode); |
| 930 | tomoyo_put_number_union(&e.major); |
| 931 | tomoyo_put_number_union(&e.minor); |
| 932 | return error; |
| 933 | } |
| 934 | |
| 935 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 936 | * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 937 | * |
| 938 | * @type: Type of operation. |
| 939 | * @filename1: First filename. |
| 940 | * @filename2: Second filename. |
| 941 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 942 | * @is_delete: True if it is a delete request. |
| 943 | * |
| 944 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 945 | * |
| 946 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 947 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 948 | static int tomoyo_update_path2_acl(const u8 type, const char *filename1, |
| 949 | const char *filename2, |
| 950 | struct tomoyo_domain_info *const domain, |
| 951 | const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 952 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 953 | const u8 perm = 1 << type; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 954 | struct tomoyo_path2_acl e = { |
| 955 | .head.type = TOMOYO_TYPE_PATH2_ACL, |
| 956 | .perm = perm |
| 957 | }; |
| 958 | struct tomoyo_acl_info *ptr; |
| 959 | int error = is_delete ? -ENOENT : -ENOMEM; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 960 | |
| 961 | if (!domain) |
| 962 | return -EINVAL; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 963 | if (!tomoyo_parse_name_union(filename1, &e.name1) || |
| 964 | !tomoyo_parse_name_union(filename2, &e.name2)) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 965 | goto out; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 966 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 967 | goto out; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 968 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 969 | struct tomoyo_path2_acl *acl = |
| 970 | container_of(ptr, struct tomoyo_path2_acl, head); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 971 | if (!tomoyo_is_same_path2_acl(acl, &e)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 972 | continue; |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 973 | if (is_delete) |
| 974 | acl->perm &= ~perm; |
| 975 | else |
| 976 | acl->perm |= perm; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 977 | error = 0; |
| 978 | break; |
| 979 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 980 | if (!is_delete && error) { |
| 981 | struct tomoyo_path2_acl *entry = |
| 982 | tomoyo_commit_ok(&e, sizeof(e)); |
| 983 | if (entry) { |
| 984 | list_add_tail_rcu(&entry->head.list, |
| 985 | &domain->acl_info_list); |
| 986 | error = 0; |
| 987 | } |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 988 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 989 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 990 | out: |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 991 | tomoyo_put_name_union(&e.name1); |
| 992 | tomoyo_put_name_union(&e.name2); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 993 | return error; |
| 994 | } |
| 995 | |
| 996 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 997 | * tomoyo_path_number3_acl - Check permission for path/number/number/number operation. |
| 998 | * |
| 999 | * @r: Pointer to "struct tomoyo_request_info". |
| 1000 | * @filename: Filename to check. |
| 1001 | * @perm: Permission. |
| 1002 | * @mode: Create mode. |
| 1003 | * @major: Device major number. |
| 1004 | * @minor: Device minor number. |
| 1005 | * |
| 1006 | * Returns 0 on success, -EPERM otherwise. |
| 1007 | * |
| 1008 | * Caller holds tomoyo_read_lock(). |
| 1009 | */ |
| 1010 | static int tomoyo_path_number3_acl(struct tomoyo_request_info *r, |
| 1011 | const struct tomoyo_path_info *filename, |
| 1012 | const u16 perm, const unsigned int mode, |
| 1013 | const unsigned int major, |
| 1014 | const unsigned int minor) |
| 1015 | { |
| 1016 | struct tomoyo_domain_info *domain = r->domain; |
| 1017 | struct tomoyo_acl_info *ptr; |
| 1018 | int error = -EPERM; |
| 1019 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
| 1020 | struct tomoyo_path_number3_acl *acl; |
| 1021 | if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL) |
| 1022 | continue; |
| 1023 | acl = container_of(ptr, struct tomoyo_path_number3_acl, head); |
| 1024 | if (!tomoyo_compare_number_union(mode, &acl->mode)) |
| 1025 | continue; |
| 1026 | if (!tomoyo_compare_number_union(major, &acl->major)) |
| 1027 | continue; |
| 1028 | if (!tomoyo_compare_number_union(minor, &acl->minor)) |
| 1029 | continue; |
| 1030 | if (!(acl->perm & perm)) |
| 1031 | continue; |
| 1032 | if (!tomoyo_compare_name_union(filename, &acl->name)) |
| 1033 | continue; |
| 1034 | error = 0; |
| 1035 | break; |
| 1036 | } |
| 1037 | return error; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1041 | * tomoyo_path2_acl - Check permission for double path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1042 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1043 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1044 | * @type: Type of operation. |
| 1045 | * @filename1: First filename to check. |
| 1046 | * @filename2: Second filename to check. |
| 1047 | * |
| 1048 | * Returns 0 on success, -EPERM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1049 | * |
| 1050 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1051 | */ |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1052 | static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1053 | const struct tomoyo_path_info *filename1, |
| 1054 | const struct tomoyo_path_info *filename2) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1055 | { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1056 | const struct tomoyo_domain_info *domain = r->domain; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1057 | struct tomoyo_acl_info *ptr; |
| 1058 | const u8 perm = 1 << type; |
| 1059 | int error = -EPERM; |
| 1060 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1061 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1062 | struct tomoyo_path2_acl *acl; |
| 1063 | if (ptr->type != TOMOYO_TYPE_PATH2_ACL) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1064 | continue; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1065 | acl = container_of(ptr, struct tomoyo_path2_acl, head); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1066 | if (!(acl->perm & perm)) |
| 1067 | continue; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 1068 | if (!tomoyo_compare_name_union(filename1, &acl->name1)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1069 | continue; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 1070 | if (!tomoyo_compare_name_union(filename2, &acl->name2)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1071 | continue; |
| 1072 | error = 0; |
| 1073 | break; |
| 1074 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1075 | return error; |
| 1076 | } |
| 1077 | |
| 1078 | /** |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1079 | * tomoyo_path_permission - Check permission for single path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1080 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1081 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1082 | * @operation: Type of operation. |
| 1083 | * @filename: Filename to check. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1084 | * |
| 1085 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1086 | * |
| 1087 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1088 | */ |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1089 | static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation, |
| 1090 | const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1091 | { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1092 | const char *msg; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1093 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1094 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1095 | next: |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1096 | r->type = tomoyo_p2mac[operation]; |
| 1097 | r->mode = tomoyo_get_mode(r->profile, r->type); |
| 1098 | if (r->mode == TOMOYO_CONFIG_DISABLED) |
| 1099 | return 0; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1100 | do { |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 1101 | error = tomoyo_path_acl(r, filename, 1 << operation); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1102 | if (!error) |
| 1103 | break; |
| 1104 | msg = tomoyo_path2keyword(operation); |
| 1105 | tomoyo_warn_log(r, "%s %s", msg, filename->name); |
| 1106 | error = tomoyo_supervisor(r, "allow_%s %s\n", msg, |
| 1107 | tomoyo_file_pattern(filename)); |
| 1108 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1109 | if (r->mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1110 | error = 0; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1111 | /* |
| 1112 | * Since "allow_truncate" doesn't imply "allow_rewrite" permission, |
| 1113 | * we need to check "allow_rewrite" permission if the filename is |
| 1114 | * specified by "deny_rewrite" keyword. |
| 1115 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1116 | if (!error && operation == TOMOYO_TYPE_TRUNCATE && |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1117 | tomoyo_is_no_rewrite_file(filename)) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1118 | operation = TOMOYO_TYPE_REWRITE; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1119 | goto next; |
| 1120 | } |
| 1121 | return error; |
| 1122 | } |
| 1123 | |
| 1124 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1125 | * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation. |
| 1126 | * |
| 1127 | * @r: Pointer to "struct tomoyo_request_info". |
| 1128 | * @type: Operation. |
| 1129 | * @filename: Filename to check. |
| 1130 | * @number: Number. |
| 1131 | * |
| 1132 | * Returns 0 on success, -EPERM otherwise. |
| 1133 | * |
| 1134 | * Caller holds tomoyo_read_lock(). |
| 1135 | */ |
| 1136 | static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type, |
| 1137 | const struct tomoyo_path_info *filename, |
| 1138 | const unsigned long number) |
| 1139 | { |
| 1140 | struct tomoyo_domain_info *domain = r->domain; |
| 1141 | struct tomoyo_acl_info *ptr; |
| 1142 | const u8 perm = 1 << type; |
| 1143 | int error = -EPERM; |
| 1144 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
| 1145 | struct tomoyo_path_number_acl *acl; |
| 1146 | if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL) |
| 1147 | continue; |
| 1148 | acl = container_of(ptr, struct tomoyo_path_number_acl, |
| 1149 | head); |
| 1150 | if (!(acl->perm & perm) || |
| 1151 | !tomoyo_compare_number_union(number, &acl->number) || |
| 1152 | !tomoyo_compare_name_union(filename, &acl->name)) |
| 1153 | continue; |
| 1154 | error = 0; |
| 1155 | break; |
| 1156 | } |
| 1157 | return error; |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL. |
| 1162 | * |
| 1163 | * @type: Type of operation. |
| 1164 | * @filename: Filename. |
| 1165 | * @number: Number. |
| 1166 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1167 | * @is_delete: True if it is a delete request. |
| 1168 | * |
| 1169 | * Returns 0 on success, negative value otherwise. |
| 1170 | */ |
| 1171 | static inline int tomoyo_update_path_number_acl(const u8 type, |
| 1172 | const char *filename, |
| 1173 | char *number, |
| 1174 | struct tomoyo_domain_info * |
| 1175 | const domain, |
| 1176 | const bool is_delete) |
| 1177 | { |
| 1178 | const u8 perm = 1 << type; |
| 1179 | struct tomoyo_acl_info *ptr; |
| 1180 | struct tomoyo_path_number_acl e = { |
| 1181 | .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL, |
| 1182 | .perm = perm |
| 1183 | }; |
| 1184 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 1185 | if (!domain) |
| 1186 | return -EINVAL; |
| 1187 | if (!tomoyo_parse_name_union(filename, &e.name)) |
| 1188 | return -EINVAL; |
| 1189 | if (!tomoyo_parse_number_union(number, &e.number)) |
| 1190 | goto out; |
| 1191 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 1192 | goto out; |
| 1193 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
| 1194 | struct tomoyo_path_number_acl *acl = |
| 1195 | container_of(ptr, struct tomoyo_path_number_acl, head); |
| 1196 | if (!tomoyo_is_same_path_number_acl(acl, &e)) |
| 1197 | continue; |
| 1198 | if (is_delete) |
| 1199 | acl->perm &= ~perm; |
| 1200 | else |
| 1201 | acl->perm |= perm; |
| 1202 | error = 0; |
| 1203 | break; |
| 1204 | } |
| 1205 | if (!is_delete && error) { |
| 1206 | struct tomoyo_path_number_acl *entry = |
| 1207 | tomoyo_commit_ok(&e, sizeof(e)); |
| 1208 | if (entry) { |
| 1209 | list_add_tail_rcu(&entry->head.list, |
| 1210 | &domain->acl_info_list); |
| 1211 | error = 0; |
| 1212 | } |
| 1213 | } |
| 1214 | mutex_unlock(&tomoyo_policy_lock); |
| 1215 | out: |
| 1216 | tomoyo_put_name_union(&e.name); |
| 1217 | tomoyo_put_number_union(&e.number); |
| 1218 | return error; |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp". |
| 1223 | * |
| 1224 | * @r: Pointer to "strct tomoyo_request_info". |
| 1225 | * @filename: Filename to check. |
| 1226 | * @number: Number. |
| 1227 | * |
| 1228 | * Returns 0 on success, negative value otherwise. |
| 1229 | * |
| 1230 | * Caller holds tomoyo_read_lock(). |
| 1231 | */ |
| 1232 | static int tomoyo_path_number_perm2(struct tomoyo_request_info *r, |
| 1233 | const u8 type, |
| 1234 | const struct tomoyo_path_info *filename, |
| 1235 | const unsigned long number) |
| 1236 | { |
| 1237 | char buffer[64]; |
| 1238 | int error; |
| 1239 | u8 radix; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1240 | const char *msg; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1241 | |
| 1242 | if (!filename) |
| 1243 | return 0; |
| 1244 | switch (type) { |
| 1245 | case TOMOYO_TYPE_CREATE: |
| 1246 | case TOMOYO_TYPE_MKDIR: |
| 1247 | case TOMOYO_TYPE_MKFIFO: |
| 1248 | case TOMOYO_TYPE_MKSOCK: |
| 1249 | case TOMOYO_TYPE_CHMOD: |
| 1250 | radix = TOMOYO_VALUE_TYPE_OCTAL; |
| 1251 | break; |
| 1252 | case TOMOYO_TYPE_IOCTL: |
| 1253 | radix = TOMOYO_VALUE_TYPE_HEXADECIMAL; |
| 1254 | break; |
| 1255 | default: |
| 1256 | radix = TOMOYO_VALUE_TYPE_DECIMAL; |
| 1257 | break; |
| 1258 | } |
| 1259 | tomoyo_print_ulong(buffer, sizeof(buffer), number, radix); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1260 | do { |
| 1261 | error = tomoyo_path_number_acl(r, type, filename, number); |
| 1262 | if (!error) |
| 1263 | break; |
| 1264 | msg = tomoyo_path_number2keyword(type); |
| 1265 | tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer); |
| 1266 | error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg, |
| 1267 | tomoyo_file_pattern(filename), |
| 1268 | buffer); |
| 1269 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1270 | if (r->mode != TOMOYO_CONFIG_ENFORCING) |
| 1271 | error = 0; |
| 1272 | return error; |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp". |
| 1277 | * |
| 1278 | * @type: Type of operation. |
| 1279 | * @path: Pointer to "struct path". |
| 1280 | * @number: Number. |
| 1281 | * |
| 1282 | * Returns 0 on success, negative value otherwise. |
| 1283 | */ |
| 1284 | int tomoyo_path_number_perm(const u8 type, struct path *path, |
| 1285 | unsigned long number) |
| 1286 | { |
| 1287 | struct tomoyo_request_info r; |
| 1288 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1289 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1290 | int idx; |
| 1291 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1292 | if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type]) |
| 1293 | == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1294 | return 0; |
| 1295 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1296 | if (!tomoyo_get_realpath(&buf, path)) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1297 | goto out; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1298 | if (type == TOMOYO_TYPE_MKDIR) |
| 1299 | tomoyo_add_slash(&buf); |
| 1300 | error = tomoyo_path_number_perm2(&r, type, &buf, number); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1301 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1302 | kfree(buf.name); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1303 | tomoyo_read_unlock(idx); |
| 1304 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 1305 | error = 0; |
| 1306 | return error; |
| 1307 | } |
| 1308 | |
| 1309 | /** |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1310 | * tomoyo_check_exec_perm - Check permission for "execute". |
| 1311 | * |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1312 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1313 | * @filename: Check permission for "execute". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1314 | * |
| 1315 | * Returns 0 on success, negativevalue otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1316 | * |
| 1317 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1318 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1319 | int tomoyo_check_exec_perm(struct tomoyo_request_info *r, |
Tetsuo Handa | bcb8697 | 2009-06-04 15:14:34 +0900 | [diff] [blame] | 1320 | const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1321 | { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1322 | if (r->mode == TOMOYO_CONFIG_DISABLED) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1323 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1324 | return tomoyo_file_perm(r, filename, 1); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * tomoyo_check_open_permission - Check permission for "read" and "write". |
| 1329 | * |
| 1330 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1331 | * @path: Pointer to "struct path". |
| 1332 | * @flag: Flags for open(). |
| 1333 | * |
| 1334 | * Returns 0 on success, negative value otherwise. |
| 1335 | */ |
| 1336 | int tomoyo_check_open_permission(struct tomoyo_domain_info *domain, |
| 1337 | struct path *path, const int flag) |
| 1338 | { |
| 1339 | const u8 acc_mode = ACC_MODE(flag); |
| 1340 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1341 | struct tomoyo_path_info buf; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1342 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1343 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1344 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1345 | if (!path->mnt || |
| 1346 | (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1347 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1348 | buf.name = NULL; |
| 1349 | r.mode = TOMOYO_CONFIG_DISABLED; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1350 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1351 | if (!tomoyo_get_realpath(&buf, path)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1352 | goto out; |
| 1353 | error = 0; |
| 1354 | /* |
| 1355 | * If the filename is specified by "deny_rewrite" keyword, |
| 1356 | * we need to check "allow_rewrite" permission when the filename is not |
| 1357 | * opened for append mode or the filename is truncated at open time. |
| 1358 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1359 | if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND) |
| 1360 | && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE) |
| 1361 | != TOMOYO_CONFIG_DISABLED) { |
| 1362 | if (!tomoyo_get_realpath(&buf, path)) { |
| 1363 | error = -ENOMEM; |
| 1364 | goto out; |
| 1365 | } |
| 1366 | if (tomoyo_is_no_rewrite_file(&buf)) |
| 1367 | error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, |
| 1368 | &buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1369 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1370 | if (!error && acc_mode && |
| 1371 | tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN) |
| 1372 | != TOMOYO_CONFIG_DISABLED) { |
| 1373 | if (!buf.name && !tomoyo_get_realpath(&buf, path)) { |
| 1374 | error = -ENOMEM; |
| 1375 | goto out; |
| 1376 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1377 | error = tomoyo_file_perm(&r, &buf, acc_mode); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1378 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1379 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1380 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1381 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1382 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1383 | error = 0; |
| 1384 | return error; |
| 1385 | } |
| 1386 | |
| 1387 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 1388 | * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1389 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1390 | * @operation: Type of operation. |
| 1391 | * @path: Pointer to "struct path". |
| 1392 | * |
| 1393 | * Returns 0 on success, negative value otherwise. |
| 1394 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 1395 | int tomoyo_path_perm(const u8 operation, struct path *path) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1396 | { |
| 1397 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1398 | struct tomoyo_path_info buf; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1399 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1400 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1401 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1402 | if (!path->mnt) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1403 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1404 | if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation]) |
| 1405 | == TOMOYO_CONFIG_DISABLED) |
| 1406 | return 0; |
| 1407 | buf.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1408 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1409 | if (!tomoyo_get_realpath(&buf, path)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1410 | goto out; |
| 1411 | switch (operation) { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1412 | case TOMOYO_TYPE_REWRITE: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1413 | if (!tomoyo_is_no_rewrite_file(&buf)) { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1414 | error = 0; |
| 1415 | goto out; |
| 1416 | } |
| 1417 | break; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1418 | case TOMOYO_TYPE_RMDIR: |
| 1419 | case TOMOYO_TYPE_CHROOT: |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1420 | case TOMOYO_TYPE_UMOUNT: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1421 | tomoyo_add_slash(&buf); |
| 1422 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1423 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1424 | error = tomoyo_path_permission(&r, operation, &buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1425 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1426 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1427 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1428 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1429 | error = 0; |
| 1430 | return error; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1434 | * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation. |
| 1435 | * |
| 1436 | * @r: Pointer to "struct tomoyo_request_info". |
| 1437 | * @operation: Type of operation. |
| 1438 | * @filename: Filename to check. |
| 1439 | * @mode: Create mode. |
| 1440 | * @dev: Device number. |
| 1441 | * |
| 1442 | * Returns 0 on success, negative value otherwise. |
| 1443 | * |
| 1444 | * Caller holds tomoyo_read_lock(). |
| 1445 | */ |
| 1446 | static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r, |
| 1447 | const u8 operation, |
| 1448 | const struct tomoyo_path_info *filename, |
| 1449 | const unsigned int mode, |
| 1450 | const unsigned int dev) |
| 1451 | { |
| 1452 | int error; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1453 | const char *msg; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1454 | const unsigned int major = MAJOR(dev); |
| 1455 | const unsigned int minor = MINOR(dev); |
| 1456 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1457 | do { |
| 1458 | error = tomoyo_path_number3_acl(r, filename, 1 << operation, |
| 1459 | mode, major, minor); |
| 1460 | if (!error) |
| 1461 | break; |
| 1462 | msg = tomoyo_path_number32keyword(operation); |
| 1463 | tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name, |
| 1464 | mode, major, minor); |
| 1465 | error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg, |
| 1466 | tomoyo_file_pattern(filename), mode, |
| 1467 | major, minor); |
| 1468 | } while (error == TOMOYO_RETRY_REQUEST); |
| 1469 | if (r->mode != TOMOYO_CONFIG_ENFORCING) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1470 | error = 0; |
| 1471 | return error; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar". |
| 1476 | * |
| 1477 | * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK) |
| 1478 | * @path: Pointer to "struct path". |
| 1479 | * @mode: Create mode. |
| 1480 | * @dev: Device number. |
| 1481 | * |
| 1482 | * Returns 0 on success, negative value otherwise. |
| 1483 | */ |
| 1484 | int tomoyo_path_number3_perm(const u8 operation, struct path *path, |
| 1485 | const unsigned int mode, unsigned int dev) |
| 1486 | { |
| 1487 | struct tomoyo_request_info r; |
| 1488 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1489 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1490 | int idx; |
| 1491 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1492 | if (!path->mnt || |
| 1493 | tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation]) |
| 1494 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1495 | return 0; |
| 1496 | idx = tomoyo_read_lock(); |
| 1497 | error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1498 | if (tomoyo_get_realpath(&buf, path)) { |
| 1499 | error = tomoyo_path_number3_perm2(&r, operation, &buf, mode, |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1500 | new_decode_dev(dev)); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1501 | kfree(buf.name); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1502 | } |
| 1503 | tomoyo_read_unlock(idx); |
| 1504 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 1505 | error = 0; |
| 1506 | return error; |
| 1507 | } |
| 1508 | |
| 1509 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1510 | * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1511 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1512 | * @operation: Type of operation. |
| 1513 | * @path1: Pointer to "struct path". |
| 1514 | * @path2: Pointer to "struct path". |
| 1515 | * |
| 1516 | * Returns 0 on success, negative value otherwise. |
| 1517 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 1518 | int tomoyo_path2_perm(const u8 operation, struct path *path1, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1519 | struct path *path2) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1520 | { |
| 1521 | int error = -ENOMEM; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1522 | const char *msg; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1523 | struct tomoyo_path_info buf1; |
| 1524 | struct tomoyo_path_info buf2; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1525 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1526 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1527 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1528 | if (!path1->mnt || !path2->mnt || |
| 1529 | tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation]) |
| 1530 | == TOMOYO_CONFIG_DISABLED) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1531 | return 0; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1532 | buf1.name = NULL; |
| 1533 | buf2.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1534 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1535 | if (!tomoyo_get_realpath(&buf1, path1) || |
| 1536 | !tomoyo_get_realpath(&buf2, path2)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1537 | goto out; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame^] | 1538 | switch (operation) { |
| 1539 | struct dentry *dentry; |
| 1540 | case TOMOYO_TYPE_RENAME: |
| 1541 | case TOMOYO_TYPE_LINK: |
| 1542 | dentry = path1->dentry; |
| 1543 | if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode)) |
| 1544 | break; |
| 1545 | /* fall through */ |
| 1546 | case TOMOYO_TYPE_PIVOT_ROOT: |
| 1547 | tomoyo_add_slash(&buf1); |
| 1548 | tomoyo_add_slash(&buf2); |
| 1549 | break; |
| 1550 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1551 | do { |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1552 | error = tomoyo_path2_acl(&r, operation, &buf1, &buf2); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1553 | if (!error) |
| 1554 | break; |
| 1555 | msg = tomoyo_path22keyword(operation); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1556 | tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1557 | error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg, |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1558 | tomoyo_file_pattern(&buf1), |
| 1559 | tomoyo_file_pattern(&buf2)); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1560 | } while (error == TOMOYO_RETRY_REQUEST); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1561 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1562 | kfree(buf1.name); |
| 1563 | kfree(buf2.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1564 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1565 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1566 | error = 0; |
| 1567 | return error; |
| 1568 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1569 | |
| 1570 | /** |
| 1571 | * tomoyo_write_file_policy - Update file related list. |
| 1572 | * |
| 1573 | * @data: String to parse. |
| 1574 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1575 | * @is_delete: True if it is a delete request. |
| 1576 | * |
| 1577 | * Returns 0 on success, negative value otherwise. |
| 1578 | * |
| 1579 | * Caller holds tomoyo_read_lock(). |
| 1580 | */ |
| 1581 | int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain, |
| 1582 | const bool is_delete) |
| 1583 | { |
| 1584 | char *w[5]; |
| 1585 | u8 type; |
| 1586 | if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0]) |
| 1587 | return -EINVAL; |
| 1588 | if (strncmp(w[0], "allow_", 6)) { |
| 1589 | unsigned int perm; |
| 1590 | if (sscanf(w[0], "%u", &perm) == 1) |
| 1591 | return tomoyo_update_file_acl((u8) perm, w[1], domain, |
| 1592 | is_delete); |
| 1593 | goto out; |
| 1594 | } |
| 1595 | w[0] += 6; |
| 1596 | for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) { |
| 1597 | if (strcmp(w[0], tomoyo_path_keyword[type])) |
| 1598 | continue; |
| 1599 | return tomoyo_update_path_acl(type, w[1], domain, is_delete); |
| 1600 | } |
| 1601 | if (!w[2][0]) |
| 1602 | goto out; |
| 1603 | for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) { |
| 1604 | if (strcmp(w[0], tomoyo_path2_keyword[type])) |
| 1605 | continue; |
| 1606 | return tomoyo_update_path2_acl(type, w[1], w[2], domain, |
| 1607 | is_delete); |
| 1608 | } |
| 1609 | for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) { |
| 1610 | if (strcmp(w[0], tomoyo_path_number_keyword[type])) |
| 1611 | continue; |
| 1612 | return tomoyo_update_path_number_acl(type, w[1], w[2], domain, |
| 1613 | is_delete); |
| 1614 | } |
| 1615 | if (!w[3][0] || !w[4][0]) |
| 1616 | goto out; |
| 1617 | for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) { |
| 1618 | if (strcmp(w[0], tomoyo_path_number3_keyword[type])) |
| 1619 | continue; |
| 1620 | return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3], |
| 1621 | w[4], domain, is_delete); |
| 1622 | } |
| 1623 | out: |
| 1624 | return -EINVAL; |
| 1625 | } |