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 | { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 510 | bool done = true; |
| 511 | |
| 512 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 513 | return; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 514 | list_for_each_cookie(head->read_var2, |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 515 | &tomoyo_policy_list[TOMOYO_ID_MANAGER]) { |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 516 | struct tomoyo_policy_manager_entry *ptr = |
| 517 | list_entry(head->read_var2, typeof(*ptr), head.list); |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 518 | if (ptr->head.is_deleted) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 519 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 520 | done = tomoyo_io_printf(head, "%s\n", ptr->manager->name); |
| 521 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 522 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 523 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 524 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 528 | * tomoyo_policy_manager - Check whether the current process is a policy manager. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 529 | * |
| 530 | * Returns true if the current process is permitted to modify policy |
| 531 | * via /sys/kernel/security/tomoyo/ interface. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 532 | * |
| 533 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 534 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 535 | static bool tomoyo_policy_manager(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 536 | { |
| 537 | struct tomoyo_policy_manager_entry *ptr; |
| 538 | const char *exe; |
| 539 | const struct task_struct *task = current; |
| 540 | const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname; |
| 541 | bool found = false; |
| 542 | |
| 543 | if (!tomoyo_policy_loaded) |
| 544 | return true; |
| 545 | if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid)) |
| 546 | return false; |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 547 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], |
| 548 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 549 | if (!ptr->head.is_deleted && ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 550 | && !tomoyo_pathcmp(domainname, ptr->manager)) { |
| 551 | found = true; |
| 552 | break; |
| 553 | } |
| 554 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 555 | if (found) |
| 556 | return true; |
| 557 | exe = tomoyo_get_exe(); |
| 558 | if (!exe) |
| 559 | return false; |
Tetsuo Handa | a230f9e | 2010-06-17 16:53:24 +0900 | [diff] [blame] | 560 | list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], |
| 561 | head.list) { |
Tetsuo Handa | 82e0f00 | 2010-06-15 09:22:42 +0900 | [diff] [blame] | 562 | if (!ptr->head.is_deleted && !ptr->is_domain |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 563 | && !strcmp(exe, ptr->manager->name)) { |
| 564 | found = true; |
| 565 | break; |
| 566 | } |
| 567 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 568 | if (!found) { /* Reduce error messages. */ |
| 569 | static pid_t last_pid; |
| 570 | const pid_t pid = current->pid; |
| 571 | if (last_pid != pid) { |
| 572 | printk(KERN_WARNING "%s ( %s ) is not permitted to " |
| 573 | "update policies.\n", domainname->name, exe); |
| 574 | last_pid = pid; |
| 575 | } |
| 576 | } |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 577 | kfree(exe); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 578 | return found; |
| 579 | } |
| 580 | |
| 581 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 582 | * tomoyo_select_one - Parse select command. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 583 | * |
| 584 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 585 | * @data: String to parse. |
| 586 | * |
| 587 | * Returns true on success, false otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 588 | * |
| 589 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 590 | */ |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 591 | static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 592 | { |
| 593 | unsigned int pid; |
| 594 | struct tomoyo_domain_info *domain = NULL; |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 595 | bool global_pid = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 596 | |
Tetsuo Handa | 063821c | 2010-06-24 12:00:25 +0900 | [diff] [blame] | 597 | if (!strcmp(data, "allow_execute")) { |
| 598 | head->print_execute_only = true; |
| 599 | return true; |
| 600 | } |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 601 | if (sscanf(data, "pid=%u", &pid) == 1 || |
| 602 | (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 603 | struct task_struct *p; |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 604 | rcu_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 605 | read_lock(&tasklist_lock); |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 606 | if (global_pid) |
| 607 | p = find_task_by_pid_ns(pid, &init_pid_ns); |
| 608 | else |
| 609 | p = find_task_by_vpid(pid); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 610 | if (p) |
| 611 | domain = tomoyo_real_domain(p); |
| 612 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 613 | rcu_read_unlock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 614 | } else if (!strncmp(data, "domain=", 7)) { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 615 | if (tomoyo_domain_def(data + 7)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 616 | domain = tomoyo_find_domain(data + 7); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 617 | } else |
| 618 | return false; |
| 619 | head->write_var1 = domain; |
| 620 | /* Accessing read_buf is safe because head->io_sem is held. */ |
| 621 | if (!head->read_buf) |
| 622 | return true; /* Do nothing if open(O_WRONLY). */ |
| 623 | head->read_avail = 0; |
| 624 | tomoyo_io_printf(head, "# select %s\n", data); |
| 625 | head->read_single_domain = true; |
| 626 | head->read_eof = !domain; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 627 | head->read_var1 = &domain->list; |
| 628 | head->read_var2 = NULL; |
| 629 | head->read_bit = 0; |
| 630 | head->read_step = 0; |
| 631 | if (domain && domain->is_deleted) |
| 632 | tomoyo_io_printf(head, "# This is a deleted domain.\n"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | |
| 636 | /** |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 637 | * tomoyo_delete_domain - Delete a domain. |
| 638 | * |
| 639 | * @domainname: The name of domain. |
| 640 | * |
| 641 | * Returns 0. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 642 | * |
| 643 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 644 | */ |
| 645 | static int tomoyo_delete_domain(char *domainname) |
| 646 | { |
| 647 | struct tomoyo_domain_info *domain; |
| 648 | struct tomoyo_path_info name; |
| 649 | |
| 650 | name.name = domainname; |
| 651 | tomoyo_fill_path_info(&name); |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 652 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 653 | return 0; |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 654 | /* Is there an active domain? */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 655 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 656 | /* Never delete tomoyo_kernel_domain */ |
| 657 | if (domain == &tomoyo_kernel_domain) |
| 658 | continue; |
| 659 | if (domain->is_deleted || |
| 660 | tomoyo_pathcmp(domain->domainname, &name)) |
| 661 | continue; |
| 662 | domain->is_deleted = true; |
| 663 | break; |
| 664 | } |
Tetsuo Handa | f737d95 | 2010-01-03 21:16:32 +0900 | [diff] [blame] | 665 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | ccf135f | 2009-06-19 10:29:34 +0900 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 670 | * tomoyo_write_domain_policy2 - Write domain policy. |
| 671 | * |
| 672 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 673 | * |
| 674 | * Returns 0 on success, negative value otherwise. |
| 675 | * |
| 676 | * Caller holds tomoyo_read_lock(). |
| 677 | */ |
| 678 | static int tomoyo_write_domain_policy2(char *data, |
| 679 | struct tomoyo_domain_info *domain, |
| 680 | const bool is_delete) |
| 681 | { |
| 682 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT)) |
| 683 | return tomoyo_write_mount_policy(data, domain, is_delete); |
| 684 | return tomoyo_write_file_policy(data, domain, is_delete); |
| 685 | } |
| 686 | |
| 687 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 688 | * tomoyo_write_domain_policy - Write domain policy. |
| 689 | * |
| 690 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 691 | * |
| 692 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 693 | * |
| 694 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 695 | */ |
| 696 | static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head) |
| 697 | { |
| 698 | char *data = head->write_buf; |
| 699 | struct tomoyo_domain_info *domain = head->write_var1; |
| 700 | bool is_delete = false; |
| 701 | bool is_select = false; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 702 | unsigned int profile; |
| 703 | |
| 704 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE)) |
| 705 | is_delete = true; |
| 706 | else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT)) |
| 707 | is_select = true; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 708 | if (is_select && tomoyo_select_one(head, data)) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 709 | return 0; |
| 710 | /* Don't allow updating policies by non manager programs. */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 711 | if (!tomoyo_policy_manager()) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 712 | return -EPERM; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 713 | if (tomoyo_domain_def(data)) { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 714 | domain = NULL; |
| 715 | if (is_delete) |
| 716 | tomoyo_delete_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 717 | else if (is_select) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 718 | domain = tomoyo_find_domain(data); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 719 | else |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 720 | domain = tomoyo_find_or_assign_new_domain(data, 0); |
| 721 | head->write_var1 = domain; |
| 722 | return 0; |
| 723 | } |
| 724 | if (!domain) |
| 725 | return -EINVAL; |
| 726 | |
| 727 | if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1 |
| 728 | && profile < TOMOYO_MAX_PROFILES) { |
| 729 | if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded) |
| 730 | domain->profile = (u8) profile; |
| 731 | return 0; |
| 732 | } |
| 733 | if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) { |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 734 | domain->ignore_global_allow_read = !is_delete; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 735 | return 0; |
| 736 | } |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 737 | if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) { |
| 738 | domain->quota_warned = !is_delete; |
| 739 | return 0; |
| 740 | } |
| 741 | if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) { |
| 742 | domain->transition_failed = !is_delete; |
| 743 | return 0; |
| 744 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 745 | return tomoyo_write_domain_policy2(data, domain, is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /** |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 749 | * tomoyo_fns - Find next set bit. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 750 | * |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 751 | * @perm: 8 bits value. |
| 752 | * @bit: First bit to find. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 753 | * |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 754 | * Returns next on-bit on success, 8 otherwise. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 755 | */ |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 756 | static u8 tomoyo_fns(const u8 perm, u8 bit) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 757 | { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 758 | for ( ; bit < 8; bit++) |
| 759 | if (perm & (1 << bit)) |
| 760 | break; |
| 761 | return bit; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 765 | * tomoyo_print_entry - Print an ACL entry. |
| 766 | * |
| 767 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 768 | * @acl: Pointer to an ACL entry. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 769 | * |
| 770 | * Returns true on success, false otherwise. |
| 771 | */ |
| 772 | static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 773 | struct tomoyo_acl_info *acl) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 774 | { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 775 | const u8 acl_type = acl->type; |
| 776 | u8 bit = head->read_bit; |
| 777 | int pos = head->read_avail; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 778 | |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 779 | if (acl->is_deleted) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 780 | return true; |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 781 | next: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 782 | if (acl_type == TOMOYO_TYPE_PATH_ACL) { |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 783 | struct tomoyo_path_acl *ptr = |
| 784 | container_of(acl, typeof(*ptr), head); |
| 785 | const u16 perm = ptr->perm; |
| 786 | for ( ; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { |
| 787 | if (!(perm & (1 << bit))) |
| 788 | continue; |
| 789 | if (head->print_execute_only && |
| 790 | bit != TOMOYO_TYPE_EXECUTE) |
| 791 | continue; |
| 792 | /* Print "read/write" instead of "read" and "write". */ |
| 793 | if ((bit == TOMOYO_TYPE_READ || |
| 794 | bit == TOMOYO_TYPE_WRITE) |
| 795 | && (perm & (1 << TOMOYO_TYPE_READ_WRITE))) |
| 796 | continue; |
| 797 | break; |
| 798 | } |
| 799 | if (bit >= TOMOYO_MAX_PATH_OPERATION) |
| 800 | goto done; |
| 801 | if (!tomoyo_io_printf(head, "allow_%s ", |
| 802 | tomoyo_path_keyword[bit]) || |
| 803 | !tomoyo_print_name_union(head, &ptr->name)) |
| 804 | goto out; |
| 805 | } else if (head->print_execute_only) { |
Tetsuo Handa | 063821c | 2010-06-24 12:00:25 +0900 | [diff] [blame] | 806 | return true; |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 807 | } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) { |
| 808 | struct tomoyo_path2_acl *ptr = |
| 809 | container_of(acl, typeof(*ptr), head); |
| 810 | bit = tomoyo_fns(ptr->perm, bit); |
| 811 | if (bit >= TOMOYO_MAX_PATH2_OPERATION) |
| 812 | goto done; |
| 813 | if (!tomoyo_io_printf(head, "allow_%s ", |
| 814 | tomoyo_path2_keyword[bit]) || |
| 815 | !tomoyo_print_name_union(head, &ptr->name1) || |
| 816 | !tomoyo_print_name_union(head, &ptr->name2)) |
| 817 | goto out; |
| 818 | } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) { |
| 819 | struct tomoyo_path_number_acl *ptr = |
| 820 | container_of(acl, typeof(*ptr), head); |
| 821 | bit = tomoyo_fns(ptr->perm, bit); |
| 822 | if (bit >= TOMOYO_MAX_PATH_NUMBER_OPERATION) |
| 823 | goto done; |
| 824 | if (!tomoyo_io_printf(head, "allow_%s", |
| 825 | tomoyo_path_number_keyword[bit]) || |
| 826 | !tomoyo_print_name_union(head, &ptr->name) || |
| 827 | !tomoyo_print_number_union(head, &ptr->number)) |
| 828 | goto out; |
| 829 | } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) { |
| 830 | struct tomoyo_mkdev_acl *ptr = |
| 831 | container_of(acl, typeof(*ptr), head); |
| 832 | bit = tomoyo_fns(ptr->perm, bit); |
| 833 | if (bit >= TOMOYO_MAX_MKDEV_OPERATION) |
| 834 | goto done; |
| 835 | if (!tomoyo_io_printf(head, "allow_%s", |
| 836 | tomoyo_mkdev_keyword[bit]) || |
| 837 | !tomoyo_print_name_union(head, &ptr->name) || |
| 838 | !tomoyo_print_number_union(head, &ptr->mode) || |
| 839 | !tomoyo_print_number_union(head, &ptr->major) || |
| 840 | !tomoyo_print_number_union(head, &ptr->minor)) |
| 841 | goto out; |
| 842 | } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { |
| 843 | struct tomoyo_mount_acl *ptr = |
| 844 | container_of(acl, typeof(*ptr), head); |
| 845 | if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) || |
| 846 | !tomoyo_print_name_union(head, &ptr->dev_name) || |
| 847 | !tomoyo_print_name_union(head, &ptr->dir_name) || |
| 848 | !tomoyo_print_name_union(head, &ptr->fs_type) || |
| 849 | !tomoyo_print_number_union(head, &ptr->flags)) |
| 850 | goto out; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 851 | } |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 852 | if (!tomoyo_io_printf(head, "\n")) |
| 853 | goto out; |
| 854 | head->read_bit = bit; |
| 855 | if (acl_type != TOMOYO_TYPE_MOUNT_ACL) { |
| 856 | bit++; |
| 857 | goto next; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 858 | } |
Tetsuo Handa | 5db5a39 | 2010-06-24 12:24:19 +0900 | [diff] [blame^] | 859 | done: |
| 860 | head->read_bit = 0; |
| 861 | return true; |
| 862 | out: |
| 863 | head->read_avail = pos; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * tomoyo_read_domain_policy - Read domain policy. |
| 869 | * |
| 870 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 871 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 872 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 873 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 874 | static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 875 | { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 876 | bool done = true; |
| 877 | |
| 878 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 879 | return; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 880 | if (head->read_step == 0) |
| 881 | head->read_step = 1; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 882 | list_for_each_cookie(head->read_var1, &tomoyo_domain_list) { |
| 883 | struct tomoyo_domain_info *domain = |
| 884 | list_entry(head->read_var1, typeof(*domain), list); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 885 | const char *quota_exceeded = ""; |
| 886 | const char *transition_failed = ""; |
| 887 | const char *ignore_global_allow_read = ""; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 888 | if (head->read_step != 1) |
| 889 | goto acl_loop; |
| 890 | if (domain->is_deleted && !head->read_single_domain) |
| 891 | continue; |
| 892 | /* Print domainname and flags. */ |
| 893 | if (domain->quota_warned) |
| 894 | quota_exceeded = "quota_exceeded\n"; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 895 | if (domain->transition_failed) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 896 | transition_failed = "transition_failed\n"; |
Tetsuo Handa | ea13ddb | 2010-02-03 06:43:06 +0900 | [diff] [blame] | 897 | if (domain->ignore_global_allow_read) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 898 | ignore_global_allow_read |
| 899 | = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n"; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 900 | done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE |
| 901 | "%u\n%s%s%s\n", |
| 902 | domain->domainname->name, |
| 903 | domain->profile, quota_exceeded, |
| 904 | transition_failed, |
| 905 | ignore_global_allow_read); |
| 906 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 907 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 908 | head->read_step = 2; |
| 909 | acl_loop: |
| 910 | if (head->read_step == 3) |
| 911 | goto tail_mark; |
| 912 | /* Print ACL entries in the domain. */ |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 913 | list_for_each_cookie(head->read_var2, |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 914 | &domain->acl_info_list) { |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 915 | struct tomoyo_acl_info *ptr = |
| 916 | list_entry(head->read_var2, typeof(*ptr), list); |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 917 | done = tomoyo_print_entry(head, ptr); |
| 918 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 919 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 920 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 921 | if (!done) |
| 922 | break; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 923 | head->read_var2 = NULL; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 924 | head->read_step = 3; |
| 925 | tail_mark: |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 926 | done = tomoyo_io_printf(head, "\n"); |
| 927 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 928 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 929 | head->read_step = 1; |
| 930 | if (head->read_single_domain) |
| 931 | break; |
| 932 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 933 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /** |
| 937 | * tomoyo_write_domain_profile - Assign profile for specified domain. |
| 938 | * |
| 939 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 940 | * |
| 941 | * Returns 0 on success, -EINVAL otherwise. |
| 942 | * |
| 943 | * This is equivalent to doing |
| 944 | * |
| 945 | * ( echo "select " $domainname; echo "use_profile " $profile ) | |
Tetsuo Handa | 9b244373 | 2010-06-03 20:35:53 +0900 | [diff] [blame] | 946 | * /usr/sbin/tomoyo-loadpolicy -d |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 947 | * |
| 948 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 949 | */ |
| 950 | static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head) |
| 951 | { |
| 952 | char *data = head->write_buf; |
| 953 | char *cp = strchr(data, ' '); |
| 954 | struct tomoyo_domain_info *domain; |
| 955 | unsigned long profile; |
| 956 | |
| 957 | if (!cp) |
| 958 | return -EINVAL; |
| 959 | *cp = '\0'; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 960 | domain = tomoyo_find_domain(cp + 1); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 961 | if (strict_strtoul(data, 10, &profile)) |
| 962 | return -EINVAL; |
| 963 | if (domain && profile < TOMOYO_MAX_PROFILES |
| 964 | && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)) |
| 965 | domain->profile = (u8) profile; |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * tomoyo_read_domain_profile - Read only domainname and profile. |
| 971 | * |
| 972 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 973 | * |
| 974 | * Returns list of profile number and domainname pairs. |
| 975 | * |
| 976 | * This is equivalent to doing |
| 977 | * |
| 978 | * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy | |
| 979 | * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" ) |
| 980 | * domainname = $0; } else if ( $1 == "use_profile" ) { |
| 981 | * print $2 " " domainname; domainname = ""; } } ; ' |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 982 | * |
| 983 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 984 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 985 | static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 986 | { |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 987 | bool done = true; |
| 988 | |
| 989 | if (head->read_eof) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 990 | return; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 991 | list_for_each_cookie(head->read_var1, &tomoyo_domain_list) { |
| 992 | struct tomoyo_domain_info *domain = |
| 993 | list_entry(head->read_var1, typeof(*domain), list); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 994 | if (domain->is_deleted) |
| 995 | continue; |
Tetsuo Handa | 7d2948b | 2009-06-02 20:42:24 +0900 | [diff] [blame] | 996 | done = tomoyo_io_printf(head, "%u %s\n", domain->profile, |
| 997 | domain->domainname->name); |
| 998 | if (!done) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 999 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1000 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1001 | head->read_eof = done; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * tomoyo_write_pid: Specify PID to obtain domainname. |
| 1006 | * |
| 1007 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1008 | * |
| 1009 | * Returns 0. |
| 1010 | */ |
| 1011 | static int tomoyo_write_pid(struct tomoyo_io_buffer *head) |
| 1012 | { |
| 1013 | unsigned long pid; |
| 1014 | /* No error check. */ |
| 1015 | strict_strtoul(head->write_buf, 10, &pid); |
| 1016 | head->read_step = (int) pid; |
| 1017 | head->read_eof = false; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * tomoyo_read_pid - Get domainname of the specified PID. |
| 1023 | * |
| 1024 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1025 | * |
| 1026 | * Returns the domainname which the specified PID is in on success, |
| 1027 | * empty string otherwise. |
| 1028 | * The PID is specified by tomoyo_write_pid() so that the user can obtain |
| 1029 | * using read()/write() interface rather than sysctl() interface. |
| 1030 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1031 | static void tomoyo_read_pid(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1032 | { |
| 1033 | if (head->read_avail == 0 && !head->read_eof) { |
| 1034 | const int pid = head->read_step; |
| 1035 | struct task_struct *p; |
| 1036 | struct tomoyo_domain_info *domain = NULL; |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 1037 | rcu_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1038 | read_lock(&tasklist_lock); |
| 1039 | p = find_task_by_vpid(pid); |
| 1040 | if (p) |
| 1041 | domain = tomoyo_real_domain(p); |
| 1042 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 1fcdc7c | 2010-02-25 17:19:25 +0900 | [diff] [blame] | 1043 | rcu_read_unlock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1044 | if (domain) |
| 1045 | tomoyo_io_printf(head, "%d %u %s", pid, domain->profile, |
| 1046 | domain->domainname->name); |
| 1047 | head->read_eof = true; |
| 1048 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1049 | } |
| 1050 | |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1051 | static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = { |
| 1052 | [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] |
| 1053 | = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN, |
| 1054 | [TOMOYO_TRANSITION_CONTROL_INITIALIZE] |
| 1055 | = TOMOYO_KEYWORD_INITIALIZE_DOMAIN, |
| 1056 | [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN, |
| 1057 | [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN |
| 1058 | }; |
| 1059 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1060 | /** |
| 1061 | * tomoyo_write_exception_policy - Write exception policy. |
| 1062 | * |
| 1063 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1064 | * |
| 1065 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1066 | * |
| 1067 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1068 | */ |
| 1069 | static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head) |
| 1070 | { |
| 1071 | char *data = head->write_buf; |
| 1072 | bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE); |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1073 | u8 i; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1074 | |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1075 | for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) { |
| 1076 | if (tomoyo_str_starts(&data, tomoyo_transition_type[i])) |
| 1077 | return tomoyo_write_transition_control(data, is_delete, |
| 1078 | i); |
| 1079 | } |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 1080 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR)) |
| 1081 | return tomoyo_write_aggregator_policy(data, is_delete); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1082 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ)) |
| 1083 | return tomoyo_write_globally_readable_policy(data, is_delete); |
| 1084 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN)) |
| 1085 | return tomoyo_write_pattern_policy(data, is_delete); |
| 1086 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE)) |
| 1087 | return tomoyo_write_no_rewrite_policy(data, is_delete); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 1088 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP)) |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 1089 | return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 1090 | if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP)) |
Tetsuo Handa | 7c2ea22 | 2010-06-17 16:55:58 +0900 | [diff] [blame] | 1091 | return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | } |
| 1094 | |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1095 | static void tomoyo_print_number(char *buffer, int buffer_len, |
| 1096 | const struct tomoyo_number_union *ptr) |
| 1097 | { |
| 1098 | int i; |
| 1099 | unsigned long min = ptr->values[0]; |
| 1100 | const unsigned long max = ptr->values[1]; |
| 1101 | u8 min_type = ptr->min_type; |
| 1102 | const u8 max_type = ptr->max_type; |
| 1103 | memset(buffer, 0, buffer_len); |
| 1104 | buffer_len -= 2; |
| 1105 | for (i = 0; i < 2; i++) { |
| 1106 | int len; |
| 1107 | switch (min_type) { |
| 1108 | case TOMOYO_VALUE_TYPE_HEXADECIMAL: |
| 1109 | snprintf(buffer, buffer_len, "0x%lX", min); |
| 1110 | break; |
| 1111 | case TOMOYO_VALUE_TYPE_OCTAL: |
| 1112 | snprintf(buffer, buffer_len, "0%lo", min); |
| 1113 | break; |
| 1114 | default: |
| 1115 | snprintf(buffer, buffer_len, "%lu", min); |
| 1116 | break; |
| 1117 | } |
| 1118 | if (min == max && min_type == max_type) |
| 1119 | break; |
| 1120 | len = strlen(buffer); |
| 1121 | buffer[len++] = '-'; |
| 1122 | buffer += len; |
| 1123 | buffer_len -= len; |
| 1124 | min_type = max_type; |
| 1125 | min = max; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = { |
| 1130 | [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP, |
| 1131 | [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP |
| 1132 | }; |
| 1133 | |
| 1134 | /** |
| 1135 | * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list. |
| 1136 | * |
| 1137 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1138 | * @idx: Index number. |
| 1139 | * |
| 1140 | * Returns true on success, false otherwise. |
| 1141 | * |
| 1142 | * Caller holds tomoyo_read_lock(). |
| 1143 | */ |
| 1144 | static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) |
| 1145 | { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1146 | const char *w[3] = { "", "", "" }; |
| 1147 | w[0] = tomoyo_group_name[idx]; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1148 | list_for_each_cookie(head->read_var1, &tomoyo_group_list[idx]) { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1149 | struct tomoyo_group *group = |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1150 | list_entry(head->read_var1, typeof(*group), list); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1151 | w[1] = group->group_name->name; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1152 | list_for_each_cookie(head->read_var2, &group->member_list) { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1153 | char buffer[128]; |
| 1154 | struct tomoyo_acl_head *ptr = |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1155 | list_entry(head->read_var2, typeof(*ptr), list); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1156 | if (ptr->is_deleted) |
| 1157 | continue; |
| 1158 | if (idx == TOMOYO_PATH_GROUP) { |
| 1159 | w[2] = container_of(ptr, |
| 1160 | struct tomoyo_path_group, |
| 1161 | head)->member_name->name; |
| 1162 | } else if (idx == TOMOYO_NUMBER_GROUP) { |
| 1163 | tomoyo_print_number(buffer, sizeof(buffer), |
| 1164 | &container_of |
| 1165 | (ptr, struct |
| 1166 | tomoyo_number_group, |
| 1167 | head)->number); |
| 1168 | w[2] = buffer; |
| 1169 | } |
| 1170 | if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1], |
| 1171 | w[2])) |
| 1172 | return false; |
| 1173 | } |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1174 | head->read_var2 = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1175 | } |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1176 | head->read_var1 = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * tomoyo_read_policy - Read "struct tomoyo_..._entry" list. |
| 1182 | * |
| 1183 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1184 | * @idx: Index number. |
| 1185 | * |
| 1186 | * Returns true on success, false otherwise. |
| 1187 | * |
| 1188 | * Caller holds tomoyo_read_lock(). |
| 1189 | */ |
| 1190 | static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) |
| 1191 | { |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1192 | list_for_each_cookie(head->read_var2, &tomoyo_policy_list[idx]) { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1193 | const char *w[4] = { "", "", "", "" }; |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1194 | struct tomoyo_acl_head *acl = |
| 1195 | container_of(head->read_var2, typeof(*acl), list); |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1196 | if (acl->is_deleted) |
| 1197 | continue; |
| 1198 | switch (idx) { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1199 | case TOMOYO_ID_TRANSITION_CONTROL: |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1200 | { |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1201 | struct tomoyo_transition_control *ptr = |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1202 | container_of(acl, typeof(*ptr), head); |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1203 | w[0] = tomoyo_transition_type[ptr->type]; |
| 1204 | if (ptr->program) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1205 | w[1] = ptr->program->name; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1206 | if (ptr->domainname) |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1207 | w[3] = ptr->domainname->name; |
Tetsuo Handa | 5448ec4 | 2010-06-21 11:14:39 +0900 | [diff] [blame] | 1208 | if (w[1][0] && w[3][0]) |
| 1209 | w[2] = " from "; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1210 | } |
| 1211 | break; |
| 1212 | case TOMOYO_ID_GLOBALLY_READABLE: |
| 1213 | { |
| 1214 | struct tomoyo_globally_readable_file_entry *ptr |
| 1215 | = container_of(acl, typeof(*ptr), head); |
| 1216 | w[0] = TOMOYO_KEYWORD_ALLOW_READ; |
| 1217 | w[1] = ptr->filename->name; |
| 1218 | } |
| 1219 | break; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1220 | case TOMOYO_ID_AGGREGATOR: |
| 1221 | { |
| 1222 | struct tomoyo_aggregator_entry *ptr = |
| 1223 | container_of(acl, typeof(*ptr), head); |
| 1224 | w[0] = TOMOYO_KEYWORD_AGGREGATOR; |
| 1225 | w[1] = ptr->original_name->name; |
| 1226 | w[2] = " "; |
| 1227 | w[3] = ptr->aggregated_name->name; |
| 1228 | } |
| 1229 | break; |
| 1230 | case TOMOYO_ID_PATTERN: |
| 1231 | { |
| 1232 | struct tomoyo_pattern_entry *ptr = |
| 1233 | container_of(acl, typeof(*ptr), head); |
| 1234 | w[0] = TOMOYO_KEYWORD_FILE_PATTERN; |
| 1235 | w[1] = ptr->pattern->name; |
| 1236 | } |
| 1237 | break; |
| 1238 | case TOMOYO_ID_NO_REWRITE: |
| 1239 | { |
| 1240 | struct tomoyo_no_rewrite_entry *ptr = |
| 1241 | container_of(acl, typeof(*ptr), head); |
| 1242 | w[0] = TOMOYO_KEYWORD_DENY_REWRITE; |
| 1243 | w[1] = ptr->pattern->name; |
| 1244 | } |
| 1245 | break; |
| 1246 | default: |
| 1247 | continue; |
| 1248 | } |
| 1249 | if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], |
| 1250 | w[3])) |
| 1251 | return false; |
| 1252 | } |
Tetsuo Handa | 475e6fa | 2010-06-24 11:28:14 +0900 | [diff] [blame] | 1253 | head->read_var2 = NULL; |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1254 | return true; |
| 1255 | } |
| 1256 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1257 | /** |
| 1258 | * tomoyo_read_exception_policy - Read exception policy. |
| 1259 | * |
| 1260 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1261 | * |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1262 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1263 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1264 | static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1265 | { |
Tetsuo Handa | 31845e8 | 2010-06-17 16:54:33 +0900 | [diff] [blame] | 1266 | if (head->read_eof) |
| 1267 | return; |
| 1268 | while (head->read_step < TOMOYO_MAX_POLICY && |
| 1269 | tomoyo_read_policy(head, head->read_step)) |
| 1270 | head->read_step++; |
| 1271 | if (head->read_step < TOMOYO_MAX_POLICY) |
| 1272 | return; |
| 1273 | while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP && |
| 1274 | tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY)) |
| 1275 | head->read_step++; |
| 1276 | if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP) |
| 1277 | return; |
| 1278 | head->read_eof = true; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1279 | } |
| 1280 | |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1281 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1282 | * tomoyo_print_header - Get header line of audit log. |
| 1283 | * |
| 1284 | * @r: Pointer to "struct tomoyo_request_info". |
| 1285 | * |
| 1286 | * Returns string representation. |
| 1287 | * |
| 1288 | * This function uses kmalloc(), so caller must kfree() if this function |
| 1289 | * didn't return NULL. |
| 1290 | */ |
| 1291 | static char *tomoyo_print_header(struct tomoyo_request_info *r) |
| 1292 | { |
| 1293 | static const char *tomoyo_mode_4[4] = { |
| 1294 | "disabled", "learning", "permissive", "enforcing" |
| 1295 | }; |
| 1296 | struct timeval tv; |
| 1297 | const pid_t gpid = task_pid_nr(current); |
| 1298 | static const int tomoyo_buffer_len = 4096; |
| 1299 | char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS); |
| 1300 | if (!buffer) |
| 1301 | return NULL; |
| 1302 | do_gettimeofday(&tv); |
| 1303 | snprintf(buffer, tomoyo_buffer_len - 1, |
| 1304 | "#timestamp=%lu profile=%u mode=%s (global-pid=%u)" |
| 1305 | " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u" |
| 1306 | " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }", |
| 1307 | tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid, |
| 1308 | (pid_t) sys_getpid(), (pid_t) sys_getppid(), |
| 1309 | current_uid(), current_gid(), current_euid(), |
| 1310 | current_egid(), current_suid(), current_sgid(), |
| 1311 | current_fsuid(), current_fsgid()); |
| 1312 | return buffer; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * tomoyo_init_audit_log - Allocate buffer for audit logs. |
| 1317 | * |
| 1318 | * @len: Required size. |
| 1319 | * @r: Pointer to "struct tomoyo_request_info". |
| 1320 | * |
| 1321 | * Returns pointer to allocated memory. |
| 1322 | * |
| 1323 | * The @len is updated to add the header lines' size on success. |
| 1324 | * |
| 1325 | * This function uses kzalloc(), so caller must kfree() if this function |
| 1326 | * didn't return NULL. |
| 1327 | */ |
| 1328 | static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r) |
| 1329 | { |
| 1330 | char *buf = NULL; |
| 1331 | const char *header; |
| 1332 | const char *domainname; |
| 1333 | if (!r->domain) |
| 1334 | r->domain = tomoyo_domain(); |
| 1335 | domainname = r->domain->domainname->name; |
| 1336 | header = tomoyo_print_header(r); |
| 1337 | if (!header) |
| 1338 | return NULL; |
| 1339 | *len += strlen(domainname) + strlen(header) + 10; |
| 1340 | buf = kzalloc(*len, GFP_NOFS); |
| 1341 | if (buf) |
| 1342 | snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname); |
| 1343 | kfree(header); |
| 1344 | return buf; |
| 1345 | } |
| 1346 | |
| 1347 | /* Wait queue for tomoyo_query_list. */ |
| 1348 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait); |
| 1349 | |
| 1350 | /* Lock for manipulating tomoyo_query_list. */ |
| 1351 | static DEFINE_SPINLOCK(tomoyo_query_list_lock); |
| 1352 | |
| 1353 | /* Structure for query. */ |
| 1354 | struct tomoyo_query_entry { |
| 1355 | struct list_head list; |
| 1356 | char *query; |
| 1357 | int query_len; |
| 1358 | unsigned int serial; |
| 1359 | int timer; |
| 1360 | int answer; |
| 1361 | }; |
| 1362 | |
| 1363 | /* The list for "struct tomoyo_query_entry". */ |
| 1364 | static LIST_HEAD(tomoyo_query_list); |
| 1365 | |
| 1366 | /* |
| 1367 | * Number of "struct file" referring /sys/kernel/security/tomoyo/query |
| 1368 | * interface. |
| 1369 | */ |
| 1370 | static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); |
| 1371 | |
| 1372 | /** |
| 1373 | * tomoyo_supervisor - Ask for the supervisor's decision. |
| 1374 | * |
| 1375 | * @r: Pointer to "struct tomoyo_request_info". |
| 1376 | * @fmt: The printf()'s format string, followed by parameters. |
| 1377 | * |
| 1378 | * Returns 0 if the supervisor decided to permit the access request which |
| 1379 | * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the |
| 1380 | * supervisor decided to retry the access request which violated the policy in |
| 1381 | * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise. |
| 1382 | */ |
| 1383 | int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) |
| 1384 | { |
| 1385 | va_list args; |
| 1386 | int error = -EPERM; |
| 1387 | int pos; |
| 1388 | int len; |
| 1389 | static unsigned int tomoyo_serial; |
| 1390 | struct tomoyo_query_entry *tomoyo_query_entry = NULL; |
| 1391 | bool quota_exceeded = false; |
| 1392 | char *header; |
| 1393 | switch (r->mode) { |
| 1394 | char *buffer; |
| 1395 | case TOMOYO_CONFIG_LEARNING: |
| 1396 | if (!tomoyo_domain_quota_is_ok(r)) |
| 1397 | return 0; |
| 1398 | va_start(args, fmt); |
| 1399 | len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4; |
| 1400 | va_end(args); |
| 1401 | buffer = kmalloc(len, GFP_NOFS); |
| 1402 | if (!buffer) |
| 1403 | return 0; |
| 1404 | va_start(args, fmt); |
| 1405 | vsnprintf(buffer, len - 1, fmt, args); |
| 1406 | va_end(args); |
| 1407 | tomoyo_normalize_line(buffer); |
| 1408 | tomoyo_write_domain_policy2(buffer, r->domain, false); |
| 1409 | kfree(buffer); |
| 1410 | /* fall through */ |
| 1411 | case TOMOYO_CONFIG_PERMISSIVE: |
| 1412 | return 0; |
| 1413 | } |
| 1414 | if (!r->domain) |
| 1415 | r->domain = tomoyo_domain(); |
| 1416 | if (!atomic_read(&tomoyo_query_observers)) |
| 1417 | return -EPERM; |
| 1418 | va_start(args, fmt); |
| 1419 | len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32; |
| 1420 | va_end(args); |
| 1421 | header = tomoyo_init_audit_log(&len, r); |
| 1422 | if (!header) |
| 1423 | goto out; |
| 1424 | tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS); |
| 1425 | if (!tomoyo_query_entry) |
| 1426 | goto out; |
| 1427 | tomoyo_query_entry->query = kzalloc(len, GFP_NOFS); |
| 1428 | if (!tomoyo_query_entry->query) |
| 1429 | goto out; |
| 1430 | len = ksize(tomoyo_query_entry->query); |
| 1431 | INIT_LIST_HEAD(&tomoyo_query_entry->list); |
| 1432 | spin_lock(&tomoyo_query_list_lock); |
| 1433 | if (tomoyo_quota_for_query && tomoyo_query_memory_size + len + |
| 1434 | sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) { |
| 1435 | quota_exceeded = true; |
| 1436 | } else { |
| 1437 | tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry); |
| 1438 | tomoyo_query_entry->serial = tomoyo_serial++; |
| 1439 | } |
| 1440 | spin_unlock(&tomoyo_query_list_lock); |
| 1441 | if (quota_exceeded) |
| 1442 | goto out; |
| 1443 | pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s", |
| 1444 | tomoyo_query_entry->serial, r->retry, header); |
| 1445 | kfree(header); |
| 1446 | header = NULL; |
| 1447 | va_start(args, fmt); |
| 1448 | vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args); |
| 1449 | tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1; |
| 1450 | va_end(args); |
| 1451 | spin_lock(&tomoyo_query_list_lock); |
| 1452 | list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list); |
| 1453 | spin_unlock(&tomoyo_query_list_lock); |
| 1454 | /* Give 10 seconds for supervisor's opinion. */ |
| 1455 | for (tomoyo_query_entry->timer = 0; |
| 1456 | atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100; |
| 1457 | tomoyo_query_entry->timer++) { |
| 1458 | wake_up(&tomoyo_query_wait); |
| 1459 | set_current_state(TASK_INTERRUPTIBLE); |
| 1460 | schedule_timeout(HZ / 10); |
| 1461 | if (tomoyo_query_entry->answer) |
| 1462 | break; |
| 1463 | } |
| 1464 | spin_lock(&tomoyo_query_list_lock); |
| 1465 | list_del(&tomoyo_query_entry->list); |
| 1466 | tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry); |
| 1467 | spin_unlock(&tomoyo_query_list_lock); |
| 1468 | switch (tomoyo_query_entry->answer) { |
| 1469 | case 3: /* Asked to retry by administrator. */ |
| 1470 | error = TOMOYO_RETRY_REQUEST; |
| 1471 | r->retry++; |
| 1472 | break; |
| 1473 | case 1: |
| 1474 | /* Granted by administrator. */ |
| 1475 | error = 0; |
| 1476 | break; |
| 1477 | case 0: |
| 1478 | /* Timed out. */ |
| 1479 | break; |
| 1480 | default: |
| 1481 | /* Rejected by administrator. */ |
| 1482 | break; |
| 1483 | } |
| 1484 | out: |
| 1485 | if (tomoyo_query_entry) |
| 1486 | kfree(tomoyo_query_entry->query); |
| 1487 | kfree(tomoyo_query_entry); |
| 1488 | kfree(header); |
| 1489 | return error; |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query. |
| 1494 | * |
| 1495 | * @file: Pointer to "struct file". |
| 1496 | * @wait: Pointer to "poll_table". |
| 1497 | * |
| 1498 | * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise. |
| 1499 | * |
| 1500 | * Waits for access requests which violated policy in enforcing mode. |
| 1501 | */ |
| 1502 | static int tomoyo_poll_query(struct file *file, poll_table *wait) |
| 1503 | { |
| 1504 | struct list_head *tmp; |
| 1505 | bool found = false; |
| 1506 | u8 i; |
| 1507 | for (i = 0; i < 2; i++) { |
| 1508 | spin_lock(&tomoyo_query_list_lock); |
| 1509 | list_for_each(tmp, &tomoyo_query_list) { |
| 1510 | struct tomoyo_query_entry *ptr |
| 1511 | = list_entry(tmp, struct tomoyo_query_entry, |
| 1512 | list); |
| 1513 | if (ptr->answer) |
| 1514 | continue; |
| 1515 | found = true; |
| 1516 | break; |
| 1517 | } |
| 1518 | spin_unlock(&tomoyo_query_list_lock); |
| 1519 | if (found) |
| 1520 | return POLLIN | POLLRDNORM; |
| 1521 | if (i) |
| 1522 | break; |
| 1523 | poll_wait(file, &tomoyo_query_wait, wait); |
| 1524 | } |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * tomoyo_read_query - Read access requests which violated policy in enforcing mode. |
| 1530 | * |
| 1531 | * @head: Pointer to "struct tomoyo_io_buffer". |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1532 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1533 | static void tomoyo_read_query(struct tomoyo_io_buffer *head) |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1534 | { |
| 1535 | struct list_head *tmp; |
| 1536 | int pos = 0; |
| 1537 | int len = 0; |
| 1538 | char *buf; |
| 1539 | if (head->read_avail) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1540 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1541 | if (head->read_buf) { |
| 1542 | kfree(head->read_buf); |
| 1543 | head->read_buf = NULL; |
| 1544 | head->readbuf_size = 0; |
| 1545 | } |
| 1546 | spin_lock(&tomoyo_query_list_lock); |
| 1547 | list_for_each(tmp, &tomoyo_query_list) { |
| 1548 | struct tomoyo_query_entry *ptr |
| 1549 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1550 | if (ptr->answer) |
| 1551 | continue; |
| 1552 | if (pos++ != head->read_step) |
| 1553 | continue; |
| 1554 | len = ptr->query_len; |
| 1555 | break; |
| 1556 | } |
| 1557 | spin_unlock(&tomoyo_query_list_lock); |
| 1558 | if (!len) { |
| 1559 | head->read_step = 0; |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1560 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1561 | } |
| 1562 | buf = kzalloc(len, GFP_NOFS); |
| 1563 | if (!buf) |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1564 | return; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1565 | pos = 0; |
| 1566 | spin_lock(&tomoyo_query_list_lock); |
| 1567 | list_for_each(tmp, &tomoyo_query_list) { |
| 1568 | struct tomoyo_query_entry *ptr |
| 1569 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1570 | if (ptr->answer) |
| 1571 | continue; |
| 1572 | if (pos++ != head->read_step) |
| 1573 | continue; |
| 1574 | /* |
| 1575 | * Some query can be skipped because tomoyo_query_list |
| 1576 | * can change, but I don't care. |
| 1577 | */ |
| 1578 | if (len == ptr->query_len) |
| 1579 | memmove(buf, ptr->query, len); |
| 1580 | break; |
| 1581 | } |
| 1582 | spin_unlock(&tomoyo_query_list_lock); |
| 1583 | if (buf[0]) { |
| 1584 | head->read_avail = len; |
| 1585 | head->readbuf_size = head->read_avail; |
| 1586 | head->read_buf = buf; |
| 1587 | head->read_step++; |
| 1588 | } else { |
| 1589 | kfree(buf); |
| 1590 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /** |
| 1594 | * tomoyo_write_answer - Write the supervisor's decision. |
| 1595 | * |
| 1596 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1597 | * |
| 1598 | * Returns 0 on success, -EINVAL otherwise. |
| 1599 | */ |
| 1600 | static int tomoyo_write_answer(struct tomoyo_io_buffer *head) |
| 1601 | { |
| 1602 | char *data = head->write_buf; |
| 1603 | struct list_head *tmp; |
| 1604 | unsigned int serial; |
| 1605 | unsigned int answer; |
| 1606 | spin_lock(&tomoyo_query_list_lock); |
| 1607 | list_for_each(tmp, &tomoyo_query_list) { |
| 1608 | struct tomoyo_query_entry *ptr |
| 1609 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1610 | ptr->timer = 0; |
| 1611 | } |
| 1612 | spin_unlock(&tomoyo_query_list_lock); |
| 1613 | if (sscanf(data, "A%u=%u", &serial, &answer) != 2) |
| 1614 | return -EINVAL; |
| 1615 | spin_lock(&tomoyo_query_list_lock); |
| 1616 | list_for_each(tmp, &tomoyo_query_list) { |
| 1617 | struct tomoyo_query_entry *ptr |
| 1618 | = list_entry(tmp, struct tomoyo_query_entry, list); |
| 1619 | if (ptr->serial != serial) |
| 1620 | continue; |
| 1621 | if (!ptr->answer) |
| 1622 | ptr->answer = answer; |
| 1623 | break; |
| 1624 | } |
| 1625 | spin_unlock(&tomoyo_query_list_lock); |
| 1626 | return 0; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1630 | * tomoyo_read_version: Get version. |
| 1631 | * |
| 1632 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1633 | * |
| 1634 | * Returns version information. |
| 1635 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1636 | static void tomoyo_read_version(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1637 | { |
| 1638 | if (!head->read_eof) { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1639 | tomoyo_io_printf(head, "2.3.0-pre"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1640 | head->read_eof = true; |
| 1641 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /** |
| 1645 | * tomoyo_read_self_domain - Get the current process's domainname. |
| 1646 | * |
| 1647 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 1648 | * |
| 1649 | * Returns the current process's domainname. |
| 1650 | */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1651 | static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1652 | { |
| 1653 | if (!head->read_eof) { |
| 1654 | /* |
| 1655 | * tomoyo_domain()->domainname != NULL |
| 1656 | * because every process belongs to a domain and |
| 1657 | * the domain's name cannot be NULL. |
| 1658 | */ |
| 1659 | tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name); |
| 1660 | head->read_eof = true; |
| 1661 | } |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface. |
| 1666 | * |
| 1667 | * @type: Type of interface. |
| 1668 | * @file: Pointer to "struct file". |
| 1669 | * |
| 1670 | * Associates policy handler and returns 0 on success, -ENOMEM otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1671 | * |
| 1672 | * Caller acquires tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1673 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1674 | int tomoyo_open_control(const u8 type, struct file *file) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1675 | { |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1676 | struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1677 | |
| 1678 | if (!head) |
| 1679 | return -ENOMEM; |
| 1680 | mutex_init(&head->io_sem); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1681 | head->type = type; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1682 | switch (type) { |
| 1683 | case TOMOYO_DOMAINPOLICY: |
| 1684 | /* /sys/kernel/security/tomoyo/domain_policy */ |
| 1685 | head->write = tomoyo_write_domain_policy; |
| 1686 | head->read = tomoyo_read_domain_policy; |
| 1687 | break; |
| 1688 | case TOMOYO_EXCEPTIONPOLICY: |
| 1689 | /* /sys/kernel/security/tomoyo/exception_policy */ |
| 1690 | head->write = tomoyo_write_exception_policy; |
| 1691 | head->read = tomoyo_read_exception_policy; |
| 1692 | break; |
| 1693 | case TOMOYO_SELFDOMAIN: |
| 1694 | /* /sys/kernel/security/tomoyo/self_domain */ |
| 1695 | head->read = tomoyo_read_self_domain; |
| 1696 | break; |
| 1697 | case TOMOYO_DOMAIN_STATUS: |
| 1698 | /* /sys/kernel/security/tomoyo/.domain_status */ |
| 1699 | head->write = tomoyo_write_domain_profile; |
| 1700 | head->read = tomoyo_read_domain_profile; |
| 1701 | break; |
| 1702 | case TOMOYO_PROCESS_STATUS: |
| 1703 | /* /sys/kernel/security/tomoyo/.process_status */ |
| 1704 | head->write = tomoyo_write_pid; |
| 1705 | head->read = tomoyo_read_pid; |
| 1706 | break; |
| 1707 | case TOMOYO_VERSION: |
| 1708 | /* /sys/kernel/security/tomoyo/version */ |
| 1709 | head->read = tomoyo_read_version; |
| 1710 | head->readbuf_size = 128; |
| 1711 | break; |
| 1712 | case TOMOYO_MEMINFO: |
| 1713 | /* /sys/kernel/security/tomoyo/meminfo */ |
| 1714 | head->write = tomoyo_write_memory_quota; |
| 1715 | head->read = tomoyo_read_memory_counter; |
| 1716 | head->readbuf_size = 512; |
| 1717 | break; |
| 1718 | case TOMOYO_PROFILE: |
| 1719 | /* /sys/kernel/security/tomoyo/profile */ |
| 1720 | head->write = tomoyo_write_profile; |
| 1721 | head->read = tomoyo_read_profile; |
| 1722 | break; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1723 | case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */ |
| 1724 | head->poll = tomoyo_poll_query; |
| 1725 | head->write = tomoyo_write_answer; |
| 1726 | head->read = tomoyo_read_query; |
| 1727 | break; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1728 | case TOMOYO_MANAGER: |
| 1729 | /* /sys/kernel/security/tomoyo/manager */ |
| 1730 | head->write = tomoyo_write_manager_policy; |
| 1731 | head->read = tomoyo_read_manager_policy; |
| 1732 | break; |
| 1733 | } |
| 1734 | if (!(file->f_mode & FMODE_READ)) { |
| 1735 | /* |
| 1736 | * No need to allocate read_buf since it is not opened |
| 1737 | * for reading. |
| 1738 | */ |
| 1739 | head->read = NULL; |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1740 | head->poll = NULL; |
| 1741 | } else if (!head->poll) { |
| 1742 | /* Don't allocate read_buf for poll() access. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1743 | if (!head->readbuf_size) |
| 1744 | head->readbuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1745 | head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1746 | if (!head->read_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1747 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1748 | return -ENOMEM; |
| 1749 | } |
| 1750 | } |
| 1751 | if (!(file->f_mode & FMODE_WRITE)) { |
| 1752 | /* |
| 1753 | * No need to allocate write_buf since it is not opened |
| 1754 | * for writing. |
| 1755 | */ |
| 1756 | head->write = NULL; |
| 1757 | } else if (head->write) { |
| 1758 | head->writebuf_size = 4096 * 2; |
Tetsuo Handa | 4e5d6f7 | 2010-04-28 14:17:42 +0900 | [diff] [blame] | 1759 | head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1760 | if (!head->write_buf) { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1761 | kfree(head->read_buf); |
| 1762 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1763 | return -ENOMEM; |
| 1764 | } |
| 1765 | } |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1766 | if (type != TOMOYO_QUERY) |
| 1767 | head->reader_idx = tomoyo_read_lock(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1768 | file->private_data = head; |
| 1769 | /* |
| 1770 | * Call the handler now if the file is |
| 1771 | * /sys/kernel/security/tomoyo/self_domain |
| 1772 | * so that the user can use |
| 1773 | * cat < /sys/kernel/security/tomoyo/self_domain" |
| 1774 | * to know the current process's domainname. |
| 1775 | */ |
| 1776 | if (type == TOMOYO_SELFDOMAIN) |
| 1777 | tomoyo_read_control(file, NULL, 0); |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1778 | /* |
| 1779 | * If the file is /sys/kernel/security/tomoyo/query , increment the |
| 1780 | * observer counter. |
| 1781 | * The obserber counter is used by tomoyo_supervisor() to see if |
| 1782 | * there is some process monitoring /sys/kernel/security/tomoyo/query. |
| 1783 | */ |
| 1784 | else if (type == TOMOYO_QUERY) |
| 1785 | atomic_inc(&tomoyo_query_observers); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | /** |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1790 | * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface. |
| 1791 | * |
| 1792 | * @file: Pointer to "struct file". |
| 1793 | * @wait: Pointer to "poll_table". |
| 1794 | * |
| 1795 | * Waits for read readiness. |
| 1796 | * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd . |
| 1797 | */ |
| 1798 | int tomoyo_poll_control(struct file *file, poll_table *wait) |
| 1799 | { |
| 1800 | struct tomoyo_io_buffer *head = file->private_data; |
| 1801 | if (!head->poll) |
| 1802 | return -ENOSYS; |
| 1803 | return head->poll(file, wait); |
| 1804 | } |
| 1805 | |
| 1806 | /** |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1807 | * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface. |
| 1808 | * |
| 1809 | * @file: Pointer to "struct file". |
| 1810 | * @buffer: Poiner to buffer to write to. |
| 1811 | * @buffer_len: Size of @buffer. |
| 1812 | * |
| 1813 | * Returns bytes read on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1814 | * |
| 1815 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1816 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1817 | int tomoyo_read_control(struct file *file, char __user *buffer, |
| 1818 | const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1819 | { |
| 1820 | int len = 0; |
| 1821 | struct tomoyo_io_buffer *head = file->private_data; |
| 1822 | char *cp; |
| 1823 | |
| 1824 | if (!head->read) |
| 1825 | return -ENOSYS; |
| 1826 | if (mutex_lock_interruptible(&head->io_sem)) |
| 1827 | return -EINTR; |
| 1828 | /* Call the policy handler. */ |
Tetsuo Handa | 8fbe71f | 2010-06-16 16:29:59 +0900 | [diff] [blame] | 1829 | head->read(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1830 | if (len < 0) |
| 1831 | goto out; |
| 1832 | /* Write to buffer. */ |
| 1833 | len = head->read_avail; |
| 1834 | if (len > buffer_len) |
| 1835 | len = buffer_len; |
| 1836 | if (!len) |
| 1837 | goto out; |
| 1838 | /* head->read_buf changes by some functions. */ |
| 1839 | cp = head->read_buf; |
| 1840 | if (copy_to_user(buffer, cp, len)) { |
| 1841 | len = -EFAULT; |
| 1842 | goto out; |
| 1843 | } |
| 1844 | head->read_avail -= len; |
| 1845 | memmove(cp, cp + len, head->read_avail); |
| 1846 | out: |
| 1847 | mutex_unlock(&head->io_sem); |
| 1848 | return len; |
| 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface. |
| 1853 | * |
| 1854 | * @file: Pointer to "struct file". |
| 1855 | * @buffer: Pointer to buffer to read from. |
| 1856 | * @buffer_len: Size of @buffer. |
| 1857 | * |
| 1858 | * Returns @buffer_len on success, negative value otherwise. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1859 | * |
| 1860 | * Caller holds tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1861 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1862 | int tomoyo_write_control(struct file *file, const char __user *buffer, |
| 1863 | const int buffer_len) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1864 | { |
| 1865 | struct tomoyo_io_buffer *head = file->private_data; |
| 1866 | int error = buffer_len; |
| 1867 | int avail_len = buffer_len; |
| 1868 | char *cp0 = head->write_buf; |
| 1869 | |
| 1870 | if (!head->write) |
| 1871 | return -ENOSYS; |
| 1872 | if (!access_ok(VERIFY_READ, buffer, buffer_len)) |
| 1873 | return -EFAULT; |
| 1874 | /* Don't allow updating policies by non manager programs. */ |
| 1875 | if (head->write != tomoyo_write_pid && |
| 1876 | head->write != tomoyo_write_domain_policy && |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 1877 | !tomoyo_policy_manager()) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1878 | return -EPERM; |
| 1879 | if (mutex_lock_interruptible(&head->io_sem)) |
| 1880 | return -EINTR; |
| 1881 | /* Read a line and dispatch it to the policy handler. */ |
| 1882 | while (avail_len > 0) { |
| 1883 | char c; |
| 1884 | if (head->write_avail >= head->writebuf_size - 1) { |
| 1885 | error = -ENOMEM; |
| 1886 | break; |
| 1887 | } else if (get_user(c, buffer)) { |
| 1888 | error = -EFAULT; |
| 1889 | break; |
| 1890 | } |
| 1891 | buffer++; |
| 1892 | avail_len--; |
| 1893 | cp0[head->write_avail++] = c; |
| 1894 | if (c != '\n') |
| 1895 | continue; |
| 1896 | cp0[head->write_avail - 1] = '\0'; |
| 1897 | head->write_avail = 0; |
| 1898 | tomoyo_normalize_line(cp0); |
| 1899 | head->write(head); |
| 1900 | } |
| 1901 | mutex_unlock(&head->io_sem); |
| 1902 | return error; |
| 1903 | } |
| 1904 | |
| 1905 | /** |
| 1906 | * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface. |
| 1907 | * |
| 1908 | * @file: Pointer to "struct file". |
| 1909 | * |
| 1910 | * Releases memory and returns 0. |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 1911 | * |
| 1912 | * Caller looses tomoyo_read_lock(). |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1913 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1914 | int tomoyo_close_control(struct file *file) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1915 | { |
| 1916 | struct tomoyo_io_buffer *head = file->private_data; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 1917 | const bool is_write = !!head->write_buf; |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1918 | |
Tetsuo Handa | 17fcfbd | 2010-05-17 10:11:36 +0900 | [diff] [blame] | 1919 | /* |
| 1920 | * If the file is /sys/kernel/security/tomoyo/query , decrement the |
| 1921 | * observer counter. |
| 1922 | */ |
| 1923 | if (head->type == TOMOYO_QUERY) |
| 1924 | atomic_dec(&tomoyo_query_observers); |
| 1925 | else |
| 1926 | tomoyo_read_unlock(head->reader_idx); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1927 | /* Release memory used for policy I/O. */ |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1928 | kfree(head->read_buf); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1929 | head->read_buf = NULL; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1930 | kfree(head->write_buf); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1931 | head->write_buf = NULL; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 1932 | kfree(head); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1933 | head = NULL; |
| 1934 | file->private_data = NULL; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 1935 | if (is_write) |
| 1936 | tomoyo_run_gc(); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1941 | * tomoyo_check_profile - Check all profiles currently assigned to domains are defined. |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1942 | */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1943 | void tomoyo_check_profile(void) |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1944 | { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1945 | struct tomoyo_domain_info *domain; |
| 1946 | const int idx = tomoyo_read_lock(); |
| 1947 | tomoyo_policy_loaded = true; |
| 1948 | /* Check all profiles currently assigned to domains are defined. */ |
| 1949 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 1950 | const u8 profile = domain->profile; |
| 1951 | if (tomoyo_profile_ptr[profile]) |
| 1952 | continue; |
| 1953 | panic("Profile %u (used by '%s') not defined.\n", |
| 1954 | profile, domain->domainname->name); |
| 1955 | } |
| 1956 | tomoyo_read_unlock(idx); |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 1957 | if (tomoyo_profile_version != 20090903) |
| 1958 | panic("Profile version %u is not supported.\n", |
| 1959 | tomoyo_profile_version); |
| 1960 | printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n"); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1961 | printk(KERN_INFO "Mandatory Access Control activated.\n"); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 1962 | } |