xref: /openbmc/linux/security/tomoyo/common.h (revision 3ddf17f0)
19590837bSKentaro Takeda /*
29590837bSKentaro Takeda  * security/tomoyo/common.h
39590837bSKentaro Takeda  *
476bb0895STetsuo Handa  * Header file for TOMOYO.
59590837bSKentaro Takeda  *
676bb0895STetsuo Handa  * Copyright (C) 2005-2010  NTT DATA CORPORATION
79590837bSKentaro Takeda  */
89590837bSKentaro Takeda 
99590837bSKentaro Takeda #ifndef _SECURITY_TOMOYO_COMMON_H
109590837bSKentaro Takeda #define _SECURITY_TOMOYO_COMMON_H
119590837bSKentaro Takeda 
129590837bSKentaro Takeda #include <linux/ctype.h>
139590837bSKentaro Takeda #include <linux/string.h>
149590837bSKentaro Takeda #include <linux/mm.h>
159590837bSKentaro Takeda #include <linux/file.h>
169590837bSKentaro Takeda #include <linux/kmod.h>
179590837bSKentaro Takeda #include <linux/fs.h>
189590837bSKentaro Takeda #include <linux/sched.h>
199590837bSKentaro Takeda #include <linux/namei.h>
209590837bSKentaro Takeda #include <linux/mount.h>
219590837bSKentaro Takeda #include <linux/list.h>
2276bb0895STetsuo Handa #include <linux/cred.h>
2317fcfbd9STetsuo Handa #include <linux/poll.h>
2476bb0895STetsuo Handa struct linux_binprm;
259590837bSKentaro Takeda 
2676bb0895STetsuo Handa /********** Constants definitions. **********/
2776bb0895STetsuo Handa 
2876bb0895STetsuo Handa /*
2976bb0895STetsuo Handa  * TOMOYO uses this hash only when appending a string into the string
3076bb0895STetsuo Handa  * table. Frequency of appending strings is very low. So we don't need
3176bb0895STetsuo Handa  * large (e.g. 64k) hash size. 256 will be sufficient.
3276bb0895STetsuo Handa  */
3376bb0895STetsuo Handa #define TOMOYO_HASH_BITS  8
3476bb0895STetsuo Handa #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
3576bb0895STetsuo Handa 
36c8c57e84STetsuo Handa #define TOMOYO_EXEC_TMPSIZE     4096
3776bb0895STetsuo Handa 
3876bb0895STetsuo Handa /* Profile number is an integer between 0 and 255. */
3976bb0895STetsuo Handa #define TOMOYO_MAX_PROFILES 256
4076bb0895STetsuo Handa 
4132997144STetsuo Handa /* Group number is an integer between 0 and 255. */
4232997144STetsuo Handa #define TOMOYO_MAX_ACL_GROUPS 256
4332997144STetsuo Handa 
44b5bc60b4STetsuo Handa /* Index numbers for operation mode. */
45cb0abe6aSTetsuo Handa enum tomoyo_mode_index {
46cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_DISABLED,
47cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_LEARNING,
48cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_PERMISSIVE,
4957c2590fSTetsuo Handa 	TOMOYO_CONFIG_ENFORCING,
50eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_MAX_MODE,
51eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_WANT_REJECT_LOG =  64,
52eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_WANT_GRANT_LOG  = 128,
53eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_USE_DEFAULT     = 255,
54cb0abe6aSTetsuo Handa };
55cb0abe6aSTetsuo Handa 
56b5bc60b4STetsuo Handa /* Index numbers for entry type. */
57a230f9e7STetsuo Handa enum tomoyo_policy_id {
58a230f9e7STetsuo Handa 	TOMOYO_ID_GROUP,
59a230f9e7STetsuo Handa 	TOMOYO_ID_PATH_GROUP,
60a230f9e7STetsuo Handa 	TOMOYO_ID_NUMBER_GROUP,
615448ec4fSTetsuo Handa 	TOMOYO_ID_TRANSITION_CONTROL,
62a230f9e7STetsuo Handa 	TOMOYO_ID_AGGREGATOR,
63a230f9e7STetsuo Handa 	TOMOYO_ID_MANAGER,
64a230f9e7STetsuo Handa 	TOMOYO_ID_NAME,
65a230f9e7STetsuo Handa 	TOMOYO_ID_ACL,
66a230f9e7STetsuo Handa 	TOMOYO_ID_DOMAIN,
67a230f9e7STetsuo Handa 	TOMOYO_MAX_POLICY
68a230f9e7STetsuo Handa };
69a230f9e7STetsuo Handa 
702c47ab93STetsuo Handa /* Index numbers for domain's attributes. */
712c47ab93STetsuo Handa enum tomoyo_domain_info_flags_index {
722c47ab93STetsuo Handa 	/* Quota warnning flag.   */
732c47ab93STetsuo Handa 	TOMOYO_DIF_QUOTA_WARNED,
742c47ab93STetsuo Handa 	/*
752c47ab93STetsuo Handa 	 * This domain was unable to create a new domain at
762c47ab93STetsuo Handa 	 * tomoyo_find_next_domain() because the name of the domain to be
772c47ab93STetsuo Handa 	 * created was too long or it could not allocate memory.
782c47ab93STetsuo Handa 	 * More than one process continued execve() without domain transition.
792c47ab93STetsuo Handa 	 */
802c47ab93STetsuo Handa 	TOMOYO_DIF_TRANSITION_FAILED,
812c47ab93STetsuo Handa 	TOMOYO_MAX_DOMAIN_INFO_FLAGS
822c47ab93STetsuo Handa };
832c47ab93STetsuo Handa 
84b5bc60b4STetsuo Handa /* Index numbers for group entries. */
85a230f9e7STetsuo Handa enum tomoyo_group_id {
86a230f9e7STetsuo Handa 	TOMOYO_PATH_GROUP,
87a230f9e7STetsuo Handa 	TOMOYO_NUMBER_GROUP,
88a230f9e7STetsuo Handa 	TOMOYO_MAX_GROUP
89a230f9e7STetsuo Handa };
90a230f9e7STetsuo Handa 
91b5bc60b4STetsuo Handa /* Index numbers for type of numeric values. */
92b5bc60b4STetsuo Handa enum tomoyo_value_type {
93b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_INVALID,
94b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_DECIMAL,
95b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_OCTAL,
96b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_HEXADECIMAL,
97b5bc60b4STetsuo Handa };
984c3e9e2dSTetsuo Handa 
99b5bc60b4STetsuo Handa /* Index numbers for domain transition control keywords. */
1005448ec4fSTetsuo Handa enum tomoyo_transition_type {
1015448ec4fSTetsuo Handa 	/* Do not change this order, */
102bd03a3e4STetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_RESET,
103bd03a3e4STetsuo Handa 	TOMOYO_TRANSITION_CONTROL_RESET,
1045448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
1055448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_INITIALIZE,
1065448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_KEEP,
1075448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_KEEP,
1085448ec4fSTetsuo Handa 	TOMOYO_MAX_TRANSITION_TYPE
1095448ec4fSTetsuo Handa };
1105448ec4fSTetsuo Handa 
11176bb0895STetsuo Handa /* Index numbers for Access Controls. */
112084da356STetsuo Handa enum tomoyo_acl_entry_type_index {
1137ef61233STetsuo Handa 	TOMOYO_TYPE_PATH_ACL,
1147ef61233STetsuo Handa 	TOMOYO_TYPE_PATH2_ACL,
115a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_PATH_NUMBER_ACL,
11675093152STetsuo Handa 	TOMOYO_TYPE_MKDEV_ACL,
1172106ccd9STetsuo Handa 	TOMOYO_TYPE_MOUNT_ACL,
118084da356STetsuo Handa };
11976bb0895STetsuo Handa 
120b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname. */
121084da356STetsuo Handa enum tomoyo_path_acl_index {
1227ef61233STetsuo Handa 	TOMOYO_TYPE_EXECUTE,
1237ef61233STetsuo Handa 	TOMOYO_TYPE_READ,
1247ef61233STetsuo Handa 	TOMOYO_TYPE_WRITE,
1257c75964fSTetsuo Handa 	TOMOYO_TYPE_APPEND,
1267ef61233STetsuo Handa 	TOMOYO_TYPE_UNLINK,
1277c75964fSTetsuo Handa 	TOMOYO_TYPE_GETATTR,
1287ef61233STetsuo Handa 	TOMOYO_TYPE_RMDIR,
1297ef61233STetsuo Handa 	TOMOYO_TYPE_TRUNCATE,
1307ef61233STetsuo Handa 	TOMOYO_TYPE_SYMLINK,
1317ef61233STetsuo Handa 	TOMOYO_TYPE_CHROOT,
1327ef61233STetsuo Handa 	TOMOYO_TYPE_UMOUNT,
1337ef61233STetsuo Handa 	TOMOYO_MAX_PATH_OPERATION
134084da356STetsuo Handa };
13576bb0895STetsuo Handa 
136b22b8b9fSTetsuo Handa /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
137eadd99ccSTetsuo Handa enum tomoyo_memory_stat_type {
138eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_POLICY,
139eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_AUDIT,
140eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_QUERY,
141eadd99ccSTetsuo Handa 	TOMOYO_MAX_MEMORY_STAT
142eadd99ccSTetsuo Handa };
143eadd99ccSTetsuo Handa 
14475093152STetsuo Handa enum tomoyo_mkdev_acl_index {
145a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKBLOCK,
146a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKCHAR,
14775093152STetsuo Handa 	TOMOYO_MAX_MKDEV_OPERATION
148a1f9bb6aSTetsuo Handa };
149a1f9bb6aSTetsuo Handa 
150b5bc60b4STetsuo Handa /* Index numbers for access controls with two pathnames. */
151084da356STetsuo Handa enum tomoyo_path2_acl_index {
1527ef61233STetsuo Handa 	TOMOYO_TYPE_LINK,
1537ef61233STetsuo Handa 	TOMOYO_TYPE_RENAME,
1547ef61233STetsuo Handa 	TOMOYO_TYPE_PIVOT_ROOT,
1557ef61233STetsuo Handa 	TOMOYO_MAX_PATH2_OPERATION
156084da356STetsuo Handa };
15776bb0895STetsuo Handa 
158b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname and one number. */
159a1f9bb6aSTetsuo Handa enum tomoyo_path_number_acl_index {
160a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CREATE,
161a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKDIR,
162a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKFIFO,
163a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKSOCK,
164a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_IOCTL,
165a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHMOD,
166a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHOWN,
167a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHGRP,
168a1f9bb6aSTetsuo Handa 	TOMOYO_MAX_PATH_NUMBER_OPERATION
169a1f9bb6aSTetsuo Handa };
170a1f9bb6aSTetsuo Handa 
171b5bc60b4STetsuo Handa /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
172084da356STetsuo Handa enum tomoyo_securityfs_interface_index {
173084da356STetsuo Handa 	TOMOYO_DOMAINPOLICY,
174084da356STetsuo Handa 	TOMOYO_EXCEPTIONPOLICY,
175084da356STetsuo Handa 	TOMOYO_DOMAIN_STATUS,
176084da356STetsuo Handa 	TOMOYO_PROCESS_STATUS,
177b22b8b9fSTetsuo Handa 	TOMOYO_STAT,
178084da356STetsuo Handa 	TOMOYO_SELFDOMAIN,
179eadd99ccSTetsuo Handa 	TOMOYO_AUDIT,
180084da356STetsuo Handa 	TOMOYO_VERSION,
181084da356STetsuo Handa 	TOMOYO_PROFILE,
18217fcfbd9STetsuo Handa 	TOMOYO_QUERY,
183084da356STetsuo Handa 	TOMOYO_MANAGER
184084da356STetsuo Handa };
18576bb0895STetsuo Handa 
186b5bc60b4STetsuo Handa /* Index numbers for special mount operations. */
187b5bc60b4STetsuo Handa enum tomoyo_special_mount {
188b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_BIND,            /* mount --bind /source /dest   */
189b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MOVE,            /* mount --move /old /new       */
190b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_REMOUNT,         /* mount -o remount /dir        */
191b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
192b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_PRIVATE,    /* mount --make-private /dir    */
193b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SLAVE,      /* mount --make-slave /dir      */
194b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SHARED,     /* mount --make-shared /dir     */
195b5bc60b4STetsuo Handa 	TOMOYO_MAX_SPECIAL_MOUNT
196b5bc60b4STetsuo Handa };
197b5bc60b4STetsuo Handa 
198b5bc60b4STetsuo Handa /* Index numbers for functionality. */
19957c2590fSTetsuo Handa enum tomoyo_mac_index {
20057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_EXECUTE,
20157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_OPEN,
20257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CREATE,
20357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UNLINK,
2047c75964fSTetsuo Handa 	TOMOYO_MAC_FILE_GETATTR,
20557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKDIR,
20657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RMDIR,
20757c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKFIFO,
20857c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKSOCK,
20957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_TRUNCATE,
21057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_SYMLINK,
21157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKBLOCK,
21257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKCHAR,
21357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_LINK,
21457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RENAME,
21557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHMOD,
21657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHOWN,
21757c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHGRP,
21857c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_IOCTL,
21957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHROOT,
22057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MOUNT,
22157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UMOUNT,
22257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_PIVOT_ROOT,
22357c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_INDEX
22457c2590fSTetsuo Handa };
22557c2590fSTetsuo Handa 
226b5bc60b4STetsuo Handa /* Index numbers for category of functionality. */
22757c2590fSTetsuo Handa enum tomoyo_mac_category_index {
22857c2590fSTetsuo Handa 	TOMOYO_MAC_CATEGORY_FILE,
22957c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_CATEGORY_INDEX
23057c2590fSTetsuo Handa };
23157c2590fSTetsuo Handa 
232b5bc60b4STetsuo Handa /*
233b5bc60b4STetsuo Handa  * Retry this request. Returned by tomoyo_supervisor() if policy violation has
234b5bc60b4STetsuo Handa  * occurred in enforcing mode and the userspace daemon decided to retry.
235b5bc60b4STetsuo Handa  *
236b5bc60b4STetsuo Handa  * We must choose a positive value in order to distinguish "granted" (which is
237b5bc60b4STetsuo Handa  * 0) and "rejected" (which is a negative value) and "retry".
238b5bc60b4STetsuo Handa  */
239b5bc60b4STetsuo Handa #define TOMOYO_RETRY_REQUEST 1
24017fcfbd9STetsuo Handa 
241b22b8b9fSTetsuo Handa /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
242b22b8b9fSTetsuo Handa enum tomoyo_policy_stat_type {
243b22b8b9fSTetsuo Handa 	/* Do not change this order. */
244b22b8b9fSTetsuo Handa 	TOMOYO_STAT_POLICY_UPDATES,
245b22b8b9fSTetsuo Handa 	TOMOYO_STAT_POLICY_LEARNING,   /* == TOMOYO_CONFIG_LEARNING */
246b22b8b9fSTetsuo Handa 	TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
247b22b8b9fSTetsuo Handa 	TOMOYO_STAT_POLICY_ENFORCING,  /* == TOMOYO_CONFIG_ENFORCING */
248b22b8b9fSTetsuo Handa 	TOMOYO_MAX_POLICY_STAT
249b22b8b9fSTetsuo Handa };
250b22b8b9fSTetsuo Handa 
251d5ca1725STetsuo Handa /* Index numbers for profile's PREFERENCE values. */
252d5ca1725STetsuo Handa enum tomoyo_pref_index {
253eadd99ccSTetsuo Handa 	TOMOYO_PREF_MAX_AUDIT_LOG,
254d5ca1725STetsuo Handa 	TOMOYO_PREF_MAX_LEARNING_ENTRY,
255d5ca1725STetsuo Handa 	TOMOYO_MAX_PREF
256d5ca1725STetsuo Handa };
257d5ca1725STetsuo Handa 
25876bb0895STetsuo Handa /********** Structure definitions. **********/
2599590837bSKentaro Takeda 
260b5bc60b4STetsuo Handa /* Common header for holding ACL entries. */
26182e0f001STetsuo Handa struct tomoyo_acl_head {
26282e0f001STetsuo Handa 	struct list_head list;
26382e0f001STetsuo Handa 	bool is_deleted;
26482e0f001STetsuo Handa } __packed;
26582e0f001STetsuo Handa 
2660df7e8b8STetsuo Handa /* Common header for shared entries. */
2670df7e8b8STetsuo Handa struct tomoyo_shared_acl_head {
2680df7e8b8STetsuo Handa 	struct list_head list;
2690df7e8b8STetsuo Handa 	atomic_t users;
2700df7e8b8STetsuo Handa } __packed;
2710df7e8b8STetsuo Handa 
272bd03a3e4STetsuo Handa struct tomoyo_policy_namespace;
273bd03a3e4STetsuo Handa 
274b5bc60b4STetsuo Handa /* Structure for request info. */
275cb0abe6aSTetsuo Handa struct tomoyo_request_info {
276cb0abe6aSTetsuo Handa 	struct tomoyo_domain_info *domain;
277cf6e9a64STetsuo Handa 	/* For holding parameters. */
278cf6e9a64STetsuo Handa 	union {
279cf6e9a64STetsuo Handa 		struct {
280cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
281484ca79cSTetsuo Handa 			/* For using wildcards at tomoyo_find_next_domain(). */
282484ca79cSTetsuo Handa 			const struct tomoyo_path_info *matched_path;
283b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path_acl_index". */
284cf6e9a64STetsuo Handa 			u8 operation;
285cf6e9a64STetsuo Handa 		} path;
286cf6e9a64STetsuo Handa 		struct {
287cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename1;
288cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename2;
289b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path2_acl_index". */
290cf6e9a64STetsuo Handa 			u8 operation;
291cf6e9a64STetsuo Handa 		} path2;
292cf6e9a64STetsuo Handa 		struct {
293cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
294cf6e9a64STetsuo Handa 			unsigned int mode;
295cf6e9a64STetsuo Handa 			unsigned int major;
296cf6e9a64STetsuo Handa 			unsigned int minor;
297b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_mkdev_acl_index". */
298cf6e9a64STetsuo Handa 			u8 operation;
299cf6e9a64STetsuo Handa 		} mkdev;
300cf6e9a64STetsuo Handa 		struct {
301cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
302cf6e9a64STetsuo Handa 			unsigned long number;
303b5bc60b4STetsuo Handa 			/*
304b5bc60b4STetsuo Handa 			 * One of values in
305b5bc60b4STetsuo Handa 			 * "enum tomoyo_path_number_acl_index".
306b5bc60b4STetsuo Handa 			 */
307cf6e9a64STetsuo Handa 			u8 operation;
308cf6e9a64STetsuo Handa 		} path_number;
309cf6e9a64STetsuo Handa 		struct {
310cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *type;
311cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dir;
312cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dev;
313cf6e9a64STetsuo Handa 			unsigned long flags;
314cf6e9a64STetsuo Handa 			int need_dev;
315cf6e9a64STetsuo Handa 		} mount;
316cf6e9a64STetsuo Handa 	} param;
317cf6e9a64STetsuo Handa 	u8 param_type;
318cf6e9a64STetsuo Handa 	bool granted;
31917fcfbd9STetsuo Handa 	u8 retry;
32017fcfbd9STetsuo Handa 	u8 profile;
321cb0abe6aSTetsuo Handa 	u8 mode; /* One of tomoyo_mode_index . */
32257c2590fSTetsuo Handa 	u8 type;
323cb0abe6aSTetsuo Handa };
324cb0abe6aSTetsuo Handa 
325b5bc60b4STetsuo Handa /* Structure for holding a token. */
3269590837bSKentaro Takeda struct tomoyo_path_info {
3279590837bSKentaro Takeda 	const char *name;
3289590837bSKentaro Takeda 	u32 hash;          /* = full_name_hash(name, strlen(name)) */
3299590837bSKentaro Takeda 	u16 const_len;     /* = tomoyo_const_part_length(name)     */
3309590837bSKentaro Takeda 	bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
3319590837bSKentaro Takeda 	bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
3329590837bSKentaro Takeda };
3339590837bSKentaro Takeda 
334b5bc60b4STetsuo Handa /* Structure for holding string data. */
335e2bf6907STetsuo Handa struct tomoyo_name {
3360df7e8b8STetsuo Handa 	struct tomoyo_shared_acl_head head;
33776bb0895STetsuo Handa 	struct tomoyo_path_info entry;
33876bb0895STetsuo Handa };
3399590837bSKentaro Takeda 
340b5bc60b4STetsuo Handa /* Structure for holding a word. */
3417762fbffSTetsuo Handa struct tomoyo_name_union {
342b5bc60b4STetsuo Handa 	/* Either @filename or @group is NULL. */
3437762fbffSTetsuo Handa 	const struct tomoyo_path_info *filename;
344a98aa4deSTetsuo Handa 	struct tomoyo_group *group;
3457762fbffSTetsuo Handa };
3467762fbffSTetsuo Handa 
347b5bc60b4STetsuo Handa /* Structure for holding a number. */
3484c3e9e2dSTetsuo Handa struct tomoyo_number_union {
3494c3e9e2dSTetsuo Handa 	unsigned long values[2];
350b5bc60b4STetsuo Handa 	struct tomoyo_group *group; /* Maybe NULL. */
351b5bc60b4STetsuo Handa 	/* One of values in "enum tomoyo_value_type". */
3520df7e8b8STetsuo Handa 	u8 value_type[2];
3534c3e9e2dSTetsuo Handa };
3544c3e9e2dSTetsuo Handa 
355a98aa4deSTetsuo Handa /* Structure for "path_group"/"number_group" directive. */
356a98aa4deSTetsuo Handa struct tomoyo_group {
3570df7e8b8STetsuo Handa 	struct tomoyo_shared_acl_head head;
358a98aa4deSTetsuo Handa 	const struct tomoyo_path_info *group_name;
359a98aa4deSTetsuo Handa 	struct list_head member_list;
360a98aa4deSTetsuo Handa };
361a98aa4deSTetsuo Handa 
3627762fbffSTetsuo Handa /* Structure for "path_group" directive. */
3637762fbffSTetsuo Handa struct tomoyo_path_group {
36482e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3657762fbffSTetsuo Handa 	const struct tomoyo_path_info *member_name;
3667762fbffSTetsuo Handa };
3677762fbffSTetsuo Handa 
3684c3e9e2dSTetsuo Handa /* Structure for "number_group" directive. */
369a98aa4deSTetsuo Handa struct tomoyo_number_group {
37082e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3714c3e9e2dSTetsuo Handa 	struct tomoyo_number_union number;
3724c3e9e2dSTetsuo Handa };
3734c3e9e2dSTetsuo Handa 
374b5bc60b4STetsuo Handa /* Common header for individual entries. */
3759590837bSKentaro Takeda struct tomoyo_acl_info {
3769590837bSKentaro Takeda 	struct list_head list;
377237ab459STetsuo Handa 	bool is_deleted;
378b5bc60b4STetsuo Handa 	u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
3799590837bSKentaro Takeda } __packed;
3809590837bSKentaro Takeda 
381b5bc60b4STetsuo Handa /* Structure for domain information. */
3829590837bSKentaro Takeda struct tomoyo_domain_info {
3839590837bSKentaro Takeda 	struct list_head list;
3849590837bSKentaro Takeda 	struct list_head acl_info_list;
3859590837bSKentaro Takeda 	/* Name of this domain. Never NULL.          */
3869590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname;
387bd03a3e4STetsuo Handa 	/* Namespace for this domain. Never NULL. */
388bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
3899590837bSKentaro Takeda 	u8 profile;        /* Profile number to use. */
39032997144STetsuo Handa 	u8 group;          /* Group number to use.   */
391a0558fc3STetsuo Handa 	bool is_deleted;   /* Delete flag.           */
3922c47ab93STetsuo Handa 	bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
393ec8e6a4eSTetsuo Handa 	atomic_t users; /* Number of referring credentials. */
3949590837bSKentaro Takeda };
3959590837bSKentaro Takeda 
3969590837bSKentaro Takeda /*
397b5bc60b4STetsuo Handa  * Structure for "file execute", "file read", "file write", "file append",
398b5bc60b4STetsuo Handa  * "file unlink", "file getattr", "file rmdir", "file truncate",
399b5bc60b4STetsuo Handa  * "file symlink", "file chroot" and "file unmount" directive.
4009590837bSKentaro Takeda  */
4017ef61233STetsuo Handa struct tomoyo_path_acl {
4027ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
403b5bc60b4STetsuo Handa 	u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
4047762fbffSTetsuo Handa 	struct tomoyo_name_union name;
4059590837bSKentaro Takeda };
4069590837bSKentaro Takeda 
407c3fa109aSTetsuo Handa /*
408b5bc60b4STetsuo Handa  * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
409b5bc60b4STetsuo Handa  * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
410a1f9bb6aSTetsuo Handa  */
411a1f9bb6aSTetsuo Handa struct tomoyo_path_number_acl {
412a1f9bb6aSTetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
413b5bc60b4STetsuo Handa 	/* Bitmask of values in "enum tomoyo_path_number_acl_index". */
414a1f9bb6aSTetsuo Handa 	u8 perm;
415a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
416a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union number;
417a1f9bb6aSTetsuo Handa };
418a1f9bb6aSTetsuo Handa 
419b5bc60b4STetsuo Handa /* Structure for "file mkblock" and "file mkchar" directive. */
42075093152STetsuo Handa struct tomoyo_mkdev_acl {
42175093152STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
422b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
423a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
424a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union mode;
425a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union major;
426a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union minor;
427a1f9bb6aSTetsuo Handa };
428a1f9bb6aSTetsuo Handa 
429a1f9bb6aSTetsuo Handa /*
430b5bc60b4STetsuo Handa  * Structure for "file rename", "file link" and "file pivot_root" directive.
431c3fa109aSTetsuo Handa  */
4327ef61233STetsuo Handa struct tomoyo_path2_acl {
4337ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
434b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
4357762fbffSTetsuo Handa 	struct tomoyo_name_union name1;
4367762fbffSTetsuo Handa 	struct tomoyo_name_union name2;
4379590837bSKentaro Takeda };
4389590837bSKentaro Takeda 
439b5bc60b4STetsuo Handa /* Structure for "file mount" directive. */
4402106ccd9STetsuo Handa struct tomoyo_mount_acl {
4412106ccd9STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
4422106ccd9STetsuo Handa 	struct tomoyo_name_union dev_name;
4432106ccd9STetsuo Handa 	struct tomoyo_name_union dir_name;
4442106ccd9STetsuo Handa 	struct tomoyo_name_union fs_type;
4452106ccd9STetsuo Handa 	struct tomoyo_number_union flags;
4462106ccd9STetsuo Handa };
4472106ccd9STetsuo Handa 
448a238cf5bSTetsuo Handa /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
449a238cf5bSTetsuo Handa struct tomoyo_acl_param {
450a238cf5bSTetsuo Handa 	char *data;
451a238cf5bSTetsuo Handa 	struct list_head *list;
452bd03a3e4STetsuo Handa 	struct tomoyo_policy_namespace *ns;
453a238cf5bSTetsuo Handa 	bool is_delete;
454a238cf5bSTetsuo Handa };
455a238cf5bSTetsuo Handa 
4560d2171d7STetsuo Handa #define TOMOYO_MAX_IO_READ_QUEUE 64
457f23571e8STetsuo Handa 
4582106ccd9STetsuo Handa /*
459f23571e8STetsuo Handa  * Structure for reading/writing policy via /sys/kernel/security/tomoyo
460f23571e8STetsuo Handa  * interfaces.
461c3fa109aSTetsuo Handa  */
4629590837bSKentaro Takeda struct tomoyo_io_buffer {
4638fbe71f0STetsuo Handa 	void (*read) (struct tomoyo_io_buffer *);
4649590837bSKentaro Takeda 	int (*write) (struct tomoyo_io_buffer *);
46517fcfbd9STetsuo Handa 	int (*poll) (struct file *file, poll_table *wait);
4669590837bSKentaro Takeda 	/* Exclusive lock for this structure.   */
4679590837bSKentaro Takeda 	struct mutex io_sem;
468f23571e8STetsuo Handa 	char __user *read_user_buf;
4692c47ab93STetsuo Handa 	size_t read_user_buf_avail;
470f23571e8STetsuo Handa 	struct {
471bd03a3e4STetsuo Handa 		struct list_head *ns;
472f23571e8STetsuo Handa 		struct list_head *domain;
473f23571e8STetsuo Handa 		struct list_head *group;
474f23571e8STetsuo Handa 		struct list_head *acl;
4752c47ab93STetsuo Handa 		size_t avail;
4762c47ab93STetsuo Handa 		unsigned int step;
4772c47ab93STetsuo Handa 		unsigned int query_index;
478f23571e8STetsuo Handa 		u16 index;
47932997144STetsuo Handa 		u8 acl_group_index;
480f23571e8STetsuo Handa 		u8 bit;
481f23571e8STetsuo Handa 		u8 w_pos;
482f23571e8STetsuo Handa 		bool eof;
483f23571e8STetsuo Handa 		bool print_this_domain_only;
484bd03a3e4STetsuo Handa 		bool print_transition_related_only;
485f23571e8STetsuo Handa 		const char *w[TOMOYO_MAX_IO_READ_QUEUE];
486f23571e8STetsuo Handa 	} r;
4870df7e8b8STetsuo Handa 	struct {
488bd03a3e4STetsuo Handa 		struct tomoyo_policy_namespace *ns;
4899590837bSKentaro Takeda 		/* The position currently writing to.   */
4900df7e8b8STetsuo Handa 		struct tomoyo_domain_info *domain;
4910df7e8b8STetsuo Handa 		/* Bytes available for writing.         */
4922c47ab93STetsuo Handa 		size_t avail;
493bd03a3e4STetsuo Handa 		bool is_delete;
4940df7e8b8STetsuo Handa 	} w;
4959590837bSKentaro Takeda 	/* Buffer for reading.                  */
4969590837bSKentaro Takeda 	char *read_buf;
4979590837bSKentaro Takeda 	/* Size of read buffer.                 */
4982c47ab93STetsuo Handa 	size_t readbuf_size;
4999590837bSKentaro Takeda 	/* Buffer for writing.                  */
5009590837bSKentaro Takeda 	char *write_buf;
5019590837bSKentaro Takeda 	/* Size of write buffer.                */
5022c47ab93STetsuo Handa 	size_t writebuf_size;
50317fcfbd9STetsuo Handa 	/* Type of this interface.              */
5042c47ab93STetsuo Handa 	enum tomoyo_securityfs_interface_index type;
5052e503bbbSTetsuo Handa 	/* Users counter protected by tomoyo_io_buffer_list_lock. */
5062e503bbbSTetsuo Handa 	u8 users;
5072e503bbbSTetsuo Handa 	/* List for telling GC not to kfree() elements. */
5082e503bbbSTetsuo Handa 	struct list_head list;
5099590837bSKentaro Takeda };
5109590837bSKentaro Takeda 
51176bb0895STetsuo Handa /*
512b5bc60b4STetsuo Handa  * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
513b5bc60b4STetsuo Handa  * "no_keep_domain" keyword.
51476bb0895STetsuo Handa  */
5155448ec4fSTetsuo Handa struct tomoyo_transition_control {
51682e0f001STetsuo Handa 	struct tomoyo_acl_head head;
5175448ec4fSTetsuo Handa 	u8 type; /* One of values in "enum tomoyo_transition_type".  */
51876bb0895STetsuo Handa 	/* True if the domainname is tomoyo_get_last_name(). */
51976bb0895STetsuo Handa 	bool is_last_name;
5205448ec4fSTetsuo Handa 	const struct tomoyo_path_info *domainname; /* Maybe NULL */
5215448ec4fSTetsuo Handa 	const struct tomoyo_path_info *program;    /* Maybe NULL */
52276bb0895STetsuo Handa };
52376bb0895STetsuo Handa 
524b5bc60b4STetsuo Handa /* Structure for "aggregator" keyword. */
525e2bf6907STetsuo Handa struct tomoyo_aggregator {
52682e0f001STetsuo Handa 	struct tomoyo_acl_head head;
5271084307cSTetsuo Handa 	const struct tomoyo_path_info *original_name;
5281084307cSTetsuo Handa 	const struct tomoyo_path_info *aggregated_name;
5291084307cSTetsuo Handa };
5301084307cSTetsuo Handa 
531b5bc60b4STetsuo Handa /* Structure for policy manager. */
532e2bf6907STetsuo Handa struct tomoyo_manager {
53382e0f001STetsuo Handa 	struct tomoyo_acl_head head;
53482e0f001STetsuo Handa 	bool is_domain;  /* True if manager is a domainname. */
53576bb0895STetsuo Handa 	/* A path to program or a domainname. */
53676bb0895STetsuo Handa 	const struct tomoyo_path_info *manager;
53776bb0895STetsuo Handa };
53876bb0895STetsuo Handa 
53957c2590fSTetsuo Handa struct tomoyo_preference {
54057c2590fSTetsuo Handa 	unsigned int learning_max_entry;
54157c2590fSTetsuo Handa 	bool enforcing_verbose;
54257c2590fSTetsuo Handa 	bool learning_verbose;
54357c2590fSTetsuo Handa 	bool permissive_verbose;
54457c2590fSTetsuo Handa };
54557c2590fSTetsuo Handa 
546b5bc60b4STetsuo Handa /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
54757c2590fSTetsuo Handa struct tomoyo_profile {
54857c2590fSTetsuo Handa 	const struct tomoyo_path_info *comment;
54957c2590fSTetsuo Handa 	struct tomoyo_preference *learning;
55057c2590fSTetsuo Handa 	struct tomoyo_preference *permissive;
55157c2590fSTetsuo Handa 	struct tomoyo_preference *enforcing;
55257c2590fSTetsuo Handa 	struct tomoyo_preference preference;
55357c2590fSTetsuo Handa 	u8 default_config;
55457c2590fSTetsuo Handa 	u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
555d5ca1725STetsuo Handa 	unsigned int pref[TOMOYO_MAX_PREF];
55657c2590fSTetsuo Handa };
55757c2590fSTetsuo Handa 
558eadd99ccSTetsuo Handa /* Structure for representing YYYY/MM/DD hh/mm/ss. */
559eadd99ccSTetsuo Handa struct tomoyo_time {
560eadd99ccSTetsuo Handa 	u16 year;
561eadd99ccSTetsuo Handa 	u8 month;
562eadd99ccSTetsuo Handa 	u8 day;
563eadd99ccSTetsuo Handa 	u8 hour;
564eadd99ccSTetsuo Handa 	u8 min;
565eadd99ccSTetsuo Handa 	u8 sec;
566eadd99ccSTetsuo Handa };
567eadd99ccSTetsuo Handa 
568bd03a3e4STetsuo Handa /* Structure for policy namespace. */
569bd03a3e4STetsuo Handa struct tomoyo_policy_namespace {
570bd03a3e4STetsuo Handa 	/* Profile table. Memory is allocated as needed. */
571bd03a3e4STetsuo Handa 	struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
572bd03a3e4STetsuo Handa 	/* List of "struct tomoyo_group". */
573bd03a3e4STetsuo Handa 	struct list_head group_list[TOMOYO_MAX_GROUP];
574bd03a3e4STetsuo Handa 	/* List of policy. */
575bd03a3e4STetsuo Handa 	struct list_head policy_list[TOMOYO_MAX_POLICY];
576bd03a3e4STetsuo Handa 	/* The global ACL referred by "use_group" keyword. */
577bd03a3e4STetsuo Handa 	struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
578bd03a3e4STetsuo Handa 	/* List for connecting to tomoyo_namespace_list list. */
579bd03a3e4STetsuo Handa 	struct list_head namespace_list;
580bd03a3e4STetsuo Handa 	/* Profile version. Currently only 20100903 is defined. */
581bd03a3e4STetsuo Handa 	unsigned int profile_version;
582bd03a3e4STetsuo Handa 	/* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
583bd03a3e4STetsuo Handa 	const char *name;
584bd03a3e4STetsuo Handa };
585bd03a3e4STetsuo Handa 
58676bb0895STetsuo Handa /********** Function prototypes. **********/
58776bb0895STetsuo Handa 
5882106ccd9STetsuo Handa bool tomoyo_compare_number_union(const unsigned long value,
5892106ccd9STetsuo Handa 				 const struct tomoyo_number_union *ptr);
59075093152STetsuo Handa bool tomoyo_correct_domain(const unsigned char *domainname);
59175093152STetsuo Handa bool tomoyo_correct_path(const char *filename);
59275093152STetsuo Handa bool tomoyo_correct_word(const char *string);
59375093152STetsuo Handa bool tomoyo_domain_def(const unsigned char *buffer);
5943ddf17f0STetsuo Handa bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
5953ddf17f0STetsuo Handa bool tomoyo_memory_ok(void *ptr);
5964c3e9e2dSTetsuo Handa bool tomoyo_number_matches_group(const unsigned long min,
5974c3e9e2dSTetsuo Handa 				 const unsigned long max,
598a98aa4deSTetsuo Handa 				 const struct tomoyo_group *group);
5993ddf17f0STetsuo Handa bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
6003ddf17f0STetsuo Handa 			     struct tomoyo_name_union *ptr);
601a238cf5bSTetsuo Handa bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
602a238cf5bSTetsuo Handa 			       struct tomoyo_number_union *ptr);
6033ddf17f0STetsuo Handa bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
6043ddf17f0STetsuo Handa 				 const struct tomoyo_path_info *pattern);
6053ddf17f0STetsuo Handa bool tomoyo_permstr(const char *string, const char *keyword);
6063ddf17f0STetsuo Handa bool tomoyo_str_starts(char **src, const char *find);
6073ddf17f0STetsuo Handa char *tomoyo_encode(const char *str);
6083ddf17f0STetsuo Handa char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
6093ddf17f0STetsuo Handa 		      va_list args);
6103ddf17f0STetsuo Handa char *tomoyo_read_token(struct tomoyo_acl_param *param);
6113ddf17f0STetsuo Handa char *tomoyo_realpath_from_path(struct path *path);
6123ddf17f0STetsuo Handa char *tomoyo_realpath_nofollow(const char *pathname);
6133ddf17f0STetsuo Handa const char *tomoyo_get_exe(void);
6143ddf17f0STetsuo Handa const char *tomoyo_yesno(const unsigned int value);
6153ddf17f0STetsuo Handa const struct tomoyo_path_info *tomoyo_compare_name_union
6163ddf17f0STetsuo Handa (const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
6173ddf17f0STetsuo Handa const struct tomoyo_path_info *tomoyo_get_name(const char *name);
6183ddf17f0STetsuo Handa const struct tomoyo_path_info *tomoyo_path_matches_group
6193ddf17f0STetsuo Handa (const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
6203ddf17f0STetsuo Handa int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
6213ddf17f0STetsuo Handa 				 struct path *path, const int flag);
6223ddf17f0STetsuo Handa int tomoyo_close_control(struct tomoyo_io_buffer *head);
6233ddf17f0STetsuo Handa int tomoyo_find_next_domain(struct linux_binprm *bprm);
6243ddf17f0STetsuo Handa int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
6253ddf17f0STetsuo Handa 		    const u8 index);
6262106ccd9STetsuo Handa int tomoyo_init_request_info(struct tomoyo_request_info *r,
62757c2590fSTetsuo Handa 			     struct tomoyo_domain_info *domain,
62857c2590fSTetsuo Handa 			     const u8 index);
6293ddf17f0STetsuo Handa int tomoyo_mkdev_perm(const u8 operation, struct path *path,
6303ddf17f0STetsuo Handa 		      const unsigned int mode, unsigned int dev);
631b5bc60b4STetsuo Handa int tomoyo_mount_permission(char *dev_name, struct path *path,
632b5bc60b4STetsuo Handa 			    const char *type, unsigned long flags,
633b5bc60b4STetsuo Handa 			    void *data_page);
6343ddf17f0STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file);
63597d6931eSTetsuo Handa int tomoyo_path2_perm(const u8 operation, struct path *path1,
63697d6931eSTetsuo Handa 		      struct path *path2);
6373ddf17f0STetsuo Handa int tomoyo_path_number_perm(const u8 operation, struct path *path,
6383ddf17f0STetsuo Handa 			    unsigned long number);
6393ddf17f0STetsuo Handa int tomoyo_path_perm(const u8 operation, struct path *path);
6403ddf17f0STetsuo Handa int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
6413ddf17f0STetsuo Handa 			   const struct tomoyo_path_info *filename);
6423ddf17f0STetsuo Handa int tomoyo_poll_control(struct file *file, poll_table *wait);
6433ddf17f0STetsuo Handa int tomoyo_poll_log(struct file *file, poll_table *wait);
6443ddf17f0STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
6453ddf17f0STetsuo Handa 	__printf(2, 3);
646237ab459STetsuo Handa int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
647a238cf5bSTetsuo Handa 			 struct tomoyo_acl_param *param,
6483ddf17f0STetsuo Handa 			 bool (*check_duplicate)
6493ddf17f0STetsuo Handa 			 (const struct tomoyo_acl_info *,
6503ddf17f0STetsuo Handa 			  const struct tomoyo_acl_info *),
6513ddf17f0STetsuo Handa 			 bool (*merge_duplicate)
6523ddf17f0STetsuo Handa 			 (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
653237ab459STetsuo Handa 			  const bool));
65436f5e1ffSTetsuo Handa int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
655a238cf5bSTetsuo Handa 			 struct tomoyo_acl_param *param,
6563ddf17f0STetsuo Handa 			 bool (*check_duplicate)
6573ddf17f0STetsuo Handa 			 (const struct tomoyo_acl_head *,
6583ddf17f0STetsuo Handa 			  const struct tomoyo_acl_head *));
6593ddf17f0STetsuo Handa int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
6603ddf17f0STetsuo Handa int tomoyo_write_file(struct tomoyo_acl_param *param);
6613ddf17f0STetsuo Handa int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
6623ddf17f0STetsuo Handa int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
6633ddf17f0STetsuo Handa 				    const u8 type);
6643ddf17f0STetsuo Handa ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
6653ddf17f0STetsuo Handa 			    const int buffer_len);
6663ddf17f0STetsuo Handa ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
6673ddf17f0STetsuo Handa 			     const char __user *buffer, const int buffer_len);
6683ddf17f0STetsuo Handa struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
6693ddf17f0STetsuo Handa 						const bool transit);
6703ddf17f0STetsuo Handa struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
6713ddf17f0STetsuo Handa struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
6723ddf17f0STetsuo Handa 				      const u8 idx);
6733ddf17f0STetsuo Handa struct tomoyo_policy_namespace *tomoyo_assign_namespace
6743ddf17f0STetsuo Handa (const char *domainname);
6753ddf17f0STetsuo Handa struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
6763ddf17f0STetsuo Handa 				      const u8 profile);
6773ddf17f0STetsuo Handa unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
6783ddf17f0STetsuo Handa 				const u8 index);
6793ddf17f0STetsuo Handa void *tomoyo_commit_ok(void *data, const unsigned int size);
6803ddf17f0STetsuo Handa void __init tomoyo_load_builtin_policy(void);
6813ddf17f0STetsuo Handa void __init tomoyo_mm_init(void);
68299a85259STetsuo Handa void tomoyo_check_acl(struct tomoyo_request_info *r,
683484ca79cSTetsuo Handa 		      bool (*check_entry) (struct tomoyo_request_info *,
68499a85259STetsuo Handa 					   const struct tomoyo_acl_info *));
6853ddf17f0STetsuo Handa void tomoyo_check_profile(void);
6863ddf17f0STetsuo Handa void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp);
6873ddf17f0STetsuo Handa void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
6883ddf17f0STetsuo Handa void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
6893ddf17f0STetsuo Handa void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
6903ddf17f0STetsuo Handa 	 __printf(2, 3);
6913ddf17f0STetsuo Handa void tomoyo_load_policy(const char *filename);
6923ddf17f0STetsuo Handa void tomoyo_memory_free(void *ptr);
6933ddf17f0STetsuo Handa void tomoyo_normalize_line(unsigned char *buffer);
6943ddf17f0STetsuo Handa void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
6953ddf17f0STetsuo Handa void tomoyo_print_ulong(char *buffer, const int buffer_len,
6963ddf17f0STetsuo Handa 			const unsigned long value, const u8 type);
6973ddf17f0STetsuo Handa void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
6983ddf17f0STetsuo Handa void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
699eadd99ccSTetsuo Handa void tomoyo_read_log(struct tomoyo_io_buffer *head);
7003ddf17f0STetsuo Handa void tomoyo_update_stat(const u8 index);
7013ddf17f0STetsuo Handa void tomoyo_warn_oom(const char *function);
7023ddf17f0STetsuo Handa void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
7033ddf17f0STetsuo Handa 	__printf(2, 3);
7043ddf17f0STetsuo Handa void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
705eadd99ccSTetsuo Handa 		       va_list args);
706eadd99ccSTetsuo Handa 
70776bb0895STetsuo Handa /********** External variable definitions. **********/
70876bb0895STetsuo Handa 
70976bb0895STetsuo Handa extern bool tomoyo_policy_loaded;
7103ddf17f0STetsuo Handa extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
7113ddf17f0STetsuo Handa extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
7123ddf17f0STetsuo Handa 					      + TOMOYO_MAX_MAC_CATEGORY_INDEX];
7133ddf17f0STetsuo Handa extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
7142c47ab93STetsuo Handa extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
7152c47ab93STetsuo Handa extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
7163ddf17f0STetsuo Handa extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
7170d2171d7STetsuo Handa extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
7180d2171d7STetsuo Handa extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
7193ddf17f0STetsuo Handa extern struct list_head tomoyo_domain_list;
7203ddf17f0STetsuo Handa extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
7213ddf17f0STetsuo Handa extern struct list_head tomoyo_namespace_list;
7223ddf17f0STetsuo Handa extern struct mutex tomoyo_policy_lock;
7233ddf17f0STetsuo Handa extern struct srcu_struct tomoyo_ss;
7243ddf17f0STetsuo Handa extern struct tomoyo_domain_info tomoyo_kernel_domain;
7253ddf17f0STetsuo Handa extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
726eadd99ccSTetsuo Handa extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
727eadd99ccSTetsuo Handa extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
72817fcfbd9STetsuo Handa 
72976bb0895STetsuo Handa /********** Inlined functions. **********/
73076bb0895STetsuo Handa 
731b5bc60b4STetsuo Handa /**
732b5bc60b4STetsuo Handa  * tomoyo_read_lock - Take lock for protecting policy.
733b5bc60b4STetsuo Handa  *
734b5bc60b4STetsuo Handa  * Returns index number for tomoyo_read_unlock().
735b5bc60b4STetsuo Handa  */
73676bb0895STetsuo Handa static inline int tomoyo_read_lock(void)
73776bb0895STetsuo Handa {
73876bb0895STetsuo Handa 	return srcu_read_lock(&tomoyo_ss);
73976bb0895STetsuo Handa }
74076bb0895STetsuo Handa 
741b5bc60b4STetsuo Handa /**
742b5bc60b4STetsuo Handa  * tomoyo_read_unlock - Release lock for protecting policy.
743b5bc60b4STetsuo Handa  *
744b5bc60b4STetsuo Handa  * @idx: Index number returned by tomoyo_read_lock().
745b5bc60b4STetsuo Handa  *
746b5bc60b4STetsuo Handa  * Returns nothing.
747b5bc60b4STetsuo Handa  */
74876bb0895STetsuo Handa static inline void tomoyo_read_unlock(int idx)
74976bb0895STetsuo Handa {
75076bb0895STetsuo Handa 	srcu_read_unlock(&tomoyo_ss, idx);
75176bb0895STetsuo Handa }
75276bb0895STetsuo Handa 
753b5bc60b4STetsuo Handa /**
754b5bc60b4STetsuo Handa  * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
755b5bc60b4STetsuo Handa  *
756b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_path_info".
757b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_path_info".
758b5bc60b4STetsuo Handa  *
759b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
760b5bc60b4STetsuo Handa  */
7619590837bSKentaro Takeda static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
7629590837bSKentaro Takeda 				  const struct tomoyo_path_info *b)
7639590837bSKentaro Takeda {
7649590837bSKentaro Takeda 	return a->hash != b->hash || strcmp(a->name, b->name);
7659590837bSKentaro Takeda }
7669590837bSKentaro Takeda 
7679590837bSKentaro Takeda /**
768b5bc60b4STetsuo Handa  * tomoyo_put_name - Drop reference on "struct tomoyo_name".
769b5bc60b4STetsuo Handa  *
770b5bc60b4STetsuo Handa  * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
771b5bc60b4STetsuo Handa  *
772b5bc60b4STetsuo Handa  * Returns nothing.
773b5bc60b4STetsuo Handa  */
77476bb0895STetsuo Handa static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
77576bb0895STetsuo Handa {
77676bb0895STetsuo Handa 	if (name) {
777e2bf6907STetsuo Handa 		struct tomoyo_name *ptr =
778e2bf6907STetsuo Handa 			container_of(name, typeof(*ptr), entry);
7790df7e8b8STetsuo Handa 		atomic_dec(&ptr->head.users);
78076bb0895STetsuo Handa 	}
78176bb0895STetsuo Handa }
7829590837bSKentaro Takeda 
783b5bc60b4STetsuo Handa /**
784b5bc60b4STetsuo Handa  * tomoyo_put_group - Drop reference on "struct tomoyo_group".
785b5bc60b4STetsuo Handa  *
786b5bc60b4STetsuo Handa  * @group: Pointer to "struct tomoyo_group". Maybe NULL.
787b5bc60b4STetsuo Handa  *
788b5bc60b4STetsuo Handa  * Returns nothing.
789b5bc60b4STetsuo Handa  */
790a98aa4deSTetsuo Handa static inline void tomoyo_put_group(struct tomoyo_group *group)
7914c3e9e2dSTetsuo Handa {
7924c3e9e2dSTetsuo Handa 	if (group)
7930df7e8b8STetsuo Handa 		atomic_dec(&group->head.users);
7944c3e9e2dSTetsuo Handa }
7954c3e9e2dSTetsuo Handa 
796b5bc60b4STetsuo Handa /**
797b5bc60b4STetsuo Handa  * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
798b5bc60b4STetsuo Handa  *
799b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" for current thread.
800b5bc60b4STetsuo Handa  */
80176bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_domain(void)
80276bb0895STetsuo Handa {
80376bb0895STetsuo Handa 	return current_cred()->security;
80476bb0895STetsuo Handa }
8059590837bSKentaro Takeda 
806b5bc60b4STetsuo Handa /**
807b5bc60b4STetsuo Handa  * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
808b5bc60b4STetsuo Handa  *
809b5bc60b4STetsuo Handa  * @task: Pointer to "struct task_struct".
810b5bc60b4STetsuo Handa  *
811b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_security" for specified thread.
812b5bc60b4STetsuo Handa  */
81376bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
81476bb0895STetsuo Handa 							    *task)
81576bb0895STetsuo Handa {
81676bb0895STetsuo Handa 	return task_cred_xxx(task, security);
81776bb0895STetsuo Handa }
8189590837bSKentaro Takeda 
819b5bc60b4STetsuo Handa /**
820b5bc60b4STetsuo Handa  * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
821b5bc60b4STetsuo Handa  *
822b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_name_union".
823b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_name_union".
824b5bc60b4STetsuo Handa  *
825b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
826b5bc60b4STetsuo Handa  */
82775093152STetsuo Handa static inline bool tomoyo_same_name_union
828b5bc60b4STetsuo Handa (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
8297762fbffSTetsuo Handa {
8300df7e8b8STetsuo Handa 	return a->filename == b->filename && a->group == b->group;
8317762fbffSTetsuo Handa }
8327762fbffSTetsuo Handa 
833b5bc60b4STetsuo Handa /**
834b5bc60b4STetsuo Handa  * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
835b5bc60b4STetsuo Handa  *
836b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_number_union".
837b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_number_union".
838b5bc60b4STetsuo Handa  *
839b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
840b5bc60b4STetsuo Handa  */
84175093152STetsuo Handa static inline bool tomoyo_same_number_union
842b5bc60b4STetsuo Handa (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
8434c3e9e2dSTetsuo Handa {
844b5bc60b4STetsuo Handa 	return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
8450df7e8b8STetsuo Handa 		a->group == b->group && a->value_type[0] == b->value_type[0] &&
8460df7e8b8STetsuo Handa 		a->value_type[1] == b->value_type[1];
8474c3e9e2dSTetsuo Handa }
8484c3e9e2dSTetsuo Handa 
849bd03a3e4STetsuo Handa /**
850bd03a3e4STetsuo Handa  * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
851bd03a3e4STetsuo Handa  *
852bd03a3e4STetsuo Handa  * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
853bd03a3e4STetsuo Handa  */
854bd03a3e4STetsuo Handa static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
855bd03a3e4STetsuo Handa {
856bd03a3e4STetsuo Handa 	return tomoyo_domain()->ns;
857bd03a3e4STetsuo Handa }
858bd03a3e4STetsuo Handa 
859eadd99ccSTetsuo Handa #if defined(CONFIG_SLOB)
860eadd99ccSTetsuo Handa 
861eadd99ccSTetsuo Handa /**
862eadd99ccSTetsuo Handa  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
863eadd99ccSTetsuo Handa  *
864eadd99ccSTetsuo Handa  * @size: Size to be rounded up.
865eadd99ccSTetsuo Handa  *
866eadd99ccSTetsuo Handa  * Returns @size.
867eadd99ccSTetsuo Handa  *
868eadd99ccSTetsuo Handa  * Since SLOB does not round up, this function simply returns @size.
869eadd99ccSTetsuo Handa  */
870eadd99ccSTetsuo Handa static inline int tomoyo_round2(size_t size)
871eadd99ccSTetsuo Handa {
872eadd99ccSTetsuo Handa 	return size;
873eadd99ccSTetsuo Handa }
874eadd99ccSTetsuo Handa 
875eadd99ccSTetsuo Handa #else
876eadd99ccSTetsuo Handa 
877eadd99ccSTetsuo Handa /**
878eadd99ccSTetsuo Handa  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
879eadd99ccSTetsuo Handa  *
880eadd99ccSTetsuo Handa  * @size: Size to be rounded up.
881eadd99ccSTetsuo Handa  *
882eadd99ccSTetsuo Handa  * Returns rounded size.
883eadd99ccSTetsuo Handa  *
884eadd99ccSTetsuo Handa  * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
885eadd99ccSTetsuo Handa  * (e.g.) 128 bytes.
886eadd99ccSTetsuo Handa  */
887eadd99ccSTetsuo Handa static inline int tomoyo_round2(size_t size)
888eadd99ccSTetsuo Handa {
889eadd99ccSTetsuo Handa #if PAGE_SIZE == 4096
890eadd99ccSTetsuo Handa 	size_t bsize = 32;
891eadd99ccSTetsuo Handa #else
892eadd99ccSTetsuo Handa 	size_t bsize = 64;
893eadd99ccSTetsuo Handa #endif
894eadd99ccSTetsuo Handa 	if (!size)
895eadd99ccSTetsuo Handa 		return 0;
896eadd99ccSTetsuo Handa 	while (size > bsize)
897eadd99ccSTetsuo Handa 		bsize <<= 1;
898eadd99ccSTetsuo Handa 	return bsize;
899eadd99ccSTetsuo Handa }
900eadd99ccSTetsuo Handa 
901eadd99ccSTetsuo Handa #endif
902eadd99ccSTetsuo Handa 
9039590837bSKentaro Takeda /**
9049590837bSKentaro Takeda  * list_for_each_cookie - iterate over a list with cookie.
9059590837bSKentaro Takeda  * @pos:        the &struct list_head to use as a loop cursor.
9069590837bSKentaro Takeda  * @head:       the head for your list.
9079590837bSKentaro Takeda  */
908475e6fa3STetsuo Handa #define list_for_each_cookie(pos, head)					\
909475e6fa3STetsuo Handa 	if (!pos)							\
910475e6fa3STetsuo Handa 		pos =  srcu_dereference((head)->next, &tomoyo_ss);	\
911475e6fa3STetsuo Handa 	for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
912fdb8ebb7STetsuo Handa 
9139590837bSKentaro Takeda #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
914