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