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