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