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