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_EXECUTE] = "execute", |
| 15 | [TOMOYO_TYPE_READ] = "read", |
| 16 | [TOMOYO_TYPE_WRITE] = "write", |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 17 | [TOMOYO_TYPE_APPEND] = "append", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 18 | [TOMOYO_TYPE_UNLINK] = "unlink", |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 19 | [TOMOYO_TYPE_GETATTR] = "getattr", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 20 | [TOMOYO_TYPE_RMDIR] = "rmdir", |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 21 | [TOMOYO_TYPE_TRUNCATE] = "truncate", |
| 22 | [TOMOYO_TYPE_SYMLINK] = "symlink", |
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 | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 52 | /* |
| 53 | * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index". |
| 54 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 55 | static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 56 | [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE, |
| 57 | [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN, |
| 58 | [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 59 | [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN, |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 60 | [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 61 | [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR, |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 62 | [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR, |
| 63 | [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE, |
| 64 | [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK, |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 65 | [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT, |
| 66 | [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT, |
| 67 | }; |
| 68 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 69 | /* |
| 70 | * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index". |
| 71 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 72 | static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 73 | [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK, |
| 74 | [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR, |
| 75 | }; |
| 76 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 77 | /* |
| 78 | * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index". |
| 79 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 80 | static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = { |
| 81 | [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK, |
| 82 | [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME, |
| 83 | [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT, |
| 84 | }; |
| 85 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 86 | /* |
| 87 | * Mapping table from "enum tomoyo_path_number_acl_index" to |
| 88 | * "enum tomoyo_mac_index". |
| 89 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 90 | static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = { |
| 91 | [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE, |
| 92 | [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR, |
| 93 | [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO, |
| 94 | [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK, |
| 95 | [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL, |
| 96 | [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD, |
| 97 | [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN, |
| 98 | [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP, |
| 99 | }; |
| 100 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 101 | /** |
| 102 | * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union". |
| 103 | * |
| 104 | * @ptr: Pointer to "struct tomoyo_name_union". |
| 105 | * |
| 106 | * Returns nothing. |
| 107 | */ |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 108 | void tomoyo_put_name_union(struct tomoyo_name_union *ptr) |
| 109 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 110 | tomoyo_put_group(ptr->group); |
| 111 | tomoyo_put_name(ptr->filename); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 114 | /** |
| 115 | * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not. |
| 116 | * |
| 117 | * @name: Pointer to "struct tomoyo_path_info". |
| 118 | * @ptr: Pointer to "struct tomoyo_name_union". |
| 119 | * |
| 120 | * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise. |
| 121 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 122 | const struct tomoyo_path_info * |
| 123 | tomoyo_compare_name_union(const struct tomoyo_path_info *name, |
| 124 | const struct tomoyo_name_union *ptr) |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 125 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 126 | if (ptr->group) |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 127 | return tomoyo_path_matches_group(name, ptr->group); |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 128 | if (tomoyo_path_matches_pattern(name, ptr->filename)) |
| 129 | return ptr->filename; |
| 130 | return NULL; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 133 | /** |
| 134 | * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union". |
| 135 | * |
| 136 | * @ptr: Pointer to "struct tomoyo_number_union". |
| 137 | * |
| 138 | * Returns nothing. |
| 139 | */ |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 140 | void tomoyo_put_number_union(struct tomoyo_number_union *ptr) |
| 141 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 142 | tomoyo_put_group(ptr->group); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 145 | /** |
| 146 | * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not. |
| 147 | * |
| 148 | * @value: Number to check. |
| 149 | * @ptr: Pointer to "struct tomoyo_number_union". |
| 150 | * |
| 151 | * Returns true if @value matches @ptr, false otherwise. |
| 152 | */ |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 153 | bool tomoyo_compare_number_union(const unsigned long value, |
| 154 | const struct tomoyo_number_union *ptr) |
| 155 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 156 | if (ptr->group) |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 157 | return tomoyo_number_matches_group(value, value, ptr->group); |
| 158 | return value >= ptr->values[0] && value <= ptr->values[1]; |
| 159 | } |
| 160 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 161 | /** |
| 162 | * tomoyo_add_slash - Add trailing '/' if needed. |
| 163 | * |
| 164 | * @buf: Pointer to "struct tomoyo_path_info". |
| 165 | * |
| 166 | * Returns nothing. |
| 167 | * |
| 168 | * @buf must be generated by tomoyo_encode() because this function does not |
| 169 | * allocate memory for adding '/'. |
| 170 | */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 171 | static void tomoyo_add_slash(struct tomoyo_path_info *buf) |
| 172 | { |
| 173 | if (buf->is_dir) |
| 174 | return; |
| 175 | /* |
| 176 | * This is OK because tomoyo_encode() reserves space for appending "/". |
| 177 | */ |
| 178 | strcat((char *) buf->name, "/"); |
| 179 | tomoyo_fill_path_info(buf); |
| 180 | } |
| 181 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 182 | /** |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 183 | * tomoyo_get_realpath - Get realpath. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 184 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 185 | * @buf: Pointer to "struct tomoyo_path_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 186 | * @path: Pointer to "struct path". |
| 187 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 188 | * Returns true on success, false otherwise. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 189 | */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 190 | 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] | 191 | { |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 192 | buf->name = tomoyo_realpath_from_path(path); |
| 193 | if (buf->name) { |
| 194 | tomoyo_fill_path_info(buf); |
| 195 | return true; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 196 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 197 | return false; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 200 | /** |
| 201 | * tomoyo_audit_path_log - Audit path request log. |
| 202 | * |
| 203 | * @r: Pointer to "struct tomoyo_request_info". |
| 204 | * |
| 205 | * Returns 0 on success, negative value otherwise. |
| 206 | */ |
| 207 | static int tomoyo_audit_path_log(struct tomoyo_request_info *r) |
| 208 | { |
| 209 | const char *operation = tomoyo_path_keyword[r->param.path.operation]; |
| 210 | const struct tomoyo_path_info *filename = r->param.path.filename; |
| 211 | if (r->granted) |
| 212 | return 0; |
| 213 | tomoyo_warn_log(r, "%s %s", operation, filename->name); |
| 214 | return tomoyo_supervisor(r, "allow_%s %s\n", operation, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 215 | filename->name); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * tomoyo_audit_path2_log - Audit path/path request log. |
| 220 | * |
| 221 | * @r: Pointer to "struct tomoyo_request_info". |
| 222 | * |
| 223 | * Returns 0 on success, negative value otherwise. |
| 224 | */ |
| 225 | static int tomoyo_audit_path2_log(struct tomoyo_request_info *r) |
| 226 | { |
| 227 | const char *operation = tomoyo_path2_keyword[r->param.path2.operation]; |
| 228 | const struct tomoyo_path_info *filename1 = r->param.path2.filename1; |
| 229 | const struct tomoyo_path_info *filename2 = r->param.path2.filename2; |
| 230 | if (r->granted) |
| 231 | return 0; |
| 232 | tomoyo_warn_log(r, "%s %s %s", operation, filename1->name, |
| 233 | filename2->name); |
| 234 | return tomoyo_supervisor(r, "allow_%s %s %s\n", operation, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 235 | filename1->name, filename2->name); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * tomoyo_audit_mkdev_log - Audit path/number/number/number request log. |
| 240 | * |
| 241 | * @r: Pointer to "struct tomoyo_request_info". |
| 242 | * |
| 243 | * Returns 0 on success, negative value otherwise. |
| 244 | */ |
| 245 | static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r) |
| 246 | { |
Tetsuo Handa | 71c2823 | 2010-06-16 16:26:38 +0900 | [diff] [blame] | 247 | const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation]; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 248 | const struct tomoyo_path_info *filename = r->param.mkdev.filename; |
| 249 | const unsigned int major = r->param.mkdev.major; |
| 250 | const unsigned int minor = r->param.mkdev.minor; |
| 251 | const unsigned int mode = r->param.mkdev.mode; |
| 252 | if (r->granted) |
| 253 | return 0; |
| 254 | tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode, |
| 255 | major, minor); |
| 256 | return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 257 | filename->name, mode, major, minor); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * tomoyo_audit_path_number_log - Audit path/number request log. |
| 262 | * |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 263 | * @r: Pointer to "struct tomoyo_request_info". |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 264 | * |
| 265 | * Returns 0 on success, negative value otherwise. |
| 266 | */ |
| 267 | static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r) |
| 268 | { |
| 269 | const u8 type = r->param.path_number.operation; |
| 270 | u8 radix; |
| 271 | const struct tomoyo_path_info *filename = r->param.path_number.filename; |
| 272 | const char *operation = tomoyo_path_number_keyword[type]; |
| 273 | char buffer[64]; |
| 274 | if (r->granted) |
| 275 | return 0; |
| 276 | switch (type) { |
| 277 | case TOMOYO_TYPE_CREATE: |
| 278 | case TOMOYO_TYPE_MKDIR: |
| 279 | case TOMOYO_TYPE_MKFIFO: |
| 280 | case TOMOYO_TYPE_MKSOCK: |
| 281 | case TOMOYO_TYPE_CHMOD: |
| 282 | radix = TOMOYO_VALUE_TYPE_OCTAL; |
| 283 | break; |
| 284 | case TOMOYO_TYPE_IOCTL: |
| 285 | radix = TOMOYO_VALUE_TYPE_HEXADECIMAL; |
| 286 | break; |
| 287 | default: |
| 288 | radix = TOMOYO_VALUE_TYPE_DECIMAL; |
| 289 | break; |
| 290 | } |
| 291 | tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number, |
| 292 | radix); |
| 293 | tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer); |
| 294 | return tomoyo_supervisor(r, "allow_%s %s %s\n", operation, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 295 | filename->name, buffer); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 298 | /** |
| 299 | * tomoyo_check_path_acl - Check permission for path operation. |
| 300 | * |
| 301 | * @r: Pointer to "struct tomoyo_request_info". |
| 302 | * @ptr: Pointer to "struct tomoyo_acl_info". |
| 303 | * |
| 304 | * Returns true if granted, false otherwise. |
| 305 | * |
| 306 | * To be able to use wildcard for domain transition, this function sets |
| 307 | * matching entry on success. Since the caller holds tomoyo_read_lock(), |
| 308 | * it is safe to set matching entry. |
| 309 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 310 | static bool tomoyo_check_path_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 311 | const struct tomoyo_acl_info *ptr) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 312 | { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 313 | const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl), |
| 314 | head); |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 315 | if (acl->perm & (1 << r->param.path.operation)) { |
| 316 | r->param.path.matched_path = |
| 317 | tomoyo_compare_name_union(r->param.path.filename, |
| 318 | &acl->name); |
| 319 | return r->param.path.matched_path != NULL; |
| 320 | } |
| 321 | return false; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 322 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 323 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 324 | /** |
| 325 | * tomoyo_check_path_number_acl - Check permission for path number operation. |
| 326 | * |
| 327 | * @r: Pointer to "struct tomoyo_request_info". |
| 328 | * @ptr: Pointer to "struct tomoyo_acl_info". |
| 329 | * |
| 330 | * Returns true if granted, false otherwise. |
| 331 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 332 | static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 333 | const struct tomoyo_acl_info *ptr) |
| 334 | { |
| 335 | const struct tomoyo_path_number_acl *acl = |
| 336 | container_of(ptr, typeof(*acl), head); |
| 337 | return (acl->perm & (1 << r->param.path_number.operation)) && |
| 338 | tomoyo_compare_number_union(r->param.path_number.number, |
| 339 | &acl->number) && |
| 340 | tomoyo_compare_name_union(r->param.path_number.filename, |
| 341 | &acl->name); |
| 342 | } |
| 343 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 344 | /** |
| 345 | * tomoyo_check_path2_acl - Check permission for path path operation. |
| 346 | * |
| 347 | * @r: Pointer to "struct tomoyo_request_info". |
| 348 | * @ptr: Pointer to "struct tomoyo_acl_info". |
| 349 | * |
| 350 | * Returns true if granted, false otherwise. |
| 351 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 352 | static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 353 | const struct tomoyo_acl_info *ptr) |
| 354 | { |
| 355 | const struct tomoyo_path2_acl *acl = |
| 356 | container_of(ptr, typeof(*acl), head); |
| 357 | return (acl->perm & (1 << r->param.path2.operation)) && |
| 358 | tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1) |
| 359 | && tomoyo_compare_name_union(r->param.path2.filename2, |
| 360 | &acl->name2); |
| 361 | } |
| 362 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 363 | /** |
| 364 | * tomoyo_check_mkdev_acl - Check permission for path number number number operation. |
| 365 | * |
| 366 | * @r: Pointer to "struct tomoyo_request_info". |
| 367 | * @ptr: Pointer to "struct tomoyo_acl_info". |
| 368 | * |
| 369 | * Returns true if granted, false otherwise. |
| 370 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 371 | static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 372 | const struct tomoyo_acl_info *ptr) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 373 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 374 | const struct tomoyo_mkdev_acl *acl = |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 375 | container_of(ptr, typeof(*acl), head); |
| 376 | return (acl->perm & (1 << r->param.mkdev.operation)) && |
| 377 | tomoyo_compare_number_union(r->param.mkdev.mode, |
| 378 | &acl->mode) && |
| 379 | tomoyo_compare_number_union(r->param.mkdev.major, |
| 380 | &acl->major) && |
| 381 | tomoyo_compare_number_union(r->param.mkdev.minor, |
| 382 | &acl->minor) && |
| 383 | tomoyo_compare_name_union(r->param.mkdev.filename, |
| 384 | &acl->name); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 385 | } |
| 386 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 387 | /** |
| 388 | * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry. |
| 389 | * |
| 390 | * @a: Pointer to "struct tomoyo_acl_info". |
| 391 | * @b: Pointer to "struct tomoyo_acl_info". |
| 392 | * |
| 393 | * Returns true if @a == @b except permission bits, false otherwise. |
| 394 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 395 | static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a, |
| 396 | const struct tomoyo_acl_info *b) |
| 397 | { |
| 398 | const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head); |
| 399 | const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 400 | return tomoyo_same_name_union(&p1->name, &p2->name); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 403 | /** |
| 404 | * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry. |
| 405 | * |
| 406 | * @a: Pointer to "struct tomoyo_acl_info". |
| 407 | * @b: Pointer to "struct tomoyo_acl_info". |
| 408 | * @is_delete: True for @a &= ~@b, false for @a |= @b. |
| 409 | * |
| 410 | * Returns true if @a is empty, false otherwise. |
| 411 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 412 | static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a, |
| 413 | struct tomoyo_acl_info *b, |
| 414 | const bool is_delete) |
| 415 | { |
| 416 | u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head) |
| 417 | ->perm; |
| 418 | u16 perm = *a_perm; |
| 419 | const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm; |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 420 | if (is_delete) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 421 | perm &= ~b_perm; |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 422 | else |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 423 | perm |= b_perm; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 424 | *a_perm = perm; |
| 425 | return !perm; |
| 426 | } |
| 427 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 428 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 429 | * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 430 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 431 | * @perm: Permission. |
| 432 | * @param: Pointer to "struct tomoyo_acl_param". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 433 | * |
| 434 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 435 | * |
| 436 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 437 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 438 | static int tomoyo_update_path_acl(const u16 perm, |
| 439 | struct tomoyo_acl_param *param) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 440 | { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 441 | struct tomoyo_path_acl e = { |
| 442 | .head.type = TOMOYO_TYPE_PATH_ACL, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 443 | .perm = perm |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 444 | }; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 445 | int error; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 446 | if (!tomoyo_parse_name_union(param, &e.name)) |
| 447 | error = -EINVAL; |
| 448 | else |
| 449 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 450 | tomoyo_same_path_acl, |
| 451 | tomoyo_merge_path_acl); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 452 | tomoyo_put_name_union(&e.name); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 453 | return error; |
| 454 | } |
| 455 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 456 | /** |
| 457 | * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry. |
| 458 | * |
| 459 | * @a: Pointer to "struct tomoyo_acl_info". |
| 460 | * @b: Pointer to "struct tomoyo_acl_info". |
| 461 | * |
| 462 | * Returns true if @a == @b except permission bits, false otherwise. |
| 463 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 464 | static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 465 | const struct tomoyo_acl_info *b) |
| 466 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 467 | const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head); |
| 468 | const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head); |
| 469 | return tomoyo_same_name_union(&p1->name, &p2->name) && |
| 470 | tomoyo_same_number_union(&p1->mode, &p2->mode) && |
| 471 | tomoyo_same_number_union(&p1->major, &p2->major) && |
| 472 | tomoyo_same_number_union(&p1->minor, &p2->minor); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 473 | } |
| 474 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 475 | /** |
| 476 | * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry. |
| 477 | * |
| 478 | * @a: Pointer to "struct tomoyo_acl_info". |
| 479 | * @b: Pointer to "struct tomoyo_acl_info". |
| 480 | * @is_delete: True for @a &= ~@b, false for @a |= @b. |
| 481 | * |
| 482 | * Returns true if @a is empty, false otherwise. |
| 483 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 484 | static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a, |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 485 | struct tomoyo_acl_info *b, |
| 486 | const bool is_delete) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 487 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 488 | u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl, |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 489 | head)->perm; |
| 490 | u8 perm = *a_perm; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 491 | const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 492 | ->perm; |
| 493 | if (is_delete) |
| 494 | perm &= ~b_perm; |
| 495 | else |
| 496 | perm |= b_perm; |
| 497 | *a_perm = perm; |
| 498 | return !perm; |
| 499 | } |
| 500 | |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 501 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 502 | * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list. |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 503 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 504 | * @perm: Permission. |
| 505 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 506 | * |
| 507 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 508 | * |
| 509 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 510 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 511 | static int tomoyo_update_mkdev_acl(const u8 perm, |
| 512 | struct tomoyo_acl_param *param) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 513 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 514 | struct tomoyo_mkdev_acl e = { |
| 515 | .head.type = TOMOYO_TYPE_MKDEV_ACL, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 516 | .perm = perm |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 517 | }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 518 | int error; |
| 519 | if (!tomoyo_parse_name_union(param, &e.name) || |
| 520 | !tomoyo_parse_number_union(param, &e.mode) || |
| 521 | !tomoyo_parse_number_union(param, &e.major) || |
| 522 | !tomoyo_parse_number_union(param, &e.minor)) |
| 523 | error = -EINVAL; |
| 524 | else |
| 525 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 526 | tomoyo_same_mkdev_acl, |
| 527 | tomoyo_merge_mkdev_acl); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 528 | tomoyo_put_name_union(&e.name); |
| 529 | tomoyo_put_number_union(&e.mode); |
| 530 | tomoyo_put_number_union(&e.major); |
| 531 | tomoyo_put_number_union(&e.minor); |
| 532 | return error; |
| 533 | } |
| 534 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 535 | /** |
| 536 | * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry. |
| 537 | * |
| 538 | * @a: Pointer to "struct tomoyo_acl_info". |
| 539 | * @b: Pointer to "struct tomoyo_acl_info". |
| 540 | * |
| 541 | * Returns true if @a == @b except permission bits, false otherwise. |
| 542 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 543 | static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a, |
| 544 | const struct tomoyo_acl_info *b) |
| 545 | { |
| 546 | const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head); |
| 547 | const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 548 | return tomoyo_same_name_union(&p1->name1, &p2->name1) && |
| 549 | tomoyo_same_name_union(&p1->name2, &p2->name2); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 550 | } |
| 551 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 552 | /** |
| 553 | * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry. |
| 554 | * |
| 555 | * @a: Pointer to "struct tomoyo_acl_info". |
| 556 | * @b: Pointer to "struct tomoyo_acl_info". |
| 557 | * @is_delete: True for @a &= ~@b, false for @a |= @b. |
| 558 | * |
| 559 | * Returns true if @a is empty, false otherwise. |
| 560 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 561 | static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a, |
| 562 | struct tomoyo_acl_info *b, |
| 563 | const bool is_delete) |
| 564 | { |
| 565 | u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head) |
| 566 | ->perm; |
| 567 | u8 perm = *a_perm; |
| 568 | const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm; |
| 569 | if (is_delete) |
| 570 | perm &= ~b_perm; |
| 571 | else |
| 572 | perm |= b_perm; |
| 573 | *a_perm = perm; |
| 574 | return !perm; |
| 575 | } |
| 576 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 577 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 578 | * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 579 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 580 | * @perm: Permission. |
| 581 | * @param: Pointer to "struct tomoyo_acl_param". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 582 | * |
| 583 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 584 | * |
| 585 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 586 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 587 | static int tomoyo_update_path2_acl(const u8 perm, |
| 588 | struct tomoyo_acl_param *param) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 589 | { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 590 | struct tomoyo_path2_acl e = { |
| 591 | .head.type = TOMOYO_TYPE_PATH2_ACL, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 592 | .perm = perm |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 593 | }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 594 | int error; |
| 595 | if (!tomoyo_parse_name_union(param, &e.name1) || |
| 596 | !tomoyo_parse_name_union(param, &e.name2)) |
| 597 | error = -EINVAL; |
| 598 | else |
| 599 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 600 | tomoyo_same_path2_acl, |
| 601 | tomoyo_merge_path2_acl); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 602 | tomoyo_put_name_union(&e.name1); |
| 603 | tomoyo_put_name_union(&e.name2); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 604 | return error; |
| 605 | } |
| 606 | |
| 607 | /** |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 608 | * tomoyo_path_permission - Check permission for single path operation. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 609 | * |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 610 | * @r: Pointer to "struct tomoyo_request_info". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 611 | * @operation: Type of operation. |
| 612 | * @filename: Filename to check. |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 613 | * |
| 614 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 615 | * |
| 616 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 617 | */ |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 618 | int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation, |
| 619 | const struct tomoyo_path_info *filename) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 620 | { |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 621 | int error; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 622 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 623 | r->type = tomoyo_p2mac[operation]; |
| 624 | r->mode = tomoyo_get_mode(r->profile, r->type); |
| 625 | if (r->mode == TOMOYO_CONFIG_DISABLED) |
| 626 | return 0; |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 627 | r->param_type = TOMOYO_TYPE_PATH_ACL; |
| 628 | r->param.path.filename = filename; |
| 629 | r->param.path.operation = operation; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 630 | do { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 631 | tomoyo_check_acl(r, tomoyo_check_path_acl); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 632 | error = tomoyo_audit_path_log(r); |
Tetsuo Handa | 05336de | 2010-06-16 16:20:24 +0900 | [diff] [blame] | 633 | /* |
| 634 | * Do not retry for execute request, for alias may have |
| 635 | * changed. |
| 636 | */ |
| 637 | } while (error == TOMOYO_RETRY_REQUEST && |
| 638 | operation != TOMOYO_TYPE_EXECUTE); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 639 | return error; |
| 640 | } |
| 641 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 642 | /** |
| 643 | * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry. |
| 644 | * |
| 645 | * @a: Pointer to "struct tomoyo_acl_info". |
| 646 | * @b: Pointer to "struct tomoyo_acl_info". |
| 647 | * |
| 648 | * Returns true if @a == @b except permission bits, false otherwise. |
| 649 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 650 | static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a, |
| 651 | const struct tomoyo_acl_info *b) |
| 652 | { |
| 653 | const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1), |
| 654 | head); |
| 655 | const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2), |
| 656 | head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 657 | return tomoyo_same_name_union(&p1->name, &p2->name) && |
| 658 | tomoyo_same_number_union(&p1->number, &p2->number); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 659 | } |
| 660 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 661 | /** |
| 662 | * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry. |
| 663 | * |
| 664 | * @a: Pointer to "struct tomoyo_acl_info". |
| 665 | * @b: Pointer to "struct tomoyo_acl_info". |
| 666 | * @is_delete: True for @a &= ~@b, false for @a |= @b. |
| 667 | * |
| 668 | * Returns true if @a is empty, false otherwise. |
| 669 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 670 | static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a, |
| 671 | struct tomoyo_acl_info *b, |
| 672 | const bool is_delete) |
| 673 | { |
| 674 | u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl, |
| 675 | head)->perm; |
| 676 | u8 perm = *a_perm; |
| 677 | const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head) |
| 678 | ->perm; |
| 679 | if (is_delete) |
| 680 | perm &= ~b_perm; |
| 681 | else |
| 682 | perm |= b_perm; |
| 683 | *a_perm = perm; |
| 684 | return !perm; |
| 685 | } |
| 686 | |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 687 | /** |
| 688 | * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL. |
| 689 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 690 | * @perm: Permission. |
| 691 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 692 | * |
| 693 | * Returns 0 on success, negative value otherwise. |
| 694 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 695 | static int tomoyo_update_path_number_acl(const u8 perm, |
| 696 | struct tomoyo_acl_param *param) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 697 | { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 698 | struct tomoyo_path_number_acl e = { |
| 699 | .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 700 | .perm = perm |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 701 | }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 702 | int error; |
| 703 | if (!tomoyo_parse_name_union(param, &e.name) || |
| 704 | !tomoyo_parse_number_union(param, &e.number)) |
| 705 | error = -EINVAL; |
| 706 | else |
| 707 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 708 | tomoyo_same_path_number_acl, |
| 709 | tomoyo_merge_path_number_acl); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 710 | tomoyo_put_name_union(&e.name); |
| 711 | tomoyo_put_number_union(&e.number); |
| 712 | return error; |
| 713 | } |
| 714 | |
| 715 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 716 | * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp". |
| 717 | * |
| 718 | * @type: Type of operation. |
| 719 | * @path: Pointer to "struct path". |
| 720 | * @number: Number. |
| 721 | * |
| 722 | * Returns 0 on success, negative value otherwise. |
| 723 | */ |
| 724 | int tomoyo_path_number_perm(const u8 type, struct path *path, |
| 725 | unsigned long number) |
| 726 | { |
| 727 | struct tomoyo_request_info r; |
| 728 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 729 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 730 | int idx; |
| 731 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 732 | if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type]) |
| 733 | == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 734 | return 0; |
| 735 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 736 | if (!tomoyo_get_realpath(&buf, path)) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 737 | goto out; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 738 | if (type == TOMOYO_TYPE_MKDIR) |
| 739 | tomoyo_add_slash(&buf); |
Tetsuo Handa | cb917cf | 2010-06-16 16:28:21 +0900 | [diff] [blame] | 740 | r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL; |
| 741 | r.param.path_number.operation = type; |
| 742 | r.param.path_number.filename = &buf; |
| 743 | r.param.path_number.number = number; |
| 744 | do { |
| 745 | tomoyo_check_acl(&r, tomoyo_check_path_number_acl); |
| 746 | error = tomoyo_audit_path_number_log(&r); |
| 747 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 748 | kfree(buf.name); |
Tetsuo Handa | cb917cf | 2010-06-16 16:28:21 +0900 | [diff] [blame] | 749 | out: |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 750 | tomoyo_read_unlock(idx); |
| 751 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 752 | error = 0; |
| 753 | return error; |
| 754 | } |
| 755 | |
| 756 | /** |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 757 | * tomoyo_check_open_permission - Check permission for "read" and "write". |
| 758 | * |
| 759 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 760 | * @path: Pointer to "struct path". |
| 761 | * @flag: Flags for open(). |
| 762 | * |
| 763 | * Returns 0 on success, negative value otherwise. |
| 764 | */ |
| 765 | int tomoyo_check_open_permission(struct tomoyo_domain_info *domain, |
| 766 | struct path *path, const int flag) |
| 767 | { |
| 768 | const u8 acc_mode = ACC_MODE(flag); |
Tetsuo Handa | eae61f3 | 2011-03-02 16:54:24 +0900 | [diff] [blame] | 769 | int error = 0; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 770 | struct tomoyo_path_info buf; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 771 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 772 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 773 | |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 774 | if (!path->mnt) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 775 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 776 | buf.name = NULL; |
| 777 | r.mode = TOMOYO_CONFIG_DISABLED; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 778 | idx = tomoyo_read_lock(); |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 779 | if (acc_mode && |
| 780 | tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 781 | != TOMOYO_CONFIG_DISABLED) { |
| 782 | if (!tomoyo_get_realpath(&buf, path)) { |
| 783 | error = -ENOMEM; |
| 784 | goto out; |
| 785 | } |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 786 | if (acc_mode & MAY_READ) |
| 787 | error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ, |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 788 | &buf); |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 789 | if (!error && (acc_mode & MAY_WRITE)) |
| 790 | error = tomoyo_path_permission(&r, (flag & O_APPEND) ? |
| 791 | TOMOYO_TYPE_APPEND : |
| 792 | TOMOYO_TYPE_WRITE, |
| 793 | &buf); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 794 | } |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 795 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 796 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 797 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 798 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 799 | error = 0; |
| 800 | return error; |
| 801 | } |
| 802 | |
| 803 | /** |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 804 | * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 805 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 806 | * @operation: Type of operation. |
| 807 | * @path: Pointer to "struct path". |
| 808 | * |
| 809 | * Returns 0 on success, negative value otherwise. |
| 810 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 811 | int tomoyo_path_perm(const u8 operation, struct path *path) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 812 | { |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 813 | struct tomoyo_request_info r; |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 814 | int error; |
| 815 | struct tomoyo_path_info buf; |
| 816 | bool is_enforce; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 817 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 818 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 819 | if (!path->mnt) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 820 | return 0; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 821 | if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation]) |
| 822 | == TOMOYO_CONFIG_DISABLED) |
| 823 | return 0; |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 824 | is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING); |
| 825 | error = -ENOMEM; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 826 | buf.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 827 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 828 | if (!tomoyo_get_realpath(&buf, path)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 829 | goto out; |
| 830 | switch (operation) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 831 | case TOMOYO_TYPE_RMDIR: |
| 832 | case TOMOYO_TYPE_CHROOT: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 833 | tomoyo_add_slash(&buf); |
| 834 | break; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 835 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 836 | error = tomoyo_path_permission(&r, operation, &buf); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 837 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 838 | kfree(buf.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 839 | tomoyo_read_unlock(idx); |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 840 | if (!is_enforce) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 841 | error = 0; |
| 842 | return error; |
| 843 | } |
| 844 | |
| 845 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 846 | * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 847 | * |
| 848 | * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK) |
| 849 | * @path: Pointer to "struct path". |
| 850 | * @mode: Create mode. |
| 851 | * @dev: Device number. |
| 852 | * |
| 853 | * Returns 0 on success, negative value otherwise. |
| 854 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 855 | int tomoyo_mkdev_perm(const u8 operation, struct path *path, |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 856 | const unsigned int mode, unsigned int dev) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 857 | { |
| 858 | struct tomoyo_request_info r; |
| 859 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 860 | struct tomoyo_path_info buf; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 861 | int idx; |
| 862 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 863 | if (!path->mnt || |
| 864 | tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation]) |
| 865 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 866 | return 0; |
| 867 | idx = tomoyo_read_lock(); |
| 868 | error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 869 | if (tomoyo_get_realpath(&buf, path)) { |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 870 | dev = new_decode_dev(dev); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 871 | r.param_type = TOMOYO_TYPE_MKDEV_ACL; |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 872 | r.param.mkdev.filename = &buf; |
| 873 | r.param.mkdev.operation = operation; |
| 874 | r.param.mkdev.mode = mode; |
| 875 | r.param.mkdev.major = MAJOR(dev); |
| 876 | r.param.mkdev.minor = MINOR(dev); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 877 | tomoyo_check_acl(&r, tomoyo_check_mkdev_acl); |
| 878 | error = tomoyo_audit_mkdev_log(&r); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 879 | kfree(buf.name); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 880 | } |
| 881 | tomoyo_read_unlock(idx); |
| 882 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
| 883 | error = 0; |
| 884 | return error; |
| 885 | } |
| 886 | |
| 887 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 888 | * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root". |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 889 | * |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 890 | * @operation: Type of operation. |
| 891 | * @path1: Pointer to "struct path". |
| 892 | * @path2: Pointer to "struct path". |
| 893 | * |
| 894 | * Returns 0 on success, negative value otherwise. |
| 895 | */ |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 896 | int tomoyo_path2_perm(const u8 operation, struct path *path1, |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 897 | struct path *path2) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 898 | { |
| 899 | int error = -ENOMEM; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 900 | struct tomoyo_path_info buf1; |
| 901 | struct tomoyo_path_info buf2; |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 902 | struct tomoyo_request_info r; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 903 | int idx; |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 904 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 905 | if (!path1->mnt || !path2->mnt || |
| 906 | tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation]) |
| 907 | == TOMOYO_CONFIG_DISABLED) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 908 | return 0; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 909 | buf1.name = NULL; |
| 910 | buf2.name = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 911 | idx = tomoyo_read_lock(); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 912 | if (!tomoyo_get_realpath(&buf1, path1) || |
| 913 | !tomoyo_get_realpath(&buf2, path2)) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 914 | goto out; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 915 | switch (operation) { |
| 916 | struct dentry *dentry; |
| 917 | case TOMOYO_TYPE_RENAME: |
| 918 | case TOMOYO_TYPE_LINK: |
| 919 | dentry = path1->dentry; |
| 920 | if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode)) |
| 921 | break; |
| 922 | /* fall through */ |
| 923 | case TOMOYO_TYPE_PIVOT_ROOT: |
| 924 | tomoyo_add_slash(&buf1); |
| 925 | tomoyo_add_slash(&buf2); |
| 926 | break; |
| 927 | } |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 928 | r.param_type = TOMOYO_TYPE_PATH2_ACL; |
| 929 | r.param.path2.operation = operation; |
| 930 | r.param.path2.filename1 = &buf1; |
| 931 | r.param.path2.filename2 = &buf2; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 932 | do { |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 933 | tomoyo_check_acl(&r, tomoyo_check_path2_acl); |
| 934 | error = tomoyo_audit_path2_log(&r); |
| 935 | } while (error == TOMOYO_RETRY_REQUEST); |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 936 | out: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 937 | kfree(buf1.name); |
| 938 | kfree(buf2.name); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 939 | tomoyo_read_unlock(idx); |
Tetsuo Handa | cb0abe6 | 2010-05-17 10:08:05 +0900 | [diff] [blame] | 940 | if (r.mode != TOMOYO_CONFIG_ENFORCING) |
Kentaro Takeda | b69a54e | 2009-02-05 17:18:14 +0900 | [diff] [blame] | 941 | error = 0; |
| 942 | return error; |
| 943 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 944 | |
| 945 | /** |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 946 | * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry. |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 947 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 948 | * @a: Pointer to "struct tomoyo_acl_info". |
| 949 | * @b: Pointer to "struct tomoyo_acl_info". |
| 950 | * |
| 951 | * Returns true if @a == @b, false otherwise. |
| 952 | */ |
| 953 | static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a, |
| 954 | const struct tomoyo_acl_info *b) |
| 955 | { |
| 956 | const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head); |
| 957 | const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head); |
| 958 | return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) && |
| 959 | tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) && |
| 960 | tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) && |
| 961 | tomoyo_same_number_union(&p1->flags, &p2->flags); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list. |
| 966 | * |
| 967 | * @param: Pointer to "struct tomoyo_acl_param". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 968 | * |
| 969 | * Returns 0 on success, negative value otherwise. |
| 970 | * |
| 971 | * Caller holds tomoyo_read_lock(). |
| 972 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 973 | static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 974 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 975 | struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL }; |
| 976 | int error; |
| 977 | if (!tomoyo_parse_name_union(param, &e.dev_name) || |
| 978 | !tomoyo_parse_name_union(param, &e.dir_name) || |
| 979 | !tomoyo_parse_name_union(param, &e.fs_type) || |
| 980 | !tomoyo_parse_number_union(param, &e.flags)) |
| 981 | error = -EINVAL; |
| 982 | else |
| 983 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 984 | tomoyo_same_mount_acl, NULL); |
| 985 | tomoyo_put_name_union(&e.dev_name); |
| 986 | tomoyo_put_name_union(&e.dir_name); |
| 987 | tomoyo_put_name_union(&e.fs_type); |
| 988 | tomoyo_put_number_union(&e.flags); |
| 989 | return error; |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * tomoyo_write_file - Update file related list. |
| 994 | * |
| 995 | * @param: Pointer to "struct tomoyo_acl_param". |
| 996 | * |
| 997 | * Returns 0 on success, negative value otherwise. |
| 998 | * |
| 999 | * Caller holds tomoyo_read_lock(). |
| 1000 | */ |
| 1001 | int tomoyo_write_file(struct tomoyo_acl_param *param) |
| 1002 | { |
| 1003 | u16 perm = 0; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1004 | u8 type; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame^] | 1005 | const char *operation = tomoyo_read_token(param); |
| 1006 | for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) |
| 1007 | if (tomoyo_permstr(operation, tomoyo_path_keyword[type])) |
| 1008 | perm |= 1 << type; |
| 1009 | if (perm) |
| 1010 | return tomoyo_update_path_acl(perm, param); |
| 1011 | for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) |
| 1012 | if (tomoyo_permstr(operation, tomoyo_path2_keyword[type])) |
| 1013 | perm |= 1 << type; |
| 1014 | if (perm) |
| 1015 | return tomoyo_update_path2_acl(perm, param); |
| 1016 | for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) |
| 1017 | if (tomoyo_permstr(operation, |
| 1018 | tomoyo_path_number_keyword[type])) |
| 1019 | perm |= 1 << type; |
| 1020 | if (perm) |
| 1021 | return tomoyo_update_path_number_acl(perm, param); |
| 1022 | for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) |
| 1023 | if (tomoyo_permstr(operation, tomoyo_mkdev_keyword[type])) |
| 1024 | perm |= 1 << type; |
| 1025 | if (perm) |
| 1026 | return tomoyo_update_mkdev_acl(perm, param); |
| 1027 | if (tomoyo_permstr(operation, "mount")) |
| 1028 | return tomoyo_update_mount_acl(param); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 1029 | return -EINVAL; |
| 1030 | } |