Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/common.c |
| 3 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 4 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 9 | #include <linux/security.h> |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 10 | #include "common.h" |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 11 | |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 12 | /* String table for operation mode. */ |
| 13 | const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = { |
| 14 | [TOMOYO_CONFIG_DISABLED] = "disabled", |
| 15 | [TOMOYO_CONFIG_LEARNING] = "learning", |
| 16 | [TOMOYO_CONFIG_PERMISSIVE] = "permissive", |
| 17 | [TOMOYO_CONFIG_ENFORCING] = "enforcing" |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 18 | }; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 19 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 20 | /* String table for /sys/kernel/security/tomoyo/profile */ |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 21 | const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 22 | + TOMOYO_MAX_MAC_CATEGORY_INDEX] = { |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 23 | [TOMOYO_MAC_FILE_EXECUTE] = "execute", |
| 24 | [TOMOYO_MAC_FILE_OPEN] = "open", |
| 25 | [TOMOYO_MAC_FILE_CREATE] = "create", |
| 26 | [TOMOYO_MAC_FILE_UNLINK] = "unlink", |
| 27 | [TOMOYO_MAC_FILE_GETATTR] = "getattr", |
| 28 | [TOMOYO_MAC_FILE_MKDIR] = "mkdir", |
| 29 | [TOMOYO_MAC_FILE_RMDIR] = "rmdir", |
| 30 | [TOMOYO_MAC_FILE_MKFIFO] = "mkfifo", |
| 31 | [TOMOYO_MAC_FILE_MKSOCK] = "mksock", |
| 32 | [TOMOYO_MAC_FILE_TRUNCATE] = "truncate", |
| 33 | [TOMOYO_MAC_FILE_SYMLINK] = "symlink", |
| 34 | [TOMOYO_MAC_FILE_MKBLOCK] = "mkblock", |
| 35 | [TOMOYO_MAC_FILE_MKCHAR] = "mkchar", |
| 36 | [TOMOYO_MAC_FILE_LINK] = "link", |
| 37 | [TOMOYO_MAC_FILE_RENAME] = "rename", |
| 38 | [TOMOYO_MAC_FILE_CHMOD] = "chmod", |
| 39 | [TOMOYO_MAC_FILE_CHOWN] = "chown", |
| 40 | [TOMOYO_MAC_FILE_CHGRP] = "chgrp", |
| 41 | [TOMOYO_MAC_FILE_IOCTL] = "ioctl", |
| 42 | [TOMOYO_MAC_FILE_CHROOT] = "chroot", |
| 43 | [TOMOYO_MAC_FILE_MOUNT] = "mount", |
| 44 | [TOMOYO_MAC_FILE_UMOUNT] = "unmount", |
| 45 | [TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root", |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 46 | [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file", |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 47 | }; |
| 48 | |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 49 | /* String table for conditions. */ |
| 50 | const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = { |
| 51 | [TOMOYO_TASK_UID] = "task.uid", |
| 52 | [TOMOYO_TASK_EUID] = "task.euid", |
| 53 | [TOMOYO_TASK_SUID] = "task.suid", |
| 54 | [TOMOYO_TASK_FSUID] = "task.fsuid", |
| 55 | [TOMOYO_TASK_GID] = "task.gid", |
| 56 | [TOMOYO_TASK_EGID] = "task.egid", |
| 57 | [TOMOYO_TASK_SGID] = "task.sgid", |
| 58 | [TOMOYO_TASK_FSGID] = "task.fsgid", |
| 59 | [TOMOYO_TASK_PID] = "task.pid", |
| 60 | [TOMOYO_TASK_PPID] = "task.ppid", |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 61 | [TOMOYO_EXEC_ARGC] = "exec.argc", |
| 62 | [TOMOYO_EXEC_ENVC] = "exec.envc", |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame] | 63 | [TOMOYO_TYPE_IS_SOCKET] = "socket", |
| 64 | [TOMOYO_TYPE_IS_SYMLINK] = "symlink", |
| 65 | [TOMOYO_TYPE_IS_FILE] = "file", |
| 66 | [TOMOYO_TYPE_IS_BLOCK_DEV] = "block", |
| 67 | [TOMOYO_TYPE_IS_DIRECTORY] = "directory", |
| 68 | [TOMOYO_TYPE_IS_CHAR_DEV] = "char", |
| 69 | [TOMOYO_TYPE_IS_FIFO] = "fifo", |
| 70 | [TOMOYO_MODE_SETUID] = "setuid", |
| 71 | [TOMOYO_MODE_SETGID] = "setgid", |
| 72 | [TOMOYO_MODE_STICKY] = "sticky", |
| 73 | [TOMOYO_MODE_OWNER_READ] = "owner_read", |
| 74 | [TOMOYO_MODE_OWNER_WRITE] = "owner_write", |
| 75 | [TOMOYO_MODE_OWNER_EXECUTE] = "owner_execute", |
| 76 | [TOMOYO_MODE_GROUP_READ] = "group_read", |
| 77 | [TOMOYO_MODE_GROUP_WRITE] = "group_write", |
| 78 | [TOMOYO_MODE_GROUP_EXECUTE] = "group_execute", |
| 79 | [TOMOYO_MODE_OTHERS_READ] = "others_read", |
| 80 | [TOMOYO_MODE_OTHERS_WRITE] = "others_write", |
| 81 | [TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute", |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 82 | [TOMOYO_EXEC_REALPATH] = "exec.realpath", |
| 83 | [TOMOYO_SYMLINK_TARGET] = "symlink.target", |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame] | 84 | [TOMOYO_PATH1_UID] = "path1.uid", |
| 85 | [TOMOYO_PATH1_GID] = "path1.gid", |
| 86 | [TOMOYO_PATH1_INO] = "path1.ino", |
| 87 | [TOMOYO_PATH1_MAJOR] = "path1.major", |
| 88 | [TOMOYO_PATH1_MINOR] = "path1.minor", |
| 89 | [TOMOYO_PATH1_PERM] = "path1.perm", |
| 90 | [TOMOYO_PATH1_TYPE] = "path1.type", |
| 91 | [TOMOYO_PATH1_DEV_MAJOR] = "path1.dev_major", |
| 92 | [TOMOYO_PATH1_DEV_MINOR] = "path1.dev_minor", |
| 93 | [TOMOYO_PATH2_UID] = "path2.uid", |
| 94 | [TOMOYO_PATH2_GID] = "path2.gid", |
| 95 | [TOMOYO_PATH2_INO] = "path2.ino", |
| 96 | [TOMOYO_PATH2_MAJOR] = "path2.major", |
| 97 | [TOMOYO_PATH2_MINOR] = "path2.minor", |
| 98 | [TOMOYO_PATH2_PERM] = "path2.perm", |
| 99 | [TOMOYO_PATH2_TYPE] = "path2.type", |
| 100 | [TOMOYO_PATH2_DEV_MAJOR] = "path2.dev_major", |
| 101 | [TOMOYO_PATH2_DEV_MINOR] = "path2.dev_minor", |
| 102 | [TOMOYO_PATH1_PARENT_UID] = "path1.parent.uid", |
| 103 | [TOMOYO_PATH1_PARENT_GID] = "path1.parent.gid", |
| 104 | [TOMOYO_PATH1_PARENT_INO] = "path1.parent.ino", |
| 105 | [TOMOYO_PATH1_PARENT_PERM] = "path1.parent.perm", |
| 106 | [TOMOYO_PATH2_PARENT_UID] = "path2.parent.uid", |
| 107 | [TOMOYO_PATH2_PARENT_GID] = "path2.parent.gid", |
| 108 | [TOMOYO_PATH2_PARENT_INO] = "path2.parent.ino", |
| 109 | [TOMOYO_PATH2_PARENT_PERM] = "path2.parent.perm", |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 110 | }; |
| 111 | |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 112 | /* String table for PREFERENCE keyword. */ |
| 113 | static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 114 | [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log", |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 115 | [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry", |
| 116 | }; |
| 117 | |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 118 | /* String table for path operation. */ |
| 119 | const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = { |
| 120 | [TOMOYO_TYPE_EXECUTE] = "execute", |
| 121 | [TOMOYO_TYPE_READ] = "read", |
| 122 | [TOMOYO_TYPE_WRITE] = "write", |
| 123 | [TOMOYO_TYPE_APPEND] = "append", |
| 124 | [TOMOYO_TYPE_UNLINK] = "unlink", |
| 125 | [TOMOYO_TYPE_GETATTR] = "getattr", |
| 126 | [TOMOYO_TYPE_RMDIR] = "rmdir", |
| 127 | [TOMOYO_TYPE_TRUNCATE] = "truncate", |
| 128 | [TOMOYO_TYPE_SYMLINK] = "symlink", |
| 129 | [TOMOYO_TYPE_CHROOT] = "chroot", |
| 130 | [TOMOYO_TYPE_UMOUNT] = "unmount", |
| 131 | }; |
| 132 | |
| 133 | /* String table for categories. */ |
| 134 | static const char * const tomoyo_category_keywords |
| 135 | [TOMOYO_MAX_MAC_CATEGORY_INDEX] = { |
| 136 | [TOMOYO_MAC_CATEGORY_FILE] = "file", |
| 137 | }; |
| 138 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 139 | /* Permit policy management by non-root user? */ |
| 140 | static bool tomoyo_manage_by_non_root; |
| 141 | |
| 142 | /* Utility functions. */ |
| 143 | |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 144 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 145 | * tomoyo_yesno - Return "yes" or "no". |
| 146 | * |
| 147 | * @value: Bool value. |
| 148 | */ |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 149 | const char *tomoyo_yesno(const unsigned int value) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 150 | { |
| 151 | return value ? "yes" : "no"; |
| 152 | } |
| 153 | |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 154 | /** |
| 155 | * tomoyo_addprintf - strncat()-like-snprintf(). |
| 156 | * |
| 157 | * @buffer: Buffer to write to. Must be '\0'-terminated. |
| 158 | * @len: Size of @buffer. |
| 159 | * @fmt: The printf()'s format string, followed by parameters. |
| 160 | * |
| 161 | * Returns nothing. |
| 162 | */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 163 | static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...) |
| 164 | { |
| 165 | va_list args; |
| 166 | const int pos = strlen(buffer); |
| 167 | va_start(args, fmt); |
| 168 | vsnprintf(buffer + pos, len - pos - 1, fmt, args); |
| 169 | va_end(args); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * tomoyo_flush - Flush queued string to userspace's buffer. |
| 174 | * |
| 175 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 176 | * |
| 177 | * Returns true if all data was flushed, false otherwise. |
| 178 | */ |
| 179 | static bool tomoyo_flush(struct tomoyo_io_buffer *head) |
| 180 | { |
| 181 | while (head->r.w_pos) { |
| 182 | const char *w = head->r.w[0]; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 183 | size_t len = strlen(w); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 184 | if (len) { |
| 185 | if (len > head->read_user_buf_avail) |
| 186 | len = head->read_user_buf_avail; |
| 187 | if (!len) |
| 188 | return false; |
| 189 | if (copy_to_user(head->read_user_buf, w, len)) |
| 190 | return false; |
| 191 | head->read_user_buf_avail -= len; |
| 192 | head->read_user_buf += len; |
| 193 | w += len; |
| 194 | } |
Tetsuo Handa | c0fa797 | 2011-04-03 00:12:54 +0900 | [diff] [blame] | 195 | head->r.w[0] = w; |
| 196 | if (*w) |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 197 | return false; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 198 | /* Add '\0' for audit logs and query. */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 199 | if (head->poll) { |
| 200 | if (!head->read_user_buf_avail || |
| 201 | copy_to_user(head->read_user_buf, "", 1)) |
| 202 | return false; |
| 203 | head->read_user_buf_avail--; |
| 204 | head->read_user_buf++; |
| 205 | } |
| 206 | head->r.w_pos--; |
| 207 | for (len = 0; len < head->r.w_pos; len++) |
| 208 | head->r.w[len] = head->r.w[len + 1]; |
| 209 | } |
| 210 | head->r.avail = 0; |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure. |
| 216 | * |
| 217 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 218 | * @string: String to print. |
| 219 | * |
| 220 | * Note that @string has to be kept valid until @head is kfree()d. |
| 221 | * This means that char[] allocated on stack memory cannot be passed to |
| 222 | * this function. Use tomoyo_io_printf() for char[] allocated on stack memory. |
| 223 | */ |
| 224 | static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string) |
| 225 | { |
| 226 | if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) { |
| 227 | head->r.w[head->r.w_pos++] = string; |
| 228 | tomoyo_flush(head); |
| 229 | } else |
| 230 | WARN_ON(1); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure. |
| 235 | * |
| 236 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 237 | * @fmt: The printf()'s format string, followed by parameters. |
| 238 | */ |
| 239 | void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) |
| 240 | { |
| 241 | va_list args; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 242 | size_t len; |
| 243 | size_t pos = head->r.avail; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 244 | int size = head->readbuf_size - pos; |
| 245 | if (size <= 0) |
| 246 | return; |
| 247 | va_start(args, fmt); |
| 248 | len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1; |
| 249 | va_end(args); |
| 250 | if (pos + len >= head->readbuf_size) { |
| 251 | WARN_ON(1); |
| 252 | return; |
| 253 | } |
| 254 | head->r.avail += len; |
| 255 | tomoyo_set_string(head, head->read_buf + pos); |
| 256 | } |
| 257 | |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 258 | /** |
| 259 | * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure. |
| 260 | * |
| 261 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 262 | * |
| 263 | * Returns nothing. |
| 264 | */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 265 | static void tomoyo_set_space(struct tomoyo_io_buffer *head) |
| 266 | { |
| 267 | tomoyo_set_string(head, " "); |
| 268 | } |
| 269 | |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 270 | /** |
| 271 | * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure. |
| 272 | * |
| 273 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 274 | * |
| 275 | * Returns nothing. |
| 276 | */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 277 | static bool tomoyo_set_lf(struct tomoyo_io_buffer *head) |
| 278 | { |
| 279 | tomoyo_set_string(head, "\n"); |
| 280 | return !head->r.w_pos; |
| 281 | } |
| 282 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 283 | /** |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 284 | * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure. |
| 285 | * |
| 286 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 287 | * |
| 288 | * Returns nothing. |
| 289 | */ |
| 290 | static void tomoyo_set_slash(struct tomoyo_io_buffer *head) |
| 291 | { |
| 292 | tomoyo_set_string(head, "/"); |
| 293 | } |
| 294 | |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 295 | /* List of namespaces. */ |
| 296 | LIST_HEAD(tomoyo_namespace_list); |
| 297 | /* True if namespace other than tomoyo_kernel_namespace is defined. */ |
| 298 | static bool tomoyo_namespace_enabled; |
| 299 | |
| 300 | /** |
| 301 | * tomoyo_init_policy_namespace - Initialize namespace. |
| 302 | * |
| 303 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
| 304 | * |
| 305 | * Returns nothing. |
| 306 | */ |
| 307 | void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns) |
| 308 | { |
| 309 | unsigned int idx; |
| 310 | for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++) |
| 311 | INIT_LIST_HEAD(&ns->acl_group[idx]); |
| 312 | for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++) |
| 313 | INIT_LIST_HEAD(&ns->group_list[idx]); |
| 314 | for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++) |
| 315 | INIT_LIST_HEAD(&ns->policy_list[idx]); |
| 316 | ns->profile_version = 20100903; |
| 317 | tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list); |
| 318 | list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * tomoyo_print_namespace - Print namespace header. |
| 323 | * |
| 324 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 325 | * |
| 326 | * Returns nothing. |
| 327 | */ |
| 328 | static void tomoyo_print_namespace(struct tomoyo_io_buffer *head) |
| 329 | { |
| 330 | if (!tomoyo_namespace_enabled) |
| 331 | return; |
| 332 | tomoyo_set_string(head, |
| 333 | container_of(head->r.ns, |
| 334 | struct tomoyo_policy_namespace, |
| 335 | namespace_list)->name); |
| 336 | tomoyo_set_space(head); |
| 337 | } |
| 338 | |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 339 | /** |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 340 | * tomoyo_print_name_union - Print a tomoyo_name_union. |
| 341 | * |
| 342 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 343 | * @ptr: Pointer to "struct tomoyo_name_union". |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 344 | */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 345 | static void tomoyo_print_name_union(struct tomoyo_io_buffer *head, |
| 346 | const struct tomoyo_name_union *ptr) |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 347 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 348 | tomoyo_set_space(head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 349 | if (ptr->group) { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 350 | tomoyo_set_string(head, "@"); |
| 351 | tomoyo_set_string(head, ptr->group->group_name->name); |
| 352 | } else { |
| 353 | tomoyo_set_string(head, ptr->filename->name); |
| 354 | } |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 358 | * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote. |
| 359 | * |
| 360 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 361 | * @ptr: Pointer to "struct tomoyo_name_union". |
| 362 | * |
| 363 | * Returns nothing. |
| 364 | */ |
| 365 | static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head, |
| 366 | const struct tomoyo_name_union *ptr) |
| 367 | { |
| 368 | if (ptr->group) { |
| 369 | tomoyo_set_string(head, "@"); |
| 370 | tomoyo_set_string(head, ptr->group->group_name->name); |
| 371 | } else { |
| 372 | tomoyo_set_string(head, "\""); |
| 373 | tomoyo_set_string(head, ptr->filename->name); |
| 374 | tomoyo_set_string(head, "\""); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 379 | * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space. |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 380 | * |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 381 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 382 | * @ptr: Pointer to "struct tomoyo_number_union". |
| 383 | * |
| 384 | * Returns nothing. |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 385 | */ |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 386 | static void tomoyo_print_number_union_nospace |
| 387 | (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr) |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 388 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 389 | if (ptr->group) { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 390 | tomoyo_set_string(head, "@"); |
| 391 | tomoyo_set_string(head, ptr->group->group_name->name); |
| 392 | } else { |
| 393 | int i; |
| 394 | unsigned long min = ptr->values[0]; |
| 395 | const unsigned long max = ptr->values[1]; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 396 | u8 min_type = ptr->value_type[0]; |
| 397 | const u8 max_type = ptr->value_type[1]; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 398 | char buffer[128]; |
| 399 | buffer[0] = '\0'; |
| 400 | for (i = 0; i < 2; i++) { |
| 401 | switch (min_type) { |
| 402 | case TOMOYO_VALUE_TYPE_HEXADECIMAL: |
| 403 | tomoyo_addprintf(buffer, sizeof(buffer), |
| 404 | "0x%lX", min); |
| 405 | break; |
| 406 | case TOMOYO_VALUE_TYPE_OCTAL: |
| 407 | tomoyo_addprintf(buffer, sizeof(buffer), |
| 408 | "0%lo", min); |
| 409 | break; |
| 410 | default: |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 411 | tomoyo_addprintf(buffer, sizeof(buffer), "%lu", |
| 412 | min); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | if (min == max && min_type == max_type) |
| 416 | break; |
| 417 | tomoyo_addprintf(buffer, sizeof(buffer), "-"); |
| 418 | min_type = max_type; |
| 419 | min = max; |
| 420 | } |
| 421 | tomoyo_io_printf(head, "%s", buffer); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 422 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 426 | * tomoyo_print_number_union - Print a tomoyo_number_union. |
| 427 | * |
| 428 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 429 | * @ptr: Pointer to "struct tomoyo_number_union". |
| 430 | * |
| 431 | * Returns nothing. |
| 432 | */ |
| 433 | static void tomoyo_print_number_union(struct tomoyo_io_buffer *head, |
| 434 | const struct tomoyo_number_union *ptr) |
| 435 | { |
| 436 | tomoyo_set_space(head); |
| 437 | tomoyo_print_number_union_nospace(head, ptr); |
| 438 | } |
| 439 | |
| 440 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 441 | * tomoyo_assign_profile - Create a new profile. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 442 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 443 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 444 | * @profile: Profile number to create. |
| 445 | * |
| 446 | * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise. |
| 447 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 448 | static struct tomoyo_profile *tomoyo_assign_profile |
| 449 | (struct tomoyo_policy_namespace *ns, const unsigned int profile) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 450 | { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 451 | struct tomoyo_profile *ptr; |
| 452 | struct tomoyo_profile *entry; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 453 | if (profile >= TOMOYO_MAX_PROFILES) |
| 454 | return NULL; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 455 | ptr = ns->profile_ptr[profile]; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 456 | if (ptr) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 457 | return ptr; |
| 458 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
| 459 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 460 | goto out; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 461 | ptr = ns->profile_ptr[profile]; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 462 | if (!ptr && tomoyo_memory_ok(entry)) { |
| 463 | ptr = entry; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 464 | ptr->default_config = TOMOYO_CONFIG_DISABLED | |
| 465 | TOMOYO_CONFIG_WANT_GRANT_LOG | |
| 466 | TOMOYO_CONFIG_WANT_REJECT_LOG; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 467 | memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT, |
| 468 | sizeof(ptr->config)); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 469 | ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024; |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 470 | ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 471 | mb(); /* Avoid out-of-order execution. */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 472 | ns->profile_ptr[profile] = ptr; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 473 | entry = NULL; |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 474 | } |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 475 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 476 | out: |
| 477 | kfree(entry); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 478 | return ptr; |
| 479 | } |
| 480 | |
| 481 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 482 | * tomoyo_profile - Find a profile. |
| 483 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 484 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 485 | * @profile: Profile number to find. |
| 486 | * |
| 487 | * Returns pointer to "struct tomoyo_profile". |
| 488 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 489 | struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, |
| 490 | const u8 profile) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 491 | { |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 492 | static struct tomoyo_profile tomoyo_null_profile; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 493 | struct tomoyo_profile *ptr = ns->profile_ptr[profile]; |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 494 | if (!ptr) |
| 495 | ptr = &tomoyo_null_profile; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 496 | return ptr; |
| 497 | } |
| 498 | |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 499 | /** |
| 500 | * tomoyo_find_yesno - Find values for specified keyword. |
| 501 | * |
| 502 | * @string: String to check. |
| 503 | * @find: Name of keyword. |
| 504 | * |
| 505 | * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise. |
| 506 | */ |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 507 | static s8 tomoyo_find_yesno(const char *string, const char *find) |
| 508 | { |
| 509 | const char *cp = strstr(string, find); |
| 510 | if (cp) { |
| 511 | cp += strlen(find); |
| 512 | if (!strncmp(cp, "=yes", 4)) |
| 513 | return 1; |
| 514 | else if (!strncmp(cp, "=no", 3)) |
| 515 | return 0; |
| 516 | } |
| 517 | return -1; |
| 518 | } |
| 519 | |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 520 | /** |
| 521 | * tomoyo_set_uint - Set value for specified preference. |
| 522 | * |
| 523 | * @i: Pointer to "unsigned int". |
| 524 | * @string: String to check. |
| 525 | * @find: Name of keyword. |
| 526 | * |
| 527 | * Returns nothing. |
| 528 | */ |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 529 | static void tomoyo_set_uint(unsigned int *i, const char *string, |
| 530 | const char *find) |
| 531 | { |
| 532 | const char *cp = strstr(string, find); |
| 533 | if (cp) |
| 534 | sscanf(cp + strlen(find), "=%u", i); |
| 535 | } |
| 536 | |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 537 | /** |
| 538 | * tomoyo_set_mode - Set mode for specified profile. |
| 539 | * |
| 540 | * @name: Name of functionality. |
| 541 | * @value: Mode for @name. |
| 542 | * @profile: Pointer to "struct tomoyo_profile". |
| 543 | * |
| 544 | * Returns 0 on success, negative value otherwise. |
| 545 | */ |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 546 | static int tomoyo_set_mode(char *name, const char *value, |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 547 | struct tomoyo_profile *profile) |
| 548 | { |
| 549 | u8 i; |
| 550 | u8 config; |
| 551 | if (!strcmp(name, "CONFIG")) { |
| 552 | i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; |
| 553 | config = profile->default_config; |
| 554 | } else if (tomoyo_str_starts(&name, "CONFIG::")) { |
| 555 | config = 0; |
| 556 | for (i = 0; i < TOMOYO_MAX_MAC_INDEX |
| 557 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 558 | int len = 0; |
| 559 | if (i < TOMOYO_MAX_MAC_INDEX) { |
| 560 | const u8 c = tomoyo_index2category[i]; |
| 561 | const char *category = |
| 562 | tomoyo_category_keywords[c]; |
| 563 | len = strlen(category); |
| 564 | if (strncmp(name, category, len) || |
| 565 | name[len++] != ':' || name[len++] != ':') |
| 566 | continue; |
| 567 | } |
| 568 | if (strcmp(name + len, tomoyo_mac_keywords[i])) |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 569 | continue; |
| 570 | config = profile->config[i]; |
| 571 | break; |
| 572 | } |
| 573 | if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) |
| 574 | return -EINVAL; |
| 575 | } else { |
| 576 | return -EINVAL; |
| 577 | } |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 578 | if (strstr(value, "use_default")) { |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 579 | config = TOMOYO_CONFIG_USE_DEFAULT; |
| 580 | } else { |
| 581 | u8 mode; |
| 582 | for (mode = 0; mode < 4; mode++) |
| 583 | if (strstr(value, tomoyo_mode[mode])) |
| 584 | /* |
| 585 | * Update lower 3 bits in order to distinguish |
| 586 | * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'. |
| 587 | */ |
| 588 | config = (config & ~7) | mode; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 589 | if (config != TOMOYO_CONFIG_USE_DEFAULT) { |
| 590 | switch (tomoyo_find_yesno(value, "grant_log")) { |
| 591 | case 1: |
| 592 | config |= TOMOYO_CONFIG_WANT_GRANT_LOG; |
| 593 | break; |
| 594 | case 0: |
| 595 | config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG; |
| 596 | break; |
| 597 | } |
| 598 | switch (tomoyo_find_yesno(value, "reject_log")) { |
| 599 | case 1: |
| 600 | config |= TOMOYO_CONFIG_WANT_REJECT_LOG; |
| 601 | break; |
| 602 | case 0: |
| 603 | config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG; |
| 604 | break; |
| 605 | } |
| 606 | } |
Tetsuo Handa | 8e56868 | 2010-06-25 09:30:09 +0900 | [diff] [blame] | 607 | } |
| 608 | if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) |
| 609 | profile->config[i] = config; |
| 610 | else if (config != TOMOYO_CONFIG_USE_DEFAULT) |
| 611 | profile->default_config = config; |
| 612 | return 0; |
| 613 | } |
| 614 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 615 | /** |
| 616 | * tomoyo_write_profile - Write profile table. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 617 | * |
| 618 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 619 | * |
| 620 | * Returns 0 on success, negative value otherwise. |
| 621 | */ |
| 622 | static int tomoyo_write_profile(struct tomoyo_io_buffer *head) |
| 623 | { |
| 624 | char *data = head->write_buf; |
| 625 | unsigned int i; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 626 | char *cp; |
| 627 | struct tomoyo_profile *profile; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 628 | if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version) |
| 629 | == 1) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 630 | return 0; |
| 631 | i = simple_strtoul(data, &cp, 10); |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 632 | if (*cp != '-') |
| 633 | return -EINVAL; |
| 634 | data = cp + 1; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 635 | profile = tomoyo_assign_profile(head->w.ns, i); |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 636 | if (!profile) |
| 637 | return -EINVAL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 638 | cp = strchr(data, '='); |
| 639 | if (!cp) |
| 640 | return -EINVAL; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 641 | *cp++ = '\0'; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 642 | if (!strcmp(data, "COMMENT")) { |
Tetsuo Handa | 2a086e5 | 2011-04-03 00:09:26 +0900 | [diff] [blame] | 643 | static DEFINE_SPINLOCK(lock); |
| 644 | const struct tomoyo_path_info *new_comment |
| 645 | = tomoyo_get_name(cp); |
| 646 | const struct tomoyo_path_info *old_comment; |
| 647 | if (!new_comment) |
| 648 | return -ENOMEM; |
| 649 | spin_lock(&lock); |
| 650 | old_comment = profile->comment; |
| 651 | profile->comment = new_comment; |
| 652 | spin_unlock(&lock); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 653 | tomoyo_put_name(old_comment); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 654 | return 0; |
| 655 | } |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 656 | if (!strcmp(data, "PREFERENCE")) { |
| 657 | for (i = 0; i < TOMOYO_MAX_PREF; i++) |
| 658 | tomoyo_set_uint(&profile->pref[i], cp, |
| 659 | tomoyo_pref_keywords[i]); |
| 660 | return 0; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 661 | } |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 662 | return tomoyo_set_mode(data, cp, profile); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 663 | } |
| 664 | |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 665 | /** |
| 666 | * tomoyo_print_config - Print mode for specified functionality. |
| 667 | * |
| 668 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 669 | * @config: Mode for that functionality. |
| 670 | * |
| 671 | * Returns nothing. |
| 672 | * |
| 673 | * Caller prints functionality's name. |
| 674 | */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 675 | static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config) |
| 676 | { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 677 | tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n", |
| 678 | tomoyo_mode[config & 3], |
| 679 | tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG), |
| 680 | tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG)); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 681 | } |
| 682 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 683 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 684 | * tomoyo_read_profile - Read profile table. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 685 | * |
| 686 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 687 | * |
| 688 | * Returns nothing. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 689 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 690 | static void tomoyo_read_profile(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 691 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 692 | u8 index; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 693 | struct tomoyo_policy_namespace *ns = |
| 694 | container_of(head->r.ns, typeof(*ns), namespace_list); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 695 | const struct tomoyo_profile *profile; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 696 | if (head->r.eof) |
| 697 | return; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 698 | next: |
| 699 | index = head->r.index; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 700 | profile = ns->profile_ptr[index]; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 701 | switch (head->r.step) { |
| 702 | case 0: |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 703 | tomoyo_print_namespace(head); |
| 704 | tomoyo_io_printf(head, "PROFILE_VERSION=%u\n", |
| 705 | ns->profile_version); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 706 | head->r.step++; |
| 707 | break; |
| 708 | case 1: |
| 709 | for ( ; head->r.index < TOMOYO_MAX_PROFILES; |
| 710 | head->r.index++) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 711 | if (ns->profile_ptr[head->r.index]) |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 712 | break; |
| 713 | if (head->r.index == TOMOYO_MAX_PROFILES) |
| 714 | return; |
| 715 | head->r.step++; |
| 716 | break; |
| 717 | case 2: |
| 718 | { |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 719 | u8 i; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 720 | const struct tomoyo_path_info *comment = |
| 721 | profile->comment; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 722 | tomoyo_print_namespace(head); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 723 | tomoyo_io_printf(head, "%u-COMMENT=", index); |
| 724 | tomoyo_set_string(head, comment ? comment->name : ""); |
| 725 | tomoyo_set_lf(head); |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 726 | tomoyo_io_printf(head, "%u-PREFERENCE={ ", index); |
| 727 | for (i = 0; i < TOMOYO_MAX_PREF; i++) |
| 728 | tomoyo_io_printf(head, "%s=%u ", |
| 729 | tomoyo_pref_keywords[i], |
| 730 | profile->pref[i]); |
| 731 | tomoyo_set_string(head, "}\n"); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 732 | head->r.step++; |
| 733 | } |
| 734 | break; |
| 735 | case 3: |
| 736 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 737 | tomoyo_print_namespace(head); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 738 | tomoyo_io_printf(head, "%u-%s", index, "CONFIG"); |
| 739 | tomoyo_print_config(head, profile->default_config); |
| 740 | head->r.bit = 0; |
| 741 | head->r.step++; |
| 742 | } |
| 743 | break; |
| 744 | case 4: |
| 745 | for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX |
| 746 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) { |
| 747 | const u8 i = head->r.bit; |
| 748 | const u8 config = profile->config[i]; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 749 | if (config == TOMOYO_CONFIG_USE_DEFAULT) |
| 750 | continue; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 751 | tomoyo_print_namespace(head); |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 752 | if (i < TOMOYO_MAX_MAC_INDEX) |
| 753 | tomoyo_io_printf(head, "%u-CONFIG::%s::%s", |
| 754 | index, |
| 755 | tomoyo_category_keywords |
| 756 | [tomoyo_index2category[i]], |
| 757 | tomoyo_mac_keywords[i]); |
| 758 | else |
| 759 | tomoyo_io_printf(head, "%u-CONFIG::%s", index, |
| 760 | tomoyo_mac_keywords[i]); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 761 | tomoyo_print_config(head, config); |
| 762 | head->r.bit++; |
| 763 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 764 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 765 | if (head->r.bit == TOMOYO_MAX_MAC_INDEX |
| 766 | + TOMOYO_MAX_MAC_CATEGORY_INDEX) { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 767 | head->r.index++; |
| 768 | head->r.step = 1; |
| 769 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 770 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 771 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 772 | if (tomoyo_flush(head)) |
| 773 | goto next; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 774 | } |
| 775 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 776 | /** |
| 777 | * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry. |
| 778 | * |
| 779 | * @a: Pointer to "struct tomoyo_acl_head". |
| 780 | * @b: Pointer to "struct tomoyo_acl_head". |
| 781 | * |
| 782 | * Returns true if @a == @b, false otherwise. |
| 783 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 784 | static bool tomoyo_same_manager(const struct tomoyo_acl_head *a, |
| 785 | const struct tomoyo_acl_head *b) |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 786 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 787 | return container_of(a, struct tomoyo_manager, head)->manager == |
| 788 | container_of(b, struct tomoyo_manager, head)->manager; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 789 | } |
| 790 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 791 | /** |
| 792 | * tomoyo_update_manager_entry - Add a manager entry. |
| 793 | * |
| 794 | * @manager: The path to manager or the domainnamme. |
| 795 | * @is_delete: True if it is a delete request. |
| 796 | * |
| 797 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 798 | * |
| 799 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 800 | */ |
| 801 | static int tomoyo_update_manager_entry(const char *manager, |
| 802 | const bool is_delete) |
| 803 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 804 | struct tomoyo_manager e = { }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 805 | struct tomoyo_acl_param param = { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 806 | /* .ns = &tomoyo_kernel_namespace, */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 807 | .is_delete = is_delete, |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 808 | .list = &tomoyo_kernel_namespace. |
| 809 | policy_list[TOMOYO_ID_MANAGER], |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 810 | }; |
| 811 | int error = is_delete ? -ENOENT : -ENOMEM; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 812 | if (tomoyo_domain_def(manager)) { |
| 813 | if (!tomoyo_correct_domain(manager)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 814 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 815 | e.is_domain = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 816 | } else { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 817 | if (!tomoyo_correct_path(manager)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 818 | return -EINVAL; |
| 819 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 820 | e.manager = tomoyo_get_name(manager); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 821 | if (e.manager) { |
| 822 | error = tomoyo_update_policy(&e.head, sizeof(e), ¶m, |
| 823 | tomoyo_same_manager); |
| 824 | tomoyo_put_name(e.manager); |
| 825 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 826 | return error; |
| 827 | } |
| 828 | |
| 829 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 830 | * tomoyo_write_manager - Write manager policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 831 | * |
| 832 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 833 | * |
| 834 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 835 | * |
| 836 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 837 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 838 | static int tomoyo_write_manager(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 839 | { |
| 840 | char *data = head->write_buf; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 841 | |
| 842 | if (!strcmp(data, "manage_by_non_root")) { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 843 | tomoyo_manage_by_non_root = !head->w.is_delete; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 844 | return 0; |
| 845 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 846 | return tomoyo_update_manager_entry(data, head->w.is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 850 | * tomoyo_read_manager - Read manager policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 851 | * |
| 852 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 853 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 854 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 855 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 856 | static void tomoyo_read_manager(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 857 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 858 | if (head->r.eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 859 | return; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 860 | list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace. |
| 861 | policy_list[TOMOYO_ID_MANAGER]) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 862 | struct tomoyo_manager *ptr = |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 863 | list_entry(head->r.acl, typeof(*ptr), head.list); |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 864 | if (ptr->head.is_deleted) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 865 | continue; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 866 | if (!tomoyo_flush(head)) |
| 867 | return; |
| 868 | tomoyo_set_string(head, ptr->manager->name); |
| 869 | tomoyo_set_lf(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 870 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 871 | head->r.eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 875 | * tomoyo_manager - Check whether the current process is a policy manager. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 876 | * |
| 877 | * Returns true if the current process is permitted to modify policy |
| 878 | * via /sys/kernel/security/tomoyo/ interface. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 879 | * |
| 880 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 881 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 882 | static bool tomoyo_manager(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 883 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 884 | struct tomoyo_manager *ptr; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 885 | const char *exe; |
| 886 | const struct task_struct *task = current; |
| 887 | const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname; |
| 888 | bool found = false; |
| 889 | |
| 890 | if (!tomoyo_policy_loaded) |
| 891 | return true; |
| 892 | if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid)) |
| 893 | return false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 894 | list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace. |
| 895 | policy_list[TOMOYO_ID_MANAGER], head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 896 | if (!ptr->head.is_deleted && ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 897 | && !tomoyo_pathcmp(domainname, ptr->manager)) { |
| 898 | found = true; |
| 899 | break; |
| 900 | } |
| 901 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 902 | if (found) |
| 903 | return true; |
| 904 | exe = tomoyo_get_exe(); |
| 905 | if (!exe) |
| 906 | return false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 907 | list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace. |
| 908 | policy_list[TOMOYO_ID_MANAGER], head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 909 | if (!ptr->head.is_deleted && !ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 910 | && !strcmp(exe, ptr->manager->name)) { |
| 911 | found = true; |
| 912 | break; |
| 913 | } |
| 914 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 915 | if (!found) { /* Reduce error messages. */ |
| 916 | static pid_t last_pid; |
| 917 | const pid_t pid = current->pid; |
| 918 | if (last_pid != pid) { |
| 919 | printk(KERN_WARNING "%s ( %s ) is not permitted to " |
| 920 | "update policies.\n", domainname->name, exe); |
| 921 | last_pid = pid; |
| 922 | } |
| 923 | } |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 924 | kfree(exe); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 925 | return found; |
| 926 | } |
| 927 | |
| 928 | /** |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 929 | * tomoyo_select_domain - Parse select command. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 930 | * |
| 931 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 932 | * @data: String to parse. |
| 933 | * |
| 934 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 935 | * |
| 936 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 937 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 938 | static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, |
| 939 | const char *data) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 940 | { |
| 941 | unsigned int pid; |
| 942 | struct tomoyo_domain_info *domain = NULL; |
Tetsuo Handa | 9b24437 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 943 | bool global_pid = false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 944 | if (strncmp(data, "select ", 7)) |
| 945 | return false; |
| 946 | data += 7; |
Tetsuo Handa | 9b24437 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 947 | if (sscanf(data, "pid=%u", &pid) == 1 || |
| 948 | (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 949 | struct task_struct *p; |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 950 | rcu_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 951 | read_lock(&tasklist_lock); |
Tetsuo Handa | 9b24437 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 952 | if (global_pid) |
| 953 | p = find_task_by_pid_ns(pid, &init_pid_ns); |
| 954 | else |
| 955 | p = find_task_by_vpid(pid); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 956 | if (p) |
| 957 | domain = tomoyo_real_domain(p); |
| 958 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 959 | rcu_read_unlock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 960 | } else if (!strncmp(data, "domain=", 7)) { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 961 | if (tomoyo_domain_def(data + 7)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 962 | domain = tomoyo_find_domain(data + 7); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 963 | } else |
| 964 | return false; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 965 | head->w.domain = domain; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 966 | /* Accessing read_buf is safe because head->io_sem is held. */ |
| 967 | if (!head->read_buf) |
| 968 | return true; /* Do nothing if open(O_WRONLY). */ |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 969 | memset(&head->r, 0, sizeof(head->r)); |
| 970 | head->r.print_this_domain_only = true; |
Dan Carpenter | 68eda8f | 2010-08-08 00:17:51 +0200 | [diff] [blame] | 971 | if (domain) |
| 972 | head->r.domain = &domain->list; |
| 973 | else |
| 974 | head->r.eof = 1; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 975 | tomoyo_io_printf(head, "# select %s\n", data); |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 976 | if (domain && domain->is_deleted) |
| 977 | tomoyo_io_printf(head, "# This is a deleted domain.\n"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 978 | return true; |
| 979 | } |
| 980 | |
| 981 | /** |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 982 | * tomoyo_delete_domain - Delete a domain. |
| 983 | * |
| 984 | * @domainname: The name of domain. |
| 985 | * |
| 986 | * Returns 0. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 987 | * |
| 988 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 989 | */ |
| 990 | static int tomoyo_delete_domain(char *domainname) |
| 991 | { |
| 992 | struct tomoyo_domain_info *domain; |
| 993 | struct tomoyo_path_info name; |
| 994 | |
| 995 | name.name = domainname; |
| 996 | tomoyo_fill_path_info(&name); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 997 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 998 | return 0; |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 999 | /* Is there an active domain? */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1000 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 1001 | /* Never delete tomoyo_kernel_domain */ |
| 1002 | if (domain == &tomoyo_kernel_domain) |
| 1003 | continue; |
| 1004 | if (domain->is_deleted || |
| 1005 | tomoyo_pathcmp(domain->domainname, &name)) |
| 1006 | continue; |
| 1007 | domain->is_deleted = true; |
| 1008 | break; |
| 1009 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 1010 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1015 | * tomoyo_write_domain2 - Write domain policy. |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1016 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1017 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1018 | * @list: Pointer to "struct list_head". |
| 1019 | * @data: Policy to be interpreted. |
| 1020 | * @is_delete: True if it is a delete request. |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1021 | * |
| 1022 | * Returns 0 on success, negative value otherwise. |
| 1023 | * |
| 1024 | * Caller holds tomoyo_read_lock(). |
| 1025 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1026 | static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns, |
| 1027 | struct list_head *list, char *data, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1028 | const bool is_delete) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1029 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1030 | struct tomoyo_acl_param param = { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1031 | .ns = ns, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1032 | .list = list, |
| 1033 | .data = data, |
| 1034 | .is_delete = is_delete, |
| 1035 | }; |
| 1036 | static const struct { |
| 1037 | const char *keyword; |
| 1038 | int (*write) (struct tomoyo_acl_param *); |
| 1039 | } tomoyo_callback[1] = { |
| 1040 | { "file ", tomoyo_write_file }, |
| 1041 | }; |
| 1042 | u8 i; |
| 1043 | for (i = 0; i < 1; i++) { |
| 1044 | if (!tomoyo_str_starts(¶m.data, |
| 1045 | tomoyo_callback[i].keyword)) |
| 1046 | continue; |
| 1047 | return tomoyo_callback[i].write(¶m); |
| 1048 | } |
| 1049 | return -EINVAL; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1050 | } |
| 1051 | |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1052 | /* String table for domain flags. */ |
| 1053 | const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = { |
| 1054 | [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n", |
| 1055 | [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n", |
| 1056 | }; |
| 1057 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1058 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1059 | * tomoyo_write_domain - Write domain policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1060 | * |
| 1061 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1062 | * |
| 1063 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1064 | * |
| 1065 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1066 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1067 | static int tomoyo_write_domain(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1068 | { |
| 1069 | char *data = head->write_buf; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1070 | struct tomoyo_policy_namespace *ns; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 1071 | struct tomoyo_domain_info *domain = head->w.domain; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1072 | const bool is_delete = head->w.is_delete; |
| 1073 | bool is_select = !is_delete && tomoyo_str_starts(&data, "select "); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1074 | unsigned int profile; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1075 | if (*data == '<') { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1076 | domain = NULL; |
| 1077 | if (is_delete) |
| 1078 | tomoyo_delete_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1079 | else if (is_select) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1080 | domain = tomoyo_find_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1081 | else |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1082 | domain = tomoyo_assign_domain(data, false); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 1083 | head->w.domain = domain; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | if (!domain) |
| 1087 | return -EINVAL; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1088 | ns = domain->ns; |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 1089 | if (sscanf(data, "use_profile %u", &profile) == 1 |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1090 | && profile < TOMOYO_MAX_PROFILES) { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1091 | if (!tomoyo_policy_loaded || ns->profile_ptr[profile]) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1092 | domain->profile = (u8) profile; |
| 1093 | return 0; |
| 1094 | } |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1095 | if (sscanf(data, "use_group %u\n", &profile) == 1 |
| 1096 | && profile < TOMOYO_MAX_ACL_GROUPS) { |
| 1097 | if (!is_delete) |
| 1098 | domain->group = (u8) profile; |
| 1099 | return 0; |
| 1100 | } |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1101 | for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) { |
| 1102 | const char *cp = tomoyo_dif[profile]; |
| 1103 | if (strncmp(data, cp, strlen(cp) - 1)) |
| 1104 | continue; |
| 1105 | domain->flags[profile] = !is_delete; |
Tetsuo Handa | 9b24437 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1108 | return tomoyo_write_domain2(ns, &domain->acl_info_list, data, |
| 1109 | is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1113 | * tomoyo_print_condition - Print condition part. |
| 1114 | * |
| 1115 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1116 | * @cond: Pointer to "struct tomoyo_condition". |
| 1117 | * |
| 1118 | * Returns true on success, false otherwise. |
| 1119 | */ |
| 1120 | static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, |
| 1121 | const struct tomoyo_condition *cond) |
| 1122 | { |
| 1123 | switch (head->r.cond_step) { |
| 1124 | case 0: |
| 1125 | head->r.cond_index = 0; |
| 1126 | head->r.cond_step++; |
| 1127 | /* fall through */ |
| 1128 | case 1: |
| 1129 | { |
| 1130 | const u16 condc = cond->condc; |
| 1131 | const struct tomoyo_condition_element *condp = |
| 1132 | (typeof(condp)) (cond + 1); |
| 1133 | const struct tomoyo_number_union *numbers_p = |
| 1134 | (typeof(numbers_p)) (condp + condc); |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1135 | const struct tomoyo_name_union *names_p = |
| 1136 | (typeof(names_p)) |
| 1137 | (numbers_p + cond->numbers_count); |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1138 | const struct tomoyo_argv *argv = |
| 1139 | (typeof(argv)) (names_p + cond->names_count); |
| 1140 | const struct tomoyo_envp *envp = |
| 1141 | (typeof(envp)) (argv + cond->argc); |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1142 | u16 skip; |
| 1143 | for (skip = 0; skip < head->r.cond_index; skip++) { |
| 1144 | const u8 left = condp->left; |
| 1145 | const u8 right = condp->right; |
| 1146 | condp++; |
| 1147 | switch (left) { |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1148 | case TOMOYO_ARGV_ENTRY: |
| 1149 | argv++; |
| 1150 | continue; |
| 1151 | case TOMOYO_ENVP_ENTRY: |
| 1152 | envp++; |
| 1153 | continue; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1154 | case TOMOYO_NUMBER_UNION: |
| 1155 | numbers_p++; |
| 1156 | break; |
| 1157 | } |
| 1158 | switch (right) { |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1159 | case TOMOYO_NAME_UNION: |
| 1160 | names_p++; |
| 1161 | break; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1162 | case TOMOYO_NUMBER_UNION: |
| 1163 | numbers_p++; |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | while (head->r.cond_index < condc) { |
| 1168 | const u8 match = condp->equals; |
| 1169 | const u8 left = condp->left; |
| 1170 | const u8 right = condp->right; |
| 1171 | if (!tomoyo_flush(head)) |
| 1172 | return false; |
| 1173 | condp++; |
| 1174 | head->r.cond_index++; |
| 1175 | tomoyo_set_space(head); |
| 1176 | switch (left) { |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1177 | case TOMOYO_ARGV_ENTRY: |
| 1178 | tomoyo_io_printf(head, |
| 1179 | "exec.argv[%lu]%s=\"", |
| 1180 | argv->index, argv-> |
| 1181 | is_not ? "!" : ""); |
| 1182 | tomoyo_set_string(head, |
| 1183 | argv->value->name); |
| 1184 | tomoyo_set_string(head, "\""); |
| 1185 | argv++; |
| 1186 | continue; |
| 1187 | case TOMOYO_ENVP_ENTRY: |
| 1188 | tomoyo_set_string(head, |
| 1189 | "exec.envp[\""); |
| 1190 | tomoyo_set_string(head, |
| 1191 | envp->name->name); |
| 1192 | tomoyo_io_printf(head, "\"]%s=", envp-> |
| 1193 | is_not ? "!" : ""); |
| 1194 | if (envp->value) { |
| 1195 | tomoyo_set_string(head, "\""); |
| 1196 | tomoyo_set_string(head, envp-> |
| 1197 | value->name); |
| 1198 | tomoyo_set_string(head, "\""); |
| 1199 | } else { |
| 1200 | tomoyo_set_string(head, |
| 1201 | "NULL"); |
| 1202 | } |
| 1203 | envp++; |
| 1204 | continue; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1205 | case TOMOYO_NUMBER_UNION: |
| 1206 | tomoyo_print_number_union_nospace |
| 1207 | (head, numbers_p++); |
| 1208 | break; |
| 1209 | default: |
| 1210 | tomoyo_set_string(head, |
| 1211 | tomoyo_condition_keyword[left]); |
| 1212 | break; |
| 1213 | } |
| 1214 | tomoyo_set_string(head, match ? "=" : "!="); |
| 1215 | switch (right) { |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1216 | case TOMOYO_NAME_UNION: |
| 1217 | tomoyo_print_name_union_quoted |
| 1218 | (head, names_p++); |
| 1219 | break; |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1220 | case TOMOYO_NUMBER_UNION: |
| 1221 | tomoyo_print_number_union_nospace |
| 1222 | (head, numbers_p++); |
| 1223 | break; |
| 1224 | default: |
| 1225 | tomoyo_set_string(head, |
| 1226 | tomoyo_condition_keyword[right]); |
| 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | head->r.cond_step++; |
| 1232 | /* fall through */ |
| 1233 | case 2: |
| 1234 | if (!tomoyo_flush(head)) |
| 1235 | break; |
| 1236 | head->r.cond_step++; |
| 1237 | /* fall through */ |
| 1238 | case 3: |
| 1239 | tomoyo_set_lf(head); |
| 1240 | return true; |
| 1241 | } |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1246 | * tomoyo_set_group - Print "acl_group " header keyword and category name. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1247 | * |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1248 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1249 | * @category: Category name. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1250 | * |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1251 | * Returns nothing. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1252 | */ |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1253 | static void tomoyo_set_group(struct tomoyo_io_buffer *head, |
| 1254 | const char *category) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1255 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1256 | if (head->type == TOMOYO_EXCEPTIONPOLICY) { |
| 1257 | tomoyo_print_namespace(head); |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1258 | tomoyo_io_printf(head, "acl_group %u ", |
| 1259 | head->r.acl_group_index); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1260 | } |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1261 | tomoyo_set_string(head, category); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1265 | * tomoyo_print_entry - Print an ACL entry. |
| 1266 | * |
| 1267 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1268 | * @acl: Pointer to an ACL entry. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1269 | * |
| 1270 | * Returns true on success, false otherwise. |
| 1271 | */ |
| 1272 | static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1273 | struct tomoyo_acl_info *acl) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1274 | { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1275 | const u8 acl_type = acl->type; |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1276 | bool first = true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1277 | u8 bit; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1278 | |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1279 | if (head->r.print_cond_part) |
| 1280 | goto print_cond_part; |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1281 | if (acl->is_deleted) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 1282 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1283 | if (!tomoyo_flush(head)) |
| 1284 | return false; |
| 1285 | else if (acl_type == TOMOYO_TYPE_PATH_ACL) { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1286 | struct tomoyo_path_acl *ptr = |
| 1287 | container_of(acl, typeof(*ptr), head); |
| 1288 | const u16 perm = ptr->perm; |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1289 | for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1290 | if (!(perm & (1 << bit))) |
| 1291 | continue; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1292 | if (head->r.print_transition_related_only && |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1293 | bit != TOMOYO_TYPE_EXECUTE) |
| 1294 | continue; |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1295 | if (first) { |
| 1296 | tomoyo_set_group(head, "file "); |
| 1297 | first = false; |
| 1298 | } else { |
| 1299 | tomoyo_set_slash(head); |
| 1300 | } |
| 1301 | tomoyo_set_string(head, tomoyo_path_keyword[bit]); |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1302 | } |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1303 | if (first) |
| 1304 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1305 | tomoyo_print_name_union(head, &ptr->name); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1306 | } else if (head->r.print_transition_related_only) { |
Tetsuo Handa | 063821c | 2010-06-24 12:00:25 +0900 | [diff] [blame] | 1307 | return true; |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1308 | } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) { |
| 1309 | struct tomoyo_path2_acl *ptr = |
| 1310 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1311 | const u8 perm = ptr->perm; |
| 1312 | for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) { |
| 1313 | if (!(perm & (1 << bit))) |
| 1314 | continue; |
| 1315 | if (first) { |
| 1316 | tomoyo_set_group(head, "file "); |
| 1317 | first = false; |
| 1318 | } else { |
| 1319 | tomoyo_set_slash(head); |
| 1320 | } |
| 1321 | tomoyo_set_string(head, tomoyo_mac_keywords |
| 1322 | [tomoyo_pp2mac[bit]]); |
| 1323 | } |
| 1324 | if (first) |
| 1325 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1326 | tomoyo_print_name_union(head, &ptr->name1); |
| 1327 | tomoyo_print_name_union(head, &ptr->name2); |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1328 | } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) { |
| 1329 | struct tomoyo_path_number_acl *ptr = |
| 1330 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1331 | const u8 perm = ptr->perm; |
| 1332 | for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) { |
| 1333 | if (!(perm & (1 << bit))) |
| 1334 | continue; |
| 1335 | if (first) { |
| 1336 | tomoyo_set_group(head, "file "); |
| 1337 | first = false; |
| 1338 | } else { |
| 1339 | tomoyo_set_slash(head); |
| 1340 | } |
| 1341 | tomoyo_set_string(head, tomoyo_mac_keywords |
| 1342 | [tomoyo_pn2mac[bit]]); |
| 1343 | } |
| 1344 | if (first) |
| 1345 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1346 | tomoyo_print_name_union(head, &ptr->name); |
| 1347 | tomoyo_print_number_union(head, &ptr->number); |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1348 | } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) { |
| 1349 | struct tomoyo_mkdev_acl *ptr = |
| 1350 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1351 | const u8 perm = ptr->perm; |
| 1352 | for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) { |
| 1353 | if (!(perm & (1 << bit))) |
| 1354 | continue; |
| 1355 | if (first) { |
| 1356 | tomoyo_set_group(head, "file "); |
| 1357 | first = false; |
| 1358 | } else { |
| 1359 | tomoyo_set_slash(head); |
| 1360 | } |
| 1361 | tomoyo_set_string(head, tomoyo_mac_keywords |
| 1362 | [tomoyo_pnnn2mac[bit]]); |
| 1363 | } |
| 1364 | if (first) |
| 1365 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1366 | tomoyo_print_name_union(head, &ptr->name); |
| 1367 | tomoyo_print_number_union(head, &ptr->mode); |
| 1368 | tomoyo_print_number_union(head, &ptr->major); |
| 1369 | tomoyo_print_number_union(head, &ptr->minor); |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1370 | } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { |
| 1371 | struct tomoyo_mount_acl *ptr = |
| 1372 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1373 | tomoyo_set_group(head, "file mount"); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1374 | tomoyo_print_name_union(head, &ptr->dev_name); |
| 1375 | tomoyo_print_name_union(head, &ptr->dir_name); |
| 1376 | tomoyo_print_name_union(head, &ptr->fs_type); |
| 1377 | tomoyo_print_number_union(head, &ptr->flags); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1378 | } |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 1379 | if (acl->cond) { |
| 1380 | head->r.print_cond_part = true; |
| 1381 | head->r.cond_step = 0; |
| 1382 | if (!tomoyo_flush(head)) |
| 1383 | return false; |
| 1384 | print_cond_part: |
| 1385 | if (!tomoyo_print_condition(head, acl->cond)) |
| 1386 | return false; |
| 1387 | head->r.print_cond_part = false; |
| 1388 | } else { |
| 1389 | tomoyo_set_lf(head); |
| 1390 | } |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame] | 1391 | return true; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * tomoyo_read_domain2 - Read domain policy. |
| 1396 | * |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1397 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1398 | * @list: Pointer to "struct list_head". |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1399 | * |
| 1400 | * Caller holds tomoyo_read_lock(). |
| 1401 | * |
| 1402 | * Returns true on success, false otherwise. |
| 1403 | */ |
| 1404 | static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head, |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1405 | struct list_head *list) |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1406 | { |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1407 | list_for_each_cookie(head->r.acl, list) { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1408 | struct tomoyo_acl_info *ptr = |
| 1409 | list_entry(head->r.acl, typeof(*ptr), list); |
| 1410 | if (!tomoyo_print_entry(head, ptr)) |
| 1411 | return false; |
| 1412 | } |
| 1413 | head->r.acl = NULL; |
| 1414 | return true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1418 | * tomoyo_read_domain - Read domain policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1419 | * |
| 1420 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1421 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1422 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1423 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1424 | static void tomoyo_read_domain(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1425 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1426 | if (head->r.eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1427 | return; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1428 | list_for_each_cookie(head->r.domain, &tomoyo_domain_list) { |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1429 | struct tomoyo_domain_info *domain = |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1430 | list_entry(head->r.domain, typeof(*domain), list); |
| 1431 | switch (head->r.step) { |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1432 | u8 i; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1433 | case 0: |
| 1434 | if (domain->is_deleted && |
| 1435 | !head->r.print_this_domain_only) |
| 1436 | continue; |
| 1437 | /* Print domainname and flags. */ |
| 1438 | tomoyo_set_string(head, domain->domainname->name); |
| 1439 | tomoyo_set_lf(head); |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 1440 | tomoyo_io_printf(head, "use_profile %u\n", |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1441 | domain->profile); |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1442 | tomoyo_io_printf(head, "use_group %u\n", |
| 1443 | domain->group); |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1444 | for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++) |
| 1445 | if (domain->flags[i]) |
| 1446 | tomoyo_set_string(head, tomoyo_dif[i]); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1447 | head->r.step++; |
| 1448 | tomoyo_set_lf(head); |
| 1449 | /* fall through */ |
| 1450 | case 1: |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1451 | if (!tomoyo_read_domain2(head, &domain->acl_info_list)) |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1452 | return; |
| 1453 | head->r.step++; |
| 1454 | if (!tomoyo_set_lf(head)) |
| 1455 | return; |
| 1456 | /* fall through */ |
| 1457 | case 2: |
| 1458 | head->r.step = 0; |
| 1459 | if (head->r.print_this_domain_only) |
| 1460 | goto done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1461 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1462 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1463 | done: |
| 1464 | head->r.eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1468 | * tomoyo_write_pid: Specify PID to obtain domainname. |
| 1469 | * |
| 1470 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1471 | * |
| 1472 | * Returns 0. |
| 1473 | */ |
| 1474 | static int tomoyo_write_pid(struct tomoyo_io_buffer *head) |
| 1475 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1476 | head->r.eof = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * tomoyo_read_pid - Get domainname of the specified PID. |
| 1482 | * |
| 1483 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1484 | * |
| 1485 | * Returns the domainname which the specified PID is in on success, |
| 1486 | * empty string otherwise. |
| 1487 | * The PID is specified by tomoyo_write_pid() so that the user can obtain |
| 1488 | * using read()/write() interface rather than sysctl() interface. |
| 1489 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1490 | static void tomoyo_read_pid(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1491 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1492 | char *buf = head->write_buf; |
| 1493 | bool global_pid = false; |
| 1494 | unsigned int pid; |
| 1495 | struct task_struct *p; |
| 1496 | struct tomoyo_domain_info *domain = NULL; |
| 1497 | |
| 1498 | /* Accessing write_buf is safe because head->io_sem is held. */ |
| 1499 | if (!buf) { |
| 1500 | head->r.eof = true; |
| 1501 | return; /* Do nothing if open(O_RDONLY). */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1502 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1503 | if (head->r.w_pos || head->r.eof) |
| 1504 | return; |
| 1505 | head->r.eof = true; |
| 1506 | if (tomoyo_str_starts(&buf, "global-pid ")) |
| 1507 | global_pid = true; |
| 1508 | pid = (unsigned int) simple_strtoul(buf, NULL, 10); |
| 1509 | rcu_read_lock(); |
| 1510 | read_lock(&tasklist_lock); |
| 1511 | if (global_pid) |
| 1512 | p = find_task_by_pid_ns(pid, &init_pid_ns); |
| 1513 | else |
| 1514 | p = find_task_by_vpid(pid); |
| 1515 | if (p) |
| 1516 | domain = tomoyo_real_domain(p); |
| 1517 | read_unlock(&tasklist_lock); |
| 1518 | rcu_read_unlock(); |
| 1519 | if (!domain) |
| 1520 | return; |
| 1521 | tomoyo_io_printf(head, "%u %u ", pid, domain->profile); |
| 1522 | tomoyo_set_string(head, domain->domainname->name); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1523 | } |
| 1524 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 1525 | /* String table for domain transition control keywords. */ |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1526 | static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1527 | [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ", |
| 1528 | [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ", |
| 1529 | [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ", |
| 1530 | [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ", |
| 1531 | [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ", |
| 1532 | [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ", |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1533 | }; |
| 1534 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 1535 | /* String table for grouping keywords. */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1536 | static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 1537 | [TOMOYO_PATH_GROUP] = "path_group ", |
| 1538 | [TOMOYO_NUMBER_GROUP] = "number_group ", |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1539 | }; |
| 1540 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1541 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1542 | * tomoyo_write_exception - Write exception policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1543 | * |
| 1544 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1545 | * |
| 1546 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1547 | * |
| 1548 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1549 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1550 | static int tomoyo_write_exception(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1551 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1552 | const bool is_delete = head->w.is_delete; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1553 | struct tomoyo_acl_param param = { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1554 | .ns = head->w.ns, |
| 1555 | .is_delete = is_delete, |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1556 | .data = head->write_buf, |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1557 | }; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1558 | u8 i; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1559 | if (tomoyo_str_starts(¶m.data, "aggregator ")) |
| 1560 | return tomoyo_write_aggregator(¶m); |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1561 | for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1562 | if (tomoyo_str_starts(¶m.data, tomoyo_transition_type[i])) |
| 1563 | return tomoyo_write_transition_control(¶m, i); |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1564 | for (i = 0; i < TOMOYO_MAX_GROUP; i++) |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 1565 | if (tomoyo_str_starts(¶m.data, tomoyo_group_name[i])) |
| 1566 | return tomoyo_write_group(¶m, i); |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1567 | if (tomoyo_str_starts(¶m.data, "acl_group ")) { |
| 1568 | unsigned int group; |
| 1569 | char *data; |
| 1570 | group = simple_strtoul(param.data, &data, 10); |
| 1571 | if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ') |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1572 | return tomoyo_write_domain2 |
| 1573 | (head->w.ns, &head->w.ns->acl_group[group], |
| 1574 | data, is_delete); |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1575 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1579 | /** |
| 1580 | * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list. |
| 1581 | * |
| 1582 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1583 | * @idx: Index number. |
| 1584 | * |
| 1585 | * Returns true on success, false otherwise. |
| 1586 | * |
| 1587 | * Caller holds tomoyo_read_lock(). |
| 1588 | */ |
| 1589 | static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) |
| 1590 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1591 | struct tomoyo_policy_namespace *ns = |
| 1592 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 1593 | struct list_head *list = &ns->group_list[idx]; |
| 1594 | list_for_each_cookie(head->r.group, list) { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1595 | struct tomoyo_group *group = |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 1596 | list_entry(head->r.group, typeof(*group), head.list); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1597 | list_for_each_cookie(head->r.acl, &group->member_list) { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1598 | struct tomoyo_acl_head *ptr = |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1599 | list_entry(head->r.acl, typeof(*ptr), list); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1600 | if (ptr->is_deleted) |
| 1601 | continue; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1602 | if (!tomoyo_flush(head)) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1603 | return false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1604 | tomoyo_print_namespace(head); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1605 | tomoyo_set_string(head, tomoyo_group_name[idx]); |
| 1606 | tomoyo_set_string(head, group->group_name->name); |
| 1607 | if (idx == TOMOYO_PATH_GROUP) { |
| 1608 | tomoyo_set_space(head); |
| 1609 | tomoyo_set_string(head, container_of |
| 1610 | (ptr, struct tomoyo_path_group, |
| 1611 | head)->member_name->name); |
| 1612 | } else if (idx == TOMOYO_NUMBER_GROUP) { |
| 1613 | tomoyo_print_number_union(head, &container_of |
| 1614 | (ptr, |
| 1615 | struct tomoyo_number_group, |
| 1616 | head)->number); |
| 1617 | } |
| 1618 | tomoyo_set_lf(head); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1619 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1620 | head->r.acl = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1621 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1622 | head->r.group = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
| 1627 | * tomoyo_read_policy - Read "struct tomoyo_..._entry" list. |
| 1628 | * |
| 1629 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1630 | * @idx: Index number. |
| 1631 | * |
| 1632 | * Returns true on success, false otherwise. |
| 1633 | * |
| 1634 | * Caller holds tomoyo_read_lock(). |
| 1635 | */ |
| 1636 | static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) |
| 1637 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1638 | struct tomoyo_policy_namespace *ns = |
| 1639 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 1640 | struct list_head *list = &ns->policy_list[idx]; |
| 1641 | list_for_each_cookie(head->r.acl, list) { |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1642 | struct tomoyo_acl_head *acl = |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1643 | container_of(head->r.acl, typeof(*acl), list); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1644 | if (acl->is_deleted) |
| 1645 | continue; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1646 | if (!tomoyo_flush(head)) |
| 1647 | return false; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1648 | switch (idx) { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1649 | case TOMOYO_ID_TRANSITION_CONTROL: |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1650 | { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1651 | struct tomoyo_transition_control *ptr = |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1652 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1653 | tomoyo_print_namespace(head); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1654 | tomoyo_set_string(head, tomoyo_transition_type |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1655 | [ptr->type]); |
Tetsuo Handa | 0d2171d | 2011-06-26 23:17:46 +0900 | [diff] [blame] | 1656 | tomoyo_set_string(head, ptr->program ? |
| 1657 | ptr->program->name : "any"); |
| 1658 | tomoyo_set_string(head, " from "); |
| 1659 | tomoyo_set_string(head, ptr->domainname ? |
| 1660 | ptr->domainname->name : |
| 1661 | "any"); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1662 | } |
| 1663 | break; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1664 | case TOMOYO_ID_AGGREGATOR: |
| 1665 | { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1666 | struct tomoyo_aggregator *ptr = |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1667 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1668 | tomoyo_print_namespace(head); |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 1669 | tomoyo_set_string(head, "aggregator "); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1670 | tomoyo_set_string(head, |
| 1671 | ptr->original_name->name); |
| 1672 | tomoyo_set_space(head); |
| 1673 | tomoyo_set_string(head, |
| 1674 | ptr->aggregated_name->name); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1675 | } |
| 1676 | break; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1677 | default: |
| 1678 | continue; |
| 1679 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1680 | tomoyo_set_lf(head); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1681 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1682 | head->r.acl = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1683 | return true; |
| 1684 | } |
| 1685 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1686 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1687 | * tomoyo_read_exception - Read exception policy. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1688 | * |
| 1689 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1690 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1691 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1692 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1693 | static void tomoyo_read_exception(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1694 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1695 | struct tomoyo_policy_namespace *ns = |
| 1696 | container_of(head->r.ns, typeof(*ns), namespace_list); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1697 | if (head->r.eof) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1698 | return; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1699 | while (head->r.step < TOMOYO_MAX_POLICY && |
| 1700 | tomoyo_read_policy(head, head->r.step)) |
| 1701 | head->r.step++; |
| 1702 | if (head->r.step < TOMOYO_MAX_POLICY) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1703 | return; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1704 | while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP && |
| 1705 | tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY)) |
| 1706 | head->r.step++; |
| 1707 | if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1708 | return; |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1709 | while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP |
| 1710 | + TOMOYO_MAX_ACL_GROUPS) { |
| 1711 | head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY |
| 1712 | - TOMOYO_MAX_GROUP; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1713 | if (!tomoyo_read_domain2(head, &ns->acl_group |
Tetsuo Handa | 3299714 | 2011-06-26 23:19:28 +0900 | [diff] [blame] | 1714 | [head->r.acl_group_index])) |
| 1715 | return; |
| 1716 | head->r.step++; |
| 1717 | } |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1718 | head->r.eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1719 | } |
| 1720 | |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1721 | /* Wait queue for kernel -> userspace notification. */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1722 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1723 | /* Wait queue for userspace -> kernel notification. */ |
| 1724 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1725 | |
| 1726 | /* Structure for query. */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1727 | struct tomoyo_query { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1728 | struct list_head list; |
| 1729 | char *query; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1730 | size_t query_len; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1731 | unsigned int serial; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1732 | u8 timer; |
| 1733 | u8 answer; |
| 1734 | u8 retry; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1735 | }; |
| 1736 | |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1737 | /* The list for "struct tomoyo_query". */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1738 | static LIST_HEAD(tomoyo_query_list); |
| 1739 | |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1740 | /* Lock for manipulating tomoyo_query_list. */ |
| 1741 | static DEFINE_SPINLOCK(tomoyo_query_list_lock); |
| 1742 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1743 | /* |
| 1744 | * Number of "struct file" referring /sys/kernel/security/tomoyo/query |
| 1745 | * interface. |
| 1746 | */ |
| 1747 | static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); |
| 1748 | |
| 1749 | /** |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1750 | * tomoyo_truncate - Truncate a line. |
| 1751 | * |
| 1752 | * @str: String to truncate. |
| 1753 | * |
| 1754 | * Returns length of truncated @str. |
| 1755 | */ |
| 1756 | static int tomoyo_truncate(char *str) |
| 1757 | { |
| 1758 | char *start = str; |
| 1759 | while (*(unsigned char *) str > (unsigned char) ' ') |
| 1760 | str++; |
| 1761 | *str = '\0'; |
| 1762 | return strlen(start) + 1; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1766 | * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode. |
| 1767 | * |
| 1768 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 1769 | * @header: Lines containing ACL. |
| 1770 | * |
| 1771 | * Returns nothing. |
| 1772 | */ |
| 1773 | static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header) |
| 1774 | { |
| 1775 | char *buffer; |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1776 | char *realpath = NULL; |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1777 | char *argv0 = NULL; |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1778 | char *symlink = NULL; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1779 | char *cp = strchr(header, '\n'); |
| 1780 | int len; |
| 1781 | if (!cp) |
| 1782 | return; |
| 1783 | cp = strchr(cp + 1, '\n'); |
| 1784 | if (!cp) |
| 1785 | return; |
| 1786 | *cp++ = '\0'; |
| 1787 | len = strlen(cp) + 1; |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1788 | /* strstr() will return NULL if ordering is wrong. */ |
| 1789 | if (*cp == 'f') { |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1790 | argv0 = strstr(header, " argv[]={ \""); |
| 1791 | if (argv0) { |
| 1792 | argv0 += 10; |
| 1793 | len += tomoyo_truncate(argv0) + 14; |
| 1794 | } |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1795 | realpath = strstr(header, " exec={ realpath=\""); |
| 1796 | if (realpath) { |
| 1797 | realpath += 8; |
| 1798 | len += tomoyo_truncate(realpath) + 6; |
| 1799 | } |
| 1800 | symlink = strstr(header, " symlink.target=\""); |
| 1801 | if (symlink) |
| 1802 | len += tomoyo_truncate(symlink + 1) + 1; |
| 1803 | } |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1804 | buffer = kmalloc(len, GFP_NOFS); |
| 1805 | if (!buffer) |
| 1806 | return; |
| 1807 | snprintf(buffer, len - 1, "%s", cp); |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1808 | if (realpath) |
| 1809 | tomoyo_addprintf(buffer, len, " exec.%s", realpath); |
Tetsuo Handa | 5b63685 | 2011-07-08 13:24:54 +0900 | [diff] [blame] | 1810 | if (argv0) |
| 1811 | tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0); |
Tetsuo Handa | 2ca9bf4 | 2011-07-08 13:23:44 +0900 | [diff] [blame] | 1812 | if (symlink) |
| 1813 | tomoyo_addprintf(buffer, len, "%s", symlink); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1814 | tomoyo_normalize_line(buffer); |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 1815 | if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer, |
| 1816 | false)) |
| 1817 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1818 | kfree(buffer); |
| 1819 | } |
| 1820 | |
| 1821 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1822 | * tomoyo_supervisor - Ask for the supervisor's decision. |
| 1823 | * |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1824 | * @r: Pointer to "struct tomoyo_request_info". |
| 1825 | * @fmt: The printf()'s format string, followed by parameters. |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1826 | * |
| 1827 | * Returns 0 if the supervisor decided to permit the access request which |
| 1828 | * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the |
| 1829 | * supervisor decided to retry the access request which violated the policy in |
| 1830 | * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise. |
| 1831 | */ |
| 1832 | int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) |
| 1833 | { |
| 1834 | va_list args; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1835 | int error; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1836 | int len; |
| 1837 | static unsigned int tomoyo_serial; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1838 | struct tomoyo_query entry = { }; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1839 | bool quota_exceeded = false; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1840 | va_start(args, fmt); |
| 1841 | len = vsnprintf((char *) &len, 1, fmt, args) + 1; |
| 1842 | va_end(args); |
| 1843 | /* Write /sys/kernel/security/tomoyo/audit. */ |
| 1844 | va_start(args, fmt); |
| 1845 | tomoyo_write_log2(r, len, fmt, args); |
| 1846 | va_end(args); |
| 1847 | /* Nothing more to do if granted. */ |
| 1848 | if (r->granted) |
| 1849 | return 0; |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 1850 | if (r->mode) |
| 1851 | tomoyo_update_stat(r->mode); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1852 | switch (r->mode) { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1853 | case TOMOYO_CONFIG_ENFORCING: |
| 1854 | error = -EPERM; |
| 1855 | if (atomic_read(&tomoyo_query_observers)) |
| 1856 | break; |
| 1857 | goto out; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1858 | case TOMOYO_CONFIG_LEARNING: |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1859 | error = 0; |
| 1860 | /* Check max_learning_entry parameter. */ |
| 1861 | if (tomoyo_domain_quota_is_ok(r)) |
| 1862 | break; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1863 | /* fall through */ |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1864 | default: |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1867 | /* Get message. */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1868 | va_start(args, fmt); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1869 | entry.query = tomoyo_init_log(r, len, fmt, args); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1870 | va_end(args); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1871 | if (!entry.query) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1872 | goto out; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1873 | entry.query_len = strlen(entry.query) + 1; |
| 1874 | if (!error) { |
| 1875 | tomoyo_add_entry(r->domain, entry.query); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1876 | goto out; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1877 | } |
| 1878 | len = tomoyo_round2(entry.query_len); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1879 | spin_lock(&tomoyo_query_list_lock); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1880 | if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] && |
| 1881 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len |
| 1882 | >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1883 | quota_exceeded = true; |
| 1884 | } else { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1885 | entry.serial = tomoyo_serial++; |
| 1886 | entry.retry = r->retry; |
| 1887 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len; |
| 1888 | list_add_tail(&entry.list, &tomoyo_query_list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1889 | } |
| 1890 | spin_unlock(&tomoyo_query_list_lock); |
| 1891 | if (quota_exceeded) |
| 1892 | goto out; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1893 | /* Give 10 seconds for supervisor's opinion. */ |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1894 | while (entry.timer < 10) { |
| 1895 | wake_up_all(&tomoyo_query_wait); |
| 1896 | if (wait_event_interruptible_timeout |
| 1897 | (tomoyo_answer_wait, entry.answer || |
| 1898 | !atomic_read(&tomoyo_query_observers), HZ)) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1899 | break; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1900 | else |
| 1901 | entry.timer++; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1902 | } |
| 1903 | spin_lock(&tomoyo_query_list_lock); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1904 | list_del(&entry.list); |
| 1905 | tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1906 | spin_unlock(&tomoyo_query_list_lock); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1907 | switch (entry.answer) { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1908 | case 3: /* Asked to retry by administrator. */ |
| 1909 | error = TOMOYO_RETRY_REQUEST; |
| 1910 | r->retry++; |
| 1911 | break; |
| 1912 | case 1: |
| 1913 | /* Granted by administrator. */ |
| 1914 | error = 0; |
| 1915 | break; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1916 | default: |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1917 | /* Timed out or rejected by administrator. */ |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1918 | break; |
| 1919 | } |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1920 | out: |
| 1921 | kfree(entry.query); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1922 | return error; |
| 1923 | } |
| 1924 | |
| 1925 | /** |
| 1926 | * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query. |
| 1927 | * |
| 1928 | * @file: Pointer to "struct file". |
| 1929 | * @wait: Pointer to "poll_table". |
| 1930 | * |
| 1931 | * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. |
| 1932 | * |
| 1933 | * Waits for access requests which violated policy in enforcing mode. |
| 1934 | */ |
| 1935 | static int tomoyo_poll_query(struct file *file, poll_table *wait) |
| 1936 | { |
| 1937 | struct list_head *tmp; |
| 1938 | bool found = false; |
| 1939 | u8 i; |
| 1940 | for (i = 0; i < 2; i++) { |
| 1941 | spin_lock(&tomoyo_query_list_lock); |
| 1942 | list_for_each(tmp, &tomoyo_query_list) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1943 | struct tomoyo_query *ptr = |
| 1944 | list_entry(tmp, typeof(*ptr), list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1945 | if (ptr->answer) |
| 1946 | continue; |
| 1947 | found = true; |
| 1948 | break; |
| 1949 | } |
| 1950 | spin_unlock(&tomoyo_query_list_lock); |
| 1951 | if (found) |
| 1952 | return POLLIN | POLLRDNORM; |
| 1953 | if (i) |
| 1954 | break; |
| 1955 | poll_wait(file, &tomoyo_query_wait, wait); |
| 1956 | } |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * tomoyo_read_query - Read access requests which violated policy in enforcing mode. |
| 1962 | * |
| 1963 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1964 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1965 | static void tomoyo_read_query(struct tomoyo_io_buffer *head) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1966 | { |
| 1967 | struct list_head *tmp; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1968 | unsigned int pos = 0; |
| 1969 | size_t len = 0; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1970 | char *buf; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1971 | if (head->r.w_pos) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1972 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1973 | if (head->read_buf) { |
| 1974 | kfree(head->read_buf); |
| 1975 | head->read_buf = NULL; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1976 | } |
| 1977 | spin_lock(&tomoyo_query_list_lock); |
| 1978 | list_for_each(tmp, &tomoyo_query_list) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1979 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1980 | if (ptr->answer) |
| 1981 | continue; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1982 | if (pos++ != head->r.query_index) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1983 | continue; |
| 1984 | len = ptr->query_len; |
| 1985 | break; |
| 1986 | } |
| 1987 | spin_unlock(&tomoyo_query_list_lock); |
| 1988 | if (!len) { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 1989 | head->r.query_index = 0; |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1990 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1991 | } |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 1992 | buf = kzalloc(len + 32, GFP_NOFS); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1993 | if (!buf) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1994 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1995 | pos = 0; |
| 1996 | spin_lock(&tomoyo_query_list_lock); |
| 1997 | list_for_each(tmp, &tomoyo_query_list) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 1998 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1999 | if (ptr->answer) |
| 2000 | continue; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2001 | if (pos++ != head->r.query_index) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2002 | continue; |
| 2003 | /* |
| 2004 | * Some query can be skipped because tomoyo_query_list |
| 2005 | * can change, but I don't care. |
| 2006 | */ |
| 2007 | if (len == ptr->query_len) |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 2008 | snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial, |
| 2009 | ptr->retry, ptr->query); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2010 | break; |
| 2011 | } |
| 2012 | spin_unlock(&tomoyo_query_list_lock); |
| 2013 | if (buf[0]) { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2014 | head->read_buf = buf; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2015 | head->r.w[head->r.w_pos++] = buf; |
| 2016 | head->r.query_index++; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2017 | } else { |
| 2018 | kfree(buf); |
| 2019 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /** |
| 2023 | * tomoyo_write_answer - Write the supervisor's decision. |
| 2024 | * |
| 2025 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2026 | * |
| 2027 | * Returns 0 on success, -EINVAL otherwise. |
| 2028 | */ |
| 2029 | static int tomoyo_write_answer(struct tomoyo_io_buffer *head) |
| 2030 | { |
| 2031 | char *data = head->write_buf; |
| 2032 | struct list_head *tmp; |
| 2033 | unsigned int serial; |
| 2034 | unsigned int answer; |
| 2035 | spin_lock(&tomoyo_query_list_lock); |
| 2036 | list_for_each(tmp, &tomoyo_query_list) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 2037 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2038 | ptr->timer = 0; |
| 2039 | } |
| 2040 | spin_unlock(&tomoyo_query_list_lock); |
| 2041 | if (sscanf(data, "A%u=%u", &serial, &answer) != 2) |
| 2042 | return -EINVAL; |
| 2043 | spin_lock(&tomoyo_query_list_lock); |
| 2044 | list_for_each(tmp, &tomoyo_query_list) { |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 2045 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2046 | if (ptr->serial != serial) |
| 2047 | continue; |
| 2048 | if (!ptr->answer) |
| 2049 | ptr->answer = answer; |
| 2050 | break; |
| 2051 | } |
| 2052 | spin_unlock(&tomoyo_query_list_lock); |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2057 | * tomoyo_read_version: Get version. |
| 2058 | * |
| 2059 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2060 | * |
| 2061 | * Returns version information. |
| 2062 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 2063 | static void tomoyo_read_version(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2064 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2065 | if (!head->r.eof) { |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 2066 | tomoyo_io_printf(head, "2.4.0"); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2067 | head->r.eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2068 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * tomoyo_read_self_domain - Get the current process's domainname. |
| 2073 | * |
| 2074 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2075 | * |
| 2076 | * Returns the current process's domainname. |
| 2077 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 2078 | static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2079 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2080 | if (!head->r.eof) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2081 | /* |
| 2082 | * tomoyo_domain()->domainname != NULL |
| 2083 | * because every process belongs to a domain and |
| 2084 | * the domain's name cannot be NULL. |
| 2085 | */ |
| 2086 | tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2087 | head->r.eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2088 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2089 | } |
| 2090 | |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 2091 | /* String table for /sys/kernel/security/tomoyo/stat interface. */ |
| 2092 | static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = { |
| 2093 | [TOMOYO_STAT_POLICY_UPDATES] = "update:", |
| 2094 | [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:", |
| 2095 | [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:", |
| 2096 | [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:", |
| 2097 | }; |
| 2098 | |
| 2099 | /* String table for /sys/kernel/security/tomoyo/stat interface. */ |
| 2100 | static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = { |
| 2101 | [TOMOYO_MEMORY_POLICY] = "policy:", |
| 2102 | [TOMOYO_MEMORY_AUDIT] = "audit log:", |
| 2103 | [TOMOYO_MEMORY_QUERY] = "query message:", |
| 2104 | }; |
| 2105 | |
| 2106 | /* Timestamp counter for last updated. */ |
| 2107 | static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT]; |
| 2108 | /* Counter for number of updates. */ |
| 2109 | static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT]; |
| 2110 | |
| 2111 | /** |
| 2112 | * tomoyo_update_stat - Update statistic counters. |
| 2113 | * |
| 2114 | * @index: Index for policy type. |
| 2115 | * |
| 2116 | * Returns nothing. |
| 2117 | */ |
| 2118 | void tomoyo_update_stat(const u8 index) |
| 2119 | { |
| 2120 | struct timeval tv; |
| 2121 | do_gettimeofday(&tv); |
| 2122 | /* |
| 2123 | * I don't use atomic operations because race condition is not fatal. |
| 2124 | */ |
| 2125 | tomoyo_stat_updated[index]++; |
| 2126 | tomoyo_stat_modified[index] = tv.tv_sec; |
| 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * tomoyo_read_stat - Read statistic data. |
| 2131 | * |
| 2132 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2133 | * |
| 2134 | * Returns nothing. |
| 2135 | */ |
| 2136 | static void tomoyo_read_stat(struct tomoyo_io_buffer *head) |
| 2137 | { |
| 2138 | u8 i; |
| 2139 | unsigned int total = 0; |
| 2140 | if (head->r.eof) |
| 2141 | return; |
| 2142 | for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) { |
| 2143 | tomoyo_io_printf(head, "Policy %-30s %10u", |
| 2144 | tomoyo_policy_headers[i], |
| 2145 | tomoyo_stat_updated[i]); |
| 2146 | if (tomoyo_stat_modified[i]) { |
| 2147 | struct tomoyo_time stamp; |
| 2148 | tomoyo_convert_time(tomoyo_stat_modified[i], &stamp); |
| 2149 | tomoyo_io_printf(head, " (Last: %04u/%02u/%02u " |
| 2150 | "%02u:%02u:%02u)", |
| 2151 | stamp.year, stamp.month, stamp.day, |
| 2152 | stamp.hour, stamp.min, stamp.sec); |
| 2153 | } |
| 2154 | tomoyo_set_lf(head); |
| 2155 | } |
| 2156 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) { |
| 2157 | unsigned int used = tomoyo_memory_used[i]; |
| 2158 | total += used; |
| 2159 | tomoyo_io_printf(head, "Memory used by %-22s %10u", |
| 2160 | tomoyo_memory_headers[i], used); |
| 2161 | used = tomoyo_memory_quota[i]; |
| 2162 | if (used) |
| 2163 | tomoyo_io_printf(head, " (Quota: %10u)", used); |
| 2164 | tomoyo_set_lf(head); |
| 2165 | } |
| 2166 | tomoyo_io_printf(head, "Total memory used: %10u\n", |
| 2167 | total); |
| 2168 | head->r.eof = true; |
| 2169 | } |
| 2170 | |
| 2171 | /** |
| 2172 | * tomoyo_write_stat - Set memory quota. |
| 2173 | * |
| 2174 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2175 | * |
| 2176 | * Returns 0. |
| 2177 | */ |
| 2178 | static int tomoyo_write_stat(struct tomoyo_io_buffer *head) |
| 2179 | { |
| 2180 | char *data = head->write_buf; |
| 2181 | u8 i; |
| 2182 | if (tomoyo_str_starts(&data, "Memory used by ")) |
| 2183 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) |
| 2184 | if (tomoyo_str_starts(&data, tomoyo_memory_headers[i])) |
| 2185 | sscanf(data, "%u", &tomoyo_memory_quota[i]); |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2189 | /** |
| 2190 | * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface. |
| 2191 | * |
| 2192 | * @type: Type of interface. |
| 2193 | * @file: Pointer to "struct file". |
| 2194 | * |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2195 | * Returns 0 on success, negative value otherwise. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2196 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2197 | int tomoyo_open_control(const u8 type, struct file *file) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2198 | { |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 2199 | struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2200 | |
| 2201 | if (!head) |
| 2202 | return -ENOMEM; |
| 2203 | mutex_init(&head->io_sem); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2204 | head->type = type; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2205 | switch (type) { |
| 2206 | case TOMOYO_DOMAINPOLICY: |
| 2207 | /* /sys/kernel/security/tomoyo/domain_policy */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 2208 | head->write = tomoyo_write_domain; |
| 2209 | head->read = tomoyo_read_domain; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2210 | break; |
| 2211 | case TOMOYO_EXCEPTIONPOLICY: |
| 2212 | /* /sys/kernel/security/tomoyo/exception_policy */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 2213 | head->write = tomoyo_write_exception; |
| 2214 | head->read = tomoyo_read_exception; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2215 | break; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 2216 | case TOMOYO_AUDIT: |
| 2217 | /* /sys/kernel/security/tomoyo/audit */ |
| 2218 | head->poll = tomoyo_poll_log; |
| 2219 | head->read = tomoyo_read_log; |
| 2220 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2221 | case TOMOYO_SELFDOMAIN: |
| 2222 | /* /sys/kernel/security/tomoyo/self_domain */ |
| 2223 | head->read = tomoyo_read_self_domain; |
| 2224 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2225 | case TOMOYO_PROCESS_STATUS: |
| 2226 | /* /sys/kernel/security/tomoyo/.process_status */ |
| 2227 | head->write = tomoyo_write_pid; |
| 2228 | head->read = tomoyo_read_pid; |
| 2229 | break; |
| 2230 | case TOMOYO_VERSION: |
| 2231 | /* /sys/kernel/security/tomoyo/version */ |
| 2232 | head->read = tomoyo_read_version; |
| 2233 | head->readbuf_size = 128; |
| 2234 | break; |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 2235 | case TOMOYO_STAT: |
| 2236 | /* /sys/kernel/security/tomoyo/stat */ |
| 2237 | head->write = tomoyo_write_stat; |
| 2238 | head->read = tomoyo_read_stat; |
| 2239 | head->readbuf_size = 1024; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2240 | break; |
| 2241 | case TOMOYO_PROFILE: |
| 2242 | /* /sys/kernel/security/tomoyo/profile */ |
| 2243 | head->write = tomoyo_write_profile; |
| 2244 | head->read = tomoyo_read_profile; |
| 2245 | break; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2246 | case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */ |
| 2247 | head->poll = tomoyo_poll_query; |
| 2248 | head->write = tomoyo_write_answer; |
| 2249 | head->read = tomoyo_read_query; |
| 2250 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2251 | case TOMOYO_MANAGER: |
| 2252 | /* /sys/kernel/security/tomoyo/manager */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 2253 | head->write = tomoyo_write_manager; |
| 2254 | head->read = tomoyo_read_manager; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2255 | break; |
| 2256 | } |
| 2257 | if (!(file->f_mode & FMODE_READ)) { |
| 2258 | /* |
| 2259 | * No need to allocate read_buf since it is not opened |
| 2260 | * for reading. |
| 2261 | */ |
| 2262 | head->read = NULL; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2263 | head->poll = NULL; |
| 2264 | } else if (!head->poll) { |
| 2265 | /* Don't allocate read_buf for poll() access. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2266 | if (!head->readbuf_size) |
| 2267 | head->readbuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 2268 | head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2269 | if (!head->read_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 2270 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2271 | return -ENOMEM; |
| 2272 | } |
| 2273 | } |
| 2274 | if (!(file->f_mode & FMODE_WRITE)) { |
| 2275 | /* |
| 2276 | * No need to allocate write_buf since it is not opened |
| 2277 | * for writing. |
| 2278 | */ |
| 2279 | head->write = NULL; |
| 2280 | } else if (head->write) { |
| 2281 | head->writebuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 2282 | head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2283 | if (!head->write_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 2284 | kfree(head->read_buf); |
| 2285 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2286 | return -ENOMEM; |
| 2287 | } |
| 2288 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2289 | /* |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2290 | * If the file is /sys/kernel/security/tomoyo/query , increment the |
| 2291 | * observer counter. |
| 2292 | * The obserber counter is used by tomoyo_supervisor() to see if |
| 2293 | * there is some process monitoring /sys/kernel/security/tomoyo/query. |
| 2294 | */ |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 2295 | if (type == TOMOYO_QUERY) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2296 | atomic_inc(&tomoyo_query_observers); |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2297 | file->private_data = head; |
| 2298 | tomoyo_notify_gc(head, true); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2299 | return 0; |
| 2300 | } |
| 2301 | |
| 2302 | /** |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 2303 | * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface. |
| 2304 | * |
| 2305 | * @file: Pointer to "struct file". |
| 2306 | * @wait: Pointer to "poll_table". |
| 2307 | * |
| 2308 | * Waits for read readiness. |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 2309 | * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and |
| 2310 | * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd. |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 2311 | */ |
| 2312 | int tomoyo_poll_control(struct file *file, poll_table *wait) |
| 2313 | { |
| 2314 | struct tomoyo_io_buffer *head = file->private_data; |
| 2315 | if (!head->poll) |
| 2316 | return -ENOSYS; |
| 2317 | return head->poll(file, wait); |
| 2318 | } |
| 2319 | |
| 2320 | /** |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2321 | * tomoyo_set_namespace_cursor - Set namespace to read. |
| 2322 | * |
| 2323 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2324 | * |
| 2325 | * Returns nothing. |
| 2326 | */ |
| 2327 | static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head) |
| 2328 | { |
| 2329 | struct list_head *ns; |
| 2330 | if (head->type != TOMOYO_EXCEPTIONPOLICY && |
| 2331 | head->type != TOMOYO_PROFILE) |
| 2332 | return; |
| 2333 | /* |
| 2334 | * If this is the first read, or reading previous namespace finished |
| 2335 | * and has more namespaces to read, update the namespace cursor. |
| 2336 | */ |
| 2337 | ns = head->r.ns; |
| 2338 | if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) { |
| 2339 | /* Clearing is OK because tomoyo_flush() returned true. */ |
| 2340 | memset(&head->r, 0, sizeof(head->r)); |
| 2341 | head->r.ns = ns ? ns->next : tomoyo_namespace_list.next; |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | /** |
| 2346 | * tomoyo_has_more_namespace - Check for unread namespaces. |
| 2347 | * |
| 2348 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 2349 | * |
| 2350 | * Returns true if we have more entries to print, false otherwise. |
| 2351 | */ |
| 2352 | static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head) |
| 2353 | { |
| 2354 | return (head->type == TOMOYO_EXCEPTIONPOLICY || |
| 2355 | head->type == TOMOYO_PROFILE) && head->r.eof && |
| 2356 | head->r.ns->next != &tomoyo_namespace_list; |
| 2357 | } |
| 2358 | |
| 2359 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2360 | * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface. |
| 2361 | * |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2362 | * @head: Pointer to "struct tomoyo_io_buffer". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2363 | * @buffer: Poiner to buffer to write to. |
| 2364 | * @buffer_len: Size of @buffer. |
| 2365 | * |
| 2366 | * Returns bytes read on success, negative value otherwise. |
| 2367 | */ |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 2368 | ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer, |
| 2369 | const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2370 | { |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2371 | int len; |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2372 | int idx; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2373 | |
| 2374 | if (!head->read) |
| 2375 | return -ENOSYS; |
| 2376 | if (mutex_lock_interruptible(&head->io_sem)) |
| 2377 | return -EINTR; |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2378 | head->read_user_buf = buffer; |
| 2379 | head->read_user_buf_avail = buffer_len; |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2380 | idx = tomoyo_read_lock(); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2381 | if (tomoyo_flush(head)) |
| 2382 | /* Call the policy handler. */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2383 | do { |
| 2384 | tomoyo_set_namespace_cursor(head); |
| 2385 | head->read(head); |
| 2386 | } while (tomoyo_flush(head) && |
| 2387 | tomoyo_has_more_namespace(head)); |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2388 | tomoyo_read_unlock(idx); |
Tetsuo Handa | f23571e | 2010-06-24 14:57:16 +0900 | [diff] [blame] | 2389 | len = head->read_user_buf - buffer; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2390 | mutex_unlock(&head->io_sem); |
| 2391 | return len; |
| 2392 | } |
| 2393 | |
| 2394 | /** |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2395 | * tomoyo_parse_policy - Parse a policy line. |
| 2396 | * |
| 2397 | * @head: Poiter to "struct tomoyo_io_buffer". |
| 2398 | * @line: Line to parse. |
| 2399 | * |
| 2400 | * Returns 0 on success, negative value otherwise. |
| 2401 | * |
| 2402 | * Caller holds tomoyo_read_lock(). |
| 2403 | */ |
| 2404 | static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line) |
| 2405 | { |
| 2406 | /* Delete request? */ |
| 2407 | head->w.is_delete = !strncmp(line, "delete ", 7); |
| 2408 | if (head->w.is_delete) |
| 2409 | memmove(line, line + 7, strlen(line + 7) + 1); |
| 2410 | /* Selecting namespace to update. */ |
| 2411 | if (head->type == TOMOYO_EXCEPTIONPOLICY || |
| 2412 | head->type == TOMOYO_PROFILE) { |
| 2413 | if (*line == '<') { |
| 2414 | char *cp = strchr(line, ' '); |
| 2415 | if (cp) { |
| 2416 | *cp++ = '\0'; |
| 2417 | head->w.ns = tomoyo_assign_namespace(line); |
| 2418 | memmove(line, cp, strlen(cp) + 1); |
| 2419 | } else |
| 2420 | head->w.ns = NULL; |
| 2421 | } else |
| 2422 | head->w.ns = &tomoyo_kernel_namespace; |
| 2423 | /* Don't allow updating if namespace is invalid. */ |
| 2424 | if (!head->w.ns) |
| 2425 | return -ENOENT; |
| 2426 | } |
| 2427 | /* Do the update. */ |
| 2428 | return head->write(head); |
| 2429 | } |
| 2430 | |
| 2431 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2432 | * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface. |
| 2433 | * |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2434 | * @head: Pointer to "struct tomoyo_io_buffer". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2435 | * @buffer: Pointer to buffer to read from. |
| 2436 | * @buffer_len: Size of @buffer. |
| 2437 | * |
| 2438 | * Returns @buffer_len on success, negative value otherwise. |
| 2439 | */ |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 2440 | ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, |
| 2441 | const char __user *buffer, const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2442 | { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2443 | int error = buffer_len; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2444 | size_t avail_len = buffer_len; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2445 | char *cp0 = head->write_buf; |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2446 | int idx; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2447 | if (!head->write) |
| 2448 | return -ENOSYS; |
| 2449 | if (!access_ok(VERIFY_READ, buffer, buffer_len)) |
| 2450 | return -EFAULT; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2451 | if (mutex_lock_interruptible(&head->io_sem)) |
| 2452 | return -EINTR; |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2453 | idx = tomoyo_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2454 | /* Read a line and dispatch it to the policy handler. */ |
| 2455 | while (avail_len > 0) { |
| 2456 | char c; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2457 | if (head->w.avail >= head->writebuf_size - 1) { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2458 | const int len = head->writebuf_size * 2; |
| 2459 | char *cp = kzalloc(len, GFP_NOFS); |
| 2460 | if (!cp) { |
| 2461 | error = -ENOMEM; |
| 2462 | break; |
| 2463 | } |
| 2464 | memmove(cp, cp0, head->w.avail); |
| 2465 | kfree(cp0); |
| 2466 | head->write_buf = cp; |
| 2467 | cp0 = cp; |
| 2468 | head->writebuf_size = len; |
| 2469 | } |
| 2470 | if (get_user(c, buffer)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2471 | error = -EFAULT; |
| 2472 | break; |
| 2473 | } |
| 2474 | buffer++; |
| 2475 | avail_len--; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2476 | cp0[head->w.avail++] = c; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2477 | if (c != '\n') |
| 2478 | continue; |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2479 | cp0[head->w.avail - 1] = '\0'; |
| 2480 | head->w.avail = 0; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2481 | tomoyo_normalize_line(cp0); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2482 | if (!strcmp(cp0, "reset")) { |
| 2483 | head->w.ns = &tomoyo_kernel_namespace; |
| 2484 | head->w.domain = NULL; |
| 2485 | memset(&head->r, 0, sizeof(head->r)); |
| 2486 | continue; |
| 2487 | } |
| 2488 | /* Don't allow updating policies by non manager programs. */ |
| 2489 | switch (head->type) { |
| 2490 | case TOMOYO_PROCESS_STATUS: |
| 2491 | /* This does not write anything. */ |
| 2492 | break; |
| 2493 | case TOMOYO_DOMAINPOLICY: |
| 2494 | if (tomoyo_select_domain(head, cp0)) |
| 2495 | continue; |
| 2496 | /* fall through */ |
| 2497 | case TOMOYO_EXCEPTIONPOLICY: |
| 2498 | if (!strcmp(cp0, "select transition_only")) { |
| 2499 | head->r.print_transition_related_only = true; |
| 2500 | continue; |
| 2501 | } |
| 2502 | /* fall through */ |
| 2503 | default: |
| 2504 | if (!tomoyo_manager()) { |
| 2505 | error = -EPERM; |
| 2506 | goto out; |
| 2507 | } |
| 2508 | } |
| 2509 | switch (tomoyo_parse_policy(head, cp0)) { |
| 2510 | case -EPERM: |
| 2511 | error = -EPERM; |
| 2512 | goto out; |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 2513 | case 0: |
| 2514 | switch (head->type) { |
| 2515 | case TOMOYO_DOMAINPOLICY: |
| 2516 | case TOMOYO_EXCEPTIONPOLICY: |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 2517 | case TOMOYO_STAT: |
| 2518 | case TOMOYO_PROFILE: |
| 2519 | case TOMOYO_MANAGER: |
| 2520 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); |
| 2521 | break; |
| 2522 | default: |
| 2523 | break; |
| 2524 | } |
| 2525 | break; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2526 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2527 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2528 | out: |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2529 | tomoyo_read_unlock(idx); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2530 | mutex_unlock(&head->io_sem); |
| 2531 | return error; |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface. |
| 2536 | * |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2537 | * @head: Pointer to "struct tomoyo_io_buffer". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2538 | * |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2539 | * Returns 0. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2540 | */ |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 2541 | int tomoyo_close_control(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2542 | { |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 2543 | /* |
| 2544 | * If the file is /sys/kernel/security/tomoyo/query , decrement the |
| 2545 | * observer counter. |
| 2546 | */ |
Tetsuo Handa | 2e503bb | 2011-06-26 23:20:55 +0900 | [diff] [blame] | 2547 | if (head->type == TOMOYO_QUERY && |
| 2548 | atomic_dec_and_test(&tomoyo_query_observers)) |
| 2549 | wake_up_all(&tomoyo_answer_wait); |
| 2550 | tomoyo_notify_gc(head, false); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2551 | return 0; |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2555 | * tomoyo_check_profile - Check all profiles currently assigned to domains are defined. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2556 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2557 | void tomoyo_check_profile(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2558 | { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2559 | struct tomoyo_domain_info *domain; |
| 2560 | const int idx = tomoyo_read_lock(); |
| 2561 | tomoyo_policy_loaded = true; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2562 | printk(KERN_INFO "TOMOYO: 2.4.0\n"); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2563 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 2564 | const u8 profile = domain->profile; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2565 | const struct tomoyo_policy_namespace *ns = domain->ns; |
| 2566 | if (ns->profile_version != 20100903) |
| 2567 | printk(KERN_ERR |
| 2568 | "Profile version %u is not supported.\n", |
| 2569 | ns->profile_version); |
| 2570 | else if (!ns->profile_ptr[profile]) |
| 2571 | printk(KERN_ERR |
| 2572 | "Profile %u (used by '%s') is not defined.\n", |
| 2573 | profile, domain->domainname->name); |
| 2574 | else |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2575 | continue; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2576 | printk(KERN_ERR |
| 2577 | "Userland tools for TOMOYO 2.4 must be installed and " |
| 2578 | "policy must be initialized.\n"); |
| 2579 | printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ " |
Tetsuo Handa | 9f1c1d4 | 2010-10-08 14:43:22 +0900 | [diff] [blame] | 2580 | "for more information.\n"); |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 2581 | panic("STOP!"); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2582 | } |
| 2583 | tomoyo_read_unlock(idx); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2584 | printk(KERN_INFO "Mandatory Access Control activated.\n"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 2585 | } |
Tetsuo Handa | efe836a | 2011-06-26 23:22:18 +0900 | [diff] [blame] | 2586 | |
| 2587 | /** |
| 2588 | * tomoyo_load_builtin_policy - Load built-in policy. |
| 2589 | * |
| 2590 | * Returns nothing. |
| 2591 | */ |
| 2592 | void __init tomoyo_load_builtin_policy(void) |
| 2593 | { |
| 2594 | /* |
| 2595 | * This include file is manually created and contains built-in policy |
| 2596 | * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy", |
| 2597 | * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager", |
| 2598 | * "tomoyo_builtin_stat" in the form of "static char [] __initdata". |
| 2599 | */ |
| 2600 | #include "builtin-policy.h" |
| 2601 | u8 i; |
| 2602 | const int idx = tomoyo_read_lock(); |
| 2603 | for (i = 0; i < 5; i++) { |
| 2604 | struct tomoyo_io_buffer head = { }; |
| 2605 | char *start = ""; |
| 2606 | switch (i) { |
| 2607 | case 0: |
| 2608 | start = tomoyo_builtin_profile; |
| 2609 | head.type = TOMOYO_PROFILE; |
| 2610 | head.write = tomoyo_write_profile; |
| 2611 | break; |
| 2612 | case 1: |
| 2613 | start = tomoyo_builtin_exception_policy; |
| 2614 | head.type = TOMOYO_EXCEPTIONPOLICY; |
| 2615 | head.write = tomoyo_write_exception; |
| 2616 | break; |
| 2617 | case 2: |
| 2618 | start = tomoyo_builtin_domain_policy; |
| 2619 | head.type = TOMOYO_DOMAINPOLICY; |
| 2620 | head.write = tomoyo_write_domain; |
| 2621 | break; |
| 2622 | case 3: |
| 2623 | start = tomoyo_builtin_manager; |
| 2624 | head.type = TOMOYO_MANAGER; |
| 2625 | head.write = tomoyo_write_manager; |
| 2626 | break; |
| 2627 | case 4: |
| 2628 | start = tomoyo_builtin_stat; |
| 2629 | head.type = TOMOYO_STAT; |
| 2630 | head.write = tomoyo_write_stat; |
| 2631 | break; |
| 2632 | } |
| 2633 | while (1) { |
| 2634 | char *end = strchr(start, '\n'); |
| 2635 | if (!end) |
| 2636 | break; |
| 2637 | *end = '\0'; |
| 2638 | tomoyo_normalize_line(start); |
| 2639 | head.write_buf = start; |
| 2640 | tomoyo_parse_policy(&head, start); |
| 2641 | start = end + 1; |
| 2642 | } |
| 2643 | } |
| 2644 | tomoyo_read_unlock(idx); |
Tetsuo Handa | 0e4ae0e | 2011-06-26 23:22:59 +0900 | [diff] [blame] | 2645 | #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER |
| 2646 | tomoyo_check_profile(); |
| 2647 | #endif |
Tetsuo Handa | efe836a | 2011-06-26 23:22:18 +0900 | [diff] [blame] | 2648 | } |