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