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