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