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