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 | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 14 | static struct tomoyo_profile tomoyo_default_profile = { |
| 15 | .learning = &tomoyo_default_profile.preference, |
| 16 | .permissive = &tomoyo_default_profile.preference, |
| 17 | .enforcing = &tomoyo_default_profile.preference, |
| 18 | .preference.enforcing_verbose = true, |
| 19 | .preference.learning_max_entry = 2048, |
| 20 | .preference.learning_verbose = false, |
| 21 | .preference.permissive_verbose = true |
| 22 | }; |
| 23 | |
| 24 | /* Profile version. Currently only 20090903 is defined. */ |
| 25 | static unsigned int tomoyo_profile_version; |
| 26 | |
| 27 | /* Profile table. Memory is allocated as needed. */ |
| 28 | static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES]; |
| 29 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 30 | /* String table for functionality that takes 4 modes. */ |
| 31 | static const char *tomoyo_mode_4[4] = { |
| 32 | "disabled", "learning", "permissive", "enforcing" |
| 33 | }; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 34 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 35 | /* String table for /sys/kernel/security/tomoyo/profile */ |
| 36 | static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX |
| 37 | + TOMOYO_MAX_MAC_CATEGORY_INDEX] = { |
| 38 | [TOMOYO_MAC_FILE_EXECUTE] = "file::execute", |
| 39 | [TOMOYO_MAC_FILE_OPEN] = "file::open", |
| 40 | [TOMOYO_MAC_FILE_CREATE] = "file::create", |
| 41 | [TOMOYO_MAC_FILE_UNLINK] = "file::unlink", |
| 42 | [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir", |
| 43 | [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir", |
| 44 | [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo", |
| 45 | [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock", |
| 46 | [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate", |
| 47 | [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink", |
| 48 | [TOMOYO_MAC_FILE_REWRITE] = "file::rewrite", |
| 49 | [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock", |
| 50 | [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar", |
| 51 | [TOMOYO_MAC_FILE_LINK] = "file::link", |
| 52 | [TOMOYO_MAC_FILE_RENAME] = "file::rename", |
| 53 | [TOMOYO_MAC_FILE_CHMOD] = "file::chmod", |
| 54 | [TOMOYO_MAC_FILE_CHOWN] = "file::chown", |
| 55 | [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp", |
| 56 | [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl", |
| 57 | [TOMOYO_MAC_FILE_CHROOT] = "file::chroot", |
| 58 | [TOMOYO_MAC_FILE_MOUNT] = "file::mount", |
| 59 | [TOMOYO_MAC_FILE_UMOUNT] = "file::umount", |
| 60 | [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root", |
| 61 | [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file", |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 62 | }; |
| 63 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 64 | /* Permit policy management by non-root user? */ |
| 65 | static bool tomoyo_manage_by_non_root; |
| 66 | |
| 67 | /* Utility functions. */ |
| 68 | |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 69 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 70 | * tomoyo_yesno - Return "yes" or "no". |
| 71 | * |
| 72 | * @value: Bool value. |
| 73 | */ |
| 74 | static const char *tomoyo_yesno(const unsigned int value) |
| 75 | { |
| 76 | return value ? "yes" : "no"; |
| 77 | } |
| 78 | |
| 79 | /** |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 80 | * tomoyo_print_name_union - Print a tomoyo_name_union. |
| 81 | * |
| 82 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 83 | * @ptr: Pointer to "struct tomoyo_name_union". |
| 84 | * |
| 85 | * Returns true on success, false otherwise. |
| 86 | */ |
| 87 | static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head, |
| 88 | const struct tomoyo_name_union *ptr) |
| 89 | { |
| 90 | int pos = head->read_avail; |
| 91 | if (pos && head->read_buf[pos - 1] == ' ') |
| 92 | head->read_avail--; |
| 93 | if (ptr->is_group) |
| 94 | return tomoyo_io_printf(head, " @%s", |
| 95 | ptr->group->group_name->name); |
| 96 | return tomoyo_io_printf(head, " %s", ptr->filename->name); |
| 97 | } |
| 98 | |
| 99 | /** |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 100 | * tomoyo_print_number_union - Print a tomoyo_number_union. |
| 101 | * |
| 102 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 103 | * @ptr: Pointer to "struct tomoyo_number_union". |
| 104 | * |
| 105 | * Returns true on success, false otherwise. |
| 106 | */ |
| 107 | bool tomoyo_print_number_union(struct tomoyo_io_buffer *head, |
| 108 | const struct tomoyo_number_union *ptr) |
| 109 | { |
| 110 | unsigned long min; |
| 111 | unsigned long max; |
| 112 | u8 min_type; |
| 113 | u8 max_type; |
| 114 | if (!tomoyo_io_printf(head, " ")) |
| 115 | return false; |
| 116 | if (ptr->is_group) |
| 117 | return tomoyo_io_printf(head, "@%s", |
| 118 | ptr->group->group_name->name); |
| 119 | min_type = ptr->min_type; |
| 120 | max_type = ptr->max_type; |
| 121 | min = ptr->values[0]; |
| 122 | max = ptr->values[1]; |
| 123 | switch (min_type) { |
| 124 | case TOMOYO_VALUE_TYPE_HEXADECIMAL: |
| 125 | if (!tomoyo_io_printf(head, "0x%lX", min)) |
| 126 | return false; |
| 127 | break; |
| 128 | case TOMOYO_VALUE_TYPE_OCTAL: |
| 129 | if (!tomoyo_io_printf(head, "0%lo", min)) |
| 130 | return false; |
| 131 | break; |
| 132 | default: |
| 133 | if (!tomoyo_io_printf(head, "%lu", min)) |
| 134 | return false; |
| 135 | break; |
| 136 | } |
| 137 | if (min == max && min_type == max_type) |
| 138 | return true; |
| 139 | switch (max_type) { |
| 140 | case TOMOYO_VALUE_TYPE_HEXADECIMAL: |
| 141 | return tomoyo_io_printf(head, "-0x%lX", max); |
| 142 | case TOMOYO_VALUE_TYPE_OCTAL: |
| 143 | return tomoyo_io_printf(head, "-0%lo", max); |
| 144 | default: |
| 145 | return tomoyo_io_printf(head, "-%lu", max); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 150 | * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure. |
| 151 | * |
| 152 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 153 | * @fmt: The printf()'s format string, followed by parameters. |
| 154 | * |
| 155 | * Returns true if output was written, false otherwise. |
| 156 | * |
| 157 | * The snprintf() will truncate, but tomoyo_io_printf() won't. |
| 158 | */ |
| 159 | bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) |
| 160 | { |
| 161 | va_list args; |
| 162 | int len; |
| 163 | int pos = head->read_avail; |
| 164 | int size = head->readbuf_size - pos; |
| 165 | |
| 166 | if (size <= 0) |
| 167 | return false; |
| 168 | va_start(args, fmt); |
| 169 | len = vsnprintf(head->read_buf + pos, size, fmt, args); |
| 170 | va_end(args); |
| 171 | if (pos + len >= head->readbuf_size) |
| 172 | return false; |
| 173 | head->read_avail += len; |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 178 | * tomoyo_find_or_assign_new_profile - Create a new profile. |
| 179 | * |
| 180 | * @profile: Profile number to create. |
| 181 | * |
| 182 | * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise. |
| 183 | */ |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 184 | static struct tomoyo_profile *tomoyo_find_or_assign_new_profile |
| 185 | (const unsigned int profile) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 186 | { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 187 | struct tomoyo_profile *ptr; |
| 188 | struct tomoyo_profile *entry; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 189 | if (profile >= TOMOYO_MAX_PROFILES) |
| 190 | return NULL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 191 | ptr = tomoyo_profile_ptr[profile]; |
| 192 | if (ptr) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 193 | return ptr; |
| 194 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
| 195 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 196 | goto out; |
| 197 | ptr = tomoyo_profile_ptr[profile]; |
| 198 | if (!ptr && tomoyo_memory_ok(entry)) { |
| 199 | ptr = entry; |
| 200 | ptr->learning = &tomoyo_default_profile.preference; |
| 201 | ptr->permissive = &tomoyo_default_profile.preference; |
| 202 | ptr->enforcing = &tomoyo_default_profile.preference; |
| 203 | ptr->default_config = TOMOYO_CONFIG_DISABLED; |
| 204 | memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT, |
| 205 | sizeof(ptr->config)); |
| 206 | mb(); /* Avoid out-of-order execution. */ |
| 207 | tomoyo_profile_ptr[profile] = ptr; |
| 208 | entry = NULL; |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 209 | } |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 210 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 211 | out: |
| 212 | kfree(entry); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 213 | return ptr; |
| 214 | } |
| 215 | |
| 216 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 217 | * tomoyo_profile - Find a profile. |
| 218 | * |
| 219 | * @profile: Profile number to find. |
| 220 | * |
| 221 | * Returns pointer to "struct tomoyo_profile". |
| 222 | */ |
| 223 | struct tomoyo_profile *tomoyo_profile(const u8 profile) |
| 224 | { |
| 225 | struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile]; |
| 226 | if (!tomoyo_policy_loaded) |
| 227 | return &tomoyo_default_profile; |
| 228 | BUG_ON(!ptr); |
| 229 | return ptr; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * tomoyo_write_profile - Write profile table. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 234 | * |
| 235 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 236 | * |
| 237 | * Returns 0 on success, negative value otherwise. |
| 238 | */ |
| 239 | static int tomoyo_write_profile(struct tomoyo_io_buffer *head) |
| 240 | { |
| 241 | char *data = head->write_buf; |
| 242 | unsigned int i; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 243 | int value; |
| 244 | int mode; |
| 245 | u8 config; |
| 246 | bool use_default = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 247 | char *cp; |
| 248 | struct tomoyo_profile *profile; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 249 | if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1) |
| 250 | return 0; |
| 251 | i = simple_strtoul(data, &cp, 10); |
| 252 | if (data == cp) { |
| 253 | profile = &tomoyo_default_profile; |
| 254 | } else { |
| 255 | if (*cp != '-') |
| 256 | return -EINVAL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 257 | data = cp + 1; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 258 | profile = tomoyo_find_or_assign_new_profile(i); |
| 259 | if (!profile) |
| 260 | return -EINVAL; |
| 261 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 262 | cp = strchr(data, '='); |
| 263 | if (!cp) |
| 264 | return -EINVAL; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 265 | *cp++ = '\0'; |
| 266 | if (profile != &tomoyo_default_profile) |
| 267 | use_default = strstr(cp, "use_default") != NULL; |
| 268 | if (strstr(cp, "verbose=yes")) |
| 269 | value = 1; |
| 270 | else if (strstr(cp, "verbose=no")) |
| 271 | value = 0; |
| 272 | else |
| 273 | value = -1; |
| 274 | if (!strcmp(data, "PREFERENCE::enforcing")) { |
| 275 | if (use_default) { |
| 276 | profile->enforcing = &tomoyo_default_profile.preference; |
| 277 | return 0; |
| 278 | } |
| 279 | profile->enforcing = &profile->preference; |
| 280 | if (value >= 0) |
| 281 | profile->preference.enforcing_verbose = value; |
| 282 | return 0; |
| 283 | } |
| 284 | if (!strcmp(data, "PREFERENCE::permissive")) { |
| 285 | if (use_default) { |
| 286 | profile->permissive = &tomoyo_default_profile.preference; |
| 287 | return 0; |
| 288 | } |
| 289 | profile->permissive = &profile->preference; |
| 290 | if (value >= 0) |
| 291 | profile->preference.permissive_verbose = value; |
| 292 | return 0; |
| 293 | } |
| 294 | if (!strcmp(data, "PREFERENCE::learning")) { |
| 295 | char *cp2; |
| 296 | if (use_default) { |
| 297 | profile->learning = &tomoyo_default_profile.preference; |
| 298 | return 0; |
| 299 | } |
| 300 | profile->learning = &profile->preference; |
| 301 | if (value >= 0) |
| 302 | profile->preference.learning_verbose = value; |
| 303 | cp2 = strstr(cp, "max_entry="); |
| 304 | if (cp2) |
| 305 | sscanf(cp2 + 10, "%u", |
| 306 | &profile->preference.learning_max_entry); |
| 307 | return 0; |
| 308 | } |
| 309 | if (profile == &tomoyo_default_profile) |
| 310 | return -EINVAL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 311 | if (!strcmp(data, "COMMENT")) { |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 312 | const struct tomoyo_path_info *old_comment = profile->comment; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 313 | profile->comment = tomoyo_get_name(cp); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 314 | tomoyo_put_name(old_comment); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 315 | return 0; |
| 316 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 317 | if (!strcmp(data, "CONFIG")) { |
| 318 | i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; |
| 319 | config = profile->default_config; |
| 320 | } else if (tomoyo_str_starts(&data, "CONFIG::")) { |
| 321 | config = 0; |
| 322 | for (i = 0; i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { |
| 323 | if (strcmp(data, tomoyo_mac_keywords[i])) |
| 324 | continue; |
| 325 | config = profile->config[i]; |
| 326 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 327 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 328 | if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) |
| 329 | return -EINVAL; |
| 330 | } else { |
| 331 | return -EINVAL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 332 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 333 | if (use_default) { |
| 334 | config = TOMOYO_CONFIG_USE_DEFAULT; |
| 335 | } else { |
| 336 | for (mode = 3; mode >= 0; mode--) |
| 337 | if (strstr(cp, tomoyo_mode_4[mode])) |
| 338 | /* |
| 339 | * Update lower 3 bits in order to distinguish |
| 340 | * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'. |
| 341 | */ |
| 342 | config = (config & ~7) | mode; |
| 343 | } |
| 344 | if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) |
| 345 | profile->config[i] = config; |
| 346 | else if (config != TOMOYO_CONFIG_USE_DEFAULT) |
| 347 | profile->default_config = config; |
| 348 | return 0; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 352 | * tomoyo_read_profile - Read profile table. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 353 | * |
| 354 | * @head: Pointer to "struct tomoyo_io_buffer". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 355 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 356 | static void tomoyo_read_profile(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 357 | { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 358 | int index; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 359 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 360 | return; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 361 | if (head->read_bit) |
| 362 | goto body; |
| 363 | tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903"); |
| 364 | tomoyo_io_printf(head, "PREFERENCE::learning={ verbose=%s " |
| 365 | "max_entry=%u }\n", |
| 366 | tomoyo_yesno(tomoyo_default_profile.preference. |
| 367 | learning_verbose), |
| 368 | tomoyo_default_profile.preference.learning_max_entry); |
| 369 | tomoyo_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n", |
| 370 | tomoyo_yesno(tomoyo_default_profile.preference. |
| 371 | permissive_verbose)); |
| 372 | tomoyo_io_printf(head, "PREFERENCE::enforcing={ verbose=%s }\n", |
| 373 | tomoyo_yesno(tomoyo_default_profile.preference. |
| 374 | enforcing_verbose)); |
| 375 | head->read_bit = 1; |
| 376 | body: |
| 377 | for (index = head->read_step; index < TOMOYO_MAX_PROFILES; index++) { |
| 378 | bool done; |
| 379 | u8 config; |
| 380 | int i; |
| 381 | int pos; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 382 | const struct tomoyo_profile *profile |
| 383 | = tomoyo_profile_ptr[index]; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 384 | const struct tomoyo_path_info *comment; |
| 385 | head->read_step = index; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 386 | if (!profile) |
| 387 | continue; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 388 | pos = head->read_avail; |
| 389 | comment = profile->comment; |
| 390 | done = tomoyo_io_printf(head, "%u-COMMENT=%s\n", index, |
| 391 | comment ? comment->name : ""); |
| 392 | if (!done) |
| 393 | goto out; |
| 394 | config = profile->default_config; |
| 395 | if (!tomoyo_io_printf(head, "%u-CONFIG={ mode=%s }\n", index, |
| 396 | tomoyo_mode_4[config & 3])) |
| 397 | goto out; |
| 398 | for (i = 0; i < TOMOYO_MAX_MAC_INDEX + |
| 399 | TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { |
| 400 | config = profile->config[i]; |
| 401 | if (config == TOMOYO_CONFIG_USE_DEFAULT) |
| 402 | continue; |
| 403 | if (!tomoyo_io_printf(head, |
| 404 | "%u-CONFIG::%s={ mode=%s }\n", |
| 405 | index, tomoyo_mac_keywords[i], |
| 406 | tomoyo_mode_4[config & 3])) |
| 407 | goto out; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 408 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 409 | if (profile->learning != &tomoyo_default_profile.preference && |
| 410 | !tomoyo_io_printf(head, "%u-PREFERENCE::learning={ " |
| 411 | "verbose=%s max_entry=%u }\n", index, |
| 412 | tomoyo_yesno(profile->preference. |
| 413 | learning_verbose), |
| 414 | profile->preference.learning_max_entry)) |
| 415 | goto out; |
| 416 | if (profile->permissive != &tomoyo_default_profile.preference |
| 417 | && !tomoyo_io_printf(head, "%u-PREFERENCE::permissive={ " |
| 418 | "verbose=%s }\n", index, |
| 419 | tomoyo_yesno(profile->preference. |
| 420 | permissive_verbose))) |
| 421 | goto out; |
| 422 | if (profile->enforcing != &tomoyo_default_profile.preference && |
| 423 | !tomoyo_io_printf(head, "%u-PREFERENCE::enforcing={ " |
| 424 | "verbose=%s }\n", index, |
| 425 | tomoyo_yesno(profile->preference. |
| 426 | enforcing_verbose))) |
| 427 | goto out; |
| 428 | continue; |
| 429 | out: |
| 430 | head->read_avail = pos; |
| 431 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 432 | } |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 433 | if (index == TOMOYO_MAX_PROFILES) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 434 | head->read_eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 435 | } |
| 436 | |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 437 | static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a, |
| 438 | const struct tomoyo_acl_head *b) |
| 439 | { |
| 440 | return container_of(a, struct tomoyo_policy_manager_entry, head) |
| 441 | ->manager == |
| 442 | container_of(b, struct tomoyo_policy_manager_entry, head) |
| 443 | ->manager; |
| 444 | } |
| 445 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 446 | /** |
| 447 | * tomoyo_update_manager_entry - Add a manager entry. |
| 448 | * |
| 449 | * @manager: The path to manager or the domainnamme. |
| 450 | * @is_delete: True if it is a delete request. |
| 451 | * |
| 452 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 453 | * |
| 454 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 455 | */ |
| 456 | static int tomoyo_update_manager_entry(const char *manager, |
| 457 | const bool is_delete) |
| 458 | { |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 459 | struct tomoyo_policy_manager_entry e = { }; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 460 | int error; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 461 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 462 | if (tomoyo_domain_def(manager)) { |
| 463 | if (!tomoyo_correct_domain(manager)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 464 | return -EINVAL; |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 465 | e.is_domain = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 466 | } else { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 467 | if (!tomoyo_correct_path(manager)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 468 | return -EINVAL; |
| 469 | } |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 470 | e.manager = tomoyo_get_name(manager); |
| 471 | if (!e.manager) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 472 | return -ENOMEM; |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 473 | error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame^] | 474 | &tomoyo_policy_list[TOMOYO_ID_MANAGER], |
Tetsuo Handa | 36f5e1f | 2010-06-15 09:23:26 +0900 | [diff] [blame] | 475 | tomoyo_same_manager_entry); |
Tetsuo Handa | 9e4b50e | 2010-05-06 12:40:02 +0900 | [diff] [blame] | 476 | tomoyo_put_name(e.manager); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 477 | return error; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * tomoyo_write_manager_policy - Write manager policy. |
| 482 | * |
| 483 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 484 | * |
| 485 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 486 | * |
| 487 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 488 | */ |
| 489 | static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head) |
| 490 | { |
| 491 | char *data = head->write_buf; |
| 492 | bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE); |
| 493 | |
| 494 | if (!strcmp(data, "manage_by_non_root")) { |
| 495 | tomoyo_manage_by_non_root = !is_delete; |
| 496 | return 0; |
| 497 | } |
| 498 | return tomoyo_update_manager_entry(data, is_delete); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * tomoyo_read_manager_policy - Read manager policy. |
| 503 | * |
| 504 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 505 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 506 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 507 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 508 | static void tomoyo_read_manager_policy(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 509 | { |
| 510 | struct list_head *pos; |
| 511 | bool done = true; |
| 512 | |
| 513 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 514 | return; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 515 | list_for_each_cookie(pos, head->read_var2, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame^] | 516 | &tomoyo_policy_list[TOMOYO_ID_MANAGER]) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 517 | struct tomoyo_policy_manager_entry *ptr; |
| 518 | ptr = list_entry(pos, struct tomoyo_policy_manager_entry, |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 519 | head.list); |
| 520 | if (ptr->head.is_deleted) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 521 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 522 | done = tomoyo_io_printf(head, "%s\n", ptr->manager->name); |
| 523 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 524 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 525 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 526 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 530 | * tomoyo_policy_manager - Check whether the current process is a policy manager. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 531 | * |
| 532 | * Returns true if the current process is permitted to modify policy |
| 533 | * via /sys/kernel/security/tomoyo/ interface. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 534 | * |
| 535 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 536 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 537 | static bool tomoyo_policy_manager(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 538 | { |
| 539 | struct tomoyo_policy_manager_entry *ptr; |
| 540 | const char *exe; |
| 541 | const struct task_struct *task = current; |
| 542 | const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname; |
| 543 | bool found = false; |
| 544 | |
| 545 | if (!tomoyo_policy_loaded) |
| 546 | return true; |
| 547 | if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid)) |
| 548 | return false; |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame^] | 549 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], |
| 550 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 551 | if (!ptr->head.is_deleted && ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 552 | && !tomoyo_pathcmp(domainname, ptr->manager)) { |
| 553 | found = true; |
| 554 | break; |
| 555 | } |
| 556 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 557 | if (found) |
| 558 | return true; |
| 559 | exe = tomoyo_get_exe(); |
| 560 | if (!exe) |
| 561 | return false; |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame^] | 562 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], |
| 563 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 564 | if (!ptr->head.is_deleted && !ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 565 | && !strcmp(exe, ptr->manager->name)) { |
| 566 | found = true; |
| 567 | break; |
| 568 | } |
| 569 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 570 | if (!found) { /* Reduce error messages. */ |
| 571 | static pid_t last_pid; |
| 572 | const pid_t pid = current->pid; |
| 573 | if (last_pid != pid) { |
| 574 | printk(KERN_WARNING "%s ( %s ) is not permitted to " |
| 575 | "update policies.\n", domainname->name, exe); |
| 576 | last_pid = pid; |
| 577 | } |
| 578 | } |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 579 | kfree(exe); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 580 | return found; |
| 581 | } |
| 582 | |
| 583 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 584 | * tomoyo_select_one - Parse select command. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 585 | * |
| 586 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 587 | * @data: String to parse. |
| 588 | * |
| 589 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 590 | * |
| 591 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 592 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 593 | static bool tomoyo_select_one(struct tomoyo_io_buffer *head, |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 594 | const char *data) |
| 595 | { |
| 596 | unsigned int pid; |
| 597 | struct tomoyo_domain_info *domain = NULL; |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 598 | bool global_pid = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 599 | |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 600 | if (sscanf(data, "pid=%u", &pid) == 1 || |
| 601 | (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 602 | struct task_struct *p; |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 603 | rcu_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 604 | read_lock(&tasklist_lock); |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 605 | if (global_pid) |
| 606 | p = find_task_by_pid_ns(pid, &init_pid_ns); |
| 607 | else |
| 608 | p = find_task_by_vpid(pid); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 609 | if (p) |
| 610 | domain = tomoyo_real_domain(p); |
| 611 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 612 | rcu_read_unlock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 613 | } else if (!strncmp(data, "domain=", 7)) { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 614 | if (tomoyo_domain_def(data + 7)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 615 | domain = tomoyo_find_domain(data + 7); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 616 | } else |
| 617 | return false; |
| 618 | head->write_var1 = domain; |
| 619 | /* Accessing read_buf is safe because head->io_sem is held. */ |
| 620 | if (!head->read_buf) |
| 621 | return true; /* Do nothing if open(O_WRONLY). */ |
| 622 | head->read_avail = 0; |
| 623 | tomoyo_io_printf(head, "# select %s\n", data); |
| 624 | head->read_single_domain = true; |
| 625 | head->read_eof = !domain; |
| 626 | if (domain) { |
| 627 | struct tomoyo_domain_info *d; |
| 628 | head->read_var1 = NULL; |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 629 | list_for_each_entry_rcu(d, &tomoyo_domain_list, list) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 630 | if (d == domain) |
| 631 | break; |
| 632 | head->read_var1 = &d->list; |
| 633 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 634 | head->read_var2 = NULL; |
| 635 | head->read_bit = 0; |
| 636 | head->read_step = 0; |
| 637 | if (domain->is_deleted) |
| 638 | tomoyo_io_printf(head, "# This is a deleted domain.\n"); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | /** |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 644 | * tomoyo_delete_domain - Delete a domain. |
| 645 | * |
| 646 | * @domainname: The name of domain. |
| 647 | * |
| 648 | * Returns 0. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 649 | * |
| 650 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 651 | */ |
| 652 | static int tomoyo_delete_domain(char *domainname) |
| 653 | { |
| 654 | struct tomoyo_domain_info *domain; |
| 655 | struct tomoyo_path_info name; |
| 656 | |
| 657 | name.name = domainname; |
| 658 | tomoyo_fill_path_info(&name); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 659 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 660 | return 0; |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 661 | /* Is there an active domain? */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 662 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 663 | /* Never delete tomoyo_kernel_domain */ |
| 664 | if (domain == &tomoyo_kernel_domain) |
| 665 | continue; |
| 666 | if (domain->is_deleted || |
| 667 | tomoyo_pathcmp(domain->domainname, &name)) |
| 668 | continue; |
| 669 | domain->is_deleted = true; |
| 670 | break; |
| 671 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 672 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 677 | * tomoyo_write_domain_policy2 - Write domain policy. |
| 678 | * |
| 679 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 680 | * |
| 681 | * Returns 0 on success, negative value otherwise. |
| 682 | * |
| 683 | * Caller holds tomoyo_read_lock(). |
| 684 | */ |
| 685 | static int tomoyo_write_domain_policy2(char *data, |
| 686 | struct tomoyo_domain_info *domain, |
| 687 | const bool is_delete) |
| 688 | { |
| 689 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT)) |
| 690 | return tomoyo_write_mount_policy(data, domain, is_delete); |
| 691 | return tomoyo_write_file_policy(data, domain, is_delete); |
| 692 | } |
| 693 | |
| 694 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 695 | * tomoyo_write_domain_policy - Write domain policy. |
| 696 | * |
| 697 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 698 | * |
| 699 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 700 | * |
| 701 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 702 | */ |
| 703 | static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head) |
| 704 | { |
| 705 | char *data = head->write_buf; |
| 706 | struct tomoyo_domain_info *domain = head->write_var1; |
| 707 | bool is_delete = false; |
| 708 | bool is_select = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 709 | unsigned int profile; |
| 710 | |
| 711 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE)) |
| 712 | is_delete = true; |
| 713 | else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT)) |
| 714 | is_select = true; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 715 | if (is_select && tomoyo_select_one(head, data)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 716 | return 0; |
| 717 | /* Don't allow updating policies by non manager programs. */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 718 | if (!tomoyo_policy_manager()) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 719 | return -EPERM; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 720 | if (tomoyo_domain_def(data)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 721 | domain = NULL; |
| 722 | if (is_delete) |
| 723 | tomoyo_delete_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 724 | else if (is_select) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 725 | domain = tomoyo_find_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 726 | else |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 727 | domain = tomoyo_find_or_assign_new_domain(data, 0); |
| 728 | head->write_var1 = domain; |
| 729 | return 0; |
| 730 | } |
| 731 | if (!domain) |
| 732 | return -EINVAL; |
| 733 | |
| 734 | if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1 |
| 735 | && profile < TOMOYO_MAX_PROFILES) { |
| 736 | if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded) |
| 737 | domain->profile = (u8) profile; |
| 738 | return 0; |
| 739 | } |
| 740 | if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) { |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 741 | domain->ignore_global_allow_read = !is_delete; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 742 | return 0; |
| 743 | } |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 744 | if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) { |
| 745 | domain->quota_warned = !is_delete; |
| 746 | return 0; |
| 747 | } |
| 748 | if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) { |
| 749 | domain->transition_failed = !is_delete; |
| 750 | return 0; |
| 751 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 752 | return tomoyo_write_domain_policy2(data, domain, is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 756 | * tomoyo_print_path_acl - Print a single path ACL entry. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 757 | * |
| 758 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 759 | * @ptr: Pointer to "struct tomoyo_path_acl". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 760 | * |
| 761 | * Returns true on success, false otherwise. |
| 762 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 763 | static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head, |
| 764 | struct tomoyo_path_acl *ptr) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 765 | { |
| 766 | int pos; |
| 767 | u8 bit; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 768 | const u16 perm = ptr->perm; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 769 | |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 770 | for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 771 | if (!(perm & (1 << bit))) |
| 772 | continue; |
| 773 | /* Print "read/write" instead of "read" and "write". */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 774 | if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE) |
| 775 | && (perm & (1 << TOMOYO_TYPE_READ_WRITE))) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 776 | continue; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 777 | pos = head->read_avail; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 778 | if (!tomoyo_io_printf(head, "allow_%s ", |
Tetsuo Handa | 71c2823 | 2010-06-16 16:26:38 +0900 | [diff] [blame] | 779 | tomoyo_path_keyword[bit]) || |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 780 | !tomoyo_print_name_union(head, &ptr->name) || |
| 781 | !tomoyo_io_printf(head, "\n")) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 782 | goto out; |
| 783 | } |
| 784 | head->read_bit = 0; |
| 785 | return true; |
| 786 | out: |
| 787 | head->read_bit = bit; |
| 788 | head->read_avail = pos; |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | /** |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 793 | * tomoyo_print_path2_acl - Print a double path ACL entry. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 794 | * |
| 795 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 796 | * @ptr: Pointer to "struct tomoyo_path2_acl". |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 797 | * |
| 798 | * Returns true on success, false otherwise. |
| 799 | */ |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 800 | static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head, |
| 801 | struct tomoyo_path2_acl *ptr) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 802 | { |
| 803 | int pos; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 804 | const u8 perm = ptr->perm; |
| 805 | u8 bit; |
| 806 | |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 807 | for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 808 | if (!(perm & (1 << bit))) |
| 809 | continue; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 810 | pos = head->read_avail; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 811 | if (!tomoyo_io_printf(head, "allow_%s ", |
Tetsuo Handa | 71c2823 | 2010-06-16 16:26:38 +0900 | [diff] [blame] | 812 | tomoyo_path2_keyword[bit]) || |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 813 | !tomoyo_print_name_union(head, &ptr->name1) || |
| 814 | !tomoyo_print_name_union(head, &ptr->name2) || |
| 815 | !tomoyo_io_printf(head, "\n")) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 816 | goto out; |
| 817 | } |
| 818 | head->read_bit = 0; |
| 819 | return true; |
| 820 | out: |
| 821 | head->read_bit = bit; |
| 822 | head->read_avail = pos; |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | /** |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 827 | * tomoyo_print_path_number_acl - Print a path_number ACL entry. |
| 828 | * |
| 829 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 830 | * @ptr: Pointer to "struct tomoyo_path_number_acl". |
| 831 | * |
| 832 | * Returns true on success, false otherwise. |
| 833 | */ |
| 834 | static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head, |
| 835 | struct tomoyo_path_number_acl *ptr) |
| 836 | { |
| 837 | int pos; |
| 838 | u8 bit; |
| 839 | const u8 perm = ptr->perm; |
| 840 | for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; |
| 841 | bit++) { |
| 842 | if (!(perm & (1 << bit))) |
| 843 | continue; |
| 844 | pos = head->read_avail; |
| 845 | if (!tomoyo_io_printf(head, "allow_%s", |
Tetsuo Handa | 71c2823 | 2010-06-16 16:26:38 +0900 | [diff] [blame] | 846 | tomoyo_path_number_keyword[bit]) || |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 847 | !tomoyo_print_name_union(head, &ptr->name) || |
| 848 | !tomoyo_print_number_union(head, &ptr->number) || |
| 849 | !tomoyo_io_printf(head, "\n")) |
| 850 | goto out; |
| 851 | } |
| 852 | head->read_bit = 0; |
| 853 | return true; |
| 854 | out: |
| 855 | head->read_bit = bit; |
| 856 | head->read_avail = pos; |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 861 | * tomoyo_print_mkdev_acl - Print a mkdev ACL entry. |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 862 | * |
| 863 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 864 | * @ptr: Pointer to "struct tomoyo_mkdev_acl". |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 865 | * |
| 866 | * Returns true on success, false otherwise. |
| 867 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 868 | static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer *head, |
| 869 | struct tomoyo_mkdev_acl *ptr) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 870 | { |
| 871 | int pos; |
| 872 | u8 bit; |
| 873 | const u16 perm = ptr->perm; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 874 | for (bit = head->read_bit; bit < TOMOYO_MAX_MKDEV_OPERATION; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 875 | bit++) { |
| 876 | if (!(perm & (1 << bit))) |
| 877 | continue; |
| 878 | pos = head->read_avail; |
| 879 | if (!tomoyo_io_printf(head, "allow_%s", |
Tetsuo Handa | 71c2823 | 2010-06-16 16:26:38 +0900 | [diff] [blame] | 880 | tomoyo_mkdev_keyword[bit]) || |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 881 | !tomoyo_print_name_union(head, &ptr->name) || |
| 882 | !tomoyo_print_number_union(head, &ptr->mode) || |
| 883 | !tomoyo_print_number_union(head, &ptr->major) || |
| 884 | !tomoyo_print_number_union(head, &ptr->minor) || |
| 885 | !tomoyo_io_printf(head, "\n")) |
| 886 | goto out; |
| 887 | } |
| 888 | head->read_bit = 0; |
| 889 | return true; |
| 890 | out: |
| 891 | head->read_bit = bit; |
| 892 | head->read_avail = pos; |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 897 | * tomoyo_print_mount_acl - Print a mount ACL entry. |
| 898 | * |
| 899 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 900 | * @ptr: Pointer to "struct tomoyo_mount_acl". |
| 901 | * |
| 902 | * Returns true on success, false otherwise. |
| 903 | */ |
| 904 | static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head, |
| 905 | struct tomoyo_mount_acl *ptr) |
| 906 | { |
| 907 | const int pos = head->read_avail; |
| 908 | if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) || |
| 909 | !tomoyo_print_name_union(head, &ptr->dev_name) || |
| 910 | !tomoyo_print_name_union(head, &ptr->dir_name) || |
| 911 | !tomoyo_print_name_union(head, &ptr->fs_type) || |
| 912 | !tomoyo_print_number_union(head, &ptr->flags) || |
| 913 | !tomoyo_io_printf(head, "\n")) { |
| 914 | head->read_avail = pos; |
| 915 | return false; |
| 916 | } |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 921 | * tomoyo_print_entry - Print an ACL entry. |
| 922 | * |
| 923 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 924 | * @ptr: Pointer to an ACL entry. |
| 925 | * |
| 926 | * Returns true on success, false otherwise. |
| 927 | */ |
| 928 | static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, |
| 929 | struct tomoyo_acl_info *ptr) |
| 930 | { |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 931 | const u8 acl_type = ptr->type; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 932 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 933 | if (ptr->is_deleted) |
| 934 | return true; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 935 | if (acl_type == TOMOYO_TYPE_PATH_ACL) { |
| 936 | struct tomoyo_path_acl *acl |
| 937 | = container_of(ptr, struct tomoyo_path_acl, head); |
| 938 | return tomoyo_print_path_acl(head, acl); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 939 | } |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 940 | if (acl_type == TOMOYO_TYPE_PATH2_ACL) { |
| 941 | struct tomoyo_path2_acl *acl |
| 942 | = container_of(ptr, struct tomoyo_path2_acl, head); |
| 943 | return tomoyo_print_path2_acl(head, acl); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 944 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 945 | if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) { |
| 946 | struct tomoyo_path_number_acl *acl |
| 947 | = container_of(ptr, struct tomoyo_path_number_acl, |
| 948 | head); |
| 949 | return tomoyo_print_path_number_acl(head, acl); |
| 950 | } |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 951 | if (acl_type == TOMOYO_TYPE_MKDEV_ACL) { |
| 952 | struct tomoyo_mkdev_acl *acl |
| 953 | = container_of(ptr, struct tomoyo_mkdev_acl, |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 954 | head); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 955 | return tomoyo_print_mkdev_acl(head, acl); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 956 | } |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 957 | if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { |
| 958 | struct tomoyo_mount_acl *acl |
| 959 | = container_of(ptr, struct tomoyo_mount_acl, head); |
| 960 | return tomoyo_print_mount_acl(head, acl); |
| 961 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 962 | BUG(); /* This must not happen. */ |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * tomoyo_read_domain_policy - Read domain policy. |
| 968 | * |
| 969 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 970 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 971 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 972 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 973 | static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 974 | { |
| 975 | struct list_head *dpos; |
| 976 | struct list_head *apos; |
| 977 | bool done = true; |
| 978 | |
| 979 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 980 | return; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 981 | if (head->read_step == 0) |
| 982 | head->read_step = 1; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 983 | list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) { |
| 984 | struct tomoyo_domain_info *domain; |
| 985 | const char *quota_exceeded = ""; |
| 986 | const char *transition_failed = ""; |
| 987 | const char *ignore_global_allow_read = ""; |
| 988 | domain = list_entry(dpos, struct tomoyo_domain_info, list); |
| 989 | if (head->read_step != 1) |
| 990 | goto acl_loop; |
| 991 | if (domain->is_deleted && !head->read_single_domain) |
| 992 | continue; |
| 993 | /* Print domainname and flags. */ |
| 994 | if (domain->quota_warned) |
| 995 | quota_exceeded = "quota_exceeded\n"; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 996 | if (domain->transition_failed) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 997 | transition_failed = "transition_failed\n"; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 998 | if (domain->ignore_global_allow_read) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 999 | ignore_global_allow_read |
| 1000 | = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n"; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 1001 | done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE |
| 1002 | "%u\n%s%s%s\n", |
| 1003 | domain->domainname->name, |
| 1004 | domain->profile, quota_exceeded, |
| 1005 | transition_failed, |
| 1006 | ignore_global_allow_read); |
| 1007 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1008 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1009 | head->read_step = 2; |
| 1010 | acl_loop: |
| 1011 | if (head->read_step == 3) |
| 1012 | goto tail_mark; |
| 1013 | /* Print ACL entries in the domain. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1014 | list_for_each_cookie(apos, head->read_var2, |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 1015 | &domain->acl_info_list) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1016 | struct tomoyo_acl_info *ptr |
| 1017 | = list_entry(apos, struct tomoyo_acl_info, |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 1018 | list); |
| 1019 | done = tomoyo_print_entry(head, ptr); |
| 1020 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1021 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1022 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1023 | if (!done) |
| 1024 | break; |
| 1025 | head->read_step = 3; |
| 1026 | tail_mark: |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 1027 | done = tomoyo_io_printf(head, "\n"); |
| 1028 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1029 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1030 | head->read_step = 1; |
| 1031 | if (head->read_single_domain) |
| 1032 | break; |
| 1033 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1034 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * tomoyo_write_domain_profile - Assign profile for specified domain. |
| 1039 | * |
| 1040 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1041 | * |
| 1042 | * Returns 0 on success, -EINVAL otherwise. |
| 1043 | * |
| 1044 | * This is equivalent to doing |
| 1045 | * |
| 1046 | * ( echo "select " $domainname; echo "use_profile " $profile ) | |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 1047 | * /usr/sbin/tomoyo-loadpolicy -d |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1048 | * |
| 1049 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1050 | */ |
| 1051 | static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head) |
| 1052 | { |
| 1053 | char *data = head->write_buf; |
| 1054 | char *cp = strchr(data, ' '); |
| 1055 | struct tomoyo_domain_info *domain; |
| 1056 | unsigned long profile; |
| 1057 | |
| 1058 | if (!cp) |
| 1059 | return -EINVAL; |
| 1060 | *cp = '\0'; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1061 | domain = tomoyo_find_domain(cp + 1); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1062 | if (strict_strtoul(data, 10, &profile)) |
| 1063 | return -EINVAL; |
| 1064 | if (domain && profile < TOMOYO_MAX_PROFILES |
| 1065 | && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)) |
| 1066 | domain->profile = (u8) profile; |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * tomoyo_read_domain_profile - Read only domainname and profile. |
| 1072 | * |
| 1073 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1074 | * |
| 1075 | * Returns list of profile number and domainname pairs. |
| 1076 | * |
| 1077 | * This is equivalent to doing |
| 1078 | * |
| 1079 | * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy | |
| 1080 | * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" ) |
| 1081 | * domainname = $0; } else if ( $1 == "use_profile" ) { |
| 1082 | * print $2 " " domainname; domainname = ""; } } ; ' |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1083 | * |
| 1084 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1085 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1086 | static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1087 | { |
| 1088 | struct list_head *pos; |
| 1089 | bool done = true; |
| 1090 | |
| 1091 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1092 | return; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1093 | list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) { |
| 1094 | struct tomoyo_domain_info *domain; |
| 1095 | domain = list_entry(pos, struct tomoyo_domain_info, list); |
| 1096 | if (domain->is_deleted) |
| 1097 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 1098 | done = tomoyo_io_printf(head, "%u %s\n", domain->profile, |
| 1099 | domain->domainname->name); |
| 1100 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1101 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1102 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1103 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * tomoyo_write_pid: Specify PID to obtain domainname. |
| 1108 | * |
| 1109 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1110 | * |
| 1111 | * Returns 0. |
| 1112 | */ |
| 1113 | static int tomoyo_write_pid(struct tomoyo_io_buffer *head) |
| 1114 | { |
| 1115 | unsigned long pid; |
| 1116 | /* No error check. */ |
| 1117 | strict_strtoul(head->write_buf, 10, &pid); |
| 1118 | head->read_step = (int) pid; |
| 1119 | head->read_eof = false; |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * tomoyo_read_pid - Get domainname of the specified PID. |
| 1125 | * |
| 1126 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1127 | * |
| 1128 | * Returns the domainname which the specified PID is in on success, |
| 1129 | * empty string otherwise. |
| 1130 | * The PID is specified by tomoyo_write_pid() so that the user can obtain |
| 1131 | * using read()/write() interface rather than sysctl() interface. |
| 1132 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1133 | static void tomoyo_read_pid(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1134 | { |
| 1135 | if (head->read_avail == 0 && !head->read_eof) { |
| 1136 | const int pid = head->read_step; |
| 1137 | struct task_struct *p; |
| 1138 | struct tomoyo_domain_info *domain = NULL; |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 1139 | rcu_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1140 | read_lock(&tasklist_lock); |
| 1141 | p = find_task_by_vpid(pid); |
| 1142 | if (p) |
| 1143 | domain = tomoyo_real_domain(p); |
| 1144 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 1145 | rcu_read_unlock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1146 | if (domain) |
| 1147 | tomoyo_io_printf(head, "%d %u %s", pid, domain->profile, |
| 1148 | domain->domainname->name); |
| 1149 | head->read_eof = true; |
| 1150 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * tomoyo_write_exception_policy - Write exception policy. |
| 1155 | * |
| 1156 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1157 | * |
| 1158 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1159 | * |
| 1160 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1161 | */ |
| 1162 | static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head) |
| 1163 | { |
| 1164 | char *data = head->write_buf; |
| 1165 | bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE); |
| 1166 | |
| 1167 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN)) |
| 1168 | return tomoyo_write_domain_keeper_policy(data, false, |
| 1169 | is_delete); |
| 1170 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN)) |
| 1171 | return tomoyo_write_domain_keeper_policy(data, true, is_delete); |
| 1172 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN)) |
| 1173 | return tomoyo_write_domain_initializer_policy(data, false, |
| 1174 | is_delete); |
| 1175 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN)) |
| 1176 | return tomoyo_write_domain_initializer_policy(data, true, |
| 1177 | is_delete); |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 1178 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR)) |
| 1179 | return tomoyo_write_aggregator_policy(data, is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1180 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS)) |
| 1181 | return tomoyo_write_alias_policy(data, is_delete); |
| 1182 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ)) |
| 1183 | return tomoyo_write_globally_readable_policy(data, is_delete); |
| 1184 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN)) |
| 1185 | return tomoyo_write_pattern_policy(data, is_delete); |
| 1186 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE)) |
| 1187 | return tomoyo_write_no_rewrite_policy(data, is_delete); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 1188 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP)) |
| 1189 | return tomoyo_write_path_group_policy(data, is_delete); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 1190 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP)) |
| 1191 | return tomoyo_write_number_group_policy(data, is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * tomoyo_read_exception_policy - Read exception policy. |
| 1197 | * |
| 1198 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1199 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1200 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1201 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1202 | static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1203 | { |
| 1204 | if (!head->read_eof) { |
| 1205 | switch (head->read_step) { |
| 1206 | case 0: |
| 1207 | head->read_var2 = NULL; |
| 1208 | head->read_step = 1; |
| 1209 | case 1: |
| 1210 | if (!tomoyo_read_domain_keeper_policy(head)) |
| 1211 | break; |
| 1212 | head->read_var2 = NULL; |
| 1213 | head->read_step = 2; |
| 1214 | case 2: |
| 1215 | if (!tomoyo_read_globally_readable_policy(head)) |
| 1216 | break; |
| 1217 | head->read_var2 = NULL; |
| 1218 | head->read_step = 3; |
| 1219 | case 3: |
| 1220 | head->read_var2 = NULL; |
| 1221 | head->read_step = 4; |
| 1222 | case 4: |
| 1223 | if (!tomoyo_read_domain_initializer_policy(head)) |
| 1224 | break; |
| 1225 | head->read_var2 = NULL; |
| 1226 | head->read_step = 5; |
| 1227 | case 5: |
| 1228 | if (!tomoyo_read_alias_policy(head)) |
| 1229 | break; |
| 1230 | head->read_var2 = NULL; |
| 1231 | head->read_step = 6; |
| 1232 | case 6: |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 1233 | if (!tomoyo_read_aggregator_policy(head)) |
| 1234 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1235 | head->read_var2 = NULL; |
| 1236 | head->read_step = 7; |
| 1237 | case 7: |
| 1238 | if (!tomoyo_read_file_pattern(head)) |
| 1239 | break; |
| 1240 | head->read_var2 = NULL; |
| 1241 | head->read_step = 8; |
| 1242 | case 8: |
| 1243 | if (!tomoyo_read_no_rewrite_policy(head)) |
| 1244 | break; |
| 1245 | head->read_var2 = NULL; |
| 1246 | head->read_step = 9; |
| 1247 | case 9: |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 1248 | if (!tomoyo_read_path_group_policy(head)) |
| 1249 | break; |
| 1250 | head->read_var1 = NULL; |
| 1251 | head->read_var2 = NULL; |
| 1252 | head->read_step = 10; |
| 1253 | case 10: |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 1254 | if (!tomoyo_read_number_group_policy(head)) |
| 1255 | break; |
| 1256 | head->read_var1 = NULL; |
| 1257 | head->read_var2 = NULL; |
| 1258 | head->read_step = 11; |
| 1259 | case 11: |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1260 | head->read_eof = true; |
| 1261 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1262 | } |
| 1263 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1264 | } |
| 1265 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1266 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1267 | * tomoyo_print_header - Get header line of audit log. |
| 1268 | * |
| 1269 | * @r: Pointer to "struct tomoyo_request_info". |
| 1270 | * |
| 1271 | * Returns string representation. |
| 1272 | * |
| 1273 | * This function uses kmalloc(), so caller must kfree() if this function |
| 1274 | * didn't return NULL. |
| 1275 | */ |
| 1276 | static char *tomoyo_print_header(struct tomoyo_request_info *r) |
| 1277 | { |
| 1278 | static const char *tomoyo_mode_4[4] = { |
| 1279 | "disabled", "learning", "permissive", "enforcing" |
| 1280 | }; |
| 1281 | struct timeval tv; |
| 1282 | const pid_t gpid = task_pid_nr(current); |
| 1283 | static const int tomoyo_buffer_len = 4096; |
| 1284 | char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS); |
| 1285 | if (!buffer) |
| 1286 | return NULL; |
| 1287 | do_gettimeofday(&tv); |
| 1288 | snprintf(buffer, tomoyo_buffer_len - 1, |
| 1289 | "#timestamp=%lu profile=%u mode=%s (global-pid=%u)" |
| 1290 | " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u" |
| 1291 | " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }", |
| 1292 | tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid, |
| 1293 | (pid_t) sys_getpid(), (pid_t) sys_getppid(), |
| 1294 | current_uid(), current_gid(), current_euid(), |
| 1295 | current_egid(), current_suid(), current_sgid(), |
| 1296 | current_fsuid(), current_fsgid()); |
| 1297 | return buffer; |
| 1298 | } |
| 1299 | |
| 1300 | /** |
| 1301 | * tomoyo_init_audit_log - Allocate buffer for audit logs. |
| 1302 | * |
| 1303 | * @len: Required size. |
| 1304 | * @r: Pointer to "struct tomoyo_request_info". |
| 1305 | * |
| 1306 | * Returns pointer to allocated memory. |
| 1307 | * |
| 1308 | * The @len is updated to add the header lines' size on success. |
| 1309 | * |
| 1310 | * This function uses kzalloc(), so caller must kfree() if this function |
| 1311 | * didn't return NULL. |
| 1312 | */ |
| 1313 | static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r) |
| 1314 | { |
| 1315 | char *buf = NULL; |
| 1316 | const char *header; |
| 1317 | const char *domainname; |
| 1318 | if (!r->domain) |
| 1319 | r->domain = tomoyo_domain(); |
| 1320 | domainname = r->domain->domainname->name; |
| 1321 | header = tomoyo_print_header(r); |
| 1322 | if (!header) |
| 1323 | return NULL; |
| 1324 | *len += strlen(domainname) + strlen(header) + 10; |
| 1325 | buf = kzalloc(*len, GFP_NOFS); |
| 1326 | if (buf) |
| 1327 | snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname); |
| 1328 | kfree(header); |
| 1329 | return buf; |
| 1330 | } |
| 1331 | |
| 1332 | /* Wait queue for tomoyo_query_list. */ |
| 1333 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait); |
| 1334 | |
| 1335 | /* Lock for manipulating tomoyo_query_list. */ |
| 1336 | static DEFINE_SPINLOCK(tomoyo_query_list_lock); |
| 1337 | |
| 1338 | /* Structure for query. */ |
| 1339 | struct tomoyo_query_entry { |
| 1340 | struct list_head list; |
| 1341 | char *query; |
| 1342 | int query_len; |
| 1343 | unsigned int serial; |
| 1344 | int timer; |
| 1345 | int answer; |
| 1346 | }; |
| 1347 | |
| 1348 | /* The list for "struct tomoyo_query_entry". */ |
| 1349 | static LIST_HEAD(tomoyo_query_list); |
| 1350 | |
| 1351 | /* |
| 1352 | * Number of "struct file" referring /sys/kernel/security/tomoyo/query |
| 1353 | * interface. |
| 1354 | */ |
| 1355 | static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); |
| 1356 | |
| 1357 | /** |
| 1358 | * tomoyo_supervisor - Ask for the supervisor's decision. |
| 1359 | * |
| 1360 | * @r: Pointer to "struct tomoyo_request_info". |
| 1361 | * @fmt: The printf()'s format string, followed by parameters. |
| 1362 | * |
| 1363 | * Returns 0 if the supervisor decided to permit the access request which |
| 1364 | * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the |
| 1365 | * supervisor decided to retry the access request which violated the policy in |
| 1366 | * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise. |
| 1367 | */ |
| 1368 | int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) |
| 1369 | { |
| 1370 | va_list args; |
| 1371 | int error = -EPERM; |
| 1372 | int pos; |
| 1373 | int len; |
| 1374 | static unsigned int tomoyo_serial; |
| 1375 | struct tomoyo_query_entry *tomoyo_query_entry = NULL; |
| 1376 | bool quota_exceeded = false; |
| 1377 | char *header; |
| 1378 | switch (r->mode) { |
| 1379 | char *buffer; |
| 1380 | case TOMOYO_CONFIG_LEARNING: |
| 1381 | if (!tomoyo_domain_quota_is_ok(r)) |
| 1382 | return 0; |
| 1383 | va_start(args, fmt); |
| 1384 | len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4; |
| 1385 | va_end(args); |
| 1386 | buffer = kmalloc(len, GFP_NOFS); |
| 1387 | if (!buffer) |
| 1388 | return 0; |
| 1389 | va_start(args, fmt); |
| 1390 | vsnprintf(buffer, len - 1, fmt, args); |
| 1391 | va_end(args); |
| 1392 | tomoyo_normalize_line(buffer); |
| 1393 | tomoyo_write_domain_policy2(buffer, r->domain, false); |
| 1394 | kfree(buffer); |
| 1395 | /* fall through */ |
| 1396 | case TOMOYO_CONFIG_PERMISSIVE: |
| 1397 | return 0; |
| 1398 | } |
| 1399 | if (!r->domain) |
| 1400 | r->domain = tomoyo_domain(); |
| 1401 | if (!atomic_read(&tomoyo_query_observers)) |
| 1402 | return -EPERM; |
| 1403 | va_start(args, fmt); |
| 1404 | len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32; |
| 1405 | va_end(args); |
| 1406 | header = tomoyo_init_audit_log(&len, r); |
| 1407 | if (!header) |
| 1408 | goto out; |
| 1409 | tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS); |
| 1410 | if (!tomoyo_query_entry) |
| 1411 | goto out; |
| 1412 | tomoyo_query_entry->query = kzalloc(len, GFP_NOFS); |
| 1413 | if (!tomoyo_query_entry->query) |
| 1414 | goto out; |
| 1415 | len = ksize(tomoyo_query_entry->query); |
| 1416 | INIT_LIST_HEAD(&tomoyo_query_entry->list); |
| 1417 | spin_lock(&tomoyo_query_list_lock); |
| 1418 | if (tomoyo_quota_for_query && tomoyo_query_memory_size + len + |
| 1419 | sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) { |
| 1420 | quota_exceeded = true; |
| 1421 | } else { |
| 1422 | tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry); |
| 1423 | tomoyo_query_entry->serial = tomoyo_serial++; |
| 1424 | } |
| 1425 | spin_unlock(&tomoyo_query_list_lock); |
| 1426 | if (quota_exceeded) |
| 1427 | goto out; |
| 1428 | pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s", |
| 1429 | tomoyo_query_entry->serial, r->retry, header); |
| 1430 | kfree(header); |
| 1431 | header = NULL; |
| 1432 | va_start(args, fmt); |
| 1433 | vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args); |
| 1434 | tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1; |
| 1435 | va_end(args); |
| 1436 | spin_lock(&tomoyo_query_list_lock); |
| 1437 | list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list); |
| 1438 | spin_unlock(&tomoyo_query_list_lock); |
| 1439 | /* Give 10 seconds for supervisor's opinion. */ |
| 1440 | for (tomoyo_query_entry->timer = 0; |
| 1441 | atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100; |
| 1442 | tomoyo_query_entry->timer++) { |
| 1443 | wake_up(&tomoyo_query_wait); |
| 1444 | set_current_state(TASK_INTERRUPTIBLE); |
| 1445 | schedule_timeout(HZ / 10); |
| 1446 | if (tomoyo_query_entry->answer) |
| 1447 | break; |
| 1448 | } |
| 1449 | spin_lock(&tomoyo_query_list_lock); |
| 1450 | list_del(&tomoyo_query_entry->list); |
| 1451 | tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry); |
| 1452 | spin_unlock(&tomoyo_query_list_lock); |
| 1453 | switch (tomoyo_query_entry->answer) { |
| 1454 | case 3: /* Asked to retry by administrator. */ |
| 1455 | error = TOMOYO_RETRY_REQUEST; |
| 1456 | r->retry++; |
| 1457 | break; |
| 1458 | case 1: |
| 1459 | /* Granted by administrator. */ |
| 1460 | error = 0; |
| 1461 | break; |
| 1462 | case 0: |
| 1463 | /* Timed out. */ |
| 1464 | break; |
| 1465 | default: |
| 1466 | /* Rejected by administrator. */ |
| 1467 | break; |
| 1468 | } |
| 1469 | out: |
| 1470 | if (tomoyo_query_entry) |
| 1471 | kfree(tomoyo_query_entry->query); |
| 1472 | kfree(tomoyo_query_entry); |
| 1473 | kfree(header); |
| 1474 | return error; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query. |
| 1479 | * |
| 1480 | * @file: Pointer to "struct file". |
| 1481 | * @wait: Pointer to "poll_table". |
| 1482 | * |
| 1483 | * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. |
| 1484 | * |
| 1485 | * Waits for access requests which violated policy in enforcing mode. |
| 1486 | */ |
| 1487 | static int tomoyo_poll_query(struct file *file, poll_table *wait) |
| 1488 | { |
| 1489 | struct list_head *tmp; |
| 1490 | bool found = false; |
| 1491 | u8 i; |
| 1492 | for (i = 0; i < 2; i++) { |
| 1493 | spin_lock(&tomoyo_query_list_lock); |
| 1494 | list_for_each(tmp, &tomoyo_query_list) { |
| 1495 | struct tomoyo_query_entry *ptr |
| 1496 | = list_entry(tmp, struct tomoyo_query_entry, |
| 1497 | list); |
| 1498 | if (ptr->answer) |
| 1499 | continue; |
| 1500 | found = true; |
| 1501 | break; |
| 1502 | } |
| 1503 | spin_unlock(&tomoyo_query_list_lock); |
| 1504 | if (found) |
| 1505 | return POLLIN | POLLRDNORM; |
| 1506 | if (i) |
| 1507 | break; |
| 1508 | poll_wait(file, &tomoyo_query_wait, wait); |
| 1509 | } |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * tomoyo_read_query - Read access requests which violated policy in enforcing mode. |
| 1515 | * |
| 1516 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1517 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1518 | static void tomoyo_read_query(struct tomoyo_io_buffer *head) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1519 | { |
| 1520 | struct list_head *tmp; |
| 1521 | int pos = 0; |
| 1522 | int len = 0; |
| 1523 | char *buf; |
| 1524 | if (head->read_avail) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1525 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1526 | if (head->read_buf) { |
| 1527 | kfree(head->read_buf); |
| 1528 | head->read_buf = NULL; |
| 1529 | head->readbuf_size = 0; |
| 1530 | } |
| 1531 | spin_lock(&tomoyo_query_list_lock); |
| 1532 | list_for_each(tmp, &tomoyo_query_list) { |
| 1533 | struct tomoyo_query_entry *ptr |
| 1534 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1535 | if (ptr->answer) |
| 1536 | continue; |
| 1537 | if (pos++ != head->read_step) |
| 1538 | continue; |
| 1539 | len = ptr->query_len; |
| 1540 | break; |
| 1541 | } |
| 1542 | spin_unlock(&tomoyo_query_list_lock); |
| 1543 | if (!len) { |
| 1544 | head->read_step = 0; |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1545 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1546 | } |
| 1547 | buf = kzalloc(len, GFP_NOFS); |
| 1548 | if (!buf) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1549 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1550 | pos = 0; |
| 1551 | spin_lock(&tomoyo_query_list_lock); |
| 1552 | list_for_each(tmp, &tomoyo_query_list) { |
| 1553 | struct tomoyo_query_entry *ptr |
| 1554 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1555 | if (ptr->answer) |
| 1556 | continue; |
| 1557 | if (pos++ != head->read_step) |
| 1558 | continue; |
| 1559 | /* |
| 1560 | * Some query can be skipped because tomoyo_query_list |
| 1561 | * can change, but I don't care. |
| 1562 | */ |
| 1563 | if (len == ptr->query_len) |
| 1564 | memmove(buf, ptr->query, len); |
| 1565 | break; |
| 1566 | } |
| 1567 | spin_unlock(&tomoyo_query_list_lock); |
| 1568 | if (buf[0]) { |
| 1569 | head->read_avail = len; |
| 1570 | head->readbuf_size = head->read_avail; |
| 1571 | head->read_buf = buf; |
| 1572 | head->read_step++; |
| 1573 | } else { |
| 1574 | kfree(buf); |
| 1575 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * tomoyo_write_answer - Write the supervisor's decision. |
| 1580 | * |
| 1581 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1582 | * |
| 1583 | * Returns 0 on success, -EINVAL otherwise. |
| 1584 | */ |
| 1585 | static int tomoyo_write_answer(struct tomoyo_io_buffer *head) |
| 1586 | { |
| 1587 | char *data = head->write_buf; |
| 1588 | struct list_head *tmp; |
| 1589 | unsigned int serial; |
| 1590 | unsigned int answer; |
| 1591 | spin_lock(&tomoyo_query_list_lock); |
| 1592 | list_for_each(tmp, &tomoyo_query_list) { |
| 1593 | struct tomoyo_query_entry *ptr |
| 1594 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1595 | ptr->timer = 0; |
| 1596 | } |
| 1597 | spin_unlock(&tomoyo_query_list_lock); |
| 1598 | if (sscanf(data, "A%u=%u", &serial, &answer) != 2) |
| 1599 | return -EINVAL; |
| 1600 | spin_lock(&tomoyo_query_list_lock); |
| 1601 | list_for_each(tmp, &tomoyo_query_list) { |
| 1602 | struct tomoyo_query_entry *ptr |
| 1603 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1604 | if (ptr->serial != serial) |
| 1605 | continue; |
| 1606 | if (!ptr->answer) |
| 1607 | ptr->answer = answer; |
| 1608 | break; |
| 1609 | } |
| 1610 | spin_unlock(&tomoyo_query_list_lock); |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1615 | * tomoyo_read_version: Get version. |
| 1616 | * |
| 1617 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1618 | * |
| 1619 | * Returns version information. |
| 1620 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1621 | static void tomoyo_read_version(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1622 | { |
| 1623 | if (!head->read_eof) { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1624 | tomoyo_io_printf(head, "2.3.0-pre"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1625 | head->read_eof = true; |
| 1626 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * tomoyo_read_self_domain - Get the current process's domainname. |
| 1631 | * |
| 1632 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1633 | * |
| 1634 | * Returns the current process's domainname. |
| 1635 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1636 | static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1637 | { |
| 1638 | if (!head->read_eof) { |
| 1639 | /* |
| 1640 | * tomoyo_domain()->domainname != NULL |
| 1641 | * because every process belongs to a domain and |
| 1642 | * the domain's name cannot be NULL. |
| 1643 | */ |
| 1644 | tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name); |
| 1645 | head->read_eof = true; |
| 1646 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface. |
| 1651 | * |
| 1652 | * @type: Type of interface. |
| 1653 | * @file: Pointer to "struct file". |
| 1654 | * |
| 1655 | * Associates policy handler and returns 0 on success, -ENOMEM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1656 | * |
| 1657 | * Caller acquires tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1658 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1659 | int tomoyo_open_control(const u8 type, struct file *file) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1660 | { |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1661 | struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1662 | |
| 1663 | if (!head) |
| 1664 | return -ENOMEM; |
| 1665 | mutex_init(&head->io_sem); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1666 | head->type = type; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1667 | switch (type) { |
| 1668 | case TOMOYO_DOMAINPOLICY: |
| 1669 | /* /sys/kernel/security/tomoyo/domain_policy */ |
| 1670 | head->write = tomoyo_write_domain_policy; |
| 1671 | head->read = tomoyo_read_domain_policy; |
| 1672 | break; |
| 1673 | case TOMOYO_EXCEPTIONPOLICY: |
| 1674 | /* /sys/kernel/security/tomoyo/exception_policy */ |
| 1675 | head->write = tomoyo_write_exception_policy; |
| 1676 | head->read = tomoyo_read_exception_policy; |
| 1677 | break; |
| 1678 | case TOMOYO_SELFDOMAIN: |
| 1679 | /* /sys/kernel/security/tomoyo/self_domain */ |
| 1680 | head->read = tomoyo_read_self_domain; |
| 1681 | break; |
| 1682 | case TOMOYO_DOMAIN_STATUS: |
| 1683 | /* /sys/kernel/security/tomoyo/.domain_status */ |
| 1684 | head->write = tomoyo_write_domain_profile; |
| 1685 | head->read = tomoyo_read_domain_profile; |
| 1686 | break; |
| 1687 | case TOMOYO_PROCESS_STATUS: |
| 1688 | /* /sys/kernel/security/tomoyo/.process_status */ |
| 1689 | head->write = tomoyo_write_pid; |
| 1690 | head->read = tomoyo_read_pid; |
| 1691 | break; |
| 1692 | case TOMOYO_VERSION: |
| 1693 | /* /sys/kernel/security/tomoyo/version */ |
| 1694 | head->read = tomoyo_read_version; |
| 1695 | head->readbuf_size = 128; |
| 1696 | break; |
| 1697 | case TOMOYO_MEMINFO: |
| 1698 | /* /sys/kernel/security/tomoyo/meminfo */ |
| 1699 | head->write = tomoyo_write_memory_quota; |
| 1700 | head->read = tomoyo_read_memory_counter; |
| 1701 | head->readbuf_size = 512; |
| 1702 | break; |
| 1703 | case TOMOYO_PROFILE: |
| 1704 | /* /sys/kernel/security/tomoyo/profile */ |
| 1705 | head->write = tomoyo_write_profile; |
| 1706 | head->read = tomoyo_read_profile; |
| 1707 | break; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1708 | case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */ |
| 1709 | head->poll = tomoyo_poll_query; |
| 1710 | head->write = tomoyo_write_answer; |
| 1711 | head->read = tomoyo_read_query; |
| 1712 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1713 | case TOMOYO_MANAGER: |
| 1714 | /* /sys/kernel/security/tomoyo/manager */ |
| 1715 | head->write = tomoyo_write_manager_policy; |
| 1716 | head->read = tomoyo_read_manager_policy; |
| 1717 | break; |
| 1718 | } |
| 1719 | if (!(file->f_mode & FMODE_READ)) { |
| 1720 | /* |
| 1721 | * No need to allocate read_buf since it is not opened |
| 1722 | * for reading. |
| 1723 | */ |
| 1724 | head->read = NULL; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1725 | head->poll = NULL; |
| 1726 | } else if (!head->poll) { |
| 1727 | /* Don't allocate read_buf for poll() access. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1728 | if (!head->readbuf_size) |
| 1729 | head->readbuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1730 | head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1731 | if (!head->read_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1732 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1733 | return -ENOMEM; |
| 1734 | } |
| 1735 | } |
| 1736 | if (!(file->f_mode & FMODE_WRITE)) { |
| 1737 | /* |
| 1738 | * No need to allocate write_buf since it is not opened |
| 1739 | * for writing. |
| 1740 | */ |
| 1741 | head->write = NULL; |
| 1742 | } else if (head->write) { |
| 1743 | head->writebuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1744 | head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1745 | if (!head->write_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1746 | kfree(head->read_buf); |
| 1747 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1748 | return -ENOMEM; |
| 1749 | } |
| 1750 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1751 | if (type != TOMOYO_QUERY) |
| 1752 | head->reader_idx = tomoyo_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1753 | file->private_data = head; |
| 1754 | /* |
| 1755 | * Call the handler now if the file is |
| 1756 | * /sys/kernel/security/tomoyo/self_domain |
| 1757 | * so that the user can use |
| 1758 | * cat < /sys/kernel/security/tomoyo/self_domain" |
| 1759 | * to know the current process's domainname. |
| 1760 | */ |
| 1761 | if (type == TOMOYO_SELFDOMAIN) |
| 1762 | tomoyo_read_control(file, NULL, 0); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1763 | /* |
| 1764 | * If the file is /sys/kernel/security/tomoyo/query , increment the |
| 1765 | * observer counter. |
| 1766 | * The obserber counter is used by tomoyo_supervisor() to see if |
| 1767 | * there is some process monitoring /sys/kernel/security/tomoyo/query. |
| 1768 | */ |
| 1769 | else if (type == TOMOYO_QUERY) |
| 1770 | atomic_inc(&tomoyo_query_observers); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1775 | * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface. |
| 1776 | * |
| 1777 | * @file: Pointer to "struct file". |
| 1778 | * @wait: Pointer to "poll_table". |
| 1779 | * |
| 1780 | * Waits for read readiness. |
| 1781 | * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd . |
| 1782 | */ |
| 1783 | int tomoyo_poll_control(struct file *file, poll_table *wait) |
| 1784 | { |
| 1785 | struct tomoyo_io_buffer *head = file->private_data; |
| 1786 | if (!head->poll) |
| 1787 | return -ENOSYS; |
| 1788 | return head->poll(file, wait); |
| 1789 | } |
| 1790 | |
| 1791 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1792 | * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface. |
| 1793 | * |
| 1794 | * @file: Pointer to "struct file". |
| 1795 | * @buffer: Poiner to buffer to write to. |
| 1796 | * @buffer_len: Size of @buffer. |
| 1797 | * |
| 1798 | * Returns bytes read on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1799 | * |
| 1800 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1801 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1802 | int tomoyo_read_control(struct file *file, char __user *buffer, |
| 1803 | const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1804 | { |
| 1805 | int len = 0; |
| 1806 | struct tomoyo_io_buffer *head = file->private_data; |
| 1807 | char *cp; |
| 1808 | |
| 1809 | if (!head->read) |
| 1810 | return -ENOSYS; |
| 1811 | if (mutex_lock_interruptible(&head->io_sem)) |
| 1812 | return -EINTR; |
| 1813 | /* Call the policy handler. */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1814 | head->read(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1815 | if (len < 0) |
| 1816 | goto out; |
| 1817 | /* Write to buffer. */ |
| 1818 | len = head->read_avail; |
| 1819 | if (len > buffer_len) |
| 1820 | len = buffer_len; |
| 1821 | if (!len) |
| 1822 | goto out; |
| 1823 | /* head->read_buf changes by some functions. */ |
| 1824 | cp = head->read_buf; |
| 1825 | if (copy_to_user(buffer, cp, len)) { |
| 1826 | len = -EFAULT; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | head->read_avail -= len; |
| 1830 | memmove(cp, cp + len, head->read_avail); |
| 1831 | out: |
| 1832 | mutex_unlock(&head->io_sem); |
| 1833 | return len; |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface. |
| 1838 | * |
| 1839 | * @file: Pointer to "struct file". |
| 1840 | * @buffer: Pointer to buffer to read from. |
| 1841 | * @buffer_len: Size of @buffer. |
| 1842 | * |
| 1843 | * Returns @buffer_len on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1844 | * |
| 1845 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1846 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1847 | int tomoyo_write_control(struct file *file, const char __user *buffer, |
| 1848 | const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1849 | { |
| 1850 | struct tomoyo_io_buffer *head = file->private_data; |
| 1851 | int error = buffer_len; |
| 1852 | int avail_len = buffer_len; |
| 1853 | char *cp0 = head->write_buf; |
| 1854 | |
| 1855 | if (!head->write) |
| 1856 | return -ENOSYS; |
| 1857 | if (!access_ok(VERIFY_READ, buffer, buffer_len)) |
| 1858 | return -EFAULT; |
| 1859 | /* Don't allow updating policies by non manager programs. */ |
| 1860 | if (head->write != tomoyo_write_pid && |
| 1861 | head->write != tomoyo_write_domain_policy && |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1862 | !tomoyo_policy_manager()) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1863 | return -EPERM; |
| 1864 | if (mutex_lock_interruptible(&head->io_sem)) |
| 1865 | return -EINTR; |
| 1866 | /* Read a line and dispatch it to the policy handler. */ |
| 1867 | while (avail_len > 0) { |
| 1868 | char c; |
| 1869 | if (head->write_avail >= head->writebuf_size - 1) { |
| 1870 | error = -ENOMEM; |
| 1871 | break; |
| 1872 | } else if (get_user(c, buffer)) { |
| 1873 | error = -EFAULT; |
| 1874 | break; |
| 1875 | } |
| 1876 | buffer++; |
| 1877 | avail_len--; |
| 1878 | cp0[head->write_avail++] = c; |
| 1879 | if (c != '\n') |
| 1880 | continue; |
| 1881 | cp0[head->write_avail - 1] = '\0'; |
| 1882 | head->write_avail = 0; |
| 1883 | tomoyo_normalize_line(cp0); |
| 1884 | head->write(head); |
| 1885 | } |
| 1886 | mutex_unlock(&head->io_sem); |
| 1887 | return error; |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface. |
| 1892 | * |
| 1893 | * @file: Pointer to "struct file". |
| 1894 | * |
| 1895 | * Releases memory and returns 0. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1896 | * |
| 1897 | * Caller looses tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1898 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1899 | int tomoyo_close_control(struct file *file) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1900 | { |
| 1901 | struct tomoyo_io_buffer *head = file->private_data; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 1902 | const bool is_write = !!head->write_buf; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1903 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1904 | /* |
| 1905 | * If the file is /sys/kernel/security/tomoyo/query , decrement the |
| 1906 | * observer counter. |
| 1907 | */ |
| 1908 | if (head->type == TOMOYO_QUERY) |
| 1909 | atomic_dec(&tomoyo_query_observers); |
| 1910 | else |
| 1911 | tomoyo_read_unlock(head->reader_idx); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1912 | /* Release memory used for policy I/O. */ |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1913 | kfree(head->read_buf); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1914 | head->read_buf = NULL; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1915 | kfree(head->write_buf); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1916 | head->write_buf = NULL; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1917 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1918 | head = NULL; |
| 1919 | file->private_data = NULL; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 1920 | if (is_write) |
| 1921 | tomoyo_run_gc(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1926 | * tomoyo_check_profile - Check all profiles currently assigned to domains are defined. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1927 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1928 | void tomoyo_check_profile(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1929 | { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1930 | struct tomoyo_domain_info *domain; |
| 1931 | const int idx = tomoyo_read_lock(); |
| 1932 | tomoyo_policy_loaded = true; |
| 1933 | /* Check all profiles currently assigned to domains are defined. */ |
| 1934 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 1935 | const u8 profile = domain->profile; |
| 1936 | if (tomoyo_profile_ptr[profile]) |
| 1937 | continue; |
| 1938 | panic("Profile %u (used by '%s') not defined.\n", |
| 1939 | profile, domain->domainname->name); |
| 1940 | } |
| 1941 | tomoyo_read_unlock(idx); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1942 | if (tomoyo_profile_version != 20090903) |
| 1943 | panic("Profile version %u is not supported.\n", |
| 1944 | tomoyo_profile_version); |
| 1945 | printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n"); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1946 | printk(KERN_INFO "Mandatory Access Control activated.\n"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1947 | } |