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