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