2 * security/tomoyo/common.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/uaccess.h>
8 #include <linux/slab.h>
9 #include <linux/security.h>
12 /* String table for operation mode. */
13 const char * const tomoyo_mode
[TOMOYO_CONFIG_MAX_MODE
] = {
14 [TOMOYO_CONFIG_DISABLED
] = "disabled",
15 [TOMOYO_CONFIG_LEARNING
] = "learning",
16 [TOMOYO_CONFIG_PERMISSIVE
] = "permissive",
17 [TOMOYO_CONFIG_ENFORCING
] = "enforcing"
20 /* String table for /sys/kernel/security/tomoyo/profile */
21 const char * const tomoyo_mac_keywords
[TOMOYO_MAX_MAC_INDEX
22 + TOMOYO_MAX_MAC_CATEGORY_INDEX
] = {
23 /* CONFIG::file group */
24 [TOMOYO_MAC_FILE_EXECUTE
] = "execute",
25 [TOMOYO_MAC_FILE_OPEN
] = "open",
26 [TOMOYO_MAC_FILE_CREATE
] = "create",
27 [TOMOYO_MAC_FILE_UNLINK
] = "unlink",
28 [TOMOYO_MAC_FILE_GETATTR
] = "getattr",
29 [TOMOYO_MAC_FILE_MKDIR
] = "mkdir",
30 [TOMOYO_MAC_FILE_RMDIR
] = "rmdir",
31 [TOMOYO_MAC_FILE_MKFIFO
] = "mkfifo",
32 [TOMOYO_MAC_FILE_MKSOCK
] = "mksock",
33 [TOMOYO_MAC_FILE_TRUNCATE
] = "truncate",
34 [TOMOYO_MAC_FILE_SYMLINK
] = "symlink",
35 [TOMOYO_MAC_FILE_MKBLOCK
] = "mkblock",
36 [TOMOYO_MAC_FILE_MKCHAR
] = "mkchar",
37 [TOMOYO_MAC_FILE_LINK
] = "link",
38 [TOMOYO_MAC_FILE_RENAME
] = "rename",
39 [TOMOYO_MAC_FILE_CHMOD
] = "chmod",
40 [TOMOYO_MAC_FILE_CHOWN
] = "chown",
41 [TOMOYO_MAC_FILE_CHGRP
] = "chgrp",
42 [TOMOYO_MAC_FILE_IOCTL
] = "ioctl",
43 [TOMOYO_MAC_FILE_CHROOT
] = "chroot",
44 [TOMOYO_MAC_FILE_MOUNT
] = "mount",
45 [TOMOYO_MAC_FILE_UMOUNT
] = "unmount",
46 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = "pivot_root",
47 /* CONFIG::network group */
48 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND
] = "inet_stream_bind",
49 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN
] = "inet_stream_listen",
50 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT
] = "inet_stream_connect",
51 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND
] = "inet_dgram_bind",
52 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND
] = "inet_dgram_send",
53 [TOMOYO_MAC_NETWORK_INET_RAW_BIND
] = "inet_raw_bind",
54 [TOMOYO_MAC_NETWORK_INET_RAW_SEND
] = "inet_raw_send",
55 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND
] = "unix_stream_bind",
56 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN
] = "unix_stream_listen",
57 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT
] = "unix_stream_connect",
58 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND
] = "unix_dgram_bind",
59 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND
] = "unix_dgram_send",
60 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND
] = "unix_seqpacket_bind",
61 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN
] = "unix_seqpacket_listen",
62 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT
] = "unix_seqpacket_connect",
63 /* CONFIG::misc group */
64 [TOMOYO_MAC_ENVIRON
] = "env",
66 [TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAC_CATEGORY_FILE
] = "file",
67 [TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAC_CATEGORY_NETWORK
] = "network",
68 [TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAC_CATEGORY_MISC
] = "misc",
71 /* String table for conditions. */
72 const char * const tomoyo_condition_keyword
[TOMOYO_MAX_CONDITION_KEYWORD
] = {
73 [TOMOYO_TASK_UID
] = "task.uid",
74 [TOMOYO_TASK_EUID
] = "task.euid",
75 [TOMOYO_TASK_SUID
] = "task.suid",
76 [TOMOYO_TASK_FSUID
] = "task.fsuid",
77 [TOMOYO_TASK_GID
] = "task.gid",
78 [TOMOYO_TASK_EGID
] = "task.egid",
79 [TOMOYO_TASK_SGID
] = "task.sgid",
80 [TOMOYO_TASK_FSGID
] = "task.fsgid",
81 [TOMOYO_TASK_PID
] = "task.pid",
82 [TOMOYO_TASK_PPID
] = "task.ppid",
83 [TOMOYO_EXEC_ARGC
] = "exec.argc",
84 [TOMOYO_EXEC_ENVC
] = "exec.envc",
85 [TOMOYO_TYPE_IS_SOCKET
] = "socket",
86 [TOMOYO_TYPE_IS_SYMLINK
] = "symlink",
87 [TOMOYO_TYPE_IS_FILE
] = "file",
88 [TOMOYO_TYPE_IS_BLOCK_DEV
] = "block",
89 [TOMOYO_TYPE_IS_DIRECTORY
] = "directory",
90 [TOMOYO_TYPE_IS_CHAR_DEV
] = "char",
91 [TOMOYO_TYPE_IS_FIFO
] = "fifo",
92 [TOMOYO_MODE_SETUID
] = "setuid",
93 [TOMOYO_MODE_SETGID
] = "setgid",
94 [TOMOYO_MODE_STICKY
] = "sticky",
95 [TOMOYO_MODE_OWNER_READ
] = "owner_read",
96 [TOMOYO_MODE_OWNER_WRITE
] = "owner_write",
97 [TOMOYO_MODE_OWNER_EXECUTE
] = "owner_execute",
98 [TOMOYO_MODE_GROUP_READ
] = "group_read",
99 [TOMOYO_MODE_GROUP_WRITE
] = "group_write",
100 [TOMOYO_MODE_GROUP_EXECUTE
] = "group_execute",
101 [TOMOYO_MODE_OTHERS_READ
] = "others_read",
102 [TOMOYO_MODE_OTHERS_WRITE
] = "others_write",
103 [TOMOYO_MODE_OTHERS_EXECUTE
] = "others_execute",
104 [TOMOYO_EXEC_REALPATH
] = "exec.realpath",
105 [TOMOYO_SYMLINK_TARGET
] = "symlink.target",
106 [TOMOYO_PATH1_UID
] = "path1.uid",
107 [TOMOYO_PATH1_GID
] = "path1.gid",
108 [TOMOYO_PATH1_INO
] = "path1.ino",
109 [TOMOYO_PATH1_MAJOR
] = "path1.major",
110 [TOMOYO_PATH1_MINOR
] = "path1.minor",
111 [TOMOYO_PATH1_PERM
] = "path1.perm",
112 [TOMOYO_PATH1_TYPE
] = "path1.type",
113 [TOMOYO_PATH1_DEV_MAJOR
] = "path1.dev_major",
114 [TOMOYO_PATH1_DEV_MINOR
] = "path1.dev_minor",
115 [TOMOYO_PATH2_UID
] = "path2.uid",
116 [TOMOYO_PATH2_GID
] = "path2.gid",
117 [TOMOYO_PATH2_INO
] = "path2.ino",
118 [TOMOYO_PATH2_MAJOR
] = "path2.major",
119 [TOMOYO_PATH2_MINOR
] = "path2.minor",
120 [TOMOYO_PATH2_PERM
] = "path2.perm",
121 [TOMOYO_PATH2_TYPE
] = "path2.type",
122 [TOMOYO_PATH2_DEV_MAJOR
] = "path2.dev_major",
123 [TOMOYO_PATH2_DEV_MINOR
] = "path2.dev_minor",
124 [TOMOYO_PATH1_PARENT_UID
] = "path1.parent.uid",
125 [TOMOYO_PATH1_PARENT_GID
] = "path1.parent.gid",
126 [TOMOYO_PATH1_PARENT_INO
] = "path1.parent.ino",
127 [TOMOYO_PATH1_PARENT_PERM
] = "path1.parent.perm",
128 [TOMOYO_PATH2_PARENT_UID
] = "path2.parent.uid",
129 [TOMOYO_PATH2_PARENT_GID
] = "path2.parent.gid",
130 [TOMOYO_PATH2_PARENT_INO
] = "path2.parent.ino",
131 [TOMOYO_PATH2_PARENT_PERM
] = "path2.parent.perm",
134 /* String table for PREFERENCE keyword. */
135 static const char * const tomoyo_pref_keywords
[TOMOYO_MAX_PREF
] = {
136 [TOMOYO_PREF_MAX_AUDIT_LOG
] = "max_audit_log",
137 [TOMOYO_PREF_MAX_LEARNING_ENTRY
] = "max_learning_entry",
140 /* String table for path operation. */
141 const char * const tomoyo_path_keyword
[TOMOYO_MAX_PATH_OPERATION
] = {
142 [TOMOYO_TYPE_EXECUTE
] = "execute",
143 [TOMOYO_TYPE_READ
] = "read",
144 [TOMOYO_TYPE_WRITE
] = "write",
145 [TOMOYO_TYPE_APPEND
] = "append",
146 [TOMOYO_TYPE_UNLINK
] = "unlink",
147 [TOMOYO_TYPE_GETATTR
] = "getattr",
148 [TOMOYO_TYPE_RMDIR
] = "rmdir",
149 [TOMOYO_TYPE_TRUNCATE
] = "truncate",
150 [TOMOYO_TYPE_SYMLINK
] = "symlink",
151 [TOMOYO_TYPE_CHROOT
] = "chroot",
152 [TOMOYO_TYPE_UMOUNT
] = "unmount",
155 /* String table for socket's operation. */
156 const char * const tomoyo_socket_keyword
[TOMOYO_MAX_NETWORK_OPERATION
] = {
157 [TOMOYO_NETWORK_BIND
] = "bind",
158 [TOMOYO_NETWORK_LISTEN
] = "listen",
159 [TOMOYO_NETWORK_CONNECT
] = "connect",
160 [TOMOYO_NETWORK_SEND
] = "send",
163 /* String table for categories. */
164 static const char * const tomoyo_category_keywords
165 [TOMOYO_MAX_MAC_CATEGORY_INDEX
] = {
166 [TOMOYO_MAC_CATEGORY_FILE
] = "file",
167 [TOMOYO_MAC_CATEGORY_NETWORK
] = "network",
168 [TOMOYO_MAC_CATEGORY_MISC
] = "misc",
171 /* Permit policy management by non-root user? */
172 static bool tomoyo_manage_by_non_root
;
174 /* Utility functions. */
177 * tomoyo_yesno - Return "yes" or "no".
179 * @value: Bool value.
181 const char *tomoyo_yesno(const unsigned int value
)
183 return value
? "yes" : "no";
187 * tomoyo_addprintf - strncat()-like-snprintf().
189 * @buffer: Buffer to write to. Must be '\0'-terminated.
190 * @len: Size of @buffer.
191 * @fmt: The printf()'s format string, followed by parameters.
195 static void tomoyo_addprintf(char *buffer
, int len
, const char *fmt
, ...)
198 const int pos
= strlen(buffer
);
200 vsnprintf(buffer
+ pos
, len
- pos
- 1, fmt
, args
);
205 * tomoyo_flush - Flush queued string to userspace's buffer.
207 * @head: Pointer to "struct tomoyo_io_buffer".
209 * Returns true if all data was flushed, false otherwise.
211 static bool tomoyo_flush(struct tomoyo_io_buffer
*head
)
213 while (head
->r
.w_pos
) {
214 const char *w
= head
->r
.w
[0];
215 size_t len
= strlen(w
);
217 if (len
> head
->read_user_buf_avail
)
218 len
= head
->read_user_buf_avail
;
221 if (copy_to_user(head
->read_user_buf
, w
, len
))
223 head
->read_user_buf_avail
-= len
;
224 head
->read_user_buf
+= len
;
230 /* Add '\0' for audit logs and query. */
232 if (!head
->read_user_buf_avail
||
233 copy_to_user(head
->read_user_buf
, "", 1))
235 head
->read_user_buf_avail
--;
236 head
->read_user_buf
++;
239 for (len
= 0; len
< head
->r
.w_pos
; len
++)
240 head
->r
.w
[len
] = head
->r
.w
[len
+ 1];
247 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
249 * @head: Pointer to "struct tomoyo_io_buffer".
250 * @string: String to print.
252 * Note that @string has to be kept valid until @head is kfree()d.
253 * This means that char[] allocated on stack memory cannot be passed to
254 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
256 static void tomoyo_set_string(struct tomoyo_io_buffer
*head
, const char *string
)
258 if (head
->r
.w_pos
< TOMOYO_MAX_IO_READ_QUEUE
) {
259 head
->r
.w
[head
->r
.w_pos
++] = string
;
266 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
268 * @head: Pointer to "struct tomoyo_io_buffer".
269 * @fmt: The printf()'s format string, followed by parameters.
271 void tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
275 size_t pos
= head
->r
.avail
;
276 int size
= head
->readbuf_size
- pos
;
280 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
) + 1;
282 if (pos
+ len
>= head
->readbuf_size
) {
286 head
->r
.avail
+= len
;
287 tomoyo_set_string(head
, head
->read_buf
+ pos
);
291 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
293 * @head: Pointer to "struct tomoyo_io_buffer".
297 static void tomoyo_set_space(struct tomoyo_io_buffer
*head
)
299 tomoyo_set_string(head
, " ");
303 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
305 * @head: Pointer to "struct tomoyo_io_buffer".
309 static bool tomoyo_set_lf(struct tomoyo_io_buffer
*head
)
311 tomoyo_set_string(head
, "\n");
312 return !head
->r
.w_pos
;
316 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
318 * @head: Pointer to "struct tomoyo_io_buffer".
322 static void tomoyo_set_slash(struct tomoyo_io_buffer
*head
)
324 tomoyo_set_string(head
, "/");
327 /* List of namespaces. */
328 LIST_HEAD(tomoyo_namespace_list
);
329 /* True if namespace other than tomoyo_kernel_namespace is defined. */
330 static bool tomoyo_namespace_enabled
;
333 * tomoyo_init_policy_namespace - Initialize namespace.
335 * @ns: Pointer to "struct tomoyo_policy_namespace".
339 void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace
*ns
)
342 for (idx
= 0; idx
< TOMOYO_MAX_ACL_GROUPS
; idx
++)
343 INIT_LIST_HEAD(&ns
->acl_group
[idx
]);
344 for (idx
= 0; idx
< TOMOYO_MAX_GROUP
; idx
++)
345 INIT_LIST_HEAD(&ns
->group_list
[idx
]);
346 for (idx
= 0; idx
< TOMOYO_MAX_POLICY
; idx
++)
347 INIT_LIST_HEAD(&ns
->policy_list
[idx
]);
348 ns
->profile_version
= 20110903;
349 tomoyo_namespace_enabled
= !list_empty(&tomoyo_namespace_list
);
350 list_add_tail_rcu(&ns
->namespace_list
, &tomoyo_namespace_list
);
354 * tomoyo_print_namespace - Print namespace header.
356 * @head: Pointer to "struct tomoyo_io_buffer".
360 static void tomoyo_print_namespace(struct tomoyo_io_buffer
*head
)
362 if (!tomoyo_namespace_enabled
)
364 tomoyo_set_string(head
,
365 container_of(head
->r
.ns
,
366 struct tomoyo_policy_namespace
,
367 namespace_list
)->name
);
368 tomoyo_set_space(head
);
372 * tomoyo_print_name_union - Print a tomoyo_name_union.
374 * @head: Pointer to "struct tomoyo_io_buffer".
375 * @ptr: Pointer to "struct tomoyo_name_union".
377 static void tomoyo_print_name_union(struct tomoyo_io_buffer
*head
,
378 const struct tomoyo_name_union
*ptr
)
380 tomoyo_set_space(head
);
382 tomoyo_set_string(head
, "@");
383 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
385 tomoyo_set_string(head
, ptr
->filename
->name
);
390 * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
392 * @head: Pointer to "struct tomoyo_io_buffer".
393 * @ptr: Pointer to "struct tomoyo_name_union".
397 static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer
*head
,
398 const struct tomoyo_name_union
*ptr
)
401 tomoyo_set_string(head
, "@");
402 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
404 tomoyo_set_string(head
, "\"");
405 tomoyo_set_string(head
, ptr
->filename
->name
);
406 tomoyo_set_string(head
, "\"");
411 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
413 * @head: Pointer to "struct tomoyo_io_buffer".
414 * @ptr: Pointer to "struct tomoyo_number_union".
418 static void tomoyo_print_number_union_nospace
419 (struct tomoyo_io_buffer
*head
, const struct tomoyo_number_union
*ptr
)
422 tomoyo_set_string(head
, "@");
423 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
426 unsigned long min
= ptr
->values
[0];
427 const unsigned long max
= ptr
->values
[1];
428 u8 min_type
= ptr
->value_type
[0];
429 const u8 max_type
= ptr
->value_type
[1];
432 for (i
= 0; i
< 2; i
++) {
434 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
435 tomoyo_addprintf(buffer
, sizeof(buffer
),
438 case TOMOYO_VALUE_TYPE_OCTAL
:
439 tomoyo_addprintf(buffer
, sizeof(buffer
),
443 tomoyo_addprintf(buffer
, sizeof(buffer
), "%lu",
447 if (min
== max
&& min_type
== max_type
)
449 tomoyo_addprintf(buffer
, sizeof(buffer
), "-");
453 tomoyo_io_printf(head
, "%s", buffer
);
458 * tomoyo_print_number_union - Print a tomoyo_number_union.
460 * @head: Pointer to "struct tomoyo_io_buffer".
461 * @ptr: Pointer to "struct tomoyo_number_union".
465 static void tomoyo_print_number_union(struct tomoyo_io_buffer
*head
,
466 const struct tomoyo_number_union
*ptr
)
468 tomoyo_set_space(head
);
469 tomoyo_print_number_union_nospace(head
, ptr
);
473 * tomoyo_assign_profile - Create a new profile.
475 * @ns: Pointer to "struct tomoyo_policy_namespace".
476 * @profile: Profile number to create.
478 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
480 static struct tomoyo_profile
*tomoyo_assign_profile
481 (struct tomoyo_policy_namespace
*ns
, const unsigned int profile
)
483 struct tomoyo_profile
*ptr
;
484 struct tomoyo_profile
*entry
;
485 if (profile
>= TOMOYO_MAX_PROFILES
)
487 ptr
= ns
->profile_ptr
[profile
];
490 entry
= kzalloc(sizeof(*entry
), GFP_NOFS
);
491 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
493 ptr
= ns
->profile_ptr
[profile
];
494 if (!ptr
&& tomoyo_memory_ok(entry
)) {
496 ptr
->default_config
= TOMOYO_CONFIG_DISABLED
|
497 TOMOYO_CONFIG_WANT_GRANT_LOG
|
498 TOMOYO_CONFIG_WANT_REJECT_LOG
;
499 memset(ptr
->config
, TOMOYO_CONFIG_USE_DEFAULT
,
500 sizeof(ptr
->config
));
501 ptr
->pref
[TOMOYO_PREF_MAX_AUDIT_LOG
] = 1024;
502 ptr
->pref
[TOMOYO_PREF_MAX_LEARNING_ENTRY
] = 2048;
503 mb(); /* Avoid out-of-order execution. */
504 ns
->profile_ptr
[profile
] = ptr
;
507 mutex_unlock(&tomoyo_policy_lock
);
514 * tomoyo_profile - Find a profile.
516 * @ns: Pointer to "struct tomoyo_policy_namespace".
517 * @profile: Profile number to find.
519 * Returns pointer to "struct tomoyo_profile".
521 struct tomoyo_profile
*tomoyo_profile(const struct tomoyo_policy_namespace
*ns
,
524 static struct tomoyo_profile tomoyo_null_profile
;
525 struct tomoyo_profile
*ptr
= ns
->profile_ptr
[profile
];
527 ptr
= &tomoyo_null_profile
;
532 * tomoyo_find_yesno - Find values for specified keyword.
534 * @string: String to check.
535 * @find: Name of keyword.
537 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
539 static s8
tomoyo_find_yesno(const char *string
, const char *find
)
541 const char *cp
= strstr(string
, find
);
544 if (!strncmp(cp
, "=yes", 4))
546 else if (!strncmp(cp
, "=no", 3))
553 * tomoyo_set_uint - Set value for specified preference.
555 * @i: Pointer to "unsigned int".
556 * @string: String to check.
557 * @find: Name of keyword.
561 static void tomoyo_set_uint(unsigned int *i
, const char *string
,
564 const char *cp
= strstr(string
, find
);
566 sscanf(cp
+ strlen(find
), "=%u", i
);
570 * tomoyo_set_mode - Set mode for specified profile.
572 * @name: Name of functionality.
573 * @value: Mode for @name.
574 * @profile: Pointer to "struct tomoyo_profile".
576 * Returns 0 on success, negative value otherwise.
578 static int tomoyo_set_mode(char *name
, const char *value
,
579 struct tomoyo_profile
*profile
)
583 if (!strcmp(name
, "CONFIG")) {
584 i
= TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
;
585 config
= profile
->default_config
;
586 } else if (tomoyo_str_starts(&name
, "CONFIG::")) {
588 for (i
= 0; i
< TOMOYO_MAX_MAC_INDEX
589 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; i
++) {
591 if (i
< TOMOYO_MAX_MAC_INDEX
) {
592 const u8 c
= tomoyo_index2category
[i
];
593 const char *category
=
594 tomoyo_category_keywords
[c
];
595 len
= strlen(category
);
596 if (strncmp(name
, category
, len
) ||
597 name
[len
++] != ':' || name
[len
++] != ':')
600 if (strcmp(name
+ len
, tomoyo_mac_keywords
[i
]))
602 config
= profile
->config
[i
];
605 if (i
== TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
610 if (strstr(value
, "use_default")) {
611 config
= TOMOYO_CONFIG_USE_DEFAULT
;
614 for (mode
= 0; mode
< 4; mode
++)
615 if (strstr(value
, tomoyo_mode
[mode
]))
617 * Update lower 3 bits in order to distinguish
618 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
620 config
= (config
& ~7) | mode
;
621 if (config
!= TOMOYO_CONFIG_USE_DEFAULT
) {
622 switch (tomoyo_find_yesno(value
, "grant_log")) {
624 config
|= TOMOYO_CONFIG_WANT_GRANT_LOG
;
627 config
&= ~TOMOYO_CONFIG_WANT_GRANT_LOG
;
630 switch (tomoyo_find_yesno(value
, "reject_log")) {
632 config
|= TOMOYO_CONFIG_WANT_REJECT_LOG
;
635 config
&= ~TOMOYO_CONFIG_WANT_REJECT_LOG
;
640 if (i
< TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
641 profile
->config
[i
] = config
;
642 else if (config
!= TOMOYO_CONFIG_USE_DEFAULT
)
643 profile
->default_config
= config
;
648 * tomoyo_write_profile - Write profile table.
650 * @head: Pointer to "struct tomoyo_io_buffer".
652 * Returns 0 on success, negative value otherwise.
654 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
656 char *data
= head
->write_buf
;
659 struct tomoyo_profile
*profile
;
660 if (sscanf(data
, "PROFILE_VERSION=%u", &head
->w
.ns
->profile_version
)
663 i
= simple_strtoul(data
, &cp
, 10);
667 profile
= tomoyo_assign_profile(head
->w
.ns
, i
);
670 cp
= strchr(data
, '=');
674 if (!strcmp(data
, "COMMENT")) {
675 static DEFINE_SPINLOCK(lock
);
676 const struct tomoyo_path_info
*new_comment
677 = tomoyo_get_name(cp
);
678 const struct tomoyo_path_info
*old_comment
;
682 old_comment
= profile
->comment
;
683 profile
->comment
= new_comment
;
685 tomoyo_put_name(old_comment
);
688 if (!strcmp(data
, "PREFERENCE")) {
689 for (i
= 0; i
< TOMOYO_MAX_PREF
; i
++)
690 tomoyo_set_uint(&profile
->pref
[i
], cp
,
691 tomoyo_pref_keywords
[i
]);
694 return tomoyo_set_mode(data
, cp
, profile
);
698 * tomoyo_print_config - Print mode for specified functionality.
700 * @head: Pointer to "struct tomoyo_io_buffer".
701 * @config: Mode for that functionality.
705 * Caller prints functionality's name.
707 static void tomoyo_print_config(struct tomoyo_io_buffer
*head
, const u8 config
)
709 tomoyo_io_printf(head
, "={ mode=%s grant_log=%s reject_log=%s }\n",
710 tomoyo_mode
[config
& 3],
711 tomoyo_yesno(config
& TOMOYO_CONFIG_WANT_GRANT_LOG
),
712 tomoyo_yesno(config
& TOMOYO_CONFIG_WANT_REJECT_LOG
));
716 * tomoyo_read_profile - Read profile table.
718 * @head: Pointer to "struct tomoyo_io_buffer".
722 static void tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
725 struct tomoyo_policy_namespace
*ns
=
726 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
727 const struct tomoyo_profile
*profile
;
731 index
= head
->r
.index
;
732 profile
= ns
->profile_ptr
[index
];
733 switch (head
->r
.step
) {
735 tomoyo_print_namespace(head
);
736 tomoyo_io_printf(head
, "PROFILE_VERSION=%u\n",
737 ns
->profile_version
);
741 for ( ; head
->r
.index
< TOMOYO_MAX_PROFILES
;
743 if (ns
->profile_ptr
[head
->r
.index
])
745 if (head
->r
.index
== TOMOYO_MAX_PROFILES
)
752 const struct tomoyo_path_info
*comment
=
754 tomoyo_print_namespace(head
);
755 tomoyo_io_printf(head
, "%u-COMMENT=", index
);
756 tomoyo_set_string(head
, comment
? comment
->name
: "");
758 tomoyo_io_printf(head
, "%u-PREFERENCE={ ", index
);
759 for (i
= 0; i
< TOMOYO_MAX_PREF
; i
++)
760 tomoyo_io_printf(head
, "%s=%u ",
761 tomoyo_pref_keywords
[i
],
763 tomoyo_set_string(head
, "}\n");
769 tomoyo_print_namespace(head
);
770 tomoyo_io_printf(head
, "%u-%s", index
, "CONFIG");
771 tomoyo_print_config(head
, profile
->default_config
);
777 for ( ; head
->r
.bit
< TOMOYO_MAX_MAC_INDEX
778 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; head
->r
.bit
++) {
779 const u8 i
= head
->r
.bit
;
780 const u8 config
= profile
->config
[i
];
781 if (config
== TOMOYO_CONFIG_USE_DEFAULT
)
783 tomoyo_print_namespace(head
);
784 if (i
< TOMOYO_MAX_MAC_INDEX
)
785 tomoyo_io_printf(head
, "%u-CONFIG::%s::%s",
787 tomoyo_category_keywords
788 [tomoyo_index2category
[i
]],
789 tomoyo_mac_keywords
[i
]);
791 tomoyo_io_printf(head
, "%u-CONFIG::%s", index
,
792 tomoyo_mac_keywords
[i
]);
793 tomoyo_print_config(head
, config
);
797 if (head
->r
.bit
== TOMOYO_MAX_MAC_INDEX
798 + TOMOYO_MAX_MAC_CATEGORY_INDEX
) {
804 if (tomoyo_flush(head
))
809 * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
811 * @a: Pointer to "struct tomoyo_acl_head".
812 * @b: Pointer to "struct tomoyo_acl_head".
814 * Returns true if @a == @b, false otherwise.
816 static bool tomoyo_same_manager(const struct tomoyo_acl_head
*a
,
817 const struct tomoyo_acl_head
*b
)
819 return container_of(a
, struct tomoyo_manager
, head
)->manager
==
820 container_of(b
, struct tomoyo_manager
, head
)->manager
;
824 * tomoyo_update_manager_entry - Add a manager entry.
826 * @manager: The path to manager or the domainnamme.
827 * @is_delete: True if it is a delete request.
829 * Returns 0 on success, negative value otherwise.
831 * Caller holds tomoyo_read_lock().
833 static int tomoyo_update_manager_entry(const char *manager
,
834 const bool is_delete
)
836 struct tomoyo_manager e
= { };
837 struct tomoyo_acl_param param
= {
838 /* .ns = &tomoyo_kernel_namespace, */
839 .is_delete
= is_delete
,
840 .list
= &tomoyo_kernel_namespace
.
841 policy_list
[TOMOYO_ID_MANAGER
],
843 int error
= is_delete
? -ENOENT
: -ENOMEM
;
844 if (tomoyo_domain_def(manager
)) {
845 if (!tomoyo_correct_domain(manager
))
849 if (!tomoyo_correct_path(manager
))
852 e
.manager
= tomoyo_get_name(manager
);
854 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), ¶m
,
855 tomoyo_same_manager
);
856 tomoyo_put_name(e
.manager
);
862 * tomoyo_write_manager - Write manager policy.
864 * @head: Pointer to "struct tomoyo_io_buffer".
866 * Returns 0 on success, negative value otherwise.
868 * Caller holds tomoyo_read_lock().
870 static int tomoyo_write_manager(struct tomoyo_io_buffer
*head
)
872 char *data
= head
->write_buf
;
874 if (!strcmp(data
, "manage_by_non_root")) {
875 tomoyo_manage_by_non_root
= !head
->w
.is_delete
;
878 return tomoyo_update_manager_entry(data
, head
->w
.is_delete
);
882 * tomoyo_read_manager - Read manager policy.
884 * @head: Pointer to "struct tomoyo_io_buffer".
886 * Caller holds tomoyo_read_lock().
888 static void tomoyo_read_manager(struct tomoyo_io_buffer
*head
)
892 list_for_each_cookie(head
->r
.acl
, &tomoyo_kernel_namespace
.
893 policy_list
[TOMOYO_ID_MANAGER
]) {
894 struct tomoyo_manager
*ptr
=
895 list_entry(head
->r
.acl
, typeof(*ptr
), head
.list
);
896 if (ptr
->head
.is_deleted
)
898 if (!tomoyo_flush(head
))
900 tomoyo_set_string(head
, ptr
->manager
->name
);
907 * tomoyo_manager - Check whether the current process is a policy manager.
909 * Returns true if the current process is permitted to modify policy
910 * via /sys/kernel/security/tomoyo/ interface.
912 * Caller holds tomoyo_read_lock().
914 static bool tomoyo_manager(void)
916 struct tomoyo_manager
*ptr
;
918 const struct task_struct
*task
= current
;
919 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
922 if (!tomoyo_policy_loaded
)
924 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
926 list_for_each_entry_rcu(ptr
, &tomoyo_kernel_namespace
.
927 policy_list
[TOMOYO_ID_MANAGER
], head
.list
) {
928 if (!ptr
->head
.is_deleted
&& ptr
->is_domain
929 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
936 exe
= tomoyo_get_exe();
939 list_for_each_entry_rcu(ptr
, &tomoyo_kernel_namespace
.
940 policy_list
[TOMOYO_ID_MANAGER
], head
.list
) {
941 if (!ptr
->head
.is_deleted
&& !ptr
->is_domain
942 && !strcmp(exe
, ptr
->manager
->name
)) {
947 if (!found
) { /* Reduce error messages. */
948 static pid_t last_pid
;
949 const pid_t pid
= current
->pid
;
950 if (last_pid
!= pid
) {
951 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
952 "update policies.\n", domainname
->name
, exe
);
961 * tomoyo_select_domain - Parse select command.
963 * @head: Pointer to "struct tomoyo_io_buffer".
964 * @data: String to parse.
966 * Returns true on success, false otherwise.
968 * Caller holds tomoyo_read_lock().
970 static bool tomoyo_select_domain(struct tomoyo_io_buffer
*head
,
974 struct tomoyo_domain_info
*domain
= NULL
;
975 bool global_pid
= false;
976 if (strncmp(data
, "select ", 7))
979 if (sscanf(data
, "pid=%u", &pid
) == 1 ||
980 (global_pid
= true, sscanf(data
, "global-pid=%u", &pid
) == 1)) {
981 struct task_struct
*p
;
983 read_lock(&tasklist_lock
);
985 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
987 p
= find_task_by_vpid(pid
);
989 domain
= tomoyo_real_domain(p
);
990 read_unlock(&tasklist_lock
);
992 } else if (!strncmp(data
, "domain=", 7)) {
993 if (tomoyo_domain_def(data
+ 7))
994 domain
= tomoyo_find_domain(data
+ 7);
997 head
->w
.domain
= domain
;
998 /* Accessing read_buf is safe because head->io_sem is held. */
1000 return true; /* Do nothing if open(O_WRONLY). */
1001 memset(&head
->r
, 0, sizeof(head
->r
));
1002 head
->r
.print_this_domain_only
= true;
1004 head
->r
.domain
= &domain
->list
;
1007 tomoyo_io_printf(head
, "# select %s\n", data
);
1008 if (domain
&& domain
->is_deleted
)
1009 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
1014 * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1016 * @a: Pointer to "struct tomoyo_acl_info".
1017 * @b: Pointer to "struct tomoyo_acl_info".
1019 * Returns true if @a == @b, false otherwise.
1021 static bool tomoyo_same_task_acl(const struct tomoyo_acl_info
*a
,
1022 const struct tomoyo_acl_info
*b
)
1024 const struct tomoyo_task_acl
*p1
= container_of(a
, typeof(*p1
), head
);
1025 const struct tomoyo_task_acl
*p2
= container_of(b
, typeof(*p2
), head
);
1026 return p1
->domainname
== p2
->domainname
;
1030 * tomoyo_write_task - Update task related list.
1032 * @param: Pointer to "struct tomoyo_acl_param".
1034 * Returns 0 on success, negative value otherwise.
1036 * Caller holds tomoyo_read_lock().
1038 static int tomoyo_write_task(struct tomoyo_acl_param
*param
)
1040 int error
= -EINVAL
;
1041 if (tomoyo_str_starts(¶m
->data
, "manual_domain_transition ")) {
1042 struct tomoyo_task_acl e
= {
1043 .head
.type
= TOMOYO_TYPE_MANUAL_TASK_ACL
,
1044 .domainname
= tomoyo_get_domainname(param
),
1047 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
1048 tomoyo_same_task_acl
,
1050 tomoyo_put_name(e
.domainname
);
1056 * tomoyo_delete_domain - Delete a domain.
1058 * @domainname: The name of domain.
1062 * Caller holds tomoyo_read_lock().
1064 static int tomoyo_delete_domain(char *domainname
)
1066 struct tomoyo_domain_info
*domain
;
1067 struct tomoyo_path_info name
;
1069 name
.name
= domainname
;
1070 tomoyo_fill_path_info(&name
);
1071 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
1073 /* Is there an active domain? */
1074 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
1075 /* Never delete tomoyo_kernel_domain */
1076 if (domain
== &tomoyo_kernel_domain
)
1078 if (domain
->is_deleted
||
1079 tomoyo_pathcmp(domain
->domainname
, &name
))
1081 domain
->is_deleted
= true;
1084 mutex_unlock(&tomoyo_policy_lock
);
1089 * tomoyo_write_domain2 - Write domain policy.
1091 * @ns: Pointer to "struct tomoyo_policy_namespace".
1092 * @list: Pointer to "struct list_head".
1093 * @data: Policy to be interpreted.
1094 * @is_delete: True if it is a delete request.
1096 * Returns 0 on success, negative value otherwise.
1098 * Caller holds tomoyo_read_lock().
1100 static int tomoyo_write_domain2(struct tomoyo_policy_namespace
*ns
,
1101 struct list_head
*list
, char *data
,
1102 const bool is_delete
)
1104 struct tomoyo_acl_param param
= {
1108 .is_delete
= is_delete
,
1110 static const struct {
1111 const char *keyword
;
1112 int (*write
) (struct tomoyo_acl_param
*);
1113 } tomoyo_callback
[5] = {
1114 { "file ", tomoyo_write_file
},
1115 { "network inet ", tomoyo_write_inet_network
},
1116 { "network unix ", tomoyo_write_unix_network
},
1117 { "misc ", tomoyo_write_misc
},
1118 { "task ", tomoyo_write_task
},
1122 for (i
= 0; i
< ARRAY_SIZE(tomoyo_callback
); i
++) {
1123 if (!tomoyo_str_starts(¶m
.data
,
1124 tomoyo_callback
[i
].keyword
))
1126 return tomoyo_callback
[i
].write(¶m
);
1131 /* String table for domain flags. */
1132 const char * const tomoyo_dif
[TOMOYO_MAX_DOMAIN_INFO_FLAGS
] = {
1133 [TOMOYO_DIF_QUOTA_WARNED
] = "quota_exceeded\n",
1134 [TOMOYO_DIF_TRANSITION_FAILED
] = "transition_failed\n",
1138 * tomoyo_write_domain - Write domain policy.
1140 * @head: Pointer to "struct tomoyo_io_buffer".
1142 * Returns 0 on success, negative value otherwise.
1144 * Caller holds tomoyo_read_lock().
1146 static int tomoyo_write_domain(struct tomoyo_io_buffer
*head
)
1148 char *data
= head
->write_buf
;
1149 struct tomoyo_policy_namespace
*ns
;
1150 struct tomoyo_domain_info
*domain
= head
->w
.domain
;
1151 const bool is_delete
= head
->w
.is_delete
;
1152 bool is_select
= !is_delete
&& tomoyo_str_starts(&data
, "select ");
1153 unsigned int profile
;
1157 tomoyo_delete_domain(data
);
1159 domain
= tomoyo_find_domain(data
);
1161 domain
= tomoyo_assign_domain(data
, false);
1162 head
->w
.domain
= domain
;
1168 if (sscanf(data
, "use_profile %u", &profile
) == 1
1169 && profile
< TOMOYO_MAX_PROFILES
) {
1170 if (!tomoyo_policy_loaded
|| ns
->profile_ptr
[profile
])
1171 domain
->profile
= (u8
) profile
;
1174 if (sscanf(data
, "use_group %u\n", &profile
) == 1
1175 && profile
< TOMOYO_MAX_ACL_GROUPS
) {
1177 domain
->group
= (u8
) profile
;
1180 for (profile
= 0; profile
< TOMOYO_MAX_DOMAIN_INFO_FLAGS
; profile
++) {
1181 const char *cp
= tomoyo_dif
[profile
];
1182 if (strncmp(data
, cp
, strlen(cp
) - 1))
1184 domain
->flags
[profile
] = !is_delete
;
1187 return tomoyo_write_domain2(ns
, &domain
->acl_info_list
, data
,
1192 * tomoyo_print_condition - Print condition part.
1194 * @head: Pointer to "struct tomoyo_io_buffer".
1195 * @cond: Pointer to "struct tomoyo_condition".
1197 * Returns true on success, false otherwise.
1199 static bool tomoyo_print_condition(struct tomoyo_io_buffer
*head
,
1200 const struct tomoyo_condition
*cond
)
1202 switch (head
->r
.cond_step
) {
1204 head
->r
.cond_index
= 0;
1205 head
->r
.cond_step
++;
1206 if (cond
->transit
) {
1207 tomoyo_set_space(head
);
1208 tomoyo_set_string(head
, cond
->transit
->name
);
1213 const u16 condc
= cond
->condc
;
1214 const struct tomoyo_condition_element
*condp
=
1215 (typeof(condp
)) (cond
+ 1);
1216 const struct tomoyo_number_union
*numbers_p
=
1217 (typeof(numbers_p
)) (condp
+ condc
);
1218 const struct tomoyo_name_union
*names_p
=
1220 (numbers_p
+ cond
->numbers_count
);
1221 const struct tomoyo_argv
*argv
=
1222 (typeof(argv
)) (names_p
+ cond
->names_count
);
1223 const struct tomoyo_envp
*envp
=
1224 (typeof(envp
)) (argv
+ cond
->argc
);
1226 for (skip
= 0; skip
< head
->r
.cond_index
; skip
++) {
1227 const u8 left
= condp
->left
;
1228 const u8 right
= condp
->right
;
1231 case TOMOYO_ARGV_ENTRY
:
1234 case TOMOYO_ENVP_ENTRY
:
1237 case TOMOYO_NUMBER_UNION
:
1242 case TOMOYO_NAME_UNION
:
1245 case TOMOYO_NUMBER_UNION
:
1250 while (head
->r
.cond_index
< condc
) {
1251 const u8 match
= condp
->equals
;
1252 const u8 left
= condp
->left
;
1253 const u8 right
= condp
->right
;
1254 if (!tomoyo_flush(head
))
1257 head
->r
.cond_index
++;
1258 tomoyo_set_space(head
);
1260 case TOMOYO_ARGV_ENTRY
:
1261 tomoyo_io_printf(head
,
1262 "exec.argv[%lu]%s=\"",
1265 tomoyo_set_string(head
,
1267 tomoyo_set_string(head
, "\"");
1270 case TOMOYO_ENVP_ENTRY
:
1271 tomoyo_set_string(head
,
1273 tomoyo_set_string(head
,
1275 tomoyo_io_printf(head
, "\"]%s=", envp
->
1278 tomoyo_set_string(head
, "\"");
1279 tomoyo_set_string(head
, envp
->
1281 tomoyo_set_string(head
, "\"");
1283 tomoyo_set_string(head
,
1288 case TOMOYO_NUMBER_UNION
:
1289 tomoyo_print_number_union_nospace
1290 (head
, numbers_p
++);
1293 tomoyo_set_string(head
,
1294 tomoyo_condition_keyword
[left
]);
1297 tomoyo_set_string(head
, match
? "=" : "!=");
1299 case TOMOYO_NAME_UNION
:
1300 tomoyo_print_name_union_quoted
1303 case TOMOYO_NUMBER_UNION
:
1304 tomoyo_print_number_union_nospace
1305 (head
, numbers_p
++);
1308 tomoyo_set_string(head
,
1309 tomoyo_condition_keyword
[right
]);
1314 head
->r
.cond_step
++;
1317 if (!tomoyo_flush(head
))
1319 head
->r
.cond_step
++;
1322 if (cond
->grant_log
!= TOMOYO_GRANTLOG_AUTO
)
1323 tomoyo_io_printf(head
, " grant_log=%s",
1324 tomoyo_yesno(cond
->grant_log
==
1325 TOMOYO_GRANTLOG_YES
));
1326 tomoyo_set_lf(head
);
1333 * tomoyo_set_group - Print "acl_group " header keyword and category name.
1335 * @head: Pointer to "struct tomoyo_io_buffer".
1336 * @category: Category name.
1340 static void tomoyo_set_group(struct tomoyo_io_buffer
*head
,
1341 const char *category
)
1343 if (head
->type
== TOMOYO_EXCEPTIONPOLICY
) {
1344 tomoyo_print_namespace(head
);
1345 tomoyo_io_printf(head
, "acl_group %u ",
1346 head
->r
.acl_group_index
);
1348 tomoyo_set_string(head
, category
);
1352 * tomoyo_print_entry - Print an ACL entry.
1354 * @head: Pointer to "struct tomoyo_io_buffer".
1355 * @acl: Pointer to an ACL entry.
1357 * Returns true on success, false otherwise.
1359 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
1360 struct tomoyo_acl_info
*acl
)
1362 const u8 acl_type
= acl
->type
;
1366 if (head
->r
.print_cond_part
)
1367 goto print_cond_part
;
1368 if (acl
->is_deleted
)
1370 if (!tomoyo_flush(head
))
1372 else if (acl_type
== TOMOYO_TYPE_PATH_ACL
) {
1373 struct tomoyo_path_acl
*ptr
=
1374 container_of(acl
, typeof(*ptr
), head
);
1375 const u16 perm
= ptr
->perm
;
1376 for (bit
= 0; bit
< TOMOYO_MAX_PATH_OPERATION
; bit
++) {
1377 if (!(perm
& (1 << bit
)))
1379 if (head
->r
.print_transition_related_only
&&
1380 bit
!= TOMOYO_TYPE_EXECUTE
)
1383 tomoyo_set_group(head
, "file ");
1386 tomoyo_set_slash(head
);
1388 tomoyo_set_string(head
, tomoyo_path_keyword
[bit
]);
1392 tomoyo_print_name_union(head
, &ptr
->name
);
1393 } else if (acl_type
== TOMOYO_TYPE_MANUAL_TASK_ACL
) {
1394 struct tomoyo_task_acl
*ptr
=
1395 container_of(acl
, typeof(*ptr
), head
);
1396 tomoyo_set_group(head
, "task ");
1397 tomoyo_set_string(head
, "manual_domain_transition ");
1398 tomoyo_set_string(head
, ptr
->domainname
->name
);
1399 } else if (head
->r
.print_transition_related_only
) {
1401 } else if (acl_type
== TOMOYO_TYPE_PATH2_ACL
) {
1402 struct tomoyo_path2_acl
*ptr
=
1403 container_of(acl
, typeof(*ptr
), head
);
1404 const u8 perm
= ptr
->perm
;
1405 for (bit
= 0; bit
< TOMOYO_MAX_PATH2_OPERATION
; bit
++) {
1406 if (!(perm
& (1 << bit
)))
1409 tomoyo_set_group(head
, "file ");
1412 tomoyo_set_slash(head
);
1414 tomoyo_set_string(head
, tomoyo_mac_keywords
1415 [tomoyo_pp2mac
[bit
]]);
1419 tomoyo_print_name_union(head
, &ptr
->name1
);
1420 tomoyo_print_name_union(head
, &ptr
->name2
);
1421 } else if (acl_type
== TOMOYO_TYPE_PATH_NUMBER_ACL
) {
1422 struct tomoyo_path_number_acl
*ptr
=
1423 container_of(acl
, typeof(*ptr
), head
);
1424 const u8 perm
= ptr
->perm
;
1425 for (bit
= 0; bit
< TOMOYO_MAX_PATH_NUMBER_OPERATION
; bit
++) {
1426 if (!(perm
& (1 << bit
)))
1429 tomoyo_set_group(head
, "file ");
1432 tomoyo_set_slash(head
);
1434 tomoyo_set_string(head
, tomoyo_mac_keywords
1435 [tomoyo_pn2mac
[bit
]]);
1439 tomoyo_print_name_union(head
, &ptr
->name
);
1440 tomoyo_print_number_union(head
, &ptr
->number
);
1441 } else if (acl_type
== TOMOYO_TYPE_MKDEV_ACL
) {
1442 struct tomoyo_mkdev_acl
*ptr
=
1443 container_of(acl
, typeof(*ptr
), head
);
1444 const u8 perm
= ptr
->perm
;
1445 for (bit
= 0; bit
< TOMOYO_MAX_MKDEV_OPERATION
; bit
++) {
1446 if (!(perm
& (1 << bit
)))
1449 tomoyo_set_group(head
, "file ");
1452 tomoyo_set_slash(head
);
1454 tomoyo_set_string(head
, tomoyo_mac_keywords
1455 [tomoyo_pnnn2mac
[bit
]]);
1459 tomoyo_print_name_union(head
, &ptr
->name
);
1460 tomoyo_print_number_union(head
, &ptr
->mode
);
1461 tomoyo_print_number_union(head
, &ptr
->major
);
1462 tomoyo_print_number_union(head
, &ptr
->minor
);
1463 } else if (acl_type
== TOMOYO_TYPE_INET_ACL
) {
1464 struct tomoyo_inet_acl
*ptr
=
1465 container_of(acl
, typeof(*ptr
), head
);
1466 const u8 perm
= ptr
->perm
;
1468 for (bit
= 0; bit
< TOMOYO_MAX_NETWORK_OPERATION
; bit
++) {
1469 if (!(perm
& (1 << bit
)))
1472 tomoyo_set_group(head
, "network inet ");
1473 tomoyo_set_string(head
, tomoyo_proto_keyword
1475 tomoyo_set_space(head
);
1478 tomoyo_set_slash(head
);
1480 tomoyo_set_string(head
, tomoyo_socket_keyword
[bit
]);
1484 tomoyo_set_space(head
);
1485 if (ptr
->address
.group
) {
1486 tomoyo_set_string(head
, "@");
1487 tomoyo_set_string(head
, ptr
->address
.group
->group_name
1491 tomoyo_print_ip(buf
, sizeof(buf
), &ptr
->address
);
1492 tomoyo_io_printf(head
, "%s", buf
);
1494 tomoyo_print_number_union(head
, &ptr
->port
);
1495 } else if (acl_type
== TOMOYO_TYPE_UNIX_ACL
) {
1496 struct tomoyo_unix_acl
*ptr
=
1497 container_of(acl
, typeof(*ptr
), head
);
1498 const u8 perm
= ptr
->perm
;
1500 for (bit
= 0; bit
< TOMOYO_MAX_NETWORK_OPERATION
; bit
++) {
1501 if (!(perm
& (1 << bit
)))
1504 tomoyo_set_group(head
, "network unix ");
1505 tomoyo_set_string(head
, tomoyo_proto_keyword
1507 tomoyo_set_space(head
);
1510 tomoyo_set_slash(head
);
1512 tomoyo_set_string(head
, tomoyo_socket_keyword
[bit
]);
1516 tomoyo_print_name_union(head
, &ptr
->name
);
1517 } else if (acl_type
== TOMOYO_TYPE_MOUNT_ACL
) {
1518 struct tomoyo_mount_acl
*ptr
=
1519 container_of(acl
, typeof(*ptr
), head
);
1520 tomoyo_set_group(head
, "file mount");
1521 tomoyo_print_name_union(head
, &ptr
->dev_name
);
1522 tomoyo_print_name_union(head
, &ptr
->dir_name
);
1523 tomoyo_print_name_union(head
, &ptr
->fs_type
);
1524 tomoyo_print_number_union(head
, &ptr
->flags
);
1525 } else if (acl_type
== TOMOYO_TYPE_ENV_ACL
) {
1526 struct tomoyo_env_acl
*ptr
=
1527 container_of(acl
, typeof(*ptr
), head
);
1529 tomoyo_set_group(head
, "misc env ");
1530 tomoyo_set_string(head
, ptr
->env
->name
);
1533 head
->r
.print_cond_part
= true;
1534 head
->r
.cond_step
= 0;
1535 if (!tomoyo_flush(head
))
1538 if (!tomoyo_print_condition(head
, acl
->cond
))
1540 head
->r
.print_cond_part
= false;
1542 tomoyo_set_lf(head
);
1548 * tomoyo_read_domain2 - Read domain policy.
1550 * @head: Pointer to "struct tomoyo_io_buffer".
1551 * @list: Pointer to "struct list_head".
1553 * Caller holds tomoyo_read_lock().
1555 * Returns true on success, false otherwise.
1557 static bool tomoyo_read_domain2(struct tomoyo_io_buffer
*head
,
1558 struct list_head
*list
)
1560 list_for_each_cookie(head
->r
.acl
, list
) {
1561 struct tomoyo_acl_info
*ptr
=
1562 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1563 if (!tomoyo_print_entry(head
, ptr
))
1571 * tomoyo_read_domain - Read domain policy.
1573 * @head: Pointer to "struct tomoyo_io_buffer".
1575 * Caller holds tomoyo_read_lock().
1577 static void tomoyo_read_domain(struct tomoyo_io_buffer
*head
)
1581 list_for_each_cookie(head
->r
.domain
, &tomoyo_domain_list
) {
1582 struct tomoyo_domain_info
*domain
=
1583 list_entry(head
->r
.domain
, typeof(*domain
), list
);
1584 switch (head
->r
.step
) {
1587 if (domain
->is_deleted
&&
1588 !head
->r
.print_this_domain_only
)
1590 /* Print domainname and flags. */
1591 tomoyo_set_string(head
, domain
->domainname
->name
);
1592 tomoyo_set_lf(head
);
1593 tomoyo_io_printf(head
, "use_profile %u\n",
1595 tomoyo_io_printf(head
, "use_group %u\n",
1597 for (i
= 0; i
< TOMOYO_MAX_DOMAIN_INFO_FLAGS
; i
++)
1598 if (domain
->flags
[i
])
1599 tomoyo_set_string(head
, tomoyo_dif
[i
]);
1601 tomoyo_set_lf(head
);
1604 if (!tomoyo_read_domain2(head
, &domain
->acl_info_list
))
1607 if (!tomoyo_set_lf(head
))
1612 if (head
->r
.print_this_domain_only
)
1621 * tomoyo_write_pid: Specify PID to obtain domainname.
1623 * @head: Pointer to "struct tomoyo_io_buffer".
1627 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1629 head
->r
.eof
= false;
1634 * tomoyo_read_pid - Get domainname of the specified PID.
1636 * @head: Pointer to "struct tomoyo_io_buffer".
1638 * Returns the domainname which the specified PID is in on success,
1639 * empty string otherwise.
1640 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1641 * using read()/write() interface rather than sysctl() interface.
1643 static void tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1645 char *buf
= head
->write_buf
;
1646 bool global_pid
= false;
1648 struct task_struct
*p
;
1649 struct tomoyo_domain_info
*domain
= NULL
;
1651 /* Accessing write_buf is safe because head->io_sem is held. */
1654 return; /* Do nothing if open(O_RDONLY). */
1656 if (head
->r
.w_pos
|| head
->r
.eof
)
1659 if (tomoyo_str_starts(&buf
, "global-pid "))
1661 pid
= (unsigned int) simple_strtoul(buf
, NULL
, 10);
1663 read_lock(&tasklist_lock
);
1665 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
1667 p
= find_task_by_vpid(pid
);
1669 domain
= tomoyo_real_domain(p
);
1670 read_unlock(&tasklist_lock
);
1674 tomoyo_io_printf(head
, "%u %u ", pid
, domain
->profile
);
1675 tomoyo_set_string(head
, domain
->domainname
->name
);
1678 /* String table for domain transition control keywords. */
1679 static const char *tomoyo_transition_type
[TOMOYO_MAX_TRANSITION_TYPE
] = {
1680 [TOMOYO_TRANSITION_CONTROL_NO_RESET
] = "no_reset_domain ",
1681 [TOMOYO_TRANSITION_CONTROL_RESET
] = "reset_domain ",
1682 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
] = "no_initialize_domain ",
1683 [TOMOYO_TRANSITION_CONTROL_INITIALIZE
] = "initialize_domain ",
1684 [TOMOYO_TRANSITION_CONTROL_NO_KEEP
] = "no_keep_domain ",
1685 [TOMOYO_TRANSITION_CONTROL_KEEP
] = "keep_domain ",
1688 /* String table for grouping keywords. */
1689 static const char *tomoyo_group_name
[TOMOYO_MAX_GROUP
] = {
1690 [TOMOYO_PATH_GROUP
] = "path_group ",
1691 [TOMOYO_NUMBER_GROUP
] = "number_group ",
1692 [TOMOYO_ADDRESS_GROUP
] = "address_group ",
1696 * tomoyo_write_exception - Write exception policy.
1698 * @head: Pointer to "struct tomoyo_io_buffer".
1700 * Returns 0 on success, negative value otherwise.
1702 * Caller holds tomoyo_read_lock().
1704 static int tomoyo_write_exception(struct tomoyo_io_buffer
*head
)
1706 const bool is_delete
= head
->w
.is_delete
;
1707 struct tomoyo_acl_param param
= {
1709 .is_delete
= is_delete
,
1710 .data
= head
->write_buf
,
1713 if (tomoyo_str_starts(¶m
.data
, "aggregator "))
1714 return tomoyo_write_aggregator(¶m
);
1715 for (i
= 0; i
< TOMOYO_MAX_TRANSITION_TYPE
; i
++)
1716 if (tomoyo_str_starts(¶m
.data
, tomoyo_transition_type
[i
]))
1717 return tomoyo_write_transition_control(¶m
, i
);
1718 for (i
= 0; i
< TOMOYO_MAX_GROUP
; i
++)
1719 if (tomoyo_str_starts(¶m
.data
, tomoyo_group_name
[i
]))
1720 return tomoyo_write_group(¶m
, i
);
1721 if (tomoyo_str_starts(¶m
.data
, "acl_group ")) {
1724 group
= simple_strtoul(param
.data
, &data
, 10);
1725 if (group
< TOMOYO_MAX_ACL_GROUPS
&& *data
++ == ' ')
1726 return tomoyo_write_domain2
1727 (head
->w
.ns
, &head
->w
.ns
->acl_group
[group
],
1734 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
1736 * @head: Pointer to "struct tomoyo_io_buffer".
1737 * @idx: Index number.
1739 * Returns true on success, false otherwise.
1741 * Caller holds tomoyo_read_lock().
1743 static bool tomoyo_read_group(struct tomoyo_io_buffer
*head
, const int idx
)
1745 struct tomoyo_policy_namespace
*ns
=
1746 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1747 struct list_head
*list
= &ns
->group_list
[idx
];
1748 list_for_each_cookie(head
->r
.group
, list
) {
1749 struct tomoyo_group
*group
=
1750 list_entry(head
->r
.group
, typeof(*group
), head
.list
);
1751 list_for_each_cookie(head
->r
.acl
, &group
->member_list
) {
1752 struct tomoyo_acl_head
*ptr
=
1753 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1754 if (ptr
->is_deleted
)
1756 if (!tomoyo_flush(head
))
1758 tomoyo_print_namespace(head
);
1759 tomoyo_set_string(head
, tomoyo_group_name
[idx
]);
1760 tomoyo_set_string(head
, group
->group_name
->name
);
1761 if (idx
== TOMOYO_PATH_GROUP
) {
1762 tomoyo_set_space(head
);
1763 tomoyo_set_string(head
, container_of
1764 (ptr
, struct tomoyo_path_group
,
1765 head
)->member_name
->name
);
1766 } else if (idx
== TOMOYO_NUMBER_GROUP
) {
1767 tomoyo_print_number_union(head
, &container_of
1769 struct tomoyo_number_group
,
1771 } else if (idx
== TOMOYO_ADDRESS_GROUP
) {
1774 struct tomoyo_address_group
*member
=
1775 container_of(ptr
, typeof(*member
),
1777 tomoyo_print_ip(buffer
, sizeof(buffer
),
1779 tomoyo_io_printf(head
, " %s", buffer
);
1781 tomoyo_set_lf(head
);
1785 head
->r
.group
= NULL
;
1790 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1792 * @head: Pointer to "struct tomoyo_io_buffer".
1793 * @idx: Index number.
1795 * Returns true on success, false otherwise.
1797 * Caller holds tomoyo_read_lock().
1799 static bool tomoyo_read_policy(struct tomoyo_io_buffer
*head
, const int idx
)
1801 struct tomoyo_policy_namespace
*ns
=
1802 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1803 struct list_head
*list
= &ns
->policy_list
[idx
];
1804 list_for_each_cookie(head
->r
.acl
, list
) {
1805 struct tomoyo_acl_head
*acl
=
1806 container_of(head
->r
.acl
, typeof(*acl
), list
);
1807 if (acl
->is_deleted
)
1809 if (!tomoyo_flush(head
))
1812 case TOMOYO_ID_TRANSITION_CONTROL
:
1814 struct tomoyo_transition_control
*ptr
=
1815 container_of(acl
, typeof(*ptr
), head
);
1816 tomoyo_print_namespace(head
);
1817 tomoyo_set_string(head
, tomoyo_transition_type
1819 tomoyo_set_string(head
, ptr
->program
?
1820 ptr
->program
->name
: "any");
1821 tomoyo_set_string(head
, " from ");
1822 tomoyo_set_string(head
, ptr
->domainname
?
1823 ptr
->domainname
->name
:
1827 case TOMOYO_ID_AGGREGATOR
:
1829 struct tomoyo_aggregator
*ptr
=
1830 container_of(acl
, typeof(*ptr
), head
);
1831 tomoyo_print_namespace(head
);
1832 tomoyo_set_string(head
, "aggregator ");
1833 tomoyo_set_string(head
,
1834 ptr
->original_name
->name
);
1835 tomoyo_set_space(head
);
1836 tomoyo_set_string(head
,
1837 ptr
->aggregated_name
->name
);
1843 tomoyo_set_lf(head
);
1850 * tomoyo_read_exception - Read exception policy.
1852 * @head: Pointer to "struct tomoyo_io_buffer".
1854 * Caller holds tomoyo_read_lock().
1856 static void tomoyo_read_exception(struct tomoyo_io_buffer
*head
)
1858 struct tomoyo_policy_namespace
*ns
=
1859 container_of(head
->r
.ns
, typeof(*ns
), namespace_list
);
1862 while (head
->r
.step
< TOMOYO_MAX_POLICY
&&
1863 tomoyo_read_policy(head
, head
->r
.step
))
1865 if (head
->r
.step
< TOMOYO_MAX_POLICY
)
1867 while (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
&&
1868 tomoyo_read_group(head
, head
->r
.step
- TOMOYO_MAX_POLICY
))
1870 if (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
)
1872 while (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
1873 + TOMOYO_MAX_ACL_GROUPS
) {
1874 head
->r
.acl_group_index
= head
->r
.step
- TOMOYO_MAX_POLICY
1876 if (!tomoyo_read_domain2(head
, &ns
->acl_group
1877 [head
->r
.acl_group_index
]))
1884 /* Wait queue for kernel -> userspace notification. */
1885 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait
);
1886 /* Wait queue for userspace -> kernel notification. */
1887 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait
);
1889 /* Structure for query. */
1890 struct tomoyo_query
{
1891 struct list_head list
;
1894 unsigned int serial
;
1900 /* The list for "struct tomoyo_query". */
1901 static LIST_HEAD(tomoyo_query_list
);
1903 /* Lock for manipulating tomoyo_query_list. */
1904 static DEFINE_SPINLOCK(tomoyo_query_list_lock
);
1907 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1910 static atomic_t tomoyo_query_observers
= ATOMIC_INIT(0);
1913 * tomoyo_truncate - Truncate a line.
1915 * @str: String to truncate.
1917 * Returns length of truncated @str.
1919 static int tomoyo_truncate(char *str
)
1922 while (*(unsigned char *) str
> (unsigned char) ' ')
1925 return strlen(start
) + 1;
1929 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1931 * @domain: Pointer to "struct tomoyo_domain_info".
1932 * @header: Lines containing ACL.
1936 static void tomoyo_add_entry(struct tomoyo_domain_info
*domain
, char *header
)
1939 char *realpath
= NULL
;
1941 char *symlink
= NULL
;
1942 char *cp
= strchr(header
, '\n');
1946 cp
= strchr(cp
+ 1, '\n');
1950 len
= strlen(cp
) + 1;
1951 /* strstr() will return NULL if ordering is wrong. */
1953 argv0
= strstr(header
, " argv[]={ \"");
1956 len
+= tomoyo_truncate(argv0
) + 14;
1958 realpath
= strstr(header
, " exec={ realpath=\"");
1961 len
+= tomoyo_truncate(realpath
) + 6;
1963 symlink
= strstr(header
, " symlink.target=\"");
1965 len
+= tomoyo_truncate(symlink
+ 1) + 1;
1967 buffer
= kmalloc(len
, GFP_NOFS
);
1970 snprintf(buffer
, len
- 1, "%s", cp
);
1972 tomoyo_addprintf(buffer
, len
, " exec.%s", realpath
);
1974 tomoyo_addprintf(buffer
, len
, " exec.argv[0]=%s", argv0
);
1976 tomoyo_addprintf(buffer
, len
, "%s", symlink
);
1977 tomoyo_normalize_line(buffer
);
1978 if (!tomoyo_write_domain2(domain
->ns
, &domain
->acl_info_list
, buffer
,
1980 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
1985 * tomoyo_supervisor - Ask for the supervisor's decision.
1987 * @r: Pointer to "struct tomoyo_request_info".
1988 * @fmt: The printf()'s format string, followed by parameters.
1990 * Returns 0 if the supervisor decided to permit the access request which
1991 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1992 * supervisor decided to retry the access request which violated the policy in
1993 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1995 int tomoyo_supervisor(struct tomoyo_request_info
*r
, const char *fmt
, ...)
2000 static unsigned int tomoyo_serial
;
2001 struct tomoyo_query entry
= { };
2002 bool quota_exceeded
= false;
2003 va_start(args
, fmt
);
2004 len
= vsnprintf((char *) &len
, 1, fmt
, args
) + 1;
2006 /* Write /sys/kernel/security/tomoyo/audit. */
2007 va_start(args
, fmt
);
2008 tomoyo_write_log2(r
, len
, fmt
, args
);
2010 /* Nothing more to do if granted. */
2014 tomoyo_update_stat(r
->mode
);
2016 case TOMOYO_CONFIG_ENFORCING
:
2018 if (atomic_read(&tomoyo_query_observers
))
2021 case TOMOYO_CONFIG_LEARNING
:
2023 /* Check max_learning_entry parameter. */
2024 if (tomoyo_domain_quota_is_ok(r
))
2031 va_start(args
, fmt
);
2032 entry
.query
= tomoyo_init_log(r
, len
, fmt
, args
);
2036 entry
.query_len
= strlen(entry
.query
) + 1;
2038 tomoyo_add_entry(r
->domain
, entry
.query
);
2041 len
= tomoyo_round2(entry
.query_len
);
2042 spin_lock(&tomoyo_query_list_lock
);
2043 if (tomoyo_memory_quota
[TOMOYO_MEMORY_QUERY
] &&
2044 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] + len
2045 >= tomoyo_memory_quota
[TOMOYO_MEMORY_QUERY
]) {
2046 quota_exceeded
= true;
2048 entry
.serial
= tomoyo_serial
++;
2049 entry
.retry
= r
->retry
;
2050 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] += len
;
2051 list_add_tail(&entry
.list
, &tomoyo_query_list
);
2053 spin_unlock(&tomoyo_query_list_lock
);
2056 /* Give 10 seconds for supervisor's opinion. */
2057 while (entry
.timer
< 10) {
2058 wake_up_all(&tomoyo_query_wait
);
2059 if (wait_event_interruptible_timeout
2060 (tomoyo_answer_wait
, entry
.answer
||
2061 !atomic_read(&tomoyo_query_observers
), HZ
))
2066 spin_lock(&tomoyo_query_list_lock
);
2067 list_del(&entry
.list
);
2068 tomoyo_memory_used
[TOMOYO_MEMORY_QUERY
] -= len
;
2069 spin_unlock(&tomoyo_query_list_lock
);
2070 switch (entry
.answer
) {
2071 case 3: /* Asked to retry by administrator. */
2072 error
= TOMOYO_RETRY_REQUEST
;
2076 /* Granted by administrator. */
2080 /* Timed out or rejected by administrator. */
2089 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
2091 * @file: Pointer to "struct file".
2092 * @wait: Pointer to "poll_table".
2094 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2096 * Waits for access requests which violated policy in enforcing mode.
2098 static int tomoyo_poll_query(struct file
*file
, poll_table
*wait
)
2100 struct list_head
*tmp
;
2103 for (i
= 0; i
< 2; i
++) {
2104 spin_lock(&tomoyo_query_list_lock
);
2105 list_for_each(tmp
, &tomoyo_query_list
) {
2106 struct tomoyo_query
*ptr
=
2107 list_entry(tmp
, typeof(*ptr
), list
);
2113 spin_unlock(&tomoyo_query_list_lock
);
2115 return POLLIN
| POLLRDNORM
;
2118 poll_wait(file
, &tomoyo_query_wait
, wait
);
2124 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
2126 * @head: Pointer to "struct tomoyo_io_buffer".
2128 static void tomoyo_read_query(struct tomoyo_io_buffer
*head
)
2130 struct list_head
*tmp
;
2131 unsigned int pos
= 0;
2136 if (head
->read_buf
) {
2137 kfree(head
->read_buf
);
2138 head
->read_buf
= NULL
;
2140 spin_lock(&tomoyo_query_list_lock
);
2141 list_for_each(tmp
, &tomoyo_query_list
) {
2142 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2145 if (pos
++ != head
->r
.query_index
)
2147 len
= ptr
->query_len
;
2150 spin_unlock(&tomoyo_query_list_lock
);
2152 head
->r
.query_index
= 0;
2155 buf
= kzalloc(len
+ 32, GFP_NOFS
);
2159 spin_lock(&tomoyo_query_list_lock
);
2160 list_for_each(tmp
, &tomoyo_query_list
) {
2161 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2164 if (pos
++ != head
->r
.query_index
)
2167 * Some query can be skipped because tomoyo_query_list
2168 * can change, but I don't care.
2170 if (len
== ptr
->query_len
)
2171 snprintf(buf
, len
+ 31, "Q%u-%hu\n%s", ptr
->serial
,
2172 ptr
->retry
, ptr
->query
);
2175 spin_unlock(&tomoyo_query_list_lock
);
2177 head
->read_buf
= buf
;
2178 head
->r
.w
[head
->r
.w_pos
++] = buf
;
2179 head
->r
.query_index
++;
2186 * tomoyo_write_answer - Write the supervisor's decision.
2188 * @head: Pointer to "struct tomoyo_io_buffer".
2190 * Returns 0 on success, -EINVAL otherwise.
2192 static int tomoyo_write_answer(struct tomoyo_io_buffer
*head
)
2194 char *data
= head
->write_buf
;
2195 struct list_head
*tmp
;
2196 unsigned int serial
;
2197 unsigned int answer
;
2198 spin_lock(&tomoyo_query_list_lock
);
2199 list_for_each(tmp
, &tomoyo_query_list
) {
2200 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2203 spin_unlock(&tomoyo_query_list_lock
);
2204 if (sscanf(data
, "A%u=%u", &serial
, &answer
) != 2)
2206 spin_lock(&tomoyo_query_list_lock
);
2207 list_for_each(tmp
, &tomoyo_query_list
) {
2208 struct tomoyo_query
*ptr
= list_entry(tmp
, typeof(*ptr
), list
);
2209 if (ptr
->serial
!= serial
)
2212 ptr
->answer
= answer
;
2215 spin_unlock(&tomoyo_query_list_lock
);
2220 * tomoyo_read_version: Get version.
2222 * @head: Pointer to "struct tomoyo_io_buffer".
2224 * Returns version information.
2226 static void tomoyo_read_version(struct tomoyo_io_buffer
*head
)
2229 tomoyo_io_printf(head
, "2.5.0");
2234 /* String table for /sys/kernel/security/tomoyo/stat interface. */
2235 static const char * const tomoyo_policy_headers
[TOMOYO_MAX_POLICY_STAT
] = {
2236 [TOMOYO_STAT_POLICY_UPDATES
] = "update:",
2237 [TOMOYO_STAT_POLICY_LEARNING
] = "violation in learning mode:",
2238 [TOMOYO_STAT_POLICY_PERMISSIVE
] = "violation in permissive mode:",
2239 [TOMOYO_STAT_POLICY_ENFORCING
] = "violation in enforcing mode:",
2242 /* String table for /sys/kernel/security/tomoyo/stat interface. */
2243 static const char * const tomoyo_memory_headers
[TOMOYO_MAX_MEMORY_STAT
] = {
2244 [TOMOYO_MEMORY_POLICY
] = "policy:",
2245 [TOMOYO_MEMORY_AUDIT
] = "audit log:",
2246 [TOMOYO_MEMORY_QUERY
] = "query message:",
2249 /* Timestamp counter for last updated. */
2250 static unsigned int tomoyo_stat_updated
[TOMOYO_MAX_POLICY_STAT
];
2251 /* Counter for number of updates. */
2252 static unsigned int tomoyo_stat_modified
[TOMOYO_MAX_POLICY_STAT
];
2255 * tomoyo_update_stat - Update statistic counters.
2257 * @index: Index for policy type.
2261 void tomoyo_update_stat(const u8 index
)
2264 do_gettimeofday(&tv
);
2266 * I don't use atomic operations because race condition is not fatal.
2268 tomoyo_stat_updated
[index
]++;
2269 tomoyo_stat_modified
[index
] = tv
.tv_sec
;
2273 * tomoyo_read_stat - Read statistic data.
2275 * @head: Pointer to "struct tomoyo_io_buffer".
2279 static void tomoyo_read_stat(struct tomoyo_io_buffer
*head
)
2282 unsigned int total
= 0;
2285 for (i
= 0; i
< TOMOYO_MAX_POLICY_STAT
; i
++) {
2286 tomoyo_io_printf(head
, "Policy %-30s %10u",
2287 tomoyo_policy_headers
[i
],
2288 tomoyo_stat_updated
[i
]);
2289 if (tomoyo_stat_modified
[i
]) {
2290 struct tomoyo_time stamp
;
2291 tomoyo_convert_time(tomoyo_stat_modified
[i
], &stamp
);
2292 tomoyo_io_printf(head
, " (Last: %04u/%02u/%02u "
2294 stamp
.year
, stamp
.month
, stamp
.day
,
2295 stamp
.hour
, stamp
.min
, stamp
.sec
);
2297 tomoyo_set_lf(head
);
2299 for (i
= 0; i
< TOMOYO_MAX_MEMORY_STAT
; i
++) {
2300 unsigned int used
= tomoyo_memory_used
[i
];
2302 tomoyo_io_printf(head
, "Memory used by %-22s %10u",
2303 tomoyo_memory_headers
[i
], used
);
2304 used
= tomoyo_memory_quota
[i
];
2306 tomoyo_io_printf(head
, " (Quota: %10u)", used
);
2307 tomoyo_set_lf(head
);
2309 tomoyo_io_printf(head
, "Total memory used: %10u\n",
2315 * tomoyo_write_stat - Set memory quota.
2317 * @head: Pointer to "struct tomoyo_io_buffer".
2321 static int tomoyo_write_stat(struct tomoyo_io_buffer
*head
)
2323 char *data
= head
->write_buf
;
2325 if (tomoyo_str_starts(&data
, "Memory used by "))
2326 for (i
= 0; i
< TOMOYO_MAX_MEMORY_STAT
; i
++)
2327 if (tomoyo_str_starts(&data
, tomoyo_memory_headers
[i
]))
2328 sscanf(data
, "%u", &tomoyo_memory_quota
[i
]);
2333 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2335 * @type: Type of interface.
2336 * @file: Pointer to "struct file".
2338 * Returns 0 on success, negative value otherwise.
2340 int tomoyo_open_control(const u8 type
, struct file
*file
)
2342 struct tomoyo_io_buffer
*head
= kzalloc(sizeof(*head
), GFP_NOFS
);
2346 mutex_init(&head
->io_sem
);
2349 case TOMOYO_DOMAINPOLICY
:
2350 /* /sys/kernel/security/tomoyo/domain_policy */
2351 head
->write
= tomoyo_write_domain
;
2352 head
->read
= tomoyo_read_domain
;
2354 case TOMOYO_EXCEPTIONPOLICY
:
2355 /* /sys/kernel/security/tomoyo/exception_policy */
2356 head
->write
= tomoyo_write_exception
;
2357 head
->read
= tomoyo_read_exception
;
2360 /* /sys/kernel/security/tomoyo/audit */
2361 head
->poll
= tomoyo_poll_log
;
2362 head
->read
= tomoyo_read_log
;
2364 case TOMOYO_PROCESS_STATUS
:
2365 /* /sys/kernel/security/tomoyo/.process_status */
2366 head
->write
= tomoyo_write_pid
;
2367 head
->read
= tomoyo_read_pid
;
2369 case TOMOYO_VERSION
:
2370 /* /sys/kernel/security/tomoyo/version */
2371 head
->read
= tomoyo_read_version
;
2372 head
->readbuf_size
= 128;
2375 /* /sys/kernel/security/tomoyo/stat */
2376 head
->write
= tomoyo_write_stat
;
2377 head
->read
= tomoyo_read_stat
;
2378 head
->readbuf_size
= 1024;
2380 case TOMOYO_PROFILE
:
2381 /* /sys/kernel/security/tomoyo/profile */
2382 head
->write
= tomoyo_write_profile
;
2383 head
->read
= tomoyo_read_profile
;
2385 case TOMOYO_QUERY
: /* /sys/kernel/security/tomoyo/query */
2386 head
->poll
= tomoyo_poll_query
;
2387 head
->write
= tomoyo_write_answer
;
2388 head
->read
= tomoyo_read_query
;
2390 case TOMOYO_MANAGER
:
2391 /* /sys/kernel/security/tomoyo/manager */
2392 head
->write
= tomoyo_write_manager
;
2393 head
->read
= tomoyo_read_manager
;
2396 if (!(file
->f_mode
& FMODE_READ
)) {
2398 * No need to allocate read_buf since it is not opened
2403 } else if (!head
->poll
) {
2404 /* Don't allocate read_buf for poll() access. */
2405 if (!head
->readbuf_size
)
2406 head
->readbuf_size
= 4096 * 2;
2407 head
->read_buf
= kzalloc(head
->readbuf_size
, GFP_NOFS
);
2408 if (!head
->read_buf
) {
2413 if (!(file
->f_mode
& FMODE_WRITE
)) {
2415 * No need to allocate write_buf since it is not opened
2419 } else if (head
->write
) {
2420 head
->writebuf_size
= 4096 * 2;
2421 head
->write_buf
= kzalloc(head
->writebuf_size
, GFP_NOFS
);
2422 if (!head
->write_buf
) {
2423 kfree(head
->read_buf
);
2429 * If the file is /sys/kernel/security/tomoyo/query , increment the
2431 * The obserber counter is used by tomoyo_supervisor() to see if
2432 * there is some process monitoring /sys/kernel/security/tomoyo/query.
2434 if (type
== TOMOYO_QUERY
)
2435 atomic_inc(&tomoyo_query_observers
);
2436 file
->private_data
= head
;
2437 tomoyo_notify_gc(head
, true);
2442 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
2444 * @file: Pointer to "struct file".
2445 * @wait: Pointer to "poll_table".
2447 * Waits for read readiness.
2448 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2449 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
2451 int tomoyo_poll_control(struct file
*file
, poll_table
*wait
)
2453 struct tomoyo_io_buffer
*head
= file
->private_data
;
2456 return head
->poll(file
, wait
);
2460 * tomoyo_set_namespace_cursor - Set namespace to read.
2462 * @head: Pointer to "struct tomoyo_io_buffer".
2466 static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer
*head
)
2468 struct list_head
*ns
;
2469 if (head
->type
!= TOMOYO_EXCEPTIONPOLICY
&&
2470 head
->type
!= TOMOYO_PROFILE
)
2473 * If this is the first read, or reading previous namespace finished
2474 * and has more namespaces to read, update the namespace cursor.
2477 if (!ns
|| (head
->r
.eof
&& ns
->next
!= &tomoyo_namespace_list
)) {
2478 /* Clearing is OK because tomoyo_flush() returned true. */
2479 memset(&head
->r
, 0, sizeof(head
->r
));
2480 head
->r
.ns
= ns
? ns
->next
: tomoyo_namespace_list
.next
;
2485 * tomoyo_has_more_namespace - Check for unread namespaces.
2487 * @head: Pointer to "struct tomoyo_io_buffer".
2489 * Returns true if we have more entries to print, false otherwise.
2491 static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer
*head
)
2493 return (head
->type
== TOMOYO_EXCEPTIONPOLICY
||
2494 head
->type
== TOMOYO_PROFILE
) && head
->r
.eof
&&
2495 head
->r
.ns
->next
!= &tomoyo_namespace_list
;
2499 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2501 * @head: Pointer to "struct tomoyo_io_buffer".
2502 * @buffer: Poiner to buffer to write to.
2503 * @buffer_len: Size of @buffer.
2505 * Returns bytes read on success, negative value otherwise.
2507 ssize_t
tomoyo_read_control(struct tomoyo_io_buffer
*head
, char __user
*buffer
,
2508 const int buffer_len
)
2515 if (mutex_lock_interruptible(&head
->io_sem
))
2517 head
->read_user_buf
= buffer
;
2518 head
->read_user_buf_avail
= buffer_len
;
2519 idx
= tomoyo_read_lock();
2520 if (tomoyo_flush(head
))
2521 /* Call the policy handler. */
2523 tomoyo_set_namespace_cursor(head
);
2525 } while (tomoyo_flush(head
) &&
2526 tomoyo_has_more_namespace(head
));
2527 tomoyo_read_unlock(idx
);
2528 len
= head
->read_user_buf
- buffer
;
2529 mutex_unlock(&head
->io_sem
);
2534 * tomoyo_parse_policy - Parse a policy line.
2536 * @head: Poiter to "struct tomoyo_io_buffer".
2537 * @line: Line to parse.
2539 * Returns 0 on success, negative value otherwise.
2541 * Caller holds tomoyo_read_lock().
2543 static int tomoyo_parse_policy(struct tomoyo_io_buffer
*head
, char *line
)
2545 /* Delete request? */
2546 head
->w
.is_delete
= !strncmp(line
, "delete ", 7);
2547 if (head
->w
.is_delete
)
2548 memmove(line
, line
+ 7, strlen(line
+ 7) + 1);
2549 /* Selecting namespace to update. */
2550 if (head
->type
== TOMOYO_EXCEPTIONPOLICY
||
2551 head
->type
== TOMOYO_PROFILE
) {
2553 char *cp
= strchr(line
, ' ');
2556 head
->w
.ns
= tomoyo_assign_namespace(line
);
2557 memmove(line
, cp
, strlen(cp
) + 1);
2561 head
->w
.ns
= &tomoyo_kernel_namespace
;
2562 /* Don't allow updating if namespace is invalid. */
2566 /* Do the update. */
2567 return head
->write(head
);
2571 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2573 * @head: Pointer to "struct tomoyo_io_buffer".
2574 * @buffer: Pointer to buffer to read from.
2575 * @buffer_len: Size of @buffer.
2577 * Returns @buffer_len on success, negative value otherwise.
2579 ssize_t
tomoyo_write_control(struct tomoyo_io_buffer
*head
,
2580 const char __user
*buffer
, const int buffer_len
)
2582 int error
= buffer_len
;
2583 size_t avail_len
= buffer_len
;
2584 char *cp0
= head
->write_buf
;
2588 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
2590 if (mutex_lock_interruptible(&head
->io_sem
))
2592 idx
= tomoyo_read_lock();
2593 /* Read a line and dispatch it to the policy handler. */
2594 while (avail_len
> 0) {
2596 if (head
->w
.avail
>= head
->writebuf_size
- 1) {
2597 const int len
= head
->writebuf_size
* 2;
2598 char *cp
= kzalloc(len
, GFP_NOFS
);
2603 memmove(cp
, cp0
, head
->w
.avail
);
2605 head
->write_buf
= cp
;
2607 head
->writebuf_size
= len
;
2609 if (get_user(c
, buffer
)) {
2615 cp0
[head
->w
.avail
++] = c
;
2618 cp0
[head
->w
.avail
- 1] = '\0';
2620 tomoyo_normalize_line(cp0
);
2621 if (!strcmp(cp0
, "reset")) {
2622 head
->w
.ns
= &tomoyo_kernel_namespace
;
2623 head
->w
.domain
= NULL
;
2624 memset(&head
->r
, 0, sizeof(head
->r
));
2627 /* Don't allow updating policies by non manager programs. */
2628 switch (head
->type
) {
2629 case TOMOYO_PROCESS_STATUS
:
2630 /* This does not write anything. */
2632 case TOMOYO_DOMAINPOLICY
:
2633 if (tomoyo_select_domain(head
, cp0
))
2636 case TOMOYO_EXCEPTIONPOLICY
:
2637 if (!strcmp(cp0
, "select transition_only")) {
2638 head
->r
.print_transition_related_only
= true;
2643 if (!tomoyo_manager()) {
2648 switch (tomoyo_parse_policy(head
, cp0
)) {
2653 switch (head
->type
) {
2654 case TOMOYO_DOMAINPOLICY
:
2655 case TOMOYO_EXCEPTIONPOLICY
:
2657 case TOMOYO_PROFILE
:
2658 case TOMOYO_MANAGER
:
2659 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
2668 tomoyo_read_unlock(idx
);
2669 mutex_unlock(&head
->io_sem
);
2674 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2676 * @head: Pointer to "struct tomoyo_io_buffer".
2680 int tomoyo_close_control(struct tomoyo_io_buffer
*head
)
2683 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2686 if (head
->type
== TOMOYO_QUERY
&&
2687 atomic_dec_and_test(&tomoyo_query_observers
))
2688 wake_up_all(&tomoyo_answer_wait
);
2689 tomoyo_notify_gc(head
, false);
2694 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2696 void tomoyo_check_profile(void)
2698 struct tomoyo_domain_info
*domain
;
2699 const int idx
= tomoyo_read_lock();
2700 tomoyo_policy_loaded
= true;
2701 printk(KERN_INFO
"TOMOYO: 2.5.0\n");
2702 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
2703 const u8 profile
= domain
->profile
;
2704 const struct tomoyo_policy_namespace
*ns
= domain
->ns
;
2705 if (ns
->profile_version
!= 20110903)
2707 "Profile version %u is not supported.\n",
2708 ns
->profile_version
);
2709 else if (!ns
->profile_ptr
[profile
])
2711 "Profile %u (used by '%s') is not defined.\n",
2712 profile
, domain
->domainname
->name
);
2716 "Userland tools for TOMOYO 2.5 must be installed and "
2717 "policy must be initialized.\n");
2718 printk(KERN_ERR
"Please see http://tomoyo.sourceforge.jp/2.5/ "
2719 "for more information.\n");
2722 tomoyo_read_unlock(idx
);
2723 printk(KERN_INFO
"Mandatory Access Control activated.\n");
2727 * tomoyo_load_builtin_policy - Load built-in policy.
2731 void __init
tomoyo_load_builtin_policy(void)
2734 * This include file is manually created and contains built-in policy
2735 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2736 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2737 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2739 #include "builtin-policy.h"
2741 const int idx
= tomoyo_read_lock();
2742 for (i
= 0; i
< 5; i
++) {
2743 struct tomoyo_io_buffer head
= { };
2747 start
= tomoyo_builtin_profile
;
2748 head
.type
= TOMOYO_PROFILE
;
2749 head
.write
= tomoyo_write_profile
;
2752 start
= tomoyo_builtin_exception_policy
;
2753 head
.type
= TOMOYO_EXCEPTIONPOLICY
;
2754 head
.write
= tomoyo_write_exception
;
2757 start
= tomoyo_builtin_domain_policy
;
2758 head
.type
= TOMOYO_DOMAINPOLICY
;
2759 head
.write
= tomoyo_write_domain
;
2762 start
= tomoyo_builtin_manager
;
2763 head
.type
= TOMOYO_MANAGER
;
2764 head
.write
= tomoyo_write_manager
;
2767 start
= tomoyo_builtin_stat
;
2768 head
.type
= TOMOYO_STAT
;
2769 head
.write
= tomoyo_write_stat
;
2773 char *end
= strchr(start
, '\n');
2777 tomoyo_normalize_line(start
);
2778 head
.write_buf
= start
;
2779 tomoyo_parse_policy(&head
, start
);
2783 tomoyo_read_unlock(idx
);
2784 #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2785 tomoyo_check_profile();