2 * security/tomoyo/common.c
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/uaccess.h>
10 #include <linux/slab.h>
11 #include <linux/security.h>
14 /* String table for operation mode. */
15 const char * const tomoyo_mode
[TOMOYO_CONFIG_MAX_MODE
] = {
16 [TOMOYO_CONFIG_DISABLED
] = "disabled",
17 [TOMOYO_CONFIG_LEARNING
] = "learning",
18 [TOMOYO_CONFIG_PERMISSIVE
] = "permissive",
19 [TOMOYO_CONFIG_ENFORCING
] = "enforcing"
22 /* String table for /sys/kernel/security/tomoyo/profile */
23 const char * const tomoyo_mac_keywords
[TOMOYO_MAX_MAC_INDEX
24 + TOMOYO_MAX_MAC_CATEGORY_INDEX
] = {
25 [TOMOYO_MAC_FILE_EXECUTE
] = "execute",
26 [TOMOYO_MAC_FILE_OPEN
] = "open",
27 [TOMOYO_MAC_FILE_CREATE
] = "create",
28 [TOMOYO_MAC_FILE_UNLINK
] = "unlink",
29 [TOMOYO_MAC_FILE_GETATTR
] = "getattr",
30 [TOMOYO_MAC_FILE_MKDIR
] = "mkdir",
31 [TOMOYO_MAC_FILE_RMDIR
] = "rmdir",
32 [TOMOYO_MAC_FILE_MKFIFO
] = "mkfifo",
33 [TOMOYO_MAC_FILE_MKSOCK
] = "mksock",
34 [TOMOYO_MAC_FILE_TRUNCATE
] = "truncate",
35 [TOMOYO_MAC_FILE_SYMLINK
] = "symlink",
36 [TOMOYO_MAC_FILE_MKBLOCK
] = "mkblock",
37 [TOMOYO_MAC_FILE_MKCHAR
] = "mkchar",
38 [TOMOYO_MAC_FILE_LINK
] = "link",
39 [TOMOYO_MAC_FILE_RENAME
] = "rename",
40 [TOMOYO_MAC_FILE_CHMOD
] = "chmod",
41 [TOMOYO_MAC_FILE_CHOWN
] = "chown",
42 [TOMOYO_MAC_FILE_CHGRP
] = "chgrp",
43 [TOMOYO_MAC_FILE_IOCTL
] = "ioctl",
44 [TOMOYO_MAC_FILE_CHROOT
] = "chroot",
45 [TOMOYO_MAC_FILE_MOUNT
] = "mount",
46 [TOMOYO_MAC_FILE_UMOUNT
] = "unmount",
47 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = "pivot_root",
48 [TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAC_CATEGORY_FILE
] = "file",
51 /* String table for conditions. */
52 const char * const tomoyo_condition_keyword
[TOMOYO_MAX_CONDITION_KEYWORD
] = {
53 [TOMOYO_TASK_UID
] = "task.uid",
54 [TOMOYO_TASK_EUID
] = "task.euid",
55 [TOMOYO_TASK_SUID
] = "task.suid",
56 [TOMOYO_TASK_FSUID
] = "task.fsuid",
57 [TOMOYO_TASK_GID
] = "task.gid",
58 [TOMOYO_TASK_EGID
] = "task.egid",
59 [TOMOYO_TASK_SGID
] = "task.sgid",
60 [TOMOYO_TASK_FSGID
] = "task.fsgid",
61 [TOMOYO_TASK_PID
] = "task.pid",
62 [TOMOYO_TASK_PPID
] = "task.ppid",
63 [TOMOYO_EXEC_ARGC
] = "exec.argc",
64 [TOMOYO_EXEC_ENVC
] = "exec.envc",
65 [TOMOYO_TYPE_IS_SOCKET
] = "socket",
66 [TOMOYO_TYPE_IS_SYMLINK
] = "symlink",
67 [TOMOYO_TYPE_IS_FILE
] = "file",
68 [TOMOYO_TYPE_IS_BLOCK_DEV
] = "block",
69 [TOMOYO_TYPE_IS_DIRECTORY
] = "directory",
70 [TOMOYO_TYPE_IS_CHAR_DEV
] = "char",
71 [TOMOYO_TYPE_IS_FIFO
] = "fifo",
72 [TOMOYO_MODE_SETUID
] = "setuid",
73 [TOMOYO_MODE_SETGID
] = "setgid",
74 [TOMOYO_MODE_STICKY
] = "sticky",
75 [TOMOYO_MODE_OWNER_READ
] = "owner_read",
76 [TOMOYO_MODE_OWNER_WRITE
] = "owner_write",
77 [TOMOYO_MODE_OWNER_EXECUTE
] = "owner_execute",
78 [TOMOYO_MODE_GROUP_READ
] = "group_read",
79 [TOMOYO_MODE_GROUP_WRITE
] = "group_write",
80 [TOMOYO_MODE_GROUP_EXECUTE
] = "group_execute",
81 [TOMOYO_MODE_OTHERS_READ
] = "others_read",
82 [TOMOYO_MODE_OTHERS_WRITE
] = "others_write",
83 [TOMOYO_MODE_OTHERS_EXECUTE
] = "others_execute",
84 [TOMOYO_EXEC_REALPATH
] = "exec.realpath",
85 [TOMOYO_SYMLINK_TARGET
] = "symlink.target",
86 [TOMOYO_PATH1_UID
] = "path1.uid",
87 [TOMOYO_PATH1_GID
] = "path1.gid",
88 [TOMOYO_PATH1_INO
] = "path1.ino",
89 [TOMOYO_PATH1_MAJOR
] = "path1.major",
90 [TOMOYO_PATH1_MINOR
] = "path1.minor",
91 [TOMOYO_PATH1_PERM
] = "path1.perm",
92 [TOMOYO_PATH1_TYPE
] = "path1.type",
93 [TOMOYO_PATH1_DEV_MAJOR
] = "path1.dev_major",
94 [TOMOYO_PATH1_DEV_MINOR
] = "path1.dev_minor",
95 [TOMOYO_PATH2_UID
] = "path2.uid",
96 [TOMOYO_PATH2_GID
] = "path2.gid",
97 [TOMOYO_PATH2_INO
] = "path2.ino",
98 [TOMOYO_PATH2_MAJOR
] = "path2.major",
99 [TOMOYO_PATH2_MINOR
] = "path2.minor",
100 [TOMOYO_PATH2_PERM
] = "path2.perm",
101 [TOMOYO_PATH2_TYPE
] = "path2.type",
102 [TOMOYO_PATH2_DEV_MAJOR
] = "path2.dev_major",
103 [TOMOYO_PATH2_DEV_MINOR
] = "path2.dev_minor",
104 [TOMOYO_PATH1_PARENT_UID
] = "path1.parent.uid",
105 [TOMOYO_PATH1_PARENT_GID
] = "path1.parent.gid",
106 [TOMOYO_PATH1_PARENT_INO
] = "path1.parent.ino",
107 [TOMOYO_PATH1_PARENT_PERM
] = "path1.parent.perm",
108 [TOMOYO_PATH2_PARENT_UID
] = "path2.parent.uid",
109 [TOMOYO_PATH2_PARENT_GID
] = "path2.parent.gid",
110 [TOMOYO_PATH2_PARENT_INO
] = "path2.parent.ino",
111 [TOMOYO_PATH2_PARENT_PERM
] = "path2.parent.perm",
114 /* String table for PREFERENCE keyword. */
115 static const char * const tomoyo_pref_keywords
[TOMOYO_MAX_PREF
] = {
116 [TOMOYO_PREF_MAX_AUDIT_LOG
] = "max_audit_log",
117 [TOMOYO_PREF_MAX_LEARNING_ENTRY
] = "max_learning_entry",
120 /* String table for path operation. */
121 const char * const tomoyo_path_keyword
[TOMOYO_MAX_PATH_OPERATION
] = {
122 [TOMOYO_TYPE_EXECUTE
] = "execute",
123 [TOMOYO_TYPE_READ
] = "read",
124 [TOMOYO_TYPE_WRITE
] = "write",
125 [TOMOYO_TYPE_APPEND
] = "append",
126 [TOMOYO_TYPE_UNLINK
] = "unlink",
127 [TOMOYO_TYPE_GETATTR
] = "getattr",
128 [TOMOYO_TYPE_RMDIR
] = "rmdir",
129 [TOMOYO_TYPE_TRUNCATE
] = "truncate",
130 [TOMOYO_TYPE_SYMLINK
] = "symlink",
131 [TOMOYO_TYPE_CHROOT
] = "chroot",
132 [TOMOYO_TYPE_UMOUNT
] = "unmount",
135 /* String table for categories. */
136 static const char * const tomoyo_category_keywords
137 [TOMOYO_MAX_MAC_CATEGORY_INDEX
] = {
138 [TOMOYO_MAC_CATEGORY_FILE
] = "file",
141 /* Permit policy management by non-root user? */
142 static bool tomoyo_manage_by_non_root
;
144 /* Utility functions. */
147 * tomoyo_yesno - Return "yes" or "no".
149 * @value: Bool value.
151 const char *tomoyo_yesno(const unsigned int value
)
153 return value
? "yes" : "no";
157 * tomoyo_addprintf - strncat()-like-snprintf().
159 * @buffer: Buffer to write to. Must be '\0'-terminated.
160 * @len: Size of @buffer.
161 * @fmt: The printf()'s format string, followed by parameters.
165 static void tomoyo_addprintf(char *buffer
, int len
, const char *fmt
, ...)
168 const int pos
= strlen(buffer
);
170 vsnprintf(buffer
+ pos
, len
- pos
- 1, fmt
, args
);
175 * tomoyo_flush - Flush queued string to userspace's buffer.
177 * @head: Pointer to "struct tomoyo_io_buffer".
179 * Returns true if all data was flushed, false otherwise.
181 static bool tomoyo_flush(struct tomoyo_io_buffer
*head
)
183 while (head
->r
.w_pos
) {
184 const char *w
= head
->r
.w
[0];
185 size_t len
= strlen(w
);
187 if (len
> head
->read_user_buf_avail
)
188 len
= head
->read_user_buf_avail
;
191 if (copy_to_user(head
->read_user_buf
, w
, len
))
193 head
->read_user_buf_avail
-= len
;
194 head
->read_user_buf
+= len
;
200 /* Add '\0' for audit logs and query. */
202 if (!head
->read_user_buf_avail
||
203 copy_to_user(head
->read_user_buf
, "", 1))
205 head
->read_user_buf_avail
--;
206 head
->read_user_buf
++;
209 for (len
= 0; len
< head
->r
.w_pos
; len
++)
210 head
->r
.w
[len
] = head
->r
.w
[len
+ 1];
217 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
219 * @head: Pointer to "struct tomoyo_io_buffer".
220 * @string: String to print.
222 * Note that @string has to be kept valid until @head is kfree()d.
223 * This means that char[] allocated on stack memory cannot be passed to
224 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
226 static void tomoyo_set_string(struct tomoyo_io_buffer
*head
, const char *string
)
228 if (head
->r
.w_pos
< TOMOYO_MAX_IO_READ_QUEUE
) {
229 head
->r
.w
[head
->r
.w_pos
++] = string
;
236 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
238 * @head: Pointer to "struct tomoyo_io_buffer".
239 * @fmt: The printf()'s format string, followed by parameters.
241 void tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
245 size_t pos
= head
->r
.avail
;
246 int size
= head
->readbuf_size
- pos
;
250 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
) + 1;
252 if (pos
+ len
>= head
->readbuf_size
) {
256 head
->r
.avail
+= len
;
257 tomoyo_set_string(head
, head
->read_buf
+ pos
);
261 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
263 * @head: Pointer to "struct tomoyo_io_buffer".
267 static void tomoyo_set_space(struct tomoyo_io_buffer
*head
)
269 tomoyo_set_string(head
, " ");
273 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
275 * @head: Pointer to "struct tomoyo_io_buffer".
279 static bool tomoyo_set_lf(struct tomoyo_io_buffer
*head
)
281 tomoyo_set_string(head
, "\n");
282 return !head
->r
.w_pos
;
286 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
288 * @head: Pointer to "struct tomoyo_io_buffer".
292 static void tomoyo_set_slash(struct tomoyo_io_buffer
*head
)
294 tomoyo_set_string(head
, "/");
297 /* List of namespaces. */
298 LIST_HEAD(tomoyo_namespace_list
);
299 /* True if namespace other than tomoyo_kernel_namespace is defined. */
300 static bool tomoyo_namespace_enabled
;
303 * tomoyo_init_policy_namespace - Initialize namespace.
305 * @ns: Pointer to "struct tomoyo_policy_namespace".
309 void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace
*ns
)
312 for (idx
= 0; idx
< TOMOYO_MAX_ACL_GROUPS
; idx
++)
313 INIT_LIST_HEAD(&ns
->acl_group
[idx
]);
314 for (idx
= 0; idx
< TOMOYO_MAX_GROUP
; idx
++)
315 INIT_LIST_HEAD(&ns
->group_list
[idx
]);
316 for (idx
= 0; idx
< TOMOYO_MAX_POLICY
; idx
++)
317 INIT_LIST_HEAD(&ns
->policy_list
[idx
]);
318 ns
->profile_version
= 20100903;
319 tomoyo_namespace_enabled
= !list_empty(&tomoyo_namespace_list
);
320 list_add_tail_rcu(&ns
->namespace_list
, &tomoyo_namespace_list
);
324 * tomoyo_print_namespace - Print namespace header.
326 * @head: Pointer to "struct tomoyo_io_buffer".
330 static void tomoyo_print_namespace(struct tomoyo_io_buffer
*head
)
332 if (!tomoyo_namespace_enabled
)
334 tomoyo_set_string(head
,
335 container_of(head
->r
.ns
,
336 struct tomoyo_policy_namespace
,
337 namespace_list
)->name
);
338 tomoyo_set_space(head
);
342 * tomoyo_print_name_union - Print a tomoyo_name_union.
344 * @head: Pointer to "struct tomoyo_io_buffer".
345 * @ptr: Pointer to "struct tomoyo_name_union".
347 static void tomoyo_print_name_union(struct tomoyo_io_buffer
*head
,
348 const struct tomoyo_name_union
*ptr
)
350 tomoyo_set_space(head
);
352 tomoyo_set_string(head
, "@");
353 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
355 tomoyo_set_string(head
, ptr
->filename
->name
);
360 * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
362 * @head: Pointer to "struct tomoyo_io_buffer".
363 * @ptr: Pointer to "struct tomoyo_name_union".
367 static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer
*head
,
368 const struct tomoyo_name_union
*ptr
)
371 tomoyo_set_string(head
, "@");
372 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
374 tomoyo_set_string(head
, "\"");
375 tomoyo_set_string(head
, ptr
->filename
->name
);
376 tomoyo_set_string(head
, "\"");
381 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
383 * @head: Pointer to "struct tomoyo_io_buffer".
384 * @ptr: Pointer to "struct tomoyo_number_union".
388 static void tomoyo_print_number_union_nospace
389 (struct tomoyo_io_buffer
*head
, const struct tomoyo_number_union
*ptr
)
392 tomoyo_set_string(head
, "@");
393 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
396 unsigned long min
= ptr
->values
[0];
397 const unsigned long max
= ptr
->values
[1];
398 u8 min_type
= ptr
->value_type
[0];
399 const u8 max_type
= ptr
->value_type
[1];
402 for (i
= 0; i
< 2; i
++) {
404 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
405 tomoyo_addprintf(buffer
, sizeof(buffer
),
408 case TOMOYO_VALUE_TYPE_OCTAL
:
409 tomoyo_addprintf(buffer
, sizeof(buffer
),
413 tomoyo_addprintf(buffer
, sizeof(buffer
), "%lu",
417 if (min
== max
&& min_type
== max_type
)
419 tomoyo_addprintf(buffer
, sizeof(buffer
), "-");
423 tomoyo_io_printf(head
, "%s", buffer
);
428 * tomoyo_print_number_union - Print a tomoyo_number_union.
430 * @head: Pointer to "struct tomoyo_io_buffer".
431 * @ptr: Pointer to "struct tomoyo_number_union".
435 static void tomoyo_print_number_union(struct tomoyo_io_buffer
*head
,
436 const struct tomoyo_number_union
*ptr
)
438 tomoyo_set_space(head
);
439 tomoyo_print_number_union_nospace(head
, ptr
);
443 * tomoyo_assign_profile - Create a new profile.
445 * @ns: Pointer to "struct tomoyo_policy_namespace".
446 * @profile: Profile number to create.
448 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
450 static struct tomoyo_profile
*tomoyo_assign_profile
451 (struct tomoyo_policy_namespace
*ns
, const unsigned int profile
)
453 struct tomoyo_profile
*ptr
;
454 struct tomoyo_profile
*entry
;
455 if (profile
>= TOMOYO_MAX_PROFILES
)
457 ptr
= ns
->profile_ptr
[profile
];
460 entry
= kzalloc(sizeof(*entry
), GFP_NOFS
);
461 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
463 ptr
= ns
->profile_ptr
[profile
];
464 if (!ptr
&& tomoyo_memory_ok(entry
)) {
466 ptr
->default_config
= TOMOYO_CONFIG_DISABLED
|
467 TOMOYO_CONFIG_WANT_GRANT_LOG
|
468 TOMOYO_CONFIG_WANT_REJECT_LOG
;
469 memset(ptr
->config
, TOMOYO_CONFIG_USE_DEFAULT
,
470 sizeof(ptr
->config
));
471 ptr
->pref
[TOMOYO_PREF_MAX_AUDIT_LOG
] = 1024;
472 ptr
->pref
[TOMOYO_PREF_MAX_LEARNING_ENTRY
] = 2048;
473 mb(); /* Avoid out-of-order execution. */
474 ns
->profile_ptr
[profile
] = ptr
;
477 mutex_unlock(&tomoyo_policy_lock
);
484 * tomoyo_profile - Find a profile.
486 * @ns: Pointer to "struct tomoyo_policy_namespace".
487 * @profile: Profile number to find.
489 * Returns pointer to "struct tomoyo_profile".
491 struct tomoyo_profile
*tomoyo_profile(const struct tomoyo_policy_namespace
*ns
,
494 static struct tomoyo_profile tomoyo_null_profile
;
495 struct tomoyo_profile
*ptr
= ns
->profile_ptr
[profile
];
497 ptr
= &tomoyo_null_profile
;
502 * tomoyo_find_yesno - Find values for specified keyword.
504 * @string: String to check.
505 * @find: Name of keyword.
507 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
509 static s8
tomoyo_find_yesno(const char *string
, const char *find
)
511 const char *cp
= strstr(string
, find
);
514 if (!strncmp(cp
, "=yes", 4))
516 else if (!strncmp(cp
, "=no", 3))
523 * tomoyo_set_uint - Set value for specified preference.
525 * @i: Pointer to "unsigned int".
526 * @string: String to check.
527 * @find: Name of keyword.
531 static void tomoyo_set_uint(unsigned int *i
, const char *string
,
534 const char *cp
= strstr(string
, find
);
536 sscanf(cp
+ strlen(find
), "=%u", i
);
540 * tomoyo_set_mode - Set mode for specified profile.
542 * @name: Name of functionality.
543 * @value: Mode for @name.
544 * @profile: Pointer to "struct tomoyo_profile".
546 * Returns 0 on success, negative value otherwise.
548 static int tomoyo_set_mode(char *name
, const char *value
,
549 struct tomoyo_profile
*profile
)
553 if (!strcmp(name
, "CONFIG")) {
554 i
= TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
;
555 config
= profile
->default_config
;
556 } else if (tomoyo_str_starts(&name
, "CONFIG::")) {
558 for (i
= 0; i
< TOMOYO_MAX_MAC_INDEX
559 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; i
++) {
561 if (i
< TOMOYO_MAX_MAC_INDEX
) {
562 const u8 c
= tomoyo_index2category
[i
];
563 const char *category
=
564 tomoyo_category_keywords
[c
];
565 len
= strlen(category
);
566 if (strncmp(name
, category
, len
) ||
567 name
[len
++] != ':' || name
[len
++] != ':')
570 if (strcmp(name
+ len
, tomoyo_mac_keywords
[i
]))
572 config
= profile
->config
[i
];
575 if (i
== TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
580 if (strstr(value
, "use_default")) {
581 config
= TOMOYO_CONFIG_USE_DEFAULT
;
584 for (mode
= 0; mode
< 4; mode
++)
585 if (strstr(value
, tomoyo_mode
[mode
]))
587 * Update lower 3 bits in order to distinguish
588 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
590 config
= (config
& ~7) | mode
;
591 if (config
!= TOMOYO_CONFIG_USE_DEFAULT
) {
592 switch (tomoyo_find_yesno(value
, "grant_log")) {
594 config
|= TOMOYO_CONFIG_WANT_GRANT_LOG
;
597 config
&= ~TOMOYO_CONFIG_WANT_GRANT_LOG
;
600 switch (tomoyo_find_yesno(value
, "reject_log")) {
602 config
|= TOMOYO_CONFIG_WANT_REJECT_LOG
;
605 config
&= ~TOMOYO_CONFIG_WANT_REJECT_LOG
;
610 if (i
< TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
611 profile
->config
[i
] = config
;
612 else if (config
!= TOMOYO_CONFIG_USE_DEFAULT
)
613 profile
->default_config
= config
;
618 * tomoyo_write_profile - Write profile table.
620 * @head: Pointer to "struct tomoyo_io_buffer".
622 * Returns 0 on success, negative value otherwise.
624 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
626 char *data
= head
->write_buf
;
629 struct tomoyo_profile
*profile
;
630 if (sscanf(data
, "PROFILE_VERSION=%u", &head
->w
.ns
->profile_version
)
633 i
= simple_strtoul(data
, &cp
, 10);
637 profile
= tomoyo_assign_profile(head
->w
.ns
, i
);
640 cp
= strchr(data
, '=');
644 if (!strcmp(data
, "COMMENT")) {
645 static DEFINE_SPINLOCK(lock
);
646 const struct tomoyo_path_info
*new_comment
647 = tomoyo_get_name(cp
);
648 const struct tomoyo_path_info
*old_comment
;
652 old_comment
= profile
->comment
;
653 profile
->comment
= new_comment
;
655 tomoyo_put_name(old_comment
);
658 if (!strcmp(data
, "PREFERENCE")) {
659 for (i
= 0; i
< TOMOYO_MAX_PREF
; i
++)
660 tomoyo_set_uint(&profile
->pref
[i
], cp
,
661 tomoyo_pref_keywords
[i
]);
664 return tomoyo_set_mode(data
, cp
, profile
);
668 * tomoyo_print_config - Print mode for specified functionality.
670 * @head: Pointer to "struct tomoyo_io_buffer".
671 * @config: Mode for that functionality.
675 * Caller prints functionality's name.
677 static void tomoyo_print_config(struct tomoyo_io_buffer
*head
, const u8 config
)
679 tomoyo_io_printf(head
, "={ mode=%s grant_log=%s reject_log=%s }\n",
680 tomoyo_mode
[config
& 3],
681 tomoyo_yesno(config
& TOMOYO_CONFIG_WANT_GRANT_LOG
),
682 tomoyo_yesno(config
& TOMOYO_CONFIG_WANT_REJECT_LOG
));
686 * tomoyo_read_profile - Read profile table.
688 * @head: Pointer to "struct tomoyo_io_buffer".
692 static void tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
695 struct tomoyo_policy_namespace
*ns
=
696 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
697 const struct tomoyo_profile
*profile
;
701 index
= head
->r
.index
;
702 profile
= ns
->profile_ptr
[index
];
703 switch (head
->r
.step
) {
705 tomoyo_print_namespace(head
);
706 tomoyo_io_printf(head
, "PROFILE_VERSION=%u\n",
707 ns
->profile_version
);
711 for ( ; head
->r
.index
< TOMOYO_MAX_PROFILES
;
713 if (ns
->profile_ptr
[head
->r
.index
])
715 if (head
->r
.index
== TOMOYO_MAX_PROFILES
)
722 const struct tomoyo_path_info
*comment
=
724 tomoyo_print_namespace(head
);
725 tomoyo_io_printf(head
, "%u-COMMENT=", index
);
726 tomoyo_set_string(head
, comment
? comment
->name
: "");
728 tomoyo_io_printf(head
, "%u-PREFERENCE={ ", index
);
729 for (i
= 0; i
< TOMOYO_MAX_PREF
; i
++)
730 tomoyo_io_printf(head
, "%s=%u ",
731 tomoyo_pref_keywords
[i
],
733 tomoyo_set_string(head
, "}\n");
739 tomoyo_print_namespace(head
);
740 tomoyo_io_printf(head
, "%u-%s", index
, "CONFIG");
741 tomoyo_print_config(head
, profile
->default_config
);
747 for ( ; head
->r
.bit
< TOMOYO_MAX_MAC_INDEX
748 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; head
->r
.bit
++) {
749 const u8 i
= head
->r
.bit
;
750 const u8 config
= profile
->config
[i
];
751 if (config
== TOMOYO_CONFIG_USE_DEFAULT
)
753 tomoyo_print_namespace(head
);
754 if (i
< TOMOYO_MAX_MAC_INDEX
)
755 tomoyo_io_printf(head
, "%u-CONFIG::%s::%s",
757 tomoyo_category_keywords
758 [tomoyo_index2category
[i
]],
759 tomoyo_mac_keywords
[i
]);
761 tomoyo_io_printf(head
, "%u-CONFIG::%s", index
,
762 tomoyo_mac_keywords
[i
]);
763 tomoyo_print_config(head
, config
);
767 if (head
->r
.bit
== TOMOYO_MAX_MAC_INDEX
768 + TOMOYO_MAX_MAC_CATEGORY_INDEX
) {
774 if (tomoyo_flush(head
))
778 static bool tomoyo_same_manager(const struct tomoyo_acl_head
*a
,
779 const struct tomoyo_acl_head
*b
)
781 return container_of(a
, struct tomoyo_manager
, head
)->manager
==
782 container_of(b
, struct tomoyo_manager
, head
)->manager
;
786 * tomoyo_update_manager_entry - Add a manager entry.
788 * @manager: The path to manager or the domainnamme.
789 * @is_delete: True if it is a delete request.
791 * Returns 0 on success, negative value otherwise.
793 * Caller holds tomoyo_read_lock().
795 static int tomoyo_update_manager_entry(const char *manager
,
796 const bool is_delete
)
798 struct tomoyo_manager e
= { };
799 struct tomoyo_acl_param param
= {
800 /* .ns = &tomoyo_kernel_namespace, */
801 .is_delete
= is_delete
,
802 .list
= &tomoyo_kernel_namespace
.
803 policy_list
[TOMOYO_ID_MANAGER
],
805 int error
= is_delete
? -ENOENT
: -ENOMEM
;
806 if (tomoyo_domain_def(manager
)) {
807 if (!tomoyo_correct_domain(manager
))
811 if (!tomoyo_correct_path(manager
))
814 e
.manager
= tomoyo_get_name(manager
);
816 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), ¶m
,
817 tomoyo_same_manager
);
818 tomoyo_put_name(e
.manager
);
824 * tomoyo_write_manager - Write manager policy.
826 * @head: Pointer to "struct tomoyo_io_buffer".
828 * Returns 0 on success, negative value otherwise.
830 * Caller holds tomoyo_read_lock().
832 static int tomoyo_write_manager(struct tomoyo_io_buffer
*head
)
834 char *data
= head
->write_buf
;
836 if (!strcmp(data
, "manage_by_non_root")) {
837 tomoyo_manage_by_non_root
= !head
->w
.is_delete
;
840 return tomoyo_update_manager_entry(data
, head
->w
.is_delete
);
844 * tomoyo_read_manager - Read manager policy.
846 * @head: Pointer to "struct tomoyo_io_buffer".
848 * Caller holds tomoyo_read_lock().
850 static void tomoyo_read_manager(struct tomoyo_io_buffer
*head
)
854 list_for_each_cookie(head
->r
.acl
, &tomoyo_kernel_namespace
.
855 policy_list
[TOMOYO_ID_MANAGER
]) {
856 struct tomoyo_manager
*ptr
=
857 list_entry(head
->r
.acl
, typeof(*ptr
), head
.list
);
858 if (ptr
->head
.is_deleted
)
860 if (!tomoyo_flush(head
))
862 tomoyo_set_string(head
, ptr
->manager
->name
);
869 * tomoyo_manager - Check whether the current process is a policy manager.
871 * Returns true if the current process is permitted to modify policy
872 * via /sys/kernel/security/tomoyo/ interface.
874 * Caller holds tomoyo_read_lock().
876 static bool tomoyo_manager(void)
878 struct tomoyo_manager
*ptr
;
880 const struct task_struct
*task
= current
;
881 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
884 if (!tomoyo_policy_loaded
)
886 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
888 list_for_each_entry_rcu(ptr
, &tomoyo_kernel_namespace
.
889 policy_list
[TOMOYO_ID_MANAGER
], head
.list
) {
890 if (!ptr
->head
.is_deleted
&& ptr
->is_domain
891 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
898 exe
= tomoyo_get_exe();
901 list_for_each_entry_rcu(ptr
, &tomoyo_kernel_namespace
.
902 policy_list
[TOMOYO_ID_MANAGER
], head
.list
) {
903 if (!ptr
->head
.is_deleted
&& !ptr
->is_domain
904 && !strcmp(exe
, ptr
->manager
->name
)) {
909 if (!found
) { /* Reduce error messages. */
910 static pid_t last_pid
;
911 const pid_t pid
= current
->pid
;
912 if (last_pid
!= pid
) {
913 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
914 "update policies.\n", domainname
->name
, exe
);
923 * tomoyo_select_domain - Parse select command.
925 * @head: Pointer to "struct tomoyo_io_buffer".
926 * @data: String to parse.
928 * Returns true on success, false otherwise.
930 * Caller holds tomoyo_read_lock().
932 static bool tomoyo_select_domain(struct tomoyo_io_buffer
*head
,
936 struct tomoyo_domain_info
*domain
= NULL
;
937 bool global_pid
= false;
938 if (strncmp(data
, "select ", 7))
941 if (sscanf(data
, "pid=%u", &pid
) == 1 ||
942 (global_pid
= true, sscanf(data
, "global-pid=%u", &pid
) == 1)) {
943 struct task_struct
*p
;
945 read_lock(&tasklist_lock
);
947 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
949 p
= find_task_by_vpid(pid
);
951 domain
= tomoyo_real_domain(p
);
952 read_unlock(&tasklist_lock
);
954 } else if (!strncmp(data
, "domain=", 7)) {
955 if (tomoyo_domain_def(data
+ 7))
956 domain
= tomoyo_find_domain(data
+ 7);
959 head
->w
.domain
= domain
;
960 /* Accessing read_buf is safe because head->io_sem is held. */
962 return true; /* Do nothing if open(O_WRONLY). */
963 memset(&head
->r
, 0, sizeof(head
->r
));
964 head
->r
.print_this_domain_only
= true;
966 head
->r
.domain
= &domain
->list
;
969 tomoyo_io_printf(head
, "# select %s\n", data
);
970 if (domain
&& domain
->is_deleted
)
971 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
976 * tomoyo_delete_domain - Delete a domain.
978 * @domainname: The name of domain.
982 * Caller holds tomoyo_read_lock().
984 static int tomoyo_delete_domain(char *domainname
)
986 struct tomoyo_domain_info
*domain
;
987 struct tomoyo_path_info name
;
989 name
.name
= domainname
;
990 tomoyo_fill_path_info(&name
);
991 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
993 /* Is there an active domain? */
994 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
995 /* Never delete tomoyo_kernel_domain */
996 if (domain
== &tomoyo_kernel_domain
)
998 if (domain
->is_deleted
||
999 tomoyo_pathcmp(domain
->domainname
, &name
))
1001 domain
->is_deleted
= true;
1004 mutex_unlock(&tomoyo_policy_lock
);
1009 * tomoyo_write_domain2 - Write domain policy.
1011 * @ns: Pointer to "struct tomoyo_policy_namespace".
1012 * @list: Pointer to "struct list_head".
1013 * @data: Policy to be interpreted.
1014 * @is_delete: True if it is a delete request.
1016 * Returns 0 on success, negative value otherwise.
1018 * Caller holds tomoyo_read_lock().
1020 static int tomoyo_write_domain2(struct tomoyo_policy_namespace
*ns
,
1021 struct list_head
*list
, char *data
,
1022 const bool is_delete
)
1024 struct tomoyo_acl_param param
= {
1028 .is_delete
= is_delete
,
1030 static const struct {
1031 const char *keyword
;
1032 int (*write
) (struct tomoyo_acl_param
*);
1033 } tomoyo_callback
[1] = {
1034 { "file ", tomoyo_write_file
},
1037 for (i
= 0; i
< 1; i
++) {
1038 if (!tomoyo_str_starts(¶m
.data
,
1039 tomoyo_callback
[i
].keyword
))
1041 return tomoyo_callback
[i
].write(¶m
);
1046 /* String table for domain flags. */
1047 const char * const tomoyo_dif
[TOMOYO_MAX_DOMAIN_INFO_FLAGS
] = {
1048 [TOMOYO_DIF_QUOTA_WARNED
] = "quota_exceeded\n",
1049 [TOMOYO_DIF_TRANSITION_FAILED
] = "transition_failed\n",
1053 * tomoyo_write_domain - Write domain policy.
1055 * @head: Pointer to "struct tomoyo_io_buffer".
1057 * Returns 0 on success, negative value otherwise.
1059 * Caller holds tomoyo_read_lock().
1061 static int tomoyo_write_domain(struct tomoyo_io_buffer
*head
)
1063 char *data
= head
->write_buf
;
1064 struct tomoyo_policy_namespace
*ns
;
1065 struct tomoyo_domain_info
*domain
= head
->w
.domain
;
1066 const bool is_delete
= head
->w
.is_delete
;
1067 bool is_select
= !is_delete
&& tomoyo_str_starts(&data
, "select ");
1068 unsigned int profile
;
1072 tomoyo_delete_domain(data
);
1074 domain
= tomoyo_find_domain(data
);
1076 domain
= tomoyo_assign_domain(data
, false);
1077 head
->w
.domain
= domain
;
1083 if (sscanf(data
, "use_profile %u", &profile
) == 1
1084 && profile
< TOMOYO_MAX_PROFILES
) {
1085 if (!tomoyo_policy_loaded
|| ns
->profile_ptr
[profile
])
1086 domain
->profile
= (u8
) profile
;
1089 if (sscanf(data
, "use_group %u\n", &profile
) == 1
1090 && profile
< TOMOYO_MAX_ACL_GROUPS
) {
1092 domain
->group
= (u8
) profile
;
1095 for (profile
= 0; profile
< TOMOYO_MAX_DOMAIN_INFO_FLAGS
; profile
++) {
1096 const char *cp
= tomoyo_dif
[profile
];
1097 if (strncmp(data
, cp
, strlen(cp
) - 1))
1099 domain
->flags
[profile
] = !is_delete
;
1102 return tomoyo_write_domain2(ns
, &domain
->acl_info_list
, data
,
1107 * tomoyo_print_condition - Print condition part.
1109 * @head: Pointer to "struct tomoyo_io_buffer".
1110 * @cond: Pointer to "struct tomoyo_condition".
1112 * Returns true on success, false otherwise.
1114 static bool tomoyo_print_condition(struct tomoyo_io_buffer
*head
,
1115 const struct tomoyo_condition
*cond
)
1117 switch (head
->r
.cond_step
) {
1119 head
->r
.cond_index
= 0;
1120 head
->r
.cond_step
++;
1124 const u16 condc
= cond
->condc
;
1125 const struct tomoyo_condition_element
*condp
=
1126 (typeof(condp
)) (cond
+ 1);
1127 const struct tomoyo_number_union
*numbers_p
=
1128 (typeof(numbers_p
)) (condp
+ condc
);
1129 const struct tomoyo_name_union
*names_p
=
1131 (numbers_p
+ cond
->numbers_count
);
1132 const struct tomoyo_argv
*argv
=
1133 (typeof(argv
)) (names_p
+ cond
->names_count
);
1134 const struct tomoyo_envp
*envp
=
1135 (typeof(envp
)) (argv
+ cond
->argc
);
1137 for (skip
= 0; skip
< head
->r
.cond_index
; skip
++) {
1138 const u8 left
= condp
->left
;
1139 const u8 right
= condp
->right
;
1142 case TOMOYO_ARGV_ENTRY
:
1145 case TOMOYO_ENVP_ENTRY
:
1148 case TOMOYO_NUMBER_UNION
:
1153 case TOMOYO_NAME_UNION
:
1156 case TOMOYO_NUMBER_UNION
:
1161 while (head
->r
.cond_index
< condc
) {
1162 const u8 match
= condp
->equals
;
1163 const u8 left
= condp
->left
;
1164 const u8 right
= condp
->right
;
1165 if (!tomoyo_flush(head
))
1168 head
->r
.cond_index
++;
1169 tomoyo_set_space(head
);
1171 case TOMOYO_ARGV_ENTRY
:
1172 tomoyo_io_printf(head
,
1173 "exec.argv[%lu]%s=\"",
1176 tomoyo_set_string(head
,
1178 tomoyo_set_string(head
, "\"");
1181 case TOMOYO_ENVP_ENTRY
:
1182 tomoyo_set_string(head
,
1184 tomoyo_set_string(head
,
1186 tomoyo_io_printf(head
, "\"]%s=", envp
->
1189 tomoyo_set_string(head
, "\"");
1190 tomoyo_set_string(head
, envp
->
1192 tomoyo_set_string(head
, "\"");
1194 tomoyo_set_string(head
,
1199 case TOMOYO_NUMBER_UNION
:
1200 tomoyo_print_number_union_nospace
1201 (head
, numbers_p
++);
1204 tomoyo_set_string(head
,
1205 tomoyo_condition_keyword
[left
]);
1208 tomoyo_set_string(head
, match
? "=" : "!=");
1210 case TOMOYO_NAME_UNION
:
1211 tomoyo_print_name_union_quoted
1214 case TOMOYO_NUMBER_UNION
:
1215 tomoyo_print_number_union_nospace
1216 (head
, numbers_p
++);
1219 tomoyo_set_string(head
,
1220 tomoyo_condition_keyword
[right
]);
1225 head
->r
.cond_step
++;
1228 if (!tomoyo_flush(head
))
1230 head
->r
.cond_step
++;
1233 tomoyo_set_lf(head
);
1240 * tomoyo_set_group - Print "acl_group " header keyword and category name.
1242 * @head: Pointer to "struct tomoyo_io_buffer".
1243 * @category: Category name.
1247 static void tomoyo_set_group(struct tomoyo_io_buffer
*head
,
1248 const char *category
)
1250 if (head
->type
== TOMOYO_EXCEPTIONPOLICY
) {
1251 tomoyo_print_namespace(head
);
1252 tomoyo_io_printf(head
, "acl_group %u ",
1253 head
->r
.acl_group_index
);
1255 tomoyo_set_string(head
, category
);
1259 * tomoyo_print_entry - Print an ACL entry.
1261 * @head: Pointer to "struct tomoyo_io_buffer".
1262 * @acl: Pointer to an ACL entry.
1264 * Returns true on success, false otherwise.
1266 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
1267 struct tomoyo_acl_info
*acl
)
1269 const u8 acl_type
= acl
->type
;
1273 if (head
->r
.print_cond_part
)
1274 goto print_cond_part
;
1275 if (acl
->is_deleted
)
1277 if (!tomoyo_flush(head
))
1279 else if (acl_type
== TOMOYO_TYPE_PATH_ACL
) {
1280 struct tomoyo_path_acl
*ptr
=
1281 container_of(acl
, typeof(*ptr
), head
);
1282 const u16 perm
= ptr
->perm
;
1283 for (bit
= 0; bit
< TOMOYO_MAX_PATH_OPERATION
; bit
++) {
1284 if (!(perm
& (1 << bit
)))
1286 if (head
->r
.print_transition_related_only
&&
1287 bit
!= TOMOYO_TYPE_EXECUTE
)
1290 tomoyo_set_group(head
, "file ");
1293 tomoyo_set_slash(head
);
1295 tomoyo_set_string(head
, tomoyo_path_keyword
[bit
]);
1299 tomoyo_print_name_union(head
, &ptr
->name
);
1300 } else if (head
->r
.print_transition_related_only
) {
1302 } else if (acl_type
== TOMOYO_TYPE_PATH2_ACL
) {
1303 struct tomoyo_path2_acl
*ptr
=
1304 container_of(acl
, typeof(*ptr
), head
);
1305 const u8 perm
= ptr
->perm
;
1306 for (bit
= 0; bit
< TOMOYO_MAX_PATH2_OPERATION
; bit
++) {
1307 if (!(perm
& (1 << bit
)))
1310 tomoyo_set_group(head
, "file ");
1313 tomoyo_set_slash(head
);
1315 tomoyo_set_string(head
, tomoyo_mac_keywords
1316 [tomoyo_pp2mac
[bit
]]);
1320 tomoyo_print_name_union(head
, &ptr
->name1
);
1321 tomoyo_print_name_union(head
, &ptr
->name2
);
1322 } else if (acl_type
== TOMOYO_TYPE_PATH_NUMBER_ACL
) {
1323 struct tomoyo_path_number_acl
*ptr
=
1324 container_of(acl
, typeof(*ptr
), head
);
1325 const u8 perm
= ptr
->perm
;
1326 for (bit
= 0; bit
< TOMOYO_MAX_PATH_NUMBER_OPERATION
; bit
++) {
1327 if (!(perm
& (1 << bit
)))
1330 tomoyo_set_group(head
, "file ");
1333 tomoyo_set_slash(head
);
1335 tomoyo_set_string(head
, tomoyo_mac_keywords
1336 [tomoyo_pn2mac
[bit
]]);
1340 tomoyo_print_name_union(head
, &ptr
->name
);
1341 tomoyo_print_number_union(head
, &ptr
->number
);
1342 } else if (acl_type
== TOMOYO_TYPE_MKDEV_ACL
) {
1343 struct tomoyo_mkdev_acl
*ptr
=
1344 container_of(acl
, typeof(*ptr
), head
);
1345 const u8 perm
= ptr
->perm
;
1346 for (bit
= 0; bit
< TOMOYO_MAX_MKDEV_OPERATION
; bit
++) {
1347 if (!(perm
& (1 << bit
)))
1350 tomoyo_set_group(head
, "file ");
1353 tomoyo_set_slash(head
);
1355 tomoyo_set_string(head
, tomoyo_mac_keywords
1356 [tomoyo_pnnn2mac
[bit
]]);
1360 tomoyo_print_name_union(head
, &ptr
->name
);
1361 tomoyo_print_number_union(head
, &ptr
->mode
);
1362 tomoyo_print_number_union(head
, &ptr
->major
);
1363 tomoyo_print_number_union(head
, &ptr
->minor
);
1364 } else if (acl_type
== TOMOYO_TYPE_MOUNT_ACL
) {
1365 struct tomoyo_mount_acl
*ptr
=
1366 container_of(acl
, typeof(*ptr
), head
);
1367 tomoyo_set_group(head
, "file mount");
1368 tomoyo_print_name_union(head
, &ptr
->dev_name
);
1369 tomoyo_print_name_union(head
, &ptr
->dir_name
);
1370 tomoyo_print_name_union(head
, &ptr
->fs_type
);
1371 tomoyo_print_number_union(head
, &ptr
->flags
);
1374 head
->r
.print_cond_part
= true;
1375 head
->r
.cond_step
= 0;
1376 if (!tomoyo_flush(head
))
1379 if (!tomoyo_print_condition(head
, acl
->cond
))
1381 head
->r
.print_cond_part
= false;
1383 tomoyo_set_lf(head
);
1389 * tomoyo_read_domain2 - Read domain policy.
1391 * @head: Pointer to "struct tomoyo_io_buffer".
1392 * @list: Pointer to "struct list_head".
1394 * Caller holds tomoyo_read_lock().
1396 * Returns true on success, false otherwise.
1398 static bool tomoyo_read_domain2(struct tomoyo_io_buffer
*head
,
1399 struct list_head
*list
)
1401 list_for_each_cookie(head
->r
.acl
, list
) {
1402 struct tomoyo_acl_info
*ptr
=
1403 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1404 if (!tomoyo_print_entry(head
, ptr
))
1412 * tomoyo_read_domain - Read domain policy.
1414 * @head: Pointer to "struct tomoyo_io_buffer".
1416 * Caller holds tomoyo_read_lock().
1418 static void tomoyo_read_domain(struct tomoyo_io_buffer
*head
)
1422 list_for_each_cookie(head
->r
.domain
, &tomoyo_domain_list
) {
1423 struct tomoyo_domain_info
*domain
=
1424 list_entry(head
->r
.domain
, typeof(*domain
), list
);
1425 switch (head
->r
.step
) {
1428 if (domain
->is_deleted
&&
1429 !head
->r
.print_this_domain_only
)
1431 /* Print domainname and flags. */
1432 tomoyo_set_string(head
, domain
->domainname
->name
);
1433 tomoyo_set_lf(head
);
1434 tomoyo_io_printf(head
, "use_profile %u\n",
1436 tomoyo_io_printf(head
, "use_group %u\n",
1438 for (i
= 0; i
< TOMOYO_MAX_DOMAIN_INFO_FLAGS
; i
++)
1439 if (domain
->flags
[i
])
1440 tomoyo_set_string(head
, tomoyo_dif
[i
]);
1442 tomoyo_set_lf(head
);
1445 if (!tomoyo_read_domain2(head
, &domain
->acl_info_list
))
1448 if (!tomoyo_set_lf(head
))
1453 if (head
->r
.print_this_domain_only
)
1462 * tomoyo_write_pid: Specify PID to obtain domainname.
1464 * @head: Pointer to "struct tomoyo_io_buffer".
1468 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1470 head
->r
.eof
= false;
1475 * tomoyo_read_pid - Get domainname of the specified PID.
1477 * @head: Pointer to "struct tomoyo_io_buffer".
1479 * Returns the domainname which the specified PID is in on success,
1480 * empty string otherwise.
1481 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1482 * using read()/write() interface rather than sysctl() interface.
1484 static void tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1486 char *buf
= head
->write_buf
;
1487 bool global_pid
= false;
1489 struct task_struct
*p
;
1490 struct tomoyo_domain_info
*domain
= NULL
;
1492 /* Accessing write_buf is safe because head->io_sem is held. */
1495 return; /* Do nothing if open(O_RDONLY). */
1497 if (head
->r
.w_pos
|| head
->r
.eof
)
1500 if (tomoyo_str_starts(&buf
, "global-pid "))
1502 pid
= (unsigned int) simple_strtoul(buf
, NULL
, 10);
1504 read_lock(&tasklist_lock
);
1506 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
1508 p
= find_task_by_vpid(pid
);
1510 domain
= tomoyo_real_domain(p
);
1511 read_unlock(&tasklist_lock
);
1515 tomoyo_io_printf(head
, "%u %u ", pid
, domain
->profile
);
1516 tomoyo_set_string(head
, domain
->domainname
->name
);
1519 static const char *tomoyo_transition_type
[TOMOYO_MAX_TRANSITION_TYPE
] = {
1520 [TOMOYO_TRANSITION_CONTROL_NO_RESET
] = "no_reset_domain ",
1521 [TOMOYO_TRANSITION_CONTROL_RESET
] = "reset_domain ",
1522 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
] = "no_initialize_domain ",
1523 [TOMOYO_TRANSITION_CONTROL_INITIALIZE
] = "initialize_domain ",
1524 [TOMOYO_TRANSITION_CONTROL_NO_KEEP
] = "no_keep_domain ",
1525 [TOMOYO_TRANSITION_CONTROL_KEEP
] = "keep_domain ",
1528 static const char *tomoyo_group_name
[TOMOYO_MAX_GROUP
] = {
1529 [TOMOYO_PATH_GROUP
] = "path_group ",
1530 [TOMOYO_NUMBER_GROUP
] = "number_group ",
1534 * tomoyo_write_exception - Write exception policy.
1536 * @head: Pointer to "struct tomoyo_io_buffer".
1538 * Returns 0 on success, negative value otherwise.
1540 * Caller holds tomoyo_read_lock().
1542 static int tomoyo_write_exception(struct tomoyo_io_buffer
*head
)
1544 const bool is_delete
= head
->w
.is_delete
;
1545 struct tomoyo_acl_param param
= {
1547 .is_delete
= is_delete
,
1548 .data
= head
->write_buf
,
1551 if (tomoyo_str_starts(¶m
.data
, "aggregator "))
1552 return tomoyo_write_aggregator(¶m
);
1553 for (i
= 0; i
< TOMOYO_MAX_TRANSITION_TYPE
; i
++)
1554 if (tomoyo_str_starts(¶m
.data
, tomoyo_transition_type
[i
]))
1555 return tomoyo_write_transition_control(¶m
, i
);
1556 for (i
= 0; i
< TOMOYO_MAX_GROUP
; i
++)
1557 if (tomoyo_str_starts(¶m
.data
, tomoyo_group_name
[i
]))
1558 return tomoyo_write_group(¶m
, i
);
1559 if (tomoyo_str_starts(¶m
.data
, "acl_group ")) {
1562 group
= simple_strtoul(param
.data
, &data
, 10);
1563 if (group
< TOMOYO_MAX_ACL_GROUPS
&& *data
++ == ' ')
1564 return tomoyo_write_domain2
1565 (head
->w
.ns
, &head
->w
.ns
->acl_group
[group
],
1572 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1574 * @head: Pointer to "struct tomoyo_io_buffer".
1575 * @idx: Index number.
1577 * Returns true on success, false otherwise.
1579 * Caller holds tomoyo_read_lock().
1581 static bool tomoyo_read_group(struct tomoyo_io_buffer
*head
, const int idx
)
1583 struct tomoyo_policy_namespace
*ns
=
1584 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1585 struct list_head
*list
= &ns
->group_list
[idx
];
1586 list_for_each_cookie(head
->r
.group
, list
) {
1587 struct tomoyo_group
*group
=
1588 list_entry(head
->r
.group
, typeof(*group
), head
.list
);
1589 list_for_each_cookie(head
->r
.acl
, &group
->member_list
) {
1590 struct tomoyo_acl_head
*ptr
=
1591 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1592 if (ptr
->is_deleted
)
1594 if (!tomoyo_flush(head
))
1596 tomoyo_print_namespace(head
);
1597 tomoyo_set_string(head
, tomoyo_group_name
[idx
]);
1598 tomoyo_set_string(head
, group
->group_name
->name
);
1599 if (idx
== TOMOYO_PATH_GROUP
) {
1600 tomoyo_set_space(head
);
1601 tomoyo_set_string(head
, container_of
1602 (ptr
, struct tomoyo_path_group
,
1603 head
)->member_name
->name
);
1604 } else if (idx
== TOMOYO_NUMBER_GROUP
) {
1605 tomoyo_print_number_union(head
, &container_of
1607 struct tomoyo_number_group
,
1610 tomoyo_set_lf(head
);
1614 head
->r
.group
= NULL
;
1619 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1621 * @head: Pointer to "struct tomoyo_io_buffer".
1622 * @idx: Index number.
1624 * Returns true on success, false otherwise.
1626 * Caller holds tomoyo_read_lock().
1628 static bool tomoyo_read_policy(struct tomoyo_io_buffer
*head
, const int idx
)
1630 struct tomoyo_policy_namespace
*ns
=
1631 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1632 struct list_head
*list
= &ns
->policy_list
[idx
];
1633 list_for_each_cookie(head
->r
.acl
, list
) {
1634 struct tomoyo_acl_head
*acl
=
1635 container_of(head
->r
.acl
, typeof(*acl
), list
);
1636 if (acl
->is_deleted
)
1638 if (!tomoyo_flush(head
))
1641 case TOMOYO_ID_TRANSITION_CONTROL
:
1643 struct tomoyo_transition_control
*ptr
=
1644 container_of(acl
, typeof(*ptr
), head
);
1645 tomoyo_print_namespace(head
);
1646 tomoyo_set_string(head
, tomoyo_transition_type
1648 tomoyo_set_string(head
, ptr
->program
?
1649 ptr
->program
->name
: "any");
1650 tomoyo_set_string(head
, " from ");
1651 tomoyo_set_string(head
, ptr
->domainname
?
1652 ptr
->domainname
->name
:
1656 case TOMOYO_ID_AGGREGATOR
:
1658 struct tomoyo_aggregator
*ptr
=
1659 container_of(acl
, typeof(*ptr
), head
);
1660 tomoyo_print_namespace(head
);
1661 tomoyo_set_string(head
, "aggregator ");
1662 tomoyo_set_string(head
,
1663 ptr
->original_name
->name
);
1664 tomoyo_set_space(head
);
1665 tomoyo_set_string(head
,
1666 ptr
->aggregated_name
->name
);
1672 tomoyo_set_lf(head
);
1679 * tomoyo_read_exception - Read exception policy.
1681 * @head: Pointer to "struct tomoyo_io_buffer".
1683 * Caller holds tomoyo_read_lock().
1685 static void tomoyo_read_exception(struct tomoyo_io_buffer
*head
)
1687 struct tomoyo_policy_namespace
*ns
=
1688 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1691 while (head
->r
.step
< TOMOYO_MAX_POLICY
&&
1692 tomoyo_read_policy(head
, head
->r
.step
))
1694 if (head
->r
.step
< TOMOYO_MAX_POLICY
)
1696 while (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
&&
1697 tomoyo_read_group(head
, head
->r
.step
- TOMOYO_MAX_POLICY
))
1699 if (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
)
1701 while (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
1702 + TOMOYO_MAX_ACL_GROUPS
) {
1703 head
->r
.acl_group_index
= head
->r
.step
- TOMOYO_MAX_POLICY
1705 if (!tomoyo_read_domain2(head
, &ns
->acl_group
1706 [head
->r
.acl_group_index
]))
1713 /* Wait queue for kernel -> userspace notification. */
1714 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait
);
1715 /* Wait queue for userspace -> kernel notification. */
1716 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait
);
1718 /* Structure for query. */
1719 struct tomoyo_query
{
1720 struct list_head list
;
1723 unsigned int serial
;
1729 /* The list for "struct tomoyo_query". */
1730 static LIST_HEAD(tomoyo_query_list
);
1732 /* Lock for manipulating tomoyo_query_list. */
1733 static DEFINE_SPINLOCK(tomoyo_query_list_lock
);
1736 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1739 static atomic_t tomoyo_query_observers
= ATOMIC_INIT(0);
1742 * tomoyo_truncate - Truncate a line.
1744 * @str: String to truncate.
1746 * Returns length of truncated @str.
1748 static int tomoyo_truncate(char *str
)
1751 while (*(unsigned char *) str
> (unsigned char) ' ')
1754 return strlen(start
) + 1;
1758 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1760 * @domain: Pointer to "struct tomoyo_domain_info".
1761 * @header: Lines containing ACL.
1765 static void tomoyo_add_entry(struct tomoyo_domain_info
*domain
, char *header
)
1768 char *realpath
= NULL
;
1770 char *symlink
= NULL
;
1771 char *cp
= strchr(header
, '\n');
1775 cp
= strchr(cp
+ 1, '\n');
1779 len
= strlen(cp
) + 1;
1780 /* strstr() will return NULL if ordering is wrong. */
1782 argv0
= strstr(header
, " argv[]={ \"");
1785 len
+= tomoyo_truncate(argv0
) + 14;
1787 realpath
= strstr(header
, " exec={ realpath=\"");
1790 len
+= tomoyo_truncate(realpath
) + 6;
1792 symlink
= strstr(header
, " symlink.target=\"");
1794 len
+= tomoyo_truncate(symlink
+ 1) + 1;
1796 buffer
= kmalloc(len
, GFP_NOFS
);
1799 snprintf(buffer
, len
- 1, "%s", cp
);
1801 tomoyo_addprintf(buffer
, len
, " exec.%s", realpath
);
1803 tomoyo_addprintf(buffer
, len
, " exec.argv[0]=%s", argv0
);
1805 tomoyo_addprintf(buffer
, len
, "%s", symlink
);
1806 tomoyo_normalize_line(buffer
);
1807 if (!tomoyo_write_domain2(domain
->ns
, &domain
->acl_info_list
, buffer
,
1809 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
1814 * tomoyo_supervisor - Ask for the supervisor's decision.
1816 * @r: Pointer to "struct tomoyo_request_info".
1817 * @fmt: The printf()'s format string, followed by parameters.
1819 * Returns 0 if the supervisor decided to permit the access request which
1820 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1821 * supervisor decided to retry the access request which violated the policy in
1822 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1824 int tomoyo_supervisor(struct tomoyo_request_info
*r
, const char *fmt
, ...)
1829 static unsigned int tomoyo_serial
;
1830 struct tomoyo_query entry
= { };
1831 bool quota_exceeded
= false;
1832 va_start(args
, fmt
);
1833 len
= vsnprintf((char *) &len
, 1, fmt
, args
) + 1;
1835 /* Write /sys/kernel/security/tomoyo/audit. */
1836 va_start(args
, fmt
);
1837 tomoyo_write_log2(r
, len
, fmt
, args
);
1839 /* Nothing more to do if granted. */
1843 tomoyo_update_stat(r
->mode
);
1845 case TOMOYO_CONFIG_ENFORCING
:
1847 if (atomic_read(&tomoyo_query_observers
))
1850 case TOMOYO_CONFIG_LEARNING
:
1852 /* Check max_learning_entry parameter. */
1853 if (tomoyo_domain_quota_is_ok(r
))
1860 va_start(args
, fmt
);
1861 entry
.query
= tomoyo_init_log(r
, len
, fmt
, args
);
1865 entry
.query_len
= strlen(entry
.query
) + 1;
1867 tomoyo_add_entry(r
->domain
, entry
.query
);
1870 len
= tomoyo_round2(entry
.query_len
);
1871 spin_lock(&tomoyo_query_list_lock
);
1872 if (tomoyo_memory_quota
[TOMOYO_MEMORY_QUERY
] &&
1873 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] + len
1874 >= tomoyo_memory_quota
[TOMOYO_MEMORY_QUERY
]) {
1875 quota_exceeded
= true;
1877 entry
.serial
= tomoyo_serial
++;
1878 entry
.retry
= r
->retry
;
1879 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] += len
;
1880 list_add_tail(&entry
.list
, &tomoyo_query_list
);
1882 spin_unlock(&tomoyo_query_list_lock
);
1885 /* Give 10 seconds for supervisor's opinion. */
1886 while (entry
.timer
< 10) {
1887 wake_up_all(&tomoyo_query_wait
);
1888 if (wait_event_interruptible_timeout
1889 (tomoyo_answer_wait
, entry
.answer
||
1890 !atomic_read(&tomoyo_query_observers
), HZ
))
1895 spin_lock(&tomoyo_query_list_lock
);
1896 list_del(&entry
.list
);
1897 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] -= len
;
1898 spin_unlock(&tomoyo_query_list_lock
);
1899 switch (entry
.answer
) {
1900 case 3: /* Asked to retry by administrator. */
1901 error
= TOMOYO_RETRY_REQUEST
;
1905 /* Granted by administrator. */
1909 /* Timed out or rejected by administrator. */
1918 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1920 * @file: Pointer to "struct file".
1921 * @wait: Pointer to "poll_table".
1923 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1925 * Waits for access requests which violated policy in enforcing mode.
1927 static int tomoyo_poll_query(struct file
*file
, poll_table
*wait
)
1929 struct list_head
*tmp
;
1932 for (i
= 0; i
< 2; i
++) {
1933 spin_lock(&tomoyo_query_list_lock
);
1934 list_for_each(tmp
, &tomoyo_query_list
) {
1935 struct tomoyo_query
*ptr
=
1936 list_entry(tmp
, typeof(*ptr
), list
);
1942 spin_unlock(&tomoyo_query_list_lock
);
1944 return POLLIN
| POLLRDNORM
;
1947 poll_wait(file
, &tomoyo_query_wait
, wait
);
1953 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1955 * @head: Pointer to "struct tomoyo_io_buffer".
1957 static void tomoyo_read_query(struct tomoyo_io_buffer
*head
)
1959 struct list_head
*tmp
;
1960 unsigned int pos
= 0;
1965 if (head
->read_buf
) {
1966 kfree(head
->read_buf
);
1967 head
->read_buf
= NULL
;
1969 spin_lock(&tomoyo_query_list_lock
);
1970 list_for_each(tmp
, &tomoyo_query_list
) {
1971 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
1974 if (pos
++ != head
->r
.query_index
)
1976 len
= ptr
->query_len
;
1979 spin_unlock(&tomoyo_query_list_lock
);
1981 head
->r
.query_index
= 0;
1984 buf
= kzalloc(len
+ 32, GFP_NOFS
);
1988 spin_lock(&tomoyo_query_list_lock
);
1989 list_for_each(tmp
, &tomoyo_query_list
) {
1990 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
1993 if (pos
++ != head
->r
.query_index
)
1996 * Some query can be skipped because tomoyo_query_list
1997 * can change, but I don't care.
1999 if (len
== ptr
->query_len
)
2000 snprintf(buf
, len
+ 31, "Q%u-%hu\n%s", ptr
->serial
,
2001 ptr
->retry
, ptr
->query
);
2004 spin_unlock(&tomoyo_query_list_lock
);
2006 head
->read_buf
= buf
;
2007 head
->r
.w
[head
->r
.w_pos
++] = buf
;
2008 head
->r
.query_index
++;
2015 * tomoyo_write_answer - Write the supervisor's decision.
2017 * @head: Pointer to "struct tomoyo_io_buffer".
2019 * Returns 0 on success, -EINVAL otherwise.
2021 static int tomoyo_write_answer(struct tomoyo_io_buffer
*head
)
2023 char *data
= head
->write_buf
;
2024 struct list_head
*tmp
;
2025 unsigned int serial
;
2026 unsigned int answer
;
2027 spin_lock(&tomoyo_query_list_lock
);
2028 list_for_each(tmp
, &tomoyo_query_list
) {
2029 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2032 spin_unlock(&tomoyo_query_list_lock
);
2033 if (sscanf(data
, "A%u=%u", &serial
, &answer
) != 2)
2035 spin_lock(&tomoyo_query_list_lock
);
2036 list_for_each(tmp
, &tomoyo_query_list
) {
2037 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2038 if (ptr
->serial
!= serial
)
2041 ptr
->answer
= answer
;
2044 spin_unlock(&tomoyo_query_list_lock
);
2049 * tomoyo_read_version: Get version.
2051 * @head: Pointer to "struct tomoyo_io_buffer".
2053 * Returns version information.
2055 static void tomoyo_read_version(struct tomoyo_io_buffer
*head
)
2058 tomoyo_io_printf(head
, "2.4.0");
2064 * tomoyo_read_self_domain - Get the current process's domainname.
2066 * @head: Pointer to "struct tomoyo_io_buffer".
2068 * Returns the current process's domainname.
2070 static void tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
2074 * tomoyo_domain()->domainname != NULL
2075 * because every process belongs to a domain and
2076 * the domain's name cannot be NULL.
2078 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
2083 /* String table for /sys/kernel/security/tomoyo/stat interface. */
2084 static const char * const tomoyo_policy_headers
[TOMOYO_MAX_POLICY_STAT
] = {
2085 [TOMOYO_STAT_POLICY_UPDATES
] = "update:",
2086 [TOMOYO_STAT_POLICY_LEARNING
] = "violation in learning mode:",
2087 [TOMOYO_STAT_POLICY_PERMISSIVE
] = "violation in permissive mode:",
2088 [TOMOYO_STAT_POLICY_ENFORCING
] = "violation in enforcing mode:",
2091 /* String table for /sys/kernel/security/tomoyo/stat interface. */
2092 static const char * const tomoyo_memory_headers
[TOMOYO_MAX_MEMORY_STAT
] = {
2093 [TOMOYO_MEMORY_POLICY
] = "policy:",
2094 [TOMOYO_MEMORY_AUDIT
] = "audit log:",
2095 [TOMOYO_MEMORY_QUERY
] = "query message:",
2098 /* Timestamp counter for last updated. */
2099 static unsigned int tomoyo_stat_updated
[TOMOYO_MAX_POLICY_STAT
];
2100 /* Counter for number of updates. */
2101 static unsigned int tomoyo_stat_modified
[TOMOYO_MAX_POLICY_STAT
];
2104 * tomoyo_update_stat - Update statistic counters.
2106 * @index: Index for policy type.
2110 void tomoyo_update_stat(const u8 index
)
2113 do_gettimeofday(&tv
);
2115 * I don't use atomic operations because race condition is not fatal.
2117 tomoyo_stat_updated
[index
]++;
2118 tomoyo_stat_modified
[index
] = tv
.tv_sec
;
2122 * tomoyo_read_stat - Read statistic data.
2124 * @head: Pointer to "struct tomoyo_io_buffer".
2128 static void tomoyo_read_stat(struct tomoyo_io_buffer
*head
)
2131 unsigned int total
= 0;
2134 for (i
= 0; i
< TOMOYO_MAX_POLICY_STAT
; i
++) {
2135 tomoyo_io_printf(head
, "Policy %-30s %10u",
2136 tomoyo_policy_headers
[i
],
2137 tomoyo_stat_updated
[i
]);
2138 if (tomoyo_stat_modified
[i
]) {
2139 struct tomoyo_time stamp
;
2140 tomoyo_convert_time(tomoyo_stat_modified
[i
], &stamp
);
2141 tomoyo_io_printf(head
, " (Last: %04u/%02u/%02u "
2143 stamp
.year
, stamp
.month
, stamp
.day
,
2144 stamp
.hour
, stamp
.min
, stamp
.sec
);
2146 tomoyo_set_lf(head
);
2148 for (i
= 0; i
< TOMOYO_MAX_MEMORY_STAT
; i
++) {
2149 unsigned int used
= tomoyo_memory_used
[i
];
2151 tomoyo_io_printf(head
, "Memory used by %-22s %10u",
2152 tomoyo_memory_headers
[i
], used
);
2153 used
= tomoyo_memory_quota
[i
];
2155 tomoyo_io_printf(head
, " (Quota: %10u)", used
);
2156 tomoyo_set_lf(head
);
2158 tomoyo_io_printf(head
, "Total memory used: %10u\n",
2164 * tomoyo_write_stat - Set memory quota.
2166 * @head: Pointer to "struct tomoyo_io_buffer".
2170 static int tomoyo_write_stat(struct tomoyo_io_buffer
*head
)
2172 char *data
= head
->write_buf
;
2174 if (tomoyo_str_starts(&data
, "Memory used by "))
2175 for (i
= 0; i
< TOMOYO_MAX_MEMORY_STAT
; i
++)
2176 if (tomoyo_str_starts(&data
, tomoyo_memory_headers
[i
]))
2177 sscanf(data
, "%u", &tomoyo_memory_quota
[i
]);
2182 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2184 * @type: Type of interface.
2185 * @file: Pointer to "struct file".
2187 * Returns 0 on success, negative value otherwise.
2189 int tomoyo_open_control(const u8 type
, struct file
*file
)
2191 struct tomoyo_io_buffer
*head
= kzalloc(sizeof(*head
), GFP_NOFS
);
2195 mutex_init(&head
->io_sem
);
2198 case TOMOYO_DOMAINPOLICY
:
2199 /* /sys/kernel/security/tomoyo/domain_policy */
2200 head
->write
= tomoyo_write_domain
;
2201 head
->read
= tomoyo_read_domain
;
2203 case TOMOYO_EXCEPTIONPOLICY
:
2204 /* /sys/kernel/security/tomoyo/exception_policy */
2205 head
->write
= tomoyo_write_exception
;
2206 head
->read
= tomoyo_read_exception
;
2209 /* /sys/kernel/security/tomoyo/audit */
2210 head
->poll
= tomoyo_poll_log
;
2211 head
->read
= tomoyo_read_log
;
2213 case TOMOYO_SELFDOMAIN
:
2214 /* /sys/kernel/security/tomoyo/self_domain */
2215 head
->read
= tomoyo_read_self_domain
;
2217 case TOMOYO_PROCESS_STATUS
:
2218 /* /sys/kernel/security/tomoyo/.process_status */
2219 head
->write
= tomoyo_write_pid
;
2220 head
->read
= tomoyo_read_pid
;
2222 case TOMOYO_VERSION
:
2223 /* /sys/kernel/security/tomoyo/version */
2224 head
->read
= tomoyo_read_version
;
2225 head
->readbuf_size
= 128;
2228 /* /sys/kernel/security/tomoyo/stat */
2229 head
->write
= tomoyo_write_stat
;
2230 head
->read
= tomoyo_read_stat
;
2231 head
->readbuf_size
= 1024;
2233 case TOMOYO_PROFILE
:
2234 /* /sys/kernel/security/tomoyo/profile */
2235 head
->write
= tomoyo_write_profile
;
2236 head
->read
= tomoyo_read_profile
;
2238 case TOMOYO_QUERY
: /* /sys/kernel/security/tomoyo/query */
2239 head
->poll
= tomoyo_poll_query
;
2240 head
->write
= tomoyo_write_answer
;
2241 head
->read
= tomoyo_read_query
;
2243 case TOMOYO_MANAGER
:
2244 /* /sys/kernel/security/tomoyo/manager */
2245 head
->write
= tomoyo_write_manager
;
2246 head
->read
= tomoyo_read_manager
;
2249 if (!(file
->f_mode
& FMODE_READ
)) {
2251 * No need to allocate read_buf since it is not opened
2256 } else if (!head
->poll
) {
2257 /* Don't allocate read_buf for poll() access. */
2258 if (!head
->readbuf_size
)
2259 head
->readbuf_size
= 4096 * 2;
2260 head
->read_buf
= kzalloc(head
->readbuf_size
, GFP_NOFS
);
2261 if (!head
->read_buf
) {
2266 if (!(file
->f_mode
& FMODE_WRITE
)) {
2268 * No need to allocate write_buf since it is not opened
2272 } else if (head
->write
) {
2273 head
->writebuf_size
= 4096 * 2;
2274 head
->write_buf
= kzalloc(head
->writebuf_size
, GFP_NOFS
);
2275 if (!head
->write_buf
) {
2276 kfree(head
->read_buf
);
2282 * If the file is /sys/kernel/security/tomoyo/query , increment the
2284 * The obserber counter is used by tomoyo_supervisor() to see if
2285 * there is some process monitoring /sys/kernel/security/tomoyo/query.
2287 if (type
== TOMOYO_QUERY
)
2288 atomic_inc(&tomoyo_query_observers
);
2289 file
->private_data
= head
;
2290 tomoyo_notify_gc(head
, true);
2295 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
2297 * @file: Pointer to "struct file".
2298 * @wait: Pointer to "poll_table".
2300 * Waits for read readiness.
2301 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2302 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
2304 int tomoyo_poll_control(struct file
*file
, poll_table
*wait
)
2306 struct tomoyo_io_buffer
*head
= file
->private_data
;
2309 return head
->poll(file
, wait
);
2313 * tomoyo_set_namespace_cursor - Set namespace to read.
2315 * @head: Pointer to "struct tomoyo_io_buffer".
2319 static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer
*head
)
2321 struct list_head
*ns
;
2322 if (head
->type
!= TOMOYO_EXCEPTIONPOLICY
&&
2323 head
->type
!= TOMOYO_PROFILE
)
2326 * If this is the first read, or reading previous namespace finished
2327 * and has more namespaces to read, update the namespace cursor.
2330 if (!ns
|| (head
->r
.eof
&& ns
->next
!= &tomoyo_namespace_list
)) {
2331 /* Clearing is OK because tomoyo_flush() returned true. */
2332 memset(&head
->r
, 0, sizeof(head
->r
));
2333 head
->r
.ns
= ns
? ns
->next
: tomoyo_namespace_list
.next
;
2338 * tomoyo_has_more_namespace - Check for unread namespaces.
2340 * @head: Pointer to "struct tomoyo_io_buffer".
2342 * Returns true if we have more entries to print, false otherwise.
2344 static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer
*head
)
2346 return (head
->type
== TOMOYO_EXCEPTIONPOLICY
||
2347 head
->type
== TOMOYO_PROFILE
) && head
->r
.eof
&&
2348 head
->r
.ns
->next
!= &tomoyo_namespace_list
;
2352 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2354 * @head: Pointer to "struct tomoyo_io_buffer".
2355 * @buffer: Poiner to buffer to write to.
2356 * @buffer_len: Size of @buffer.
2358 * Returns bytes read on success, negative value otherwise.
2360 ssize_t
tomoyo_read_control(struct tomoyo_io_buffer
*head
, char __user
*buffer
,
2361 const int buffer_len
)
2368 if (mutex_lock_interruptible(&head
->io_sem
))
2370 head
->read_user_buf
= buffer
;
2371 head
->read_user_buf_avail
= buffer_len
;
2372 idx
= tomoyo_read_lock();
2373 if (tomoyo_flush(head
))
2374 /* Call the policy handler. */
2376 tomoyo_set_namespace_cursor(head
);
2378 } while (tomoyo_flush(head
) &&
2379 tomoyo_has_more_namespace(head
));
2380 tomoyo_read_unlock(idx
);
2381 len
= head
->read_user_buf
- buffer
;
2382 mutex_unlock(&head
->io_sem
);
2387 * tomoyo_parse_policy - Parse a policy line.
2389 * @head: Poiter to "struct tomoyo_io_buffer".
2390 * @line: Line to parse.
2392 * Returns 0 on success, negative value otherwise.
2394 * Caller holds tomoyo_read_lock().
2396 static int tomoyo_parse_policy(struct tomoyo_io_buffer
*head
, char *line
)
2398 /* Delete request? */
2399 head
->w
.is_delete
= !strncmp(line
, "delete ", 7);
2400 if (head
->w
.is_delete
)
2401 memmove(line
, line
+ 7, strlen(line
+ 7) + 1);
2402 /* Selecting namespace to update. */
2403 if (head
->type
== TOMOYO_EXCEPTIONPOLICY
||
2404 head
->type
== TOMOYO_PROFILE
) {
2406 char *cp
= strchr(line
, ' ');
2409 head
->w
.ns
= tomoyo_assign_namespace(line
);
2410 memmove(line
, cp
, strlen(cp
) + 1);
2414 head
->w
.ns
= &tomoyo_kernel_namespace
;
2415 /* Don't allow updating if namespace is invalid. */
2419 /* Do the update. */
2420 return head
->write(head
);
2424 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2426 * @head: Pointer to "struct tomoyo_io_buffer".
2427 * @buffer: Pointer to buffer to read from.
2428 * @buffer_len: Size of @buffer.
2430 * Returns @buffer_len on success, negative value otherwise.
2432 ssize_t
tomoyo_write_control(struct tomoyo_io_buffer
*head
,
2433 const char __user
*buffer
, const int buffer_len
)
2435 int error
= buffer_len
;
2436 size_t avail_len
= buffer_len
;
2437 char *cp0
= head
->write_buf
;
2441 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
2443 if (mutex_lock_interruptible(&head
->io_sem
))
2445 idx
= tomoyo_read_lock();
2446 /* Read a line and dispatch it to the policy handler. */
2447 while (avail_len
> 0) {
2449 if (head
->w
.avail
>= head
->writebuf_size
- 1) {
2450 const int len
= head
->writebuf_size
* 2;
2451 char *cp
= kzalloc(len
, GFP_NOFS
);
2456 memmove(cp
, cp0
, head
->w
.avail
);
2458 head
->write_buf
= cp
;
2460 head
->writebuf_size
= len
;
2462 if (get_user(c
, buffer
)) {
2468 cp0
[head
->w
.avail
++] = c
;
2471 cp0
[head
->w
.avail
- 1] = '\0';
2473 tomoyo_normalize_line(cp0
);
2474 if (!strcmp(cp0
, "reset")) {
2475 head
->w
.ns
= &tomoyo_kernel_namespace
;
2476 head
->w
.domain
= NULL
;
2477 memset(&head
->r
, 0, sizeof(head
->r
));
2480 /* Don't allow updating policies by non manager programs. */
2481 switch (head
->type
) {
2482 case TOMOYO_PROCESS_STATUS
:
2483 /* This does not write anything. */
2485 case TOMOYO_DOMAINPOLICY
:
2486 if (tomoyo_select_domain(head
, cp0
))
2489 case TOMOYO_EXCEPTIONPOLICY
:
2490 if (!strcmp(cp0
, "select transition_only")) {
2491 head
->r
.print_transition_related_only
= true;
2496 if (!tomoyo_manager()) {
2501 switch (tomoyo_parse_policy(head
, cp0
)) {
2506 switch (head
->type
) {
2507 case TOMOYO_DOMAINPOLICY
:
2508 case TOMOYO_EXCEPTIONPOLICY
:
2510 case TOMOYO_PROFILE
:
2511 case TOMOYO_MANAGER
:
2512 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
2521 tomoyo_read_unlock(idx
);
2522 mutex_unlock(&head
->io_sem
);
2527 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2529 * @head: Pointer to "struct tomoyo_io_buffer".
2533 int tomoyo_close_control(struct tomoyo_io_buffer
*head
)
2536 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2539 if (head
->type
== TOMOYO_QUERY
&&
2540 atomic_dec_and_test(&tomoyo_query_observers
))
2541 wake_up_all(&tomoyo_answer_wait
);
2542 tomoyo_notify_gc(head
, false);
2547 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2549 void tomoyo_check_profile(void)
2551 struct tomoyo_domain_info
*domain
;
2552 const int idx
= tomoyo_read_lock();
2553 tomoyo_policy_loaded
= true;
2554 printk(KERN_INFO
"TOMOYO: 2.4.0\n");
2555 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
2556 const u8 profile
= domain
->profile
;
2557 const struct tomoyo_policy_namespace
*ns
= domain
->ns
;
2558 if (ns
->profile_version
!= 20100903)
2560 "Profile version %u is not supported.\n",
2561 ns
->profile_version
);
2562 else if (!ns
->profile_ptr
[profile
])
2564 "Profile %u (used by '%s') is not defined.\n",
2565 profile
, domain
->domainname
->name
);
2569 "Userland tools for TOMOYO 2.4 must be installed and "
2570 "policy must be initialized.\n");
2571 printk(KERN_ERR
"Please see http://tomoyo.sourceforge.jp/2.4/ "
2572 "for more information.\n");
2575 tomoyo_read_unlock(idx
);
2576 printk(KERN_INFO
"Mandatory Access Control activated.\n");
2580 * tomoyo_load_builtin_policy - Load built-in policy.
2584 void __init
tomoyo_load_builtin_policy(void)
2587 * This include file is manually created and contains built-in policy
2588 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2589 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2590 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2592 #include "builtin-policy.h"
2594 const int idx
= tomoyo_read_lock();
2595 for (i
= 0; i
< 5; i
++) {
2596 struct tomoyo_io_buffer head
= { };
2600 start
= tomoyo_builtin_profile
;
2601 head
.type
= TOMOYO_PROFILE
;
2602 head
.write
= tomoyo_write_profile
;
2605 start
= tomoyo_builtin_exception_policy
;
2606 head
.type
= TOMOYO_EXCEPTIONPOLICY
;
2607 head
.write
= tomoyo_write_exception
;
2610 start
= tomoyo_builtin_domain_policy
;
2611 head
.type
= TOMOYO_DOMAINPOLICY
;
2612 head
.write
= tomoyo_write_domain
;
2615 start
= tomoyo_builtin_manager
;
2616 head
.type
= TOMOYO_MANAGER
;
2617 head
.write
= tomoyo_write_manager
;
2620 start
= tomoyo_builtin_stat
;
2621 head
.type
= TOMOYO_STAT
;
2622 head
.write
= tomoyo_write_stat
;
2626 char *end
= strchr(start
, '\n');
2630 tomoyo_normalize_line(start
);
2631 head
.write_buf
= start
;
2632 tomoyo_parse_policy(&head
, start
);
2636 tomoyo_read_unlock(idx
);
2637 #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2638 tomoyo_check_profile();