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