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) |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame] | 93 | tomoyo_put_group(ptr->group); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 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) |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame] | 109 | tomoyo_put_group(ptr->group); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 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, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 182 | tomoyo_pattern(filename)); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 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, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 202 | tomoyo_pattern(filename1), |
| 203 | tomoyo_pattern(filename2)); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 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, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 225 | tomoyo_pattern(filename), mode, major, minor); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
| 229 | * tomoyo_audit_path_number_log - Audit path/number request log. |
| 230 | * |
| 231 | * @r: Pointer to "struct tomoyo_request_info". |
| 232 | * @error: Error code. |
| 233 | * |
| 234 | * Returns 0 on success, negative value otherwise. |
| 235 | */ |
| 236 | static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r) |
| 237 | { |
| 238 | const u8 type = r->param.path_number.operation; |
| 239 | u8 radix; |
| 240 | const struct tomoyo_path_info *filename = r->param.path_number.filename; |
| 241 | const char *operation = tomoyo_path_number_keyword[type]; |
| 242 | char buffer[64]; |
| 243 | if (r->granted) |
| 244 | return 0; |
| 245 | switch (type) { |
| 246 | case TOMOYO_TYPE_CREATE: |
| 247 | case TOMOYO_TYPE_MKDIR: |
| 248 | case TOMOYO_TYPE_MKFIFO: |
| 249 | case TOMOYO_TYPE_MKSOCK: |
| 250 | case TOMOYO_TYPE_CHMOD: |
| 251 | radix = TOMOYO_VALUE_TYPE_OCTAL; |
| 252 | break; |
| 253 | case TOMOYO_TYPE_IOCTL: |
| 254 | radix = TOMOYO_VALUE_TYPE_HEXADECIMAL; |
| 255 | break; |
| 256 | default: |
| 257 | radix = TOMOYO_VALUE_TYPE_DECIMAL; |
| 258 | break; |
| 259 | } |
| 260 | tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number, |
| 261 | radix); |
| 262 | tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer); |
| 263 | return tomoyo_supervisor(r, "allow_%s %s %s\n", operation, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 264 | tomoyo_pattern(filename), buffer); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 267 | static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a, |
| 268 | const struct tomoyo_acl_head *b) |
| 269 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 270 | return container_of(a, struct tomoyo_readable_file, |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 271 | head)->filename == |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 272 | container_of(b, struct tomoyo_readable_file, |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 273 | head)->filename; |
| 274 | } |
| 275 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 276 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 277 | * tomoyo_update_globally_readable_entry - Update "struct tomoyo_readable_file" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 278 | * |
| 279 | * @filename: Filename unconditionally permitted to open() for reading. |
| 280 | * @is_delete: True if it is a delete request. |
| 281 | * |
| 282 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 283 | * |
| 284 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 285 | */ |
| 286 | static int tomoyo_update_globally_readable_entry(const char *filename, |
| 287 | const bool is_delete) |
| 288 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 289 | struct tomoyo_readable_file e = { }; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 290 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 291 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 292 | if (!tomoyo_correct_word(filename)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 293 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 294 | e.filename = tomoyo_get_name(filename); |
| 295 | if (!e.filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 296 | return -ENOMEM; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 297 | error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 298 | &tomoyo_policy_list |
| 299 | [TOMOYO_ID_GLOBALLY_READABLE], |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 300 | tomoyo_same_globally_readable); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 301 | tomoyo_put_name(e.filename); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 302 | return error; |
| 303 | } |
| 304 | |
| 305 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 306 | * 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] | 307 | * |
| 308 | * @filename: The filename to check. |
| 309 | * |
| 310 | * Returns true if any domain can open @filename for reading, false 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 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 314 | static bool tomoyo_globally_readable_file(const struct tomoyo_path_info * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 315 | filename) |
| 316 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 317 | struct tomoyo_readable_file *ptr; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 318 | bool found = false; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 319 | |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 320 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list |
| 321 | [TOMOYO_ID_GLOBALLY_READABLE], head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 322 | if (!ptr->head.is_deleted && |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 323 | tomoyo_path_matches_pattern(filename, ptr->filename)) { |
| 324 | found = true; |
| 325 | break; |
| 326 | } |
| 327 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 328 | return found; |
| 329 | } |
| 330 | |
| 331 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 332 | * tomoyo_write_globally_readable - Write "struct tomoyo_readable_file" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 333 | * |
| 334 | * @data: String to parse. |
| 335 | * @is_delete: True if it is a delete request. |
| 336 | * |
| 337 | * Returns 0 on success, negative value 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 | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 341 | int tomoyo_write_globally_readable(char *data, const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 342 | { |
| 343 | return tomoyo_update_globally_readable_entry(data, is_delete); |
| 344 | } |
| 345 | |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 346 | static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a, |
| 347 | const struct tomoyo_acl_head *b) |
| 348 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 349 | return container_of(a, struct tomoyo_no_pattern, head)->pattern == |
| 350 | container_of(b, struct tomoyo_no_pattern, head)->pattern; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 351 | } |
| 352 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 353 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 354 | * tomoyo_update_file_pattern_entry - Update "struct tomoyo_no_pattern" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 355 | * |
| 356 | * @pattern: Pathname pattern. |
| 357 | * @is_delete: True if it is a delete request. |
| 358 | * |
| 359 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 360 | * |
| 361 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 362 | */ |
| 363 | static int tomoyo_update_file_pattern_entry(const char *pattern, |
| 364 | const bool is_delete) |
| 365 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 366 | struct tomoyo_no_pattern e = { }; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 367 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 368 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 369 | if (!tomoyo_correct_word(pattern)) |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 370 | return -EINVAL; |
| 371 | e.pattern = tomoyo_get_name(pattern); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 372 | if (!e.pattern) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 373 | return -ENOMEM; |
| 374 | error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 375 | &tomoyo_policy_list[TOMOYO_ID_PATTERN], |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 376 | tomoyo_same_pattern); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 377 | tomoyo_put_name(e.pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 378 | return error; |
| 379 | } |
| 380 | |
| 381 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 382 | * tomoyo_pattern - Get patterned pathname. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 383 | * |
| 384 | * @filename: The filename to find patterned pathname. |
| 385 | * |
| 386 | * Returns pointer to pathname pattern if matched, @filename otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 387 | * |
| 388 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 389 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 390 | const char *tomoyo_pattern(const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 391 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 392 | struct tomoyo_no_pattern *ptr; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 393 | const struct tomoyo_path_info *pattern = NULL; |
| 394 | |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 395 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_PATTERN], |
| 396 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 397 | if (ptr->head.is_deleted) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 398 | continue; |
| 399 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 400 | continue; |
| 401 | pattern = ptr->pattern; |
| 402 | if (tomoyo_strendswith(pattern->name, "/\\*")) { |
| 403 | /* Do nothing. Try to find the better match. */ |
| 404 | } else { |
| 405 | /* This would be the better match. Use this. */ |
| 406 | break; |
| 407 | } |
| 408 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 409 | if (pattern) |
| 410 | filename = pattern; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 411 | return filename->name; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 415 | * tomoyo_write_pattern - Write "struct tomoyo_no_pattern" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 416 | * |
| 417 | * @data: String to parse. |
| 418 | * @is_delete: True if it is a delete request. |
| 419 | * |
| 420 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 421 | * |
| 422 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 423 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 424 | int tomoyo_write_pattern(char *data, const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 425 | { |
| 426 | return tomoyo_update_file_pattern_entry(data, is_delete); |
| 427 | } |
| 428 | |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 429 | static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a, |
| 430 | const struct tomoyo_acl_head *b) |
| 431 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 432 | return container_of(a, struct tomoyo_no_rewrite, head)->pattern |
| 433 | == container_of(b, struct tomoyo_no_rewrite, head) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 434 | ->pattern; |
| 435 | } |
| 436 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 437 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 438 | * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 439 | * |
| 440 | * @pattern: Pathname pattern that are not rewritable by default. |
| 441 | * @is_delete: True if it is a delete request. |
| 442 | * |
| 443 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 444 | * |
| 445 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 446 | */ |
| 447 | static int tomoyo_update_no_rewrite_entry(const char *pattern, |
| 448 | const bool is_delete) |
| 449 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 450 | struct tomoyo_no_rewrite e = { }; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 451 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 452 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 453 | if (!tomoyo_correct_word(pattern)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 454 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 455 | e.pattern = tomoyo_get_name(pattern); |
| 456 | if (!e.pattern) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 457 | return -ENOMEM; |
| 458 | error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 459 | &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE], |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 460 | tomoyo_same_no_rewrite); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 461 | tomoyo_put_name(e.pattern); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 462 | return error; |
| 463 | } |
| 464 | |
| 465 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 466 | * 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] | 467 | * |
| 468 | * @filename: Filename to check. |
| 469 | * |
| 470 | * Returns true if @filename is specified by "deny_rewrite" directive, |
| 471 | * false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 472 | * |
| 473 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 474 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 475 | static bool tomoyo_no_rewrite_file(const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 476 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 477 | struct tomoyo_no_rewrite *ptr; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 478 | bool found = false; |
| 479 | |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 480 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE], |
| 481 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 482 | if (ptr->head.is_deleted) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 483 | continue; |
| 484 | if (!tomoyo_path_matches_pattern(filename, ptr->pattern)) |
| 485 | continue; |
| 486 | found = true; |
| 487 | break; |
| 488 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 489 | return found; |
| 490 | } |
| 491 | |
| 492 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 493 | * tomoyo_write_no_rewrite - Write "struct tomoyo_no_rewrite" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 494 | * |
| 495 | * @data: String to parse. |
| 496 | * @is_delete: True if it is a delete request. |
| 497 | * |
| 498 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 499 | * |
| 500 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 501 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 502 | int tomoyo_write_no_rewrite(char *data, const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 503 | { |
| 504 | return tomoyo_update_no_rewrite_entry(data, is_delete); |
| 505 | } |
| 506 | |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 507 | static bool tomoyo_check_path_acl(const struct tomoyo_request_info *r, |
| 508 | const struct tomoyo_acl_info *ptr) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 509 | { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 510 | const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl), |
| 511 | head); |
| 512 | return (acl->perm & (1 << r->param.path.operation)) && |
| 513 | tomoyo_compare_name_union(r->param.path.filename, &acl->name); |
| 514 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 515 | |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 516 | static bool tomoyo_check_path_number_acl(const struct tomoyo_request_info *r, |
| 517 | const struct tomoyo_acl_info *ptr) |
| 518 | { |
| 519 | const struct tomoyo_path_number_acl *acl = |
| 520 | container_of(ptr, typeof(*acl), head); |
| 521 | return (acl->perm & (1 << r->param.path_number.operation)) && |
| 522 | tomoyo_compare_number_union(r->param.path_number.number, |
| 523 | &acl->number) && |
| 524 | tomoyo_compare_name_union(r->param.path_number.filename, |
| 525 | &acl->name); |
| 526 | } |
| 527 | |
| 528 | static bool tomoyo_check_path2_acl(const struct tomoyo_request_info *r, |
| 529 | const struct tomoyo_acl_info *ptr) |
| 530 | { |
| 531 | const struct tomoyo_path2_acl *acl = |
| 532 | container_of(ptr, typeof(*acl), head); |
| 533 | return (acl->perm & (1 << r->param.path2.operation)) && |
| 534 | tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1) |
| 535 | && tomoyo_compare_name_union(r->param.path2.filename2, |
| 536 | &acl->name2); |
| 537 | } |
| 538 | |
| 539 | static bool tomoyo_check_mkdev_acl(const struct tomoyo_request_info *r, |
| 540 | const struct tomoyo_acl_info *ptr) |
| 541 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 542 | const struct tomoyo_mkdev_acl *acl = |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 543 | container_of(ptr, typeof(*acl), head); |
| 544 | return (acl->perm & (1 << r->param.mkdev.operation)) && |
| 545 | tomoyo_compare_number_union(r->param.mkdev.mode, |
| 546 | &acl->mode) && |
| 547 | tomoyo_compare_number_union(r->param.mkdev.major, |
| 548 | &acl->major) && |
| 549 | tomoyo_compare_number_union(r->param.mkdev.minor, |
| 550 | &acl->minor) && |
| 551 | tomoyo_compare_name_union(r->param.mkdev.filename, |
| 552 | &acl->name); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 553 | } |
| 554 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 555 | static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a, |
| 556 | const struct tomoyo_acl_info *b) |
| 557 | { |
| 558 | const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head); |
| 559 | const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 560 | return tomoyo_same_acl_head(&p1->head, &p2->head) && |
| 561 | tomoyo_same_name_union(&p1->name, &p2->name); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a, |
| 565 | struct tomoyo_acl_info *b, |
| 566 | const bool is_delete) |
| 567 | { |
| 568 | u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head) |
| 569 | ->perm; |
| 570 | u16 perm = *a_perm; |
| 571 | const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm; |
| 572 | if (is_delete) { |
| 573 | perm &= ~b_perm; |
| 574 | if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK) |
| 575 | perm &= ~(1 << TOMOYO_TYPE_READ_WRITE); |
| 576 | else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE))) |
| 577 | perm &= ~TOMOYO_RW_MASK; |
| 578 | } else { |
| 579 | perm |= b_perm; |
| 580 | if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK) |
| 581 | perm |= (1 << TOMOYO_TYPE_READ_WRITE); |
| 582 | else if (perm & (1 << TOMOYO_TYPE_READ_WRITE)) |
| 583 | perm |= TOMOYO_RW_MASK; |
| 584 | } |
| 585 | *a_perm = perm; |
| 586 | return !perm; |
| 587 | } |
| 588 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 589 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 590 | * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 591 | * |
| 592 | * @type: Type of operation. |
| 593 | * @filename: Filename. |
| 594 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 595 | * @is_delete: True if it is a delete request. |
| 596 | * |
| 597 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 598 | * |
| 599 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 600 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 601 | static int tomoyo_update_path_acl(const u8 type, const char *filename, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 602 | struct tomoyo_domain_info * const domain, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 603 | const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 604 | { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 605 | struct tomoyo_path_acl e = { |
| 606 | .head.type = TOMOYO_TYPE_PATH_ACL, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 607 | .perm = 1 << type |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 608 | }; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 609 | int error; |
| 610 | if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE)) |
| 611 | e.perm |= TOMOYO_RW_MASK; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 612 | if (!tomoyo_parse_name_union(filename, &e.name)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 613 | return -EINVAL; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 614 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
| 615 | tomoyo_same_path_acl, |
| 616 | tomoyo_merge_path_acl); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 617 | tomoyo_put_name_union(&e.name); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 618 | return error; |
| 619 | } |
| 620 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 621 | static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 622 | const struct tomoyo_acl_info *b) |
| 623 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 624 | const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 625 | head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 626 | const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 627 | head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 628 | return tomoyo_same_acl_head(&p1->head, &p2->head) |
| 629 | && tomoyo_same_name_union(&p1->name, &p2->name) |
| 630 | && tomoyo_same_number_union(&p1->mode, &p2->mode) |
| 631 | && tomoyo_same_number_union(&p1->major, &p2->major) |
| 632 | && tomoyo_same_number_union(&p1->minor, &p2->minor); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 633 | } |
| 634 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 635 | static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 636 | struct tomoyo_acl_info *b, |
| 637 | const bool is_delete) |
| 638 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 639 | u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 640 | head)->perm; |
| 641 | u8 perm = *a_perm; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 642 | const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 643 | ->perm; |
| 644 | if (is_delete) |
| 645 | perm &= ~b_perm; |
| 646 | else |
| 647 | perm |= b_perm; |
| 648 | *a_perm = perm; |
| 649 | return !perm; |
| 650 | } |
| 651 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 652 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 653 | * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list. |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 654 | * |
| 655 | * @type: Type of operation. |
| 656 | * @filename: Filename. |
| 657 | * @mode: Create mode. |
| 658 | * @major: Device major number. |
| 659 | * @minor: Device minor number. |
| 660 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 661 | * @is_delete: True if it is a delete request. |
| 662 | * |
| 663 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 664 | * |
| 665 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 666 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 667 | static int tomoyo_update_mkdev_acl(const u8 type, const char *filename, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 668 | char *mode, char *major, char *minor, |
| 669 | struct tomoyo_domain_info * const |
| 670 | domain, const bool is_delete) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 671 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 672 | struct tomoyo_mkdev_acl e = { |
| 673 | .head.type = TOMOYO_TYPE_MKDEV_ACL, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 674 | .perm = 1 << type |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 675 | }; |
| 676 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 677 | if (!tomoyo_parse_name_union(filename, &e.name) || |
| 678 | !tomoyo_parse_number_union(mode, &e.mode) || |
| 679 | !tomoyo_parse_number_union(major, &e.major) || |
| 680 | !tomoyo_parse_number_union(minor, &e.minor)) |
| 681 | goto out; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 682 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 683 | tomoyo_same_mkdev_acl, |
| 684 | tomoyo_merge_mkdev_acl); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 685 | out: |
| 686 | tomoyo_put_name_union(&e.name); |
| 687 | tomoyo_put_number_union(&e.mode); |
| 688 | tomoyo_put_number_union(&e.major); |
| 689 | tomoyo_put_number_union(&e.minor); |
| 690 | return error; |
| 691 | } |
| 692 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 693 | static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a, |
| 694 | const struct tomoyo_acl_info *b) |
| 695 | { |
| 696 | const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head); |
| 697 | const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 698 | return tomoyo_same_acl_head(&p1->head, &p2->head) |
| 699 | && tomoyo_same_name_union(&p1->name1, &p2->name1) |
| 700 | && tomoyo_same_name_union(&p1->name2, &p2->name2); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a, |
| 704 | struct tomoyo_acl_info *b, |
| 705 | const bool is_delete) |
| 706 | { |
| 707 | u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head) |
| 708 | ->perm; |
| 709 | u8 perm = *a_perm; |
| 710 | const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm; |
| 711 | if (is_delete) |
| 712 | perm &= ~b_perm; |
| 713 | else |
| 714 | perm |= b_perm; |
| 715 | *a_perm = perm; |
| 716 | return !perm; |
| 717 | } |
| 718 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 719 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 720 | * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 721 | * |
| 722 | * @type: Type of operation. |
| 723 | * @filename1: First filename. |
| 724 | * @filename2: Second filename. |
| 725 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 726 | * @is_delete: True if it is a delete request. |
| 727 | * |
| 728 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 729 | * |
| 730 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 731 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 732 | static int tomoyo_update_path2_acl(const u8 type, const char *filename1, |
| 733 | const char *filename2, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 734 | struct tomoyo_domain_info * const domain, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 735 | const bool is_delete) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 736 | { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 737 | struct tomoyo_path2_acl e = { |
| 738 | .head.type = TOMOYO_TYPE_PATH2_ACL, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 739 | .perm = 1 << type |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 740 | }; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 741 | int error = is_delete ? -ENOENT : -ENOMEM; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 742 | if (!tomoyo_parse_name_union(filename1, &e.name1) || |
| 743 | !tomoyo_parse_name_union(filename2, &e.name2)) |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 744 | goto out; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 745 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
| 746 | tomoyo_same_path2_acl, |
| 747 | tomoyo_merge_path2_acl); |
Tetsuo Handa | ca0b7df | 2010-02-07 20:23:59 +0900 | [diff] [blame] | 748 | out: |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 749 | tomoyo_put_name_union(&e.name1); |
| 750 | tomoyo_put_name_union(&e.name2); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 751 | return error; |
| 752 | } |
| 753 | |
| 754 | /** |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 755 | * tomoyo_path_permission - Check permission for single path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 756 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 757 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 758 | * @operation: Type of operation. |
| 759 | * @filename: Filename to check. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 760 | * |
| 761 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 762 | * |
| 763 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 764 | */ |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 765 | int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation, |
| 766 | const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 767 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 768 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 769 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 770 | next: |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 771 | r->type = tomoyo_p2mac[operation]; |
| 772 | r->mode = tomoyo_get_mode(r->profile, r->type); |
| 773 | if (r->mode == TOMOYO_CONFIG_DISABLED) |
| 774 | return 0; |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 775 | r->param_type = TOMOYO_TYPE_PATH_ACL; |
| 776 | r->param.path.filename = filename; |
| 777 | r->param.path.operation = operation; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 778 | do { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 779 | tomoyo_check_acl(r, tomoyo_check_path_acl); |
| 780 | if (!r->granted && operation == TOMOYO_TYPE_READ && |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 781 | !r->domain->ignore_global_allow_read && |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 782 | tomoyo_globally_readable_file(filename)) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 783 | r->granted = true; |
| 784 | error = tomoyo_audit_path_log(r); |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 785 | /* |
| 786 | * Do not retry for execute request, for alias may have |
| 787 | * changed. |
| 788 | */ |
| 789 | } while (error == TOMOYO_RETRY_REQUEST && |
| 790 | operation != TOMOYO_TYPE_EXECUTE); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 791 | /* |
| 792 | * Since "allow_truncate" doesn't imply "allow_rewrite" permission, |
| 793 | * we need to check "allow_rewrite" permission if the filename is |
| 794 | * specified by "deny_rewrite" keyword. |
| 795 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 796 | if (!error && operation == TOMOYO_TYPE_TRUNCATE && |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 797 | tomoyo_no_rewrite_file(filename)) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 798 | operation = TOMOYO_TYPE_REWRITE; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 799 | goto next; |
| 800 | } |
| 801 | return error; |
| 802 | } |
| 803 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 804 | static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a, |
| 805 | const struct tomoyo_acl_info *b) |
| 806 | { |
| 807 | const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1), |
| 808 | head); |
| 809 | const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2), |
| 810 | head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 811 | return tomoyo_same_acl_head(&p1->head, &p2->head) |
| 812 | && tomoyo_same_name_union(&p1->name, &p2->name) |
| 813 | && tomoyo_same_number_union(&p1->number, &p2->number); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a, |
| 817 | struct tomoyo_acl_info *b, |
| 818 | const bool is_delete) |
| 819 | { |
| 820 | u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl, |
| 821 | head)->perm; |
| 822 | u8 perm = *a_perm; |
| 823 | const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head) |
| 824 | ->perm; |
| 825 | if (is_delete) |
| 826 | perm &= ~b_perm; |
| 827 | else |
| 828 | perm |= b_perm; |
| 829 | *a_perm = perm; |
| 830 | return !perm; |
| 831 | } |
| 832 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 833 | /** |
| 834 | * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL. |
| 835 | * |
| 836 | * @type: Type of operation. |
| 837 | * @filename: Filename. |
| 838 | * @number: Number. |
| 839 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 840 | * @is_delete: True if it is a delete request. |
| 841 | * |
| 842 | * Returns 0 on success, negative value otherwise. |
| 843 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 844 | static int tomoyo_update_path_number_acl(const u8 type, const char *filename, |
| 845 | char *number, |
| 846 | struct tomoyo_domain_info * const |
| 847 | domain, |
| 848 | const bool is_delete) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 849 | { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 850 | struct tomoyo_path_number_acl e = { |
| 851 | .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 852 | .perm = 1 << type |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 853 | }; |
| 854 | int error = is_delete ? -ENOENT : -ENOMEM; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 855 | if (!tomoyo_parse_name_union(filename, &e.name)) |
| 856 | return -EINVAL; |
| 857 | if (!tomoyo_parse_number_union(number, &e.number)) |
| 858 | goto out; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 859 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
| 860 | tomoyo_same_path_number_acl, |
| 861 | tomoyo_merge_path_number_acl); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 862 | out: |
| 863 | tomoyo_put_name_union(&e.name); |
| 864 | tomoyo_put_number_union(&e.number); |
| 865 | return error; |
| 866 | } |
| 867 | |
| 868 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 869 | * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp". |
| 870 | * |
| 871 | * @type: Type of operation. |
| 872 | * @path: Pointer to "struct path". |
| 873 | * @number: Number. |
| 874 | * |
| 875 | * Returns 0 on success, negative value otherwise. |
| 876 | */ |
| 877 | int tomoyo_path_number_perm(const u8 type, struct path *path, |
| 878 | unsigned long number) |
| 879 | { |
| 880 | struct tomoyo_request_info r; |
| 881 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 882 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 883 | int idx; |
| 884 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 885 | if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type]) |
| 886 | == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 887 | return 0; |
| 888 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 889 | if (!tomoyo_get_realpath(&buf, path)) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 890 | goto out; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 891 | if (type == TOMOYO_TYPE_MKDIR) |
| 892 | tomoyo_add_slash(&buf); |
Tetsuo Handa | cb917cf | 2010-06-16 16:28:21 +0900 | [diff] [blame] | 893 | r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL; |
| 894 | r.param.path_number.operation = type; |
| 895 | r.param.path_number.filename = &buf; |
| 896 | r.param.path_number.number = number; |
| 897 | do { |
| 898 | tomoyo_check_acl(&r, tomoyo_check_path_number_acl); |
| 899 | error = tomoyo_audit_path_number_log(&r); |
| 900 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 901 | kfree(buf.name); |
Tetsuo Handa | cb917cf | 2010-06-16 16:28:21 +0900 | [diff] [blame] | 902 | out: |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 903 | tomoyo_read_unlock(idx); |
| 904 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 905 | error = 0; |
| 906 | return error; |
| 907 | } |
| 908 | |
| 909 | /** |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 910 | * tomoyo_check_open_permission - Check permission for "read" and "write". |
| 911 | * |
| 912 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 913 | * @path: Pointer to "struct path". |
| 914 | * @flag: Flags for open(). |
| 915 | * |
| 916 | * Returns 0 on success, negative value otherwise. |
| 917 | */ |
| 918 | int tomoyo_check_open_permission(struct tomoyo_domain_info *domain, |
| 919 | struct path *path, const int flag) |
| 920 | { |
| 921 | const u8 acc_mode = ACC_MODE(flag); |
| 922 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 923 | struct tomoyo_path_info buf; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 924 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 925 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 926 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 927 | if (!path->mnt || |
| 928 | (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 929 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 930 | buf.name = NULL; |
| 931 | r.mode = TOMOYO_CONFIG_DISABLED; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 932 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 933 | if (!tomoyo_get_realpath(&buf, path)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 934 | goto out; |
| 935 | error = 0; |
| 936 | /* |
| 937 | * If the filename is specified by "deny_rewrite" keyword, |
| 938 | * we need to check "allow_rewrite" permission when the filename is not |
| 939 | * opened for append mode or the filename is truncated at open time. |
| 940 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 941 | if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND) |
| 942 | && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE) |
| 943 | != TOMOYO_CONFIG_DISABLED) { |
| 944 | if (!tomoyo_get_realpath(&buf, path)) { |
| 945 | error = -ENOMEM; |
| 946 | goto out; |
| 947 | } |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 948 | if (tomoyo_no_rewrite_file(&buf)) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 949 | error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, |
| 950 | &buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 951 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 952 | if (!error && acc_mode && |
| 953 | tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN) |
| 954 | != TOMOYO_CONFIG_DISABLED) { |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 955 | u8 operation; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 956 | if (!buf.name && !tomoyo_get_realpath(&buf, path)) { |
| 957 | error = -ENOMEM; |
| 958 | goto out; |
| 959 | } |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 960 | if (acc_mode == (MAY_READ | MAY_WRITE)) |
| 961 | operation = TOMOYO_TYPE_READ_WRITE; |
| 962 | else if (acc_mode == MAY_READ) |
| 963 | operation = TOMOYO_TYPE_READ; |
| 964 | else |
| 965 | operation = TOMOYO_TYPE_WRITE; |
| 966 | error = tomoyo_path_permission(&r, operation, &buf); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 967 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 968 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 969 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 970 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 971 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 972 | error = 0; |
| 973 | return error; |
| 974 | } |
| 975 | |
| 976 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 977 | * 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] | 978 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 979 | * @operation: Type of operation. |
| 980 | * @path: Pointer to "struct path". |
| 981 | * |
| 982 | * Returns 0 on success, negative value otherwise. |
| 983 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 984 | int tomoyo_path_perm(const u8 operation, struct path *path) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 985 | { |
| 986 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 987 | struct tomoyo_path_info buf; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 988 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 989 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 990 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 991 | if (!path->mnt) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 992 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 993 | if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation]) |
| 994 | == TOMOYO_CONFIG_DISABLED) |
| 995 | return 0; |
| 996 | buf.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 997 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 998 | if (!tomoyo_get_realpath(&buf, path)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 999 | goto out; |
| 1000 | switch (operation) { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1001 | case TOMOYO_TYPE_REWRITE: |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1002 | if (!tomoyo_no_rewrite_file(&buf)) { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1003 | error = 0; |
| 1004 | goto out; |
| 1005 | } |
| 1006 | break; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1007 | case TOMOYO_TYPE_RMDIR: |
| 1008 | case TOMOYO_TYPE_CHROOT: |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1009 | case TOMOYO_TYPE_UMOUNT: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1010 | tomoyo_add_slash(&buf); |
| 1011 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1012 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1013 | error = tomoyo_path_permission(&r, operation, &buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1014 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1015 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1016 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1017 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1018 | error = 0; |
| 1019 | return error; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1023 | * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1024 | * |
| 1025 | * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK) |
| 1026 | * @path: Pointer to "struct path". |
| 1027 | * @mode: Create mode. |
| 1028 | * @dev: Device number. |
| 1029 | * |
| 1030 | * Returns 0 on success, negative value otherwise. |
| 1031 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1032 | int tomoyo_mkdev_perm(const u8 operation, struct path *path, |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1033 | const unsigned int mode, unsigned int dev) |
| 1034 | { |
| 1035 | struct tomoyo_request_info r; |
| 1036 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1037 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1038 | int idx; |
| 1039 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1040 | if (!path->mnt || |
| 1041 | tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation]) |
| 1042 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1043 | return 0; |
| 1044 | idx = tomoyo_read_lock(); |
| 1045 | error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1046 | if (tomoyo_get_realpath(&buf, path)) { |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 1047 | dev = new_decode_dev(dev); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1048 | r.param_type = TOMOYO_TYPE_MKDEV_ACL; |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 1049 | r.param.mkdev.filename = &buf; |
| 1050 | r.param.mkdev.operation = operation; |
| 1051 | r.param.mkdev.mode = mode; |
| 1052 | r.param.mkdev.major = MAJOR(dev); |
| 1053 | r.param.mkdev.minor = MINOR(dev); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 1054 | tomoyo_check_acl(&r, tomoyo_check_mkdev_acl); |
| 1055 | error = tomoyo_audit_mkdev_log(&r); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1056 | kfree(buf.name); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1057 | } |
| 1058 | tomoyo_read_unlock(idx); |
| 1059 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 1060 | error = 0; |
| 1061 | return error; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1065 | * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1066 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1067 | * @operation: Type of operation. |
| 1068 | * @path1: Pointer to "struct path". |
| 1069 | * @path2: Pointer to "struct path". |
| 1070 | * |
| 1071 | * Returns 0 on success, negative value otherwise. |
| 1072 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 1073 | int tomoyo_path2_perm(const u8 operation, struct path *path1, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 1074 | struct path *path2) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1075 | { |
| 1076 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1077 | struct tomoyo_path_info buf1; |
| 1078 | struct tomoyo_path_info buf2; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1079 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1080 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1081 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1082 | if (!path1->mnt || !path2->mnt || |
| 1083 | tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation]) |
| 1084 | == TOMOYO_CONFIG_DISABLED) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1085 | return 0; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1086 | buf1.name = NULL; |
| 1087 | buf2.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1088 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1089 | if (!tomoyo_get_realpath(&buf1, path1) || |
| 1090 | !tomoyo_get_realpath(&buf2, path2)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1091 | goto out; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1092 | switch (operation) { |
| 1093 | struct dentry *dentry; |
| 1094 | case TOMOYO_TYPE_RENAME: |
| 1095 | case TOMOYO_TYPE_LINK: |
| 1096 | dentry = path1->dentry; |
| 1097 | if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode)) |
| 1098 | break; |
| 1099 | /* fall through */ |
| 1100 | case TOMOYO_TYPE_PIVOT_ROOT: |
| 1101 | tomoyo_add_slash(&buf1); |
| 1102 | tomoyo_add_slash(&buf2); |
| 1103 | break; |
| 1104 | } |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 1105 | r.param_type = TOMOYO_TYPE_PATH2_ACL; |
| 1106 | r.param.path2.operation = operation; |
| 1107 | r.param.path2.filename1 = &buf1; |
| 1108 | r.param.path2.filename2 = &buf2; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1109 | do { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 1110 | tomoyo_check_acl(&r, tomoyo_check_path2_acl); |
| 1111 | error = tomoyo_audit_path2_log(&r); |
| 1112 | } while (error == TOMOYO_RETRY_REQUEST); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1113 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 1114 | kfree(buf1.name); |
| 1115 | kfree(buf2.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1116 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 1117 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 1118 | error = 0; |
| 1119 | return error; |
| 1120 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1121 | |
| 1122 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1123 | * tomoyo_write_file - Update file related list. |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1124 | * |
| 1125 | * @data: String to parse. |
| 1126 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1127 | * @is_delete: True if it is a delete request. |
| 1128 | * |
| 1129 | * Returns 0 on success, negative value otherwise. |
| 1130 | * |
| 1131 | * Caller holds tomoyo_read_lock(). |
| 1132 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1133 | int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain, |
| 1134 | const bool is_delete) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1135 | { |
| 1136 | char *w[5]; |
| 1137 | u8 type; |
| 1138 | if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0]) |
| 1139 | return -EINVAL; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 1140 | if (strncmp(w[0], "allow_", 6)) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1141 | goto out; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1142 | w[0] += 6; |
| 1143 | for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) { |
| 1144 | if (strcmp(w[0], tomoyo_path_keyword[type])) |
| 1145 | continue; |
| 1146 | return tomoyo_update_path_acl(type, w[1], domain, is_delete); |
| 1147 | } |
| 1148 | if (!w[2][0]) |
| 1149 | goto out; |
| 1150 | for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) { |
| 1151 | if (strcmp(w[0], tomoyo_path2_keyword[type])) |
| 1152 | continue; |
| 1153 | return tomoyo_update_path2_acl(type, w[1], w[2], domain, |
| 1154 | is_delete); |
| 1155 | } |
| 1156 | for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) { |
| 1157 | if (strcmp(w[0], tomoyo_path_number_keyword[type])) |
| 1158 | continue; |
| 1159 | return tomoyo_update_path_number_acl(type, w[1], w[2], domain, |
| 1160 | is_delete); |
| 1161 | } |
| 1162 | if (!w[3][0] || !w[4][0]) |
| 1163 | goto out; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1164 | for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) { |
| 1165 | if (strcmp(w[0], tomoyo_mkdev_keyword[type])) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1166 | continue; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1167 | return tomoyo_update_mkdev_acl(type, w[1], w[2], w[3], |
| 1168 | w[4], domain, is_delete); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1169 | } |
| 1170 | out: |
| 1171 | return -EINVAL; |
| 1172 | } |