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