common.h revision fdb8ebb729bbb640e64028a4f579a02ebc405727
1/* 2 * security/tomoyo/common.h 3 * 4 * Common functions for TOMOYO. 5 * 6 * Copyright (C) 2005-2009 NTT DATA CORPORATION 7 * 8 * Version: 2.2.0 2009/04/01 9 * 10 */ 11 12#ifndef _SECURITY_TOMOYO_COMMON_H 13#define _SECURITY_TOMOYO_COMMON_H 14 15#include <linux/ctype.h> 16#include <linux/string.h> 17#include <linux/mm.h> 18#include <linux/file.h> 19#include <linux/kmod.h> 20#include <linux/fs.h> 21#include <linux/sched.h> 22#include <linux/namei.h> 23#include <linux/mount.h> 24#include <linux/list.h> 25 26struct dentry; 27struct vfsmount; 28 29/* 30 * tomoyo_page_buffer is a structure which is used for holding a pathname 31 * obtained from "struct dentry" and "struct vfsmount" pair. 32 * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small 33 * (because TOMOYO escapes non ASCII printable characters using \ooo format), 34 * we will make the buffer larger. 35 */ 36struct tomoyo_page_buffer { 37 char buffer[4096]; 38}; 39 40/* 41 * tomoyo_path_info is a structure which is used for holding a string data 42 * used by TOMOYO. 43 * This structure has several fields for supporting pattern matching. 44 * 45 * (1) "name" is the '\0' terminated string data. 46 * (2) "hash" is full_name_hash(name, strlen(name)). 47 * This allows tomoyo_pathcmp() to compare by hash before actually compare 48 * using strcmp(). 49 * (3) "const_len" is the length of the initial segment of "name" which 50 * consists entirely of non wildcard characters. In other words, the length 51 * which we can compare two strings using strncmp(). 52 * (4) "is_dir" is a bool which is true if "name" ends with "/", 53 * false otherwise. 54 * TOMOYO distinguishes directory and non-directory. A directory ends with 55 * "/" and non-directory does not end with "/". 56 * (5) "is_patterned" is a bool which is true if "name" contains wildcard 57 * characters, false otherwise. This allows TOMOYO to use "hash" and 58 * strcmp() for string comparison if "is_patterned" is false. 59 */ 60struct tomoyo_path_info { 61 const char *name; 62 u32 hash; /* = full_name_hash(name, strlen(name)) */ 63 u16 const_len; /* = tomoyo_const_part_length(name) */ 64 bool is_dir; /* = tomoyo_strendswith(name, "/") */ 65 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */ 66}; 67 68/* 69 * This is the max length of a token. 70 * 71 * A token consists of only ASCII printable characters. 72 * Non printable characters in a token is represented in \ooo style 73 * octal string. Thus, \ itself is represented as \\. 74 */ 75#define TOMOYO_MAX_PATHNAME_LEN 4000 76 77/* 78 * tomoyo_path_info_with_data is a structure which is used for holding a 79 * pathname obtained from "struct dentry" and "struct vfsmount" pair. 80 * 81 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info" 82 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of 83 * buffer for the pathname only. 84 * 85 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release 86 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree() 87 * so that we don't need to return two pointers to the caller. If the caller 88 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove 89 * "struct tomoyo_path_info_with_data". 90 */ 91struct tomoyo_path_info_with_data { 92 /* Keep "head" first, for this pointer is passed to tomoyo_free(). */ 93 struct tomoyo_path_info head; 94 char barrier1[16]; /* Safeguard for overrun. */ 95 char body[TOMOYO_MAX_PATHNAME_LEN]; 96 char barrier2[16]; /* Safeguard for overrun. */ 97}; 98 99/* 100 * tomoyo_acl_info is a structure which is used for holding 101 * 102 * (1) "list" which is linked to the ->acl_info_list of 103 * "struct tomoyo_domain_info" 104 * (2) "type" which tells 105 * (a) type & 0x7F : type of the entry (either 106 * "struct tomoyo_single_path_acl_record" or 107 * "struct tomoyo_double_path_acl_record") 108 * (b) type & 0x80 : whether the entry is marked as "deleted". 109 * 110 * Packing "struct tomoyo_acl_info" allows 111 * "struct tomoyo_single_path_acl_record" to embed "u8" + "u16" and 112 * "struct tomoyo_double_path_acl_record" to embed "u8" 113 * without enlarging their structure size. 114 */ 115struct tomoyo_acl_info { 116 struct list_head list; 117 /* 118 * Type of this ACL entry. 119 * 120 * MSB is is_deleted flag. 121 */ 122 u8 type; 123} __packed; 124 125/* This ACL entry is deleted. */ 126#define TOMOYO_ACL_DELETED 0x80 127 128/* 129 * tomoyo_domain_info is a structure which is used for holding permissions 130 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain. 131 * It has following fields. 132 * 133 * (1) "list" which is linked to tomoyo_domain_list . 134 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info". 135 * (3) "domainname" which holds the name of the domain. 136 * (4) "profile" which remembers profile number assigned to this domain. 137 * (5) "is_deleted" is a bool which is true if this domain is marked as 138 * "deleted", false otherwise. 139 * (6) "quota_warned" is a bool which is used for suppressing warning message 140 * when learning mode learned too much entries. 141 * (7) "flags" which remembers this domain's attributes. 142 * 143 * A domain's lifecycle is an analogy of files on / directory. 144 * Multiple domains with the same domainname cannot be created (as with 145 * creating files with the same filename fails with -EEXIST). 146 * If a process reached a domain, that process can reside in that domain after 147 * that domain is marked as "deleted" (as with a process can access an already 148 * open()ed file after that file was unlink()ed). 149 */ 150struct tomoyo_domain_info { 151 struct list_head list; 152 struct list_head acl_info_list; 153 /* Name of this domain. Never NULL. */ 154 const struct tomoyo_path_info *domainname; 155 u8 profile; /* Profile number to use. */ 156 bool is_deleted; /* Delete flag. */ 157 bool quota_warned; /* Quota warnning flag. */ 158 /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */ 159 u8 flags; 160}; 161 162/* Profile number is an integer between 0 and 255. */ 163#define TOMOYO_MAX_PROFILES 256 164 165/* Ignore "allow_read" directive in exception policy. */ 166#define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1 167/* 168 * This domain was unable to create a new domain at tomoyo_find_next_domain() 169 * because the name of the domain to be created was too long or 170 * it could not allocate memory. 171 * More than one process continued execve() without domain transition. 172 */ 173#define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2 174 175/* 176 * tomoyo_single_path_acl_record is a structure which is used for holding an 177 * entry with one pathname operation (e.g. open(), mkdir()). 178 * It has following fields. 179 * 180 * (1) "head" which is a "struct tomoyo_acl_info". 181 * (2) "perm" which is a bitmask of permitted operations. 182 * (3) "filename" is the pathname. 183 * 184 * Directives held by this structure are "allow_read/write", "allow_execute", 185 * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir", 186 * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock", 187 * "allow_mkchar", "allow_truncate", "allow_symlink", "allow_rewrite", 188 * "allow_chmod", "allow_chown", "allow_chgrp", "allow_chroot", "allow_mount" 189 * and "allow_unmount". 190 */ 191struct tomoyo_single_path_acl_record { 192 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */ 193 u8 perm_high; 194 u16 perm; 195 /* Pointer to single pathname. */ 196 const struct tomoyo_path_info *filename; 197}; 198 199/* 200 * tomoyo_double_path_acl_record is a structure which is used for holding an 201 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()). 202 * It has following fields. 203 * 204 * (1) "head" which is a "struct tomoyo_acl_info". 205 * (2) "perm" which is a bitmask of permitted operations. 206 * (3) "filename1" is the source/old pathname. 207 * (4) "filename2" is the destination/new pathname. 208 * 209 * Directives held by this structure are "allow_rename", "allow_link" and 210 * "allow_pivot_root". 211 */ 212struct tomoyo_double_path_acl_record { 213 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */ 214 u8 perm; 215 /* Pointer to single pathname. */ 216 const struct tomoyo_path_info *filename1; 217 /* Pointer to single pathname. */ 218 const struct tomoyo_path_info *filename2; 219}; 220 221/* Keywords for ACLs. */ 222#define TOMOYO_KEYWORD_ALIAS "alias " 223#define TOMOYO_KEYWORD_ALLOW_READ "allow_read " 224#define TOMOYO_KEYWORD_DELETE "delete " 225#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite " 226#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern " 227#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain " 228#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain " 229#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain " 230#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain " 231#define TOMOYO_KEYWORD_SELECT "select " 232#define TOMOYO_KEYWORD_USE_PROFILE "use_profile " 233#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read" 234/* A domain definition starts with <kernel>. */ 235#define TOMOYO_ROOT_NAME "<kernel>" 236#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1) 237 238/* Index numbers for Access Controls. */ 239#define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */ 240#define TOMOYO_MAX_ACCEPT_ENTRY 1 241#define TOMOYO_VERBOSE 2 242#define TOMOYO_MAX_CONTROL_INDEX 3 243 244/* 245 * tomoyo_io_buffer is a structure which is used for reading and modifying 246 * configuration via /sys/kernel/security/tomoyo/ interface. 247 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as 248 * cursors. 249 * 250 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of 251 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info" 252 * entry has a list of "struct tomoyo_acl_info", we need two cursors when 253 * reading (one is for traversing tomoyo_domain_list and the other is for 254 * traversing "struct tomoyo_acl_info"->acl_info_list ). 255 * 256 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 257 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the 258 * domain with the domainname specified by the rest of that line (NULL is set 259 * if seek failed). 260 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 261 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that 262 * line (->write_var1 is set to NULL if a domain was deleted). 263 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 264 * neither "select " nor "delete ", an entry or a domain specified by that line 265 * is appended. 266 */ 267struct tomoyo_io_buffer { 268 int (*read) (struct tomoyo_io_buffer *); 269 int (*write) (struct tomoyo_io_buffer *); 270 /* Exclusive lock for this structure. */ 271 struct mutex io_sem; 272 /* Index returned by tomoyo_read_lock(). */ 273 int reader_idx; 274 /* The position currently reading from. */ 275 struct list_head *read_var1; 276 /* Extra variables for reading. */ 277 struct list_head *read_var2; 278 /* The position currently writing to. */ 279 struct tomoyo_domain_info *write_var1; 280 /* The step for reading. */ 281 int read_step; 282 /* Buffer for reading. */ 283 char *read_buf; 284 /* EOF flag for reading. */ 285 bool read_eof; 286 /* Read domain ACL of specified PID? */ 287 bool read_single_domain; 288 /* Extra variable for reading. */ 289 u8 read_bit; 290 /* Bytes available for reading. */ 291 int read_avail; 292 /* Size of read buffer. */ 293 int readbuf_size; 294 /* Buffer for writing. */ 295 char *write_buf; 296 /* Bytes available for writing. */ 297 int write_avail; 298 /* Size of write buffer. */ 299 int writebuf_size; 300}; 301 302/* Check whether the domain has too many ACL entries to hold. */ 303bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain); 304/* Transactional sprintf() for policy dump. */ 305bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) 306 __attribute__ ((format(printf, 2, 3))); 307/* Check whether the domainname is correct. */ 308bool tomoyo_is_correct_domain(const unsigned char *domainname, 309 const char *function); 310/* Check whether the token is correct. */ 311bool tomoyo_is_correct_path(const char *filename, const s8 start_type, 312 const s8 pattern_type, const s8 end_type, 313 const char *function); 314/* Check whether the token can be a domainname. */ 315bool tomoyo_is_domain_def(const unsigned char *buffer); 316/* Check whether the given filename matches the given pattern. */ 317bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename, 318 const struct tomoyo_path_info *pattern); 319/* Read "alias" entry in exception policy. */ 320bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head); 321/* 322 * Read "initialize_domain" and "no_initialize_domain" entry 323 * in exception policy. 324 */ 325bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head); 326/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */ 327bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head); 328/* Read "file_pattern" entry in exception policy. */ 329bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head); 330/* Read "allow_read" entry in exception policy. */ 331bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head); 332/* Read "deny_rewrite" entry in exception policy. */ 333bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head); 334/* Write domain policy violation warning message to console? */ 335bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain); 336/* Convert double path operation to operation name. */ 337const char *tomoyo_dp2keyword(const u8 operation); 338/* Get the last component of the given domainname. */ 339const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain); 340/* Get warning message. */ 341const char *tomoyo_get_msg(const bool is_enforce); 342/* Convert single path operation to operation name. */ 343const char *tomoyo_sp2keyword(const u8 operation); 344/* Create "alias" entry in exception policy. */ 345int tomoyo_write_alias_policy(char *data, const bool is_delete); 346/* 347 * Create "initialize_domain" and "no_initialize_domain" entry 348 * in exception policy. 349 */ 350int tomoyo_write_domain_initializer_policy(char *data, const bool is_not, 351 const bool is_delete); 352/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */ 353int tomoyo_write_domain_keeper_policy(char *data, const bool is_not, 354 const bool is_delete); 355/* 356 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write", 357 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir", 358 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar", 359 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and 360 * "allow_link" entry in domain policy. 361 */ 362int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain, 363 const bool is_delete); 364/* Create "allow_read" entry in exception policy. */ 365int tomoyo_write_globally_readable_policy(char *data, const bool is_delete); 366/* Create "deny_rewrite" entry in exception policy. */ 367int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete); 368/* Create "file_pattern" entry in exception policy. */ 369int tomoyo_write_pattern_policy(char *data, const bool is_delete); 370/* Find a domain by the given name. */ 371struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname); 372/* Find or create a domain by the given name. */ 373struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char * 374 domainname, 375 const u8 profile); 376/* Check mode for specified functionality. */ 377unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain, 378 const u8 index); 379/* Allocate memory for structures. */ 380void *tomoyo_alloc_acl_element(const u8 acl_type); 381/* Fill in "struct tomoyo_path_info" members. */ 382void tomoyo_fill_path_info(struct tomoyo_path_info *ptr); 383/* Run policy loader when /sbin/init starts. */ 384void tomoyo_load_policy(const char *filename); 385/* Change "struct tomoyo_domain_info"->flags. */ 386void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain, 387 const bool is_delete, const u8 flags); 388 389/* strcmp() for "struct tomoyo_path_info" structure. */ 390static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a, 391 const struct tomoyo_path_info *b) 392{ 393 return a->hash != b->hash || strcmp(a->name, b->name); 394} 395 396/* Get type of an ACL entry. */ 397static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr) 398{ 399 return ptr->type & ~TOMOYO_ACL_DELETED; 400} 401 402/* Get type of an ACL entry. */ 403static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr) 404{ 405 return ptr->type; 406} 407 408/** 409 * tomoyo_is_valid - Check whether the character is a valid char. 410 * 411 * @c: The character to check. 412 * 413 * Returns true if @c is a valid character, false otherwise. 414 */ 415static inline bool tomoyo_is_valid(const unsigned char c) 416{ 417 return c > ' ' && c < 127; 418} 419 420/** 421 * tomoyo_is_invalid - Check whether the character is an invalid char. 422 * 423 * @c: The character to check. 424 * 425 * Returns true if @c is an invalid character, false otherwise. 426 */ 427static inline bool tomoyo_is_invalid(const unsigned char c) 428{ 429 return c && (c <= ' ' || c >= 127); 430} 431 432/* The list for "struct tomoyo_domain_info". */ 433extern struct list_head tomoyo_domain_list; 434extern struct rw_semaphore tomoyo_domain_list_lock; 435 436/* Lock for domain->acl_info_list. */ 437extern struct rw_semaphore tomoyo_domain_acl_info_list_lock; 438 439/* Has /sbin/init started? */ 440extern bool tomoyo_policy_loaded; 441 442/* The kernel's domain. */ 443extern struct tomoyo_domain_info tomoyo_kernel_domain; 444 445/** 446 * list_for_each_cookie - iterate over a list with cookie. 447 * @pos: the &struct list_head to use as a loop cursor. 448 * @cookie: the &struct list_head to use as a cookie. 449 * @head: the head for your list. 450 * 451 * Same with list_for_each_rcu() except that this primitive uses @cookie 452 * so that we can continue iteration. 453 * @cookie must be NULL when iteration starts, and @cookie will become 454 * NULL when iteration finishes. 455 */ 456#define list_for_each_cookie(pos, cookie, head) \ 457 for (({ if (!cookie) \ 458 cookie = head; }), \ 459 pos = rcu_dereference((cookie)->next); \ 460 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \ 461 (cookie) = pos, pos = rcu_dereference(pos->next)) 462 463extern struct srcu_struct tomoyo_ss; 464 465static inline int tomoyo_read_lock(void) 466{ 467 return srcu_read_lock(&tomoyo_ss); 468} 469 470static inline void tomoyo_read_unlock(int idx) 471{ 472 srcu_read_unlock(&tomoyo_ss, idx); 473} 474 475#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */ 476