xref: /linux/security/tomoyo/common.c (revision cdcf6723add57a0ffb37cfde1ca54a00f5715b71)
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);
200*cdcf6723STetsuo Handa 
201f23571e8STetsuo Handa 	va_start(args, fmt);
202f23571e8STetsuo Handa 	vsnprintf(buffer + pos, len - pos - 1, fmt, args);
203f23571e8STetsuo Handa 	va_end(args);
204f23571e8STetsuo Handa }
205f23571e8STetsuo Handa 
206f23571e8STetsuo Handa /**
207f23571e8STetsuo Handa  * tomoyo_flush - Flush queued string to userspace's buffer.
208f23571e8STetsuo Handa  *
209f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
210f23571e8STetsuo Handa  *
211f23571e8STetsuo Handa  * Returns true if all data was flushed, false otherwise.
212f23571e8STetsuo Handa  */
213f23571e8STetsuo Handa static bool tomoyo_flush(struct tomoyo_io_buffer *head)
214f23571e8STetsuo Handa {
215f23571e8STetsuo Handa 	while (head->r.w_pos) {
216f23571e8STetsuo Handa 		const char *w = head->r.w[0];
2172c47ab93STetsuo Handa 		size_t len = strlen(w);
218*cdcf6723STetsuo Handa 
219f23571e8STetsuo Handa 		if (len) {
220f23571e8STetsuo Handa 			if (len > head->read_user_buf_avail)
221f23571e8STetsuo Handa 				len = head->read_user_buf_avail;
222f23571e8STetsuo Handa 			if (!len)
223f23571e8STetsuo Handa 				return false;
224f23571e8STetsuo Handa 			if (copy_to_user(head->read_user_buf, w, len))
225f23571e8STetsuo Handa 				return false;
226f23571e8STetsuo Handa 			head->read_user_buf_avail -= len;
227f23571e8STetsuo Handa 			head->read_user_buf += len;
228f23571e8STetsuo Handa 			w += len;
229f23571e8STetsuo Handa 		}
230f23571e8STetsuo Handa 		head->r.w[0] = w;
231c0fa797aSTetsuo Handa 		if (*w)
232f23571e8STetsuo Handa 			return false;
233eadd99ccSTetsuo Handa 		/* Add '\0' for audit logs and query. */
234f23571e8STetsuo Handa 		if (head->poll) {
235f23571e8STetsuo Handa 			if (!head->read_user_buf_avail ||
236f23571e8STetsuo Handa 			    copy_to_user(head->read_user_buf, "", 1))
237f23571e8STetsuo Handa 				return false;
238f23571e8STetsuo Handa 			head->read_user_buf_avail--;
239f23571e8STetsuo Handa 			head->read_user_buf++;
240f23571e8STetsuo Handa 		}
241f23571e8STetsuo Handa 		head->r.w_pos--;
242f23571e8STetsuo Handa 		for (len = 0; len < head->r.w_pos; len++)
243f23571e8STetsuo Handa 			head->r.w[len] = head->r.w[len + 1];
244f23571e8STetsuo Handa 	}
245f23571e8STetsuo Handa 	head->r.avail = 0;
246f23571e8STetsuo Handa 	return true;
247f23571e8STetsuo Handa }
248f23571e8STetsuo Handa 
249f23571e8STetsuo Handa /**
250f23571e8STetsuo Handa  * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
251f23571e8STetsuo Handa  *
252f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
253f23571e8STetsuo Handa  * @string: String to print.
254f23571e8STetsuo Handa  *
255f23571e8STetsuo Handa  * Note that @string has to be kept valid until @head is kfree()d.
256f23571e8STetsuo Handa  * This means that char[] allocated on stack memory cannot be passed to
257f23571e8STetsuo Handa  * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
258f23571e8STetsuo Handa  */
259f23571e8STetsuo Handa static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
260f23571e8STetsuo Handa {
261f23571e8STetsuo Handa 	if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
262f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = string;
263f23571e8STetsuo Handa 		tomoyo_flush(head);
264f23571e8STetsuo Handa 	} else
265f23571e8STetsuo Handa 		WARN_ON(1);
266f23571e8STetsuo Handa }
267f23571e8STetsuo Handa 
268778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
269778c4a4dSTetsuo Handa 			     ...) __printf(2, 3);
270778c4a4dSTetsuo Handa 
271f23571e8STetsuo Handa /**
272f23571e8STetsuo Handa  * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
273f23571e8STetsuo Handa  *
274f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
275f23571e8STetsuo Handa  * @fmt:  The printf()'s format string, followed by parameters.
276f23571e8STetsuo Handa  */
277778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
278778c4a4dSTetsuo Handa 			     ...)
279f23571e8STetsuo Handa {
280f23571e8STetsuo Handa 	va_list args;
2812c47ab93STetsuo Handa 	size_t len;
2822c47ab93STetsuo Handa 	size_t pos = head->r.avail;
283f23571e8STetsuo Handa 	int size = head->readbuf_size - pos;
284*cdcf6723STetsuo Handa 
285f23571e8STetsuo Handa 	if (size <= 0)
286f23571e8STetsuo Handa 		return;
287f23571e8STetsuo Handa 	va_start(args, fmt);
288f23571e8STetsuo Handa 	len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
289f23571e8STetsuo Handa 	va_end(args);
290f23571e8STetsuo Handa 	if (pos + len >= head->readbuf_size) {
291f23571e8STetsuo Handa 		WARN_ON(1);
292f23571e8STetsuo Handa 		return;
293f23571e8STetsuo Handa 	}
294f23571e8STetsuo Handa 	head->r.avail += len;
295f23571e8STetsuo Handa 	tomoyo_set_string(head, head->read_buf + pos);
296f23571e8STetsuo Handa }
297f23571e8STetsuo Handa 
2980d2171d7STetsuo Handa /**
2990d2171d7STetsuo Handa  * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
3000d2171d7STetsuo Handa  *
3010d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3020d2171d7STetsuo Handa  *
3030d2171d7STetsuo Handa  * Returns nothing.
3040d2171d7STetsuo Handa  */
305f23571e8STetsuo Handa static void tomoyo_set_space(struct tomoyo_io_buffer *head)
306f23571e8STetsuo Handa {
307f23571e8STetsuo Handa 	tomoyo_set_string(head, " ");
308f23571e8STetsuo Handa }
309f23571e8STetsuo Handa 
3100d2171d7STetsuo Handa /**
3110d2171d7STetsuo Handa  * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
3120d2171d7STetsuo Handa  *
3130d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3140d2171d7STetsuo Handa  *
3150d2171d7STetsuo Handa  * Returns nothing.
3160d2171d7STetsuo Handa  */
317f23571e8STetsuo Handa static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
318f23571e8STetsuo Handa {
319f23571e8STetsuo Handa 	tomoyo_set_string(head, "\n");
320f23571e8STetsuo Handa 	return !head->r.w_pos;
321f23571e8STetsuo Handa }
322f23571e8STetsuo Handa 
32357c2590fSTetsuo Handa /**
3240d2171d7STetsuo Handa  * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
3250d2171d7STetsuo Handa  *
3260d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3270d2171d7STetsuo Handa  *
3280d2171d7STetsuo Handa  * Returns nothing.
3290d2171d7STetsuo Handa  */
3300d2171d7STetsuo Handa static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
3310d2171d7STetsuo Handa {
3320d2171d7STetsuo Handa 	tomoyo_set_string(head, "/");
3330d2171d7STetsuo Handa }
3340d2171d7STetsuo Handa 
335bd03a3e4STetsuo Handa /* List of namespaces. */
336bd03a3e4STetsuo Handa LIST_HEAD(tomoyo_namespace_list);
337bd03a3e4STetsuo Handa /* True if namespace other than tomoyo_kernel_namespace is defined. */
338bd03a3e4STetsuo Handa static bool tomoyo_namespace_enabled;
339bd03a3e4STetsuo Handa 
340bd03a3e4STetsuo Handa /**
341bd03a3e4STetsuo Handa  * tomoyo_init_policy_namespace - Initialize namespace.
342bd03a3e4STetsuo Handa  *
343bd03a3e4STetsuo Handa  * @ns: Pointer to "struct tomoyo_policy_namespace".
344bd03a3e4STetsuo Handa  *
345bd03a3e4STetsuo Handa  * Returns nothing.
346bd03a3e4STetsuo Handa  */
347bd03a3e4STetsuo Handa void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
348bd03a3e4STetsuo Handa {
349bd03a3e4STetsuo Handa 	unsigned int idx;
350*cdcf6723STetsuo Handa 
351bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
352bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->acl_group[idx]);
353bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
354bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->group_list[idx]);
355bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
356bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->policy_list[idx]);
357843d183cSTetsuo Handa 	ns->profile_version = 20110903;
358bd03a3e4STetsuo Handa 	tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
359bd03a3e4STetsuo Handa 	list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
360bd03a3e4STetsuo Handa }
361bd03a3e4STetsuo Handa 
362bd03a3e4STetsuo Handa /**
363bd03a3e4STetsuo Handa  * tomoyo_print_namespace - Print namespace header.
364bd03a3e4STetsuo Handa  *
365bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
366bd03a3e4STetsuo Handa  *
367bd03a3e4STetsuo Handa  * Returns nothing.
368bd03a3e4STetsuo Handa  */
369bd03a3e4STetsuo Handa static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
370bd03a3e4STetsuo Handa {
371bd03a3e4STetsuo Handa 	if (!tomoyo_namespace_enabled)
372bd03a3e4STetsuo Handa 		return;
373bd03a3e4STetsuo Handa 	tomoyo_set_string(head,
374bd03a3e4STetsuo Handa 			  container_of(head->r.ns,
375bd03a3e4STetsuo Handa 				       struct tomoyo_policy_namespace,
376bd03a3e4STetsuo Handa 				       namespace_list)->name);
377bd03a3e4STetsuo Handa 	tomoyo_set_space(head);
378bd03a3e4STetsuo Handa }
379bd03a3e4STetsuo Handa 
3800d2171d7STetsuo Handa /**
3817762fbffSTetsuo Handa  * tomoyo_print_name_union - Print a tomoyo_name_union.
3827762fbffSTetsuo Handa  *
3837762fbffSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3847762fbffSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3857762fbffSTetsuo Handa  */
386f23571e8STetsuo Handa static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
3877762fbffSTetsuo Handa 				    const struct tomoyo_name_union *ptr)
3887762fbffSTetsuo Handa {
389f23571e8STetsuo Handa 	tomoyo_set_space(head);
3900df7e8b8STetsuo Handa 	if (ptr->group) {
391f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
392f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
393f23571e8STetsuo Handa 	} else {
394f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
395f23571e8STetsuo Handa 	}
3967762fbffSTetsuo Handa }
3977762fbffSTetsuo Handa 
3987762fbffSTetsuo Handa /**
3992ca9bf45STetsuo Handa  * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
4002ca9bf45STetsuo Handa  *
4012ca9bf45STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4022ca9bf45STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
4032ca9bf45STetsuo Handa  *
4042ca9bf45STetsuo Handa  * Returns nothing.
4052ca9bf45STetsuo Handa  */
4062ca9bf45STetsuo Handa static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
4072ca9bf45STetsuo Handa 					   const struct tomoyo_name_union *ptr)
4082ca9bf45STetsuo Handa {
4092ca9bf45STetsuo Handa 	if (ptr->group) {
4102ca9bf45STetsuo Handa 		tomoyo_set_string(head, "@");
4112ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
4122ca9bf45STetsuo Handa 	} else {
4132ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4142ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
4152ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4162ca9bf45STetsuo Handa 	}
4172ca9bf45STetsuo Handa }
4182ca9bf45STetsuo Handa 
4192ca9bf45STetsuo Handa /**
4202066a361STetsuo Handa  * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
4214c3e9e2dSTetsuo Handa  *
4224c3e9e2dSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4234c3e9e2dSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4242066a361STetsuo Handa  *
4252066a361STetsuo Handa  * Returns nothing.
4264c3e9e2dSTetsuo Handa  */
4272066a361STetsuo Handa static void tomoyo_print_number_union_nospace
4282066a361STetsuo Handa (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
4294c3e9e2dSTetsuo Handa {
4300df7e8b8STetsuo Handa 	if (ptr->group) {
431f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
432f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
433f23571e8STetsuo Handa 	} else {
434f23571e8STetsuo Handa 		int i;
435f23571e8STetsuo Handa 		unsigned long min = ptr->values[0];
436f23571e8STetsuo Handa 		const unsigned long max = ptr->values[1];
4370df7e8b8STetsuo Handa 		u8 min_type = ptr->value_type[0];
4380df7e8b8STetsuo Handa 		const u8 max_type = ptr->value_type[1];
439f23571e8STetsuo Handa 		char buffer[128];
440*cdcf6723STetsuo Handa 
441f23571e8STetsuo Handa 		buffer[0] = '\0';
442f23571e8STetsuo Handa 		for (i = 0; i < 2; i++) {
4434c3e9e2dSTetsuo Handa 			switch (min_type) {
4444c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_HEXADECIMAL:
445f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
446f23571e8STetsuo Handa 						 "0x%lX", min);
4474c3e9e2dSTetsuo Handa 				break;
4484c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_OCTAL:
449f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
450f23571e8STetsuo Handa 						 "0%lo", min);
4514c3e9e2dSTetsuo Handa 				break;
4524c3e9e2dSTetsuo Handa 			default:
4532066a361STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
4542066a361STetsuo Handa 						 min);
4554c3e9e2dSTetsuo Handa 				break;
4564c3e9e2dSTetsuo Handa 			}
4574c3e9e2dSTetsuo Handa 			if (min == max && min_type == max_type)
458f23571e8STetsuo Handa 				break;
459f23571e8STetsuo Handa 			tomoyo_addprintf(buffer, sizeof(buffer), "-");
460f23571e8STetsuo Handa 			min_type = max_type;
461f23571e8STetsuo Handa 			min = max;
4624c3e9e2dSTetsuo Handa 		}
463f23571e8STetsuo Handa 		tomoyo_io_printf(head, "%s", buffer);
4644c3e9e2dSTetsuo Handa 	}
4659590837bSKentaro Takeda }
4669590837bSKentaro Takeda 
4679590837bSKentaro Takeda /**
4682066a361STetsuo Handa  * tomoyo_print_number_union - Print a tomoyo_number_union.
4692066a361STetsuo Handa  *
4702066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4712066a361STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4722066a361STetsuo Handa  *
4732066a361STetsuo Handa  * Returns nothing.
4742066a361STetsuo Handa  */
4752066a361STetsuo Handa static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
4762066a361STetsuo Handa 				      const struct tomoyo_number_union *ptr)
4772066a361STetsuo Handa {
4782066a361STetsuo Handa 	tomoyo_set_space(head);
4792066a361STetsuo Handa 	tomoyo_print_number_union_nospace(head, ptr);
4802066a361STetsuo Handa }
4812066a361STetsuo Handa 
4822066a361STetsuo Handa /**
483e2bf6907STetsuo Handa  * tomoyo_assign_profile - Create a new profile.
4849590837bSKentaro Takeda  *
485bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
4869590837bSKentaro Takeda  * @profile: Profile number to create.
4879590837bSKentaro Takeda  *
4889590837bSKentaro Takeda  * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
4899590837bSKentaro Takeda  */
490bd03a3e4STetsuo Handa static struct tomoyo_profile *tomoyo_assign_profile
491bd03a3e4STetsuo Handa (struct tomoyo_policy_namespace *ns, const unsigned int profile)
4929590837bSKentaro Takeda {
49357c2590fSTetsuo Handa 	struct tomoyo_profile *ptr;
49457c2590fSTetsuo Handa 	struct tomoyo_profile *entry;
495*cdcf6723STetsuo Handa 
4969590837bSKentaro Takeda 	if (profile >= TOMOYO_MAX_PROFILES)
4979590837bSKentaro Takeda 		return NULL;
498bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
4999590837bSKentaro Takeda 	if (ptr)
50057c2590fSTetsuo Handa 		return ptr;
50157c2590fSTetsuo Handa 	entry = kzalloc(sizeof(*entry), GFP_NOFS);
50257c2590fSTetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
50357c2590fSTetsuo Handa 		goto out;
504bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
50557c2590fSTetsuo Handa 	if (!ptr && tomoyo_memory_ok(entry)) {
50657c2590fSTetsuo Handa 		ptr = entry;
507eadd99ccSTetsuo Handa 		ptr->default_config = TOMOYO_CONFIG_DISABLED |
508eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_GRANT_LOG |
509eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_REJECT_LOG;
51057c2590fSTetsuo Handa 		memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
51157c2590fSTetsuo Handa 		       sizeof(ptr->config));
5126afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] =
5136afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_AUDIT_LOG;
5146afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] =
5156afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_ACCEPT_ENTRY;
5169590837bSKentaro Takeda 		mb(); /* Avoid out-of-order execution. */
517bd03a3e4STetsuo Handa 		ns->profile_ptr[profile] = ptr;
51857c2590fSTetsuo Handa 		entry = NULL;
51957c2590fSTetsuo Handa 	}
52029282381STetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
52157c2590fSTetsuo Handa  out:
52257c2590fSTetsuo Handa 	kfree(entry);
5239590837bSKentaro Takeda 	return ptr;
5249590837bSKentaro Takeda }
5259590837bSKentaro Takeda 
5269590837bSKentaro Takeda /**
52757c2590fSTetsuo Handa  * tomoyo_profile - Find a profile.
52857c2590fSTetsuo Handa  *
529bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
53057c2590fSTetsuo Handa  * @profile: Profile number to find.
53157c2590fSTetsuo Handa  *
53257c2590fSTetsuo Handa  * Returns pointer to "struct tomoyo_profile".
53357c2590fSTetsuo Handa  */
534bd03a3e4STetsuo Handa struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
535bd03a3e4STetsuo Handa 				      const u8 profile)
53657c2590fSTetsuo Handa {
537d5ca1725STetsuo Handa 	static struct tomoyo_profile tomoyo_null_profile;
538bd03a3e4STetsuo Handa 	struct tomoyo_profile *ptr = ns->profile_ptr[profile];
539*cdcf6723STetsuo Handa 
540d5ca1725STetsuo Handa 	if (!ptr)
541d5ca1725STetsuo Handa 		ptr = &tomoyo_null_profile;
54257c2590fSTetsuo Handa 	return ptr;
54357c2590fSTetsuo Handa }
54457c2590fSTetsuo Handa 
545d5ca1725STetsuo Handa /**
546d5ca1725STetsuo Handa  * tomoyo_find_yesno - Find values for specified keyword.
547d5ca1725STetsuo Handa  *
548d5ca1725STetsuo Handa  * @string: String to check.
549d5ca1725STetsuo Handa  * @find:   Name of keyword.
550d5ca1725STetsuo Handa  *
551d5ca1725STetsuo Handa  * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
552d5ca1725STetsuo Handa  */
5538e568687STetsuo Handa static s8 tomoyo_find_yesno(const char *string, const char *find)
5548e568687STetsuo Handa {
5558e568687STetsuo Handa 	const char *cp = strstr(string, find);
556*cdcf6723STetsuo Handa 
5578e568687STetsuo Handa 	if (cp) {
5588e568687STetsuo Handa 		cp += strlen(find);
5598e568687STetsuo Handa 		if (!strncmp(cp, "=yes", 4))
5608e568687STetsuo Handa 			return 1;
5618e568687STetsuo Handa 		else if (!strncmp(cp, "=no", 3))
5628e568687STetsuo Handa 			return 0;
5638e568687STetsuo Handa 	}
5648e568687STetsuo Handa 	return -1;
5658e568687STetsuo Handa }
5668e568687STetsuo Handa 
567d5ca1725STetsuo Handa /**
568d5ca1725STetsuo Handa  * tomoyo_set_uint - Set value for specified preference.
569d5ca1725STetsuo Handa  *
570d5ca1725STetsuo Handa  * @i:      Pointer to "unsigned int".
571d5ca1725STetsuo Handa  * @string: String to check.
572d5ca1725STetsuo Handa  * @find:   Name of keyword.
573d5ca1725STetsuo Handa  *
574d5ca1725STetsuo Handa  * Returns nothing.
575d5ca1725STetsuo Handa  */
5768e568687STetsuo Handa static void tomoyo_set_uint(unsigned int *i, const char *string,
5778e568687STetsuo Handa 			    const char *find)
5788e568687STetsuo Handa {
5798e568687STetsuo Handa 	const char *cp = strstr(string, find);
580*cdcf6723STetsuo Handa 
5818e568687STetsuo Handa 	if (cp)
5828e568687STetsuo Handa 		sscanf(cp + strlen(find), "=%u", i);
5838e568687STetsuo Handa }
5848e568687STetsuo Handa 
585d5ca1725STetsuo Handa /**
586d5ca1725STetsuo Handa  * tomoyo_set_mode - Set mode for specified profile.
587d5ca1725STetsuo Handa  *
588d5ca1725STetsuo Handa  * @name:    Name of functionality.
589d5ca1725STetsuo Handa  * @value:   Mode for @name.
590d5ca1725STetsuo Handa  * @profile: Pointer to "struct tomoyo_profile".
591d5ca1725STetsuo Handa  *
592d5ca1725STetsuo Handa  * Returns 0 on success, negative value otherwise.
593d5ca1725STetsuo Handa  */
5948e568687STetsuo Handa static int tomoyo_set_mode(char *name, const char *value,
5958e568687STetsuo Handa 			   struct tomoyo_profile *profile)
5968e568687STetsuo Handa {
5978e568687STetsuo Handa 	u8 i;
5988e568687STetsuo Handa 	u8 config;
599*cdcf6723STetsuo Handa 
6008e568687STetsuo Handa 	if (!strcmp(name, "CONFIG")) {
6018e568687STetsuo Handa 		i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
6028e568687STetsuo Handa 		config = profile->default_config;
6038e568687STetsuo Handa 	} else if (tomoyo_str_starts(&name, "CONFIG::")) {
6048e568687STetsuo Handa 		config = 0;
6058e568687STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MAC_INDEX
6068e568687STetsuo Handa 			     + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
6072c47ab93STetsuo Handa 			int len = 0;
608*cdcf6723STetsuo Handa 
6092c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX) {
6102c47ab93STetsuo Handa 				const u8 c = tomoyo_index2category[i];
6112c47ab93STetsuo Handa 				const char *category =
6122c47ab93STetsuo Handa 					tomoyo_category_keywords[c];
613*cdcf6723STetsuo Handa 
6142c47ab93STetsuo Handa 				len = strlen(category);
6152c47ab93STetsuo Handa 				if (strncmp(name, category, len) ||
6162c47ab93STetsuo Handa 				    name[len++] != ':' || name[len++] != ':')
6172c47ab93STetsuo Handa 					continue;
6182c47ab93STetsuo Handa 			}
6192c47ab93STetsuo Handa 			if (strcmp(name + len, tomoyo_mac_keywords[i]))
6208e568687STetsuo Handa 				continue;
6218e568687STetsuo Handa 			config = profile->config[i];
6228e568687STetsuo Handa 			break;
6238e568687STetsuo Handa 		}
6248e568687STetsuo Handa 		if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6258e568687STetsuo Handa 			return -EINVAL;
6268e568687STetsuo Handa 	} else {
6278e568687STetsuo Handa 		return -EINVAL;
6288e568687STetsuo Handa 	}
629d5ca1725STetsuo Handa 	if (strstr(value, "use_default")) {
6308e568687STetsuo Handa 		config = TOMOYO_CONFIG_USE_DEFAULT;
6318e568687STetsuo Handa 	} else {
6328e568687STetsuo Handa 		u8 mode;
633*cdcf6723STetsuo Handa 
6348e568687STetsuo Handa 		for (mode = 0; mode < 4; mode++)
6358e568687STetsuo Handa 			if (strstr(value, tomoyo_mode[mode]))
6368e568687STetsuo Handa 				/*
6378e568687STetsuo Handa 				 * Update lower 3 bits in order to distinguish
6388e568687STetsuo Handa 				 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
6398e568687STetsuo Handa 				 */
6408e568687STetsuo Handa 				config = (config & ~7) | mode;
641eadd99ccSTetsuo Handa 		if (config != TOMOYO_CONFIG_USE_DEFAULT) {
642eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "grant_log")) {
643eadd99ccSTetsuo Handa 			case 1:
644eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
645eadd99ccSTetsuo Handa 				break;
646eadd99ccSTetsuo Handa 			case 0:
647eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
648eadd99ccSTetsuo Handa 				break;
649eadd99ccSTetsuo Handa 			}
650eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "reject_log")) {
651eadd99ccSTetsuo Handa 			case 1:
652eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
653eadd99ccSTetsuo Handa 				break;
654eadd99ccSTetsuo Handa 			case 0:
655eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
656eadd99ccSTetsuo Handa 				break;
657eadd99ccSTetsuo Handa 			}
658eadd99ccSTetsuo Handa 		}
6598e568687STetsuo Handa 	}
6608e568687STetsuo Handa 	if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6618e568687STetsuo Handa 		profile->config[i] = config;
6628e568687STetsuo Handa 	else if (config != TOMOYO_CONFIG_USE_DEFAULT)
6638e568687STetsuo Handa 		profile->default_config = config;
6648e568687STetsuo Handa 	return 0;
6658e568687STetsuo Handa }
6668e568687STetsuo Handa 
66757c2590fSTetsuo Handa /**
66857c2590fSTetsuo Handa  * tomoyo_write_profile - Write profile table.
6699590837bSKentaro Takeda  *
6709590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
6719590837bSKentaro Takeda  *
6729590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
6739590837bSKentaro Takeda  */
6749590837bSKentaro Takeda static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
6759590837bSKentaro Takeda {
6769590837bSKentaro Takeda 	char *data = head->write_buf;
6779590837bSKentaro Takeda 	unsigned int i;
6789590837bSKentaro Takeda 	char *cp;
6799590837bSKentaro Takeda 	struct tomoyo_profile *profile;
680*cdcf6723STetsuo Handa 
681bd03a3e4STetsuo Handa 	if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
682bd03a3e4STetsuo Handa 	    == 1)
68357c2590fSTetsuo Handa 		return 0;
68457c2590fSTetsuo Handa 	i = simple_strtoul(data, &cp, 10);
68557c2590fSTetsuo Handa 	if (*cp != '-')
6869590837bSKentaro Takeda 		return -EINVAL;
6879590837bSKentaro Takeda 	data = cp + 1;
688bd03a3e4STetsuo Handa 	profile = tomoyo_assign_profile(head->w.ns, i);
6899590837bSKentaro Takeda 	if (!profile)
6909590837bSKentaro Takeda 		return -EINVAL;
6919590837bSKentaro Takeda 	cp = strchr(data, '=');
6929590837bSKentaro Takeda 	if (!cp)
6939590837bSKentaro Takeda 		return -EINVAL;
69457c2590fSTetsuo Handa 	*cp++ = '\0';
6959590837bSKentaro Takeda 	if (!strcmp(data, "COMMENT")) {
6962a086e5dSTetsuo Handa 		static DEFINE_SPINLOCK(lock);
6972a086e5dSTetsuo Handa 		const struct tomoyo_path_info *new_comment
6982a086e5dSTetsuo Handa 			= tomoyo_get_name(cp);
6992a086e5dSTetsuo Handa 		const struct tomoyo_path_info *old_comment;
700*cdcf6723STetsuo Handa 
7012a086e5dSTetsuo Handa 		if (!new_comment)
7022a086e5dSTetsuo Handa 			return -ENOMEM;
7032a086e5dSTetsuo Handa 		spin_lock(&lock);
7042a086e5dSTetsuo Handa 		old_comment = profile->comment;
7052a086e5dSTetsuo Handa 		profile->comment = new_comment;
7062a086e5dSTetsuo Handa 		spin_unlock(&lock);
707bf24fb01STetsuo Handa 		tomoyo_put_name(old_comment);
7089590837bSKentaro Takeda 		return 0;
7099590837bSKentaro Takeda 	}
710d5ca1725STetsuo Handa 	if (!strcmp(data, "PREFERENCE")) {
711d5ca1725STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_PREF; i++)
712d5ca1725STetsuo Handa 			tomoyo_set_uint(&profile->pref[i], cp,
713d5ca1725STetsuo Handa 					tomoyo_pref_keywords[i]);
714d5ca1725STetsuo Handa 		return 0;
7159590837bSKentaro Takeda 	}
716d5ca1725STetsuo Handa 	return tomoyo_set_mode(data, cp, profile);
717f23571e8STetsuo Handa }
718f23571e8STetsuo Handa 
719eadd99ccSTetsuo Handa /**
720eadd99ccSTetsuo Handa  * tomoyo_print_config - Print mode for specified functionality.
721eadd99ccSTetsuo Handa  *
722eadd99ccSTetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
723eadd99ccSTetsuo Handa  * @config: Mode for that functionality.
724eadd99ccSTetsuo Handa  *
725eadd99ccSTetsuo Handa  * Returns nothing.
726eadd99ccSTetsuo Handa  *
727eadd99ccSTetsuo Handa  * Caller prints functionality's name.
728eadd99ccSTetsuo Handa  */
729f23571e8STetsuo Handa static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
730f23571e8STetsuo Handa {
731eadd99ccSTetsuo Handa 	tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
732eadd99ccSTetsuo Handa 			 tomoyo_mode[config & 3],
733eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
734eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
735f23571e8STetsuo Handa }
736f23571e8STetsuo Handa 
7379590837bSKentaro Takeda /**
73857c2590fSTetsuo Handa  * tomoyo_read_profile - Read profile table.
7399590837bSKentaro Takeda  *
7409590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
741eadd99ccSTetsuo Handa  *
742eadd99ccSTetsuo Handa  * Returns nothing.
7439590837bSKentaro Takeda  */
7448fbe71f0STetsuo Handa static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
7459590837bSKentaro Takeda {
746f23571e8STetsuo Handa 	u8 index;
747bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
748bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
749f23571e8STetsuo Handa 	const struct tomoyo_profile *profile;
750*cdcf6723STetsuo Handa 
751bd03a3e4STetsuo Handa 	if (head->r.eof)
752bd03a3e4STetsuo Handa 		return;
753f23571e8STetsuo Handa  next:
754f23571e8STetsuo Handa 	index = head->r.index;
755bd03a3e4STetsuo Handa 	profile = ns->profile_ptr[index];
756f23571e8STetsuo Handa 	switch (head->r.step) {
757f23571e8STetsuo Handa 	case 0:
758bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
759bd03a3e4STetsuo Handa 		tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
760bd03a3e4STetsuo Handa 				 ns->profile_version);
761f23571e8STetsuo Handa 		head->r.step++;
762f23571e8STetsuo Handa 		break;
763f23571e8STetsuo Handa 	case 1:
764f23571e8STetsuo Handa 		for ( ; head->r.index < TOMOYO_MAX_PROFILES;
765f23571e8STetsuo Handa 		      head->r.index++)
766bd03a3e4STetsuo Handa 			if (ns->profile_ptr[head->r.index])
767f23571e8STetsuo Handa 				break;
7684d818971STetsuo Handa 		if (head->r.index == TOMOYO_MAX_PROFILES) {
7694d818971STetsuo Handa 			head->r.eof = true;
770f23571e8STetsuo Handa 			return;
7714d818971STetsuo Handa 		}
772f23571e8STetsuo Handa 		head->r.step++;
773f23571e8STetsuo Handa 		break;
774f23571e8STetsuo Handa 	case 2:
775f23571e8STetsuo Handa 		{
776d5ca1725STetsuo Handa 			u8 i;
777f23571e8STetsuo Handa 			const struct tomoyo_path_info *comment =
778f23571e8STetsuo Handa 				profile->comment;
779*cdcf6723STetsuo Handa 
780bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
781f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-COMMENT=", index);
782f23571e8STetsuo Handa 			tomoyo_set_string(head, comment ? comment->name : "");
783f23571e8STetsuo Handa 			tomoyo_set_lf(head);
7844d818971STetsuo Handa 			tomoyo_print_namespace(head);
785d5ca1725STetsuo Handa 			tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
786d5ca1725STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_PREF; i++)
787d5ca1725STetsuo Handa 				tomoyo_io_printf(head, "%s=%u ",
788d5ca1725STetsuo Handa 						 tomoyo_pref_keywords[i],
789d5ca1725STetsuo Handa 						 profile->pref[i]);
790d5ca1725STetsuo Handa 			tomoyo_set_string(head, "}\n");
791f23571e8STetsuo Handa 			head->r.step++;
792f23571e8STetsuo Handa 		}
793f23571e8STetsuo Handa 		break;
794f23571e8STetsuo Handa 	case 3:
795f23571e8STetsuo Handa 		{
796bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
797f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
798f23571e8STetsuo Handa 			tomoyo_print_config(head, profile->default_config);
799f23571e8STetsuo Handa 			head->r.bit = 0;
800f23571e8STetsuo Handa 			head->r.step++;
801f23571e8STetsuo Handa 		}
802f23571e8STetsuo Handa 		break;
803f23571e8STetsuo Handa 	case 4:
804f23571e8STetsuo Handa 		for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
805f23571e8STetsuo Handa 			      + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
806f23571e8STetsuo Handa 			const u8 i = head->r.bit;
807f23571e8STetsuo Handa 			const u8 config = profile->config[i];
808*cdcf6723STetsuo Handa 
80957c2590fSTetsuo Handa 			if (config == TOMOYO_CONFIG_USE_DEFAULT)
8109590837bSKentaro Takeda 				continue;
811bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
8122c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX)
8132c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
8142c47ab93STetsuo Handa 						 index,
8152c47ab93STetsuo Handa 						 tomoyo_category_keywords
8162c47ab93STetsuo Handa 						 [tomoyo_index2category[i]],
8172c47ab93STetsuo Handa 						 tomoyo_mac_keywords[i]);
8182c47ab93STetsuo Handa 			else
8192c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s", index,
820f23571e8STetsuo Handa 						 tomoyo_mac_keywords[i]);
821f23571e8STetsuo Handa 			tomoyo_print_config(head, config);
822f23571e8STetsuo Handa 			head->r.bit++;
8239590837bSKentaro Takeda 			break;
8249590837bSKentaro Takeda 		}
825f23571e8STetsuo Handa 		if (head->r.bit == TOMOYO_MAX_MAC_INDEX
826f23571e8STetsuo Handa 		    + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
827f23571e8STetsuo Handa 			head->r.index++;
828f23571e8STetsuo Handa 			head->r.step = 1;
829f23571e8STetsuo Handa 		}
830f23571e8STetsuo Handa 		break;
831f23571e8STetsuo Handa 	}
832f23571e8STetsuo Handa 	if (tomoyo_flush(head))
833f23571e8STetsuo Handa 		goto next;
8349590837bSKentaro Takeda }
8359590837bSKentaro Takeda 
8360f2a55d5STetsuo Handa /**
8370f2a55d5STetsuo Handa  * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
8380f2a55d5STetsuo Handa  *
8390f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
8400f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
8410f2a55d5STetsuo Handa  *
8420f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
8430f2a55d5STetsuo Handa  */
844e2bf6907STetsuo Handa static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
84536f5e1ffSTetsuo Handa 				const struct tomoyo_acl_head *b)
84636f5e1ffSTetsuo Handa {
847e2bf6907STetsuo Handa 	return container_of(a, struct tomoyo_manager, head)->manager ==
848e2bf6907STetsuo Handa 		container_of(b, struct tomoyo_manager, head)->manager;
84936f5e1ffSTetsuo Handa }
85036f5e1ffSTetsuo Handa 
8519590837bSKentaro Takeda /**
8529590837bSKentaro Takeda  * tomoyo_update_manager_entry - Add a manager entry.
8539590837bSKentaro Takeda  *
8549590837bSKentaro Takeda  * @manager:   The path to manager or the domainnamme.
8559590837bSKentaro Takeda  * @is_delete: True if it is a delete request.
8569590837bSKentaro Takeda  *
8579590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
858fdb8ebb7STetsuo Handa  *
859fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8609590837bSKentaro Takeda  */
8619590837bSKentaro Takeda static int tomoyo_update_manager_entry(const char *manager,
8629590837bSKentaro Takeda 				       const bool is_delete)
8639590837bSKentaro Takeda {
864e2bf6907STetsuo Handa 	struct tomoyo_manager e = { };
865a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
866bd03a3e4STetsuo Handa 		/* .ns = &tomoyo_kernel_namespace, */
867a238cf5bSTetsuo Handa 		.is_delete = is_delete,
868*cdcf6723STetsuo Handa 		.list = &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER],
869a238cf5bSTetsuo Handa 	};
870a238cf5bSTetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
871*cdcf6723STetsuo Handa 
87277b513ddSTetsuo Handa 	if (!tomoyo_correct_domain(manager) &&
87377b513ddSTetsuo Handa 	    !tomoyo_correct_word(manager))
8749590837bSKentaro Takeda 		return -EINVAL;
8759e4b50e9STetsuo Handa 	e.manager = tomoyo_get_name(manager);
876a238cf5bSTetsuo Handa 	if (e.manager) {
877a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), &param,
878e2bf6907STetsuo Handa 					     tomoyo_same_manager);
8799e4b50e9STetsuo Handa 		tomoyo_put_name(e.manager);
880a238cf5bSTetsuo Handa 	}
8819590837bSKentaro Takeda 	return error;
8829590837bSKentaro Takeda }
8839590837bSKentaro Takeda 
8849590837bSKentaro Takeda /**
885e2bf6907STetsuo Handa  * tomoyo_write_manager - Write manager policy.
8869590837bSKentaro Takeda  *
8879590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8889590837bSKentaro Takeda  *
8899590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
890fdb8ebb7STetsuo Handa  *
891fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8929590837bSKentaro Takeda  */
893e2bf6907STetsuo Handa static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
8949590837bSKentaro Takeda {
8959590837bSKentaro Takeda 	char *data = head->write_buf;
8969590837bSKentaro Takeda 
8979590837bSKentaro Takeda 	if (!strcmp(data, "manage_by_non_root")) {
898bd03a3e4STetsuo Handa 		tomoyo_manage_by_non_root = !head->w.is_delete;
8999590837bSKentaro Takeda 		return 0;
9009590837bSKentaro Takeda 	}
901bd03a3e4STetsuo Handa 	return tomoyo_update_manager_entry(data, head->w.is_delete);
9029590837bSKentaro Takeda }
9039590837bSKentaro Takeda 
9049590837bSKentaro Takeda /**
905e2bf6907STetsuo Handa  * tomoyo_read_manager - Read manager policy.
9069590837bSKentaro Takeda  *
9079590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9089590837bSKentaro Takeda  *
909fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9109590837bSKentaro Takeda  */
911e2bf6907STetsuo Handa static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
9129590837bSKentaro Takeda {
913f23571e8STetsuo Handa 	if (head->r.eof)
9148fbe71f0STetsuo Handa 		return;
915*cdcf6723STetsuo Handa 	list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER]) {
916e2bf6907STetsuo Handa 		struct tomoyo_manager *ptr =
917f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), head.list);
918*cdcf6723STetsuo Handa 
91982e0f001STetsuo Handa 		if (ptr->head.is_deleted)
9209590837bSKentaro Takeda 			continue;
921f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
922f23571e8STetsuo Handa 			return;
923f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->manager->name);
924f23571e8STetsuo Handa 		tomoyo_set_lf(head);
9259590837bSKentaro Takeda 	}
926f23571e8STetsuo Handa 	head->r.eof = true;
9279590837bSKentaro Takeda }
9289590837bSKentaro Takeda 
9299590837bSKentaro Takeda /**
930e2bf6907STetsuo Handa  * tomoyo_manager - Check whether the current process is a policy manager.
9319590837bSKentaro Takeda  *
9329590837bSKentaro Takeda  * Returns true if the current process is permitted to modify policy
9339590837bSKentaro Takeda  * via /sys/kernel/security/tomoyo/ interface.
934fdb8ebb7STetsuo Handa  *
935fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9369590837bSKentaro Takeda  */
937e2bf6907STetsuo Handa static bool tomoyo_manager(void)
9389590837bSKentaro Takeda {
939e2bf6907STetsuo Handa 	struct tomoyo_manager *ptr;
9409590837bSKentaro Takeda 	const char *exe;
9419590837bSKentaro Takeda 	const struct task_struct *task = current;
9429590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
9439590837bSKentaro Takeda 	bool found = false;
9449590837bSKentaro Takeda 
9459590837bSKentaro Takeda 	if (!tomoyo_policy_loaded)
9469590837bSKentaro Takeda 		return true;
947609fcd1bSEric W. Biederman 	if (!tomoyo_manage_by_non_root &&
948609fcd1bSEric W. Biederman 	    (!uid_eq(task->cred->uid,  GLOBAL_ROOT_UID) ||
949609fcd1bSEric W. Biederman 	     !uid_eq(task->cred->euid, GLOBAL_ROOT_UID)))
9509590837bSKentaro Takeda 		return false;
9519590837bSKentaro Takeda 	exe = tomoyo_get_exe();
9529590837bSKentaro Takeda 	if (!exe)
9539590837bSKentaro Takeda 		return false;
954*cdcf6723STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER], head.list) {
95577b513ddSTetsuo Handa 		if (!ptr->head.is_deleted &&
95677b513ddSTetsuo Handa 		    (!tomoyo_pathcmp(domainname, ptr->manager) ||
95777b513ddSTetsuo Handa 		     !strcmp(exe, ptr->manager->name))) {
9589590837bSKentaro Takeda 			found = true;
9599590837bSKentaro Takeda 			break;
9609590837bSKentaro Takeda 		}
9619590837bSKentaro Takeda 	}
9629590837bSKentaro Takeda 	if (!found) { /* Reduce error messages. */
9639590837bSKentaro Takeda 		static pid_t last_pid;
9649590837bSKentaro Takeda 		const pid_t pid = current->pid;
965*cdcf6723STetsuo Handa 
9669590837bSKentaro Takeda 		if (last_pid != pid) {
967*cdcf6723STetsuo Handa 			pr_warn("%s ( %s ) is not permitted to update policies.\n",
968*cdcf6723STetsuo Handa 				domainname->name, exe);
9699590837bSKentaro Takeda 			last_pid = pid;
9709590837bSKentaro Takeda 		}
9719590837bSKentaro Takeda 	}
9728e2d39a1STetsuo Handa 	kfree(exe);
9739590837bSKentaro Takeda 	return found;
9749590837bSKentaro Takeda }
9759590837bSKentaro Takeda 
97659df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
97759df3166STetsuo Handa (unsigned int serial);
97859df3166STetsuo Handa 
9799590837bSKentaro Takeda /**
980bd03a3e4STetsuo Handa  * tomoyo_select_domain - Parse select command.
9819590837bSKentaro Takeda  *
9829590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9839590837bSKentaro Takeda  * @data: String to parse.
9849590837bSKentaro Takeda  *
9859590837bSKentaro Takeda  * Returns true on success, false otherwise.
986fdb8ebb7STetsuo Handa  *
987fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9889590837bSKentaro Takeda  */
989bd03a3e4STetsuo Handa static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
990bd03a3e4STetsuo Handa 				 const char *data)
9919590837bSKentaro Takeda {
9929590837bSKentaro Takeda 	unsigned int pid;
9939590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
9949b244373STetsuo Handa 	bool global_pid = false;
995*cdcf6723STetsuo Handa 
996bd03a3e4STetsuo Handa 	if (strncmp(data, "select ", 7))
997bd03a3e4STetsuo Handa 		return false;
998bd03a3e4STetsuo Handa 	data += 7;
9999b244373STetsuo Handa 	if (sscanf(data, "pid=%u", &pid) == 1 ||
10009b244373STetsuo Handa 	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
10019590837bSKentaro Takeda 		struct task_struct *p;
1002*cdcf6723STetsuo Handa 
10031fcdc7c5STetsuo Handa 		rcu_read_lock();
10049b244373STetsuo Handa 		if (global_pid)
10059b244373STetsuo Handa 			p = find_task_by_pid_ns(pid, &init_pid_ns);
10069b244373STetsuo Handa 		else
10079590837bSKentaro Takeda 			p = find_task_by_vpid(pid);
10089590837bSKentaro Takeda 		if (p)
10098c6cb983STetsuo Handa 			domain = tomoyo_task(p)->domain_info;
10101fcdc7c5STetsuo Handa 		rcu_read_unlock();
10119590837bSKentaro Takeda 	} else if (!strncmp(data, "domain=", 7)) {
101275093152STetsuo Handa 		if (tomoyo_domain_def(data + 7))
10139590837bSKentaro Takeda 			domain = tomoyo_find_domain(data + 7);
101459df3166STetsuo Handa 	} else if (sscanf(data, "Q=%u", &pid) == 1) {
101559df3166STetsuo Handa 		domain = tomoyo_find_domain_by_qid(pid);
10169590837bSKentaro Takeda 	} else
10179590837bSKentaro Takeda 		return false;
10180df7e8b8STetsuo Handa 	head->w.domain = domain;
10199590837bSKentaro Takeda 	/* Accessing read_buf is safe because head->io_sem is held. */
10209590837bSKentaro Takeda 	if (!head->read_buf)
10219590837bSKentaro Takeda 		return true; /* Do nothing if open(O_WRONLY). */
1022f23571e8STetsuo Handa 	memset(&head->r, 0, sizeof(head->r));
1023f23571e8STetsuo Handa 	head->r.print_this_domain_only = true;
102468eda8f5SDan Carpenter 	if (domain)
1025f23571e8STetsuo Handa 		head->r.domain = &domain->list;
102668eda8f5SDan Carpenter 	else
102768eda8f5SDan Carpenter 		head->r.eof = 1;
10289590837bSKentaro Takeda 	tomoyo_io_printf(head, "# select %s\n", data);
1029475e6fa3STetsuo Handa 	if (domain && domain->is_deleted)
10309590837bSKentaro Takeda 		tomoyo_io_printf(head, "# This is a deleted domain.\n");
10319590837bSKentaro Takeda 	return true;
10329590837bSKentaro Takeda }
10339590837bSKentaro Takeda 
10349590837bSKentaro Takeda /**
1035731d37aaSTetsuo Handa  * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1036731d37aaSTetsuo Handa  *
1037731d37aaSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_info".
1038731d37aaSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_info".
1039731d37aaSTetsuo Handa  *
1040731d37aaSTetsuo Handa  * Returns true if @a == @b, false otherwise.
1041731d37aaSTetsuo Handa  */
1042731d37aaSTetsuo Handa static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1043731d37aaSTetsuo Handa 				 const struct tomoyo_acl_info *b)
1044731d37aaSTetsuo Handa {
1045731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1046731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1047*cdcf6723STetsuo Handa 
1048731d37aaSTetsuo Handa 	return p1->domainname == p2->domainname;
1049731d37aaSTetsuo Handa }
1050731d37aaSTetsuo Handa 
1051731d37aaSTetsuo Handa /**
1052731d37aaSTetsuo Handa  * tomoyo_write_task - Update task related list.
1053731d37aaSTetsuo Handa  *
1054731d37aaSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
1055731d37aaSTetsuo Handa  *
1056731d37aaSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1057731d37aaSTetsuo Handa  *
1058731d37aaSTetsuo Handa  * Caller holds tomoyo_read_lock().
1059731d37aaSTetsuo Handa  */
1060731d37aaSTetsuo Handa static int tomoyo_write_task(struct tomoyo_acl_param *param)
1061731d37aaSTetsuo Handa {
1062731d37aaSTetsuo Handa 	int error = -EINVAL;
1063*cdcf6723STetsuo Handa 
1064731d37aaSTetsuo Handa 	if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1065731d37aaSTetsuo Handa 		struct tomoyo_task_acl e = {
1066731d37aaSTetsuo Handa 			.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1067731d37aaSTetsuo Handa 			.domainname = tomoyo_get_domainname(param),
1068731d37aaSTetsuo Handa 		};
1069*cdcf6723STetsuo Handa 
1070731d37aaSTetsuo Handa 		if (e.domainname)
1071731d37aaSTetsuo Handa 			error = tomoyo_update_domain(&e.head, sizeof(e), param,
1072731d37aaSTetsuo Handa 						     tomoyo_same_task_acl,
1073731d37aaSTetsuo Handa 						     NULL);
1074731d37aaSTetsuo Handa 		tomoyo_put_name(e.domainname);
1075731d37aaSTetsuo Handa 	}
1076731d37aaSTetsuo Handa 	return error;
1077731d37aaSTetsuo Handa }
1078731d37aaSTetsuo Handa 
1079731d37aaSTetsuo Handa /**
1080ccf135f5STetsuo Handa  * tomoyo_delete_domain - Delete a domain.
1081ccf135f5STetsuo Handa  *
1082ccf135f5STetsuo Handa  * @domainname: The name of domain.
1083ccf135f5STetsuo Handa  *
10847d7473dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1085fdb8ebb7STetsuo Handa  *
1086fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1087ccf135f5STetsuo Handa  */
1088ccf135f5STetsuo Handa static int tomoyo_delete_domain(char *domainname)
1089ccf135f5STetsuo Handa {
1090ccf135f5STetsuo Handa 	struct tomoyo_domain_info *domain;
1091ccf135f5STetsuo Handa 	struct tomoyo_path_info name;
1092ccf135f5STetsuo Handa 
1093ccf135f5STetsuo Handa 	name.name = domainname;
1094ccf135f5STetsuo Handa 	tomoyo_fill_path_info(&name);
109529282381STetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
10967d7473dbSTetsuo Handa 		return -EINTR;
1097ccf135f5STetsuo Handa 	/* Is there an active domain? */
1098fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1099ccf135f5STetsuo Handa 		/* Never delete tomoyo_kernel_domain */
1100ccf135f5STetsuo Handa 		if (domain == &tomoyo_kernel_domain)
1101ccf135f5STetsuo Handa 			continue;
1102ccf135f5STetsuo Handa 		if (domain->is_deleted ||
1103ccf135f5STetsuo Handa 		    tomoyo_pathcmp(domain->domainname, &name))
1104ccf135f5STetsuo Handa 			continue;
1105ccf135f5STetsuo Handa 		domain->is_deleted = true;
1106ccf135f5STetsuo Handa 		break;
1107ccf135f5STetsuo Handa 	}
1108f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
1109ccf135f5STetsuo Handa 	return 0;
1110ccf135f5STetsuo Handa }
1111ccf135f5STetsuo Handa 
1112ccf135f5STetsuo Handa /**
1113e2bf6907STetsuo Handa  * tomoyo_write_domain2 - Write domain policy.
111417fcfbd9STetsuo Handa  *
1115bd03a3e4STetsuo Handa  * @ns:        Pointer to "struct tomoyo_policy_namespace".
1116a238cf5bSTetsuo Handa  * @list:      Pointer to "struct list_head".
1117a238cf5bSTetsuo Handa  * @data:      Policy to be interpreted.
1118a238cf5bSTetsuo Handa  * @is_delete: True if it is a delete request.
111917fcfbd9STetsuo Handa  *
112017fcfbd9STetsuo Handa  * Returns 0 on success, negative value otherwise.
112117fcfbd9STetsuo Handa  *
112217fcfbd9STetsuo Handa  * Caller holds tomoyo_read_lock().
112317fcfbd9STetsuo Handa  */
1124bd03a3e4STetsuo Handa static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1125bd03a3e4STetsuo Handa 				struct list_head *list, char *data,
112617fcfbd9STetsuo Handa 				const bool is_delete)
112717fcfbd9STetsuo Handa {
1128a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1129bd03a3e4STetsuo Handa 		.ns = ns,
1130a238cf5bSTetsuo Handa 		.list = list,
1131a238cf5bSTetsuo Handa 		.data = data,
1132a238cf5bSTetsuo Handa 		.is_delete = is_delete,
1133a238cf5bSTetsuo Handa 	};
1134a238cf5bSTetsuo Handa 	static const struct {
1135a238cf5bSTetsuo Handa 		const char *keyword;
1136*cdcf6723STetsuo Handa 		int (*write)(struct tomoyo_acl_param *param);
1137731d37aaSTetsuo Handa 	} tomoyo_callback[5] = {
1138a238cf5bSTetsuo Handa 		{ "file ", tomoyo_write_file },
1139059d84dbSTetsuo Handa 		{ "network inet ", tomoyo_write_inet_network },
1140059d84dbSTetsuo Handa 		{ "network unix ", tomoyo_write_unix_network },
1141d58e0da8STetsuo Handa 		{ "misc ", tomoyo_write_misc },
1142731d37aaSTetsuo Handa 		{ "task ", tomoyo_write_task },
1143a238cf5bSTetsuo Handa 	};
1144a238cf5bSTetsuo Handa 	u8 i;
1145d58e0da8STetsuo Handa 
1146d58e0da8STetsuo Handa 	for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
1147a238cf5bSTetsuo Handa 		if (!tomoyo_str_starts(&param.data,
1148a238cf5bSTetsuo Handa 				       tomoyo_callback[i].keyword))
1149a238cf5bSTetsuo Handa 			continue;
1150a238cf5bSTetsuo Handa 		return tomoyo_callback[i].write(&param);
1151a238cf5bSTetsuo Handa 	}
1152a238cf5bSTetsuo Handa 	return -EINVAL;
115317fcfbd9STetsuo Handa }
115417fcfbd9STetsuo Handa 
11552c47ab93STetsuo Handa /* String table for domain flags. */
11562c47ab93STetsuo Handa const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
11572c47ab93STetsuo Handa 	[TOMOYO_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
11582c47ab93STetsuo Handa 	[TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
11592c47ab93STetsuo Handa };
11602c47ab93STetsuo Handa 
116117fcfbd9STetsuo Handa /**
1162e2bf6907STetsuo Handa  * tomoyo_write_domain - Write domain policy.
11639590837bSKentaro Takeda  *
11649590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
11659590837bSKentaro Takeda  *
11669590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1167fdb8ebb7STetsuo Handa  *
1168fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
11699590837bSKentaro Takeda  */
1170e2bf6907STetsuo Handa static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
11719590837bSKentaro Takeda {
11729590837bSKentaro Takeda 	char *data = head->write_buf;
1173bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
11740df7e8b8STetsuo Handa 	struct tomoyo_domain_info *domain = head->w.domain;
1175bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1176bd03a3e4STetsuo Handa 	bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
11779590837bSKentaro Takeda 	unsigned int profile;
1178*cdcf6723STetsuo Handa 
1179bd03a3e4STetsuo Handa 	if (*data == '<') {
11807d7473dbSTetsuo Handa 		int ret = 0;
1181*cdcf6723STetsuo Handa 
11829590837bSKentaro Takeda 		domain = NULL;
11839590837bSKentaro Takeda 		if (is_delete)
11847d7473dbSTetsuo Handa 			ret = tomoyo_delete_domain(data);
1185fdb8ebb7STetsuo Handa 		else if (is_select)
11869590837bSKentaro Takeda 			domain = tomoyo_find_domain(data);
1187fdb8ebb7STetsuo Handa 		else
1188bd03a3e4STetsuo Handa 			domain = tomoyo_assign_domain(data, false);
11890df7e8b8STetsuo Handa 		head->w.domain = domain;
11907d7473dbSTetsuo Handa 		return ret;
11919590837bSKentaro Takeda 	}
11929590837bSKentaro Takeda 	if (!domain)
11939590837bSKentaro Takeda 		return -EINVAL;
1194bd03a3e4STetsuo Handa 	ns = domain->ns;
1195b5bc60b4STetsuo Handa 	if (sscanf(data, "use_profile %u", &profile) == 1
11969590837bSKentaro Takeda 	    && profile < TOMOYO_MAX_PROFILES) {
1197bd03a3e4STetsuo Handa 		if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
11989590837bSKentaro Takeda 			domain->profile = (u8) profile;
11999590837bSKentaro Takeda 		return 0;
12009590837bSKentaro Takeda 	}
120132997144STetsuo Handa 	if (sscanf(data, "use_group %u\n", &profile) == 1
120232997144STetsuo Handa 	    && profile < TOMOYO_MAX_ACL_GROUPS) {
120332997144STetsuo Handa 		if (!is_delete)
120432997144STetsuo Handa 			domain->group = (u8) profile;
120532997144STetsuo Handa 		return 0;
120632997144STetsuo Handa 	}
12072c47ab93STetsuo Handa 	for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
12082c47ab93STetsuo Handa 		const char *cp = tomoyo_dif[profile];
1209*cdcf6723STetsuo Handa 
12102c47ab93STetsuo Handa 		if (strncmp(data, cp, strlen(cp) - 1))
12112c47ab93STetsuo Handa 			continue;
12122c47ab93STetsuo Handa 		domain->flags[profile] = !is_delete;
12139b244373STetsuo Handa 		return 0;
12149b244373STetsuo Handa 	}
1215bd03a3e4STetsuo Handa 	return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1216bd03a3e4STetsuo Handa 				    is_delete);
12179590837bSKentaro Takeda }
12189590837bSKentaro Takeda 
12199590837bSKentaro Takeda /**
12202066a361STetsuo Handa  * tomoyo_print_condition - Print condition part.
12212066a361STetsuo Handa  *
12222066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
12232066a361STetsuo Handa  * @cond: Pointer to "struct tomoyo_condition".
12242066a361STetsuo Handa  *
12252066a361STetsuo Handa  * Returns true on success, false otherwise.
12262066a361STetsuo Handa  */
12272066a361STetsuo Handa static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
12282066a361STetsuo Handa 				   const struct tomoyo_condition *cond)
12292066a361STetsuo Handa {
12302066a361STetsuo Handa 	switch (head->r.cond_step) {
12312066a361STetsuo Handa 	case 0:
12322066a361STetsuo Handa 		head->r.cond_index = 0;
12332066a361STetsuo Handa 		head->r.cond_step++;
12346bce98edSTetsuo Handa 		if (cond->transit) {
12356bce98edSTetsuo Handa 			tomoyo_set_space(head);
12366bce98edSTetsuo Handa 			tomoyo_set_string(head, cond->transit->name);
12376bce98edSTetsuo Handa 		}
12382066a361STetsuo Handa 		/* fall through */
12392066a361STetsuo Handa 	case 1:
12402066a361STetsuo Handa 		{
12412066a361STetsuo Handa 			const u16 condc = cond->condc;
12422066a361STetsuo Handa 			const struct tomoyo_condition_element *condp =
12432066a361STetsuo Handa 				(typeof(condp)) (cond + 1);
12442066a361STetsuo Handa 			const struct tomoyo_number_union *numbers_p =
12452066a361STetsuo Handa 				(typeof(numbers_p)) (condp + condc);
12462ca9bf45STetsuo Handa 			const struct tomoyo_name_union *names_p =
12472ca9bf45STetsuo Handa 				(typeof(names_p))
12482ca9bf45STetsuo Handa 				(numbers_p + cond->numbers_count);
12495b636857STetsuo Handa 			const struct tomoyo_argv *argv =
12505b636857STetsuo Handa 				(typeof(argv)) (names_p + cond->names_count);
12515b636857STetsuo Handa 			const struct tomoyo_envp *envp =
12525b636857STetsuo Handa 				(typeof(envp)) (argv + cond->argc);
12532066a361STetsuo Handa 			u16 skip;
1254*cdcf6723STetsuo Handa 
12552066a361STetsuo Handa 			for (skip = 0; skip < head->r.cond_index; skip++) {
12562066a361STetsuo Handa 				const u8 left = condp->left;
12572066a361STetsuo Handa 				const u8 right = condp->right;
1258*cdcf6723STetsuo Handa 
12592066a361STetsuo Handa 				condp++;
12602066a361STetsuo Handa 				switch (left) {
12615b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12625b636857STetsuo Handa 					argv++;
12635b636857STetsuo Handa 					continue;
12645b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12655b636857STetsuo Handa 					envp++;
12665b636857STetsuo Handa 					continue;
12672066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12682066a361STetsuo Handa 					numbers_p++;
12692066a361STetsuo Handa 					break;
12702066a361STetsuo Handa 				}
12712066a361STetsuo Handa 				switch (right) {
12722ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
12732ca9bf45STetsuo Handa 					names_p++;
12742ca9bf45STetsuo Handa 					break;
12752066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12762066a361STetsuo Handa 					numbers_p++;
12772066a361STetsuo Handa 					break;
12782066a361STetsuo Handa 				}
12792066a361STetsuo Handa 			}
12802066a361STetsuo Handa 			while (head->r.cond_index < condc) {
12812066a361STetsuo Handa 				const u8 match = condp->equals;
12822066a361STetsuo Handa 				const u8 left = condp->left;
12832066a361STetsuo Handa 				const u8 right = condp->right;
1284*cdcf6723STetsuo Handa 
12852066a361STetsuo Handa 				if (!tomoyo_flush(head))
12862066a361STetsuo Handa 					return false;
12872066a361STetsuo Handa 				condp++;
12882066a361STetsuo Handa 				head->r.cond_index++;
12892066a361STetsuo Handa 				tomoyo_set_space(head);
12902066a361STetsuo Handa 				switch (left) {
12915b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12925b636857STetsuo Handa 					tomoyo_io_printf(head,
12935b636857STetsuo Handa 							 "exec.argv[%lu]%s=\"",
1294*cdcf6723STetsuo Handa 							 argv->index, argv->is_not ? "!" : "");
12955b636857STetsuo Handa 					tomoyo_set_string(head,
12965b636857STetsuo Handa 							  argv->value->name);
12975b636857STetsuo Handa 					tomoyo_set_string(head, "\"");
12985b636857STetsuo Handa 					argv++;
12995b636857STetsuo Handa 					continue;
13005b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
13015b636857STetsuo Handa 					tomoyo_set_string(head,
13025b636857STetsuo Handa 							  "exec.envp[\"");
13035b636857STetsuo Handa 					tomoyo_set_string(head,
13045b636857STetsuo Handa 							  envp->name->name);
1305*cdcf6723STetsuo Handa 					tomoyo_io_printf(head, "\"]%s=", envp->is_not ? "!" : "");
13065b636857STetsuo Handa 					if (envp->value) {
13075b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
1308*cdcf6723STetsuo Handa 						tomoyo_set_string(head, envp->value->name);
13095b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
13105b636857STetsuo Handa 					} else {
13115b636857STetsuo Handa 						tomoyo_set_string(head,
13125b636857STetsuo Handa 								  "NULL");
13135b636857STetsuo Handa 					}
13145b636857STetsuo Handa 					envp++;
13155b636857STetsuo Handa 					continue;
13162066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13172066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13182066a361STetsuo Handa 						(head, numbers_p++);
13192066a361STetsuo Handa 					break;
13202066a361STetsuo Handa 				default:
13212066a361STetsuo Handa 					tomoyo_set_string(head,
13222066a361STetsuo Handa 					       tomoyo_condition_keyword[left]);
13232066a361STetsuo Handa 					break;
13242066a361STetsuo Handa 				}
13252066a361STetsuo Handa 				tomoyo_set_string(head, match ? "=" : "!=");
13262066a361STetsuo Handa 				switch (right) {
13272ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
13282ca9bf45STetsuo Handa 					tomoyo_print_name_union_quoted
13292ca9bf45STetsuo Handa 						(head, names_p++);
13302ca9bf45STetsuo Handa 					break;
13312066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13322066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13332066a361STetsuo Handa 						(head, numbers_p++);
13342066a361STetsuo Handa 					break;
13352066a361STetsuo Handa 				default:
13362066a361STetsuo Handa 					tomoyo_set_string(head,
13372066a361STetsuo Handa 					  tomoyo_condition_keyword[right]);
13382066a361STetsuo Handa 					break;
13392066a361STetsuo Handa 				}
13402066a361STetsuo Handa 			}
13412066a361STetsuo Handa 		}
13422066a361STetsuo Handa 		head->r.cond_step++;
13432066a361STetsuo Handa 		/* fall through */
13442066a361STetsuo Handa 	case 2:
13452066a361STetsuo Handa 		if (!tomoyo_flush(head))
13462066a361STetsuo Handa 			break;
13472066a361STetsuo Handa 		head->r.cond_step++;
13482066a361STetsuo Handa 		/* fall through */
13492066a361STetsuo Handa 	case 3:
13501f067a68STetsuo Handa 		if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13511f067a68STetsuo Handa 			tomoyo_io_printf(head, " grant_log=%s",
13521f067a68STetsuo Handa 					 tomoyo_yesno(cond->grant_log ==
13531f067a68STetsuo Handa 						      TOMOYO_GRANTLOG_YES));
13542066a361STetsuo Handa 		tomoyo_set_lf(head);
13552066a361STetsuo Handa 		return true;
13562066a361STetsuo Handa 	}
13572066a361STetsuo Handa 	return false;
13582066a361STetsuo Handa }
13592066a361STetsuo Handa 
13602066a361STetsuo Handa /**
136132997144STetsuo Handa  * tomoyo_set_group - Print "acl_group " header keyword and category name.
13629590837bSKentaro Takeda  *
13630d2171d7STetsuo Handa  * @head:     Pointer to "struct tomoyo_io_buffer".
13640d2171d7STetsuo Handa  * @category: Category name.
13659590837bSKentaro Takeda  *
13660d2171d7STetsuo Handa  * Returns nothing.
13679590837bSKentaro Takeda  */
13680d2171d7STetsuo Handa static void tomoyo_set_group(struct tomoyo_io_buffer *head,
13690d2171d7STetsuo Handa 			     const char *category)
13709590837bSKentaro Takeda {
1371bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1372bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
137332997144STetsuo Handa 		tomoyo_io_printf(head, "acl_group %u ",
137432997144STetsuo Handa 				 head->r.acl_group_index);
1375bd03a3e4STetsuo Handa 	}
13760d2171d7STetsuo Handa 	tomoyo_set_string(head, category);
13772106ccd9STetsuo Handa }
13782106ccd9STetsuo Handa 
13792106ccd9STetsuo Handa /**
13809590837bSKentaro Takeda  * tomoyo_print_entry - Print an ACL entry.
13819590837bSKentaro Takeda  *
13829590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
13835db5a39bSTetsuo Handa  * @acl:  Pointer to an ACL entry.
13849590837bSKentaro Takeda  *
13859590837bSKentaro Takeda  * Returns true on success, false otherwise.
13869590837bSKentaro Takeda  */
13879590837bSKentaro Takeda static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13885db5a39bSTetsuo Handa 			       struct tomoyo_acl_info *acl)
13899590837bSKentaro Takeda {
13905db5a39bSTetsuo Handa 	const u8 acl_type = acl->type;
13910d2171d7STetsuo Handa 	bool first = true;
1392f23571e8STetsuo Handa 	u8 bit;
13939590837bSKentaro Takeda 
13942066a361STetsuo Handa 	if (head->r.print_cond_part)
13952066a361STetsuo Handa 		goto print_cond_part;
13965db5a39bSTetsuo Handa 	if (acl->is_deleted)
1397237ab459STetsuo Handa 		return true;
1398f23571e8STetsuo Handa 	if (!tomoyo_flush(head))
1399f23571e8STetsuo Handa 		return false;
1400f23571e8STetsuo Handa 	else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
14015db5a39bSTetsuo Handa 		struct tomoyo_path_acl *ptr =
14025db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14035db5a39bSTetsuo Handa 		const u16 perm = ptr->perm;
1404*cdcf6723STetsuo Handa 
14050d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
14065db5a39bSTetsuo Handa 			if (!(perm & (1 << bit)))
14075db5a39bSTetsuo Handa 				continue;
1408bd03a3e4STetsuo Handa 			if (head->r.print_transition_related_only &&
14095db5a39bSTetsuo Handa 			    bit != TOMOYO_TYPE_EXECUTE)
14105db5a39bSTetsuo Handa 				continue;
14110d2171d7STetsuo Handa 			if (first) {
14120d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14130d2171d7STetsuo Handa 				first = false;
14140d2171d7STetsuo Handa 			} else {
14150d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14169590837bSKentaro Takeda 			}
14170d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_path_keyword[bit]);
14180d2171d7STetsuo Handa 		}
14190d2171d7STetsuo Handa 		if (first)
14200d2171d7STetsuo Handa 			return true;
1421f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1422731d37aaSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1423731d37aaSTetsuo Handa 		struct tomoyo_task_acl *ptr =
1424731d37aaSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1425*cdcf6723STetsuo Handa 
1426731d37aaSTetsuo Handa 		tomoyo_set_group(head, "task ");
1427731d37aaSTetsuo Handa 		tomoyo_set_string(head, "manual_domain_transition ");
1428731d37aaSTetsuo Handa 		tomoyo_set_string(head, ptr->domainname->name);
1429bd03a3e4STetsuo Handa 	} else if (head->r.print_transition_related_only) {
1430063821c8STetsuo Handa 		return true;
14315db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
14325db5a39bSTetsuo Handa 		struct tomoyo_path2_acl *ptr =
14335db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14340d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
1435*cdcf6723STetsuo Handa 
14360d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14370d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14380d2171d7STetsuo Handa 				continue;
14390d2171d7STetsuo Handa 			if (first) {
14400d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14410d2171d7STetsuo Handa 				first = false;
14420d2171d7STetsuo Handa 			} else {
14430d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14440d2171d7STetsuo Handa 			}
14450d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14460d2171d7STetsuo Handa 					  [tomoyo_pp2mac[bit]]);
14470d2171d7STetsuo Handa 		}
14480d2171d7STetsuo Handa 		if (first)
14490d2171d7STetsuo Handa 			return true;
1450f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name1);
1451f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name2);
14525db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
14535db5a39bSTetsuo Handa 		struct tomoyo_path_number_acl *ptr =
14545db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14550d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
1456*cdcf6723STetsuo Handa 
14570d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14580d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14590d2171d7STetsuo Handa 				continue;
14600d2171d7STetsuo Handa 			if (first) {
14610d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14620d2171d7STetsuo Handa 				first = false;
14630d2171d7STetsuo Handa 			} else {
14640d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14650d2171d7STetsuo Handa 			}
14660d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14670d2171d7STetsuo Handa 					  [tomoyo_pn2mac[bit]]);
14680d2171d7STetsuo Handa 		}
14690d2171d7STetsuo Handa 		if (first)
14700d2171d7STetsuo Handa 			return true;
1471f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1472f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->number);
14735db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
14745db5a39bSTetsuo Handa 		struct tomoyo_mkdev_acl *ptr =
14755db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14760d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
1477*cdcf6723STetsuo Handa 
14780d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14790d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14800d2171d7STetsuo Handa 				continue;
14810d2171d7STetsuo Handa 			if (first) {
14820d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14830d2171d7STetsuo Handa 				first = false;
14840d2171d7STetsuo Handa 			} else {
14850d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14860d2171d7STetsuo Handa 			}
14870d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14880d2171d7STetsuo Handa 					  [tomoyo_pnnn2mac[bit]]);
14890d2171d7STetsuo Handa 		}
14900d2171d7STetsuo Handa 		if (first)
14910d2171d7STetsuo Handa 			return true;
1492f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1493f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->mode);
1494f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->major);
1495f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->minor);
1496059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1497059d84dbSTetsuo Handa 		struct tomoyo_inet_acl *ptr =
1498059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1499059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1500059d84dbSTetsuo Handa 
1501059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1502059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1503059d84dbSTetsuo Handa 				continue;
1504059d84dbSTetsuo Handa 			if (first) {
1505059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network inet ");
1506059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1507059d84dbSTetsuo Handa 						  [ptr->protocol]);
1508059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1509059d84dbSTetsuo Handa 				first = false;
1510059d84dbSTetsuo Handa 			} else {
1511059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1512059d84dbSTetsuo Handa 			}
1513059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1514059d84dbSTetsuo Handa 		}
1515059d84dbSTetsuo Handa 		if (first)
1516059d84dbSTetsuo Handa 			return true;
1517059d84dbSTetsuo Handa 		tomoyo_set_space(head);
1518059d84dbSTetsuo Handa 		if (ptr->address.group) {
1519059d84dbSTetsuo Handa 			tomoyo_set_string(head, "@");
1520059d84dbSTetsuo Handa 			tomoyo_set_string(head, ptr->address.group->group_name
1521059d84dbSTetsuo Handa 					  ->name);
1522059d84dbSTetsuo Handa 		} else {
1523059d84dbSTetsuo Handa 			char buf[128];
1524*cdcf6723STetsuo Handa 
1525059d84dbSTetsuo Handa 			tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1526059d84dbSTetsuo Handa 			tomoyo_io_printf(head, "%s", buf);
1527059d84dbSTetsuo Handa 		}
1528059d84dbSTetsuo Handa 		tomoyo_print_number_union(head, &ptr->port);
1529059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1530059d84dbSTetsuo Handa 		struct tomoyo_unix_acl *ptr =
1531059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1532059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1533059d84dbSTetsuo Handa 
1534059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1535059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1536059d84dbSTetsuo Handa 				continue;
1537059d84dbSTetsuo Handa 			if (first) {
1538059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network unix ");
1539059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1540059d84dbSTetsuo Handa 						  [ptr->protocol]);
1541059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1542059d84dbSTetsuo Handa 				first = false;
1543059d84dbSTetsuo Handa 			} else {
1544059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1545059d84dbSTetsuo Handa 			}
1546059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1547059d84dbSTetsuo Handa 		}
1548059d84dbSTetsuo Handa 		if (first)
1549059d84dbSTetsuo Handa 			return true;
1550059d84dbSTetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
15515db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15525db5a39bSTetsuo Handa 		struct tomoyo_mount_acl *ptr =
15535db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1554*cdcf6723STetsuo Handa 
15550d2171d7STetsuo Handa 		tomoyo_set_group(head, "file mount");
1556f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dev_name);
1557f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dir_name);
1558f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->fs_type);
1559f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->flags);
1560d58e0da8STetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1561d58e0da8STetsuo Handa 		struct tomoyo_env_acl *ptr =
1562d58e0da8STetsuo Handa 			container_of(acl, typeof(*ptr), head);
1563d58e0da8STetsuo Handa 
1564d58e0da8STetsuo Handa 		tomoyo_set_group(head, "misc env ");
1565d58e0da8STetsuo Handa 		tomoyo_set_string(head, ptr->env->name);
15669590837bSKentaro Takeda 	}
15672066a361STetsuo Handa 	if (acl->cond) {
15682066a361STetsuo Handa 		head->r.print_cond_part = true;
15692066a361STetsuo Handa 		head->r.cond_step = 0;
15702066a361STetsuo Handa 		if (!tomoyo_flush(head))
15712066a361STetsuo Handa 			return false;
15722066a361STetsuo Handa print_cond_part:
15732066a361STetsuo Handa 		if (!tomoyo_print_condition(head, acl->cond))
15742066a361STetsuo Handa 			return false;
15752066a361STetsuo Handa 		head->r.print_cond_part = false;
15762066a361STetsuo Handa 	} else {
15770d2171d7STetsuo Handa 		tomoyo_set_lf(head);
15782066a361STetsuo Handa 	}
15795db5a39bSTetsuo Handa 	return true;
1580f23571e8STetsuo Handa }
1581f23571e8STetsuo Handa 
1582f23571e8STetsuo Handa /**
1583f23571e8STetsuo Handa  * tomoyo_read_domain2 - Read domain policy.
1584f23571e8STetsuo Handa  *
1585f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
158632997144STetsuo Handa  * @list: Pointer to "struct list_head".
1587f23571e8STetsuo Handa  *
1588f23571e8STetsuo Handa  * Caller holds tomoyo_read_lock().
1589f23571e8STetsuo Handa  *
1590f23571e8STetsuo Handa  * Returns true on success, false otherwise.
1591f23571e8STetsuo Handa  */
1592f23571e8STetsuo Handa static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
159332997144STetsuo Handa 				struct list_head *list)
1594f23571e8STetsuo Handa {
159532997144STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1596f23571e8STetsuo Handa 		struct tomoyo_acl_info *ptr =
1597f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), list);
1598*cdcf6723STetsuo Handa 
1599f23571e8STetsuo Handa 		if (!tomoyo_print_entry(head, ptr))
16009590837bSKentaro Takeda 			return false;
16019590837bSKentaro Takeda 	}
1602f23571e8STetsuo Handa 	head->r.acl = NULL;
1603f23571e8STetsuo Handa 	return true;
1604f23571e8STetsuo Handa }
16059590837bSKentaro Takeda 
16069590837bSKentaro Takeda /**
1607e2bf6907STetsuo Handa  * tomoyo_read_domain - Read domain policy.
16089590837bSKentaro Takeda  *
16099590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16109590837bSKentaro Takeda  *
1611fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
16129590837bSKentaro Takeda  */
1613e2bf6907STetsuo Handa static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
16149590837bSKentaro Takeda {
1615f23571e8STetsuo Handa 	if (head->r.eof)
16168fbe71f0STetsuo Handa 		return;
1617f23571e8STetsuo Handa 	list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1618475e6fa3STetsuo Handa 		struct tomoyo_domain_info *domain =
1619f23571e8STetsuo Handa 			list_entry(head->r.domain, typeof(*domain), list);
16202c47ab93STetsuo Handa 		u8 i;
1621*cdcf6723STetsuo Handa 
1622*cdcf6723STetsuo Handa 		switch (head->r.step) {
1623f23571e8STetsuo Handa 		case 0:
1624f23571e8STetsuo Handa 			if (domain->is_deleted &&
1625f23571e8STetsuo Handa 			    !head->r.print_this_domain_only)
16269590837bSKentaro Takeda 				continue;
16279590837bSKentaro Takeda 			/* Print domainname and flags. */
1628f23571e8STetsuo Handa 			tomoyo_set_string(head, domain->domainname->name);
1629f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1630b5bc60b4STetsuo Handa 			tomoyo_io_printf(head, "use_profile %u\n",
1631f23571e8STetsuo Handa 					 domain->profile);
163232997144STetsuo Handa 			tomoyo_io_printf(head, "use_group %u\n",
163332997144STetsuo Handa 					 domain->group);
16342c47ab93STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
16352c47ab93STetsuo Handa 				if (domain->flags[i])
16362c47ab93STetsuo Handa 					tomoyo_set_string(head, tomoyo_dif[i]);
1637f23571e8STetsuo Handa 			head->r.step++;
1638f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1639f23571e8STetsuo Handa 			/* fall through */
1640f23571e8STetsuo Handa 		case 1:
164132997144STetsuo Handa 			if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1642f23571e8STetsuo Handa 				return;
1643f23571e8STetsuo Handa 			head->r.step++;
1644f23571e8STetsuo Handa 			if (!tomoyo_set_lf(head))
1645f23571e8STetsuo Handa 				return;
1646f23571e8STetsuo Handa 			/* fall through */
1647f23571e8STetsuo Handa 		case 2:
1648f23571e8STetsuo Handa 			head->r.step = 0;
1649f23571e8STetsuo Handa 			if (head->r.print_this_domain_only)
1650f23571e8STetsuo Handa 				goto done;
16519590837bSKentaro Takeda 		}
16529590837bSKentaro Takeda 	}
1653f23571e8STetsuo Handa  done:
1654f23571e8STetsuo Handa 	head->r.eof = true;
16559590837bSKentaro Takeda }
16569590837bSKentaro Takeda 
16579590837bSKentaro Takeda /**
16589590837bSKentaro Takeda  * tomoyo_write_pid: Specify PID to obtain domainname.
16599590837bSKentaro Takeda  *
16609590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16619590837bSKentaro Takeda  *
16629590837bSKentaro Takeda  * Returns 0.
16639590837bSKentaro Takeda  */
16649590837bSKentaro Takeda static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
16659590837bSKentaro Takeda {
1666f23571e8STetsuo Handa 	head->r.eof = false;
16679590837bSKentaro Takeda 	return 0;
16689590837bSKentaro Takeda }
16699590837bSKentaro Takeda 
16709590837bSKentaro Takeda /**
16719590837bSKentaro Takeda  * tomoyo_read_pid - Get domainname of the specified PID.
16729590837bSKentaro Takeda  *
16739590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16749590837bSKentaro Takeda  *
16759590837bSKentaro Takeda  * Returns the domainname which the specified PID is in on success,
16769590837bSKentaro Takeda  * empty string otherwise.
16779590837bSKentaro Takeda  * The PID is specified by tomoyo_write_pid() so that the user can obtain
16789590837bSKentaro Takeda  * using read()/write() interface rather than sysctl() interface.
16799590837bSKentaro Takeda  */
16808fbe71f0STetsuo Handa static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16819590837bSKentaro Takeda {
1682f23571e8STetsuo Handa 	char *buf = head->write_buf;
1683f23571e8STetsuo Handa 	bool global_pid = false;
1684f23571e8STetsuo Handa 	unsigned int pid;
16859590837bSKentaro Takeda 	struct task_struct *p;
16869590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
1687f23571e8STetsuo Handa 
1688f23571e8STetsuo Handa 	/* Accessing write_buf is safe because head->io_sem is held. */
1689f23571e8STetsuo Handa 	if (!buf) {
1690f23571e8STetsuo Handa 		head->r.eof = true;
1691f23571e8STetsuo Handa 		return; /* Do nothing if open(O_RDONLY). */
1692f23571e8STetsuo Handa 	}
1693f23571e8STetsuo Handa 	if (head->r.w_pos || head->r.eof)
1694f23571e8STetsuo Handa 		return;
1695f23571e8STetsuo Handa 	head->r.eof = true;
1696f23571e8STetsuo Handa 	if (tomoyo_str_starts(&buf, "global-pid "))
1697f23571e8STetsuo Handa 		global_pid = true;
1698dbdb75bdSDing Xiang 	if (kstrtouint(buf, 10, &pid))
1699dbdb75bdSDing Xiang 		return;
17001fcdc7c5STetsuo Handa 	rcu_read_lock();
1701f23571e8STetsuo Handa 	if (global_pid)
1702f23571e8STetsuo Handa 		p = find_task_by_pid_ns(pid, &init_pid_ns);
1703f23571e8STetsuo Handa 	else
17049590837bSKentaro Takeda 		p = find_task_by_vpid(pid);
17059590837bSKentaro Takeda 	if (p)
17068c6cb983STetsuo Handa 		domain = tomoyo_task(p)->domain_info;
17071fcdc7c5STetsuo Handa 	rcu_read_unlock();
1708f23571e8STetsuo Handa 	if (!domain)
1709f23571e8STetsuo Handa 		return;
1710f23571e8STetsuo Handa 	tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1711f23571e8STetsuo Handa 	tomoyo_set_string(head, domain->domainname->name);
17129590837bSKentaro Takeda }
17139590837bSKentaro Takeda 
17140f2a55d5STetsuo Handa /* String table for domain transition control keywords. */
17155448ec4fSTetsuo Handa static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1716bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
1717bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_RESET]         = "reset_domain ",
1718b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1719b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1720b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1721b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
17225448ec4fSTetsuo Handa };
17235448ec4fSTetsuo Handa 
17240f2a55d5STetsuo Handa /* String table for grouping keywords. */
1725e2bf6907STetsuo Handa static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1726b5bc60b4STetsuo Handa 	[TOMOYO_PATH_GROUP]    = "path_group ",
1727b5bc60b4STetsuo Handa 	[TOMOYO_NUMBER_GROUP]  = "number_group ",
1728059d84dbSTetsuo Handa 	[TOMOYO_ADDRESS_GROUP] = "address_group ",
1729e2bf6907STetsuo Handa };
1730e2bf6907STetsuo Handa 
17319590837bSKentaro Takeda /**
1732e2bf6907STetsuo Handa  * tomoyo_write_exception - Write exception policy.
17339590837bSKentaro Takeda  *
17349590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
17359590837bSKentaro Takeda  *
17369590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1737fdb8ebb7STetsuo Handa  *
1738fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
17399590837bSKentaro Takeda  */
1740e2bf6907STetsuo Handa static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
17419590837bSKentaro Takeda {
1742bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1743a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1744bd03a3e4STetsuo Handa 		.ns = head->w.ns,
1745bd03a3e4STetsuo Handa 		.is_delete = is_delete,
1746a238cf5bSTetsuo Handa 		.data = head->write_buf,
1747e2bf6907STetsuo Handa 	};
1748a238cf5bSTetsuo Handa 	u8 i;
1749*cdcf6723STetsuo Handa 
1750a238cf5bSTetsuo Handa 	if (tomoyo_str_starts(&param.data, "aggregator "))
1751a238cf5bSTetsuo Handa 		return tomoyo_write_aggregator(&param);
1752e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1753a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1754a238cf5bSTetsuo Handa 			return tomoyo_write_transition_control(&param, i);
1755e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1756a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1757a238cf5bSTetsuo Handa 			return tomoyo_write_group(&param, i);
175832997144STetsuo Handa 	if (tomoyo_str_starts(&param.data, "acl_group ")) {
175932997144STetsuo Handa 		unsigned int group;
176032997144STetsuo Handa 		char *data;
1761*cdcf6723STetsuo Handa 
176232997144STetsuo Handa 		group = simple_strtoul(param.data, &data, 10);
176332997144STetsuo Handa 		if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1764bd03a3e4STetsuo Handa 			return tomoyo_write_domain2
1765bd03a3e4STetsuo Handa 				(head->w.ns, &head->w.ns->acl_group[group],
1766bd03a3e4STetsuo Handa 				 data, is_delete);
176732997144STetsuo Handa 	}
17689590837bSKentaro Takeda 	return -EINVAL;
17699590837bSKentaro Takeda }
17709590837bSKentaro Takeda 
177131845e8cSTetsuo Handa /**
1772059d84dbSTetsuo Handa  * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
177331845e8cSTetsuo Handa  *
177431845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
177531845e8cSTetsuo Handa  * @idx:  Index number.
177631845e8cSTetsuo Handa  *
177731845e8cSTetsuo Handa  * Returns true on success, false otherwise.
177831845e8cSTetsuo Handa  *
177931845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
178031845e8cSTetsuo Handa  */
178131845e8cSTetsuo Handa static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
178231845e8cSTetsuo Handa {
1783bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1784bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1785bd03a3e4STetsuo Handa 	struct list_head *list = &ns->group_list[idx];
1786*cdcf6723STetsuo Handa 
1787bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.group, list) {
178831845e8cSTetsuo Handa 		struct tomoyo_group *group =
17890df7e8b8STetsuo Handa 			list_entry(head->r.group, typeof(*group), head.list);
1790*cdcf6723STetsuo Handa 
1791f23571e8STetsuo Handa 		list_for_each_cookie(head->r.acl, &group->member_list) {
179231845e8cSTetsuo Handa 			struct tomoyo_acl_head *ptr =
1793f23571e8STetsuo Handa 				list_entry(head->r.acl, typeof(*ptr), list);
1794*cdcf6723STetsuo Handa 
179531845e8cSTetsuo Handa 			if (ptr->is_deleted)
179631845e8cSTetsuo Handa 				continue;
1797f23571e8STetsuo Handa 			if (!tomoyo_flush(head))
179831845e8cSTetsuo Handa 				return false;
1799bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
1800f23571e8STetsuo Handa 			tomoyo_set_string(head, tomoyo_group_name[idx]);
1801f23571e8STetsuo Handa 			tomoyo_set_string(head, group->group_name->name);
1802f23571e8STetsuo Handa 			if (idx == TOMOYO_PATH_GROUP) {
1803f23571e8STetsuo Handa 				tomoyo_set_space(head);
1804f23571e8STetsuo Handa 				tomoyo_set_string(head, container_of
1805f23571e8STetsuo Handa 					       (ptr, struct tomoyo_path_group,
1806f23571e8STetsuo Handa 						head)->member_name->name);
1807f23571e8STetsuo Handa 			} else if (idx == TOMOYO_NUMBER_GROUP) {
1808f23571e8STetsuo Handa 				tomoyo_print_number_union(head, &container_of
1809f23571e8STetsuo Handa 							  (ptr,
1810f23571e8STetsuo Handa 						   struct tomoyo_number_group,
1811f23571e8STetsuo Handa 							   head)->number);
1812059d84dbSTetsuo Handa 			} else if (idx == TOMOYO_ADDRESS_GROUP) {
1813059d84dbSTetsuo Handa 				char buffer[128];
1814059d84dbSTetsuo Handa 				struct tomoyo_address_group *member =
1815059d84dbSTetsuo Handa 					container_of(ptr, typeof(*member),
1816059d84dbSTetsuo Handa 						     head);
1817*cdcf6723STetsuo Handa 
1818059d84dbSTetsuo Handa 				tomoyo_print_ip(buffer, sizeof(buffer),
1819059d84dbSTetsuo Handa 						&member->address);
1820059d84dbSTetsuo Handa 				tomoyo_io_printf(head, " %s", buffer);
182131845e8cSTetsuo Handa 			}
1822f23571e8STetsuo Handa 			tomoyo_set_lf(head);
182331845e8cSTetsuo Handa 		}
1824f23571e8STetsuo Handa 		head->r.acl = NULL;
1825f23571e8STetsuo Handa 	}
1826f23571e8STetsuo Handa 	head->r.group = NULL;
182731845e8cSTetsuo Handa 	return true;
182831845e8cSTetsuo Handa }
182931845e8cSTetsuo Handa 
183031845e8cSTetsuo Handa /**
183131845e8cSTetsuo Handa  * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
183231845e8cSTetsuo Handa  *
183331845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
183431845e8cSTetsuo Handa  * @idx:  Index number.
183531845e8cSTetsuo Handa  *
183631845e8cSTetsuo Handa  * Returns true on success, false otherwise.
183731845e8cSTetsuo Handa  *
183831845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
183931845e8cSTetsuo Handa  */
184031845e8cSTetsuo Handa static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
184131845e8cSTetsuo Handa {
1842bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1843bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1844bd03a3e4STetsuo Handa 	struct list_head *list = &ns->policy_list[idx];
1845*cdcf6723STetsuo Handa 
1846bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1847475e6fa3STetsuo Handa 		struct tomoyo_acl_head *acl =
1848f23571e8STetsuo Handa 			container_of(head->r.acl, typeof(*acl), list);
184931845e8cSTetsuo Handa 		if (acl->is_deleted)
185031845e8cSTetsuo Handa 			continue;
1851f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
1852f23571e8STetsuo Handa 			return false;
185331845e8cSTetsuo Handa 		switch (idx) {
18545448ec4fSTetsuo Handa 		case TOMOYO_ID_TRANSITION_CONTROL:
185531845e8cSTetsuo Handa 			{
18565448ec4fSTetsuo Handa 				struct tomoyo_transition_control *ptr =
185731845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1858*cdcf6723STetsuo Handa 
1859bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
18600d2171d7STetsuo Handa 				tomoyo_set_string(head, tomoyo_transition_type
1861f23571e8STetsuo Handa 						  [ptr->type]);
18620d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->program ?
18630d2171d7STetsuo Handa 						  ptr->program->name : "any");
1864f23571e8STetsuo Handa 				tomoyo_set_string(head, " from ");
18650d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->domainname ?
18660d2171d7STetsuo Handa 						  ptr->domainname->name :
18670d2171d7STetsuo Handa 						  "any");
186831845e8cSTetsuo Handa 			}
186931845e8cSTetsuo Handa 			break;
187031845e8cSTetsuo Handa 		case TOMOYO_ID_AGGREGATOR:
187131845e8cSTetsuo Handa 			{
1872e2bf6907STetsuo Handa 				struct tomoyo_aggregator *ptr =
187331845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1874*cdcf6723STetsuo Handa 
1875bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
1876b5bc60b4STetsuo Handa 				tomoyo_set_string(head, "aggregator ");
1877f23571e8STetsuo Handa 				tomoyo_set_string(head,
1878f23571e8STetsuo Handa 						  ptr->original_name->name);
1879f23571e8STetsuo Handa 				tomoyo_set_space(head);
1880f23571e8STetsuo Handa 				tomoyo_set_string(head,
1881f23571e8STetsuo Handa 					       ptr->aggregated_name->name);
188231845e8cSTetsuo Handa 			}
188331845e8cSTetsuo Handa 			break;
188431845e8cSTetsuo Handa 		default:
188531845e8cSTetsuo Handa 			continue;
188631845e8cSTetsuo Handa 		}
1887f23571e8STetsuo Handa 		tomoyo_set_lf(head);
188831845e8cSTetsuo Handa 	}
1889f23571e8STetsuo Handa 	head->r.acl = NULL;
189031845e8cSTetsuo Handa 	return true;
189131845e8cSTetsuo Handa }
189231845e8cSTetsuo Handa 
18939590837bSKentaro Takeda /**
1894e2bf6907STetsuo Handa  * tomoyo_read_exception - Read exception policy.
18959590837bSKentaro Takeda  *
18969590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
18979590837bSKentaro Takeda  *
1898fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
18999590837bSKentaro Takeda  */
1900e2bf6907STetsuo Handa static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
19019590837bSKentaro Takeda {
1902bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1903bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1904*cdcf6723STetsuo Handa 
1905f23571e8STetsuo Handa 	if (head->r.eof)
190631845e8cSTetsuo Handa 		return;
1907f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY &&
1908f23571e8STetsuo Handa 	       tomoyo_read_policy(head, head->r.step))
1909f23571e8STetsuo Handa 		head->r.step++;
1910f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY)
191131845e8cSTetsuo Handa 		return;
1912f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1913f23571e8STetsuo Handa 	       tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1914f23571e8STetsuo Handa 		head->r.step++;
1915f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
191631845e8cSTetsuo Handa 		return;
191732997144STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
191832997144STetsuo Handa 	       + TOMOYO_MAX_ACL_GROUPS) {
191932997144STetsuo Handa 		head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
192032997144STetsuo Handa 			- TOMOYO_MAX_GROUP;
1921bd03a3e4STetsuo Handa 		if (!tomoyo_read_domain2(head, &ns->acl_group
192232997144STetsuo Handa 					 [head->r.acl_group_index]))
192332997144STetsuo Handa 			return;
192432997144STetsuo Handa 		head->r.step++;
192532997144STetsuo Handa 	}
1926f23571e8STetsuo Handa 	head->r.eof = true;
19279590837bSKentaro Takeda }
19289590837bSKentaro Takeda 
1929eadd99ccSTetsuo Handa /* Wait queue for kernel -> userspace notification. */
193017fcfbd9STetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1931eadd99ccSTetsuo Handa /* Wait queue for userspace -> kernel notification. */
1932eadd99ccSTetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
193317fcfbd9STetsuo Handa 
193417fcfbd9STetsuo Handa /* Structure for query. */
1935e2bf6907STetsuo Handa struct tomoyo_query {
193617fcfbd9STetsuo Handa 	struct list_head list;
193759df3166STetsuo Handa 	struct tomoyo_domain_info *domain;
193817fcfbd9STetsuo Handa 	char *query;
1939eadd99ccSTetsuo Handa 	size_t query_len;
194017fcfbd9STetsuo Handa 	unsigned int serial;
1941eadd99ccSTetsuo Handa 	u8 timer;
1942eadd99ccSTetsuo Handa 	u8 answer;
1943eadd99ccSTetsuo Handa 	u8 retry;
194417fcfbd9STetsuo Handa };
194517fcfbd9STetsuo Handa 
1946e2bf6907STetsuo Handa /* The list for "struct tomoyo_query". */
194717fcfbd9STetsuo Handa static LIST_HEAD(tomoyo_query_list);
194817fcfbd9STetsuo Handa 
1949eadd99ccSTetsuo Handa /* Lock for manipulating tomoyo_query_list. */
1950eadd99ccSTetsuo Handa static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1951eadd99ccSTetsuo Handa 
195217fcfbd9STetsuo Handa /*
195317fcfbd9STetsuo Handa  * Number of "struct file" referring /sys/kernel/security/tomoyo/query
195417fcfbd9STetsuo Handa  * interface.
195517fcfbd9STetsuo Handa  */
195617fcfbd9STetsuo Handa static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
195717fcfbd9STetsuo Handa 
195817fcfbd9STetsuo Handa /**
19592ca9bf45STetsuo Handa  * tomoyo_truncate - Truncate a line.
19602ca9bf45STetsuo Handa  *
19612ca9bf45STetsuo Handa  * @str: String to truncate.
19622ca9bf45STetsuo Handa  *
19632ca9bf45STetsuo Handa  * Returns length of truncated @str.
19642ca9bf45STetsuo Handa  */
19652ca9bf45STetsuo Handa static int tomoyo_truncate(char *str)
19662ca9bf45STetsuo Handa {
19672ca9bf45STetsuo Handa 	char *start = str;
1968*cdcf6723STetsuo Handa 
19692ca9bf45STetsuo Handa 	while (*(unsigned char *) str > (unsigned char) ' ')
19702ca9bf45STetsuo Handa 		str++;
19712ca9bf45STetsuo Handa 	*str = '\0';
19722ca9bf45STetsuo Handa 	return strlen(start) + 1;
19732ca9bf45STetsuo Handa }
19742ca9bf45STetsuo Handa 
19752ca9bf45STetsuo Handa /**
1976eadd99ccSTetsuo Handa  * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1977eadd99ccSTetsuo Handa  *
1978eadd99ccSTetsuo Handa  * @domain: Pointer to "struct tomoyo_domain_info".
1979eadd99ccSTetsuo Handa  * @header: Lines containing ACL.
1980eadd99ccSTetsuo Handa  *
1981eadd99ccSTetsuo Handa  * Returns nothing.
1982eadd99ccSTetsuo Handa  */
1983eadd99ccSTetsuo Handa static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1984eadd99ccSTetsuo Handa {
1985eadd99ccSTetsuo Handa 	char *buffer;
19862ca9bf45STetsuo Handa 	char *realpath = NULL;
19875b636857STetsuo Handa 	char *argv0 = NULL;
19882ca9bf45STetsuo Handa 	char *symlink = NULL;
1989eadd99ccSTetsuo Handa 	char *cp = strchr(header, '\n');
1990eadd99ccSTetsuo Handa 	int len;
1991*cdcf6723STetsuo Handa 
1992eadd99ccSTetsuo Handa 	if (!cp)
1993eadd99ccSTetsuo Handa 		return;
1994eadd99ccSTetsuo Handa 	cp = strchr(cp + 1, '\n');
1995eadd99ccSTetsuo Handa 	if (!cp)
1996eadd99ccSTetsuo Handa 		return;
1997eadd99ccSTetsuo Handa 	*cp++ = '\0';
1998eadd99ccSTetsuo Handa 	len = strlen(cp) + 1;
19992ca9bf45STetsuo Handa 	/* strstr() will return NULL if ordering is wrong. */
20002ca9bf45STetsuo Handa 	if (*cp == 'f') {
20015b636857STetsuo Handa 		argv0 = strstr(header, " argv[]={ \"");
20025b636857STetsuo Handa 		if (argv0) {
20035b636857STetsuo Handa 			argv0 += 10;
20045b636857STetsuo Handa 			len += tomoyo_truncate(argv0) + 14;
20055b636857STetsuo Handa 		}
20062ca9bf45STetsuo Handa 		realpath = strstr(header, " exec={ realpath=\"");
20072ca9bf45STetsuo Handa 		if (realpath) {
20082ca9bf45STetsuo Handa 			realpath += 8;
20092ca9bf45STetsuo Handa 			len += tomoyo_truncate(realpath) + 6;
20102ca9bf45STetsuo Handa 		}
20112ca9bf45STetsuo Handa 		symlink = strstr(header, " symlink.target=\"");
20122ca9bf45STetsuo Handa 		if (symlink)
20132ca9bf45STetsuo Handa 			len += tomoyo_truncate(symlink + 1) + 1;
20142ca9bf45STetsuo Handa 	}
2015eadd99ccSTetsuo Handa 	buffer = kmalloc(len, GFP_NOFS);
2016eadd99ccSTetsuo Handa 	if (!buffer)
2017eadd99ccSTetsuo Handa 		return;
2018eadd99ccSTetsuo Handa 	snprintf(buffer, len - 1, "%s", cp);
20192ca9bf45STetsuo Handa 	if (realpath)
20202ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.%s", realpath);
20215b636857STetsuo Handa 	if (argv0)
20225b636857STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
20232ca9bf45STetsuo Handa 	if (symlink)
20242ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, "%s", symlink);
2025eadd99ccSTetsuo Handa 	tomoyo_normalize_line(buffer);
2026b22b8b9fSTetsuo Handa 	if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
2027b22b8b9fSTetsuo Handa 				  false))
2028b22b8b9fSTetsuo Handa 		tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2029eadd99ccSTetsuo Handa 	kfree(buffer);
2030eadd99ccSTetsuo Handa }
2031eadd99ccSTetsuo Handa 
2032eadd99ccSTetsuo Handa /**
203317fcfbd9STetsuo Handa  * tomoyo_supervisor - Ask for the supervisor's decision.
203417fcfbd9STetsuo Handa  *
203517fcfbd9STetsuo Handa  * @r:   Pointer to "struct tomoyo_request_info".
203617fcfbd9STetsuo Handa  * @fmt: The printf()'s format string, followed by parameters.
203717fcfbd9STetsuo Handa  *
203817fcfbd9STetsuo Handa  * Returns 0 if the supervisor decided to permit the access request which
203917fcfbd9STetsuo Handa  * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
204017fcfbd9STetsuo Handa  * supervisor decided to retry the access request which violated the policy in
204117fcfbd9STetsuo Handa  * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
204217fcfbd9STetsuo Handa  */
204317fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
204417fcfbd9STetsuo Handa {
204517fcfbd9STetsuo Handa 	va_list args;
2046eadd99ccSTetsuo Handa 	int error;
204717fcfbd9STetsuo Handa 	int len;
204817fcfbd9STetsuo Handa 	static unsigned int tomoyo_serial;
2049eadd99ccSTetsuo Handa 	struct tomoyo_query entry = { };
205017fcfbd9STetsuo Handa 	bool quota_exceeded = false;
2051*cdcf6723STetsuo Handa 
2052eadd99ccSTetsuo Handa 	va_start(args, fmt);
2053eadd99ccSTetsuo Handa 	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2054eadd99ccSTetsuo Handa 	va_end(args);
2055eadd99ccSTetsuo Handa 	/* Write /sys/kernel/security/tomoyo/audit. */
2056eadd99ccSTetsuo Handa 	va_start(args, fmt);
2057eadd99ccSTetsuo Handa 	tomoyo_write_log2(r, len, fmt, args);
2058eadd99ccSTetsuo Handa 	va_end(args);
2059eadd99ccSTetsuo Handa 	/* Nothing more to do if granted. */
2060eadd99ccSTetsuo Handa 	if (r->granted)
2061eadd99ccSTetsuo Handa 		return 0;
2062b22b8b9fSTetsuo Handa 	if (r->mode)
2063b22b8b9fSTetsuo Handa 		tomoyo_update_stat(r->mode);
206417fcfbd9STetsuo Handa 	switch (r->mode) {
2065eadd99ccSTetsuo Handa 	case TOMOYO_CONFIG_ENFORCING:
2066eadd99ccSTetsuo Handa 		error = -EPERM;
2067eadd99ccSTetsuo Handa 		if (atomic_read(&tomoyo_query_observers))
2068eadd99ccSTetsuo Handa 			break;
2069eadd99ccSTetsuo Handa 		goto out;
207017fcfbd9STetsuo Handa 	case TOMOYO_CONFIG_LEARNING:
2071eadd99ccSTetsuo Handa 		error = 0;
2072eadd99ccSTetsuo Handa 		/* Check max_learning_entry parameter. */
2073eadd99ccSTetsuo Handa 		if (tomoyo_domain_quota_is_ok(r))
2074eadd99ccSTetsuo Handa 			break;
207517fcfbd9STetsuo Handa 		/* fall through */
2076eadd99ccSTetsuo Handa 	default:
207717fcfbd9STetsuo Handa 		return 0;
207817fcfbd9STetsuo Handa 	}
2079eadd99ccSTetsuo Handa 	/* Get message. */
208017fcfbd9STetsuo Handa 	va_start(args, fmt);
2081eadd99ccSTetsuo Handa 	entry.query = tomoyo_init_log(r, len, fmt, args);
208217fcfbd9STetsuo Handa 	va_end(args);
2083eadd99ccSTetsuo Handa 	if (!entry.query)
208417fcfbd9STetsuo Handa 		goto out;
2085eadd99ccSTetsuo Handa 	entry.query_len = strlen(entry.query) + 1;
2086eadd99ccSTetsuo Handa 	if (!error) {
2087eadd99ccSTetsuo Handa 		tomoyo_add_entry(r->domain, entry.query);
208817fcfbd9STetsuo Handa 		goto out;
2089eadd99ccSTetsuo Handa 	}
2090eadd99ccSTetsuo Handa 	len = tomoyo_round2(entry.query_len);
209159df3166STetsuo Handa 	entry.domain = r->domain;
209217fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2093eadd99ccSTetsuo Handa 	if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2094eadd99ccSTetsuo Handa 	    tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2095eadd99ccSTetsuo Handa 	    >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
209617fcfbd9STetsuo Handa 		quota_exceeded = true;
209717fcfbd9STetsuo Handa 	} else {
2098eadd99ccSTetsuo Handa 		entry.serial = tomoyo_serial++;
2099eadd99ccSTetsuo Handa 		entry.retry = r->retry;
2100eadd99ccSTetsuo Handa 		tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2101eadd99ccSTetsuo Handa 		list_add_tail(&entry.list, &tomoyo_query_list);
210217fcfbd9STetsuo Handa 	}
210317fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
210417fcfbd9STetsuo Handa 	if (quota_exceeded)
210517fcfbd9STetsuo Handa 		goto out;
210617fcfbd9STetsuo Handa 	/* Give 10 seconds for supervisor's opinion. */
2107eadd99ccSTetsuo Handa 	while (entry.timer < 10) {
2108eadd99ccSTetsuo Handa 		wake_up_all(&tomoyo_query_wait);
2109eadd99ccSTetsuo Handa 		if (wait_event_interruptible_timeout
2110eadd99ccSTetsuo Handa 		    (tomoyo_answer_wait, entry.answer ||
2111eadd99ccSTetsuo Handa 		     !atomic_read(&tomoyo_query_observers), HZ))
211217fcfbd9STetsuo Handa 			break;
2113eadd99ccSTetsuo Handa 		entry.timer++;
211417fcfbd9STetsuo Handa 	}
211517fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2116eadd99ccSTetsuo Handa 	list_del(&entry.list);
2117eadd99ccSTetsuo Handa 	tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
211817fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2119eadd99ccSTetsuo Handa 	switch (entry.answer) {
212017fcfbd9STetsuo Handa 	case 3: /* Asked to retry by administrator. */
212117fcfbd9STetsuo Handa 		error = TOMOYO_RETRY_REQUEST;
212217fcfbd9STetsuo Handa 		r->retry++;
212317fcfbd9STetsuo Handa 		break;
212417fcfbd9STetsuo Handa 	case 1:
212517fcfbd9STetsuo Handa 		/* Granted by administrator. */
212617fcfbd9STetsuo Handa 		error = 0;
212717fcfbd9STetsuo Handa 		break;
212817fcfbd9STetsuo Handa 	default:
2129eadd99ccSTetsuo Handa 		/* Timed out or rejected by administrator. */
213017fcfbd9STetsuo Handa 		break;
213117fcfbd9STetsuo Handa 	}
213217fcfbd9STetsuo Handa out:
2133eadd99ccSTetsuo Handa 	kfree(entry.query);
213417fcfbd9STetsuo Handa 	return error;
213517fcfbd9STetsuo Handa }
213617fcfbd9STetsuo Handa 
213717fcfbd9STetsuo Handa /**
213859df3166STetsuo Handa  * tomoyo_find_domain_by_qid - Get domain by query id.
213959df3166STetsuo Handa  *
214059df3166STetsuo Handa  * @serial: Query ID assigned by tomoyo_supervisor().
214159df3166STetsuo Handa  *
214259df3166STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
214359df3166STetsuo Handa  */
214459df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
214559df3166STetsuo Handa (unsigned int serial)
214659df3166STetsuo Handa {
214759df3166STetsuo Handa 	struct tomoyo_query *ptr;
214859df3166STetsuo Handa 	struct tomoyo_domain_info *domain = NULL;
2149*cdcf6723STetsuo Handa 
215059df3166STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
215159df3166STetsuo Handa 	list_for_each_entry(ptr, &tomoyo_query_list, list) {
21526041e834STetsuo Handa 		if (ptr->serial != serial)
215359df3166STetsuo Handa 			continue;
215459df3166STetsuo Handa 		domain = ptr->domain;
215559df3166STetsuo Handa 		break;
215659df3166STetsuo Handa 	}
215759df3166STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
215859df3166STetsuo Handa 	return domain;
215959df3166STetsuo Handa }
216059df3166STetsuo Handa 
216159df3166STetsuo Handa /**
216217fcfbd9STetsuo Handa  * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
216317fcfbd9STetsuo Handa  *
216417fcfbd9STetsuo Handa  * @file: Pointer to "struct file".
216517fcfbd9STetsuo Handa  * @wait: Pointer to "poll_table".
216617fcfbd9STetsuo Handa  *
2167a9a08845SLinus Torvalds  * Returns EPOLLIN | EPOLLRDNORM when ready to read, 0 otherwise.
216817fcfbd9STetsuo Handa  *
216917fcfbd9STetsuo Handa  * Waits for access requests which violated policy in enforcing mode.
217017fcfbd9STetsuo Handa  */
2171c0d4be28SAl Viro static __poll_t tomoyo_poll_query(struct file *file, poll_table *wait)
217217fcfbd9STetsuo Handa {
21736041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
2174a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
217517fcfbd9STetsuo Handa 	poll_wait(file, &tomoyo_query_wait, wait);
21766041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
2177a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
217817fcfbd9STetsuo Handa 	return 0;
217917fcfbd9STetsuo Handa }
218017fcfbd9STetsuo Handa 
218117fcfbd9STetsuo Handa /**
218217fcfbd9STetsuo Handa  * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
218317fcfbd9STetsuo Handa  *
218417fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
218517fcfbd9STetsuo Handa  */
21868fbe71f0STetsuo Handa static void tomoyo_read_query(struct tomoyo_io_buffer *head)
218717fcfbd9STetsuo Handa {
218817fcfbd9STetsuo Handa 	struct list_head *tmp;
21892c47ab93STetsuo Handa 	unsigned int pos = 0;
21902c47ab93STetsuo Handa 	size_t len = 0;
219117fcfbd9STetsuo Handa 	char *buf;
2192*cdcf6723STetsuo Handa 
2193f23571e8STetsuo Handa 	if (head->r.w_pos)
21948fbe71f0STetsuo Handa 		return;
219517fcfbd9STetsuo Handa 	kfree(head->read_buf);
219617fcfbd9STetsuo Handa 	head->read_buf = NULL;
219717fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
219817fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2199e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2200*cdcf6723STetsuo Handa 
2201f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
220217fcfbd9STetsuo Handa 			continue;
220317fcfbd9STetsuo Handa 		len = ptr->query_len;
220417fcfbd9STetsuo Handa 		break;
220517fcfbd9STetsuo Handa 	}
220617fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
220717fcfbd9STetsuo Handa 	if (!len) {
2208f23571e8STetsuo Handa 		head->r.query_index = 0;
22098fbe71f0STetsuo Handa 		return;
221017fcfbd9STetsuo Handa 	}
2211eadd99ccSTetsuo Handa 	buf = kzalloc(len + 32, GFP_NOFS);
221217fcfbd9STetsuo Handa 	if (!buf)
22138fbe71f0STetsuo Handa 		return;
221417fcfbd9STetsuo Handa 	pos = 0;
221517fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
221617fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2217e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2218*cdcf6723STetsuo Handa 
2219f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
222017fcfbd9STetsuo Handa 			continue;
222117fcfbd9STetsuo Handa 		/*
222217fcfbd9STetsuo Handa 		 * Some query can be skipped because tomoyo_query_list
222317fcfbd9STetsuo Handa 		 * can change, but I don't care.
222417fcfbd9STetsuo Handa 		 */
222517fcfbd9STetsuo Handa 		if (len == ptr->query_len)
2226eadd99ccSTetsuo Handa 			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2227eadd99ccSTetsuo Handa 				 ptr->retry, ptr->query);
222817fcfbd9STetsuo Handa 		break;
222917fcfbd9STetsuo Handa 	}
223017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
223117fcfbd9STetsuo Handa 	if (buf[0]) {
223217fcfbd9STetsuo Handa 		head->read_buf = buf;
2233f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = buf;
2234f23571e8STetsuo Handa 		head->r.query_index++;
223517fcfbd9STetsuo Handa 	} else {
223617fcfbd9STetsuo Handa 		kfree(buf);
223717fcfbd9STetsuo Handa 	}
223817fcfbd9STetsuo Handa }
223917fcfbd9STetsuo Handa 
224017fcfbd9STetsuo Handa /**
224117fcfbd9STetsuo Handa  * tomoyo_write_answer - Write the supervisor's decision.
224217fcfbd9STetsuo Handa  *
224317fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
224417fcfbd9STetsuo Handa  *
224517fcfbd9STetsuo Handa  * Returns 0 on success, -EINVAL otherwise.
224617fcfbd9STetsuo Handa  */
224717fcfbd9STetsuo Handa static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
224817fcfbd9STetsuo Handa {
224917fcfbd9STetsuo Handa 	char *data = head->write_buf;
225017fcfbd9STetsuo Handa 	struct list_head *tmp;
225117fcfbd9STetsuo Handa 	unsigned int serial;
225217fcfbd9STetsuo Handa 	unsigned int answer;
2253*cdcf6723STetsuo Handa 
225417fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
225517fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2256e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2257*cdcf6723STetsuo Handa 
225817fcfbd9STetsuo Handa 		ptr->timer = 0;
225917fcfbd9STetsuo Handa 	}
226017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
226117fcfbd9STetsuo Handa 	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
226217fcfbd9STetsuo Handa 		return -EINVAL;
226317fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
226417fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2265e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2266*cdcf6723STetsuo Handa 
226717fcfbd9STetsuo Handa 		if (ptr->serial != serial)
226817fcfbd9STetsuo Handa 			continue;
226917fcfbd9STetsuo Handa 		ptr->answer = answer;
22706041e834STetsuo Handa 		/* Remove from tomoyo_query_list. */
22716041e834STetsuo Handa 		if (ptr->answer)
22726041e834STetsuo Handa 			list_del_init(&ptr->list);
227317fcfbd9STetsuo Handa 		break;
227417fcfbd9STetsuo Handa 	}
227517fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
227617fcfbd9STetsuo Handa 	return 0;
227717fcfbd9STetsuo Handa }
227817fcfbd9STetsuo Handa 
227917fcfbd9STetsuo Handa /**
22809590837bSKentaro Takeda  * tomoyo_read_version: Get version.
22819590837bSKentaro Takeda  *
22829590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
22839590837bSKentaro Takeda  *
22849590837bSKentaro Takeda  * Returns version information.
22859590837bSKentaro Takeda  */
22868fbe71f0STetsuo Handa static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22879590837bSKentaro Takeda {
2288f23571e8STetsuo Handa 	if (!head->r.eof) {
2289843d183cSTetsuo Handa 		tomoyo_io_printf(head, "2.5.0");
2290f23571e8STetsuo Handa 		head->r.eof = true;
22919590837bSKentaro Takeda 	}
22929590837bSKentaro Takeda }
22939590837bSKentaro Takeda 
2294b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2295b22b8b9fSTetsuo Handa static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2296b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_UPDATES]    = "update:",
2297b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2298b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2299b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2300b22b8b9fSTetsuo Handa };
2301b22b8b9fSTetsuo Handa 
2302b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2303b22b8b9fSTetsuo Handa static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2304b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_POLICY] = "policy:",
2305b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_AUDIT]  = "audit log:",
2306b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_QUERY]  = "query message:",
2307b22b8b9fSTetsuo Handa };
2308b22b8b9fSTetsuo Handa 
2309b22b8b9fSTetsuo Handa /* Timestamp counter for last updated. */
2310b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2311b22b8b9fSTetsuo Handa /* Counter for number of updates. */
231292734092SArnd Bergmann static time64_t tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2313b22b8b9fSTetsuo Handa 
2314b22b8b9fSTetsuo Handa /**
2315b22b8b9fSTetsuo Handa  * tomoyo_update_stat - Update statistic counters.
2316b22b8b9fSTetsuo Handa  *
2317b22b8b9fSTetsuo Handa  * @index: Index for policy type.
2318b22b8b9fSTetsuo Handa  *
2319b22b8b9fSTetsuo Handa  * Returns nothing.
2320b22b8b9fSTetsuo Handa  */
2321b22b8b9fSTetsuo Handa void tomoyo_update_stat(const u8 index)
2322b22b8b9fSTetsuo Handa {
2323b22b8b9fSTetsuo Handa 	/*
2324b22b8b9fSTetsuo Handa 	 * I don't use atomic operations because race condition is not fatal.
2325b22b8b9fSTetsuo Handa 	 */
2326b22b8b9fSTetsuo Handa 	tomoyo_stat_updated[index]++;
232792734092SArnd Bergmann 	tomoyo_stat_modified[index] = ktime_get_real_seconds();
2328b22b8b9fSTetsuo Handa }
2329b22b8b9fSTetsuo Handa 
2330b22b8b9fSTetsuo Handa /**
2331b22b8b9fSTetsuo Handa  * tomoyo_read_stat - Read statistic data.
2332b22b8b9fSTetsuo Handa  *
2333b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2334b22b8b9fSTetsuo Handa  *
2335b22b8b9fSTetsuo Handa  * Returns nothing.
2336b22b8b9fSTetsuo Handa  */
2337b22b8b9fSTetsuo Handa static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2338b22b8b9fSTetsuo Handa {
2339b22b8b9fSTetsuo Handa 	u8 i;
2340b22b8b9fSTetsuo Handa 	unsigned int total = 0;
2341*cdcf6723STetsuo Handa 
2342b22b8b9fSTetsuo Handa 	if (head->r.eof)
2343b22b8b9fSTetsuo Handa 		return;
2344b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2345b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Policy %-30s %10u",
2346b22b8b9fSTetsuo Handa 				 tomoyo_policy_headers[i],
2347b22b8b9fSTetsuo Handa 				 tomoyo_stat_updated[i]);
2348b22b8b9fSTetsuo Handa 		if (tomoyo_stat_modified[i]) {
2349b22b8b9fSTetsuo Handa 			struct tomoyo_time stamp;
2350*cdcf6723STetsuo Handa 
2351b22b8b9fSTetsuo Handa 			tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2352*cdcf6723STetsuo Handa 			tomoyo_io_printf(head, " (Last: %04u/%02u/%02u %02u:%02u:%02u)",
2353b22b8b9fSTetsuo Handa 					 stamp.year, stamp.month, stamp.day,
2354b22b8b9fSTetsuo Handa 					 stamp.hour, stamp.min, stamp.sec);
2355b22b8b9fSTetsuo Handa 		}
2356b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2357b22b8b9fSTetsuo Handa 	}
2358b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2359b22b8b9fSTetsuo Handa 		unsigned int used = tomoyo_memory_used[i];
2360*cdcf6723STetsuo Handa 
2361b22b8b9fSTetsuo Handa 		total += used;
2362b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Memory used by %-22s %10u",
2363b22b8b9fSTetsuo Handa 				 tomoyo_memory_headers[i], used);
2364b22b8b9fSTetsuo Handa 		used = tomoyo_memory_quota[i];
2365b22b8b9fSTetsuo Handa 		if (used)
2366b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Quota: %10u)", used);
2367b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2368b22b8b9fSTetsuo Handa 	}
2369b22b8b9fSTetsuo Handa 	tomoyo_io_printf(head, "Total memory used:                    %10u\n",
2370b22b8b9fSTetsuo Handa 			 total);
2371b22b8b9fSTetsuo Handa 	head->r.eof = true;
2372b22b8b9fSTetsuo Handa }
2373b22b8b9fSTetsuo Handa 
2374b22b8b9fSTetsuo Handa /**
2375b22b8b9fSTetsuo Handa  * tomoyo_write_stat - Set memory quota.
2376b22b8b9fSTetsuo Handa  *
2377b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2378b22b8b9fSTetsuo Handa  *
2379b22b8b9fSTetsuo Handa  * Returns 0.
2380b22b8b9fSTetsuo Handa  */
2381b22b8b9fSTetsuo Handa static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2382b22b8b9fSTetsuo Handa {
2383b22b8b9fSTetsuo Handa 	char *data = head->write_buf;
2384b22b8b9fSTetsuo Handa 	u8 i;
2385*cdcf6723STetsuo Handa 
2386b22b8b9fSTetsuo Handa 	if (tomoyo_str_starts(&data, "Memory used by "))
2387b22b8b9fSTetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2388b22b8b9fSTetsuo Handa 			if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2389b22b8b9fSTetsuo Handa 				sscanf(data, "%u", &tomoyo_memory_quota[i]);
2390b22b8b9fSTetsuo Handa 	return 0;
2391b22b8b9fSTetsuo Handa }
2392b22b8b9fSTetsuo Handa 
23939590837bSKentaro Takeda /**
23949590837bSKentaro Takeda  * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
23959590837bSKentaro Takeda  *
23969590837bSKentaro Takeda  * @type: Type of interface.
23979590837bSKentaro Takeda  * @file: Pointer to "struct file".
23989590837bSKentaro Takeda  *
23992e503bbbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
24009590837bSKentaro Takeda  */
2401c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file)
24029590837bSKentaro Takeda {
24034e5d6f7eSTetsuo Handa 	struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
24049590837bSKentaro Takeda 
24059590837bSKentaro Takeda 	if (!head)
24069590837bSKentaro Takeda 		return -ENOMEM;
24079590837bSKentaro Takeda 	mutex_init(&head->io_sem);
240817fcfbd9STetsuo Handa 	head->type = type;
24099590837bSKentaro Takeda 	switch (type) {
24109590837bSKentaro Takeda 	case TOMOYO_DOMAINPOLICY:
24119590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/domain_policy */
2412e2bf6907STetsuo Handa 		head->write = tomoyo_write_domain;
2413e2bf6907STetsuo Handa 		head->read = tomoyo_read_domain;
24149590837bSKentaro Takeda 		break;
24159590837bSKentaro Takeda 	case TOMOYO_EXCEPTIONPOLICY:
24169590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/exception_policy */
2417e2bf6907STetsuo Handa 		head->write = tomoyo_write_exception;
2418e2bf6907STetsuo Handa 		head->read = tomoyo_read_exception;
24199590837bSKentaro Takeda 		break;
2420eadd99ccSTetsuo Handa 	case TOMOYO_AUDIT:
2421eadd99ccSTetsuo Handa 		/* /sys/kernel/security/tomoyo/audit */
2422eadd99ccSTetsuo Handa 		head->poll = tomoyo_poll_log;
2423eadd99ccSTetsuo Handa 		head->read = tomoyo_read_log;
2424eadd99ccSTetsuo Handa 		break;
24259590837bSKentaro Takeda 	case TOMOYO_PROCESS_STATUS:
24269590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/.process_status */
24279590837bSKentaro Takeda 		head->write = tomoyo_write_pid;
24289590837bSKentaro Takeda 		head->read = tomoyo_read_pid;
24299590837bSKentaro Takeda 		break;
24309590837bSKentaro Takeda 	case TOMOYO_VERSION:
24319590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/version */
24329590837bSKentaro Takeda 		head->read = tomoyo_read_version;
24339590837bSKentaro Takeda 		head->readbuf_size = 128;
24349590837bSKentaro Takeda 		break;
2435b22b8b9fSTetsuo Handa 	case TOMOYO_STAT:
2436b22b8b9fSTetsuo Handa 		/* /sys/kernel/security/tomoyo/stat */
2437b22b8b9fSTetsuo Handa 		head->write = tomoyo_write_stat;
2438b22b8b9fSTetsuo Handa 		head->read = tomoyo_read_stat;
2439b22b8b9fSTetsuo Handa 		head->readbuf_size = 1024;
24409590837bSKentaro Takeda 		break;
24419590837bSKentaro Takeda 	case TOMOYO_PROFILE:
24429590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/profile */
24439590837bSKentaro Takeda 		head->write = tomoyo_write_profile;
24449590837bSKentaro Takeda 		head->read = tomoyo_read_profile;
24459590837bSKentaro Takeda 		break;
244617fcfbd9STetsuo Handa 	case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
244717fcfbd9STetsuo Handa 		head->poll = tomoyo_poll_query;
244817fcfbd9STetsuo Handa 		head->write = tomoyo_write_answer;
244917fcfbd9STetsuo Handa 		head->read = tomoyo_read_query;
245017fcfbd9STetsuo Handa 		break;
24519590837bSKentaro Takeda 	case TOMOYO_MANAGER:
24529590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/manager */
2453e2bf6907STetsuo Handa 		head->write = tomoyo_write_manager;
2454e2bf6907STetsuo Handa 		head->read = tomoyo_read_manager;
24559590837bSKentaro Takeda 		break;
24569590837bSKentaro Takeda 	}
24579590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_READ)) {
24589590837bSKentaro Takeda 		/*
24599590837bSKentaro Takeda 		 * No need to allocate read_buf since it is not opened
24609590837bSKentaro Takeda 		 * for reading.
24619590837bSKentaro Takeda 		 */
24629590837bSKentaro Takeda 		head->read = NULL;
246317fcfbd9STetsuo Handa 		head->poll = NULL;
246417fcfbd9STetsuo Handa 	} else if (!head->poll) {
246517fcfbd9STetsuo Handa 		/* Don't allocate read_buf for poll() access. */
24669590837bSKentaro Takeda 		if (!head->readbuf_size)
24679590837bSKentaro Takeda 			head->readbuf_size = 4096 * 2;
24684e5d6f7eSTetsuo Handa 		head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
24699590837bSKentaro Takeda 		if (!head->read_buf) {
24708e2d39a1STetsuo Handa 			kfree(head);
24719590837bSKentaro Takeda 			return -ENOMEM;
24729590837bSKentaro Takeda 		}
24739590837bSKentaro Takeda 	}
24749590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_WRITE)) {
24759590837bSKentaro Takeda 		/*
24769590837bSKentaro Takeda 		 * No need to allocate write_buf since it is not opened
24779590837bSKentaro Takeda 		 * for writing.
24789590837bSKentaro Takeda 		 */
24799590837bSKentaro Takeda 		head->write = NULL;
24809590837bSKentaro Takeda 	} else if (head->write) {
24819590837bSKentaro Takeda 		head->writebuf_size = 4096 * 2;
24824e5d6f7eSTetsuo Handa 		head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
24839590837bSKentaro Takeda 		if (!head->write_buf) {
24848e2d39a1STetsuo Handa 			kfree(head->read_buf);
24858e2d39a1STetsuo Handa 			kfree(head);
24869590837bSKentaro Takeda 			return -ENOMEM;
24879590837bSKentaro Takeda 		}
24889590837bSKentaro Takeda 	}
24899590837bSKentaro Takeda 	/*
249017fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , increment the
249117fcfbd9STetsuo Handa 	 * observer counter.
249217fcfbd9STetsuo Handa 	 * The obserber counter is used by tomoyo_supervisor() to see if
249317fcfbd9STetsuo Handa 	 * there is some process monitoring /sys/kernel/security/tomoyo/query.
249417fcfbd9STetsuo Handa 	 */
24957c75964fSTetsuo Handa 	if (type == TOMOYO_QUERY)
249617fcfbd9STetsuo Handa 		atomic_inc(&tomoyo_query_observers);
24972e503bbbSTetsuo Handa 	file->private_data = head;
24982e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, true);
24999590837bSKentaro Takeda 	return 0;
25009590837bSKentaro Takeda }
25019590837bSKentaro Takeda 
25029590837bSKentaro Takeda /**
25030849e3baSTetsuo Handa  * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
25040849e3baSTetsuo Handa  *
25050849e3baSTetsuo Handa  * @file: Pointer to "struct file".
25066041e834STetsuo Handa  * @wait: Pointer to "poll_table". Maybe NULL.
25070849e3baSTetsuo Handa  *
2508a9a08845SLinus Torvalds  * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
2509a9a08845SLinus Torvalds  * EPOLLOUT | EPOLLWRNORM otherwise.
25100849e3baSTetsuo Handa  */
2511c0d4be28SAl Viro __poll_t tomoyo_poll_control(struct file *file, poll_table *wait)
25120849e3baSTetsuo Handa {
25130849e3baSTetsuo Handa 	struct tomoyo_io_buffer *head = file->private_data;
2514*cdcf6723STetsuo Handa 
25156041e834STetsuo Handa 	if (head->poll)
2516a9a08845SLinus Torvalds 		return head->poll(file, wait) | EPOLLOUT | EPOLLWRNORM;
2517a9a08845SLinus Torvalds 	return EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM;
25180849e3baSTetsuo Handa }
25190849e3baSTetsuo Handa 
25200849e3baSTetsuo Handa /**
2521bd03a3e4STetsuo Handa  * tomoyo_set_namespace_cursor - Set namespace to read.
2522bd03a3e4STetsuo Handa  *
2523bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2524bd03a3e4STetsuo Handa  *
2525bd03a3e4STetsuo Handa  * Returns nothing.
2526bd03a3e4STetsuo Handa  */
2527bd03a3e4STetsuo Handa static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2528bd03a3e4STetsuo Handa {
2529bd03a3e4STetsuo Handa 	struct list_head *ns;
2530*cdcf6723STetsuo Handa 
2531bd03a3e4STetsuo Handa 	if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2532bd03a3e4STetsuo Handa 	    head->type != TOMOYO_PROFILE)
2533bd03a3e4STetsuo Handa 		return;
2534bd03a3e4STetsuo Handa 	/*
2535bd03a3e4STetsuo Handa 	 * If this is the first read, or reading previous namespace finished
2536bd03a3e4STetsuo Handa 	 * and has more namespaces to read, update the namespace cursor.
2537bd03a3e4STetsuo Handa 	 */
2538bd03a3e4STetsuo Handa 	ns = head->r.ns;
2539bd03a3e4STetsuo Handa 	if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2540bd03a3e4STetsuo Handa 		/* Clearing is OK because tomoyo_flush() returned true. */
2541bd03a3e4STetsuo Handa 		memset(&head->r, 0, sizeof(head->r));
2542bd03a3e4STetsuo Handa 		head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2543bd03a3e4STetsuo Handa 	}
2544bd03a3e4STetsuo Handa }
2545bd03a3e4STetsuo Handa 
2546bd03a3e4STetsuo Handa /**
2547bd03a3e4STetsuo Handa  * tomoyo_has_more_namespace - Check for unread namespaces.
2548bd03a3e4STetsuo Handa  *
2549bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2550bd03a3e4STetsuo Handa  *
2551bd03a3e4STetsuo Handa  * Returns true if we have more entries to print, false otherwise.
2552bd03a3e4STetsuo Handa  */
2553bd03a3e4STetsuo Handa static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2554bd03a3e4STetsuo Handa {
2555bd03a3e4STetsuo Handa 	return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2556bd03a3e4STetsuo Handa 		head->type == TOMOYO_PROFILE) && head->r.eof &&
2557bd03a3e4STetsuo Handa 		head->r.ns->next != &tomoyo_namespace_list;
2558bd03a3e4STetsuo Handa }
2559bd03a3e4STetsuo Handa 
2560bd03a3e4STetsuo Handa /**
25619590837bSKentaro Takeda  * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
25629590837bSKentaro Takeda  *
25630df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25649590837bSKentaro Takeda  * @buffer:     Poiner to buffer to write to.
25659590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25669590837bSKentaro Takeda  *
25679590837bSKentaro Takeda  * Returns bytes read on success, negative value otherwise.
25689590837bSKentaro Takeda  */
25692c47ab93STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
25709590837bSKentaro Takeda 			    const int buffer_len)
25719590837bSKentaro Takeda {
2572f23571e8STetsuo Handa 	int len;
25732e503bbbSTetsuo Handa 	int idx;
25749590837bSKentaro Takeda 
25759590837bSKentaro Takeda 	if (!head->read)
2576*cdcf6723STetsuo Handa 		return -EINVAL;
25779590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25789590837bSKentaro Takeda 		return -EINTR;
2579f23571e8STetsuo Handa 	head->read_user_buf = buffer;
2580f23571e8STetsuo Handa 	head->read_user_buf_avail = buffer_len;
25812e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
2582f23571e8STetsuo Handa 	if (tomoyo_flush(head))
25839590837bSKentaro Takeda 		/* Call the policy handler. */
2584bd03a3e4STetsuo Handa 		do {
2585bd03a3e4STetsuo Handa 			tomoyo_set_namespace_cursor(head);
25868fbe71f0STetsuo Handa 			head->read(head);
2587bd03a3e4STetsuo Handa 		} while (tomoyo_flush(head) &&
2588bd03a3e4STetsuo Handa 			 tomoyo_has_more_namespace(head));
25892e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
2590f23571e8STetsuo Handa 	len = head->read_user_buf - buffer;
25919590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
25929590837bSKentaro Takeda 	return len;
25939590837bSKentaro Takeda }
25949590837bSKentaro Takeda 
25959590837bSKentaro Takeda /**
2596bd03a3e4STetsuo Handa  * tomoyo_parse_policy - Parse a policy line.
2597bd03a3e4STetsuo Handa  *
2598bd03a3e4STetsuo Handa  * @head: Poiter to "struct tomoyo_io_buffer".
2599bd03a3e4STetsuo Handa  * @line: Line to parse.
2600bd03a3e4STetsuo Handa  *
2601bd03a3e4STetsuo Handa  * Returns 0 on success, negative value otherwise.
2602bd03a3e4STetsuo Handa  *
2603bd03a3e4STetsuo Handa  * Caller holds tomoyo_read_lock().
2604bd03a3e4STetsuo Handa  */
2605bd03a3e4STetsuo Handa static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2606bd03a3e4STetsuo Handa {
2607bd03a3e4STetsuo Handa 	/* Delete request? */
2608bd03a3e4STetsuo Handa 	head->w.is_delete = !strncmp(line, "delete ", 7);
2609bd03a3e4STetsuo Handa 	if (head->w.is_delete)
2610bd03a3e4STetsuo Handa 		memmove(line, line + 7, strlen(line + 7) + 1);
2611bd03a3e4STetsuo Handa 	/* Selecting namespace to update. */
2612bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2613bd03a3e4STetsuo Handa 	    head->type == TOMOYO_PROFILE) {
2614bd03a3e4STetsuo Handa 		if (*line == '<') {
2615bd03a3e4STetsuo Handa 			char *cp = strchr(line, ' ');
2616*cdcf6723STetsuo Handa 
2617bd03a3e4STetsuo Handa 			if (cp) {
2618bd03a3e4STetsuo Handa 				*cp++ = '\0';
2619bd03a3e4STetsuo Handa 				head->w.ns = tomoyo_assign_namespace(line);
2620bd03a3e4STetsuo Handa 				memmove(line, cp, strlen(cp) + 1);
2621bd03a3e4STetsuo Handa 			} else
2622bd03a3e4STetsuo Handa 				head->w.ns = NULL;
2623bd03a3e4STetsuo Handa 		} else
2624bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2625bd03a3e4STetsuo Handa 		/* Don't allow updating if namespace is invalid. */
2626bd03a3e4STetsuo Handa 		if (!head->w.ns)
2627bd03a3e4STetsuo Handa 			return -ENOENT;
2628bd03a3e4STetsuo Handa 	}
2629bd03a3e4STetsuo Handa 	/* Do the update. */
2630bd03a3e4STetsuo Handa 	return head->write(head);
2631bd03a3e4STetsuo Handa }
2632bd03a3e4STetsuo Handa 
2633bd03a3e4STetsuo Handa /**
26349590837bSKentaro Takeda  * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
26359590837bSKentaro Takeda  *
26360df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
26379590837bSKentaro Takeda  * @buffer:     Pointer to buffer to read from.
26389590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
26399590837bSKentaro Takeda  *
26409590837bSKentaro Takeda  * Returns @buffer_len on success, negative value otherwise.
26419590837bSKentaro Takeda  */
26422c47ab93STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
26430df7e8b8STetsuo Handa 			     const char __user *buffer, const int buffer_len)
26449590837bSKentaro Takeda {
26459590837bSKentaro Takeda 	int error = buffer_len;
2646bd03a3e4STetsuo Handa 	size_t avail_len = buffer_len;
26479590837bSKentaro Takeda 	char *cp0 = head->write_buf;
26482e503bbbSTetsuo Handa 	int idx;
2649*cdcf6723STetsuo Handa 
26509590837bSKentaro Takeda 	if (!head->write)
2651*cdcf6723STetsuo Handa 		return -EINVAL;
265296d4f267SLinus Torvalds 	if (!access_ok(buffer, buffer_len))
26539590837bSKentaro Takeda 		return -EFAULT;
26549590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
26559590837bSKentaro Takeda 		return -EINTR;
2656e0b057b4STetsuo Handa 	head->read_user_buf_avail = 0;
26572e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
26589590837bSKentaro Takeda 	/* Read a line and dispatch it to the policy handler. */
26599590837bSKentaro Takeda 	while (avail_len > 0) {
26609590837bSKentaro Takeda 		char c;
2661*cdcf6723STetsuo Handa 
26620df7e8b8STetsuo Handa 		if (head->w.avail >= head->writebuf_size - 1) {
2663bd03a3e4STetsuo Handa 			const int len = head->writebuf_size * 2;
2664bd03a3e4STetsuo Handa 			char *cp = kzalloc(len, GFP_NOFS);
2665*cdcf6723STetsuo Handa 
2666bd03a3e4STetsuo Handa 			if (!cp) {
26679590837bSKentaro Takeda 				error = -ENOMEM;
26689590837bSKentaro Takeda 				break;
2669bd03a3e4STetsuo Handa 			}
2670bd03a3e4STetsuo Handa 			memmove(cp, cp0, head->w.avail);
2671bd03a3e4STetsuo Handa 			kfree(cp0);
2672bd03a3e4STetsuo Handa 			head->write_buf = cp;
2673bd03a3e4STetsuo Handa 			cp0 = cp;
2674bd03a3e4STetsuo Handa 			head->writebuf_size = len;
2675bd03a3e4STetsuo Handa 		}
2676bd03a3e4STetsuo Handa 		if (get_user(c, buffer)) {
26779590837bSKentaro Takeda 			error = -EFAULT;
26789590837bSKentaro Takeda 			break;
26799590837bSKentaro Takeda 		}
26809590837bSKentaro Takeda 		buffer++;
26819590837bSKentaro Takeda 		avail_len--;
26820df7e8b8STetsuo Handa 		cp0[head->w.avail++] = c;
26839590837bSKentaro Takeda 		if (c != '\n')
26849590837bSKentaro Takeda 			continue;
26850df7e8b8STetsuo Handa 		cp0[head->w.avail - 1] = '\0';
26860df7e8b8STetsuo Handa 		head->w.avail = 0;
26879590837bSKentaro Takeda 		tomoyo_normalize_line(cp0);
2688bd03a3e4STetsuo Handa 		if (!strcmp(cp0, "reset")) {
2689bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2690bd03a3e4STetsuo Handa 			head->w.domain = NULL;
2691bd03a3e4STetsuo Handa 			memset(&head->r, 0, sizeof(head->r));
2692bd03a3e4STetsuo Handa 			continue;
26939590837bSKentaro Takeda 		}
2694bd03a3e4STetsuo Handa 		/* Don't allow updating policies by non manager programs. */
2695bd03a3e4STetsuo Handa 		switch (head->type) {
2696bd03a3e4STetsuo Handa 		case TOMOYO_PROCESS_STATUS:
2697bd03a3e4STetsuo Handa 			/* This does not write anything. */
2698bd03a3e4STetsuo Handa 			break;
2699bd03a3e4STetsuo Handa 		case TOMOYO_DOMAINPOLICY:
2700bd03a3e4STetsuo Handa 			if (tomoyo_select_domain(head, cp0))
2701bd03a3e4STetsuo Handa 				continue;
2702bd03a3e4STetsuo Handa 			/* fall through */
2703bd03a3e4STetsuo Handa 		case TOMOYO_EXCEPTIONPOLICY:
2704bd03a3e4STetsuo Handa 			if (!strcmp(cp0, "select transition_only")) {
2705bd03a3e4STetsuo Handa 				head->r.print_transition_related_only = true;
2706bd03a3e4STetsuo Handa 				continue;
2707bd03a3e4STetsuo Handa 			}
2708bd03a3e4STetsuo Handa 			/* fall through */
2709bd03a3e4STetsuo Handa 		default:
2710bd03a3e4STetsuo Handa 			if (!tomoyo_manager()) {
2711bd03a3e4STetsuo Handa 				error = -EPERM;
2712bd03a3e4STetsuo Handa 				goto out;
2713bd03a3e4STetsuo Handa 			}
2714bd03a3e4STetsuo Handa 		}
2715bd03a3e4STetsuo Handa 		switch (tomoyo_parse_policy(head, cp0)) {
2716bd03a3e4STetsuo Handa 		case -EPERM:
2717bd03a3e4STetsuo Handa 			error = -EPERM;
2718bd03a3e4STetsuo Handa 			goto out;
2719b22b8b9fSTetsuo Handa 		case 0:
2720b22b8b9fSTetsuo Handa 			switch (head->type) {
2721b22b8b9fSTetsuo Handa 			case TOMOYO_DOMAINPOLICY:
2722b22b8b9fSTetsuo Handa 			case TOMOYO_EXCEPTIONPOLICY:
2723b22b8b9fSTetsuo Handa 			case TOMOYO_STAT:
2724b22b8b9fSTetsuo Handa 			case TOMOYO_PROFILE:
2725b22b8b9fSTetsuo Handa 			case TOMOYO_MANAGER:
2726b22b8b9fSTetsuo Handa 				tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2727b22b8b9fSTetsuo Handa 				break;
2728b22b8b9fSTetsuo Handa 			default:
2729b22b8b9fSTetsuo Handa 				break;
2730b22b8b9fSTetsuo Handa 			}
2731b22b8b9fSTetsuo Handa 			break;
2732bd03a3e4STetsuo Handa 		}
2733bd03a3e4STetsuo Handa 	}
2734bd03a3e4STetsuo Handa out:
27352e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
27369590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
27379590837bSKentaro Takeda 	return error;
27389590837bSKentaro Takeda }
27399590837bSKentaro Takeda 
27409590837bSKentaro Takeda /**
27419590837bSKentaro Takeda  * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
27429590837bSKentaro Takeda  *
27430df7e8b8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
27449590837bSKentaro Takeda  */
2745e53cfda5SAl Viro void tomoyo_close_control(struct tomoyo_io_buffer *head)
27469590837bSKentaro Takeda {
274717fcfbd9STetsuo Handa 	/*
274817fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , decrement the
274917fcfbd9STetsuo Handa 	 * observer counter.
275017fcfbd9STetsuo Handa 	 */
27512e503bbbSTetsuo Handa 	if (head->type == TOMOYO_QUERY &&
27522e503bbbSTetsuo Handa 	    atomic_dec_and_test(&tomoyo_query_observers))
27532e503bbbSTetsuo Handa 		wake_up_all(&tomoyo_answer_wait);
27542e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, false);
27559590837bSKentaro Takeda }
27569590837bSKentaro Takeda 
27579590837bSKentaro Takeda /**
2758c3ef1500STetsuo Handa  * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
27599590837bSKentaro Takeda  */
2760c3ef1500STetsuo Handa void tomoyo_check_profile(void)
27619590837bSKentaro Takeda {
2762c3ef1500STetsuo Handa 	struct tomoyo_domain_info *domain;
2763c3ef1500STetsuo Handa 	const int idx = tomoyo_read_lock();
2764*cdcf6723STetsuo Handa 
2765c3ef1500STetsuo Handa 	tomoyo_policy_loaded = true;
2766*cdcf6723STetsuo Handa 	pr_info("TOMOYO: 2.5.0\n");
2767c3ef1500STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2768c3ef1500STetsuo Handa 		const u8 profile = domain->profile;
2769bd03a3e4STetsuo Handa 		const struct tomoyo_policy_namespace *ns = domain->ns;
2770*cdcf6723STetsuo Handa 
2771843d183cSTetsuo Handa 		if (ns->profile_version != 20110903)
2772*cdcf6723STetsuo Handa 			pr_err("Profile version %u is not supported.\n",
2773bd03a3e4STetsuo Handa 			       ns->profile_version);
2774bd03a3e4STetsuo Handa 		else if (!ns->profile_ptr[profile])
2775*cdcf6723STetsuo Handa 			pr_err("Profile %u (used by '%s') is not defined.\n",
2776c3ef1500STetsuo Handa 			       profile, domain->domainname->name);
2777bd03a3e4STetsuo Handa 		else
2778bd03a3e4STetsuo Handa 			continue;
2779*cdcf6723STetsuo Handa 		pr_err("Userland tools for TOMOYO 2.5 must be installed and policy must be initialized.\n");
2780*cdcf6723STetsuo Handa 		pr_err("Please see http://tomoyo.sourceforge.jp/2.5/ for more information.\n");
2781bd03a3e4STetsuo Handa 		panic("STOP!");
27829590837bSKentaro Takeda 	}
2783c3ef1500STetsuo Handa 	tomoyo_read_unlock(idx);
2784*cdcf6723STetsuo Handa 	pr_info("Mandatory Access Control activated.\n");
27859590837bSKentaro Takeda }
2786efe836abSTetsuo Handa 
2787efe836abSTetsuo Handa /**
2788efe836abSTetsuo Handa  * tomoyo_load_builtin_policy - Load built-in policy.
2789efe836abSTetsuo Handa  *
2790efe836abSTetsuo Handa  * Returns nothing.
2791efe836abSTetsuo Handa  */
2792efe836abSTetsuo Handa void __init tomoyo_load_builtin_policy(void)
2793efe836abSTetsuo Handa {
2794efe836abSTetsuo Handa 	/*
2795efe836abSTetsuo Handa 	 * This include file is manually created and contains built-in policy
2796efe836abSTetsuo Handa 	 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2797efe836abSTetsuo Handa 	 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2798efe836abSTetsuo Handa 	 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2799efe836abSTetsuo Handa 	 */
2800efe836abSTetsuo Handa #include "builtin-policy.h"
2801efe836abSTetsuo Handa 	u8 i;
2802efe836abSTetsuo Handa 	const int idx = tomoyo_read_lock();
2803*cdcf6723STetsuo Handa 
2804efe836abSTetsuo Handa 	for (i = 0; i < 5; i++) {
2805efe836abSTetsuo Handa 		struct tomoyo_io_buffer head = { };
2806efe836abSTetsuo Handa 		char *start = "";
2807*cdcf6723STetsuo Handa 
2808efe836abSTetsuo Handa 		switch (i) {
2809efe836abSTetsuo Handa 		case 0:
2810efe836abSTetsuo Handa 			start = tomoyo_builtin_profile;
2811efe836abSTetsuo Handa 			head.type = TOMOYO_PROFILE;
2812efe836abSTetsuo Handa 			head.write = tomoyo_write_profile;
2813efe836abSTetsuo Handa 			break;
2814efe836abSTetsuo Handa 		case 1:
2815efe836abSTetsuo Handa 			start = tomoyo_builtin_exception_policy;
2816efe836abSTetsuo Handa 			head.type = TOMOYO_EXCEPTIONPOLICY;
2817efe836abSTetsuo Handa 			head.write = tomoyo_write_exception;
2818efe836abSTetsuo Handa 			break;
2819efe836abSTetsuo Handa 		case 2:
2820efe836abSTetsuo Handa 			start = tomoyo_builtin_domain_policy;
2821efe836abSTetsuo Handa 			head.type = TOMOYO_DOMAINPOLICY;
2822efe836abSTetsuo Handa 			head.write = tomoyo_write_domain;
2823efe836abSTetsuo Handa 			break;
2824efe836abSTetsuo Handa 		case 3:
2825efe836abSTetsuo Handa 			start = tomoyo_builtin_manager;
2826efe836abSTetsuo Handa 			head.type = TOMOYO_MANAGER;
2827efe836abSTetsuo Handa 			head.write = tomoyo_write_manager;
2828efe836abSTetsuo Handa 			break;
2829efe836abSTetsuo Handa 		case 4:
2830efe836abSTetsuo Handa 			start = tomoyo_builtin_stat;
2831efe836abSTetsuo Handa 			head.type = TOMOYO_STAT;
2832efe836abSTetsuo Handa 			head.write = tomoyo_write_stat;
2833efe836abSTetsuo Handa 			break;
2834efe836abSTetsuo Handa 		}
2835efe836abSTetsuo Handa 		while (1) {
2836efe836abSTetsuo Handa 			char *end = strchr(start, '\n');
2837*cdcf6723STetsuo Handa 
2838efe836abSTetsuo Handa 			if (!end)
2839efe836abSTetsuo Handa 				break;
2840efe836abSTetsuo Handa 			*end = '\0';
2841efe836abSTetsuo Handa 			tomoyo_normalize_line(start);
2842efe836abSTetsuo Handa 			head.write_buf = start;
2843efe836abSTetsuo Handa 			tomoyo_parse_policy(&head, start);
2844efe836abSTetsuo Handa 			start = end + 1;
2845efe836abSTetsuo Handa 		}
2846efe836abSTetsuo Handa 	}
2847efe836abSTetsuo Handa 	tomoyo_read_unlock(idx);
28480e4ae0e0STetsuo Handa #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
28490e4ae0e0STetsuo Handa 	tomoyo_check_profile();
28500e4ae0e0STetsuo Handa #endif
2851efe836abSTetsuo Handa }
2852