xref: /linux/security/tomoyo/common.c (revision e53cfda5d2c90a6dd763eb72034c775add729e40)
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;
7514d818971STetsuo Handa 		if (head->r.index == TOMOYO_MAX_PROFILES) {
7524d818971STetsuo Handa 			head->r.eof = true;
753f23571e8STetsuo Handa 			return;
7544d818971STetsuo Handa 		}
755f23571e8STetsuo Handa 		head->r.step++;
756f23571e8STetsuo Handa 		break;
757f23571e8STetsuo Handa 	case 2:
758f23571e8STetsuo Handa 		{
759d5ca1725STetsuo Handa 			u8 i;
760f23571e8STetsuo Handa 			const struct tomoyo_path_info *comment =
761f23571e8STetsuo Handa 				profile->comment;
762bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
763f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-COMMENT=", index);
764f23571e8STetsuo Handa 			tomoyo_set_string(head, comment ? comment->name : "");
765f23571e8STetsuo Handa 			tomoyo_set_lf(head);
7664d818971STetsuo Handa 			tomoyo_print_namespace(head);
767d5ca1725STetsuo Handa 			tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
768d5ca1725STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_PREF; i++)
769d5ca1725STetsuo Handa 				tomoyo_io_printf(head, "%s=%u ",
770d5ca1725STetsuo Handa 						 tomoyo_pref_keywords[i],
771d5ca1725STetsuo Handa 						 profile->pref[i]);
772d5ca1725STetsuo Handa 			tomoyo_set_string(head, "}\n");
773f23571e8STetsuo Handa 			head->r.step++;
774f23571e8STetsuo Handa 		}
775f23571e8STetsuo Handa 		break;
776f23571e8STetsuo Handa 	case 3:
777f23571e8STetsuo Handa 		{
778bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
779f23571e8STetsuo Handa 			tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
780f23571e8STetsuo Handa 			tomoyo_print_config(head, profile->default_config);
781f23571e8STetsuo Handa 			head->r.bit = 0;
782f23571e8STetsuo Handa 			head->r.step++;
783f23571e8STetsuo Handa 		}
784f23571e8STetsuo Handa 		break;
785f23571e8STetsuo Handa 	case 4:
786f23571e8STetsuo Handa 		for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
787f23571e8STetsuo Handa 			      + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
788f23571e8STetsuo Handa 			const u8 i = head->r.bit;
789f23571e8STetsuo Handa 			const u8 config = profile->config[i];
79057c2590fSTetsuo Handa 			if (config == TOMOYO_CONFIG_USE_DEFAULT)
7919590837bSKentaro Takeda 				continue;
792bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
7932c47ab93STetsuo Handa 			if (i < TOMOYO_MAX_MAC_INDEX)
7942c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
7952c47ab93STetsuo Handa 						 index,
7962c47ab93STetsuo Handa 						 tomoyo_category_keywords
7972c47ab93STetsuo Handa 						 [tomoyo_index2category[i]],
7982c47ab93STetsuo Handa 						 tomoyo_mac_keywords[i]);
7992c47ab93STetsuo Handa 			else
8002c47ab93STetsuo Handa 				tomoyo_io_printf(head, "%u-CONFIG::%s", index,
801f23571e8STetsuo Handa 						 tomoyo_mac_keywords[i]);
802f23571e8STetsuo Handa 			tomoyo_print_config(head, config);
803f23571e8STetsuo Handa 			head->r.bit++;
8049590837bSKentaro Takeda 			break;
8059590837bSKentaro Takeda 		}
806f23571e8STetsuo Handa 		if (head->r.bit == TOMOYO_MAX_MAC_INDEX
807f23571e8STetsuo Handa 		    + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
808f23571e8STetsuo Handa 			head->r.index++;
809f23571e8STetsuo Handa 			head->r.step = 1;
810f23571e8STetsuo Handa 		}
811f23571e8STetsuo Handa 		break;
812f23571e8STetsuo Handa 	}
813f23571e8STetsuo Handa 	if (tomoyo_flush(head))
814f23571e8STetsuo Handa 		goto next;
8159590837bSKentaro Takeda }
8169590837bSKentaro Takeda 
8170f2a55d5STetsuo Handa /**
8180f2a55d5STetsuo Handa  * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
8190f2a55d5STetsuo Handa  *
8200f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
8210f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
8220f2a55d5STetsuo Handa  *
8230f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
8240f2a55d5STetsuo Handa  */
825e2bf6907STetsuo Handa static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
82636f5e1ffSTetsuo Handa 				const struct tomoyo_acl_head *b)
82736f5e1ffSTetsuo Handa {
828e2bf6907STetsuo Handa 	return container_of(a, struct tomoyo_manager, head)->manager ==
829e2bf6907STetsuo Handa 		container_of(b, struct tomoyo_manager, head)->manager;
83036f5e1ffSTetsuo Handa }
83136f5e1ffSTetsuo Handa 
8329590837bSKentaro Takeda /**
8339590837bSKentaro Takeda  * tomoyo_update_manager_entry - Add a manager entry.
8349590837bSKentaro Takeda  *
8359590837bSKentaro Takeda  * @manager:   The path to manager or the domainnamme.
8369590837bSKentaro Takeda  * @is_delete: True if it is a delete request.
8379590837bSKentaro Takeda  *
8389590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
839fdb8ebb7STetsuo Handa  *
840fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8419590837bSKentaro Takeda  */
8429590837bSKentaro Takeda static int tomoyo_update_manager_entry(const char *manager,
8439590837bSKentaro Takeda 				       const bool is_delete)
8449590837bSKentaro Takeda {
845e2bf6907STetsuo Handa 	struct tomoyo_manager e = { };
846a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
847bd03a3e4STetsuo Handa 		/* .ns = &tomoyo_kernel_namespace, */
848a238cf5bSTetsuo Handa 		.is_delete = is_delete,
849bd03a3e4STetsuo Handa 		.list = &tomoyo_kernel_namespace.
850bd03a3e4STetsuo Handa 		policy_list[TOMOYO_ID_MANAGER],
851a238cf5bSTetsuo Handa 	};
852a238cf5bSTetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
85377b513ddSTetsuo Handa 	if (!tomoyo_correct_domain(manager) &&
85477b513ddSTetsuo Handa 	    !tomoyo_correct_word(manager))
8559590837bSKentaro Takeda 		return -EINVAL;
8569e4b50e9STetsuo Handa 	e.manager = tomoyo_get_name(manager);
857a238cf5bSTetsuo Handa 	if (e.manager) {
858a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), &param,
859e2bf6907STetsuo Handa 					     tomoyo_same_manager);
8609e4b50e9STetsuo Handa 		tomoyo_put_name(e.manager);
861a238cf5bSTetsuo Handa 	}
8629590837bSKentaro Takeda 	return error;
8639590837bSKentaro Takeda }
8649590837bSKentaro Takeda 
8659590837bSKentaro Takeda /**
866e2bf6907STetsuo Handa  * tomoyo_write_manager - Write manager policy.
8679590837bSKentaro Takeda  *
8689590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8699590837bSKentaro Takeda  *
8709590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
871fdb8ebb7STetsuo Handa  *
872fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8739590837bSKentaro Takeda  */
874e2bf6907STetsuo Handa static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
8759590837bSKentaro Takeda {
8769590837bSKentaro Takeda 	char *data = head->write_buf;
8779590837bSKentaro Takeda 
8789590837bSKentaro Takeda 	if (!strcmp(data, "manage_by_non_root")) {
879bd03a3e4STetsuo Handa 		tomoyo_manage_by_non_root = !head->w.is_delete;
8809590837bSKentaro Takeda 		return 0;
8819590837bSKentaro Takeda 	}
882bd03a3e4STetsuo Handa 	return tomoyo_update_manager_entry(data, head->w.is_delete);
8839590837bSKentaro Takeda }
8849590837bSKentaro Takeda 
8859590837bSKentaro Takeda /**
886e2bf6907STetsuo Handa  * tomoyo_read_manager - Read manager policy.
8879590837bSKentaro Takeda  *
8889590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
8899590837bSKentaro Takeda  *
890fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
8919590837bSKentaro Takeda  */
892e2bf6907STetsuo Handa static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
8939590837bSKentaro Takeda {
894f23571e8STetsuo Handa 	if (head->r.eof)
8958fbe71f0STetsuo Handa 		return;
896bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
897bd03a3e4STetsuo Handa 			     policy_list[TOMOYO_ID_MANAGER]) {
898e2bf6907STetsuo Handa 		struct tomoyo_manager *ptr =
899f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), head.list);
90082e0f001STetsuo Handa 		if (ptr->head.is_deleted)
9019590837bSKentaro Takeda 			continue;
902f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
903f23571e8STetsuo Handa 			return;
904f23571e8STetsuo Handa 		tomoyo_set_string(head, ptr->manager->name);
905f23571e8STetsuo Handa 		tomoyo_set_lf(head);
9069590837bSKentaro Takeda 	}
907f23571e8STetsuo Handa 	head->r.eof = true;
9089590837bSKentaro Takeda }
9099590837bSKentaro Takeda 
9109590837bSKentaro Takeda /**
911e2bf6907STetsuo Handa  * tomoyo_manager - Check whether the current process is a policy manager.
9129590837bSKentaro Takeda  *
9139590837bSKentaro Takeda  * Returns true if the current process is permitted to modify policy
9149590837bSKentaro Takeda  * via /sys/kernel/security/tomoyo/ interface.
915fdb8ebb7STetsuo Handa  *
916fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9179590837bSKentaro Takeda  */
918e2bf6907STetsuo Handa static bool tomoyo_manager(void)
9199590837bSKentaro Takeda {
920e2bf6907STetsuo Handa 	struct tomoyo_manager *ptr;
9219590837bSKentaro Takeda 	const char *exe;
9229590837bSKentaro Takeda 	const struct task_struct *task = current;
9239590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
9249590837bSKentaro Takeda 	bool found = false;
9259590837bSKentaro Takeda 
9269590837bSKentaro Takeda 	if (!tomoyo_policy_loaded)
9279590837bSKentaro Takeda 		return true;
928609fcd1bSEric W. Biederman 	if (!tomoyo_manage_by_non_root &&
929609fcd1bSEric W. Biederman 	    (!uid_eq(task->cred->uid,  GLOBAL_ROOT_UID) ||
930609fcd1bSEric W. Biederman 	     !uid_eq(task->cred->euid, GLOBAL_ROOT_UID)))
9319590837bSKentaro Takeda 		return false;
9329590837bSKentaro Takeda 	exe = tomoyo_get_exe();
9339590837bSKentaro Takeda 	if (!exe)
9349590837bSKentaro Takeda 		return false;
935bd03a3e4STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
936bd03a3e4STetsuo Handa 				policy_list[TOMOYO_ID_MANAGER], head.list) {
93777b513ddSTetsuo Handa 		if (!ptr->head.is_deleted &&
93877b513ddSTetsuo Handa 		    (!tomoyo_pathcmp(domainname, ptr->manager) ||
93977b513ddSTetsuo Handa 		     !strcmp(exe, ptr->manager->name))) {
9409590837bSKentaro Takeda 			found = true;
9419590837bSKentaro Takeda 			break;
9429590837bSKentaro Takeda 		}
9439590837bSKentaro Takeda 	}
9449590837bSKentaro Takeda 	if (!found) { /* Reduce error messages. */
9459590837bSKentaro Takeda 		static pid_t last_pid;
9469590837bSKentaro Takeda 		const pid_t pid = current->pid;
9479590837bSKentaro Takeda 		if (last_pid != pid) {
9489590837bSKentaro Takeda 			printk(KERN_WARNING "%s ( %s ) is not permitted to "
9499590837bSKentaro Takeda 			       "update policies.\n", domainname->name, exe);
9509590837bSKentaro Takeda 			last_pid = pid;
9519590837bSKentaro Takeda 		}
9529590837bSKentaro Takeda 	}
9538e2d39a1STetsuo Handa 	kfree(exe);
9549590837bSKentaro Takeda 	return found;
9559590837bSKentaro Takeda }
9569590837bSKentaro Takeda 
95759df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
95859df3166STetsuo Handa (unsigned int serial);
95959df3166STetsuo Handa 
9609590837bSKentaro Takeda /**
961bd03a3e4STetsuo Handa  * tomoyo_select_domain - Parse select command.
9629590837bSKentaro Takeda  *
9639590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
9649590837bSKentaro Takeda  * @data: String to parse.
9659590837bSKentaro Takeda  *
9669590837bSKentaro Takeda  * Returns true on success, false otherwise.
967fdb8ebb7STetsuo Handa  *
968fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
9699590837bSKentaro Takeda  */
970bd03a3e4STetsuo Handa static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
971bd03a3e4STetsuo Handa 				 const char *data)
9729590837bSKentaro Takeda {
9739590837bSKentaro Takeda 	unsigned int pid;
9749590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
9759b244373STetsuo Handa 	bool global_pid = false;
976bd03a3e4STetsuo Handa 	if (strncmp(data, "select ", 7))
977bd03a3e4STetsuo Handa 		return false;
978bd03a3e4STetsuo Handa 	data += 7;
9799b244373STetsuo Handa 	if (sscanf(data, "pid=%u", &pid) == 1 ||
9809b244373STetsuo Handa 	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9819590837bSKentaro Takeda 		struct task_struct *p;
9821fcdc7c5STetsuo Handa 		rcu_read_lock();
9839b244373STetsuo Handa 		if (global_pid)
9849b244373STetsuo Handa 			p = find_task_by_pid_ns(pid, &init_pid_ns);
9859b244373STetsuo Handa 		else
9869590837bSKentaro Takeda 			p = find_task_by_vpid(pid);
9879590837bSKentaro Takeda 		if (p)
9889590837bSKentaro Takeda 			domain = tomoyo_real_domain(p);
9891fcdc7c5STetsuo Handa 		rcu_read_unlock();
9909590837bSKentaro Takeda 	} else if (!strncmp(data, "domain=", 7)) {
99175093152STetsuo Handa 		if (tomoyo_domain_def(data + 7))
9929590837bSKentaro Takeda 			domain = tomoyo_find_domain(data + 7);
99359df3166STetsuo Handa 	} else if (sscanf(data, "Q=%u", &pid) == 1) {
99459df3166STetsuo Handa 		domain = tomoyo_find_domain_by_qid(pid);
9959590837bSKentaro Takeda 	} else
9969590837bSKentaro Takeda 		return false;
9970df7e8b8STetsuo Handa 	head->w.domain = domain;
9989590837bSKentaro Takeda 	/* Accessing read_buf is safe because head->io_sem is held. */
9999590837bSKentaro Takeda 	if (!head->read_buf)
10009590837bSKentaro Takeda 		return true; /* Do nothing if open(O_WRONLY). */
1001f23571e8STetsuo Handa 	memset(&head->r, 0, sizeof(head->r));
1002f23571e8STetsuo Handa 	head->r.print_this_domain_only = true;
100368eda8f5SDan Carpenter 	if (domain)
1004f23571e8STetsuo Handa 		head->r.domain = &domain->list;
100568eda8f5SDan Carpenter 	else
100668eda8f5SDan Carpenter 		head->r.eof = 1;
10079590837bSKentaro Takeda 	tomoyo_io_printf(head, "# select %s\n", data);
1008475e6fa3STetsuo Handa 	if (domain && domain->is_deleted)
10099590837bSKentaro Takeda 		tomoyo_io_printf(head, "# This is a deleted domain.\n");
10109590837bSKentaro Takeda 	return true;
10119590837bSKentaro Takeda }
10129590837bSKentaro Takeda 
10139590837bSKentaro Takeda /**
1014731d37aaSTetsuo Handa  * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1015731d37aaSTetsuo Handa  *
1016731d37aaSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_info".
1017731d37aaSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_info".
1018731d37aaSTetsuo Handa  *
1019731d37aaSTetsuo Handa  * Returns true if @a == @b, false otherwise.
1020731d37aaSTetsuo Handa  */
1021731d37aaSTetsuo Handa static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1022731d37aaSTetsuo Handa 			      const struct tomoyo_acl_info *b)
1023731d37aaSTetsuo Handa {
1024731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1025731d37aaSTetsuo Handa 	const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1026731d37aaSTetsuo Handa 	return p1->domainname == p2->domainname;
1027731d37aaSTetsuo Handa }
1028731d37aaSTetsuo Handa 
1029731d37aaSTetsuo Handa /**
1030731d37aaSTetsuo Handa  * tomoyo_write_task - Update task related list.
1031731d37aaSTetsuo Handa  *
1032731d37aaSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
1033731d37aaSTetsuo Handa  *
1034731d37aaSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1035731d37aaSTetsuo Handa  *
1036731d37aaSTetsuo Handa  * Caller holds tomoyo_read_lock().
1037731d37aaSTetsuo Handa  */
1038731d37aaSTetsuo Handa static int tomoyo_write_task(struct tomoyo_acl_param *param)
1039731d37aaSTetsuo Handa {
1040731d37aaSTetsuo Handa 	int error = -EINVAL;
1041731d37aaSTetsuo Handa 	if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1042731d37aaSTetsuo Handa 		struct tomoyo_task_acl e = {
1043731d37aaSTetsuo Handa 			.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1044731d37aaSTetsuo Handa 			.domainname = tomoyo_get_domainname(param),
1045731d37aaSTetsuo Handa 		};
1046731d37aaSTetsuo Handa 		if (e.domainname)
1047731d37aaSTetsuo Handa 			error = tomoyo_update_domain(&e.head, sizeof(e), param,
1048731d37aaSTetsuo Handa 						     tomoyo_same_task_acl,
1049731d37aaSTetsuo Handa 						     NULL);
1050731d37aaSTetsuo Handa 		tomoyo_put_name(e.domainname);
1051731d37aaSTetsuo Handa 	}
1052731d37aaSTetsuo Handa 	return error;
1053731d37aaSTetsuo Handa }
1054731d37aaSTetsuo Handa 
1055731d37aaSTetsuo Handa /**
1056ccf135f5STetsuo Handa  * tomoyo_delete_domain - Delete a domain.
1057ccf135f5STetsuo Handa  *
1058ccf135f5STetsuo Handa  * @domainname: The name of domain.
1059ccf135f5STetsuo Handa  *
10607d7473dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
1061fdb8ebb7STetsuo Handa  *
1062fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1063ccf135f5STetsuo Handa  */
1064ccf135f5STetsuo Handa static int tomoyo_delete_domain(char *domainname)
1065ccf135f5STetsuo Handa {
1066ccf135f5STetsuo Handa 	struct tomoyo_domain_info *domain;
1067ccf135f5STetsuo Handa 	struct tomoyo_path_info name;
1068ccf135f5STetsuo Handa 
1069ccf135f5STetsuo Handa 	name.name = domainname;
1070ccf135f5STetsuo Handa 	tomoyo_fill_path_info(&name);
107129282381STetsuo Handa 	if (mutex_lock_interruptible(&tomoyo_policy_lock))
10727d7473dbSTetsuo Handa 		return -EINTR;
1073ccf135f5STetsuo Handa 	/* Is there an active domain? */
1074fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1075ccf135f5STetsuo Handa 		/* Never delete tomoyo_kernel_domain */
1076ccf135f5STetsuo Handa 		if (domain == &tomoyo_kernel_domain)
1077ccf135f5STetsuo Handa 			continue;
1078ccf135f5STetsuo Handa 		if (domain->is_deleted ||
1079ccf135f5STetsuo Handa 		    tomoyo_pathcmp(domain->domainname, &name))
1080ccf135f5STetsuo Handa 			continue;
1081ccf135f5STetsuo Handa 		domain->is_deleted = true;
1082ccf135f5STetsuo Handa 		break;
1083ccf135f5STetsuo Handa 	}
1084f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
1085ccf135f5STetsuo Handa 	return 0;
1086ccf135f5STetsuo Handa }
1087ccf135f5STetsuo Handa 
1088ccf135f5STetsuo Handa /**
1089e2bf6907STetsuo Handa  * tomoyo_write_domain2 - Write domain policy.
109017fcfbd9STetsuo Handa  *
1091bd03a3e4STetsuo Handa  * @ns:        Pointer to "struct tomoyo_policy_namespace".
1092a238cf5bSTetsuo Handa  * @list:      Pointer to "struct list_head".
1093a238cf5bSTetsuo Handa  * @data:      Policy to be interpreted.
1094a238cf5bSTetsuo Handa  * @is_delete: True if it is a delete request.
109517fcfbd9STetsuo Handa  *
109617fcfbd9STetsuo Handa  * Returns 0 on success, negative value otherwise.
109717fcfbd9STetsuo Handa  *
109817fcfbd9STetsuo Handa  * Caller holds tomoyo_read_lock().
109917fcfbd9STetsuo Handa  */
1100bd03a3e4STetsuo Handa static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1101bd03a3e4STetsuo Handa 				struct list_head *list, char *data,
110217fcfbd9STetsuo Handa 				const bool is_delete)
110317fcfbd9STetsuo Handa {
1104a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1105bd03a3e4STetsuo Handa 		.ns = ns,
1106a238cf5bSTetsuo Handa 		.list = list,
1107a238cf5bSTetsuo Handa 		.data = data,
1108a238cf5bSTetsuo Handa 		.is_delete = is_delete,
1109a238cf5bSTetsuo Handa 	};
1110a238cf5bSTetsuo Handa 	static const struct {
1111a238cf5bSTetsuo Handa 		const char *keyword;
1112a238cf5bSTetsuo Handa 		int (*write) (struct tomoyo_acl_param *);
1113731d37aaSTetsuo Handa 	} tomoyo_callback[5] = {
1114a238cf5bSTetsuo Handa 		{ "file ", tomoyo_write_file },
1115059d84dbSTetsuo Handa 		{ "network inet ", tomoyo_write_inet_network },
1116059d84dbSTetsuo Handa 		{ "network unix ", tomoyo_write_unix_network },
1117d58e0da8STetsuo Handa 		{ "misc ", tomoyo_write_misc },
1118731d37aaSTetsuo Handa 		{ "task ", tomoyo_write_task },
1119a238cf5bSTetsuo Handa 	};
1120a238cf5bSTetsuo Handa 	u8 i;
1121d58e0da8STetsuo Handa 
1122d58e0da8STetsuo Handa 	for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
1123a238cf5bSTetsuo Handa 		if (!tomoyo_str_starts(&param.data,
1124a238cf5bSTetsuo Handa 				       tomoyo_callback[i].keyword))
1125a238cf5bSTetsuo Handa 			continue;
1126a238cf5bSTetsuo Handa 		return tomoyo_callback[i].write(&param);
1127a238cf5bSTetsuo Handa 	}
1128a238cf5bSTetsuo Handa 	return -EINVAL;
112917fcfbd9STetsuo Handa }
113017fcfbd9STetsuo Handa 
11312c47ab93STetsuo Handa /* String table for domain flags. */
11322c47ab93STetsuo Handa const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
11332c47ab93STetsuo Handa 	[TOMOYO_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
11342c47ab93STetsuo Handa 	[TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
11352c47ab93STetsuo Handa };
11362c47ab93STetsuo Handa 
113717fcfbd9STetsuo Handa /**
1138e2bf6907STetsuo Handa  * tomoyo_write_domain - Write domain policy.
11399590837bSKentaro Takeda  *
11409590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
11419590837bSKentaro Takeda  *
11429590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1143fdb8ebb7STetsuo Handa  *
1144fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
11459590837bSKentaro Takeda  */
1146e2bf6907STetsuo Handa static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
11479590837bSKentaro Takeda {
11489590837bSKentaro Takeda 	char *data = head->write_buf;
1149bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
11500df7e8b8STetsuo Handa 	struct tomoyo_domain_info *domain = head->w.domain;
1151bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1152bd03a3e4STetsuo Handa 	bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
11539590837bSKentaro Takeda 	unsigned int profile;
1154bd03a3e4STetsuo Handa 	if (*data == '<') {
11557d7473dbSTetsuo Handa 		int ret = 0;
11569590837bSKentaro Takeda 		domain = NULL;
11579590837bSKentaro Takeda 		if (is_delete)
11587d7473dbSTetsuo Handa 			ret = tomoyo_delete_domain(data);
1159fdb8ebb7STetsuo Handa 		else if (is_select)
11609590837bSKentaro Takeda 			domain = tomoyo_find_domain(data);
1161fdb8ebb7STetsuo Handa 		else
1162bd03a3e4STetsuo Handa 			domain = tomoyo_assign_domain(data, false);
11630df7e8b8STetsuo Handa 		head->w.domain = domain;
11647d7473dbSTetsuo Handa 		return ret;
11659590837bSKentaro Takeda 	}
11669590837bSKentaro Takeda 	if (!domain)
11679590837bSKentaro Takeda 		return -EINVAL;
1168bd03a3e4STetsuo Handa 	ns = domain->ns;
1169b5bc60b4STetsuo Handa 	if (sscanf(data, "use_profile %u", &profile) == 1
11709590837bSKentaro Takeda 	    && profile < TOMOYO_MAX_PROFILES) {
1171bd03a3e4STetsuo Handa 		if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
11729590837bSKentaro Takeda 			domain->profile = (u8) profile;
11739590837bSKentaro Takeda 		return 0;
11749590837bSKentaro Takeda 	}
117532997144STetsuo Handa 	if (sscanf(data, "use_group %u\n", &profile) == 1
117632997144STetsuo Handa 	    && profile < TOMOYO_MAX_ACL_GROUPS) {
117732997144STetsuo Handa 		if (!is_delete)
117832997144STetsuo Handa 			domain->group = (u8) profile;
117932997144STetsuo Handa 		return 0;
118032997144STetsuo Handa 	}
11812c47ab93STetsuo Handa 	for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
11822c47ab93STetsuo Handa 		const char *cp = tomoyo_dif[profile];
11832c47ab93STetsuo Handa 		if (strncmp(data, cp, strlen(cp) - 1))
11842c47ab93STetsuo Handa 			continue;
11852c47ab93STetsuo Handa 		domain->flags[profile] = !is_delete;
11869b244373STetsuo Handa 		return 0;
11879b244373STetsuo Handa 	}
1188bd03a3e4STetsuo Handa 	return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1189bd03a3e4STetsuo Handa 				    is_delete);
11909590837bSKentaro Takeda }
11919590837bSKentaro Takeda 
11929590837bSKentaro Takeda /**
11932066a361STetsuo Handa  * tomoyo_print_condition - Print condition part.
11942066a361STetsuo Handa  *
11952066a361STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
11962066a361STetsuo Handa  * @cond: Pointer to "struct tomoyo_condition".
11972066a361STetsuo Handa  *
11982066a361STetsuo Handa  * Returns true on success, false otherwise.
11992066a361STetsuo Handa  */
12002066a361STetsuo Handa static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
12012066a361STetsuo Handa 				   const struct tomoyo_condition *cond)
12022066a361STetsuo Handa {
12032066a361STetsuo Handa 	switch (head->r.cond_step) {
12042066a361STetsuo Handa 	case 0:
12052066a361STetsuo Handa 		head->r.cond_index = 0;
12062066a361STetsuo Handa 		head->r.cond_step++;
12076bce98edSTetsuo Handa 		if (cond->transit) {
12086bce98edSTetsuo Handa 			tomoyo_set_space(head);
12096bce98edSTetsuo Handa 			tomoyo_set_string(head, cond->transit->name);
12106bce98edSTetsuo Handa 		}
12112066a361STetsuo Handa 		/* fall through */
12122066a361STetsuo Handa 	case 1:
12132066a361STetsuo Handa 		{
12142066a361STetsuo Handa 			const u16 condc = cond->condc;
12152066a361STetsuo Handa 			const struct tomoyo_condition_element *condp =
12162066a361STetsuo Handa 				(typeof(condp)) (cond + 1);
12172066a361STetsuo Handa 			const struct tomoyo_number_union *numbers_p =
12182066a361STetsuo Handa 				(typeof(numbers_p)) (condp + condc);
12192ca9bf45STetsuo Handa 			const struct tomoyo_name_union *names_p =
12202ca9bf45STetsuo Handa 				(typeof(names_p))
12212ca9bf45STetsuo Handa 				(numbers_p + cond->numbers_count);
12225b636857STetsuo Handa 			const struct tomoyo_argv *argv =
12235b636857STetsuo Handa 				(typeof(argv)) (names_p + cond->names_count);
12245b636857STetsuo Handa 			const struct tomoyo_envp *envp =
12255b636857STetsuo Handa 				(typeof(envp)) (argv + cond->argc);
12262066a361STetsuo Handa 			u16 skip;
12272066a361STetsuo Handa 			for (skip = 0; skip < head->r.cond_index; skip++) {
12282066a361STetsuo Handa 				const u8 left = condp->left;
12292066a361STetsuo Handa 				const u8 right = condp->right;
12302066a361STetsuo Handa 				condp++;
12312066a361STetsuo Handa 				switch (left) {
12325b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12335b636857STetsuo Handa 					argv++;
12345b636857STetsuo Handa 					continue;
12355b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12365b636857STetsuo Handa 					envp++;
12375b636857STetsuo Handa 					continue;
12382066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12392066a361STetsuo Handa 					numbers_p++;
12402066a361STetsuo Handa 					break;
12412066a361STetsuo Handa 				}
12422066a361STetsuo Handa 				switch (right) {
12432ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
12442ca9bf45STetsuo Handa 					names_p++;
12452ca9bf45STetsuo Handa 					break;
12462066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12472066a361STetsuo Handa 					numbers_p++;
12482066a361STetsuo Handa 					break;
12492066a361STetsuo Handa 				}
12502066a361STetsuo Handa 			}
12512066a361STetsuo Handa 			while (head->r.cond_index < condc) {
12522066a361STetsuo Handa 				const u8 match = condp->equals;
12532066a361STetsuo Handa 				const u8 left = condp->left;
12542066a361STetsuo Handa 				const u8 right = condp->right;
12552066a361STetsuo Handa 				if (!tomoyo_flush(head))
12562066a361STetsuo Handa 					return false;
12572066a361STetsuo Handa 				condp++;
12582066a361STetsuo Handa 				head->r.cond_index++;
12592066a361STetsuo Handa 				tomoyo_set_space(head);
12602066a361STetsuo Handa 				switch (left) {
12615b636857STetsuo Handa 				case TOMOYO_ARGV_ENTRY:
12625b636857STetsuo Handa 					tomoyo_io_printf(head,
12635b636857STetsuo Handa 							 "exec.argv[%lu]%s=\"",
12645b636857STetsuo Handa 							 argv->index, argv->
12655b636857STetsuo Handa 							 is_not ? "!" : "");
12665b636857STetsuo Handa 					tomoyo_set_string(head,
12675b636857STetsuo Handa 							  argv->value->name);
12685b636857STetsuo Handa 					tomoyo_set_string(head, "\"");
12695b636857STetsuo Handa 					argv++;
12705b636857STetsuo Handa 					continue;
12715b636857STetsuo Handa 				case TOMOYO_ENVP_ENTRY:
12725b636857STetsuo Handa 					tomoyo_set_string(head,
12735b636857STetsuo Handa 							  "exec.envp[\"");
12745b636857STetsuo Handa 					tomoyo_set_string(head,
12755b636857STetsuo Handa 							  envp->name->name);
12765b636857STetsuo Handa 					tomoyo_io_printf(head, "\"]%s=", envp->
12775b636857STetsuo Handa 							 is_not ? "!" : "");
12785b636857STetsuo Handa 					if (envp->value) {
12795b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12805b636857STetsuo Handa 						tomoyo_set_string(head, envp->
12815b636857STetsuo Handa 								  value->name);
12825b636857STetsuo Handa 						tomoyo_set_string(head, "\"");
12835b636857STetsuo Handa 					} else {
12845b636857STetsuo Handa 						tomoyo_set_string(head,
12855b636857STetsuo Handa 								  "NULL");
12865b636857STetsuo Handa 					}
12875b636857STetsuo Handa 					envp++;
12885b636857STetsuo Handa 					continue;
12892066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
12902066a361STetsuo Handa 					tomoyo_print_number_union_nospace
12912066a361STetsuo Handa 						(head, numbers_p++);
12922066a361STetsuo Handa 					break;
12932066a361STetsuo Handa 				default:
12942066a361STetsuo Handa 					tomoyo_set_string(head,
12952066a361STetsuo Handa 					       tomoyo_condition_keyword[left]);
12962066a361STetsuo Handa 					break;
12972066a361STetsuo Handa 				}
12982066a361STetsuo Handa 				tomoyo_set_string(head, match ? "=" : "!=");
12992066a361STetsuo Handa 				switch (right) {
13002ca9bf45STetsuo Handa 				case TOMOYO_NAME_UNION:
13012ca9bf45STetsuo Handa 					tomoyo_print_name_union_quoted
13022ca9bf45STetsuo Handa 						(head, names_p++);
13032ca9bf45STetsuo Handa 					break;
13042066a361STetsuo Handa 				case TOMOYO_NUMBER_UNION:
13052066a361STetsuo Handa 					tomoyo_print_number_union_nospace
13062066a361STetsuo Handa 						(head, numbers_p++);
13072066a361STetsuo Handa 					break;
13082066a361STetsuo Handa 				default:
13092066a361STetsuo Handa 					tomoyo_set_string(head,
13102066a361STetsuo Handa 					  tomoyo_condition_keyword[right]);
13112066a361STetsuo Handa 					break;
13122066a361STetsuo Handa 				}
13132066a361STetsuo Handa 			}
13142066a361STetsuo Handa 		}
13152066a361STetsuo Handa 		head->r.cond_step++;
13162066a361STetsuo Handa 		/* fall through */
13172066a361STetsuo Handa 	case 2:
13182066a361STetsuo Handa 		if (!tomoyo_flush(head))
13192066a361STetsuo Handa 			break;
13202066a361STetsuo Handa 		head->r.cond_step++;
13212066a361STetsuo Handa 		/* fall through */
13222066a361STetsuo Handa 	case 3:
13231f067a68STetsuo Handa 		if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13241f067a68STetsuo Handa 			tomoyo_io_printf(head, " grant_log=%s",
13251f067a68STetsuo Handa 					 tomoyo_yesno(cond->grant_log ==
13261f067a68STetsuo Handa 						      TOMOYO_GRANTLOG_YES));
13272066a361STetsuo Handa 		tomoyo_set_lf(head);
13282066a361STetsuo Handa 		return true;
13292066a361STetsuo Handa 	}
13302066a361STetsuo Handa 	return false;
13312066a361STetsuo Handa }
13322066a361STetsuo Handa 
13332066a361STetsuo Handa /**
133432997144STetsuo Handa  * tomoyo_set_group - Print "acl_group " header keyword and category name.
13359590837bSKentaro Takeda  *
13360d2171d7STetsuo Handa  * @head:     Pointer to "struct tomoyo_io_buffer".
13370d2171d7STetsuo Handa  * @category: Category name.
13389590837bSKentaro Takeda  *
13390d2171d7STetsuo Handa  * Returns nothing.
13409590837bSKentaro Takeda  */
13410d2171d7STetsuo Handa static void tomoyo_set_group(struct tomoyo_io_buffer *head,
13420d2171d7STetsuo Handa 			     const char *category)
13439590837bSKentaro Takeda {
1344bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1345bd03a3e4STetsuo Handa 		tomoyo_print_namespace(head);
134632997144STetsuo Handa 		tomoyo_io_printf(head, "acl_group %u ",
134732997144STetsuo Handa 				 head->r.acl_group_index);
1348bd03a3e4STetsuo Handa 	}
13490d2171d7STetsuo Handa 	tomoyo_set_string(head, category);
13502106ccd9STetsuo Handa }
13512106ccd9STetsuo Handa 
13522106ccd9STetsuo Handa /**
13539590837bSKentaro Takeda  * tomoyo_print_entry - Print an ACL entry.
13549590837bSKentaro Takeda  *
13559590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
13565db5a39bSTetsuo Handa  * @acl:  Pointer to an ACL entry.
13579590837bSKentaro Takeda  *
13589590837bSKentaro Takeda  * Returns true on success, false otherwise.
13599590837bSKentaro Takeda  */
13609590837bSKentaro Takeda static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13615db5a39bSTetsuo Handa 			       struct tomoyo_acl_info *acl)
13629590837bSKentaro Takeda {
13635db5a39bSTetsuo Handa 	const u8 acl_type = acl->type;
13640d2171d7STetsuo Handa 	bool first = true;
1365f23571e8STetsuo Handa 	u8 bit;
13669590837bSKentaro Takeda 
13672066a361STetsuo Handa 	if (head->r.print_cond_part)
13682066a361STetsuo Handa 		goto print_cond_part;
13695db5a39bSTetsuo Handa 	if (acl->is_deleted)
1370237ab459STetsuo Handa 		return true;
1371f23571e8STetsuo Handa 	if (!tomoyo_flush(head))
1372f23571e8STetsuo Handa 		return false;
1373f23571e8STetsuo Handa 	else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
13745db5a39bSTetsuo Handa 		struct tomoyo_path_acl *ptr =
13755db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
13765db5a39bSTetsuo Handa 		const u16 perm = ptr->perm;
13770d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13785db5a39bSTetsuo Handa 			if (!(perm & (1 << bit)))
13795db5a39bSTetsuo Handa 				continue;
1380bd03a3e4STetsuo Handa 			if (head->r.print_transition_related_only &&
13815db5a39bSTetsuo Handa 			    bit != TOMOYO_TYPE_EXECUTE)
13825db5a39bSTetsuo Handa 				continue;
13830d2171d7STetsuo Handa 			if (first) {
13840d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
13850d2171d7STetsuo Handa 				first = false;
13860d2171d7STetsuo Handa 			} else {
13870d2171d7STetsuo Handa 				tomoyo_set_slash(head);
13889590837bSKentaro Takeda 			}
13890d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_path_keyword[bit]);
13900d2171d7STetsuo Handa 		}
13910d2171d7STetsuo Handa 		if (first)
13920d2171d7STetsuo Handa 			return true;
1393f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1394731d37aaSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1395731d37aaSTetsuo Handa 		struct tomoyo_task_acl *ptr =
1396731d37aaSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1397731d37aaSTetsuo Handa 		tomoyo_set_group(head, "task ");
1398731d37aaSTetsuo Handa 		tomoyo_set_string(head, "manual_domain_transition ");
1399731d37aaSTetsuo Handa 		tomoyo_set_string(head, ptr->domainname->name);
1400bd03a3e4STetsuo Handa 	} else if (head->r.print_transition_related_only) {
1401063821c8STetsuo Handa 		return true;
14025db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
14035db5a39bSTetsuo Handa 		struct tomoyo_path2_acl *ptr =
14045db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14050d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14060d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14070d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14080d2171d7STetsuo Handa 				continue;
14090d2171d7STetsuo Handa 			if (first) {
14100d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14110d2171d7STetsuo Handa 				first = false;
14120d2171d7STetsuo Handa 			} else {
14130d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14140d2171d7STetsuo Handa 			}
14150d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14160d2171d7STetsuo Handa 					  [tomoyo_pp2mac[bit]]);
14170d2171d7STetsuo Handa 		}
14180d2171d7STetsuo Handa 		if (first)
14190d2171d7STetsuo Handa 			return true;
1420f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name1);
1421f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name2);
14225db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
14235db5a39bSTetsuo Handa 		struct tomoyo_path_number_acl *ptr =
14245db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14250d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14260d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14270d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14280d2171d7STetsuo Handa 				continue;
14290d2171d7STetsuo Handa 			if (first) {
14300d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14310d2171d7STetsuo Handa 				first = false;
14320d2171d7STetsuo Handa 			} else {
14330d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14340d2171d7STetsuo Handa 			}
14350d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14360d2171d7STetsuo Handa 					  [tomoyo_pn2mac[bit]]);
14370d2171d7STetsuo Handa 		}
14380d2171d7STetsuo Handa 		if (first)
14390d2171d7STetsuo Handa 			return true;
1440f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1441f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->number);
14425db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
14435db5a39bSTetsuo Handa 		struct tomoyo_mkdev_acl *ptr =
14445db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
14450d2171d7STetsuo Handa 		const u8 perm = ptr->perm;
14460d2171d7STetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14470d2171d7STetsuo Handa 			if (!(perm & (1 << bit)))
14480d2171d7STetsuo Handa 				continue;
14490d2171d7STetsuo Handa 			if (first) {
14500d2171d7STetsuo Handa 				tomoyo_set_group(head, "file ");
14510d2171d7STetsuo Handa 				first = false;
14520d2171d7STetsuo Handa 			} else {
14530d2171d7STetsuo Handa 				tomoyo_set_slash(head);
14540d2171d7STetsuo Handa 			}
14550d2171d7STetsuo Handa 			tomoyo_set_string(head, tomoyo_mac_keywords
14560d2171d7STetsuo Handa 					  [tomoyo_pnnn2mac[bit]]);
14570d2171d7STetsuo Handa 		}
14580d2171d7STetsuo Handa 		if (first)
14590d2171d7STetsuo Handa 			return true;
1460f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
1461f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->mode);
1462f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->major);
1463f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->minor);
1464059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1465059d84dbSTetsuo Handa 		struct tomoyo_inet_acl *ptr =
1466059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1467059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1468059d84dbSTetsuo Handa 
1469059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1470059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1471059d84dbSTetsuo Handa 				continue;
1472059d84dbSTetsuo Handa 			if (first) {
1473059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network inet ");
1474059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1475059d84dbSTetsuo Handa 						  [ptr->protocol]);
1476059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1477059d84dbSTetsuo Handa 				first = false;
1478059d84dbSTetsuo Handa 			} else {
1479059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1480059d84dbSTetsuo Handa 			}
1481059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1482059d84dbSTetsuo Handa 		}
1483059d84dbSTetsuo Handa 		if (first)
1484059d84dbSTetsuo Handa 			return true;
1485059d84dbSTetsuo Handa 		tomoyo_set_space(head);
1486059d84dbSTetsuo Handa 		if (ptr->address.group) {
1487059d84dbSTetsuo Handa 			tomoyo_set_string(head, "@");
1488059d84dbSTetsuo Handa 			tomoyo_set_string(head, ptr->address.group->group_name
1489059d84dbSTetsuo Handa 					  ->name);
1490059d84dbSTetsuo Handa 		} else {
1491059d84dbSTetsuo Handa 			char buf[128];
1492059d84dbSTetsuo Handa 			tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1493059d84dbSTetsuo Handa 			tomoyo_io_printf(head, "%s", buf);
1494059d84dbSTetsuo Handa 		}
1495059d84dbSTetsuo Handa 		tomoyo_print_number_union(head, &ptr->port);
1496059d84dbSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1497059d84dbSTetsuo Handa 		struct tomoyo_unix_acl *ptr =
1498059d84dbSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
1499059d84dbSTetsuo Handa 		const u8 perm = ptr->perm;
1500059d84dbSTetsuo Handa 
1501059d84dbSTetsuo Handa 		for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1502059d84dbSTetsuo Handa 			if (!(perm & (1 << bit)))
1503059d84dbSTetsuo Handa 				continue;
1504059d84dbSTetsuo Handa 			if (first) {
1505059d84dbSTetsuo Handa 				tomoyo_set_group(head, "network unix ");
1506059d84dbSTetsuo Handa 				tomoyo_set_string(head, tomoyo_proto_keyword
1507059d84dbSTetsuo Handa 						  [ptr->protocol]);
1508059d84dbSTetsuo Handa 				tomoyo_set_space(head);
1509059d84dbSTetsuo Handa 				first = false;
1510059d84dbSTetsuo Handa 			} else {
1511059d84dbSTetsuo Handa 				tomoyo_set_slash(head);
1512059d84dbSTetsuo Handa 			}
1513059d84dbSTetsuo Handa 			tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1514059d84dbSTetsuo Handa 		}
1515059d84dbSTetsuo Handa 		if (first)
1516059d84dbSTetsuo Handa 			return true;
1517059d84dbSTetsuo Handa 		tomoyo_print_name_union(head, &ptr->name);
15185db5a39bSTetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15195db5a39bSTetsuo Handa 		struct tomoyo_mount_acl *ptr =
15205db5a39bSTetsuo Handa 			container_of(acl, typeof(*ptr), head);
15210d2171d7STetsuo Handa 		tomoyo_set_group(head, "file mount");
1522f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dev_name);
1523f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->dir_name);
1524f23571e8STetsuo Handa 		tomoyo_print_name_union(head, &ptr->fs_type);
1525f23571e8STetsuo Handa 		tomoyo_print_number_union(head, &ptr->flags);
1526d58e0da8STetsuo Handa 	} else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1527d58e0da8STetsuo Handa 		struct tomoyo_env_acl *ptr =
1528d58e0da8STetsuo Handa 			container_of(acl, typeof(*ptr), head);
1529d58e0da8STetsuo Handa 
1530d58e0da8STetsuo Handa 		tomoyo_set_group(head, "misc env ");
1531d58e0da8STetsuo Handa 		tomoyo_set_string(head, ptr->env->name);
15329590837bSKentaro Takeda 	}
15332066a361STetsuo Handa 	if (acl->cond) {
15342066a361STetsuo Handa 		head->r.print_cond_part = true;
15352066a361STetsuo Handa 		head->r.cond_step = 0;
15362066a361STetsuo Handa 		if (!tomoyo_flush(head))
15372066a361STetsuo Handa 			return false;
15382066a361STetsuo Handa print_cond_part:
15392066a361STetsuo Handa 		if (!tomoyo_print_condition(head, acl->cond))
15402066a361STetsuo Handa 			return false;
15412066a361STetsuo Handa 		head->r.print_cond_part = false;
15422066a361STetsuo Handa 	} else {
15430d2171d7STetsuo Handa 		tomoyo_set_lf(head);
15442066a361STetsuo Handa 	}
15455db5a39bSTetsuo Handa 	return true;
1546f23571e8STetsuo Handa }
1547f23571e8STetsuo Handa 
1548f23571e8STetsuo Handa /**
1549f23571e8STetsuo Handa  * tomoyo_read_domain2 - Read domain policy.
1550f23571e8STetsuo Handa  *
1551f23571e8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
155232997144STetsuo Handa  * @list: Pointer to "struct list_head".
1553f23571e8STetsuo Handa  *
1554f23571e8STetsuo Handa  * Caller holds tomoyo_read_lock().
1555f23571e8STetsuo Handa  *
1556f23571e8STetsuo Handa  * Returns true on success, false otherwise.
1557f23571e8STetsuo Handa  */
1558f23571e8STetsuo Handa static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
155932997144STetsuo Handa 				struct list_head *list)
1560f23571e8STetsuo Handa {
156132997144STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1562f23571e8STetsuo Handa 		struct tomoyo_acl_info *ptr =
1563f23571e8STetsuo Handa 			list_entry(head->r.acl, typeof(*ptr), list);
1564f23571e8STetsuo Handa 		if (!tomoyo_print_entry(head, ptr))
15659590837bSKentaro Takeda 			return false;
15669590837bSKentaro Takeda 	}
1567f23571e8STetsuo Handa 	head->r.acl = NULL;
1568f23571e8STetsuo Handa 	return true;
1569f23571e8STetsuo Handa }
15709590837bSKentaro Takeda 
15719590837bSKentaro Takeda /**
1572e2bf6907STetsuo Handa  * tomoyo_read_domain - Read domain policy.
15739590837bSKentaro Takeda  *
15749590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
15759590837bSKentaro Takeda  *
1576fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
15779590837bSKentaro Takeda  */
1578e2bf6907STetsuo Handa static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
15799590837bSKentaro Takeda {
1580f23571e8STetsuo Handa 	if (head->r.eof)
15818fbe71f0STetsuo Handa 		return;
1582f23571e8STetsuo Handa 	list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1583475e6fa3STetsuo Handa 		struct tomoyo_domain_info *domain =
1584f23571e8STetsuo Handa 			list_entry(head->r.domain, typeof(*domain), list);
1585f23571e8STetsuo Handa 		switch (head->r.step) {
15862c47ab93STetsuo Handa 			u8 i;
1587f23571e8STetsuo Handa 		case 0:
1588f23571e8STetsuo Handa 			if (domain->is_deleted &&
1589f23571e8STetsuo Handa 			    !head->r.print_this_domain_only)
15909590837bSKentaro Takeda 				continue;
15919590837bSKentaro Takeda 			/* Print domainname and flags. */
1592f23571e8STetsuo Handa 			tomoyo_set_string(head, domain->domainname->name);
1593f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1594b5bc60b4STetsuo Handa 			tomoyo_io_printf(head, "use_profile %u\n",
1595f23571e8STetsuo Handa 					 domain->profile);
159632997144STetsuo Handa 			tomoyo_io_printf(head, "use_group %u\n",
159732997144STetsuo Handa 					 domain->group);
15982c47ab93STetsuo Handa 			for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
15992c47ab93STetsuo Handa 				if (domain->flags[i])
16002c47ab93STetsuo Handa 					tomoyo_set_string(head, tomoyo_dif[i]);
1601f23571e8STetsuo Handa 			head->r.step++;
1602f23571e8STetsuo Handa 			tomoyo_set_lf(head);
1603f23571e8STetsuo Handa 			/* fall through */
1604f23571e8STetsuo Handa 		case 1:
160532997144STetsuo Handa 			if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1606f23571e8STetsuo Handa 				return;
1607f23571e8STetsuo Handa 			head->r.step++;
1608f23571e8STetsuo Handa 			if (!tomoyo_set_lf(head))
1609f23571e8STetsuo Handa 				return;
1610f23571e8STetsuo Handa 			/* fall through */
1611f23571e8STetsuo Handa 		case 2:
1612f23571e8STetsuo Handa 			head->r.step = 0;
1613f23571e8STetsuo Handa 			if (head->r.print_this_domain_only)
1614f23571e8STetsuo Handa 				goto done;
16159590837bSKentaro Takeda 		}
16169590837bSKentaro Takeda 	}
1617f23571e8STetsuo Handa  done:
1618f23571e8STetsuo Handa 	head->r.eof = true;
16199590837bSKentaro Takeda }
16209590837bSKentaro Takeda 
16219590837bSKentaro Takeda /**
16229590837bSKentaro Takeda  * tomoyo_write_pid: Specify PID to obtain domainname.
16239590837bSKentaro Takeda  *
16249590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16259590837bSKentaro Takeda  *
16269590837bSKentaro Takeda  * Returns 0.
16279590837bSKentaro Takeda  */
16289590837bSKentaro Takeda static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
16299590837bSKentaro Takeda {
1630f23571e8STetsuo Handa 	head->r.eof = false;
16319590837bSKentaro Takeda 	return 0;
16329590837bSKentaro Takeda }
16339590837bSKentaro Takeda 
16349590837bSKentaro Takeda /**
16359590837bSKentaro Takeda  * tomoyo_read_pid - Get domainname of the specified PID.
16369590837bSKentaro Takeda  *
16379590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16389590837bSKentaro Takeda  *
16399590837bSKentaro Takeda  * Returns the domainname which the specified PID is in on success,
16409590837bSKentaro Takeda  * empty string otherwise.
16419590837bSKentaro Takeda  * The PID is specified by tomoyo_write_pid() so that the user can obtain
16429590837bSKentaro Takeda  * using read()/write() interface rather than sysctl() interface.
16439590837bSKentaro Takeda  */
16448fbe71f0STetsuo Handa static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16459590837bSKentaro Takeda {
1646f23571e8STetsuo Handa 	char *buf = head->write_buf;
1647f23571e8STetsuo Handa 	bool global_pid = false;
1648f23571e8STetsuo Handa 	unsigned int pid;
16499590837bSKentaro Takeda 	struct task_struct *p;
16509590837bSKentaro Takeda 	struct tomoyo_domain_info *domain = NULL;
1651f23571e8STetsuo Handa 
1652f23571e8STetsuo Handa 	/* Accessing write_buf is safe because head->io_sem is held. */
1653f23571e8STetsuo Handa 	if (!buf) {
1654f23571e8STetsuo Handa 		head->r.eof = true;
1655f23571e8STetsuo Handa 		return; /* Do nothing if open(O_RDONLY). */
1656f23571e8STetsuo Handa 	}
1657f23571e8STetsuo Handa 	if (head->r.w_pos || head->r.eof)
1658f23571e8STetsuo Handa 		return;
1659f23571e8STetsuo Handa 	head->r.eof = true;
1660f23571e8STetsuo Handa 	if (tomoyo_str_starts(&buf, "global-pid "))
1661f23571e8STetsuo Handa 		global_pid = true;
1662f23571e8STetsuo Handa 	pid = (unsigned int) simple_strtoul(buf, NULL, 10);
16631fcdc7c5STetsuo Handa 	rcu_read_lock();
1664f23571e8STetsuo Handa 	if (global_pid)
1665f23571e8STetsuo Handa 		p = find_task_by_pid_ns(pid, &init_pid_ns);
1666f23571e8STetsuo Handa 	else
16679590837bSKentaro Takeda 		p = find_task_by_vpid(pid);
16689590837bSKentaro Takeda 	if (p)
16699590837bSKentaro Takeda 		domain = tomoyo_real_domain(p);
16701fcdc7c5STetsuo Handa 	rcu_read_unlock();
1671f23571e8STetsuo Handa 	if (!domain)
1672f23571e8STetsuo Handa 		return;
1673f23571e8STetsuo Handa 	tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1674f23571e8STetsuo Handa 	tomoyo_set_string(head, domain->domainname->name);
16759590837bSKentaro Takeda }
16769590837bSKentaro Takeda 
16770f2a55d5STetsuo Handa /* String table for domain transition control keywords. */
16785448ec4fSTetsuo Handa static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1679bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
1680bd03a3e4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_RESET]         = "reset_domain ",
1681b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1682b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1683b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1684b5bc60b4STetsuo Handa 	[TOMOYO_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
16855448ec4fSTetsuo Handa };
16865448ec4fSTetsuo Handa 
16870f2a55d5STetsuo Handa /* String table for grouping keywords. */
1688e2bf6907STetsuo Handa static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1689b5bc60b4STetsuo Handa 	[TOMOYO_PATH_GROUP]    = "path_group ",
1690b5bc60b4STetsuo Handa 	[TOMOYO_NUMBER_GROUP]  = "number_group ",
1691059d84dbSTetsuo Handa 	[TOMOYO_ADDRESS_GROUP] = "address_group ",
1692e2bf6907STetsuo Handa };
1693e2bf6907STetsuo Handa 
16949590837bSKentaro Takeda /**
1695e2bf6907STetsuo Handa  * tomoyo_write_exception - Write exception policy.
16969590837bSKentaro Takeda  *
16979590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
16989590837bSKentaro Takeda  *
16999590837bSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1700fdb8ebb7STetsuo Handa  *
1701fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
17029590837bSKentaro Takeda  */
1703e2bf6907STetsuo Handa static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
17049590837bSKentaro Takeda {
1705bd03a3e4STetsuo Handa 	const bool is_delete = head->w.is_delete;
1706a238cf5bSTetsuo Handa 	struct tomoyo_acl_param param = {
1707bd03a3e4STetsuo Handa 		.ns = head->w.ns,
1708bd03a3e4STetsuo Handa 		.is_delete = is_delete,
1709a238cf5bSTetsuo Handa 		.data = head->write_buf,
1710e2bf6907STetsuo Handa 	};
1711a238cf5bSTetsuo Handa 	u8 i;
1712a238cf5bSTetsuo Handa 	if (tomoyo_str_starts(&param.data, "aggregator "))
1713a238cf5bSTetsuo Handa 		return tomoyo_write_aggregator(&param);
1714e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1715a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1716a238cf5bSTetsuo Handa 			return tomoyo_write_transition_control(&param, i);
1717e2bf6907STetsuo Handa 	for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1718a238cf5bSTetsuo Handa 		if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1719a238cf5bSTetsuo Handa 			return tomoyo_write_group(&param, i);
172032997144STetsuo Handa 	if (tomoyo_str_starts(&param.data, "acl_group ")) {
172132997144STetsuo Handa 		unsigned int group;
172232997144STetsuo Handa 		char *data;
172332997144STetsuo Handa 		group = simple_strtoul(param.data, &data, 10);
172432997144STetsuo Handa 		if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1725bd03a3e4STetsuo Handa 			return tomoyo_write_domain2
1726bd03a3e4STetsuo Handa 				(head->w.ns, &head->w.ns->acl_group[group],
1727bd03a3e4STetsuo Handa 				 data, is_delete);
172832997144STetsuo Handa 	}
17299590837bSKentaro Takeda 	return -EINVAL;
17309590837bSKentaro Takeda }
17319590837bSKentaro Takeda 
173231845e8cSTetsuo Handa /**
1733059d84dbSTetsuo Handa  * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
173431845e8cSTetsuo Handa  *
173531845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
173631845e8cSTetsuo Handa  * @idx:  Index number.
173731845e8cSTetsuo Handa  *
173831845e8cSTetsuo Handa  * Returns true on success, false otherwise.
173931845e8cSTetsuo Handa  *
174031845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
174131845e8cSTetsuo Handa  */
174231845e8cSTetsuo Handa static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
174331845e8cSTetsuo Handa {
1744bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1745bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1746bd03a3e4STetsuo Handa 	struct list_head *list = &ns->group_list[idx];
1747bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.group, list) {
174831845e8cSTetsuo Handa 		struct tomoyo_group *group =
17490df7e8b8STetsuo Handa 			list_entry(head->r.group, typeof(*group), head.list);
1750f23571e8STetsuo Handa 		list_for_each_cookie(head->r.acl, &group->member_list) {
175131845e8cSTetsuo Handa 			struct tomoyo_acl_head *ptr =
1752f23571e8STetsuo Handa 				list_entry(head->r.acl, typeof(*ptr), list);
175331845e8cSTetsuo Handa 			if (ptr->is_deleted)
175431845e8cSTetsuo Handa 				continue;
1755f23571e8STetsuo Handa 			if (!tomoyo_flush(head))
175631845e8cSTetsuo Handa 				return false;
1757bd03a3e4STetsuo Handa 			tomoyo_print_namespace(head);
1758f23571e8STetsuo Handa 			tomoyo_set_string(head, tomoyo_group_name[idx]);
1759f23571e8STetsuo Handa 			tomoyo_set_string(head, group->group_name->name);
1760f23571e8STetsuo Handa 			if (idx == TOMOYO_PATH_GROUP) {
1761f23571e8STetsuo Handa 				tomoyo_set_space(head);
1762f23571e8STetsuo Handa 				tomoyo_set_string(head, container_of
1763f23571e8STetsuo Handa 					       (ptr, struct tomoyo_path_group,
1764f23571e8STetsuo Handa 						head)->member_name->name);
1765f23571e8STetsuo Handa 			} else if (idx == TOMOYO_NUMBER_GROUP) {
1766f23571e8STetsuo Handa 				tomoyo_print_number_union(head, &container_of
1767f23571e8STetsuo Handa 							  (ptr,
1768f23571e8STetsuo Handa 						   struct tomoyo_number_group,
1769f23571e8STetsuo Handa 							   head)->number);
1770059d84dbSTetsuo Handa 			} else if (idx == TOMOYO_ADDRESS_GROUP) {
1771059d84dbSTetsuo Handa 				char buffer[128];
1772059d84dbSTetsuo Handa 
1773059d84dbSTetsuo Handa 				struct tomoyo_address_group *member =
1774059d84dbSTetsuo Handa 					container_of(ptr, typeof(*member),
1775059d84dbSTetsuo Handa 						     head);
1776059d84dbSTetsuo Handa 				tomoyo_print_ip(buffer, sizeof(buffer),
1777059d84dbSTetsuo Handa 						&member->address);
1778059d84dbSTetsuo Handa 				tomoyo_io_printf(head, " %s", buffer);
177931845e8cSTetsuo Handa 			}
1780f23571e8STetsuo Handa 			tomoyo_set_lf(head);
178131845e8cSTetsuo Handa 		}
1782f23571e8STetsuo Handa 		head->r.acl = NULL;
1783f23571e8STetsuo Handa 	}
1784f23571e8STetsuo Handa 	head->r.group = NULL;
178531845e8cSTetsuo Handa 	return true;
178631845e8cSTetsuo Handa }
178731845e8cSTetsuo Handa 
178831845e8cSTetsuo Handa /**
178931845e8cSTetsuo Handa  * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
179031845e8cSTetsuo Handa  *
179131845e8cSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
179231845e8cSTetsuo Handa  * @idx:  Index number.
179331845e8cSTetsuo Handa  *
179431845e8cSTetsuo Handa  * Returns true on success, false otherwise.
179531845e8cSTetsuo Handa  *
179631845e8cSTetsuo Handa  * Caller holds tomoyo_read_lock().
179731845e8cSTetsuo Handa  */
179831845e8cSTetsuo Handa static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
179931845e8cSTetsuo Handa {
1800bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1801bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1802bd03a3e4STetsuo Handa 	struct list_head *list = &ns->policy_list[idx];
1803bd03a3e4STetsuo Handa 	list_for_each_cookie(head->r.acl, list) {
1804475e6fa3STetsuo Handa 		struct tomoyo_acl_head *acl =
1805f23571e8STetsuo Handa 			container_of(head->r.acl, typeof(*acl), list);
180631845e8cSTetsuo Handa 		if (acl->is_deleted)
180731845e8cSTetsuo Handa 			continue;
1808f23571e8STetsuo Handa 		if (!tomoyo_flush(head))
1809f23571e8STetsuo Handa 			return false;
181031845e8cSTetsuo Handa 		switch (idx) {
18115448ec4fSTetsuo Handa 		case TOMOYO_ID_TRANSITION_CONTROL:
181231845e8cSTetsuo Handa 			{
18135448ec4fSTetsuo Handa 				struct tomoyo_transition_control *ptr =
181431845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1815bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
18160d2171d7STetsuo Handa 				tomoyo_set_string(head, tomoyo_transition_type
1817f23571e8STetsuo Handa 						  [ptr->type]);
18180d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->program ?
18190d2171d7STetsuo Handa 						  ptr->program->name : "any");
1820f23571e8STetsuo Handa 				tomoyo_set_string(head, " from ");
18210d2171d7STetsuo Handa 				tomoyo_set_string(head, ptr->domainname ?
18220d2171d7STetsuo Handa 						  ptr->domainname->name :
18230d2171d7STetsuo Handa 						  "any");
182431845e8cSTetsuo Handa 			}
182531845e8cSTetsuo Handa 			break;
182631845e8cSTetsuo Handa 		case TOMOYO_ID_AGGREGATOR:
182731845e8cSTetsuo Handa 			{
1828e2bf6907STetsuo Handa 				struct tomoyo_aggregator *ptr =
182931845e8cSTetsuo Handa 					container_of(acl, typeof(*ptr), head);
1830bd03a3e4STetsuo Handa 				tomoyo_print_namespace(head);
1831b5bc60b4STetsuo Handa 				tomoyo_set_string(head, "aggregator ");
1832f23571e8STetsuo Handa 				tomoyo_set_string(head,
1833f23571e8STetsuo Handa 						  ptr->original_name->name);
1834f23571e8STetsuo Handa 				tomoyo_set_space(head);
1835f23571e8STetsuo Handa 				tomoyo_set_string(head,
1836f23571e8STetsuo Handa 					       ptr->aggregated_name->name);
183731845e8cSTetsuo Handa 			}
183831845e8cSTetsuo Handa 			break;
183931845e8cSTetsuo Handa 		default:
184031845e8cSTetsuo Handa 			continue;
184131845e8cSTetsuo Handa 		}
1842f23571e8STetsuo Handa 		tomoyo_set_lf(head);
184331845e8cSTetsuo Handa 	}
1844f23571e8STetsuo Handa 	head->r.acl = NULL;
184531845e8cSTetsuo Handa 	return true;
184631845e8cSTetsuo Handa }
184731845e8cSTetsuo Handa 
18489590837bSKentaro Takeda /**
1849e2bf6907STetsuo Handa  * tomoyo_read_exception - Read exception policy.
18509590837bSKentaro Takeda  *
18519590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
18529590837bSKentaro Takeda  *
1853fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
18549590837bSKentaro Takeda  */
1855e2bf6907STetsuo Handa static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
18569590837bSKentaro Takeda {
1857bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns =
1858bd03a3e4STetsuo Handa 		container_of(head->r.ns, typeof(*ns), namespace_list);
1859f23571e8STetsuo Handa 	if (head->r.eof)
186031845e8cSTetsuo Handa 		return;
1861f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY &&
1862f23571e8STetsuo Handa 	       tomoyo_read_policy(head, head->r.step))
1863f23571e8STetsuo Handa 		head->r.step++;
1864f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY)
186531845e8cSTetsuo Handa 		return;
1866f23571e8STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1867f23571e8STetsuo Handa 	       tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1868f23571e8STetsuo Handa 		head->r.step++;
1869f23571e8STetsuo Handa 	if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
187031845e8cSTetsuo Handa 		return;
187132997144STetsuo Handa 	while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
187232997144STetsuo Handa 	       + TOMOYO_MAX_ACL_GROUPS) {
187332997144STetsuo Handa 		head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
187432997144STetsuo Handa 			- TOMOYO_MAX_GROUP;
1875bd03a3e4STetsuo Handa 		if (!tomoyo_read_domain2(head, &ns->acl_group
187632997144STetsuo Handa 					 [head->r.acl_group_index]))
187732997144STetsuo Handa 			return;
187832997144STetsuo Handa 		head->r.step++;
187932997144STetsuo Handa 	}
1880f23571e8STetsuo Handa 	head->r.eof = true;
18819590837bSKentaro Takeda }
18829590837bSKentaro Takeda 
1883eadd99ccSTetsuo Handa /* Wait queue for kernel -> userspace notification. */
188417fcfbd9STetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1885eadd99ccSTetsuo Handa /* Wait queue for userspace -> kernel notification. */
1886eadd99ccSTetsuo Handa static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
188717fcfbd9STetsuo Handa 
188817fcfbd9STetsuo Handa /* Structure for query. */
1889e2bf6907STetsuo Handa struct tomoyo_query {
189017fcfbd9STetsuo Handa 	struct list_head list;
189159df3166STetsuo Handa 	struct tomoyo_domain_info *domain;
189217fcfbd9STetsuo Handa 	char *query;
1893eadd99ccSTetsuo Handa 	size_t query_len;
189417fcfbd9STetsuo Handa 	unsigned int serial;
1895eadd99ccSTetsuo Handa 	u8 timer;
1896eadd99ccSTetsuo Handa 	u8 answer;
1897eadd99ccSTetsuo Handa 	u8 retry;
189817fcfbd9STetsuo Handa };
189917fcfbd9STetsuo Handa 
1900e2bf6907STetsuo Handa /* The list for "struct tomoyo_query". */
190117fcfbd9STetsuo Handa static LIST_HEAD(tomoyo_query_list);
190217fcfbd9STetsuo Handa 
1903eadd99ccSTetsuo Handa /* Lock for manipulating tomoyo_query_list. */
1904eadd99ccSTetsuo Handa static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1905eadd99ccSTetsuo Handa 
190617fcfbd9STetsuo Handa /*
190717fcfbd9STetsuo Handa  * Number of "struct file" referring /sys/kernel/security/tomoyo/query
190817fcfbd9STetsuo Handa  * interface.
190917fcfbd9STetsuo Handa  */
191017fcfbd9STetsuo Handa static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
191117fcfbd9STetsuo Handa 
191217fcfbd9STetsuo Handa /**
19132ca9bf45STetsuo Handa  * tomoyo_truncate - Truncate a line.
19142ca9bf45STetsuo Handa  *
19152ca9bf45STetsuo Handa  * @str: String to truncate.
19162ca9bf45STetsuo Handa  *
19172ca9bf45STetsuo Handa  * Returns length of truncated @str.
19182ca9bf45STetsuo Handa  */
19192ca9bf45STetsuo Handa static int tomoyo_truncate(char *str)
19202ca9bf45STetsuo Handa {
19212ca9bf45STetsuo Handa 	char *start = str;
19222ca9bf45STetsuo Handa 	while (*(unsigned char *) str > (unsigned char) ' ')
19232ca9bf45STetsuo Handa 		str++;
19242ca9bf45STetsuo Handa 	*str = '\0';
19252ca9bf45STetsuo Handa 	return strlen(start) + 1;
19262ca9bf45STetsuo Handa }
19272ca9bf45STetsuo Handa 
19282ca9bf45STetsuo Handa /**
1929eadd99ccSTetsuo Handa  * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1930eadd99ccSTetsuo Handa  *
1931eadd99ccSTetsuo Handa  * @domain: Pointer to "struct tomoyo_domain_info".
1932eadd99ccSTetsuo Handa  * @header: Lines containing ACL.
1933eadd99ccSTetsuo Handa  *
1934eadd99ccSTetsuo Handa  * Returns nothing.
1935eadd99ccSTetsuo Handa  */
1936eadd99ccSTetsuo Handa static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1937eadd99ccSTetsuo Handa {
1938eadd99ccSTetsuo Handa 	char *buffer;
19392ca9bf45STetsuo Handa 	char *realpath = NULL;
19405b636857STetsuo Handa 	char *argv0 = NULL;
19412ca9bf45STetsuo Handa 	char *symlink = NULL;
1942eadd99ccSTetsuo Handa 	char *cp = strchr(header, '\n');
1943eadd99ccSTetsuo Handa 	int len;
1944eadd99ccSTetsuo Handa 	if (!cp)
1945eadd99ccSTetsuo Handa 		return;
1946eadd99ccSTetsuo Handa 	cp = strchr(cp + 1, '\n');
1947eadd99ccSTetsuo Handa 	if (!cp)
1948eadd99ccSTetsuo Handa 		return;
1949eadd99ccSTetsuo Handa 	*cp++ = '\0';
1950eadd99ccSTetsuo Handa 	len = strlen(cp) + 1;
19512ca9bf45STetsuo Handa 	/* strstr() will return NULL if ordering is wrong. */
19522ca9bf45STetsuo Handa 	if (*cp == 'f') {
19535b636857STetsuo Handa 		argv0 = strstr(header, " argv[]={ \"");
19545b636857STetsuo Handa 		if (argv0) {
19555b636857STetsuo Handa 			argv0 += 10;
19565b636857STetsuo Handa 			len += tomoyo_truncate(argv0) + 14;
19575b636857STetsuo Handa 		}
19582ca9bf45STetsuo Handa 		realpath = strstr(header, " exec={ realpath=\"");
19592ca9bf45STetsuo Handa 		if (realpath) {
19602ca9bf45STetsuo Handa 			realpath += 8;
19612ca9bf45STetsuo Handa 			len += tomoyo_truncate(realpath) + 6;
19622ca9bf45STetsuo Handa 		}
19632ca9bf45STetsuo Handa 		symlink = strstr(header, " symlink.target=\"");
19642ca9bf45STetsuo Handa 		if (symlink)
19652ca9bf45STetsuo Handa 			len += tomoyo_truncate(symlink + 1) + 1;
19662ca9bf45STetsuo Handa 	}
1967eadd99ccSTetsuo Handa 	buffer = kmalloc(len, GFP_NOFS);
1968eadd99ccSTetsuo Handa 	if (!buffer)
1969eadd99ccSTetsuo Handa 		return;
1970eadd99ccSTetsuo Handa 	snprintf(buffer, len - 1, "%s", cp);
19712ca9bf45STetsuo Handa 	if (realpath)
19722ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.%s", realpath);
19735b636857STetsuo Handa 	if (argv0)
19745b636857STetsuo Handa 		tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
19752ca9bf45STetsuo Handa 	if (symlink)
19762ca9bf45STetsuo Handa 		tomoyo_addprintf(buffer, len, "%s", symlink);
1977eadd99ccSTetsuo Handa 	tomoyo_normalize_line(buffer);
1978b22b8b9fSTetsuo Handa 	if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1979b22b8b9fSTetsuo Handa 				  false))
1980b22b8b9fSTetsuo Handa 		tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
1981eadd99ccSTetsuo Handa 	kfree(buffer);
1982eadd99ccSTetsuo Handa }
1983eadd99ccSTetsuo Handa 
1984eadd99ccSTetsuo Handa /**
198517fcfbd9STetsuo Handa  * tomoyo_supervisor - Ask for the supervisor's decision.
198617fcfbd9STetsuo Handa  *
198717fcfbd9STetsuo Handa  * @r:   Pointer to "struct tomoyo_request_info".
198817fcfbd9STetsuo Handa  * @fmt: The printf()'s format string, followed by parameters.
198917fcfbd9STetsuo Handa  *
199017fcfbd9STetsuo Handa  * Returns 0 if the supervisor decided to permit the access request which
199117fcfbd9STetsuo Handa  * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
199217fcfbd9STetsuo Handa  * supervisor decided to retry the access request which violated the policy in
199317fcfbd9STetsuo Handa  * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
199417fcfbd9STetsuo Handa  */
199517fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
199617fcfbd9STetsuo Handa {
199717fcfbd9STetsuo Handa 	va_list args;
1998eadd99ccSTetsuo Handa 	int error;
199917fcfbd9STetsuo Handa 	int len;
200017fcfbd9STetsuo Handa 	static unsigned int tomoyo_serial;
2001eadd99ccSTetsuo Handa 	struct tomoyo_query entry = { };
200217fcfbd9STetsuo Handa 	bool quota_exceeded = false;
2003eadd99ccSTetsuo Handa 	va_start(args, fmt);
2004eadd99ccSTetsuo Handa 	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2005eadd99ccSTetsuo Handa 	va_end(args);
2006eadd99ccSTetsuo Handa 	/* Write /sys/kernel/security/tomoyo/audit. */
2007eadd99ccSTetsuo Handa 	va_start(args, fmt);
2008eadd99ccSTetsuo Handa 	tomoyo_write_log2(r, len, fmt, args);
2009eadd99ccSTetsuo Handa 	va_end(args);
2010eadd99ccSTetsuo Handa 	/* Nothing more to do if granted. */
2011eadd99ccSTetsuo Handa 	if (r->granted)
2012eadd99ccSTetsuo Handa 		return 0;
2013b22b8b9fSTetsuo Handa 	if (r->mode)
2014b22b8b9fSTetsuo Handa 		tomoyo_update_stat(r->mode);
201517fcfbd9STetsuo Handa 	switch (r->mode) {
2016eadd99ccSTetsuo Handa 	case TOMOYO_CONFIG_ENFORCING:
2017eadd99ccSTetsuo Handa 		error = -EPERM;
2018eadd99ccSTetsuo Handa 		if (atomic_read(&tomoyo_query_observers))
2019eadd99ccSTetsuo Handa 			break;
2020eadd99ccSTetsuo Handa 		goto out;
202117fcfbd9STetsuo Handa 	case TOMOYO_CONFIG_LEARNING:
2022eadd99ccSTetsuo Handa 		error = 0;
2023eadd99ccSTetsuo Handa 		/* Check max_learning_entry parameter. */
2024eadd99ccSTetsuo Handa 		if (tomoyo_domain_quota_is_ok(r))
2025eadd99ccSTetsuo Handa 			break;
202617fcfbd9STetsuo Handa 		/* fall through */
2027eadd99ccSTetsuo Handa 	default:
202817fcfbd9STetsuo Handa 		return 0;
202917fcfbd9STetsuo Handa 	}
2030eadd99ccSTetsuo Handa 	/* Get message. */
203117fcfbd9STetsuo Handa 	va_start(args, fmt);
2032eadd99ccSTetsuo Handa 	entry.query = tomoyo_init_log(r, len, fmt, args);
203317fcfbd9STetsuo Handa 	va_end(args);
2034eadd99ccSTetsuo Handa 	if (!entry.query)
203517fcfbd9STetsuo Handa 		goto out;
2036eadd99ccSTetsuo Handa 	entry.query_len = strlen(entry.query) + 1;
2037eadd99ccSTetsuo Handa 	if (!error) {
2038eadd99ccSTetsuo Handa 		tomoyo_add_entry(r->domain, entry.query);
203917fcfbd9STetsuo Handa 		goto out;
2040eadd99ccSTetsuo Handa 	}
2041eadd99ccSTetsuo Handa 	len = tomoyo_round2(entry.query_len);
204259df3166STetsuo Handa 	entry.domain = r->domain;
204317fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2044eadd99ccSTetsuo Handa 	if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2045eadd99ccSTetsuo Handa 	    tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2046eadd99ccSTetsuo Handa 	    >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
204717fcfbd9STetsuo Handa 		quota_exceeded = true;
204817fcfbd9STetsuo Handa 	} else {
2049eadd99ccSTetsuo Handa 		entry.serial = tomoyo_serial++;
2050eadd99ccSTetsuo Handa 		entry.retry = r->retry;
2051eadd99ccSTetsuo Handa 		tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2052eadd99ccSTetsuo Handa 		list_add_tail(&entry.list, &tomoyo_query_list);
205317fcfbd9STetsuo Handa 	}
205417fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
205517fcfbd9STetsuo Handa 	if (quota_exceeded)
205617fcfbd9STetsuo Handa 		goto out;
205717fcfbd9STetsuo Handa 	/* Give 10 seconds for supervisor's opinion. */
2058eadd99ccSTetsuo Handa 	while (entry.timer < 10) {
2059eadd99ccSTetsuo Handa 		wake_up_all(&tomoyo_query_wait);
2060eadd99ccSTetsuo Handa 		if (wait_event_interruptible_timeout
2061eadd99ccSTetsuo Handa 		    (tomoyo_answer_wait, entry.answer ||
2062eadd99ccSTetsuo Handa 		     !atomic_read(&tomoyo_query_observers), HZ))
206317fcfbd9STetsuo Handa 			break;
2064eadd99ccSTetsuo Handa 		else
2065eadd99ccSTetsuo Handa 			entry.timer++;
206617fcfbd9STetsuo Handa 	}
206717fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
2068eadd99ccSTetsuo Handa 	list_del(&entry.list);
2069eadd99ccSTetsuo Handa 	tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
207017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
2071eadd99ccSTetsuo Handa 	switch (entry.answer) {
207217fcfbd9STetsuo Handa 	case 3: /* Asked to retry by administrator. */
207317fcfbd9STetsuo Handa 		error = TOMOYO_RETRY_REQUEST;
207417fcfbd9STetsuo Handa 		r->retry++;
207517fcfbd9STetsuo Handa 		break;
207617fcfbd9STetsuo Handa 	case 1:
207717fcfbd9STetsuo Handa 		/* Granted by administrator. */
207817fcfbd9STetsuo Handa 		error = 0;
207917fcfbd9STetsuo Handa 		break;
208017fcfbd9STetsuo Handa 	default:
2081eadd99ccSTetsuo Handa 		/* Timed out or rejected by administrator. */
208217fcfbd9STetsuo Handa 		break;
208317fcfbd9STetsuo Handa 	}
208417fcfbd9STetsuo Handa out:
2085eadd99ccSTetsuo Handa 	kfree(entry.query);
208617fcfbd9STetsuo Handa 	return error;
208717fcfbd9STetsuo Handa }
208817fcfbd9STetsuo Handa 
208917fcfbd9STetsuo Handa /**
209059df3166STetsuo Handa  * tomoyo_find_domain_by_qid - Get domain by query id.
209159df3166STetsuo Handa  *
209259df3166STetsuo Handa  * @serial: Query ID assigned by tomoyo_supervisor().
209359df3166STetsuo Handa  *
209459df3166STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
209559df3166STetsuo Handa  */
209659df3166STetsuo Handa static struct tomoyo_domain_info *tomoyo_find_domain_by_qid
209759df3166STetsuo Handa (unsigned int serial)
209859df3166STetsuo Handa {
209959df3166STetsuo Handa 	struct tomoyo_query *ptr;
210059df3166STetsuo Handa 	struct tomoyo_domain_info *domain = NULL;
210159df3166STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
210259df3166STetsuo Handa 	list_for_each_entry(ptr, &tomoyo_query_list, list) {
21036041e834STetsuo Handa 		if (ptr->serial != serial)
210459df3166STetsuo Handa 			continue;
210559df3166STetsuo Handa 		domain = ptr->domain;
210659df3166STetsuo Handa 		break;
210759df3166STetsuo Handa 	}
210859df3166STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
210959df3166STetsuo Handa 	return domain;
211059df3166STetsuo Handa }
211159df3166STetsuo Handa 
211259df3166STetsuo Handa /**
211317fcfbd9STetsuo Handa  * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
211417fcfbd9STetsuo Handa  *
211517fcfbd9STetsuo Handa  * @file: Pointer to "struct file".
211617fcfbd9STetsuo Handa  * @wait: Pointer to "poll_table".
211717fcfbd9STetsuo Handa  *
211817fcfbd9STetsuo Handa  * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
211917fcfbd9STetsuo Handa  *
212017fcfbd9STetsuo Handa  * Waits for access requests which violated policy in enforcing mode.
212117fcfbd9STetsuo Handa  */
21226041e834STetsuo Handa static unsigned int tomoyo_poll_query(struct file *file, poll_table *wait)
212317fcfbd9STetsuo Handa {
21246041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
212517fcfbd9STetsuo Handa 		return POLLIN | POLLRDNORM;
212617fcfbd9STetsuo Handa 	poll_wait(file, &tomoyo_query_wait, wait);
21276041e834STetsuo Handa 	if (!list_empty(&tomoyo_query_list))
21286041e834STetsuo Handa 		return POLLIN | POLLRDNORM;
212917fcfbd9STetsuo Handa 	return 0;
213017fcfbd9STetsuo Handa }
213117fcfbd9STetsuo Handa 
213217fcfbd9STetsuo Handa /**
213317fcfbd9STetsuo Handa  * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
213417fcfbd9STetsuo Handa  *
213517fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
213617fcfbd9STetsuo Handa  */
21378fbe71f0STetsuo Handa static void tomoyo_read_query(struct tomoyo_io_buffer *head)
213817fcfbd9STetsuo Handa {
213917fcfbd9STetsuo Handa 	struct list_head *tmp;
21402c47ab93STetsuo Handa 	unsigned int pos = 0;
21412c47ab93STetsuo Handa 	size_t len = 0;
214217fcfbd9STetsuo Handa 	char *buf;
2143f23571e8STetsuo Handa 	if (head->r.w_pos)
21448fbe71f0STetsuo Handa 		return;
214517fcfbd9STetsuo Handa 	if (head->read_buf) {
214617fcfbd9STetsuo Handa 		kfree(head->read_buf);
214717fcfbd9STetsuo Handa 		head->read_buf = NULL;
214817fcfbd9STetsuo Handa 	}
214917fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
215017fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2151e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2152f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
215317fcfbd9STetsuo Handa 			continue;
215417fcfbd9STetsuo Handa 		len = ptr->query_len;
215517fcfbd9STetsuo Handa 		break;
215617fcfbd9STetsuo Handa 	}
215717fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
215817fcfbd9STetsuo Handa 	if (!len) {
2159f23571e8STetsuo Handa 		head->r.query_index = 0;
21608fbe71f0STetsuo Handa 		return;
216117fcfbd9STetsuo Handa 	}
2162eadd99ccSTetsuo Handa 	buf = kzalloc(len + 32, GFP_NOFS);
216317fcfbd9STetsuo Handa 	if (!buf)
21648fbe71f0STetsuo Handa 		return;
216517fcfbd9STetsuo Handa 	pos = 0;
216617fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
216717fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2168e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2169f23571e8STetsuo Handa 		if (pos++ != head->r.query_index)
217017fcfbd9STetsuo Handa 			continue;
217117fcfbd9STetsuo Handa 		/*
217217fcfbd9STetsuo Handa 		 * Some query can be skipped because tomoyo_query_list
217317fcfbd9STetsuo Handa 		 * can change, but I don't care.
217417fcfbd9STetsuo Handa 		 */
217517fcfbd9STetsuo Handa 		if (len == ptr->query_len)
2176eadd99ccSTetsuo Handa 			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2177eadd99ccSTetsuo Handa 				 ptr->retry, ptr->query);
217817fcfbd9STetsuo Handa 		break;
217917fcfbd9STetsuo Handa 	}
218017fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
218117fcfbd9STetsuo Handa 	if (buf[0]) {
218217fcfbd9STetsuo Handa 		head->read_buf = buf;
2183f23571e8STetsuo Handa 		head->r.w[head->r.w_pos++] = buf;
2184f23571e8STetsuo Handa 		head->r.query_index++;
218517fcfbd9STetsuo Handa 	} else {
218617fcfbd9STetsuo Handa 		kfree(buf);
218717fcfbd9STetsuo Handa 	}
218817fcfbd9STetsuo Handa }
218917fcfbd9STetsuo Handa 
219017fcfbd9STetsuo Handa /**
219117fcfbd9STetsuo Handa  * tomoyo_write_answer - Write the supervisor's decision.
219217fcfbd9STetsuo Handa  *
219317fcfbd9STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
219417fcfbd9STetsuo Handa  *
219517fcfbd9STetsuo Handa  * Returns 0 on success, -EINVAL otherwise.
219617fcfbd9STetsuo Handa  */
219717fcfbd9STetsuo Handa static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
219817fcfbd9STetsuo Handa {
219917fcfbd9STetsuo Handa 	char *data = head->write_buf;
220017fcfbd9STetsuo Handa 	struct list_head *tmp;
220117fcfbd9STetsuo Handa 	unsigned int serial;
220217fcfbd9STetsuo Handa 	unsigned int answer;
220317fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
220417fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2205e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
220617fcfbd9STetsuo Handa 		ptr->timer = 0;
220717fcfbd9STetsuo Handa 	}
220817fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
220917fcfbd9STetsuo Handa 	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
221017fcfbd9STetsuo Handa 		return -EINVAL;
221117fcfbd9STetsuo Handa 	spin_lock(&tomoyo_query_list_lock);
221217fcfbd9STetsuo Handa 	list_for_each(tmp, &tomoyo_query_list) {
2213e2bf6907STetsuo Handa 		struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
221417fcfbd9STetsuo Handa 		if (ptr->serial != serial)
221517fcfbd9STetsuo Handa 			continue;
221617fcfbd9STetsuo Handa 		ptr->answer = answer;
22176041e834STetsuo Handa 		/* Remove from tomoyo_query_list. */
22186041e834STetsuo Handa 		if (ptr->answer)
22196041e834STetsuo Handa 			list_del_init(&ptr->list);
222017fcfbd9STetsuo Handa 		break;
222117fcfbd9STetsuo Handa 	}
222217fcfbd9STetsuo Handa 	spin_unlock(&tomoyo_query_list_lock);
222317fcfbd9STetsuo Handa 	return 0;
222417fcfbd9STetsuo Handa }
222517fcfbd9STetsuo Handa 
222617fcfbd9STetsuo Handa /**
22279590837bSKentaro Takeda  * tomoyo_read_version: Get version.
22289590837bSKentaro Takeda  *
22299590837bSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
22309590837bSKentaro Takeda  *
22319590837bSKentaro Takeda  * Returns version information.
22329590837bSKentaro Takeda  */
22338fbe71f0STetsuo Handa static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22349590837bSKentaro Takeda {
2235f23571e8STetsuo Handa 	if (!head->r.eof) {
2236843d183cSTetsuo Handa 		tomoyo_io_printf(head, "2.5.0");
2237f23571e8STetsuo Handa 		head->r.eof = true;
22389590837bSKentaro Takeda 	}
22399590837bSKentaro Takeda }
22409590837bSKentaro Takeda 
2241b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2242b22b8b9fSTetsuo Handa static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2243b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_UPDATES]    = "update:",
2244b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2245b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2246b22b8b9fSTetsuo Handa 	[TOMOYO_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2247b22b8b9fSTetsuo Handa };
2248b22b8b9fSTetsuo Handa 
2249b22b8b9fSTetsuo Handa /* String table for /sys/kernel/security/tomoyo/stat interface. */
2250b22b8b9fSTetsuo Handa static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2251b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_POLICY] = "policy:",
2252b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_AUDIT]  = "audit log:",
2253b22b8b9fSTetsuo Handa 	[TOMOYO_MEMORY_QUERY]  = "query message:",
2254b22b8b9fSTetsuo Handa };
2255b22b8b9fSTetsuo Handa 
2256b22b8b9fSTetsuo Handa /* Timestamp counter for last updated. */
2257b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2258b22b8b9fSTetsuo Handa /* Counter for number of updates. */
2259b22b8b9fSTetsuo Handa static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2260b22b8b9fSTetsuo Handa 
2261b22b8b9fSTetsuo Handa /**
2262b22b8b9fSTetsuo Handa  * tomoyo_update_stat - Update statistic counters.
2263b22b8b9fSTetsuo Handa  *
2264b22b8b9fSTetsuo Handa  * @index: Index for policy type.
2265b22b8b9fSTetsuo Handa  *
2266b22b8b9fSTetsuo Handa  * Returns nothing.
2267b22b8b9fSTetsuo Handa  */
2268b22b8b9fSTetsuo Handa void tomoyo_update_stat(const u8 index)
2269b22b8b9fSTetsuo Handa {
2270b22b8b9fSTetsuo Handa 	struct timeval tv;
2271b22b8b9fSTetsuo Handa 	do_gettimeofday(&tv);
2272b22b8b9fSTetsuo Handa 	/*
2273b22b8b9fSTetsuo Handa 	 * I don't use atomic operations because race condition is not fatal.
2274b22b8b9fSTetsuo Handa 	 */
2275b22b8b9fSTetsuo Handa 	tomoyo_stat_updated[index]++;
2276b22b8b9fSTetsuo Handa 	tomoyo_stat_modified[index] = tv.tv_sec;
2277b22b8b9fSTetsuo Handa }
2278b22b8b9fSTetsuo Handa 
2279b22b8b9fSTetsuo Handa /**
2280b22b8b9fSTetsuo Handa  * tomoyo_read_stat - Read statistic data.
2281b22b8b9fSTetsuo Handa  *
2282b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2283b22b8b9fSTetsuo Handa  *
2284b22b8b9fSTetsuo Handa  * Returns nothing.
2285b22b8b9fSTetsuo Handa  */
2286b22b8b9fSTetsuo Handa static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2287b22b8b9fSTetsuo Handa {
2288b22b8b9fSTetsuo Handa 	u8 i;
2289b22b8b9fSTetsuo Handa 	unsigned int total = 0;
2290b22b8b9fSTetsuo Handa 	if (head->r.eof)
2291b22b8b9fSTetsuo Handa 		return;
2292b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2293b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Policy %-30s %10u",
2294b22b8b9fSTetsuo Handa 				 tomoyo_policy_headers[i],
2295b22b8b9fSTetsuo Handa 				 tomoyo_stat_updated[i]);
2296b22b8b9fSTetsuo Handa 		if (tomoyo_stat_modified[i]) {
2297b22b8b9fSTetsuo Handa 			struct tomoyo_time stamp;
2298b22b8b9fSTetsuo Handa 			tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2299b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2300b22b8b9fSTetsuo Handa 					 "%02u:%02u:%02u)",
2301b22b8b9fSTetsuo Handa 					 stamp.year, stamp.month, stamp.day,
2302b22b8b9fSTetsuo Handa 					 stamp.hour, stamp.min, stamp.sec);
2303b22b8b9fSTetsuo Handa 		}
2304b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2305b22b8b9fSTetsuo Handa 	}
2306b22b8b9fSTetsuo Handa 	for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2307b22b8b9fSTetsuo Handa 		unsigned int used = tomoyo_memory_used[i];
2308b22b8b9fSTetsuo Handa 		total += used;
2309b22b8b9fSTetsuo Handa 		tomoyo_io_printf(head, "Memory used by %-22s %10u",
2310b22b8b9fSTetsuo Handa 				 tomoyo_memory_headers[i], used);
2311b22b8b9fSTetsuo Handa 		used = tomoyo_memory_quota[i];
2312b22b8b9fSTetsuo Handa 		if (used)
2313b22b8b9fSTetsuo Handa 			tomoyo_io_printf(head, " (Quota: %10u)", used);
2314b22b8b9fSTetsuo Handa 		tomoyo_set_lf(head);
2315b22b8b9fSTetsuo Handa 	}
2316b22b8b9fSTetsuo Handa 	tomoyo_io_printf(head, "Total memory used:                    %10u\n",
2317b22b8b9fSTetsuo Handa 			 total);
2318b22b8b9fSTetsuo Handa 	head->r.eof = true;
2319b22b8b9fSTetsuo Handa }
2320b22b8b9fSTetsuo Handa 
2321b22b8b9fSTetsuo Handa /**
2322b22b8b9fSTetsuo Handa  * tomoyo_write_stat - Set memory quota.
2323b22b8b9fSTetsuo Handa  *
2324b22b8b9fSTetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2325b22b8b9fSTetsuo Handa  *
2326b22b8b9fSTetsuo Handa  * Returns 0.
2327b22b8b9fSTetsuo Handa  */
2328b22b8b9fSTetsuo Handa static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2329b22b8b9fSTetsuo Handa {
2330b22b8b9fSTetsuo Handa 	char *data = head->write_buf;
2331b22b8b9fSTetsuo Handa 	u8 i;
2332b22b8b9fSTetsuo Handa 	if (tomoyo_str_starts(&data, "Memory used by "))
2333b22b8b9fSTetsuo Handa 		for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2334b22b8b9fSTetsuo Handa 			if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2335b22b8b9fSTetsuo Handa 				sscanf(data, "%u", &tomoyo_memory_quota[i]);
2336b22b8b9fSTetsuo Handa 	return 0;
2337b22b8b9fSTetsuo Handa }
2338b22b8b9fSTetsuo Handa 
23399590837bSKentaro Takeda /**
23409590837bSKentaro Takeda  * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
23419590837bSKentaro Takeda  *
23429590837bSKentaro Takeda  * @type: Type of interface.
23439590837bSKentaro Takeda  * @file: Pointer to "struct file".
23449590837bSKentaro Takeda  *
23452e503bbbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
23469590837bSKentaro Takeda  */
2347c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file)
23489590837bSKentaro Takeda {
23494e5d6f7eSTetsuo Handa 	struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
23509590837bSKentaro Takeda 
23519590837bSKentaro Takeda 	if (!head)
23529590837bSKentaro Takeda 		return -ENOMEM;
23539590837bSKentaro Takeda 	mutex_init(&head->io_sem);
235417fcfbd9STetsuo Handa 	head->type = type;
23559590837bSKentaro Takeda 	switch (type) {
23569590837bSKentaro Takeda 	case TOMOYO_DOMAINPOLICY:
23579590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/domain_policy */
2358e2bf6907STetsuo Handa 		head->write = tomoyo_write_domain;
2359e2bf6907STetsuo Handa 		head->read = tomoyo_read_domain;
23609590837bSKentaro Takeda 		break;
23619590837bSKentaro Takeda 	case TOMOYO_EXCEPTIONPOLICY:
23629590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/exception_policy */
2363e2bf6907STetsuo Handa 		head->write = tomoyo_write_exception;
2364e2bf6907STetsuo Handa 		head->read = tomoyo_read_exception;
23659590837bSKentaro Takeda 		break;
2366eadd99ccSTetsuo Handa 	case TOMOYO_AUDIT:
2367eadd99ccSTetsuo Handa 		/* /sys/kernel/security/tomoyo/audit */
2368eadd99ccSTetsuo Handa 		head->poll = tomoyo_poll_log;
2369eadd99ccSTetsuo Handa 		head->read = tomoyo_read_log;
2370eadd99ccSTetsuo Handa 		break;
23719590837bSKentaro Takeda 	case TOMOYO_PROCESS_STATUS:
23729590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/.process_status */
23739590837bSKentaro Takeda 		head->write = tomoyo_write_pid;
23749590837bSKentaro Takeda 		head->read = tomoyo_read_pid;
23759590837bSKentaro Takeda 		break;
23769590837bSKentaro Takeda 	case TOMOYO_VERSION:
23779590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/version */
23789590837bSKentaro Takeda 		head->read = tomoyo_read_version;
23799590837bSKentaro Takeda 		head->readbuf_size = 128;
23809590837bSKentaro Takeda 		break;
2381b22b8b9fSTetsuo Handa 	case TOMOYO_STAT:
2382b22b8b9fSTetsuo Handa 		/* /sys/kernel/security/tomoyo/stat */
2383b22b8b9fSTetsuo Handa 		head->write = tomoyo_write_stat;
2384b22b8b9fSTetsuo Handa 		head->read = tomoyo_read_stat;
2385b22b8b9fSTetsuo Handa 		head->readbuf_size = 1024;
23869590837bSKentaro Takeda 		break;
23879590837bSKentaro Takeda 	case TOMOYO_PROFILE:
23889590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/profile */
23899590837bSKentaro Takeda 		head->write = tomoyo_write_profile;
23909590837bSKentaro Takeda 		head->read = tomoyo_read_profile;
23919590837bSKentaro Takeda 		break;
239217fcfbd9STetsuo Handa 	case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
239317fcfbd9STetsuo Handa 		head->poll = tomoyo_poll_query;
239417fcfbd9STetsuo Handa 		head->write = tomoyo_write_answer;
239517fcfbd9STetsuo Handa 		head->read = tomoyo_read_query;
239617fcfbd9STetsuo Handa 		break;
23979590837bSKentaro Takeda 	case TOMOYO_MANAGER:
23989590837bSKentaro Takeda 		/* /sys/kernel/security/tomoyo/manager */
2399e2bf6907STetsuo Handa 		head->write = tomoyo_write_manager;
2400e2bf6907STetsuo Handa 		head->read = tomoyo_read_manager;
24019590837bSKentaro Takeda 		break;
24029590837bSKentaro Takeda 	}
24039590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_READ)) {
24049590837bSKentaro Takeda 		/*
24059590837bSKentaro Takeda 		 * No need to allocate read_buf since it is not opened
24069590837bSKentaro Takeda 		 * for reading.
24079590837bSKentaro Takeda 		 */
24089590837bSKentaro Takeda 		head->read = NULL;
240917fcfbd9STetsuo Handa 		head->poll = NULL;
241017fcfbd9STetsuo Handa 	} else if (!head->poll) {
241117fcfbd9STetsuo Handa 		/* Don't allocate read_buf for poll() access. */
24129590837bSKentaro Takeda 		if (!head->readbuf_size)
24139590837bSKentaro Takeda 			head->readbuf_size = 4096 * 2;
24144e5d6f7eSTetsuo Handa 		head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
24159590837bSKentaro Takeda 		if (!head->read_buf) {
24168e2d39a1STetsuo Handa 			kfree(head);
24179590837bSKentaro Takeda 			return -ENOMEM;
24189590837bSKentaro Takeda 		}
24199590837bSKentaro Takeda 	}
24209590837bSKentaro Takeda 	if (!(file->f_mode & FMODE_WRITE)) {
24219590837bSKentaro Takeda 		/*
24229590837bSKentaro Takeda 		 * No need to allocate write_buf since it is not opened
24239590837bSKentaro Takeda 		 * for writing.
24249590837bSKentaro Takeda 		 */
24259590837bSKentaro Takeda 		head->write = NULL;
24269590837bSKentaro Takeda 	} else if (head->write) {
24279590837bSKentaro Takeda 		head->writebuf_size = 4096 * 2;
24284e5d6f7eSTetsuo Handa 		head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
24299590837bSKentaro Takeda 		if (!head->write_buf) {
24308e2d39a1STetsuo Handa 			kfree(head->read_buf);
24318e2d39a1STetsuo Handa 			kfree(head);
24329590837bSKentaro Takeda 			return -ENOMEM;
24339590837bSKentaro Takeda 		}
24349590837bSKentaro Takeda 	}
24359590837bSKentaro Takeda 	/*
243617fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , increment the
243717fcfbd9STetsuo Handa 	 * observer counter.
243817fcfbd9STetsuo Handa 	 * The obserber counter is used by tomoyo_supervisor() to see if
243917fcfbd9STetsuo Handa 	 * there is some process monitoring /sys/kernel/security/tomoyo/query.
244017fcfbd9STetsuo Handa 	 */
24417c75964fSTetsuo Handa 	if (type == TOMOYO_QUERY)
244217fcfbd9STetsuo Handa 		atomic_inc(&tomoyo_query_observers);
24432e503bbbSTetsuo Handa 	file->private_data = head;
24442e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, true);
24459590837bSKentaro Takeda 	return 0;
24469590837bSKentaro Takeda }
24479590837bSKentaro Takeda 
24489590837bSKentaro Takeda /**
24490849e3baSTetsuo Handa  * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
24500849e3baSTetsuo Handa  *
24510849e3baSTetsuo Handa  * @file: Pointer to "struct file".
24526041e834STetsuo Handa  * @wait: Pointer to "poll_table". Maybe NULL.
24530849e3baSTetsuo Handa  *
24546041e834STetsuo Handa  * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write,
24556041e834STetsuo Handa  * POLLOUT | POLLWRNORM otherwise.
24560849e3baSTetsuo Handa  */
24576041e834STetsuo Handa unsigned int tomoyo_poll_control(struct file *file, poll_table *wait)
24580849e3baSTetsuo Handa {
24590849e3baSTetsuo Handa 	struct tomoyo_io_buffer *head = file->private_data;
24606041e834STetsuo Handa 	if (head->poll)
24616041e834STetsuo Handa 		return head->poll(file, wait) | POLLOUT | POLLWRNORM;
24626041e834STetsuo Handa 	return POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM;
24630849e3baSTetsuo Handa }
24640849e3baSTetsuo Handa 
24650849e3baSTetsuo Handa /**
2466bd03a3e4STetsuo Handa  * tomoyo_set_namespace_cursor - Set namespace to read.
2467bd03a3e4STetsuo Handa  *
2468bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2469bd03a3e4STetsuo Handa  *
2470bd03a3e4STetsuo Handa  * Returns nothing.
2471bd03a3e4STetsuo Handa  */
2472bd03a3e4STetsuo Handa static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2473bd03a3e4STetsuo Handa {
2474bd03a3e4STetsuo Handa 	struct list_head *ns;
2475bd03a3e4STetsuo Handa 	if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2476bd03a3e4STetsuo Handa 	    head->type != TOMOYO_PROFILE)
2477bd03a3e4STetsuo Handa 		return;
2478bd03a3e4STetsuo Handa 	/*
2479bd03a3e4STetsuo Handa 	 * If this is the first read, or reading previous namespace finished
2480bd03a3e4STetsuo Handa 	 * and has more namespaces to read, update the namespace cursor.
2481bd03a3e4STetsuo Handa 	 */
2482bd03a3e4STetsuo Handa 	ns = head->r.ns;
2483bd03a3e4STetsuo Handa 	if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2484bd03a3e4STetsuo Handa 		/* Clearing is OK because tomoyo_flush() returned true. */
2485bd03a3e4STetsuo Handa 		memset(&head->r, 0, sizeof(head->r));
2486bd03a3e4STetsuo Handa 		head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2487bd03a3e4STetsuo Handa 	}
2488bd03a3e4STetsuo Handa }
2489bd03a3e4STetsuo Handa 
2490bd03a3e4STetsuo Handa /**
2491bd03a3e4STetsuo Handa  * tomoyo_has_more_namespace - Check for unread namespaces.
2492bd03a3e4STetsuo Handa  *
2493bd03a3e4STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
2494bd03a3e4STetsuo Handa  *
2495bd03a3e4STetsuo Handa  * Returns true if we have more entries to print, false otherwise.
2496bd03a3e4STetsuo Handa  */
2497bd03a3e4STetsuo Handa static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2498bd03a3e4STetsuo Handa {
2499bd03a3e4STetsuo Handa 	return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2500bd03a3e4STetsuo Handa 		head->type == TOMOYO_PROFILE) && head->r.eof &&
2501bd03a3e4STetsuo Handa 		head->r.ns->next != &tomoyo_namespace_list;
2502bd03a3e4STetsuo Handa }
2503bd03a3e4STetsuo Handa 
2504bd03a3e4STetsuo Handa /**
25059590837bSKentaro Takeda  * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
25069590837bSKentaro Takeda  *
25070df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25089590837bSKentaro Takeda  * @buffer:     Poiner to buffer to write to.
25099590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25109590837bSKentaro Takeda  *
25119590837bSKentaro Takeda  * Returns bytes read on success, negative value otherwise.
25129590837bSKentaro Takeda  */
25132c47ab93STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
25149590837bSKentaro Takeda 			    const int buffer_len)
25159590837bSKentaro Takeda {
2516f23571e8STetsuo Handa 	int len;
25172e503bbbSTetsuo Handa 	int idx;
25189590837bSKentaro Takeda 
25199590837bSKentaro Takeda 	if (!head->read)
25209590837bSKentaro Takeda 		return -ENOSYS;
25219590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25229590837bSKentaro Takeda 		return -EINTR;
2523f23571e8STetsuo Handa 	head->read_user_buf = buffer;
2524f23571e8STetsuo Handa 	head->read_user_buf_avail = buffer_len;
25252e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
2526f23571e8STetsuo Handa 	if (tomoyo_flush(head))
25279590837bSKentaro Takeda 		/* Call the policy handler. */
2528bd03a3e4STetsuo Handa 		do {
2529bd03a3e4STetsuo Handa 			tomoyo_set_namespace_cursor(head);
25308fbe71f0STetsuo Handa 			head->read(head);
2531bd03a3e4STetsuo Handa 		} while (tomoyo_flush(head) &&
2532bd03a3e4STetsuo Handa 			 tomoyo_has_more_namespace(head));
25332e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
2534f23571e8STetsuo Handa 	len = head->read_user_buf - buffer;
25359590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
25369590837bSKentaro Takeda 	return len;
25379590837bSKentaro Takeda }
25389590837bSKentaro Takeda 
25399590837bSKentaro Takeda /**
2540bd03a3e4STetsuo Handa  * tomoyo_parse_policy - Parse a policy line.
2541bd03a3e4STetsuo Handa  *
2542bd03a3e4STetsuo Handa  * @head: Poiter to "struct tomoyo_io_buffer".
2543bd03a3e4STetsuo Handa  * @line: Line to parse.
2544bd03a3e4STetsuo Handa  *
2545bd03a3e4STetsuo Handa  * Returns 0 on success, negative value otherwise.
2546bd03a3e4STetsuo Handa  *
2547bd03a3e4STetsuo Handa  * Caller holds tomoyo_read_lock().
2548bd03a3e4STetsuo Handa  */
2549bd03a3e4STetsuo Handa static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2550bd03a3e4STetsuo Handa {
2551bd03a3e4STetsuo Handa 	/* Delete request? */
2552bd03a3e4STetsuo Handa 	head->w.is_delete = !strncmp(line, "delete ", 7);
2553bd03a3e4STetsuo Handa 	if (head->w.is_delete)
2554bd03a3e4STetsuo Handa 		memmove(line, line + 7, strlen(line + 7) + 1);
2555bd03a3e4STetsuo Handa 	/* Selecting namespace to update. */
2556bd03a3e4STetsuo Handa 	if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2557bd03a3e4STetsuo Handa 	    head->type == TOMOYO_PROFILE) {
2558bd03a3e4STetsuo Handa 		if (*line == '<') {
2559bd03a3e4STetsuo Handa 			char *cp = strchr(line, ' ');
2560bd03a3e4STetsuo Handa 			if (cp) {
2561bd03a3e4STetsuo Handa 				*cp++ = '\0';
2562bd03a3e4STetsuo Handa 				head->w.ns = tomoyo_assign_namespace(line);
2563bd03a3e4STetsuo Handa 				memmove(line, cp, strlen(cp) + 1);
2564bd03a3e4STetsuo Handa 			} else
2565bd03a3e4STetsuo Handa 				head->w.ns = NULL;
2566bd03a3e4STetsuo Handa 		} else
2567bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2568bd03a3e4STetsuo Handa 		/* Don't allow updating if namespace is invalid. */
2569bd03a3e4STetsuo Handa 		if (!head->w.ns)
2570bd03a3e4STetsuo Handa 			return -ENOENT;
2571bd03a3e4STetsuo Handa 	}
2572bd03a3e4STetsuo Handa 	/* Do the update. */
2573bd03a3e4STetsuo Handa 	return head->write(head);
2574bd03a3e4STetsuo Handa }
2575bd03a3e4STetsuo Handa 
2576bd03a3e4STetsuo Handa /**
25779590837bSKentaro Takeda  * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
25789590837bSKentaro Takeda  *
25790df7e8b8STetsuo Handa  * @head:       Pointer to "struct tomoyo_io_buffer".
25809590837bSKentaro Takeda  * @buffer:     Pointer to buffer to read from.
25819590837bSKentaro Takeda  * @buffer_len: Size of @buffer.
25829590837bSKentaro Takeda  *
25839590837bSKentaro Takeda  * Returns @buffer_len on success, negative value otherwise.
25849590837bSKentaro Takeda  */
25852c47ab93STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
25860df7e8b8STetsuo Handa 			     const char __user *buffer, const int buffer_len)
25879590837bSKentaro Takeda {
25889590837bSKentaro Takeda 	int error = buffer_len;
2589bd03a3e4STetsuo Handa 	size_t avail_len = buffer_len;
25909590837bSKentaro Takeda 	char *cp0 = head->write_buf;
25912e503bbbSTetsuo Handa 	int idx;
25929590837bSKentaro Takeda 	if (!head->write)
25939590837bSKentaro Takeda 		return -ENOSYS;
25949590837bSKentaro Takeda 	if (!access_ok(VERIFY_READ, buffer, buffer_len))
25959590837bSKentaro Takeda 		return -EFAULT;
25969590837bSKentaro Takeda 	if (mutex_lock_interruptible(&head->io_sem))
25979590837bSKentaro Takeda 		return -EINTR;
2598e0b057b4STetsuo Handa 	head->read_user_buf_avail = 0;
25992e503bbbSTetsuo Handa 	idx = tomoyo_read_lock();
26009590837bSKentaro Takeda 	/* Read a line and dispatch it to the policy handler. */
26019590837bSKentaro Takeda 	while (avail_len > 0) {
26029590837bSKentaro Takeda 		char c;
26030df7e8b8STetsuo Handa 		if (head->w.avail >= head->writebuf_size - 1) {
2604bd03a3e4STetsuo Handa 			const int len = head->writebuf_size * 2;
2605bd03a3e4STetsuo Handa 			char *cp = kzalloc(len, GFP_NOFS);
2606bd03a3e4STetsuo Handa 			if (!cp) {
26079590837bSKentaro Takeda 				error = -ENOMEM;
26089590837bSKentaro Takeda 				break;
2609bd03a3e4STetsuo Handa 			}
2610bd03a3e4STetsuo Handa 			memmove(cp, cp0, head->w.avail);
2611bd03a3e4STetsuo Handa 			kfree(cp0);
2612bd03a3e4STetsuo Handa 			head->write_buf = cp;
2613bd03a3e4STetsuo Handa 			cp0 = cp;
2614bd03a3e4STetsuo Handa 			head->writebuf_size = len;
2615bd03a3e4STetsuo Handa 		}
2616bd03a3e4STetsuo Handa 		if (get_user(c, buffer)) {
26179590837bSKentaro Takeda 			error = -EFAULT;
26189590837bSKentaro Takeda 			break;
26199590837bSKentaro Takeda 		}
26209590837bSKentaro Takeda 		buffer++;
26219590837bSKentaro Takeda 		avail_len--;
26220df7e8b8STetsuo Handa 		cp0[head->w.avail++] = c;
26239590837bSKentaro Takeda 		if (c != '\n')
26249590837bSKentaro Takeda 			continue;
26250df7e8b8STetsuo Handa 		cp0[head->w.avail - 1] = '\0';
26260df7e8b8STetsuo Handa 		head->w.avail = 0;
26279590837bSKentaro Takeda 		tomoyo_normalize_line(cp0);
2628bd03a3e4STetsuo Handa 		if (!strcmp(cp0, "reset")) {
2629bd03a3e4STetsuo Handa 			head->w.ns = &tomoyo_kernel_namespace;
2630bd03a3e4STetsuo Handa 			head->w.domain = NULL;
2631bd03a3e4STetsuo Handa 			memset(&head->r, 0, sizeof(head->r));
2632bd03a3e4STetsuo Handa 			continue;
26339590837bSKentaro Takeda 		}
2634bd03a3e4STetsuo Handa 		/* Don't allow updating policies by non manager programs. */
2635bd03a3e4STetsuo Handa 		switch (head->type) {
2636bd03a3e4STetsuo Handa 		case TOMOYO_PROCESS_STATUS:
2637bd03a3e4STetsuo Handa 			/* This does not write anything. */
2638bd03a3e4STetsuo Handa 			break;
2639bd03a3e4STetsuo Handa 		case TOMOYO_DOMAINPOLICY:
2640bd03a3e4STetsuo Handa 			if (tomoyo_select_domain(head, cp0))
2641bd03a3e4STetsuo Handa 				continue;
2642bd03a3e4STetsuo Handa 			/* fall through */
2643bd03a3e4STetsuo Handa 		case TOMOYO_EXCEPTIONPOLICY:
2644bd03a3e4STetsuo Handa 			if (!strcmp(cp0, "select transition_only")) {
2645bd03a3e4STetsuo Handa 				head->r.print_transition_related_only = true;
2646bd03a3e4STetsuo Handa 				continue;
2647bd03a3e4STetsuo Handa 			}
2648bd03a3e4STetsuo Handa 			/* fall through */
2649bd03a3e4STetsuo Handa 		default:
2650bd03a3e4STetsuo Handa 			if (!tomoyo_manager()) {
2651bd03a3e4STetsuo Handa 				error = -EPERM;
2652bd03a3e4STetsuo Handa 				goto out;
2653bd03a3e4STetsuo Handa 			}
2654bd03a3e4STetsuo Handa 		}
2655bd03a3e4STetsuo Handa 		switch (tomoyo_parse_policy(head, cp0)) {
2656bd03a3e4STetsuo Handa 		case -EPERM:
2657bd03a3e4STetsuo Handa 			error = -EPERM;
2658bd03a3e4STetsuo Handa 			goto out;
2659b22b8b9fSTetsuo Handa 		case 0:
2660b22b8b9fSTetsuo Handa 			switch (head->type) {
2661b22b8b9fSTetsuo Handa 			case TOMOYO_DOMAINPOLICY:
2662b22b8b9fSTetsuo Handa 			case TOMOYO_EXCEPTIONPOLICY:
2663b22b8b9fSTetsuo Handa 			case TOMOYO_STAT:
2664b22b8b9fSTetsuo Handa 			case TOMOYO_PROFILE:
2665b22b8b9fSTetsuo Handa 			case TOMOYO_MANAGER:
2666b22b8b9fSTetsuo Handa 				tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2667b22b8b9fSTetsuo Handa 				break;
2668b22b8b9fSTetsuo Handa 			default:
2669b22b8b9fSTetsuo Handa 				break;
2670b22b8b9fSTetsuo Handa 			}
2671b22b8b9fSTetsuo Handa 			break;
2672bd03a3e4STetsuo Handa 		}
2673bd03a3e4STetsuo Handa 	}
2674bd03a3e4STetsuo Handa out:
26752e503bbbSTetsuo Handa 	tomoyo_read_unlock(idx);
26769590837bSKentaro Takeda 	mutex_unlock(&head->io_sem);
26779590837bSKentaro Takeda 	return error;
26789590837bSKentaro Takeda }
26799590837bSKentaro Takeda 
26809590837bSKentaro Takeda /**
26819590837bSKentaro Takeda  * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
26829590837bSKentaro Takeda  *
26830df7e8b8STetsuo Handa  * @head: Pointer to "struct tomoyo_io_buffer".
26849590837bSKentaro Takeda  */
2685*e53cfda5SAl Viro void tomoyo_close_control(struct tomoyo_io_buffer *head)
26869590837bSKentaro Takeda {
268717fcfbd9STetsuo Handa 	/*
268817fcfbd9STetsuo Handa 	 * If the file is /sys/kernel/security/tomoyo/query , decrement the
268917fcfbd9STetsuo Handa 	 * observer counter.
269017fcfbd9STetsuo Handa 	 */
26912e503bbbSTetsuo Handa 	if (head->type == TOMOYO_QUERY &&
26922e503bbbSTetsuo Handa 	    atomic_dec_and_test(&tomoyo_query_observers))
26932e503bbbSTetsuo Handa 		wake_up_all(&tomoyo_answer_wait);
26942e503bbbSTetsuo Handa 	tomoyo_notify_gc(head, false);
26959590837bSKentaro Takeda }
26969590837bSKentaro Takeda 
26979590837bSKentaro Takeda /**
2698c3ef1500STetsuo Handa  * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
26999590837bSKentaro Takeda  */
2700c3ef1500STetsuo Handa void tomoyo_check_profile(void)
27019590837bSKentaro Takeda {
2702c3ef1500STetsuo Handa 	struct tomoyo_domain_info *domain;
2703c3ef1500STetsuo Handa 	const int idx = tomoyo_read_lock();
2704c3ef1500STetsuo Handa 	tomoyo_policy_loaded = true;
2705843d183cSTetsuo Handa 	printk(KERN_INFO "TOMOYO: 2.5.0\n");
2706c3ef1500STetsuo Handa 	list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2707c3ef1500STetsuo Handa 		const u8 profile = domain->profile;
2708bd03a3e4STetsuo Handa 		const struct tomoyo_policy_namespace *ns = domain->ns;
2709843d183cSTetsuo Handa 		if (ns->profile_version != 20110903)
2710bd03a3e4STetsuo Handa 			printk(KERN_ERR
2711bd03a3e4STetsuo Handa 			       "Profile version %u is not supported.\n",
2712bd03a3e4STetsuo Handa 			       ns->profile_version);
2713bd03a3e4STetsuo Handa 		else if (!ns->profile_ptr[profile])
2714bd03a3e4STetsuo Handa 			printk(KERN_ERR
2715bd03a3e4STetsuo Handa 			       "Profile %u (used by '%s') is not defined.\n",
2716c3ef1500STetsuo Handa 			       profile, domain->domainname->name);
2717bd03a3e4STetsuo Handa 		else
2718bd03a3e4STetsuo Handa 			continue;
2719bd03a3e4STetsuo Handa 		printk(KERN_ERR
2720843d183cSTetsuo Handa 		       "Userland tools for TOMOYO 2.5 must be installed and "
2721bd03a3e4STetsuo Handa 		       "policy must be initialized.\n");
2722843d183cSTetsuo Handa 		printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
2723bd03a3e4STetsuo Handa 		       "for more information.\n");
2724bd03a3e4STetsuo Handa 		panic("STOP!");
27259590837bSKentaro Takeda 	}
2726c3ef1500STetsuo Handa 	tomoyo_read_unlock(idx);
2727c3ef1500STetsuo Handa 	printk(KERN_INFO "Mandatory Access Control activated.\n");
27289590837bSKentaro Takeda }
2729efe836abSTetsuo Handa 
2730efe836abSTetsuo Handa /**
2731efe836abSTetsuo Handa  * tomoyo_load_builtin_policy - Load built-in policy.
2732efe836abSTetsuo Handa  *
2733efe836abSTetsuo Handa  * Returns nothing.
2734efe836abSTetsuo Handa  */
2735efe836abSTetsuo Handa void __init tomoyo_load_builtin_policy(void)
2736efe836abSTetsuo Handa {
2737efe836abSTetsuo Handa 	/*
2738efe836abSTetsuo Handa 	 * This include file is manually created and contains built-in policy
2739efe836abSTetsuo Handa 	 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2740efe836abSTetsuo Handa 	 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2741efe836abSTetsuo Handa 	 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2742efe836abSTetsuo Handa 	 */
2743efe836abSTetsuo Handa #include "builtin-policy.h"
2744efe836abSTetsuo Handa 	u8 i;
2745efe836abSTetsuo Handa 	const int idx = tomoyo_read_lock();
2746efe836abSTetsuo Handa 	for (i = 0; i < 5; i++) {
2747efe836abSTetsuo Handa 		struct tomoyo_io_buffer head = { };
2748efe836abSTetsuo Handa 		char *start = "";
2749efe836abSTetsuo Handa 		switch (i) {
2750efe836abSTetsuo Handa 		case 0:
2751efe836abSTetsuo Handa 			start = tomoyo_builtin_profile;
2752efe836abSTetsuo Handa 			head.type = TOMOYO_PROFILE;
2753efe836abSTetsuo Handa 			head.write = tomoyo_write_profile;
2754efe836abSTetsuo Handa 			break;
2755efe836abSTetsuo Handa 		case 1:
2756efe836abSTetsuo Handa 			start = tomoyo_builtin_exception_policy;
2757efe836abSTetsuo Handa 			head.type = TOMOYO_EXCEPTIONPOLICY;
2758efe836abSTetsuo Handa 			head.write = tomoyo_write_exception;
2759efe836abSTetsuo Handa 			break;
2760efe836abSTetsuo Handa 		case 2:
2761efe836abSTetsuo Handa 			start = tomoyo_builtin_domain_policy;
2762efe836abSTetsuo Handa 			head.type = TOMOYO_DOMAINPOLICY;
2763efe836abSTetsuo Handa 			head.write = tomoyo_write_domain;
2764efe836abSTetsuo Handa 			break;
2765efe836abSTetsuo Handa 		case 3:
2766efe836abSTetsuo Handa 			start = tomoyo_builtin_manager;
2767efe836abSTetsuo Handa 			head.type = TOMOYO_MANAGER;
2768efe836abSTetsuo Handa 			head.write = tomoyo_write_manager;
2769efe836abSTetsuo Handa 			break;
2770efe836abSTetsuo Handa 		case 4:
2771efe836abSTetsuo Handa 			start = tomoyo_builtin_stat;
2772efe836abSTetsuo Handa 			head.type = TOMOYO_STAT;
2773efe836abSTetsuo Handa 			head.write = tomoyo_write_stat;
2774efe836abSTetsuo Handa 			break;
2775efe836abSTetsuo Handa 		}
2776efe836abSTetsuo Handa 		while (1) {
2777efe836abSTetsuo Handa 			char *end = strchr(start, '\n');
2778efe836abSTetsuo Handa 			if (!end)
2779efe836abSTetsuo Handa 				break;
2780efe836abSTetsuo Handa 			*end = '\0';
2781efe836abSTetsuo Handa 			tomoyo_normalize_line(start);
2782efe836abSTetsuo Handa 			head.write_buf = start;
2783efe836abSTetsuo Handa 			tomoyo_parse_policy(&head, start);
2784efe836abSTetsuo Handa 			start = end + 1;
2785efe836abSTetsuo Handa 		}
2786efe836abSTetsuo Handa 	}
2787efe836abSTetsuo Handa 	tomoyo_read_unlock(idx);
27880e4ae0e0STetsuo Handa #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
27890e4ae0e0STetsuo Handa 	tomoyo_check_profile();
27900e4ae0e0STetsuo Handa #endif
2791efe836abSTetsuo Handa }
2792