xref: /openbmc/linux/security/tomoyo/common.h (revision eadd99cc)
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,
47eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_MAX_MODE,
48eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_WANT_REJECT_LOG =  64,
49eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_WANT_GRANT_LOG  = 128,
50eadd99ccSTetsuo Handa 	TOMOYO_CONFIG_USE_DEFAULT     = 255,
51cb0abe6aSTetsuo Handa };
52cb0abe6aSTetsuo Handa 
53b5bc60b4STetsuo Handa /* Index numbers for entry type. */
54a230f9e7STetsuo Handa enum tomoyo_policy_id {
55a230f9e7STetsuo Handa 	TOMOYO_ID_GROUP,
56a230f9e7STetsuo Handa 	TOMOYO_ID_PATH_GROUP,
57a230f9e7STetsuo Handa 	TOMOYO_ID_NUMBER_GROUP,
585448ec4fSTetsuo Handa 	TOMOYO_ID_TRANSITION_CONTROL,
59a230f9e7STetsuo Handa 	TOMOYO_ID_AGGREGATOR,
60a230f9e7STetsuo Handa 	TOMOYO_ID_MANAGER,
61a230f9e7STetsuo Handa 	TOMOYO_ID_NAME,
62a230f9e7STetsuo Handa 	TOMOYO_ID_ACL,
63a230f9e7STetsuo Handa 	TOMOYO_ID_DOMAIN,
64a230f9e7STetsuo Handa 	TOMOYO_MAX_POLICY
65a230f9e7STetsuo Handa };
66a230f9e7STetsuo Handa 
67b5bc60b4STetsuo Handa /* Index numbers for group entries. */
68a230f9e7STetsuo Handa enum tomoyo_group_id {
69a230f9e7STetsuo Handa 	TOMOYO_PATH_GROUP,
70a230f9e7STetsuo Handa 	TOMOYO_NUMBER_GROUP,
71a230f9e7STetsuo Handa 	TOMOYO_MAX_GROUP
72a230f9e7STetsuo Handa };
73a230f9e7STetsuo Handa 
7476bb0895STetsuo Handa /* A domain definition starts with <kernel>. */
7576bb0895STetsuo Handa #define TOMOYO_ROOT_NAME                         "<kernel>"
7676bb0895STetsuo Handa #define TOMOYO_ROOT_NAME_LEN                     (sizeof(TOMOYO_ROOT_NAME) - 1)
7776bb0895STetsuo Handa 
78b5bc60b4STetsuo Handa /* Index numbers for type of numeric values. */
79b5bc60b4STetsuo Handa enum tomoyo_value_type {
80b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_INVALID,
81b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_DECIMAL,
82b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_OCTAL,
83b5bc60b4STetsuo Handa 	TOMOYO_VALUE_TYPE_HEXADECIMAL,
84b5bc60b4STetsuo Handa };
854c3e9e2dSTetsuo Handa 
86b5bc60b4STetsuo Handa /* Index numbers for domain transition control keywords. */
875448ec4fSTetsuo Handa enum tomoyo_transition_type {
885448ec4fSTetsuo Handa 	/* Do not change this order, */
895448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
905448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_INITIALIZE,
915448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_NO_KEEP,
925448ec4fSTetsuo Handa 	TOMOYO_TRANSITION_CONTROL_KEEP,
935448ec4fSTetsuo Handa 	TOMOYO_MAX_TRANSITION_TYPE
945448ec4fSTetsuo Handa };
955448ec4fSTetsuo Handa 
9676bb0895STetsuo Handa /* Index numbers for Access Controls. */
97084da356STetsuo Handa enum tomoyo_acl_entry_type_index {
987ef61233STetsuo Handa 	TOMOYO_TYPE_PATH_ACL,
997ef61233STetsuo Handa 	TOMOYO_TYPE_PATH2_ACL,
100a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_PATH_NUMBER_ACL,
10175093152STetsuo Handa 	TOMOYO_TYPE_MKDEV_ACL,
1022106ccd9STetsuo Handa 	TOMOYO_TYPE_MOUNT_ACL,
103084da356STetsuo Handa };
10476bb0895STetsuo Handa 
105b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname. */
106084da356STetsuo Handa enum tomoyo_path_acl_index {
1077ef61233STetsuo Handa 	TOMOYO_TYPE_EXECUTE,
1087ef61233STetsuo Handa 	TOMOYO_TYPE_READ,
1097ef61233STetsuo Handa 	TOMOYO_TYPE_WRITE,
1107c75964fSTetsuo Handa 	TOMOYO_TYPE_APPEND,
1117ef61233STetsuo Handa 	TOMOYO_TYPE_UNLINK,
1127c75964fSTetsuo Handa 	TOMOYO_TYPE_GETATTR,
1137ef61233STetsuo Handa 	TOMOYO_TYPE_RMDIR,
1147ef61233STetsuo Handa 	TOMOYO_TYPE_TRUNCATE,
1157ef61233STetsuo Handa 	TOMOYO_TYPE_SYMLINK,
1167ef61233STetsuo Handa 	TOMOYO_TYPE_CHROOT,
1177ef61233STetsuo Handa 	TOMOYO_TYPE_UMOUNT,
1187ef61233STetsuo Handa 	TOMOYO_MAX_PATH_OPERATION
119084da356STetsuo Handa };
12076bb0895STetsuo Handa 
121eadd99ccSTetsuo Handa enum tomoyo_memory_stat_type {
122eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_POLICY,
123eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_AUDIT,
124eadd99ccSTetsuo Handa 	TOMOYO_MEMORY_QUERY,
125eadd99ccSTetsuo Handa 	TOMOYO_MAX_MEMORY_STAT
126eadd99ccSTetsuo Handa };
127eadd99ccSTetsuo Handa 
12875093152STetsuo Handa enum tomoyo_mkdev_acl_index {
129a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKBLOCK,
130a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKCHAR,
13175093152STetsuo Handa 	TOMOYO_MAX_MKDEV_OPERATION
132a1f9bb6aSTetsuo Handa };
133a1f9bb6aSTetsuo Handa 
134b5bc60b4STetsuo Handa /* Index numbers for access controls with two pathnames. */
135084da356STetsuo Handa enum tomoyo_path2_acl_index {
1367ef61233STetsuo Handa 	TOMOYO_TYPE_LINK,
1377ef61233STetsuo Handa 	TOMOYO_TYPE_RENAME,
1387ef61233STetsuo Handa 	TOMOYO_TYPE_PIVOT_ROOT,
1397ef61233STetsuo Handa 	TOMOYO_MAX_PATH2_OPERATION
140084da356STetsuo Handa };
14176bb0895STetsuo Handa 
142b5bc60b4STetsuo Handa /* Index numbers for access controls with one pathname and one number. */
143a1f9bb6aSTetsuo Handa enum tomoyo_path_number_acl_index {
144a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CREATE,
145a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKDIR,
146a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKFIFO,
147a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_MKSOCK,
148a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_IOCTL,
149a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHMOD,
150a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHOWN,
151a1f9bb6aSTetsuo Handa 	TOMOYO_TYPE_CHGRP,
152a1f9bb6aSTetsuo Handa 	TOMOYO_MAX_PATH_NUMBER_OPERATION
153a1f9bb6aSTetsuo Handa };
154a1f9bb6aSTetsuo Handa 
155b5bc60b4STetsuo Handa /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
156084da356STetsuo Handa enum tomoyo_securityfs_interface_index {
157084da356STetsuo Handa 	TOMOYO_DOMAINPOLICY,
158084da356STetsuo Handa 	TOMOYO_EXCEPTIONPOLICY,
159084da356STetsuo Handa 	TOMOYO_DOMAIN_STATUS,
160084da356STetsuo Handa 	TOMOYO_PROCESS_STATUS,
161084da356STetsuo Handa 	TOMOYO_MEMINFO,
162084da356STetsuo Handa 	TOMOYO_SELFDOMAIN,
163eadd99ccSTetsuo Handa 	TOMOYO_AUDIT,
164084da356STetsuo Handa 	TOMOYO_VERSION,
165084da356STetsuo Handa 	TOMOYO_PROFILE,
16617fcfbd9STetsuo Handa 	TOMOYO_QUERY,
167084da356STetsuo Handa 	TOMOYO_MANAGER
168084da356STetsuo Handa };
16976bb0895STetsuo Handa 
170b5bc60b4STetsuo Handa /* Index numbers for special mount operations. */
171b5bc60b4STetsuo Handa enum tomoyo_special_mount {
172b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_BIND,            /* mount --bind /source /dest   */
173b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MOVE,            /* mount --move /old /new       */
174b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_REMOUNT,         /* mount -o remount /dir        */
175b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
176b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_PRIVATE,    /* mount --make-private /dir    */
177b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SLAVE,      /* mount --make-slave /dir      */
178b5bc60b4STetsuo Handa 	TOMOYO_MOUNT_MAKE_SHARED,     /* mount --make-shared /dir     */
179b5bc60b4STetsuo Handa 	TOMOYO_MAX_SPECIAL_MOUNT
180b5bc60b4STetsuo Handa };
181b5bc60b4STetsuo Handa 
182b5bc60b4STetsuo Handa /* Index numbers for functionality. */
18357c2590fSTetsuo Handa enum tomoyo_mac_index {
18457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_EXECUTE,
18557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_OPEN,
18657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CREATE,
18757c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UNLINK,
1887c75964fSTetsuo Handa 	TOMOYO_MAC_FILE_GETATTR,
18957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKDIR,
19057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RMDIR,
19157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKFIFO,
19257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKSOCK,
19357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_TRUNCATE,
19457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_SYMLINK,
19557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKBLOCK,
19657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MKCHAR,
19757c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_LINK,
19857c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_RENAME,
19957c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHMOD,
20057c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHOWN,
20157c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHGRP,
20257c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_IOCTL,
20357c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_CHROOT,
20457c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_MOUNT,
20557c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_UMOUNT,
20657c2590fSTetsuo Handa 	TOMOYO_MAC_FILE_PIVOT_ROOT,
20757c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_INDEX
20857c2590fSTetsuo Handa };
20957c2590fSTetsuo Handa 
210b5bc60b4STetsuo Handa /* Index numbers for category of functionality. */
21157c2590fSTetsuo Handa enum tomoyo_mac_category_index {
21257c2590fSTetsuo Handa 	TOMOYO_MAC_CATEGORY_FILE,
21357c2590fSTetsuo Handa 	TOMOYO_MAX_MAC_CATEGORY_INDEX
21457c2590fSTetsuo Handa };
21557c2590fSTetsuo Handa 
216b5bc60b4STetsuo Handa /*
217b5bc60b4STetsuo Handa  * Retry this request. Returned by tomoyo_supervisor() if policy violation has
218b5bc60b4STetsuo Handa  * occurred in enforcing mode and the userspace daemon decided to retry.
219b5bc60b4STetsuo Handa  *
220b5bc60b4STetsuo Handa  * We must choose a positive value in order to distinguish "granted" (which is
221b5bc60b4STetsuo Handa  * 0) and "rejected" (which is a negative value) and "retry".
222b5bc60b4STetsuo Handa  */
223b5bc60b4STetsuo Handa #define TOMOYO_RETRY_REQUEST 1
22417fcfbd9STetsuo Handa 
225d5ca1725STetsuo Handa /* Index numbers for profile's PREFERENCE values. */
226d5ca1725STetsuo Handa enum tomoyo_pref_index {
227eadd99ccSTetsuo Handa 	TOMOYO_PREF_MAX_AUDIT_LOG,
228d5ca1725STetsuo Handa 	TOMOYO_PREF_MAX_LEARNING_ENTRY,
229d5ca1725STetsuo Handa 	TOMOYO_MAX_PREF
230d5ca1725STetsuo Handa };
231d5ca1725STetsuo Handa 
23276bb0895STetsuo Handa /********** Structure definitions. **********/
2339590837bSKentaro Takeda 
234b5bc60b4STetsuo Handa /* Common header for holding ACL entries. */
23582e0f001STetsuo Handa struct tomoyo_acl_head {
23682e0f001STetsuo Handa 	struct list_head list;
23782e0f001STetsuo Handa 	bool is_deleted;
23882e0f001STetsuo Handa } __packed;
23982e0f001STetsuo Handa 
2400df7e8b8STetsuo Handa /* Common header for shared entries. */
2410df7e8b8STetsuo Handa struct tomoyo_shared_acl_head {
2420df7e8b8STetsuo Handa 	struct list_head list;
2430df7e8b8STetsuo Handa 	atomic_t users;
2440df7e8b8STetsuo Handa } __packed;
2450df7e8b8STetsuo Handa 
246b5bc60b4STetsuo Handa /* Structure for request info. */
247cb0abe6aSTetsuo Handa struct tomoyo_request_info {
248cb0abe6aSTetsuo Handa 	struct tomoyo_domain_info *domain;
249cf6e9a64STetsuo Handa 	/* For holding parameters. */
250cf6e9a64STetsuo Handa 	union {
251cf6e9a64STetsuo Handa 		struct {
252cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
253484ca79cSTetsuo Handa 			/* For using wildcards at tomoyo_find_next_domain(). */
254484ca79cSTetsuo Handa 			const struct tomoyo_path_info *matched_path;
255b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path_acl_index". */
256cf6e9a64STetsuo Handa 			u8 operation;
257cf6e9a64STetsuo Handa 		} path;
258cf6e9a64STetsuo Handa 		struct {
259cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename1;
260cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename2;
261b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_path2_acl_index". */
262cf6e9a64STetsuo Handa 			u8 operation;
263cf6e9a64STetsuo Handa 		} path2;
264cf6e9a64STetsuo Handa 		struct {
265cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
266cf6e9a64STetsuo Handa 			unsigned int mode;
267cf6e9a64STetsuo Handa 			unsigned int major;
268cf6e9a64STetsuo Handa 			unsigned int minor;
269b5bc60b4STetsuo Handa 			/* One of values in "enum tomoyo_mkdev_acl_index". */
270cf6e9a64STetsuo Handa 			u8 operation;
271cf6e9a64STetsuo Handa 		} mkdev;
272cf6e9a64STetsuo Handa 		struct {
273cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *filename;
274cf6e9a64STetsuo Handa 			unsigned long number;
275b5bc60b4STetsuo Handa 			/*
276b5bc60b4STetsuo Handa 			 * One of values in
277b5bc60b4STetsuo Handa 			 * "enum tomoyo_path_number_acl_index".
278b5bc60b4STetsuo Handa 			 */
279cf6e9a64STetsuo Handa 			u8 operation;
280cf6e9a64STetsuo Handa 		} path_number;
281cf6e9a64STetsuo Handa 		struct {
282cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *type;
283cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dir;
284cf6e9a64STetsuo Handa 			const struct tomoyo_path_info *dev;
285cf6e9a64STetsuo Handa 			unsigned long flags;
286cf6e9a64STetsuo Handa 			int need_dev;
287cf6e9a64STetsuo Handa 		} mount;
288cf6e9a64STetsuo Handa 	} param;
289cf6e9a64STetsuo Handa 	u8 param_type;
290cf6e9a64STetsuo Handa 	bool granted;
29117fcfbd9STetsuo Handa 	u8 retry;
29217fcfbd9STetsuo Handa 	u8 profile;
293cb0abe6aSTetsuo Handa 	u8 mode; /* One of tomoyo_mode_index . */
29457c2590fSTetsuo Handa 	u8 type;
295cb0abe6aSTetsuo Handa };
296cb0abe6aSTetsuo Handa 
297b5bc60b4STetsuo Handa /* Structure for holding a token. */
2989590837bSKentaro Takeda struct tomoyo_path_info {
2999590837bSKentaro Takeda 	const char *name;
3009590837bSKentaro Takeda 	u32 hash;          /* = full_name_hash(name, strlen(name)) */
3019590837bSKentaro Takeda 	u16 const_len;     /* = tomoyo_const_part_length(name)     */
3029590837bSKentaro Takeda 	bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
3039590837bSKentaro Takeda 	bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
3049590837bSKentaro Takeda };
3059590837bSKentaro Takeda 
306b5bc60b4STetsuo Handa /* Structure for holding string data. */
307e2bf6907STetsuo Handa struct tomoyo_name {
3080df7e8b8STetsuo Handa 	struct tomoyo_shared_acl_head head;
30976bb0895STetsuo Handa 	struct tomoyo_path_info entry;
31076bb0895STetsuo Handa };
3119590837bSKentaro Takeda 
312b5bc60b4STetsuo Handa /* Structure for holding a word. */
3137762fbffSTetsuo Handa struct tomoyo_name_union {
314b5bc60b4STetsuo Handa 	/* Either @filename or @group is NULL. */
3157762fbffSTetsuo Handa 	const struct tomoyo_path_info *filename;
316a98aa4deSTetsuo Handa 	struct tomoyo_group *group;
3177762fbffSTetsuo Handa };
3187762fbffSTetsuo Handa 
319b5bc60b4STetsuo Handa /* Structure for holding a number. */
3204c3e9e2dSTetsuo Handa struct tomoyo_number_union {
3214c3e9e2dSTetsuo Handa 	unsigned long values[2];
322b5bc60b4STetsuo Handa 	struct tomoyo_group *group; /* Maybe NULL. */
323b5bc60b4STetsuo Handa 	/* One of values in "enum tomoyo_value_type". */
3240df7e8b8STetsuo Handa 	u8 value_type[2];
3254c3e9e2dSTetsuo Handa };
3264c3e9e2dSTetsuo Handa 
327a98aa4deSTetsuo Handa /* Structure for "path_group"/"number_group" directive. */
328a98aa4deSTetsuo Handa struct tomoyo_group {
3290df7e8b8STetsuo Handa 	struct tomoyo_shared_acl_head head;
330a98aa4deSTetsuo Handa 	const struct tomoyo_path_info *group_name;
331a98aa4deSTetsuo Handa 	struct list_head member_list;
332a98aa4deSTetsuo Handa };
333a98aa4deSTetsuo Handa 
3347762fbffSTetsuo Handa /* Structure for "path_group" directive. */
3357762fbffSTetsuo Handa struct tomoyo_path_group {
33682e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3377762fbffSTetsuo Handa 	const struct tomoyo_path_info *member_name;
3387762fbffSTetsuo Handa };
3397762fbffSTetsuo Handa 
3404c3e9e2dSTetsuo Handa /* Structure for "number_group" directive. */
341a98aa4deSTetsuo Handa struct tomoyo_number_group {
34282e0f001STetsuo Handa 	struct tomoyo_acl_head head;
3434c3e9e2dSTetsuo Handa 	struct tomoyo_number_union number;
3444c3e9e2dSTetsuo Handa };
3454c3e9e2dSTetsuo Handa 
346b5bc60b4STetsuo Handa /* Common header for individual entries. */
3479590837bSKentaro Takeda struct tomoyo_acl_info {
3489590837bSKentaro Takeda 	struct list_head list;
349237ab459STetsuo Handa 	bool is_deleted;
350b5bc60b4STetsuo Handa 	u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
3519590837bSKentaro Takeda } __packed;
3529590837bSKentaro Takeda 
353b5bc60b4STetsuo Handa /* Structure for domain information. */
3549590837bSKentaro Takeda struct tomoyo_domain_info {
3559590837bSKentaro Takeda 	struct list_head list;
3569590837bSKentaro Takeda 	struct list_head acl_info_list;
3579590837bSKentaro Takeda 	/* Name of this domain. Never NULL.          */
3589590837bSKentaro Takeda 	const struct tomoyo_path_info *domainname;
3599590837bSKentaro Takeda 	u8 profile;        /* Profile number to use. */
360a0558fc3STetsuo Handa 	bool is_deleted;   /* Delete flag.           */
3619590837bSKentaro Takeda 	bool quota_warned; /* Quota warnning flag.   */
362ea13ddbaSTetsuo Handa 	bool transition_failed; /* Domain transition failed flag. */
363ec8e6a4eSTetsuo Handa 	atomic_t users; /* Number of referring credentials. */
3649590837bSKentaro Takeda };
3659590837bSKentaro Takeda 
3669590837bSKentaro Takeda /*
367b5bc60b4STetsuo Handa  * Structure for "file execute", "file read", "file write", "file append",
368b5bc60b4STetsuo Handa  * "file unlink", "file getattr", "file rmdir", "file truncate",
369b5bc60b4STetsuo Handa  * "file symlink", "file chroot" and "file unmount" directive.
3709590837bSKentaro Takeda  */
3717ef61233STetsuo Handa struct tomoyo_path_acl {
3727ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
373b5bc60b4STetsuo Handa 	u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
3747762fbffSTetsuo Handa 	struct tomoyo_name_union name;
3759590837bSKentaro Takeda };
3769590837bSKentaro Takeda 
377c3fa109aSTetsuo Handa /*
378b5bc60b4STetsuo Handa  * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
379b5bc60b4STetsuo Handa  * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
380a1f9bb6aSTetsuo Handa  */
381a1f9bb6aSTetsuo Handa struct tomoyo_path_number_acl {
382a1f9bb6aSTetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
383b5bc60b4STetsuo Handa 	/* Bitmask of values in "enum tomoyo_path_number_acl_index". */
384a1f9bb6aSTetsuo Handa 	u8 perm;
385a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
386a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union number;
387a1f9bb6aSTetsuo Handa };
388a1f9bb6aSTetsuo Handa 
389b5bc60b4STetsuo Handa /* Structure for "file mkblock" and "file mkchar" directive. */
39075093152STetsuo Handa struct tomoyo_mkdev_acl {
39175093152STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
392b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
393a1f9bb6aSTetsuo Handa 	struct tomoyo_name_union name;
394a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union mode;
395a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union major;
396a1f9bb6aSTetsuo Handa 	struct tomoyo_number_union minor;
397a1f9bb6aSTetsuo Handa };
398a1f9bb6aSTetsuo Handa 
399a1f9bb6aSTetsuo Handa /*
400b5bc60b4STetsuo Handa  * Structure for "file rename", "file link" and "file pivot_root" directive.
401c3fa109aSTetsuo Handa  */
4027ef61233STetsuo Handa struct tomoyo_path2_acl {
4037ef61233STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
404b5bc60b4STetsuo Handa 	u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
4057762fbffSTetsuo Handa 	struct tomoyo_name_union name1;
4067762fbffSTetsuo Handa 	struct tomoyo_name_union name2;
4079590837bSKentaro Takeda };
4089590837bSKentaro Takeda 
409b5bc60b4STetsuo Handa /* Structure for "file mount" directive. */
4102106ccd9STetsuo Handa struct tomoyo_mount_acl {
4112106ccd9STetsuo Handa 	struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
4122106ccd9STetsuo Handa 	struct tomoyo_name_union dev_name;
4132106ccd9STetsuo Handa 	struct tomoyo_name_union dir_name;
4142106ccd9STetsuo Handa 	struct tomoyo_name_union fs_type;
4152106ccd9STetsuo Handa 	struct tomoyo_number_union flags;
4162106ccd9STetsuo Handa };
4172106ccd9STetsuo Handa 
418a238cf5bSTetsuo Handa /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
419a238cf5bSTetsuo Handa struct tomoyo_acl_param {
420a238cf5bSTetsuo Handa 	char *data;
421a238cf5bSTetsuo Handa 	struct list_head *list;
422a238cf5bSTetsuo Handa 	bool is_delete;
423a238cf5bSTetsuo Handa };
424a238cf5bSTetsuo Handa 
4250d2171d7STetsuo Handa #define TOMOYO_MAX_IO_READ_QUEUE 64
426f23571e8STetsuo Handa 
4272106ccd9STetsuo Handa /*
428f23571e8STetsuo Handa  * Structure for reading/writing policy via /sys/kernel/security/tomoyo
429f23571e8STetsuo Handa  * interfaces.
430c3fa109aSTetsuo Handa  */
4319590837bSKentaro Takeda struct tomoyo_io_buffer {
4328fbe71f0STetsuo Handa 	void (*read) (struct tomoyo_io_buffer *);
4339590837bSKentaro Takeda 	int (*write) (struct tomoyo_io_buffer *);
43417fcfbd9STetsuo Handa 	int (*poll) (struct file *file, poll_table *wait);
4359590837bSKentaro Takeda 	/* Exclusive lock for this structure.   */
4369590837bSKentaro Takeda 	struct mutex io_sem;
437fdb8ebb7STetsuo Handa 	/* Index returned by tomoyo_read_lock(). */
438fdb8ebb7STetsuo Handa 	int reader_idx;
439f23571e8STetsuo Handa 	char __user *read_user_buf;
440f23571e8STetsuo Handa 	int read_user_buf_avail;
441f23571e8STetsuo Handa 	struct {
442f23571e8STetsuo Handa 		struct list_head *domain;
443f23571e8STetsuo Handa 		struct list_head *group;
444f23571e8STetsuo Handa 		struct list_head *acl;
445f23571e8STetsuo Handa 		int avail;
446f23571e8STetsuo Handa 		int step;
447f23571e8STetsuo Handa 		int query_index;
448f23571e8STetsuo Handa 		u16 index;
449f23571e8STetsuo Handa 		u8 bit;
450f23571e8STetsuo Handa 		u8 w_pos;
451f23571e8STetsuo Handa 		bool eof;
452f23571e8STetsuo Handa 		bool print_this_domain_only;
453f23571e8STetsuo Handa 		bool print_execute_only;
454f23571e8STetsuo Handa 		const char *w[TOMOYO_MAX_IO_READ_QUEUE];
455f23571e8STetsuo Handa 	} r;
4560df7e8b8STetsuo Handa 	struct {
4579590837bSKentaro Takeda 		/* The position currently writing to.   */
4580df7e8b8STetsuo Handa 		struct tomoyo_domain_info *domain;
4590df7e8b8STetsuo Handa 		/* Bytes available for writing.         */
4600df7e8b8STetsuo Handa 		int avail;
4610df7e8b8STetsuo Handa 	} w;
4629590837bSKentaro Takeda 	/* Buffer for reading.                  */
4639590837bSKentaro Takeda 	char *read_buf;
4649590837bSKentaro Takeda 	/* Size of read buffer.                 */
4659590837bSKentaro Takeda 	int readbuf_size;
4669590837bSKentaro Takeda 	/* Buffer for writing.                  */
4679590837bSKentaro Takeda 	char *write_buf;
4689590837bSKentaro Takeda 	/* Size of write buffer.                */
4699590837bSKentaro Takeda 	int writebuf_size;
47017fcfbd9STetsuo Handa 	/* Type of this interface.              */
47117fcfbd9STetsuo Handa 	u8 type;
4729590837bSKentaro Takeda };
4739590837bSKentaro Takeda 
47476bb0895STetsuo Handa /*
475b5bc60b4STetsuo Handa  * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
476b5bc60b4STetsuo Handa  * "no_keep_domain" keyword.
47776bb0895STetsuo Handa  */
4785448ec4fSTetsuo Handa struct tomoyo_transition_control {
47982e0f001STetsuo Handa 	struct tomoyo_acl_head head;
4805448ec4fSTetsuo Handa 	u8 type; /* One of values in "enum tomoyo_transition_type".  */
48176bb0895STetsuo Handa 	/* True if the domainname is tomoyo_get_last_name(). */
48276bb0895STetsuo Handa 	bool is_last_name;
4835448ec4fSTetsuo Handa 	const struct tomoyo_path_info *domainname; /* Maybe NULL */
4845448ec4fSTetsuo Handa 	const struct tomoyo_path_info *program;    /* Maybe NULL */
48576bb0895STetsuo Handa };
48676bb0895STetsuo Handa 
487b5bc60b4STetsuo Handa /* Structure for "aggregator" keyword. */
488e2bf6907STetsuo Handa struct tomoyo_aggregator {
48982e0f001STetsuo Handa 	struct tomoyo_acl_head head;
4901084307cSTetsuo Handa 	const struct tomoyo_path_info *original_name;
4911084307cSTetsuo Handa 	const struct tomoyo_path_info *aggregated_name;
4921084307cSTetsuo Handa };
4931084307cSTetsuo Handa 
494b5bc60b4STetsuo Handa /* Structure for policy manager. */
495e2bf6907STetsuo Handa struct tomoyo_manager {
49682e0f001STetsuo Handa 	struct tomoyo_acl_head head;
49782e0f001STetsuo Handa 	bool is_domain;  /* True if manager is a domainname. */
49876bb0895STetsuo Handa 	/* A path to program or a domainname. */
49976bb0895STetsuo Handa 	const struct tomoyo_path_info *manager;
50076bb0895STetsuo Handa };
50176bb0895STetsuo Handa 
50257c2590fSTetsuo Handa struct tomoyo_preference {
50357c2590fSTetsuo Handa 	unsigned int learning_max_entry;
50457c2590fSTetsuo Handa 	bool enforcing_verbose;
50557c2590fSTetsuo Handa 	bool learning_verbose;
50657c2590fSTetsuo Handa 	bool permissive_verbose;
50757c2590fSTetsuo Handa };
50857c2590fSTetsuo Handa 
509b5bc60b4STetsuo Handa /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
51057c2590fSTetsuo Handa struct tomoyo_profile {
51157c2590fSTetsuo Handa 	const struct tomoyo_path_info *comment;
51257c2590fSTetsuo Handa 	struct tomoyo_preference *learning;
51357c2590fSTetsuo Handa 	struct tomoyo_preference *permissive;
51457c2590fSTetsuo Handa 	struct tomoyo_preference *enforcing;
51557c2590fSTetsuo Handa 	struct tomoyo_preference preference;
51657c2590fSTetsuo Handa 	u8 default_config;
51757c2590fSTetsuo Handa 	u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
518d5ca1725STetsuo Handa 	unsigned int pref[TOMOYO_MAX_PREF];
51957c2590fSTetsuo Handa };
52057c2590fSTetsuo Handa 
521eadd99ccSTetsuo Handa /* Structure for representing YYYY/MM/DD hh/mm/ss. */
522eadd99ccSTetsuo Handa struct tomoyo_time {
523eadd99ccSTetsuo Handa 	u16 year;
524eadd99ccSTetsuo Handa 	u8 month;
525eadd99ccSTetsuo Handa 	u8 day;
526eadd99ccSTetsuo Handa 	u8 hour;
527eadd99ccSTetsuo Handa 	u8 min;
528eadd99ccSTetsuo Handa 	u8 sec;
529eadd99ccSTetsuo Handa };
530eadd99ccSTetsuo Handa 
53176bb0895STetsuo Handa /********** Function prototypes. **********/
53276bb0895STetsuo Handa 
533c3ef1500STetsuo Handa bool tomoyo_str_starts(char **src, const char *find);
534c3ef1500STetsuo Handa const char *tomoyo_get_exe(void);
535c3ef1500STetsuo Handa void tomoyo_normalize_line(unsigned char *buffer);
536c3ef1500STetsuo Handa void tomoyo_check_profile(void);
537c3ef1500STetsuo Handa int tomoyo_open_control(const u8 type, struct file *file);
5380df7e8b8STetsuo Handa int tomoyo_close_control(struct tomoyo_io_buffer *head);
5390849e3baSTetsuo Handa int tomoyo_poll_control(struct file *file, poll_table *wait);
5400df7e8b8STetsuo Handa int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
541c3ef1500STetsuo Handa 			const int buffer_len);
5420df7e8b8STetsuo Handa int tomoyo_write_control(struct tomoyo_io_buffer *head,
5430df7e8b8STetsuo Handa 			 const char __user *buffer, const int buffer_len);
544c3ef1500STetsuo Handa bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
545c3ef1500STetsuo Handa void tomoyo_warn_oom(const char *function);
546484ca79cSTetsuo Handa const struct tomoyo_path_info *
547484ca79cSTetsuo Handa tomoyo_compare_name_union(const struct tomoyo_path_info *name,
5487762fbffSTetsuo Handa 			  const struct tomoyo_name_union *ptr);
5492106ccd9STetsuo Handa bool tomoyo_compare_number_union(const unsigned long value,
5502106ccd9STetsuo Handa 				 const struct tomoyo_number_union *ptr);
55157c2590fSTetsuo Handa int tomoyo_get_mode(const u8 profile, const u8 index);
552f23571e8STetsuo Handa void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
5539590837bSKentaro Takeda 	__attribute__ ((format(printf, 2, 3)));
55475093152STetsuo Handa bool tomoyo_correct_domain(const unsigned char *domainname);
55575093152STetsuo Handa bool tomoyo_correct_path(const char *filename);
55675093152STetsuo Handa bool tomoyo_correct_word(const char *string);
55775093152STetsuo Handa bool tomoyo_domain_def(const unsigned char *buffer);
558a238cf5bSTetsuo Handa bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
5597762fbffSTetsuo Handa 			     struct tomoyo_name_union *ptr);
560484ca79cSTetsuo Handa const struct tomoyo_path_info *
561484ca79cSTetsuo Handa tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
562a98aa4deSTetsuo Handa 			  const struct tomoyo_group *group);
5634c3e9e2dSTetsuo Handa bool tomoyo_number_matches_group(const unsigned long min,
5644c3e9e2dSTetsuo Handa 				 const unsigned long max,
565a98aa4deSTetsuo Handa 				 const struct tomoyo_group *group);
5669590837bSKentaro Takeda bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
5679590837bSKentaro Takeda 				 const struct tomoyo_path_info *pattern);
568a238cf5bSTetsuo Handa bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
569a238cf5bSTetsuo Handa 			       struct tomoyo_number_union *ptr);
5707762fbffSTetsuo Handa bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
5719590837bSKentaro Takeda bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
5722106ccd9STetsuo Handa int tomoyo_init_request_info(struct tomoyo_request_info *r,
57357c2590fSTetsuo Handa 			     struct tomoyo_domain_info *domain,
57457c2590fSTetsuo Handa 			     const u8 index);
575b5bc60b4STetsuo Handa int tomoyo_mount_permission(char *dev_name, struct path *path,
576b5bc60b4STetsuo Handa 			    const char *type, unsigned long flags,
577b5bc60b4STetsuo Handa 			    void *data_page);
578a238cf5bSTetsuo Handa int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
579a238cf5bSTetsuo Handa int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
5805448ec4fSTetsuo Handa 				    const u8 type);
581a238cf5bSTetsuo Handa int tomoyo_write_file(struct tomoyo_acl_param *param);
582a238cf5bSTetsuo Handa int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
58317fcfbd9STetsuo Handa int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
58417fcfbd9STetsuo Handa      __attribute__ ((format(printf, 2, 3)));
5859590837bSKentaro Takeda struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
586e2bf6907STetsuo Handa struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
5879590837bSKentaro Takeda 						const u8 profile);
58857c2590fSTetsuo Handa struct tomoyo_profile *tomoyo_profile(const u8 profile);
589a238cf5bSTetsuo Handa struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
590a238cf5bSTetsuo Handa 				      const u8 idx);
5919590837bSKentaro Takeda unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
5929590837bSKentaro Takeda 				const u8 index);
5939590837bSKentaro Takeda void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
5949590837bSKentaro Takeda void tomoyo_load_policy(const char *filename);
5954c3e9e2dSTetsuo Handa void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
596c8c57e84STetsuo Handa char *tomoyo_encode(const char *str);
59776bb0895STetsuo Handa char *tomoyo_realpath_nofollow(const char *pathname);
59876bb0895STetsuo Handa char *tomoyo_realpath_from_path(struct path *path);
59976bb0895STetsuo Handa bool tomoyo_memory_ok(void *ptr);
6009e4b50e9STetsuo Handa void *tomoyo_commit_ok(void *data, const unsigned int size);
60176bb0895STetsuo Handa const struct tomoyo_path_info *tomoyo_get_name(const char *name);
6028fbe71f0STetsuo Handa void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
60376bb0895STetsuo Handa int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
604c3ef1500STetsuo Handa void __init tomoyo_mm_init(void);
60505336deeSTetsuo Handa int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
60676bb0895STetsuo Handa 			   const struct tomoyo_path_info *filename);
60776bb0895STetsuo Handa int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
60876bb0895STetsuo Handa 				 struct path *path, const int flag);
609a1f9bb6aSTetsuo Handa int tomoyo_path_number_perm(const u8 operation, struct path *path,
610a1f9bb6aSTetsuo Handa 			    unsigned long number);
61175093152STetsuo Handa int tomoyo_mkdev_perm(const u8 operation, struct path *path,
612a1f9bb6aSTetsuo Handa 		      const unsigned int mode, unsigned int dev);
61397d6931eSTetsuo Handa int tomoyo_path_perm(const u8 operation, struct path *path);
61497d6931eSTetsuo Handa int tomoyo_path2_perm(const u8 operation, struct path *path1,
61597d6931eSTetsuo Handa 		      struct path *path2);
61676bb0895STetsuo Handa int tomoyo_find_next_domain(struct linux_binprm *bprm);
617a1f9bb6aSTetsuo Handa void tomoyo_print_ulong(char *buffer, const int buffer_len,
618a1f9bb6aSTetsuo Handa 			const unsigned long value, const u8 type);
6197762fbffSTetsuo Handa void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
620847b173eSTetsuo Handa void tomoyo_run_gc(void);
621847b173eSTetsuo Handa void tomoyo_memory_free(void *ptr);
622237ab459STetsuo Handa int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
623a238cf5bSTetsuo Handa 			 struct tomoyo_acl_param *param,
624237ab459STetsuo Handa 			 bool (*check_duplicate) (const struct tomoyo_acl_info
625237ab459STetsuo Handa 						  *,
626237ab459STetsuo Handa 						  const struct tomoyo_acl_info
627237ab459STetsuo Handa 						  *),
628237ab459STetsuo Handa 			 bool (*merge_duplicate) (struct tomoyo_acl_info *,
629237ab459STetsuo Handa 						  struct tomoyo_acl_info *,
630237ab459STetsuo Handa 						  const bool));
63136f5e1ffSTetsuo Handa int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
632a238cf5bSTetsuo Handa 			 struct tomoyo_acl_param *param,
63336f5e1ffSTetsuo Handa 			 bool (*check_duplicate) (const struct tomoyo_acl_head
63436f5e1ffSTetsuo Handa 						  *,
63536f5e1ffSTetsuo Handa 						  const struct tomoyo_acl_head
63636f5e1ffSTetsuo Handa 						  *));
63799a85259STetsuo Handa void tomoyo_check_acl(struct tomoyo_request_info *r,
638484ca79cSTetsuo Handa 		      bool (*check_entry) (struct tomoyo_request_info *,
63999a85259STetsuo Handa 					   const struct tomoyo_acl_info *));
640a238cf5bSTetsuo Handa char *tomoyo_read_token(struct tomoyo_acl_param *param);
641a238cf5bSTetsuo Handa bool tomoyo_permstr(const char *string, const char *keyword);
642237ab459STetsuo Handa 
643eadd99ccSTetsuo Handa const char *tomoyo_yesno(const unsigned int value);
644eadd99ccSTetsuo Handa void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
645eadd99ccSTetsuo Handa 		       va_list args);
646eadd99ccSTetsuo Handa void tomoyo_read_log(struct tomoyo_io_buffer *head);
647eadd99ccSTetsuo Handa int tomoyo_poll_log(struct file *file, poll_table *wait);
648eadd99ccSTetsuo Handa char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
649eadd99ccSTetsuo Handa 		      va_list args);
650eadd99ccSTetsuo Handa 
65176bb0895STetsuo Handa /********** External variable definitions. **********/
65276bb0895STetsuo Handa 
65376bb0895STetsuo Handa /* Lock for GC. */
65476bb0895STetsuo Handa extern struct srcu_struct tomoyo_ss;
65576bb0895STetsuo Handa 
65676bb0895STetsuo Handa /* The list for "struct tomoyo_domain_info". */
65776bb0895STetsuo Handa extern struct list_head tomoyo_domain_list;
65876bb0895STetsuo Handa 
659a230f9e7STetsuo Handa extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
660a230f9e7STetsuo Handa extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
661847b173eSTetsuo Handa extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
662847b173eSTetsuo Handa 
66376bb0895STetsuo Handa /* Lock for protecting policy. */
66476bb0895STetsuo Handa extern struct mutex tomoyo_policy_lock;
66576bb0895STetsuo Handa 
66676bb0895STetsuo Handa /* Has /sbin/init started? */
66776bb0895STetsuo Handa extern bool tomoyo_policy_loaded;
66876bb0895STetsuo Handa 
66976bb0895STetsuo Handa /* The kernel's domain. */
67076bb0895STetsuo Handa extern struct tomoyo_domain_info tomoyo_kernel_domain;
67176bb0895STetsuo Handa 
67271c28236STetsuo Handa extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
67371c28236STetsuo Handa extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
67471c28236STetsuo Handa extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
67571c28236STetsuo Handa extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
67671c28236STetsuo Handa 
6770d2171d7STetsuo Handa extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
6780d2171d7STetsuo Handa extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
6790d2171d7STetsuo Handa extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
6800d2171d7STetsuo Handa 
681eadd99ccSTetsuo Handa extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
682eadd99ccSTetsuo Handa extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
683eadd99ccSTetsuo Handa extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
68417fcfbd9STetsuo Handa 
68576bb0895STetsuo Handa /********** Inlined functions. **********/
68676bb0895STetsuo Handa 
687b5bc60b4STetsuo Handa /**
688b5bc60b4STetsuo Handa  * tomoyo_read_lock - Take lock for protecting policy.
689b5bc60b4STetsuo Handa  *
690b5bc60b4STetsuo Handa  * Returns index number for tomoyo_read_unlock().
691b5bc60b4STetsuo Handa  */
69276bb0895STetsuo Handa static inline int tomoyo_read_lock(void)
69376bb0895STetsuo Handa {
69476bb0895STetsuo Handa 	return srcu_read_lock(&tomoyo_ss);
69576bb0895STetsuo Handa }
69676bb0895STetsuo Handa 
697b5bc60b4STetsuo Handa /**
698b5bc60b4STetsuo Handa  * tomoyo_read_unlock - Release lock for protecting policy.
699b5bc60b4STetsuo Handa  *
700b5bc60b4STetsuo Handa  * @idx: Index number returned by tomoyo_read_lock().
701b5bc60b4STetsuo Handa  *
702b5bc60b4STetsuo Handa  * Returns nothing.
703b5bc60b4STetsuo Handa  */
70476bb0895STetsuo Handa static inline void tomoyo_read_unlock(int idx)
70576bb0895STetsuo Handa {
70676bb0895STetsuo Handa 	srcu_read_unlock(&tomoyo_ss, idx);
70776bb0895STetsuo Handa }
70876bb0895STetsuo Handa 
709b5bc60b4STetsuo Handa /**
710b5bc60b4STetsuo Handa  * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
711b5bc60b4STetsuo Handa  *
712b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_path_info".
713b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_path_info".
714b5bc60b4STetsuo Handa  *
715b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
716b5bc60b4STetsuo Handa  */
7179590837bSKentaro Takeda static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
7189590837bSKentaro Takeda 				  const struct tomoyo_path_info *b)
7199590837bSKentaro Takeda {
7209590837bSKentaro Takeda 	return a->hash != b->hash || strcmp(a->name, b->name);
7219590837bSKentaro Takeda }
7229590837bSKentaro Takeda 
7239590837bSKentaro Takeda /**
724b5bc60b4STetsuo Handa  * tomoyo_put_name - Drop reference on "struct tomoyo_name".
725b5bc60b4STetsuo Handa  *
726b5bc60b4STetsuo Handa  * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
727b5bc60b4STetsuo Handa  *
728b5bc60b4STetsuo Handa  * Returns nothing.
729b5bc60b4STetsuo Handa  */
73076bb0895STetsuo Handa static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
73176bb0895STetsuo Handa {
73276bb0895STetsuo Handa 	if (name) {
733e2bf6907STetsuo Handa 		struct tomoyo_name *ptr =
734e2bf6907STetsuo Handa 			container_of(name, typeof(*ptr), entry);
7350df7e8b8STetsuo Handa 		atomic_dec(&ptr->head.users);
73676bb0895STetsuo Handa 	}
73776bb0895STetsuo Handa }
7389590837bSKentaro Takeda 
739b5bc60b4STetsuo Handa /**
740b5bc60b4STetsuo Handa  * tomoyo_put_group - Drop reference on "struct tomoyo_group".
741b5bc60b4STetsuo Handa  *
742b5bc60b4STetsuo Handa  * @group: Pointer to "struct tomoyo_group". Maybe NULL.
743b5bc60b4STetsuo Handa  *
744b5bc60b4STetsuo Handa  * Returns nothing.
745b5bc60b4STetsuo Handa  */
746a98aa4deSTetsuo Handa static inline void tomoyo_put_group(struct tomoyo_group *group)
7474c3e9e2dSTetsuo Handa {
7484c3e9e2dSTetsuo Handa 	if (group)
7490df7e8b8STetsuo Handa 		atomic_dec(&group->head.users);
7504c3e9e2dSTetsuo Handa }
7514c3e9e2dSTetsuo Handa 
752b5bc60b4STetsuo Handa /**
753b5bc60b4STetsuo Handa  * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
754b5bc60b4STetsuo Handa  *
755b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" for current thread.
756b5bc60b4STetsuo Handa  */
75776bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_domain(void)
75876bb0895STetsuo Handa {
75976bb0895STetsuo Handa 	return current_cred()->security;
76076bb0895STetsuo Handa }
7619590837bSKentaro Takeda 
762b5bc60b4STetsuo Handa /**
763b5bc60b4STetsuo Handa  * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
764b5bc60b4STetsuo Handa  *
765b5bc60b4STetsuo Handa  * @task: Pointer to "struct task_struct".
766b5bc60b4STetsuo Handa  *
767b5bc60b4STetsuo Handa  * Returns pointer to "struct tomoyo_security" for specified thread.
768b5bc60b4STetsuo Handa  */
76976bb0895STetsuo Handa static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
77076bb0895STetsuo Handa 							    *task)
77176bb0895STetsuo Handa {
77276bb0895STetsuo Handa 	return task_cred_xxx(task, security);
77376bb0895STetsuo Handa }
7749590837bSKentaro Takeda 
775b5bc60b4STetsuo Handa /**
776b5bc60b4STetsuo Handa  * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
777b5bc60b4STetsuo Handa  *
778b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_name_union".
779b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_name_union".
780b5bc60b4STetsuo Handa  *
781b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
782b5bc60b4STetsuo Handa  */
78375093152STetsuo Handa static inline bool tomoyo_same_name_union
784b5bc60b4STetsuo Handa (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
7857762fbffSTetsuo Handa {
7860df7e8b8STetsuo Handa 	return a->filename == b->filename && a->group == b->group;
7877762fbffSTetsuo Handa }
7887762fbffSTetsuo Handa 
789b5bc60b4STetsuo Handa /**
790b5bc60b4STetsuo Handa  * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
791b5bc60b4STetsuo Handa  *
792b5bc60b4STetsuo Handa  * @a: Pointer to "struct tomoyo_number_union".
793b5bc60b4STetsuo Handa  * @b: Pointer to "struct tomoyo_number_union".
794b5bc60b4STetsuo Handa  *
795b5bc60b4STetsuo Handa  * Returns true if @a == @b, false otherwise.
796b5bc60b4STetsuo Handa  */
79775093152STetsuo Handa static inline bool tomoyo_same_number_union
798b5bc60b4STetsuo Handa (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
7994c3e9e2dSTetsuo Handa {
800b5bc60b4STetsuo Handa 	return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
8010df7e8b8STetsuo Handa 		a->group == b->group && a->value_type[0] == b->value_type[0] &&
8020df7e8b8STetsuo Handa 		a->value_type[1] == b->value_type[1];
8034c3e9e2dSTetsuo Handa }
8044c3e9e2dSTetsuo Handa 
805eadd99ccSTetsuo Handa #if defined(CONFIG_SLOB)
806eadd99ccSTetsuo Handa 
807eadd99ccSTetsuo Handa /**
808eadd99ccSTetsuo Handa  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
809eadd99ccSTetsuo Handa  *
810eadd99ccSTetsuo Handa  * @size: Size to be rounded up.
811eadd99ccSTetsuo Handa  *
812eadd99ccSTetsuo Handa  * Returns @size.
813eadd99ccSTetsuo Handa  *
814eadd99ccSTetsuo Handa  * Since SLOB does not round up, this function simply returns @size.
815eadd99ccSTetsuo Handa  */
816eadd99ccSTetsuo Handa static inline int tomoyo_round2(size_t size)
817eadd99ccSTetsuo Handa {
818eadd99ccSTetsuo Handa 	return size;
819eadd99ccSTetsuo Handa }
820eadd99ccSTetsuo Handa 
821eadd99ccSTetsuo Handa #else
822eadd99ccSTetsuo Handa 
823eadd99ccSTetsuo Handa /**
824eadd99ccSTetsuo Handa  * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
825eadd99ccSTetsuo Handa  *
826eadd99ccSTetsuo Handa  * @size: Size to be rounded up.
827eadd99ccSTetsuo Handa  *
828eadd99ccSTetsuo Handa  * Returns rounded size.
829eadd99ccSTetsuo Handa  *
830eadd99ccSTetsuo Handa  * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
831eadd99ccSTetsuo Handa  * (e.g.) 128 bytes.
832eadd99ccSTetsuo Handa  */
833eadd99ccSTetsuo Handa static inline int tomoyo_round2(size_t size)
834eadd99ccSTetsuo Handa {
835eadd99ccSTetsuo Handa #if PAGE_SIZE == 4096
836eadd99ccSTetsuo Handa 	size_t bsize = 32;
837eadd99ccSTetsuo Handa #else
838eadd99ccSTetsuo Handa 	size_t bsize = 64;
839eadd99ccSTetsuo Handa #endif
840eadd99ccSTetsuo Handa 	if (!size)
841eadd99ccSTetsuo Handa 		return 0;
842eadd99ccSTetsuo Handa 	while (size > bsize)
843eadd99ccSTetsuo Handa 		bsize <<= 1;
844eadd99ccSTetsuo Handa 	return bsize;
845eadd99ccSTetsuo Handa }
846eadd99ccSTetsuo Handa 
847eadd99ccSTetsuo Handa #endif
848eadd99ccSTetsuo Handa 
8499590837bSKentaro Takeda /**
8509590837bSKentaro Takeda  * list_for_each_cookie - iterate over a list with cookie.
8519590837bSKentaro Takeda  * @pos:        the &struct list_head to use as a loop cursor.
8529590837bSKentaro Takeda  * @head:       the head for your list.
8539590837bSKentaro Takeda  */
854475e6fa3STetsuo Handa #define list_for_each_cookie(pos, head)					\
855475e6fa3STetsuo Handa 	if (!pos)							\
856475e6fa3STetsuo Handa 		pos =  srcu_dereference((head)->next, &tomoyo_ss);	\
857475e6fa3STetsuo Handa 	for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
858fdb8ebb7STetsuo Handa 
8599590837bSKentaro Takeda #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
860