xref: /OK3568_Linux_fs/kernel/security/tomoyo/common.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * security/tomoyo/common.h
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Header file for TOMOYO.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2005-2011  NTT DATA CORPORATION
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef _SECURITY_TOMOYO_COMMON_H
11*4882a593Smuzhiyun #define _SECURITY_TOMOYO_COMMON_H
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define pr_fmt(fmt) fmt
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/ctype.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun #include <linux/mm.h>
18*4882a593Smuzhiyun #include <linux/file.h>
19*4882a593Smuzhiyun #include <linux/kmod.h>
20*4882a593Smuzhiyun #include <linux/fs.h>
21*4882a593Smuzhiyun #include <linux/sched.h>
22*4882a593Smuzhiyun #include <linux/namei.h>
23*4882a593Smuzhiyun #include <linux/mount.h>
24*4882a593Smuzhiyun #include <linux/list.h>
25*4882a593Smuzhiyun #include <linux/cred.h>
26*4882a593Smuzhiyun #include <linux/poll.h>
27*4882a593Smuzhiyun #include <linux/binfmts.h>
28*4882a593Smuzhiyun #include <linux/highmem.h>
29*4882a593Smuzhiyun #include <linux/net.h>
30*4882a593Smuzhiyun #include <linux/inet.h>
31*4882a593Smuzhiyun #include <linux/in.h>
32*4882a593Smuzhiyun #include <linux/in6.h>
33*4882a593Smuzhiyun #include <linux/un.h>
34*4882a593Smuzhiyun #include <linux/lsm_hooks.h>
35*4882a593Smuzhiyun #include <net/sock.h>
36*4882a593Smuzhiyun #include <net/af_unix.h>
37*4882a593Smuzhiyun #include <net/ip.h>
38*4882a593Smuzhiyun #include <net/ipv6.h>
39*4882a593Smuzhiyun #include <net/udp.h>
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /********** Constants definitions. **********/
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * TOMOYO uses this hash only when appending a string into the string
45*4882a593Smuzhiyun  * table. Frequency of appending strings is very low. So we don't need
46*4882a593Smuzhiyun  * large (e.g. 64k) hash size. 256 will be sufficient.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #define TOMOYO_HASH_BITS  8
49*4882a593Smuzhiyun #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  * TOMOYO checks only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET.
53*4882a593Smuzhiyun  * Therefore, we don't need SOCK_MAX.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun #define TOMOYO_SOCK_MAX 6
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define TOMOYO_EXEC_TMPSIZE     4096
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* Garbage collector is trying to kfree() this element. */
60*4882a593Smuzhiyun #define TOMOYO_GC_IN_PROGRESS -1
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* Profile number is an integer between 0 and 255. */
63*4882a593Smuzhiyun #define TOMOYO_MAX_PROFILES 256
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Group number is an integer between 0 and 255. */
66*4882a593Smuzhiyun #define TOMOYO_MAX_ACL_GROUPS 256
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /* Index numbers for "struct tomoyo_condition". */
69*4882a593Smuzhiyun enum tomoyo_conditions_index {
70*4882a593Smuzhiyun 	TOMOYO_TASK_UID,             /* current_uid()   */
71*4882a593Smuzhiyun 	TOMOYO_TASK_EUID,            /* current_euid()  */
72*4882a593Smuzhiyun 	TOMOYO_TASK_SUID,            /* current_suid()  */
73*4882a593Smuzhiyun 	TOMOYO_TASK_FSUID,           /* current_fsuid() */
74*4882a593Smuzhiyun 	TOMOYO_TASK_GID,             /* current_gid()   */
75*4882a593Smuzhiyun 	TOMOYO_TASK_EGID,            /* current_egid()  */
76*4882a593Smuzhiyun 	TOMOYO_TASK_SGID,            /* current_sgid()  */
77*4882a593Smuzhiyun 	TOMOYO_TASK_FSGID,           /* current_fsgid() */
78*4882a593Smuzhiyun 	TOMOYO_TASK_PID,             /* sys_getpid()   */
79*4882a593Smuzhiyun 	TOMOYO_TASK_PPID,            /* sys_getppid()  */
80*4882a593Smuzhiyun 	TOMOYO_EXEC_ARGC,            /* "struct linux_binprm *"->argc */
81*4882a593Smuzhiyun 	TOMOYO_EXEC_ENVC,            /* "struct linux_binprm *"->envc */
82*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_SOCKET,       /* S_IFSOCK */
83*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_SYMLINK,      /* S_IFLNK */
84*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_FILE,         /* S_IFREG */
85*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_BLOCK_DEV,    /* S_IFBLK */
86*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_DIRECTORY,    /* S_IFDIR */
87*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_CHAR_DEV,     /* S_IFCHR */
88*4882a593Smuzhiyun 	TOMOYO_TYPE_IS_FIFO,         /* S_IFIFO */
89*4882a593Smuzhiyun 	TOMOYO_MODE_SETUID,          /* S_ISUID */
90*4882a593Smuzhiyun 	TOMOYO_MODE_SETGID,          /* S_ISGID */
91*4882a593Smuzhiyun 	TOMOYO_MODE_STICKY,          /* S_ISVTX */
92*4882a593Smuzhiyun 	TOMOYO_MODE_OWNER_READ,      /* S_IRUSR */
93*4882a593Smuzhiyun 	TOMOYO_MODE_OWNER_WRITE,     /* S_IWUSR */
94*4882a593Smuzhiyun 	TOMOYO_MODE_OWNER_EXECUTE,   /* S_IXUSR */
95*4882a593Smuzhiyun 	TOMOYO_MODE_GROUP_READ,      /* S_IRGRP */
96*4882a593Smuzhiyun 	TOMOYO_MODE_GROUP_WRITE,     /* S_IWGRP */
97*4882a593Smuzhiyun 	TOMOYO_MODE_GROUP_EXECUTE,   /* S_IXGRP */
98*4882a593Smuzhiyun 	TOMOYO_MODE_OTHERS_READ,     /* S_IROTH */
99*4882a593Smuzhiyun 	TOMOYO_MODE_OTHERS_WRITE,    /* S_IWOTH */
100*4882a593Smuzhiyun 	TOMOYO_MODE_OTHERS_EXECUTE,  /* S_IXOTH */
101*4882a593Smuzhiyun 	TOMOYO_EXEC_REALPATH,
102*4882a593Smuzhiyun 	TOMOYO_SYMLINK_TARGET,
103*4882a593Smuzhiyun 	TOMOYO_PATH1_UID,
104*4882a593Smuzhiyun 	TOMOYO_PATH1_GID,
105*4882a593Smuzhiyun 	TOMOYO_PATH1_INO,
106*4882a593Smuzhiyun 	TOMOYO_PATH1_MAJOR,
107*4882a593Smuzhiyun 	TOMOYO_PATH1_MINOR,
108*4882a593Smuzhiyun 	TOMOYO_PATH1_PERM,
109*4882a593Smuzhiyun 	TOMOYO_PATH1_TYPE,
110*4882a593Smuzhiyun 	TOMOYO_PATH1_DEV_MAJOR,
111*4882a593Smuzhiyun 	TOMOYO_PATH1_DEV_MINOR,
112*4882a593Smuzhiyun 	TOMOYO_PATH2_UID,
113*4882a593Smuzhiyun 	TOMOYO_PATH2_GID,
114*4882a593Smuzhiyun 	TOMOYO_PATH2_INO,
115*4882a593Smuzhiyun 	TOMOYO_PATH2_MAJOR,
116*4882a593Smuzhiyun 	TOMOYO_PATH2_MINOR,
117*4882a593Smuzhiyun 	TOMOYO_PATH2_PERM,
118*4882a593Smuzhiyun 	TOMOYO_PATH2_TYPE,
119*4882a593Smuzhiyun 	TOMOYO_PATH2_DEV_MAJOR,
120*4882a593Smuzhiyun 	TOMOYO_PATH2_DEV_MINOR,
121*4882a593Smuzhiyun 	TOMOYO_PATH1_PARENT_UID,
122*4882a593Smuzhiyun 	TOMOYO_PATH1_PARENT_GID,
123*4882a593Smuzhiyun 	TOMOYO_PATH1_PARENT_INO,
124*4882a593Smuzhiyun 	TOMOYO_PATH1_PARENT_PERM,
125*4882a593Smuzhiyun 	TOMOYO_PATH2_PARENT_UID,
126*4882a593Smuzhiyun 	TOMOYO_PATH2_PARENT_GID,
127*4882a593Smuzhiyun 	TOMOYO_PATH2_PARENT_INO,
128*4882a593Smuzhiyun 	TOMOYO_PATH2_PARENT_PERM,
129*4882a593Smuzhiyun 	TOMOYO_MAX_CONDITION_KEYWORD,
130*4882a593Smuzhiyun 	TOMOYO_NUMBER_UNION,
131*4882a593Smuzhiyun 	TOMOYO_NAME_UNION,
132*4882a593Smuzhiyun 	TOMOYO_ARGV_ENTRY,
133*4882a593Smuzhiyun 	TOMOYO_ENVP_ENTRY,
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /* Index numbers for stat(). */
138*4882a593Smuzhiyun enum tomoyo_path_stat_index {
139*4882a593Smuzhiyun 	/* Do not change this order. */
140*4882a593Smuzhiyun 	TOMOYO_PATH1,
141*4882a593Smuzhiyun 	TOMOYO_PATH1_PARENT,
142*4882a593Smuzhiyun 	TOMOYO_PATH2,
143*4882a593Smuzhiyun 	TOMOYO_PATH2_PARENT,
144*4882a593Smuzhiyun 	TOMOYO_MAX_PATH_STAT
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* Index numbers for operation mode. */
148*4882a593Smuzhiyun enum tomoyo_mode_index {
149*4882a593Smuzhiyun 	TOMOYO_CONFIG_DISABLED,
150*4882a593Smuzhiyun 	TOMOYO_CONFIG_LEARNING,
151*4882a593Smuzhiyun 	TOMOYO_CONFIG_PERMISSIVE,
152*4882a593Smuzhiyun 	TOMOYO_CONFIG_ENFORCING,
153*4882a593Smuzhiyun 	TOMOYO_CONFIG_MAX_MODE,
154*4882a593Smuzhiyun 	TOMOYO_CONFIG_WANT_REJECT_LOG =  64,
155*4882a593Smuzhiyun 	TOMOYO_CONFIG_WANT_GRANT_LOG  = 128,
156*4882a593Smuzhiyun 	TOMOYO_CONFIG_USE_DEFAULT     = 255,
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /* Index numbers for entry type. */
160*4882a593Smuzhiyun enum tomoyo_policy_id {
161*4882a593Smuzhiyun 	TOMOYO_ID_GROUP,
162*4882a593Smuzhiyun 	TOMOYO_ID_ADDRESS_GROUP,
163*4882a593Smuzhiyun 	TOMOYO_ID_PATH_GROUP,
164*4882a593Smuzhiyun 	TOMOYO_ID_NUMBER_GROUP,
165*4882a593Smuzhiyun 	TOMOYO_ID_TRANSITION_CONTROL,
166*4882a593Smuzhiyun 	TOMOYO_ID_AGGREGATOR,
167*4882a593Smuzhiyun 	TOMOYO_ID_MANAGER,
168*4882a593Smuzhiyun 	TOMOYO_ID_CONDITION,
169*4882a593Smuzhiyun 	TOMOYO_ID_NAME,
170*4882a593Smuzhiyun 	TOMOYO_ID_ACL,
171*4882a593Smuzhiyun 	TOMOYO_ID_DOMAIN,
172*4882a593Smuzhiyun 	TOMOYO_MAX_POLICY
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /* Index numbers for domain's attributes. */
176*4882a593Smuzhiyun enum tomoyo_domain_info_flags_index {
177*4882a593Smuzhiyun 	/* Quota warnning flag.   */
178*4882a593Smuzhiyun 	TOMOYO_DIF_QUOTA_WARNED,
179*4882a593Smuzhiyun 	/*
180*4882a593Smuzhiyun 	 * This domain was unable to create a new domain at
181*4882a593Smuzhiyun 	 * tomoyo_find_next_domain() because the name of the domain to be
182*4882a593Smuzhiyun 	 * created was too long or it could not allocate memory.
183*4882a593Smuzhiyun 	 * More than one process continued execve() without domain transition.
184*4882a593Smuzhiyun 	 */
185*4882a593Smuzhiyun 	TOMOYO_DIF_TRANSITION_FAILED,
186*4882a593Smuzhiyun 	TOMOYO_MAX_DOMAIN_INFO_FLAGS
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /* Index numbers for audit type. */
190*4882a593Smuzhiyun enum tomoyo_grant_log {
191*4882a593Smuzhiyun 	/* Follow profile's configuration. */
192*4882a593Smuzhiyun 	TOMOYO_GRANTLOG_AUTO,
193*4882a593Smuzhiyun 	/* Do not generate grant log. */
194*4882a593Smuzhiyun 	TOMOYO_GRANTLOG_NO,
195*4882a593Smuzhiyun 	/* Generate grant_log. */
196*4882a593Smuzhiyun 	TOMOYO_GRANTLOG_YES,
197*4882a593Smuzhiyun };
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun /* Index numbers for group entries. */
200*4882a593Smuzhiyun enum tomoyo_group_id {
201*4882a593Smuzhiyun 	TOMOYO_PATH_GROUP,
202*4882a593Smuzhiyun 	TOMOYO_NUMBER_GROUP,
203*4882a593Smuzhiyun 	TOMOYO_ADDRESS_GROUP,
204*4882a593Smuzhiyun 	TOMOYO_MAX_GROUP
205*4882a593Smuzhiyun };
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /* Index numbers for type of numeric values. */
208*4882a593Smuzhiyun enum tomoyo_value_type {
209*4882a593Smuzhiyun 	TOMOYO_VALUE_TYPE_INVALID,
210*4882a593Smuzhiyun 	TOMOYO_VALUE_TYPE_DECIMAL,
211*4882a593Smuzhiyun 	TOMOYO_VALUE_TYPE_OCTAL,
212*4882a593Smuzhiyun 	TOMOYO_VALUE_TYPE_HEXADECIMAL,
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /* Index numbers for domain transition control keywords. */
216*4882a593Smuzhiyun enum tomoyo_transition_type {
217*4882a593Smuzhiyun 	/* Do not change this order, */
218*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_NO_RESET,
219*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_RESET,
220*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
221*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_INITIALIZE,
222*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_NO_KEEP,
223*4882a593Smuzhiyun 	TOMOYO_TRANSITION_CONTROL_KEEP,
224*4882a593Smuzhiyun 	TOMOYO_MAX_TRANSITION_TYPE
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /* Index numbers for Access Controls. */
228*4882a593Smuzhiyun enum tomoyo_acl_entry_type_index {
229*4882a593Smuzhiyun 	TOMOYO_TYPE_PATH_ACL,
230*4882a593Smuzhiyun 	TOMOYO_TYPE_PATH2_ACL,
231*4882a593Smuzhiyun 	TOMOYO_TYPE_PATH_NUMBER_ACL,
232*4882a593Smuzhiyun 	TOMOYO_TYPE_MKDEV_ACL,
233*4882a593Smuzhiyun 	TOMOYO_TYPE_MOUNT_ACL,
234*4882a593Smuzhiyun 	TOMOYO_TYPE_INET_ACL,
235*4882a593Smuzhiyun 	TOMOYO_TYPE_UNIX_ACL,
236*4882a593Smuzhiyun 	TOMOYO_TYPE_ENV_ACL,
237*4882a593Smuzhiyun 	TOMOYO_TYPE_MANUAL_TASK_ACL,
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /* Index numbers for access controls with one pathname. */
241*4882a593Smuzhiyun enum tomoyo_path_acl_index {
242*4882a593Smuzhiyun 	TOMOYO_TYPE_EXECUTE,
243*4882a593Smuzhiyun 	TOMOYO_TYPE_READ,
244*4882a593Smuzhiyun 	TOMOYO_TYPE_WRITE,
245*4882a593Smuzhiyun 	TOMOYO_TYPE_APPEND,
246*4882a593Smuzhiyun 	TOMOYO_TYPE_UNLINK,
247*4882a593Smuzhiyun 	TOMOYO_TYPE_GETATTR,
248*4882a593Smuzhiyun 	TOMOYO_TYPE_RMDIR,
249*4882a593Smuzhiyun 	TOMOYO_TYPE_TRUNCATE,
250*4882a593Smuzhiyun 	TOMOYO_TYPE_SYMLINK,
251*4882a593Smuzhiyun 	TOMOYO_TYPE_CHROOT,
252*4882a593Smuzhiyun 	TOMOYO_TYPE_UMOUNT,
253*4882a593Smuzhiyun 	TOMOYO_MAX_PATH_OPERATION
254*4882a593Smuzhiyun };
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
257*4882a593Smuzhiyun enum tomoyo_memory_stat_type {
258*4882a593Smuzhiyun 	TOMOYO_MEMORY_POLICY,
259*4882a593Smuzhiyun 	TOMOYO_MEMORY_AUDIT,
260*4882a593Smuzhiyun 	TOMOYO_MEMORY_QUERY,
261*4882a593Smuzhiyun 	TOMOYO_MAX_MEMORY_STAT
262*4882a593Smuzhiyun };
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun enum tomoyo_mkdev_acl_index {
265*4882a593Smuzhiyun 	TOMOYO_TYPE_MKBLOCK,
266*4882a593Smuzhiyun 	TOMOYO_TYPE_MKCHAR,
267*4882a593Smuzhiyun 	TOMOYO_MAX_MKDEV_OPERATION
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /* Index numbers for socket operations. */
271*4882a593Smuzhiyun enum tomoyo_network_acl_index {
272*4882a593Smuzhiyun 	TOMOYO_NETWORK_BIND,    /* bind() operation. */
273*4882a593Smuzhiyun 	TOMOYO_NETWORK_LISTEN,  /* listen() operation. */
274*4882a593Smuzhiyun 	TOMOYO_NETWORK_CONNECT, /* connect() operation. */
275*4882a593Smuzhiyun 	TOMOYO_NETWORK_SEND,    /* send() operation. */
276*4882a593Smuzhiyun 	TOMOYO_MAX_NETWORK_OPERATION
277*4882a593Smuzhiyun };
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun /* Index numbers for access controls with two pathnames. */
280*4882a593Smuzhiyun enum tomoyo_path2_acl_index {
281*4882a593Smuzhiyun 	TOMOYO_TYPE_LINK,
282*4882a593Smuzhiyun 	TOMOYO_TYPE_RENAME,
283*4882a593Smuzhiyun 	TOMOYO_TYPE_PIVOT_ROOT,
284*4882a593Smuzhiyun 	TOMOYO_MAX_PATH2_OPERATION
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /* Index numbers for access controls with one pathname and one number. */
288*4882a593Smuzhiyun enum tomoyo_path_number_acl_index {
289*4882a593Smuzhiyun 	TOMOYO_TYPE_CREATE,
290*4882a593Smuzhiyun 	TOMOYO_TYPE_MKDIR,
291*4882a593Smuzhiyun 	TOMOYO_TYPE_MKFIFO,
292*4882a593Smuzhiyun 	TOMOYO_TYPE_MKSOCK,
293*4882a593Smuzhiyun 	TOMOYO_TYPE_IOCTL,
294*4882a593Smuzhiyun 	TOMOYO_TYPE_CHMOD,
295*4882a593Smuzhiyun 	TOMOYO_TYPE_CHOWN,
296*4882a593Smuzhiyun 	TOMOYO_TYPE_CHGRP,
297*4882a593Smuzhiyun 	TOMOYO_MAX_PATH_NUMBER_OPERATION
298*4882a593Smuzhiyun };
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
301*4882a593Smuzhiyun enum tomoyo_securityfs_interface_index {
302*4882a593Smuzhiyun 	TOMOYO_DOMAINPOLICY,
303*4882a593Smuzhiyun 	TOMOYO_EXCEPTIONPOLICY,
304*4882a593Smuzhiyun 	TOMOYO_PROCESS_STATUS,
305*4882a593Smuzhiyun 	TOMOYO_STAT,
306*4882a593Smuzhiyun 	TOMOYO_AUDIT,
307*4882a593Smuzhiyun 	TOMOYO_VERSION,
308*4882a593Smuzhiyun 	TOMOYO_PROFILE,
309*4882a593Smuzhiyun 	TOMOYO_QUERY,
310*4882a593Smuzhiyun 	TOMOYO_MANAGER
311*4882a593Smuzhiyun };
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /* Index numbers for special mount operations. */
314*4882a593Smuzhiyun enum tomoyo_special_mount {
315*4882a593Smuzhiyun 	TOMOYO_MOUNT_BIND,            /* mount --bind /source /dest   */
316*4882a593Smuzhiyun 	TOMOYO_MOUNT_MOVE,            /* mount --move /old /new       */
317*4882a593Smuzhiyun 	TOMOYO_MOUNT_REMOUNT,         /* mount -o remount /dir        */
318*4882a593Smuzhiyun 	TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
319*4882a593Smuzhiyun 	TOMOYO_MOUNT_MAKE_PRIVATE,    /* mount --make-private /dir    */
320*4882a593Smuzhiyun 	TOMOYO_MOUNT_MAKE_SLAVE,      /* mount --make-slave /dir      */
321*4882a593Smuzhiyun 	TOMOYO_MOUNT_MAKE_SHARED,     /* mount --make-shared /dir     */
322*4882a593Smuzhiyun 	TOMOYO_MAX_SPECIAL_MOUNT
323*4882a593Smuzhiyun };
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /* Index numbers for functionality. */
326*4882a593Smuzhiyun enum tomoyo_mac_index {
327*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_EXECUTE,
328*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_OPEN,
329*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_CREATE,
330*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_UNLINK,
331*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_GETATTR,
332*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MKDIR,
333*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_RMDIR,
334*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MKFIFO,
335*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MKSOCK,
336*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_TRUNCATE,
337*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_SYMLINK,
338*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MKBLOCK,
339*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MKCHAR,
340*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_LINK,
341*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_RENAME,
342*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_CHMOD,
343*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_CHOWN,
344*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_CHGRP,
345*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_IOCTL,
346*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_CHROOT,
347*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_MOUNT,
348*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_UMOUNT,
349*4882a593Smuzhiyun 	TOMOYO_MAC_FILE_PIVOT_ROOT,
350*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_STREAM_BIND,
351*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN,
352*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT,
353*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_DGRAM_BIND,
354*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_DGRAM_SEND,
355*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_RAW_BIND,
356*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_INET_RAW_SEND,
357*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND,
358*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN,
359*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT,
360*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND,
361*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND,
362*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND,
363*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
364*4882a593Smuzhiyun 	TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
365*4882a593Smuzhiyun 	TOMOYO_MAC_ENVIRON,
366*4882a593Smuzhiyun 	TOMOYO_MAX_MAC_INDEX
367*4882a593Smuzhiyun };
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /* Index numbers for category of functionality. */
370*4882a593Smuzhiyun enum tomoyo_mac_category_index {
371*4882a593Smuzhiyun 	TOMOYO_MAC_CATEGORY_FILE,
372*4882a593Smuzhiyun 	TOMOYO_MAC_CATEGORY_NETWORK,
373*4882a593Smuzhiyun 	TOMOYO_MAC_CATEGORY_MISC,
374*4882a593Smuzhiyun 	TOMOYO_MAX_MAC_CATEGORY_INDEX
375*4882a593Smuzhiyun };
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun  * Retry this request. Returned by tomoyo_supervisor() if policy violation has
379*4882a593Smuzhiyun  * occurred in enforcing mode and the userspace daemon decided to retry.
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * We must choose a positive value in order to distinguish "granted" (which is
382*4882a593Smuzhiyun  * 0) and "rejected" (which is a negative value) and "retry".
383*4882a593Smuzhiyun  */
384*4882a593Smuzhiyun #define TOMOYO_RETRY_REQUEST 1
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
387*4882a593Smuzhiyun enum tomoyo_policy_stat_type {
388*4882a593Smuzhiyun 	/* Do not change this order. */
389*4882a593Smuzhiyun 	TOMOYO_STAT_POLICY_UPDATES,
390*4882a593Smuzhiyun 	TOMOYO_STAT_POLICY_LEARNING,   /* == TOMOYO_CONFIG_LEARNING */
391*4882a593Smuzhiyun 	TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
392*4882a593Smuzhiyun 	TOMOYO_STAT_POLICY_ENFORCING,  /* == TOMOYO_CONFIG_ENFORCING */
393*4882a593Smuzhiyun 	TOMOYO_MAX_POLICY_STAT
394*4882a593Smuzhiyun };
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun /* Index numbers for profile's PREFERENCE values. */
397*4882a593Smuzhiyun enum tomoyo_pref_index {
398*4882a593Smuzhiyun 	TOMOYO_PREF_MAX_AUDIT_LOG,
399*4882a593Smuzhiyun 	TOMOYO_PREF_MAX_LEARNING_ENTRY,
400*4882a593Smuzhiyun 	TOMOYO_MAX_PREF
401*4882a593Smuzhiyun };
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /********** Structure definitions. **********/
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /* Common header for holding ACL entries. */
406*4882a593Smuzhiyun struct tomoyo_acl_head {
407*4882a593Smuzhiyun 	struct list_head list;
408*4882a593Smuzhiyun 	s8 is_deleted; /* true or false or TOMOYO_GC_IN_PROGRESS */
409*4882a593Smuzhiyun } __packed;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun /* Common header for shared entries. */
412*4882a593Smuzhiyun struct tomoyo_shared_acl_head {
413*4882a593Smuzhiyun 	struct list_head list;
414*4882a593Smuzhiyun 	atomic_t users;
415*4882a593Smuzhiyun } __packed;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun struct tomoyo_policy_namespace;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /* Structure for request info. */
420*4882a593Smuzhiyun struct tomoyo_request_info {
421*4882a593Smuzhiyun 	/*
422*4882a593Smuzhiyun 	 * For holding parameters specific to operations which deal files.
423*4882a593Smuzhiyun 	 * NULL if not dealing files.
424*4882a593Smuzhiyun 	 */
425*4882a593Smuzhiyun 	struct tomoyo_obj_info *obj;
426*4882a593Smuzhiyun 	/*
427*4882a593Smuzhiyun 	 * For holding parameters specific to execve() request.
428*4882a593Smuzhiyun 	 * NULL if not dealing execve().
429*4882a593Smuzhiyun 	 */
430*4882a593Smuzhiyun 	struct tomoyo_execve *ee;
431*4882a593Smuzhiyun 	struct tomoyo_domain_info *domain;
432*4882a593Smuzhiyun 	/* For holding parameters. */
433*4882a593Smuzhiyun 	union {
434*4882a593Smuzhiyun 		struct {
435*4882a593Smuzhiyun 			const struct tomoyo_path_info *filename;
436*4882a593Smuzhiyun 			/* For using wildcards at tomoyo_find_next_domain(). */
437*4882a593Smuzhiyun 			const struct tomoyo_path_info *matched_path;
438*4882a593Smuzhiyun 			/* One of values in "enum tomoyo_path_acl_index". */
439*4882a593Smuzhiyun 			u8 operation;
440*4882a593Smuzhiyun 		} path;
441*4882a593Smuzhiyun 		struct {
442*4882a593Smuzhiyun 			const struct tomoyo_path_info *filename1;
443*4882a593Smuzhiyun 			const struct tomoyo_path_info *filename2;
444*4882a593Smuzhiyun 			/* One of values in "enum tomoyo_path2_acl_index". */
445*4882a593Smuzhiyun 			u8 operation;
446*4882a593Smuzhiyun 		} path2;
447*4882a593Smuzhiyun 		struct {
448*4882a593Smuzhiyun 			const struct tomoyo_path_info *filename;
449*4882a593Smuzhiyun 			unsigned int mode;
450*4882a593Smuzhiyun 			unsigned int major;
451*4882a593Smuzhiyun 			unsigned int minor;
452*4882a593Smuzhiyun 			/* One of values in "enum tomoyo_mkdev_acl_index". */
453*4882a593Smuzhiyun 			u8 operation;
454*4882a593Smuzhiyun 		} mkdev;
455*4882a593Smuzhiyun 		struct {
456*4882a593Smuzhiyun 			const struct tomoyo_path_info *filename;
457*4882a593Smuzhiyun 			unsigned long number;
458*4882a593Smuzhiyun 			/*
459*4882a593Smuzhiyun 			 * One of values in
460*4882a593Smuzhiyun 			 * "enum tomoyo_path_number_acl_index".
461*4882a593Smuzhiyun 			 */
462*4882a593Smuzhiyun 			u8 operation;
463*4882a593Smuzhiyun 		} path_number;
464*4882a593Smuzhiyun 		struct {
465*4882a593Smuzhiyun 			const struct tomoyo_path_info *name;
466*4882a593Smuzhiyun 		} environ;
467*4882a593Smuzhiyun 		struct {
468*4882a593Smuzhiyun 			const __be32 *address;
469*4882a593Smuzhiyun 			u16 port;
470*4882a593Smuzhiyun 			/* One of values smaller than TOMOYO_SOCK_MAX. */
471*4882a593Smuzhiyun 			u8 protocol;
472*4882a593Smuzhiyun 			/* One of values in "enum tomoyo_network_acl_index". */
473*4882a593Smuzhiyun 			u8 operation;
474*4882a593Smuzhiyun 			bool is_ipv6;
475*4882a593Smuzhiyun 		} inet_network;
476*4882a593Smuzhiyun 		struct {
477*4882a593Smuzhiyun 			const struct tomoyo_path_info *address;
478*4882a593Smuzhiyun 			/* One of values smaller than TOMOYO_SOCK_MAX. */
479*4882a593Smuzhiyun 			u8 protocol;
480*4882a593Smuzhiyun 			/* One of values in "enum tomoyo_network_acl_index". */
481*4882a593Smuzhiyun 			u8 operation;
482*4882a593Smuzhiyun 		} unix_network;
483*4882a593Smuzhiyun 		struct {
484*4882a593Smuzhiyun 			const struct tomoyo_path_info *type;
485*4882a593Smuzhiyun 			const struct tomoyo_path_info *dir;
486*4882a593Smuzhiyun 			const struct tomoyo_path_info *dev;
487*4882a593Smuzhiyun 			unsigned long flags;
488*4882a593Smuzhiyun 			int need_dev;
489*4882a593Smuzhiyun 		} mount;
490*4882a593Smuzhiyun 		struct {
491*4882a593Smuzhiyun 			const struct tomoyo_path_info *domainname;
492*4882a593Smuzhiyun 		} task;
493*4882a593Smuzhiyun 	} param;
494*4882a593Smuzhiyun 	struct tomoyo_acl_info *matched_acl;
495*4882a593Smuzhiyun 	u8 param_type;
496*4882a593Smuzhiyun 	bool granted;
497*4882a593Smuzhiyun 	u8 retry;
498*4882a593Smuzhiyun 	u8 profile;
499*4882a593Smuzhiyun 	u8 mode; /* One of tomoyo_mode_index . */
500*4882a593Smuzhiyun 	u8 type;
501*4882a593Smuzhiyun };
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /* Structure for holding a token. */
504*4882a593Smuzhiyun struct tomoyo_path_info {
505*4882a593Smuzhiyun 	const char *name;
506*4882a593Smuzhiyun 	u32 hash;          /* = full_name_hash(name, strlen(name)) */
507*4882a593Smuzhiyun 	u16 const_len;     /* = tomoyo_const_part_length(name)     */
508*4882a593Smuzhiyun 	bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
509*4882a593Smuzhiyun 	bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
510*4882a593Smuzhiyun };
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun /* Structure for holding string data. */
513*4882a593Smuzhiyun struct tomoyo_name {
514*4882a593Smuzhiyun 	struct tomoyo_shared_acl_head head;
515*4882a593Smuzhiyun 	struct tomoyo_path_info entry;
516*4882a593Smuzhiyun };
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /* Structure for holding a word. */
519*4882a593Smuzhiyun struct tomoyo_name_union {
520*4882a593Smuzhiyun 	/* Either @filename or @group is NULL. */
521*4882a593Smuzhiyun 	const struct tomoyo_path_info *filename;
522*4882a593Smuzhiyun 	struct tomoyo_group *group;
523*4882a593Smuzhiyun };
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun /* Structure for holding a number. */
526*4882a593Smuzhiyun struct tomoyo_number_union {
527*4882a593Smuzhiyun 	unsigned long values[2];
528*4882a593Smuzhiyun 	struct tomoyo_group *group; /* Maybe NULL. */
529*4882a593Smuzhiyun 	/* One of values in "enum tomoyo_value_type". */
530*4882a593Smuzhiyun 	u8 value_type[2];
531*4882a593Smuzhiyun };
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun /* Structure for holding an IP address. */
534*4882a593Smuzhiyun struct tomoyo_ipaddr_union {
535*4882a593Smuzhiyun 	struct in6_addr ip[2]; /* Big endian. */
536*4882a593Smuzhiyun 	struct tomoyo_group *group; /* Pointer to address group. */
537*4882a593Smuzhiyun 	bool is_ipv6; /* Valid only if @group == NULL. */
538*4882a593Smuzhiyun };
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /* Structure for "path_group"/"number_group"/"address_group" directive. */
541*4882a593Smuzhiyun struct tomoyo_group {
542*4882a593Smuzhiyun 	struct tomoyo_shared_acl_head head;
543*4882a593Smuzhiyun 	const struct tomoyo_path_info *group_name;
544*4882a593Smuzhiyun 	struct list_head member_list;
545*4882a593Smuzhiyun };
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun /* Structure for "path_group" directive. */
548*4882a593Smuzhiyun struct tomoyo_path_group {
549*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
550*4882a593Smuzhiyun 	const struct tomoyo_path_info *member_name;
551*4882a593Smuzhiyun };
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun /* Structure for "number_group" directive. */
554*4882a593Smuzhiyun struct tomoyo_number_group {
555*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
556*4882a593Smuzhiyun 	struct tomoyo_number_union number;
557*4882a593Smuzhiyun };
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun /* Structure for "address_group" directive. */
560*4882a593Smuzhiyun struct tomoyo_address_group {
561*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
562*4882a593Smuzhiyun 	/* Structure for holding an IP address. */
563*4882a593Smuzhiyun 	struct tomoyo_ipaddr_union address;
564*4882a593Smuzhiyun };
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun /* Subset of "struct stat". Used by conditional ACL and audit logs. */
567*4882a593Smuzhiyun struct tomoyo_mini_stat {
568*4882a593Smuzhiyun 	kuid_t uid;
569*4882a593Smuzhiyun 	kgid_t gid;
570*4882a593Smuzhiyun 	ino_t ino;
571*4882a593Smuzhiyun 	umode_t mode;
572*4882a593Smuzhiyun 	dev_t dev;
573*4882a593Smuzhiyun 	dev_t rdev;
574*4882a593Smuzhiyun };
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /* Structure for dumping argv[] and envp[] of "struct linux_binprm". */
577*4882a593Smuzhiyun struct tomoyo_page_dump {
578*4882a593Smuzhiyun 	struct page *page;    /* Previously dumped page. */
579*4882a593Smuzhiyun 	char *data;           /* Contents of "page". Size is PAGE_SIZE. */
580*4882a593Smuzhiyun };
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun /* Structure for attribute checks in addition to pathname checks. */
583*4882a593Smuzhiyun struct tomoyo_obj_info {
584*4882a593Smuzhiyun 	/*
585*4882a593Smuzhiyun 	 * True if tomoyo_get_attributes() was already called, false otherwise.
586*4882a593Smuzhiyun 	 */
587*4882a593Smuzhiyun 	bool validate_done;
588*4882a593Smuzhiyun 	/* True if @stat[] is valid. */
589*4882a593Smuzhiyun 	bool stat_valid[TOMOYO_MAX_PATH_STAT];
590*4882a593Smuzhiyun 	/* First pathname. Initialized with { NULL, NULL } if no path. */
591*4882a593Smuzhiyun 	struct path path1;
592*4882a593Smuzhiyun 	/* Second pathname. Initialized with { NULL, NULL } if no path. */
593*4882a593Smuzhiyun 	struct path path2;
594*4882a593Smuzhiyun 	/*
595*4882a593Smuzhiyun 	 * Information on @path1, @path1's parent directory, @path2, @path2's
596*4882a593Smuzhiyun 	 * parent directory.
597*4882a593Smuzhiyun 	 */
598*4882a593Smuzhiyun 	struct tomoyo_mini_stat stat[TOMOYO_MAX_PATH_STAT];
599*4882a593Smuzhiyun 	/*
600*4882a593Smuzhiyun 	 * Content of symbolic link to be created. NULL for operations other
601*4882a593Smuzhiyun 	 * than symlink().
602*4882a593Smuzhiyun 	 */
603*4882a593Smuzhiyun 	struct tomoyo_path_info *symlink_target;
604*4882a593Smuzhiyun };
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun /* Structure for argv[]. */
607*4882a593Smuzhiyun struct tomoyo_argv {
608*4882a593Smuzhiyun 	unsigned long index;
609*4882a593Smuzhiyun 	const struct tomoyo_path_info *value;
610*4882a593Smuzhiyun 	bool is_not;
611*4882a593Smuzhiyun };
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /* Structure for envp[]. */
614*4882a593Smuzhiyun struct tomoyo_envp {
615*4882a593Smuzhiyun 	const struct tomoyo_path_info *name;
616*4882a593Smuzhiyun 	const struct tomoyo_path_info *value;
617*4882a593Smuzhiyun 	bool is_not;
618*4882a593Smuzhiyun };
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun /* Structure for execve() operation. */
621*4882a593Smuzhiyun struct tomoyo_execve {
622*4882a593Smuzhiyun 	struct tomoyo_request_info r;
623*4882a593Smuzhiyun 	struct tomoyo_obj_info obj;
624*4882a593Smuzhiyun 	struct linux_binprm *bprm;
625*4882a593Smuzhiyun 	const struct tomoyo_path_info *transition;
626*4882a593Smuzhiyun 	/* For dumping argv[] and envp[]. */
627*4882a593Smuzhiyun 	struct tomoyo_page_dump dump;
628*4882a593Smuzhiyun 	/* For temporary use. */
629*4882a593Smuzhiyun 	char *tmp; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
630*4882a593Smuzhiyun };
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun /* Structure for entries which follows "struct tomoyo_condition". */
633*4882a593Smuzhiyun struct tomoyo_condition_element {
634*4882a593Smuzhiyun 	/*
635*4882a593Smuzhiyun 	 * Left hand operand. A "struct tomoyo_argv" for TOMOYO_ARGV_ENTRY, a
636*4882a593Smuzhiyun 	 * "struct tomoyo_envp" for TOMOYO_ENVP_ENTRY is attached to the tail
637*4882a593Smuzhiyun 	 * of the array of this struct.
638*4882a593Smuzhiyun 	 */
639*4882a593Smuzhiyun 	u8 left;
640*4882a593Smuzhiyun 	/*
641*4882a593Smuzhiyun 	 * Right hand operand. A "struct tomoyo_number_union" for
642*4882a593Smuzhiyun 	 * TOMOYO_NUMBER_UNION, a "struct tomoyo_name_union" for
643*4882a593Smuzhiyun 	 * TOMOYO_NAME_UNION is attached to the tail of the array of this
644*4882a593Smuzhiyun 	 * struct.
645*4882a593Smuzhiyun 	 */
646*4882a593Smuzhiyun 	u8 right;
647*4882a593Smuzhiyun 	/* Equation operator. True if equals or overlaps, false otherwise. */
648*4882a593Smuzhiyun 	bool equals;
649*4882a593Smuzhiyun };
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun /* Structure for optional arguments. */
652*4882a593Smuzhiyun struct tomoyo_condition {
653*4882a593Smuzhiyun 	struct tomoyo_shared_acl_head head;
654*4882a593Smuzhiyun 	u32 size; /* Memory size allocated for this entry. */
655*4882a593Smuzhiyun 	u16 condc; /* Number of conditions in this struct. */
656*4882a593Smuzhiyun 	u16 numbers_count; /* Number of "struct tomoyo_number_union values". */
657*4882a593Smuzhiyun 	u16 names_count; /* Number of "struct tomoyo_name_union names". */
658*4882a593Smuzhiyun 	u16 argc; /* Number of "struct tomoyo_argv". */
659*4882a593Smuzhiyun 	u16 envc; /* Number of "struct tomoyo_envp". */
660*4882a593Smuzhiyun 	u8 grant_log; /* One of values in "enum tomoyo_grant_log". */
661*4882a593Smuzhiyun 	const struct tomoyo_path_info *transit; /* Maybe NULL. */
662*4882a593Smuzhiyun 	/*
663*4882a593Smuzhiyun 	 * struct tomoyo_condition_element condition[condc];
664*4882a593Smuzhiyun 	 * struct tomoyo_number_union values[numbers_count];
665*4882a593Smuzhiyun 	 * struct tomoyo_name_union names[names_count];
666*4882a593Smuzhiyun 	 * struct tomoyo_argv argv[argc];
667*4882a593Smuzhiyun 	 * struct tomoyo_envp envp[envc];
668*4882a593Smuzhiyun 	 */
669*4882a593Smuzhiyun };
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun /* Common header for individual entries. */
672*4882a593Smuzhiyun struct tomoyo_acl_info {
673*4882a593Smuzhiyun 	struct list_head list;
674*4882a593Smuzhiyun 	struct tomoyo_condition *cond; /* Maybe NULL. */
675*4882a593Smuzhiyun 	s8 is_deleted; /* true or false or TOMOYO_GC_IN_PROGRESS */
676*4882a593Smuzhiyun 	u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
677*4882a593Smuzhiyun } __packed;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun /* Structure for domain information. */
680*4882a593Smuzhiyun struct tomoyo_domain_info {
681*4882a593Smuzhiyun 	struct list_head list;
682*4882a593Smuzhiyun 	struct list_head acl_info_list;
683*4882a593Smuzhiyun 	/* Name of this domain. Never NULL.          */
684*4882a593Smuzhiyun 	const struct tomoyo_path_info *domainname;
685*4882a593Smuzhiyun 	/* Namespace for this domain. Never NULL. */
686*4882a593Smuzhiyun 	struct tomoyo_policy_namespace *ns;
687*4882a593Smuzhiyun 	/* Group numbers to use.   */
688*4882a593Smuzhiyun 	unsigned long group[TOMOYO_MAX_ACL_GROUPS / BITS_PER_LONG];
689*4882a593Smuzhiyun 	u8 profile;        /* Profile number to use. */
690*4882a593Smuzhiyun 	bool is_deleted;   /* Delete flag.           */
691*4882a593Smuzhiyun 	bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
692*4882a593Smuzhiyun 	atomic_t users; /* Number of referring tasks. */
693*4882a593Smuzhiyun };
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun /*
696*4882a593Smuzhiyun  * Structure for "task manual_domain_transition" directive.
697*4882a593Smuzhiyun  */
698*4882a593Smuzhiyun struct tomoyo_task_acl {
699*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MANUAL_TASK_ACL */
700*4882a593Smuzhiyun 	/* Pointer to domainname. */
701*4882a593Smuzhiyun 	const struct tomoyo_path_info *domainname;
702*4882a593Smuzhiyun };
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun /*
705*4882a593Smuzhiyun  * Structure for "file execute", "file read", "file write", "file append",
706*4882a593Smuzhiyun  * "file unlink", "file getattr", "file rmdir", "file truncate",
707*4882a593Smuzhiyun  * "file symlink", "file chroot" and "file unmount" directive.
708*4882a593Smuzhiyun  */
709*4882a593Smuzhiyun struct tomoyo_path_acl {
710*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
711*4882a593Smuzhiyun 	u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
712*4882a593Smuzhiyun 	struct tomoyo_name_union name;
713*4882a593Smuzhiyun };
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun /*
716*4882a593Smuzhiyun  * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
717*4882a593Smuzhiyun  * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
718*4882a593Smuzhiyun  */
719*4882a593Smuzhiyun struct tomoyo_path_number_acl {
720*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
721*4882a593Smuzhiyun 	/* Bitmask of values in "enum tomoyo_path_number_acl_index". */
722*4882a593Smuzhiyun 	u8 perm;
723*4882a593Smuzhiyun 	struct tomoyo_name_union name;
724*4882a593Smuzhiyun 	struct tomoyo_number_union number;
725*4882a593Smuzhiyun };
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun /* Structure for "file mkblock" and "file mkchar" directive. */
728*4882a593Smuzhiyun struct tomoyo_mkdev_acl {
729*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
730*4882a593Smuzhiyun 	u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
731*4882a593Smuzhiyun 	struct tomoyo_name_union name;
732*4882a593Smuzhiyun 	struct tomoyo_number_union mode;
733*4882a593Smuzhiyun 	struct tomoyo_number_union major;
734*4882a593Smuzhiyun 	struct tomoyo_number_union minor;
735*4882a593Smuzhiyun };
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun /*
738*4882a593Smuzhiyun  * Structure for "file rename", "file link" and "file pivot_root" directive.
739*4882a593Smuzhiyun  */
740*4882a593Smuzhiyun struct tomoyo_path2_acl {
741*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
742*4882a593Smuzhiyun 	u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
743*4882a593Smuzhiyun 	struct tomoyo_name_union name1;
744*4882a593Smuzhiyun 	struct tomoyo_name_union name2;
745*4882a593Smuzhiyun };
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun /* Structure for "file mount" directive. */
748*4882a593Smuzhiyun struct tomoyo_mount_acl {
749*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
750*4882a593Smuzhiyun 	struct tomoyo_name_union dev_name;
751*4882a593Smuzhiyun 	struct tomoyo_name_union dir_name;
752*4882a593Smuzhiyun 	struct tomoyo_name_union fs_type;
753*4882a593Smuzhiyun 	struct tomoyo_number_union flags;
754*4882a593Smuzhiyun };
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun /* Structure for "misc env" directive in domain policy. */
757*4882a593Smuzhiyun struct tomoyo_env_acl {
758*4882a593Smuzhiyun 	struct tomoyo_acl_info head;        /* type = TOMOYO_TYPE_ENV_ACL  */
759*4882a593Smuzhiyun 	const struct tomoyo_path_info *env; /* environment variable */
760*4882a593Smuzhiyun };
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun /* Structure for "network inet" directive. */
763*4882a593Smuzhiyun struct tomoyo_inet_acl {
764*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_INET_ACL */
765*4882a593Smuzhiyun 	u8 protocol;
766*4882a593Smuzhiyun 	u8 perm; /* Bitmask of values in "enum tomoyo_network_acl_index" */
767*4882a593Smuzhiyun 	struct tomoyo_ipaddr_union address;
768*4882a593Smuzhiyun 	struct tomoyo_number_union port;
769*4882a593Smuzhiyun };
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /* Structure for "network unix" directive. */
772*4882a593Smuzhiyun struct tomoyo_unix_acl {
773*4882a593Smuzhiyun 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_UNIX_ACL */
774*4882a593Smuzhiyun 	u8 protocol;
775*4882a593Smuzhiyun 	u8 perm; /* Bitmask of values in "enum tomoyo_network_acl_index" */
776*4882a593Smuzhiyun 	struct tomoyo_name_union name;
777*4882a593Smuzhiyun };
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
780*4882a593Smuzhiyun struct tomoyo_acl_param {
781*4882a593Smuzhiyun 	char *data;
782*4882a593Smuzhiyun 	struct list_head *list;
783*4882a593Smuzhiyun 	struct tomoyo_policy_namespace *ns;
784*4882a593Smuzhiyun 	bool is_delete;
785*4882a593Smuzhiyun };
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun #define TOMOYO_MAX_IO_READ_QUEUE 64
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /*
790*4882a593Smuzhiyun  * Structure for reading/writing policy via /sys/kernel/security/tomoyo
791*4882a593Smuzhiyun  * interfaces.
792*4882a593Smuzhiyun  */
793*4882a593Smuzhiyun struct tomoyo_io_buffer {
794*4882a593Smuzhiyun 	void (*read)(struct tomoyo_io_buffer *head);
795*4882a593Smuzhiyun 	int (*write)(struct tomoyo_io_buffer *head);
796*4882a593Smuzhiyun 	__poll_t (*poll)(struct file *file, poll_table *wait);
797*4882a593Smuzhiyun 	/* Exclusive lock for this structure.   */
798*4882a593Smuzhiyun 	struct mutex io_sem;
799*4882a593Smuzhiyun 	char __user *read_user_buf;
800*4882a593Smuzhiyun 	size_t read_user_buf_avail;
801*4882a593Smuzhiyun 	struct {
802*4882a593Smuzhiyun 		struct list_head *ns;
803*4882a593Smuzhiyun 		struct list_head *domain;
804*4882a593Smuzhiyun 		struct list_head *group;
805*4882a593Smuzhiyun 		struct list_head *acl;
806*4882a593Smuzhiyun 		size_t avail;
807*4882a593Smuzhiyun 		unsigned int step;
808*4882a593Smuzhiyun 		unsigned int query_index;
809*4882a593Smuzhiyun 		u16 index;
810*4882a593Smuzhiyun 		u16 cond_index;
811*4882a593Smuzhiyun 		u8 acl_group_index;
812*4882a593Smuzhiyun 		u8 cond_step;
813*4882a593Smuzhiyun 		u8 bit;
814*4882a593Smuzhiyun 		u8 w_pos;
815*4882a593Smuzhiyun 		bool eof;
816*4882a593Smuzhiyun 		bool print_this_domain_only;
817*4882a593Smuzhiyun 		bool print_transition_related_only;
818*4882a593Smuzhiyun 		bool print_cond_part;
819*4882a593Smuzhiyun 		const char *w[TOMOYO_MAX_IO_READ_QUEUE];
820*4882a593Smuzhiyun 	} r;
821*4882a593Smuzhiyun 	struct {
822*4882a593Smuzhiyun 		struct tomoyo_policy_namespace *ns;
823*4882a593Smuzhiyun 		/* The position currently writing to.   */
824*4882a593Smuzhiyun 		struct tomoyo_domain_info *domain;
825*4882a593Smuzhiyun 		/* Bytes available for writing.         */
826*4882a593Smuzhiyun 		size_t avail;
827*4882a593Smuzhiyun 		bool is_delete;
828*4882a593Smuzhiyun 	} w;
829*4882a593Smuzhiyun 	/* Buffer for reading.                  */
830*4882a593Smuzhiyun 	char *read_buf;
831*4882a593Smuzhiyun 	/* Size of read buffer.                 */
832*4882a593Smuzhiyun 	size_t readbuf_size;
833*4882a593Smuzhiyun 	/* Buffer for writing.                  */
834*4882a593Smuzhiyun 	char *write_buf;
835*4882a593Smuzhiyun 	/* Size of write buffer.                */
836*4882a593Smuzhiyun 	size_t writebuf_size;
837*4882a593Smuzhiyun 	/* Type of this interface.              */
838*4882a593Smuzhiyun 	enum tomoyo_securityfs_interface_index type;
839*4882a593Smuzhiyun 	/* Users counter protected by tomoyo_io_buffer_list_lock. */
840*4882a593Smuzhiyun 	u8 users;
841*4882a593Smuzhiyun 	/* List for telling GC not to kfree() elements. */
842*4882a593Smuzhiyun 	struct list_head list;
843*4882a593Smuzhiyun };
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun /*
846*4882a593Smuzhiyun  * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
847*4882a593Smuzhiyun  * "no_keep_domain" keyword.
848*4882a593Smuzhiyun  */
849*4882a593Smuzhiyun struct tomoyo_transition_control {
850*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
851*4882a593Smuzhiyun 	u8 type; /* One of values in "enum tomoyo_transition_type".  */
852*4882a593Smuzhiyun 	/* True if the domainname is tomoyo_get_last_name(). */
853*4882a593Smuzhiyun 	bool is_last_name;
854*4882a593Smuzhiyun 	const struct tomoyo_path_info *domainname; /* Maybe NULL */
855*4882a593Smuzhiyun 	const struct tomoyo_path_info *program;    /* Maybe NULL */
856*4882a593Smuzhiyun };
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun /* Structure for "aggregator" keyword. */
859*4882a593Smuzhiyun struct tomoyo_aggregator {
860*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
861*4882a593Smuzhiyun 	const struct tomoyo_path_info *original_name;
862*4882a593Smuzhiyun 	const struct tomoyo_path_info *aggregated_name;
863*4882a593Smuzhiyun };
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun /* Structure for policy manager. */
866*4882a593Smuzhiyun struct tomoyo_manager {
867*4882a593Smuzhiyun 	struct tomoyo_acl_head head;
868*4882a593Smuzhiyun 	/* A path to program or a domainname. */
869*4882a593Smuzhiyun 	const struct tomoyo_path_info *manager;
870*4882a593Smuzhiyun };
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun struct tomoyo_preference {
873*4882a593Smuzhiyun 	unsigned int learning_max_entry;
874*4882a593Smuzhiyun 	bool enforcing_verbose;
875*4882a593Smuzhiyun 	bool learning_verbose;
876*4882a593Smuzhiyun 	bool permissive_verbose;
877*4882a593Smuzhiyun };
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
880*4882a593Smuzhiyun struct tomoyo_profile {
881*4882a593Smuzhiyun 	const struct tomoyo_path_info *comment;
882*4882a593Smuzhiyun 	struct tomoyo_preference *learning;
883*4882a593Smuzhiyun 	struct tomoyo_preference *permissive;
884*4882a593Smuzhiyun 	struct tomoyo_preference *enforcing;
885*4882a593Smuzhiyun 	struct tomoyo_preference preference;
886*4882a593Smuzhiyun 	u8 default_config;
887*4882a593Smuzhiyun 	u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
888*4882a593Smuzhiyun 	unsigned int pref[TOMOYO_MAX_PREF];
889*4882a593Smuzhiyun };
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun /* Structure for representing YYYY/MM/DD hh/mm/ss. */
892*4882a593Smuzhiyun struct tomoyo_time {
893*4882a593Smuzhiyun 	u16 year;
894*4882a593Smuzhiyun 	u8 month;
895*4882a593Smuzhiyun 	u8 day;
896*4882a593Smuzhiyun 	u8 hour;
897*4882a593Smuzhiyun 	u8 min;
898*4882a593Smuzhiyun 	u8 sec;
899*4882a593Smuzhiyun };
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun /* Structure for policy namespace. */
902*4882a593Smuzhiyun struct tomoyo_policy_namespace {
903*4882a593Smuzhiyun 	/* Profile table. Memory is allocated as needed. */
904*4882a593Smuzhiyun 	struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
905*4882a593Smuzhiyun 	/* List of "struct tomoyo_group". */
906*4882a593Smuzhiyun 	struct list_head group_list[TOMOYO_MAX_GROUP];
907*4882a593Smuzhiyun 	/* List of policy. */
908*4882a593Smuzhiyun 	struct list_head policy_list[TOMOYO_MAX_POLICY];
909*4882a593Smuzhiyun 	/* The global ACL referred by "use_group" keyword. */
910*4882a593Smuzhiyun 	struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
911*4882a593Smuzhiyun 	/* List for connecting to tomoyo_namespace_list list. */
912*4882a593Smuzhiyun 	struct list_head namespace_list;
913*4882a593Smuzhiyun 	/* Profile version. Currently only 20150505 is defined. */
914*4882a593Smuzhiyun 	unsigned int profile_version;
915*4882a593Smuzhiyun 	/* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
916*4882a593Smuzhiyun 	const char *name;
917*4882a593Smuzhiyun };
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun /* Structure for "struct task_struct"->security. */
920*4882a593Smuzhiyun struct tomoyo_task {
921*4882a593Smuzhiyun 	struct tomoyo_domain_info *domain_info;
922*4882a593Smuzhiyun 	struct tomoyo_domain_info *old_domain_info;
923*4882a593Smuzhiyun };
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun /********** Function prototypes. **********/
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
928*4882a593Smuzhiyun 				  const struct tomoyo_group *group);
929*4882a593Smuzhiyun bool tomoyo_compare_number_union(const unsigned long value,
930*4882a593Smuzhiyun 				 const struct tomoyo_number_union *ptr);
931*4882a593Smuzhiyun bool tomoyo_condition(struct tomoyo_request_info *r,
932*4882a593Smuzhiyun 		      const struct tomoyo_condition *cond);
933*4882a593Smuzhiyun bool tomoyo_correct_domain(const unsigned char *domainname);
934*4882a593Smuzhiyun bool tomoyo_correct_path(const char *filename);
935*4882a593Smuzhiyun bool tomoyo_correct_word(const char *string);
936*4882a593Smuzhiyun bool tomoyo_domain_def(const unsigned char *buffer);
937*4882a593Smuzhiyun bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
938*4882a593Smuzhiyun bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
939*4882a593Smuzhiyun 		      struct tomoyo_page_dump *dump);
940*4882a593Smuzhiyun bool tomoyo_memory_ok(void *ptr);
941*4882a593Smuzhiyun bool tomoyo_number_matches_group(const unsigned long min,
942*4882a593Smuzhiyun 				 const unsigned long max,
943*4882a593Smuzhiyun 				 const struct tomoyo_group *group);
944*4882a593Smuzhiyun bool tomoyo_parse_ipaddr_union(struct tomoyo_acl_param *param,
945*4882a593Smuzhiyun 			       struct tomoyo_ipaddr_union *ptr);
946*4882a593Smuzhiyun bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
947*4882a593Smuzhiyun 			     struct tomoyo_name_union *ptr);
948*4882a593Smuzhiyun bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
949*4882a593Smuzhiyun 			       struct tomoyo_number_union *ptr);
950*4882a593Smuzhiyun bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
951*4882a593Smuzhiyun 				 const struct tomoyo_path_info *pattern);
952*4882a593Smuzhiyun bool tomoyo_permstr(const char *string, const char *keyword);
953*4882a593Smuzhiyun bool tomoyo_str_starts(char **src, const char *find);
954*4882a593Smuzhiyun char *tomoyo_encode(const char *str);
955*4882a593Smuzhiyun char *tomoyo_encode2(const char *str, int str_len);
956*4882a593Smuzhiyun char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
957*4882a593Smuzhiyun 		      va_list args);
958*4882a593Smuzhiyun char *tomoyo_read_token(struct tomoyo_acl_param *param);
959*4882a593Smuzhiyun char *tomoyo_realpath_from_path(const struct path *path);
960*4882a593Smuzhiyun char *tomoyo_realpath_nofollow(const char *pathname);
961*4882a593Smuzhiyun const char *tomoyo_get_exe(void);
962*4882a593Smuzhiyun const char *tomoyo_yesno(const unsigned int value);
963*4882a593Smuzhiyun const struct tomoyo_path_info *tomoyo_compare_name_union
964*4882a593Smuzhiyun (const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
965*4882a593Smuzhiyun const struct tomoyo_path_info *tomoyo_get_domainname
966*4882a593Smuzhiyun (struct tomoyo_acl_param *param);
967*4882a593Smuzhiyun const struct tomoyo_path_info *tomoyo_get_name(const char *name);
968*4882a593Smuzhiyun const struct tomoyo_path_info *tomoyo_path_matches_group
969*4882a593Smuzhiyun (const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
970*4882a593Smuzhiyun int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
971*4882a593Smuzhiyun 				 const struct path *path, const int flag);
972*4882a593Smuzhiyun void tomoyo_close_control(struct tomoyo_io_buffer *head);
973*4882a593Smuzhiyun int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env);
974*4882a593Smuzhiyun int tomoyo_execute_permission(struct tomoyo_request_info *r,
975*4882a593Smuzhiyun 			      const struct tomoyo_path_info *filename);
976*4882a593Smuzhiyun int tomoyo_find_next_domain(struct linux_binprm *bprm);
977*4882a593Smuzhiyun int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
978*4882a593Smuzhiyun 		    const u8 index);
979*4882a593Smuzhiyun int tomoyo_init_request_info(struct tomoyo_request_info *r,
980*4882a593Smuzhiyun 			     struct tomoyo_domain_info *domain,
981*4882a593Smuzhiyun 			     const u8 index);
982*4882a593Smuzhiyun int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
983*4882a593Smuzhiyun 		      const unsigned int mode, unsigned int dev);
984*4882a593Smuzhiyun int tomoyo_mount_permission(const char *dev_name, const struct path *path,
985*4882a593Smuzhiyun 			    const char *type, unsigned long flags,
986*4882a593Smuzhiyun 			    void *data_page);
987*4882a593Smuzhiyun int tomoyo_open_control(const u8 type, struct file *file);
988*4882a593Smuzhiyun int tomoyo_path2_perm(const u8 operation, const struct path *path1,
989*4882a593Smuzhiyun 		      const struct path *path2);
990*4882a593Smuzhiyun int tomoyo_path_number_perm(const u8 operation, const struct path *path,
991*4882a593Smuzhiyun 			    unsigned long number);
992*4882a593Smuzhiyun int tomoyo_path_perm(const u8 operation, const struct path *path,
993*4882a593Smuzhiyun 		     const char *target);
994*4882a593Smuzhiyun __poll_t tomoyo_poll_control(struct file *file, poll_table *wait);
995*4882a593Smuzhiyun __poll_t tomoyo_poll_log(struct file *file, poll_table *wait);
996*4882a593Smuzhiyun int tomoyo_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
997*4882a593Smuzhiyun 				  int addr_len);
998*4882a593Smuzhiyun int tomoyo_socket_connect_permission(struct socket *sock,
999*4882a593Smuzhiyun 				     struct sockaddr *addr, int addr_len);
1000*4882a593Smuzhiyun int tomoyo_socket_listen_permission(struct socket *sock);
1001*4882a593Smuzhiyun int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
1002*4882a593Smuzhiyun 				     int size);
1003*4882a593Smuzhiyun int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1004*4882a593Smuzhiyun 	__printf(2, 3);
1005*4882a593Smuzhiyun int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
1006*4882a593Smuzhiyun 			 struct tomoyo_acl_param *param,
1007*4882a593Smuzhiyun 			 bool (*check_duplicate)
1008*4882a593Smuzhiyun 			 (const struct tomoyo_acl_info *,
1009*4882a593Smuzhiyun 			  const struct tomoyo_acl_info *),
1010*4882a593Smuzhiyun 			 bool (*merge_duplicate)
1011*4882a593Smuzhiyun 			 (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
1012*4882a593Smuzhiyun 			  const bool));
1013*4882a593Smuzhiyun int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
1014*4882a593Smuzhiyun 			 struct tomoyo_acl_param *param,
1015*4882a593Smuzhiyun 			 bool (*check_duplicate)
1016*4882a593Smuzhiyun 			 (const struct tomoyo_acl_head *,
1017*4882a593Smuzhiyun 			  const struct tomoyo_acl_head *));
1018*4882a593Smuzhiyun int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
1019*4882a593Smuzhiyun int tomoyo_write_file(struct tomoyo_acl_param *param);
1020*4882a593Smuzhiyun int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
1021*4882a593Smuzhiyun int tomoyo_write_misc(struct tomoyo_acl_param *param);
1022*4882a593Smuzhiyun int tomoyo_write_inet_network(struct tomoyo_acl_param *param);
1023*4882a593Smuzhiyun int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
1024*4882a593Smuzhiyun 				    const u8 type);
1025*4882a593Smuzhiyun int tomoyo_write_unix_network(struct tomoyo_acl_param *param);
1026*4882a593Smuzhiyun ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
1027*4882a593Smuzhiyun 			    const int buffer_len);
1028*4882a593Smuzhiyun ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
1029*4882a593Smuzhiyun 			     const char __user *buffer, const int buffer_len);
1030*4882a593Smuzhiyun struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
1031*4882a593Smuzhiyun struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
1032*4882a593Smuzhiyun 						const bool transit);
1033*4882a593Smuzhiyun struct tomoyo_domain_info *tomoyo_domain(void);
1034*4882a593Smuzhiyun struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
1035*4882a593Smuzhiyun struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
1036*4882a593Smuzhiyun 				      const u8 idx);
1037*4882a593Smuzhiyun struct tomoyo_policy_namespace *tomoyo_assign_namespace
1038*4882a593Smuzhiyun (const char *domainname);
1039*4882a593Smuzhiyun struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
1040*4882a593Smuzhiyun 				      const u8 profile);
1041*4882a593Smuzhiyun unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
1042*4882a593Smuzhiyun 				const u8 index);
1043*4882a593Smuzhiyun u8 tomoyo_parse_ulong(unsigned long *result, char **str);
1044*4882a593Smuzhiyun void *tomoyo_commit_ok(void *data, const unsigned int size);
1045*4882a593Smuzhiyun void __init tomoyo_load_builtin_policy(void);
1046*4882a593Smuzhiyun void __init tomoyo_mm_init(void);
1047*4882a593Smuzhiyun void tomoyo_check_acl(struct tomoyo_request_info *r,
1048*4882a593Smuzhiyun 		      bool (*check_entry)(struct tomoyo_request_info *,
1049*4882a593Smuzhiyun 					  const struct tomoyo_acl_info *));
1050*4882a593Smuzhiyun void tomoyo_check_profile(void);
1051*4882a593Smuzhiyun void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp);
1052*4882a593Smuzhiyun void tomoyo_del_condition(struct list_head *element);
1053*4882a593Smuzhiyun void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
1054*4882a593Smuzhiyun void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
1055*4882a593Smuzhiyun void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
1056*4882a593Smuzhiyun void tomoyo_load_policy(const char *filename);
1057*4882a593Smuzhiyun void tomoyo_normalize_line(unsigned char *buffer);
1058*4882a593Smuzhiyun void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
1059*4882a593Smuzhiyun void tomoyo_print_ip(char *buf, const unsigned int size,
1060*4882a593Smuzhiyun 		     const struct tomoyo_ipaddr_union *ptr);
1061*4882a593Smuzhiyun void tomoyo_print_ulong(char *buffer, const int buffer_len,
1062*4882a593Smuzhiyun 			const unsigned long value, const u8 type);
1063*4882a593Smuzhiyun void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
1064*4882a593Smuzhiyun void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
1065*4882a593Smuzhiyun void tomoyo_read_log(struct tomoyo_io_buffer *head);
1066*4882a593Smuzhiyun void tomoyo_update_stat(const u8 index);
1067*4882a593Smuzhiyun void tomoyo_warn_oom(const char *function);
1068*4882a593Smuzhiyun void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
1069*4882a593Smuzhiyun 	__printf(2, 3);
1070*4882a593Smuzhiyun void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
1071*4882a593Smuzhiyun 		       va_list args);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun /********** External variable definitions. **********/
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun extern bool tomoyo_policy_loaded;
1076*4882a593Smuzhiyun extern int tomoyo_enabled;
1077*4882a593Smuzhiyun extern const char * const tomoyo_condition_keyword
1078*4882a593Smuzhiyun [TOMOYO_MAX_CONDITION_KEYWORD];
1079*4882a593Smuzhiyun extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
1080*4882a593Smuzhiyun extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
1081*4882a593Smuzhiyun 					      + TOMOYO_MAX_MAC_CATEGORY_INDEX];
1082*4882a593Smuzhiyun extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
1083*4882a593Smuzhiyun extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
1084*4882a593Smuzhiyun extern const char * const tomoyo_proto_keyword[TOMOYO_SOCK_MAX];
1085*4882a593Smuzhiyun extern const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION];
1086*4882a593Smuzhiyun extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
1087*4882a593Smuzhiyun extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
1088*4882a593Smuzhiyun extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
1089*4882a593Smuzhiyun extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
1090*4882a593Smuzhiyun extern struct list_head tomoyo_condition_list;
1091*4882a593Smuzhiyun extern struct list_head tomoyo_domain_list;
1092*4882a593Smuzhiyun extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
1093*4882a593Smuzhiyun extern struct list_head tomoyo_namespace_list;
1094*4882a593Smuzhiyun extern struct mutex tomoyo_policy_lock;
1095*4882a593Smuzhiyun extern struct srcu_struct tomoyo_ss;
1096*4882a593Smuzhiyun extern struct tomoyo_domain_info tomoyo_kernel_domain;
1097*4882a593Smuzhiyun extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
1098*4882a593Smuzhiyun extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
1099*4882a593Smuzhiyun extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
1100*4882a593Smuzhiyun extern struct lsm_blob_sizes tomoyo_blob_sizes;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun /********** Inlined functions. **********/
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun /**
1105*4882a593Smuzhiyun  * tomoyo_read_lock - Take lock for protecting policy.
1106*4882a593Smuzhiyun  *
1107*4882a593Smuzhiyun  * Returns index number for tomoyo_read_unlock().
1108*4882a593Smuzhiyun  */
tomoyo_read_lock(void)1109*4882a593Smuzhiyun static inline int tomoyo_read_lock(void)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun 	return srcu_read_lock(&tomoyo_ss);
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /**
1115*4882a593Smuzhiyun  * tomoyo_read_unlock - Release lock for protecting policy.
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * @idx: Index number returned by tomoyo_read_lock().
1118*4882a593Smuzhiyun  *
1119*4882a593Smuzhiyun  * Returns nothing.
1120*4882a593Smuzhiyun  */
tomoyo_read_unlock(int idx)1121*4882a593Smuzhiyun static inline void tomoyo_read_unlock(int idx)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	srcu_read_unlock(&tomoyo_ss, idx);
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun /**
1127*4882a593Smuzhiyun  * tomoyo_sys_getppid - Copy of getppid().
1128*4882a593Smuzhiyun  *
1129*4882a593Smuzhiyun  * Returns parent process's PID.
1130*4882a593Smuzhiyun  *
1131*4882a593Smuzhiyun  * Alpha does not have getppid() defined. To be able to build this module on
1132*4882a593Smuzhiyun  * Alpha, I have to copy getppid() from kernel/timer.c.
1133*4882a593Smuzhiyun  */
tomoyo_sys_getppid(void)1134*4882a593Smuzhiyun static inline pid_t tomoyo_sys_getppid(void)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun 	pid_t pid;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	rcu_read_lock();
1139*4882a593Smuzhiyun 	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1140*4882a593Smuzhiyun 	rcu_read_unlock();
1141*4882a593Smuzhiyun 	return pid;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun /**
1145*4882a593Smuzhiyun  * tomoyo_sys_getpid - Copy of getpid().
1146*4882a593Smuzhiyun  *
1147*4882a593Smuzhiyun  * Returns current thread's PID.
1148*4882a593Smuzhiyun  *
1149*4882a593Smuzhiyun  * Alpha does not have getpid() defined. To be able to build this module on
1150*4882a593Smuzhiyun  * Alpha, I have to copy getpid() from kernel/timer.c.
1151*4882a593Smuzhiyun  */
tomoyo_sys_getpid(void)1152*4882a593Smuzhiyun static inline pid_t tomoyo_sys_getpid(void)
1153*4882a593Smuzhiyun {
1154*4882a593Smuzhiyun 	return task_tgid_vnr(current);
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun /**
1158*4882a593Smuzhiyun  * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
1159*4882a593Smuzhiyun  *
1160*4882a593Smuzhiyun  * @a: Pointer to "struct tomoyo_path_info".
1161*4882a593Smuzhiyun  * @b: Pointer to "struct tomoyo_path_info".
1162*4882a593Smuzhiyun  *
1163*4882a593Smuzhiyun  * Returns true if @a == @b, false otherwise.
1164*4882a593Smuzhiyun  */
tomoyo_pathcmp(const struct tomoyo_path_info * a,const struct tomoyo_path_info * b)1165*4882a593Smuzhiyun static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
1166*4882a593Smuzhiyun 				  const struct tomoyo_path_info *b)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun 	return a->hash != b->hash || strcmp(a->name, b->name);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun  * tomoyo_put_name - Drop reference on "struct tomoyo_name".
1173*4882a593Smuzhiyun  *
1174*4882a593Smuzhiyun  * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
1175*4882a593Smuzhiyun  *
1176*4882a593Smuzhiyun  * Returns nothing.
1177*4882a593Smuzhiyun  */
tomoyo_put_name(const struct tomoyo_path_info * name)1178*4882a593Smuzhiyun static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun 	if (name) {
1181*4882a593Smuzhiyun 		struct tomoyo_name *ptr =
1182*4882a593Smuzhiyun 			container_of(name, typeof(*ptr), entry);
1183*4882a593Smuzhiyun 		atomic_dec(&ptr->head.users);
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun /**
1188*4882a593Smuzhiyun  * tomoyo_put_condition - Drop reference on "struct tomoyo_condition".
1189*4882a593Smuzhiyun  *
1190*4882a593Smuzhiyun  * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
1191*4882a593Smuzhiyun  *
1192*4882a593Smuzhiyun  * Returns nothing.
1193*4882a593Smuzhiyun  */
tomoyo_put_condition(struct tomoyo_condition * cond)1194*4882a593Smuzhiyun static inline void tomoyo_put_condition(struct tomoyo_condition *cond)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun 	if (cond)
1197*4882a593Smuzhiyun 		atomic_dec(&cond->head.users);
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun /**
1201*4882a593Smuzhiyun  * tomoyo_put_group - Drop reference on "struct tomoyo_group".
1202*4882a593Smuzhiyun  *
1203*4882a593Smuzhiyun  * @group: Pointer to "struct tomoyo_group". Maybe NULL.
1204*4882a593Smuzhiyun  *
1205*4882a593Smuzhiyun  * Returns nothing.
1206*4882a593Smuzhiyun  */
tomoyo_put_group(struct tomoyo_group * group)1207*4882a593Smuzhiyun static inline void tomoyo_put_group(struct tomoyo_group *group)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	if (group)
1210*4882a593Smuzhiyun 		atomic_dec(&group->head.users);
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun /**
1214*4882a593Smuzhiyun  * tomoyo_task - Get "struct tomoyo_task" for specified thread.
1215*4882a593Smuzhiyun  *
1216*4882a593Smuzhiyun  * @task - Pointer to "struct task_struct".
1217*4882a593Smuzhiyun  *
1218*4882a593Smuzhiyun  * Returns pointer to "struct tomoyo_task" for specified thread.
1219*4882a593Smuzhiyun  */
tomoyo_task(struct task_struct * task)1220*4882a593Smuzhiyun static inline struct tomoyo_task *tomoyo_task(struct task_struct *task)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun 	return task->security + tomoyo_blob_sizes.lbs_task;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun /**
1226*4882a593Smuzhiyun  * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
1227*4882a593Smuzhiyun  *
1228*4882a593Smuzhiyun  * @a: Pointer to "struct tomoyo_name_union".
1229*4882a593Smuzhiyun  * @b: Pointer to "struct tomoyo_name_union".
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  * Returns true if @a == @b, false otherwise.
1232*4882a593Smuzhiyun  */
tomoyo_same_name_union(const struct tomoyo_name_union * a,const struct tomoyo_name_union * b)1233*4882a593Smuzhiyun static inline bool tomoyo_same_name_union
1234*4882a593Smuzhiyun (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun 	return a->filename == b->filename && a->group == b->group;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun /**
1240*4882a593Smuzhiyun  * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
1241*4882a593Smuzhiyun  *
1242*4882a593Smuzhiyun  * @a: Pointer to "struct tomoyo_number_union".
1243*4882a593Smuzhiyun  * @b: Pointer to "struct tomoyo_number_union".
1244*4882a593Smuzhiyun  *
1245*4882a593Smuzhiyun  * Returns true if @a == @b, false otherwise.
1246*4882a593Smuzhiyun  */
tomoyo_same_number_union(const struct tomoyo_number_union * a,const struct tomoyo_number_union * b)1247*4882a593Smuzhiyun static inline bool tomoyo_same_number_union
1248*4882a593Smuzhiyun (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
1249*4882a593Smuzhiyun {
1250*4882a593Smuzhiyun 	return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
1251*4882a593Smuzhiyun 		a->group == b->group && a->value_type[0] == b->value_type[0] &&
1252*4882a593Smuzhiyun 		a->value_type[1] == b->value_type[1];
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun /**
1256*4882a593Smuzhiyun  * tomoyo_same_ipaddr_union - Check for duplicated "struct tomoyo_ipaddr_union" entry.
1257*4882a593Smuzhiyun  *
1258*4882a593Smuzhiyun  * @a: Pointer to "struct tomoyo_ipaddr_union".
1259*4882a593Smuzhiyun  * @b: Pointer to "struct tomoyo_ipaddr_union".
1260*4882a593Smuzhiyun  *
1261*4882a593Smuzhiyun  * Returns true if @a == @b, false otherwise.
1262*4882a593Smuzhiyun  */
tomoyo_same_ipaddr_union(const struct tomoyo_ipaddr_union * a,const struct tomoyo_ipaddr_union * b)1263*4882a593Smuzhiyun static inline bool tomoyo_same_ipaddr_union
1264*4882a593Smuzhiyun (const struct tomoyo_ipaddr_union *a, const struct tomoyo_ipaddr_union *b)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun 	return !memcmp(a->ip, b->ip, sizeof(a->ip)) && a->group == b->group &&
1267*4882a593Smuzhiyun 		a->is_ipv6 == b->is_ipv6;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun /**
1271*4882a593Smuzhiyun  * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
1272*4882a593Smuzhiyun  *
1273*4882a593Smuzhiyun  * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
1274*4882a593Smuzhiyun  */
tomoyo_current_namespace(void)1275*4882a593Smuzhiyun static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	return tomoyo_domain()->ns;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun #if defined(CONFIG_SLOB)
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun /**
1283*4882a593Smuzhiyun  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
1284*4882a593Smuzhiyun  *
1285*4882a593Smuzhiyun  * @size: Size to be rounded up.
1286*4882a593Smuzhiyun  *
1287*4882a593Smuzhiyun  * Returns @size.
1288*4882a593Smuzhiyun  *
1289*4882a593Smuzhiyun  * Since SLOB does not round up, this function simply returns @size.
1290*4882a593Smuzhiyun  */
tomoyo_round2(size_t size)1291*4882a593Smuzhiyun static inline int tomoyo_round2(size_t size)
1292*4882a593Smuzhiyun {
1293*4882a593Smuzhiyun 	return size;
1294*4882a593Smuzhiyun }
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun #else
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun /**
1299*4882a593Smuzhiyun  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
1300*4882a593Smuzhiyun  *
1301*4882a593Smuzhiyun  * @size: Size to be rounded up.
1302*4882a593Smuzhiyun  *
1303*4882a593Smuzhiyun  * Returns rounded size.
1304*4882a593Smuzhiyun  *
1305*4882a593Smuzhiyun  * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
1306*4882a593Smuzhiyun  * (e.g.) 128 bytes.
1307*4882a593Smuzhiyun  */
tomoyo_round2(size_t size)1308*4882a593Smuzhiyun static inline int tomoyo_round2(size_t size)
1309*4882a593Smuzhiyun {
1310*4882a593Smuzhiyun #if PAGE_SIZE == 4096
1311*4882a593Smuzhiyun 	size_t bsize = 32;
1312*4882a593Smuzhiyun #else
1313*4882a593Smuzhiyun 	size_t bsize = 64;
1314*4882a593Smuzhiyun #endif
1315*4882a593Smuzhiyun 	if (!size)
1316*4882a593Smuzhiyun 		return 0;
1317*4882a593Smuzhiyun 	while (size > bsize)
1318*4882a593Smuzhiyun 		bsize <<= 1;
1319*4882a593Smuzhiyun 	return bsize;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun #endif
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun /**
1325*4882a593Smuzhiyun  * list_for_each_cookie - iterate over a list with cookie.
1326*4882a593Smuzhiyun  * @pos:        the &struct list_head to use as a loop cursor.
1327*4882a593Smuzhiyun  * @head:       the head for your list.
1328*4882a593Smuzhiyun  */
1329*4882a593Smuzhiyun #define list_for_each_cookie(pos, head)					\
1330*4882a593Smuzhiyun 	if (!pos)							\
1331*4882a593Smuzhiyun 		pos =  srcu_dereference((head)->next, &tomoyo_ss);	\
1332*4882a593Smuzhiyun 	for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
1335