xref: /linux/security/tomoyo/common.c (revision 59df3166ef293288d164ab3362a717743e62d20c)
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 
265778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
266778c4a4dSTetsuo Handa 			     ...) __printf(2, 3);
267778c4a4dSTetsuo Handa 
268f23571e8STetsuo Handa /**
269f23571e8STetsuo Handa  * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
270f23571e8STetsuo Handa  *
271f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
272f23571e8STetsuo Handa  * @fmt:  The printf()'s format string, followed by parameters.
273f23571e8STetsuo Handa  */
274778c4a4dSTetsuo Handa static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt,
275778c4a4dSTetsuo Handa 			     ...)
276f23571e8STetsuo Handa {
277f23571e8STetsuo Handa 	va_list args;
2782c47ab93STetsuo Handa 	size_t len;
2792c47ab93STetsuo Handa 	size_t pos = head->r.avail;
280f23571e8STetsuo Handa 	int size = head->readbuf_size - pos;
281f23571e8STetsuo Handa 	if (size <= 0)
282f23571e8STetsuo Handa 		return;
283f23571e8STetsuo Handa 	va_start(args, fmt);
284f23571e8STetsuo Handa 	len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
285f23571e8STetsuo Handa 	va_end(args);
286f23571e8STetsuo Handa 	if (pos + len >= head->readbuf_size) {
287f23571e8STetsuo Handa 		WARN_ON(1);
288f23571e8STetsuo Handa 		return;
289f23571e8STetsuo Handa 	}
290f23571e8STetsuo Handa 	head->r.avail += len;
291f23571e8STetsuo Handa 	tomoyo_set_string(head, head->read_buf + pos);
292f23571e8STetsuo Handa }
293f23571e8STetsuo Handa 
2940d2171d7STetsuo Handa /**
2950d2171d7STetsuo Handa  * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
2960d2171d7STetsuo Handa  *
2970d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2980d2171d7STetsuo Handa  *
2990d2171d7STetsuo Handa  * Returns nothing.
3000d2171d7STetsuo Handa  */
301f23571e8STetsuo Handa static void tomoyo_set_space(struct tomoyo_io_buffer *head)
302f23571e8STetsuo Handa {
303f23571e8STetsuo Handa 	tomoyo_set_string(head, " ");
304f23571e8STetsuo Handa }
305f23571e8STetsuo Handa 
3060d2171d7STetsuo Handa /**
3070d2171d7STetsuo Handa  * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
3080d2171d7STetsuo Handa  *
3090d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3100d2171d7STetsuo Handa  *
3110d2171d7STetsuo Handa  * Returns nothing.
3120d2171d7STetsuo Handa  */
313f23571e8STetsuo Handa static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
314f23571e8STetsuo Handa {
315f23571e8STetsuo Handa 	tomoyo_set_string(head, "\n");
316f23571e8STetsuo Handa 	return !head->r.w_pos;
317f23571e8STetsuo Handa }
318f23571e8STetsuo Handa 
31957c2590fSTetsuo Handa /**
3200d2171d7STetsuo Handa  * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
3210d2171d7STetsuo Handa  *
3220d2171d7STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3230d2171d7STetsuo Handa  *
3240d2171d7STetsuo Handa  * Returns nothing.
3250d2171d7STetsuo Handa  */
3260d2171d7STetsuo Handa static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
3270d2171d7STetsuo Handa {
3280d2171d7STetsuo Handa 	tomoyo_set_string(head, "/");
3290d2171d7STetsuo Handa }
3300d2171d7STetsuo Handa 
331bd03a3e4STetsuo Handa /* List of namespaces. */
332bd03a3e4STetsuo Handa LIST_HEAD(tomoyo_namespace_list);
333bd03a3e4STetsuo Handa /* True if namespace other than tomoyo_kernel_namespace is defined. */
334bd03a3e4STetsuo Handa static bool tomoyo_namespace_enabled;
335bd03a3e4STetsuo Handa 
336bd03a3e4STetsuo Handa /**
337bd03a3e4STetsuo Handa  * tomoyo_init_policy_namespace - Initialize namespace.
338bd03a3e4STetsuo Handa  *
339bd03a3e4STetsuo Handa  * @ns: Pointer to "struct tomoyo_policy_namespace".
340bd03a3e4STetsuo Handa  *
341bd03a3e4STetsuo Handa  * Returns nothing.
342bd03a3e4STetsuo Handa  */
343bd03a3e4STetsuo Handa void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
344bd03a3e4STetsuo Handa {
345bd03a3e4STetsuo Handa 	unsigned int idx;
346bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
347bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->acl_group[idx]);
348bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
349bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->group_list[idx]);
350bd03a3e4STetsuo Handa 	for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
351bd03a3e4STetsuo Handa 		INIT_LIST_HEAD(&ns->policy_list[idx]);
352843d183cSTetsuo Handa 	ns->profile_version = 20110903;
353bd03a3e4STetsuo Handa 	tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
354bd03a3e4STetsuo Handa 	list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
355bd03a3e4STetsuo Handa }
356bd03a3e4STetsuo Handa 
357bd03a3e4STetsuo Handa /**
358bd03a3e4STetsuo Handa  * tomoyo_print_namespace - Print namespace header.
359bd03a3e4STetsuo Handa  *
360bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
361bd03a3e4STetsuo Handa  *
362bd03a3e4STetsuo Handa  * Returns nothing.
363bd03a3e4STetsuo Handa  */
364bd03a3e4STetsuo Handa static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
365bd03a3e4STetsuo Handa {
366bd03a3e4STetsuo Handa 	if (!tomoyo_namespace_enabled)
367bd03a3e4STetsuo Handa 		return;
368bd03a3e4STetsuo Handa 	tomoyo_set_string(head,
369bd03a3e4STetsuo Handa 			  container_of(head->r.ns,
370bd03a3e4STetsuo Handa 				       struct tomoyo_policy_namespace,
371bd03a3e4STetsuo Handa 				       namespace_list)->name);
372bd03a3e4STetsuo Handa 	tomoyo_set_space(head);
373bd03a3e4STetsuo Handa }
374bd03a3e4STetsuo Handa 
3750d2171d7STetsuo Handa /**
3767762fbffSTetsuo Handa  * tomoyo_print_name_union - Print a tomoyo_name_union.
3777762fbffSTetsuo Handa  *
3787762fbffSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3797762fbffSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3807762fbffSTetsuo Handa  */
381f23571e8STetsuo Handa static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
3827762fbffSTetsuo Handa 				    const struct tomoyo_name_union *ptr)
3837762fbffSTetsuo Handa {
384f23571e8STetsuo Handa 	tomoyo_set_space(head);
3850df7e8b8STetsuo Handa 	if (ptr->group) {
386f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
387f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
388f23571e8STetsuo Handa 	} else {
389f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
390f23571e8STetsuo Handa 	}
3917762fbffSTetsuo Handa }
3927762fbffSTetsuo Handa 
3937762fbffSTetsuo Handa /**
3942ca9bf45STetsuo Handa  * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
3952ca9bf45STetsuo Handa  *
3962ca9bf45STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
3972ca9bf45STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_name_union".
3982ca9bf45STetsuo Handa  *
3992ca9bf45STetsuo Handa  * Returns nothing.
4002ca9bf45STetsuo Handa  */
4012ca9bf45STetsuo Handa static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
4022ca9bf45STetsuo Handa 					   const struct tomoyo_name_union *ptr)
4032ca9bf45STetsuo Handa {
4042ca9bf45STetsuo Handa 	if (ptr->group) {
4052ca9bf45STetsuo Handa 		tomoyo_set_string(head, "@");
4062ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
4072ca9bf45STetsuo Handa 	} else {
4082ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4092ca9bf45STetsuo Handa 		tomoyo_set_string(head, ptr->filename->name);
4102ca9bf45STetsuo Handa 		tomoyo_set_string(head, "\"");
4112ca9bf45STetsuo Handa 	}
4122ca9bf45STetsuo Handa }
4132ca9bf45STetsuo Handa 
4142ca9bf45STetsuo Handa /**
4152066a361STetsuo Handa  * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
4164c3e9e2dSTetsuo Handa  *
4174c3e9e2dSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4184c3e9e2dSTetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4192066a361STetsuo Handa  *
4202066a361STetsuo Handa  * Returns nothing.
4214c3e9e2dSTetsuo Handa  */
4222066a361STetsuo Handa static void tomoyo_print_number_union_nospace
4232066a361STetsuo Handa (struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
4244c3e9e2dSTetsuo Handa {
4250df7e8b8STetsuo Handa 	if (ptr->group) {
426f23571e8STetsuo Handa 		tomoyo_set_string(head, "@");
427f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->group->group_name->name);
428f23571e8STetsuo Handa 	} else {
429f23571e8STetsuo Handa 		int i;
430f23571e8STetsuo Handa 		unsigned long min = ptr->values[0];
431f23571e8STetsuo Handa 		const unsigned long max = ptr->values[1];
4320df7e8b8STetsuo Handa 		u8 min_type = ptr->value_type[0];
4330df7e8b8STetsuo Handa 		const u8 max_type = ptr->value_type[1];
434f23571e8STetsuo Handa 		char buffer[128];
435f23571e8STetsuo Handa 		buffer[0] = '\0';
436f23571e8STetsuo Handa 		for (i = 0; i < 2; i++) {
4374c3e9e2dSTetsuo Handa 			switch (min_type) {
4384c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_HEXADECIMAL:
439f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
440f23571e8STetsuo Handa 						 "0x%lX", min);
4414c3e9e2dSTetsuo Handa 				break;
4424c3e9e2dSTetsuo Handa 			case TOMOYO_VALUE_TYPE_OCTAL:
443f23571e8STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer),
444f23571e8STetsuo Handa 						 "0%lo", min);
4454c3e9e2dSTetsuo Handa 				break;
4464c3e9e2dSTetsuo Handa 			default:
4472066a361STetsuo Handa 				tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
4482066a361STetsuo Handa 						 min);
4494c3e9e2dSTetsuo Handa 				break;
4504c3e9e2dSTetsuo Handa 			}
4514c3e9e2dSTetsuo Handa 			if (min == max && min_type == max_type)
452f23571e8STetsuo Handa 				break;
453f23571e8STetsuo Handa 			tomoyo_addprintf(buffer, sizeof(buffer), "-");
454f23571e8STetsuo Handa 			min_type = max_type;
455f23571e8STetsuo Handa 			min = max;
4564c3e9e2dSTetsuo Handa 		}
457f23571e8STetsuo Handa 		tomoyo_io_printf(head, "%s", buffer);
4584c3e9e2dSTetsuo Handa 	}
4599590837bSKentaro Takeda }
4609590837bSKentaro Takeda 
4619590837bSKentaro Takeda /**
4622066a361STetsuo Handa  * tomoyo_print_number_union - Print a tomoyo_number_union.
4632066a361STetsuo Handa  *
4642066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
4652066a361STetsuo Handa  * @ptr:  Pointer to "struct tomoyo_number_union".
4662066a361STetsuo Handa  *
4672066a361STetsuo Handa  * Returns nothing.
4682066a361STetsuo Handa  */
4692066a361STetsuo Handa static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
4702066a361STetsuo Handa 				      const struct tomoyo_number_union *ptr)
4712066a361STetsuo Handa {
4722066a361STetsuo Handa 	tomoyo_set_space(head);
4732066a361STetsuo Handa 	tomoyo_print_number_union_nospace(head, ptr);
4742066a361STetsuo Handa }
4752066a361STetsuo Handa 
4762066a361STetsuo Handa /**
477e2bf6907STetsuo Handa  * tomoyo_assign_profile - Create a new profile.
4789590837bSKentaro Takeda  *
479bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
4809590837bSKentaro Takeda  * @profile: Profile number to create.
4819590837bSKentaro Takeda  *
4829590837bSKentaro Takeda  * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
4839590837bSKentaro Takeda  */
484bd03a3e4STetsuo Handa static struct tomoyo_profile *tomoyo_assign_profile
485bd03a3e4STetsuo Handa (struct tomoyo_policy_namespace *ns, const unsigned int profile)
4869590837bSKentaro Takeda {
48757c2590fSTetsuo Handa 	struct tomoyo_profile *ptr;
48857c2590fSTetsuo Handa 	struct tomoyo_profile *entry;
4899590837bSKentaro Takeda 	if (profile >= TOMOYO_MAX_PROFILES)
4909590837bSKentaro Takeda 		return NULL;
491bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
4929590837bSKentaro Takeda 	if (ptr)
49357c2590fSTetsuo Handa 		return ptr;
49457c2590fSTetsuo Handa 	entry = kzalloc(sizeof(*entry), GFP_NOFS);
49557c2590fSTetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
49657c2590fSTetsuo Handa 		goto out;
497bd03a3e4STetsuo Handa 	ptr = ns->profile_ptr[profile];
49857c2590fSTetsuo Handa 	if (!ptr && tomoyo_memory_ok(entry)) {
49957c2590fSTetsuo Handa 		ptr = entry;
500eadd99ccSTetsuo Handa 		ptr->default_config = TOMOYO_CONFIG_DISABLED |
501eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_GRANT_LOG |
502eadd99ccSTetsuo Handa 			TOMOYO_CONFIG_WANT_REJECT_LOG;
50357c2590fSTetsuo Handa 		memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
50457c2590fSTetsuo Handa 		       sizeof(ptr->config));
5056afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] =
5066afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_AUDIT_LOG;
5076afcb3b7STetsuo Handa 		ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] =
5086afcb3b7STetsuo Handa 			CONFIG_SECURITY_TOMOYO_MAX_ACCEPT_ENTRY;
5099590837bSKentaro Takeda 		mb(); /* Avoid out-of-order execution. */
510bd03a3e4STetsuo Handa 		ns->profile_ptr[profile] = ptr;
51157c2590fSTetsuo Handa 		entry = NULL;
51257c2590fSTetsuo Handa 	}
51329282381STetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
51457c2590fSTetsuo Handa  out:
51557c2590fSTetsuo Handa 	kfree(entry);
5169590837bSKentaro Takeda 	return ptr;
5179590837bSKentaro Takeda }
5189590837bSKentaro Takeda 
5199590837bSKentaro Takeda /**
52057c2590fSTetsuo Handa  * tomoyo_profile - Find a profile.
52157c2590fSTetsuo Handa  *
522bd03a3e4STetsuo Handa  * @ns:      Pointer to "struct tomoyo_policy_namespace".
52357c2590fSTetsuo Handa  * @profile: Profile number to find.
52457c2590fSTetsuo Handa  *
52557c2590fSTetsuo Handa  * Returns pointer to "struct tomoyo_profile".
52657c2590fSTetsuo Handa  */
527bd03a3e4STetsuo Handa struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
528bd03a3e4STetsuo Handa 				      const u8 profile)
52957c2590fSTetsuo Handa {
530d5ca1725STetsuo Handa 	static struct tomoyo_profile tomoyo_null_profile;
531bd03a3e4STetsuo Handa 	struct tomoyo_profile *ptr = ns->profile_ptr[profile];
532d5ca1725STetsuo Handa 	if (!ptr)
533d5ca1725STetsuo Handa 		ptr = &tomoyo_null_profile;
53457c2590fSTetsuo Handa 	return ptr;
53557c2590fSTetsuo Handa }
53657c2590fSTetsuo Handa 
537d5ca1725STetsuo Handa /**
538d5ca1725STetsuo Handa  * tomoyo_find_yesno - Find values for specified keyword.
539d5ca1725STetsuo Handa  *
540d5ca1725STetsuo Handa  * @string: String to check.
541d5ca1725STetsuo Handa  * @find:   Name of keyword.
542d5ca1725STetsuo Handa  *
543d5ca1725STetsuo Handa  * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
544d5ca1725STetsuo Handa  */
5458e568687STetsuo Handa static s8 tomoyo_find_yesno(const char *string, const char *find)
5468e568687STetsuo Handa {
5478e568687STetsuo Handa 	const char *cp = strstr(string, find);
5488e568687STetsuo Handa 	if (cp) {
5498e568687STetsuo Handa 		cp += strlen(find);
5508e568687STetsuo Handa 		if (!strncmp(cp, "=yes", 4))
5518e568687STetsuo Handa 			return 1;
5528e568687STetsuo Handa 		else if (!strncmp(cp, "=no", 3))
5538e568687STetsuo Handa 			return 0;
5548e568687STetsuo Handa 	}
5558e568687STetsuo Handa 	return -1;
5568e568687STetsuo Handa }
5578e568687STetsuo Handa 
558d5ca1725STetsuo Handa /**
559d5ca1725STetsuo Handa  * tomoyo_set_uint - Set value for specified preference.
560d5ca1725STetsuo Handa  *
561d5ca1725STetsuo Handa  * @i:      Pointer to "unsigned int".
562d5ca1725STetsuo Handa  * @string: String to check.
563d5ca1725STetsuo Handa  * @find:   Name of keyword.
564d5ca1725STetsuo Handa  *
565d5ca1725STetsuo Handa  * Returns nothing.
566d5ca1725STetsuo Handa  */
5678e568687STetsuo Handa static void tomoyo_set_uint(unsigned int *i, const char *string,
5688e568687STetsuo Handa 			    const char *find)
5698e568687STetsuo Handa {
5708e568687STetsuo Handa 	const char *cp = strstr(string, find);
5718e568687STetsuo Handa 	if (cp)
5728e568687STetsuo Handa 		sscanf(cp + strlen(find), "=%u", i);
5738e568687STetsuo Handa }
5748e568687STetsuo Handa 
575d5ca1725STetsuo Handa /**
576d5ca1725STetsuo Handa  * tomoyo_set_mode - Set mode for specified profile.
577d5ca1725STetsuo Handa  *
578d5ca1725STetsuo Handa  * @name:    Name of functionality.
579d5ca1725STetsuo Handa  * @value:   Mode for @name.
580d5ca1725STetsuo Handa  * @profile: Pointer to "struct tomoyo_profile".
581d5ca1725STetsuo Handa  *
582d5ca1725STetsuo Handa  * Returns 0 on success, negative value otherwise.
583d5ca1725STetsuo Handa  */
5848e568687STetsuo Handa static int tomoyo_set_mode(char *name, const char *value,
5858e568687STetsuo Handa 			   struct tomoyo_profile *profile)
5868e568687STetsuo Handa {
5878e568687STetsuo Handa 	u8 i;
5888e568687STetsuo Handa 	u8 config;
5898e568687STetsuo Handa 	if (!strcmp(name, "CONFIG")) {
5908e568687STetsuo Handa 		i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
5918e568687STetsuo Handa 		config = profile->default_config;
5928e568687STetsuo Handa 	} else if (tomoyo_str_starts(&name, "CONFIG::")) {
5938e568687STetsuo Handa 		config = 0;
5948e568687STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MAC_INDEX
5958e568687STetsuo Handa 			     + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
5962c47ab93STetsuo Handa 			int len = 0;
5972c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX) {
5982c47ab93STetsuo Handa 				const u8 c = tomoyo_index2category[i];
5992c47ab93STetsuo Handa 				const char *category =
6002c47ab93STetsuo Handa 					tomoyo_category_keywords[c];
6012c47ab93STetsuo Handa 				len = strlen(category);
6022c47ab93STetsuo Handa 				if (strncmp(name, category, len) ||
6032c47ab93STetsuo Handa 				    name[len++] != ':' || name[len++] != ':')
6042c47ab93STetsuo Handa 					continue;
6052c47ab93STetsuo Handa 			}
6062c47ab93STetsuo Handa 			if (strcmp(name + len, tomoyo_mac_keywords[i]))
6078e568687STetsuo Handa 				continue;
6088e568687STetsuo Handa 			config = profile->config[i];
6098e568687STetsuo Handa 			break;
6108e568687STetsuo Handa 		}
6118e568687STetsuo Handa 		if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6128e568687STetsuo Handa 			return -EINVAL;
6138e568687STetsuo Handa 	} else {
6148e568687STetsuo Handa 		return -EINVAL;
6158e568687STetsuo Handa 	}
616d5ca1725STetsuo Handa 	if (strstr(value, "use_default")) {
6178e568687STetsuo Handa 		config = TOMOYO_CONFIG_USE_DEFAULT;
6188e568687STetsuo Handa 	} else {
6198e568687STetsuo Handa 		u8 mode;
6208e568687STetsuo Handa 		for (mode = 0; mode < 4; mode++)
6218e568687STetsuo Handa 			if (strstr(value, tomoyo_mode[mode]))
6228e568687STetsuo Handa 				/*
6238e568687STetsuo Handa 				 * Update lower 3 bits in order to distinguish
6248e568687STetsuo Handa 				 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
6258e568687STetsuo Handa 				 */
6268e568687STetsuo Handa 				config = (config & ~7) | mode;
627eadd99ccSTetsuo Handa 		if (config != TOMOYO_CONFIG_USE_DEFAULT) {
628eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "grant_log")) {
629eadd99ccSTetsuo Handa 			case 1:
630eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
631eadd99ccSTetsuo Handa 				break;
632eadd99ccSTetsuo Handa 			case 0:
633eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
634eadd99ccSTetsuo Handa 				break;
635eadd99ccSTetsuo Handa 			}
636eadd99ccSTetsuo Handa 			switch (tomoyo_find_yesno(value, "reject_log")) {
637eadd99ccSTetsuo Handa 			case 1:
638eadd99ccSTetsuo Handa 				config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
639eadd99ccSTetsuo Handa 				break;
640eadd99ccSTetsuo Handa 			case 0:
641eadd99ccSTetsuo Handa 				config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
642eadd99ccSTetsuo Handa 				break;
643eadd99ccSTetsuo Handa 			}
644eadd99ccSTetsuo Handa 		}
6458e568687STetsuo Handa 	}
6468e568687STetsuo Handa 	if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
6478e568687STetsuo Handa 		profile->config[i] = config;
6488e568687STetsuo Handa 	else if (config != TOMOYO_CONFIG_USE_DEFAULT)
6498e568687STetsuo Handa 		profile->default_config = config;
6508e568687STetsuo Handa 	return 0;
6518e568687STetsuo Handa }
6528e568687STetsuo Handa 
65357c2590fSTetsuo Handa /**
65457c2590fSTetsuo Handa  * tomoyo_write_profile - Write profile table.
6559590837bSKentaro Takeda  *
6569590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
6579590837bSKentaro Takeda  *
6589590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
6599590837bSKentaro Takeda  */
6609590837bSKentaro Takeda static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
6619590837bSKentaro Takeda {
6629590837bSKentaro Takeda 	char *data = head->write_buf;
6639590837bSKentaro Takeda 	unsigned int i;
6649590837bSKentaro Takeda 	char *cp;
6659590837bSKentaro Takeda 	struct tomoyo_profile *profile;
666bd03a3e4STetsuo Handa 	if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
667bd03a3e4STetsuo Handa 	    == 1)
66857c2590fSTetsuo Handa 		return 0;
66957c2590fSTetsuo Handa 	i = simple_strtoul(data, &cp, 10);
67057c2590fSTetsuo Handa 	if (*cp != '-')
6719590837bSKentaro Takeda 		return -EINVAL;
6729590837bSKentaro Takeda 	data = cp + 1;
673bd03a3e4STetsuo Handa 	profile = tomoyo_assign_profile(head->w.ns, i);
6749590837bSKentaro Takeda 	if (!profile)
6759590837bSKentaro Takeda 		return -EINVAL;
6769590837bSKentaro Takeda 	cp = strchr(data, '=');
6779590837bSKentaro Takeda 	if (!cp)
6789590837bSKentaro Takeda 		return -EINVAL;
67957c2590fSTetsuo Handa 	*cp++ = '\0';
6809590837bSKentaro Takeda 	if (!strcmp(data, "COMMENT")) {
6812a086e5dSTetsuo Handa 		static DEFINE_SPINLOCK(lock);
6822a086e5dSTetsuo Handa 		const struct tomoyo_path_info *new_comment
6832a086e5dSTetsuo Handa 			= tomoyo_get_name(cp);
6842a086e5dSTetsuo Handa 		const struct tomoyo_path_info *old_comment;
6852a086e5dSTetsuo Handa 		if (!new_comment)
6862a086e5dSTetsuo Handa 			return -ENOMEM;
6872a086e5dSTetsuo Handa 		spin_lock(&lock);
6882a086e5dSTetsuo Handa 		old_comment = profile->comment;
6892a086e5dSTetsuo Handa 		profile->comment = new_comment;
6902a086e5dSTetsuo Handa 		spin_unlock(&lock);
691bf24fb01STetsuo Handa 		tomoyo_put_name(old_comment);
6929590837bSKentaro Takeda 		return 0;
6939590837bSKentaro Takeda 	}
694d5ca1725STetsuo Handa 	if (!strcmp(data, "PREFERENCE")) {
695d5ca1725STetsuo Handa 		for (i = 0; i < TOMOYO_MAX_PREF; i++)
696d5ca1725STetsuo Handa 			tomoyo_set_uint(&profile->pref[i], cp,
697d5ca1725STetsuo Handa 					tomoyo_pref_keywords[i]);
698d5ca1725STetsuo Handa 		return 0;
6999590837bSKentaro Takeda 	}
700d5ca1725STetsuo Handa 	return tomoyo_set_mode(data, cp, profile);
701f23571e8STetsuo Handa }
702f23571e8STetsuo Handa 
703eadd99ccSTetsuo Handa /**
704eadd99ccSTetsuo Handa  * tomoyo_print_config - Print mode for specified functionality.
705eadd99ccSTetsuo Handa  *
706eadd99ccSTetsuo Handa  * @head:   Pointer to "struct tomoyo_io_buffer".
707eadd99ccSTetsuo Handa  * @config: Mode for that functionality.
708eadd99ccSTetsuo Handa  *
709eadd99ccSTetsuo Handa  * Returns nothing.
710eadd99ccSTetsuo Handa  *
711eadd99ccSTetsuo Handa  * Caller prints functionality's name.
712eadd99ccSTetsuo Handa  */
713f23571e8STetsuo Handa static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
714f23571e8STetsuo Handa {
715eadd99ccSTetsuo Handa 	tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
716eadd99ccSTetsuo Handa 			 tomoyo_mode[config & 3],
717eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
718eadd99ccSTetsuo Handa 			 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
719f23571e8STetsuo Handa }
720f23571e8STetsuo Handa 
7219590837bSKentaro Takeda /**
72257c2590fSTetsuo Handa  * tomoyo_read_profile - Read profile table.
7239590837bSKentaro Takeda  *
7249590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
725eadd99ccSTetsuo Handa  *
726eadd99ccSTetsuo Handa  * Returns nothing.
7279590837bSKentaro Takeda  */
7288fbe71f0STetsuo Handa static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
7299590837bSKentaro Takeda {
730f23571e8STetsuo Handa 	u8 index;
731bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
732bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
733f23571e8STetsuo Handa 	const struct tomoyo_profile *profile;
734bd03a3e4STetsuo Handa 	if (head->r.eof)
735bd03a3e4STetsuo Handa 		return;
736f23571e8STetsuo Handa  next:
737f23571e8STetsuo Handa 	index = head->r.index;
738bd03a3e4STetsuo Handa 	profile = ns->profile_ptr[index];
739f23571e8STetsuo Handa 	switch (head->r.step) {
740f23571e8STetsuo Handa 	case 0:
741bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
742bd03a3e4STetsuo Handa 		tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
743bd03a3e4STetsuo Handa 				 ns->profile_version);
744f23571e8STetsuo Handa 		head->r.step++;
745f23571e8STetsuo Handa 		break;
746f23571e8STetsuo Handa 	case 1:
747f23571e8STetsuo Handa 		for ( ; head->r.index < TOMOYO_MAX_PROFILES;
748f23571e8STetsuo Handa 		      head->r.index++)
749bd03a3e4STetsuo Handa 			if (ns->profile_ptr[head->r.index])
750f23571e8STetsuo Handa 				break;
751f23571e8STetsuo Handa 		if (head->r.index == TOMOYO_MAX_PROFILES)
752f23571e8STetsuo Handa 			return;
753f23571e8STetsuo Handa 		head->r.step++;
754f23571e8STetsuo Handa 		break;
755f23571e8STetsuo Handa 	case 2:
756f23571e8STetsuo Handa 		{
757d5ca1725STetsuo Handa 			u8 i;
758f23571e8STetsuo Handa 			const struct tomoyo_path_info *comment =
759f23571e8STetsuo Handa 				profile->comment;
760bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
761f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-COMMENT=", index);
762f23571e8STetsuo Handa 			tomoyo_set_string(head, comment ? comment->name : "");
763f23571e8STetsuo Handa 			tomoyo_set_lf(head);
764d5ca1725STetsuo Handa 			tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
765d5ca1725STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_PREF; i++)
766d5ca1725STetsuo Handa 				tomoyo_io_printf(head, "%s=%u ",
767d5ca1725STetsuo Handa 						 tomoyo_pref_keywords[i],
768d5ca1725STetsuo Handa 						 profile->pref[i]);
769d5ca1725STetsuo Handa 			tomoyo_set_string(head, "}\n");
770f23571e8STetsuo Handa 			head->r.step++;
771f23571e8STetsuo Handa 		}
772f23571e8STetsuo Handa 		break;
773f23571e8STetsuo Handa 	case 3:
774f23571e8STetsuo Handa 		{
775bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
776f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
777f23571e8STetsuo Handa 			tomoyo_print_config(head, profile->default_config);
778f23571e8STetsuo Handa 			head->r.bit = 0;
779f23571e8STetsuo Handa 			head->r.step++;
780f23571e8STetsuo Handa 		}
781f23571e8STetsuo Handa 		break;
782f23571e8STetsuo Handa 	case 4:
783f23571e8STetsuo Handa 		for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
784f23571e8STetsuo Handa 			      + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
785f23571e8STetsuo Handa 			const u8 i = head->r.bit;
786f23571e8STetsuo Handa 			const u8 config = profile->config[i];
78757c2590fSTetsuo Handa 			if (config == TOMOYO_CONFIG_USE_DEFAULT)
7889590837bSKentaro Takeda 				continue;
789bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
7902c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX)
7912c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
7922c47ab93STetsuo Handa 						 index,
7932c47ab93STetsuo Handa 						 tomoyo_category_keywords
7942c47ab93STetsuo Handa 						 [tomoyo_index2category[i]],
7952c47ab93STetsuo Handa 						 tomoyo_mac_keywords[i]);
7962c47ab93STetsuo Handa 			else
7972c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s", index,
798f23571e8STetsuo Handa 						 tomoyo_mac_keywords[i]);
799f23571e8STetsuo Handa 			tomoyo_print_config(head, config);
800f23571e8STetsuo Handa 			head->r.bit++;
8019590837bSKentaro Takeda 			break;
8029590837bSKentaro Takeda 		}
803f23571e8STetsuo Handa 		if (head->r.bit == TOMOYO_MAX_MAC_INDEX
804f23571e8STetsuo Handa 		    + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
805f23571e8STetsuo Handa 			head->r.index++;
806f23571e8STetsuo Handa 			head->r.step = 1;
807f23571e8STetsuo Handa 		}
808f23571e8STetsuo Handa 		break;
809f23571e8STetsuo Handa 	}
810f23571e8STetsuo Handa 	if (tomoyo_flush(head))
811f23571e8STetsuo Handa 		goto next;
8129590837bSKentaro Takeda }
8139590837bSKentaro Takeda 
8140f2a55d5STetsuo Handa /**
8150f2a55d5STetsuo Handa  * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
8160f2a55d5STetsuo Handa  *
8170f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
8180f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
8190f2a55d5STetsuo Handa  *
8200f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
8210f2a55d5STetsuo Handa  */
822e2bf6907STetsuo Handa static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
82336f5e1ffSTetsuo Handa 				const struct tomoyo_acl_head *b)
82436f5e1ffSTetsuo Handa {
825e2bf6907STetsuo Handa 	return container_of(a, struct tomoyo_manager, head)->manager ==
826e2bf6907STetsuo Handa 		container_of(b, struct tomoyo_manager, head)->manager;
82736f5e1ffSTetsuo Handa }
82836f5e1ffSTetsuo Handa 
8299590837bSKentaro Takeda /**
8309590837bSKentaro Takeda  * tomoyo_update_manager_entry - Add a manager entry.
8319590837bSKentaro Takeda  *
8329590837bSKentaro Takeda  * @manager:   The path to manager or the domainnamme.
8339590837bSKentaro Takeda  * @is_delete: True if it is a delete request.
8349590837bSKentaro Takeda  *
8359590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
836fdb8ebb7STetsuo Handa  *
837fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8389590837bSKentaro Takeda  */
8399590837bSKentaro Takeda static int tomoyo_update_manager_entry(const char *manager,
8409590837bSKentaro Takeda 				       const bool is_delete)
8419590837bSKentaro Takeda {
842e2bf6907STetsuo Handa 	struct tomoyo_manager e = { };
843a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
844bd03a3e4STetsuo Handa 		/* .ns = &tomoyo_kernel_namespace, */
845a238cf5bSTetsuo Handa 		.is_delete = is_delete,
846bd03a3e4STetsuo Handa 		.list = &tomoyo_kernel_namespace.
847bd03a3e4STetsuo Handa 		policy_list[TOMOYO_ID_MANAGER],
848a238cf5bSTetsuo Handa 	};
849a238cf5bSTetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
85075093152STetsuo Handa 	if (tomoyo_domain_def(manager)) {
85175093152STetsuo Handa 		if (!tomoyo_correct_domain(manager))
8529590837bSKentaro Takeda 			return -EINVAL;
8539e4b50e9STetsuo Handa 		e.is_domain = true;
8549590837bSKentaro Takeda 	} else {
85575093152STetsuo Handa 		if (!tomoyo_correct_path(manager))
8569590837bSKentaro Takeda 			return -EINVAL;
8579590837bSKentaro Takeda 	}
8589e4b50e9STetsuo Handa 	e.manager = tomoyo_get_name(manager);
859a238cf5bSTetsuo Handa 	if (e.manager) {
860a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), &param,
861e2bf6907STetsuo Handa 					     tomoyo_same_manager);
8629e4b50e9STetsuo Handa 		tomoyo_put_name(e.manager);
863a238cf5bSTetsuo Handa 	}
8649590837bSKentaro Takeda 	return error;
8659590837bSKentaro Takeda }
8669590837bSKentaro Takeda 
8679590837bSKentaro Takeda /**
868e2bf6907STetsuo Handa  * tomoyo_write_manager - Write manager policy.
8699590837bSKentaro Takeda  *
8709590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8719590837bSKentaro Takeda  *
8729590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
873fdb8ebb7STetsuo Handa  *
874fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8759590837bSKentaro Takeda  */
876e2bf6907STetsuo Handa static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
8779590837bSKentaro Takeda {
8789590837bSKentaro Takeda 	char *data = head->write_buf;
8799590837bSKentaro Takeda 
8809590837bSKentaro Takeda 	if (!strcmp(data, "manage_by_non_root")) {
881bd03a3e4STetsuo Handa 		tomoyo_manage_by_non_root = !head->w.is_delete;
8829590837bSKentaro Takeda 		return 0;
8839590837bSKentaro Takeda 	}
884bd03a3e4STetsuo Handa 	return tomoyo_update_manager_entry(data, head->w.is_delete);
8859590837bSKentaro Takeda }
8869590837bSKentaro Takeda 
8879590837bSKentaro Takeda /**
888e2bf6907STetsuo Handa  * tomoyo_read_manager - Read manager policy.
8899590837bSKentaro Takeda  *
8909590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8919590837bSKentaro Takeda  *
892fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8939590837bSKentaro Takeda  */
894e2bf6907STetsuo Handa static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
8959590837bSKentaro Takeda {
896f23571e8STetsuo Handa 	if (head->r.eof)
8978fbe71f0STetsuo Handa 		return;
898bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
899bd03a3e4STetsuo Handa 			     policy_list[TOMOYO_ID_MANAGER]) {
900e2bf6907STetsuo Handa 		struct tomoyo_manager *ptr =
901f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), head.list);
90282e0f001STetsuo Handa 		if (ptr->head.is_deleted)
9039590837bSKentaro Takeda 			continue;
904f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
905f23571e8STetsuo Handa 			return;
906f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->manager->name);
907f23571e8STetsuo Handa 		tomoyo_set_lf(head);
9089590837bSKentaro Takeda 	}
909f23571e8STetsuo Handa 	head->r.eof = true;
9109590837bSKentaro Takeda }
9119590837bSKentaro Takeda 
9129590837bSKentaro Takeda /**
913e2bf6907STetsuo Handa  * tomoyo_manager - Check whether the current process is a policy manager.
9149590837bSKentaro Takeda  *
9159590837bSKentaro Takeda  * Returns true if the current process is permitted to modify policy
9169590837bSKentaro Takeda  * via /sys/kernel/security/tomoyo/ interface.
917fdb8ebb7STetsuo Handa  *
918fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9199590837bSKentaro Takeda  */
920e2bf6907STetsuo Handa static bool tomoyo_manager(void)
9219590837bSKentaro Takeda {
922e2bf6907STetsuo Handa 	struct tomoyo_manager *ptr;
9239590837bSKentaro Takeda 	const char *exe;
9249590837bSKentaro Takeda 	const struct task_struct *task = current;
9259590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
9269590837bSKentaro Takeda 	bool found = false;
9279590837bSKentaro Takeda 
9289590837bSKentaro Takeda 	if (!tomoyo_policy_loaded)
9299590837bSKentaro Takeda 		return true;
9309590837bSKentaro Takeda 	if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
9319590837bSKentaro Takeda 		return false;
932bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
933bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
93482e0f001STetsuo Handa 		if (!ptr->head.is_deleted && ptr->is_domain
9359590837bSKentaro Takeda 		    && !tomoyo_pathcmp(domainname, ptr->manager)) {
9369590837bSKentaro Takeda 			found = true;
9379590837bSKentaro Takeda 			break;
9389590837bSKentaro Takeda 		}
9399590837bSKentaro Takeda 	}
9409590837bSKentaro Takeda 	if (found)
9419590837bSKentaro Takeda 		return true;
9429590837bSKentaro Takeda 	exe = tomoyo_get_exe();
9439590837bSKentaro Takeda 	if (!exe)
9449590837bSKentaro Takeda 		return false;
945bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
946bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
94782e0f001STetsuo Handa 		if (!ptr->head.is_deleted && !ptr->is_domain
9489590837bSKentaro Takeda 		    && !strcmp(exe, ptr->manager->name)) {
9499590837bSKentaro Takeda 			found = true;
9509590837bSKentaro Takeda 			break;
9519590837bSKentaro Takeda 		}
9529590837bSKentaro Takeda 	}
9539590837bSKentaro Takeda 	if (!found) { /* Reduce error messages. */
9549590837bSKentaro Takeda 		static pid_t last_pid;
9559590837bSKentaro Takeda 		const pid_t pid = current->pid;
9569590837bSKentaro Takeda 		if (last_pid != pid) {
9579590837bSKentaro Takeda 			printk(KERN_WARNING "%s ( %s ) is not permitted to "
9589590837bSKentaro Takeda 			       "update policies.\n", domainname->name, exe);
9599590837bSKentaro Takeda 			last_pid = pid;
9609590837bSKentaro Takeda 		}
9619590837bSKentaro Takeda 	}
9628e2d39a1STetsuo Handa 	kfree(exe);
9639590837bSKentaro Takeda 	return found;
9649590837bSKentaro Takeda }
9659590837bSKentaro Takeda 
966*59df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
967*59df3166STetsuo Handa (unsigned int serial);
968*59df3166STetsuo Handa 
9699590837bSKentaro Takeda /**
970bd03a3e4STetsuo Handa  * tomoyo_select_domain - Parse select command.
9719590837bSKentaro Takeda  *
9729590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9739590837bSKentaro Takeda  * @data: String to parse.
9749590837bSKentaro Takeda  *
9759590837bSKentaro Takeda  * Returns true on success, false otherwise.
976fdb8ebb7STetsuo Handa  *
977fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9789590837bSKentaro Takeda  */
979bd03a3e4STetsuo Handa static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
980bd03a3e4STetsuo Handa 				 const char *data)
9819590837bSKentaro Takeda {
9829590837bSKentaro Takeda 	unsigned int pid;
9839590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
9849b244373STetsuo Handa 	bool global_pid = false;
985bd03a3e4STetsuo Handa 	if (strncmp(data, "select ", 7))
986bd03a3e4STetsuo Handa 		return false;
987bd03a3e4STetsuo Handa 	data += 7;
9889b244373STetsuo Handa 	if (sscanf(data, "pid=%u", &pid) == 1 ||
9899b244373STetsuo Handa 	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9909590837bSKentaro Takeda 		struct task_struct *p;
9911fcdc7c5STetsuo Handa 		rcu_read_lock();
9929b244373STetsuo Handa 		if (global_pid)
9939b244373STetsuo Handa 			p = find_task_by_pid_ns(pid, &init_pid_ns);
9949b244373STetsuo Handa 		else
9959590837bSKentaro Takeda 			p = find_task_by_vpid(pid);
9969590837bSKentaro Takeda 		if (p)
9979590837bSKentaro Takeda 			domain = tomoyo_real_domain(p);
9981fcdc7c5STetsuo Handa 		rcu_read_unlock();
9999590837bSKentaro Takeda 	} else if (!strncmp(data, "domain=", 7)) {
100075093152STetsuo Handa 		if (tomoyo_domain_def(data + 7))
10019590837bSKentaro Takeda 			domain = tomoyo_find_domain(data + 7);
1002*59df3166STetsuo Handa 	} else if (sscanf(data, "Q=%u", &pid) == 1) {
1003*59df3166STetsuo Handa 		domain = tomoyo_find_domain_by_qid(pid);
10049590837bSKentaro Takeda 	} else
10059590837bSKentaro Takeda 		return false;
10060df7e8b8STetsuo Handa 	head->w.domain = domain;
10079590837bSKentaro Takeda 	/* Accessing read_buf is safe because head->io_sem is held. */
10089590837bSKentaro Takeda 	if (!head->read_buf)
10099590837bSKentaro Takeda 		return true; /* Do nothing if open(O_WRONLY). */
1010f23571e8STetsuo Handa 	memset(&head->r, 0, sizeof(head->r));
1011f23571e8STetsuo Handa 	head->r.print_this_domain_only = true;
101268eda8f5SDan Carpenter 	if (domain)
1013f23571e8STetsuo Handa 		head->r.domain = &domain->list;
101468eda8f5SDan Carpenter 	else
101568eda8f5SDan Carpenter 		head->r.eof = 1;
10169590837bSKentaro Takeda 	tomoyo_io_printf(head, "# select %s\n", data);
1017475e6fa3STetsuo Handa 	if (domain && domain->is_deleted)
10189590837bSKentaro Takeda 		tomoyo_io_printf(head, "# This is a deleted domain.\n");
10199590837bSKentaro Takeda 	return true;
10209590837bSKentaro Takeda }
10219590837bSKentaro Takeda 
10229590837bSKentaro Takeda /**
1023731d37aaSTetsuo Handa  * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1024731d37aaSTetsuo Handa  *
1025731d37aaSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_info".
1026731d37aaSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_info".
1027731d37aaSTetsuo Handa  *
1028731d37aaSTetsuo Handa  * Returns true if @a == @b, false otherwise.
1029731d37aaSTetsuo Handa  */
1030731d37aaSTetsuo Handa static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1031731d37aaSTetsuo Handa 			      const struct tomoyo_acl_info *b)
1032731d37aaSTetsuo Handa {
1033731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1034731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1035731d37aaSTetsuo Handa 	return p1->domainname == p2->domainname;
1036731d37aaSTetsuo Handa }
1037731d37aaSTetsuo Handa 
1038731d37aaSTetsuo Handa /**
1039731d37aaSTetsuo Handa  * tomoyo_write_task - Update task related list.
1040731d37aaSTetsuo Handa  *
1041731d37aaSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
1042731d37aaSTetsuo Handa  *
1043731d37aaSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1044731d37aaSTetsuo Handa  *
1045731d37aaSTetsuo Handa  * Caller holds tomoyo_read_lock().
1046731d37aaSTetsuo Handa  */
1047731d37aaSTetsuo Handa static int tomoyo_write_task(struct tomoyo_acl_param *param)
1048731d37aaSTetsuo Handa {
1049731d37aaSTetsuo Handa 	int error = -EINVAL;
1050731d37aaSTetsuo Handa 	if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1051731d37aaSTetsuo Handa 		struct tomoyo_task_acl e = {
1052731d37aaSTetsuo Handa 			.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1053731d37aaSTetsuo Handa 			.domainname = tomoyo_get_domainname(param),
1054731d37aaSTetsuo Handa 		};
1055731d37aaSTetsuo Handa 		if (e.domainname)
1056731d37aaSTetsuo Handa 			error = tomoyo_update_domain(&e.head, sizeof(e), param,
1057731d37aaSTetsuo Handa 						     tomoyo_same_task_acl,
1058731d37aaSTetsuo Handa 						     NULL);
1059731d37aaSTetsuo Handa 		tomoyo_put_name(e.domainname);
1060731d37aaSTetsuo Handa 	}
1061731d37aaSTetsuo Handa 	return error;
1062731d37aaSTetsuo Handa }
1063731d37aaSTetsuo Handa 
1064731d37aaSTetsuo Handa /**
1065ccf135f5STetsuo Handa  * tomoyo_delete_domain - Delete a domain.
1066ccf135f5STetsuo Handa  *
1067ccf135f5STetsuo Handa  * @domainname: The name of domain.
1068ccf135f5STetsuo Handa  *
1069ccf135f5STetsuo Handa  * Returns 0.
1070fdb8ebb7STetsuo Handa  *
1071fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1072ccf135f5STetsuo Handa  */
1073ccf135f5STetsuo Handa static int tomoyo_delete_domain(char *domainname)
1074ccf135f5STetsuo Handa {
1075ccf135f5STetsuo Handa 	struct tomoyo_domain_info *domain;
1076ccf135f5STetsuo Handa 	struct tomoyo_path_info name;
1077ccf135f5STetsuo Handa 
1078ccf135f5STetsuo Handa 	name.name = domainname;
1079ccf135f5STetsuo Handa 	tomoyo_fill_path_info(&name);
108029282381STetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
108129282381STetsuo Handa 		return 0;
1082ccf135f5STetsuo Handa 	/* Is there an active domain? */
1083fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1084ccf135f5STetsuo Handa 		/* Never delete tomoyo_kernel_domain */
1085ccf135f5STetsuo Handa 		if (domain == &tomoyo_kernel_domain)
1086ccf135f5STetsuo Handa 			continue;
1087ccf135f5STetsuo Handa 		if (domain->is_deleted ||
1088ccf135f5STetsuo Handa 		    tomoyo_pathcmp(domain->domainname, &name))
1089ccf135f5STetsuo Handa 			continue;
1090ccf135f5STetsuo Handa 		domain->is_deleted = true;
1091ccf135f5STetsuo Handa 		break;
1092ccf135f5STetsuo Handa 	}
1093f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
1094ccf135f5STetsuo Handa 	return 0;
1095ccf135f5STetsuo Handa }
1096ccf135f5STetsuo Handa 
1097ccf135f5STetsuo Handa /**
1098e2bf6907STetsuo Handa  * tomoyo_write_domain2 - Write domain policy.
109917fcfbd9STetsuo Handa  *
1100bd03a3e4STetsuo Handa  * @ns:        Pointer to "struct tomoyo_policy_namespace".
1101a238cf5bSTetsuo Handa  * @list:      Pointer to "struct list_head".
1102a238cf5bSTetsuo Handa  * @data:      Policy to be interpreted.
1103a238cf5bSTetsuo Handa  * @is_delete: True if it is a delete request.
110417fcfbd9STetsuo Handa  *
110517fcfbd9STetsuo Handa  * Returns 0 on success, negative value otherwise.
110617fcfbd9STetsuo Handa  *
110717fcfbd9STetsuo Handa  * Caller holds tomoyo_read_lock().
110817fcfbd9STetsuo Handa  */
1109bd03a3e4STetsuo Handa static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1110bd03a3e4STetsuo Handa 				struct list_head *list, char *data,
111117fcfbd9STetsuo Handa 				const bool is_delete)
111217fcfbd9STetsuo Handa {
1113a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1114bd03a3e4STetsuo Handa 		.ns = ns,
1115a238cf5bSTetsuo Handa 		.list = list,
1116a238cf5bSTetsuo Handa 		.data = data,
1117a238cf5bSTetsuo Handa 		.is_delete = is_delete,
1118a238cf5bSTetsuo Handa 	};
1119a238cf5bSTetsuo Handa 	static const struct {
1120a238cf5bSTetsuo Handa 		const char *keyword;
1121a238cf5bSTetsuo Handa 		int (*write) (struct tomoyo_acl_param *);
1122731d37aaSTetsuo Handa 	} tomoyo_callback[5] = {
1123a238cf5bSTetsuo Handa 		{ "file ", tomoyo_write_file },
1124059d84dbSTetsuo Handa 		{ "network inet ", tomoyo_write_inet_network },
1125059d84dbSTetsuo Handa 		{ "network unix ", tomoyo_write_unix_network },
1126d58e0da8STetsuo Handa 		{ "misc ", tomoyo_write_misc },
1127731d37aaSTetsuo Handa 		{ "task ", tomoyo_write_task },
1128a238cf5bSTetsuo Handa 	};
1129a238cf5bSTetsuo Handa 	u8 i;
1130d58e0da8STetsuo Handa 
1131d58e0da8STetsuo Handa 	for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
1132a238cf5bSTetsuo Handa 		if (!tomoyo_str_starts(&param.data,
1133a238cf5bSTetsuo Handa 				       tomoyo_callback[i].keyword))
1134a238cf5bSTetsuo Handa 			continue;
1135a238cf5bSTetsuo Handa 		return tomoyo_callback[i].write(&param);
1136a238cf5bSTetsuo Handa 	}
1137a238cf5bSTetsuo Handa 	return -EINVAL;
113817fcfbd9STetsuo Handa }
113917fcfbd9STetsuo Handa 
11402c47ab93STetsuo Handa /* String table for domain flags. */
11412c47ab93STetsuo Handa const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
11422c47ab93STetsuo Handa 	[TOMOYO_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
11432c47ab93STetsuo Handa 	[TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
11442c47ab93STetsuo Handa };
11452c47ab93STetsuo Handa 
114617fcfbd9STetsuo Handa /**
1147e2bf6907STetsuo Handa  * tomoyo_write_domain - Write domain policy.
11489590837bSKentaro Takeda  *
11499590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
11509590837bSKentaro Takeda  *
11519590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1152fdb8ebb7STetsuo Handa  *
1153fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
11549590837bSKentaro Takeda  */
1155e2bf6907STetsuo Handa static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
11569590837bSKentaro Takeda {
11579590837bSKentaro Takeda 	char *data = head->write_buf;
1158bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
11590df7e8b8STetsuo Handa 	struct tomoyo_domain_info *domain = head->w.domain;
1160bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1161bd03a3e4STetsuo Handa 	bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
11629590837bSKentaro Takeda 	unsigned int profile;
1163bd03a3e4STetsuo Handa 	if (*data == '<') {
11649590837bSKentaro Takeda 		domain = NULL;
11659590837bSKentaro Takeda 		if (is_delete)
11669590837bSKentaro Takeda 			tomoyo_delete_domain(data);
1167fdb8ebb7STetsuo Handa 		else if (is_select)
11689590837bSKentaro Takeda 			domain = tomoyo_find_domain(data);
1169fdb8ebb7STetsuo Handa 		else
1170bd03a3e4STetsuo Handa 			domain = tomoyo_assign_domain(data, false);
11710df7e8b8STetsuo Handa 		head->w.domain = domain;
11729590837bSKentaro Takeda 		return 0;
11739590837bSKentaro Takeda 	}
11749590837bSKentaro Takeda 	if (!domain)
11759590837bSKentaro Takeda 		return -EINVAL;
1176bd03a3e4STetsuo Handa 	ns = domain->ns;
1177b5bc60b4STetsuo Handa 	if (sscanf(data, "use_profile %u", &profile) == 1
11789590837bSKentaro Takeda 	    && profile < TOMOYO_MAX_PROFILES) {
1179bd03a3e4STetsuo Handa 		if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
11809590837bSKentaro Takeda 			domain->profile = (u8) profile;
11819590837bSKentaro Takeda 		return 0;
11829590837bSKentaro Takeda 	}
118332997144STetsuo Handa 	if (sscanf(data, "use_group %u\n", &profile) == 1
118432997144STetsuo Handa 	    && profile < TOMOYO_MAX_ACL_GROUPS) {
118532997144STetsuo Handa 		if (!is_delete)
118632997144STetsuo Handa 			domain->group = (u8) profile;
118732997144STetsuo Handa 		return 0;
118832997144STetsuo Handa 	}
11892c47ab93STetsuo Handa 	for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
11902c47ab93STetsuo Handa 		const char *cp = tomoyo_dif[profile];
11912c47ab93STetsuo Handa 		if (strncmp(data, cp, strlen(cp) - 1))
11922c47ab93STetsuo Handa 			continue;
11932c47ab93STetsuo Handa 		domain->flags[profile] = !is_delete;
11949b244373STetsuo Handa 		return 0;
11959b244373STetsuo Handa 	}
1196bd03a3e4STetsuo Handa 	return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1197bd03a3e4STetsuo Handa 				    is_delete);
11989590837bSKentaro Takeda }
11999590837bSKentaro Takeda 
12009590837bSKentaro Takeda /**
12012066a361STetsuo Handa  * tomoyo_print_condition - Print condition part.
12022066a361STetsuo Handa  *
12032066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
12042066a361STetsuo Handa  * @cond: Pointer to "struct tomoyo_condition".
12052066a361STetsuo Handa  *
12062066a361STetsuo Handa  * Returns true on success, false otherwise.
12072066a361STetsuo Handa  */
12082066a361STetsuo Handa static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
12092066a361STetsuo Handa 				   const struct tomoyo_condition *cond)
12102066a361STetsuo Handa {
12112066a361STetsuo Handa 	switch (head->r.cond_step) {
12122066a361STetsuo Handa 	case 0:
12132066a361STetsuo Handa 		head->r.cond_index = 0;
12142066a361STetsuo Handa 		head->r.cond_step++;
12156bce98edSTetsuo Handa 		if (cond->transit) {
12166bce98edSTetsuo Handa 			tomoyo_set_space(head);
12176bce98edSTetsuo Handa 			tomoyo_set_string(head, cond->transit->name);
12186bce98edSTetsuo Handa 		}
12192066a361STetsuo Handa 		/* fall through */
12202066a361STetsuo Handa 	case 1:
12212066a361STetsuo Handa 		{
12222066a361STetsuo Handa 			const u16 condc = cond->condc;
12232066a361STetsuo Handa 			const struct tomoyo_condition_element *condp =
12242066a361STetsuo Handa 				(typeof(condp)) (cond + 1);
12252066a361STetsuo Handa 			const struct tomoyo_number_union *numbers_p =
12262066a361STetsuo Handa 				(typeof(numbers_p)) (condp + condc);
12272ca9bf45STetsuo Handa 			const struct tomoyo_name_union *names_p =
12282ca9bf45STetsuo Handa 				(typeof(names_p))
12292ca9bf45STetsuo Handa 				(numbers_p + cond->numbers_count);
12305b636857STetsuo Handa 			const struct tomoyo_argv *argv =
12315b636857STetsuo Handa 				(typeof(argv)) (names_p + cond->names_count);
12325b636857STetsuo Handa 			const struct tomoyo_envp *envp =
12335b636857STetsuo Handa 				(typeof(envp)) (argv + cond->argc);
12342066a361STetsuo Handa 			u16 skip;
12352066a361STetsuo Handa 			for (skip = 0; skip < head->r.cond_index; skip++) {
12362066a361STetsuo Handa 				const u8 left = condp->left;
12372066a361STetsuo Handa 				const u8 right = condp->right;
12382066a361STetsuo Handa 				condp++;
12392066a361STetsuo Handa 				switch (left) {
12405b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12415b636857STetsuo Handa 					argv++;
12425b636857STetsuo Handa 					continue;
12435b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12445b636857STetsuo Handa 					envp++;
12455b636857STetsuo Handa 					continue;
12462066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12472066a361STetsuo Handa 					numbers_p++;
12482066a361STetsuo Handa 					break;
12492066a361STetsuo Handa 				}
12502066a361STetsuo Handa 				switch (right) {
12512ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
12522ca9bf45STetsuo Handa 					names_p++;
12532ca9bf45STetsuo Handa 					break;
12542066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12552066a361STetsuo Handa 					numbers_p++;
12562066a361STetsuo Handa 					break;
12572066a361STetsuo Handa 				}
12582066a361STetsuo Handa 			}
12592066a361STetsuo Handa 			while (head->r.cond_index < condc) {
12602066a361STetsuo Handa 				const u8 match = condp->equals;
12612066a361STetsuo Handa 				const u8 left = condp->left;
12622066a361STetsuo Handa 				const u8 right = condp->right;
12632066a361STetsuo Handa 				if (!tomoyo_flush(head))
12642066a361STetsuo Handa 					return false;
12652066a361STetsuo Handa 				condp++;
12662066a361STetsuo Handa 				head->r.cond_index++;
12672066a361STetsuo Handa 				tomoyo_set_space(head);
12682066a361STetsuo Handa 				switch (left) {
12695b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12705b636857STetsuo Handa 					tomoyo_io_printf(head,
12715b636857STetsuo Handa 							 "exec.argv[%lu]%s=\"",
12725b636857STetsuo Handa 							 argv->index, argv->
12735b636857STetsuo Handa 							 is_not ? "!" : "");
12745b636857STetsuo Handa 					tomoyo_set_string(head,
12755b636857STetsuo Handa 							  argv->value->name);
12765b636857STetsuo Handa 					tomoyo_set_string(head, "\"");
12775b636857STetsuo Handa 					argv++;
12785b636857STetsuo Handa 					continue;
12795b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12805b636857STetsuo Handa 					tomoyo_set_string(head,
12815b636857STetsuo Handa 							  "exec.envp[\"");
12825b636857STetsuo Handa 					tomoyo_set_string(head,
12835b636857STetsuo Handa 							  envp->name->name);
12845b636857STetsuo Handa 					tomoyo_io_printf(head, "\"]%s=", envp->
12855b636857STetsuo Handa 							 is_not ? "!" : "");
12865b636857STetsuo Handa 					if (envp->value) {
12875b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12885b636857STetsuo Handa 						tomoyo_set_string(head, envp->
12895b636857STetsuo Handa 								  value->name);
12905b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12915b636857STetsuo Handa 					} else {
12925b636857STetsuo Handa 						tomoyo_set_string(head,
12935b636857STetsuo Handa 								  "NULL");
12945b636857STetsuo Handa 					}
12955b636857STetsuo Handa 					envp++;
12965b636857STetsuo Handa 					continue;
12972066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12982066a361STetsuo Handa 					tomoyo_print_number_union_nospace
12992066a361STetsuo Handa 						(head, numbers_p++);
13002066a361STetsuo Handa 					break;
13012066a361STetsuo Handa 				default:
13022066a361STetsuo Handa 					tomoyo_set_string(head,
13032066a361STetsuo Handa 					       tomoyo_condition_keyword[left]);
13042066a361STetsuo Handa 					break;
13052066a361STetsuo Handa 				}
13062066a361STetsuo Handa 				tomoyo_set_string(head, match ? "=" : "!=");
13072066a361STetsuo Handa 				switch (right) {
13082ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
13092ca9bf45STetsuo Handa 					tomoyo_print_name_union_quoted
13102ca9bf45STetsuo Handa 						(head, names_p++);
13112ca9bf45STetsuo Handa 					break;
13122066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13132066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13142066a361STetsuo Handa 						(head, numbers_p++);
13152066a361STetsuo Handa 					break;
13162066a361STetsuo Handa 				default:
13172066a361STetsuo Handa 					tomoyo_set_string(head,
13182066a361STetsuo Handa 					  tomoyo_condition_keyword[right]);
13192066a361STetsuo Handa 					break;
13202066a361STetsuo Handa 				}
13212066a361STetsuo Handa 			}
13222066a361STetsuo Handa 		}
13232066a361STetsuo Handa 		head->r.cond_step++;
13242066a361STetsuo Handa 		/* fall through */
13252066a361STetsuo Handa 	case 2:
13262066a361STetsuo Handa 		if (!tomoyo_flush(head))
13272066a361STetsuo Handa 			break;
13282066a361STetsuo Handa 		head->r.cond_step++;
13292066a361STetsuo Handa 		/* fall through */
13302066a361STetsuo Handa 	case 3:
13311f067a68STetsuo Handa 		if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13321f067a68STetsuo Handa 			tomoyo_io_printf(head, " grant_log=%s",
13331f067a68STetsuo Handa 					 tomoyo_yesno(cond->grant_log ==
13341f067a68STetsuo Handa 						      TOMOYO_GRANTLOG_YES));
13352066a361STetsuo Handa 		tomoyo_set_lf(head);
13362066a361STetsuo Handa 		return true;
13372066a361STetsuo Handa 	}
13382066a361STetsuo Handa 	return false;
13392066a361STetsuo Handa }
13402066a361STetsuo Handa 
13412066a361STetsuo Handa /**
134232997144STetsuo Handa  * tomoyo_set_group - Print "acl_group " header keyword and category name.
13439590837bSKentaro Takeda  *
13440d2171d7STetsuo Handa  * @head:     Pointer to "struct tomoyo_io_buffer".
13450d2171d7STetsuo Handa  * @category: Category name.
13469590837bSKentaro Takeda  *
13470d2171d7STetsuo Handa  * Returns nothing.
13489590837bSKentaro Takeda  */
13490d2171d7STetsuo Handa static void tomoyo_set_group(struct tomoyo_io_buffer *head,
13500d2171d7STetsuo Handa 			     const char *category)
13519590837bSKentaro Takeda {
1352bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1353bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
135432997144STetsuo Handa 		tomoyo_io_printf(head, "acl_group %u ",
135532997144STetsuo Handa 				 head->r.acl_group_index);
1356bd03a3e4STetsuo Handa 	}
13570d2171d7STetsuo Handa 	tomoyo_set_string(head, category);
13582106ccd9STetsuo Handa }
13592106ccd9STetsuo Handa 
13602106ccd9STetsuo Handa /**
13619590837bSKentaro Takeda  * tomoyo_print_entry - Print an ACL entry.
13629590837bSKentaro Takeda  *
13639590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
13645db5a39bSTetsuo Handa  * @acl:  Pointer to an ACL entry.
13659590837bSKentaro Takeda  *
13669590837bSKentaro Takeda  * Returns true on success, false otherwise.
13679590837bSKentaro Takeda  */
13689590837bSKentaro Takeda static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13695db5a39bSTetsuo Handa 			       struct tomoyo_acl_info *acl)
13709590837bSKentaro Takeda {
13715db5a39bSTetsuo Handa 	const u8 acl_type = acl->type;
13720d2171d7STetsuo Handa 	bool first = true;
1373f23571e8STetsuo Handa 	u8 bit;
13749590837bSKentaro Takeda 
13752066a361STetsuo Handa 	if (head->r.print_cond_part)
13762066a361STetsuo Handa 		goto print_cond_part;
13775db5a39bSTetsuo Handa 	if (acl->is_deleted)
1378237ab459STetsuo Handa 		return true;
1379f23571e8STetsuo Handa 	if (!tomoyo_flush(head))
1380f23571e8STetsuo Handa 		return false;
1381f23571e8STetsuo Handa 	else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
13825db5a39bSTetsuo Handa 		struct tomoyo_path_acl *ptr =
13835db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
13845db5a39bSTetsuo Handa 		const u16 perm = ptr->perm;
13850d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13865db5a39bSTetsuo Handa 			if (!(perm & (1 << bit)))
13875db5a39bSTetsuo Handa 				continue;
1388bd03a3e4STetsuo Handa 			if (head->r.print_transition_related_only &&
13895db5a39bSTetsuo Handa 			    bit != TOMOYO_TYPE_EXECUTE)
13905db5a39bSTetsuo Handa 				continue;
13910d2171d7STetsuo Handa 			if (first) {
13920d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
13930d2171d7STetsuo Handa 				first = false;
13940d2171d7STetsuo Handa 			} else {
13950d2171d7STetsuo Handa 				tomoyo_set_slash(head);
13969590837bSKentaro Takeda 			}
13970d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_path_keyword[bit]);
13980d2171d7STetsuo Handa 		}
13990d2171d7STetsuo Handa 		if (first)
14000d2171d7STetsuo Handa 			return true;
1401f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1402731d37aaSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1403731d37aaSTetsuo Handa 		struct tomoyo_task_acl *ptr =
1404731d37aaSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1405731d37aaSTetsuo Handa 		tomoyo_set_group(head, "task ");
1406731d37aaSTetsuo Handa 		tomoyo_set_string(head, "manual_domain_transition ");
1407731d37aaSTetsuo Handa 		tomoyo_set_string(head, ptr->domainname->name);
1408bd03a3e4STetsuo Handa 	} else if (head->r.print_transition_related_only) {
1409063821c8STetsuo Handa 		return true;
14105db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
14115db5a39bSTetsuo Handa 		struct tomoyo_path2_acl *ptr =
14125db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14130d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14140d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14150d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14160d2171d7STetsuo Handa 				continue;
14170d2171d7STetsuo Handa 			if (first) {
14180d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14190d2171d7STetsuo Handa 				first = false;
14200d2171d7STetsuo Handa 			} else {
14210d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14220d2171d7STetsuo Handa 			}
14230d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14240d2171d7STetsuo Handa 					  [tomoyo_pp2mac[bit]]);
14250d2171d7STetsuo Handa 		}
14260d2171d7STetsuo Handa 		if (first)
14270d2171d7STetsuo Handa 			return true;
1428f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name1);
1429f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name2);
14305db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
14315db5a39bSTetsuo Handa 		struct tomoyo_path_number_acl *ptr =
14325db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14330d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14340d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14350d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14360d2171d7STetsuo Handa 				continue;
14370d2171d7STetsuo Handa 			if (first) {
14380d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14390d2171d7STetsuo Handa 				first = false;
14400d2171d7STetsuo Handa 			} else {
14410d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14420d2171d7STetsuo Handa 			}
14430d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14440d2171d7STetsuo Handa 					  [tomoyo_pn2mac[bit]]);
14450d2171d7STetsuo Handa 		}
14460d2171d7STetsuo Handa 		if (first)
14470d2171d7STetsuo Handa 			return true;
1448f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1449f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->number);
14505db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
14515db5a39bSTetsuo Handa 		struct tomoyo_mkdev_acl *ptr =
14525db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14530d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14540d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14550d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14560d2171d7STetsuo Handa 				continue;
14570d2171d7STetsuo Handa 			if (first) {
14580d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14590d2171d7STetsuo Handa 				first = false;
14600d2171d7STetsuo Handa 			} else {
14610d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14620d2171d7STetsuo Handa 			}
14630d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14640d2171d7STetsuo Handa 					  [tomoyo_pnnn2mac[bit]]);
14650d2171d7STetsuo Handa 		}
14660d2171d7STetsuo Handa 		if (first)
14670d2171d7STetsuo Handa 			return true;
1468f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1469f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->mode);
1470f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->major);
1471f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->minor);
1472059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1473059d84dbSTetsuo Handa 		struct tomoyo_inet_acl *ptr =
1474059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1475059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1476059d84dbSTetsuo Handa 
1477059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1478059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1479059d84dbSTetsuo Handa 				continue;
1480059d84dbSTetsuo Handa 			if (first) {
1481059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network inet ");
1482059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1483059d84dbSTetsuo Handa 						  [ptr->protocol]);
1484059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1485059d84dbSTetsuo Handa 				first = false;
1486059d84dbSTetsuo Handa 			} else {
1487059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1488059d84dbSTetsuo Handa 			}
1489059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1490059d84dbSTetsuo Handa 		}
1491059d84dbSTetsuo Handa 		if (first)
1492059d84dbSTetsuo Handa 			return true;
1493059d84dbSTetsuo Handa 		tomoyo_set_space(head);
1494059d84dbSTetsuo Handa 		if (ptr->address.group) {
1495059d84dbSTetsuo Handa 			tomoyo_set_string(head, "@");
1496059d84dbSTetsuo Handa 			tomoyo_set_string(head, ptr->address.group->group_name
1497059d84dbSTetsuo Handa 					  ->name);
1498059d84dbSTetsuo Handa 		} else {
1499059d84dbSTetsuo Handa 			char buf[128];
1500059d84dbSTetsuo Handa 			tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1501059d84dbSTetsuo Handa 			tomoyo_io_printf(head, "%s", buf);
1502059d84dbSTetsuo Handa 		}
1503059d84dbSTetsuo Handa 		tomoyo_print_number_union(head, &ptr->port);
1504059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1505059d84dbSTetsuo Handa 		struct tomoyo_unix_acl *ptr =
1506059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1507059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1508059d84dbSTetsuo Handa 
1509059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1510059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1511059d84dbSTetsuo Handa 				continue;
1512059d84dbSTetsuo Handa 			if (first) {
1513059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network unix ");
1514059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1515059d84dbSTetsuo Handa 						  [ptr->protocol]);
1516059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1517059d84dbSTetsuo Handa 				first = false;
1518059d84dbSTetsuo Handa 			} else {
1519059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1520059d84dbSTetsuo Handa 			}
1521059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1522059d84dbSTetsuo Handa 		}
1523059d84dbSTetsuo Handa 		if (first)
1524059d84dbSTetsuo Handa 			return true;
1525059d84dbSTetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
15265db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15275db5a39bSTetsuo Handa 		struct tomoyo_mount_acl *ptr =
15285db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
15290d2171d7STetsuo Handa 		tomoyo_set_group(head, "file mount");
1530f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dev_name);
1531f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dir_name);
1532f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->fs_type);
1533f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->flags);
1534d58e0da8STetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1535d58e0da8STetsuo Handa 		struct tomoyo_env_acl *ptr =
1536d58e0da8STetsuo Handa 			container_of(acl, typeof(*ptr), head);
1537d58e0da8STetsuo Handa 
1538d58e0da8STetsuo Handa 		tomoyo_set_group(head, "misc env ");
1539d58e0da8STetsuo Handa 		tomoyo_set_string(head, ptr->env->name);
15409590837bSKentaro Takeda 	}
15412066a361STetsuo Handa 	if (acl->cond) {
15422066a361STetsuo Handa 		head->r.print_cond_part = true;
15432066a361STetsuo Handa 		head->r.cond_step = 0;
15442066a361STetsuo Handa 		if (!tomoyo_flush(head))
15452066a361STetsuo Handa 			return false;
15462066a361STetsuo Handa print_cond_part:
15472066a361STetsuo Handa 		if (!tomoyo_print_condition(head, acl->cond))
15482066a361STetsuo Handa 			return false;
15492066a361STetsuo Handa 		head->r.print_cond_part = false;
15502066a361STetsuo Handa 	} else {
15510d2171d7STetsuo Handa 		tomoyo_set_lf(head);
15522066a361STetsuo Handa 	}
15535db5a39bSTetsuo Handa 	return true;
1554f23571e8STetsuo Handa }
1555f23571e8STetsuo Handa 
1556f23571e8STetsuo Handa /**
1557f23571e8STetsuo Handa  * tomoyo_read_domain2 - Read domain policy.
1558f23571e8STetsuo Handa  *
1559f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
156032997144STetsuo Handa  * @list: Pointer to "struct list_head".
1561f23571e8STetsuo Handa  *
1562f23571e8STetsuo Handa  * Caller holds tomoyo_read_lock().
1563f23571e8STetsuo Handa  *
1564f23571e8STetsuo Handa  * Returns true on success, false otherwise.
1565f23571e8STetsuo Handa  */
1566f23571e8STetsuo Handa static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
156732997144STetsuo Handa 				struct list_head *list)
1568f23571e8STetsuo Handa {
156932997144STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1570f23571e8STetsuo Handa 		struct tomoyo_acl_info *ptr =
1571f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), list);
1572f23571e8STetsuo Handa 		if (!tomoyo_print_entry(head, ptr))
15739590837bSKentaro Takeda 			return false;
15749590837bSKentaro Takeda 	}
1575f23571e8STetsuo Handa 	head->r.acl = NULL;
1576f23571e8STetsuo Handa 	return true;
1577f23571e8STetsuo Handa }
15789590837bSKentaro Takeda 
15799590837bSKentaro Takeda /**
1580e2bf6907STetsuo Handa  * tomoyo_read_domain - Read domain policy.
15819590837bSKentaro Takeda  *
15829590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
15839590837bSKentaro Takeda  *
1584fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
15859590837bSKentaro Takeda  */
1586e2bf6907STetsuo Handa static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
15879590837bSKentaro Takeda {
1588f23571e8STetsuo Handa 	if (head->r.eof)
15898fbe71f0STetsuo Handa 		return;
1590f23571e8STetsuo Handa 	list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1591475e6fa3STetsuo Handa 		struct tomoyo_domain_info *domain =
1592f23571e8STetsuo Handa 			list_entry(head->r.domain, typeof(*domain), list);
1593f23571e8STetsuo Handa 		switch (head->r.step) {
15942c47ab93STetsuo Handa 			u8 i;
1595f23571e8STetsuo Handa 		case 0:
1596f23571e8STetsuo Handa 			if (domain->is_deleted &&
1597f23571e8STetsuo Handa 			    !head->r.print_this_domain_only)
15989590837bSKentaro Takeda 				continue;
15999590837bSKentaro Takeda 			/* Print domainname and flags. */
1600f23571e8STetsuo Handa 			tomoyo_set_string(head, domain->domainname->name);
1601f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1602b5bc60b4STetsuo Handa 			tomoyo_io_printf(head, "use_profile %u\n",
1603f23571e8STetsuo Handa 					 domain->profile);
160432997144STetsuo Handa 			tomoyo_io_printf(head, "use_group %u\n",
160532997144STetsuo Handa 					 domain->group);
16062c47ab93STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
16072c47ab93STetsuo Handa 				if (domain->flags[i])
16082c47ab93STetsuo Handa 					tomoyo_set_string(head, tomoyo_dif[i]);
1609f23571e8STetsuo Handa 			head->r.step++;
1610f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1611f23571e8STetsuo Handa 			/* fall through */
1612f23571e8STetsuo Handa 		case 1:
161332997144STetsuo Handa 			if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1614f23571e8STetsuo Handa 				return;
1615f23571e8STetsuo Handa 			head->r.step++;
1616f23571e8STetsuo Handa 			if (!tomoyo_set_lf(head))
1617f23571e8STetsuo Handa 				return;
1618f23571e8STetsuo Handa 			/* fall through */
1619f23571e8STetsuo Handa 		case 2:
1620f23571e8STetsuo Handa 			head->r.step = 0;
1621f23571e8STetsuo Handa 			if (head->r.print_this_domain_only)
1622f23571e8STetsuo Handa 				goto done;
16239590837bSKentaro Takeda 		}
16249590837bSKentaro Takeda 	}
1625f23571e8STetsuo Handa  done:
1626f23571e8STetsuo Handa 	head->r.eof = true;
16279590837bSKentaro Takeda }
16289590837bSKentaro Takeda 
16299590837bSKentaro Takeda /**
16309590837bSKentaro Takeda  * tomoyo_write_pid: Specify PID to obtain domainname.
16319590837bSKentaro Takeda  *
16329590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16339590837bSKentaro Takeda  *
16349590837bSKentaro Takeda  * Returns 0.
16359590837bSKentaro Takeda  */
16369590837bSKentaro Takeda static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
16379590837bSKentaro Takeda {
1638f23571e8STetsuo Handa 	head->r.eof = false;
16399590837bSKentaro Takeda 	return 0;
16409590837bSKentaro Takeda }
16419590837bSKentaro Takeda 
16429590837bSKentaro Takeda /**
16439590837bSKentaro Takeda  * tomoyo_read_pid - Get domainname of the specified PID.
16449590837bSKentaro Takeda  *
16459590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16469590837bSKentaro Takeda  *
16479590837bSKentaro Takeda  * Returns the domainname which the specified PID is in on success,
16489590837bSKentaro Takeda  * empty string otherwise.
16499590837bSKentaro Takeda  * The PID is specified by tomoyo_write_pid() so that the user can obtain
16509590837bSKentaro Takeda  * using read()/write() interface rather than sysctl() interface.
16519590837bSKentaro Takeda  */
16528fbe71f0STetsuo Handa static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16539590837bSKentaro Takeda {
1654f23571e8STetsuo Handa 	char *buf = head->write_buf;
1655f23571e8STetsuo Handa 	bool global_pid = false;
1656f23571e8STetsuo Handa 	unsigned int pid;
16579590837bSKentaro Takeda 	struct task_struct *p;
16589590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
1659f23571e8STetsuo Handa 
1660f23571e8STetsuo Handa 	/* Accessing write_buf is safe because head->io_sem is held. */
1661f23571e8STetsuo Handa 	if (!buf) {
1662f23571e8STetsuo Handa 		head->r.eof = true;
1663f23571e8STetsuo Handa 		return; /* Do nothing if open(O_RDONLY). */
1664f23571e8STetsuo Handa 	}
1665f23571e8STetsuo Handa 	if (head->r.w_pos || head->r.eof)
1666f23571e8STetsuo Handa 		return;
1667f23571e8STetsuo Handa 	head->r.eof = true;
1668f23571e8STetsuo Handa 	if (tomoyo_str_starts(&buf, "global-pid "))
1669f23571e8STetsuo Handa 		global_pid = true;
1670f23571e8STetsuo Handa 	pid = (unsigned int) simple_strtoul(buf, NULL, 10);
16711fcdc7c5STetsuo Handa 	rcu_read_lock();
1672f23571e8STetsuo Handa 	if (global_pid)
1673f23571e8STetsuo Handa 		p = find_task_by_pid_ns(pid, &init_pid_ns);
1674f23571e8STetsuo Handa 	else
16759590837bSKentaro Takeda 		p = find_task_by_vpid(pid);
16769590837bSKentaro Takeda 	if (p)
16779590837bSKentaro Takeda 		domain = tomoyo_real_domain(p);
16781fcdc7c5STetsuo Handa 	rcu_read_unlock();
1679f23571e8STetsuo Handa 	if (!domain)
1680f23571e8STetsuo Handa 		return;
1681f23571e8STetsuo Handa 	tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1682f23571e8STetsuo Handa 	tomoyo_set_string(head, domain->domainname->name);
16839590837bSKentaro Takeda }
16849590837bSKentaro Takeda 
16850f2a55d5STetsuo Handa /* String table for domain transition control keywords. */
16865448ec4fSTetsuo Handa static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1687bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
1688bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_RESET]         = "reset_domain ",
1689b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1690b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1691b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1692b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
16935448ec4fSTetsuo Handa };
16945448ec4fSTetsuo Handa 
16950f2a55d5STetsuo Handa /* String table for grouping keywords. */
1696e2bf6907STetsuo Handa static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1697b5bc60b4STetsuo Handa 	[TOMOYO_PATH_GROUP]    = "path_group ",
1698b5bc60b4STetsuo Handa 	[TOMOYO_NUMBER_GROUP]  = "number_group ",
1699059d84dbSTetsuo Handa 	[TOMOYO_ADDRESS_GROUP] = "address_group ",
1700e2bf6907STetsuo Handa };
1701e2bf6907STetsuo Handa 
17029590837bSKentaro Takeda /**
1703e2bf6907STetsuo Handa  * tomoyo_write_exception - Write exception policy.
17049590837bSKentaro Takeda  *
17059590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
17069590837bSKentaro Takeda  *
17079590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1708fdb8ebb7STetsuo Handa  *
1709fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
17109590837bSKentaro Takeda  */
1711e2bf6907STetsuo Handa static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
17129590837bSKentaro Takeda {
1713bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1714a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1715bd03a3e4STetsuo Handa 		.ns = head->w.ns,
1716bd03a3e4STetsuo Handa 		.is_delete = is_delete,
1717a238cf5bSTetsuo Handa 		.data = head->write_buf,
1718e2bf6907STetsuo Handa 	};
1719a238cf5bSTetsuo Handa 	u8 i;
1720a238cf5bSTetsuo Handa 	if (tomoyo_str_starts(&param.data, "aggregator "))
1721a238cf5bSTetsuo Handa 		return tomoyo_write_aggregator(&param);
1722e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1723a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1724a238cf5bSTetsuo Handa 			return tomoyo_write_transition_control(&param, i);
1725e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1726a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1727a238cf5bSTetsuo Handa 			return tomoyo_write_group(&param, i);
172832997144STetsuo Handa 	if (tomoyo_str_starts(&param.data, "acl_group ")) {
172932997144STetsuo Handa 		unsigned int group;
173032997144STetsuo Handa 		char *data;
173132997144STetsuo Handa 		group = simple_strtoul(param.data, &data, 10);
173232997144STetsuo Handa 		if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1733bd03a3e4STetsuo Handa 			return tomoyo_write_domain2
1734bd03a3e4STetsuo Handa 				(head->w.ns, &head->w.ns->acl_group[group],
1735bd03a3e4STetsuo Handa 				 data, is_delete);
173632997144STetsuo Handa 	}
17379590837bSKentaro Takeda 	return -EINVAL;
17389590837bSKentaro Takeda }
17399590837bSKentaro Takeda 
174031845e8cSTetsuo Handa /**
1741059d84dbSTetsuo Handa  * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
174231845e8cSTetsuo Handa  *
174331845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
174431845e8cSTetsuo Handa  * @idx:  Index number.
174531845e8cSTetsuo Handa  *
174631845e8cSTetsuo Handa  * Returns true on success, false otherwise.
174731845e8cSTetsuo Handa  *
174831845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
174931845e8cSTetsuo Handa  */
175031845e8cSTetsuo Handa static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
175131845e8cSTetsuo Handa {
1752bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1753bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1754bd03a3e4STetsuo Handa 	struct list_head *list = &ns->group_list[idx];
1755bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.group, list) {
175631845e8cSTetsuo Handa 		struct tomoyo_group *group =
17570df7e8b8STetsuo Handa 			list_entry(head->r.group, typeof(*group), head.list);
1758f23571e8STetsuo Handa 		list_for_each_cookie(head->r.acl, &group->member_list) {
175931845e8cSTetsuo Handa 			struct tomoyo_acl_head *ptr =
1760f23571e8STetsuo Handa 				list_entry(head->r.acl, typeof(*ptr), list);
176131845e8cSTetsuo Handa 			if (ptr->is_deleted)
176231845e8cSTetsuo Handa 				continue;
1763f23571e8STetsuo Handa 			if (!tomoyo_flush(head))
176431845e8cSTetsuo Handa 				return false;
1765bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
1766f23571e8STetsuo Handa 			tomoyo_set_string(head, tomoyo_group_name[idx]);
1767f23571e8STetsuo Handa 			tomoyo_set_string(head, group->group_name->name);
1768f23571e8STetsuo Handa 			if (idx == TOMOYO_PATH_GROUP) {
1769f23571e8STetsuo Handa 				tomoyo_set_space(head);
1770f23571e8STetsuo Handa 				tomoyo_set_string(head, container_of
1771f23571e8STetsuo Handa 					       (ptr, struct tomoyo_path_group,
1772f23571e8STetsuo Handa 						head)->member_name->name);
1773f23571e8STetsuo Handa 			} else if (idx == TOMOYO_NUMBER_GROUP) {
1774f23571e8STetsuo Handa 				tomoyo_print_number_union(head, &container_of
1775f23571e8STetsuo Handa 							  (ptr,
1776f23571e8STetsuo Handa 						   struct tomoyo_number_group,
1777f23571e8STetsuo Handa 							   head)->number);
1778059d84dbSTetsuo Handa 			} else if (idx == TOMOYO_ADDRESS_GROUP) {
1779059d84dbSTetsuo Handa 				char buffer[128];
1780059d84dbSTetsuo Handa 
1781059d84dbSTetsuo Handa 				struct tomoyo_address_group *member =
1782059d84dbSTetsuo Handa 					container_of(ptr, typeof(*member),
1783059d84dbSTetsuo Handa 						     head);
1784059d84dbSTetsuo Handa 				tomoyo_print_ip(buffer, sizeof(buffer),
1785059d84dbSTetsuo Handa 						&member->address);
1786059d84dbSTetsuo Handa 				tomoyo_io_printf(head, " %s", buffer);
178731845e8cSTetsuo Handa 			}
1788f23571e8STetsuo Handa 			tomoyo_set_lf(head);
178931845e8cSTetsuo Handa 		}
1790f23571e8STetsuo Handa 		head->r.acl = NULL;
1791f23571e8STetsuo Handa 	}
1792f23571e8STetsuo Handa 	head->r.group = NULL;
179331845e8cSTetsuo Handa 	return true;
179431845e8cSTetsuo Handa }
179531845e8cSTetsuo Handa 
179631845e8cSTetsuo Handa /**
179731845e8cSTetsuo Handa  * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
179831845e8cSTetsuo Handa  *
179931845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
180031845e8cSTetsuo Handa  * @idx:  Index number.
180131845e8cSTetsuo Handa  *
180231845e8cSTetsuo Handa  * Returns true on success, false otherwise.
180331845e8cSTetsuo Handa  *
180431845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
180531845e8cSTetsuo Handa  */
180631845e8cSTetsuo Handa static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
180731845e8cSTetsuo Handa {
1808bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1809bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1810bd03a3e4STetsuo Handa 	struct list_head *list = &ns->policy_list[idx];
1811bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1812475e6fa3STetsuo Handa 		struct tomoyo_acl_head *acl =
1813f23571e8STetsuo Handa 			container_of(head->r.acl, typeof(*acl), list);
181431845e8cSTetsuo Handa 		if (acl->is_deleted)
181531845e8cSTetsuo Handa 			continue;
1816f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
1817f23571e8STetsuo Handa 			return false;
181831845e8cSTetsuo Handa 		switch (idx) {
18195448ec4fSTetsuo Handa 		case TOMOYO_ID_TRANSITION_CONTROL:
182031845e8cSTetsuo Handa 			{
18215448ec4fSTetsuo Handa 				struct tomoyo_transition_control *ptr =
182231845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1823bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
18240d2171d7STetsuo Handa 				tomoyo_set_string(head, tomoyo_transition_type
1825f23571e8STetsuo Handa 						  [ptr->type]);
18260d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->program ?
18270d2171d7STetsuo Handa 						  ptr->program->name : "any");
1828f23571e8STetsuo Handa 				tomoyo_set_string(head, " from ");
18290d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->domainname ?
18300d2171d7STetsuo Handa 						  ptr->domainname->name :
18310d2171d7STetsuo Handa 						  "any");
183231845e8cSTetsuo Handa 			}
183331845e8cSTetsuo Handa 			break;
183431845e8cSTetsuo Handa 		case TOMOYO_ID_AGGREGATOR:
183531845e8cSTetsuo Handa 			{
1836e2bf6907STetsuo Handa 				struct tomoyo_aggregator *ptr =
183731845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1838bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
1839b5bc60b4STetsuo Handa 				tomoyo_set_string(head, "aggregator ");
1840f23571e8STetsuo Handa 				tomoyo_set_string(head,
1841f23571e8STetsuo Handa 						  ptr->original_name->name);
1842f23571e8STetsuo Handa 				tomoyo_set_space(head);
1843f23571e8STetsuo Handa 				tomoyo_set_string(head,
1844f23571e8STetsuo Handa 					       ptr->aggregated_name->name);
184531845e8cSTetsuo Handa 			}
184631845e8cSTetsuo Handa 			break;
184731845e8cSTetsuo Handa 		default:
184831845e8cSTetsuo Handa 			continue;
184931845e8cSTetsuo Handa 		}
1850f23571e8STetsuo Handa 		tomoyo_set_lf(head);
185131845e8cSTetsuo Handa 	}
1852f23571e8STetsuo Handa 	head->r.acl = NULL;
185331845e8cSTetsuo Handa 	return true;
185431845e8cSTetsuo Handa }
185531845e8cSTetsuo Handa 
18569590837bSKentaro Takeda /**
1857e2bf6907STetsuo Handa  * tomoyo_read_exception - Read exception policy.
18589590837bSKentaro Takeda  *
18599590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
18609590837bSKentaro Takeda  *
1861fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
18629590837bSKentaro Takeda  */
1863e2bf6907STetsuo Handa static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
18649590837bSKentaro Takeda {
1865bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1866bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1867f23571e8STetsuo Handa 	if (head->r.eof)
186831845e8cSTetsuo Handa 		return;
1869f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY &&
1870f23571e8STetsuo Handa 	       tomoyo_read_policy(head, head->r.step))
1871f23571e8STetsuo Handa 		head->r.step++;
1872f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY)
187331845e8cSTetsuo Handa 		return;
1874f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1875f23571e8STetsuo Handa 	       tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1876f23571e8STetsuo Handa 		head->r.step++;
1877f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
187831845e8cSTetsuo Handa 		return;
187932997144STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
188032997144STetsuo Handa 	       + TOMOYO_MAX_ACL_GROUPS) {
188132997144STetsuo Handa 		head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
188232997144STetsuo Handa 			- TOMOYO_MAX_GROUP;
1883bd03a3e4STetsuo Handa 		if (!tomoyo_read_domain2(head, &ns->acl_group
188432997144STetsuo Handa 					 [head->r.acl_group_index]))
188532997144STetsuo Handa 			return;
188632997144STetsuo Handa 		head->r.step++;
188732997144STetsuo Handa 	}
1888f23571e8STetsuo Handa 	head->r.eof = true;
18899590837bSKentaro Takeda }
18909590837bSKentaro Takeda 
1891eadd99ccSTetsuo Handa /* Wait queue for kernel -> userspace notification. */
189217fcfbd9STetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1893eadd99ccSTetsuo Handa /* Wait queue for userspace -> kernel notification. */
1894eadd99ccSTetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
189517fcfbd9STetsuo Handa 
189617fcfbd9STetsuo Handa /* Structure for query. */
1897e2bf6907STetsuo Handa struct tomoyo_query {
189817fcfbd9STetsuo Handa 	struct list_head list;
1899*59df3166STetsuo Handa 	struct tomoyo_domain_info *domain;
190017fcfbd9STetsuo Handa 	char *query;
1901eadd99ccSTetsuo Handa 	size_t query_len;
190217fcfbd9STetsuo Handa 	unsigned int serial;
1903eadd99ccSTetsuo Handa 	u8 timer;
1904eadd99ccSTetsuo Handa 	u8 answer;
1905eadd99ccSTetsuo Handa 	u8 retry;
190617fcfbd9STetsuo Handa };
190717fcfbd9STetsuo Handa 
1908e2bf6907STetsuo Handa /* The list for "struct tomoyo_query". */
190917fcfbd9STetsuo Handa static LIST_HEAD(tomoyo_query_list);
191017fcfbd9STetsuo Handa 
1911eadd99ccSTetsuo Handa /* Lock for manipulating tomoyo_query_list. */
1912eadd99ccSTetsuo Handa static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1913eadd99ccSTetsuo Handa 
191417fcfbd9STetsuo Handa /*
191517fcfbd9STetsuo Handa  * Number of "struct file" referring /sys/kernel/security/tomoyo/query
191617fcfbd9STetsuo Handa  * interface.
191717fcfbd9STetsuo Handa  */
191817fcfbd9STetsuo Handa static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
191917fcfbd9STetsuo Handa 
192017fcfbd9STetsuo Handa /**
19212ca9bf45STetsuo Handa  * tomoyo_truncate - Truncate a line.
19222ca9bf45STetsuo Handa  *
19232ca9bf45STetsuo Handa  * @str: String to truncate.
19242ca9bf45STetsuo Handa  *
19252ca9bf45STetsuo Handa  * Returns length of truncated @str.
19262ca9bf45STetsuo Handa  */
19272ca9bf45STetsuo Handa static int tomoyo_truncate(char *str)
19282ca9bf45STetsuo Handa {
19292ca9bf45STetsuo Handa 	char *start = str;
19302ca9bf45STetsuo Handa 	while (*(unsigned char *) str > (unsigned char) ' ')
19312ca9bf45STetsuo Handa 		str++;
19322ca9bf45STetsuo Handa 	*str = '\0';
19332ca9bf45STetsuo Handa 	return strlen(start) + 1;
19342ca9bf45STetsuo Handa }
19352ca9bf45STetsuo Handa 
19362ca9bf45STetsuo Handa /**
1937eadd99ccSTetsuo Handa  * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1938eadd99ccSTetsuo Handa  *
1939eadd99ccSTetsuo Handa  * @domain: Pointer to "struct tomoyo_domain_info".
1940eadd99ccSTetsuo Handa  * @header: Lines containing ACL.
1941eadd99ccSTetsuo Handa  *
1942eadd99ccSTetsuo Handa  * Returns nothing.
1943eadd99ccSTetsuo Handa  */
1944eadd99ccSTetsuo Handa static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1945eadd99ccSTetsuo Handa {
1946eadd99ccSTetsuo Handa 	char *buffer;
19472ca9bf45STetsuo Handa 	char *realpath = NULL;
19485b636857STetsuo Handa 	char *argv0 = NULL;
19492ca9bf45STetsuo Handa 	char *symlink = NULL;
1950eadd99ccSTetsuo Handa 	char *cp = strchr(header, '\n');
1951eadd99ccSTetsuo Handa 	int len;
1952eadd99ccSTetsuo Handa 	if (!cp)
1953eadd99ccSTetsuo Handa 		return;
1954eadd99ccSTetsuo Handa 	cp = strchr(cp + 1, '\n');
1955eadd99ccSTetsuo Handa 	if (!cp)
1956eadd99ccSTetsuo Handa 		return;
1957eadd99ccSTetsuo Handa 	*cp++ = '\0';
1958eadd99ccSTetsuo Handa 	len = strlen(cp) + 1;
19592ca9bf45STetsuo Handa 	/* strstr() will return NULL if ordering is wrong. */
19602ca9bf45STetsuo Handa 	if (*cp == 'f') {
19615b636857STetsuo Handa 		argv0 = strstr(header, " argv[]={ \"");
19625b636857STetsuo Handa 		if (argv0) {
19635b636857STetsuo Handa 			argv0 += 10;
19645b636857STetsuo Handa 			len += tomoyo_truncate(argv0) + 14;
19655b636857STetsuo Handa 		}
19662ca9bf45STetsuo Handa 		realpath = strstr(header, " exec={ realpath=\"");
19672ca9bf45STetsuo Handa 		if (realpath) {
19682ca9bf45STetsuo Handa 			realpath += 8;
19692ca9bf45STetsuo Handa 			len += tomoyo_truncate(realpath) + 6;
19702ca9bf45STetsuo Handa 		}
19712ca9bf45STetsuo Handa 		symlink = strstr(header, " symlink.target=\"");
19722ca9bf45STetsuo Handa 		if (symlink)
19732ca9bf45STetsuo Handa 			len += tomoyo_truncate(symlink + 1) + 1;
19742ca9bf45STetsuo Handa 	}
1975eadd99ccSTetsuo Handa 	buffer = kmalloc(len, GFP_NOFS);
1976eadd99ccSTetsuo Handa 	if (!buffer)
1977eadd99ccSTetsuo Handa 		return;
1978eadd99ccSTetsuo Handa 	snprintf(buffer, len - 1, "%s", cp);
19792ca9bf45STetsuo Handa 	if (realpath)
19802ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.%s", realpath);
19815b636857STetsuo Handa 	if (argv0)
19825b636857STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
19832ca9bf45STetsuo Handa 	if (symlink)
19842ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, "%s", symlink);
1985eadd99ccSTetsuo Handa 	tomoyo_normalize_line(buffer);
1986b22b8b9fSTetsuo Handa 	if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1987b22b8b9fSTetsuo Handa 				  false))
1988b22b8b9fSTetsuo Handa 		tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
1989eadd99ccSTetsuo Handa 	kfree(buffer);
1990eadd99ccSTetsuo Handa }
1991eadd99ccSTetsuo Handa 
1992eadd99ccSTetsuo Handa /**
199317fcfbd9STetsuo Handa  * tomoyo_supervisor - Ask for the supervisor's decision.
199417fcfbd9STetsuo Handa  *
199517fcfbd9STetsuo Handa  * @r:   Pointer to "struct tomoyo_request_info".
199617fcfbd9STetsuo Handa  * @fmt: The printf()'s format string, followed by parameters.
199717fcfbd9STetsuo Handa  *
199817fcfbd9STetsuo Handa  * Returns 0 if the supervisor decided to permit the access request which
199917fcfbd9STetsuo Handa  * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
200017fcfbd9STetsuo Handa  * supervisor decided to retry the access request which violated the policy in
200117fcfbd9STetsuo Handa  * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
200217fcfbd9STetsuo Handa  */
200317fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
200417fcfbd9STetsuo Handa {
200517fcfbd9STetsuo Handa 	va_list args;
2006eadd99ccSTetsuo Handa 	int error;
200717fcfbd9STetsuo Handa 	int len;
200817fcfbd9STetsuo Handa 	static unsigned int tomoyo_serial;
2009eadd99ccSTetsuo Handa 	struct tomoyo_query entry = { };
201017fcfbd9STetsuo Handa 	bool quota_exceeded = false;
2011eadd99ccSTetsuo Handa 	va_start(args, fmt);
2012eadd99ccSTetsuo Handa 	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2013eadd99ccSTetsuo Handa 	va_end(args);
2014eadd99ccSTetsuo Handa 	/* Write /sys/kernel/security/tomoyo/audit. */
2015eadd99ccSTetsuo Handa 	va_start(args, fmt);
2016eadd99ccSTetsuo Handa 	tomoyo_write_log2(r, len, fmt, args);
2017eadd99ccSTetsuo Handa 	va_end(args);
2018eadd99ccSTetsuo Handa 	/* Nothing more to do if granted. */
2019eadd99ccSTetsuo Handa 	if (r->granted)
2020eadd99ccSTetsuo Handa 		return 0;
2021b22b8b9fSTetsuo Handa 	if (r->mode)
2022b22b8b9fSTetsuo Handa 		tomoyo_update_stat(r->mode);
202317fcfbd9STetsuo Handa 	switch (r->mode) {
2024eadd99ccSTetsuo Handa 	case TOMOYO_CONFIG_ENFORCING:
2025eadd99ccSTetsuo Handa 		error = -EPERM;
2026eadd99ccSTetsuo Handa 		if (atomic_read(&tomoyo_query_observers))
2027eadd99ccSTetsuo Handa 			break;
2028eadd99ccSTetsuo Handa 		goto out;
202917fcfbd9STetsuo Handa 	case TOMOYO_CONFIG_LEARNING:
2030eadd99ccSTetsuo Handa 		error = 0;
2031eadd99ccSTetsuo Handa 		/* Check max_learning_entry parameter. */
2032eadd99ccSTetsuo Handa 		if (tomoyo_domain_quota_is_ok(r))
2033eadd99ccSTetsuo Handa 			break;
203417fcfbd9STetsuo Handa 		/* fall through */
2035eadd99ccSTetsuo Handa 	default:
203617fcfbd9STetsuo Handa 		return 0;
203717fcfbd9STetsuo Handa 	}
2038eadd99ccSTetsuo Handa 	/* Get message. */
203917fcfbd9STetsuo Handa 	va_start(args, fmt);
2040eadd99ccSTetsuo Handa 	entry.query = tomoyo_init_log(r, len, fmt, args);
204117fcfbd9STetsuo Handa 	va_end(args);
2042eadd99ccSTetsuo Handa 	if (!entry.query)
204317fcfbd9STetsuo Handa 		goto out;
2044eadd99ccSTetsuo Handa 	entry.query_len = strlen(entry.query) + 1;
2045eadd99ccSTetsuo Handa 	if (!error) {
2046eadd99ccSTetsuo Handa 		tomoyo_add_entry(r->domain, entry.query);
204717fcfbd9STetsuo Handa 		goto out;
2048eadd99ccSTetsuo Handa 	}
2049eadd99ccSTetsuo Handa 	len = tomoyo_round2(entry.query_len);
2050*59df3166STetsuo Handa 	entry.domain = r->domain;
205117fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2052eadd99ccSTetsuo Handa 	if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2053eadd99ccSTetsuo Handa 	    tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2054eadd99ccSTetsuo Handa 	    >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
205517fcfbd9STetsuo Handa 		quota_exceeded = true;
205617fcfbd9STetsuo Handa 	} else {
2057eadd99ccSTetsuo Handa 		entry.serial = tomoyo_serial++;
2058eadd99ccSTetsuo Handa 		entry.retry = r->retry;
2059eadd99ccSTetsuo Handa 		tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2060eadd99ccSTetsuo Handa 		list_add_tail(&entry.list, &tomoyo_query_list);
206117fcfbd9STetsuo Handa 	}
206217fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
206317fcfbd9STetsuo Handa 	if (quota_exceeded)
206417fcfbd9STetsuo Handa 		goto out;
206517fcfbd9STetsuo Handa 	/* Give 10 seconds for supervisor's opinion. */
2066eadd99ccSTetsuo Handa 	while (entry.timer < 10) {
2067eadd99ccSTetsuo Handa 		wake_up_all(&tomoyo_query_wait);
2068eadd99ccSTetsuo Handa 		if (wait_event_interruptible_timeout
2069eadd99ccSTetsuo Handa 		    (tomoyo_answer_wait, entry.answer ||
2070eadd99ccSTetsuo Handa 		     !atomic_read(&tomoyo_query_observers), HZ))
207117fcfbd9STetsuo Handa 			break;
2072eadd99ccSTetsuo Handa 		else
2073eadd99ccSTetsuo Handa 			entry.timer++;
207417fcfbd9STetsuo Handa 	}
207517fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2076eadd99ccSTetsuo Handa 	list_del(&entry.list);
2077eadd99ccSTetsuo Handa 	tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
207817fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2079eadd99ccSTetsuo Handa 	switch (entry.answer) {
208017fcfbd9STetsuo Handa 	case 3: /* Asked to retry by administrator. */
208117fcfbd9STetsuo Handa 		error = TOMOYO_RETRY_REQUEST;
208217fcfbd9STetsuo Handa 		r->retry++;
208317fcfbd9STetsuo Handa 		break;
208417fcfbd9STetsuo Handa 	case 1:
208517fcfbd9STetsuo Handa 		/* Granted by administrator. */
208617fcfbd9STetsuo Handa 		error = 0;
208717fcfbd9STetsuo Handa 		break;
208817fcfbd9STetsuo Handa 	default:
2089eadd99ccSTetsuo Handa 		/* Timed out or rejected by administrator. */
209017fcfbd9STetsuo Handa 		break;
209117fcfbd9STetsuo Handa 	}
209217fcfbd9STetsuo Handa out:
2093eadd99ccSTetsuo Handa 	kfree(entry.query);
209417fcfbd9STetsuo Handa 	return error;
209517fcfbd9STetsuo Handa }
209617fcfbd9STetsuo Handa 
209717fcfbd9STetsuo Handa /**
2098*59df3166STetsuo Handa  * tomoyo_find_domain_by_qid - Get domain by query id.
2099*59df3166STetsuo Handa  *
2100*59df3166STetsuo Handa  * @serial: Query ID assigned by tomoyo_supervisor().
2101*59df3166STetsuo Handa  *
2102*59df3166STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
2103*59df3166STetsuo Handa  */
2104*59df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
2105*59df3166STetsuo Handa (unsigned int serial)
2106*59df3166STetsuo Handa {
2107*59df3166STetsuo Handa 	struct tomoyo_query *ptr;
2108*59df3166STetsuo Handa 	struct tomoyo_domain_info *domain = NULL;
2109*59df3166STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2110*59df3166STetsuo Handa 	list_for_each_entry(ptr, &tomoyo_query_list, list) {
2111*59df3166STetsuo Handa 		if (ptr->serial != serial || ptr->answer)
2112*59df3166STetsuo Handa 			continue;
2113*59df3166STetsuo Handa 		domain = ptr->domain;
2114*59df3166STetsuo Handa 		break;
2115*59df3166STetsuo Handa 	}
2116*59df3166STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2117*59df3166STetsuo Handa 	return domain;
2118*59df3166STetsuo Handa }
2119*59df3166STetsuo Handa 
2120*59df3166STetsuo Handa /**
212117fcfbd9STetsuo Handa  * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
212217fcfbd9STetsuo Handa  *
212317fcfbd9STetsuo Handa  * @file: Pointer to "struct file".
212417fcfbd9STetsuo Handa  * @wait: Pointer to "poll_table".
212517fcfbd9STetsuo Handa  *
212617fcfbd9STetsuo Handa  * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
212717fcfbd9STetsuo Handa  *
212817fcfbd9STetsuo Handa  * Waits for access requests which violated policy in enforcing mode.
212917fcfbd9STetsuo Handa  */
213017fcfbd9STetsuo Handa static int tomoyo_poll_query(struct file *file, poll_table *wait)
213117fcfbd9STetsuo Handa {
213217fcfbd9STetsuo Handa 	struct list_head *tmp;
213317fcfbd9STetsuo Handa 	bool found = false;
213417fcfbd9STetsuo Handa 	u8 i;
213517fcfbd9STetsuo Handa 	for (i = 0; i < 2; i++) {
213617fcfbd9STetsuo Handa 		spin_lock(&tomoyo_query_list_lock);
213717fcfbd9STetsuo Handa 		list_for_each(tmp, &tomoyo_query_list) {
2138e2bf6907STetsuo Handa 			struct tomoyo_query *ptr =
2139e2bf6907STetsuo Handa 				list_entry(tmp, typeof(*ptr), list);
214017fcfbd9STetsuo Handa 			if (ptr->answer)
214117fcfbd9STetsuo Handa 				continue;
214217fcfbd9STetsuo Handa 			found = true;
214317fcfbd9STetsuo Handa 			break;
214417fcfbd9STetsuo Handa 		}
214517fcfbd9STetsuo Handa 		spin_unlock(&tomoyo_query_list_lock);
214617fcfbd9STetsuo Handa 		if (found)
214717fcfbd9STetsuo Handa 			return POLLIN | POLLRDNORM;
214817fcfbd9STetsuo Handa 		if (i)
214917fcfbd9STetsuo Handa 			break;
215017fcfbd9STetsuo Handa 		poll_wait(file, &tomoyo_query_wait, wait);
215117fcfbd9STetsuo Handa 	}
215217fcfbd9STetsuo Handa 	return 0;
215317fcfbd9STetsuo Handa }
215417fcfbd9STetsuo Handa 
215517fcfbd9STetsuo Handa /**
215617fcfbd9STetsuo Handa  * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
215717fcfbd9STetsuo Handa  *
215817fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
215917fcfbd9STetsuo Handa  */
21608fbe71f0STetsuo Handa static void tomoyo_read_query(struct tomoyo_io_buffer *head)
216117fcfbd9STetsuo Handa {
216217fcfbd9STetsuo Handa 	struct list_head *tmp;
21632c47ab93STetsuo Handa 	unsigned int pos = 0;
21642c47ab93STetsuo Handa 	size_t len = 0;
216517fcfbd9STetsuo Handa 	char *buf;
2166f23571e8STetsuo Handa 	if (head->r.w_pos)
21678fbe71f0STetsuo Handa 		return;
216817fcfbd9STetsuo Handa 	if (head->read_buf) {
216917fcfbd9STetsuo Handa 		kfree(head->read_buf);
217017fcfbd9STetsuo Handa 		head->read_buf = NULL;
217117fcfbd9STetsuo Handa 	}
217217fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
217317fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2174e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
217517fcfbd9STetsuo Handa 		if (ptr->answer)
217617fcfbd9STetsuo Handa 			continue;
2177f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
217817fcfbd9STetsuo Handa 			continue;
217917fcfbd9STetsuo Handa 		len = ptr->query_len;
218017fcfbd9STetsuo Handa 		break;
218117fcfbd9STetsuo Handa 	}
218217fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
218317fcfbd9STetsuo Handa 	if (!len) {
2184f23571e8STetsuo Handa 		head->r.query_index = 0;
21858fbe71f0STetsuo Handa 		return;
218617fcfbd9STetsuo Handa 	}
2187eadd99ccSTetsuo Handa 	buf = kzalloc(len + 32, GFP_NOFS);
218817fcfbd9STetsuo Handa 	if (!buf)
21898fbe71f0STetsuo Handa 		return;
219017fcfbd9STetsuo Handa 	pos = 0;
219117fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
219217fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2193e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
219417fcfbd9STetsuo Handa 		if (ptr->answer)
219517fcfbd9STetsuo Handa 			continue;
2196f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
219717fcfbd9STetsuo Handa 			continue;
219817fcfbd9STetsuo Handa 		/*
219917fcfbd9STetsuo Handa 		 * Some query can be skipped because tomoyo_query_list
220017fcfbd9STetsuo Handa 		 * can change, but I don't care.
220117fcfbd9STetsuo Handa 		 */
220217fcfbd9STetsuo Handa 		if (len == ptr->query_len)
2203eadd99ccSTetsuo Handa 			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2204eadd99ccSTetsuo Handa 				 ptr->retry, ptr->query);
220517fcfbd9STetsuo Handa 		break;
220617fcfbd9STetsuo Handa 	}
220717fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
220817fcfbd9STetsuo Handa 	if (buf[0]) {
220917fcfbd9STetsuo Handa 		head->read_buf = buf;
2210f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = buf;
2211f23571e8STetsuo Handa 		head->r.query_index++;
221217fcfbd9STetsuo Handa 	} else {
221317fcfbd9STetsuo Handa 		kfree(buf);
221417fcfbd9STetsuo Handa 	}
221517fcfbd9STetsuo Handa }
221617fcfbd9STetsuo Handa 
221717fcfbd9STetsuo Handa /**
221817fcfbd9STetsuo Handa  * tomoyo_write_answer - Write the supervisor's decision.
221917fcfbd9STetsuo Handa  *
222017fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
222117fcfbd9STetsuo Handa  *
222217fcfbd9STetsuo Handa  * Returns 0 on success, -EINVAL otherwise.
222317fcfbd9STetsuo Handa  */
222417fcfbd9STetsuo Handa static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
222517fcfbd9STetsuo Handa {
222617fcfbd9STetsuo Handa 	char *data = head->write_buf;
222717fcfbd9STetsuo Handa 	struct list_head *tmp;
222817fcfbd9STetsuo Handa 	unsigned int serial;
222917fcfbd9STetsuo Handa 	unsigned int answer;
223017fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
223117fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2232e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
223317fcfbd9STetsuo Handa 		ptr->timer = 0;
223417fcfbd9STetsuo Handa 	}
223517fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
223617fcfbd9STetsuo Handa 	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
223717fcfbd9STetsuo Handa 		return -EINVAL;
223817fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
223917fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2240e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
224117fcfbd9STetsuo Handa 		if (ptr->serial != serial)
224217fcfbd9STetsuo Handa 			continue;
224317fcfbd9STetsuo Handa 		if (!ptr->answer)
224417fcfbd9STetsuo Handa 			ptr->answer = answer;
224517fcfbd9STetsuo Handa 		break;
224617fcfbd9STetsuo Handa 	}
224717fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
224817fcfbd9STetsuo Handa 	return 0;
224917fcfbd9STetsuo Handa }
225017fcfbd9STetsuo Handa 
225117fcfbd9STetsuo Handa /**
22529590837bSKentaro Takeda  * tomoyo_read_version: Get version.
22539590837bSKentaro Takeda  *
22549590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
22559590837bSKentaro Takeda  *
22569590837bSKentaro Takeda  * Returns version information.
22579590837bSKentaro Takeda  */
22588fbe71f0STetsuo Handa static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22599590837bSKentaro Takeda {
2260f23571e8STetsuo Handa 	if (!head->r.eof) {
2261843d183cSTetsuo Handa 		tomoyo_io_printf(head, "2.5.0");
2262f23571e8STetsuo Handa 		head->r.eof = true;
22639590837bSKentaro Takeda 	}
22649590837bSKentaro Takeda }
22659590837bSKentaro Takeda 
2266b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2267b22b8b9fSTetsuo Handa static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2268b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_UPDATES]    = "update:",
2269b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2270b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2271b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2272b22b8b9fSTetsuo Handa };
2273b22b8b9fSTetsuo Handa 
2274b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2275b22b8b9fSTetsuo Handa static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2276b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_POLICY] = "policy:",
2277b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_AUDIT]  = "audit log:",
2278b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_QUERY]  = "query message:",
2279b22b8b9fSTetsuo Handa };
2280b22b8b9fSTetsuo Handa 
2281b22b8b9fSTetsuo Handa /* Timestamp counter for last updated. */
2282b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2283b22b8b9fSTetsuo Handa /* Counter for number of updates. */
2284b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2285b22b8b9fSTetsuo Handa 
2286b22b8b9fSTetsuo Handa /**
2287b22b8b9fSTetsuo Handa  * tomoyo_update_stat - Update statistic counters.
2288b22b8b9fSTetsuo Handa  *
2289b22b8b9fSTetsuo Handa  * @index: Index for policy type.
2290b22b8b9fSTetsuo Handa  *
2291b22b8b9fSTetsuo Handa  * Returns nothing.
2292b22b8b9fSTetsuo Handa  */
2293b22b8b9fSTetsuo Handa void tomoyo_update_stat(const u8 index)
2294b22b8b9fSTetsuo Handa {
2295b22b8b9fSTetsuo Handa 	struct timeval tv;
2296b22b8b9fSTetsuo Handa 	do_gettimeofday(&tv);
2297b22b8b9fSTetsuo Handa 	/*
2298b22b8b9fSTetsuo Handa 	 * I don't use atomic operations because race condition is not fatal.
2299b22b8b9fSTetsuo Handa 	 */
2300b22b8b9fSTetsuo Handa 	tomoyo_stat_updated[index]++;
2301b22b8b9fSTetsuo Handa 	tomoyo_stat_modified[index] = tv.tv_sec;
2302b22b8b9fSTetsuo Handa }
2303b22b8b9fSTetsuo Handa 
2304b22b8b9fSTetsuo Handa /**
2305b22b8b9fSTetsuo Handa  * tomoyo_read_stat - Read statistic data.
2306b22b8b9fSTetsuo Handa  *
2307b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2308b22b8b9fSTetsuo Handa  *
2309b22b8b9fSTetsuo Handa  * Returns nothing.
2310b22b8b9fSTetsuo Handa  */
2311b22b8b9fSTetsuo Handa static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2312b22b8b9fSTetsuo Handa {
2313b22b8b9fSTetsuo Handa 	u8 i;
2314b22b8b9fSTetsuo Handa 	unsigned int total = 0;
2315b22b8b9fSTetsuo Handa 	if (head->r.eof)
2316b22b8b9fSTetsuo Handa 		return;
2317b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2318b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Policy %-30s %10u",
2319b22b8b9fSTetsuo Handa 				 tomoyo_policy_headers[i],
2320b22b8b9fSTetsuo Handa 				 tomoyo_stat_updated[i]);
2321b22b8b9fSTetsuo Handa 		if (tomoyo_stat_modified[i]) {
2322b22b8b9fSTetsuo Handa 			struct tomoyo_time stamp;
2323b22b8b9fSTetsuo Handa 			tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2324b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2325b22b8b9fSTetsuo Handa 					 "%02u:%02u:%02u)",
2326b22b8b9fSTetsuo Handa 					 stamp.year, stamp.month, stamp.day,
2327b22b8b9fSTetsuo Handa 					 stamp.hour, stamp.min, stamp.sec);
2328b22b8b9fSTetsuo Handa 		}
2329b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2330b22b8b9fSTetsuo Handa 	}
2331b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2332b22b8b9fSTetsuo Handa 		unsigned int used = tomoyo_memory_used[i];
2333b22b8b9fSTetsuo Handa 		total += used;
2334b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Memory used by %-22s %10u",
2335b22b8b9fSTetsuo Handa 				 tomoyo_memory_headers[i], used);
2336b22b8b9fSTetsuo Handa 		used = tomoyo_memory_quota[i];
2337b22b8b9fSTetsuo Handa 		if (used)
2338b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Quota: %10u)", used);
2339b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2340b22b8b9fSTetsuo Handa 	}
2341b22b8b9fSTetsuo Handa 	tomoyo_io_printf(head, "Total memory used:                    %10u\n",
2342b22b8b9fSTetsuo Handa 			 total);
2343b22b8b9fSTetsuo Handa 	head->r.eof = true;
2344b22b8b9fSTetsuo Handa }
2345b22b8b9fSTetsuo Handa 
2346b22b8b9fSTetsuo Handa /**
2347b22b8b9fSTetsuo Handa  * tomoyo_write_stat - Set memory quota.
2348b22b8b9fSTetsuo Handa  *
2349b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2350b22b8b9fSTetsuo Handa  *
2351b22b8b9fSTetsuo Handa  * Returns 0.
2352b22b8b9fSTetsuo Handa  */
2353b22b8b9fSTetsuo Handa static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2354b22b8b9fSTetsuo Handa {
2355b22b8b9fSTetsuo Handa 	char *data = head->write_buf;
2356b22b8b9fSTetsuo Handa 	u8 i;
2357b22b8b9fSTetsuo Handa 	if (tomoyo_str_starts(&data, "Memory used by "))
2358b22b8b9fSTetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2359b22b8b9fSTetsuo Handa 			if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2360b22b8b9fSTetsuo Handa 				sscanf(data, "%u", &tomoyo_memory_quota[i]);
2361b22b8b9fSTetsuo Handa 	return 0;
2362b22b8b9fSTetsuo Handa }
2363b22b8b9fSTetsuo Handa 
23649590837bSKentaro Takeda /**
23659590837bSKentaro Takeda  * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
23669590837bSKentaro Takeda  *
23679590837bSKentaro Takeda  * @type: Type of interface.
23689590837bSKentaro Takeda  * @file: Pointer to "struct file".
23699590837bSKentaro Takeda  *
23702e503bbbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
23719590837bSKentaro Takeda  */
2372c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file)
23739590837bSKentaro Takeda {
23744e5d6f7eSTetsuo Handa 	struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
23759590837bSKentaro Takeda 
23769590837bSKentaro Takeda 	if (!head)
23779590837bSKentaro Takeda 		return -ENOMEM;
23789590837bSKentaro Takeda 	mutex_init(&head->io_sem);
237917fcfbd9STetsuo Handa 	head->type = type;
23809590837bSKentaro Takeda 	switch (type) {
23819590837bSKentaro Takeda 	case TOMOYO_DOMAINPOLICY:
23829590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/domain_policy */
2383e2bf6907STetsuo Handa 		head->write = tomoyo_write_domain;
2384e2bf6907STetsuo Handa 		head->read = tomoyo_read_domain;
23859590837bSKentaro Takeda 		break;
23869590837bSKentaro Takeda 	case TOMOYO_EXCEPTIONPOLICY:
23879590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/exception_policy */
2388e2bf6907STetsuo Handa 		head->write = tomoyo_write_exception;
2389e2bf6907STetsuo Handa 		head->read = tomoyo_read_exception;
23909590837bSKentaro Takeda 		break;
2391eadd99ccSTetsuo Handa 	case TOMOYO_AUDIT:
2392eadd99ccSTetsuo Handa 		/* /sys/kernel/security/tomoyo/audit */
2393eadd99ccSTetsuo Handa 		head->poll = tomoyo_poll_log;
2394eadd99ccSTetsuo Handa 		head->read = tomoyo_read_log;
2395eadd99ccSTetsuo Handa 		break;
23969590837bSKentaro Takeda 	case TOMOYO_PROCESS_STATUS:
23979590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/.process_status */
23989590837bSKentaro Takeda 		head->write = tomoyo_write_pid;
23999590837bSKentaro Takeda 		head->read = tomoyo_read_pid;
24009590837bSKentaro Takeda 		break;
24019590837bSKentaro Takeda 	case TOMOYO_VERSION:
24029590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/version */
24039590837bSKentaro Takeda 		head->read = tomoyo_read_version;
24049590837bSKentaro Takeda 		head->readbuf_size = 128;
24059590837bSKentaro Takeda 		break;
2406b22b8b9fSTetsuo Handa 	case TOMOYO_STAT:
2407b22b8b9fSTetsuo Handa 		/* /sys/kernel/security/tomoyo/stat */
2408b22b8b9fSTetsuo Handa 		head->write = tomoyo_write_stat;
2409b22b8b9fSTetsuo Handa 		head->read = tomoyo_read_stat;
2410b22b8b9fSTetsuo Handa 		head->readbuf_size = 1024;
24119590837bSKentaro Takeda 		break;
24129590837bSKentaro Takeda 	case TOMOYO_PROFILE:
24139590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/profile */
24149590837bSKentaro Takeda 		head->write = tomoyo_write_profile;
24159590837bSKentaro Takeda 		head->read = tomoyo_read_profile;
24169590837bSKentaro Takeda 		break;
241717fcfbd9STetsuo Handa 	case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
241817fcfbd9STetsuo Handa 		head->poll = tomoyo_poll_query;
241917fcfbd9STetsuo Handa 		head->write = tomoyo_write_answer;
242017fcfbd9STetsuo Handa 		head->read = tomoyo_read_query;
242117fcfbd9STetsuo Handa 		break;
24229590837bSKentaro Takeda 	case TOMOYO_MANAGER:
24239590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/manager */
2424e2bf6907STetsuo Handa 		head->write = tomoyo_write_manager;
2425e2bf6907STetsuo Handa 		head->read = tomoyo_read_manager;
24269590837bSKentaro Takeda 		break;
24279590837bSKentaro Takeda 	}
24289590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_READ)) {
24299590837bSKentaro Takeda 		/*
24309590837bSKentaro Takeda 		 * No need to allocate read_buf since it is not opened
24319590837bSKentaro Takeda 		 * for reading.
24329590837bSKentaro Takeda 		 */
24339590837bSKentaro Takeda 		head->read = NULL;
243417fcfbd9STetsuo Handa 		head->poll = NULL;
243517fcfbd9STetsuo Handa 	} else if (!head->poll) {
243617fcfbd9STetsuo Handa 		/* Don't allocate read_buf for poll() access. */
24379590837bSKentaro Takeda 		if (!head->readbuf_size)
24389590837bSKentaro Takeda 			head->readbuf_size = 4096 * 2;
24394e5d6f7eSTetsuo Handa 		head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
24409590837bSKentaro Takeda 		if (!head->read_buf) {
24418e2d39a1STetsuo Handa 			kfree(head);
24429590837bSKentaro Takeda 			return -ENOMEM;
24439590837bSKentaro Takeda 		}
24449590837bSKentaro Takeda 	}
24459590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_WRITE)) {
24469590837bSKentaro Takeda 		/*
24479590837bSKentaro Takeda 		 * No need to allocate write_buf since it is not opened
24489590837bSKentaro Takeda 		 * for writing.
24499590837bSKentaro Takeda 		 */
24509590837bSKentaro Takeda 		head->write = NULL;
24519590837bSKentaro Takeda 	} else if (head->write) {
24529590837bSKentaro Takeda 		head->writebuf_size = 4096 * 2;
24534e5d6f7eSTetsuo Handa 		head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
24549590837bSKentaro Takeda 		if (!head->write_buf) {
24558e2d39a1STetsuo Handa 			kfree(head->read_buf);
24568e2d39a1STetsuo Handa 			kfree(head);
24579590837bSKentaro Takeda 			return -ENOMEM;
24589590837bSKentaro Takeda 		}
24599590837bSKentaro Takeda 	}
24609590837bSKentaro Takeda 	/*
246117fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , increment the
246217fcfbd9STetsuo Handa 	 * observer counter.
246317fcfbd9STetsuo Handa 	 * The obserber counter is used by tomoyo_supervisor() to see if
246417fcfbd9STetsuo Handa 	 * there is some process monitoring /sys/kernel/security/tomoyo/query.
246517fcfbd9STetsuo Handa 	 */
24667c75964fSTetsuo Handa 	if (type == TOMOYO_QUERY)
246717fcfbd9STetsuo Handa 		atomic_inc(&tomoyo_query_observers);
24682e503bbbSTetsuo Handa 	file->private_data = head;
24692e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, true);
24709590837bSKentaro Takeda 	return 0;
24719590837bSKentaro Takeda }
24729590837bSKentaro Takeda 
24739590837bSKentaro Takeda /**
24740849e3baSTetsuo Handa  * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
24750849e3baSTetsuo Handa  *
24760849e3baSTetsuo Handa  * @file: Pointer to "struct file".
24770849e3baSTetsuo Handa  * @wait: Pointer to "poll_table".
24780849e3baSTetsuo Handa  *
24790849e3baSTetsuo Handa  * Waits for read readiness.
2480eadd99ccSTetsuo Handa  * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2481eadd99ccSTetsuo Handa  * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
24820849e3baSTetsuo Handa  */
24830849e3baSTetsuo Handa int tomoyo_poll_control(struct file *file, poll_table *wait)
24840849e3baSTetsuo Handa {
24850849e3baSTetsuo Handa 	struct tomoyo_io_buffer *head = file->private_data;
24860849e3baSTetsuo Handa 	if (!head->poll)
24870849e3baSTetsuo Handa 		return -ENOSYS;
24880849e3baSTetsuo Handa 	return head->poll(file, wait);
24890849e3baSTetsuo Handa }
24900849e3baSTetsuo Handa 
24910849e3baSTetsuo Handa /**
2492bd03a3e4STetsuo Handa  * tomoyo_set_namespace_cursor - Set namespace to read.
2493bd03a3e4STetsuo Handa  *
2494bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2495bd03a3e4STetsuo Handa  *
2496bd03a3e4STetsuo Handa  * Returns nothing.
2497bd03a3e4STetsuo Handa  */
2498bd03a3e4STetsuo Handa static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2499bd03a3e4STetsuo Handa {
2500bd03a3e4STetsuo Handa 	struct list_head *ns;
2501bd03a3e4STetsuo Handa 	if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2502bd03a3e4STetsuo Handa 	    head->type != TOMOYO_PROFILE)
2503bd03a3e4STetsuo Handa 		return;
2504bd03a3e4STetsuo Handa 	/*
2505bd03a3e4STetsuo Handa 	 * If this is the first read, or reading previous namespace finished
2506bd03a3e4STetsuo Handa 	 * and has more namespaces to read, update the namespace cursor.
2507bd03a3e4STetsuo Handa 	 */
2508bd03a3e4STetsuo Handa 	ns = head->r.ns;
2509bd03a3e4STetsuo Handa 	if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2510bd03a3e4STetsuo Handa 		/* Clearing is OK because tomoyo_flush() returned true. */
2511bd03a3e4STetsuo Handa 		memset(&head->r, 0, sizeof(head->r));
2512bd03a3e4STetsuo Handa 		head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2513bd03a3e4STetsuo Handa 	}
2514bd03a3e4STetsuo Handa }
2515bd03a3e4STetsuo Handa 
2516bd03a3e4STetsuo Handa /**
2517bd03a3e4STetsuo Handa  * tomoyo_has_more_namespace - Check for unread namespaces.
2518bd03a3e4STetsuo Handa  *
2519bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2520bd03a3e4STetsuo Handa  *
2521bd03a3e4STetsuo Handa  * Returns true if we have more entries to print, false otherwise.
2522bd03a3e4STetsuo Handa  */
2523bd03a3e4STetsuo Handa static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2524bd03a3e4STetsuo Handa {
2525bd03a3e4STetsuo Handa 	return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2526bd03a3e4STetsuo Handa 		head->type == TOMOYO_PROFILE) && head->r.eof &&
2527bd03a3e4STetsuo Handa 		head->r.ns->next != &tomoyo_namespace_list;
2528bd03a3e4STetsuo Handa }
2529bd03a3e4STetsuo Handa 
2530bd03a3e4STetsuo Handa /**
25319590837bSKentaro Takeda  * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
25329590837bSKentaro Takeda  *
25330df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25349590837bSKentaro Takeda  * @buffer:     Poiner to buffer to write to.
25359590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25369590837bSKentaro Takeda  *
25379590837bSKentaro Takeda  * Returns bytes read on success, negative value otherwise.
25389590837bSKentaro Takeda  */
25392c47ab93STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
25409590837bSKentaro Takeda 			    const int buffer_len)
25419590837bSKentaro Takeda {
2542f23571e8STetsuo Handa 	int len;
25432e503bbbSTetsuo Handa 	int idx;
25449590837bSKentaro Takeda 
25459590837bSKentaro Takeda 	if (!head->read)
25469590837bSKentaro Takeda 		return -ENOSYS;
25479590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25489590837bSKentaro Takeda 		return -EINTR;
2549f23571e8STetsuo Handa 	head->read_user_buf = buffer;
2550f23571e8STetsuo Handa 	head->read_user_buf_avail = buffer_len;
25512e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
2552f23571e8STetsuo Handa 	if (tomoyo_flush(head))
25539590837bSKentaro Takeda 		/* Call the policy handler. */
2554bd03a3e4STetsuo Handa 		do {
2555bd03a3e4STetsuo Handa 			tomoyo_set_namespace_cursor(head);
25568fbe71f0STetsuo Handa 			head->read(head);
2557bd03a3e4STetsuo Handa 		} while (tomoyo_flush(head) &&
2558bd03a3e4STetsuo Handa 			 tomoyo_has_more_namespace(head));
25592e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
2560f23571e8STetsuo Handa 	len = head->read_user_buf - buffer;
25619590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
25629590837bSKentaro Takeda 	return len;
25639590837bSKentaro Takeda }
25649590837bSKentaro Takeda 
25659590837bSKentaro Takeda /**
2566bd03a3e4STetsuo Handa  * tomoyo_parse_policy - Parse a policy line.
2567bd03a3e4STetsuo Handa  *
2568bd03a3e4STetsuo Handa  * @head: Poiter to "struct tomoyo_io_buffer".
2569bd03a3e4STetsuo Handa  * @line: Line to parse.
2570bd03a3e4STetsuo Handa  *
2571bd03a3e4STetsuo Handa  * Returns 0 on success, negative value otherwise.
2572bd03a3e4STetsuo Handa  *
2573bd03a3e4STetsuo Handa  * Caller holds tomoyo_read_lock().
2574bd03a3e4STetsuo Handa  */
2575bd03a3e4STetsuo Handa static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2576bd03a3e4STetsuo Handa {
2577bd03a3e4STetsuo Handa 	/* Delete request? */
2578bd03a3e4STetsuo Handa 	head->w.is_delete = !strncmp(line, "delete ", 7);
2579bd03a3e4STetsuo Handa 	if (head->w.is_delete)
2580bd03a3e4STetsuo Handa 		memmove(line, line + 7, strlen(line + 7) + 1);
2581bd03a3e4STetsuo Handa 	/* Selecting namespace to update. */
2582bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2583bd03a3e4STetsuo Handa 	    head->type == TOMOYO_PROFILE) {
2584bd03a3e4STetsuo Handa 		if (*line == '<') {
2585bd03a3e4STetsuo Handa 			char *cp = strchr(line, ' ');
2586bd03a3e4STetsuo Handa 			if (cp) {
2587bd03a3e4STetsuo Handa 				*cp++ = '\0';
2588bd03a3e4STetsuo Handa 				head->w.ns = tomoyo_assign_namespace(line);
2589bd03a3e4STetsuo Handa 				memmove(line, cp, strlen(cp) + 1);
2590bd03a3e4STetsuo Handa 			} else
2591bd03a3e4STetsuo Handa 				head->w.ns = NULL;
2592bd03a3e4STetsuo Handa 		} else
2593bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2594bd03a3e4STetsuo Handa 		/* Don't allow updating if namespace is invalid. */
2595bd03a3e4STetsuo Handa 		if (!head->w.ns)
2596bd03a3e4STetsuo Handa 			return -ENOENT;
2597bd03a3e4STetsuo Handa 	}
2598bd03a3e4STetsuo Handa 	/* Do the update. */
2599bd03a3e4STetsuo Handa 	return head->write(head);
2600bd03a3e4STetsuo Handa }
2601bd03a3e4STetsuo Handa 
2602bd03a3e4STetsuo Handa /**
26039590837bSKentaro Takeda  * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
26049590837bSKentaro Takeda  *
26050df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
26069590837bSKentaro Takeda  * @buffer:     Pointer to buffer to read from.
26079590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
26089590837bSKentaro Takeda  *
26099590837bSKentaro Takeda  * Returns @buffer_len on success, negative value otherwise.
26109590837bSKentaro Takeda  */
26112c47ab93STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
26120df7e8b8STetsuo Handa 			     const char __user *buffer, const int buffer_len)
26139590837bSKentaro Takeda {
26149590837bSKentaro Takeda 	int error = buffer_len;
2615bd03a3e4STetsuo Handa 	size_t avail_len = buffer_len;
26169590837bSKentaro Takeda 	char *cp0 = head->write_buf;
26172e503bbbSTetsuo Handa 	int idx;
26189590837bSKentaro Takeda 	if (!head->write)
26199590837bSKentaro Takeda 		return -ENOSYS;
26209590837bSKentaro Takeda 	if (!access_ok(VERIFY_READ, buffer, buffer_len))
26219590837bSKentaro Takeda 		return -EFAULT;
26229590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
26239590837bSKentaro Takeda 		return -EINTR;
2624e0b057b4STetsuo Handa 	head->read_user_buf_avail = 0;
26252e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
26269590837bSKentaro Takeda 	/* Read a line and dispatch it to the policy handler. */
26279590837bSKentaro Takeda 	while (avail_len > 0) {
26289590837bSKentaro Takeda 		char c;
26290df7e8b8STetsuo Handa 		if (head->w.avail >= head->writebuf_size - 1) {
2630bd03a3e4STetsuo Handa 			const int len = head->writebuf_size * 2;
2631bd03a3e4STetsuo Handa 			char *cp = kzalloc(len, GFP_NOFS);
2632bd03a3e4STetsuo Handa 			if (!cp) {
26339590837bSKentaro Takeda 				error = -ENOMEM;
26349590837bSKentaro Takeda 				break;
2635bd03a3e4STetsuo Handa 			}
2636bd03a3e4STetsuo Handa 			memmove(cp, cp0, head->w.avail);
2637bd03a3e4STetsuo Handa 			kfree(cp0);
2638bd03a3e4STetsuo Handa 			head->write_buf = cp;
2639bd03a3e4STetsuo Handa 			cp0 = cp;
2640bd03a3e4STetsuo Handa 			head->writebuf_size = len;
2641bd03a3e4STetsuo Handa 		}
2642bd03a3e4STetsuo Handa 		if (get_user(c, buffer)) {
26439590837bSKentaro Takeda 			error = -EFAULT;
26449590837bSKentaro Takeda 			break;
26459590837bSKentaro Takeda 		}
26469590837bSKentaro Takeda 		buffer++;
26479590837bSKentaro Takeda 		avail_len--;
26480df7e8b8STetsuo Handa 		cp0[head->w.avail++] = c;
26499590837bSKentaro Takeda 		if (c != '\n')
26509590837bSKentaro Takeda 			continue;
26510df7e8b8STetsuo Handa 		cp0[head->w.avail - 1] = '\0';
26520df7e8b8STetsuo Handa 		head->w.avail = 0;
26539590837bSKentaro Takeda 		tomoyo_normalize_line(cp0);
2654bd03a3e4STetsuo Handa 		if (!strcmp(cp0, "reset")) {
2655bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2656bd03a3e4STetsuo Handa 			head->w.domain = NULL;
2657bd03a3e4STetsuo Handa 			memset(&head->r, 0, sizeof(head->r));
2658bd03a3e4STetsuo Handa 			continue;
26599590837bSKentaro Takeda 		}
2660bd03a3e4STetsuo Handa 		/* Don't allow updating policies by non manager programs. */
2661bd03a3e4STetsuo Handa 		switch (head->type) {
2662bd03a3e4STetsuo Handa 		case TOMOYO_PROCESS_STATUS:
2663bd03a3e4STetsuo Handa 			/* This does not write anything. */
2664bd03a3e4STetsuo Handa 			break;
2665bd03a3e4STetsuo Handa 		case TOMOYO_DOMAINPOLICY:
2666bd03a3e4STetsuo Handa 			if (tomoyo_select_domain(head, cp0))
2667bd03a3e4STetsuo Handa 				continue;
2668bd03a3e4STetsuo Handa 			/* fall through */
2669bd03a3e4STetsuo Handa 		case TOMOYO_EXCEPTIONPOLICY:
2670bd03a3e4STetsuo Handa 			if (!strcmp(cp0, "select transition_only")) {
2671bd03a3e4STetsuo Handa 				head->r.print_transition_related_only = true;
2672bd03a3e4STetsuo Handa 				continue;
2673bd03a3e4STetsuo Handa 			}
2674bd03a3e4STetsuo Handa 			/* fall through */
2675bd03a3e4STetsuo Handa 		default:
2676bd03a3e4STetsuo Handa 			if (!tomoyo_manager()) {
2677bd03a3e4STetsuo Handa 				error = -EPERM;
2678bd03a3e4STetsuo Handa 				goto out;
2679bd03a3e4STetsuo Handa 			}
2680bd03a3e4STetsuo Handa 		}
2681bd03a3e4STetsuo Handa 		switch (tomoyo_parse_policy(head, cp0)) {
2682bd03a3e4STetsuo Handa 		case -EPERM:
2683bd03a3e4STetsuo Handa 			error = -EPERM;
2684bd03a3e4STetsuo Handa 			goto out;
2685b22b8b9fSTetsuo Handa 		case 0:
2686b22b8b9fSTetsuo Handa 			switch (head->type) {
2687b22b8b9fSTetsuo Handa 			case TOMOYO_DOMAINPOLICY:
2688b22b8b9fSTetsuo Handa 			case TOMOYO_EXCEPTIONPOLICY:
2689b22b8b9fSTetsuo Handa 			case TOMOYO_STAT:
2690b22b8b9fSTetsuo Handa 			case TOMOYO_PROFILE:
2691b22b8b9fSTetsuo Handa 			case TOMOYO_MANAGER:
2692b22b8b9fSTetsuo Handa 				tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2693b22b8b9fSTetsuo Handa 				break;
2694b22b8b9fSTetsuo Handa 			default:
2695b22b8b9fSTetsuo Handa 				break;
2696b22b8b9fSTetsuo Handa 			}
2697b22b8b9fSTetsuo Handa 			break;
2698bd03a3e4STetsuo Handa 		}
2699bd03a3e4STetsuo Handa 	}
2700bd03a3e4STetsuo Handa out:
27012e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
27029590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
27039590837bSKentaro Takeda 	return error;
27049590837bSKentaro Takeda }
27059590837bSKentaro Takeda 
27069590837bSKentaro Takeda /**
27079590837bSKentaro Takeda  * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
27089590837bSKentaro Takeda  *
27090df7e8b8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
27109590837bSKentaro Takeda  *
27112e503bbbSTetsuo Handa  * Returns 0.
27129590837bSKentaro Takeda  */
27130df7e8b8STetsuo Handa int tomoyo_close_control(struct tomoyo_io_buffer *head)
27149590837bSKentaro Takeda {
271517fcfbd9STetsuo Handa 	/*
271617fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , decrement the
271717fcfbd9STetsuo Handa 	 * observer counter.
271817fcfbd9STetsuo Handa 	 */
27192e503bbbSTetsuo Handa 	if (head->type == TOMOYO_QUERY &&
27202e503bbbSTetsuo Handa 	    atomic_dec_and_test(&tomoyo_query_observers))
27212e503bbbSTetsuo Handa 		wake_up_all(&tomoyo_answer_wait);
27222e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, false);
27239590837bSKentaro Takeda 	return 0;
27249590837bSKentaro Takeda }
27259590837bSKentaro Takeda 
27269590837bSKentaro Takeda /**
2727c3ef1500STetsuo Handa  * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
27289590837bSKentaro Takeda  */
2729c3ef1500STetsuo Handa void tomoyo_check_profile(void)
27309590837bSKentaro Takeda {
2731c3ef1500STetsuo Handa 	struct tomoyo_domain_info *domain;
2732c3ef1500STetsuo Handa 	const int idx = tomoyo_read_lock();
2733c3ef1500STetsuo Handa 	tomoyo_policy_loaded = true;
2734843d183cSTetsuo Handa 	printk(KERN_INFO "TOMOYO: 2.5.0\n");
2735c3ef1500STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2736c3ef1500STetsuo Handa 		const u8 profile = domain->profile;
2737bd03a3e4STetsuo Handa 		const struct tomoyo_policy_namespace *ns = domain->ns;
2738843d183cSTetsuo Handa 		if (ns->profile_version != 20110903)
2739bd03a3e4STetsuo Handa 			printk(KERN_ERR
2740bd03a3e4STetsuo Handa 			       "Profile version %u is not supported.\n",
2741bd03a3e4STetsuo Handa 			       ns->profile_version);
2742bd03a3e4STetsuo Handa 		else if (!ns->profile_ptr[profile])
2743bd03a3e4STetsuo Handa 			printk(KERN_ERR
2744bd03a3e4STetsuo Handa 			       "Profile %u (used by '%s') is not defined.\n",
2745c3ef1500STetsuo Handa 			       profile, domain->domainname->name);
2746bd03a3e4STetsuo Handa 		else
2747bd03a3e4STetsuo Handa 			continue;
2748bd03a3e4STetsuo Handa 		printk(KERN_ERR
2749843d183cSTetsuo Handa 		       "Userland tools for TOMOYO 2.5 must be installed and "
2750bd03a3e4STetsuo Handa 		       "policy must be initialized.\n");
2751843d183cSTetsuo Handa 		printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
2752bd03a3e4STetsuo Handa 		       "for more information.\n");
2753bd03a3e4STetsuo Handa 		panic("STOP!");
27549590837bSKentaro Takeda 	}
2755c3ef1500STetsuo Handa 	tomoyo_read_unlock(idx);
2756c3ef1500STetsuo Handa 	printk(KERN_INFO "Mandatory Access Control activated.\n");
27579590837bSKentaro Takeda }
2758efe836abSTetsuo Handa 
2759efe836abSTetsuo Handa /**
2760efe836abSTetsuo Handa  * tomoyo_load_builtin_policy - Load built-in policy.
2761efe836abSTetsuo Handa  *
2762efe836abSTetsuo Handa  * Returns nothing.
2763efe836abSTetsuo Handa  */
2764efe836abSTetsuo Handa void __init tomoyo_load_builtin_policy(void)
2765efe836abSTetsuo Handa {
2766efe836abSTetsuo Handa 	/*
2767efe836abSTetsuo Handa 	 * This include file is manually created and contains built-in policy
2768efe836abSTetsuo Handa 	 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2769efe836abSTetsuo Handa 	 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2770efe836abSTetsuo Handa 	 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2771efe836abSTetsuo Handa 	 */
2772efe836abSTetsuo Handa #include "builtin-policy.h"
2773efe836abSTetsuo Handa 	u8 i;
2774efe836abSTetsuo Handa 	const int idx = tomoyo_read_lock();
2775efe836abSTetsuo Handa 	for (i = 0; i < 5; i++) {
2776efe836abSTetsuo Handa 		struct tomoyo_io_buffer head = { };
2777efe836abSTetsuo Handa 		char *start = "";
2778efe836abSTetsuo Handa 		switch (i) {
2779efe836abSTetsuo Handa 		case 0:
2780efe836abSTetsuo Handa 			start = tomoyo_builtin_profile;
2781efe836abSTetsuo Handa 			head.type = TOMOYO_PROFILE;
2782efe836abSTetsuo Handa 			head.write = tomoyo_write_profile;
2783efe836abSTetsuo Handa 			break;
2784efe836abSTetsuo Handa 		case 1:
2785efe836abSTetsuo Handa 			start = tomoyo_builtin_exception_policy;
2786efe836abSTetsuo Handa 			head.type = TOMOYO_EXCEPTIONPOLICY;
2787efe836abSTetsuo Handa 			head.write = tomoyo_write_exception;
2788efe836abSTetsuo Handa 			break;
2789efe836abSTetsuo Handa 		case 2:
2790efe836abSTetsuo Handa 			start = tomoyo_builtin_domain_policy;
2791efe836abSTetsuo Handa 			head.type = TOMOYO_DOMAINPOLICY;
2792efe836abSTetsuo Handa 			head.write = tomoyo_write_domain;
2793efe836abSTetsuo Handa 			break;
2794efe836abSTetsuo Handa 		case 3:
2795efe836abSTetsuo Handa 			start = tomoyo_builtin_manager;
2796efe836abSTetsuo Handa 			head.type = TOMOYO_MANAGER;
2797efe836abSTetsuo Handa 			head.write = tomoyo_write_manager;
2798efe836abSTetsuo Handa 			break;
2799efe836abSTetsuo Handa 		case 4:
2800efe836abSTetsuo Handa 			start = tomoyo_builtin_stat;
2801efe836abSTetsuo Handa 			head.type = TOMOYO_STAT;
2802efe836abSTetsuo Handa 			head.write = tomoyo_write_stat;
2803efe836abSTetsuo Handa 			break;
2804efe836abSTetsuo Handa 		}
2805efe836abSTetsuo Handa 		while (1) {
2806efe836abSTetsuo Handa 			char *end = strchr(start, '\n');
2807efe836abSTetsuo Handa 			if (!end)
2808efe836abSTetsuo Handa 				break;
2809efe836abSTetsuo Handa 			*end = '\0';
2810efe836abSTetsuo Handa 			tomoyo_normalize_line(start);
2811efe836abSTetsuo Handa 			head.write_buf = start;
2812efe836abSTetsuo Handa 			tomoyo_parse_policy(&head, start);
2813efe836abSTetsuo Handa 			start = end + 1;
2814efe836abSTetsuo Handa 		}
2815efe836abSTetsuo Handa 	}
2816efe836abSTetsuo Handa 	tomoyo_read_unlock(idx);
28170e4ae0e0STetsuo Handa #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
28180e4ae0e0STetsuo Handa 	tomoyo_check_profile();
28190e4ae0e0STetsuo Handa #endif
2820efe836abSTetsuo Handa }
2821