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