blob: 6c68981c0f5f51b70e47fb6098a3b262497b1ad3 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda95908372009-02-05 17:18:13 +09007 */
8
9#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090011#include <linux/security.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090012#include "common.h"
Kentaro Takeda95908372009-02-05 17:18:13 +090013
Tetsuo Handa57c25902010-06-03 20:38:44 +090014static 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. */
25static unsigned int tomoyo_profile_version;
26
27/* Profile table. Memory is allocated as needed. */
28static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
29
Kentaro Takeda95908372009-02-05 17:18:13 +090030/* String table for functionality that takes 4 modes. */
31static const char *tomoyo_mode_4[4] = {
32 "disabled", "learning", "permissive", "enforcing"
33};
Kentaro Takeda95908372009-02-05 17:18:13 +090034
Tetsuo Handa57c25902010-06-03 20:38:44 +090035/* String table for /sys/kernel/security/tomoyo/profile */
36static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
38 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
39 [TOMOYO_MAC_FILE_OPEN] = "file::open",
40 [TOMOYO_MAC_FILE_CREATE] = "file::create",
41 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
42 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
43 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
44 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
45 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
46 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
47 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
48 [TOMOYO_MAC_FILE_REWRITE] = "file::rewrite",
49 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
50 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
51 [TOMOYO_MAC_FILE_LINK] = "file::link",
52 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
53 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
54 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
55 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
56 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
57 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
58 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
59 [TOMOYO_MAC_FILE_UMOUNT] = "file::umount",
60 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
61 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090062};
63
Kentaro Takeda95908372009-02-05 17:18:13 +090064/* Permit policy management by non-root user? */
65static bool tomoyo_manage_by_non_root;
66
67/* Utility functions. */
68
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090069/**
Tetsuo Handa57c25902010-06-03 20:38:44 +090070 * tomoyo_yesno - Return "yes" or "no".
71 *
72 * @value: Bool value.
73 */
74static const char *tomoyo_yesno(const unsigned int value)
75{
76 return value ? "yes" : "no";
77}
78
79/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090080 * 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 */
87static 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
99/**
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900100 * 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 */
107bool 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
149/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900150 * 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 */
159bool 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
177/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900178 * 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 */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900184static struct tomoyo_profile *tomoyo_find_or_assign_new_profile
185(const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900186{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900187 struct tomoyo_profile *ptr;
188 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900189 if (profile >= TOMOYO_MAX_PROFILES)
190 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900191 ptr = tomoyo_profile_ptr[profile];
192 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900193 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;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900209 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900210 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900211 out:
212 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900213 return ptr;
214}
215
216/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900217 * tomoyo_profile - Find a profile.
218 *
219 * @profile: Profile number to find.
220 *
221 * Returns pointer to "struct tomoyo_profile".
222 */
223struct 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.
Kentaro Takeda95908372009-02-05 17:18:13 +0900234 *
235 * @head: Pointer to "struct tomoyo_io_buffer".
236 *
237 * Returns 0 on success, negative value otherwise.
238 */
239static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
240{
241 char *data = head->write_buf;
242 unsigned int i;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900243 int value;
244 int mode;
245 u8 config;
246 bool use_default = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900247 char *cp;
248 struct tomoyo_profile *profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900249 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;
Kentaro Takeda95908372009-02-05 17:18:13 +0900257 data = cp + 1;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900258 profile = tomoyo_find_or_assign_new_profile(i);
259 if (!profile)
260 return -EINVAL;
261 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900262 cp = strchr(data, '=');
263 if (!cp)
264 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900265 *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;
Kentaro Takeda95908372009-02-05 17:18:13 +0900311 if (!strcmp(data, "COMMENT")) {
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900312 const struct tomoyo_path_info *old_comment = profile->comment;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900313 profile->comment = tomoyo_get_name(cp);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900314 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900315 return 0;
316 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900317 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;
Kentaro Takeda95908372009-02-05 17:18:13 +0900327 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900328 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
329 return -EINVAL;
330 } else {
331 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900332 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900333 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;
Kentaro Takeda95908372009-02-05 17:18:13 +0900349}
350
351/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900352 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900353 *
354 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +0900355 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900356static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900357{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900358 int index;
Kentaro Takeda95908372009-02-05 17:18:13 +0900359 if (head->read_eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900360 return;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900361 if (head->read_bit)
362 goto body;
363 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
364 tomoyo_io_printf(head, "PREFERENCE::learning={ verbose=%s "
365 "max_entry=%u }\n",
366 tomoyo_yesno(tomoyo_default_profile.preference.
367 learning_verbose),
368 tomoyo_default_profile.preference.learning_max_entry);
369 tomoyo_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",
370 tomoyo_yesno(tomoyo_default_profile.preference.
371 permissive_verbose));
372 tomoyo_io_printf(head, "PREFERENCE::enforcing={ verbose=%s }\n",
373 tomoyo_yesno(tomoyo_default_profile.preference.
374 enforcing_verbose));
375 head->read_bit = 1;
376 body:
377 for (index = head->read_step; index < TOMOYO_MAX_PROFILES; index++) {
378 bool done;
379 u8 config;
380 int i;
381 int pos;
Kentaro Takeda95908372009-02-05 17:18:13 +0900382 const struct tomoyo_profile *profile
383 = tomoyo_profile_ptr[index];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900384 const struct tomoyo_path_info *comment;
385 head->read_step = index;
Kentaro Takeda95908372009-02-05 17:18:13 +0900386 if (!profile)
387 continue;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900388 pos = head->read_avail;
389 comment = profile->comment;
390 done = tomoyo_io_printf(head, "%u-COMMENT=%s\n", index,
391 comment ? comment->name : "");
392 if (!done)
393 goto out;
394 config = profile->default_config;
395 if (!tomoyo_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,
396 tomoyo_mode_4[config & 3]))
397 goto out;
398 for (i = 0; i < TOMOYO_MAX_MAC_INDEX +
399 TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
400 config = profile->config[i];
401 if (config == TOMOYO_CONFIG_USE_DEFAULT)
402 continue;
403 if (!tomoyo_io_printf(head,
404 "%u-CONFIG::%s={ mode=%s }\n",
405 index, tomoyo_mac_keywords[i],
406 tomoyo_mode_4[config & 3]))
407 goto out;
Kentaro Takeda95908372009-02-05 17:18:13 +0900408 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900409 if (profile->learning != &tomoyo_default_profile.preference &&
410 !tomoyo_io_printf(head, "%u-PREFERENCE::learning={ "
411 "verbose=%s max_entry=%u }\n", index,
412 tomoyo_yesno(profile->preference.
413 learning_verbose),
414 profile->preference.learning_max_entry))
415 goto out;
416 if (profile->permissive != &tomoyo_default_profile.preference
417 && !tomoyo_io_printf(head, "%u-PREFERENCE::permissive={ "
418 "verbose=%s }\n", index,
419 tomoyo_yesno(profile->preference.
420 permissive_verbose)))
421 goto out;
422 if (profile->enforcing != &tomoyo_default_profile.preference &&
423 !tomoyo_io_printf(head, "%u-PREFERENCE::enforcing={ "
424 "verbose=%s }\n", index,
425 tomoyo_yesno(profile->preference.
426 enforcing_verbose)))
427 goto out;
428 continue;
429 out:
430 head->read_avail = pos;
431 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900432 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900433 if (index == TOMOYO_MAX_PROFILES)
Kentaro Takeda95908372009-02-05 17:18:13 +0900434 head->read_eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900435}
436
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900437static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a,
438 const struct tomoyo_acl_head *b)
439{
440 return container_of(a, struct tomoyo_policy_manager_entry, head)
441 ->manager ==
442 container_of(b, struct tomoyo_policy_manager_entry, head)
443 ->manager;
444}
445
Kentaro Takeda95908372009-02-05 17:18:13 +0900446/**
447 * tomoyo_update_manager_entry - Add a manager entry.
448 *
449 * @manager: The path to manager or the domainnamme.
450 * @is_delete: True if it is a delete request.
451 *
452 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900453 *
454 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900455 */
456static int tomoyo_update_manager_entry(const char *manager,
457 const bool is_delete)
458{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900459 struct tomoyo_policy_manager_entry e = { };
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900460 int error;
Kentaro Takeda95908372009-02-05 17:18:13 +0900461
Tetsuo Handa75093152010-06-16 16:23:55 +0900462 if (tomoyo_domain_def(manager)) {
463 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900464 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900465 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900466 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900467 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900468 return -EINVAL;
469 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900470 e.manager = tomoyo_get_name(manager);
471 if (!e.manager)
Kentaro Takeda95908372009-02-05 17:18:13 +0900472 return -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900473 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900474 &tomoyo_policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900475 tomoyo_same_manager_entry);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900476 tomoyo_put_name(e.manager);
Kentaro Takeda95908372009-02-05 17:18:13 +0900477 return error;
478}
479
480/**
481 * tomoyo_write_manager_policy - Write manager policy.
482 *
483 * @head: Pointer to "struct tomoyo_io_buffer".
484 *
485 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900486 *
487 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900488 */
489static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
490{
491 char *data = head->write_buf;
492 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
493
494 if (!strcmp(data, "manage_by_non_root")) {
495 tomoyo_manage_by_non_root = !is_delete;
496 return 0;
497 }
498 return tomoyo_update_manager_entry(data, is_delete);
499}
500
501/**
502 * tomoyo_read_manager_policy - Read manager policy.
503 *
504 * @head: Pointer to "struct tomoyo_io_buffer".
505 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900506 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900507 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900508static void tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900509{
Kentaro Takeda95908372009-02-05 17:18:13 +0900510 bool done = true;
511
512 if (head->read_eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900513 return;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900514 list_for_each_cookie(head->read_var2,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900515 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900516 struct tomoyo_policy_manager_entry *ptr =
517 list_entry(head->read_var2, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900518 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900519 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900520 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
521 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +0900522 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900523 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900524 head->read_eof = done;
Kentaro Takeda95908372009-02-05 17:18:13 +0900525}
526
527/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900528 * tomoyo_policy_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900529 *
530 * Returns true if the current process is permitted to modify policy
531 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900532 *
533 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900534 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900535static bool tomoyo_policy_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900536{
537 struct tomoyo_policy_manager_entry *ptr;
538 const char *exe;
539 const struct task_struct *task = current;
540 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
541 bool found = false;
542
543 if (!tomoyo_policy_loaded)
544 return true;
545 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
546 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900547 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
548 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900549 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900550 && !tomoyo_pathcmp(domainname, ptr->manager)) {
551 found = true;
552 break;
553 }
554 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900555 if (found)
556 return true;
557 exe = tomoyo_get_exe();
558 if (!exe)
559 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900560 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
561 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900562 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900563 && !strcmp(exe, ptr->manager->name)) {
564 found = true;
565 break;
566 }
567 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900568 if (!found) { /* Reduce error messages. */
569 static pid_t last_pid;
570 const pid_t pid = current->pid;
571 if (last_pid != pid) {
572 printk(KERN_WARNING "%s ( %s ) is not permitted to "
573 "update policies.\n", domainname->name, exe);
574 last_pid = pid;
575 }
576 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900577 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900578 return found;
579}
580
581/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900582 * tomoyo_select_one - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900583 *
584 * @head: Pointer to "struct tomoyo_io_buffer".
585 * @data: String to parse.
586 *
587 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900588 *
589 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900590 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900591static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900592{
593 unsigned int pid;
594 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900595 bool global_pid = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900596
Tetsuo Handa063821c2010-06-24 12:00:25 +0900597 if (!strcmp(data, "allow_execute")) {
598 head->print_execute_only = true;
599 return true;
600 }
Tetsuo Handa9b244372010-06-03 20:35:53 +0900601 if (sscanf(data, "pid=%u", &pid) == 1 ||
602 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900603 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900604 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900605 read_lock(&tasklist_lock);
Tetsuo Handa9b244372010-06-03 20:35:53 +0900606 if (global_pid)
607 p = find_task_by_pid_ns(pid, &init_pid_ns);
608 else
609 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900610 if (p)
611 domain = tomoyo_real_domain(p);
612 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900613 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900614 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900615 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900616 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900617 } else
618 return false;
619 head->write_var1 = domain;
620 /* Accessing read_buf is safe because head->io_sem is held. */
621 if (!head->read_buf)
622 return true; /* Do nothing if open(O_WRONLY). */
623 head->read_avail = 0;
624 tomoyo_io_printf(head, "# select %s\n", data);
625 head->read_single_domain = true;
626 head->read_eof = !domain;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900627 head->read_var1 = &domain->list;
628 head->read_var2 = NULL;
629 head->read_bit = 0;
630 head->read_step = 0;
631 if (domain && domain->is_deleted)
632 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900633 return true;
634}
635
636/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900637 * tomoyo_delete_domain - Delete a domain.
638 *
639 * @domainname: The name of domain.
640 *
641 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900642 *
643 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900644 */
645static int tomoyo_delete_domain(char *domainname)
646{
647 struct tomoyo_domain_info *domain;
648 struct tomoyo_path_info name;
649
650 name.name = domainname;
651 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +0900652 if (mutex_lock_interruptible(&tomoyo_policy_lock))
653 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900654 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900655 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900656 /* Never delete tomoyo_kernel_domain */
657 if (domain == &tomoyo_kernel_domain)
658 continue;
659 if (domain->is_deleted ||
660 tomoyo_pathcmp(domain->domainname, &name))
661 continue;
662 domain->is_deleted = true;
663 break;
664 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900665 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900666 return 0;
667}
668
669/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900670 * tomoyo_write_domain_policy2 - Write domain policy.
671 *
672 * @head: Pointer to "struct tomoyo_io_buffer".
673 *
674 * Returns 0 on success, negative value otherwise.
675 *
676 * Caller holds tomoyo_read_lock().
677 */
678static int tomoyo_write_domain_policy2(char *data,
679 struct tomoyo_domain_info *domain,
680 const bool is_delete)
681{
682 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
683 return tomoyo_write_mount_policy(data, domain, is_delete);
684 return tomoyo_write_file_policy(data, domain, is_delete);
685}
686
687/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900688 * tomoyo_write_domain_policy - Write domain policy.
689 *
690 * @head: Pointer to "struct tomoyo_io_buffer".
691 *
692 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900693 *
694 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900695 */
696static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
697{
698 char *data = head->write_buf;
699 struct tomoyo_domain_info *domain = head->write_var1;
700 bool is_delete = false;
701 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900702 unsigned int profile;
703
704 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
705 is_delete = true;
706 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
707 is_select = true;
Tetsuo Handa75093152010-06-16 16:23:55 +0900708 if (is_select && tomoyo_select_one(head, data))
Kentaro Takeda95908372009-02-05 17:18:13 +0900709 return 0;
710 /* Don't allow updating policies by non manager programs. */
Tetsuo Handa75093152010-06-16 16:23:55 +0900711 if (!tomoyo_policy_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +0900712 return -EPERM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900713 if (tomoyo_domain_def(data)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900714 domain = NULL;
715 if (is_delete)
716 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900717 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +0900718 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900719 else
Kentaro Takeda95908372009-02-05 17:18:13 +0900720 domain = tomoyo_find_or_assign_new_domain(data, 0);
721 head->write_var1 = domain;
722 return 0;
723 }
724 if (!domain)
725 return -EINVAL;
726
727 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
728 && profile < TOMOYO_MAX_PROFILES) {
729 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
730 domain->profile = (u8) profile;
731 return 0;
732 }
733 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900734 domain->ignore_global_allow_read = !is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +0900735 return 0;
736 }
Tetsuo Handa9b244372010-06-03 20:35:53 +0900737 if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
738 domain->quota_warned = !is_delete;
739 return 0;
740 }
741 if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
742 domain->transition_failed = !is_delete;
743 return 0;
744 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900745 return tomoyo_write_domain_policy2(data, domain, is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900746}
747
748/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900749 * tomoyo_print_path_acl - Print a single path ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900750 *
751 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900752 * @ptr: Pointer to "struct tomoyo_path_acl".
Kentaro Takeda95908372009-02-05 17:18:13 +0900753 *
754 * Returns true on success, false otherwise.
755 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900756static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
757 struct tomoyo_path_acl *ptr)
Kentaro Takeda95908372009-02-05 17:18:13 +0900758{
759 int pos;
760 u8 bit;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900761 const u16 perm = ptr->perm;
Kentaro Takeda95908372009-02-05 17:18:13 +0900762
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900763 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900764 if (!(perm & (1 << bit)))
765 continue;
Tetsuo Handa063821c2010-06-24 12:00:25 +0900766 if (head->print_execute_only && bit != TOMOYO_TYPE_EXECUTE)
767 continue;
Kentaro Takeda95908372009-02-05 17:18:13 +0900768 /* Print "read/write" instead of "read" and "write". */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900769 if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
770 && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Kentaro Takeda95908372009-02-05 17:18:13 +0900771 continue;
Kentaro Takeda95908372009-02-05 17:18:13 +0900772 pos = head->read_avail;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900773 if (!tomoyo_io_printf(head, "allow_%s ",
Tetsuo Handa71c28232010-06-16 16:26:38 +0900774 tomoyo_path_keyword[bit]) ||
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900775 !tomoyo_print_name_union(head, &ptr->name) ||
776 !tomoyo_io_printf(head, "\n"))
Kentaro Takeda95908372009-02-05 17:18:13 +0900777 goto out;
778 }
779 head->read_bit = 0;
780 return true;
781 out:
782 head->read_bit = bit;
783 head->read_avail = pos;
784 return false;
785}
786
787/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900788 * tomoyo_print_path2_acl - Print a double path ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900789 *
790 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900791 * @ptr: Pointer to "struct tomoyo_path2_acl".
Kentaro Takeda95908372009-02-05 17:18:13 +0900792 *
793 * Returns true on success, false otherwise.
794 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900795static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
796 struct tomoyo_path2_acl *ptr)
Kentaro Takeda95908372009-02-05 17:18:13 +0900797{
798 int pos;
Kentaro Takeda95908372009-02-05 17:18:13 +0900799 const u8 perm = ptr->perm;
800 u8 bit;
801
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900802 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900803 if (!(perm & (1 << bit)))
804 continue;
Kentaro Takeda95908372009-02-05 17:18:13 +0900805 pos = head->read_avail;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900806 if (!tomoyo_io_printf(head, "allow_%s ",
Tetsuo Handa71c28232010-06-16 16:26:38 +0900807 tomoyo_path2_keyword[bit]) ||
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900808 !tomoyo_print_name_union(head, &ptr->name1) ||
809 !tomoyo_print_name_union(head, &ptr->name2) ||
810 !tomoyo_io_printf(head, "\n"))
Kentaro Takeda95908372009-02-05 17:18:13 +0900811 goto out;
812 }
813 head->read_bit = 0;
814 return true;
815 out:
816 head->read_bit = bit;
817 head->read_avail = pos;
818 return false;
819}
820
821/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900822 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
823 *
824 * @head: Pointer to "struct tomoyo_io_buffer".
825 * @ptr: Pointer to "struct tomoyo_path_number_acl".
826 *
827 * Returns true on success, false otherwise.
828 */
829static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
830 struct tomoyo_path_number_acl *ptr)
831{
832 int pos;
833 u8 bit;
834 const u8 perm = ptr->perm;
835 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
836 bit++) {
837 if (!(perm & (1 << bit)))
838 continue;
839 pos = head->read_avail;
840 if (!tomoyo_io_printf(head, "allow_%s",
Tetsuo Handa71c28232010-06-16 16:26:38 +0900841 tomoyo_path_number_keyword[bit]) ||
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900842 !tomoyo_print_name_union(head, &ptr->name) ||
843 !tomoyo_print_number_union(head, &ptr->number) ||
844 !tomoyo_io_printf(head, "\n"))
845 goto out;
846 }
847 head->read_bit = 0;
848 return true;
849 out:
850 head->read_bit = bit;
851 head->read_avail = pos;
852 return false;
853}
854
855/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900856 * tomoyo_print_mkdev_acl - Print a mkdev ACL entry.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900857 *
858 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa75093152010-06-16 16:23:55 +0900859 * @ptr: Pointer to "struct tomoyo_mkdev_acl".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900860 *
861 * Returns true on success, false otherwise.
862 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900863static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer *head,
864 struct tomoyo_mkdev_acl *ptr)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900865{
866 int pos;
867 u8 bit;
868 const u16 perm = ptr->perm;
Tetsuo Handa75093152010-06-16 16:23:55 +0900869 for (bit = head->read_bit; bit < TOMOYO_MAX_MKDEV_OPERATION;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900870 bit++) {
871 if (!(perm & (1 << bit)))
872 continue;
873 pos = head->read_avail;
874 if (!tomoyo_io_printf(head, "allow_%s",
Tetsuo Handa71c28232010-06-16 16:26:38 +0900875 tomoyo_mkdev_keyword[bit]) ||
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900876 !tomoyo_print_name_union(head, &ptr->name) ||
877 !tomoyo_print_number_union(head, &ptr->mode) ||
878 !tomoyo_print_number_union(head, &ptr->major) ||
879 !tomoyo_print_number_union(head, &ptr->minor) ||
880 !tomoyo_io_printf(head, "\n"))
881 goto out;
882 }
883 head->read_bit = 0;
884 return true;
885 out:
886 head->read_bit = bit;
887 head->read_avail = pos;
888 return false;
889}
890
891/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900892 * tomoyo_print_mount_acl - Print a mount ACL entry.
893 *
894 * @head: Pointer to "struct tomoyo_io_buffer".
895 * @ptr: Pointer to "struct tomoyo_mount_acl".
896 *
897 * Returns true on success, false otherwise.
898 */
899static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
900 struct tomoyo_mount_acl *ptr)
901{
902 const int pos = head->read_avail;
903 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
904 !tomoyo_print_name_union(head, &ptr->dev_name) ||
905 !tomoyo_print_name_union(head, &ptr->dir_name) ||
906 !tomoyo_print_name_union(head, &ptr->fs_type) ||
907 !tomoyo_print_number_union(head, &ptr->flags) ||
908 !tomoyo_io_printf(head, "\n")) {
909 head->read_avail = pos;
910 return false;
911 }
912 return true;
913}
914
915/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900916 * tomoyo_print_entry - Print an ACL entry.
917 *
918 * @head: Pointer to "struct tomoyo_io_buffer".
919 * @ptr: Pointer to an ACL entry.
920 *
921 * Returns true on success, false otherwise.
922 */
923static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
924 struct tomoyo_acl_info *ptr)
925{
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900926 const u8 acl_type = ptr->type;
Kentaro Takeda95908372009-02-05 17:18:13 +0900927
Tetsuo Handa237ab452010-06-12 20:46:22 +0900928 if (ptr->is_deleted)
929 return true;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900930 if (acl_type == TOMOYO_TYPE_PATH_ACL) {
931 struct tomoyo_path_acl *acl
932 = container_of(ptr, struct tomoyo_path_acl, head);
933 return tomoyo_print_path_acl(head, acl);
Kentaro Takeda95908372009-02-05 17:18:13 +0900934 }
Tetsuo Handa063821c2010-06-24 12:00:25 +0900935 if (head->print_execute_only)
936 return true;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900937 if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
938 struct tomoyo_path2_acl *acl
939 = container_of(ptr, struct tomoyo_path2_acl, head);
940 return tomoyo_print_path2_acl(head, acl);
Kentaro Takeda95908372009-02-05 17:18:13 +0900941 }
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900942 if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
943 struct tomoyo_path_number_acl *acl
944 = container_of(ptr, struct tomoyo_path_number_acl,
945 head);
946 return tomoyo_print_path_number_acl(head, acl);
947 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900948 if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
949 struct tomoyo_mkdev_acl *acl
950 = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900951 head);
Tetsuo Handa75093152010-06-16 16:23:55 +0900952 return tomoyo_print_mkdev_acl(head, acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900953 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900954 if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
955 struct tomoyo_mount_acl *acl
956 = container_of(ptr, struct tomoyo_mount_acl, head);
957 return tomoyo_print_mount_acl(head, acl);
958 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900959 BUG(); /* This must not happen. */
960 return false;
961}
962
963/**
964 * tomoyo_read_domain_policy - Read domain policy.
965 *
966 * @head: Pointer to "struct tomoyo_io_buffer".
967 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900968 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900969 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900970static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900971{
Kentaro Takeda95908372009-02-05 17:18:13 +0900972 bool done = true;
973
974 if (head->read_eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900975 return;
Kentaro Takeda95908372009-02-05 17:18:13 +0900976 if (head->read_step == 0)
977 head->read_step = 1;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900978 list_for_each_cookie(head->read_var1, &tomoyo_domain_list) {
979 struct tomoyo_domain_info *domain =
980 list_entry(head->read_var1, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +0900981 const char *quota_exceeded = "";
982 const char *transition_failed = "";
983 const char *ignore_global_allow_read = "";
Kentaro Takeda95908372009-02-05 17:18:13 +0900984 if (head->read_step != 1)
985 goto acl_loop;
986 if (domain->is_deleted && !head->read_single_domain)
987 continue;
988 /* Print domainname and flags. */
989 if (domain->quota_warned)
990 quota_exceeded = "quota_exceeded\n";
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900991 if (domain->transition_failed)
Kentaro Takeda95908372009-02-05 17:18:13 +0900992 transition_failed = "transition_failed\n";
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900993 if (domain->ignore_global_allow_read)
Kentaro Takeda95908372009-02-05 17:18:13 +0900994 ignore_global_allow_read
995 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900996 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
997 "%u\n%s%s%s\n",
998 domain->domainname->name,
999 domain->profile, quota_exceeded,
1000 transition_failed,
1001 ignore_global_allow_read);
1002 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001003 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001004 head->read_step = 2;
1005acl_loop:
1006 if (head->read_step == 3)
1007 goto tail_mark;
1008 /* Print ACL entries in the domain. */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001009 list_for_each_cookie(head->read_var2,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001010 &domain->acl_info_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001011 struct tomoyo_acl_info *ptr =
1012 list_entry(head->read_var2, typeof(*ptr), list);
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001013 done = tomoyo_print_entry(head, ptr);
1014 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001015 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001016 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001017 if (!done)
1018 break;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001019 head->read_var2 = NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +09001020 head->read_step = 3;
1021tail_mark:
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001022 done = tomoyo_io_printf(head, "\n");
1023 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001024 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001025 head->read_step = 1;
1026 if (head->read_single_domain)
1027 break;
1028 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001029 head->read_eof = done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001030}
1031
1032/**
1033 * tomoyo_write_domain_profile - Assign profile for specified domain.
1034 *
1035 * @head: Pointer to "struct tomoyo_io_buffer".
1036 *
1037 * Returns 0 on success, -EINVAL otherwise.
1038 *
1039 * This is equivalent to doing
1040 *
1041 * ( echo "select " $domainname; echo "use_profile " $profile ) |
Tetsuo Handa9b244372010-06-03 20:35:53 +09001042 * /usr/sbin/tomoyo-loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001043 *
1044 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001045 */
1046static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1047{
1048 char *data = head->write_buf;
1049 char *cp = strchr(data, ' ');
1050 struct tomoyo_domain_info *domain;
1051 unsigned long profile;
1052
1053 if (!cp)
1054 return -EINVAL;
1055 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001056 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001057 if (strict_strtoul(data, 10, &profile))
1058 return -EINVAL;
1059 if (domain && profile < TOMOYO_MAX_PROFILES
1060 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1061 domain->profile = (u8) profile;
1062 return 0;
1063}
1064
1065/**
1066 * tomoyo_read_domain_profile - Read only domainname and profile.
1067 *
1068 * @head: Pointer to "struct tomoyo_io_buffer".
1069 *
1070 * Returns list of profile number and domainname pairs.
1071 *
1072 * This is equivalent to doing
1073 *
1074 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1075 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1076 * domainname = $0; } else if ( $1 == "use_profile" ) {
1077 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001078 *
1079 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001080 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001081static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001082{
Kentaro Takeda95908372009-02-05 17:18:13 +09001083 bool done = true;
1084
1085 if (head->read_eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001086 return;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001087 list_for_each_cookie(head->read_var1, &tomoyo_domain_list) {
1088 struct tomoyo_domain_info *domain =
1089 list_entry(head->read_var1, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001090 if (domain->is_deleted)
1091 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001092 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1093 domain->domainname->name);
1094 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001095 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001096 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001097 head->read_eof = done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001098}
1099
1100/**
1101 * tomoyo_write_pid: Specify PID to obtain domainname.
1102 *
1103 * @head: Pointer to "struct tomoyo_io_buffer".
1104 *
1105 * Returns 0.
1106 */
1107static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1108{
1109 unsigned long pid;
1110 /* No error check. */
1111 strict_strtoul(head->write_buf, 10, &pid);
1112 head->read_step = (int) pid;
1113 head->read_eof = false;
1114 return 0;
1115}
1116
1117/**
1118 * tomoyo_read_pid - Get domainname of the specified PID.
1119 *
1120 * @head: Pointer to "struct tomoyo_io_buffer".
1121 *
1122 * Returns the domainname which the specified PID is in on success,
1123 * empty string otherwise.
1124 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1125 * using read()/write() interface rather than sysctl() interface.
1126 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001127static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001128{
1129 if (head->read_avail == 0 && !head->read_eof) {
1130 const int pid = head->read_step;
1131 struct task_struct *p;
1132 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +09001133 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001134 read_lock(&tasklist_lock);
1135 p = find_task_by_vpid(pid);
1136 if (p)
1137 domain = tomoyo_real_domain(p);
1138 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +09001139 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001140 if (domain)
1141 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1142 domain->domainname->name);
1143 head->read_eof = true;
1144 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001145}
1146
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001147static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1148 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE]
1149 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN,
1150 [TOMOYO_TRANSITION_CONTROL_INITIALIZE]
1151 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN,
1152 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN,
1153 [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN
1154};
1155
Kentaro Takeda95908372009-02-05 17:18:13 +09001156/**
1157 * tomoyo_write_exception_policy - Write exception policy.
1158 *
1159 * @head: Pointer to "struct tomoyo_io_buffer".
1160 *
1161 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001162 *
1163 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001164 */
1165static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1166{
1167 char *data = head->write_buf;
1168 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001169 u8 i;
Kentaro Takeda95908372009-02-05 17:18:13 +09001170
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001171 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) {
1172 if (tomoyo_str_starts(&data, tomoyo_transition_type[i]))
1173 return tomoyo_write_transition_control(data, is_delete,
1174 i);
1175 }
Tetsuo Handa10843072010-06-03 20:38:03 +09001176 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR))
1177 return tomoyo_write_aggregator_policy(data, is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +09001178 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1179 return tomoyo_write_globally_readable_policy(data, is_delete);
1180 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1181 return tomoyo_write_pattern_policy(data, is_delete);
1182 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1183 return tomoyo_write_no_rewrite_policy(data, is_delete);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001184 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09001185 return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001186 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09001187 return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
Kentaro Takeda95908372009-02-05 17:18:13 +09001188 return -EINVAL;
1189}
1190
Tetsuo Handa31845e82010-06-17 16:54:33 +09001191static void tomoyo_print_number(char *buffer, int buffer_len,
1192 const struct tomoyo_number_union *ptr)
1193{
1194 int i;
1195 unsigned long min = ptr->values[0];
1196 const unsigned long max = ptr->values[1];
1197 u8 min_type = ptr->min_type;
1198 const u8 max_type = ptr->max_type;
1199 memset(buffer, 0, buffer_len);
1200 buffer_len -= 2;
1201 for (i = 0; i < 2; i++) {
1202 int len;
1203 switch (min_type) {
1204 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
1205 snprintf(buffer, buffer_len, "0x%lX", min);
1206 break;
1207 case TOMOYO_VALUE_TYPE_OCTAL:
1208 snprintf(buffer, buffer_len, "0%lo", min);
1209 break;
1210 default:
1211 snprintf(buffer, buffer_len, "%lu", min);
1212 break;
1213 }
1214 if (min == max && min_type == max_type)
1215 break;
1216 len = strlen(buffer);
1217 buffer[len++] = '-';
1218 buffer += len;
1219 buffer_len -= len;
1220 min_type = max_type;
1221 min = max;
1222 }
1223}
1224
1225static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1226 [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
1227 [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
1228};
1229
1230/**
1231 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1232 *
1233 * @head: Pointer to "struct tomoyo_io_buffer".
1234 * @idx: Index number.
1235 *
1236 * Returns true on success, false otherwise.
1237 *
1238 * Caller holds tomoyo_read_lock().
1239 */
1240static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1241{
Tetsuo Handa31845e82010-06-17 16:54:33 +09001242 const char *w[3] = { "", "", "" };
1243 w[0] = tomoyo_group_name[idx];
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001244 list_for_each_cookie(head->read_var1, &tomoyo_group_list[idx]) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001245 struct tomoyo_group *group =
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001246 list_entry(head->read_var1, typeof(*group), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001247 w[1] = group->group_name->name;
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001248 list_for_each_cookie(head->read_var2, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001249 char buffer[128];
1250 struct tomoyo_acl_head *ptr =
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001251 list_entry(head->read_var2, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001252 if (ptr->is_deleted)
1253 continue;
1254 if (idx == TOMOYO_PATH_GROUP) {
1255 w[2] = container_of(ptr,
1256 struct tomoyo_path_group,
1257 head)->member_name->name;
1258 } else if (idx == TOMOYO_NUMBER_GROUP) {
1259 tomoyo_print_number(buffer, sizeof(buffer),
1260 &container_of
1261 (ptr, struct
1262 tomoyo_number_group,
1263 head)->number);
1264 w[2] = buffer;
1265 }
1266 if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1],
1267 w[2]))
1268 return false;
1269 }
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001270 head->read_var2 = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001271 }
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001272 head->read_var1 = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001273 return true;
1274}
1275
1276/**
1277 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1278 *
1279 * @head: Pointer to "struct tomoyo_io_buffer".
1280 * @idx: Index number.
1281 *
1282 * Returns true on success, false otherwise.
1283 *
1284 * Caller holds tomoyo_read_lock().
1285 */
1286static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1287{
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001288 list_for_each_cookie(head->read_var2, &tomoyo_policy_list[idx]) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001289 const char *w[4] = { "", "", "", "" };
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001290 struct tomoyo_acl_head *acl =
1291 container_of(head->read_var2, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001292 if (acl->is_deleted)
1293 continue;
1294 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001295 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001296 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001297 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001298 container_of(acl, typeof(*ptr), head);
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001299 w[0] = tomoyo_transition_type[ptr->type];
1300 if (ptr->program)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001301 w[1] = ptr->program->name;
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001302 if (ptr->domainname)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001303 w[3] = ptr->domainname->name;
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001304 if (w[1][0] && w[3][0])
1305 w[2] = " from ";
Tetsuo Handa31845e82010-06-17 16:54:33 +09001306 }
1307 break;
1308 case TOMOYO_ID_GLOBALLY_READABLE:
1309 {
1310 struct tomoyo_globally_readable_file_entry *ptr
1311 = container_of(acl, typeof(*ptr), head);
1312 w[0] = TOMOYO_KEYWORD_ALLOW_READ;
1313 w[1] = ptr->filename->name;
1314 }
1315 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001316 case TOMOYO_ID_AGGREGATOR:
1317 {
1318 struct tomoyo_aggregator_entry *ptr =
1319 container_of(acl, typeof(*ptr), head);
1320 w[0] = TOMOYO_KEYWORD_AGGREGATOR;
1321 w[1] = ptr->original_name->name;
1322 w[2] = " ";
1323 w[3] = ptr->aggregated_name->name;
1324 }
1325 break;
1326 case TOMOYO_ID_PATTERN:
1327 {
1328 struct tomoyo_pattern_entry *ptr =
1329 container_of(acl, typeof(*ptr), head);
1330 w[0] = TOMOYO_KEYWORD_FILE_PATTERN;
1331 w[1] = ptr->pattern->name;
1332 }
1333 break;
1334 case TOMOYO_ID_NO_REWRITE:
1335 {
1336 struct tomoyo_no_rewrite_entry *ptr =
1337 container_of(acl, typeof(*ptr), head);
1338 w[0] = TOMOYO_KEYWORD_DENY_REWRITE;
1339 w[1] = ptr->pattern->name;
1340 }
1341 break;
1342 default:
1343 continue;
1344 }
1345 if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2],
1346 w[3]))
1347 return false;
1348 }
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001349 head->read_var2 = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001350 return true;
1351}
1352
Kentaro Takeda95908372009-02-05 17:18:13 +09001353/**
1354 * tomoyo_read_exception_policy - Read exception policy.
1355 *
1356 * @head: Pointer to "struct tomoyo_io_buffer".
1357 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001358 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001359 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001360static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001361{
Tetsuo Handa31845e82010-06-17 16:54:33 +09001362 if (head->read_eof)
1363 return;
1364 while (head->read_step < TOMOYO_MAX_POLICY &&
1365 tomoyo_read_policy(head, head->read_step))
1366 head->read_step++;
1367 if (head->read_step < TOMOYO_MAX_POLICY)
1368 return;
1369 while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1370 tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY))
1371 head->read_step++;
1372 if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1373 return;
1374 head->read_eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001375}
1376
Kentaro Takeda95908372009-02-05 17:18:13 +09001377/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001378 * tomoyo_print_header - Get header line of audit log.
1379 *
1380 * @r: Pointer to "struct tomoyo_request_info".
1381 *
1382 * Returns string representation.
1383 *
1384 * This function uses kmalloc(), so caller must kfree() if this function
1385 * didn't return NULL.
1386 */
1387static char *tomoyo_print_header(struct tomoyo_request_info *r)
1388{
1389 static const char *tomoyo_mode_4[4] = {
1390 "disabled", "learning", "permissive", "enforcing"
1391 };
1392 struct timeval tv;
1393 const pid_t gpid = task_pid_nr(current);
1394 static const int tomoyo_buffer_len = 4096;
1395 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1396 if (!buffer)
1397 return NULL;
1398 do_gettimeofday(&tv);
1399 snprintf(buffer, tomoyo_buffer_len - 1,
1400 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1401 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1402 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1403 tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
1404 (pid_t) sys_getpid(), (pid_t) sys_getppid(),
1405 current_uid(), current_gid(), current_euid(),
1406 current_egid(), current_suid(), current_sgid(),
1407 current_fsuid(), current_fsgid());
1408 return buffer;
1409}
1410
1411/**
1412 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1413 *
1414 * @len: Required size.
1415 * @r: Pointer to "struct tomoyo_request_info".
1416 *
1417 * Returns pointer to allocated memory.
1418 *
1419 * The @len is updated to add the header lines' size on success.
1420 *
1421 * This function uses kzalloc(), so caller must kfree() if this function
1422 * didn't return NULL.
1423 */
1424static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1425{
1426 char *buf = NULL;
1427 const char *header;
1428 const char *domainname;
1429 if (!r->domain)
1430 r->domain = tomoyo_domain();
1431 domainname = r->domain->domainname->name;
1432 header = tomoyo_print_header(r);
1433 if (!header)
1434 return NULL;
1435 *len += strlen(domainname) + strlen(header) + 10;
1436 buf = kzalloc(*len, GFP_NOFS);
1437 if (buf)
1438 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1439 kfree(header);
1440 return buf;
1441}
1442
1443/* Wait queue for tomoyo_query_list. */
1444static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1445
1446/* Lock for manipulating tomoyo_query_list. */
1447static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1448
1449/* Structure for query. */
1450struct tomoyo_query_entry {
1451 struct list_head list;
1452 char *query;
1453 int query_len;
1454 unsigned int serial;
1455 int timer;
1456 int answer;
1457};
1458
1459/* The list for "struct tomoyo_query_entry". */
1460static LIST_HEAD(tomoyo_query_list);
1461
1462/*
1463 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1464 * interface.
1465 */
1466static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1467
1468/**
1469 * tomoyo_supervisor - Ask for the supervisor's decision.
1470 *
1471 * @r: Pointer to "struct tomoyo_request_info".
1472 * @fmt: The printf()'s format string, followed by parameters.
1473 *
1474 * Returns 0 if the supervisor decided to permit the access request which
1475 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1476 * supervisor decided to retry the access request which violated the policy in
1477 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1478 */
1479int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1480{
1481 va_list args;
1482 int error = -EPERM;
1483 int pos;
1484 int len;
1485 static unsigned int tomoyo_serial;
1486 struct tomoyo_query_entry *tomoyo_query_entry = NULL;
1487 bool quota_exceeded = false;
1488 char *header;
1489 switch (r->mode) {
1490 char *buffer;
1491 case TOMOYO_CONFIG_LEARNING:
1492 if (!tomoyo_domain_quota_is_ok(r))
1493 return 0;
1494 va_start(args, fmt);
1495 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1496 va_end(args);
1497 buffer = kmalloc(len, GFP_NOFS);
1498 if (!buffer)
1499 return 0;
1500 va_start(args, fmt);
1501 vsnprintf(buffer, len - 1, fmt, args);
1502 va_end(args);
1503 tomoyo_normalize_line(buffer);
1504 tomoyo_write_domain_policy2(buffer, r->domain, false);
1505 kfree(buffer);
1506 /* fall through */
1507 case TOMOYO_CONFIG_PERMISSIVE:
1508 return 0;
1509 }
1510 if (!r->domain)
1511 r->domain = tomoyo_domain();
1512 if (!atomic_read(&tomoyo_query_observers))
1513 return -EPERM;
1514 va_start(args, fmt);
1515 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1516 va_end(args);
1517 header = tomoyo_init_audit_log(&len, r);
1518 if (!header)
1519 goto out;
1520 tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
1521 if (!tomoyo_query_entry)
1522 goto out;
1523 tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
1524 if (!tomoyo_query_entry->query)
1525 goto out;
1526 len = ksize(tomoyo_query_entry->query);
1527 INIT_LIST_HEAD(&tomoyo_query_entry->list);
1528 spin_lock(&tomoyo_query_list_lock);
1529 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1530 sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
1531 quota_exceeded = true;
1532 } else {
1533 tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
1534 tomoyo_query_entry->serial = tomoyo_serial++;
1535 }
1536 spin_unlock(&tomoyo_query_list_lock);
1537 if (quota_exceeded)
1538 goto out;
1539 pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
1540 tomoyo_query_entry->serial, r->retry, header);
1541 kfree(header);
1542 header = NULL;
1543 va_start(args, fmt);
1544 vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
1545 tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
1546 va_end(args);
1547 spin_lock(&tomoyo_query_list_lock);
1548 list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
1549 spin_unlock(&tomoyo_query_list_lock);
1550 /* Give 10 seconds for supervisor's opinion. */
1551 for (tomoyo_query_entry->timer = 0;
1552 atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
1553 tomoyo_query_entry->timer++) {
1554 wake_up(&tomoyo_query_wait);
1555 set_current_state(TASK_INTERRUPTIBLE);
1556 schedule_timeout(HZ / 10);
1557 if (tomoyo_query_entry->answer)
1558 break;
1559 }
1560 spin_lock(&tomoyo_query_list_lock);
1561 list_del(&tomoyo_query_entry->list);
1562 tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
1563 spin_unlock(&tomoyo_query_list_lock);
1564 switch (tomoyo_query_entry->answer) {
1565 case 3: /* Asked to retry by administrator. */
1566 error = TOMOYO_RETRY_REQUEST;
1567 r->retry++;
1568 break;
1569 case 1:
1570 /* Granted by administrator. */
1571 error = 0;
1572 break;
1573 case 0:
1574 /* Timed out. */
1575 break;
1576 default:
1577 /* Rejected by administrator. */
1578 break;
1579 }
1580 out:
1581 if (tomoyo_query_entry)
1582 kfree(tomoyo_query_entry->query);
1583 kfree(tomoyo_query_entry);
1584 kfree(header);
1585 return error;
1586}
1587
1588/**
1589 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1590 *
1591 * @file: Pointer to "struct file".
1592 * @wait: Pointer to "poll_table".
1593 *
1594 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1595 *
1596 * Waits for access requests which violated policy in enforcing mode.
1597 */
1598static int tomoyo_poll_query(struct file *file, poll_table *wait)
1599{
1600 struct list_head *tmp;
1601 bool found = false;
1602 u8 i;
1603 for (i = 0; i < 2; i++) {
1604 spin_lock(&tomoyo_query_list_lock);
1605 list_for_each(tmp, &tomoyo_query_list) {
1606 struct tomoyo_query_entry *ptr
1607 = list_entry(tmp, struct tomoyo_query_entry,
1608 list);
1609 if (ptr->answer)
1610 continue;
1611 found = true;
1612 break;
1613 }
1614 spin_unlock(&tomoyo_query_list_lock);
1615 if (found)
1616 return POLLIN | POLLRDNORM;
1617 if (i)
1618 break;
1619 poll_wait(file, &tomoyo_query_wait, wait);
1620 }
1621 return 0;
1622}
1623
1624/**
1625 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1626 *
1627 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001628 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001629static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001630{
1631 struct list_head *tmp;
1632 int pos = 0;
1633 int len = 0;
1634 char *buf;
1635 if (head->read_avail)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001636 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001637 if (head->read_buf) {
1638 kfree(head->read_buf);
1639 head->read_buf = NULL;
1640 head->readbuf_size = 0;
1641 }
1642 spin_lock(&tomoyo_query_list_lock);
1643 list_for_each(tmp, &tomoyo_query_list) {
1644 struct tomoyo_query_entry *ptr
1645 = list_entry(tmp, struct tomoyo_query_entry, list);
1646 if (ptr->answer)
1647 continue;
1648 if (pos++ != head->read_step)
1649 continue;
1650 len = ptr->query_len;
1651 break;
1652 }
1653 spin_unlock(&tomoyo_query_list_lock);
1654 if (!len) {
1655 head->read_step = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001656 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001657 }
1658 buf = kzalloc(len, GFP_NOFS);
1659 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001660 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001661 pos = 0;
1662 spin_lock(&tomoyo_query_list_lock);
1663 list_for_each(tmp, &tomoyo_query_list) {
1664 struct tomoyo_query_entry *ptr
1665 = list_entry(tmp, struct tomoyo_query_entry, list);
1666 if (ptr->answer)
1667 continue;
1668 if (pos++ != head->read_step)
1669 continue;
1670 /*
1671 * Some query can be skipped because tomoyo_query_list
1672 * can change, but I don't care.
1673 */
1674 if (len == ptr->query_len)
1675 memmove(buf, ptr->query, len);
1676 break;
1677 }
1678 spin_unlock(&tomoyo_query_list_lock);
1679 if (buf[0]) {
1680 head->read_avail = len;
1681 head->readbuf_size = head->read_avail;
1682 head->read_buf = buf;
1683 head->read_step++;
1684 } else {
1685 kfree(buf);
1686 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001687}
1688
1689/**
1690 * tomoyo_write_answer - Write the supervisor's decision.
1691 *
1692 * @head: Pointer to "struct tomoyo_io_buffer".
1693 *
1694 * Returns 0 on success, -EINVAL otherwise.
1695 */
1696static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1697{
1698 char *data = head->write_buf;
1699 struct list_head *tmp;
1700 unsigned int serial;
1701 unsigned int answer;
1702 spin_lock(&tomoyo_query_list_lock);
1703 list_for_each(tmp, &tomoyo_query_list) {
1704 struct tomoyo_query_entry *ptr
1705 = list_entry(tmp, struct tomoyo_query_entry, list);
1706 ptr->timer = 0;
1707 }
1708 spin_unlock(&tomoyo_query_list_lock);
1709 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1710 return -EINVAL;
1711 spin_lock(&tomoyo_query_list_lock);
1712 list_for_each(tmp, &tomoyo_query_list) {
1713 struct tomoyo_query_entry *ptr
1714 = list_entry(tmp, struct tomoyo_query_entry, list);
1715 if (ptr->serial != serial)
1716 continue;
1717 if (!ptr->answer)
1718 ptr->answer = answer;
1719 break;
1720 }
1721 spin_unlock(&tomoyo_query_list_lock);
1722 return 0;
1723}
1724
1725/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001726 * tomoyo_read_version: Get version.
1727 *
1728 * @head: Pointer to "struct tomoyo_io_buffer".
1729 *
1730 * Returns version information.
1731 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001732static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001733{
1734 if (!head->read_eof) {
Tetsuo Handa57c25902010-06-03 20:38:44 +09001735 tomoyo_io_printf(head, "2.3.0-pre");
Kentaro Takeda95908372009-02-05 17:18:13 +09001736 head->read_eof = true;
1737 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001738}
1739
1740/**
1741 * tomoyo_read_self_domain - Get the current process's domainname.
1742 *
1743 * @head: Pointer to "struct tomoyo_io_buffer".
1744 *
1745 * Returns the current process's domainname.
1746 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001747static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001748{
1749 if (!head->read_eof) {
1750 /*
1751 * tomoyo_domain()->domainname != NULL
1752 * because every process belongs to a domain and
1753 * the domain's name cannot be NULL.
1754 */
1755 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1756 head->read_eof = true;
1757 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001758}
1759
1760/**
1761 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1762 *
1763 * @type: Type of interface.
1764 * @file: Pointer to "struct file".
1765 *
1766 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001767 *
1768 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001769 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001770int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001771{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001772 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001773
1774 if (!head)
1775 return -ENOMEM;
1776 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001777 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001778 switch (type) {
1779 case TOMOYO_DOMAINPOLICY:
1780 /* /sys/kernel/security/tomoyo/domain_policy */
1781 head->write = tomoyo_write_domain_policy;
1782 head->read = tomoyo_read_domain_policy;
1783 break;
1784 case TOMOYO_EXCEPTIONPOLICY:
1785 /* /sys/kernel/security/tomoyo/exception_policy */
1786 head->write = tomoyo_write_exception_policy;
1787 head->read = tomoyo_read_exception_policy;
1788 break;
1789 case TOMOYO_SELFDOMAIN:
1790 /* /sys/kernel/security/tomoyo/self_domain */
1791 head->read = tomoyo_read_self_domain;
1792 break;
1793 case TOMOYO_DOMAIN_STATUS:
1794 /* /sys/kernel/security/tomoyo/.domain_status */
1795 head->write = tomoyo_write_domain_profile;
1796 head->read = tomoyo_read_domain_profile;
1797 break;
1798 case TOMOYO_PROCESS_STATUS:
1799 /* /sys/kernel/security/tomoyo/.process_status */
1800 head->write = tomoyo_write_pid;
1801 head->read = tomoyo_read_pid;
1802 break;
1803 case TOMOYO_VERSION:
1804 /* /sys/kernel/security/tomoyo/version */
1805 head->read = tomoyo_read_version;
1806 head->readbuf_size = 128;
1807 break;
1808 case TOMOYO_MEMINFO:
1809 /* /sys/kernel/security/tomoyo/meminfo */
1810 head->write = tomoyo_write_memory_quota;
1811 head->read = tomoyo_read_memory_counter;
1812 head->readbuf_size = 512;
1813 break;
1814 case TOMOYO_PROFILE:
1815 /* /sys/kernel/security/tomoyo/profile */
1816 head->write = tomoyo_write_profile;
1817 head->read = tomoyo_read_profile;
1818 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001819 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1820 head->poll = tomoyo_poll_query;
1821 head->write = tomoyo_write_answer;
1822 head->read = tomoyo_read_query;
1823 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001824 case TOMOYO_MANAGER:
1825 /* /sys/kernel/security/tomoyo/manager */
1826 head->write = tomoyo_write_manager_policy;
1827 head->read = tomoyo_read_manager_policy;
1828 break;
1829 }
1830 if (!(file->f_mode & FMODE_READ)) {
1831 /*
1832 * No need to allocate read_buf since it is not opened
1833 * for reading.
1834 */
1835 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001836 head->poll = NULL;
1837 } else if (!head->poll) {
1838 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001839 if (!head->readbuf_size)
1840 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001841 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001842 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001843 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001844 return -ENOMEM;
1845 }
1846 }
1847 if (!(file->f_mode & FMODE_WRITE)) {
1848 /*
1849 * No need to allocate write_buf since it is not opened
1850 * for writing.
1851 */
1852 head->write = NULL;
1853 } else if (head->write) {
1854 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001855 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001856 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001857 kfree(head->read_buf);
1858 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001859 return -ENOMEM;
1860 }
1861 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001862 if (type != TOMOYO_QUERY)
1863 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001864 file->private_data = head;
1865 /*
1866 * Call the handler now if the file is
1867 * /sys/kernel/security/tomoyo/self_domain
1868 * so that the user can use
1869 * cat < /sys/kernel/security/tomoyo/self_domain"
1870 * to know the current process's domainname.
1871 */
1872 if (type == TOMOYO_SELFDOMAIN)
1873 tomoyo_read_control(file, NULL, 0);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001874 /*
1875 * If the file is /sys/kernel/security/tomoyo/query , increment the
1876 * observer counter.
1877 * The obserber counter is used by tomoyo_supervisor() to see if
1878 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1879 */
1880 else if (type == TOMOYO_QUERY)
1881 atomic_inc(&tomoyo_query_observers);
Kentaro Takeda95908372009-02-05 17:18:13 +09001882 return 0;
1883}
1884
1885/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001886 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1887 *
1888 * @file: Pointer to "struct file".
1889 * @wait: Pointer to "poll_table".
1890 *
1891 * Waits for read readiness.
1892 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1893 */
1894int tomoyo_poll_control(struct file *file, poll_table *wait)
1895{
1896 struct tomoyo_io_buffer *head = file->private_data;
1897 if (!head->poll)
1898 return -ENOSYS;
1899 return head->poll(file, wait);
1900}
1901
1902/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001903 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1904 *
1905 * @file: Pointer to "struct file".
1906 * @buffer: Poiner to buffer to write to.
1907 * @buffer_len: Size of @buffer.
1908 *
1909 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001910 *
1911 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001912 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001913int tomoyo_read_control(struct file *file, char __user *buffer,
1914 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001915{
1916 int len = 0;
1917 struct tomoyo_io_buffer *head = file->private_data;
1918 char *cp;
1919
1920 if (!head->read)
1921 return -ENOSYS;
1922 if (mutex_lock_interruptible(&head->io_sem))
1923 return -EINTR;
1924 /* Call the policy handler. */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001925 head->read(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001926 if (len < 0)
1927 goto out;
1928 /* Write to buffer. */
1929 len = head->read_avail;
1930 if (len > buffer_len)
1931 len = buffer_len;
1932 if (!len)
1933 goto out;
1934 /* head->read_buf changes by some functions. */
1935 cp = head->read_buf;
1936 if (copy_to_user(buffer, cp, len)) {
1937 len = -EFAULT;
1938 goto out;
1939 }
1940 head->read_avail -= len;
1941 memmove(cp, cp + len, head->read_avail);
1942 out:
1943 mutex_unlock(&head->io_sem);
1944 return len;
1945}
1946
1947/**
1948 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1949 *
1950 * @file: Pointer to "struct file".
1951 * @buffer: Pointer to buffer to read from.
1952 * @buffer_len: Size of @buffer.
1953 *
1954 * Returns @buffer_len on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001955 *
1956 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001957 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001958int tomoyo_write_control(struct file *file, const char __user *buffer,
1959 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001960{
1961 struct tomoyo_io_buffer *head = file->private_data;
1962 int error = buffer_len;
1963 int avail_len = buffer_len;
1964 char *cp0 = head->write_buf;
1965
1966 if (!head->write)
1967 return -ENOSYS;
1968 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1969 return -EFAULT;
1970 /* Don't allow updating policies by non manager programs. */
1971 if (head->write != tomoyo_write_pid &&
1972 head->write != tomoyo_write_domain_policy &&
Tetsuo Handa75093152010-06-16 16:23:55 +09001973 !tomoyo_policy_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +09001974 return -EPERM;
1975 if (mutex_lock_interruptible(&head->io_sem))
1976 return -EINTR;
1977 /* Read a line and dispatch it to the policy handler. */
1978 while (avail_len > 0) {
1979 char c;
1980 if (head->write_avail >= head->writebuf_size - 1) {
1981 error = -ENOMEM;
1982 break;
1983 } else if (get_user(c, buffer)) {
1984 error = -EFAULT;
1985 break;
1986 }
1987 buffer++;
1988 avail_len--;
1989 cp0[head->write_avail++] = c;
1990 if (c != '\n')
1991 continue;
1992 cp0[head->write_avail - 1] = '\0';
1993 head->write_avail = 0;
1994 tomoyo_normalize_line(cp0);
1995 head->write(head);
1996 }
1997 mutex_unlock(&head->io_sem);
1998 return error;
1999}
2000
2001/**
2002 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2003 *
2004 * @file: Pointer to "struct file".
2005 *
2006 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002007 *
2008 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002009 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002010int tomoyo_close_control(struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09002011{
2012 struct tomoyo_io_buffer *head = file->private_data;
Tetsuo Handa847b1732010-02-11 09:43:54 +09002013 const bool is_write = !!head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +09002014
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002015 /*
2016 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2017 * observer counter.
2018 */
2019 if (head->type == TOMOYO_QUERY)
2020 atomic_dec(&tomoyo_query_observers);
2021 else
2022 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002023 /* Release memory used for policy I/O. */
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002024 kfree(head->read_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09002025 head->read_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002026 kfree(head->write_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09002027 head->write_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002028 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002029 head = NULL;
2030 file->private_data = NULL;
Tetsuo Handa847b1732010-02-11 09:43:54 +09002031 if (is_write)
2032 tomoyo_run_gc();
Kentaro Takeda95908372009-02-05 17:18:13 +09002033 return 0;
2034}
2035
2036/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002037 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09002038 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002039void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09002040{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002041 struct tomoyo_domain_info *domain;
2042 const int idx = tomoyo_read_lock();
2043 tomoyo_policy_loaded = true;
2044 /* Check all profiles currently assigned to domains are defined. */
2045 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2046 const u8 profile = domain->profile;
2047 if (tomoyo_profile_ptr[profile])
2048 continue;
2049 panic("Profile %u (used by '%s') not defined.\n",
2050 profile, domain->domainname->name);
2051 }
2052 tomoyo_read_unlock(idx);
Tetsuo Handa57c25902010-06-03 20:38:44 +09002053 if (tomoyo_profile_version != 20090903)
2054 panic("Profile version %u is not supported.\n",
2055 tomoyo_profile_version);
2056 printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002057 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002058}