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