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