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