blob: 2b280350708f7af4524ea9386fa97f9c717b4996 [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 +090014/* Profile version. Currently only 20090903 is defined. */
15static unsigned int tomoyo_profile_version;
16
17/* Profile table. Memory is allocated as needed. */
18static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
19
Kentaro Takeda95908372009-02-05 17:18:13 +090020/* String table for functionality that takes 4 modes. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +090021static const char *tomoyo_mode[4] = {
Kentaro Takeda95908372009-02-05 17:18:13 +090022 "disabled", "learning", "permissive", "enforcing"
23};
Kentaro Takeda95908372009-02-05 17:18:13 +090024
Tetsuo Handa57c25902010-06-03 20:38:44 +090025/* String table for /sys/kernel/security/tomoyo/profile */
26static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
27 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
28 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
29 [TOMOYO_MAC_FILE_OPEN] = "file::open",
30 [TOMOYO_MAC_FILE_CREATE] = "file::create",
31 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
Tetsuo Handa7c759642011-06-26 23:15:31 +090032 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
Tetsuo Handa57c25902010-06-03 20:38:44 +090033 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
34 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
35 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
36 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
37 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
38 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
Tetsuo Handa57c25902010-06-03 20:38:44 +090039 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
40 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
41 [TOMOYO_MAC_FILE_LINK] = "file::link",
42 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
43 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
44 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
45 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
46 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
47 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
48 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090049 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
Tetsuo Handa57c25902010-06-03 20:38:44 +090050 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
51 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090052};
53
Tetsuo Handad5ca1722011-06-26 23:18:21 +090054/* String table for PREFERENCE keyword. */
55static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
56 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
57};
58
Kentaro Takeda95908372009-02-05 17:18:13 +090059/* Permit policy management by non-root user? */
60static bool tomoyo_manage_by_non_root;
61
62/* Utility functions. */
63
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090064/**
Tetsuo Handa57c25902010-06-03 20:38:44 +090065 * tomoyo_yesno - Return "yes" or "no".
66 *
67 * @value: Bool value.
68 */
Tetsuo Handad5ca1722011-06-26 23:18:21 +090069/*
Tetsuo Handa57c25902010-06-03 20:38:44 +090070static const char *tomoyo_yesno(const unsigned int value)
71{
72 return value ? "yes" : "no";
73}
Tetsuo Handad5ca1722011-06-26 23:18:21 +090074*/
Tetsuo Handa57c25902010-06-03 20:38:44 +090075
Tetsuo Handad5ca1722011-06-26 23:18:21 +090076/**
77 * tomoyo_addprintf - strncat()-like-snprintf().
78 *
79 * @buffer: Buffer to write to. Must be '\0'-terminated.
80 * @len: Size of @buffer.
81 * @fmt: The printf()'s format string, followed by parameters.
82 *
83 * Returns nothing.
84 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +090085static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
86{
87 va_list args;
88 const int pos = strlen(buffer);
89 va_start(args, fmt);
90 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
91 va_end(args);
92}
93
94/**
95 * tomoyo_flush - Flush queued string to userspace's buffer.
96 *
97 * @head: Pointer to "struct tomoyo_io_buffer".
98 *
99 * Returns true if all data was flushed, false otherwise.
100 */
101static bool tomoyo_flush(struct tomoyo_io_buffer *head)
102{
103 while (head->r.w_pos) {
104 const char *w = head->r.w[0];
105 int len = strlen(w);
106 if (len) {
107 if (len > head->read_user_buf_avail)
108 len = head->read_user_buf_avail;
109 if (!len)
110 return false;
111 if (copy_to_user(head->read_user_buf, w, len))
112 return false;
113 head->read_user_buf_avail -= len;
114 head->read_user_buf += len;
115 w += len;
116 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900117 head->r.w[0] = w;
118 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900119 return false;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900120 /* Add '\0' for query. */
121 if (head->poll) {
122 if (!head->read_user_buf_avail ||
123 copy_to_user(head->read_user_buf, "", 1))
124 return false;
125 head->read_user_buf_avail--;
126 head->read_user_buf++;
127 }
128 head->r.w_pos--;
129 for (len = 0; len < head->r.w_pos; len++)
130 head->r.w[len] = head->r.w[len + 1];
131 }
132 head->r.avail = 0;
133 return true;
134}
135
136/**
137 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
138 *
139 * @head: Pointer to "struct tomoyo_io_buffer".
140 * @string: String to print.
141 *
142 * Note that @string has to be kept valid until @head is kfree()d.
143 * This means that char[] allocated on stack memory cannot be passed to
144 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
145 */
146static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
147{
148 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
149 head->r.w[head->r.w_pos++] = string;
150 tomoyo_flush(head);
151 } else
152 WARN_ON(1);
153}
154
155/**
156 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
157 *
158 * @head: Pointer to "struct tomoyo_io_buffer".
159 * @fmt: The printf()'s format string, followed by parameters.
160 */
161void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
162{
163 va_list args;
164 int len;
165 int pos = head->r.avail;
166 int size = head->readbuf_size - pos;
167 if (size <= 0)
168 return;
169 va_start(args, fmt);
170 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
171 va_end(args);
172 if (pos + len >= head->readbuf_size) {
173 WARN_ON(1);
174 return;
175 }
176 head->r.avail += len;
177 tomoyo_set_string(head, head->read_buf + pos);
178}
179
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900180/**
181 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
182 *
183 * @head: Pointer to "struct tomoyo_io_buffer".
184 *
185 * Returns nothing.
186 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900187static void tomoyo_set_space(struct tomoyo_io_buffer *head)
188{
189 tomoyo_set_string(head, " ");
190}
191
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900192/**
193 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
194 *
195 * @head: Pointer to "struct tomoyo_io_buffer".
196 *
197 * Returns nothing.
198 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900199static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
200{
201 tomoyo_set_string(head, "\n");
202 return !head->r.w_pos;
203}
204
Tetsuo Handa57c25902010-06-03 20:38:44 +0900205/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900206 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
207 *
208 * @head: Pointer to "struct tomoyo_io_buffer".
209 *
210 * Returns nothing.
211 */
212static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
213{
214 tomoyo_set_string(head, "/");
215}
216
217/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900218 * tomoyo_print_name_union - Print a tomoyo_name_union.
219 *
220 * @head: Pointer to "struct tomoyo_io_buffer".
221 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900222 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900223static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
224 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900225{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900226 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900227 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900228 tomoyo_set_string(head, "@");
229 tomoyo_set_string(head, ptr->group->group_name->name);
230 } else {
231 tomoyo_set_string(head, ptr->filename->name);
232 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900233}
234
235/**
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900236 * tomoyo_print_number_union - Print a tomoyo_number_union.
237 *
238 * @head: Pointer to "struct tomoyo_io_buffer".
239 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900240 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900241static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
242 const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900243{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900244 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900245 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900246 tomoyo_set_string(head, "@");
247 tomoyo_set_string(head, ptr->group->group_name->name);
248 } else {
249 int i;
250 unsigned long min = ptr->values[0];
251 const unsigned long max = ptr->values[1];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900252 u8 min_type = ptr->value_type[0];
253 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900254 char buffer[128];
255 buffer[0] = '\0';
256 for (i = 0; i < 2; i++) {
257 switch (min_type) {
258 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
259 tomoyo_addprintf(buffer, sizeof(buffer),
260 "0x%lX", min);
261 break;
262 case TOMOYO_VALUE_TYPE_OCTAL:
263 tomoyo_addprintf(buffer, sizeof(buffer),
264 "0%lo", min);
265 break;
266 default:
267 tomoyo_addprintf(buffer, sizeof(buffer),
268 "%lu", min);
269 break;
270 }
271 if (min == max && min_type == max_type)
272 break;
273 tomoyo_addprintf(buffer, sizeof(buffer), "-");
274 min_type = max_type;
275 min = max;
276 }
277 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900278 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900279}
280
281/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900282 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900283 *
284 * @profile: Profile number to create.
285 *
286 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
287 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900288static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900289{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900290 struct tomoyo_profile *ptr;
291 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900292 if (profile >= TOMOYO_MAX_PROFILES)
293 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900294 ptr = tomoyo_profile_ptr[profile];
295 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900296 return ptr;
297 entry = kzalloc(sizeof(*entry), GFP_NOFS);
298 if (mutex_lock_interruptible(&tomoyo_policy_lock))
299 goto out;
300 ptr = tomoyo_profile_ptr[profile];
301 if (!ptr && tomoyo_memory_ok(entry)) {
302 ptr = entry;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900303 ptr->default_config = TOMOYO_CONFIG_DISABLED;
304 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
305 sizeof(ptr->config));
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900306 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900307 mb(); /* Avoid out-of-order execution. */
308 tomoyo_profile_ptr[profile] = ptr;
309 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900310 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900311 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900312 out:
313 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900314 return ptr;
315}
316
317/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900318 * tomoyo_profile - Find a profile.
319 *
320 * @profile: Profile number to find.
321 *
322 * Returns pointer to "struct tomoyo_profile".
323 */
324struct tomoyo_profile *tomoyo_profile(const u8 profile)
325{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900326 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900327 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900328 if (!ptr)
329 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900330 return ptr;
331}
332
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900333/**
334 * tomoyo_find_yesno - Find values for specified keyword.
335 *
336 * @string: String to check.
337 * @find: Name of keyword.
338 *
339 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
340 */
341/*
Tetsuo Handa8e568682010-06-25 09:30:09 +0900342static s8 tomoyo_find_yesno(const char *string, const char *find)
343{
344 const char *cp = strstr(string, find);
345 if (cp) {
346 cp += strlen(find);
347 if (!strncmp(cp, "=yes", 4))
348 return 1;
349 else if (!strncmp(cp, "=no", 3))
350 return 0;
351 }
352 return -1;
353}
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900354*/
Tetsuo Handa8e568682010-06-25 09:30:09 +0900355
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900356/**
357 * tomoyo_set_uint - Set value for specified preference.
358 *
359 * @i: Pointer to "unsigned int".
360 * @string: String to check.
361 * @find: Name of keyword.
362 *
363 * Returns nothing.
364 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900365static void tomoyo_set_uint(unsigned int *i, const char *string,
366 const char *find)
367{
368 const char *cp = strstr(string, find);
369 if (cp)
370 sscanf(cp + strlen(find), "=%u", i);
371}
372
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900373/**
374 * tomoyo_set_mode - Set mode for specified profile.
375 *
376 * @name: Name of functionality.
377 * @value: Mode for @name.
378 * @profile: Pointer to "struct tomoyo_profile".
379 *
380 * Returns 0 on success, negative value otherwise.
381 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900382static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900383 struct tomoyo_profile *profile)
384{
385 u8 i;
386 u8 config;
387 if (!strcmp(name, "CONFIG")) {
388 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
389 config = profile->default_config;
390 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
391 config = 0;
392 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
393 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
394 if (strcmp(name, tomoyo_mac_keywords[i]))
395 continue;
396 config = profile->config[i];
397 break;
398 }
399 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
400 return -EINVAL;
401 } else {
402 return -EINVAL;
403 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900404 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900405 config = TOMOYO_CONFIG_USE_DEFAULT;
406 } else {
407 u8 mode;
408 for (mode = 0; mode < 4; mode++)
409 if (strstr(value, tomoyo_mode[mode]))
410 /*
411 * Update lower 3 bits in order to distinguish
412 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
413 */
414 config = (config & ~7) | mode;
415 }
416 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
417 profile->config[i] = config;
418 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
419 profile->default_config = config;
420 return 0;
421}
422
Tetsuo Handa57c25902010-06-03 20:38:44 +0900423/**
424 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900425 *
426 * @head: Pointer to "struct tomoyo_io_buffer".
427 *
428 * Returns 0 on success, negative value otherwise.
429 */
430static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
431{
432 char *data = head->write_buf;
433 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900434 char *cp;
435 struct tomoyo_profile *profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900436 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
437 return 0;
438 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900439 if (*cp != '-')
440 return -EINVAL;
441 data = cp + 1;
442 profile = tomoyo_assign_profile(i);
443 if (!profile)
444 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900445 cp = strchr(data, '=');
446 if (!cp)
447 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900448 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900449 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900450 static DEFINE_SPINLOCK(lock);
451 const struct tomoyo_path_info *new_comment
452 = tomoyo_get_name(cp);
453 const struct tomoyo_path_info *old_comment;
454 if (!new_comment)
455 return -ENOMEM;
456 spin_lock(&lock);
457 old_comment = profile->comment;
458 profile->comment = new_comment;
459 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900460 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900461 return 0;
462 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900463 if (!strcmp(data, "PREFERENCE")) {
464 for (i = 0; i < TOMOYO_MAX_PREF; i++)
465 tomoyo_set_uint(&profile->pref[i], cp,
466 tomoyo_pref_keywords[i]);
467 return 0;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900468 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900469 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900470}
471
472static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
473{
474 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]);
475}
476
Kentaro Takeda95908372009-02-05 17:18:13 +0900477/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900478 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900479 *
480 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +0900481 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900482static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900483{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900484 u8 index;
485 const struct tomoyo_profile *profile;
486 next:
487 index = head->r.index;
488 profile = tomoyo_profile_ptr[index];
489 switch (head->r.step) {
490 case 0:
491 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900492 head->r.step++;
493 break;
494 case 1:
495 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
496 head->r.index++)
497 if (tomoyo_profile_ptr[head->r.index])
498 break;
499 if (head->r.index == TOMOYO_MAX_PROFILES)
500 return;
501 head->r.step++;
502 break;
503 case 2:
504 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900505 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900506 const struct tomoyo_path_info *comment =
507 profile->comment;
508 tomoyo_io_printf(head, "%u-COMMENT=", index);
509 tomoyo_set_string(head, comment ? comment->name : "");
510 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900511 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
512 for (i = 0; i < TOMOYO_MAX_PREF; i++)
513 tomoyo_io_printf(head, "%s=%u ",
514 tomoyo_pref_keywords[i],
515 profile->pref[i]);
516 tomoyo_set_string(head, "}\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900517 head->r.step++;
518 }
519 break;
520 case 3:
521 {
522 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
523 tomoyo_print_config(head, profile->default_config);
524 head->r.bit = 0;
525 head->r.step++;
526 }
527 break;
528 case 4:
529 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
530 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
531 const u8 i = head->r.bit;
532 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900533 if (config == TOMOYO_CONFIG_USE_DEFAULT)
534 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900535 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
536 tomoyo_mac_keywords[i]);
537 tomoyo_print_config(head, config);
538 head->r.bit++;
539 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900540 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900541 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
542 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900543 head->r.index++;
544 head->r.step = 1;
545 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900546 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900547 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900548 if (tomoyo_flush(head))
549 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900550}
551
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900552static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
553 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900554{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900555 return container_of(a, struct tomoyo_manager, head)->manager ==
556 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900557}
558
Kentaro Takeda95908372009-02-05 17:18:13 +0900559/**
560 * tomoyo_update_manager_entry - Add a manager entry.
561 *
562 * @manager: The path to manager or the domainnamme.
563 * @is_delete: True if it is a delete request.
564 *
565 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900566 *
567 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900568 */
569static int tomoyo_update_manager_entry(const char *manager,
570 const bool is_delete)
571{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900572 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900573 struct tomoyo_acl_param param = {
574 .is_delete = is_delete,
575 .list = &tomoyo_policy_list[TOMOYO_ID_MANAGER],
576 };
577 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900578 if (tomoyo_domain_def(manager)) {
579 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900580 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900581 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900582 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900583 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900584 return -EINVAL;
585 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900586 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900587 if (e.manager) {
588 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
589 tomoyo_same_manager);
590 tomoyo_put_name(e.manager);
591 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900592 return error;
593}
594
595/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900596 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900597 *
598 * @head: Pointer to "struct tomoyo_io_buffer".
599 *
600 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900601 *
602 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900603 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900604static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900605{
606 char *data = head->write_buf;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900607 bool is_delete = tomoyo_str_starts(&data, "delete ");
Kentaro Takeda95908372009-02-05 17:18:13 +0900608
609 if (!strcmp(data, "manage_by_non_root")) {
610 tomoyo_manage_by_non_root = !is_delete;
611 return 0;
612 }
613 return tomoyo_update_manager_entry(data, is_delete);
614}
615
616/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900617 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900618 *
619 * @head: Pointer to "struct tomoyo_io_buffer".
620 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900621 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900622 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900623static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900624{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900625 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900626 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900627 list_for_each_cookie(head->r.acl,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900628 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900629 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900630 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900631 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900632 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900633 if (!tomoyo_flush(head))
634 return;
635 tomoyo_set_string(head, ptr->manager->name);
636 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900637 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900638 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900639}
640
641/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900642 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900643 *
644 * Returns true if the current process is permitted to modify policy
645 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900646 *
647 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900648 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900649static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900650{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900651 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900652 const char *exe;
653 const struct task_struct *task = current;
654 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
655 bool found = false;
656
657 if (!tomoyo_policy_loaded)
658 return true;
659 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
660 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900661 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
662 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900663 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900664 && !tomoyo_pathcmp(domainname, ptr->manager)) {
665 found = true;
666 break;
667 }
668 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900669 if (found)
670 return true;
671 exe = tomoyo_get_exe();
672 if (!exe)
673 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900674 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
675 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900676 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900677 && !strcmp(exe, ptr->manager->name)) {
678 found = true;
679 break;
680 }
681 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900682 if (!found) { /* Reduce error messages. */
683 static pid_t last_pid;
684 const pid_t pid = current->pid;
685 if (last_pid != pid) {
686 printk(KERN_WARNING "%s ( %s ) is not permitted to "
687 "update policies.\n", domainname->name, exe);
688 last_pid = pid;
689 }
690 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900691 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900692 return found;
693}
694
695/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900696 * tomoyo_select_one - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900697 *
698 * @head: Pointer to "struct tomoyo_io_buffer".
699 * @data: String to parse.
700 *
701 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900702 *
703 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900704 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900705static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900706{
707 unsigned int pid;
708 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900709 bool global_pid = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900710
Tetsuo Handa063821c2010-06-24 12:00:25 +0900711 if (!strcmp(data, "allow_execute")) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900712 head->r.print_execute_only = true;
Tetsuo Handa063821c2010-06-24 12:00:25 +0900713 return true;
714 }
Tetsuo Handa9b244372010-06-03 20:35:53 +0900715 if (sscanf(data, "pid=%u", &pid) == 1 ||
716 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900717 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900718 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900719 read_lock(&tasklist_lock);
Tetsuo Handa9b244372010-06-03 20:35:53 +0900720 if (global_pid)
721 p = find_task_by_pid_ns(pid, &init_pid_ns);
722 else
723 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900724 if (p)
725 domain = tomoyo_real_domain(p);
726 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900727 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900728 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900729 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900730 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900731 } else
732 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900733 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900734 /* Accessing read_buf is safe because head->io_sem is held. */
735 if (!head->read_buf)
736 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900737 memset(&head->r, 0, sizeof(head->r));
738 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200739 if (domain)
740 head->r.domain = &domain->list;
741 else
742 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900743 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900744 if (domain && domain->is_deleted)
745 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900746 return true;
747}
748
749/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900750 * tomoyo_delete_domain - Delete a domain.
751 *
752 * @domainname: The name of domain.
753 *
754 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900755 *
756 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900757 */
758static int tomoyo_delete_domain(char *domainname)
759{
760 struct tomoyo_domain_info *domain;
761 struct tomoyo_path_info name;
762
763 name.name = domainname;
764 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +0900765 if (mutex_lock_interruptible(&tomoyo_policy_lock))
766 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900767 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900768 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900769 /* Never delete tomoyo_kernel_domain */
770 if (domain == &tomoyo_kernel_domain)
771 continue;
772 if (domain->is_deleted ||
773 tomoyo_pathcmp(domain->domainname, &name))
774 continue;
775 domain->is_deleted = true;
776 break;
777 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900778 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900779 return 0;
780}
781
782/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900783 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900784 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900785 * @list: Pointer to "struct list_head".
786 * @data: Policy to be interpreted.
787 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900788 *
789 * Returns 0 on success, negative value otherwise.
790 *
791 * Caller holds tomoyo_read_lock().
792 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900793static int tomoyo_write_domain2(struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900794 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900795{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900796 struct tomoyo_acl_param param = {
797 .list = list,
798 .data = data,
799 .is_delete = is_delete,
800 };
801 static const struct {
802 const char *keyword;
803 int (*write) (struct tomoyo_acl_param *);
804 } tomoyo_callback[1] = {
805 { "file ", tomoyo_write_file },
806 };
807 u8 i;
808 for (i = 0; i < 1; i++) {
809 if (!tomoyo_str_starts(&param.data,
810 tomoyo_callback[i].keyword))
811 continue;
812 return tomoyo_callback[i].write(&param);
813 }
814 return -EINVAL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900815}
816
817/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900818 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900819 *
820 * @head: Pointer to "struct tomoyo_io_buffer".
821 *
822 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900823 *
824 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900825 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900826static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900827{
828 char *data = head->write_buf;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900829 struct tomoyo_domain_info *domain = head->w.domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900830 bool is_delete = false;
831 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900832 unsigned int profile;
833
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900834 if (tomoyo_str_starts(&data, "delete "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900835 is_delete = true;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900836 else if (tomoyo_str_starts(&data, "select "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900837 is_select = true;
Tetsuo Handa75093152010-06-16 16:23:55 +0900838 if (is_select && tomoyo_select_one(head, data))
Kentaro Takeda95908372009-02-05 17:18:13 +0900839 return 0;
840 /* Don't allow updating policies by non manager programs. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900841 if (!tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +0900842 return -EPERM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900843 if (tomoyo_domain_def(data)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900844 domain = NULL;
845 if (is_delete)
846 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900847 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +0900848 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900849 else
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900850 domain = tomoyo_assign_domain(data, 0);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900851 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900852 return 0;
853 }
854 if (!domain)
855 return -EINVAL;
856
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900857 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +0900858 && profile < TOMOYO_MAX_PROFILES) {
859 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
860 domain->profile = (u8) profile;
861 return 0;
862 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900863 if (!strcmp(data, "quota_exceeded")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900864 domain->quota_warned = !is_delete;
865 return 0;
866 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900867 if (!strcmp(data, "transition_failed")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900868 domain->transition_failed = !is_delete;
869 return 0;
870 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900871 return tomoyo_write_domain2(&domain->acl_info_list, data, is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900872}
873
874/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900875 * tomoyo_set_group - Print category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900876 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900877 * @head: Pointer to "struct tomoyo_io_buffer".
878 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900879 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900880 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900881 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900882static void tomoyo_set_group(struct tomoyo_io_buffer *head,
883 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +0900884{
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900885 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900886}
887
888/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900889 * tomoyo_print_entry - Print an ACL entry.
890 *
891 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900892 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900893 *
894 * Returns true on success, false otherwise.
895 */
896static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900897 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +0900898{
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900899 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900900 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900901 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +0900902
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900903 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900904 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900905 if (!tomoyo_flush(head))
906 return false;
907 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900908 struct tomoyo_path_acl *ptr =
909 container_of(acl, typeof(*ptr), head);
910 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900911 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900912 if (!(perm & (1 << bit)))
913 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900914 if (head->r.print_execute_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900915 bit != TOMOYO_TYPE_EXECUTE)
916 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900917 if (first) {
918 tomoyo_set_group(head, "file ");
919 first = false;
920 } else {
921 tomoyo_set_slash(head);
922 }
923 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900924 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900925 if (first)
926 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900927 tomoyo_print_name_union(head, &ptr->name);
928 } else if (head->r.print_execute_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +0900929 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900930 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
931 struct tomoyo_path2_acl *ptr =
932 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900933 const u8 perm = ptr->perm;
934 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
935 if (!(perm & (1 << bit)))
936 continue;
937 if (first) {
938 tomoyo_set_group(head, "file ");
939 first = false;
940 } else {
941 tomoyo_set_slash(head);
942 }
943 tomoyo_set_string(head, tomoyo_mac_keywords
944 [tomoyo_pp2mac[bit]]);
945 }
946 if (first)
947 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900948 tomoyo_print_name_union(head, &ptr->name1);
949 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900950 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
951 struct tomoyo_path_number_acl *ptr =
952 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900953 const u8 perm = ptr->perm;
954 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
955 if (!(perm & (1 << bit)))
956 continue;
957 if (first) {
958 tomoyo_set_group(head, "file ");
959 first = false;
960 } else {
961 tomoyo_set_slash(head);
962 }
963 tomoyo_set_string(head, tomoyo_mac_keywords
964 [tomoyo_pn2mac[bit]]);
965 }
966 if (first)
967 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900968 tomoyo_print_name_union(head, &ptr->name);
969 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900970 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
971 struct tomoyo_mkdev_acl *ptr =
972 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900973 const u8 perm = ptr->perm;
974 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
975 if (!(perm & (1 << bit)))
976 continue;
977 if (first) {
978 tomoyo_set_group(head, "file ");
979 first = false;
980 } else {
981 tomoyo_set_slash(head);
982 }
983 tomoyo_set_string(head, tomoyo_mac_keywords
984 [tomoyo_pnnn2mac[bit]]);
985 }
986 if (first)
987 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900988 tomoyo_print_name_union(head, &ptr->name);
989 tomoyo_print_number_union(head, &ptr->mode);
990 tomoyo_print_number_union(head, &ptr->major);
991 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900992 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
993 struct tomoyo_mount_acl *ptr =
994 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900995 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900996 tomoyo_print_name_union(head, &ptr->dev_name);
997 tomoyo_print_name_union(head, &ptr->dir_name);
998 tomoyo_print_name_union(head, &ptr->fs_type);
999 tomoyo_print_number_union(head, &ptr->flags);
Kentaro Takeda95908372009-02-05 17:18:13 +09001000 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001001 tomoyo_set_lf(head);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001002 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001003}
1004
1005/**
1006 * tomoyo_read_domain2 - Read domain policy.
1007 *
1008 * @head: Pointer to "struct tomoyo_io_buffer".
1009 * @domain: Pointer to "struct tomoyo_domain_info".
1010 *
1011 * Caller holds tomoyo_read_lock().
1012 *
1013 * Returns true on success, false otherwise.
1014 */
1015static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1016 struct tomoyo_domain_info *domain)
1017{
1018 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1019 struct tomoyo_acl_info *ptr =
1020 list_entry(head->r.acl, typeof(*ptr), list);
1021 if (!tomoyo_print_entry(head, ptr))
1022 return false;
1023 }
1024 head->r.acl = NULL;
1025 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001026}
1027
1028/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001029 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001030 *
1031 * @head: Pointer to "struct tomoyo_io_buffer".
1032 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001033 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001034 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001035static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001036{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001037 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001038 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001039 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001040 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001041 list_entry(head->r.domain, typeof(*domain), list);
1042 switch (head->r.step) {
1043 case 0:
1044 if (domain->is_deleted &&
1045 !head->r.print_this_domain_only)
1046 continue;
1047 /* Print domainname and flags. */
1048 tomoyo_set_string(head, domain->domainname->name);
1049 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001050 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001051 domain->profile);
1052 if (domain->quota_warned)
1053 tomoyo_set_string(head, "quota_exceeded\n");
1054 if (domain->transition_failed)
1055 tomoyo_set_string(head, "transition_failed\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001056 head->r.step++;
1057 tomoyo_set_lf(head);
1058 /* fall through */
1059 case 1:
1060 if (!tomoyo_read_domain2(head, domain))
1061 return;
1062 head->r.step++;
1063 if (!tomoyo_set_lf(head))
1064 return;
1065 /* fall through */
1066 case 2:
1067 head->r.step = 0;
1068 if (head->r.print_this_domain_only)
1069 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001070 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001071 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001072 done:
1073 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001074}
1075
1076/**
1077 * tomoyo_write_domain_profile - Assign profile for specified domain.
1078 *
1079 * @head: Pointer to "struct tomoyo_io_buffer".
1080 *
1081 * Returns 0 on success, -EINVAL otherwise.
1082 *
1083 * This is equivalent to doing
1084 *
1085 * ( echo "select " $domainname; echo "use_profile " $profile ) |
Tetsuo Handa9b244372010-06-03 20:35:53 +09001086 * /usr/sbin/tomoyo-loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001087 *
1088 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001089 */
1090static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1091{
1092 char *data = head->write_buf;
1093 char *cp = strchr(data, ' ');
1094 struct tomoyo_domain_info *domain;
1095 unsigned long profile;
1096
1097 if (!cp)
1098 return -EINVAL;
1099 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001100 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001101 if (strict_strtoul(data, 10, &profile))
1102 return -EINVAL;
1103 if (domain && profile < TOMOYO_MAX_PROFILES
1104 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1105 domain->profile = (u8) profile;
1106 return 0;
1107}
1108
1109/**
1110 * tomoyo_read_domain_profile - Read only domainname and profile.
1111 *
1112 * @head: Pointer to "struct tomoyo_io_buffer".
1113 *
1114 * Returns list of profile number and domainname pairs.
1115 *
1116 * This is equivalent to doing
1117 *
1118 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1119 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1120 * domainname = $0; } else if ( $1 == "use_profile" ) {
1121 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001122 *
1123 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001124 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001125static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001126{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001127 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001128 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001129 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001130 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001131 list_entry(head->r.domain, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001132 if (domain->is_deleted)
1133 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001134 if (!tomoyo_flush(head))
1135 return;
1136 tomoyo_io_printf(head, "%u ", domain->profile);
1137 tomoyo_set_string(head, domain->domainname->name);
1138 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001139 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001140 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001141}
1142
1143/**
1144 * tomoyo_write_pid: Specify PID to obtain domainname.
1145 *
1146 * @head: Pointer to "struct tomoyo_io_buffer".
1147 *
1148 * Returns 0.
1149 */
1150static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1151{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001152 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001153 return 0;
1154}
1155
1156/**
1157 * tomoyo_read_pid - Get domainname of the specified PID.
1158 *
1159 * @head: Pointer to "struct tomoyo_io_buffer".
1160 *
1161 * Returns the domainname which the specified PID is in on success,
1162 * empty string otherwise.
1163 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1164 * using read()/write() interface rather than sysctl() interface.
1165 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001166static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001167{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001168 char *buf = head->write_buf;
1169 bool global_pid = false;
1170 unsigned int pid;
1171 struct task_struct *p;
1172 struct tomoyo_domain_info *domain = NULL;
1173
1174 /* Accessing write_buf is safe because head->io_sem is held. */
1175 if (!buf) {
1176 head->r.eof = true;
1177 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001178 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001179 if (head->r.w_pos || head->r.eof)
1180 return;
1181 head->r.eof = true;
1182 if (tomoyo_str_starts(&buf, "global-pid "))
1183 global_pid = true;
1184 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1185 rcu_read_lock();
1186 read_lock(&tasklist_lock);
1187 if (global_pid)
1188 p = find_task_by_pid_ns(pid, &init_pid_ns);
1189 else
1190 p = find_task_by_vpid(pid);
1191 if (p)
1192 domain = tomoyo_real_domain(p);
1193 read_unlock(&tasklist_lock);
1194 rcu_read_unlock();
1195 if (!domain)
1196 return;
1197 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1198 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001199}
1200
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001201static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001202 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1203 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1204 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1205 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001206};
1207
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001208static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001209 [TOMOYO_PATH_GROUP] = "path_group ",
1210 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001211};
1212
Kentaro Takeda95908372009-02-05 17:18:13 +09001213/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001214 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001215 *
1216 * @head: Pointer to "struct tomoyo_io_buffer".
1217 *
1218 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001219 *
1220 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001221 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001222static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001223{
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001224 struct tomoyo_acl_param param = {
1225 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001226 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001227 u8 i;
1228 param.is_delete = tomoyo_str_starts(&param.data, "delete ");
1229 if (tomoyo_str_starts(&param.data, "aggregator "))
1230 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001231 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001232 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1233 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001234 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001235 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1236 return tomoyo_write_group(&param, i);
Kentaro Takeda95908372009-02-05 17:18:13 +09001237 return -EINVAL;
1238}
1239
Tetsuo Handa31845e82010-06-17 16:54:33 +09001240/**
1241 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1242 *
1243 * @head: Pointer to "struct tomoyo_io_buffer".
1244 * @idx: Index number.
1245 *
1246 * Returns true on success, false otherwise.
1247 *
1248 * Caller holds tomoyo_read_lock().
1249 */
1250static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1251{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001252 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001253 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001254 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001255 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001256 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001257 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001258 if (ptr->is_deleted)
1259 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001260 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001261 return false;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001262 tomoyo_set_string(head, tomoyo_group_name[idx]);
1263 tomoyo_set_string(head, group->group_name->name);
1264 if (idx == TOMOYO_PATH_GROUP) {
1265 tomoyo_set_space(head);
1266 tomoyo_set_string(head, container_of
1267 (ptr, struct tomoyo_path_group,
1268 head)->member_name->name);
1269 } else if (idx == TOMOYO_NUMBER_GROUP) {
1270 tomoyo_print_number_union(head, &container_of
1271 (ptr,
1272 struct tomoyo_number_group,
1273 head)->number);
1274 }
1275 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001276 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001277 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001278 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001279 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001280 return true;
1281}
1282
1283/**
1284 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1285 *
1286 * @head: Pointer to "struct tomoyo_io_buffer".
1287 * @idx: Index number.
1288 *
1289 * Returns true on success, false otherwise.
1290 *
1291 * Caller holds tomoyo_read_lock().
1292 */
1293static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1294{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001295 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001296 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001297 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001298 if (acl->is_deleted)
1299 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001300 if (!tomoyo_flush(head))
1301 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001302 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001303 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001304 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001305 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001306 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001307 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001308 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001309 tomoyo_set_string(head, ptr->program ?
1310 ptr->program->name : "any");
1311 tomoyo_set_string(head, " from ");
1312 tomoyo_set_string(head, ptr->domainname ?
1313 ptr->domainname->name :
1314 "any");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001315 }
1316 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001317 case TOMOYO_ID_AGGREGATOR:
1318 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001319 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001320 container_of(acl, typeof(*ptr), head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001321 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001322 tomoyo_set_string(head,
1323 ptr->original_name->name);
1324 tomoyo_set_space(head);
1325 tomoyo_set_string(head,
1326 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001327 }
1328 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001329 default:
1330 continue;
1331 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001332 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001333 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001334 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001335 return true;
1336}
1337
Kentaro Takeda95908372009-02-05 17:18:13 +09001338/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001339 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001340 *
1341 * @head: Pointer to "struct tomoyo_io_buffer".
1342 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001343 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001344 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001345static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001346{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001347 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001348 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001349 while (head->r.step < TOMOYO_MAX_POLICY &&
1350 tomoyo_read_policy(head, head->r.step))
1351 head->r.step++;
1352 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001353 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001354 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1355 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1356 head->r.step++;
1357 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001358 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001359 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001360}
1361
Kentaro Takeda95908372009-02-05 17:18:13 +09001362/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001363 * tomoyo_print_header - Get header line of audit log.
1364 *
1365 * @r: Pointer to "struct tomoyo_request_info".
1366 *
1367 * Returns string representation.
1368 *
1369 * This function uses kmalloc(), so caller must kfree() if this function
1370 * didn't return NULL.
1371 */
1372static char *tomoyo_print_header(struct tomoyo_request_info *r)
1373{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001374 struct timeval tv;
1375 const pid_t gpid = task_pid_nr(current);
1376 static const int tomoyo_buffer_len = 4096;
1377 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001378 pid_t ppid;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001379 if (!buffer)
1380 return NULL;
1381 do_gettimeofday(&tv);
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001382 rcu_read_lock();
1383 ppid = task_tgid_vnr(current->real_parent);
1384 rcu_read_unlock();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001385 snprintf(buffer, tomoyo_buffer_len - 1,
1386 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1387 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1388 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001389 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001390 task_tgid_vnr(current), ppid,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001391 current_uid(), current_gid(), current_euid(),
1392 current_egid(), current_suid(), current_sgid(),
1393 current_fsuid(), current_fsgid());
1394 return buffer;
1395}
1396
1397/**
1398 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1399 *
1400 * @len: Required size.
1401 * @r: Pointer to "struct tomoyo_request_info".
1402 *
1403 * Returns pointer to allocated memory.
1404 *
1405 * The @len is updated to add the header lines' size on success.
1406 *
1407 * This function uses kzalloc(), so caller must kfree() if this function
1408 * didn't return NULL.
1409 */
1410static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1411{
1412 char *buf = NULL;
1413 const char *header;
1414 const char *domainname;
1415 if (!r->domain)
1416 r->domain = tomoyo_domain();
1417 domainname = r->domain->domainname->name;
1418 header = tomoyo_print_header(r);
1419 if (!header)
1420 return NULL;
1421 *len += strlen(domainname) + strlen(header) + 10;
1422 buf = kzalloc(*len, GFP_NOFS);
1423 if (buf)
1424 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1425 kfree(header);
1426 return buf;
1427}
1428
1429/* Wait queue for tomoyo_query_list. */
1430static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1431
1432/* Lock for manipulating tomoyo_query_list. */
1433static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1434
1435/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001436struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001437 struct list_head list;
1438 char *query;
1439 int query_len;
1440 unsigned int serial;
1441 int timer;
1442 int answer;
1443};
1444
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001445/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001446static LIST_HEAD(tomoyo_query_list);
1447
1448/*
1449 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1450 * interface.
1451 */
1452static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1453
1454/**
1455 * tomoyo_supervisor - Ask for the supervisor's decision.
1456 *
1457 * @r: Pointer to "struct tomoyo_request_info".
1458 * @fmt: The printf()'s format string, followed by parameters.
1459 *
1460 * Returns 0 if the supervisor decided to permit the access request which
1461 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1462 * supervisor decided to retry the access request which violated the policy in
1463 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1464 */
1465int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1466{
1467 va_list args;
1468 int error = -EPERM;
1469 int pos;
1470 int len;
1471 static unsigned int tomoyo_serial;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001472 struct tomoyo_query *entry = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001473 bool quota_exceeded = false;
1474 char *header;
1475 switch (r->mode) {
1476 char *buffer;
1477 case TOMOYO_CONFIG_LEARNING:
1478 if (!tomoyo_domain_quota_is_ok(r))
1479 return 0;
1480 va_start(args, fmt);
1481 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1482 va_end(args);
1483 buffer = kmalloc(len, GFP_NOFS);
1484 if (!buffer)
1485 return 0;
1486 va_start(args, fmt);
1487 vsnprintf(buffer, len - 1, fmt, args);
1488 va_end(args);
1489 tomoyo_normalize_line(buffer);
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001490 tomoyo_write_domain2(&r->domain->acl_info_list, buffer, false);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001491 kfree(buffer);
1492 /* fall through */
1493 case TOMOYO_CONFIG_PERMISSIVE:
1494 return 0;
1495 }
1496 if (!r->domain)
1497 r->domain = tomoyo_domain();
1498 if (!atomic_read(&tomoyo_query_observers))
1499 return -EPERM;
1500 va_start(args, fmt);
1501 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1502 va_end(args);
1503 header = tomoyo_init_audit_log(&len, r);
1504 if (!header)
1505 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001506 entry = kzalloc(sizeof(*entry), GFP_NOFS);
1507 if (!entry)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001508 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001509 entry->query = kzalloc(len, GFP_NOFS);
1510 if (!entry->query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001511 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001512 len = ksize(entry->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001513 spin_lock(&tomoyo_query_list_lock);
1514 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001515 sizeof(*entry) >= tomoyo_quota_for_query) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001516 quota_exceeded = true;
1517 } else {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001518 tomoyo_query_memory_size += len + sizeof(*entry);
1519 entry->serial = tomoyo_serial++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001520 }
1521 spin_unlock(&tomoyo_query_list_lock);
1522 if (quota_exceeded)
1523 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001524 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1525 entry->serial, r->retry, header);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001526 kfree(header);
1527 header = NULL;
1528 va_start(args, fmt);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001529 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1530 entry->query_len = strlen(entry->query) + 1;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001531 va_end(args);
1532 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001533 list_add_tail(&entry->list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001534 spin_unlock(&tomoyo_query_list_lock);
1535 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001536 for (entry->timer = 0;
1537 atomic_read(&tomoyo_query_observers) && entry->timer < 100;
1538 entry->timer++) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001539 wake_up(&tomoyo_query_wait);
1540 set_current_state(TASK_INTERRUPTIBLE);
1541 schedule_timeout(HZ / 10);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001542 if (entry->answer)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001543 break;
1544 }
1545 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001546 list_del(&entry->list);
1547 tomoyo_query_memory_size -= len + sizeof(*entry);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001548 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001549 switch (entry->answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001550 case 3: /* Asked to retry by administrator. */
1551 error = TOMOYO_RETRY_REQUEST;
1552 r->retry++;
1553 break;
1554 case 1:
1555 /* Granted by administrator. */
1556 error = 0;
1557 break;
1558 case 0:
1559 /* Timed out. */
1560 break;
1561 default:
1562 /* Rejected by administrator. */
1563 break;
1564 }
1565 out:
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001566 if (entry)
1567 kfree(entry->query);
1568 kfree(entry);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001569 kfree(header);
1570 return error;
1571}
1572
1573/**
1574 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1575 *
1576 * @file: Pointer to "struct file".
1577 * @wait: Pointer to "poll_table".
1578 *
1579 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1580 *
1581 * Waits for access requests which violated policy in enforcing mode.
1582 */
1583static int tomoyo_poll_query(struct file *file, poll_table *wait)
1584{
1585 struct list_head *tmp;
1586 bool found = false;
1587 u8 i;
1588 for (i = 0; i < 2; i++) {
1589 spin_lock(&tomoyo_query_list_lock);
1590 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001591 struct tomoyo_query *ptr =
1592 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001593 if (ptr->answer)
1594 continue;
1595 found = true;
1596 break;
1597 }
1598 spin_unlock(&tomoyo_query_list_lock);
1599 if (found)
1600 return POLLIN | POLLRDNORM;
1601 if (i)
1602 break;
1603 poll_wait(file, &tomoyo_query_wait, wait);
1604 }
1605 return 0;
1606}
1607
1608/**
1609 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1610 *
1611 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001612 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001613static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001614{
1615 struct list_head *tmp;
1616 int pos = 0;
1617 int len = 0;
1618 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001619 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001620 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001621 if (head->read_buf) {
1622 kfree(head->read_buf);
1623 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001624 }
1625 spin_lock(&tomoyo_query_list_lock);
1626 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001627 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001628 if (ptr->answer)
1629 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001630 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001631 continue;
1632 len = ptr->query_len;
1633 break;
1634 }
1635 spin_unlock(&tomoyo_query_list_lock);
1636 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001637 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001638 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001639 }
1640 buf = kzalloc(len, GFP_NOFS);
1641 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001642 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001643 pos = 0;
1644 spin_lock(&tomoyo_query_list_lock);
1645 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001646 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001647 if (ptr->answer)
1648 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001649 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001650 continue;
1651 /*
1652 * Some query can be skipped because tomoyo_query_list
1653 * can change, but I don't care.
1654 */
1655 if (len == ptr->query_len)
1656 memmove(buf, ptr->query, len);
1657 break;
1658 }
1659 spin_unlock(&tomoyo_query_list_lock);
1660 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001661 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001662 head->r.w[head->r.w_pos++] = buf;
1663 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001664 } else {
1665 kfree(buf);
1666 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001667}
1668
1669/**
1670 * tomoyo_write_answer - Write the supervisor's decision.
1671 *
1672 * @head: Pointer to "struct tomoyo_io_buffer".
1673 *
1674 * Returns 0 on success, -EINVAL otherwise.
1675 */
1676static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1677{
1678 char *data = head->write_buf;
1679 struct list_head *tmp;
1680 unsigned int serial;
1681 unsigned int answer;
1682 spin_lock(&tomoyo_query_list_lock);
1683 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001684 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001685 ptr->timer = 0;
1686 }
1687 spin_unlock(&tomoyo_query_list_lock);
1688 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1689 return -EINVAL;
1690 spin_lock(&tomoyo_query_list_lock);
1691 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001692 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001693 if (ptr->serial != serial)
1694 continue;
1695 if (!ptr->answer)
1696 ptr->answer = answer;
1697 break;
1698 }
1699 spin_unlock(&tomoyo_query_list_lock);
1700 return 0;
1701}
1702
1703/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001704 * tomoyo_read_version: Get version.
1705 *
1706 * @head: Pointer to "struct tomoyo_io_buffer".
1707 *
1708 * Returns version information.
1709 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001710static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001711{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001712 if (!head->r.eof) {
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001713 tomoyo_io_printf(head, "2.4.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001714 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001715 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001716}
1717
1718/**
1719 * tomoyo_read_self_domain - Get the current process's domainname.
1720 *
1721 * @head: Pointer to "struct tomoyo_io_buffer".
1722 *
1723 * Returns the current process's domainname.
1724 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001725static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001726{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001727 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001728 /*
1729 * tomoyo_domain()->domainname != NULL
1730 * because every process belongs to a domain and
1731 * the domain's name cannot be NULL.
1732 */
1733 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001734 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001735 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001736}
1737
1738/**
1739 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1740 *
1741 * @type: Type of interface.
1742 * @file: Pointer to "struct file".
1743 *
1744 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001745 *
1746 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001747 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001748int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001749{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001750 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001751
1752 if (!head)
1753 return -ENOMEM;
1754 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001755 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001756 switch (type) {
1757 case TOMOYO_DOMAINPOLICY:
1758 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001759 head->write = tomoyo_write_domain;
1760 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001761 break;
1762 case TOMOYO_EXCEPTIONPOLICY:
1763 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001764 head->write = tomoyo_write_exception;
1765 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09001766 break;
1767 case TOMOYO_SELFDOMAIN:
1768 /* /sys/kernel/security/tomoyo/self_domain */
1769 head->read = tomoyo_read_self_domain;
1770 break;
1771 case TOMOYO_DOMAIN_STATUS:
1772 /* /sys/kernel/security/tomoyo/.domain_status */
1773 head->write = tomoyo_write_domain_profile;
1774 head->read = tomoyo_read_domain_profile;
1775 break;
1776 case TOMOYO_PROCESS_STATUS:
1777 /* /sys/kernel/security/tomoyo/.process_status */
1778 head->write = tomoyo_write_pid;
1779 head->read = tomoyo_read_pid;
1780 break;
1781 case TOMOYO_VERSION:
1782 /* /sys/kernel/security/tomoyo/version */
1783 head->read = tomoyo_read_version;
1784 head->readbuf_size = 128;
1785 break;
1786 case TOMOYO_MEMINFO:
1787 /* /sys/kernel/security/tomoyo/meminfo */
1788 head->write = tomoyo_write_memory_quota;
1789 head->read = tomoyo_read_memory_counter;
1790 head->readbuf_size = 512;
1791 break;
1792 case TOMOYO_PROFILE:
1793 /* /sys/kernel/security/tomoyo/profile */
1794 head->write = tomoyo_write_profile;
1795 head->read = tomoyo_read_profile;
1796 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001797 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1798 head->poll = tomoyo_poll_query;
1799 head->write = tomoyo_write_answer;
1800 head->read = tomoyo_read_query;
1801 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001802 case TOMOYO_MANAGER:
1803 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001804 head->write = tomoyo_write_manager;
1805 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09001806 break;
1807 }
1808 if (!(file->f_mode & FMODE_READ)) {
1809 /*
1810 * No need to allocate read_buf since it is not opened
1811 * for reading.
1812 */
1813 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001814 head->poll = NULL;
1815 } else if (!head->poll) {
1816 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001817 if (!head->readbuf_size)
1818 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001819 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001820 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001821 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001822 return -ENOMEM;
1823 }
1824 }
1825 if (!(file->f_mode & FMODE_WRITE)) {
1826 /*
1827 * No need to allocate write_buf since it is not opened
1828 * for writing.
1829 */
1830 head->write = NULL;
1831 } else if (head->write) {
1832 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001833 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001834 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001835 kfree(head->read_buf);
1836 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001837 return -ENOMEM;
1838 }
1839 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001840 if (type != TOMOYO_QUERY)
1841 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001842 file->private_data = head;
1843 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001844 * If the file is /sys/kernel/security/tomoyo/query , increment the
1845 * observer counter.
1846 * The obserber counter is used by tomoyo_supervisor() to see if
1847 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1848 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09001849 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001850 atomic_inc(&tomoyo_query_observers);
Kentaro Takeda95908372009-02-05 17:18:13 +09001851 return 0;
1852}
1853
1854/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001855 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1856 *
1857 * @file: Pointer to "struct file".
1858 * @wait: Pointer to "poll_table".
1859 *
1860 * Waits for read readiness.
1861 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1862 */
1863int tomoyo_poll_control(struct file *file, poll_table *wait)
1864{
1865 struct tomoyo_io_buffer *head = file->private_data;
1866 if (!head->poll)
1867 return -ENOSYS;
1868 return head->poll(file, wait);
1869}
1870
1871/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001872 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1873 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001874 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001875 * @buffer: Poiner to buffer to write to.
1876 * @buffer_len: Size of @buffer.
1877 *
1878 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001879 *
1880 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001881 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001882int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001883 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001884{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001885 int len;
Kentaro Takeda95908372009-02-05 17:18:13 +09001886
1887 if (!head->read)
1888 return -ENOSYS;
1889 if (mutex_lock_interruptible(&head->io_sem))
1890 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001891 head->read_user_buf = buffer;
1892 head->read_user_buf_avail = buffer_len;
1893 if (tomoyo_flush(head))
1894 /* Call the policy handler. */
1895 head->read(head);
1896 tomoyo_flush(head);
1897 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09001898 mutex_unlock(&head->io_sem);
1899 return len;
1900}
1901
1902/**
1903 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1904 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001905 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001906 * @buffer: Pointer to buffer to read from.
1907 * @buffer_len: Size of @buffer.
1908 *
1909 * Returns @buffer_len 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 Handa0df7e8b2011-06-26 23:16:36 +09001913int tomoyo_write_control(struct tomoyo_io_buffer *head,
1914 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001915{
Kentaro Takeda95908372009-02-05 17:18:13 +09001916 int error = buffer_len;
1917 int avail_len = buffer_len;
1918 char *cp0 = head->write_buf;
1919
1920 if (!head->write)
1921 return -ENOSYS;
1922 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1923 return -EFAULT;
1924 /* Don't allow updating policies by non manager programs. */
1925 if (head->write != tomoyo_write_pid &&
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001926 head->write != tomoyo_write_domain && !tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +09001927 return -EPERM;
1928 if (mutex_lock_interruptible(&head->io_sem))
1929 return -EINTR;
1930 /* Read a line and dispatch it to the policy handler. */
1931 while (avail_len > 0) {
1932 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001933 if (head->w.avail >= head->writebuf_size - 1) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001934 error = -ENOMEM;
1935 break;
1936 } else if (get_user(c, buffer)) {
1937 error = -EFAULT;
1938 break;
1939 }
1940 buffer++;
1941 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001942 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09001943 if (c != '\n')
1944 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001945 cp0[head->w.avail - 1] = '\0';
1946 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001947 tomoyo_normalize_line(cp0);
1948 head->write(head);
1949 }
1950 mutex_unlock(&head->io_sem);
1951 return error;
1952}
1953
1954/**
1955 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1956 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001957 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001958 *
1959 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001960 *
1961 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001962 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001963int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001964{
Tetsuo Handa847b1732010-02-11 09:43:54 +09001965 const bool is_write = !!head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +09001966
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001967 /*
1968 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1969 * observer counter.
1970 */
1971 if (head->type == TOMOYO_QUERY)
1972 atomic_dec(&tomoyo_query_observers);
1973 else
1974 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09001975 /* Release memory used for policy I/O. */
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001976 kfree(head->read_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001977 head->read_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001978 kfree(head->write_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001979 head->write_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001980 kfree(head);
Tetsuo Handa847b1732010-02-11 09:43:54 +09001981 if (is_write)
1982 tomoyo_run_gc();
Kentaro Takeda95908372009-02-05 17:18:13 +09001983 return 0;
1984}
1985
1986/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001987 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09001988 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001989void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09001990{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001991 struct tomoyo_domain_info *domain;
1992 const int idx = tomoyo_read_lock();
1993 tomoyo_policy_loaded = true;
1994 /* Check all profiles currently assigned to domains are defined. */
1995 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1996 const u8 profile = domain->profile;
1997 if (tomoyo_profile_ptr[profile])
1998 continue;
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09001999 printk(KERN_ERR "You need to define profile %u before using it.\n",
2000 profile);
2001 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2002 "for more information.\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002003 panic("Profile %u (used by '%s') not defined.\n",
2004 profile, domain->domainname->name);
2005 }
2006 tomoyo_read_unlock(idx);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002007 if (tomoyo_profile_version != 20090903) {
2008 printk(KERN_ERR "You need to install userland programs for "
2009 "TOMOYO 2.3 and initialize policy configuration.\n");
2010 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2011 "for more information.\n");
Tetsuo Handa57c25902010-06-03 20:38:44 +09002012 panic("Profile version %u is not supported.\n",
2013 tomoyo_profile_version);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002014 }
Tetsuo Handae6f6a4c2010-07-27 17:17:06 +09002015 printk(KERN_INFO "TOMOYO: 2.3.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002016 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002017}