xref: /openbmc/linux/security/tomoyo/common.h (revision b5bc60b4)
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 
41b5bc60b4STetsuo Handa /* Index numbers for operation mode. */
42cb0abe6aSTetsuo Handa enum tomoyo_mode_index {
43cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_DISABLED,
44cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_LEARNING,
45cb0abe6aSTetsuo Handa 	TOMOYO_CONFIG_PERMISSIVE,
4657c2590fSTetsuo Handa 	TOMOYO_CONFIG_ENFORCING,
4757c2590fSTetsuo Handa 	TOMOYO_CONFIG_USE_DEFAULT = 255
48cb0abe6aSTetsuo Handa };
49cb0abe6aSTetsuo Handa 
50b5bc60b4STetsuo Handa /* Index numbers for entry type. */
51a230f9e7STetsuo Handa enum tomoyo_policy_id {
52a230f9e7STetsuo Handa 	TOMOYO_ID_GROUP,
53a230f9e7STetsuo Handa 	TOMOYO_ID_PATH_GROUP,
54a230f9e7STetsuo Handa 	TOMOYO_ID_NUMBER_GROUP,
555448ec4fSTetsuo Handa 	TOMOYO_ID_TRANSITION_CONTROL,
56a230f9e7STetsuo Handa 	TOMOYO_ID_AGGREGATOR,
57a230f9e7STetsuo Handa 	TOMOYO_ID_MANAGER,
58a230f9e7STetsuo Handa 	TOMOYO_ID_NAME,
59a230f9e7STetsuo Handa 	TOMOYO_ID_ACL,
60a230f9e7STetsuo Handa 	TOMOYO_ID_DOMAIN,
61a230f9e7STetsuo Handa 	TOMOYO_MAX_POLICY
62a230f9e7STetsuo Handa };
63a230f9e7STetsuo Handa 
64b5bc60b4STetsuo Handa /* Index numbers for group entries. */
65a230f9e7STetsuo Handa enum tomoyo_group_id {
66a230f9e7STetsuo Handa 	TOMOYO_PATH_GROUP,
67a230f9e7STetsuo Handa 	TOMOYO_NUMBER_GROUP,
68a230f9e7STetsuo Handa 	TOMOYO_MAX_GROUP
69a230f9e7STetsuo Handa };
70a230f9e7STetsuo Handa 
7176bb0895STetsuo Handa /* A domain definition starts with <kernel>. */
7276bb0895STetsuo Handa #define TOMOYO_ROOT_NAME                         "<kernel>"
7376bb0895STetsuo Handa #define TOMOYO_ROOT_NAME_LEN                     (sizeof(TOMOYO_ROOT_NAME) - 1)
7476bb0895STetsuo Handa 
75b5bc60b4STetsuo Handa /* Index numbers for type of numeric values. */
76b5bc60b4STetsuo Handa enum tomoyo_value_type {
77b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_INVALID,
78b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_DECIMAL,
79b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_OCTAL,
80b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_HEXADECIMAL,
81b5bc60b4STetsuo Handa };
824c3e9e2dSTetsuo Handa 
83b5bc60b4STetsuo Handa /* Index numbers for domain transition control keywords. */
845448ec4fSTetsuo Handa enum tomoyo_transition_type {
855448ec4fSTetsuo Handa 	/* Do not change this order, */
865448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
875448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_INITIALIZE,
885448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_KEEP,
895448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_KEEP,
905448ec4fSTetsuo Handa 	TOMOYO_MAX_TRANSITION_TYPE
915448ec4fSTetsuo Handa };
925448ec4fSTetsuo Handa 
9376bb0895STetsuo Handa /* Index numbers for Access Controls. */
94084da356STetsuo Handa enum tomoyo_acl_entry_type_index {
957ef61233STetsuo Handa 	TOMOYO_TYPE_PATH_ACL,
967ef61233STetsuo Handa 	TOMOYO_TYPE_PATH2_ACL,
97a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_PATH_NUMBER_ACL,
9875093152STetsuo Handa 	TOMOYO_TYPE_MKDEV_ACL,
992106ccd9STetsuo Handa 	TOMOYO_TYPE_MOUNT_ACL,
100084da356STetsuo Handa };
10176bb0895STetsuo Handa 
102b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname. */
103084da356STetsuo Handa enum tomoyo_path_acl_index {
1047ef61233STetsuo Handa 	TOMOYO_TYPE_EXECUTE,
1057ef61233STetsuo Handa 	TOMOYO_TYPE_READ,
1067ef61233STetsuo Handa 	TOMOYO_TYPE_WRITE,
1077c75964fSTetsuo Handa 	TOMOYO_TYPE_APPEND,
1087ef61233STetsuo Handa 	TOMOYO_TYPE_UNLINK,
1097c75964fSTetsuo Handa 	TOMOYO_TYPE_GETATTR,
1107ef61233STetsuo Handa 	TOMOYO_TYPE_RMDIR,
1117ef61233STetsuo Handa 	TOMOYO_TYPE_TRUNCATE,
1127ef61233STetsuo Handa 	TOMOYO_TYPE_SYMLINK,
1137ef61233STetsuo Handa 	TOMOYO_TYPE_CHROOT,
1147ef61233STetsuo Handa 	TOMOYO_TYPE_UMOUNT,
1157ef61233STetsuo Handa 	TOMOYO_MAX_PATH_OPERATION
116084da356STetsuo Handa };
11776bb0895STetsuo Handa 
11875093152STetsuo Handa enum tomoyo_mkdev_acl_index {
119a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKBLOCK,
120a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKCHAR,
12175093152STetsuo Handa 	TOMOYO_MAX_MKDEV_OPERATION
122a1f9bb6aSTetsuo Handa };
123a1f9bb6aSTetsuo Handa 
124b5bc60b4STetsuo Handa /* Index numbers for access controls with two pathnames. */
125084da356STetsuo Handa enum tomoyo_path2_acl_index {
1267ef61233STetsuo Handa 	TOMOYO_TYPE_LINK,
1277ef61233STetsuo Handa 	TOMOYO_TYPE_RENAME,
1287ef61233STetsuo Handa 	TOMOYO_TYPE_PIVOT_ROOT,
1297ef61233STetsuo Handa 	TOMOYO_MAX_PATH2_OPERATION
130084da356STetsuo Handa };
13176bb0895STetsuo Handa 
132b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname and one number. */
133a1f9bb6aSTetsuo Handa enum tomoyo_path_number_acl_index {
134a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CREATE,
135a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKDIR,
136a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKFIFO,
137a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKSOCK,
138a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_IOCTL,
139a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHMOD,
140a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHOWN,
141a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHGRP,
142a1f9bb6aSTetsuo Handa 	TOMOYO_MAX_PATH_NUMBER_OPERATION
143a1f9bb6aSTetsuo Handa };
144a1f9bb6aSTetsuo Handa 
145b5bc60b4STetsuo Handa /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
146084da356STetsuo Handa enum tomoyo_securityfs_interface_index {
147084da356STetsuo Handa 	TOMOYO_DOMAINPOLICY,
148084da356STetsuo Handa 	TOMOYO_EXCEPTIONPOLICY,
149084da356STetsuo Handa 	TOMOYO_DOMAIN_STATUS,
150084da356STetsuo Handa 	TOMOYO_PROCESS_STATUS,
151084da356STetsuo Handa 	TOMOYO_MEMINFO,
152084da356STetsuo Handa 	TOMOYO_SELFDOMAIN,
153084da356STetsuo Handa 	TOMOYO_VERSION,
154084da356STetsuo Handa 	TOMOYO_PROFILE,
15517fcfbd9STetsuo Handa 	TOMOYO_QUERY,
156084da356STetsuo Handa 	TOMOYO_MANAGER
157084da356STetsuo Handa };
15876bb0895STetsuo Handa 
159b5bc60b4STetsuo Handa /* Index numbers for special mount operations. */
160b5bc60b4STetsuo Handa enum tomoyo_special_mount {
161b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_BIND,            /* mount --bind /source /dest   */
162b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MOVE,            /* mount --move /old /new       */
163b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_REMOUNT,         /* mount -o remount /dir        */
164b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
165b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_PRIVATE,    /* mount --make-private /dir    */
166b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SLAVE,      /* mount --make-slave /dir      */
167b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SHARED,     /* mount --make-shared /dir     */
168b5bc60b4STetsuo Handa 	TOMOYO_MAX_SPECIAL_MOUNT
169b5bc60b4STetsuo Handa };
170b5bc60b4STetsuo Handa 
171b5bc60b4STetsuo Handa /* Index numbers for functionality. */
17257c2590fSTetsuo Handa enum tomoyo_mac_index {
17357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_EXECUTE,
17457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_OPEN,
17557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CREATE,
17657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UNLINK,
1777c75964fSTetsuo Handa 	TOMOYO_MAC_FILE_GETATTR,
17857c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKDIR,
17957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RMDIR,
18057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKFIFO,
18157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKSOCK,
18257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_TRUNCATE,
18357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_SYMLINK,
18457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKBLOCK,
18557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKCHAR,
18657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_LINK,
18757c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RENAME,
18857c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHMOD,
18957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHOWN,
19057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHGRP,
19157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_IOCTL,
19257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHROOT,
19357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MOUNT,
19457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UMOUNT,
19557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_PIVOT_ROOT,
19657c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_INDEX
19757c2590fSTetsuo Handa };
19857c2590fSTetsuo Handa 
199b5bc60b4STetsuo Handa /* Index numbers for category of functionality. */
20057c2590fSTetsuo Handa enum tomoyo_mac_category_index {
20157c2590fSTetsuo Handa 	TOMOYO_MAC_CATEGORY_FILE,
20257c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_CATEGORY_INDEX
20357c2590fSTetsuo Handa };
20457c2590fSTetsuo Handa 
205b5bc60b4STetsuo Handa /*
206b5bc60b4STetsuo Handa  * Retry this request. Returned by tomoyo_supervisor() if policy violation has
207b5bc60b4STetsuo Handa  * occurred in enforcing mode and the userspace daemon decided to retry.
208b5bc60b4STetsuo Handa  *
209b5bc60b4STetsuo Handa  * We must choose a positive value in order to distinguish "granted" (which is
210b5bc60b4STetsuo Handa  * 0) and "rejected" (which is a negative value) and "retry".
211b5bc60b4STetsuo Handa  */
212b5bc60b4STetsuo Handa #define TOMOYO_RETRY_REQUEST 1
21317fcfbd9STetsuo Handa 
21476bb0895STetsuo Handa /********** Structure definitions. **********/
2159590837bSKentaro Takeda 
216b5bc60b4STetsuo Handa /* Common header for holding ACL entries. */
21782e0f001STetsuo Handa struct tomoyo_acl_head {
21882e0f001STetsuo Handa 	struct list_head list;
21982e0f001STetsuo Handa 	bool is_deleted;
22082e0f001STetsuo Handa } __packed;
22182e0f001STetsuo Handa 
222b5bc60b4STetsuo Handa /* Structure for request info. */
223cb0abe6aSTetsuo Handa struct tomoyo_request_info {
224cb0abe6aSTetsuo Handa 	struct tomoyo_domain_info *domain;
225cf6e9a64STetsuo Handa 	/* For holding parameters. */
226cf6e9a64STetsuo Handa 	union {
227cf6e9a64STetsuo Handa 		struct {
228cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
229484ca79cSTetsuo Handa 			/* For using wildcards at tomoyo_find_next_domain(). */
230484ca79cSTetsuo Handa 			const struct tomoyo_path_info *matched_path;
231b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path_acl_index". */
232cf6e9a64STetsuo Handa 			u8 operation;
233cf6e9a64STetsuo Handa 		} path;
234cf6e9a64STetsuo Handa 		struct {
235cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename1;
236cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename2;
237b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path2_acl_index". */
238cf6e9a64STetsuo Handa 			u8 operation;
239cf6e9a64STetsuo Handa 		} path2;
240cf6e9a64STetsuo Handa 		struct {
241cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
242cf6e9a64STetsuo Handa 			unsigned int mode;
243cf6e9a64STetsuo Handa 			unsigned int major;
244cf6e9a64STetsuo Handa 			unsigned int minor;
245b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_mkdev_acl_index". */
246cf6e9a64STetsuo Handa 			u8 operation;
247cf6e9a64STetsuo Handa 		} mkdev;
248cf6e9a64STetsuo Handa 		struct {
249cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
250cf6e9a64STetsuo Handa 			unsigned long number;
251b5bc60b4STetsuo Handa 			/*
252b5bc60b4STetsuo Handa 			 * One of values in
253b5bc60b4STetsuo Handa 			 * "enum tomoyo_path_number_acl_index".
254b5bc60b4STetsuo Handa 			 */
255cf6e9a64STetsuo Handa 			u8 operation;
256cf6e9a64STetsuo Handa 		} path_number;
257cf6e9a64STetsuo Handa 		struct {
258cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *type;
259cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dir;
260cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dev;
261cf6e9a64STetsuo Handa 			unsigned long flags;
262cf6e9a64STetsuo Handa 			int need_dev;
263cf6e9a64STetsuo Handa 		} mount;
264cf6e9a64STetsuo Handa 	} param;
265cf6e9a64STetsuo Handa 	u8 param_type;
266cf6e9a64STetsuo Handa 	bool granted;
26717fcfbd9STetsuo Handa 	u8 retry;
26817fcfbd9STetsuo Handa 	u8 profile;
269cb0abe6aSTetsuo Handa 	u8 mode; /* One of tomoyo_mode_index . */
27057c2590fSTetsuo Handa 	u8 type;
271cb0abe6aSTetsuo Handa };
272cb0abe6aSTetsuo Handa 
273b5bc60b4STetsuo Handa /* Structure for holding a token. */
2749590837bSKentaro Takeda struct tomoyo_path_info {
2759590837bSKentaro Takeda 	const char *name;
2769590837bSKentaro Takeda 	u32 hash;          /* = full_name_hash(name, strlen(name)) */
2779590837bSKentaro Takeda 	u16 const_len;     /* = tomoyo_const_part_length(name)     */
2789590837bSKentaro Takeda 	bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
2799590837bSKentaro Takeda 	bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
2809590837bSKentaro Takeda };
2819590837bSKentaro Takeda 
282b5bc60b4STetsuo Handa /* Structure for holding string data. */
283e2bf6907STetsuo Handa struct tomoyo_name {
28476bb0895STetsuo Handa 	struct list_head list;
28576bb0895STetsuo Handa 	atomic_t users;
28676bb0895STetsuo Handa 	struct tomoyo_path_info entry;
28776bb0895STetsuo Handa };
2889590837bSKentaro Takeda 
289b5bc60b4STetsuo Handa /* Structure for holding a word. */
2907762fbffSTetsuo Handa struct tomoyo_name_union {
291b5bc60b4STetsuo Handa 	/* Either @filename or @group is NULL. */
2927762fbffSTetsuo Handa 	const struct tomoyo_path_info *filename;
293a98aa4deSTetsuo Handa 	struct tomoyo_group *group;
294b5bc60b4STetsuo Handa 	/* True if @group != NULL, false if @filename != NULL. */
2957762fbffSTetsuo Handa 	u8 is_group;
2967762fbffSTetsuo Handa };
2977762fbffSTetsuo Handa 
298b5bc60b4STetsuo Handa /* Structure for holding a number. */
2994c3e9e2dSTetsuo Handa struct tomoyo_number_union {
3004c3e9e2dSTetsuo Handa 	unsigned long values[2];
301b5bc60b4STetsuo Handa 	struct tomoyo_group *group; /* Maybe NULL. */
302b5bc60b4STetsuo Handa 	/* One of values in "enum tomoyo_value_type". */
3034c3e9e2dSTetsuo Handa 	u8 min_type;
3044c3e9e2dSTetsuo Handa 	u8 max_type;
305b5bc60b4STetsuo Handa 	/* True if @group != NULL, false otherwise. */
3064c3e9e2dSTetsuo Handa 	u8 is_group;
3074c3e9e2dSTetsuo Handa };
3084c3e9e2dSTetsuo Handa 
309a98aa4deSTetsuo Handa /* Structure for "path_group"/"number_group" directive. */
310a98aa4deSTetsuo Handa struct tomoyo_group {
311a98aa4deSTetsuo Handa 	struct list_head list;
312a98aa4deSTetsuo Handa 	const struct tomoyo_path_info *group_name;
313a98aa4deSTetsuo Handa 	struct list_head member_list;
314a98aa4deSTetsuo Handa 	atomic_t users;
315a98aa4deSTetsuo Handa };
316a98aa4deSTetsuo Handa 
3177762fbffSTetsuo Handa /* Structure for "path_group" directive. */
3187762fbffSTetsuo Handa struct tomoyo_path_group {
31982e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3207762fbffSTetsuo Handa 	const struct tomoyo_path_info *member_name;
3217762fbffSTetsuo Handa };
3227762fbffSTetsuo Handa 
3234c3e9e2dSTetsuo Handa /* Structure for "number_group" directive. */
324a98aa4deSTetsuo Handa struct tomoyo_number_group {
32582e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3264c3e9e2dSTetsuo Handa 	struct tomoyo_number_union number;
3274c3e9e2dSTetsuo Handa };
3284c3e9e2dSTetsuo Handa 
329b5bc60b4STetsuo Handa /* Common header for individual entries. */
3309590837bSKentaro Takeda struct tomoyo_acl_info {
3319590837bSKentaro Takeda 	struct list_head list;
332237ab459STetsuo Handa 	bool is_deleted;
333b5bc60b4STetsuo Handa 	u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
3349590837bSKentaro Takeda } __packed;
3359590837bSKentaro Takeda 
336b5bc60b4STetsuo Handa /* Structure for domain information. */
3379590837bSKentaro Takeda struct tomoyo_domain_info {
3389590837bSKentaro Takeda 	struct list_head list;
3399590837bSKentaro Takeda 	struct list_head acl_info_list;
3409590837bSKentaro Takeda 	/* Name of this domain. Never NULL.          */
3419590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname;
3429590837bSKentaro Takeda 	u8 profile;        /* Profile number to use. */
343a0558fc3STetsuo Handa 	bool is_deleted;   /* Delete flag.           */
3449590837bSKentaro Takeda 	bool quota_warned; /* Quota warnning flag.   */
345ea13ddbaSTetsuo Handa 	bool transition_failed; /* Domain transition failed flag. */
346ec8e6a4eSTetsuo Handa 	atomic_t users; /* Number of referring credentials. */
3479590837bSKentaro Takeda };
3489590837bSKentaro Takeda 
3499590837bSKentaro Takeda /*
350b5bc60b4STetsuo Handa  * Structure for "file execute", "file read", "file write", "file append",
351b5bc60b4STetsuo Handa  * "file unlink", "file getattr", "file rmdir", "file truncate",
352b5bc60b4STetsuo Handa  * "file symlink", "file chroot" and "file unmount" directive.
3539590837bSKentaro Takeda  */
3547ef61233STetsuo Handa struct tomoyo_path_acl {
3557ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
356b5bc60b4STetsuo Handa 	u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
3577762fbffSTetsuo Handa 	struct tomoyo_name_union name;
3589590837bSKentaro Takeda };
3599590837bSKentaro Takeda 
360c3fa109aSTetsuo Handa /*
361b5bc60b4STetsuo Handa  * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
362b5bc60b4STetsuo Handa  * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
363a1f9bb6aSTetsuo Handa  */
364a1f9bb6aSTetsuo Handa struct tomoyo_path_number_acl {
365a1f9bb6aSTetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
366b5bc60b4STetsuo Handa 	/* Bitmask of values in "enum tomoyo_path_number_acl_index". */
367a1f9bb6aSTetsuo Handa 	u8 perm;
368a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
369a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union number;
370a1f9bb6aSTetsuo Handa };
371a1f9bb6aSTetsuo Handa 
372b5bc60b4STetsuo Handa /* Structure for "file mkblock" and "file mkchar" directive. */
37375093152STetsuo Handa struct tomoyo_mkdev_acl {
37475093152STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
375b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
376a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
377a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union mode;
378a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union major;
379a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union minor;
380a1f9bb6aSTetsuo Handa };
381a1f9bb6aSTetsuo Handa 
382a1f9bb6aSTetsuo Handa /*
383b5bc60b4STetsuo Handa  * Structure for "file rename", "file link" and "file pivot_root" directive.
384c3fa109aSTetsuo Handa  */
3857ef61233STetsuo Handa struct tomoyo_path2_acl {
3867ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
387b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
3887762fbffSTetsuo Handa 	struct tomoyo_name_union name1;
3897762fbffSTetsuo Handa 	struct tomoyo_name_union name2;
3909590837bSKentaro Takeda };
3919590837bSKentaro Takeda 
392b5bc60b4STetsuo Handa /* Structure for "file mount" directive. */
3932106ccd9STetsuo Handa struct tomoyo_mount_acl {
3942106ccd9STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
3952106ccd9STetsuo Handa 	struct tomoyo_name_union dev_name;
3962106ccd9STetsuo Handa 	struct tomoyo_name_union dir_name;
3972106ccd9STetsuo Handa 	struct tomoyo_name_union fs_type;
3982106ccd9STetsuo Handa 	struct tomoyo_number_union flags;
3992106ccd9STetsuo Handa };
4002106ccd9STetsuo Handa 
401f23571e8STetsuo Handa #define TOMOYO_MAX_IO_READ_QUEUE 32
402f23571e8STetsuo Handa 
4032106ccd9STetsuo Handa /*
404f23571e8STetsuo Handa  * Structure for reading/writing policy via /sys/kernel/security/tomoyo
405f23571e8STetsuo Handa  * interfaces.
406c3fa109aSTetsuo Handa  */
4079590837bSKentaro Takeda struct tomoyo_io_buffer {
4088fbe71f0STetsuo Handa 	void (*read) (struct tomoyo_io_buffer *);
4099590837bSKentaro Takeda 	int (*write) (struct tomoyo_io_buffer *);
41017fcfbd9STetsuo Handa 	int (*poll) (struct file *file, poll_table *wait);
4119590837bSKentaro Takeda 	/* Exclusive lock for this structure.   */
4129590837bSKentaro Takeda 	struct mutex io_sem;
413fdb8ebb7STetsuo Handa 	/* Index returned by tomoyo_read_lock(). */
414fdb8ebb7STetsuo Handa 	int reader_idx;
415f23571e8STetsuo Handa 	char __user *read_user_buf;
416f23571e8STetsuo Handa 	int read_user_buf_avail;
417f23571e8STetsuo Handa 	struct {
418f23571e8STetsuo Handa 		struct list_head *domain;
419f23571e8STetsuo Handa 		struct list_head *group;
420f23571e8STetsuo Handa 		struct list_head *acl;
421f23571e8STetsuo Handa 		int avail;
422f23571e8STetsuo Handa 		int step;
423f23571e8STetsuo Handa 		int query_index;
424f23571e8STetsuo Handa 		u16 index;
425f23571e8STetsuo Handa 		u8 bit;
426f23571e8STetsuo Handa 		u8 w_pos;
427f23571e8STetsuo Handa 		bool eof;
428f23571e8STetsuo Handa 		bool print_this_domain_only;
429f23571e8STetsuo Handa 		bool print_execute_only;
430f23571e8STetsuo Handa 		const char *w[TOMOYO_MAX_IO_READ_QUEUE];
431f23571e8STetsuo Handa 	} r;
4329590837bSKentaro Takeda 	/* The position currently writing to.   */
4339590837bSKentaro Takeda 	struct tomoyo_domain_info *write_var1;
4349590837bSKentaro Takeda 	/* Buffer for reading.                  */
4359590837bSKentaro Takeda 	char *read_buf;
4369590837bSKentaro Takeda 	/* Size of read buffer.                 */
4379590837bSKentaro Takeda 	int readbuf_size;
4389590837bSKentaro Takeda 	/* Buffer for writing.                  */
4399590837bSKentaro Takeda 	char *write_buf;
4409590837bSKentaro Takeda 	/* Bytes available for writing.         */
4419590837bSKentaro Takeda 	int write_avail;
4429590837bSKentaro Takeda 	/* Size of write buffer.                */
4439590837bSKentaro Takeda 	int writebuf_size;
44417fcfbd9STetsuo Handa 	/* Type of this interface.              */
44517fcfbd9STetsuo Handa 	u8 type;
4469590837bSKentaro Takeda };
4479590837bSKentaro Takeda 
44876bb0895STetsuo Handa /*
449b5bc60b4STetsuo Handa  * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
450b5bc60b4STetsuo Handa  * "no_keep_domain" keyword.
45176bb0895STetsuo Handa  */
4525448ec4fSTetsuo Handa struct tomoyo_transition_control {
45382e0f001STetsuo Handa 	struct tomoyo_acl_head head;
4545448ec4fSTetsuo Handa 	u8 type; /* One of values in "enum tomoyo_transition_type".  */
45576bb0895STetsuo Handa 	/* True if the domainname is tomoyo_get_last_name(). */
45676bb0895STetsuo Handa 	bool is_last_name;
4575448ec4fSTetsuo Handa 	const struct tomoyo_path_info *domainname; /* Maybe NULL */
4585448ec4fSTetsuo Handa 	const struct tomoyo_path_info *program;    /* Maybe NULL */
45976bb0895STetsuo Handa };
46076bb0895STetsuo Handa 
461b5bc60b4STetsuo Handa /* Structure for "aggregator" keyword. */
462e2bf6907STetsuo Handa struct tomoyo_aggregator {
46382e0f001STetsuo Handa 	struct tomoyo_acl_head head;
4641084307cSTetsuo Handa 	const struct tomoyo_path_info *original_name;
4651084307cSTetsuo Handa 	const struct tomoyo_path_info *aggregated_name;
4661084307cSTetsuo Handa };
4671084307cSTetsuo Handa 
468b5bc60b4STetsuo Handa /* Structure for policy manager. */
469e2bf6907STetsuo Handa struct tomoyo_manager {
47082e0f001STetsuo Handa 	struct tomoyo_acl_head head;
47182e0f001STetsuo Handa 	bool is_domain;  /* True if manager is a domainname. */
47276bb0895STetsuo Handa 	/* A path to program or a domainname. */
47376bb0895STetsuo Handa 	const struct tomoyo_path_info *manager;
47476bb0895STetsuo Handa };
47576bb0895STetsuo Handa 
47657c2590fSTetsuo Handa struct tomoyo_preference {
47757c2590fSTetsuo Handa 	unsigned int learning_max_entry;
47857c2590fSTetsuo Handa 	bool enforcing_verbose;
47957c2590fSTetsuo Handa 	bool learning_verbose;
48057c2590fSTetsuo Handa 	bool permissive_verbose;
48157c2590fSTetsuo Handa };
48257c2590fSTetsuo Handa 
483b5bc60b4STetsuo Handa /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
48457c2590fSTetsuo Handa struct tomoyo_profile {
48557c2590fSTetsuo Handa 	const struct tomoyo_path_info *comment;
48657c2590fSTetsuo Handa 	struct tomoyo_preference *learning;
48757c2590fSTetsuo Handa 	struct tomoyo_preference *permissive;
48857c2590fSTetsuo Handa 	struct tomoyo_preference *enforcing;
48957c2590fSTetsuo Handa 	struct tomoyo_preference preference;
49057c2590fSTetsuo Handa 	u8 default_config;
49157c2590fSTetsuo Handa 	u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
49257c2590fSTetsuo Handa };
49357c2590fSTetsuo Handa 
49476bb0895STetsuo Handa /********** Function prototypes. **********/
49576bb0895STetsuo Handa 
496c3ef1500STetsuo Handa bool tomoyo_str_starts(char **src, const char *find);
497c3ef1500STetsuo Handa const char *tomoyo_get_exe(void);
498c3ef1500STetsuo Handa void tomoyo_normalize_line(unsigned char *buffer);
499c3ef1500STetsuo Handa void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
500c3ef1500STetsuo Handa      __attribute__ ((format(printf, 2, 3)));
501c3ef1500STetsuo Handa void tomoyo_check_profile(void);
502c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file);
503c3ef1500STetsuo Handa int tomoyo_close_control(struct file *file);
5040849e3baSTetsuo Handa int tomoyo_poll_control(struct file *file, poll_table *wait);
505c3ef1500STetsuo Handa int tomoyo_read_control(struct file *file, char __user *buffer,
506c3ef1500STetsuo Handa 			const int buffer_len);
507c3ef1500STetsuo Handa int tomoyo_write_control(struct file *file, const char __user *buffer,
508c3ef1500STetsuo Handa 			 const int buffer_len);
509c3ef1500STetsuo Handa bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
510c3ef1500STetsuo Handa void tomoyo_warn_oom(const char *function);
511484ca79cSTetsuo Handa const struct tomoyo_path_info *
512484ca79cSTetsuo Handa tomoyo_compare_name_union(const struct tomoyo_path_info *name,
5137762fbffSTetsuo Handa 			  const struct tomoyo_name_union *ptr);
5142106ccd9STetsuo Handa bool tomoyo_compare_number_union(const unsigned long value,
5152106ccd9STetsuo Handa 				 const struct tomoyo_number_union *ptr);
51657c2590fSTetsuo Handa int tomoyo_get_mode(const u8 profile, const u8 index);
517f23571e8STetsuo Handa void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
5189590837bSKentaro Takeda 	__attribute__ ((format(printf, 2, 3)));
51975093152STetsuo Handa bool tomoyo_correct_domain(const unsigned char *domainname);
52075093152STetsuo Handa bool tomoyo_correct_path(const char *filename);
52175093152STetsuo Handa bool tomoyo_correct_word(const char *string);
52275093152STetsuo Handa bool tomoyo_domain_def(const unsigned char *buffer);
5237762fbffSTetsuo Handa bool tomoyo_parse_name_union(const char *filename,
5247762fbffSTetsuo Handa 			     struct tomoyo_name_union *ptr);
525484ca79cSTetsuo Handa const struct tomoyo_path_info *
526484ca79cSTetsuo Handa tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
527a98aa4deSTetsuo Handa 			  const struct tomoyo_group *group);
5284c3e9e2dSTetsuo Handa bool tomoyo_number_matches_group(const unsigned long min,
5294c3e9e2dSTetsuo Handa 				 const unsigned long max,
530a98aa4deSTetsuo Handa 				 const struct tomoyo_group *group);
5319590837bSKentaro Takeda bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
5329590837bSKentaro Takeda 				 const struct tomoyo_path_info *pattern);
5334c3e9e2dSTetsuo Handa bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
5347762fbffSTetsuo Handa bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
5359590837bSKentaro Takeda bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
5362106ccd9STetsuo Handa int tomoyo_init_request_info(struct tomoyo_request_info *r,
53757c2590fSTetsuo Handa 			     struct tomoyo_domain_info *domain,
53857c2590fSTetsuo Handa 			     const u8 index);
539b5bc60b4STetsuo Handa int tomoyo_mount_permission(char *dev_name, struct path *path,
540b5bc60b4STetsuo Handa 			    const char *type, unsigned long flags,
541b5bc60b4STetsuo Handa 			    void *data_page);
542e2bf6907STetsuo Handa int tomoyo_write_aggregator(char *data, const bool is_delete);
5435448ec4fSTetsuo Handa int tomoyo_write_transition_control(char *data, const bool is_delete,
5445448ec4fSTetsuo Handa 				    const u8 type);
545e2bf6907STetsuo Handa int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain,
5469590837bSKentaro Takeda 		      const bool is_delete);
547e2bf6907STetsuo Handa int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
5482106ccd9STetsuo Handa 		       const bool is_delete);
5497c2ea22eSTetsuo Handa int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
55017fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
55117fcfbd9STetsuo Handa      __attribute__ ((format(printf, 2, 3)));
5529590837bSKentaro Takeda struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
553e2bf6907STetsuo Handa struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
5549590837bSKentaro Takeda 						const u8 profile);
55557c2590fSTetsuo Handa struct tomoyo_profile *tomoyo_profile(const u8 profile);
5567c2ea22eSTetsuo Handa struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
5579590837bSKentaro Takeda unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
5589590837bSKentaro Takeda 				const u8 index);
5599590837bSKentaro Takeda void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
5609590837bSKentaro Takeda void tomoyo_load_policy(const char *filename);
5614c3e9e2dSTetsuo Handa void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
562c8c57e84STetsuo Handa char *tomoyo_encode(const char *str);
56376bb0895STetsuo Handa char *tomoyo_realpath_nofollow(const char *pathname);
56476bb0895STetsuo Handa char *tomoyo_realpath_from_path(struct path *path);
56576bb0895STetsuo Handa bool tomoyo_memory_ok(void *ptr);
5669e4b50e9STetsuo Handa void *tomoyo_commit_ok(void *data, const unsigned int size);
56776bb0895STetsuo Handa const struct tomoyo_path_info *tomoyo_get_name(const char *name);
5688fbe71f0STetsuo Handa void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
56976bb0895STetsuo Handa int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
570c3ef1500STetsuo Handa void __init tomoyo_mm_init(void);
57105336deeSTetsuo Handa int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
57276bb0895STetsuo Handa 			   const struct tomoyo_path_info *filename);
57376bb0895STetsuo Handa int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
57476bb0895STetsuo Handa 				 struct path *path, const int flag);
575a1f9bb6aSTetsuo Handa int tomoyo_path_number_perm(const u8 operation, struct path *path,
576a1f9bb6aSTetsuo Handa 			    unsigned long number);
57775093152STetsuo Handa int tomoyo_mkdev_perm(const u8 operation, struct path *path,
578a1f9bb6aSTetsuo Handa 		      const unsigned int mode, unsigned int dev);
57997d6931eSTetsuo Handa int tomoyo_path_perm(const u8 operation, struct path *path);
58097d6931eSTetsuo Handa int tomoyo_path2_perm(const u8 operation, struct path *path1,
58197d6931eSTetsuo Handa 		      struct path *path2);
58276bb0895STetsuo Handa int tomoyo_find_next_domain(struct linux_binprm *bprm);
583a1f9bb6aSTetsuo Handa void tomoyo_print_ulong(char *buffer, const int buffer_len,
584a1f9bb6aSTetsuo Handa 			const unsigned long value, const u8 type);
5857762fbffSTetsuo Handa void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
586847b173eSTetsuo Handa void tomoyo_run_gc(void);
587847b173eSTetsuo Handa void tomoyo_memory_free(void *ptr);
588237ab459STetsuo Handa int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
589237ab459STetsuo Handa 			 bool is_delete, struct tomoyo_domain_info *domain,
590237ab459STetsuo Handa 			 bool (*check_duplicate) (const struct tomoyo_acl_info
591237ab459STetsuo Handa 						  *,
592237ab459STetsuo Handa 						  const struct tomoyo_acl_info
593237ab459STetsuo Handa 						  *),
594237ab459STetsuo Handa 			 bool (*merge_duplicate) (struct tomoyo_acl_info *,
595237ab459STetsuo Handa 						  struct tomoyo_acl_info *,
596237ab459STetsuo Handa 						  const bool));
59736f5e1ffSTetsuo Handa int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
59836f5e1ffSTetsuo Handa 			 bool is_delete, struct list_head *list,
59936f5e1ffSTetsuo Handa 			 bool (*check_duplicate) (const struct tomoyo_acl_head
60036f5e1ffSTetsuo Handa 						  *,
60136f5e1ffSTetsuo Handa 						  const struct tomoyo_acl_head
60236f5e1ffSTetsuo Handa 						  *));
60399a85259STetsuo Handa void tomoyo_check_acl(struct tomoyo_request_info *r,
604484ca79cSTetsuo Handa 		      bool (*check_entry) (struct tomoyo_request_info *,
60599a85259STetsuo Handa 					   const struct tomoyo_acl_info *));
606237ab459STetsuo Handa 
60776bb0895STetsuo Handa /********** External variable definitions. **********/
60876bb0895STetsuo Handa 
60976bb0895STetsuo Handa /* Lock for GC. */
61076bb0895STetsuo Handa extern struct srcu_struct tomoyo_ss;
61176bb0895STetsuo Handa 
61276bb0895STetsuo Handa /* The list for "struct tomoyo_domain_info". */
61376bb0895STetsuo Handa extern struct list_head tomoyo_domain_list;
61476bb0895STetsuo Handa 
615a230f9e7STetsuo Handa extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
616a230f9e7STetsuo Handa extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
617847b173eSTetsuo Handa extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
618847b173eSTetsuo Handa 
61976bb0895STetsuo Handa /* Lock for protecting policy. */
62076bb0895STetsuo Handa extern struct mutex tomoyo_policy_lock;
62176bb0895STetsuo Handa 
62276bb0895STetsuo Handa /* Has /sbin/init started? */
62376bb0895STetsuo Handa extern bool tomoyo_policy_loaded;
62476bb0895STetsuo Handa 
62576bb0895STetsuo Handa /* The kernel's domain. */
62676bb0895STetsuo Handa extern struct tomoyo_domain_info tomoyo_kernel_domain;
62776bb0895STetsuo Handa 
62871c28236STetsuo Handa extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
62971c28236STetsuo Handa extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
63071c28236STetsuo Handa extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
63171c28236STetsuo Handa extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
63271c28236STetsuo Handa 
63317fcfbd9STetsuo Handa extern unsigned int tomoyo_quota_for_query;
63417fcfbd9STetsuo Handa extern unsigned int tomoyo_query_memory_size;
63517fcfbd9STetsuo Handa 
63676bb0895STetsuo Handa /********** Inlined functions. **********/
63776bb0895STetsuo Handa 
638b5bc60b4STetsuo Handa /**
639b5bc60b4STetsuo Handa  * tomoyo_read_lock - Take lock for protecting policy.
640b5bc60b4STetsuo Handa  *
641b5bc60b4STetsuo Handa  * Returns index number for tomoyo_read_unlock().
642b5bc60b4STetsuo Handa  */
64376bb0895STetsuo Handa static inline int tomoyo_read_lock(void)
64476bb0895STetsuo Handa {
64576bb0895STetsuo Handa 	return srcu_read_lock(&tomoyo_ss);
64676bb0895STetsuo Handa }
64776bb0895STetsuo Handa 
648b5bc60b4STetsuo Handa /**
649b5bc60b4STetsuo Handa  * tomoyo_read_unlock - Release lock for protecting policy.
650b5bc60b4STetsuo Handa  *
651b5bc60b4STetsuo Handa  * @idx: Index number returned by tomoyo_read_lock().
652b5bc60b4STetsuo Handa  *
653b5bc60b4STetsuo Handa  * Returns nothing.
654b5bc60b4STetsuo Handa  */
65576bb0895STetsuo Handa static inline void tomoyo_read_unlock(int idx)
65676bb0895STetsuo Handa {
65776bb0895STetsuo Handa 	srcu_read_unlock(&tomoyo_ss, idx);
65876bb0895STetsuo Handa }
65976bb0895STetsuo Handa 
660b5bc60b4STetsuo Handa /**
661b5bc60b4STetsuo Handa  * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
662b5bc60b4STetsuo Handa  *
663b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_path_info".
664b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_path_info".
665b5bc60b4STetsuo Handa  *
666b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
667b5bc60b4STetsuo Handa  */
6689590837bSKentaro Takeda static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
6699590837bSKentaro Takeda 				  const struct tomoyo_path_info *b)
6709590837bSKentaro Takeda {
6719590837bSKentaro Takeda 	return a->hash != b->hash || strcmp(a->name, b->name);
6729590837bSKentaro Takeda }
6739590837bSKentaro Takeda 
6749590837bSKentaro Takeda /**
67575093152STetsuo Handa  * tomoyo_valid - Check whether the character is a valid char.
6769590837bSKentaro Takeda  *
6779590837bSKentaro Takeda  * @c: The character to check.
6789590837bSKentaro Takeda  *
6799590837bSKentaro Takeda  * Returns true if @c is a valid character, false otherwise.
6809590837bSKentaro Takeda  */
68175093152STetsuo Handa static inline bool tomoyo_valid(const unsigned char c)
6829590837bSKentaro Takeda {
6839590837bSKentaro Takeda 	return c > ' ' && c < 127;
6849590837bSKentaro Takeda }
6859590837bSKentaro Takeda 
6869590837bSKentaro Takeda /**
68775093152STetsuo Handa  * tomoyo_invalid - Check whether the character is an invalid char.
6889590837bSKentaro Takeda  *
6899590837bSKentaro Takeda  * @c: The character to check.
6909590837bSKentaro Takeda  *
6919590837bSKentaro Takeda  * Returns true if @c is an invalid character, false otherwise.
6929590837bSKentaro Takeda  */
69375093152STetsuo Handa static inline bool tomoyo_invalid(const unsigned char c)
6949590837bSKentaro Takeda {
6959590837bSKentaro Takeda 	return c && (c <= ' ' || c >= 127);
6969590837bSKentaro Takeda }
6979590837bSKentaro Takeda 
698b5bc60b4STetsuo Handa /**
699b5bc60b4STetsuo Handa  * tomoyo_put_name - Drop reference on "struct tomoyo_name".
700b5bc60b4STetsuo Handa  *
701b5bc60b4STetsuo Handa  * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
702b5bc60b4STetsuo Handa  *
703b5bc60b4STetsuo Handa  * Returns nothing.
704b5bc60b4STetsuo Handa  */
70576bb0895STetsuo Handa static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
70676bb0895STetsuo Handa {
70776bb0895STetsuo Handa 	if (name) {
708e2bf6907STetsuo Handa 		struct tomoyo_name *ptr =
709e2bf6907STetsuo Handa 			container_of(name, typeof(*ptr), entry);
71076bb0895STetsuo Handa 		atomic_dec(&ptr->users);
71176bb0895STetsuo Handa 	}
71276bb0895STetsuo Handa }
7139590837bSKentaro Takeda 
714b5bc60b4STetsuo Handa /**
715b5bc60b4STetsuo Handa  * tomoyo_put_group - Drop reference on "struct tomoyo_group".
716b5bc60b4STetsuo Handa  *
717b5bc60b4STetsuo Handa  * @group: Pointer to "struct tomoyo_group". Maybe NULL.
718b5bc60b4STetsuo Handa  *
719b5bc60b4STetsuo Handa  * Returns nothing.
720b5bc60b4STetsuo Handa  */
721a98aa4deSTetsuo Handa static inline void tomoyo_put_group(struct tomoyo_group *group)
7224c3e9e2dSTetsuo Handa {
7234c3e9e2dSTetsuo Handa 	if (group)
7244c3e9e2dSTetsuo Handa 		atomic_dec(&group->users);
7254c3e9e2dSTetsuo Handa }
7264c3e9e2dSTetsuo Handa 
727b5bc60b4STetsuo Handa /**
728b5bc60b4STetsuo Handa  * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
729b5bc60b4STetsuo Handa  *
730b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" for current thread.
731b5bc60b4STetsuo Handa  */
73276bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_domain(void)
73376bb0895STetsuo Handa {
73476bb0895STetsuo Handa 	return current_cred()->security;
73576bb0895STetsuo Handa }
7369590837bSKentaro Takeda 
737b5bc60b4STetsuo Handa /**
738b5bc60b4STetsuo Handa  * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
739b5bc60b4STetsuo Handa  *
740b5bc60b4STetsuo Handa  * @task: Pointer to "struct task_struct".
741b5bc60b4STetsuo Handa  *
742b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_security" for specified thread.
743b5bc60b4STetsuo Handa  */
74476bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
74576bb0895STetsuo Handa 							    *task)
74676bb0895STetsuo Handa {
74776bb0895STetsuo Handa 	return task_cred_xxx(task, security);
74876bb0895STetsuo Handa }
7499590837bSKentaro Takeda 
75075093152STetsuo Handa static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
7517762fbffSTetsuo Handa 					const struct tomoyo_acl_info *p2)
7527762fbffSTetsuo Handa {
7537762fbffSTetsuo Handa 	return p1->type == p2->type;
7547762fbffSTetsuo Handa }
7557762fbffSTetsuo Handa 
756b5bc60b4STetsuo Handa /**
757b5bc60b4STetsuo Handa  * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
758b5bc60b4STetsuo Handa  *
759b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_name_union".
760b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_name_union".
761b5bc60b4STetsuo Handa  *
762b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
763b5bc60b4STetsuo Handa  */
76475093152STetsuo Handa static inline bool tomoyo_same_name_union
765b5bc60b4STetsuo Handa (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
7667762fbffSTetsuo Handa {
767b5bc60b4STetsuo Handa 	return a->filename == b->filename && a->group == b->group &&
768b5bc60b4STetsuo Handa 		a->is_group == b->is_group;
7697762fbffSTetsuo Handa }
7707762fbffSTetsuo Handa 
771b5bc60b4STetsuo Handa /**
772b5bc60b4STetsuo Handa  * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
773b5bc60b4STetsuo Handa  *
774b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_number_union".
775b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_number_union".
776b5bc60b4STetsuo Handa  *
777b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
778b5bc60b4STetsuo Handa  */
77975093152STetsuo Handa static inline bool tomoyo_same_number_union
780b5bc60b4STetsuo Handa (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
7814c3e9e2dSTetsuo Handa {
782b5bc60b4STetsuo Handa 	return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
783b5bc60b4STetsuo Handa 		a->group == b->group && a->min_type == b->min_type &&
784b5bc60b4STetsuo Handa 		a->max_type == b->max_type && a->is_group == b->is_group;
7854c3e9e2dSTetsuo Handa }
7864c3e9e2dSTetsuo Handa 
7879590837bSKentaro Takeda /**
7889590837bSKentaro Takeda  * list_for_each_cookie - iterate over a list with cookie.
7899590837bSKentaro Takeda  * @pos:        the &struct list_head to use as a loop cursor.
7909590837bSKentaro Takeda  * @head:       the head for your list.
7919590837bSKentaro Takeda  */
792475e6fa3STetsuo Handa #define list_for_each_cookie(pos, head)					\
793475e6fa3STetsuo Handa 	if (!pos)							\
794475e6fa3STetsuo Handa 		pos =  srcu_dereference((head)->next, &tomoyo_ss);	\
795475e6fa3STetsuo Handa 	for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
796fdb8ebb7STetsuo Handa 
7979590837bSKentaro Takeda #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
798