xref: /linux/security/tomoyo/common.c (revision 8c6cb983cd52d78ab4e4c0191c73a11dcb60b866)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29590837bSKentaro Takeda /*
39590837bSKentaro Takeda  * security/tomoyo/common.c
49590837bSKentaro Takeda  *
50f2a55d5STetsuo Handa  * Copyright (C) 2005-2011  NTT DATA CORPORATION
69590837bSKentaro Takeda  */
79590837bSKentaro Takeda 
89590837bSKentaro Takeda #include <linux/uaccess.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
109590837bSKentaro Takeda #include <linux/security.h>
119590837bSKentaro Takeda #include "common.h"
129590837bSKentaro Takeda 
13eadd99ccSTetsuo Handa /* String table for operation mode. */
14eadd99ccSTetsuo Handa const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
15eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_DISABLED]   = "disabled",
16eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_LEARNING]   = "learning",
17eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_PERMISSIVE] = "permissive",
18eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_ENFORCING]  = "enforcing"
199590837bSKentaro Takeda };
209590837bSKentaro Takeda 
2157c2590fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/profile */
222c47ab93STetsuo Handa const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
2357c2590fSTetsuo Handa 				       + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
24d58e0da8STetsuo Handa 	/* CONFIG::file group */
252c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_EXECUTE]    = "execute",
262c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_OPEN]       = "open",
272c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CREATE]     = "create",
282c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_UNLINK]     = "unlink",
292c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_GETATTR]    = "getattr",
302c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKDIR]      = "mkdir",
312c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_RMDIR]      = "rmdir",
322c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKFIFO]     = "mkfifo",
332c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKSOCK]     = "mksock",
342c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_TRUNCATE]   = "truncate",
352c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_SYMLINK]    = "symlink",
362c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKBLOCK]    = "mkblock",
372c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKCHAR]     = "mkchar",
382c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_LINK]       = "link",
392c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_RENAME]     = "rename",
402c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHMOD]      = "chmod",
412c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHOWN]      = "chown",
422c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHGRP]      = "chgrp",
432c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_IOCTL]      = "ioctl",
442c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHROOT]     = "chroot",
452c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MOUNT]      = "mount",
462c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_UMOUNT]     = "unmount",
472c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root",
48059d84dbSTetsuo Handa 	/* CONFIG::network group */
49059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
50059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
51059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
52059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
53059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
54059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
55059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
56059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
57059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
58059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
59059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
60059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
61059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
62059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
63059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
64d58e0da8STetsuo Handa 	/* CONFIG::misc group */
65d58e0da8STetsuo Handa 	[TOMOYO_MAC_ENVIRON] = "env",
66d58e0da8STetsuo Handa 	/* CONFIG group */
6757c2590fSTetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
68059d84dbSTetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_NETWORK] = "network",
69d58e0da8STetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_MISC] = "misc",
709590837bSKentaro Takeda };
719590837bSKentaro Takeda 
722066a361STetsuo Handa /* String table for conditions. */
732066a361STetsuo Handa const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
742066a361STetsuo Handa 	[TOMOYO_TASK_UID]             = "task.uid",
752066a361STetsuo Handa 	[TOMOYO_TASK_EUID]            = "task.euid",
762066a361STetsuo Handa 	[TOMOYO_TASK_SUID]            = "task.suid",
772066a361STetsuo Handa 	[TOMOYO_TASK_FSUID]           = "task.fsuid",
782066a361STetsuo Handa 	[TOMOYO_TASK_GID]             = "task.gid",
792066a361STetsuo Handa 	[TOMOYO_TASK_EGID]            = "task.egid",
802066a361STetsuo Handa 	[TOMOYO_TASK_SGID]            = "task.sgid",
812066a361STetsuo Handa 	[TOMOYO_TASK_FSGID]           = "task.fsgid",
822066a361STetsuo Handa 	[TOMOYO_TASK_PID]             = "task.pid",
832066a361STetsuo Handa 	[TOMOYO_TASK_PPID]            = "task.ppid",
845b636857STetsuo Handa 	[TOMOYO_EXEC_ARGC]            = "exec.argc",
855b636857STetsuo Handa 	[TOMOYO_EXEC_ENVC]            = "exec.envc",
868761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_SOCKET]       = "socket",
878761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_SYMLINK]      = "symlink",
888761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_FILE]         = "file",
898761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_BLOCK_DEV]    = "block",
908761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_DIRECTORY]    = "directory",
918761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_CHAR_DEV]     = "char",
928761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_FIFO]         = "fifo",
938761afd4STetsuo Handa 	[TOMOYO_MODE_SETUID]          = "setuid",
948761afd4STetsuo Handa 	[TOMOYO_MODE_SETGID]          = "setgid",
958761afd4STetsuo Handa 	[TOMOYO_MODE_STICKY]          = "sticky",
968761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_READ]      = "owner_read",
978761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_WRITE]     = "owner_write",
988761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_EXECUTE]   = "owner_execute",
998761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_READ]      = "group_read",
1008761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_WRITE]     = "group_write",
1018761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_EXECUTE]   = "group_execute",
1028761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_READ]     = "others_read",
1038761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_WRITE]    = "others_write",
1048761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_EXECUTE]  = "others_execute",
1052ca9bf45STetsuo Handa 	[TOMOYO_EXEC_REALPATH]        = "exec.realpath",
1062ca9bf45STetsuo Handa 	[TOMOYO_SYMLINK_TARGET]       = "symlink.target",
1078761afd4STetsuo Handa 	[TOMOYO_PATH1_UID]            = "path1.uid",
1088761afd4STetsuo Handa 	[TOMOYO_PATH1_GID]            = "path1.gid",
1098761afd4STetsuo Handa 	[TOMOYO_PATH1_INO]            = "path1.ino",
1108761afd4STetsuo Handa 	[TOMOYO_PATH1_MAJOR]          = "path1.major",
1118761afd4STetsuo Handa 	[TOMOYO_PATH1_MINOR]          = "path1.minor",
1128761afd4STetsuo Handa 	[TOMOYO_PATH1_PERM]           = "path1.perm",
1138761afd4STetsuo Handa 	[TOMOYO_PATH1_TYPE]           = "path1.type",
1148761afd4STetsuo Handa 	[TOMOYO_PATH1_DEV_MAJOR]      = "path1.dev_major",
1158761afd4STetsuo Handa 	[TOMOYO_PATH1_DEV_MINOR]      = "path1.dev_minor",
1168761afd4STetsuo Handa 	[TOMOYO_PATH2_UID]            = "path2.uid",
1178761afd4STetsuo Handa 	[TOMOYO_PATH2_GID]            = "path2.gid",
1188761afd4STetsuo Handa 	[TOMOYO_PATH2_INO]            = "path2.ino",
1198761afd4STetsuo Handa 	[TOMOYO_PATH2_MAJOR]          = "path2.major",
1208761afd4STetsuo Handa 	[TOMOYO_PATH2_MINOR]          = "path2.minor",
1218761afd4STetsuo Handa 	[TOMOYO_PATH2_PERM]           = "path2.perm",
1228761afd4STetsuo Handa 	[TOMOYO_PATH2_TYPE]           = "path2.type",
1238761afd4STetsuo Handa 	[TOMOYO_PATH2_DEV_MAJOR]      = "path2.dev_major",
1248761afd4STetsuo Handa 	[TOMOYO_PATH2_DEV_MINOR]      = "path2.dev_minor",
1258761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_UID]     = "path1.parent.uid",
1268761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_GID]     = "path1.parent.gid",
1278761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_INO]     = "path1.parent.ino",
1288761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_PERM]    = "path1.parent.perm",
1298761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_UID]     = "path2.parent.uid",
1308761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_GID]     = "path2.parent.gid",
1318761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_INO]     = "path2.parent.ino",
1328761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_PERM]    = "path2.parent.perm",
1332066a361STetsuo Handa };
1342066a361STetsuo Handa 
135d5ca1725STetsuo Handa /* String table for PREFERENCE keyword. */
136d5ca1725STetsuo Handa static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
137eadd99ccSTetsuo Handa 	[TOMOYO_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
138d5ca1725STetsuo Handa 	[TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
139d5ca1725STetsuo Handa };
140d5ca1725STetsuo Handa 
1412c47ab93STetsuo Handa /* String table for path operation. */
1422c47ab93STetsuo Handa const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
1432c47ab93STetsuo Handa 	[TOMOYO_TYPE_EXECUTE]    = "execute",
1442c47ab93STetsuo Handa 	[TOMOYO_TYPE_READ]       = "read",
1452c47ab93STetsuo Handa 	[TOMOYO_TYPE_WRITE]      = "write",
1462c47ab93STetsuo Handa 	[TOMOYO_TYPE_APPEND]     = "append",
1472c47ab93STetsuo Handa 	[TOMOYO_TYPE_UNLINK]     = "unlink",
1482c47ab93STetsuo Handa 	[TOMOYO_TYPE_GETATTR]    = "getattr",
1492c47ab93STetsuo Handa 	[TOMOYO_TYPE_RMDIR]      = "rmdir",
1502c47ab93STetsuo Handa 	[TOMOYO_TYPE_TRUNCATE]   = "truncate",
1512c47ab93STetsuo Handa 	[TOMOYO_TYPE_SYMLINK]    = "symlink",
1522c47ab93STetsuo Handa 	[TOMOYO_TYPE_CHROOT]     = "chroot",
1532c47ab93STetsuo Handa 	[TOMOYO_TYPE_UMOUNT]     = "unmount",
1542c47ab93STetsuo Handa };
1552c47ab93STetsuo Handa 
156059d84dbSTetsuo Handa /* String table for socket's operation. */
157059d84dbSTetsuo Handa const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION] = {
158059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_BIND]    = "bind",
159059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_LISTEN]  = "listen",
160059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_CONNECT] = "connect",
161059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_SEND]    = "send",
162059d84dbSTetsuo Handa };
163059d84dbSTetsuo Handa 
1642c47ab93STetsuo Handa /* String table for categories. */
1652c47ab93STetsuo Handa static const char * const tomoyo_category_keywords
1662c47ab93STetsuo Handa [TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
1672c47ab93STetsuo Handa 	[TOMOYO_MAC_CATEGORY_FILE]    = "file",
168059d84dbSTetsuo Handa 	[TOMOYO_MAC_CATEGORY_NETWORK] = "network",
169d58e0da8STetsuo Handa 	[TOMOYO_MAC_CATEGORY_MISC]    = "misc",
1702c47ab93STetsuo Handa };
1712c47ab93STetsuo Handa 
1729590837bSKentaro Takeda /* Permit policy management by non-root user? */
1739590837bSKentaro Takeda static bool tomoyo_manage_by_non_root;
1749590837bSKentaro Takeda 
1759590837bSKentaro Takeda /* Utility functions. */
1769590837bSKentaro Takeda 
1777762fbffSTetsuo Handa /**
17857c2590fSTetsuo Handa  * tomoyo_yesno - Return "yes" or "no".
17957c2590fSTetsuo Handa  *
18057c2590fSTetsuo Handa  * @value: Bool value.
18157c2590fSTetsuo Handa  */
182eadd99ccSTetsuo Handa const char *tomoyo_yesno(const unsigned int value)
18357c2590fSTetsuo Handa {
18457c2590fSTetsuo Handa 	return value ? "yes" : "no";
18557c2590fSTetsuo Handa }
18657c2590fSTetsuo Handa 
187d5ca1725STetsuo Handa /**
188d5ca1725STetsuo Handa  * tomoyo_addprintf - strncat()-like-snprintf().
189d5ca1725STetsuo Handa  *
190d5ca1725STetsuo Handa  * @buffer: Buffer to write to. Must be '\0'-terminated.
191d5ca1725STetsuo Handa  * @len:    Size of @buffer.
192d5ca1725STetsuo Handa  * @fmt:    The printf()'s format string, followed by parameters.
193d5ca1725STetsuo Handa  *
194d5ca1725STetsuo Handa  * Returns nothing.
195d5ca1725STetsuo Handa  */
196f23571e8STetsuo Handa static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
197f23571e8STetsuo Handa {
198f23571e8STetsuo Handa 	va_list args;
199f23571e8STetsuo Handa 	const int pos = strlen(buffer);
200f23571e8STetsuo Handa 	va_start(args, fmt);
201f23571e8STetsuo Handa 	vsnprintf(buffer + pos, len - pos - 1, fmt, args);
202f23571e8STetsuo Handa 	va_end(args);
203f23571e8STetsuo Handa }
204f23571e8STetsuo Handa 
205f23571e8STetsuo Handa /**
206f23571e8STetsuo Handa  * tomoyo_flush - Flush queued string to userspace's buffer.
207f23571e8STetsuo Handa  *
208f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
209f23571e8STetsuo Handa  *
210f23571e8STetsuo Handa  * Returns true if all data was flushed, false otherwise.
211f23571e8STetsuo Handa  */
212f23571e8STetsuo Handa static bool tomoyo_flush(struct tomoyo_io_buffer *head)
213f23571e8STetsuo Handa {
214f23571e8STetsuo Handa 	while (head->r.w_pos) {
215f23571e8STetsuo Handa 		const char *w = head->r.w[0];
2162c47ab93STetsuo Handa 		size_t len = strlen(w);
217f23571e8STetsuo Handa 		if (len) {
218f23571e8STetsuo Handa 			if (len > head->read_user_buf_avail)
219f23571e8STetsuo Handa 				len = head->read_user_buf_avail;
220f23571e8STetsuo Handa 			if (!len)
221f23571e8STetsuo Handa 				return false;
222f23571e8STetsuo Handa 			if (copy_to_user(head->read_user_buf, w, len))
223f23571e8STetsuo Handa 				return false;
224f23571e8STetsuo Handa 			head->read_user_buf_avail -= len;
225f23571e8STetsuo Handa 			head->read_user_buf += len;
226f23571e8STetsuo Handa 			w += len;
227f23571e8STetsuo Handa 		}
228f23571e8STetsuo Handa 		head->r.w[0] = w;
229c0fa797aSTetsuo Handa 		if (*w)
230f23571e8STetsuo Handa 			return false;
231eadd99ccSTetsuo Handa 		/* Add '\0' for audit logs and query. */
232f23571e8STetsuo Handa 		if (head->poll) {
233f23571e8STetsuo Handa 			if (!head->read_user_buf_avail ||
234f23571e8STetsuo Handa 			    copy_to_user(head->read_user_buf, "", 1))
235f23571e8STetsuo Handa 				return false;
236f23571e8STetsuo Handa 			head->read_user_buf_avail--;
237f23571e8STetsuo Handa 			head->read_user_buf++;
238f23571e8STetsuo Handa 		}
239f23571e8STetsuo Handa 		head->r.w_pos--;
240f23571e8STetsuo Handa 		for (len = 0; len < head->r.w_pos; len++)
241f23571e8STetsuo Handa 			head->r.w[len] = head->r.w[len + 1];
242f23571e8STetsuo Handa 	}
243f23571e8STetsuo Handa 	head->r.avail = 0;
244f23571e8STetsuo Handa 	return true;
245f23571e8STetsuo Handa }
246f23571e8STetsuo Handa 
247f23571e8STetsuo Handa /**
248f23571e8STetsuo Handa  * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
249f23571e8STetsuo Handa  *
250f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
251f23571e8STetsuo Handa  * @string: String to print.
252f23571e8STetsuo Handa  *
253f23571e8STetsuo Handa  * Note that @string has to be kept valid until @head is kfree()d.
254f23571e8STetsuo Handa  * This means that char[] allocated on stack memory cannot be passed to
255f23571e8STetsuo Handa  * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
256f23571e8STetsuo Handa  */
257f23571e8STetsuo Handa static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
258f23571e8STetsuo Handa {
259f23571e8STetsuo Handa 	if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
260f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = string;
261f23571e8STetsuo Handa 		tomoyo_flush(head);
262f23571e8STetsuo Handa 	} else
263f23571e8STetsuo Handa 		WARN_ON(1);
264f23571e8STetsuo Handa }
265f23571e8STetsuo Handa 
266778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
267778c4a4dSTetsuo Handa 			     ...) __printf(2, 3);
268778c4a4dSTetsuo Handa 
269f23571e8STetsuo Handa /**
270f23571e8STetsuo Handa  * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
271f23571e8STetsuo Handa  *
272f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
273f23571e8STetsuo Handa  * @fmt:  The printf()'s format string, followed by parameters.
274f23571e8STetsuo Handa  */
275778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
276778c4a4dSTetsuo Handa 			     ...)
277f23571e8STetsuo Handa {
278f23571e8STetsuo Handa 	va_list args;
2792c47ab93STetsuo Handa 	size_t len;
2802c47ab93STetsuo Handa 	size_t pos = head->r.avail;
281f23571e8STetsuo Handa 	int size = head->readbuf_size - pos;
282f23571e8STetsuo Handa 	if (size <= 0)
283f23571e8STetsuo Handa 		return;
284f23571e8STetsuo Handa 	va_start(args, fmt);
285f23571e8STetsuo Handa 	len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
286f23571e8STetsuo Handa 	va_end(args);
287f23571e8STetsuo Handa 	if (pos + len >= head->readbuf_size) {
288f23571e8STetsuo Handa 		WARN_ON(1);
289f23571e8STetsuo Handa 		return;
290f23571e8STetsuo Handa 	}
291f23571e8STetsuo Handa 	head->r.avail += len;
292f23571e8STetsuo Handa 	tomoyo_set_string(head, head->read_buf + pos);
293f23571e8STetsuo Handa }
294f23571e8STetsuo Handa 
2950d2171d7STetsuo Handa /**
2960d2171d7STetsuo Handa  * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
2970d2171d7STetsuo Handa  *
2980d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2990d2171d7STetsuo Handa  *
3000d2171d7STetsuo Handa  * Returns nothing.
3010d2171d7STetsuo Handa  */
302f23571e8STetsuo Handa static void tomoyo_set_space(struct tomoyo_io_buffer *head)
303f23571e8STetsuo Handa {
304f23571e8STetsuo Handa 	tomoyo_set_string(head, " ");
305f23571e8STetsuo Handa }
306f23571e8STetsuo Handa 
3070d2171d7STetsuo Handa /**
3080d2171d7STetsuo Handa  * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
3090d2171d7STetsuo Handa  *
3100d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3110d2171d7STetsuo Handa  *
3120d2171d7STetsuo Handa  * Returns nothing.
3130d2171d7STetsuo Handa  */
314f23571e8STetsuo Handa static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
315f23571e8STetsuo Handa {
316f23571e8STetsuo Handa 	tomoyo_set_string(head, "\n");
317f23571e8STetsuo Handa 	return !head->r.w_pos;
318f23571e8STetsuo Handa }
319f23571e8STetsuo Handa 
32057c2590fSTetsuo Handa /**
3210d2171d7STetsuo Handa  * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
3220d2171d7STetsuo Handa  *
3230d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3240d2171d7STetsuo Handa  *
3250d2171d7STetsuo Handa  * Returns nothing.
3260d2171d7STetsuo Handa  */
3270d2171d7STetsuo Handa static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
3280d2171d7STetsuo Handa {
3290d2171d7STetsuo Handa 	tomoyo_set_string(head, "/");
3300d2171d7STetsuo Handa }
3310d2171d7STetsuo Handa 
332bd03a3e4STetsuo Handa /* List of namespaces. */
333bd03a3e4STetsuo Handa LIST_HEAD(tomoyo_namespace_list);
334bd03a3e4STetsuo Handa /* True if namespace other than tomoyo_kernel_namespace is defined. */
335bd03a3e4STetsuo Handa static bool tomoyo_namespace_enabled;
336bd03a3e4STetsuo Handa 
337bd03a3e4STetsuo Handa /**
338bd03a3e4STetsuo Handa  * tomoyo_init_policy_namespace - Initialize namespace.
339bd03a3e4STetsuo Handa  *
340bd03a3e4STetsuo Handa  * @ns: Pointer to "struct tomoyo_policy_namespace".
341bd03a3e4STetsuo Handa  *
342bd03a3e4STetsuo Handa  * Returns nothing.
343bd03a3e4STetsuo Handa  */
344bd03a3e4STetsuo Handa void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
345bd03a3e4STetsuo Handa {
346bd03a3e4STetsuo Handa 	unsigned int idx;
347bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
348bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->acl_group[idx]);
349bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
350bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->group_list[idx]);
351bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
352bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->policy_list[idx]);
353843d183cSTetsuo Handa 	ns->profile_version = 20110903;
354bd03a3e4STetsuo Handa 	tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
355bd03a3e4STetsuo Handa 	list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
356bd03a3e4STetsuo Handa }
357bd03a3e4STetsuo Handa 
358bd03a3e4STetsuo Handa /**
359bd03a3e4STetsuo Handa  * tomoyo_print_namespace - Print namespace header.
360bd03a3e4STetsuo Handa  *
361bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
362bd03a3e4STetsuo Handa  *
363bd03a3e4STetsuo Handa  * Returns nothing.
364bd03a3e4STetsuo Handa  */
365bd03a3e4STetsuo Handa static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
366bd03a3e4STetsuo Handa {
367bd03a3e4STetsuo Handa 	if (!tomoyo_namespace_enabled)
368bd03a3e4STetsuo Handa 		return;
369bd03a3e4STetsuo Handa 	tomoyo_set_string(head,
370bd03a3e4STetsuo Handa 			  container_of(head->r.ns,
371bd03a3e4STetsuo Handa 				       struct tomoyo_policy_namespace,
372bd03a3e4STetsuo Handa 				       namespace_list)->name);
373bd03a3e4STetsuo Handa 	tomoyo_set_space(head);
374bd03a3e4STetsuo Handa }
375bd03a3e4STetsuo Handa 
3760d2171d7STetsuo Handa /**
3777762fbffSTetsuo Handa  * tomoyo_print_name_union - Print a tomoyo_name_union.
3787762fbffSTetsuo Handa  *
3797762fbffSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3807762fbffSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3817762fbffSTetsuo Handa  */
382f23571e8STetsuo Handa static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
3837762fbffSTetsuo Handa 				    const struct tomoyo_name_union *ptr)
3847762fbffSTetsuo Handa {
385f23571e8STetsuo Handa 	tomoyo_set_space(head);
3860df7e8b8STetsuo Handa 	if (ptr->group) {
387f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
388f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
389f23571e8STetsuo Handa 	} else {
390f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
391f23571e8STetsuo Handa 	}
3927762fbffSTetsuo Handa }
3937762fbffSTetsuo Handa 
3947762fbffSTetsuo Handa /**
3952ca9bf45STetsuo Handa  * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
3962ca9bf45STetsuo Handa  *
3972ca9bf45STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3982ca9bf45STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3992ca9bf45STetsuo Handa  *
4002ca9bf45STetsuo Handa  * Returns nothing.
4012ca9bf45STetsuo Handa  */
4022ca9bf45STetsuo Handa static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
4032ca9bf45STetsuo Handa 					   const struct tomoyo_name_union *ptr)
4042ca9bf45STetsuo Handa {
4052ca9bf45STetsuo Handa 	if (ptr->group) {
4062ca9bf45STetsuo Handa 		tomoyo_set_string(head, "@");
4072ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
4082ca9bf45STetsuo Handa 	} else {
4092ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4102ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
4112ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4122ca9bf45STetsuo Handa 	}
4132ca9bf45STetsuo Handa }
4142ca9bf45STetsuo Handa 
4152ca9bf45STetsuo Handa /**
4162066a361STetsuo Handa  * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
4174c3e9e2dSTetsuo Handa  *
4184c3e9e2dSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4194c3e9e2dSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4202066a361STetsuo Handa  *
4212066a361STetsuo Handa  * Returns nothing.
4224c3e9e2dSTetsuo Handa  */
4232066a361STetsuo Handa static void tomoyo_print_number_union_nospace
4242066a361STetsuo Handa (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
4254c3e9e2dSTetsuo Handa {
4260df7e8b8STetsuo Handa 	if (ptr->group) {
427f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
428f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
429f23571e8STetsuo Handa 	} else {
430f23571e8STetsuo Handa 		int i;
431f23571e8STetsuo Handa 		unsigned long min = ptr->values[0];
432f23571e8STetsuo Handa 		const unsigned long max = ptr->values[1];
4330df7e8b8STetsuo Handa 		u8 min_type = ptr->value_type[0];
4340df7e8b8STetsuo Handa 		const u8 max_type = ptr->value_type[1];
435f23571e8STetsuo Handa 		char buffer[128];
436f23571e8STetsuo Handa 		buffer[0] = '\0';
437f23571e8STetsuo Handa 		for (i = 0; i < 2; i++) {
4384c3e9e2dSTetsuo Handa 			switch (min_type) {
4394c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_HEXADECIMAL:
440f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
441f23571e8STetsuo Handa 						 "0x%lX", min);
4424c3e9e2dSTetsuo Handa 				break;
4434c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_OCTAL:
444f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
445f23571e8STetsuo Handa 						 "0%lo", min);
4464c3e9e2dSTetsuo Handa 				break;
4474c3e9e2dSTetsuo Handa 			default:
4482066a361STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
4492066a361STetsuo Handa 						 min);
4504c3e9e2dSTetsuo Handa 				break;
4514c3e9e2dSTetsuo Handa 			}
4524c3e9e2dSTetsuo Handa 			if (min == max && min_type == max_type)
453f23571e8STetsuo Handa 				break;
454f23571e8STetsuo Handa 			tomoyo_addprintf(buffer, sizeof(buffer), "-");
455f23571e8STetsuo Handa 			min_type = max_type;
456f23571e8STetsuo Handa 			min = max;
4574c3e9e2dSTetsuo Handa 		}
458f23571e8STetsuo Handa 		tomoyo_io_printf(head, "%s", buffer);
4594c3e9e2dSTetsuo Handa 	}
4609590837bSKentaro Takeda }
4619590837bSKentaro Takeda 
4629590837bSKentaro Takeda /**
4632066a361STetsuo Handa  * tomoyo_print_number_union - Print a tomoyo_number_union.
4642066a361STetsuo Handa  *
4652066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4662066a361STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4672066a361STetsuo Handa  *
4682066a361STetsuo Handa  * Returns nothing.
4692066a361STetsuo Handa  */
4702066a361STetsuo Handa static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
4712066a361STetsuo Handa 				      const struct tomoyo_number_union *ptr)
4722066a361STetsuo Handa {
4732066a361STetsuo Handa 	tomoyo_set_space(head);
4742066a361STetsuo Handa 	tomoyo_print_number_union_nospace(head, ptr);
4752066a361STetsuo Handa }
4762066a361STetsuo Handa 
4772066a361STetsuo Handa /**
478e2bf6907STetsuo Handa  * tomoyo_assign_profile - Create a new profile.
4799590837bSKentaro Takeda  *
480bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
4819590837bSKentaro Takeda  * @profile: Profile number to create.
4829590837bSKentaro Takeda  *
4839590837bSKentaro Takeda  * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
4849590837bSKentaro Takeda  */
485bd03a3e4STetsuo Handa static struct tomoyo_profile *tomoyo_assign_profile
486bd03a3e4STetsuo Handa (struct tomoyo_policy_namespace *ns, const unsigned int profile)
4879590837bSKentaro Takeda {
48857c2590fSTetsuo Handa 	struct tomoyo_profile *ptr;
48957c2590fSTetsuo Handa 	struct tomoyo_profile *entry;
4909590837bSKentaro Takeda 	if (profile >= TOMOYO_MAX_PROFILES)
4919590837bSKentaro Takeda 		return NULL;
492bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
4939590837bSKentaro Takeda 	if (ptr)
49457c2590fSTetsuo Handa 		return ptr;
49557c2590fSTetsuo Handa 	entry = kzalloc(sizeof(*entry), GFP_NOFS);
49657c2590fSTetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
49757c2590fSTetsuo Handa 		goto out;
498bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
49957c2590fSTetsuo Handa 	if (!ptr && tomoyo_memory_ok(entry)) {
50057c2590fSTetsuo Handa 		ptr = entry;
501eadd99ccSTetsuo Handa 		ptr->default_config = TOMOYO_CONFIG_DISABLED |
502eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_GRANT_LOG |
503eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_REJECT_LOG;
50457c2590fSTetsuo Handa 		memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
50557c2590fSTetsuo Handa 		       sizeof(ptr->config));
5066afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] =
5076afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_AUDIT_LOG;
5086afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] =
5096afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_ACCEPT_ENTRY;
5109590837bSKentaro Takeda 		mb(); /* Avoid out-of-order execution. */
511bd03a3e4STetsuo Handa 		ns->profile_ptr[profile] = ptr;
51257c2590fSTetsuo Handa 		entry = NULL;
51357c2590fSTetsuo Handa 	}
51429282381STetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
51557c2590fSTetsuo Handa  out:
51657c2590fSTetsuo Handa 	kfree(entry);
5179590837bSKentaro Takeda 	return ptr;
5189590837bSKentaro Takeda }
5199590837bSKentaro Takeda 
5209590837bSKentaro Takeda /**
52157c2590fSTetsuo Handa  * tomoyo_profile - Find a profile.
52257c2590fSTetsuo Handa  *
523bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
52457c2590fSTetsuo Handa  * @profile: Profile number to find.
52557c2590fSTetsuo Handa  *
52657c2590fSTetsuo Handa  * Returns pointer to "struct tomoyo_profile".
52757c2590fSTetsuo Handa  */
528bd03a3e4STetsuo Handa struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
529bd03a3e4STetsuo Handa 				      const u8 profile)
53057c2590fSTetsuo Handa {
531d5ca1725STetsuo Handa 	static struct tomoyo_profile tomoyo_null_profile;
532bd03a3e4STetsuo Handa 	struct tomoyo_profile *ptr = ns->profile_ptr[profile];
533d5ca1725STetsuo Handa 	if (!ptr)
534d5ca1725STetsuo Handa 		ptr = &tomoyo_null_profile;
53557c2590fSTetsuo Handa 	return ptr;
53657c2590fSTetsuo Handa }
53757c2590fSTetsuo Handa 
538d5ca1725STetsuo Handa /**
539d5ca1725STetsuo Handa  * tomoyo_find_yesno - Find values for specified keyword.
540d5ca1725STetsuo Handa  *
541d5ca1725STetsuo Handa  * @string: String to check.
542d5ca1725STetsuo Handa  * @find:   Name of keyword.
543d5ca1725STetsuo Handa  *
544d5ca1725STetsuo Handa  * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
545d5ca1725STetsuo Handa  */
5468e568687STetsuo Handa static s8 tomoyo_find_yesno(const char *string, const char *find)
5478e568687STetsuo Handa {
5488e568687STetsuo Handa 	const char *cp = strstr(string, find);
5498e568687STetsuo Handa 	if (cp) {
5508e568687STetsuo Handa 		cp += strlen(find);
5518e568687STetsuo Handa 		if (!strncmp(cp, "=yes", 4))
5528e568687STetsuo Handa 			return 1;
5538e568687STetsuo Handa 		else if (!strncmp(cp, "=no", 3))
5548e568687STetsuo Handa 			return 0;
5558e568687STetsuo Handa 	}
5568e568687STetsuo Handa 	return -1;
5578e568687STetsuo Handa }
5588e568687STetsuo Handa 
559d5ca1725STetsuo Handa /**
560d5ca1725STetsuo Handa  * tomoyo_set_uint - Set value for specified preference.
561d5ca1725STetsuo Handa  *
562d5ca1725STetsuo Handa  * @i:      Pointer to "unsigned int".
563d5ca1725STetsuo Handa  * @string: String to check.
564d5ca1725STetsuo Handa  * @find:   Name of keyword.
565d5ca1725STetsuo Handa  *
566d5ca1725STetsuo Handa  * Returns nothing.
567d5ca1725STetsuo Handa  */
5688e568687STetsuo Handa static void tomoyo_set_uint(unsigned int *i, const char *string,
5698e568687STetsuo Handa 			    const char *find)
5708e568687STetsuo Handa {
5718e568687STetsuo Handa 	const char *cp = strstr(string, find);
5728e568687STetsuo Handa 	if (cp)
5738e568687STetsuo Handa 		sscanf(cp + strlen(find), "=%u", i);
5748e568687STetsuo Handa }
5758e568687STetsuo Handa 
576d5ca1725STetsuo Handa /**
577d5ca1725STetsuo Handa  * tomoyo_set_mode - Set mode for specified profile.
578d5ca1725STetsuo Handa  *
579d5ca1725STetsuo Handa  * @name:    Name of functionality.
580d5ca1725STetsuo Handa  * @value:   Mode for @name.
581d5ca1725STetsuo Handa  * @profile: Pointer to "struct tomoyo_profile".
582d5ca1725STetsuo Handa  *
583d5ca1725STetsuo Handa  * Returns 0 on success, negative value otherwise.
584d5ca1725STetsuo Handa  */
5858e568687STetsuo Handa static int tomoyo_set_mode(char *name, const char *value,
5868e568687STetsuo Handa 			   struct tomoyo_profile *profile)
5878e568687STetsuo Handa {
5888e568687STetsuo Handa 	u8 i;
5898e568687STetsuo Handa 	u8 config;
5908e568687STetsuo Handa 	if (!strcmp(name, "CONFIG")) {
5918e568687STetsuo Handa 		i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
5928e568687STetsuo Handa 		config = profile->default_config;
5938e568687STetsuo Handa 	} else if (tomoyo_str_starts(&name, "CONFIG::")) {
5948e568687STetsuo Handa 		config = 0;
5958e568687STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MAC_INDEX
5968e568687STetsuo Handa 			     + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
5972c47ab93STetsuo Handa 			int len = 0;
5982c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX) {
5992c47ab93STetsuo Handa 				const u8 c = tomoyo_index2category[i];
6002c47ab93STetsuo Handa 				const char *category =
6012c47ab93STetsuo Handa 					tomoyo_category_keywords[c];
6022c47ab93STetsuo Handa 				len = strlen(category);
6032c47ab93STetsuo Handa 				if (strncmp(name, category, len) ||
6042c47ab93STetsuo Handa 				    name[len++] != ':' || name[len++] != ':')
6052c47ab93STetsuo Handa 					continue;
6062c47ab93STetsuo Handa 			}
6072c47ab93STetsuo Handa 			if (strcmp(name + len, tomoyo_mac_keywords[i]))
6088e568687STetsuo Handa 				continue;
6098e568687STetsuo Handa 			config = profile->config[i];
6108e568687STetsuo Handa 			break;
6118e568687STetsuo Handa 		}
6128e568687STetsuo Handa 		if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6138e568687STetsuo Handa 			return -EINVAL;
6148e568687STetsuo Handa 	} else {
6158e568687STetsuo Handa 		return -EINVAL;
6168e568687STetsuo Handa 	}
617d5ca1725STetsuo Handa 	if (strstr(value, "use_default")) {
6188e568687STetsuo Handa 		config = TOMOYO_CONFIG_USE_DEFAULT;
6198e568687STetsuo Handa 	} else {
6208e568687STetsuo Handa 		u8 mode;
6218e568687STetsuo Handa 		for (mode = 0; mode < 4; mode++)
6228e568687STetsuo Handa 			if (strstr(value, tomoyo_mode[mode]))
6238e568687STetsuo Handa 				/*
6248e568687STetsuo Handa 				 * Update lower 3 bits in order to distinguish
6258e568687STetsuo Handa 				 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
6268e568687STetsuo Handa 				 */
6278e568687STetsuo Handa 				config = (config & ~7) | mode;
628eadd99ccSTetsuo Handa 		if (config != TOMOYO_CONFIG_USE_DEFAULT) {
629eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "grant_log")) {
630eadd99ccSTetsuo Handa 			case 1:
631eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
632eadd99ccSTetsuo Handa 				break;
633eadd99ccSTetsuo Handa 			case 0:
634eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
635eadd99ccSTetsuo Handa 				break;
636eadd99ccSTetsuo Handa 			}
637eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "reject_log")) {
638eadd99ccSTetsuo Handa 			case 1:
639eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
640eadd99ccSTetsuo Handa 				break;
641eadd99ccSTetsuo Handa 			case 0:
642eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
643eadd99ccSTetsuo Handa 				break;
644eadd99ccSTetsuo Handa 			}
645eadd99ccSTetsuo Handa 		}
6468e568687STetsuo Handa 	}
6478e568687STetsuo Handa 	if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6488e568687STetsuo Handa 		profile->config[i] = config;
6498e568687STetsuo Handa 	else if (config != TOMOYO_CONFIG_USE_DEFAULT)
6508e568687STetsuo Handa 		profile->default_config = config;
6518e568687STetsuo Handa 	return 0;
6528e568687STetsuo Handa }
6538e568687STetsuo Handa 
65457c2590fSTetsuo Handa /**
65557c2590fSTetsuo Handa  * tomoyo_write_profile - Write profile table.
6569590837bSKentaro Takeda  *
6579590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
6589590837bSKentaro Takeda  *
6599590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
6609590837bSKentaro Takeda  */
6619590837bSKentaro Takeda static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
6629590837bSKentaro Takeda {
6639590837bSKentaro Takeda 	char *data = head->write_buf;
6649590837bSKentaro Takeda 	unsigned int i;
6659590837bSKentaro Takeda 	char *cp;
6669590837bSKentaro Takeda 	struct tomoyo_profile *profile;
667bd03a3e4STetsuo Handa 	if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
668bd03a3e4STetsuo Handa 	    == 1)
66957c2590fSTetsuo Handa 		return 0;
67057c2590fSTetsuo Handa 	i = simple_strtoul(data, &cp, 10);
67157c2590fSTetsuo Handa 	if (*cp != '-')
6729590837bSKentaro Takeda 		return -EINVAL;
6739590837bSKentaro Takeda 	data = cp + 1;
674bd03a3e4STetsuo Handa 	profile = tomoyo_assign_profile(head->w.ns, i);
6759590837bSKentaro Takeda 	if (!profile)
6769590837bSKentaro Takeda 		return -EINVAL;
6779590837bSKentaro Takeda 	cp = strchr(data, '=');
6789590837bSKentaro Takeda 	if (!cp)
6799590837bSKentaro Takeda 		return -EINVAL;
68057c2590fSTetsuo Handa 	*cp++ = '\0';
6819590837bSKentaro Takeda 	if (!strcmp(data, "COMMENT")) {
6822a086e5dSTetsuo Handa 		static DEFINE_SPINLOCK(lock);
6832a086e5dSTetsuo Handa 		const struct tomoyo_path_info *new_comment
6842a086e5dSTetsuo Handa 			= tomoyo_get_name(cp);
6852a086e5dSTetsuo Handa 		const struct tomoyo_path_info *old_comment;
6862a086e5dSTetsuo Handa 		if (!new_comment)
6872a086e5dSTetsuo Handa 			return -ENOMEM;
6882a086e5dSTetsuo Handa 		spin_lock(&lock);
6892a086e5dSTetsuo Handa 		old_comment = profile->comment;
6902a086e5dSTetsuo Handa 		profile->comment = new_comment;
6912a086e5dSTetsuo Handa 		spin_unlock(&lock);
692bf24fb01STetsuo Handa 		tomoyo_put_name(old_comment);
6939590837bSKentaro Takeda 		return 0;
6949590837bSKentaro Takeda 	}
695d5ca1725STetsuo Handa 	if (!strcmp(data, "PREFERENCE")) {
696d5ca1725STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_PREF; i++)
697d5ca1725STetsuo Handa 			tomoyo_set_uint(&profile->pref[i], cp,
698d5ca1725STetsuo Handa 					tomoyo_pref_keywords[i]);
699d5ca1725STetsuo Handa 		return 0;
7009590837bSKentaro Takeda 	}
701d5ca1725STetsuo Handa 	return tomoyo_set_mode(data, cp, profile);
702f23571e8STetsuo Handa }
703f23571e8STetsuo Handa 
704eadd99ccSTetsuo Handa /**
705eadd99ccSTetsuo Handa  * tomoyo_print_config - Print mode for specified functionality.
706eadd99ccSTetsuo Handa  *
707eadd99ccSTetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
708eadd99ccSTetsuo Handa  * @config: Mode for that functionality.
709eadd99ccSTetsuo Handa  *
710eadd99ccSTetsuo Handa  * Returns nothing.
711eadd99ccSTetsuo Handa  *
712eadd99ccSTetsuo Handa  * Caller prints functionality's name.
713eadd99ccSTetsuo Handa  */
714f23571e8STetsuo Handa static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
715f23571e8STetsuo Handa {
716eadd99ccSTetsuo Handa 	tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
717eadd99ccSTetsuo Handa 			 tomoyo_mode[config & 3],
718eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
719eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
720f23571e8STetsuo Handa }
721f23571e8STetsuo Handa 
7229590837bSKentaro Takeda /**
72357c2590fSTetsuo Handa  * tomoyo_read_profile - Read profile table.
7249590837bSKentaro Takeda  *
7259590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
726eadd99ccSTetsuo Handa  *
727eadd99ccSTetsuo Handa  * Returns nothing.
7289590837bSKentaro Takeda  */
7298fbe71f0STetsuo Handa static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
7309590837bSKentaro Takeda {
731f23571e8STetsuo Handa 	u8 index;
732bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
733bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
734f23571e8STetsuo Handa 	const struct tomoyo_profile *profile;
735bd03a3e4STetsuo Handa 	if (head->r.eof)
736bd03a3e4STetsuo Handa 		return;
737f23571e8STetsuo Handa  next:
738f23571e8STetsuo Handa 	index = head->r.index;
739bd03a3e4STetsuo Handa 	profile = ns->profile_ptr[index];
740f23571e8STetsuo Handa 	switch (head->r.step) {
741f23571e8STetsuo Handa 	case 0:
742bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
743bd03a3e4STetsuo Handa 		tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
744bd03a3e4STetsuo Handa 				 ns->profile_version);
745f23571e8STetsuo Handa 		head->r.step++;
746f23571e8STetsuo Handa 		break;
747f23571e8STetsuo Handa 	case 1:
748f23571e8STetsuo Handa 		for ( ; head->r.index < TOMOYO_MAX_PROFILES;
749f23571e8STetsuo Handa 		      head->r.index++)
750bd03a3e4STetsuo Handa 			if (ns->profile_ptr[head->r.index])
751f23571e8STetsuo Handa 				break;
7524d818971STetsuo Handa 		if (head->r.index == TOMOYO_MAX_PROFILES) {
7534d818971STetsuo Handa 			head->r.eof = true;
754f23571e8STetsuo Handa 			return;
7554d818971STetsuo Handa 		}
756f23571e8STetsuo Handa 		head->r.step++;
757f23571e8STetsuo Handa 		break;
758f23571e8STetsuo Handa 	case 2:
759f23571e8STetsuo Handa 		{
760d5ca1725STetsuo Handa 			u8 i;
761f23571e8STetsuo Handa 			const struct tomoyo_path_info *comment =
762f23571e8STetsuo Handa 				profile->comment;
763bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
764f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-COMMENT=", index);
765f23571e8STetsuo Handa 			tomoyo_set_string(head, comment ? comment->name : "");
766f23571e8STetsuo Handa 			tomoyo_set_lf(head);
7674d818971STetsuo Handa 			tomoyo_print_namespace(head);
768d5ca1725STetsuo Handa 			tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
769d5ca1725STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_PREF; i++)
770d5ca1725STetsuo Handa 				tomoyo_io_printf(head, "%s=%u ",
771d5ca1725STetsuo Handa 						 tomoyo_pref_keywords[i],
772d5ca1725STetsuo Handa 						 profile->pref[i]);
773d5ca1725STetsuo Handa 			tomoyo_set_string(head, "}\n");
774f23571e8STetsuo Handa 			head->r.step++;
775f23571e8STetsuo Handa 		}
776f23571e8STetsuo Handa 		break;
777f23571e8STetsuo Handa 	case 3:
778f23571e8STetsuo Handa 		{
779bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
780f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
781f23571e8STetsuo Handa 			tomoyo_print_config(head, profile->default_config);
782f23571e8STetsuo Handa 			head->r.bit = 0;
783f23571e8STetsuo Handa 			head->r.step++;
784f23571e8STetsuo Handa 		}
785f23571e8STetsuo Handa 		break;
786f23571e8STetsuo Handa 	case 4:
787f23571e8STetsuo Handa 		for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
788f23571e8STetsuo Handa 			      + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
789f23571e8STetsuo Handa 			const u8 i = head->r.bit;
790f23571e8STetsuo Handa 			const u8 config = profile->config[i];
79157c2590fSTetsuo Handa 			if (config == TOMOYO_CONFIG_USE_DEFAULT)
7929590837bSKentaro Takeda 				continue;
793bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
7942c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX)
7952c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
7962c47ab93STetsuo Handa 						 index,
7972c47ab93STetsuo Handa 						 tomoyo_category_keywords
7982c47ab93STetsuo Handa 						 [tomoyo_index2category[i]],
7992c47ab93STetsuo Handa 						 tomoyo_mac_keywords[i]);
8002c47ab93STetsuo Handa 			else
8012c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s", index,
802f23571e8STetsuo Handa 						 tomoyo_mac_keywords[i]);
803f23571e8STetsuo Handa 			tomoyo_print_config(head, config);
804f23571e8STetsuo Handa 			head->r.bit++;
8059590837bSKentaro Takeda 			break;
8069590837bSKentaro Takeda 		}
807f23571e8STetsuo Handa 		if (head->r.bit == TOMOYO_MAX_MAC_INDEX
808f23571e8STetsuo Handa 		    + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
809f23571e8STetsuo Handa 			head->r.index++;
810f23571e8STetsuo Handa 			head->r.step = 1;
811f23571e8STetsuo Handa 		}
812f23571e8STetsuo Handa 		break;
813f23571e8STetsuo Handa 	}
814f23571e8STetsuo Handa 	if (tomoyo_flush(head))
815f23571e8STetsuo Handa 		goto next;
8169590837bSKentaro Takeda }
8179590837bSKentaro Takeda 
8180f2a55d5STetsuo Handa /**
8190f2a55d5STetsuo Handa  * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
8200f2a55d5STetsuo Handa  *
8210f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
8220f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
8230f2a55d5STetsuo Handa  *
8240f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
8250f2a55d5STetsuo Handa  */
826e2bf6907STetsuo Handa static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
82736f5e1ffSTetsuo Handa 				const struct tomoyo_acl_head *b)
82836f5e1ffSTetsuo Handa {
829e2bf6907STetsuo Handa 	return container_of(a, struct tomoyo_manager, head)->manager ==
830e2bf6907STetsuo Handa 		container_of(b, struct tomoyo_manager, head)->manager;
83136f5e1ffSTetsuo Handa }
83236f5e1ffSTetsuo Handa 
8339590837bSKentaro Takeda /**
8349590837bSKentaro Takeda  * tomoyo_update_manager_entry - Add a manager entry.
8359590837bSKentaro Takeda  *
8369590837bSKentaro Takeda  * @manager:   The path to manager or the domainnamme.
8379590837bSKentaro Takeda  * @is_delete: True if it is a delete request.
8389590837bSKentaro Takeda  *
8399590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
840fdb8ebb7STetsuo Handa  *
841fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8429590837bSKentaro Takeda  */
8439590837bSKentaro Takeda static int tomoyo_update_manager_entry(const char *manager,
8449590837bSKentaro Takeda 				       const bool is_delete)
8459590837bSKentaro Takeda {
846e2bf6907STetsuo Handa 	struct tomoyo_manager e = { };
847a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
848bd03a3e4STetsuo Handa 		/* .ns = &tomoyo_kernel_namespace, */
849a238cf5bSTetsuo Handa 		.is_delete = is_delete,
850bd03a3e4STetsuo Handa 		.list = &tomoyo_kernel_namespace.
851bd03a3e4STetsuo Handa 		policy_list[TOMOYO_ID_MANAGER],
852a238cf5bSTetsuo Handa 	};
853a238cf5bSTetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
85477b513ddSTetsuo Handa 	if (!tomoyo_correct_domain(manager) &&
85577b513ddSTetsuo Handa 	    !tomoyo_correct_word(manager))
8569590837bSKentaro Takeda 		return -EINVAL;
8579e4b50e9STetsuo Handa 	e.manager = tomoyo_get_name(manager);
858a238cf5bSTetsuo Handa 	if (e.manager) {
859a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), &param,
860e2bf6907STetsuo Handa 					     tomoyo_same_manager);
8619e4b50e9STetsuo Handa 		tomoyo_put_name(e.manager);
862a238cf5bSTetsuo Handa 	}
8639590837bSKentaro Takeda 	return error;
8649590837bSKentaro Takeda }
8659590837bSKentaro Takeda 
8669590837bSKentaro Takeda /**
867e2bf6907STetsuo Handa  * tomoyo_write_manager - Write manager policy.
8689590837bSKentaro Takeda  *
8699590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8709590837bSKentaro Takeda  *
8719590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
872fdb8ebb7STetsuo Handa  *
873fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8749590837bSKentaro Takeda  */
875e2bf6907STetsuo Handa static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
8769590837bSKentaro Takeda {
8779590837bSKentaro Takeda 	char *data = head->write_buf;
8789590837bSKentaro Takeda 
8799590837bSKentaro Takeda 	if (!strcmp(data, "manage_by_non_root")) {
880bd03a3e4STetsuo Handa 		tomoyo_manage_by_non_root = !head->w.is_delete;
8819590837bSKentaro Takeda 		return 0;
8829590837bSKentaro Takeda 	}
883bd03a3e4STetsuo Handa 	return tomoyo_update_manager_entry(data, head->w.is_delete);
8849590837bSKentaro Takeda }
8859590837bSKentaro Takeda 
8869590837bSKentaro Takeda /**
887e2bf6907STetsuo Handa  * tomoyo_read_manager - Read manager policy.
8889590837bSKentaro Takeda  *
8899590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8909590837bSKentaro Takeda  *
891fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8929590837bSKentaro Takeda  */
893e2bf6907STetsuo Handa static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
8949590837bSKentaro Takeda {
895f23571e8STetsuo Handa 	if (head->r.eof)
8968fbe71f0STetsuo Handa 		return;
897bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
898bd03a3e4STetsuo Handa 			     policy_list[TOMOYO_ID_MANAGER]) {
899e2bf6907STetsuo Handa 		struct tomoyo_manager *ptr =
900f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), head.list);
90182e0f001STetsuo Handa 		if (ptr->head.is_deleted)
9029590837bSKentaro Takeda 			continue;
903f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
904f23571e8STetsuo Handa 			return;
905f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->manager->name);
906f23571e8STetsuo Handa 		tomoyo_set_lf(head);
9079590837bSKentaro Takeda 	}
908f23571e8STetsuo Handa 	head->r.eof = true;
9099590837bSKentaro Takeda }
9109590837bSKentaro Takeda 
9119590837bSKentaro Takeda /**
912e2bf6907STetsuo Handa  * tomoyo_manager - Check whether the current process is a policy manager.
9139590837bSKentaro Takeda  *
9149590837bSKentaro Takeda  * Returns true if the current process is permitted to modify policy
9159590837bSKentaro Takeda  * via /sys/kernel/security/tomoyo/ interface.
916fdb8ebb7STetsuo Handa  *
917fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9189590837bSKentaro Takeda  */
919e2bf6907STetsuo Handa static bool tomoyo_manager(void)
9209590837bSKentaro Takeda {
921e2bf6907STetsuo Handa 	struct tomoyo_manager *ptr;
9229590837bSKentaro Takeda 	const char *exe;
9239590837bSKentaro Takeda 	const struct task_struct *task = current;
9249590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
9259590837bSKentaro Takeda 	bool found = false;
9269590837bSKentaro Takeda 
9279590837bSKentaro Takeda 	if (!tomoyo_policy_loaded)
9289590837bSKentaro Takeda 		return true;
929609fcd1bSEric W. Biederman 	if (!tomoyo_manage_by_non_root &&
930609fcd1bSEric W. Biederman 	    (!uid_eq(task->cred->uid,  GLOBAL_ROOT_UID) ||
931609fcd1bSEric W. Biederman 	     !uid_eq(task->cred->euid, GLOBAL_ROOT_UID)))
9329590837bSKentaro Takeda 		return false;
9339590837bSKentaro Takeda 	exe = tomoyo_get_exe();
9349590837bSKentaro Takeda 	if (!exe)
9359590837bSKentaro Takeda 		return false;
936bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
937bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
93877b513ddSTetsuo Handa 		if (!ptr->head.is_deleted &&
93977b513ddSTetsuo Handa 		    (!tomoyo_pathcmp(domainname, ptr->manager) ||
94077b513ddSTetsuo Handa 		     !strcmp(exe, ptr->manager->name))) {
9419590837bSKentaro Takeda 			found = true;
9429590837bSKentaro Takeda 			break;
9439590837bSKentaro Takeda 		}
9449590837bSKentaro Takeda 	}
9459590837bSKentaro Takeda 	if (!found) { /* Reduce error messages. */
9469590837bSKentaro Takeda 		static pid_t last_pid;
9479590837bSKentaro Takeda 		const pid_t pid = current->pid;
9489590837bSKentaro Takeda 		if (last_pid != pid) {
9499590837bSKentaro Takeda 			printk(KERN_WARNING "%s ( %s ) is not permitted to "
9509590837bSKentaro Takeda 			       "update policies.\n", domainname->name, exe);
9519590837bSKentaro Takeda 			last_pid = pid;
9529590837bSKentaro Takeda 		}
9539590837bSKentaro Takeda 	}
9548e2d39a1STetsuo Handa 	kfree(exe);
9559590837bSKentaro Takeda 	return found;
9569590837bSKentaro Takeda }
9579590837bSKentaro Takeda 
95859df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
95959df3166STetsuo Handa (unsigned int serial);
96059df3166STetsuo Handa 
9619590837bSKentaro Takeda /**
962bd03a3e4STetsuo Handa  * tomoyo_select_domain - Parse select command.
9639590837bSKentaro Takeda  *
9649590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9659590837bSKentaro Takeda  * @data: String to parse.
9669590837bSKentaro Takeda  *
9679590837bSKentaro Takeda  * Returns true on success, false otherwise.
968fdb8ebb7STetsuo Handa  *
969fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9709590837bSKentaro Takeda  */
971bd03a3e4STetsuo Handa static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
972bd03a3e4STetsuo Handa 				 const char *data)
9739590837bSKentaro Takeda {
9749590837bSKentaro Takeda 	unsigned int pid;
9759590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
9769b244373STetsuo Handa 	bool global_pid = false;
977bd03a3e4STetsuo Handa 	if (strncmp(data, "select ", 7))
978bd03a3e4STetsuo Handa 		return false;
979bd03a3e4STetsuo Handa 	data += 7;
9809b244373STetsuo Handa 	if (sscanf(data, "pid=%u", &pid) == 1 ||
9819b244373STetsuo Handa 	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9829590837bSKentaro Takeda 		struct task_struct *p;
9831fcdc7c5STetsuo Handa 		rcu_read_lock();
9849b244373STetsuo Handa 		if (global_pid)
9859b244373STetsuo Handa 			p = find_task_by_pid_ns(pid, &init_pid_ns);
9869b244373STetsuo Handa 		else
9879590837bSKentaro Takeda 			p = find_task_by_vpid(pid);
9889590837bSKentaro Takeda 		if (p)
989*8c6cb983STetsuo Handa 			domain = tomoyo_task(p)->domain_info;
9901fcdc7c5STetsuo Handa 		rcu_read_unlock();
9919590837bSKentaro Takeda 	} else if (!strncmp(data, "domain=", 7)) {
99275093152STetsuo Handa 		if (tomoyo_domain_def(data + 7))
9939590837bSKentaro Takeda 			domain = tomoyo_find_domain(data + 7);
99459df3166STetsuo Handa 	} else if (sscanf(data, "Q=%u", &pid) == 1) {
99559df3166STetsuo Handa 		domain = tomoyo_find_domain_by_qid(pid);
9969590837bSKentaro Takeda 	} else
9979590837bSKentaro Takeda 		return false;
9980df7e8b8STetsuo Handa 	head->w.domain = domain;
9999590837bSKentaro Takeda 	/* Accessing read_buf is safe because head->io_sem is held. */
10009590837bSKentaro Takeda 	if (!head->read_buf)
10019590837bSKentaro Takeda 		return true; /* Do nothing if open(O_WRONLY). */
1002f23571e8STetsuo Handa 	memset(&head->r, 0, sizeof(head->r));
1003f23571e8STetsuo Handa 	head->r.print_this_domain_only = true;
100468eda8f5SDan Carpenter 	if (domain)
1005f23571e8STetsuo Handa 		head->r.domain = &domain->list;
100668eda8f5SDan Carpenter 	else
100768eda8f5SDan Carpenter 		head->r.eof = 1;
10089590837bSKentaro Takeda 	tomoyo_io_printf(head, "# select %s\n", data);
1009475e6fa3STetsuo Handa 	if (domain && domain->is_deleted)
10109590837bSKentaro Takeda 		tomoyo_io_printf(head, "# This is a deleted domain.\n");
10119590837bSKentaro Takeda 	return true;
10129590837bSKentaro Takeda }
10139590837bSKentaro Takeda 
10149590837bSKentaro Takeda /**
1015731d37aaSTetsuo Handa  * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1016731d37aaSTetsuo Handa  *
1017731d37aaSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_info".
1018731d37aaSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_info".
1019731d37aaSTetsuo Handa  *
1020731d37aaSTetsuo Handa  * Returns true if @a == @b, false otherwise.
1021731d37aaSTetsuo Handa  */
1022731d37aaSTetsuo Handa static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1023731d37aaSTetsuo Handa 			      const struct tomoyo_acl_info *b)
1024731d37aaSTetsuo Handa {
1025731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1026731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1027731d37aaSTetsuo Handa 	return p1->domainname == p2->domainname;
1028731d37aaSTetsuo Handa }
1029731d37aaSTetsuo Handa 
1030731d37aaSTetsuo Handa /**
1031731d37aaSTetsuo Handa  * tomoyo_write_task - Update task related list.
1032731d37aaSTetsuo Handa  *
1033731d37aaSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
1034731d37aaSTetsuo Handa  *
1035731d37aaSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1036731d37aaSTetsuo Handa  *
1037731d37aaSTetsuo Handa  * Caller holds tomoyo_read_lock().
1038731d37aaSTetsuo Handa  */
1039731d37aaSTetsuo Handa static int tomoyo_write_task(struct tomoyo_acl_param *param)
1040731d37aaSTetsuo Handa {
1041731d37aaSTetsuo Handa 	int error = -EINVAL;
1042731d37aaSTetsuo Handa 	if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1043731d37aaSTetsuo Handa 		struct tomoyo_task_acl e = {
1044731d37aaSTetsuo Handa 			.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1045731d37aaSTetsuo Handa 			.domainname = tomoyo_get_domainname(param),
1046731d37aaSTetsuo Handa 		};
1047731d37aaSTetsuo Handa 		if (e.domainname)
1048731d37aaSTetsuo Handa 			error = tomoyo_update_domain(&e.head, sizeof(e), param,
1049731d37aaSTetsuo Handa 						     tomoyo_same_task_acl,
1050731d37aaSTetsuo Handa 						     NULL);
1051731d37aaSTetsuo Handa 		tomoyo_put_name(e.domainname);
1052731d37aaSTetsuo Handa 	}
1053731d37aaSTetsuo Handa 	return error;
1054731d37aaSTetsuo Handa }
1055731d37aaSTetsuo Handa 
1056731d37aaSTetsuo Handa /**
1057ccf135f5STetsuo Handa  * tomoyo_delete_domain - Delete a domain.
1058ccf135f5STetsuo Handa  *
1059ccf135f5STetsuo Handa  * @domainname: The name of domain.
1060ccf135f5STetsuo Handa  *
10617d7473dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1062fdb8ebb7STetsuo Handa  *
1063fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1064ccf135f5STetsuo Handa  */
1065ccf135f5STetsuo Handa static int tomoyo_delete_domain(char *domainname)
1066ccf135f5STetsuo Handa {
1067ccf135f5STetsuo Handa 	struct tomoyo_domain_info *domain;
1068ccf135f5STetsuo Handa 	struct tomoyo_path_info name;
1069ccf135f5STetsuo Handa 
1070ccf135f5STetsuo Handa 	name.name = domainname;
1071ccf135f5STetsuo Handa 	tomoyo_fill_path_info(&name);
107229282381STetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
10737d7473dbSTetsuo Handa 		return -EINTR;
1074ccf135f5STetsuo Handa 	/* Is there an active domain? */
1075fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1076ccf135f5STetsuo Handa 		/* Never delete tomoyo_kernel_domain */
1077ccf135f5STetsuo Handa 		if (domain == &tomoyo_kernel_domain)
1078ccf135f5STetsuo Handa 			continue;
1079ccf135f5STetsuo Handa 		if (domain->is_deleted ||
1080ccf135f5STetsuo Handa 		    tomoyo_pathcmp(domain->domainname, &name))
1081ccf135f5STetsuo Handa 			continue;
1082ccf135f5STetsuo Handa 		domain->is_deleted = true;
1083ccf135f5STetsuo Handa 		break;
1084ccf135f5STetsuo Handa 	}
1085f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
1086ccf135f5STetsuo Handa 	return 0;
1087ccf135f5STetsuo Handa }
1088ccf135f5STetsuo Handa 
1089ccf135f5STetsuo Handa /**
1090e2bf6907STetsuo Handa  * tomoyo_write_domain2 - Write domain policy.
109117fcfbd9STetsuo Handa  *
1092bd03a3e4STetsuo Handa  * @ns:        Pointer to "struct tomoyo_policy_namespace".
1093a238cf5bSTetsuo Handa  * @list:      Pointer to "struct list_head".
1094a238cf5bSTetsuo Handa  * @data:      Policy to be interpreted.
1095a238cf5bSTetsuo Handa  * @is_delete: True if it is a delete request.
109617fcfbd9STetsuo Handa  *
109717fcfbd9STetsuo Handa  * Returns 0 on success, negative value otherwise.
109817fcfbd9STetsuo Handa  *
109917fcfbd9STetsuo Handa  * Caller holds tomoyo_read_lock().
110017fcfbd9STetsuo Handa  */
1101bd03a3e4STetsuo Handa static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1102bd03a3e4STetsuo Handa 				struct list_head *list, char *data,
110317fcfbd9STetsuo Handa 				const bool is_delete)
110417fcfbd9STetsuo Handa {
1105a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1106bd03a3e4STetsuo Handa 		.ns = ns,
1107a238cf5bSTetsuo Handa 		.list = list,
1108a238cf5bSTetsuo Handa 		.data = data,
1109a238cf5bSTetsuo Handa 		.is_delete = is_delete,
1110a238cf5bSTetsuo Handa 	};
1111a238cf5bSTetsuo Handa 	static const struct {
1112a238cf5bSTetsuo Handa 		const char *keyword;
1113a238cf5bSTetsuo Handa 		int (*write) (struct tomoyo_acl_param *);
1114731d37aaSTetsuo Handa 	} tomoyo_callback[5] = {
1115a238cf5bSTetsuo Handa 		{ "file ", tomoyo_write_file },
1116059d84dbSTetsuo Handa 		{ "network inet ", tomoyo_write_inet_network },
1117059d84dbSTetsuo Handa 		{ "network unix ", tomoyo_write_unix_network },
1118d58e0da8STetsuo Handa 		{ "misc ", tomoyo_write_misc },
1119731d37aaSTetsuo Handa 		{ "task ", tomoyo_write_task },
1120a238cf5bSTetsuo Handa 	};
1121a238cf5bSTetsuo Handa 	u8 i;
1122d58e0da8STetsuo Handa 
1123d58e0da8STetsuo Handa 	for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
1124a238cf5bSTetsuo Handa 		if (!tomoyo_str_starts(&param.data,
1125a238cf5bSTetsuo Handa 				       tomoyo_callback[i].keyword))
1126a238cf5bSTetsuo Handa 			continue;
1127a238cf5bSTetsuo Handa 		return tomoyo_callback[i].write(&param);
1128a238cf5bSTetsuo Handa 	}
1129a238cf5bSTetsuo Handa 	return -EINVAL;
113017fcfbd9STetsuo Handa }
113117fcfbd9STetsuo Handa 
11322c47ab93STetsuo Handa /* String table for domain flags. */
11332c47ab93STetsuo Handa const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
11342c47ab93STetsuo Handa 	[TOMOYO_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
11352c47ab93STetsuo Handa 	[TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
11362c47ab93STetsuo Handa };
11372c47ab93STetsuo Handa 
113817fcfbd9STetsuo Handa /**
1139e2bf6907STetsuo Handa  * tomoyo_write_domain - Write domain policy.
11409590837bSKentaro Takeda  *
11419590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
11429590837bSKentaro Takeda  *
11439590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1144fdb8ebb7STetsuo Handa  *
1145fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
11469590837bSKentaro Takeda  */
1147e2bf6907STetsuo Handa static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
11489590837bSKentaro Takeda {
11499590837bSKentaro Takeda 	char *data = head->write_buf;
1150bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
11510df7e8b8STetsuo Handa 	struct tomoyo_domain_info *domain = head->w.domain;
1152bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1153bd03a3e4STetsuo Handa 	bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
11549590837bSKentaro Takeda 	unsigned int profile;
1155bd03a3e4STetsuo Handa 	if (*data == '<') {
11567d7473dbSTetsuo Handa 		int ret = 0;
11579590837bSKentaro Takeda 		domain = NULL;
11589590837bSKentaro Takeda 		if (is_delete)
11597d7473dbSTetsuo Handa 			ret = tomoyo_delete_domain(data);
1160fdb8ebb7STetsuo Handa 		else if (is_select)
11619590837bSKentaro Takeda 			domain = tomoyo_find_domain(data);
1162fdb8ebb7STetsuo Handa 		else
1163bd03a3e4STetsuo Handa 			domain = tomoyo_assign_domain(data, false);
11640df7e8b8STetsuo Handa 		head->w.domain = domain;
11657d7473dbSTetsuo Handa 		return ret;
11669590837bSKentaro Takeda 	}
11679590837bSKentaro Takeda 	if (!domain)
11689590837bSKentaro Takeda 		return -EINVAL;
1169bd03a3e4STetsuo Handa 	ns = domain->ns;
1170b5bc60b4STetsuo Handa 	if (sscanf(data, "use_profile %u", &profile) == 1
11719590837bSKentaro Takeda 	    && profile < TOMOYO_MAX_PROFILES) {
1172bd03a3e4STetsuo Handa 		if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
11739590837bSKentaro Takeda 			domain->profile = (u8) profile;
11749590837bSKentaro Takeda 		return 0;
11759590837bSKentaro Takeda 	}
117632997144STetsuo Handa 	if (sscanf(data, "use_group %u\n", &profile) == 1
117732997144STetsuo Handa 	    && profile < TOMOYO_MAX_ACL_GROUPS) {
117832997144STetsuo Handa 		if (!is_delete)
117932997144STetsuo Handa 			domain->group = (u8) profile;
118032997144STetsuo Handa 		return 0;
118132997144STetsuo Handa 	}
11822c47ab93STetsuo Handa 	for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
11832c47ab93STetsuo Handa 		const char *cp = tomoyo_dif[profile];
11842c47ab93STetsuo Handa 		if (strncmp(data, cp, strlen(cp) - 1))
11852c47ab93STetsuo Handa 			continue;
11862c47ab93STetsuo Handa 		domain->flags[profile] = !is_delete;
11879b244373STetsuo Handa 		return 0;
11889b244373STetsuo Handa 	}
1189bd03a3e4STetsuo Handa 	return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1190bd03a3e4STetsuo Handa 				    is_delete);
11919590837bSKentaro Takeda }
11929590837bSKentaro Takeda 
11939590837bSKentaro Takeda /**
11942066a361STetsuo Handa  * tomoyo_print_condition - Print condition part.
11952066a361STetsuo Handa  *
11962066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
11972066a361STetsuo Handa  * @cond: Pointer to "struct tomoyo_condition".
11982066a361STetsuo Handa  *
11992066a361STetsuo Handa  * Returns true on success, false otherwise.
12002066a361STetsuo Handa  */
12012066a361STetsuo Handa static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
12022066a361STetsuo Handa 				   const struct tomoyo_condition *cond)
12032066a361STetsuo Handa {
12042066a361STetsuo Handa 	switch (head->r.cond_step) {
12052066a361STetsuo Handa 	case 0:
12062066a361STetsuo Handa 		head->r.cond_index = 0;
12072066a361STetsuo Handa 		head->r.cond_step++;
12086bce98edSTetsuo Handa 		if (cond->transit) {
12096bce98edSTetsuo Handa 			tomoyo_set_space(head);
12106bce98edSTetsuo Handa 			tomoyo_set_string(head, cond->transit->name);
12116bce98edSTetsuo Handa 		}
12122066a361STetsuo Handa 		/* fall through */
12132066a361STetsuo Handa 	case 1:
12142066a361STetsuo Handa 		{
12152066a361STetsuo Handa 			const u16 condc = cond->condc;
12162066a361STetsuo Handa 			const struct tomoyo_condition_element *condp =
12172066a361STetsuo Handa 				(typeof(condp)) (cond + 1);
12182066a361STetsuo Handa 			const struct tomoyo_number_union *numbers_p =
12192066a361STetsuo Handa 				(typeof(numbers_p)) (condp + condc);
12202ca9bf45STetsuo Handa 			const struct tomoyo_name_union *names_p =
12212ca9bf45STetsuo Handa 				(typeof(names_p))
12222ca9bf45STetsuo Handa 				(numbers_p + cond->numbers_count);
12235b636857STetsuo Handa 			const struct tomoyo_argv *argv =
12245b636857STetsuo Handa 				(typeof(argv)) (names_p + cond->names_count);
12255b636857STetsuo Handa 			const struct tomoyo_envp *envp =
12265b636857STetsuo Handa 				(typeof(envp)) (argv + cond->argc);
12272066a361STetsuo Handa 			u16 skip;
12282066a361STetsuo Handa 			for (skip = 0; skip < head->r.cond_index; skip++) {
12292066a361STetsuo Handa 				const u8 left = condp->left;
12302066a361STetsuo Handa 				const u8 right = condp->right;
12312066a361STetsuo Handa 				condp++;
12322066a361STetsuo Handa 				switch (left) {
12335b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12345b636857STetsuo Handa 					argv++;
12355b636857STetsuo Handa 					continue;
12365b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12375b636857STetsuo Handa 					envp++;
12385b636857STetsuo Handa 					continue;
12392066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12402066a361STetsuo Handa 					numbers_p++;
12412066a361STetsuo Handa 					break;
12422066a361STetsuo Handa 				}
12432066a361STetsuo Handa 				switch (right) {
12442ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
12452ca9bf45STetsuo Handa 					names_p++;
12462ca9bf45STetsuo Handa 					break;
12472066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12482066a361STetsuo Handa 					numbers_p++;
12492066a361STetsuo Handa 					break;
12502066a361STetsuo Handa 				}
12512066a361STetsuo Handa 			}
12522066a361STetsuo Handa 			while (head->r.cond_index < condc) {
12532066a361STetsuo Handa 				const u8 match = condp->equals;
12542066a361STetsuo Handa 				const u8 left = condp->left;
12552066a361STetsuo Handa 				const u8 right = condp->right;
12562066a361STetsuo Handa 				if (!tomoyo_flush(head))
12572066a361STetsuo Handa 					return false;
12582066a361STetsuo Handa 				condp++;
12592066a361STetsuo Handa 				head->r.cond_index++;
12602066a361STetsuo Handa 				tomoyo_set_space(head);
12612066a361STetsuo Handa 				switch (left) {
12625b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12635b636857STetsuo Handa 					tomoyo_io_printf(head,
12645b636857STetsuo Handa 							 "exec.argv[%lu]%s=\"",
12655b636857STetsuo Handa 							 argv->index, argv->
12665b636857STetsuo Handa 							 is_not ? "!" : "");
12675b636857STetsuo Handa 					tomoyo_set_string(head,
12685b636857STetsuo Handa 							  argv->value->name);
12695b636857STetsuo Handa 					tomoyo_set_string(head, "\"");
12705b636857STetsuo Handa 					argv++;
12715b636857STetsuo Handa 					continue;
12725b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12735b636857STetsuo Handa 					tomoyo_set_string(head,
12745b636857STetsuo Handa 							  "exec.envp[\"");
12755b636857STetsuo Handa 					tomoyo_set_string(head,
12765b636857STetsuo Handa 							  envp->name->name);
12775b636857STetsuo Handa 					tomoyo_io_printf(head, "\"]%s=", envp->
12785b636857STetsuo Handa 							 is_not ? "!" : "");
12795b636857STetsuo Handa 					if (envp->value) {
12805b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12815b636857STetsuo Handa 						tomoyo_set_string(head, envp->
12825b636857STetsuo Handa 								  value->name);
12835b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12845b636857STetsuo Handa 					} else {
12855b636857STetsuo Handa 						tomoyo_set_string(head,
12865b636857STetsuo Handa 								  "NULL");
12875b636857STetsuo Handa 					}
12885b636857STetsuo Handa 					envp++;
12895b636857STetsuo Handa 					continue;
12902066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12912066a361STetsuo Handa 					tomoyo_print_number_union_nospace
12922066a361STetsuo Handa 						(head, numbers_p++);
12932066a361STetsuo Handa 					break;
12942066a361STetsuo Handa 				default:
12952066a361STetsuo Handa 					tomoyo_set_string(head,
12962066a361STetsuo Handa 					       tomoyo_condition_keyword[left]);
12972066a361STetsuo Handa 					break;
12982066a361STetsuo Handa 				}
12992066a361STetsuo Handa 				tomoyo_set_string(head, match ? "=" : "!=");
13002066a361STetsuo Handa 				switch (right) {
13012ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
13022ca9bf45STetsuo Handa 					tomoyo_print_name_union_quoted
13032ca9bf45STetsuo Handa 						(head, names_p++);
13042ca9bf45STetsuo Handa 					break;
13052066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13062066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13072066a361STetsuo Handa 						(head, numbers_p++);
13082066a361STetsuo Handa 					break;
13092066a361STetsuo Handa 				default:
13102066a361STetsuo Handa 					tomoyo_set_string(head,
13112066a361STetsuo Handa 					  tomoyo_condition_keyword[right]);
13122066a361STetsuo Handa 					break;
13132066a361STetsuo Handa 				}
13142066a361STetsuo Handa 			}
13152066a361STetsuo Handa 		}
13162066a361STetsuo Handa 		head->r.cond_step++;
13172066a361STetsuo Handa 		/* fall through */
13182066a361STetsuo Handa 	case 2:
13192066a361STetsuo Handa 		if (!tomoyo_flush(head))
13202066a361STetsuo Handa 			break;
13212066a361STetsuo Handa 		head->r.cond_step++;
13222066a361STetsuo Handa 		/* fall through */
13232066a361STetsuo Handa 	case 3:
13241f067a68STetsuo Handa 		if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13251f067a68STetsuo Handa 			tomoyo_io_printf(head, " grant_log=%s",
13261f067a68STetsuo Handa 					 tomoyo_yesno(cond->grant_log ==
13271f067a68STetsuo Handa 						      TOMOYO_GRANTLOG_YES));
13282066a361STetsuo Handa 		tomoyo_set_lf(head);
13292066a361STetsuo Handa 		return true;
13302066a361STetsuo Handa 	}
13312066a361STetsuo Handa 	return false;
13322066a361STetsuo Handa }
13332066a361STetsuo Handa 
13342066a361STetsuo Handa /**
133532997144STetsuo Handa  * tomoyo_set_group - Print "acl_group " header keyword and category name.
13369590837bSKentaro Takeda  *
13370d2171d7STetsuo Handa  * @head:     Pointer to "struct tomoyo_io_buffer".
13380d2171d7STetsuo Handa  * @category: Category name.
13399590837bSKentaro Takeda  *
13400d2171d7STetsuo Handa  * Returns nothing.
13419590837bSKentaro Takeda  */
13420d2171d7STetsuo Handa static void tomoyo_set_group(struct tomoyo_io_buffer *head,
13430d2171d7STetsuo Handa 			     const char *category)
13449590837bSKentaro Takeda {
1345bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1346bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
134732997144STetsuo Handa 		tomoyo_io_printf(head, "acl_group %u ",
134832997144STetsuo Handa 				 head->r.acl_group_index);
1349bd03a3e4STetsuo Handa 	}
13500d2171d7STetsuo Handa 	tomoyo_set_string(head, category);
13512106ccd9STetsuo Handa }
13522106ccd9STetsuo Handa 
13532106ccd9STetsuo Handa /**
13549590837bSKentaro Takeda  * tomoyo_print_entry - Print an ACL entry.
13559590837bSKentaro Takeda  *
13569590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
13575db5a39bSTetsuo Handa  * @acl:  Pointer to an ACL entry.
13589590837bSKentaro Takeda  *
13599590837bSKentaro Takeda  * Returns true on success, false otherwise.
13609590837bSKentaro Takeda  */
13619590837bSKentaro Takeda static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13625db5a39bSTetsuo Handa 			       struct tomoyo_acl_info *acl)
13639590837bSKentaro Takeda {
13645db5a39bSTetsuo Handa 	const u8 acl_type = acl->type;
13650d2171d7STetsuo Handa 	bool first = true;
1366f23571e8STetsuo Handa 	u8 bit;
13679590837bSKentaro Takeda 
13682066a361STetsuo Handa 	if (head->r.print_cond_part)
13692066a361STetsuo Handa 		goto print_cond_part;
13705db5a39bSTetsuo Handa 	if (acl->is_deleted)
1371237ab459STetsuo Handa 		return true;
1372f23571e8STetsuo Handa 	if (!tomoyo_flush(head))
1373f23571e8STetsuo Handa 		return false;
1374f23571e8STetsuo Handa 	else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
13755db5a39bSTetsuo Handa 		struct tomoyo_path_acl *ptr =
13765db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
13775db5a39bSTetsuo Handa 		const u16 perm = ptr->perm;
13780d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13795db5a39bSTetsuo Handa 			if (!(perm & (1 << bit)))
13805db5a39bSTetsuo Handa 				continue;
1381bd03a3e4STetsuo Handa 			if (head->r.print_transition_related_only &&
13825db5a39bSTetsuo Handa 			    bit != TOMOYO_TYPE_EXECUTE)
13835db5a39bSTetsuo Handa 				continue;
13840d2171d7STetsuo Handa 			if (first) {
13850d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
13860d2171d7STetsuo Handa 				first = false;
13870d2171d7STetsuo Handa 			} else {
13880d2171d7STetsuo Handa 				tomoyo_set_slash(head);
13899590837bSKentaro Takeda 			}
13900d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_path_keyword[bit]);
13910d2171d7STetsuo Handa 		}
13920d2171d7STetsuo Handa 		if (first)
13930d2171d7STetsuo Handa 			return true;
1394f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1395731d37aaSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1396731d37aaSTetsuo Handa 		struct tomoyo_task_acl *ptr =
1397731d37aaSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1398731d37aaSTetsuo Handa 		tomoyo_set_group(head, "task ");
1399731d37aaSTetsuo Handa 		tomoyo_set_string(head, "manual_domain_transition ");
1400731d37aaSTetsuo Handa 		tomoyo_set_string(head, ptr->domainname->name);
1401bd03a3e4STetsuo Handa 	} else if (head->r.print_transition_related_only) {
1402063821c8STetsuo Handa 		return true;
14035db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
14045db5a39bSTetsuo Handa 		struct tomoyo_path2_acl *ptr =
14055db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14060d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14070d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14080d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14090d2171d7STetsuo Handa 				continue;
14100d2171d7STetsuo Handa 			if (first) {
14110d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14120d2171d7STetsuo Handa 				first = false;
14130d2171d7STetsuo Handa 			} else {
14140d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14150d2171d7STetsuo Handa 			}
14160d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14170d2171d7STetsuo Handa 					  [tomoyo_pp2mac[bit]]);
14180d2171d7STetsuo Handa 		}
14190d2171d7STetsuo Handa 		if (first)
14200d2171d7STetsuo Handa 			return true;
1421f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name1);
1422f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name2);
14235db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
14245db5a39bSTetsuo Handa 		struct tomoyo_path_number_acl *ptr =
14255db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14260d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14270d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14280d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14290d2171d7STetsuo Handa 				continue;
14300d2171d7STetsuo Handa 			if (first) {
14310d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14320d2171d7STetsuo Handa 				first = false;
14330d2171d7STetsuo Handa 			} else {
14340d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14350d2171d7STetsuo Handa 			}
14360d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14370d2171d7STetsuo Handa 					  [tomoyo_pn2mac[bit]]);
14380d2171d7STetsuo Handa 		}
14390d2171d7STetsuo Handa 		if (first)
14400d2171d7STetsuo Handa 			return true;
1441f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1442f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->number);
14435db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
14445db5a39bSTetsuo Handa 		struct tomoyo_mkdev_acl *ptr =
14455db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14460d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14470d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14480d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14490d2171d7STetsuo Handa 				continue;
14500d2171d7STetsuo Handa 			if (first) {
14510d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14520d2171d7STetsuo Handa 				first = false;
14530d2171d7STetsuo Handa 			} else {
14540d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14550d2171d7STetsuo Handa 			}
14560d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14570d2171d7STetsuo Handa 					  [tomoyo_pnnn2mac[bit]]);
14580d2171d7STetsuo Handa 		}
14590d2171d7STetsuo Handa 		if (first)
14600d2171d7STetsuo Handa 			return true;
1461f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1462f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->mode);
1463f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->major);
1464f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->minor);
1465059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1466059d84dbSTetsuo Handa 		struct tomoyo_inet_acl *ptr =
1467059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1468059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1469059d84dbSTetsuo Handa 
1470059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1471059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1472059d84dbSTetsuo Handa 				continue;
1473059d84dbSTetsuo Handa 			if (first) {
1474059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network inet ");
1475059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1476059d84dbSTetsuo Handa 						  [ptr->protocol]);
1477059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1478059d84dbSTetsuo Handa 				first = false;
1479059d84dbSTetsuo Handa 			} else {
1480059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1481059d84dbSTetsuo Handa 			}
1482059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1483059d84dbSTetsuo Handa 		}
1484059d84dbSTetsuo Handa 		if (first)
1485059d84dbSTetsuo Handa 			return true;
1486059d84dbSTetsuo Handa 		tomoyo_set_space(head);
1487059d84dbSTetsuo Handa 		if (ptr->address.group) {
1488059d84dbSTetsuo Handa 			tomoyo_set_string(head, "@");
1489059d84dbSTetsuo Handa 			tomoyo_set_string(head, ptr->address.group->group_name
1490059d84dbSTetsuo Handa 					  ->name);
1491059d84dbSTetsuo Handa 		} else {
1492059d84dbSTetsuo Handa 			char buf[128];
1493059d84dbSTetsuo Handa 			tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1494059d84dbSTetsuo Handa 			tomoyo_io_printf(head, "%s", buf);
1495059d84dbSTetsuo Handa 		}
1496059d84dbSTetsuo Handa 		tomoyo_print_number_union(head, &ptr->port);
1497059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1498059d84dbSTetsuo Handa 		struct tomoyo_unix_acl *ptr =
1499059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1500059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1501059d84dbSTetsuo Handa 
1502059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1503059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1504059d84dbSTetsuo Handa 				continue;
1505059d84dbSTetsuo Handa 			if (first) {
1506059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network unix ");
1507059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1508059d84dbSTetsuo Handa 						  [ptr->protocol]);
1509059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1510059d84dbSTetsuo Handa 				first = false;
1511059d84dbSTetsuo Handa 			} else {
1512059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1513059d84dbSTetsuo Handa 			}
1514059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1515059d84dbSTetsuo Handa 		}
1516059d84dbSTetsuo Handa 		if (first)
1517059d84dbSTetsuo Handa 			return true;
1518059d84dbSTetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
15195db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15205db5a39bSTetsuo Handa 		struct tomoyo_mount_acl *ptr =
15215db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
15220d2171d7STetsuo Handa 		tomoyo_set_group(head, "file mount");
1523f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dev_name);
1524f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dir_name);
1525f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->fs_type);
1526f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->flags);
1527d58e0da8STetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1528d58e0da8STetsuo Handa 		struct tomoyo_env_acl *ptr =
1529d58e0da8STetsuo Handa 			container_of(acl, typeof(*ptr), head);
1530d58e0da8STetsuo Handa 
1531d58e0da8STetsuo Handa 		tomoyo_set_group(head, "misc env ");
1532d58e0da8STetsuo Handa 		tomoyo_set_string(head, ptr->env->name);
15339590837bSKentaro Takeda 	}
15342066a361STetsuo Handa 	if (acl->cond) {
15352066a361STetsuo Handa 		head->r.print_cond_part = true;
15362066a361STetsuo Handa 		head->r.cond_step = 0;
15372066a361STetsuo Handa 		if (!tomoyo_flush(head))
15382066a361STetsuo Handa 			return false;
15392066a361STetsuo Handa print_cond_part:
15402066a361STetsuo Handa 		if (!tomoyo_print_condition(head, acl->cond))
15412066a361STetsuo Handa 			return false;
15422066a361STetsuo Handa 		head->r.print_cond_part = false;
15432066a361STetsuo Handa 	} else {
15440d2171d7STetsuo Handa 		tomoyo_set_lf(head);
15452066a361STetsuo Handa 	}
15465db5a39bSTetsuo Handa 	return true;
1547f23571e8STetsuo Handa }
1548f23571e8STetsuo Handa 
1549f23571e8STetsuo Handa /**
1550f23571e8STetsuo Handa  * tomoyo_read_domain2 - Read domain policy.
1551f23571e8STetsuo Handa  *
1552f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
155332997144STetsuo Handa  * @list: Pointer to "struct list_head".
1554f23571e8STetsuo Handa  *
1555f23571e8STetsuo Handa  * Caller holds tomoyo_read_lock().
1556f23571e8STetsuo Handa  *
1557f23571e8STetsuo Handa  * Returns true on success, false otherwise.
1558f23571e8STetsuo Handa  */
1559f23571e8STetsuo Handa static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
156032997144STetsuo Handa 				struct list_head *list)
1561f23571e8STetsuo Handa {
156232997144STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1563f23571e8STetsuo Handa 		struct tomoyo_acl_info *ptr =
1564f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), list);
1565f23571e8STetsuo Handa 		if (!tomoyo_print_entry(head, ptr))
15669590837bSKentaro Takeda 			return false;
15679590837bSKentaro Takeda 	}
1568f23571e8STetsuo Handa 	head->r.acl = NULL;
1569f23571e8STetsuo Handa 	return true;
1570f23571e8STetsuo Handa }
15719590837bSKentaro Takeda 
15729590837bSKentaro Takeda /**
1573e2bf6907STetsuo Handa  * tomoyo_read_domain - Read domain policy.
15749590837bSKentaro Takeda  *
15759590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
15769590837bSKentaro Takeda  *
1577fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
15789590837bSKentaro Takeda  */
1579e2bf6907STetsuo Handa static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
15809590837bSKentaro Takeda {
1581f23571e8STetsuo Handa 	if (head->r.eof)
15828fbe71f0STetsuo Handa 		return;
1583f23571e8STetsuo Handa 	list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1584475e6fa3STetsuo Handa 		struct tomoyo_domain_info *domain =
1585f23571e8STetsuo Handa 			list_entry(head->r.domain, typeof(*domain), list);
1586f23571e8STetsuo Handa 		switch (head->r.step) {
15872c47ab93STetsuo Handa 			u8 i;
1588f23571e8STetsuo Handa 		case 0:
1589f23571e8STetsuo Handa 			if (domain->is_deleted &&
1590f23571e8STetsuo Handa 			    !head->r.print_this_domain_only)
15919590837bSKentaro Takeda 				continue;
15929590837bSKentaro Takeda 			/* Print domainname and flags. */
1593f23571e8STetsuo Handa 			tomoyo_set_string(head, domain->domainname->name);
1594f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1595b5bc60b4STetsuo Handa 			tomoyo_io_printf(head, "use_profile %u\n",
1596f23571e8STetsuo Handa 					 domain->profile);
159732997144STetsuo Handa 			tomoyo_io_printf(head, "use_group %u\n",
159832997144STetsuo Handa 					 domain->group);
15992c47ab93STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
16002c47ab93STetsuo Handa 				if (domain->flags[i])
16012c47ab93STetsuo Handa 					tomoyo_set_string(head, tomoyo_dif[i]);
1602f23571e8STetsuo Handa 			head->r.step++;
1603f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1604f23571e8STetsuo Handa 			/* fall through */
1605f23571e8STetsuo Handa 		case 1:
160632997144STetsuo Handa 			if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1607f23571e8STetsuo Handa 				return;
1608f23571e8STetsuo Handa 			head->r.step++;
1609f23571e8STetsuo Handa 			if (!tomoyo_set_lf(head))
1610f23571e8STetsuo Handa 				return;
1611f23571e8STetsuo Handa 			/* fall through */
1612f23571e8STetsuo Handa 		case 2:
1613f23571e8STetsuo Handa 			head->r.step = 0;
1614f23571e8STetsuo Handa 			if (head->r.print_this_domain_only)
1615f23571e8STetsuo Handa 				goto done;
16169590837bSKentaro Takeda 		}
16179590837bSKentaro Takeda 	}
1618f23571e8STetsuo Handa  done:
1619f23571e8STetsuo Handa 	head->r.eof = true;
16209590837bSKentaro Takeda }
16219590837bSKentaro Takeda 
16229590837bSKentaro Takeda /**
16239590837bSKentaro Takeda  * tomoyo_write_pid: Specify PID to obtain domainname.
16249590837bSKentaro Takeda  *
16259590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16269590837bSKentaro Takeda  *
16279590837bSKentaro Takeda  * Returns 0.
16289590837bSKentaro Takeda  */
16299590837bSKentaro Takeda static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
16309590837bSKentaro Takeda {
1631f23571e8STetsuo Handa 	head->r.eof = false;
16329590837bSKentaro Takeda 	return 0;
16339590837bSKentaro Takeda }
16349590837bSKentaro Takeda 
16359590837bSKentaro Takeda /**
16369590837bSKentaro Takeda  * tomoyo_read_pid - Get domainname of the specified PID.
16379590837bSKentaro Takeda  *
16389590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16399590837bSKentaro Takeda  *
16409590837bSKentaro Takeda  * Returns the domainname which the specified PID is in on success,
16419590837bSKentaro Takeda  * empty string otherwise.
16429590837bSKentaro Takeda  * The PID is specified by tomoyo_write_pid() so that the user can obtain
16439590837bSKentaro Takeda  * using read()/write() interface rather than sysctl() interface.
16449590837bSKentaro Takeda  */
16458fbe71f0STetsuo Handa static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16469590837bSKentaro Takeda {
1647f23571e8STetsuo Handa 	char *buf = head->write_buf;
1648f23571e8STetsuo Handa 	bool global_pid = false;
1649f23571e8STetsuo Handa 	unsigned int pid;
16509590837bSKentaro Takeda 	struct task_struct *p;
16519590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
1652f23571e8STetsuo Handa 
1653f23571e8STetsuo Handa 	/* Accessing write_buf is safe because head->io_sem is held. */
1654f23571e8STetsuo Handa 	if (!buf) {
1655f23571e8STetsuo Handa 		head->r.eof = true;
1656f23571e8STetsuo Handa 		return; /* Do nothing if open(O_RDONLY). */
1657f23571e8STetsuo Handa 	}
1658f23571e8STetsuo Handa 	if (head->r.w_pos || head->r.eof)
1659f23571e8STetsuo Handa 		return;
1660f23571e8STetsuo Handa 	head->r.eof = true;
1661f23571e8STetsuo Handa 	if (tomoyo_str_starts(&buf, "global-pid "))
1662f23571e8STetsuo Handa 		global_pid = true;
1663dbdb75bdSDing Xiang 	if (kstrtouint(buf, 10, &pid))
1664dbdb75bdSDing Xiang 		return;
16651fcdc7c5STetsuo Handa 	rcu_read_lock();
1666f23571e8STetsuo Handa 	if (global_pid)
1667f23571e8STetsuo Handa 		p = find_task_by_pid_ns(pid, &init_pid_ns);
1668f23571e8STetsuo Handa 	else
16699590837bSKentaro Takeda 		p = find_task_by_vpid(pid);
16709590837bSKentaro Takeda 	if (p)
1671*8c6cb983STetsuo Handa 		domain = tomoyo_task(p)->domain_info;
16721fcdc7c5STetsuo Handa 	rcu_read_unlock();
1673f23571e8STetsuo Handa 	if (!domain)
1674f23571e8STetsuo Handa 		return;
1675f23571e8STetsuo Handa 	tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1676f23571e8STetsuo Handa 	tomoyo_set_string(head, domain->domainname->name);
16779590837bSKentaro Takeda }
16789590837bSKentaro Takeda 
16790f2a55d5STetsuo Handa /* String table for domain transition control keywords. */
16805448ec4fSTetsuo Handa static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1681bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
1682bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_RESET]         = "reset_domain ",
1683b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1684b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1685b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1686b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
16875448ec4fSTetsuo Handa };
16885448ec4fSTetsuo Handa 
16890f2a55d5STetsuo Handa /* String table for grouping keywords. */
1690e2bf6907STetsuo Handa static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1691b5bc60b4STetsuo Handa 	[TOMOYO_PATH_GROUP]    = "path_group ",
1692b5bc60b4STetsuo Handa 	[TOMOYO_NUMBER_GROUP]  = "number_group ",
1693059d84dbSTetsuo Handa 	[TOMOYO_ADDRESS_GROUP] = "address_group ",
1694e2bf6907STetsuo Handa };
1695e2bf6907STetsuo Handa 
16969590837bSKentaro Takeda /**
1697e2bf6907STetsuo Handa  * tomoyo_write_exception - Write exception policy.
16989590837bSKentaro Takeda  *
16999590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
17009590837bSKentaro Takeda  *
17019590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1702fdb8ebb7STetsuo Handa  *
1703fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
17049590837bSKentaro Takeda  */
1705e2bf6907STetsuo Handa static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
17069590837bSKentaro Takeda {
1707bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1708a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1709bd03a3e4STetsuo Handa 		.ns = head->w.ns,
1710bd03a3e4STetsuo Handa 		.is_delete = is_delete,
1711a238cf5bSTetsuo Handa 		.data = head->write_buf,
1712e2bf6907STetsuo Handa 	};
1713a238cf5bSTetsuo Handa 	u8 i;
1714a238cf5bSTetsuo Handa 	if (tomoyo_str_starts(&param.data, "aggregator "))
1715a238cf5bSTetsuo Handa 		return tomoyo_write_aggregator(&param);
1716e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1717a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1718a238cf5bSTetsuo Handa 			return tomoyo_write_transition_control(&param, i);
1719e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1720a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1721a238cf5bSTetsuo Handa 			return tomoyo_write_group(&param, i);
172232997144STetsuo Handa 	if (tomoyo_str_starts(&param.data, "acl_group ")) {
172332997144STetsuo Handa 		unsigned int group;
172432997144STetsuo Handa 		char *data;
172532997144STetsuo Handa 		group = simple_strtoul(param.data, &data, 10);
172632997144STetsuo Handa 		if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1727bd03a3e4STetsuo Handa 			return tomoyo_write_domain2
1728bd03a3e4STetsuo Handa 				(head->w.ns, &head->w.ns->acl_group[group],
1729bd03a3e4STetsuo Handa 				 data, is_delete);
173032997144STetsuo Handa 	}
17319590837bSKentaro Takeda 	return -EINVAL;
17329590837bSKentaro Takeda }
17339590837bSKentaro Takeda 
173431845e8cSTetsuo Handa /**
1735059d84dbSTetsuo Handa  * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
173631845e8cSTetsuo Handa  *
173731845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
173831845e8cSTetsuo Handa  * @idx:  Index number.
173931845e8cSTetsuo Handa  *
174031845e8cSTetsuo Handa  * Returns true on success, false otherwise.
174131845e8cSTetsuo Handa  *
174231845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
174331845e8cSTetsuo Handa  */
174431845e8cSTetsuo Handa static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
174531845e8cSTetsuo Handa {
1746bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1747bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1748bd03a3e4STetsuo Handa 	struct list_head *list = &ns->group_list[idx];
1749bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.group, list) {
175031845e8cSTetsuo Handa 		struct tomoyo_group *group =
17510df7e8b8STetsuo Handa 			list_entry(head->r.group, typeof(*group), head.list);
1752f23571e8STetsuo Handa 		list_for_each_cookie(head->r.acl, &group->member_list) {
175331845e8cSTetsuo Handa 			struct tomoyo_acl_head *ptr =
1754f23571e8STetsuo Handa 				list_entry(head->r.acl, typeof(*ptr), list);
175531845e8cSTetsuo Handa 			if (ptr->is_deleted)
175631845e8cSTetsuo Handa 				continue;
1757f23571e8STetsuo Handa 			if (!tomoyo_flush(head))
175831845e8cSTetsuo Handa 				return false;
1759bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
1760f23571e8STetsuo Handa 			tomoyo_set_string(head, tomoyo_group_name[idx]);
1761f23571e8STetsuo Handa 			tomoyo_set_string(head, group->group_name->name);
1762f23571e8STetsuo Handa 			if (idx == TOMOYO_PATH_GROUP) {
1763f23571e8STetsuo Handa 				tomoyo_set_space(head);
1764f23571e8STetsuo Handa 				tomoyo_set_string(head, container_of
1765f23571e8STetsuo Handa 					       (ptr, struct tomoyo_path_group,
1766f23571e8STetsuo Handa 						head)->member_name->name);
1767f23571e8STetsuo Handa 			} else if (idx == TOMOYO_NUMBER_GROUP) {
1768f23571e8STetsuo Handa 				tomoyo_print_number_union(head, &container_of
1769f23571e8STetsuo Handa 							  (ptr,
1770f23571e8STetsuo Handa 						   struct tomoyo_number_group,
1771f23571e8STetsuo Handa 							   head)->number);
1772059d84dbSTetsuo Handa 			} else if (idx == TOMOYO_ADDRESS_GROUP) {
1773059d84dbSTetsuo Handa 				char buffer[128];
1774059d84dbSTetsuo Handa 
1775059d84dbSTetsuo Handa 				struct tomoyo_address_group *member =
1776059d84dbSTetsuo Handa 					container_of(ptr, typeof(*member),
1777059d84dbSTetsuo Handa 						     head);
1778059d84dbSTetsuo Handa 				tomoyo_print_ip(buffer, sizeof(buffer),
1779059d84dbSTetsuo Handa 						&member->address);
1780059d84dbSTetsuo Handa 				tomoyo_io_printf(head, " %s", buffer);
178131845e8cSTetsuo Handa 			}
1782f23571e8STetsuo Handa 			tomoyo_set_lf(head);
178331845e8cSTetsuo Handa 		}
1784f23571e8STetsuo Handa 		head->r.acl = NULL;
1785f23571e8STetsuo Handa 	}
1786f23571e8STetsuo Handa 	head->r.group = NULL;
178731845e8cSTetsuo Handa 	return true;
178831845e8cSTetsuo Handa }
178931845e8cSTetsuo Handa 
179031845e8cSTetsuo Handa /**
179131845e8cSTetsuo Handa  * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
179231845e8cSTetsuo Handa  *
179331845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
179431845e8cSTetsuo Handa  * @idx:  Index number.
179531845e8cSTetsuo Handa  *
179631845e8cSTetsuo Handa  * Returns true on success, false otherwise.
179731845e8cSTetsuo Handa  *
179831845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
179931845e8cSTetsuo Handa  */
180031845e8cSTetsuo Handa static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
180131845e8cSTetsuo Handa {
1802bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1803bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1804bd03a3e4STetsuo Handa 	struct list_head *list = &ns->policy_list[idx];
1805bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1806475e6fa3STetsuo Handa 		struct tomoyo_acl_head *acl =
1807f23571e8STetsuo Handa 			container_of(head->r.acl, typeof(*acl), list);
180831845e8cSTetsuo Handa 		if (acl->is_deleted)
180931845e8cSTetsuo Handa 			continue;
1810f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
1811f23571e8STetsuo Handa 			return false;
181231845e8cSTetsuo Handa 		switch (idx) {
18135448ec4fSTetsuo Handa 		case TOMOYO_ID_TRANSITION_CONTROL:
181431845e8cSTetsuo Handa 			{
18155448ec4fSTetsuo Handa 				struct tomoyo_transition_control *ptr =
181631845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1817bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
18180d2171d7STetsuo Handa 				tomoyo_set_string(head, tomoyo_transition_type
1819f23571e8STetsuo Handa 						  [ptr->type]);
18200d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->program ?
18210d2171d7STetsuo Handa 						  ptr->program->name : "any");
1822f23571e8STetsuo Handa 				tomoyo_set_string(head, " from ");
18230d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->domainname ?
18240d2171d7STetsuo Handa 						  ptr->domainname->name :
18250d2171d7STetsuo Handa 						  "any");
182631845e8cSTetsuo Handa 			}
182731845e8cSTetsuo Handa 			break;
182831845e8cSTetsuo Handa 		case TOMOYO_ID_AGGREGATOR:
182931845e8cSTetsuo Handa 			{
1830e2bf6907STetsuo Handa 				struct tomoyo_aggregator *ptr =
183131845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1832bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
1833b5bc60b4STetsuo Handa 				tomoyo_set_string(head, "aggregator ");
1834f23571e8STetsuo Handa 				tomoyo_set_string(head,
1835f23571e8STetsuo Handa 						  ptr->original_name->name);
1836f23571e8STetsuo Handa 				tomoyo_set_space(head);
1837f23571e8STetsuo Handa 				tomoyo_set_string(head,
1838f23571e8STetsuo Handa 					       ptr->aggregated_name->name);
183931845e8cSTetsuo Handa 			}
184031845e8cSTetsuo Handa 			break;
184131845e8cSTetsuo Handa 		default:
184231845e8cSTetsuo Handa 			continue;
184331845e8cSTetsuo Handa 		}
1844f23571e8STetsuo Handa 		tomoyo_set_lf(head);
184531845e8cSTetsuo Handa 	}
1846f23571e8STetsuo Handa 	head->r.acl = NULL;
184731845e8cSTetsuo Handa 	return true;
184831845e8cSTetsuo Handa }
184931845e8cSTetsuo Handa 
18509590837bSKentaro Takeda /**
1851e2bf6907STetsuo Handa  * tomoyo_read_exception - Read exception policy.
18529590837bSKentaro Takeda  *
18539590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
18549590837bSKentaro Takeda  *
1855fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
18569590837bSKentaro Takeda  */
1857e2bf6907STetsuo Handa static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
18589590837bSKentaro Takeda {
1859bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1860bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1861f23571e8STetsuo Handa 	if (head->r.eof)
186231845e8cSTetsuo Handa 		return;
1863f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY &&
1864f23571e8STetsuo Handa 	       tomoyo_read_policy(head, head->r.step))
1865f23571e8STetsuo Handa 		head->r.step++;
1866f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY)
186731845e8cSTetsuo Handa 		return;
1868f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1869f23571e8STetsuo Handa 	       tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1870f23571e8STetsuo Handa 		head->r.step++;
1871f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
187231845e8cSTetsuo Handa 		return;
187332997144STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
187432997144STetsuo Handa 	       + TOMOYO_MAX_ACL_GROUPS) {
187532997144STetsuo Handa 		head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
187632997144STetsuo Handa 			- TOMOYO_MAX_GROUP;
1877bd03a3e4STetsuo Handa 		if (!tomoyo_read_domain2(head, &ns->acl_group
187832997144STetsuo Handa 					 [head->r.acl_group_index]))
187932997144STetsuo Handa 			return;
188032997144STetsuo Handa 		head->r.step++;
188132997144STetsuo Handa 	}
1882f23571e8STetsuo Handa 	head->r.eof = true;
18839590837bSKentaro Takeda }
18849590837bSKentaro Takeda 
1885eadd99ccSTetsuo Handa /* Wait queue for kernel -> userspace notification. */
188617fcfbd9STetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1887eadd99ccSTetsuo Handa /* Wait queue for userspace -> kernel notification. */
1888eadd99ccSTetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
188917fcfbd9STetsuo Handa 
189017fcfbd9STetsuo Handa /* Structure for query. */
1891e2bf6907STetsuo Handa struct tomoyo_query {
189217fcfbd9STetsuo Handa 	struct list_head list;
189359df3166STetsuo Handa 	struct tomoyo_domain_info *domain;
189417fcfbd9STetsuo Handa 	char *query;
1895eadd99ccSTetsuo Handa 	size_t query_len;
189617fcfbd9STetsuo Handa 	unsigned int serial;
1897eadd99ccSTetsuo Handa 	u8 timer;
1898eadd99ccSTetsuo Handa 	u8 answer;
1899eadd99ccSTetsuo Handa 	u8 retry;
190017fcfbd9STetsuo Handa };
190117fcfbd9STetsuo Handa 
1902e2bf6907STetsuo Handa /* The list for "struct tomoyo_query". */
190317fcfbd9STetsuo Handa static LIST_HEAD(tomoyo_query_list);
190417fcfbd9STetsuo Handa 
1905eadd99ccSTetsuo Handa /* Lock for manipulating tomoyo_query_list. */
1906eadd99ccSTetsuo Handa static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1907eadd99ccSTetsuo Handa 
190817fcfbd9STetsuo Handa /*
190917fcfbd9STetsuo Handa  * Number of "struct file" referring /sys/kernel/security/tomoyo/query
191017fcfbd9STetsuo Handa  * interface.
191117fcfbd9STetsuo Handa  */
191217fcfbd9STetsuo Handa static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
191317fcfbd9STetsuo Handa 
191417fcfbd9STetsuo Handa /**
19152ca9bf45STetsuo Handa  * tomoyo_truncate - Truncate a line.
19162ca9bf45STetsuo Handa  *
19172ca9bf45STetsuo Handa  * @str: String to truncate.
19182ca9bf45STetsuo Handa  *
19192ca9bf45STetsuo Handa  * Returns length of truncated @str.
19202ca9bf45STetsuo Handa  */
19212ca9bf45STetsuo Handa static int tomoyo_truncate(char *str)
19222ca9bf45STetsuo Handa {
19232ca9bf45STetsuo Handa 	char *start = str;
19242ca9bf45STetsuo Handa 	while (*(unsigned char *) str > (unsigned char) ' ')
19252ca9bf45STetsuo Handa 		str++;
19262ca9bf45STetsuo Handa 	*str = '\0';
19272ca9bf45STetsuo Handa 	return strlen(start) + 1;
19282ca9bf45STetsuo Handa }
19292ca9bf45STetsuo Handa 
19302ca9bf45STetsuo Handa /**
1931eadd99ccSTetsuo Handa  * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1932eadd99ccSTetsuo Handa  *
1933eadd99ccSTetsuo Handa  * @domain: Pointer to "struct tomoyo_domain_info".
1934eadd99ccSTetsuo Handa  * @header: Lines containing ACL.
1935eadd99ccSTetsuo Handa  *
1936eadd99ccSTetsuo Handa  * Returns nothing.
1937eadd99ccSTetsuo Handa  */
1938eadd99ccSTetsuo Handa static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1939eadd99ccSTetsuo Handa {
1940eadd99ccSTetsuo Handa 	char *buffer;
19412ca9bf45STetsuo Handa 	char *realpath = NULL;
19425b636857STetsuo Handa 	char *argv0 = NULL;
19432ca9bf45STetsuo Handa 	char *symlink = NULL;
1944eadd99ccSTetsuo Handa 	char *cp = strchr(header, '\n');
1945eadd99ccSTetsuo Handa 	int len;
1946eadd99ccSTetsuo Handa 	if (!cp)
1947eadd99ccSTetsuo Handa 		return;
1948eadd99ccSTetsuo Handa 	cp = strchr(cp + 1, '\n');
1949eadd99ccSTetsuo Handa 	if (!cp)
1950eadd99ccSTetsuo Handa 		return;
1951eadd99ccSTetsuo Handa 	*cp++ = '\0';
1952eadd99ccSTetsuo Handa 	len = strlen(cp) + 1;
19532ca9bf45STetsuo Handa 	/* strstr() will return NULL if ordering is wrong. */
19542ca9bf45STetsuo Handa 	if (*cp == 'f') {
19555b636857STetsuo Handa 		argv0 = strstr(header, " argv[]={ \"");
19565b636857STetsuo Handa 		if (argv0) {
19575b636857STetsuo Handa 			argv0 += 10;
19585b636857STetsuo Handa 			len += tomoyo_truncate(argv0) + 14;
19595b636857STetsuo Handa 		}
19602ca9bf45STetsuo Handa 		realpath = strstr(header, " exec={ realpath=\"");
19612ca9bf45STetsuo Handa 		if (realpath) {
19622ca9bf45STetsuo Handa 			realpath += 8;
19632ca9bf45STetsuo Handa 			len += tomoyo_truncate(realpath) + 6;
19642ca9bf45STetsuo Handa 		}
19652ca9bf45STetsuo Handa 		symlink = strstr(header, " symlink.target=\"");
19662ca9bf45STetsuo Handa 		if (symlink)
19672ca9bf45STetsuo Handa 			len += tomoyo_truncate(symlink + 1) + 1;
19682ca9bf45STetsuo Handa 	}
1969eadd99ccSTetsuo Handa 	buffer = kmalloc(len, GFP_NOFS);
1970eadd99ccSTetsuo Handa 	if (!buffer)
1971eadd99ccSTetsuo Handa 		return;
1972eadd99ccSTetsuo Handa 	snprintf(buffer, len - 1, "%s", cp);
19732ca9bf45STetsuo Handa 	if (realpath)
19742ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.%s", realpath);
19755b636857STetsuo Handa 	if (argv0)
19765b636857STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
19772ca9bf45STetsuo Handa 	if (symlink)
19782ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, "%s", symlink);
1979eadd99ccSTetsuo Handa 	tomoyo_normalize_line(buffer);
1980b22b8b9fSTetsuo Handa 	if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1981b22b8b9fSTetsuo Handa 				  false))
1982b22b8b9fSTetsuo Handa 		tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
1983eadd99ccSTetsuo Handa 	kfree(buffer);
1984eadd99ccSTetsuo Handa }
1985eadd99ccSTetsuo Handa 
1986eadd99ccSTetsuo Handa /**
198717fcfbd9STetsuo Handa  * tomoyo_supervisor - Ask for the supervisor's decision.
198817fcfbd9STetsuo Handa  *
198917fcfbd9STetsuo Handa  * @r:   Pointer to "struct tomoyo_request_info".
199017fcfbd9STetsuo Handa  * @fmt: The printf()'s format string, followed by parameters.
199117fcfbd9STetsuo Handa  *
199217fcfbd9STetsuo Handa  * Returns 0 if the supervisor decided to permit the access request which
199317fcfbd9STetsuo Handa  * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
199417fcfbd9STetsuo Handa  * supervisor decided to retry the access request which violated the policy in
199517fcfbd9STetsuo Handa  * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
199617fcfbd9STetsuo Handa  */
199717fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
199817fcfbd9STetsuo Handa {
199917fcfbd9STetsuo Handa 	va_list args;
2000eadd99ccSTetsuo Handa 	int error;
200117fcfbd9STetsuo Handa 	int len;
200217fcfbd9STetsuo Handa 	static unsigned int tomoyo_serial;
2003eadd99ccSTetsuo Handa 	struct tomoyo_query entry = { };
200417fcfbd9STetsuo Handa 	bool quota_exceeded = false;
2005eadd99ccSTetsuo Handa 	va_start(args, fmt);
2006eadd99ccSTetsuo Handa 	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2007eadd99ccSTetsuo Handa 	va_end(args);
2008eadd99ccSTetsuo Handa 	/* Write /sys/kernel/security/tomoyo/audit. */
2009eadd99ccSTetsuo Handa 	va_start(args, fmt);
2010eadd99ccSTetsuo Handa 	tomoyo_write_log2(r, len, fmt, args);
2011eadd99ccSTetsuo Handa 	va_end(args);
2012eadd99ccSTetsuo Handa 	/* Nothing more to do if granted. */
2013eadd99ccSTetsuo Handa 	if (r->granted)
2014eadd99ccSTetsuo Handa 		return 0;
2015b22b8b9fSTetsuo Handa 	if (r->mode)
2016b22b8b9fSTetsuo Handa 		tomoyo_update_stat(r->mode);
201717fcfbd9STetsuo Handa 	switch (r->mode) {
2018eadd99ccSTetsuo Handa 	case TOMOYO_CONFIG_ENFORCING:
2019eadd99ccSTetsuo Handa 		error = -EPERM;
2020eadd99ccSTetsuo Handa 		if (atomic_read(&tomoyo_query_observers))
2021eadd99ccSTetsuo Handa 			break;
2022eadd99ccSTetsuo Handa 		goto out;
202317fcfbd9STetsuo Handa 	case TOMOYO_CONFIG_LEARNING:
2024eadd99ccSTetsuo Handa 		error = 0;
2025eadd99ccSTetsuo Handa 		/* Check max_learning_entry parameter. */
2026eadd99ccSTetsuo Handa 		if (tomoyo_domain_quota_is_ok(r))
2027eadd99ccSTetsuo Handa 			break;
202817fcfbd9STetsuo Handa 		/* fall through */
2029eadd99ccSTetsuo Handa 	default:
203017fcfbd9STetsuo Handa 		return 0;
203117fcfbd9STetsuo Handa 	}
2032eadd99ccSTetsuo Handa 	/* Get message. */
203317fcfbd9STetsuo Handa 	va_start(args, fmt);
2034eadd99ccSTetsuo Handa 	entry.query = tomoyo_init_log(r, len, fmt, args);
203517fcfbd9STetsuo Handa 	va_end(args);
2036eadd99ccSTetsuo Handa 	if (!entry.query)
203717fcfbd9STetsuo Handa 		goto out;
2038eadd99ccSTetsuo Handa 	entry.query_len = strlen(entry.query) + 1;
2039eadd99ccSTetsuo Handa 	if (!error) {
2040eadd99ccSTetsuo Handa 		tomoyo_add_entry(r->domain, entry.query);
204117fcfbd9STetsuo Handa 		goto out;
2042eadd99ccSTetsuo Handa 	}
2043eadd99ccSTetsuo Handa 	len = tomoyo_round2(entry.query_len);
204459df3166STetsuo Handa 	entry.domain = r->domain;
204517fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2046eadd99ccSTetsuo Handa 	if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2047eadd99ccSTetsuo Handa 	    tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2048eadd99ccSTetsuo Handa 	    >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
204917fcfbd9STetsuo Handa 		quota_exceeded = true;
205017fcfbd9STetsuo Handa 	} else {
2051eadd99ccSTetsuo Handa 		entry.serial = tomoyo_serial++;
2052eadd99ccSTetsuo Handa 		entry.retry = r->retry;
2053eadd99ccSTetsuo Handa 		tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2054eadd99ccSTetsuo Handa 		list_add_tail(&entry.list, &tomoyo_query_list);
205517fcfbd9STetsuo Handa 	}
205617fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
205717fcfbd9STetsuo Handa 	if (quota_exceeded)
205817fcfbd9STetsuo Handa 		goto out;
205917fcfbd9STetsuo Handa 	/* Give 10 seconds for supervisor's opinion. */
2060eadd99ccSTetsuo Handa 	while (entry.timer < 10) {
2061eadd99ccSTetsuo Handa 		wake_up_all(&tomoyo_query_wait);
2062eadd99ccSTetsuo Handa 		if (wait_event_interruptible_timeout
2063eadd99ccSTetsuo Handa 		    (tomoyo_answer_wait, entry.answer ||
2064eadd99ccSTetsuo Handa 		     !atomic_read(&tomoyo_query_observers), HZ))
206517fcfbd9STetsuo Handa 			break;
2066eadd99ccSTetsuo Handa 		else
2067eadd99ccSTetsuo Handa 			entry.timer++;
206817fcfbd9STetsuo Handa 	}
206917fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2070eadd99ccSTetsuo Handa 	list_del(&entry.list);
2071eadd99ccSTetsuo Handa 	tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
207217fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2073eadd99ccSTetsuo Handa 	switch (entry.answer) {
207417fcfbd9STetsuo Handa 	case 3: /* Asked to retry by administrator. */
207517fcfbd9STetsuo Handa 		error = TOMOYO_RETRY_REQUEST;
207617fcfbd9STetsuo Handa 		r->retry++;
207717fcfbd9STetsuo Handa 		break;
207817fcfbd9STetsuo Handa 	case 1:
207917fcfbd9STetsuo Handa 		/* Granted by administrator. */
208017fcfbd9STetsuo Handa 		error = 0;
208117fcfbd9STetsuo Handa 		break;
208217fcfbd9STetsuo Handa 	default:
2083eadd99ccSTetsuo Handa 		/* Timed out or rejected by administrator. */
208417fcfbd9STetsuo Handa 		break;
208517fcfbd9STetsuo Handa 	}
208617fcfbd9STetsuo Handa out:
2087eadd99ccSTetsuo Handa 	kfree(entry.query);
208817fcfbd9STetsuo Handa 	return error;
208917fcfbd9STetsuo Handa }
209017fcfbd9STetsuo Handa 
209117fcfbd9STetsuo Handa /**
209259df3166STetsuo Handa  * tomoyo_find_domain_by_qid - Get domain by query id.
209359df3166STetsuo Handa  *
209459df3166STetsuo Handa  * @serial: Query ID assigned by tomoyo_supervisor().
209559df3166STetsuo Handa  *
209659df3166STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
209759df3166STetsuo Handa  */
209859df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
209959df3166STetsuo Handa (unsigned int serial)
210059df3166STetsuo Handa {
210159df3166STetsuo Handa 	struct tomoyo_query *ptr;
210259df3166STetsuo Handa 	struct tomoyo_domain_info *domain = NULL;
210359df3166STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
210459df3166STetsuo Handa 	list_for_each_entry(ptr, &tomoyo_query_list, list) {
21056041e834STetsuo Handa 		if (ptr->serial != serial)
210659df3166STetsuo Handa 			continue;
210759df3166STetsuo Handa 		domain = ptr->domain;
210859df3166STetsuo Handa 		break;
210959df3166STetsuo Handa 	}
211059df3166STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
211159df3166STetsuo Handa 	return domain;
211259df3166STetsuo Handa }
211359df3166STetsuo Handa 
211459df3166STetsuo Handa /**
211517fcfbd9STetsuo Handa  * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
211617fcfbd9STetsuo Handa  *
211717fcfbd9STetsuo Handa  * @file: Pointer to "struct file".
211817fcfbd9STetsuo Handa  * @wait: Pointer to "poll_table".
211917fcfbd9STetsuo Handa  *
2120a9a08845SLinus Torvalds  * Returns EPOLLIN | EPOLLRDNORM when ready to read, 0 otherwise.
212117fcfbd9STetsuo Handa  *
212217fcfbd9STetsuo Handa  * Waits for access requests which violated policy in enforcing mode.
212317fcfbd9STetsuo Handa  */
2124c0d4be28SAl Viro static __poll_t tomoyo_poll_query(struct file *file, poll_table *wait)
212517fcfbd9STetsuo Handa {
21266041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
2127a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
212817fcfbd9STetsuo Handa 	poll_wait(file, &tomoyo_query_wait, wait);
21296041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
2130a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
213117fcfbd9STetsuo Handa 	return 0;
213217fcfbd9STetsuo Handa }
213317fcfbd9STetsuo Handa 
213417fcfbd9STetsuo Handa /**
213517fcfbd9STetsuo Handa  * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
213617fcfbd9STetsuo Handa  *
213717fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
213817fcfbd9STetsuo Handa  */
21398fbe71f0STetsuo Handa static void tomoyo_read_query(struct tomoyo_io_buffer *head)
214017fcfbd9STetsuo Handa {
214117fcfbd9STetsuo Handa 	struct list_head *tmp;
21422c47ab93STetsuo Handa 	unsigned int pos = 0;
21432c47ab93STetsuo Handa 	size_t len = 0;
214417fcfbd9STetsuo Handa 	char *buf;
2145f23571e8STetsuo Handa 	if (head->r.w_pos)
21468fbe71f0STetsuo Handa 		return;
214717fcfbd9STetsuo Handa 	if (head->read_buf) {
214817fcfbd9STetsuo Handa 		kfree(head->read_buf);
214917fcfbd9STetsuo Handa 		head->read_buf = NULL;
215017fcfbd9STetsuo Handa 	}
215117fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
215217fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2153e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2154f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
215517fcfbd9STetsuo Handa 			continue;
215617fcfbd9STetsuo Handa 		len = ptr->query_len;
215717fcfbd9STetsuo Handa 		break;
215817fcfbd9STetsuo Handa 	}
215917fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
216017fcfbd9STetsuo Handa 	if (!len) {
2161f23571e8STetsuo Handa 		head->r.query_index = 0;
21628fbe71f0STetsuo Handa 		return;
216317fcfbd9STetsuo Handa 	}
2164eadd99ccSTetsuo Handa 	buf = kzalloc(len + 32, GFP_NOFS);
216517fcfbd9STetsuo Handa 	if (!buf)
21668fbe71f0STetsuo Handa 		return;
216717fcfbd9STetsuo Handa 	pos = 0;
216817fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
216917fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2170e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2171f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
217217fcfbd9STetsuo Handa 			continue;
217317fcfbd9STetsuo Handa 		/*
217417fcfbd9STetsuo Handa 		 * Some query can be skipped because tomoyo_query_list
217517fcfbd9STetsuo Handa 		 * can change, but I don't care.
217617fcfbd9STetsuo Handa 		 */
217717fcfbd9STetsuo Handa 		if (len == ptr->query_len)
2178eadd99ccSTetsuo Handa 			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2179eadd99ccSTetsuo Handa 				 ptr->retry, ptr->query);
218017fcfbd9STetsuo Handa 		break;
218117fcfbd9STetsuo Handa 	}
218217fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
218317fcfbd9STetsuo Handa 	if (buf[0]) {
218417fcfbd9STetsuo Handa 		head->read_buf = buf;
2185f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = buf;
2186f23571e8STetsuo Handa 		head->r.query_index++;
218717fcfbd9STetsuo Handa 	} else {
218817fcfbd9STetsuo Handa 		kfree(buf);
218917fcfbd9STetsuo Handa 	}
219017fcfbd9STetsuo Handa }
219117fcfbd9STetsuo Handa 
219217fcfbd9STetsuo Handa /**
219317fcfbd9STetsuo Handa  * tomoyo_write_answer - Write the supervisor's decision.
219417fcfbd9STetsuo Handa  *
219517fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
219617fcfbd9STetsuo Handa  *
219717fcfbd9STetsuo Handa  * Returns 0 on success, -EINVAL otherwise.
219817fcfbd9STetsuo Handa  */
219917fcfbd9STetsuo Handa static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
220017fcfbd9STetsuo Handa {
220117fcfbd9STetsuo Handa 	char *data = head->write_buf;
220217fcfbd9STetsuo Handa 	struct list_head *tmp;
220317fcfbd9STetsuo Handa 	unsigned int serial;
220417fcfbd9STetsuo Handa 	unsigned int answer;
220517fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
220617fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2207e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
220817fcfbd9STetsuo Handa 		ptr->timer = 0;
220917fcfbd9STetsuo Handa 	}
221017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
221117fcfbd9STetsuo Handa 	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
221217fcfbd9STetsuo Handa 		return -EINVAL;
221317fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
221417fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2215e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
221617fcfbd9STetsuo Handa 		if (ptr->serial != serial)
221717fcfbd9STetsuo Handa 			continue;
221817fcfbd9STetsuo Handa 		ptr->answer = answer;
22196041e834STetsuo Handa 		/* Remove from tomoyo_query_list. */
22206041e834STetsuo Handa 		if (ptr->answer)
22216041e834STetsuo Handa 			list_del_init(&ptr->list);
222217fcfbd9STetsuo Handa 		break;
222317fcfbd9STetsuo Handa 	}
222417fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
222517fcfbd9STetsuo Handa 	return 0;
222617fcfbd9STetsuo Handa }
222717fcfbd9STetsuo Handa 
222817fcfbd9STetsuo Handa /**
22299590837bSKentaro Takeda  * tomoyo_read_version: Get version.
22309590837bSKentaro Takeda  *
22319590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
22329590837bSKentaro Takeda  *
22339590837bSKentaro Takeda  * Returns version information.
22349590837bSKentaro Takeda  */
22358fbe71f0STetsuo Handa static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22369590837bSKentaro Takeda {
2237f23571e8STetsuo Handa 	if (!head->r.eof) {
2238843d183cSTetsuo Handa 		tomoyo_io_printf(head, "2.5.0");
2239f23571e8STetsuo Handa 		head->r.eof = true;
22409590837bSKentaro Takeda 	}
22419590837bSKentaro Takeda }
22429590837bSKentaro Takeda 
2243b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2244b22b8b9fSTetsuo Handa static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2245b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_UPDATES]    = "update:",
2246b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2247b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2248b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2249b22b8b9fSTetsuo Handa };
2250b22b8b9fSTetsuo Handa 
2251b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2252b22b8b9fSTetsuo Handa static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2253b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_POLICY] = "policy:",
2254b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_AUDIT]  = "audit log:",
2255b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_QUERY]  = "query message:",
2256b22b8b9fSTetsuo Handa };
2257b22b8b9fSTetsuo Handa 
2258b22b8b9fSTetsuo Handa /* Timestamp counter for last updated. */
2259b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2260b22b8b9fSTetsuo Handa /* Counter for number of updates. */
226192734092SArnd Bergmann static time64_t tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2262b22b8b9fSTetsuo Handa 
2263b22b8b9fSTetsuo Handa /**
2264b22b8b9fSTetsuo Handa  * tomoyo_update_stat - Update statistic counters.
2265b22b8b9fSTetsuo Handa  *
2266b22b8b9fSTetsuo Handa  * @index: Index for policy type.
2267b22b8b9fSTetsuo Handa  *
2268b22b8b9fSTetsuo Handa  * Returns nothing.
2269b22b8b9fSTetsuo Handa  */
2270b22b8b9fSTetsuo Handa void tomoyo_update_stat(const u8 index)
2271b22b8b9fSTetsuo Handa {
2272b22b8b9fSTetsuo Handa 	/*
2273b22b8b9fSTetsuo Handa 	 * I don't use atomic operations because race condition is not fatal.
2274b22b8b9fSTetsuo Handa 	 */
2275b22b8b9fSTetsuo Handa 	tomoyo_stat_updated[index]++;
227692734092SArnd Bergmann 	tomoyo_stat_modified[index] = ktime_get_real_seconds();
2277b22b8b9fSTetsuo Handa }
2278b22b8b9fSTetsuo Handa 
2279b22b8b9fSTetsuo Handa /**
2280b22b8b9fSTetsuo Handa  * tomoyo_read_stat - Read statistic data.
2281b22b8b9fSTetsuo Handa  *
2282b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2283b22b8b9fSTetsuo Handa  *
2284b22b8b9fSTetsuo Handa  * Returns nothing.
2285b22b8b9fSTetsuo Handa  */
2286b22b8b9fSTetsuo Handa static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2287b22b8b9fSTetsuo Handa {
2288b22b8b9fSTetsuo Handa 	u8 i;
2289b22b8b9fSTetsuo Handa 	unsigned int total = 0;
2290b22b8b9fSTetsuo Handa 	if (head->r.eof)
2291b22b8b9fSTetsuo Handa 		return;
2292b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2293b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Policy %-30s %10u",
2294b22b8b9fSTetsuo Handa 				 tomoyo_policy_headers[i],
2295b22b8b9fSTetsuo Handa 				 tomoyo_stat_updated[i]);
2296b22b8b9fSTetsuo Handa 		if (tomoyo_stat_modified[i]) {
2297b22b8b9fSTetsuo Handa 			struct tomoyo_time stamp;
2298b22b8b9fSTetsuo Handa 			tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2299b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2300b22b8b9fSTetsuo Handa 					 "%02u:%02u:%02u)",
2301b22b8b9fSTetsuo Handa 					 stamp.year, stamp.month, stamp.day,
2302b22b8b9fSTetsuo Handa 					 stamp.hour, stamp.min, stamp.sec);
2303b22b8b9fSTetsuo Handa 		}
2304b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2305b22b8b9fSTetsuo Handa 	}
2306b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2307b22b8b9fSTetsuo Handa 		unsigned int used = tomoyo_memory_used[i];
2308b22b8b9fSTetsuo Handa 		total += used;
2309b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Memory used by %-22s %10u",
2310b22b8b9fSTetsuo Handa 				 tomoyo_memory_headers[i], used);
2311b22b8b9fSTetsuo Handa 		used = tomoyo_memory_quota[i];
2312b22b8b9fSTetsuo Handa 		if (used)
2313b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Quota: %10u)", used);
2314b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2315b22b8b9fSTetsuo Handa 	}
2316b22b8b9fSTetsuo Handa 	tomoyo_io_printf(head, "Total memory used:                    %10u\n",
2317b22b8b9fSTetsuo Handa 			 total);
2318b22b8b9fSTetsuo Handa 	head->r.eof = true;
2319b22b8b9fSTetsuo Handa }
2320b22b8b9fSTetsuo Handa 
2321b22b8b9fSTetsuo Handa /**
2322b22b8b9fSTetsuo Handa  * tomoyo_write_stat - Set memory quota.
2323b22b8b9fSTetsuo Handa  *
2324b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2325b22b8b9fSTetsuo Handa  *
2326b22b8b9fSTetsuo Handa  * Returns 0.
2327b22b8b9fSTetsuo Handa  */
2328b22b8b9fSTetsuo Handa static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2329b22b8b9fSTetsuo Handa {
2330b22b8b9fSTetsuo Handa 	char *data = head->write_buf;
2331b22b8b9fSTetsuo Handa 	u8 i;
2332b22b8b9fSTetsuo Handa 	if (tomoyo_str_starts(&data, "Memory used by "))
2333b22b8b9fSTetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2334b22b8b9fSTetsuo Handa 			if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2335b22b8b9fSTetsuo Handa 				sscanf(data, "%u", &tomoyo_memory_quota[i]);
2336b22b8b9fSTetsuo Handa 	return 0;
2337b22b8b9fSTetsuo Handa }
2338b22b8b9fSTetsuo Handa 
23399590837bSKentaro Takeda /**
23409590837bSKentaro Takeda  * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
23419590837bSKentaro Takeda  *
23429590837bSKentaro Takeda  * @type: Type of interface.
23439590837bSKentaro Takeda  * @file: Pointer to "struct file".
23449590837bSKentaro Takeda  *
23452e503bbbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
23469590837bSKentaro Takeda  */
2347c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file)
23489590837bSKentaro Takeda {
23494e5d6f7eSTetsuo Handa 	struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
23509590837bSKentaro Takeda 
23519590837bSKentaro Takeda 	if (!head)
23529590837bSKentaro Takeda 		return -ENOMEM;
23539590837bSKentaro Takeda 	mutex_init(&head->io_sem);
235417fcfbd9STetsuo Handa 	head->type = type;
23559590837bSKentaro Takeda 	switch (type) {
23569590837bSKentaro Takeda 	case TOMOYO_DOMAINPOLICY:
23579590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/domain_policy */
2358e2bf6907STetsuo Handa 		head->write = tomoyo_write_domain;
2359e2bf6907STetsuo Handa 		head->read = tomoyo_read_domain;
23609590837bSKentaro Takeda 		break;
23619590837bSKentaro Takeda 	case TOMOYO_EXCEPTIONPOLICY:
23629590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/exception_policy */
2363e2bf6907STetsuo Handa 		head->write = tomoyo_write_exception;
2364e2bf6907STetsuo Handa 		head->read = tomoyo_read_exception;
23659590837bSKentaro Takeda 		break;
2366eadd99ccSTetsuo Handa 	case TOMOYO_AUDIT:
2367eadd99ccSTetsuo Handa 		/* /sys/kernel/security/tomoyo/audit */
2368eadd99ccSTetsuo Handa 		head->poll = tomoyo_poll_log;
2369eadd99ccSTetsuo Handa 		head->read = tomoyo_read_log;
2370eadd99ccSTetsuo Handa 		break;
23719590837bSKentaro Takeda 	case TOMOYO_PROCESS_STATUS:
23729590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/.process_status */
23739590837bSKentaro Takeda 		head->write = tomoyo_write_pid;
23749590837bSKentaro Takeda 		head->read = tomoyo_read_pid;
23759590837bSKentaro Takeda 		break;
23769590837bSKentaro Takeda 	case TOMOYO_VERSION:
23779590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/version */
23789590837bSKentaro Takeda 		head->read = tomoyo_read_version;
23799590837bSKentaro Takeda 		head->readbuf_size = 128;
23809590837bSKentaro Takeda 		break;
2381b22b8b9fSTetsuo Handa 	case TOMOYO_STAT:
2382b22b8b9fSTetsuo Handa 		/* /sys/kernel/security/tomoyo/stat */
2383b22b8b9fSTetsuo Handa 		head->write = tomoyo_write_stat;
2384b22b8b9fSTetsuo Handa 		head->read = tomoyo_read_stat;
2385b22b8b9fSTetsuo Handa 		head->readbuf_size = 1024;
23869590837bSKentaro Takeda 		break;
23879590837bSKentaro Takeda 	case TOMOYO_PROFILE:
23889590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/profile */
23899590837bSKentaro Takeda 		head->write = tomoyo_write_profile;
23909590837bSKentaro Takeda 		head->read = tomoyo_read_profile;
23919590837bSKentaro Takeda 		break;
239217fcfbd9STetsuo Handa 	case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
239317fcfbd9STetsuo Handa 		head->poll = tomoyo_poll_query;
239417fcfbd9STetsuo Handa 		head->write = tomoyo_write_answer;
239517fcfbd9STetsuo Handa 		head->read = tomoyo_read_query;
239617fcfbd9STetsuo Handa 		break;
23979590837bSKentaro Takeda 	case TOMOYO_MANAGER:
23989590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/manager */
2399e2bf6907STetsuo Handa 		head->write = tomoyo_write_manager;
2400e2bf6907STetsuo Handa 		head->read = tomoyo_read_manager;
24019590837bSKentaro Takeda 		break;
24029590837bSKentaro Takeda 	}
24039590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_READ)) {
24049590837bSKentaro Takeda 		/*
24059590837bSKentaro Takeda 		 * No need to allocate read_buf since it is not opened
24069590837bSKentaro Takeda 		 * for reading.
24079590837bSKentaro Takeda 		 */
24089590837bSKentaro Takeda 		head->read = NULL;
240917fcfbd9STetsuo Handa 		head->poll = NULL;
241017fcfbd9STetsuo Handa 	} else if (!head->poll) {
241117fcfbd9STetsuo Handa 		/* Don't allocate read_buf for poll() access. */
24129590837bSKentaro Takeda 		if (!head->readbuf_size)
24139590837bSKentaro Takeda 			head->readbuf_size = 4096 * 2;
24144e5d6f7eSTetsuo Handa 		head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
24159590837bSKentaro Takeda 		if (!head->read_buf) {
24168e2d39a1STetsuo Handa 			kfree(head);
24179590837bSKentaro Takeda 			return -ENOMEM;
24189590837bSKentaro Takeda 		}
24199590837bSKentaro Takeda 	}
24209590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_WRITE)) {
24219590837bSKentaro Takeda 		/*
24229590837bSKentaro Takeda 		 * No need to allocate write_buf since it is not opened
24239590837bSKentaro Takeda 		 * for writing.
24249590837bSKentaro Takeda 		 */
24259590837bSKentaro Takeda 		head->write = NULL;
24269590837bSKentaro Takeda 	} else if (head->write) {
24279590837bSKentaro Takeda 		head->writebuf_size = 4096 * 2;
24284e5d6f7eSTetsuo Handa 		head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
24299590837bSKentaro Takeda 		if (!head->write_buf) {
24308e2d39a1STetsuo Handa 			kfree(head->read_buf);
24318e2d39a1STetsuo Handa 			kfree(head);
24329590837bSKentaro Takeda 			return -ENOMEM;
24339590837bSKentaro Takeda 		}
24349590837bSKentaro Takeda 	}
24359590837bSKentaro Takeda 	/*
243617fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , increment the
243717fcfbd9STetsuo Handa 	 * observer counter.
243817fcfbd9STetsuo Handa 	 * The obserber counter is used by tomoyo_supervisor() to see if
243917fcfbd9STetsuo Handa 	 * there is some process monitoring /sys/kernel/security/tomoyo/query.
244017fcfbd9STetsuo Handa 	 */
24417c75964fSTetsuo Handa 	if (type == TOMOYO_QUERY)
244217fcfbd9STetsuo Handa 		atomic_inc(&tomoyo_query_observers);
24432e503bbbSTetsuo Handa 	file->private_data = head;
24442e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, true);
24459590837bSKentaro Takeda 	return 0;
24469590837bSKentaro Takeda }
24479590837bSKentaro Takeda 
24489590837bSKentaro Takeda /**
24490849e3baSTetsuo Handa  * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
24500849e3baSTetsuo Handa  *
24510849e3baSTetsuo Handa  * @file: Pointer to "struct file".
24526041e834STetsuo Handa  * @wait: Pointer to "poll_table". Maybe NULL.
24530849e3baSTetsuo Handa  *
2454a9a08845SLinus Torvalds  * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
2455a9a08845SLinus Torvalds  * EPOLLOUT | EPOLLWRNORM otherwise.
24560849e3baSTetsuo Handa  */
2457c0d4be28SAl Viro __poll_t tomoyo_poll_control(struct file *file, poll_table *wait)
24580849e3baSTetsuo Handa {
24590849e3baSTetsuo Handa 	struct tomoyo_io_buffer *head = file->private_data;
24606041e834STetsuo Handa 	if (head->poll)
2461a9a08845SLinus Torvalds 		return head->poll(file, wait) | EPOLLOUT | EPOLLWRNORM;
2462a9a08845SLinus Torvalds 	return EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM;
24630849e3baSTetsuo Handa }
24640849e3baSTetsuo Handa 
24650849e3baSTetsuo Handa /**
2466bd03a3e4STetsuo Handa  * tomoyo_set_namespace_cursor - Set namespace to read.
2467bd03a3e4STetsuo Handa  *
2468bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2469bd03a3e4STetsuo Handa  *
2470bd03a3e4STetsuo Handa  * Returns nothing.
2471bd03a3e4STetsuo Handa  */
2472bd03a3e4STetsuo Handa static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2473bd03a3e4STetsuo Handa {
2474bd03a3e4STetsuo Handa 	struct list_head *ns;
2475bd03a3e4STetsuo Handa 	if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2476bd03a3e4STetsuo Handa 	    head->type != TOMOYO_PROFILE)
2477bd03a3e4STetsuo Handa 		return;
2478bd03a3e4STetsuo Handa 	/*
2479bd03a3e4STetsuo Handa 	 * If this is the first read, or reading previous namespace finished
2480bd03a3e4STetsuo Handa 	 * and has more namespaces to read, update the namespace cursor.
2481bd03a3e4STetsuo Handa 	 */
2482bd03a3e4STetsuo Handa 	ns = head->r.ns;
2483bd03a3e4STetsuo Handa 	if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2484bd03a3e4STetsuo Handa 		/* Clearing is OK because tomoyo_flush() returned true. */
2485bd03a3e4STetsuo Handa 		memset(&head->r, 0, sizeof(head->r));
2486bd03a3e4STetsuo Handa 		head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2487bd03a3e4STetsuo Handa 	}
2488bd03a3e4STetsuo Handa }
2489bd03a3e4STetsuo Handa 
2490bd03a3e4STetsuo Handa /**
2491bd03a3e4STetsuo Handa  * tomoyo_has_more_namespace - Check for unread namespaces.
2492bd03a3e4STetsuo Handa  *
2493bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2494bd03a3e4STetsuo Handa  *
2495bd03a3e4STetsuo Handa  * Returns true if we have more entries to print, false otherwise.
2496bd03a3e4STetsuo Handa  */
2497bd03a3e4STetsuo Handa static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2498bd03a3e4STetsuo Handa {
2499bd03a3e4STetsuo Handa 	return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2500bd03a3e4STetsuo Handa 		head->type == TOMOYO_PROFILE) && head->r.eof &&
2501bd03a3e4STetsuo Handa 		head->r.ns->next != &tomoyo_namespace_list;
2502bd03a3e4STetsuo Handa }
2503bd03a3e4STetsuo Handa 
2504bd03a3e4STetsuo Handa /**
25059590837bSKentaro Takeda  * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
25069590837bSKentaro Takeda  *
25070df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25089590837bSKentaro Takeda  * @buffer:     Poiner to buffer to write to.
25099590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25109590837bSKentaro Takeda  *
25119590837bSKentaro Takeda  * Returns bytes read on success, negative value otherwise.
25129590837bSKentaro Takeda  */
25132c47ab93STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
25149590837bSKentaro Takeda 			    const int buffer_len)
25159590837bSKentaro Takeda {
2516f23571e8STetsuo Handa 	int len;
25172e503bbbSTetsuo Handa 	int idx;
25189590837bSKentaro Takeda 
25199590837bSKentaro Takeda 	if (!head->read)
25209590837bSKentaro Takeda 		return -ENOSYS;
25219590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25229590837bSKentaro Takeda 		return -EINTR;
2523f23571e8STetsuo Handa 	head->read_user_buf = buffer;
2524f23571e8STetsuo Handa 	head->read_user_buf_avail = buffer_len;
25252e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
2526f23571e8STetsuo Handa 	if (tomoyo_flush(head))
25279590837bSKentaro Takeda 		/* Call the policy handler. */
2528bd03a3e4STetsuo Handa 		do {
2529bd03a3e4STetsuo Handa 			tomoyo_set_namespace_cursor(head);
25308fbe71f0STetsuo Handa 			head->read(head);
2531bd03a3e4STetsuo Handa 		} while (tomoyo_flush(head) &&
2532bd03a3e4STetsuo Handa 			 tomoyo_has_more_namespace(head));
25332e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
2534f23571e8STetsuo Handa 	len = head->read_user_buf - buffer;
25359590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
25369590837bSKentaro Takeda 	return len;
25379590837bSKentaro Takeda }
25389590837bSKentaro Takeda 
25399590837bSKentaro Takeda /**
2540bd03a3e4STetsuo Handa  * tomoyo_parse_policy - Parse a policy line.
2541bd03a3e4STetsuo Handa  *
2542bd03a3e4STetsuo Handa  * @head: Poiter to "struct tomoyo_io_buffer".
2543bd03a3e4STetsuo Handa  * @line: Line to parse.
2544bd03a3e4STetsuo Handa  *
2545bd03a3e4STetsuo Handa  * Returns 0 on success, negative value otherwise.
2546bd03a3e4STetsuo Handa  *
2547bd03a3e4STetsuo Handa  * Caller holds tomoyo_read_lock().
2548bd03a3e4STetsuo Handa  */
2549bd03a3e4STetsuo Handa static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2550bd03a3e4STetsuo Handa {
2551bd03a3e4STetsuo Handa 	/* Delete request? */
2552bd03a3e4STetsuo Handa 	head->w.is_delete = !strncmp(line, "delete ", 7);
2553bd03a3e4STetsuo Handa 	if (head->w.is_delete)
2554bd03a3e4STetsuo Handa 		memmove(line, line + 7, strlen(line + 7) + 1);
2555bd03a3e4STetsuo Handa 	/* Selecting namespace to update. */
2556bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2557bd03a3e4STetsuo Handa 	    head->type == TOMOYO_PROFILE) {
2558bd03a3e4STetsuo Handa 		if (*line == '<') {
2559bd03a3e4STetsuo Handa 			char *cp = strchr(line, ' ');
2560bd03a3e4STetsuo Handa 			if (cp) {
2561bd03a3e4STetsuo Handa 				*cp++ = '\0';
2562bd03a3e4STetsuo Handa 				head->w.ns = tomoyo_assign_namespace(line);
2563bd03a3e4STetsuo Handa 				memmove(line, cp, strlen(cp) + 1);
2564bd03a3e4STetsuo Handa 			} else
2565bd03a3e4STetsuo Handa 				head->w.ns = NULL;
2566bd03a3e4STetsuo Handa 		} else
2567bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2568bd03a3e4STetsuo Handa 		/* Don't allow updating if namespace is invalid. */
2569bd03a3e4STetsuo Handa 		if (!head->w.ns)
2570bd03a3e4STetsuo Handa 			return -ENOENT;
2571bd03a3e4STetsuo Handa 	}
2572bd03a3e4STetsuo Handa 	/* Do the update. */
2573bd03a3e4STetsuo Handa 	return head->write(head);
2574bd03a3e4STetsuo Handa }
2575bd03a3e4STetsuo Handa 
2576bd03a3e4STetsuo Handa /**
25779590837bSKentaro Takeda  * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
25789590837bSKentaro Takeda  *
25790df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25809590837bSKentaro Takeda  * @buffer:     Pointer to buffer to read from.
25819590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25829590837bSKentaro Takeda  *
25839590837bSKentaro Takeda  * Returns @buffer_len on success, negative value otherwise.
25849590837bSKentaro Takeda  */
25852c47ab93STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
25860df7e8b8STetsuo Handa 			     const char __user *buffer, const int buffer_len)
25879590837bSKentaro Takeda {
25889590837bSKentaro Takeda 	int error = buffer_len;
2589bd03a3e4STetsuo Handa 	size_t avail_len = buffer_len;
25909590837bSKentaro Takeda 	char *cp0 = head->write_buf;
25912e503bbbSTetsuo Handa 	int idx;
25929590837bSKentaro Takeda 	if (!head->write)
25939590837bSKentaro Takeda 		return -ENOSYS;
259496d4f267SLinus Torvalds 	if (!access_ok(buffer, buffer_len))
25959590837bSKentaro Takeda 		return -EFAULT;
25969590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25979590837bSKentaro Takeda 		return -EINTR;
2598e0b057b4STetsuo Handa 	head->read_user_buf_avail = 0;
25992e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
26009590837bSKentaro Takeda 	/* Read a line and dispatch it to the policy handler. */
26019590837bSKentaro Takeda 	while (avail_len > 0) {
26029590837bSKentaro Takeda 		char c;
26030df7e8b8STetsuo Handa 		if (head->w.avail >= head->writebuf_size - 1) {
2604bd03a3e4STetsuo Handa 			const int len = head->writebuf_size * 2;
2605bd03a3e4STetsuo Handa 			char *cp = kzalloc(len, GFP_NOFS);
2606bd03a3e4STetsuo Handa 			if (!cp) {
26079590837bSKentaro Takeda 				error = -ENOMEM;
26089590837bSKentaro Takeda 				break;
2609bd03a3e4STetsuo Handa 			}
2610bd03a3e4STetsuo Handa 			memmove(cp, cp0, head->w.avail);
2611bd03a3e4STetsuo Handa 			kfree(cp0);
2612bd03a3e4STetsuo Handa 			head->write_buf = cp;
2613bd03a3e4STetsuo Handa 			cp0 = cp;
2614bd03a3e4STetsuo Handa 			head->writebuf_size = len;
2615bd03a3e4STetsuo Handa 		}
2616bd03a3e4STetsuo Handa 		if (get_user(c, buffer)) {
26179590837bSKentaro Takeda 			error = -EFAULT;
26189590837bSKentaro Takeda 			break;
26199590837bSKentaro Takeda 		}
26209590837bSKentaro Takeda 		buffer++;
26219590837bSKentaro Takeda 		avail_len--;
26220df7e8b8STetsuo Handa 		cp0[head->w.avail++] = c;
26239590837bSKentaro Takeda 		if (c != '\n')
26249590837bSKentaro Takeda 			continue;
26250df7e8b8STetsuo Handa 		cp0[head->w.avail - 1] = '\0';
26260df7e8b8STetsuo Handa 		head->w.avail = 0;
26279590837bSKentaro Takeda 		tomoyo_normalize_line(cp0);
2628bd03a3e4STetsuo Handa 		if (!strcmp(cp0, "reset")) {
2629bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2630bd03a3e4STetsuo Handa 			head->w.domain = NULL;
2631bd03a3e4STetsuo Handa 			memset(&head->r, 0, sizeof(head->r));
2632bd03a3e4STetsuo Handa 			continue;
26339590837bSKentaro Takeda 		}
2634bd03a3e4STetsuo Handa 		/* Don't allow updating policies by non manager programs. */
2635bd03a3e4STetsuo Handa 		switch (head->type) {
2636bd03a3e4STetsuo Handa 		case TOMOYO_PROCESS_STATUS:
2637bd03a3e4STetsuo Handa 			/* This does not write anything. */
2638bd03a3e4STetsuo Handa 			break;
2639bd03a3e4STetsuo Handa 		case TOMOYO_DOMAINPOLICY:
2640bd03a3e4STetsuo Handa 			if (tomoyo_select_domain(head, cp0))
2641bd03a3e4STetsuo Handa 				continue;
2642bd03a3e4STetsuo Handa 			/* fall through */
2643bd03a3e4STetsuo Handa 		case TOMOYO_EXCEPTIONPOLICY:
2644bd03a3e4STetsuo Handa 			if (!strcmp(cp0, "select transition_only")) {
2645bd03a3e4STetsuo Handa 				head->r.print_transition_related_only = true;
2646bd03a3e4STetsuo Handa 				continue;
2647bd03a3e4STetsuo Handa 			}
2648bd03a3e4STetsuo Handa 			/* fall through */
2649bd03a3e4STetsuo Handa 		default:
2650bd03a3e4STetsuo Handa 			if (!tomoyo_manager()) {
2651bd03a3e4STetsuo Handa 				error = -EPERM;
2652bd03a3e4STetsuo Handa 				goto out;
2653bd03a3e4STetsuo Handa 			}
2654bd03a3e4STetsuo Handa 		}
2655bd03a3e4STetsuo Handa 		switch (tomoyo_parse_policy(head, cp0)) {
2656bd03a3e4STetsuo Handa 		case -EPERM:
2657bd03a3e4STetsuo Handa 			error = -EPERM;
2658bd03a3e4STetsuo Handa 			goto out;
2659b22b8b9fSTetsuo Handa 		case 0:
2660b22b8b9fSTetsuo Handa 			switch (head->type) {
2661b22b8b9fSTetsuo Handa 			case TOMOYO_DOMAINPOLICY:
2662b22b8b9fSTetsuo Handa 			case TOMOYO_EXCEPTIONPOLICY:
2663b22b8b9fSTetsuo Handa 			case TOMOYO_STAT:
2664b22b8b9fSTetsuo Handa 			case TOMOYO_PROFILE:
2665b22b8b9fSTetsuo Handa 			case TOMOYO_MANAGER:
2666b22b8b9fSTetsuo Handa 				tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2667b22b8b9fSTetsuo Handa 				break;
2668b22b8b9fSTetsuo Handa 			default:
2669b22b8b9fSTetsuo Handa 				break;
2670b22b8b9fSTetsuo Handa 			}
2671b22b8b9fSTetsuo Handa 			break;
2672bd03a3e4STetsuo Handa 		}
2673bd03a3e4STetsuo Handa 	}
2674bd03a3e4STetsuo Handa out:
26752e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
26769590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
26779590837bSKentaro Takeda 	return error;
26789590837bSKentaro Takeda }
26799590837bSKentaro Takeda 
26809590837bSKentaro Takeda /**
26819590837bSKentaro Takeda  * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
26829590837bSKentaro Takeda  *
26830df7e8b8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
26849590837bSKentaro Takeda  */
2685e53cfda5SAl Viro void tomoyo_close_control(struct tomoyo_io_buffer *head)
26869590837bSKentaro Takeda {
268717fcfbd9STetsuo Handa 	/*
268817fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , decrement the
268917fcfbd9STetsuo Handa 	 * observer counter.
269017fcfbd9STetsuo Handa 	 */
26912e503bbbSTetsuo Handa 	if (head->type == TOMOYO_QUERY &&
26922e503bbbSTetsuo Handa 	    atomic_dec_and_test(&tomoyo_query_observers))
26932e503bbbSTetsuo Handa 		wake_up_all(&tomoyo_answer_wait);
26942e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, false);
26959590837bSKentaro Takeda }
26969590837bSKentaro Takeda 
26979590837bSKentaro Takeda /**
2698c3ef1500STetsuo Handa  * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
26999590837bSKentaro Takeda  */
2700c3ef1500STetsuo Handa void tomoyo_check_profile(void)
27019590837bSKentaro Takeda {
2702c3ef1500STetsuo Handa 	struct tomoyo_domain_info *domain;
2703c3ef1500STetsuo Handa 	const int idx = tomoyo_read_lock();
2704c3ef1500STetsuo Handa 	tomoyo_policy_loaded = true;
2705843d183cSTetsuo Handa 	printk(KERN_INFO "TOMOYO: 2.5.0\n");
2706c3ef1500STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2707c3ef1500STetsuo Handa 		const u8 profile = domain->profile;
2708bd03a3e4STetsuo Handa 		const struct tomoyo_policy_namespace *ns = domain->ns;
2709843d183cSTetsuo Handa 		if (ns->profile_version != 20110903)
2710bd03a3e4STetsuo Handa 			printk(KERN_ERR
2711bd03a3e4STetsuo Handa 			       "Profile version %u is not supported.\n",
2712bd03a3e4STetsuo Handa 			       ns->profile_version);
2713bd03a3e4STetsuo Handa 		else if (!ns->profile_ptr[profile])
2714bd03a3e4STetsuo Handa 			printk(KERN_ERR
2715bd03a3e4STetsuo Handa 			       "Profile %u (used by '%s') is not defined.\n",
2716c3ef1500STetsuo Handa 			       profile, domain->domainname->name);
2717bd03a3e4STetsuo Handa 		else
2718bd03a3e4STetsuo Handa 			continue;
2719bd03a3e4STetsuo Handa 		printk(KERN_ERR
2720843d183cSTetsuo Handa 		       "Userland tools for TOMOYO 2.5 must be installed and "
2721bd03a3e4STetsuo Handa 		       "policy must be initialized.\n");
2722843d183cSTetsuo Handa 		printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
2723bd03a3e4STetsuo Handa 		       "for more information.\n");
2724bd03a3e4STetsuo Handa 		panic("STOP!");
27259590837bSKentaro Takeda 	}
2726c3ef1500STetsuo Handa 	tomoyo_read_unlock(idx);
2727c3ef1500STetsuo Handa 	printk(KERN_INFO "Mandatory Access Control activated.\n");
27289590837bSKentaro Takeda }
2729efe836abSTetsuo Handa 
2730efe836abSTetsuo Handa /**
2731efe836abSTetsuo Handa  * tomoyo_load_builtin_policy - Load built-in policy.
2732efe836abSTetsuo Handa  *
2733efe836abSTetsuo Handa  * Returns nothing.
2734efe836abSTetsuo Handa  */
2735efe836abSTetsuo Handa void __init tomoyo_load_builtin_policy(void)
2736efe836abSTetsuo Handa {
2737efe836abSTetsuo Handa 	/*
2738efe836abSTetsuo Handa 	 * This include file is manually created and contains built-in policy
2739efe836abSTetsuo Handa 	 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2740efe836abSTetsuo Handa 	 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2741efe836abSTetsuo Handa 	 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2742efe836abSTetsuo Handa 	 */
2743efe836abSTetsuo Handa #include "builtin-policy.h"
2744efe836abSTetsuo Handa 	u8 i;
2745efe836abSTetsuo Handa 	const int idx = tomoyo_read_lock();
2746efe836abSTetsuo Handa 	for (i = 0; i < 5; i++) {
2747efe836abSTetsuo Handa 		struct tomoyo_io_buffer head = { };
2748efe836abSTetsuo Handa 		char *start = "";
2749efe836abSTetsuo Handa 		switch (i) {
2750efe836abSTetsuo Handa 		case 0:
2751efe836abSTetsuo Handa 			start = tomoyo_builtin_profile;
2752efe836abSTetsuo Handa 			head.type = TOMOYO_PROFILE;
2753efe836abSTetsuo Handa 			head.write = tomoyo_write_profile;
2754efe836abSTetsuo Handa 			break;
2755efe836abSTetsuo Handa 		case 1:
2756efe836abSTetsuo Handa 			start = tomoyo_builtin_exception_policy;
2757efe836abSTetsuo Handa 			head.type = TOMOYO_EXCEPTIONPOLICY;
2758efe836abSTetsuo Handa 			head.write = tomoyo_write_exception;
2759efe836abSTetsuo Handa 			break;
2760efe836abSTetsuo Handa 		case 2:
2761efe836abSTetsuo Handa 			start = tomoyo_builtin_domain_policy;
2762efe836abSTetsuo Handa 			head.type = TOMOYO_DOMAINPOLICY;
2763efe836abSTetsuo Handa 			head.write = tomoyo_write_domain;
2764efe836abSTetsuo Handa 			break;
2765efe836abSTetsuo Handa 		case 3:
2766efe836abSTetsuo Handa 			start = tomoyo_builtin_manager;
2767efe836abSTetsuo Handa 			head.type = TOMOYO_MANAGER;
2768efe836abSTetsuo Handa 			head.write = tomoyo_write_manager;
2769efe836abSTetsuo Handa 			break;
2770efe836abSTetsuo Handa 		case 4:
2771efe836abSTetsuo Handa 			start = tomoyo_builtin_stat;
2772efe836abSTetsuo Handa 			head.type = TOMOYO_STAT;
2773efe836abSTetsuo Handa 			head.write = tomoyo_write_stat;
2774efe836abSTetsuo Handa 			break;
2775efe836abSTetsuo Handa 		}
2776efe836abSTetsuo Handa 		while (1) {
2777efe836abSTetsuo Handa 			char *end = strchr(start, '\n');
2778efe836abSTetsuo Handa 			if (!end)
2779efe836abSTetsuo Handa 				break;
2780efe836abSTetsuo Handa 			*end = '\0';
2781efe836abSTetsuo Handa 			tomoyo_normalize_line(start);
2782efe836abSTetsuo Handa 			head.write_buf = start;
2783efe836abSTetsuo Handa 			tomoyo_parse_policy(&head, start);
2784efe836abSTetsuo Handa 			start = end + 1;
2785efe836abSTetsuo Handa 		}
2786efe836abSTetsuo Handa 	}
2787efe836abSTetsuo Handa 	tomoyo_read_unlock(idx);
27880e4ae0e0STetsuo Handa #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
27890e4ae0e0STetsuo Handa 	tomoyo_check_profile();
27900e4ae0e0STetsuo Handa #endif
2791efe836abSTetsuo Handa }
2792