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