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