xref: /linux/security/tomoyo/common.c (revision 6bce98edc3365a8f780ff3944ac7992544c194fe)
19590837bSKentaro Takeda /*
29590837bSKentaro Takeda  * security/tomoyo/common.c
39590837bSKentaro Takeda  *
40f2a55d5STetsuo Handa  * Copyright (C) 2005-2011  NTT DATA CORPORATION
59590837bSKentaro Takeda  */
69590837bSKentaro Takeda 
79590837bSKentaro Takeda #include <linux/uaccess.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
99590837bSKentaro Takeda #include <linux/security.h>
109590837bSKentaro Takeda #include "common.h"
119590837bSKentaro Takeda 
12eadd99ccSTetsuo Handa /* String table for operation mode. */
13eadd99ccSTetsuo Handa const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
14eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_DISABLED]   = "disabled",
15eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_LEARNING]   = "learning",
16eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_PERMISSIVE] = "permissive",
17eadd99ccSTetsuo Handa 	[TOMOYO_CONFIG_ENFORCING]  = "enforcing"
189590837bSKentaro Takeda };
199590837bSKentaro Takeda 
2057c2590fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/profile */
212c47ab93STetsuo Handa const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
2257c2590fSTetsuo Handa 				       + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
23d58e0da8STetsuo Handa 	/* CONFIG::file group */
242c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_EXECUTE]    = "execute",
252c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_OPEN]       = "open",
262c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CREATE]     = "create",
272c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_UNLINK]     = "unlink",
282c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_GETATTR]    = "getattr",
292c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKDIR]      = "mkdir",
302c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_RMDIR]      = "rmdir",
312c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKFIFO]     = "mkfifo",
322c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKSOCK]     = "mksock",
332c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_TRUNCATE]   = "truncate",
342c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_SYMLINK]    = "symlink",
352c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKBLOCK]    = "mkblock",
362c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MKCHAR]     = "mkchar",
372c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_LINK]       = "link",
382c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_RENAME]     = "rename",
392c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHMOD]      = "chmod",
402c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHOWN]      = "chown",
412c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHGRP]      = "chgrp",
422c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_IOCTL]      = "ioctl",
432c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_CHROOT]     = "chroot",
442c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_MOUNT]      = "mount",
452c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_UMOUNT]     = "unmount",
462c47ab93STetsuo Handa 	[TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root",
47059d84dbSTetsuo Handa 	/* CONFIG::network group */
48059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
49059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
50059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
51059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
52059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
53059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
54059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
55059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
56059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
57059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
58059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
59059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
60059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
61059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
62059d84dbSTetsuo Handa 	[TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
63d58e0da8STetsuo Handa 	/* CONFIG::misc group */
64d58e0da8STetsuo Handa 	[TOMOYO_MAC_ENVIRON] = "env",
65d58e0da8STetsuo Handa 	/* CONFIG group */
6657c2590fSTetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
67059d84dbSTetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_NETWORK] = "network",
68d58e0da8STetsuo Handa 	[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_MISC] = "misc",
699590837bSKentaro Takeda };
709590837bSKentaro Takeda 
712066a361STetsuo Handa /* String table for conditions. */
722066a361STetsuo Handa const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
732066a361STetsuo Handa 	[TOMOYO_TASK_UID]             = "task.uid",
742066a361STetsuo Handa 	[TOMOYO_TASK_EUID]            = "task.euid",
752066a361STetsuo Handa 	[TOMOYO_TASK_SUID]            = "task.suid",
762066a361STetsuo Handa 	[TOMOYO_TASK_FSUID]           = "task.fsuid",
772066a361STetsuo Handa 	[TOMOYO_TASK_GID]             = "task.gid",
782066a361STetsuo Handa 	[TOMOYO_TASK_EGID]            = "task.egid",
792066a361STetsuo Handa 	[TOMOYO_TASK_SGID]            = "task.sgid",
802066a361STetsuo Handa 	[TOMOYO_TASK_FSGID]           = "task.fsgid",
812066a361STetsuo Handa 	[TOMOYO_TASK_PID]             = "task.pid",
822066a361STetsuo Handa 	[TOMOYO_TASK_PPID]            = "task.ppid",
835b636857STetsuo Handa 	[TOMOYO_EXEC_ARGC]            = "exec.argc",
845b636857STetsuo Handa 	[TOMOYO_EXEC_ENVC]            = "exec.envc",
858761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_SOCKET]       = "socket",
868761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_SYMLINK]      = "symlink",
878761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_FILE]         = "file",
888761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_BLOCK_DEV]    = "block",
898761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_DIRECTORY]    = "directory",
908761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_CHAR_DEV]     = "char",
918761afd4STetsuo Handa 	[TOMOYO_TYPE_IS_FIFO]         = "fifo",
928761afd4STetsuo Handa 	[TOMOYO_MODE_SETUID]          = "setuid",
938761afd4STetsuo Handa 	[TOMOYO_MODE_SETGID]          = "setgid",
948761afd4STetsuo Handa 	[TOMOYO_MODE_STICKY]          = "sticky",
958761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_READ]      = "owner_read",
968761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_WRITE]     = "owner_write",
978761afd4STetsuo Handa 	[TOMOYO_MODE_OWNER_EXECUTE]   = "owner_execute",
988761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_READ]      = "group_read",
998761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_WRITE]     = "group_write",
1008761afd4STetsuo Handa 	[TOMOYO_MODE_GROUP_EXECUTE]   = "group_execute",
1018761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_READ]     = "others_read",
1028761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_WRITE]    = "others_write",
1038761afd4STetsuo Handa 	[TOMOYO_MODE_OTHERS_EXECUTE]  = "others_execute",
1042ca9bf45STetsuo Handa 	[TOMOYO_EXEC_REALPATH]        = "exec.realpath",
1052ca9bf45STetsuo Handa 	[TOMOYO_SYMLINK_TARGET]       = "symlink.target",
1068761afd4STetsuo Handa 	[TOMOYO_PATH1_UID]            = "path1.uid",
1078761afd4STetsuo Handa 	[TOMOYO_PATH1_GID]            = "path1.gid",
1088761afd4STetsuo Handa 	[TOMOYO_PATH1_INO]            = "path1.ino",
1098761afd4STetsuo Handa 	[TOMOYO_PATH1_MAJOR]          = "path1.major",
1108761afd4STetsuo Handa 	[TOMOYO_PATH1_MINOR]          = "path1.minor",
1118761afd4STetsuo Handa 	[TOMOYO_PATH1_PERM]           = "path1.perm",
1128761afd4STetsuo Handa 	[TOMOYO_PATH1_TYPE]           = "path1.type",
1138761afd4STetsuo Handa 	[TOMOYO_PATH1_DEV_MAJOR]      = "path1.dev_major",
1148761afd4STetsuo Handa 	[TOMOYO_PATH1_DEV_MINOR]      = "path1.dev_minor",
1158761afd4STetsuo Handa 	[TOMOYO_PATH2_UID]            = "path2.uid",
1168761afd4STetsuo Handa 	[TOMOYO_PATH2_GID]            = "path2.gid",
1178761afd4STetsuo Handa 	[TOMOYO_PATH2_INO]            = "path2.ino",
1188761afd4STetsuo Handa 	[TOMOYO_PATH2_MAJOR]          = "path2.major",
1198761afd4STetsuo Handa 	[TOMOYO_PATH2_MINOR]          = "path2.minor",
1208761afd4STetsuo Handa 	[TOMOYO_PATH2_PERM]           = "path2.perm",
1218761afd4STetsuo Handa 	[TOMOYO_PATH2_TYPE]           = "path2.type",
1228761afd4STetsuo Handa 	[TOMOYO_PATH2_DEV_MAJOR]      = "path2.dev_major",
1238761afd4STetsuo Handa 	[TOMOYO_PATH2_DEV_MINOR]      = "path2.dev_minor",
1248761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_UID]     = "path1.parent.uid",
1258761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_GID]     = "path1.parent.gid",
1268761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_INO]     = "path1.parent.ino",
1278761afd4STetsuo Handa 	[TOMOYO_PATH1_PARENT_PERM]    = "path1.parent.perm",
1288761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_UID]     = "path2.parent.uid",
1298761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_GID]     = "path2.parent.gid",
1308761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_INO]     = "path2.parent.ino",
1318761afd4STetsuo Handa 	[TOMOYO_PATH2_PARENT_PERM]    = "path2.parent.perm",
1322066a361STetsuo Handa };
1332066a361STetsuo Handa 
134d5ca1725STetsuo Handa /* String table for PREFERENCE keyword. */
135d5ca1725STetsuo Handa static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
136eadd99ccSTetsuo Handa 	[TOMOYO_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
137d5ca1725STetsuo Handa 	[TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
138d5ca1725STetsuo Handa };
139d5ca1725STetsuo Handa 
1402c47ab93STetsuo Handa /* String table for path operation. */
1412c47ab93STetsuo Handa const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
1422c47ab93STetsuo Handa 	[TOMOYO_TYPE_EXECUTE]    = "execute",
1432c47ab93STetsuo Handa 	[TOMOYO_TYPE_READ]       = "read",
1442c47ab93STetsuo Handa 	[TOMOYO_TYPE_WRITE]      = "write",
1452c47ab93STetsuo Handa 	[TOMOYO_TYPE_APPEND]     = "append",
1462c47ab93STetsuo Handa 	[TOMOYO_TYPE_UNLINK]     = "unlink",
1472c47ab93STetsuo Handa 	[TOMOYO_TYPE_GETATTR]    = "getattr",
1482c47ab93STetsuo Handa 	[TOMOYO_TYPE_RMDIR]      = "rmdir",
1492c47ab93STetsuo Handa 	[TOMOYO_TYPE_TRUNCATE]   = "truncate",
1502c47ab93STetsuo Handa 	[TOMOYO_TYPE_SYMLINK]    = "symlink",
1512c47ab93STetsuo Handa 	[TOMOYO_TYPE_CHROOT]     = "chroot",
1522c47ab93STetsuo Handa 	[TOMOYO_TYPE_UMOUNT]     = "unmount",
1532c47ab93STetsuo Handa };
1542c47ab93STetsuo Handa 
155059d84dbSTetsuo Handa /* String table for socket's operation. */
156059d84dbSTetsuo Handa const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION] = {
157059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_BIND]    = "bind",
158059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_LISTEN]  = "listen",
159059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_CONNECT] = "connect",
160059d84dbSTetsuo Handa 	[TOMOYO_NETWORK_SEND]    = "send",
161059d84dbSTetsuo Handa };
162059d84dbSTetsuo Handa 
1632c47ab93STetsuo Handa /* String table for categories. */
1642c47ab93STetsuo Handa static const char * const tomoyo_category_keywords
1652c47ab93STetsuo Handa [TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
1662c47ab93STetsuo Handa 	[TOMOYO_MAC_CATEGORY_FILE]    = "file",
167059d84dbSTetsuo Handa 	[TOMOYO_MAC_CATEGORY_NETWORK] = "network",
168d58e0da8STetsuo Handa 	[TOMOYO_MAC_CATEGORY_MISC]    = "misc",
1692c47ab93STetsuo Handa };
1702c47ab93STetsuo Handa 
1719590837bSKentaro Takeda /* Permit policy management by non-root user? */
1729590837bSKentaro Takeda static bool tomoyo_manage_by_non_root;
1739590837bSKentaro Takeda 
1749590837bSKentaro Takeda /* Utility functions. */
1759590837bSKentaro Takeda 
1767762fbffSTetsuo Handa /**
17757c2590fSTetsuo Handa  * tomoyo_yesno - Return "yes" or "no".
17857c2590fSTetsuo Handa  *
17957c2590fSTetsuo Handa  * @value: Bool value.
18057c2590fSTetsuo Handa  */
181eadd99ccSTetsuo Handa const char *tomoyo_yesno(const unsigned int value)
18257c2590fSTetsuo Handa {
18357c2590fSTetsuo Handa 	return value ? "yes" : "no";
18457c2590fSTetsuo Handa }
18557c2590fSTetsuo Handa 
186d5ca1725STetsuo Handa /**
187d5ca1725STetsuo Handa  * tomoyo_addprintf - strncat()-like-snprintf().
188d5ca1725STetsuo Handa  *
189d5ca1725STetsuo Handa  * @buffer: Buffer to write to. Must be '\0'-terminated.
190d5ca1725STetsuo Handa  * @len:    Size of @buffer.
191d5ca1725STetsuo Handa  * @fmt:    The printf()'s format string, followed by parameters.
192d5ca1725STetsuo Handa  *
193d5ca1725STetsuo Handa  * Returns nothing.
194d5ca1725STetsuo Handa  */
195f23571e8STetsuo Handa static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
196f23571e8STetsuo Handa {
197f23571e8STetsuo Handa 	va_list args;
198f23571e8STetsuo Handa 	const int pos = strlen(buffer);
199f23571e8STetsuo Handa 	va_start(args, fmt);
200f23571e8STetsuo Handa 	vsnprintf(buffer + pos, len - pos - 1, fmt, args);
201f23571e8STetsuo Handa 	va_end(args);
202f23571e8STetsuo Handa }
203f23571e8STetsuo Handa 
204f23571e8STetsuo Handa /**
205f23571e8STetsuo Handa  * tomoyo_flush - Flush queued string to userspace's buffer.
206f23571e8STetsuo Handa  *
207f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
208f23571e8STetsuo Handa  *
209f23571e8STetsuo Handa  * Returns true if all data was flushed, false otherwise.
210f23571e8STetsuo Handa  */
211f23571e8STetsuo Handa static bool tomoyo_flush(struct tomoyo_io_buffer *head)
212f23571e8STetsuo Handa {
213f23571e8STetsuo Handa 	while (head->r.w_pos) {
214f23571e8STetsuo Handa 		const char *w = head->r.w[0];
2152c47ab93STetsuo Handa 		size_t len = strlen(w);
216f23571e8STetsuo Handa 		if (len) {
217f23571e8STetsuo Handa 			if (len > head->read_user_buf_avail)
218f23571e8STetsuo Handa 				len = head->read_user_buf_avail;
219f23571e8STetsuo Handa 			if (!len)
220f23571e8STetsuo Handa 				return false;
221f23571e8STetsuo Handa 			if (copy_to_user(head->read_user_buf, w, len))
222f23571e8STetsuo Handa 				return false;
223f23571e8STetsuo Handa 			head->read_user_buf_avail -= len;
224f23571e8STetsuo Handa 			head->read_user_buf += len;
225f23571e8STetsuo Handa 			w += len;
226f23571e8STetsuo Handa 		}
227f23571e8STetsuo Handa 		head->r.w[0] = w;
228c0fa797aSTetsuo Handa 		if (*w)
229f23571e8STetsuo Handa 			return false;
230eadd99ccSTetsuo Handa 		/* Add '\0' for audit logs and query. */
231f23571e8STetsuo Handa 		if (head->poll) {
232f23571e8STetsuo Handa 			if (!head->read_user_buf_avail ||
233f23571e8STetsuo Handa 			    copy_to_user(head->read_user_buf, "", 1))
234f23571e8STetsuo Handa 				return false;
235f23571e8STetsuo Handa 			head->read_user_buf_avail--;
236f23571e8STetsuo Handa 			head->read_user_buf++;
237f23571e8STetsuo Handa 		}
238f23571e8STetsuo Handa 		head->r.w_pos--;
239f23571e8STetsuo Handa 		for (len = 0; len < head->r.w_pos; len++)
240f23571e8STetsuo Handa 			head->r.w[len] = head->r.w[len + 1];
241f23571e8STetsuo Handa 	}
242f23571e8STetsuo Handa 	head->r.avail = 0;
243f23571e8STetsuo Handa 	return true;
244f23571e8STetsuo Handa }
245f23571e8STetsuo Handa 
246f23571e8STetsuo Handa /**
247f23571e8STetsuo Handa  * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
248f23571e8STetsuo Handa  *
249f23571e8STetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
250f23571e8STetsuo Handa  * @string: String to print.
251f23571e8STetsuo Handa  *
252f23571e8STetsuo Handa  * Note that @string has to be kept valid until @head is kfree()d.
253f23571e8STetsuo Handa  * This means that char[] allocated on stack memory cannot be passed to
254f23571e8STetsuo Handa  * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
255f23571e8STetsuo Handa  */
256f23571e8STetsuo Handa static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
257f23571e8STetsuo Handa {
258f23571e8STetsuo Handa 	if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
259f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = string;
260f23571e8STetsuo Handa 		tomoyo_flush(head);
261f23571e8STetsuo Handa 	} else
262f23571e8STetsuo Handa 		WARN_ON(1);
263f23571e8STetsuo Handa }
264f23571e8STetsuo Handa 
265f23571e8STetsuo Handa /**
266f23571e8STetsuo Handa  * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
267f23571e8STetsuo Handa  *
268f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
269f23571e8STetsuo Handa  * @fmt:  The printf()'s format string, followed by parameters.
270f23571e8STetsuo Handa  */
271f23571e8STetsuo Handa void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
272f23571e8STetsuo Handa {
273f23571e8STetsuo Handa 	va_list args;
2742c47ab93STetsuo Handa 	size_t len;
2752c47ab93STetsuo Handa 	size_t pos = head->r.avail;
276f23571e8STetsuo Handa 	int size = head->readbuf_size - pos;
277f23571e8STetsuo Handa 	if (size <= 0)
278f23571e8STetsuo Handa 		return;
279f23571e8STetsuo Handa 	va_start(args, fmt);
280f23571e8STetsuo Handa 	len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
281f23571e8STetsuo Handa 	va_end(args);
282f23571e8STetsuo Handa 	if (pos + len >= head->readbuf_size) {
283f23571e8STetsuo Handa 		WARN_ON(1);
284f23571e8STetsuo Handa 		return;
285f23571e8STetsuo Handa 	}
286f23571e8STetsuo Handa 	head->r.avail += len;
287f23571e8STetsuo Handa 	tomoyo_set_string(head, head->read_buf + pos);
288f23571e8STetsuo Handa }
289f23571e8STetsuo Handa 
2900d2171d7STetsuo Handa /**
2910d2171d7STetsuo Handa  * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
2920d2171d7STetsuo Handa  *
2930d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2940d2171d7STetsuo Handa  *
2950d2171d7STetsuo Handa  * Returns nothing.
2960d2171d7STetsuo Handa  */
297f23571e8STetsuo Handa static void tomoyo_set_space(struct tomoyo_io_buffer *head)
298f23571e8STetsuo Handa {
299f23571e8STetsuo Handa 	tomoyo_set_string(head, " ");
300f23571e8STetsuo Handa }
301f23571e8STetsuo Handa 
3020d2171d7STetsuo Handa /**
3030d2171d7STetsuo Handa  * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
3040d2171d7STetsuo Handa  *
3050d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3060d2171d7STetsuo Handa  *
3070d2171d7STetsuo Handa  * Returns nothing.
3080d2171d7STetsuo Handa  */
309f23571e8STetsuo Handa static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
310f23571e8STetsuo Handa {
311f23571e8STetsuo Handa 	tomoyo_set_string(head, "\n");
312f23571e8STetsuo Handa 	return !head->r.w_pos;
313f23571e8STetsuo Handa }
314f23571e8STetsuo Handa 
31557c2590fSTetsuo Handa /**
3160d2171d7STetsuo Handa  * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
3170d2171d7STetsuo Handa  *
3180d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3190d2171d7STetsuo Handa  *
3200d2171d7STetsuo Handa  * Returns nothing.
3210d2171d7STetsuo Handa  */
3220d2171d7STetsuo Handa static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
3230d2171d7STetsuo Handa {
3240d2171d7STetsuo Handa 	tomoyo_set_string(head, "/");
3250d2171d7STetsuo Handa }
3260d2171d7STetsuo Handa 
327bd03a3e4STetsuo Handa /* List of namespaces. */
328bd03a3e4STetsuo Handa LIST_HEAD(tomoyo_namespace_list);
329bd03a3e4STetsuo Handa /* True if namespace other than tomoyo_kernel_namespace is defined. */
330bd03a3e4STetsuo Handa static bool tomoyo_namespace_enabled;
331bd03a3e4STetsuo Handa 
332bd03a3e4STetsuo Handa /**
333bd03a3e4STetsuo Handa  * tomoyo_init_policy_namespace - Initialize namespace.
334bd03a3e4STetsuo Handa  *
335bd03a3e4STetsuo Handa  * @ns: Pointer to "struct tomoyo_policy_namespace".
336bd03a3e4STetsuo Handa  *
337bd03a3e4STetsuo Handa  * Returns nothing.
338bd03a3e4STetsuo Handa  */
339bd03a3e4STetsuo Handa void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
340bd03a3e4STetsuo Handa {
341bd03a3e4STetsuo Handa 	unsigned int idx;
342bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
343bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->acl_group[idx]);
344bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
345bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->group_list[idx]);
346bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
347bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->policy_list[idx]);
348843d183cSTetsuo Handa 	ns->profile_version = 20110903;
349bd03a3e4STetsuo Handa 	tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
350bd03a3e4STetsuo Handa 	list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
351bd03a3e4STetsuo Handa }
352bd03a3e4STetsuo Handa 
353bd03a3e4STetsuo Handa /**
354bd03a3e4STetsuo Handa  * tomoyo_print_namespace - Print namespace header.
355bd03a3e4STetsuo Handa  *
356bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
357bd03a3e4STetsuo Handa  *
358bd03a3e4STetsuo Handa  * Returns nothing.
359bd03a3e4STetsuo Handa  */
360bd03a3e4STetsuo Handa static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
361bd03a3e4STetsuo Handa {
362bd03a3e4STetsuo Handa 	if (!tomoyo_namespace_enabled)
363bd03a3e4STetsuo Handa 		return;
364bd03a3e4STetsuo Handa 	tomoyo_set_string(head,
365bd03a3e4STetsuo Handa 			  container_of(head->r.ns,
366bd03a3e4STetsuo Handa 				       struct tomoyo_policy_namespace,
367bd03a3e4STetsuo Handa 				       namespace_list)->name);
368bd03a3e4STetsuo Handa 	tomoyo_set_space(head);
369bd03a3e4STetsuo Handa }
370bd03a3e4STetsuo Handa 
3710d2171d7STetsuo Handa /**
3727762fbffSTetsuo Handa  * tomoyo_print_name_union - Print a tomoyo_name_union.
3737762fbffSTetsuo Handa  *
3747762fbffSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3757762fbffSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3767762fbffSTetsuo Handa  */
377f23571e8STetsuo Handa static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
3787762fbffSTetsuo Handa 				    const struct tomoyo_name_union *ptr)
3797762fbffSTetsuo Handa {
380f23571e8STetsuo Handa 	tomoyo_set_space(head);
3810df7e8b8STetsuo Handa 	if (ptr->group) {
382f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
383f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
384f23571e8STetsuo Handa 	} else {
385f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
386f23571e8STetsuo Handa 	}
3877762fbffSTetsuo Handa }
3887762fbffSTetsuo Handa 
3897762fbffSTetsuo Handa /**
3902ca9bf45STetsuo Handa  * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
3912ca9bf45STetsuo Handa  *
3922ca9bf45STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3932ca9bf45STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3942ca9bf45STetsuo Handa  *
3952ca9bf45STetsuo Handa  * Returns nothing.
3962ca9bf45STetsuo Handa  */
3972ca9bf45STetsuo Handa static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
3982ca9bf45STetsuo Handa 					   const struct tomoyo_name_union *ptr)
3992ca9bf45STetsuo Handa {
4002ca9bf45STetsuo Handa 	if (ptr->group) {
4012ca9bf45STetsuo Handa 		tomoyo_set_string(head, "@");
4022ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
4032ca9bf45STetsuo Handa 	} else {
4042ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4052ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
4062ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4072ca9bf45STetsuo Handa 	}
4082ca9bf45STetsuo Handa }
4092ca9bf45STetsuo Handa 
4102ca9bf45STetsuo Handa /**
4112066a361STetsuo Handa  * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
4124c3e9e2dSTetsuo Handa  *
4134c3e9e2dSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4144c3e9e2dSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4152066a361STetsuo Handa  *
4162066a361STetsuo Handa  * Returns nothing.
4174c3e9e2dSTetsuo Handa  */
4182066a361STetsuo Handa static void tomoyo_print_number_union_nospace
4192066a361STetsuo Handa (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
4204c3e9e2dSTetsuo Handa {
4210df7e8b8STetsuo Handa 	if (ptr->group) {
422f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
423f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
424f23571e8STetsuo Handa 	} else {
425f23571e8STetsuo Handa 		int i;
426f23571e8STetsuo Handa 		unsigned long min = ptr->values[0];
427f23571e8STetsuo Handa 		const unsigned long max = ptr->values[1];
4280df7e8b8STetsuo Handa 		u8 min_type = ptr->value_type[0];
4290df7e8b8STetsuo Handa 		const u8 max_type = ptr->value_type[1];
430f23571e8STetsuo Handa 		char buffer[128];
431f23571e8STetsuo Handa 		buffer[0] = '\0';
432f23571e8STetsuo Handa 		for (i = 0; i < 2; i++) {
4334c3e9e2dSTetsuo Handa 			switch (min_type) {
4344c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_HEXADECIMAL:
435f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
436f23571e8STetsuo Handa 						 "0x%lX", min);
4374c3e9e2dSTetsuo Handa 				break;
4384c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_OCTAL:
439f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
440f23571e8STetsuo Handa 						 "0%lo", min);
4414c3e9e2dSTetsuo Handa 				break;
4424c3e9e2dSTetsuo Handa 			default:
4432066a361STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
4442066a361STetsuo Handa 						 min);
4454c3e9e2dSTetsuo Handa 				break;
4464c3e9e2dSTetsuo Handa 			}
4474c3e9e2dSTetsuo Handa 			if (min == max && min_type == max_type)
448f23571e8STetsuo Handa 				break;
449f23571e8STetsuo Handa 			tomoyo_addprintf(buffer, sizeof(buffer), "-");
450f23571e8STetsuo Handa 			min_type = max_type;
451f23571e8STetsuo Handa 			min = max;
4524c3e9e2dSTetsuo Handa 		}
453f23571e8STetsuo Handa 		tomoyo_io_printf(head, "%s", buffer);
4544c3e9e2dSTetsuo Handa 	}
4559590837bSKentaro Takeda }
4569590837bSKentaro Takeda 
4579590837bSKentaro Takeda /**
4582066a361STetsuo Handa  * tomoyo_print_number_union - Print a tomoyo_number_union.
4592066a361STetsuo Handa  *
4602066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4612066a361STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4622066a361STetsuo Handa  *
4632066a361STetsuo Handa  * Returns nothing.
4642066a361STetsuo Handa  */
4652066a361STetsuo Handa static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
4662066a361STetsuo Handa 				      const struct tomoyo_number_union *ptr)
4672066a361STetsuo Handa {
4682066a361STetsuo Handa 	tomoyo_set_space(head);
4692066a361STetsuo Handa 	tomoyo_print_number_union_nospace(head, ptr);
4702066a361STetsuo Handa }
4712066a361STetsuo Handa 
4722066a361STetsuo Handa /**
473e2bf6907STetsuo Handa  * tomoyo_assign_profile - Create a new profile.
4749590837bSKentaro Takeda  *
475bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
4769590837bSKentaro Takeda  * @profile: Profile number to create.
4779590837bSKentaro Takeda  *
4789590837bSKentaro Takeda  * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
4799590837bSKentaro Takeda  */
480bd03a3e4STetsuo Handa static struct tomoyo_profile *tomoyo_assign_profile
481bd03a3e4STetsuo Handa (struct tomoyo_policy_namespace *ns, const unsigned int profile)
4829590837bSKentaro Takeda {
48357c2590fSTetsuo Handa 	struct tomoyo_profile *ptr;
48457c2590fSTetsuo Handa 	struct tomoyo_profile *entry;
4859590837bSKentaro Takeda 	if (profile >= TOMOYO_MAX_PROFILES)
4869590837bSKentaro Takeda 		return NULL;
487bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
4889590837bSKentaro Takeda 	if (ptr)
48957c2590fSTetsuo Handa 		return ptr;
49057c2590fSTetsuo Handa 	entry = kzalloc(sizeof(*entry), GFP_NOFS);
49157c2590fSTetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
49257c2590fSTetsuo Handa 		goto out;
493bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
49457c2590fSTetsuo Handa 	if (!ptr && tomoyo_memory_ok(entry)) {
49557c2590fSTetsuo Handa 		ptr = entry;
496eadd99ccSTetsuo Handa 		ptr->default_config = TOMOYO_CONFIG_DISABLED |
497eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_GRANT_LOG |
498eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_REJECT_LOG;
49957c2590fSTetsuo Handa 		memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
50057c2590fSTetsuo Handa 		       sizeof(ptr->config));
501eadd99ccSTetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
502d5ca1725STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
5039590837bSKentaro Takeda 		mb(); /* Avoid out-of-order execution. */
504bd03a3e4STetsuo Handa 		ns->profile_ptr[profile] = ptr;
50557c2590fSTetsuo Handa 		entry = NULL;
50657c2590fSTetsuo Handa 	}
50729282381STetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
50857c2590fSTetsuo Handa  out:
50957c2590fSTetsuo Handa 	kfree(entry);
5109590837bSKentaro Takeda 	return ptr;
5119590837bSKentaro Takeda }
5129590837bSKentaro Takeda 
5139590837bSKentaro Takeda /**
51457c2590fSTetsuo Handa  * tomoyo_profile - Find a profile.
51557c2590fSTetsuo Handa  *
516bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
51757c2590fSTetsuo Handa  * @profile: Profile number to find.
51857c2590fSTetsuo Handa  *
51957c2590fSTetsuo Handa  * Returns pointer to "struct tomoyo_profile".
52057c2590fSTetsuo Handa  */
521bd03a3e4STetsuo Handa struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
522bd03a3e4STetsuo Handa 				      const u8 profile)
52357c2590fSTetsuo Handa {
524d5ca1725STetsuo Handa 	static struct tomoyo_profile tomoyo_null_profile;
525bd03a3e4STetsuo Handa 	struct tomoyo_profile *ptr = ns->profile_ptr[profile];
526d5ca1725STetsuo Handa 	if (!ptr)
527d5ca1725STetsuo Handa 		ptr = &tomoyo_null_profile;
52857c2590fSTetsuo Handa 	return ptr;
52957c2590fSTetsuo Handa }
53057c2590fSTetsuo Handa 
531d5ca1725STetsuo Handa /**
532d5ca1725STetsuo Handa  * tomoyo_find_yesno - Find values for specified keyword.
533d5ca1725STetsuo Handa  *
534d5ca1725STetsuo Handa  * @string: String to check.
535d5ca1725STetsuo Handa  * @find:   Name of keyword.
536d5ca1725STetsuo Handa  *
537d5ca1725STetsuo Handa  * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
538d5ca1725STetsuo Handa  */
5398e568687STetsuo Handa static s8 tomoyo_find_yesno(const char *string, const char *find)
5408e568687STetsuo Handa {
5418e568687STetsuo Handa 	const char *cp = strstr(string, find);
5428e568687STetsuo Handa 	if (cp) {
5438e568687STetsuo Handa 		cp += strlen(find);
5448e568687STetsuo Handa 		if (!strncmp(cp, "=yes", 4))
5458e568687STetsuo Handa 			return 1;
5468e568687STetsuo Handa 		else if (!strncmp(cp, "=no", 3))
5478e568687STetsuo Handa 			return 0;
5488e568687STetsuo Handa 	}
5498e568687STetsuo Handa 	return -1;
5508e568687STetsuo Handa }
5518e568687STetsuo Handa 
552d5ca1725STetsuo Handa /**
553d5ca1725STetsuo Handa  * tomoyo_set_uint - Set value for specified preference.
554d5ca1725STetsuo Handa  *
555d5ca1725STetsuo Handa  * @i:      Pointer to "unsigned int".
556d5ca1725STetsuo Handa  * @string: String to check.
557d5ca1725STetsuo Handa  * @find:   Name of keyword.
558d5ca1725STetsuo Handa  *
559d5ca1725STetsuo Handa  * Returns nothing.
560d5ca1725STetsuo Handa  */
5618e568687STetsuo Handa static void tomoyo_set_uint(unsigned int *i, const char *string,
5628e568687STetsuo Handa 			    const char *find)
5638e568687STetsuo Handa {
5648e568687STetsuo Handa 	const char *cp = strstr(string, find);
5658e568687STetsuo Handa 	if (cp)
5668e568687STetsuo Handa 		sscanf(cp + strlen(find), "=%u", i);
5678e568687STetsuo Handa }
5688e568687STetsuo Handa 
569d5ca1725STetsuo Handa /**
570d5ca1725STetsuo Handa  * tomoyo_set_mode - Set mode for specified profile.
571d5ca1725STetsuo Handa  *
572d5ca1725STetsuo Handa  * @name:    Name of functionality.
573d5ca1725STetsuo Handa  * @value:   Mode for @name.
574d5ca1725STetsuo Handa  * @profile: Pointer to "struct tomoyo_profile".
575d5ca1725STetsuo Handa  *
576d5ca1725STetsuo Handa  * Returns 0 on success, negative value otherwise.
577d5ca1725STetsuo Handa  */
5788e568687STetsuo Handa static int tomoyo_set_mode(char *name, const char *value,
5798e568687STetsuo Handa 			   struct tomoyo_profile *profile)
5808e568687STetsuo Handa {
5818e568687STetsuo Handa 	u8 i;
5828e568687STetsuo Handa 	u8 config;
5838e568687STetsuo Handa 	if (!strcmp(name, "CONFIG")) {
5848e568687STetsuo Handa 		i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
5858e568687STetsuo Handa 		config = profile->default_config;
5868e568687STetsuo Handa 	} else if (tomoyo_str_starts(&name, "CONFIG::")) {
5878e568687STetsuo Handa 		config = 0;
5888e568687STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MAC_INDEX
5898e568687STetsuo Handa 			     + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
5902c47ab93STetsuo Handa 			int len = 0;
5912c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX) {
5922c47ab93STetsuo Handa 				const u8 c = tomoyo_index2category[i];
5932c47ab93STetsuo Handa 				const char *category =
5942c47ab93STetsuo Handa 					tomoyo_category_keywords[c];
5952c47ab93STetsuo Handa 				len = strlen(category);
5962c47ab93STetsuo Handa 				if (strncmp(name, category, len) ||
5972c47ab93STetsuo Handa 				    name[len++] != ':' || name[len++] != ':')
5982c47ab93STetsuo Handa 					continue;
5992c47ab93STetsuo Handa 			}
6002c47ab93STetsuo Handa 			if (strcmp(name + len, tomoyo_mac_keywords[i]))
6018e568687STetsuo Handa 				continue;
6028e568687STetsuo Handa 			config = profile->config[i];
6038e568687STetsuo Handa 			break;
6048e568687STetsuo Handa 		}
6058e568687STetsuo Handa 		if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6068e568687STetsuo Handa 			return -EINVAL;
6078e568687STetsuo Handa 	} else {
6088e568687STetsuo Handa 		return -EINVAL;
6098e568687STetsuo Handa 	}
610d5ca1725STetsuo Handa 	if (strstr(value, "use_default")) {
6118e568687STetsuo Handa 		config = TOMOYO_CONFIG_USE_DEFAULT;
6128e568687STetsuo Handa 	} else {
6138e568687STetsuo Handa 		u8 mode;
6148e568687STetsuo Handa 		for (mode = 0; mode < 4; mode++)
6158e568687STetsuo Handa 			if (strstr(value, tomoyo_mode[mode]))
6168e568687STetsuo Handa 				/*
6178e568687STetsuo Handa 				 * Update lower 3 bits in order to distinguish
6188e568687STetsuo Handa 				 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
6198e568687STetsuo Handa 				 */
6208e568687STetsuo Handa 				config = (config & ~7) | mode;
621eadd99ccSTetsuo Handa 		if (config != TOMOYO_CONFIG_USE_DEFAULT) {
622eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "grant_log")) {
623eadd99ccSTetsuo Handa 			case 1:
624eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
625eadd99ccSTetsuo Handa 				break;
626eadd99ccSTetsuo Handa 			case 0:
627eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
628eadd99ccSTetsuo Handa 				break;
629eadd99ccSTetsuo Handa 			}
630eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "reject_log")) {
631eadd99ccSTetsuo Handa 			case 1:
632eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
633eadd99ccSTetsuo Handa 				break;
634eadd99ccSTetsuo Handa 			case 0:
635eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
636eadd99ccSTetsuo Handa 				break;
637eadd99ccSTetsuo Handa 			}
638eadd99ccSTetsuo Handa 		}
6398e568687STetsuo Handa 	}
6408e568687STetsuo Handa 	if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6418e568687STetsuo Handa 		profile->config[i] = config;
6428e568687STetsuo Handa 	else if (config != TOMOYO_CONFIG_USE_DEFAULT)
6438e568687STetsuo Handa 		profile->default_config = config;
6448e568687STetsuo Handa 	return 0;
6458e568687STetsuo Handa }
6468e568687STetsuo Handa 
64757c2590fSTetsuo Handa /**
64857c2590fSTetsuo Handa  * tomoyo_write_profile - Write profile table.
6499590837bSKentaro Takeda  *
6509590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
6519590837bSKentaro Takeda  *
6529590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
6539590837bSKentaro Takeda  */
6549590837bSKentaro Takeda static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
6559590837bSKentaro Takeda {
6569590837bSKentaro Takeda 	char *data = head->write_buf;
6579590837bSKentaro Takeda 	unsigned int i;
6589590837bSKentaro Takeda 	char *cp;
6599590837bSKentaro Takeda 	struct tomoyo_profile *profile;
660bd03a3e4STetsuo Handa 	if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
661bd03a3e4STetsuo Handa 	    == 1)
66257c2590fSTetsuo Handa 		return 0;
66357c2590fSTetsuo Handa 	i = simple_strtoul(data, &cp, 10);
66457c2590fSTetsuo Handa 	if (*cp != '-')
6659590837bSKentaro Takeda 		return -EINVAL;
6669590837bSKentaro Takeda 	data = cp + 1;
667bd03a3e4STetsuo Handa 	profile = tomoyo_assign_profile(head->w.ns, i);
6689590837bSKentaro Takeda 	if (!profile)
6699590837bSKentaro Takeda 		return -EINVAL;
6709590837bSKentaro Takeda 	cp = strchr(data, '=');
6719590837bSKentaro Takeda 	if (!cp)
6729590837bSKentaro Takeda 		return -EINVAL;
67357c2590fSTetsuo Handa 	*cp++ = '\0';
6749590837bSKentaro Takeda 	if (!strcmp(data, "COMMENT")) {
6752a086e5dSTetsuo Handa 		static DEFINE_SPINLOCK(lock);
6762a086e5dSTetsuo Handa 		const struct tomoyo_path_info *new_comment
6772a086e5dSTetsuo Handa 			= tomoyo_get_name(cp);
6782a086e5dSTetsuo Handa 		const struct tomoyo_path_info *old_comment;
6792a086e5dSTetsuo Handa 		if (!new_comment)
6802a086e5dSTetsuo Handa 			return -ENOMEM;
6812a086e5dSTetsuo Handa 		spin_lock(&lock);
6822a086e5dSTetsuo Handa 		old_comment = profile->comment;
6832a086e5dSTetsuo Handa 		profile->comment = new_comment;
6842a086e5dSTetsuo Handa 		spin_unlock(&lock);
685bf24fb01STetsuo Handa 		tomoyo_put_name(old_comment);
6869590837bSKentaro Takeda 		return 0;
6879590837bSKentaro Takeda 	}
688d5ca1725STetsuo Handa 	if (!strcmp(data, "PREFERENCE")) {
689d5ca1725STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_PREF; i++)
690d5ca1725STetsuo Handa 			tomoyo_set_uint(&profile->pref[i], cp,
691d5ca1725STetsuo Handa 					tomoyo_pref_keywords[i]);
692d5ca1725STetsuo Handa 		return 0;
6939590837bSKentaro Takeda 	}
694d5ca1725STetsuo Handa 	return tomoyo_set_mode(data, cp, profile);
695f23571e8STetsuo Handa }
696f23571e8STetsuo Handa 
697eadd99ccSTetsuo Handa /**
698eadd99ccSTetsuo Handa  * tomoyo_print_config - Print mode for specified functionality.
699eadd99ccSTetsuo Handa  *
700eadd99ccSTetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
701eadd99ccSTetsuo Handa  * @config: Mode for that functionality.
702eadd99ccSTetsuo Handa  *
703eadd99ccSTetsuo Handa  * Returns nothing.
704eadd99ccSTetsuo Handa  *
705eadd99ccSTetsuo Handa  * Caller prints functionality's name.
706eadd99ccSTetsuo Handa  */
707f23571e8STetsuo Handa static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
708f23571e8STetsuo Handa {
709eadd99ccSTetsuo Handa 	tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
710eadd99ccSTetsuo Handa 			 tomoyo_mode[config & 3],
711eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
712eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
713f23571e8STetsuo Handa }
714f23571e8STetsuo Handa 
7159590837bSKentaro Takeda /**
71657c2590fSTetsuo Handa  * tomoyo_read_profile - Read profile table.
7179590837bSKentaro Takeda  *
7189590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
719eadd99ccSTetsuo Handa  *
720eadd99ccSTetsuo Handa  * Returns nothing.
7219590837bSKentaro Takeda  */
7228fbe71f0STetsuo Handa static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
7239590837bSKentaro Takeda {
724f23571e8STetsuo Handa 	u8 index;
725bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
726bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
727f23571e8STetsuo Handa 	const struct tomoyo_profile *profile;
728bd03a3e4STetsuo Handa 	if (head->r.eof)
729bd03a3e4STetsuo Handa 		return;
730f23571e8STetsuo Handa  next:
731f23571e8STetsuo Handa 	index = head->r.index;
732bd03a3e4STetsuo Handa 	profile = ns->profile_ptr[index];
733f23571e8STetsuo Handa 	switch (head->r.step) {
734f23571e8STetsuo Handa 	case 0:
735bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
736bd03a3e4STetsuo Handa 		tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
737bd03a3e4STetsuo Handa 				 ns->profile_version);
738f23571e8STetsuo Handa 		head->r.step++;
739f23571e8STetsuo Handa 		break;
740f23571e8STetsuo Handa 	case 1:
741f23571e8STetsuo Handa 		for ( ; head->r.index < TOMOYO_MAX_PROFILES;
742f23571e8STetsuo Handa 		      head->r.index++)
743bd03a3e4STetsuo Handa 			if (ns->profile_ptr[head->r.index])
744f23571e8STetsuo Handa 				break;
745f23571e8STetsuo Handa 		if (head->r.index == TOMOYO_MAX_PROFILES)
746f23571e8STetsuo Handa 			return;
747f23571e8STetsuo Handa 		head->r.step++;
748f23571e8STetsuo Handa 		break;
749f23571e8STetsuo Handa 	case 2:
750f23571e8STetsuo Handa 		{
751d5ca1725STetsuo Handa 			u8 i;
752f23571e8STetsuo Handa 			const struct tomoyo_path_info *comment =
753f23571e8STetsuo Handa 				profile->comment;
754bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
755f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-COMMENT=", index);
756f23571e8STetsuo Handa 			tomoyo_set_string(head, comment ? comment->name : "");
757f23571e8STetsuo Handa 			tomoyo_set_lf(head);
758d5ca1725STetsuo Handa 			tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
759d5ca1725STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_PREF; i++)
760d5ca1725STetsuo Handa 				tomoyo_io_printf(head, "%s=%u ",
761d5ca1725STetsuo Handa 						 tomoyo_pref_keywords[i],
762d5ca1725STetsuo Handa 						 profile->pref[i]);
763d5ca1725STetsuo Handa 			tomoyo_set_string(head, "}\n");
764f23571e8STetsuo Handa 			head->r.step++;
765f23571e8STetsuo Handa 		}
766f23571e8STetsuo Handa 		break;
767f23571e8STetsuo Handa 	case 3:
768f23571e8STetsuo Handa 		{
769bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
770f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
771f23571e8STetsuo Handa 			tomoyo_print_config(head, profile->default_config);
772f23571e8STetsuo Handa 			head->r.bit = 0;
773f23571e8STetsuo Handa 			head->r.step++;
774f23571e8STetsuo Handa 		}
775f23571e8STetsuo Handa 		break;
776f23571e8STetsuo Handa 	case 4:
777f23571e8STetsuo Handa 		for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
778f23571e8STetsuo Handa 			      + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
779f23571e8STetsuo Handa 			const u8 i = head->r.bit;
780f23571e8STetsuo Handa 			const u8 config = profile->config[i];
78157c2590fSTetsuo Handa 			if (config == TOMOYO_CONFIG_USE_DEFAULT)
7829590837bSKentaro Takeda 				continue;
783bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
7842c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX)
7852c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
7862c47ab93STetsuo Handa 						 index,
7872c47ab93STetsuo Handa 						 tomoyo_category_keywords
7882c47ab93STetsuo Handa 						 [tomoyo_index2category[i]],
7892c47ab93STetsuo Handa 						 tomoyo_mac_keywords[i]);
7902c47ab93STetsuo Handa 			else
7912c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s", index,
792f23571e8STetsuo Handa 						 tomoyo_mac_keywords[i]);
793f23571e8STetsuo Handa 			tomoyo_print_config(head, config);
794f23571e8STetsuo Handa 			head->r.bit++;
7959590837bSKentaro Takeda 			break;
7969590837bSKentaro Takeda 		}
797f23571e8STetsuo Handa 		if (head->r.bit == TOMOYO_MAX_MAC_INDEX
798f23571e8STetsuo Handa 		    + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
799f23571e8STetsuo Handa 			head->r.index++;
800f23571e8STetsuo Handa 			head->r.step = 1;
801f23571e8STetsuo Handa 		}
802f23571e8STetsuo Handa 		break;
803f23571e8STetsuo Handa 	}
804f23571e8STetsuo Handa 	if (tomoyo_flush(head))
805f23571e8STetsuo Handa 		goto next;
8069590837bSKentaro Takeda }
8079590837bSKentaro Takeda 
8080f2a55d5STetsuo Handa /**
8090f2a55d5STetsuo Handa  * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
8100f2a55d5STetsuo Handa  *
8110f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
8120f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
8130f2a55d5STetsuo Handa  *
8140f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
8150f2a55d5STetsuo Handa  */
816e2bf6907STetsuo Handa static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
81736f5e1ffSTetsuo Handa 				const struct tomoyo_acl_head *b)
81836f5e1ffSTetsuo Handa {
819e2bf6907STetsuo Handa 	return container_of(a, struct tomoyo_manager, head)->manager ==
820e2bf6907STetsuo Handa 		container_of(b, struct tomoyo_manager, head)->manager;
82136f5e1ffSTetsuo Handa }
82236f5e1ffSTetsuo Handa 
8239590837bSKentaro Takeda /**
8249590837bSKentaro Takeda  * tomoyo_update_manager_entry - Add a manager entry.
8259590837bSKentaro Takeda  *
8269590837bSKentaro Takeda  * @manager:   The path to manager or the domainnamme.
8279590837bSKentaro Takeda  * @is_delete: True if it is a delete request.
8289590837bSKentaro Takeda  *
8299590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
830fdb8ebb7STetsuo Handa  *
831fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8329590837bSKentaro Takeda  */
8339590837bSKentaro Takeda static int tomoyo_update_manager_entry(const char *manager,
8349590837bSKentaro Takeda 				       const bool is_delete)
8359590837bSKentaro Takeda {
836e2bf6907STetsuo Handa 	struct tomoyo_manager e = { };
837a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
838bd03a3e4STetsuo Handa 		/* .ns = &tomoyo_kernel_namespace, */
839a238cf5bSTetsuo Handa 		.is_delete = is_delete,
840bd03a3e4STetsuo Handa 		.list = &tomoyo_kernel_namespace.
841bd03a3e4STetsuo Handa 		policy_list[TOMOYO_ID_MANAGER],
842a238cf5bSTetsuo Handa 	};
843a238cf5bSTetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
84475093152STetsuo Handa 	if (tomoyo_domain_def(manager)) {
84575093152STetsuo Handa 		if (!tomoyo_correct_domain(manager))
8469590837bSKentaro Takeda 			return -EINVAL;
8479e4b50e9STetsuo Handa 		e.is_domain = true;
8489590837bSKentaro Takeda 	} else {
84975093152STetsuo Handa 		if (!tomoyo_correct_path(manager))
8509590837bSKentaro Takeda 			return -EINVAL;
8519590837bSKentaro Takeda 	}
8529e4b50e9STetsuo Handa 	e.manager = tomoyo_get_name(manager);
853a238cf5bSTetsuo Handa 	if (e.manager) {
854a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), &param,
855e2bf6907STetsuo Handa 					     tomoyo_same_manager);
8569e4b50e9STetsuo Handa 		tomoyo_put_name(e.manager);
857a238cf5bSTetsuo Handa 	}
8589590837bSKentaro Takeda 	return error;
8599590837bSKentaro Takeda }
8609590837bSKentaro Takeda 
8619590837bSKentaro Takeda /**
862e2bf6907STetsuo Handa  * tomoyo_write_manager - Write manager policy.
8639590837bSKentaro Takeda  *
8649590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8659590837bSKentaro Takeda  *
8669590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
867fdb8ebb7STetsuo Handa  *
868fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8699590837bSKentaro Takeda  */
870e2bf6907STetsuo Handa static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
8719590837bSKentaro Takeda {
8729590837bSKentaro Takeda 	char *data = head->write_buf;
8739590837bSKentaro Takeda 
8749590837bSKentaro Takeda 	if (!strcmp(data, "manage_by_non_root")) {
875bd03a3e4STetsuo Handa 		tomoyo_manage_by_non_root = !head->w.is_delete;
8769590837bSKentaro Takeda 		return 0;
8779590837bSKentaro Takeda 	}
878bd03a3e4STetsuo Handa 	return tomoyo_update_manager_entry(data, head->w.is_delete);
8799590837bSKentaro Takeda }
8809590837bSKentaro Takeda 
8819590837bSKentaro Takeda /**
882e2bf6907STetsuo Handa  * tomoyo_read_manager - Read manager policy.
8839590837bSKentaro Takeda  *
8849590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8859590837bSKentaro Takeda  *
886fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8879590837bSKentaro Takeda  */
888e2bf6907STetsuo Handa static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
8899590837bSKentaro Takeda {
890f23571e8STetsuo Handa 	if (head->r.eof)
8918fbe71f0STetsuo Handa 		return;
892bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
893bd03a3e4STetsuo Handa 			     policy_list[TOMOYO_ID_MANAGER]) {
894e2bf6907STetsuo Handa 		struct tomoyo_manager *ptr =
895f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), head.list);
89682e0f001STetsuo Handa 		if (ptr->head.is_deleted)
8979590837bSKentaro Takeda 			continue;
898f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
899f23571e8STetsuo Handa 			return;
900f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->manager->name);
901f23571e8STetsuo Handa 		tomoyo_set_lf(head);
9029590837bSKentaro Takeda 	}
903f23571e8STetsuo Handa 	head->r.eof = true;
9049590837bSKentaro Takeda }
9059590837bSKentaro Takeda 
9069590837bSKentaro Takeda /**
907e2bf6907STetsuo Handa  * tomoyo_manager - Check whether the current process is a policy manager.
9089590837bSKentaro Takeda  *
9099590837bSKentaro Takeda  * Returns true if the current process is permitted to modify policy
9109590837bSKentaro Takeda  * via /sys/kernel/security/tomoyo/ interface.
911fdb8ebb7STetsuo Handa  *
912fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9139590837bSKentaro Takeda  */
914e2bf6907STetsuo Handa static bool tomoyo_manager(void)
9159590837bSKentaro Takeda {
916e2bf6907STetsuo Handa 	struct tomoyo_manager *ptr;
9179590837bSKentaro Takeda 	const char *exe;
9189590837bSKentaro Takeda 	const struct task_struct *task = current;
9199590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
9209590837bSKentaro Takeda 	bool found = false;
9219590837bSKentaro Takeda 
9229590837bSKentaro Takeda 	if (!tomoyo_policy_loaded)
9239590837bSKentaro Takeda 		return true;
9249590837bSKentaro Takeda 	if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
9259590837bSKentaro Takeda 		return false;
926bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
927bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
92882e0f001STetsuo Handa 		if (!ptr->head.is_deleted && ptr->is_domain
9299590837bSKentaro Takeda 		    && !tomoyo_pathcmp(domainname, ptr->manager)) {
9309590837bSKentaro Takeda 			found = true;
9319590837bSKentaro Takeda 			break;
9329590837bSKentaro Takeda 		}
9339590837bSKentaro Takeda 	}
9349590837bSKentaro Takeda 	if (found)
9359590837bSKentaro Takeda 		return true;
9369590837bSKentaro Takeda 	exe = tomoyo_get_exe();
9379590837bSKentaro Takeda 	if (!exe)
9389590837bSKentaro Takeda 		return false;
939bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
940bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
94182e0f001STetsuo Handa 		if (!ptr->head.is_deleted && !ptr->is_domain
9429590837bSKentaro Takeda 		    && !strcmp(exe, ptr->manager->name)) {
9439590837bSKentaro Takeda 			found = true;
9449590837bSKentaro Takeda 			break;
9459590837bSKentaro Takeda 		}
9469590837bSKentaro Takeda 	}
9479590837bSKentaro Takeda 	if (!found) { /* Reduce error messages. */
9489590837bSKentaro Takeda 		static pid_t last_pid;
9499590837bSKentaro Takeda 		const pid_t pid = current->pid;
9509590837bSKentaro Takeda 		if (last_pid != pid) {
9519590837bSKentaro Takeda 			printk(KERN_WARNING "%s ( %s ) is not permitted to "
9529590837bSKentaro Takeda 			       "update policies.\n", domainname->name, exe);
9539590837bSKentaro Takeda 			last_pid = pid;
9549590837bSKentaro Takeda 		}
9559590837bSKentaro Takeda 	}
9568e2d39a1STetsuo Handa 	kfree(exe);
9579590837bSKentaro Takeda 	return found;
9589590837bSKentaro Takeda }
9599590837bSKentaro Takeda 
9609590837bSKentaro Takeda /**
961bd03a3e4STetsuo Handa  * tomoyo_select_domain - Parse select command.
9629590837bSKentaro Takeda  *
9639590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9649590837bSKentaro Takeda  * @data: String to parse.
9659590837bSKentaro Takeda  *
9669590837bSKentaro Takeda  * Returns true on success, false otherwise.
967fdb8ebb7STetsuo Handa  *
968fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9699590837bSKentaro Takeda  */
970bd03a3e4STetsuo Handa static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
971bd03a3e4STetsuo Handa 				 const char *data)
9729590837bSKentaro Takeda {
9739590837bSKentaro Takeda 	unsigned int pid;
9749590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
9759b244373STetsuo Handa 	bool global_pid = false;
976bd03a3e4STetsuo Handa 	if (strncmp(data, "select ", 7))
977bd03a3e4STetsuo Handa 		return false;
978bd03a3e4STetsuo Handa 	data += 7;
9799b244373STetsuo Handa 	if (sscanf(data, "pid=%u", &pid) == 1 ||
9809b244373STetsuo Handa 	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9819590837bSKentaro Takeda 		struct task_struct *p;
9821fcdc7c5STetsuo Handa 		rcu_read_lock();
9839590837bSKentaro Takeda 		read_lock(&tasklist_lock);
9849b244373STetsuo Handa 		if (global_pid)
9859b244373STetsuo Handa 			p = find_task_by_pid_ns(pid, &init_pid_ns);
9869b244373STetsuo Handa 		else
9879590837bSKentaro Takeda 			p = find_task_by_vpid(pid);
9889590837bSKentaro Takeda 		if (p)
9899590837bSKentaro Takeda 			domain = tomoyo_real_domain(p);
9909590837bSKentaro Takeda 		read_unlock(&tasklist_lock);
9911fcdc7c5STetsuo Handa 		rcu_read_unlock();
9929590837bSKentaro Takeda 	} else if (!strncmp(data, "domain=", 7)) {
99375093152STetsuo Handa 		if (tomoyo_domain_def(data + 7))
9949590837bSKentaro Takeda 			domain = tomoyo_find_domain(data + 7);
9959590837bSKentaro Takeda 	} else
9969590837bSKentaro Takeda 		return false;
9970df7e8b8STetsuo Handa 	head->w.domain = domain;
9989590837bSKentaro Takeda 	/* Accessing read_buf is safe because head->io_sem is held. */
9999590837bSKentaro Takeda 	if (!head->read_buf)
10009590837bSKentaro Takeda 		return true; /* Do nothing if open(O_WRONLY). */
1001f23571e8STetsuo Handa 	memset(&head->r, 0, sizeof(head->r));
1002f23571e8STetsuo Handa 	head->r.print_this_domain_only = true;
100368eda8f5SDan Carpenter 	if (domain)
1004f23571e8STetsuo Handa 		head->r.domain = &domain->list;
100568eda8f5SDan Carpenter 	else
100668eda8f5SDan Carpenter 		head->r.eof = 1;
10079590837bSKentaro Takeda 	tomoyo_io_printf(head, "# select %s\n", data);
1008475e6fa3STetsuo Handa 	if (domain && domain->is_deleted)
10099590837bSKentaro Takeda 		tomoyo_io_printf(head, "# This is a deleted domain.\n");
10109590837bSKentaro Takeda 	return true;
10119590837bSKentaro Takeda }
10129590837bSKentaro Takeda 
10139590837bSKentaro Takeda /**
1014731d37aaSTetsuo Handa  * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1015731d37aaSTetsuo Handa  *
1016731d37aaSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_info".
1017731d37aaSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_info".
1018731d37aaSTetsuo Handa  *
1019731d37aaSTetsuo Handa  * Returns true if @a == @b, false otherwise.
1020731d37aaSTetsuo Handa  */
1021731d37aaSTetsuo Handa static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1022731d37aaSTetsuo Handa 			      const struct tomoyo_acl_info *b)
1023731d37aaSTetsuo Handa {
1024731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1025731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1026731d37aaSTetsuo Handa 	return p1->domainname == p2->domainname;
1027731d37aaSTetsuo Handa }
1028731d37aaSTetsuo Handa 
1029731d37aaSTetsuo Handa /**
1030731d37aaSTetsuo Handa  * tomoyo_write_task - Update task related list.
1031731d37aaSTetsuo Handa  *
1032731d37aaSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
1033731d37aaSTetsuo Handa  *
1034731d37aaSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1035731d37aaSTetsuo Handa  *
1036731d37aaSTetsuo Handa  * Caller holds tomoyo_read_lock().
1037731d37aaSTetsuo Handa  */
1038731d37aaSTetsuo Handa static int tomoyo_write_task(struct tomoyo_acl_param *param)
1039731d37aaSTetsuo Handa {
1040731d37aaSTetsuo Handa 	int error = -EINVAL;
1041731d37aaSTetsuo Handa 	if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1042731d37aaSTetsuo Handa 		struct tomoyo_task_acl e = {
1043731d37aaSTetsuo Handa 			.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1044731d37aaSTetsuo Handa 			.domainname = tomoyo_get_domainname(param),
1045731d37aaSTetsuo Handa 		};
1046731d37aaSTetsuo Handa 		if (e.domainname)
1047731d37aaSTetsuo Handa 			error = tomoyo_update_domain(&e.head, sizeof(e), param,
1048731d37aaSTetsuo Handa 						     tomoyo_same_task_acl,
1049731d37aaSTetsuo Handa 						     NULL);
1050731d37aaSTetsuo Handa 		tomoyo_put_name(e.domainname);
1051731d37aaSTetsuo Handa 	}
1052731d37aaSTetsuo Handa 	return error;
1053731d37aaSTetsuo Handa }
1054731d37aaSTetsuo Handa 
1055731d37aaSTetsuo Handa /**
1056ccf135f5STetsuo Handa  * tomoyo_delete_domain - Delete a domain.
1057ccf135f5STetsuo Handa  *
1058ccf135f5STetsuo Handa  * @domainname: The name of domain.
1059ccf135f5STetsuo Handa  *
1060ccf135f5STetsuo Handa  * Returns 0.
1061fdb8ebb7STetsuo Handa  *
1062fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1063ccf135f5STetsuo Handa  */
1064ccf135f5STetsuo Handa static int tomoyo_delete_domain(char *domainname)
1065ccf135f5STetsuo Handa {
1066ccf135f5STetsuo Handa 	struct tomoyo_domain_info *domain;
1067ccf135f5STetsuo Handa 	struct tomoyo_path_info name;
1068ccf135f5STetsuo Handa 
1069ccf135f5STetsuo Handa 	name.name = domainname;
1070ccf135f5STetsuo Handa 	tomoyo_fill_path_info(&name);
107129282381STetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
107229282381STetsuo Handa 		return 0;
1073ccf135f5STetsuo Handa 	/* Is there an active domain? */
1074fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1075ccf135f5STetsuo Handa 		/* Never delete tomoyo_kernel_domain */
1076ccf135f5STetsuo Handa 		if (domain == &tomoyo_kernel_domain)
1077ccf135f5STetsuo Handa 			continue;
1078ccf135f5STetsuo Handa 		if (domain->is_deleted ||
1079ccf135f5STetsuo Handa 		    tomoyo_pathcmp(domain->domainname, &name))
1080ccf135f5STetsuo Handa 			continue;
1081ccf135f5STetsuo Handa 		domain->is_deleted = true;
1082ccf135f5STetsuo Handa 		break;
1083ccf135f5STetsuo Handa 	}
1084f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
1085ccf135f5STetsuo Handa 	return 0;
1086ccf135f5STetsuo Handa }
1087ccf135f5STetsuo Handa 
1088ccf135f5STetsuo Handa /**
1089e2bf6907STetsuo Handa  * tomoyo_write_domain2 - Write domain policy.
109017fcfbd9STetsuo Handa  *
1091bd03a3e4STetsuo Handa  * @ns:        Pointer to "struct tomoyo_policy_namespace".
1092a238cf5bSTetsuo Handa  * @list:      Pointer to "struct list_head".
1093a238cf5bSTetsuo Handa  * @data:      Policy to be interpreted.
1094a238cf5bSTetsuo Handa  * @is_delete: True if it is a delete request.
109517fcfbd9STetsuo Handa  *
109617fcfbd9STetsuo Handa  * Returns 0 on success, negative value otherwise.
109717fcfbd9STetsuo Handa  *
109817fcfbd9STetsuo Handa  * Caller holds tomoyo_read_lock().
109917fcfbd9STetsuo Handa  */
1100bd03a3e4STetsuo Handa static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1101bd03a3e4STetsuo Handa 				struct list_head *list, char *data,
110217fcfbd9STetsuo Handa 				const bool is_delete)
110317fcfbd9STetsuo Handa {
1104a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1105bd03a3e4STetsuo Handa 		.ns = ns,
1106a238cf5bSTetsuo Handa 		.list = list,
1107a238cf5bSTetsuo Handa 		.data = data,
1108a238cf5bSTetsuo Handa 		.is_delete = is_delete,
1109a238cf5bSTetsuo Handa 	};
1110a238cf5bSTetsuo Handa 	static const struct {
1111a238cf5bSTetsuo Handa 		const char *keyword;
1112a238cf5bSTetsuo Handa 		int (*write) (struct tomoyo_acl_param *);
1113731d37aaSTetsuo Handa 	} tomoyo_callback[5] = {
1114a238cf5bSTetsuo Handa 		{ "file ", tomoyo_write_file },
1115059d84dbSTetsuo Handa 		{ "network inet ", tomoyo_write_inet_network },
1116059d84dbSTetsuo Handa 		{ "network unix ", tomoyo_write_unix_network },
1117d58e0da8STetsuo Handa 		{ "misc ", tomoyo_write_misc },
1118731d37aaSTetsuo Handa 		{ "task ", tomoyo_write_task },
1119a238cf5bSTetsuo Handa 	};
1120a238cf5bSTetsuo Handa 	u8 i;
1121d58e0da8STetsuo Handa 
1122d58e0da8STetsuo Handa 	for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
1123a238cf5bSTetsuo Handa 		if (!tomoyo_str_starts(&param.data,
1124a238cf5bSTetsuo Handa 				       tomoyo_callback[i].keyword))
1125a238cf5bSTetsuo Handa 			continue;
1126a238cf5bSTetsuo Handa 		return tomoyo_callback[i].write(&param);
1127a238cf5bSTetsuo Handa 	}
1128a238cf5bSTetsuo Handa 	return -EINVAL;
112917fcfbd9STetsuo Handa }
113017fcfbd9STetsuo Handa 
11312c47ab93STetsuo Handa /* String table for domain flags. */
11322c47ab93STetsuo Handa const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
11332c47ab93STetsuo Handa 	[TOMOYO_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
11342c47ab93STetsuo Handa 	[TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
11352c47ab93STetsuo Handa };
11362c47ab93STetsuo Handa 
113717fcfbd9STetsuo Handa /**
1138e2bf6907STetsuo Handa  * tomoyo_write_domain - Write domain policy.
11399590837bSKentaro Takeda  *
11409590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
11419590837bSKentaro Takeda  *
11429590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1143fdb8ebb7STetsuo Handa  *
1144fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
11459590837bSKentaro Takeda  */
1146e2bf6907STetsuo Handa static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
11479590837bSKentaro Takeda {
11489590837bSKentaro Takeda 	char *data = head->write_buf;
1149bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
11500df7e8b8STetsuo Handa 	struct tomoyo_domain_info *domain = head->w.domain;
1151bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1152bd03a3e4STetsuo Handa 	bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
11539590837bSKentaro Takeda 	unsigned int profile;
1154bd03a3e4STetsuo Handa 	if (*data == '<') {
11559590837bSKentaro Takeda 		domain = NULL;
11569590837bSKentaro Takeda 		if (is_delete)
11579590837bSKentaro Takeda 			tomoyo_delete_domain(data);
1158fdb8ebb7STetsuo Handa 		else if (is_select)
11599590837bSKentaro Takeda 			domain = tomoyo_find_domain(data);
1160fdb8ebb7STetsuo Handa 		else
1161bd03a3e4STetsuo Handa 			domain = tomoyo_assign_domain(data, false);
11620df7e8b8STetsuo Handa 		head->w.domain = domain;
11639590837bSKentaro Takeda 		return 0;
11649590837bSKentaro Takeda 	}
11659590837bSKentaro Takeda 	if (!domain)
11669590837bSKentaro Takeda 		return -EINVAL;
1167bd03a3e4STetsuo Handa 	ns = domain->ns;
1168b5bc60b4STetsuo Handa 	if (sscanf(data, "use_profile %u", &profile) == 1
11699590837bSKentaro Takeda 	    && profile < TOMOYO_MAX_PROFILES) {
1170bd03a3e4STetsuo Handa 		if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
11719590837bSKentaro Takeda 			domain->profile = (u8) profile;
11729590837bSKentaro Takeda 		return 0;
11739590837bSKentaro Takeda 	}
117432997144STetsuo Handa 	if (sscanf(data, "use_group %u\n", &profile) == 1
117532997144STetsuo Handa 	    && profile < TOMOYO_MAX_ACL_GROUPS) {
117632997144STetsuo Handa 		if (!is_delete)
117732997144STetsuo Handa 			domain->group = (u8) profile;
117832997144STetsuo Handa 		return 0;
117932997144STetsuo Handa 	}
11802c47ab93STetsuo Handa 	for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
11812c47ab93STetsuo Handa 		const char *cp = tomoyo_dif[profile];
11822c47ab93STetsuo Handa 		if (strncmp(data, cp, strlen(cp) - 1))
11832c47ab93STetsuo Handa 			continue;
11842c47ab93STetsuo Handa 		domain->flags[profile] = !is_delete;
11859b244373STetsuo Handa 		return 0;
11869b244373STetsuo Handa 	}
1187bd03a3e4STetsuo Handa 	return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1188bd03a3e4STetsuo Handa 				    is_delete);
11899590837bSKentaro Takeda }
11909590837bSKentaro Takeda 
11919590837bSKentaro Takeda /**
11922066a361STetsuo Handa  * tomoyo_print_condition - Print condition part.
11932066a361STetsuo Handa  *
11942066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
11952066a361STetsuo Handa  * @cond: Pointer to "struct tomoyo_condition".
11962066a361STetsuo Handa  *
11972066a361STetsuo Handa  * Returns true on success, false otherwise.
11982066a361STetsuo Handa  */
11992066a361STetsuo Handa static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
12002066a361STetsuo Handa 				   const struct tomoyo_condition *cond)
12012066a361STetsuo Handa {
12022066a361STetsuo Handa 	switch (head->r.cond_step) {
12032066a361STetsuo Handa 	case 0:
12042066a361STetsuo Handa 		head->r.cond_index = 0;
12052066a361STetsuo Handa 		head->r.cond_step++;
1206*6bce98edSTetsuo Handa 		if (cond->transit) {
1207*6bce98edSTetsuo Handa 			tomoyo_set_space(head);
1208*6bce98edSTetsuo Handa 			tomoyo_set_string(head, cond->transit->name);
1209*6bce98edSTetsuo Handa 		}
12102066a361STetsuo Handa 		/* fall through */
12112066a361STetsuo Handa 	case 1:
12122066a361STetsuo Handa 		{
12132066a361STetsuo Handa 			const u16 condc = cond->condc;
12142066a361STetsuo Handa 			const struct tomoyo_condition_element *condp =
12152066a361STetsuo Handa 				(typeof(condp)) (cond + 1);
12162066a361STetsuo Handa 			const struct tomoyo_number_union *numbers_p =
12172066a361STetsuo Handa 				(typeof(numbers_p)) (condp + condc);
12182ca9bf45STetsuo Handa 			const struct tomoyo_name_union *names_p =
12192ca9bf45STetsuo Handa 				(typeof(names_p))
12202ca9bf45STetsuo Handa 				(numbers_p + cond->numbers_count);
12215b636857STetsuo Handa 			const struct tomoyo_argv *argv =
12225b636857STetsuo Handa 				(typeof(argv)) (names_p + cond->names_count);
12235b636857STetsuo Handa 			const struct tomoyo_envp *envp =
12245b636857STetsuo Handa 				(typeof(envp)) (argv + cond->argc);
12252066a361STetsuo Handa 			u16 skip;
12262066a361STetsuo Handa 			for (skip = 0; skip < head->r.cond_index; skip++) {
12272066a361STetsuo Handa 				const u8 left = condp->left;
12282066a361STetsuo Handa 				const u8 right = condp->right;
12292066a361STetsuo Handa 				condp++;
12302066a361STetsuo Handa 				switch (left) {
12315b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12325b636857STetsuo Handa 					argv++;
12335b636857STetsuo Handa 					continue;
12345b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12355b636857STetsuo Handa 					envp++;
12365b636857STetsuo Handa 					continue;
12372066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12382066a361STetsuo Handa 					numbers_p++;
12392066a361STetsuo Handa 					break;
12402066a361STetsuo Handa 				}
12412066a361STetsuo Handa 				switch (right) {
12422ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
12432ca9bf45STetsuo Handa 					names_p++;
12442ca9bf45STetsuo Handa 					break;
12452066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12462066a361STetsuo Handa 					numbers_p++;
12472066a361STetsuo Handa 					break;
12482066a361STetsuo Handa 				}
12492066a361STetsuo Handa 			}
12502066a361STetsuo Handa 			while (head->r.cond_index < condc) {
12512066a361STetsuo Handa 				const u8 match = condp->equals;
12522066a361STetsuo Handa 				const u8 left = condp->left;
12532066a361STetsuo Handa 				const u8 right = condp->right;
12542066a361STetsuo Handa 				if (!tomoyo_flush(head))
12552066a361STetsuo Handa 					return false;
12562066a361STetsuo Handa 				condp++;
12572066a361STetsuo Handa 				head->r.cond_index++;
12582066a361STetsuo Handa 				tomoyo_set_space(head);
12592066a361STetsuo Handa 				switch (left) {
12605b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12615b636857STetsuo Handa 					tomoyo_io_printf(head,
12625b636857STetsuo Handa 							 "exec.argv[%lu]%s=\"",
12635b636857STetsuo Handa 							 argv->index, argv->
12645b636857STetsuo Handa 							 is_not ? "!" : "");
12655b636857STetsuo Handa 					tomoyo_set_string(head,
12665b636857STetsuo Handa 							  argv->value->name);
12675b636857STetsuo Handa 					tomoyo_set_string(head, "\"");
12685b636857STetsuo Handa 					argv++;
12695b636857STetsuo Handa 					continue;
12705b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12715b636857STetsuo Handa 					tomoyo_set_string(head,
12725b636857STetsuo Handa 							  "exec.envp[\"");
12735b636857STetsuo Handa 					tomoyo_set_string(head,
12745b636857STetsuo Handa 							  envp->name->name);
12755b636857STetsuo Handa 					tomoyo_io_printf(head, "\"]%s=", envp->
12765b636857STetsuo Handa 							 is_not ? "!" : "");
12775b636857STetsuo Handa 					if (envp->value) {
12785b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12795b636857STetsuo Handa 						tomoyo_set_string(head, envp->
12805b636857STetsuo Handa 								  value->name);
12815b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12825b636857STetsuo Handa 					} else {
12835b636857STetsuo Handa 						tomoyo_set_string(head,
12845b636857STetsuo Handa 								  "NULL");
12855b636857STetsuo Handa 					}
12865b636857STetsuo Handa 					envp++;
12875b636857STetsuo Handa 					continue;
12882066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12892066a361STetsuo Handa 					tomoyo_print_number_union_nospace
12902066a361STetsuo Handa 						(head, numbers_p++);
12912066a361STetsuo Handa 					break;
12922066a361STetsuo Handa 				default:
12932066a361STetsuo Handa 					tomoyo_set_string(head,
12942066a361STetsuo Handa 					       tomoyo_condition_keyword[left]);
12952066a361STetsuo Handa 					break;
12962066a361STetsuo Handa 				}
12972066a361STetsuo Handa 				tomoyo_set_string(head, match ? "=" : "!=");
12982066a361STetsuo Handa 				switch (right) {
12992ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
13002ca9bf45STetsuo Handa 					tomoyo_print_name_union_quoted
13012ca9bf45STetsuo Handa 						(head, names_p++);
13022ca9bf45STetsuo Handa 					break;
13032066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13042066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13052066a361STetsuo Handa 						(head, numbers_p++);
13062066a361STetsuo Handa 					break;
13072066a361STetsuo Handa 				default:
13082066a361STetsuo Handa 					tomoyo_set_string(head,
13092066a361STetsuo Handa 					  tomoyo_condition_keyword[right]);
13102066a361STetsuo Handa 					break;
13112066a361STetsuo Handa 				}
13122066a361STetsuo Handa 			}
13132066a361STetsuo Handa 		}
13142066a361STetsuo Handa 		head->r.cond_step++;
13152066a361STetsuo Handa 		/* fall through */
13162066a361STetsuo Handa 	case 2:
13172066a361STetsuo Handa 		if (!tomoyo_flush(head))
13182066a361STetsuo Handa 			break;
13192066a361STetsuo Handa 		head->r.cond_step++;
13202066a361STetsuo Handa 		/* fall through */
13212066a361STetsuo Handa 	case 3:
13221f067a68STetsuo Handa 		if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13231f067a68STetsuo Handa 			tomoyo_io_printf(head, " grant_log=%s",
13241f067a68STetsuo Handa 					 tomoyo_yesno(cond->grant_log ==
13251f067a68STetsuo Handa 						      TOMOYO_GRANTLOG_YES));
13262066a361STetsuo Handa 		tomoyo_set_lf(head);
13272066a361STetsuo Handa 		return true;
13282066a361STetsuo Handa 	}
13292066a361STetsuo Handa 	return false;
13302066a361STetsuo Handa }
13312066a361STetsuo Handa 
13322066a361STetsuo Handa /**
133332997144STetsuo Handa  * tomoyo_set_group - Print "acl_group " header keyword and category name.
13349590837bSKentaro Takeda  *
13350d2171d7STetsuo Handa  * @head:     Pointer to "struct tomoyo_io_buffer".
13360d2171d7STetsuo Handa  * @category: Category name.
13379590837bSKentaro Takeda  *
13380d2171d7STetsuo Handa  * Returns nothing.
13399590837bSKentaro Takeda  */
13400d2171d7STetsuo Handa static void tomoyo_set_group(struct tomoyo_io_buffer *head,
13410d2171d7STetsuo Handa 			     const char *category)
13429590837bSKentaro Takeda {
1343bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1344bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
134532997144STetsuo Handa 		tomoyo_io_printf(head, "acl_group %u ",
134632997144STetsuo Handa 				 head->r.acl_group_index);
1347bd03a3e4STetsuo Handa 	}
13480d2171d7STetsuo Handa 	tomoyo_set_string(head, category);
13492106ccd9STetsuo Handa }
13502106ccd9STetsuo Handa 
13512106ccd9STetsuo Handa /**
13529590837bSKentaro Takeda  * tomoyo_print_entry - Print an ACL entry.
13539590837bSKentaro Takeda  *
13549590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
13555db5a39bSTetsuo Handa  * @acl:  Pointer to an ACL entry.
13569590837bSKentaro Takeda  *
13579590837bSKentaro Takeda  * Returns true on success, false otherwise.
13589590837bSKentaro Takeda  */
13599590837bSKentaro Takeda static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13605db5a39bSTetsuo Handa 			       struct tomoyo_acl_info *acl)
13619590837bSKentaro Takeda {
13625db5a39bSTetsuo Handa 	const u8 acl_type = acl->type;
13630d2171d7STetsuo Handa 	bool first = true;
1364f23571e8STetsuo Handa 	u8 bit;
13659590837bSKentaro Takeda 
13662066a361STetsuo Handa 	if (head->r.print_cond_part)
13672066a361STetsuo Handa 		goto print_cond_part;
13685db5a39bSTetsuo Handa 	if (acl->is_deleted)
1369237ab459STetsuo Handa 		return true;
1370f23571e8STetsuo Handa 	if (!tomoyo_flush(head))
1371f23571e8STetsuo Handa 		return false;
1372f23571e8STetsuo Handa 	else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
13735db5a39bSTetsuo Handa 		struct tomoyo_path_acl *ptr =
13745db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
13755db5a39bSTetsuo Handa 		const u16 perm = ptr->perm;
13760d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13775db5a39bSTetsuo Handa 			if (!(perm & (1 << bit)))
13785db5a39bSTetsuo Handa 				continue;
1379bd03a3e4STetsuo Handa 			if (head->r.print_transition_related_only &&
13805db5a39bSTetsuo Handa 			    bit != TOMOYO_TYPE_EXECUTE)
13815db5a39bSTetsuo Handa 				continue;
13820d2171d7STetsuo Handa 			if (first) {
13830d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
13840d2171d7STetsuo Handa 				first = false;
13850d2171d7STetsuo Handa 			} else {
13860d2171d7STetsuo Handa 				tomoyo_set_slash(head);
13879590837bSKentaro Takeda 			}
13880d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_path_keyword[bit]);
13890d2171d7STetsuo Handa 		}
13900d2171d7STetsuo Handa 		if (first)
13910d2171d7STetsuo Handa 			return true;
1392f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1393731d37aaSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1394731d37aaSTetsuo Handa 		struct tomoyo_task_acl *ptr =
1395731d37aaSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1396731d37aaSTetsuo Handa 		tomoyo_set_group(head, "task ");
1397731d37aaSTetsuo Handa 		tomoyo_set_string(head, "manual_domain_transition ");
1398731d37aaSTetsuo Handa 		tomoyo_set_string(head, ptr->domainname->name);
1399bd03a3e4STetsuo Handa 	} else if (head->r.print_transition_related_only) {
1400063821c8STetsuo Handa 		return true;
14015db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
14025db5a39bSTetsuo Handa 		struct tomoyo_path2_acl *ptr =
14035db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14040d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14050d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14060d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14070d2171d7STetsuo Handa 				continue;
14080d2171d7STetsuo Handa 			if (first) {
14090d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14100d2171d7STetsuo Handa 				first = false;
14110d2171d7STetsuo Handa 			} else {
14120d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14130d2171d7STetsuo Handa 			}
14140d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14150d2171d7STetsuo Handa 					  [tomoyo_pp2mac[bit]]);
14160d2171d7STetsuo Handa 		}
14170d2171d7STetsuo Handa 		if (first)
14180d2171d7STetsuo Handa 			return true;
1419f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name1);
1420f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name2);
14215db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
14225db5a39bSTetsuo Handa 		struct tomoyo_path_number_acl *ptr =
14235db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14240d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14250d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14260d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14270d2171d7STetsuo Handa 				continue;
14280d2171d7STetsuo Handa 			if (first) {
14290d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14300d2171d7STetsuo Handa 				first = false;
14310d2171d7STetsuo Handa 			} else {
14320d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14330d2171d7STetsuo Handa 			}
14340d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14350d2171d7STetsuo Handa 					  [tomoyo_pn2mac[bit]]);
14360d2171d7STetsuo Handa 		}
14370d2171d7STetsuo Handa 		if (first)
14380d2171d7STetsuo Handa 			return true;
1439f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1440f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->number);
14415db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
14425db5a39bSTetsuo Handa 		struct tomoyo_mkdev_acl *ptr =
14435db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14440d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14450d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14460d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14470d2171d7STetsuo Handa 				continue;
14480d2171d7STetsuo Handa 			if (first) {
14490d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14500d2171d7STetsuo Handa 				first = false;
14510d2171d7STetsuo Handa 			} else {
14520d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14530d2171d7STetsuo Handa 			}
14540d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14550d2171d7STetsuo Handa 					  [tomoyo_pnnn2mac[bit]]);
14560d2171d7STetsuo Handa 		}
14570d2171d7STetsuo Handa 		if (first)
14580d2171d7STetsuo Handa 			return true;
1459f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1460f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->mode);
1461f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->major);
1462f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->minor);
1463059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1464059d84dbSTetsuo Handa 		struct tomoyo_inet_acl *ptr =
1465059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1466059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1467059d84dbSTetsuo Handa 
1468059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1469059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1470059d84dbSTetsuo Handa 				continue;
1471059d84dbSTetsuo Handa 			if (first) {
1472059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network inet ");
1473059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1474059d84dbSTetsuo Handa 						  [ptr->protocol]);
1475059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1476059d84dbSTetsuo Handa 				first = false;
1477059d84dbSTetsuo Handa 			} else {
1478059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1479059d84dbSTetsuo Handa 			}
1480059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1481059d84dbSTetsuo Handa 		}
1482059d84dbSTetsuo Handa 		if (first)
1483059d84dbSTetsuo Handa 			return true;
1484059d84dbSTetsuo Handa 		tomoyo_set_space(head);
1485059d84dbSTetsuo Handa 		if (ptr->address.group) {
1486059d84dbSTetsuo Handa 			tomoyo_set_string(head, "@");
1487059d84dbSTetsuo Handa 			tomoyo_set_string(head, ptr->address.group->group_name
1488059d84dbSTetsuo Handa 					  ->name);
1489059d84dbSTetsuo Handa 		} else {
1490059d84dbSTetsuo Handa 			char buf[128];
1491059d84dbSTetsuo Handa 			tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1492059d84dbSTetsuo Handa 			tomoyo_io_printf(head, "%s", buf);
1493059d84dbSTetsuo Handa 		}
1494059d84dbSTetsuo Handa 		tomoyo_print_number_union(head, &ptr->port);
1495059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1496059d84dbSTetsuo Handa 		struct tomoyo_unix_acl *ptr =
1497059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1498059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1499059d84dbSTetsuo Handa 
1500059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1501059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1502059d84dbSTetsuo Handa 				continue;
1503059d84dbSTetsuo Handa 			if (first) {
1504059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network unix ");
1505059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1506059d84dbSTetsuo Handa 						  [ptr->protocol]);
1507059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1508059d84dbSTetsuo Handa 				first = false;
1509059d84dbSTetsuo Handa 			} else {
1510059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1511059d84dbSTetsuo Handa 			}
1512059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1513059d84dbSTetsuo Handa 		}
1514059d84dbSTetsuo Handa 		if (first)
1515059d84dbSTetsuo Handa 			return true;
1516059d84dbSTetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
15175db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15185db5a39bSTetsuo Handa 		struct tomoyo_mount_acl *ptr =
15195db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
15200d2171d7STetsuo Handa 		tomoyo_set_group(head, "file mount");
1521f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dev_name);
1522f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dir_name);
1523f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->fs_type);
1524f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->flags);
1525d58e0da8STetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1526d58e0da8STetsuo Handa 		struct tomoyo_env_acl *ptr =
1527d58e0da8STetsuo Handa 			container_of(acl, typeof(*ptr), head);
1528d58e0da8STetsuo Handa 
1529d58e0da8STetsuo Handa 		tomoyo_set_group(head, "misc env ");
1530d58e0da8STetsuo Handa 		tomoyo_set_string(head, ptr->env->name);
15319590837bSKentaro Takeda 	}
15322066a361STetsuo Handa 	if (acl->cond) {
15332066a361STetsuo Handa 		head->r.print_cond_part = true;
15342066a361STetsuo Handa 		head->r.cond_step = 0;
15352066a361STetsuo Handa 		if (!tomoyo_flush(head))
15362066a361STetsuo Handa 			return false;
15372066a361STetsuo Handa print_cond_part:
15382066a361STetsuo Handa 		if (!tomoyo_print_condition(head, acl->cond))
15392066a361STetsuo Handa 			return false;
15402066a361STetsuo Handa 		head->r.print_cond_part = false;
15412066a361STetsuo Handa 	} else {
15420d2171d7STetsuo Handa 		tomoyo_set_lf(head);
15432066a361STetsuo Handa 	}
15445db5a39bSTetsuo Handa 	return true;
1545f23571e8STetsuo Handa }
1546f23571e8STetsuo Handa 
1547f23571e8STetsuo Handa /**
1548f23571e8STetsuo Handa  * tomoyo_read_domain2 - Read domain policy.
1549f23571e8STetsuo Handa  *
1550f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
155132997144STetsuo Handa  * @list: Pointer to "struct list_head".
1552f23571e8STetsuo Handa  *
1553f23571e8STetsuo Handa  * Caller holds tomoyo_read_lock().
1554f23571e8STetsuo Handa  *
1555f23571e8STetsuo Handa  * Returns true on success, false otherwise.
1556f23571e8STetsuo Handa  */
1557f23571e8STetsuo Handa static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
155832997144STetsuo Handa 				struct list_head *list)
1559f23571e8STetsuo Handa {
156032997144STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1561f23571e8STetsuo Handa 		struct tomoyo_acl_info *ptr =
1562f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), list);
1563f23571e8STetsuo Handa 		if (!tomoyo_print_entry(head, ptr))
15649590837bSKentaro Takeda 			return false;
15659590837bSKentaro Takeda 	}
1566f23571e8STetsuo Handa 	head->r.acl = NULL;
1567f23571e8STetsuo Handa 	return true;
1568f23571e8STetsuo Handa }
15699590837bSKentaro Takeda 
15709590837bSKentaro Takeda /**
1571e2bf6907STetsuo Handa  * tomoyo_read_domain - Read domain policy.
15729590837bSKentaro Takeda  *
15739590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
15749590837bSKentaro Takeda  *
1575fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
15769590837bSKentaro Takeda  */
1577e2bf6907STetsuo Handa static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
15789590837bSKentaro Takeda {
1579f23571e8STetsuo Handa 	if (head->r.eof)
15808fbe71f0STetsuo Handa 		return;
1581f23571e8STetsuo Handa 	list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1582475e6fa3STetsuo Handa 		struct tomoyo_domain_info *domain =
1583f23571e8STetsuo Handa 			list_entry(head->r.domain, typeof(*domain), list);
1584f23571e8STetsuo Handa 		switch (head->r.step) {
15852c47ab93STetsuo Handa 			u8 i;
1586f23571e8STetsuo Handa 		case 0:
1587f23571e8STetsuo Handa 			if (domain->is_deleted &&
1588f23571e8STetsuo Handa 			    !head->r.print_this_domain_only)
15899590837bSKentaro Takeda 				continue;
15909590837bSKentaro Takeda 			/* Print domainname and flags. */
1591f23571e8STetsuo Handa 			tomoyo_set_string(head, domain->domainname->name);
1592f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1593b5bc60b4STetsuo Handa 			tomoyo_io_printf(head, "use_profile %u\n",
1594f23571e8STetsuo Handa 					 domain->profile);
159532997144STetsuo Handa 			tomoyo_io_printf(head, "use_group %u\n",
159632997144STetsuo Handa 					 domain->group);
15972c47ab93STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
15982c47ab93STetsuo Handa 				if (domain->flags[i])
15992c47ab93STetsuo Handa 					tomoyo_set_string(head, tomoyo_dif[i]);
1600f23571e8STetsuo Handa 			head->r.step++;
1601f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1602f23571e8STetsuo Handa 			/* fall through */
1603f23571e8STetsuo Handa 		case 1:
160432997144STetsuo Handa 			if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1605f23571e8STetsuo Handa 				return;
1606f23571e8STetsuo Handa 			head->r.step++;
1607f23571e8STetsuo Handa 			if (!tomoyo_set_lf(head))
1608f23571e8STetsuo Handa 				return;
1609f23571e8STetsuo Handa 			/* fall through */
1610f23571e8STetsuo Handa 		case 2:
1611f23571e8STetsuo Handa 			head->r.step = 0;
1612f23571e8STetsuo Handa 			if (head->r.print_this_domain_only)
1613f23571e8STetsuo Handa 				goto done;
16149590837bSKentaro Takeda 		}
16159590837bSKentaro Takeda 	}
1616f23571e8STetsuo Handa  done:
1617f23571e8STetsuo Handa 	head->r.eof = true;
16189590837bSKentaro Takeda }
16199590837bSKentaro Takeda 
16209590837bSKentaro Takeda /**
16219590837bSKentaro Takeda  * tomoyo_write_pid: Specify PID to obtain domainname.
16229590837bSKentaro Takeda  *
16239590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16249590837bSKentaro Takeda  *
16259590837bSKentaro Takeda  * Returns 0.
16269590837bSKentaro Takeda  */
16279590837bSKentaro Takeda static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
16289590837bSKentaro Takeda {
1629f23571e8STetsuo Handa 	head->r.eof = false;
16309590837bSKentaro Takeda 	return 0;
16319590837bSKentaro Takeda }
16329590837bSKentaro Takeda 
16339590837bSKentaro Takeda /**
16349590837bSKentaro Takeda  * tomoyo_read_pid - Get domainname of the specified PID.
16359590837bSKentaro Takeda  *
16369590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16379590837bSKentaro Takeda  *
16389590837bSKentaro Takeda  * Returns the domainname which the specified PID is in on success,
16399590837bSKentaro Takeda  * empty string otherwise.
16409590837bSKentaro Takeda  * The PID is specified by tomoyo_write_pid() so that the user can obtain
16419590837bSKentaro Takeda  * using read()/write() interface rather than sysctl() interface.
16429590837bSKentaro Takeda  */
16438fbe71f0STetsuo Handa static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16449590837bSKentaro Takeda {
1645f23571e8STetsuo Handa 	char *buf = head->write_buf;
1646f23571e8STetsuo Handa 	bool global_pid = false;
1647f23571e8STetsuo Handa 	unsigned int pid;
16489590837bSKentaro Takeda 	struct task_struct *p;
16499590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
1650f23571e8STetsuo Handa 
1651f23571e8STetsuo Handa 	/* Accessing write_buf is safe because head->io_sem is held. */
1652f23571e8STetsuo Handa 	if (!buf) {
1653f23571e8STetsuo Handa 		head->r.eof = true;
1654f23571e8STetsuo Handa 		return; /* Do nothing if open(O_RDONLY). */
1655f23571e8STetsuo Handa 	}
1656f23571e8STetsuo Handa 	if (head->r.w_pos || head->r.eof)
1657f23571e8STetsuo Handa 		return;
1658f23571e8STetsuo Handa 	head->r.eof = true;
1659f23571e8STetsuo Handa 	if (tomoyo_str_starts(&buf, "global-pid "))
1660f23571e8STetsuo Handa 		global_pid = true;
1661f23571e8STetsuo Handa 	pid = (unsigned int) simple_strtoul(buf, NULL, 10);
16621fcdc7c5STetsuo Handa 	rcu_read_lock();
16639590837bSKentaro Takeda 	read_lock(&tasklist_lock);
1664f23571e8STetsuo Handa 	if (global_pid)
1665f23571e8STetsuo Handa 		p = find_task_by_pid_ns(pid, &init_pid_ns);
1666f23571e8STetsuo Handa 	else
16679590837bSKentaro Takeda 		p = find_task_by_vpid(pid);
16689590837bSKentaro Takeda 	if (p)
16699590837bSKentaro Takeda 		domain = tomoyo_real_domain(p);
16709590837bSKentaro Takeda 	read_unlock(&tasklist_lock);
16711fcdc7c5STetsuo Handa 	rcu_read_unlock();
1672f23571e8STetsuo Handa 	if (!domain)
1673f23571e8STetsuo Handa 		return;
1674f23571e8STetsuo Handa 	tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1675f23571e8STetsuo Handa 	tomoyo_set_string(head, domain->domainname->name);
16769590837bSKentaro Takeda }
16779590837bSKentaro Takeda 
16780f2a55d5STetsuo Handa /* String table for domain transition control keywords. */
16795448ec4fSTetsuo Handa static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1680bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
1681bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_RESET]         = "reset_domain ",
1682b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1683b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1684b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1685b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
16865448ec4fSTetsuo Handa };
16875448ec4fSTetsuo Handa 
16880f2a55d5STetsuo Handa /* String table for grouping keywords. */
1689e2bf6907STetsuo Handa static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1690b5bc60b4STetsuo Handa 	[TOMOYO_PATH_GROUP]    = "path_group ",
1691b5bc60b4STetsuo Handa 	[TOMOYO_NUMBER_GROUP]  = "number_group ",
1692059d84dbSTetsuo Handa 	[TOMOYO_ADDRESS_GROUP] = "address_group ",
1693e2bf6907STetsuo Handa };
1694e2bf6907STetsuo Handa 
16959590837bSKentaro Takeda /**
1696e2bf6907STetsuo Handa  * tomoyo_write_exception - Write exception policy.
16979590837bSKentaro Takeda  *
16989590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16999590837bSKentaro Takeda  *
17009590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1701fdb8ebb7STetsuo Handa  *
1702fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
17039590837bSKentaro Takeda  */
1704e2bf6907STetsuo Handa static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
17059590837bSKentaro Takeda {
1706bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1707a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1708bd03a3e4STetsuo Handa 		.ns = head->w.ns,
1709bd03a3e4STetsuo Handa 		.is_delete = is_delete,
1710a238cf5bSTetsuo Handa 		.data = head->write_buf,
1711e2bf6907STetsuo Handa 	};
1712a238cf5bSTetsuo Handa 	u8 i;
1713a238cf5bSTetsuo Handa 	if (tomoyo_str_starts(&param.data, "aggregator "))
1714a238cf5bSTetsuo Handa 		return tomoyo_write_aggregator(&param);
1715e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1716a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1717a238cf5bSTetsuo Handa 			return tomoyo_write_transition_control(&param, i);
1718e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1719a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1720a238cf5bSTetsuo Handa 			return tomoyo_write_group(&param, i);
172132997144STetsuo Handa 	if (tomoyo_str_starts(&param.data, "acl_group ")) {
172232997144STetsuo Handa 		unsigned int group;
172332997144STetsuo Handa 		char *data;
172432997144STetsuo Handa 		group = simple_strtoul(param.data, &data, 10);
172532997144STetsuo Handa 		if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1726bd03a3e4STetsuo Handa 			return tomoyo_write_domain2
1727bd03a3e4STetsuo Handa 				(head->w.ns, &head->w.ns->acl_group[group],
1728bd03a3e4STetsuo Handa 				 data, is_delete);
172932997144STetsuo Handa 	}
17309590837bSKentaro Takeda 	return -EINVAL;
17319590837bSKentaro Takeda }
17329590837bSKentaro Takeda 
173331845e8cSTetsuo Handa /**
1734059d84dbSTetsuo Handa  * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
173531845e8cSTetsuo Handa  *
173631845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
173731845e8cSTetsuo Handa  * @idx:  Index number.
173831845e8cSTetsuo Handa  *
173931845e8cSTetsuo Handa  * Returns true on success, false otherwise.
174031845e8cSTetsuo Handa  *
174131845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
174231845e8cSTetsuo Handa  */
174331845e8cSTetsuo Handa static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
174431845e8cSTetsuo Handa {
1745bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1746bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1747bd03a3e4STetsuo Handa 	struct list_head *list = &ns->group_list[idx];
1748bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.group, list) {
174931845e8cSTetsuo Handa 		struct tomoyo_group *group =
17500df7e8b8STetsuo Handa 			list_entry(head->r.group, typeof(*group), head.list);
1751f23571e8STetsuo Handa 		list_for_each_cookie(head->r.acl, &group->member_list) {
175231845e8cSTetsuo Handa 			struct tomoyo_acl_head *ptr =
1753f23571e8STetsuo Handa 				list_entry(head->r.acl, typeof(*ptr), list);
175431845e8cSTetsuo Handa 			if (ptr->is_deleted)
175531845e8cSTetsuo Handa 				continue;
1756f23571e8STetsuo Handa 			if (!tomoyo_flush(head))
175731845e8cSTetsuo Handa 				return false;
1758bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
1759f23571e8STetsuo Handa 			tomoyo_set_string(head, tomoyo_group_name[idx]);
1760f23571e8STetsuo Handa 			tomoyo_set_string(head, group->group_name->name);
1761f23571e8STetsuo Handa 			if (idx == TOMOYO_PATH_GROUP) {
1762f23571e8STetsuo Handa 				tomoyo_set_space(head);
1763f23571e8STetsuo Handa 				tomoyo_set_string(head, container_of
1764f23571e8STetsuo Handa 					       (ptr, struct tomoyo_path_group,
1765f23571e8STetsuo Handa 						head)->member_name->name);
1766f23571e8STetsuo Handa 			} else if (idx == TOMOYO_NUMBER_GROUP) {
1767f23571e8STetsuo Handa 				tomoyo_print_number_union(head, &container_of
1768f23571e8STetsuo Handa 							  (ptr,
1769f23571e8STetsuo Handa 						   struct tomoyo_number_group,
1770f23571e8STetsuo Handa 							   head)->number);
1771059d84dbSTetsuo Handa 			} else if (idx == TOMOYO_ADDRESS_GROUP) {
1772059d84dbSTetsuo Handa 				char buffer[128];
1773059d84dbSTetsuo Handa 
1774059d84dbSTetsuo Handa 				struct tomoyo_address_group *member =
1775059d84dbSTetsuo Handa 					container_of(ptr, typeof(*member),
1776059d84dbSTetsuo Handa 						     head);
1777059d84dbSTetsuo Handa 				tomoyo_print_ip(buffer, sizeof(buffer),
1778059d84dbSTetsuo Handa 						&member->address);
1779059d84dbSTetsuo Handa 				tomoyo_io_printf(head, " %s", buffer);
178031845e8cSTetsuo Handa 			}
1781f23571e8STetsuo Handa 			tomoyo_set_lf(head);
178231845e8cSTetsuo Handa 		}
1783f23571e8STetsuo Handa 		head->r.acl = NULL;
1784f23571e8STetsuo Handa 	}
1785f23571e8STetsuo Handa 	head->r.group = NULL;
178631845e8cSTetsuo Handa 	return true;
178731845e8cSTetsuo Handa }
178831845e8cSTetsuo Handa 
178931845e8cSTetsuo Handa /**
179031845e8cSTetsuo Handa  * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
179131845e8cSTetsuo Handa  *
179231845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
179331845e8cSTetsuo Handa  * @idx:  Index number.
179431845e8cSTetsuo Handa  *
179531845e8cSTetsuo Handa  * Returns true on success, false otherwise.
179631845e8cSTetsuo Handa  *
179731845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
179831845e8cSTetsuo Handa  */
179931845e8cSTetsuo Handa static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
180031845e8cSTetsuo Handa {
1801bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1802bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1803bd03a3e4STetsuo Handa 	struct list_head *list = &ns->policy_list[idx];
1804bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1805475e6fa3STetsuo Handa 		struct tomoyo_acl_head *acl =
1806f23571e8STetsuo Handa 			container_of(head->r.acl, typeof(*acl), list);
180731845e8cSTetsuo Handa 		if (acl->is_deleted)
180831845e8cSTetsuo Handa 			continue;
1809f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
1810f23571e8STetsuo Handa 			return false;
181131845e8cSTetsuo Handa 		switch (idx) {
18125448ec4fSTetsuo Handa 		case TOMOYO_ID_TRANSITION_CONTROL:
181331845e8cSTetsuo Handa 			{
18145448ec4fSTetsuo Handa 				struct tomoyo_transition_control *ptr =
181531845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1816bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
18170d2171d7STetsuo Handa 				tomoyo_set_string(head, tomoyo_transition_type
1818f23571e8STetsuo Handa 						  [ptr->type]);
18190d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->program ?
18200d2171d7STetsuo Handa 						  ptr->program->name : "any");
1821f23571e8STetsuo Handa 				tomoyo_set_string(head, " from ");
18220d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->domainname ?
18230d2171d7STetsuo Handa 						  ptr->domainname->name :
18240d2171d7STetsuo Handa 						  "any");
182531845e8cSTetsuo Handa 			}
182631845e8cSTetsuo Handa 			break;
182731845e8cSTetsuo Handa 		case TOMOYO_ID_AGGREGATOR:
182831845e8cSTetsuo Handa 			{
1829e2bf6907STetsuo Handa 				struct tomoyo_aggregator *ptr =
183031845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1831bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
1832b5bc60b4STetsuo Handa 				tomoyo_set_string(head, "aggregator ");
1833f23571e8STetsuo Handa 				tomoyo_set_string(head,
1834f23571e8STetsuo Handa 						  ptr->original_name->name);
1835f23571e8STetsuo Handa 				tomoyo_set_space(head);
1836f23571e8STetsuo Handa 				tomoyo_set_string(head,
1837f23571e8STetsuo Handa 					       ptr->aggregated_name->name);
183831845e8cSTetsuo Handa 			}
183931845e8cSTetsuo Handa 			break;
184031845e8cSTetsuo Handa 		default:
184131845e8cSTetsuo Handa 			continue;
184231845e8cSTetsuo Handa 		}
1843f23571e8STetsuo Handa 		tomoyo_set_lf(head);
184431845e8cSTetsuo Handa 	}
1845f23571e8STetsuo Handa 	head->r.acl = NULL;
184631845e8cSTetsuo Handa 	return true;
184731845e8cSTetsuo Handa }
184831845e8cSTetsuo Handa 
18499590837bSKentaro Takeda /**
1850e2bf6907STetsuo Handa  * tomoyo_read_exception - Read exception policy.
18519590837bSKentaro Takeda  *
18529590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
18539590837bSKentaro Takeda  *
1854fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
18559590837bSKentaro Takeda  */
1856e2bf6907STetsuo Handa static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
18579590837bSKentaro Takeda {
1858bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1859bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1860f23571e8STetsuo Handa 	if (head->r.eof)
186131845e8cSTetsuo Handa 		return;
1862f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY &&
1863f23571e8STetsuo Handa 	       tomoyo_read_policy(head, head->r.step))
1864f23571e8STetsuo Handa 		head->r.step++;
1865f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY)
186631845e8cSTetsuo Handa 		return;
1867f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1868f23571e8STetsuo Handa 	       tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1869f23571e8STetsuo Handa 		head->r.step++;
1870f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
187131845e8cSTetsuo Handa 		return;
187232997144STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
187332997144STetsuo Handa 	       + TOMOYO_MAX_ACL_GROUPS) {
187432997144STetsuo Handa 		head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
187532997144STetsuo Handa 			- TOMOYO_MAX_GROUP;
1876bd03a3e4STetsuo Handa 		if (!tomoyo_read_domain2(head, &ns->acl_group
187732997144STetsuo Handa 					 [head->r.acl_group_index]))
187832997144STetsuo Handa 			return;
187932997144STetsuo Handa 		head->r.step++;
188032997144STetsuo Handa 	}
1881f23571e8STetsuo Handa 	head->r.eof = true;
18829590837bSKentaro Takeda }
18839590837bSKentaro Takeda 
1884eadd99ccSTetsuo Handa /* Wait queue for kernel -> userspace notification. */
188517fcfbd9STetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1886eadd99ccSTetsuo Handa /* Wait queue for userspace -> kernel notification. */
1887eadd99ccSTetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
188817fcfbd9STetsuo Handa 
188917fcfbd9STetsuo Handa /* Structure for query. */
1890e2bf6907STetsuo Handa struct tomoyo_query {
189117fcfbd9STetsuo Handa 	struct list_head list;
189217fcfbd9STetsuo Handa 	char *query;
1893eadd99ccSTetsuo Handa 	size_t query_len;
189417fcfbd9STetsuo Handa 	unsigned int serial;
1895eadd99ccSTetsuo Handa 	u8 timer;
1896eadd99ccSTetsuo Handa 	u8 answer;
1897eadd99ccSTetsuo Handa 	u8 retry;
189817fcfbd9STetsuo Handa };
189917fcfbd9STetsuo Handa 
1900e2bf6907STetsuo Handa /* The list for "struct tomoyo_query". */
190117fcfbd9STetsuo Handa static LIST_HEAD(tomoyo_query_list);
190217fcfbd9STetsuo Handa 
1903eadd99ccSTetsuo Handa /* Lock for manipulating tomoyo_query_list. */
1904eadd99ccSTetsuo Handa static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1905eadd99ccSTetsuo Handa 
190617fcfbd9STetsuo Handa /*
190717fcfbd9STetsuo Handa  * Number of "struct file" referring /sys/kernel/security/tomoyo/query
190817fcfbd9STetsuo Handa  * interface.
190917fcfbd9STetsuo Handa  */
191017fcfbd9STetsuo Handa static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
191117fcfbd9STetsuo Handa 
191217fcfbd9STetsuo Handa /**
19132ca9bf45STetsuo Handa  * tomoyo_truncate - Truncate a line.
19142ca9bf45STetsuo Handa  *
19152ca9bf45STetsuo Handa  * @str: String to truncate.
19162ca9bf45STetsuo Handa  *
19172ca9bf45STetsuo Handa  * Returns length of truncated @str.
19182ca9bf45STetsuo Handa  */
19192ca9bf45STetsuo Handa static int tomoyo_truncate(char *str)
19202ca9bf45STetsuo Handa {
19212ca9bf45STetsuo Handa 	char *start = str;
19222ca9bf45STetsuo Handa 	while (*(unsigned char *) str > (unsigned char) ' ')
19232ca9bf45STetsuo Handa 		str++;
19242ca9bf45STetsuo Handa 	*str = '\0';
19252ca9bf45STetsuo Handa 	return strlen(start) + 1;
19262ca9bf45STetsuo Handa }
19272ca9bf45STetsuo Handa 
19282ca9bf45STetsuo Handa /**
1929eadd99ccSTetsuo Handa  * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1930eadd99ccSTetsuo Handa  *
1931eadd99ccSTetsuo Handa  * @domain: Pointer to "struct tomoyo_domain_info".
1932eadd99ccSTetsuo Handa  * @header: Lines containing ACL.
1933eadd99ccSTetsuo Handa  *
1934eadd99ccSTetsuo Handa  * Returns nothing.
1935eadd99ccSTetsuo Handa  */
1936eadd99ccSTetsuo Handa static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1937eadd99ccSTetsuo Handa {
1938eadd99ccSTetsuo Handa 	char *buffer;
19392ca9bf45STetsuo Handa 	char *realpath = NULL;
19405b636857STetsuo Handa 	char *argv0 = NULL;
19412ca9bf45STetsuo Handa 	char *symlink = NULL;
1942eadd99ccSTetsuo Handa 	char *cp = strchr(header, '\n');
1943eadd99ccSTetsuo Handa 	int len;
1944eadd99ccSTetsuo Handa 	if (!cp)
1945eadd99ccSTetsuo Handa 		return;
1946eadd99ccSTetsuo Handa 	cp = strchr(cp + 1, '\n');
1947eadd99ccSTetsuo Handa 	if (!cp)
1948eadd99ccSTetsuo Handa 		return;
1949eadd99ccSTetsuo Handa 	*cp++ = '\0';
1950eadd99ccSTetsuo Handa 	len = strlen(cp) + 1;
19512ca9bf45STetsuo Handa 	/* strstr() will return NULL if ordering is wrong. */
19522ca9bf45STetsuo Handa 	if (*cp == 'f') {
19535b636857STetsuo Handa 		argv0 = strstr(header, " argv[]={ \"");
19545b636857STetsuo Handa 		if (argv0) {
19555b636857STetsuo Handa 			argv0 += 10;
19565b636857STetsuo Handa 			len += tomoyo_truncate(argv0) + 14;
19575b636857STetsuo Handa 		}
19582ca9bf45STetsuo Handa 		realpath = strstr(header, " exec={ realpath=\"");
19592ca9bf45STetsuo Handa 		if (realpath) {
19602ca9bf45STetsuo Handa 			realpath += 8;
19612ca9bf45STetsuo Handa 			len += tomoyo_truncate(realpath) + 6;
19622ca9bf45STetsuo Handa 		}
19632ca9bf45STetsuo Handa 		symlink = strstr(header, " symlink.target=\"");
19642ca9bf45STetsuo Handa 		if (symlink)
19652ca9bf45STetsuo Handa 			len += tomoyo_truncate(symlink + 1) + 1;
19662ca9bf45STetsuo Handa 	}
1967eadd99ccSTetsuo Handa 	buffer = kmalloc(len, GFP_NOFS);
1968eadd99ccSTetsuo Handa 	if (!buffer)
1969eadd99ccSTetsuo Handa 		return;
1970eadd99ccSTetsuo Handa 	snprintf(buffer, len - 1, "%s", cp);
19712ca9bf45STetsuo Handa 	if (realpath)
19722ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.%s", realpath);
19735b636857STetsuo Handa 	if (argv0)
19745b636857STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
19752ca9bf45STetsuo Handa 	if (symlink)
19762ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, "%s", symlink);
1977eadd99ccSTetsuo Handa 	tomoyo_normalize_line(buffer);
1978b22b8b9fSTetsuo Handa 	if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1979b22b8b9fSTetsuo Handa 				  false))
1980b22b8b9fSTetsuo Handa 		tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
1981eadd99ccSTetsuo Handa 	kfree(buffer);
1982eadd99ccSTetsuo Handa }
1983eadd99ccSTetsuo Handa 
1984eadd99ccSTetsuo Handa /**
198517fcfbd9STetsuo Handa  * tomoyo_supervisor - Ask for the supervisor's decision.
198617fcfbd9STetsuo Handa  *
198717fcfbd9STetsuo Handa  * @r:   Pointer to "struct tomoyo_request_info".
198817fcfbd9STetsuo Handa  * @fmt: The printf()'s format string, followed by parameters.
198917fcfbd9STetsuo Handa  *
199017fcfbd9STetsuo Handa  * Returns 0 if the supervisor decided to permit the access request which
199117fcfbd9STetsuo Handa  * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
199217fcfbd9STetsuo Handa  * supervisor decided to retry the access request which violated the policy in
199317fcfbd9STetsuo Handa  * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
199417fcfbd9STetsuo Handa  */
199517fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
199617fcfbd9STetsuo Handa {
199717fcfbd9STetsuo Handa 	va_list args;
1998eadd99ccSTetsuo Handa 	int error;
199917fcfbd9STetsuo Handa 	int len;
200017fcfbd9STetsuo Handa 	static unsigned int tomoyo_serial;
2001eadd99ccSTetsuo Handa 	struct tomoyo_query entry = { };
200217fcfbd9STetsuo Handa 	bool quota_exceeded = false;
2003eadd99ccSTetsuo Handa 	va_start(args, fmt);
2004eadd99ccSTetsuo Handa 	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2005eadd99ccSTetsuo Handa 	va_end(args);
2006eadd99ccSTetsuo Handa 	/* Write /sys/kernel/security/tomoyo/audit. */
2007eadd99ccSTetsuo Handa 	va_start(args, fmt);
2008eadd99ccSTetsuo Handa 	tomoyo_write_log2(r, len, fmt, args);
2009eadd99ccSTetsuo Handa 	va_end(args);
2010eadd99ccSTetsuo Handa 	/* Nothing more to do if granted. */
2011eadd99ccSTetsuo Handa 	if (r->granted)
2012eadd99ccSTetsuo Handa 		return 0;
2013b22b8b9fSTetsuo Handa 	if (r->mode)
2014b22b8b9fSTetsuo Handa 		tomoyo_update_stat(r->mode);
201517fcfbd9STetsuo Handa 	switch (r->mode) {
2016eadd99ccSTetsuo Handa 	case TOMOYO_CONFIG_ENFORCING:
2017eadd99ccSTetsuo Handa 		error = -EPERM;
2018eadd99ccSTetsuo Handa 		if (atomic_read(&tomoyo_query_observers))
2019eadd99ccSTetsuo Handa 			break;
2020eadd99ccSTetsuo Handa 		goto out;
202117fcfbd9STetsuo Handa 	case TOMOYO_CONFIG_LEARNING:
2022eadd99ccSTetsuo Handa 		error = 0;
2023eadd99ccSTetsuo Handa 		/* Check max_learning_entry parameter. */
2024eadd99ccSTetsuo Handa 		if (tomoyo_domain_quota_is_ok(r))
2025eadd99ccSTetsuo Handa 			break;
202617fcfbd9STetsuo Handa 		/* fall through */
2027eadd99ccSTetsuo Handa 	default:
202817fcfbd9STetsuo Handa 		return 0;
202917fcfbd9STetsuo Handa 	}
2030eadd99ccSTetsuo Handa 	/* Get message. */
203117fcfbd9STetsuo Handa 	va_start(args, fmt);
2032eadd99ccSTetsuo Handa 	entry.query = tomoyo_init_log(r, len, fmt, args);
203317fcfbd9STetsuo Handa 	va_end(args);
2034eadd99ccSTetsuo Handa 	if (!entry.query)
203517fcfbd9STetsuo Handa 		goto out;
2036eadd99ccSTetsuo Handa 	entry.query_len = strlen(entry.query) + 1;
2037eadd99ccSTetsuo Handa 	if (!error) {
2038eadd99ccSTetsuo Handa 		tomoyo_add_entry(r->domain, entry.query);
203917fcfbd9STetsuo Handa 		goto out;
2040eadd99ccSTetsuo Handa 	}
2041eadd99ccSTetsuo Handa 	len = tomoyo_round2(entry.query_len);
204217fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2043eadd99ccSTetsuo Handa 	if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2044eadd99ccSTetsuo Handa 	    tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2045eadd99ccSTetsuo Handa 	    >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
204617fcfbd9STetsuo Handa 		quota_exceeded = true;
204717fcfbd9STetsuo Handa 	} else {
2048eadd99ccSTetsuo Handa 		entry.serial = tomoyo_serial++;
2049eadd99ccSTetsuo Handa 		entry.retry = r->retry;
2050eadd99ccSTetsuo Handa 		tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2051eadd99ccSTetsuo Handa 		list_add_tail(&entry.list, &tomoyo_query_list);
205217fcfbd9STetsuo Handa 	}
205317fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
205417fcfbd9STetsuo Handa 	if (quota_exceeded)
205517fcfbd9STetsuo Handa 		goto out;
205617fcfbd9STetsuo Handa 	/* Give 10 seconds for supervisor's opinion. */
2057eadd99ccSTetsuo Handa 	while (entry.timer < 10) {
2058eadd99ccSTetsuo Handa 		wake_up_all(&tomoyo_query_wait);
2059eadd99ccSTetsuo Handa 		if (wait_event_interruptible_timeout
2060eadd99ccSTetsuo Handa 		    (tomoyo_answer_wait, entry.answer ||
2061eadd99ccSTetsuo Handa 		     !atomic_read(&tomoyo_query_observers), HZ))
206217fcfbd9STetsuo Handa 			break;
2063eadd99ccSTetsuo Handa 		else
2064eadd99ccSTetsuo Handa 			entry.timer++;
206517fcfbd9STetsuo Handa 	}
206617fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2067eadd99ccSTetsuo Handa 	list_del(&entry.list);
2068eadd99ccSTetsuo Handa 	tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
206917fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2070eadd99ccSTetsuo Handa 	switch (entry.answer) {
207117fcfbd9STetsuo Handa 	case 3: /* Asked to retry by administrator. */
207217fcfbd9STetsuo Handa 		error = TOMOYO_RETRY_REQUEST;
207317fcfbd9STetsuo Handa 		r->retry++;
207417fcfbd9STetsuo Handa 		break;
207517fcfbd9STetsuo Handa 	case 1:
207617fcfbd9STetsuo Handa 		/* Granted by administrator. */
207717fcfbd9STetsuo Handa 		error = 0;
207817fcfbd9STetsuo Handa 		break;
207917fcfbd9STetsuo Handa 	default:
2080eadd99ccSTetsuo Handa 		/* Timed out or rejected by administrator. */
208117fcfbd9STetsuo Handa 		break;
208217fcfbd9STetsuo Handa 	}
208317fcfbd9STetsuo Handa out:
2084eadd99ccSTetsuo Handa 	kfree(entry.query);
208517fcfbd9STetsuo Handa 	return error;
208617fcfbd9STetsuo Handa }
208717fcfbd9STetsuo Handa 
208817fcfbd9STetsuo Handa /**
208917fcfbd9STetsuo Handa  * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
209017fcfbd9STetsuo Handa  *
209117fcfbd9STetsuo Handa  * @file: Pointer to "struct file".
209217fcfbd9STetsuo Handa  * @wait: Pointer to "poll_table".
209317fcfbd9STetsuo Handa  *
209417fcfbd9STetsuo Handa  * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
209517fcfbd9STetsuo Handa  *
209617fcfbd9STetsuo Handa  * Waits for access requests which violated policy in enforcing mode.
209717fcfbd9STetsuo Handa  */
209817fcfbd9STetsuo Handa static int tomoyo_poll_query(struct file *file, poll_table *wait)
209917fcfbd9STetsuo Handa {
210017fcfbd9STetsuo Handa 	struct list_head *tmp;
210117fcfbd9STetsuo Handa 	bool found = false;
210217fcfbd9STetsuo Handa 	u8 i;
210317fcfbd9STetsuo Handa 	for (i = 0; i < 2; i++) {
210417fcfbd9STetsuo Handa 		spin_lock(&tomoyo_query_list_lock);
210517fcfbd9STetsuo Handa 		list_for_each(tmp, &tomoyo_query_list) {
2106e2bf6907STetsuo Handa 			struct tomoyo_query *ptr =
2107e2bf6907STetsuo Handa 				list_entry(tmp, typeof(*ptr), list);
210817fcfbd9STetsuo Handa 			if (ptr->answer)
210917fcfbd9STetsuo Handa 				continue;
211017fcfbd9STetsuo Handa 			found = true;
211117fcfbd9STetsuo Handa 			break;
211217fcfbd9STetsuo Handa 		}
211317fcfbd9STetsuo Handa 		spin_unlock(&tomoyo_query_list_lock);
211417fcfbd9STetsuo Handa 		if (found)
211517fcfbd9STetsuo Handa 			return POLLIN | POLLRDNORM;
211617fcfbd9STetsuo Handa 		if (i)
211717fcfbd9STetsuo Handa 			break;
211817fcfbd9STetsuo Handa 		poll_wait(file, &tomoyo_query_wait, wait);
211917fcfbd9STetsuo Handa 	}
212017fcfbd9STetsuo Handa 	return 0;
212117fcfbd9STetsuo Handa }
212217fcfbd9STetsuo Handa 
212317fcfbd9STetsuo Handa /**
212417fcfbd9STetsuo Handa  * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
212517fcfbd9STetsuo Handa  *
212617fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
212717fcfbd9STetsuo Handa  */
21288fbe71f0STetsuo Handa static void tomoyo_read_query(struct tomoyo_io_buffer *head)
212917fcfbd9STetsuo Handa {
213017fcfbd9STetsuo Handa 	struct list_head *tmp;
21312c47ab93STetsuo Handa 	unsigned int pos = 0;
21322c47ab93STetsuo Handa 	size_t len = 0;
213317fcfbd9STetsuo Handa 	char *buf;
2134f23571e8STetsuo Handa 	if (head->r.w_pos)
21358fbe71f0STetsuo Handa 		return;
213617fcfbd9STetsuo Handa 	if (head->read_buf) {
213717fcfbd9STetsuo Handa 		kfree(head->read_buf);
213817fcfbd9STetsuo Handa 		head->read_buf = NULL;
213917fcfbd9STetsuo Handa 	}
214017fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
214117fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2142e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
214317fcfbd9STetsuo Handa 		if (ptr->answer)
214417fcfbd9STetsuo Handa 			continue;
2145f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
214617fcfbd9STetsuo Handa 			continue;
214717fcfbd9STetsuo Handa 		len = ptr->query_len;
214817fcfbd9STetsuo Handa 		break;
214917fcfbd9STetsuo Handa 	}
215017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
215117fcfbd9STetsuo Handa 	if (!len) {
2152f23571e8STetsuo Handa 		head->r.query_index = 0;
21538fbe71f0STetsuo Handa 		return;
215417fcfbd9STetsuo Handa 	}
2155eadd99ccSTetsuo Handa 	buf = kzalloc(len + 32, GFP_NOFS);
215617fcfbd9STetsuo Handa 	if (!buf)
21578fbe71f0STetsuo Handa 		return;
215817fcfbd9STetsuo Handa 	pos = 0;
215917fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
216017fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2161e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
216217fcfbd9STetsuo Handa 		if (ptr->answer)
216317fcfbd9STetsuo Handa 			continue;
2164f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
216517fcfbd9STetsuo Handa 			continue;
216617fcfbd9STetsuo Handa 		/*
216717fcfbd9STetsuo Handa 		 * Some query can be skipped because tomoyo_query_list
216817fcfbd9STetsuo Handa 		 * can change, but I don't care.
216917fcfbd9STetsuo Handa 		 */
217017fcfbd9STetsuo Handa 		if (len == ptr->query_len)
2171eadd99ccSTetsuo Handa 			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2172eadd99ccSTetsuo Handa 				 ptr->retry, ptr->query);
217317fcfbd9STetsuo Handa 		break;
217417fcfbd9STetsuo Handa 	}
217517fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
217617fcfbd9STetsuo Handa 	if (buf[0]) {
217717fcfbd9STetsuo Handa 		head->read_buf = buf;
2178f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = buf;
2179f23571e8STetsuo Handa 		head->r.query_index++;
218017fcfbd9STetsuo Handa 	} else {
218117fcfbd9STetsuo Handa 		kfree(buf);
218217fcfbd9STetsuo Handa 	}
218317fcfbd9STetsuo Handa }
218417fcfbd9STetsuo Handa 
218517fcfbd9STetsuo Handa /**
218617fcfbd9STetsuo Handa  * tomoyo_write_answer - Write the supervisor's decision.
218717fcfbd9STetsuo Handa  *
218817fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
218917fcfbd9STetsuo Handa  *
219017fcfbd9STetsuo Handa  * Returns 0 on success, -EINVAL otherwise.
219117fcfbd9STetsuo Handa  */
219217fcfbd9STetsuo Handa static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
219317fcfbd9STetsuo Handa {
219417fcfbd9STetsuo Handa 	char *data = head->write_buf;
219517fcfbd9STetsuo Handa 	struct list_head *tmp;
219617fcfbd9STetsuo Handa 	unsigned int serial;
219717fcfbd9STetsuo Handa 	unsigned int answer;
219817fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
219917fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2200e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
220117fcfbd9STetsuo Handa 		ptr->timer = 0;
220217fcfbd9STetsuo Handa 	}
220317fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
220417fcfbd9STetsuo Handa 	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
220517fcfbd9STetsuo Handa 		return -EINVAL;
220617fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
220717fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2208e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
220917fcfbd9STetsuo Handa 		if (ptr->serial != serial)
221017fcfbd9STetsuo Handa 			continue;
221117fcfbd9STetsuo Handa 		if (!ptr->answer)
221217fcfbd9STetsuo Handa 			ptr->answer = answer;
221317fcfbd9STetsuo Handa 		break;
221417fcfbd9STetsuo Handa 	}
221517fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
221617fcfbd9STetsuo Handa 	return 0;
221717fcfbd9STetsuo Handa }
221817fcfbd9STetsuo Handa 
221917fcfbd9STetsuo Handa /**
22209590837bSKentaro Takeda  * tomoyo_read_version: Get version.
22219590837bSKentaro Takeda  *
22229590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
22239590837bSKentaro Takeda  *
22249590837bSKentaro Takeda  * Returns version information.
22259590837bSKentaro Takeda  */
22268fbe71f0STetsuo Handa static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22279590837bSKentaro Takeda {
2228f23571e8STetsuo Handa 	if (!head->r.eof) {
2229843d183cSTetsuo Handa 		tomoyo_io_printf(head, "2.5.0");
2230f23571e8STetsuo Handa 		head->r.eof = true;
22319590837bSKentaro Takeda 	}
22329590837bSKentaro Takeda }
22339590837bSKentaro Takeda 
2234b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2235b22b8b9fSTetsuo Handa static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2236b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_UPDATES]    = "update:",
2237b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2238b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2239b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2240b22b8b9fSTetsuo Handa };
2241b22b8b9fSTetsuo Handa 
2242b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2243b22b8b9fSTetsuo Handa static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2244b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_POLICY] = "policy:",
2245b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_AUDIT]  = "audit log:",
2246b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_QUERY]  = "query message:",
2247b22b8b9fSTetsuo Handa };
2248b22b8b9fSTetsuo Handa 
2249b22b8b9fSTetsuo Handa /* Timestamp counter for last updated. */
2250b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2251b22b8b9fSTetsuo Handa /* Counter for number of updates. */
2252b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2253b22b8b9fSTetsuo Handa 
2254b22b8b9fSTetsuo Handa /**
2255b22b8b9fSTetsuo Handa  * tomoyo_update_stat - Update statistic counters.
2256b22b8b9fSTetsuo Handa  *
2257b22b8b9fSTetsuo Handa  * @index: Index for policy type.
2258b22b8b9fSTetsuo Handa  *
2259b22b8b9fSTetsuo Handa  * Returns nothing.
2260b22b8b9fSTetsuo Handa  */
2261b22b8b9fSTetsuo Handa void tomoyo_update_stat(const u8 index)
2262b22b8b9fSTetsuo Handa {
2263b22b8b9fSTetsuo Handa 	struct timeval tv;
2264b22b8b9fSTetsuo Handa 	do_gettimeofday(&tv);
2265b22b8b9fSTetsuo Handa 	/*
2266b22b8b9fSTetsuo Handa 	 * I don't use atomic operations because race condition is not fatal.
2267b22b8b9fSTetsuo Handa 	 */
2268b22b8b9fSTetsuo Handa 	tomoyo_stat_updated[index]++;
2269b22b8b9fSTetsuo Handa 	tomoyo_stat_modified[index] = tv.tv_sec;
2270b22b8b9fSTetsuo Handa }
2271b22b8b9fSTetsuo Handa 
2272b22b8b9fSTetsuo Handa /**
2273b22b8b9fSTetsuo Handa  * tomoyo_read_stat - Read statistic data.
2274b22b8b9fSTetsuo Handa  *
2275b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2276b22b8b9fSTetsuo Handa  *
2277b22b8b9fSTetsuo Handa  * Returns nothing.
2278b22b8b9fSTetsuo Handa  */
2279b22b8b9fSTetsuo Handa static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2280b22b8b9fSTetsuo Handa {
2281b22b8b9fSTetsuo Handa 	u8 i;
2282b22b8b9fSTetsuo Handa 	unsigned int total = 0;
2283b22b8b9fSTetsuo Handa 	if (head->r.eof)
2284b22b8b9fSTetsuo Handa 		return;
2285b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2286b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Policy %-30s %10u",
2287b22b8b9fSTetsuo Handa 				 tomoyo_policy_headers[i],
2288b22b8b9fSTetsuo Handa 				 tomoyo_stat_updated[i]);
2289b22b8b9fSTetsuo Handa 		if (tomoyo_stat_modified[i]) {
2290b22b8b9fSTetsuo Handa 			struct tomoyo_time stamp;
2291b22b8b9fSTetsuo Handa 			tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2292b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2293b22b8b9fSTetsuo Handa 					 "%02u:%02u:%02u)",
2294b22b8b9fSTetsuo Handa 					 stamp.year, stamp.month, stamp.day,
2295b22b8b9fSTetsuo Handa 					 stamp.hour, stamp.min, stamp.sec);
2296b22b8b9fSTetsuo Handa 		}
2297b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2298b22b8b9fSTetsuo Handa 	}
2299b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2300b22b8b9fSTetsuo Handa 		unsigned int used = tomoyo_memory_used[i];
2301b22b8b9fSTetsuo Handa 		total += used;
2302b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Memory used by %-22s %10u",
2303b22b8b9fSTetsuo Handa 				 tomoyo_memory_headers[i], used);
2304b22b8b9fSTetsuo Handa 		used = tomoyo_memory_quota[i];
2305b22b8b9fSTetsuo Handa 		if (used)
2306b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Quota: %10u)", used);
2307b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2308b22b8b9fSTetsuo Handa 	}
2309b22b8b9fSTetsuo Handa 	tomoyo_io_printf(head, "Total memory used:                    %10u\n",
2310b22b8b9fSTetsuo Handa 			 total);
2311b22b8b9fSTetsuo Handa 	head->r.eof = true;
2312b22b8b9fSTetsuo Handa }
2313b22b8b9fSTetsuo Handa 
2314b22b8b9fSTetsuo Handa /**
2315b22b8b9fSTetsuo Handa  * tomoyo_write_stat - Set memory quota.
2316b22b8b9fSTetsuo Handa  *
2317b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2318b22b8b9fSTetsuo Handa  *
2319b22b8b9fSTetsuo Handa  * Returns 0.
2320b22b8b9fSTetsuo Handa  */
2321b22b8b9fSTetsuo Handa static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2322b22b8b9fSTetsuo Handa {
2323b22b8b9fSTetsuo Handa 	char *data = head->write_buf;
2324b22b8b9fSTetsuo Handa 	u8 i;
2325b22b8b9fSTetsuo Handa 	if (tomoyo_str_starts(&data, "Memory used by "))
2326b22b8b9fSTetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2327b22b8b9fSTetsuo Handa 			if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2328b22b8b9fSTetsuo Handa 				sscanf(data, "%u", &tomoyo_memory_quota[i]);
2329b22b8b9fSTetsuo Handa 	return 0;
2330b22b8b9fSTetsuo Handa }
2331b22b8b9fSTetsuo Handa 
23329590837bSKentaro Takeda /**
23339590837bSKentaro Takeda  * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
23349590837bSKentaro Takeda  *
23359590837bSKentaro Takeda  * @type: Type of interface.
23369590837bSKentaro Takeda  * @file: Pointer to "struct file".
23379590837bSKentaro Takeda  *
23382e503bbbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
23399590837bSKentaro Takeda  */
2340c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file)
23419590837bSKentaro Takeda {
23424e5d6f7eSTetsuo Handa 	struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
23439590837bSKentaro Takeda 
23449590837bSKentaro Takeda 	if (!head)
23459590837bSKentaro Takeda 		return -ENOMEM;
23469590837bSKentaro Takeda 	mutex_init(&head->io_sem);
234717fcfbd9STetsuo Handa 	head->type = type;
23489590837bSKentaro Takeda 	switch (type) {
23499590837bSKentaro Takeda 	case TOMOYO_DOMAINPOLICY:
23509590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/domain_policy */
2351e2bf6907STetsuo Handa 		head->write = tomoyo_write_domain;
2352e2bf6907STetsuo Handa 		head->read = tomoyo_read_domain;
23539590837bSKentaro Takeda 		break;
23549590837bSKentaro Takeda 	case TOMOYO_EXCEPTIONPOLICY:
23559590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/exception_policy */
2356e2bf6907STetsuo Handa 		head->write = tomoyo_write_exception;
2357e2bf6907STetsuo Handa 		head->read = tomoyo_read_exception;
23589590837bSKentaro Takeda 		break;
2359eadd99ccSTetsuo Handa 	case TOMOYO_AUDIT:
2360eadd99ccSTetsuo Handa 		/* /sys/kernel/security/tomoyo/audit */
2361eadd99ccSTetsuo Handa 		head->poll = tomoyo_poll_log;
2362eadd99ccSTetsuo Handa 		head->read = tomoyo_read_log;
2363eadd99ccSTetsuo Handa 		break;
23649590837bSKentaro Takeda 	case TOMOYO_PROCESS_STATUS:
23659590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/.process_status */
23669590837bSKentaro Takeda 		head->write = tomoyo_write_pid;
23679590837bSKentaro Takeda 		head->read = tomoyo_read_pid;
23689590837bSKentaro Takeda 		break;
23699590837bSKentaro Takeda 	case TOMOYO_VERSION:
23709590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/version */
23719590837bSKentaro Takeda 		head->read = tomoyo_read_version;
23729590837bSKentaro Takeda 		head->readbuf_size = 128;
23739590837bSKentaro Takeda 		break;
2374b22b8b9fSTetsuo Handa 	case TOMOYO_STAT:
2375b22b8b9fSTetsuo Handa 		/* /sys/kernel/security/tomoyo/stat */
2376b22b8b9fSTetsuo Handa 		head->write = tomoyo_write_stat;
2377b22b8b9fSTetsuo Handa 		head->read = tomoyo_read_stat;
2378b22b8b9fSTetsuo Handa 		head->readbuf_size = 1024;
23799590837bSKentaro Takeda 		break;
23809590837bSKentaro Takeda 	case TOMOYO_PROFILE:
23819590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/profile */
23829590837bSKentaro Takeda 		head->write = tomoyo_write_profile;
23839590837bSKentaro Takeda 		head->read = tomoyo_read_profile;
23849590837bSKentaro Takeda 		break;
238517fcfbd9STetsuo Handa 	case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
238617fcfbd9STetsuo Handa 		head->poll = tomoyo_poll_query;
238717fcfbd9STetsuo Handa 		head->write = tomoyo_write_answer;
238817fcfbd9STetsuo Handa 		head->read = tomoyo_read_query;
238917fcfbd9STetsuo Handa 		break;
23909590837bSKentaro Takeda 	case TOMOYO_MANAGER:
23919590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/manager */
2392e2bf6907STetsuo Handa 		head->write = tomoyo_write_manager;
2393e2bf6907STetsuo Handa 		head->read = tomoyo_read_manager;
23949590837bSKentaro Takeda 		break;
23959590837bSKentaro Takeda 	}
23969590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_READ)) {
23979590837bSKentaro Takeda 		/*
23989590837bSKentaro Takeda 		 * No need to allocate read_buf since it is not opened
23999590837bSKentaro Takeda 		 * for reading.
24009590837bSKentaro Takeda 		 */
24019590837bSKentaro Takeda 		head->read = NULL;
240217fcfbd9STetsuo Handa 		head->poll = NULL;
240317fcfbd9STetsuo Handa 	} else if (!head->poll) {
240417fcfbd9STetsuo Handa 		/* Don't allocate read_buf for poll() access. */
24059590837bSKentaro Takeda 		if (!head->readbuf_size)
24069590837bSKentaro Takeda 			head->readbuf_size = 4096 * 2;
24074e5d6f7eSTetsuo Handa 		head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
24089590837bSKentaro Takeda 		if (!head->read_buf) {
24098e2d39a1STetsuo Handa 			kfree(head);
24109590837bSKentaro Takeda 			return -ENOMEM;
24119590837bSKentaro Takeda 		}
24129590837bSKentaro Takeda 	}
24139590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_WRITE)) {
24149590837bSKentaro Takeda 		/*
24159590837bSKentaro Takeda 		 * No need to allocate write_buf since it is not opened
24169590837bSKentaro Takeda 		 * for writing.
24179590837bSKentaro Takeda 		 */
24189590837bSKentaro Takeda 		head->write = NULL;
24199590837bSKentaro Takeda 	} else if (head->write) {
24209590837bSKentaro Takeda 		head->writebuf_size = 4096 * 2;
24214e5d6f7eSTetsuo Handa 		head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
24229590837bSKentaro Takeda 		if (!head->write_buf) {
24238e2d39a1STetsuo Handa 			kfree(head->read_buf);
24248e2d39a1STetsuo Handa 			kfree(head);
24259590837bSKentaro Takeda 			return -ENOMEM;
24269590837bSKentaro Takeda 		}
24279590837bSKentaro Takeda 	}
24289590837bSKentaro Takeda 	/*
242917fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , increment the
243017fcfbd9STetsuo Handa 	 * observer counter.
243117fcfbd9STetsuo Handa 	 * The obserber counter is used by tomoyo_supervisor() to see if
243217fcfbd9STetsuo Handa 	 * there is some process monitoring /sys/kernel/security/tomoyo/query.
243317fcfbd9STetsuo Handa 	 */
24347c75964fSTetsuo Handa 	if (type == TOMOYO_QUERY)
243517fcfbd9STetsuo Handa 		atomic_inc(&tomoyo_query_observers);
24362e503bbbSTetsuo Handa 	file->private_data = head;
24372e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, true);
24389590837bSKentaro Takeda 	return 0;
24399590837bSKentaro Takeda }
24409590837bSKentaro Takeda 
24419590837bSKentaro Takeda /**
24420849e3baSTetsuo Handa  * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
24430849e3baSTetsuo Handa  *
24440849e3baSTetsuo Handa  * @file: Pointer to "struct file".
24450849e3baSTetsuo Handa  * @wait: Pointer to "poll_table".
24460849e3baSTetsuo Handa  *
24470849e3baSTetsuo Handa  * Waits for read readiness.
2448eadd99ccSTetsuo Handa  * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2449eadd99ccSTetsuo Handa  * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
24500849e3baSTetsuo Handa  */
24510849e3baSTetsuo Handa int tomoyo_poll_control(struct file *file, poll_table *wait)
24520849e3baSTetsuo Handa {
24530849e3baSTetsuo Handa 	struct tomoyo_io_buffer *head = file->private_data;
24540849e3baSTetsuo Handa 	if (!head->poll)
24550849e3baSTetsuo Handa 		return -ENOSYS;
24560849e3baSTetsuo Handa 	return head->poll(file, wait);
24570849e3baSTetsuo Handa }
24580849e3baSTetsuo Handa 
24590849e3baSTetsuo Handa /**
2460bd03a3e4STetsuo Handa  * tomoyo_set_namespace_cursor - Set namespace to read.
2461bd03a3e4STetsuo Handa  *
2462bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2463bd03a3e4STetsuo Handa  *
2464bd03a3e4STetsuo Handa  * Returns nothing.
2465bd03a3e4STetsuo Handa  */
2466bd03a3e4STetsuo Handa static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2467bd03a3e4STetsuo Handa {
2468bd03a3e4STetsuo Handa 	struct list_head *ns;
2469bd03a3e4STetsuo Handa 	if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2470bd03a3e4STetsuo Handa 	    head->type != TOMOYO_PROFILE)
2471bd03a3e4STetsuo Handa 		return;
2472bd03a3e4STetsuo Handa 	/*
2473bd03a3e4STetsuo Handa 	 * If this is the first read, or reading previous namespace finished
2474bd03a3e4STetsuo Handa 	 * and has more namespaces to read, update the namespace cursor.
2475bd03a3e4STetsuo Handa 	 */
2476bd03a3e4STetsuo Handa 	ns = head->r.ns;
2477bd03a3e4STetsuo Handa 	if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2478bd03a3e4STetsuo Handa 		/* Clearing is OK because tomoyo_flush() returned true. */
2479bd03a3e4STetsuo Handa 		memset(&head->r, 0, sizeof(head->r));
2480bd03a3e4STetsuo Handa 		head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2481bd03a3e4STetsuo Handa 	}
2482bd03a3e4STetsuo Handa }
2483bd03a3e4STetsuo Handa 
2484bd03a3e4STetsuo Handa /**
2485bd03a3e4STetsuo Handa  * tomoyo_has_more_namespace - Check for unread namespaces.
2486bd03a3e4STetsuo Handa  *
2487bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2488bd03a3e4STetsuo Handa  *
2489bd03a3e4STetsuo Handa  * Returns true if we have more entries to print, false otherwise.
2490bd03a3e4STetsuo Handa  */
2491bd03a3e4STetsuo Handa static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2492bd03a3e4STetsuo Handa {
2493bd03a3e4STetsuo Handa 	return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2494bd03a3e4STetsuo Handa 		head->type == TOMOYO_PROFILE) && head->r.eof &&
2495bd03a3e4STetsuo Handa 		head->r.ns->next != &tomoyo_namespace_list;
2496bd03a3e4STetsuo Handa }
2497bd03a3e4STetsuo Handa 
2498bd03a3e4STetsuo Handa /**
24999590837bSKentaro Takeda  * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
25009590837bSKentaro Takeda  *
25010df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25029590837bSKentaro Takeda  * @buffer:     Poiner to buffer to write to.
25039590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25049590837bSKentaro Takeda  *
25059590837bSKentaro Takeda  * Returns bytes read on success, negative value otherwise.
25069590837bSKentaro Takeda  */
25072c47ab93STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
25089590837bSKentaro Takeda 			    const int buffer_len)
25099590837bSKentaro Takeda {
2510f23571e8STetsuo Handa 	int len;
25112e503bbbSTetsuo Handa 	int idx;
25129590837bSKentaro Takeda 
25139590837bSKentaro Takeda 	if (!head->read)
25149590837bSKentaro Takeda 		return -ENOSYS;
25159590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25169590837bSKentaro Takeda 		return -EINTR;
2517f23571e8STetsuo Handa 	head->read_user_buf = buffer;
2518f23571e8STetsuo Handa 	head->read_user_buf_avail = buffer_len;
25192e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
2520f23571e8STetsuo Handa 	if (tomoyo_flush(head))
25219590837bSKentaro Takeda 		/* Call the policy handler. */
2522bd03a3e4STetsuo Handa 		do {
2523bd03a3e4STetsuo Handa 			tomoyo_set_namespace_cursor(head);
25248fbe71f0STetsuo Handa 			head->read(head);
2525bd03a3e4STetsuo Handa 		} while (tomoyo_flush(head) &&
2526bd03a3e4STetsuo Handa 			 tomoyo_has_more_namespace(head));
25272e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
2528f23571e8STetsuo Handa 	len = head->read_user_buf - buffer;
25299590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
25309590837bSKentaro Takeda 	return len;
25319590837bSKentaro Takeda }
25329590837bSKentaro Takeda 
25339590837bSKentaro Takeda /**
2534bd03a3e4STetsuo Handa  * tomoyo_parse_policy - Parse a policy line.
2535bd03a3e4STetsuo Handa  *
2536bd03a3e4STetsuo Handa  * @head: Poiter to "struct tomoyo_io_buffer".
2537bd03a3e4STetsuo Handa  * @line: Line to parse.
2538bd03a3e4STetsuo Handa  *
2539bd03a3e4STetsuo Handa  * Returns 0 on success, negative value otherwise.
2540bd03a3e4STetsuo Handa  *
2541bd03a3e4STetsuo Handa  * Caller holds tomoyo_read_lock().
2542bd03a3e4STetsuo Handa  */
2543bd03a3e4STetsuo Handa static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2544bd03a3e4STetsuo Handa {
2545bd03a3e4STetsuo Handa 	/* Delete request? */
2546bd03a3e4STetsuo Handa 	head->w.is_delete = !strncmp(line, "delete ", 7);
2547bd03a3e4STetsuo Handa 	if (head->w.is_delete)
2548bd03a3e4STetsuo Handa 		memmove(line, line + 7, strlen(line + 7) + 1);
2549bd03a3e4STetsuo Handa 	/* Selecting namespace to update. */
2550bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2551bd03a3e4STetsuo Handa 	    head->type == TOMOYO_PROFILE) {
2552bd03a3e4STetsuo Handa 		if (*line == '<') {
2553bd03a3e4STetsuo Handa 			char *cp = strchr(line, ' ');
2554bd03a3e4STetsuo Handa 			if (cp) {
2555bd03a3e4STetsuo Handa 				*cp++ = '\0';
2556bd03a3e4STetsuo Handa 				head->w.ns = tomoyo_assign_namespace(line);
2557bd03a3e4STetsuo Handa 				memmove(line, cp, strlen(cp) + 1);
2558bd03a3e4STetsuo Handa 			} else
2559bd03a3e4STetsuo Handa 				head->w.ns = NULL;
2560bd03a3e4STetsuo Handa 		} else
2561bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2562bd03a3e4STetsuo Handa 		/* Don't allow updating if namespace is invalid. */
2563bd03a3e4STetsuo Handa 		if (!head->w.ns)
2564bd03a3e4STetsuo Handa 			return -ENOENT;
2565bd03a3e4STetsuo Handa 	}
2566bd03a3e4STetsuo Handa 	/* Do the update. */
2567bd03a3e4STetsuo Handa 	return head->write(head);
2568bd03a3e4STetsuo Handa }
2569bd03a3e4STetsuo Handa 
2570bd03a3e4STetsuo Handa /**
25719590837bSKentaro Takeda  * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
25729590837bSKentaro Takeda  *
25730df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25749590837bSKentaro Takeda  * @buffer:     Pointer to buffer to read from.
25759590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25769590837bSKentaro Takeda  *
25779590837bSKentaro Takeda  * Returns @buffer_len on success, negative value otherwise.
25789590837bSKentaro Takeda  */
25792c47ab93STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
25800df7e8b8STetsuo Handa 			     const char __user *buffer, const int buffer_len)
25819590837bSKentaro Takeda {
25829590837bSKentaro Takeda 	int error = buffer_len;
2583bd03a3e4STetsuo Handa 	size_t avail_len = buffer_len;
25849590837bSKentaro Takeda 	char *cp0 = head->write_buf;
25852e503bbbSTetsuo Handa 	int idx;
25869590837bSKentaro Takeda 	if (!head->write)
25879590837bSKentaro Takeda 		return -ENOSYS;
25889590837bSKentaro Takeda 	if (!access_ok(VERIFY_READ, buffer, buffer_len))
25899590837bSKentaro Takeda 		return -EFAULT;
25909590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25919590837bSKentaro Takeda 		return -EINTR;
25922e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
25939590837bSKentaro Takeda 	/* Read a line and dispatch it to the policy handler. */
25949590837bSKentaro Takeda 	while (avail_len > 0) {
25959590837bSKentaro Takeda 		char c;
25960df7e8b8STetsuo Handa 		if (head->w.avail >= head->writebuf_size - 1) {
2597bd03a3e4STetsuo Handa 			const int len = head->writebuf_size * 2;
2598bd03a3e4STetsuo Handa 			char *cp = kzalloc(len, GFP_NOFS);
2599bd03a3e4STetsuo Handa 			if (!cp) {
26009590837bSKentaro Takeda 				error = -ENOMEM;
26019590837bSKentaro Takeda 				break;
2602bd03a3e4STetsuo Handa 			}
2603bd03a3e4STetsuo Handa 			memmove(cp, cp0, head->w.avail);
2604bd03a3e4STetsuo Handa 			kfree(cp0);
2605bd03a3e4STetsuo Handa 			head->write_buf = cp;
2606bd03a3e4STetsuo Handa 			cp0 = cp;
2607bd03a3e4STetsuo Handa 			head->writebuf_size = len;
2608bd03a3e4STetsuo Handa 		}
2609bd03a3e4STetsuo Handa 		if (get_user(c, buffer)) {
26109590837bSKentaro Takeda 			error = -EFAULT;
26119590837bSKentaro Takeda 			break;
26129590837bSKentaro Takeda 		}
26139590837bSKentaro Takeda 		buffer++;
26149590837bSKentaro Takeda 		avail_len--;
26150df7e8b8STetsuo Handa 		cp0[head->w.avail++] = c;
26169590837bSKentaro Takeda 		if (c != '\n')
26179590837bSKentaro Takeda 			continue;
26180df7e8b8STetsuo Handa 		cp0[head->w.avail - 1] = '\0';
26190df7e8b8STetsuo Handa 		head->w.avail = 0;
26209590837bSKentaro Takeda 		tomoyo_normalize_line(cp0);
2621bd03a3e4STetsuo Handa 		if (!strcmp(cp0, "reset")) {
2622bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2623bd03a3e4STetsuo Handa 			head->w.domain = NULL;
2624bd03a3e4STetsuo Handa 			memset(&head->r, 0, sizeof(head->r));
2625bd03a3e4STetsuo Handa 			continue;
26269590837bSKentaro Takeda 		}
2627bd03a3e4STetsuo Handa 		/* Don't allow updating policies by non manager programs. */
2628bd03a3e4STetsuo Handa 		switch (head->type) {
2629bd03a3e4STetsuo Handa 		case TOMOYO_PROCESS_STATUS:
2630bd03a3e4STetsuo Handa 			/* This does not write anything. */
2631bd03a3e4STetsuo Handa 			break;
2632bd03a3e4STetsuo Handa 		case TOMOYO_DOMAINPOLICY:
2633bd03a3e4STetsuo Handa 			if (tomoyo_select_domain(head, cp0))
2634bd03a3e4STetsuo Handa 				continue;
2635bd03a3e4STetsuo Handa 			/* fall through */
2636bd03a3e4STetsuo Handa 		case TOMOYO_EXCEPTIONPOLICY:
2637bd03a3e4STetsuo Handa 			if (!strcmp(cp0, "select transition_only")) {
2638bd03a3e4STetsuo Handa 				head->r.print_transition_related_only = true;
2639bd03a3e4STetsuo Handa 				continue;
2640bd03a3e4STetsuo Handa 			}
2641bd03a3e4STetsuo Handa 			/* fall through */
2642bd03a3e4STetsuo Handa 		default:
2643bd03a3e4STetsuo Handa 			if (!tomoyo_manager()) {
2644bd03a3e4STetsuo Handa 				error = -EPERM;
2645bd03a3e4STetsuo Handa 				goto out;
2646bd03a3e4STetsuo Handa 			}
2647bd03a3e4STetsuo Handa 		}
2648bd03a3e4STetsuo Handa 		switch (tomoyo_parse_policy(head, cp0)) {
2649bd03a3e4STetsuo Handa 		case -EPERM:
2650bd03a3e4STetsuo Handa 			error = -EPERM;
2651bd03a3e4STetsuo Handa 			goto out;
2652b22b8b9fSTetsuo Handa 		case 0:
2653b22b8b9fSTetsuo Handa 			switch (head->type) {
2654b22b8b9fSTetsuo Handa 			case TOMOYO_DOMAINPOLICY:
2655b22b8b9fSTetsuo Handa 			case TOMOYO_EXCEPTIONPOLICY:
2656b22b8b9fSTetsuo Handa 			case TOMOYO_STAT:
2657b22b8b9fSTetsuo Handa 			case TOMOYO_PROFILE:
2658b22b8b9fSTetsuo Handa 			case TOMOYO_MANAGER:
2659b22b8b9fSTetsuo Handa 				tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2660b22b8b9fSTetsuo Handa 				break;
2661b22b8b9fSTetsuo Handa 			default:
2662b22b8b9fSTetsuo Handa 				break;
2663b22b8b9fSTetsuo Handa 			}
2664b22b8b9fSTetsuo Handa 			break;
2665bd03a3e4STetsuo Handa 		}
2666bd03a3e4STetsuo Handa 	}
2667bd03a3e4STetsuo Handa out:
26682e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
26699590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
26709590837bSKentaro Takeda 	return error;
26719590837bSKentaro Takeda }
26729590837bSKentaro Takeda 
26739590837bSKentaro Takeda /**
26749590837bSKentaro Takeda  * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
26759590837bSKentaro Takeda  *
26760df7e8b8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
26779590837bSKentaro Takeda  *
26782e503bbbSTetsuo Handa  * Returns 0.
26799590837bSKentaro Takeda  */
26800df7e8b8STetsuo Handa int tomoyo_close_control(struct tomoyo_io_buffer *head)
26819590837bSKentaro Takeda {
268217fcfbd9STetsuo Handa 	/*
268317fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , decrement the
268417fcfbd9STetsuo Handa 	 * observer counter.
268517fcfbd9STetsuo Handa 	 */
26862e503bbbSTetsuo Handa 	if (head->type == TOMOYO_QUERY &&
26872e503bbbSTetsuo Handa 	    atomic_dec_and_test(&tomoyo_query_observers))
26882e503bbbSTetsuo Handa 		wake_up_all(&tomoyo_answer_wait);
26892e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, false);
26909590837bSKentaro Takeda 	return 0;
26919590837bSKentaro Takeda }
26929590837bSKentaro Takeda 
26939590837bSKentaro Takeda /**
2694c3ef1500STetsuo Handa  * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
26959590837bSKentaro Takeda  */
2696c3ef1500STetsuo Handa void tomoyo_check_profile(void)
26979590837bSKentaro Takeda {
2698c3ef1500STetsuo Handa 	struct tomoyo_domain_info *domain;
2699c3ef1500STetsuo Handa 	const int idx = tomoyo_read_lock();
2700c3ef1500STetsuo Handa 	tomoyo_policy_loaded = true;
2701843d183cSTetsuo Handa 	printk(KERN_INFO "TOMOYO: 2.5.0\n");
2702c3ef1500STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2703c3ef1500STetsuo Handa 		const u8 profile = domain->profile;
2704bd03a3e4STetsuo Handa 		const struct tomoyo_policy_namespace *ns = domain->ns;
2705843d183cSTetsuo Handa 		if (ns->profile_version != 20110903)
2706bd03a3e4STetsuo Handa 			printk(KERN_ERR
2707bd03a3e4STetsuo Handa 			       "Profile version %u is not supported.\n",
2708bd03a3e4STetsuo Handa 			       ns->profile_version);
2709bd03a3e4STetsuo Handa 		else if (!ns->profile_ptr[profile])
2710bd03a3e4STetsuo Handa 			printk(KERN_ERR
2711bd03a3e4STetsuo Handa 			       "Profile %u (used by '%s') is not defined.\n",
2712c3ef1500STetsuo Handa 			       profile, domain->domainname->name);
2713bd03a3e4STetsuo Handa 		else
2714bd03a3e4STetsuo Handa 			continue;
2715bd03a3e4STetsuo Handa 		printk(KERN_ERR
2716843d183cSTetsuo Handa 		       "Userland tools for TOMOYO 2.5 must be installed and "
2717bd03a3e4STetsuo Handa 		       "policy must be initialized.\n");
2718843d183cSTetsuo Handa 		printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
2719bd03a3e4STetsuo Handa 		       "for more information.\n");
2720bd03a3e4STetsuo Handa 		panic("STOP!");
27219590837bSKentaro Takeda 	}
2722c3ef1500STetsuo Handa 	tomoyo_read_unlock(idx);
2723c3ef1500STetsuo Handa 	printk(KERN_INFO "Mandatory Access Control activated.\n");
27249590837bSKentaro Takeda }
2725efe836abSTetsuo Handa 
2726efe836abSTetsuo Handa /**
2727efe836abSTetsuo Handa  * tomoyo_load_builtin_policy - Load built-in policy.
2728efe836abSTetsuo Handa  *
2729efe836abSTetsuo Handa  * Returns nothing.
2730efe836abSTetsuo Handa  */
2731efe836abSTetsuo Handa void __init tomoyo_load_builtin_policy(void)
2732efe836abSTetsuo Handa {
2733efe836abSTetsuo Handa 	/*
2734efe836abSTetsuo Handa 	 * This include file is manually created and contains built-in policy
2735efe836abSTetsuo Handa 	 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2736efe836abSTetsuo Handa 	 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2737efe836abSTetsuo Handa 	 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2738efe836abSTetsuo Handa 	 */
2739efe836abSTetsuo Handa #include "builtin-policy.h"
2740efe836abSTetsuo Handa 	u8 i;
2741efe836abSTetsuo Handa 	const int idx = tomoyo_read_lock();
2742efe836abSTetsuo Handa 	for (i = 0; i < 5; i++) {
2743efe836abSTetsuo Handa 		struct tomoyo_io_buffer head = { };
2744efe836abSTetsuo Handa 		char *start = "";
2745efe836abSTetsuo Handa 		switch (i) {
2746efe836abSTetsuo Handa 		case 0:
2747efe836abSTetsuo Handa 			start = tomoyo_builtin_profile;
2748efe836abSTetsuo Handa 			head.type = TOMOYO_PROFILE;
2749efe836abSTetsuo Handa 			head.write = tomoyo_write_profile;
2750efe836abSTetsuo Handa 			break;
2751efe836abSTetsuo Handa 		case 1:
2752efe836abSTetsuo Handa 			start = tomoyo_builtin_exception_policy;
2753efe836abSTetsuo Handa 			head.type = TOMOYO_EXCEPTIONPOLICY;
2754efe836abSTetsuo Handa 			head.write = tomoyo_write_exception;
2755efe836abSTetsuo Handa 			break;
2756efe836abSTetsuo Handa 		case 2:
2757efe836abSTetsuo Handa 			start = tomoyo_builtin_domain_policy;
2758efe836abSTetsuo Handa 			head.type = TOMOYO_DOMAINPOLICY;
2759efe836abSTetsuo Handa 			head.write = tomoyo_write_domain;
2760efe836abSTetsuo Handa 			break;
2761efe836abSTetsuo Handa 		case 3:
2762efe836abSTetsuo Handa 			start = tomoyo_builtin_manager;
2763efe836abSTetsuo Handa 			head.type = TOMOYO_MANAGER;
2764efe836abSTetsuo Handa 			head.write = tomoyo_write_manager;
2765efe836abSTetsuo Handa 			break;
2766efe836abSTetsuo Handa 		case 4:
2767efe836abSTetsuo Handa 			start = tomoyo_builtin_stat;
2768efe836abSTetsuo Handa 			head.type = TOMOYO_STAT;
2769efe836abSTetsuo Handa 			head.write = tomoyo_write_stat;
2770efe836abSTetsuo Handa 			break;
2771efe836abSTetsuo Handa 		}
2772efe836abSTetsuo Handa 		while (1) {
2773efe836abSTetsuo Handa 			char *end = strchr(start, '\n');
2774efe836abSTetsuo Handa 			if (!end)
2775efe836abSTetsuo Handa 				break;
2776efe836abSTetsuo Handa 			*end = '\0';
2777efe836abSTetsuo Handa 			tomoyo_normalize_line(start);
2778efe836abSTetsuo Handa 			head.write_buf = start;
2779efe836abSTetsuo Handa 			tomoyo_parse_policy(&head, start);
2780efe836abSTetsuo Handa 			start = end + 1;
2781efe836abSTetsuo Handa 		}
2782efe836abSTetsuo Handa 	}
2783efe836abSTetsuo Handa 	tomoyo_read_unlock(idx);
27840e4ae0e0STetsuo Handa #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
27850e4ae0e0STetsuo Handa 	tomoyo_check_profile();
27860e4ae0e0STetsuo Handa #endif
2787efe836abSTetsuo Handa }
2788