]> bbs.cooldavid.org Git - net-next-2.6.git/blob - security/tomoyo/common.h
ec3ed488ee30e966f4963fcfb2c7659cf68f69b9
[net-next-2.6.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 struct linux_binprm;
25
26 /********** Constants definitions. **********/
27
28 /*
29  * TOMOYO uses this hash only when appending a string into the string
30  * table. Frequency of appending strings is very low. So we don't need
31  * large (e.g. 64k) hash size. 256 will be sufficient.
32  */
33 #define TOMOYO_HASH_BITS  8
34 #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
35
36 #define TOMOYO_EXEC_TMPSIZE     4096
37
38 /* Profile number is an integer between 0 and 255. */
39 #define TOMOYO_MAX_PROFILES 256
40
41 enum tomoyo_mode_index {
42         TOMOYO_CONFIG_DISABLED,
43         TOMOYO_CONFIG_LEARNING,
44         TOMOYO_CONFIG_PERMISSIVE,
45         TOMOYO_CONFIG_ENFORCING,
46         TOMOYO_CONFIG_USE_DEFAULT = 255
47 };
48
49 enum tomoyo_policy_id {
50         TOMOYO_ID_GROUP,
51         TOMOYO_ID_PATH_GROUP,
52         TOMOYO_ID_NUMBER_GROUP,
53         TOMOYO_ID_DOMAIN_INITIALIZER,
54         TOMOYO_ID_DOMAIN_KEEPER,
55         TOMOYO_ID_AGGREGATOR,
56         TOMOYO_ID_ALIAS,
57         TOMOYO_ID_GLOBALLY_READABLE,
58         TOMOYO_ID_PATTERN,
59         TOMOYO_ID_NO_REWRITE,
60         TOMOYO_ID_MANAGER,
61         TOMOYO_ID_NAME,
62         TOMOYO_ID_ACL,
63         TOMOYO_ID_DOMAIN,
64         TOMOYO_MAX_POLICY
65 };
66
67 enum tomoyo_group_id {
68         TOMOYO_PATH_GROUP,
69         TOMOYO_NUMBER_GROUP,
70         TOMOYO_MAX_GROUP
71 };
72
73 /* Keywords for ACLs. */
74 #define TOMOYO_KEYWORD_AGGREGATOR                "aggregator "
75 #define TOMOYO_KEYWORD_ALIAS                     "alias "
76 #define TOMOYO_KEYWORD_ALLOW_MOUNT               "allow_mount "
77 #define TOMOYO_KEYWORD_ALLOW_READ                "allow_read "
78 #define TOMOYO_KEYWORD_DELETE                    "delete "
79 #define TOMOYO_KEYWORD_DENY_REWRITE              "deny_rewrite "
80 #define TOMOYO_KEYWORD_FILE_PATTERN              "file_pattern "
81 #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN         "initialize_domain "
82 #define TOMOYO_KEYWORD_KEEP_DOMAIN               "keep_domain "
83 #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN      "no_initialize_domain "
84 #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN            "no_keep_domain "
85 #define TOMOYO_KEYWORD_PATH_GROUP                "path_group "
86 #define TOMOYO_KEYWORD_NUMBER_GROUP              "number_group "
87 #define TOMOYO_KEYWORD_SELECT                    "select "
88 #define TOMOYO_KEYWORD_USE_PROFILE               "use_profile "
89 #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ  "ignore_global_allow_read"
90 #define TOMOYO_KEYWORD_QUOTA_EXCEEDED            "quota_exceeded"
91 #define TOMOYO_KEYWORD_TRANSITION_FAILED         "transition_failed"
92 /* A domain definition starts with <kernel>. */
93 #define TOMOYO_ROOT_NAME                         "<kernel>"
94 #define TOMOYO_ROOT_NAME_LEN                     (sizeof(TOMOYO_ROOT_NAME) - 1)
95
96 /* Value type definition. */
97 #define TOMOYO_VALUE_TYPE_INVALID     0
98 #define TOMOYO_VALUE_TYPE_DECIMAL     1
99 #define TOMOYO_VALUE_TYPE_OCTAL       2
100 #define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
101
102 /* Index numbers for Access Controls. */
103 enum tomoyo_acl_entry_type_index {
104         TOMOYO_TYPE_PATH_ACL,
105         TOMOYO_TYPE_PATH2_ACL,
106         TOMOYO_TYPE_PATH_NUMBER_ACL,
107         TOMOYO_TYPE_MKDEV_ACL,
108         TOMOYO_TYPE_MOUNT_ACL,
109 };
110
111 /* Index numbers for File Controls. */
112
113 /*
114  * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
115  * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
116  * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
117  * TOMOYO_TYPE_READ_WRITE is set.
118  * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
119  * or TOMOYO_TYPE_WRITE is cleared.
120  * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
121  * TOMOYO_TYPE_READ_WRITE is cleared.
122  */
123
124 enum tomoyo_path_acl_index {
125         TOMOYO_TYPE_READ_WRITE,
126         TOMOYO_TYPE_EXECUTE,
127         TOMOYO_TYPE_READ,
128         TOMOYO_TYPE_WRITE,
129         TOMOYO_TYPE_UNLINK,
130         TOMOYO_TYPE_RMDIR,
131         TOMOYO_TYPE_TRUNCATE,
132         TOMOYO_TYPE_SYMLINK,
133         TOMOYO_TYPE_REWRITE,
134         TOMOYO_TYPE_CHROOT,
135         TOMOYO_TYPE_UMOUNT,
136         TOMOYO_MAX_PATH_OPERATION
137 };
138
139 #define TOMOYO_RW_MASK ((1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE))
140
141 enum tomoyo_mkdev_acl_index {
142         TOMOYO_TYPE_MKBLOCK,
143         TOMOYO_TYPE_MKCHAR,
144         TOMOYO_MAX_MKDEV_OPERATION
145 };
146
147 enum tomoyo_path2_acl_index {
148         TOMOYO_TYPE_LINK,
149         TOMOYO_TYPE_RENAME,
150         TOMOYO_TYPE_PIVOT_ROOT,
151         TOMOYO_MAX_PATH2_OPERATION
152 };
153
154 enum tomoyo_path_number_acl_index {
155         TOMOYO_TYPE_CREATE,
156         TOMOYO_TYPE_MKDIR,
157         TOMOYO_TYPE_MKFIFO,
158         TOMOYO_TYPE_MKSOCK,
159         TOMOYO_TYPE_IOCTL,
160         TOMOYO_TYPE_CHMOD,
161         TOMOYO_TYPE_CHOWN,
162         TOMOYO_TYPE_CHGRP,
163         TOMOYO_MAX_PATH_NUMBER_OPERATION
164 };
165
166 enum tomoyo_securityfs_interface_index {
167         TOMOYO_DOMAINPOLICY,
168         TOMOYO_EXCEPTIONPOLICY,
169         TOMOYO_DOMAIN_STATUS,
170         TOMOYO_PROCESS_STATUS,
171         TOMOYO_MEMINFO,
172         TOMOYO_SELFDOMAIN,
173         TOMOYO_VERSION,
174         TOMOYO_PROFILE,
175         TOMOYO_QUERY,
176         TOMOYO_MANAGER
177 };
178
179 enum tomoyo_mac_index {
180         TOMOYO_MAC_FILE_EXECUTE,
181         TOMOYO_MAC_FILE_OPEN,
182         TOMOYO_MAC_FILE_CREATE,
183         TOMOYO_MAC_FILE_UNLINK,
184         TOMOYO_MAC_FILE_MKDIR,
185         TOMOYO_MAC_FILE_RMDIR,
186         TOMOYO_MAC_FILE_MKFIFO,
187         TOMOYO_MAC_FILE_MKSOCK,
188         TOMOYO_MAC_FILE_TRUNCATE,
189         TOMOYO_MAC_FILE_SYMLINK,
190         TOMOYO_MAC_FILE_REWRITE,
191         TOMOYO_MAC_FILE_MKBLOCK,
192         TOMOYO_MAC_FILE_MKCHAR,
193         TOMOYO_MAC_FILE_LINK,
194         TOMOYO_MAC_FILE_RENAME,
195         TOMOYO_MAC_FILE_CHMOD,
196         TOMOYO_MAC_FILE_CHOWN,
197         TOMOYO_MAC_FILE_CHGRP,
198         TOMOYO_MAC_FILE_IOCTL,
199         TOMOYO_MAC_FILE_CHROOT,
200         TOMOYO_MAC_FILE_MOUNT,
201         TOMOYO_MAC_FILE_UMOUNT,
202         TOMOYO_MAC_FILE_PIVOT_ROOT,
203         TOMOYO_MAX_MAC_INDEX
204 };
205
206 enum tomoyo_mac_category_index {
207         TOMOYO_MAC_CATEGORY_FILE,
208         TOMOYO_MAX_MAC_CATEGORY_INDEX
209 };
210
211 #define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
212
213 /********** Structure definitions. **********/
214
215 /*
216  * tomoyo_acl_head is a structure which is used for holding elements not in
217  * domain policy.
218  * It has following fields.
219  *
220  *  (1) "list" which is linked to tomoyo_policy_list[] .
221  *  (2) "is_deleted" is a bool which is true if marked as deleted, false
222  *      otherwise.
223  */
224 struct tomoyo_acl_head {
225         struct list_head list;
226         bool is_deleted;
227 } __packed;
228
229 /*
230  * tomoyo_request_info is a structure which is used for holding
231  *
232  * (1) Domain information of current process.
233  * (2) How many retries are made for this request.
234  * (3) Profile number used for this request.
235  * (4) Access control mode of the profile.
236  */
237 struct tomoyo_request_info {
238         struct tomoyo_domain_info *domain;
239         /* For holding parameters. */
240         union {
241                 struct {
242                         const struct tomoyo_path_info *filename;
243                         u8 operation;
244                 } path;
245                 struct {
246                         const struct tomoyo_path_info *filename1;
247                         const struct tomoyo_path_info *filename2;
248                         u8 operation;
249                 } path2;
250                 struct {
251                         const struct tomoyo_path_info *filename;
252                         unsigned int mode;
253                         unsigned int major;
254                         unsigned int minor;
255                         u8 operation;
256                 } mkdev;
257                 struct {
258                         const struct tomoyo_path_info *filename;
259                         unsigned long number;
260                         u8 operation;
261                 } path_number;
262                 struct {
263                         const struct tomoyo_path_info *type;
264                         const struct tomoyo_path_info *dir;
265                         const struct tomoyo_path_info *dev;
266                         unsigned long flags;
267                         int need_dev;
268                 } mount;
269         } param;
270         u8 param_type;
271         bool granted;
272         u8 retry;
273         u8 profile;
274         u8 mode; /* One of tomoyo_mode_index . */
275         u8 type;
276 };
277
278 /*
279  * tomoyo_path_info is a structure which is used for holding a string data
280  * used by TOMOYO.
281  * This structure has several fields for supporting pattern matching.
282  *
283  * (1) "name" is the '\0' terminated string data.
284  * (2) "hash" is full_name_hash(name, strlen(name)).
285  *     This allows tomoyo_pathcmp() to compare by hash before actually compare
286  *     using strcmp().
287  * (3) "const_len" is the length of the initial segment of "name" which
288  *     consists entirely of non wildcard characters. In other words, the length
289  *     which we can compare two strings using strncmp().
290  * (4) "is_dir" is a bool which is true if "name" ends with "/",
291  *     false otherwise.
292  *     TOMOYO distinguishes directory and non-directory. A directory ends with
293  *     "/" and non-directory does not end with "/".
294  * (5) "is_patterned" is a bool which is true if "name" contains wildcard
295  *     characters, false otherwise. This allows TOMOYO to use "hash" and
296  *     strcmp() for string comparison if "is_patterned" is false.
297  */
298 struct tomoyo_path_info {
299         const char *name;
300         u32 hash;          /* = full_name_hash(name, strlen(name)) */
301         u16 const_len;     /* = tomoyo_const_part_length(name)     */
302         bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
303         bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
304 };
305
306 /*
307  * tomoyo_name_entry is a structure which is used for linking
308  * "struct tomoyo_path_info" into tomoyo_name_list .
309  */
310 struct tomoyo_name_entry {
311         struct list_head list;
312         atomic_t users;
313         struct tomoyo_path_info entry;
314 };
315
316 struct tomoyo_name_union {
317         const struct tomoyo_path_info *filename;
318         struct tomoyo_group *group;
319         u8 is_group;
320 };
321
322 struct tomoyo_number_union {
323         unsigned long values[2];
324         struct tomoyo_group *group;
325         u8 min_type;
326         u8 max_type;
327         u8 is_group;
328 };
329
330 /* Structure for "path_group"/"number_group" directive. */
331 struct tomoyo_group {
332         struct list_head list;
333         const struct tomoyo_path_info *group_name;
334         struct list_head member_list;
335         atomic_t users;
336 };
337
338 /* Structure for "path_group" directive. */
339 struct tomoyo_path_group {
340         struct tomoyo_acl_head head;
341         const struct tomoyo_path_info *member_name;
342 };
343
344 /* Structure for "number_group" directive. */
345 struct tomoyo_number_group {
346         struct tomoyo_acl_head head;
347         struct tomoyo_number_union number;
348 };
349
350 /*
351  * tomoyo_acl_info is a structure which is used for holding
352  *
353  *  (1) "list" which is linked to the ->acl_info_list of
354  *      "struct tomoyo_domain_info"
355  *  (2) "is_deleted" is a bool which is true if this domain is marked as
356  *      "deleted", false otherwise.
357  *  (3) "type" which tells type of the entry.
358  *
359  * Packing "struct tomoyo_acl_info" allows
360  * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
361  * "struct tomoyo_path_number_acl" "struct tomoyo_mkdev_acl" to embed
362  * "u8" without enlarging their structure size.
363  */
364 struct tomoyo_acl_info {
365         struct list_head list;
366         bool is_deleted;
367         u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
368 } __packed;
369
370 /*
371  * tomoyo_domain_info is a structure which is used for holding permissions
372  * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
373  * It has following fields.
374  *
375  *  (1) "list" which is linked to tomoyo_domain_list .
376  *  (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
377  *  (3) "domainname" which holds the name of the domain.
378  *  (4) "profile" which remembers profile number assigned to this domain.
379  *  (5) "is_deleted" is a bool which is true if this domain is marked as
380  *      "deleted", false otherwise.
381  *  (6) "quota_warned" is a bool which is used for suppressing warning message
382  *      when learning mode learned too much entries.
383  *  (7) "ignore_global_allow_read" is a bool which is true if this domain
384  *      should ignore "allow_read" directive in exception policy.
385  *  (8) "transition_failed" is a bool which is set to true when this domain was
386  *      unable to create a new domain at tomoyo_find_next_domain() because the
387  *      name of the domain to be created was too long or it could not allocate
388  *      memory. If set to true, more than one process continued execve()
389  *      without domain transition.
390  *  (9) "users" is an atomic_t that holds how many "struct cred"->security
391  *      are referring this "struct tomoyo_domain_info". If is_deleted == true
392  *      and users == 0, this struct will be kfree()d upon next garbage
393  *      collection.
394  *
395  * A domain's lifecycle is an analogy of files on / directory.
396  * Multiple domains with the same domainname cannot be created (as with
397  * creating files with the same filename fails with -EEXIST).
398  * If a process reached a domain, that process can reside in that domain after
399  * that domain is marked as "deleted" (as with a process can access an already
400  * open()ed file after that file was unlink()ed).
401  */
402 struct tomoyo_domain_info {
403         struct list_head list;
404         struct list_head acl_info_list;
405         /* Name of this domain. Never NULL.          */
406         const struct tomoyo_path_info *domainname;
407         u8 profile;        /* Profile number to use. */
408         bool is_deleted;   /* Delete flag.           */
409         bool quota_warned; /* Quota warnning flag.   */
410         bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
411         bool transition_failed; /* Domain transition failed flag. */
412         atomic_t users; /* Number of referring credentials. */
413 };
414
415 /*
416  * tomoyo_path_acl is a structure which is used for holding an
417  * entry with one pathname operation (e.g. open(), mkdir()).
418  * It has following fields.
419  *
420  *  (1) "head" which is a "struct tomoyo_acl_info".
421  *  (2) "perm" which is a bitmask of permitted operations.
422  *  (3) "name" is the pathname.
423  *
424  * Directives held by this structure are "allow_read/write", "allow_execute",
425  * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
426  * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
427  * "allow_unmount".
428  */
429 struct tomoyo_path_acl {
430         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
431         u16 perm;
432         struct tomoyo_name_union name;
433 };
434
435 /*
436  * tomoyo_path_number_acl is a structure which is used for holding an
437  * entry with one pathname and one number operation.
438  * It has following fields.
439  *
440  *  (1) "head" which is a "struct tomoyo_acl_info".
441  *  (2) "perm" which is a bitmask of permitted operations.
442  *  (3) "name" is the pathname.
443  *  (4) "number" is the numeric value.
444  *
445  * Directives held by this structure are "allow_create", "allow_mkdir",
446  * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
447  * and "allow_chgrp".
448  *
449  */
450 struct tomoyo_path_number_acl {
451         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
452         u8 perm;
453         struct tomoyo_name_union name;
454         struct tomoyo_number_union number;
455 };
456
457 /*
458  * tomoyo_mkdev_acl is a structure which is used for holding an
459  * entry with one pathname and three numbers operation.
460  * It has following fields.
461  *
462  *  (1) "head" which is a "struct tomoyo_acl_info".
463  *  (2) "perm" which is a bitmask of permitted operations.
464  *  (3) "mode" is the create mode.
465  *  (4) "major" is the major number of device node.
466  *  (5) "minor" is the minor number of device node.
467  *
468  * Directives held by this structure are "allow_mkchar", "allow_mkblock".
469  *
470  */
471 struct tomoyo_mkdev_acl {
472         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
473         u8 perm;
474         struct tomoyo_name_union name;
475         struct tomoyo_number_union mode;
476         struct tomoyo_number_union major;
477         struct tomoyo_number_union minor;
478 };
479
480 /*
481  * tomoyo_path2_acl is a structure which is used for holding an
482  * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
483  * It has following fields.
484  *
485  *  (1) "head" which is a "struct tomoyo_acl_info".
486  *  (2) "perm" which is a bitmask of permitted operations.
487  *  (3) "name1" is the source/old pathname.
488  *  (4) "name2" is the destination/new pathname.
489  *
490  * Directives held by this structure are "allow_rename", "allow_link" and
491  * "allow_pivot_root".
492  */
493 struct tomoyo_path2_acl {
494         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
495         u8 perm;
496         struct tomoyo_name_union name1;
497         struct tomoyo_name_union name2;
498 };
499
500 /*
501  * tomoyo_mount_acl is a structure which is used for holding an
502  * entry for mount operation.
503  * It has following fields.
504  *
505  *  (1) "head" which is a "struct tomoyo_acl_info".
506  *  (2) "dev_name" is the device name.
507  *  (3) "dir_name" is the mount point.
508  *  (4) "fs_type" is the filesystem type.
509  *  (5) "flags" is the mount flags.
510  *
511  * Directive held by this structure is "allow_mount".
512  */
513 struct tomoyo_mount_acl {
514         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
515         struct tomoyo_name_union dev_name;
516         struct tomoyo_name_union dir_name;
517         struct tomoyo_name_union fs_type;
518         struct tomoyo_number_union flags;
519 };
520
521 /*
522  * tomoyo_io_buffer is a structure which is used for reading and modifying
523  * configuration via /sys/kernel/security/tomoyo/ interface.
524  * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
525  * cursors.
526  *
527  * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
528  * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
529  * entry has a list of "struct tomoyo_acl_info", we need two cursors when
530  * reading (one is for traversing tomoyo_domain_list and the other is for
531  * traversing "struct tomoyo_acl_info"->acl_info_list ).
532  *
533  * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
534  * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
535  * domain with the domainname specified by the rest of that line (NULL is set
536  * if seek failed).
537  * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
538  * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
539  * line (->write_var1 is set to NULL if a domain was deleted).
540  * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
541  * neither "select " nor "delete ", an entry or a domain specified by that line
542  * is appended.
543  */
544 struct tomoyo_io_buffer {
545         void (*read) (struct tomoyo_io_buffer *);
546         int (*write) (struct tomoyo_io_buffer *);
547         int (*poll) (struct file *file, poll_table *wait);
548         /* Exclusive lock for this structure.   */
549         struct mutex io_sem;
550         /* Index returned by tomoyo_read_lock(). */
551         int reader_idx;
552         /* The position currently reading from. */
553         struct list_head *read_var1;
554         /* Extra variables for reading.         */
555         struct list_head *read_var2;
556         /* The position currently writing to.   */
557         struct tomoyo_domain_info *write_var1;
558         /* The step for reading.                */
559         int read_step;
560         /* Buffer for reading.                  */
561         char *read_buf;
562         /* EOF flag for reading.                */
563         bool read_eof;
564         /* Read domain ACL of specified PID?    */
565         bool read_single_domain;
566         /* Extra variable for reading.          */
567         u8 read_bit;
568         /* Bytes available for reading.         */
569         int read_avail;
570         /* Size of read buffer.                 */
571         int readbuf_size;
572         /* Buffer for writing.                  */
573         char *write_buf;
574         /* Bytes available for writing.         */
575         int write_avail;
576         /* Size of write buffer.                */
577         int writebuf_size;
578         /* Type of this interface.              */
579         u8 type;
580 };
581
582 /*
583  * tomoyo_globally_readable_file_entry is a structure which is used for holding
584  * "allow_read" entries.
585  * It has following fields.
586  *
587  *  (1) "head" is "struct tomoyo_acl_head".
588  *  (2) "filename" is a pathname which is allowed to open(O_RDONLY).
589  */
590 struct tomoyo_globally_readable_file_entry {
591         struct tomoyo_acl_head head;
592         const struct tomoyo_path_info *filename;
593 };
594
595 /*
596  * tomoyo_pattern_entry is a structure which is used for holding
597  * "file_pattern" entries.
598  * It has following fields.
599  *
600  *  (1) "head" is "struct tomoyo_acl_head".
601  *  (2) "pattern" is a pathname pattern which is used for converting pathnames
602  *      to pathname patterns during learning mode.
603  */
604 struct tomoyo_pattern_entry {
605         struct tomoyo_acl_head head;
606         const struct tomoyo_path_info *pattern;
607 };
608
609 /*
610  * tomoyo_no_rewrite_entry is a structure which is used for holding
611  * "deny_rewrite" entries.
612  * It has following fields.
613  *
614  *  (1) "head" is "struct tomoyo_acl_head".
615  *  (2) "pattern" is a pathname which is by default not permitted to modify
616  *      already existing content.
617  */
618 struct tomoyo_no_rewrite_entry {
619         struct tomoyo_acl_head head;
620         const struct tomoyo_path_info *pattern;
621 };
622
623 /*
624  * tomoyo_domain_initializer_entry is a structure which is used for holding
625  * "initialize_domain" and "no_initialize_domain" entries.
626  * It has following fields.
627  *
628  *  (1) "head" is "struct tomoyo_acl_head".
629  *  (2) "is_not" is a bool which is true if "no_initialize_domain", false
630  *      otherwise.
631  *  (3) "is_last_name" is a bool which is true if "domainname" is "the last
632  *      component of a domainname", false otherwise.
633  *  (4) "domainname" which is "a domainname" or "the last component of a
634  *      domainname". This field is NULL if "from" clause is not specified.
635  *  (5) "program" which is a program's pathname.
636  */
637 struct tomoyo_domain_initializer_entry {
638         struct tomoyo_acl_head head;
639         bool is_not;       /* True if this entry is "no_initialize_domain".  */
640         /* True if the domainname is tomoyo_get_last_name(). */
641         bool is_last_name;
642         const struct tomoyo_path_info *domainname;    /* This may be NULL */
643         const struct tomoyo_path_info *program;
644 };
645
646 /*
647  * tomoyo_domain_keeper_entry is a structure which is used for holding
648  * "keep_domain" and "no_keep_domain" entries.
649  * It has following fields.
650  *
651  *  (1) "head" is "struct tomoyo_acl_head".
652  *  (2) "is_not" is a bool which is true if "no_initialize_domain", false
653  *      otherwise.
654  *  (3) "is_last_name" is a bool which is true if "domainname" is "the last
655  *      component of a domainname", false otherwise.
656  *  (4) "domainname" which is "a domainname" or "the last component of a
657  *      domainname".
658  *  (5) "program" which is a program's pathname.
659  *      This field is NULL if "from" clause is not specified.
660  */
661 struct tomoyo_domain_keeper_entry {
662         struct tomoyo_acl_head head;
663         bool is_not;       /* True if this entry is "no_keep_domain".        */
664         /* True if the domainname is tomoyo_get_last_name(). */
665         bool is_last_name;
666         const struct tomoyo_path_info *domainname;
667         const struct tomoyo_path_info *program;       /* This may be NULL */
668 };
669
670 /*
671  * tomoyo_aggregator_entry is a structure which is used for holding
672  * "aggregator" entries.
673  * It has following fields.
674  *
675  *  (1) "head" is "struct tomoyo_acl_head".
676  *  (2) "original_name" which is originally requested name.
677  *  (3) "aggregated_name" which is name to rewrite.
678  */
679 struct tomoyo_aggregator_entry {
680         struct tomoyo_acl_head head;
681         const struct tomoyo_path_info *original_name;
682         const struct tomoyo_path_info *aggregated_name;
683 };
684
685 /*
686  * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
687  * It has following fields.
688  *
689  *  (1) "head" is "struct tomoyo_acl_head".
690  *  (2) "original_name" which is a dereferenced pathname.
691  *  (3) "aliased_name" which is a symlink's pathname.
692  */
693 struct tomoyo_alias_entry {
694         struct tomoyo_acl_head head;
695         const struct tomoyo_path_info *original_name;
696         const struct tomoyo_path_info *aliased_name;
697 };
698
699 /*
700  * tomoyo_policy_manager_entry is a structure which is used for holding list of
701  * domainnames or programs which are permitted to modify configuration via
702  * /sys/kernel/security/tomoyo/ interface.
703  * It has following fields.
704  *
705  *  (1) "head" is "struct tomoyo_acl_head".
706  *  (2) "is_domain" is a bool which is true if "manager" is a domainname, false
707  *      otherwise.
708  *  (3) "manager" is a domainname or a program's pathname.
709  */
710 struct tomoyo_policy_manager_entry {
711         struct tomoyo_acl_head head;
712         bool is_domain;  /* True if manager is a domainname. */
713         /* A path to program or a domainname. */
714         const struct tomoyo_path_info *manager;
715 };
716
717 struct tomoyo_preference {
718         unsigned int learning_max_entry;
719         bool enforcing_verbose;
720         bool learning_verbose;
721         bool permissive_verbose;
722 };
723
724 struct tomoyo_profile {
725         const struct tomoyo_path_info *comment;
726         struct tomoyo_preference *learning;
727         struct tomoyo_preference *permissive;
728         struct tomoyo_preference *enforcing;
729         struct tomoyo_preference preference;
730         u8 default_config;
731         u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
732 };
733
734 /********** Function prototypes. **********/
735
736 extern asmlinkage long sys_getpid(void);
737 extern asmlinkage long sys_getppid(void);
738
739 /* Check whether the given string starts with the given keyword. */
740 bool tomoyo_str_starts(char **src, const char *find);
741 /* Get tomoyo_realpath() of current process. */
742 const char *tomoyo_get_exe(void);
743 /* Format string. */
744 void tomoyo_normalize_line(unsigned char *buffer);
745 /* Print warning or error message on console. */
746 void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
747      __attribute__ ((format(printf, 2, 3)));
748 /* Check all profiles currently assigned to domains are defined. */
749 void tomoyo_check_profile(void);
750 /* Open operation for /sys/kernel/security/tomoyo/ interface. */
751 int tomoyo_open_control(const u8 type, struct file *file);
752 /* Close /sys/kernel/security/tomoyo/ interface. */
753 int tomoyo_close_control(struct file *file);
754 /* Read operation for /sys/kernel/security/tomoyo/ interface. */
755 int tomoyo_read_control(struct file *file, char __user *buffer,
756                         const int buffer_len);
757 /* Write operation for /sys/kernel/security/tomoyo/ interface. */
758 int tomoyo_write_control(struct file *file, const char __user *buffer,
759                          const int buffer_len);
760 /* Check whether the domain has too many ACL entries to hold. */
761 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
762 /* Print out of memory warning message. */
763 void tomoyo_warn_oom(const char *function);
764 /* Check whether the given name matches the given name_union. */
765 bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
766                                const struct tomoyo_name_union *ptr);
767 /* Check whether the given number matches the given number_union. */
768 bool tomoyo_compare_number_union(const unsigned long value,
769                                  const struct tomoyo_number_union *ptr);
770 int tomoyo_get_mode(const u8 profile, const u8 index);
771 /* Transactional sprintf() for policy dump. */
772 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
773         __attribute__ ((format(printf, 2, 3)));
774 /* Check whether the domainname is correct. */
775 bool tomoyo_correct_domain(const unsigned char *domainname);
776 /* Check whether the token is correct. */
777 bool tomoyo_correct_path(const char *filename);
778 bool tomoyo_correct_word(const char *string);
779 /* Check whether the token can be a domainname. */
780 bool tomoyo_domain_def(const unsigned char *buffer);
781 bool tomoyo_parse_name_union(const char *filename,
782                              struct tomoyo_name_union *ptr);
783 /* Check whether the given filename matches the given path_group. */
784 bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
785                                const struct tomoyo_group *group);
786 /* Check whether the given value matches the given number_group. */
787 bool tomoyo_number_matches_group(const unsigned long min,
788                                  const unsigned long max,
789                                  const struct tomoyo_group *group);
790 /* Check whether the given filename matches the given pattern. */
791 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
792                                  const struct tomoyo_path_info *pattern);
793
794 bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
795                                const struct tomoyo_number_union *ptr);
796 bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
797 /* Tokenize a line. */
798 bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
799 /* Write domain policy violation warning message to console? */
800 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
801 /* Get the last component of the given domainname. */
802 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
803 /* Fill "struct tomoyo_request_info". */
804 int tomoyo_init_request_info(struct tomoyo_request_info *r,
805                              struct tomoyo_domain_info *domain,
806                              const u8 index);
807 /* Check permission for mount operation. */
808 int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
809                             unsigned long flags, void *data_page);
810 /* Create "aggregator" entry in exception policy. */
811 int tomoyo_write_aggregator_policy(char *data, const bool is_delete);
812 /* Create "alias" entry in exception policy. */
813 int tomoyo_write_alias_policy(char *data, const bool is_delete);
814 /*
815  * Create "initialize_domain" and "no_initialize_domain" entry
816  * in exception policy.
817  */
818 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
819                                            const bool is_delete);
820 /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
821 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
822                                       const bool is_delete);
823 /*
824  * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
825  * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
826  * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
827  * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
828  * "allow_link" entry in domain policy.
829  */
830 int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
831                              const bool is_delete);
832 /* Create "allow_read" entry in exception policy. */
833 int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
834 /* Create "allow_mount" entry in domain policy. */
835 int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
836                               const bool is_delete);
837 /* Create "deny_rewrite" entry in exception policy. */
838 int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
839 /* Create "file_pattern" entry in exception policy. */
840 int tomoyo_write_pattern_policy(char *data, const bool is_delete);
841 /* Create "path_group"/"number_group" entry in exception policy. */
842 int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
843 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
844      __attribute__ ((format(printf, 2, 3)));
845 /* Find a domain by the given name. */
846 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
847 /* Find or create a domain by the given name. */
848 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
849                                                             domainname,
850                                                             const u8 profile);
851 struct tomoyo_profile *tomoyo_profile(const u8 profile);
852 /*
853  * Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
854  */
855 struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
856
857 /* Check mode for specified functionality. */
858 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
859                                 const u8 index);
860 /* Fill in "struct tomoyo_path_info" members. */
861 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
862 /* Run policy loader when /sbin/init starts. */
863 void tomoyo_load_policy(const char *filename);
864
865 void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
866
867 /* Convert binary string to ascii string. */
868 char *tomoyo_encode(const char *str);
869
870 /*
871  * Returns realpath(3) of the given pathname but ignores chroot'ed root.
872  * These functions use kzalloc(), so the caller must call kfree()
873  * if these functions didn't return NULL.
874  */
875 char *tomoyo_realpath(const char *pathname);
876 /*
877  * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
878  */
879 char *tomoyo_realpath_nofollow(const char *pathname);
880 /* Same with tomoyo_realpath() except that the pathname is already solved. */
881 char *tomoyo_realpath_from_path(struct path *path);
882 /* Get patterned pathname. */
883 const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
884
885 /* Check memory quota. */
886 bool tomoyo_memory_ok(void *ptr);
887 void *tomoyo_commit_ok(void *data, const unsigned int size);
888
889 /*
890  * Keep the given name on the RAM.
891  * The RAM is shared, so NEVER try to modify or kfree() the returned name.
892  */
893 const struct tomoyo_path_info *tomoyo_get_name(const char *name);
894
895 /* Check for memory usage. */
896 void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
897
898 /* Set memory quota. */
899 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
900
901 /* Initialize mm related code. */
902 void __init tomoyo_mm_init(void);
903 int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
904                            const struct tomoyo_path_info *filename);
905 int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
906                                  struct path *path, const int flag);
907 int tomoyo_path_number_perm(const u8 operation, struct path *path,
908                             unsigned long number);
909 int tomoyo_mkdev_perm(const u8 operation, struct path *path,
910                       const unsigned int mode, unsigned int dev);
911 int tomoyo_path_perm(const u8 operation, struct path *path);
912 int tomoyo_path2_perm(const u8 operation, struct path *path1,
913                       struct path *path2);
914 int tomoyo_find_next_domain(struct linux_binprm *bprm);
915
916 void tomoyo_print_ulong(char *buffer, const int buffer_len,
917                         const unsigned long value, const u8 type);
918
919 /* Drop refcount on tomoyo_name_union. */
920 void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
921
922 /* Run garbage collector. */
923 void tomoyo_run_gc(void);
924
925 void tomoyo_memory_free(void *ptr);
926
927 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
928                          bool is_delete, struct tomoyo_domain_info *domain,
929                          bool (*check_duplicate) (const struct tomoyo_acl_info
930                                                   *,
931                                                   const struct tomoyo_acl_info
932                                                   *),
933                          bool (*merge_duplicate) (struct tomoyo_acl_info *,
934                                                   struct tomoyo_acl_info *,
935                                                   const bool));
936 int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
937                          bool is_delete, struct list_head *list,
938                          bool (*check_duplicate) (const struct tomoyo_acl_head
939                                                   *,
940                                                   const struct tomoyo_acl_head
941                                                   *));
942 void tomoyo_check_acl(struct tomoyo_request_info *r,
943                       bool (*check_entry) (const struct tomoyo_request_info *,
944                                            const struct tomoyo_acl_info *));
945
946 /********** External variable definitions. **********/
947
948 /* Lock for GC. */
949 extern struct srcu_struct tomoyo_ss;
950
951 /* The list for "struct tomoyo_domain_info". */
952 extern struct list_head tomoyo_domain_list;
953
954 extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
955 extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
956 extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
957
958 /* Lock for protecting policy. */
959 extern struct mutex tomoyo_policy_lock;
960
961 /* Has /sbin/init started? */
962 extern bool tomoyo_policy_loaded;
963
964 /* The kernel's domain. */
965 extern struct tomoyo_domain_info tomoyo_kernel_domain;
966
967 extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
968 extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
969 extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
970 extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
971
972 extern unsigned int tomoyo_quota_for_query;
973 extern unsigned int tomoyo_query_memory_size;
974
975 /********** Inlined functions. **********/
976
977 static inline int tomoyo_read_lock(void)
978 {
979         return srcu_read_lock(&tomoyo_ss);
980 }
981
982 static inline void tomoyo_read_unlock(int idx)
983 {
984         srcu_read_unlock(&tomoyo_ss, idx);
985 }
986
987 /* strcmp() for "struct tomoyo_path_info" structure. */
988 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
989                                   const struct tomoyo_path_info *b)
990 {
991         return a->hash != b->hash || strcmp(a->name, b->name);
992 }
993
994 /**
995  * tomoyo_valid - Check whether the character is a valid char.
996  *
997  * @c: The character to check.
998  *
999  * Returns true if @c is a valid character, false otherwise.
1000  */
1001 static inline bool tomoyo_valid(const unsigned char c)
1002 {
1003         return c > ' ' && c < 127;
1004 }
1005
1006 /**
1007  * tomoyo_invalid - Check whether the character is an invalid char.
1008  *
1009  * @c: The character to check.
1010  *
1011  * Returns true if @c is an invalid character, false otherwise.
1012  */
1013 static inline bool tomoyo_invalid(const unsigned char c)
1014 {
1015         return c && (c <= ' ' || c >= 127);
1016 }
1017
1018 static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
1019 {
1020         if (name) {
1021                 struct tomoyo_name_entry *ptr =
1022                         container_of(name, struct tomoyo_name_entry, entry);
1023                 atomic_dec(&ptr->users);
1024         }
1025 }
1026
1027 static inline void tomoyo_put_group(struct tomoyo_group *group)
1028 {
1029         if (group)
1030                 atomic_dec(&group->users);
1031 }
1032
1033 static inline struct tomoyo_domain_info *tomoyo_domain(void)
1034 {
1035         return current_cred()->security;
1036 }
1037
1038 static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
1039                                                             *task)
1040 {
1041         return task_cred_xxx(task, security);
1042 }
1043
1044 static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
1045                                            const struct tomoyo_acl_info *p2)
1046 {
1047         return p1->type == p2->type;
1048 }
1049
1050 static inline bool tomoyo_same_name_union
1051 (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
1052 {
1053         return p1->filename == p2->filename && p1->group == p2->group &&
1054                 p1->is_group == p2->is_group;
1055 }
1056
1057 static inline bool tomoyo_same_number_union
1058 (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
1059 {
1060         return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
1061                 && p1->group == p2->group && p1->min_type == p2->min_type &&
1062                 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
1063 }
1064
1065 /**
1066  * list_for_each_cookie - iterate over a list with cookie.
1067  * @pos:        the &struct list_head to use as a loop cursor.
1068  * @cookie:     the &struct list_head to use as a cookie.
1069  * @head:       the head for your list.
1070  *
1071  * Same with list_for_each_rcu() except that this primitive uses @cookie
1072  * so that we can continue iteration.
1073  * @cookie must be NULL when iteration starts, and @cookie will become
1074  * NULL when iteration finishes.
1075  */
1076 #define list_for_each_cookie(pos, cookie, head)                         \
1077         for (({ if (!cookie)                                            \
1078                                      cookie = head; }),                 \
1079                      pos = rcu_dereference((cookie)->next);             \
1080              prefetch(pos->next), pos != (head) || ((cookie) = NULL);   \
1081              (cookie) = pos, pos = rcu_dereference(pos->next))
1082
1083 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */