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