2 * security/tomoyo/common.h
4 * Header file for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #ifndef _SECURITY_TOMOYO_COMMON_H
10 #define _SECURITY_TOMOYO_COMMON_H
12 #include <linux/ctype.h>
13 #include <linux/string.h>
15 #include <linux/file.h>
16 #include <linux/kmod.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>
26 /********** Constants definitions. **********/
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.
33 #define TOMOYO_HASH_BITS 8
34 #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
36 #define TOMOYO_EXEC_TMPSIZE 4096
38 /* Profile number is an integer between 0 and 255. */
39 #define TOMOYO_MAX_PROFILES 256
41 enum tomoyo_mode_index {
42 TOMOYO_CONFIG_DISABLED,
43 TOMOYO_CONFIG_LEARNING,
44 TOMOYO_CONFIG_PERMISSIVE,
45 TOMOYO_CONFIG_ENFORCING
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)
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
76 /* Index numbers for Access Controls. */
77 enum tomoyo_mac_index {
78 TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
79 TOMOYO_MAX_ACCEPT_ENTRY,
81 TOMOYO_MAX_CONTROL_INDEX
84 /* Index numbers for Access Controls. */
85 enum tomoyo_acl_entry_type_index {
87 TOMOYO_TYPE_PATH2_ACL,
88 TOMOYO_TYPE_PATH_NUMBER_ACL,
89 TOMOYO_TYPE_PATH_NUMBER3_ACL,
90 TOMOYO_TYPE_MOUNT_ACL,
93 /* Index numbers for File Controls. */
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.
106 enum tomoyo_path_acl_index {
107 TOMOYO_TYPE_READ_WRITE,
113 TOMOYO_TYPE_TRUNCATE,
118 TOMOYO_MAX_PATH_OPERATION
121 enum tomoyo_path_number3_acl_index {
124 TOMOYO_MAX_PATH_NUMBER3_OPERATION
127 enum tomoyo_path2_acl_index {
130 TOMOYO_TYPE_PIVOT_ROOT,
131 TOMOYO_MAX_PATH2_OPERATION
134 enum tomoyo_path_number_acl_index {
143 TOMOYO_MAX_PATH_NUMBER_OPERATION
146 enum tomoyo_securityfs_interface_index {
148 TOMOYO_EXCEPTIONPOLICY,
149 TOMOYO_DOMAIN_STATUS,
150 TOMOYO_PROCESS_STATUS,
159 #define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
161 /********** Structure definitions. **********/
164 * tomoyo_request_info is a structure which is used for holding
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.
171 struct tomoyo_request_info {
172 struct tomoyo_domain_info *domain;
175 u8 mode; /* One of tomoyo_mode_index . */
179 * tomoyo_path_info is a structure which is used for holding a string data
181 * This structure has several fields for supporting pattern matching.
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
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 "/",
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.
198 struct tomoyo_path_info {
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) */
207 * tomoyo_name_entry is a structure which is used for linking
208 * "struct tomoyo_path_info" into tomoyo_name_list .
210 struct tomoyo_name_entry {
211 struct list_head list;
213 struct tomoyo_path_info entry;
216 struct tomoyo_name_union {
217 const struct tomoyo_path_info *filename;
218 struct tomoyo_path_group *group;
222 struct tomoyo_number_union {
223 unsigned long values[2];
224 struct tomoyo_number_group *group;
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;
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;
246 /* Structure for "path_group" directive. */
247 struct tomoyo_path_group_member {
248 struct list_head list;
250 const struct tomoyo_path_info *member_name;
253 /* Structure for "number_group" directive. */
254 struct tomoyo_number_group_member {
255 struct list_head list;
257 struct tomoyo_number_union number;
261 * tomoyo_acl_info is a structure which is used for holding
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").
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.
273 struct tomoyo_acl_info {
274 struct list_head list;
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.
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
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).
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. */
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.
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.
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
337 struct tomoyo_path_acl {
338 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
340 struct tomoyo_name_union name;
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.
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.
353 * Directives held by this structure are "allow_create", "allow_mkdir",
354 * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
358 struct tomoyo_path_number_acl {
359 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
361 struct tomoyo_name_union name;
362 struct tomoyo_number_union number;
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.
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.
376 * Directives held by this structure are "allow_mkchar", "allow_mkblock".
379 struct tomoyo_path_number3_acl {
380 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
382 struct tomoyo_name_union name;
383 struct tomoyo_number_union mode;
384 struct tomoyo_number_union major;
385 struct tomoyo_number_union minor;
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.
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.
398 * Directives held by this structure are "allow_rename", "allow_link" and
399 * "allow_pivot_root".
401 struct tomoyo_path2_acl {
402 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
404 struct tomoyo_name_union name1;
405 struct tomoyo_name_union name2;
409 * tomoyo_mount_acl is a structure which is used for holding an
410 * entry for mount operation.
411 * It has following fields.
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.
419 * Directives held by this structure are "allow_rename", "allow_link" and
420 * "allow_pivot_root".
422 struct tomoyo_mount_acl {
423 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
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;
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
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 ).
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
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
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. */
460 /* Index returned by tomoyo_read_lock(). */
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. */
470 /* Buffer for reading. */
472 /* EOF flag for reading. */
474 /* Read domain ACL of specified PID? */
475 bool read_single_domain;
476 /* Extra variable for reading. */
478 /* Bytes available for reading. */
480 /* Size of read buffer. */
482 /* Buffer for writing. */
484 /* Bytes available for writing. */
486 /* Size of write buffer. */
488 /* Type of this interface. */
493 * tomoyo_globally_readable_file_entry is a structure which is used for holding
494 * "allow_read" entries.
495 * It has following fields.
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
502 struct tomoyo_globally_readable_file_entry {
503 struct list_head list;
504 const struct tomoyo_path_info *filename;
509 * tomoyo_pattern_entry is a structure which is used for holding
510 * "tomoyo_pattern_list" entries.
511 * It has following fields.
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
519 struct tomoyo_pattern_entry {
520 struct list_head list;
521 const struct tomoyo_path_info *pattern;
526 * tomoyo_no_rewrite_entry is a structure which is used for holding
527 * "deny_rewrite" entries.
528 * It has following fields.
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
536 struct tomoyo_no_rewrite_entry {
537 struct list_head list;
538 const struct tomoyo_path_info *pattern;
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.
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
553 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
555 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
556 * component of a domainname", false otherwise.
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;
563 bool is_not; /* True if this entry is "no_initialize_domain". */
564 /* True if the domainname is tomoyo_get_last_name(). */
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.
573 * (1) "list" which is linked to tomoyo_domain_keeper_list .
574 * (2) "domainname" which is "a domainname" or "the last component of a
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
580 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
582 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
583 * component of a domainname", false otherwise.
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 */
590 bool is_not; /* True if this entry is "no_keep_domain". */
591 /* True if the domainname is tomoyo_get_last_name(). */
596 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
597 * It has following fields.
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
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;
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.
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
622 * (4) "is_deleted" is a bool which is true if marked as deleted, false
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. */
633 /********** Function prototypes. **********/
635 extern asmlinkage long sys_getpid(void);
636 extern asmlinkage long sys_getppid(void);
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);
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);
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);
696 /* Read "alias" entry in exception policy. */
697 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
699 * Read "initialize_domain" and "no_initialize_domain" entry
700 * in exception policy.
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);
736 * Create "initialize_domain" and "no_initialize_domain" entry
737 * in exception policy.
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);
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.
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 *
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);
778 /* Check mode for specified functionality. */
779 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
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);
786 void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
788 /* Convert binary string to ascii string. */
789 char *tomoyo_encode(const char *str);
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.
796 char *tomoyo_realpath(const char *pathname);
798 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
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);
806 /* Check memory quota. */
807 bool tomoyo_memory_ok(void *ptr);
808 void *tomoyo_commit_ok(void *data, const unsigned int size);
811 * Keep the given name on the RAM.
812 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
814 const struct tomoyo_path_info *tomoyo_get_name(const char *name);
816 /* Check for memory usage. */
817 int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
819 /* Set memory quota. */
820 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
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,
835 int tomoyo_find_next_domain(struct linux_binprm *bprm);
837 void tomoyo_print_ulong(char *buffer, const int buffer_len,
838 const unsigned long value, const u8 type);
840 /* Drop refcount on tomoyo_name_union. */
841 void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
843 /* Run garbage collector. */
844 void tomoyo_run_gc(void);
846 void tomoyo_memory_free(void *ptr);
848 /********** External variable definitions. **********/
851 extern struct srcu_struct tomoyo_ss;
853 /* The list for "struct tomoyo_domain_info". */
854 extern struct list_head tomoyo_domain_list;
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];
867 /* Lock for protecting policy. */
868 extern struct mutex tomoyo_policy_lock;
870 /* Has /sbin/init started? */
871 extern bool tomoyo_policy_loaded;
873 /* The kernel's domain. */
874 extern struct tomoyo_domain_info tomoyo_kernel_domain;
876 extern unsigned int tomoyo_quota_for_query;
877 extern unsigned int tomoyo_query_memory_size;
879 /********** Inlined functions. **********/
881 static inline int tomoyo_read_lock(void)
883 return srcu_read_lock(&tomoyo_ss);
886 static inline void tomoyo_read_unlock(int idx)
888 srcu_read_unlock(&tomoyo_ss, idx);
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)
895 return a->hash != b->hash || strcmp(a->name, b->name);
899 * tomoyo_is_valid - Check whether the character is a valid char.
901 * @c: The character to check.
903 * Returns true if @c is a valid character, false otherwise.
905 static inline bool tomoyo_is_valid(const unsigned char c)
907 return c > ' ' && c < 127;
911 * tomoyo_is_invalid - Check whether the character is an invalid char.
913 * @c: The character to check.
915 * Returns true if @c is an invalid character, false otherwise.
917 static inline bool tomoyo_is_invalid(const unsigned char c)
919 return c && (c <= ' ' || c >= 127);
922 static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
925 struct tomoyo_name_entry *ptr =
926 container_of(name, struct tomoyo_name_entry, entry);
927 atomic_dec(&ptr->users);
931 static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
934 atomic_dec(&group->users);
937 static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
940 atomic_dec(&group->users);
943 static inline struct tomoyo_domain_info *tomoyo_domain(void)
945 return current_cred()->security;
948 static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
951 return task_cred_xxx(task, security);
954 static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
955 const struct tomoyo_acl_info *p2)
957 return p1->type == p2->type;
960 static inline bool tomoyo_is_same_name_union
961 (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
963 return p1->filename == p2->filename && p1->group == p2->group &&
964 p1->is_group == p2->is_group;
967 static inline bool tomoyo_is_same_number_union
968 (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
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;
975 static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
976 const struct tomoyo_path_acl *p2)
978 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
979 tomoyo_is_same_name_union(&p1->name, &p2->name);
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)
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);
994 static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
995 const struct tomoyo_path2_acl *p2)
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);
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)
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);
1011 static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1012 const struct tomoyo_mount_acl *p2)
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);
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)
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;
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)
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;
1039 static inline bool tomoyo_is_same_alias_entry
1040 (const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1042 return p1->original_name == p2->original_name &&
1043 p1->aliased_name == p2->aliased_name;
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.
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.
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))
1064 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */