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