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