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