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 26 struct dentry; 27 struct 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 */ 36 struct 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 */ 60 struct 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 */ 91 struct 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 "u16" and 112 * "struct tomoyo_double_path_acl_record" to embed "u8" 113 * without enlarging their structure size. 114 */ 115 struct 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 */ 150 struct 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" and "allow_rewrite". 188 */ 189 struct tomoyo_single_path_acl_record { 190 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */ 191 u16 perm; 192 /* Pointer to single pathname. */ 193 const struct tomoyo_path_info *filename; 194 }; 195 196 /* 197 * tomoyo_double_path_acl_record is a structure which is used for holding an 198 * entry with two pathnames operation (i.e. link() and rename()). 199 * It has following fields. 200 * 201 * (1) "head" which is a "struct tomoyo_acl_info". 202 * (2) "perm" which is a bitmask of permitted operations. 203 * (3) "filename1" is the source/old pathname. 204 * (4) "filename2" is the destination/new pathname. 205 * 206 * Directives held by this structure are "allow_rename" and "allow_link". 207 */ 208 struct tomoyo_double_path_acl_record { 209 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */ 210 u8 perm; 211 /* Pointer to single pathname. */ 212 const struct tomoyo_path_info *filename1; 213 /* Pointer to single pathname. */ 214 const struct tomoyo_path_info *filename2; 215 }; 216 217 /* Keywords for ACLs. */ 218 #define TOMOYO_KEYWORD_ALIAS "alias " 219 #define TOMOYO_KEYWORD_ALLOW_READ "allow_read " 220 #define TOMOYO_KEYWORD_DELETE "delete " 221 #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite " 222 #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern " 223 #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain " 224 #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain " 225 #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain " 226 #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain " 227 #define TOMOYO_KEYWORD_SELECT "select " 228 #define TOMOYO_KEYWORD_USE_PROFILE "use_profile " 229 #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read" 230 /* A domain definition starts with <kernel>. */ 231 #define TOMOYO_ROOT_NAME "<kernel>" 232 #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1) 233 234 /* Index numbers for Access Controls. */ 235 #define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */ 236 #define TOMOYO_MAX_ACCEPT_ENTRY 1 237 #define TOMOYO_VERBOSE 2 238 #define TOMOYO_MAX_CONTROL_INDEX 3 239 240 /* 241 * tomoyo_io_buffer is a structure which is used for reading and modifying 242 * configuration via /sys/kernel/security/tomoyo/ interface. 243 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as 244 * cursors. 245 * 246 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of 247 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info" 248 * entry has a list of "struct tomoyo_acl_info", we need two cursors when 249 * reading (one is for traversing tomoyo_domain_list and the other is for 250 * traversing "struct tomoyo_acl_info"->acl_info_list ). 251 * 252 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 253 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the 254 * domain with the domainname specified by the rest of that line (NULL is set 255 * if seek failed). 256 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 257 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that 258 * line (->write_var1 is set to NULL if a domain was deleted). 259 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with 260 * neither "select " nor "delete ", an entry or a domain specified by that line 261 * is appended. 262 */ 263 struct tomoyo_io_buffer { 264 int (*read) (struct tomoyo_io_buffer *); 265 int (*write) (struct tomoyo_io_buffer *); 266 /* Exclusive lock for this structure. */ 267 struct mutex io_sem; 268 /* The position currently reading from. */ 269 struct list_head *read_var1; 270 /* Extra variables for reading. */ 271 struct list_head *read_var2; 272 /* The position currently writing to. */ 273 struct tomoyo_domain_info *write_var1; 274 /* The step for reading. */ 275 int read_step; 276 /* Buffer for reading. */ 277 char *read_buf; 278 /* EOF flag for reading. */ 279 bool read_eof; 280 /* Read domain ACL of specified PID? */ 281 bool read_single_domain; 282 /* Extra variable for reading. */ 283 u8 read_bit; 284 /* Bytes available for reading. */ 285 int read_avail; 286 /* Size of read buffer. */ 287 int readbuf_size; 288 /* Buffer for writing. */ 289 char *write_buf; 290 /* Bytes available for writing. */ 291 int write_avail; 292 /* Size of write buffer. */ 293 int writebuf_size; 294 }; 295 296 /* Check whether the domain has too many ACL entries to hold. */ 297 bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain); 298 /* Transactional sprintf() for policy dump. */ 299 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) 300 __attribute__ ((format(printf, 2, 3))); 301 /* Check whether the domainname is correct. */ 302 bool tomoyo_is_correct_domain(const unsigned char *domainname, 303 const char *function); 304 /* Check whether the token is correct. */ 305 bool tomoyo_is_correct_path(const char *filename, const s8 start_type, 306 const s8 pattern_type, const s8 end_type, 307 const char *function); 308 /* Check whether the token can be a domainname. */ 309 bool tomoyo_is_domain_def(const unsigned char *buffer); 310 /* Check whether the given filename matches the given pattern. */ 311 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename, 312 const struct tomoyo_path_info *pattern); 313 /* Read "alias" entry in exception policy. */ 314 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head); 315 /* 316 * Read "initialize_domain" and "no_initialize_domain" entry 317 * in exception policy. 318 */ 319 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head); 320 /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */ 321 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head); 322 /* Read "file_pattern" entry in exception policy. */ 323 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head); 324 /* Read "allow_read" entry in exception policy. */ 325 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head); 326 /* Read "deny_rewrite" entry in exception policy. */ 327 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head); 328 /* Write domain policy violation warning message to console? */ 329 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain); 330 /* Convert double path operation to operation name. */ 331 const char *tomoyo_dp2keyword(const u8 operation); 332 /* Get the last component of the given domainname. */ 333 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain); 334 /* Get warning message. */ 335 const char *tomoyo_get_msg(const bool is_enforce); 336 /* Convert single path operation to operation name. */ 337 const char *tomoyo_sp2keyword(const u8 operation); 338 /* Create "alias" entry in exception policy. */ 339 int tomoyo_write_alias_policy(char *data, const bool is_delete); 340 /* 341 * Create "initialize_domain" and "no_initialize_domain" entry 342 * in exception policy. 343 */ 344 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not, 345 const bool is_delete); 346 /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */ 347 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not, 348 const bool is_delete); 349 /* 350 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write", 351 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir", 352 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar", 353 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and 354 * "allow_link" entry in domain policy. 355 */ 356 int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain, 357 const bool is_delete); 358 /* Create "allow_read" entry in exception policy. */ 359 int tomoyo_write_globally_readable_policy(char *data, const bool is_delete); 360 /* Create "deny_rewrite" entry in exception policy. */ 361 int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete); 362 /* Create "file_pattern" entry in exception policy. */ 363 int tomoyo_write_pattern_policy(char *data, const bool is_delete); 364 /* Find a domain by the given name. */ 365 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname); 366 /* Find or create a domain by the given name. */ 367 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char * 368 domainname, 369 const u8 profile); 370 /* Check mode for specified functionality. */ 371 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain, 372 const u8 index); 373 /* Allocate memory for structures. */ 374 void *tomoyo_alloc_acl_element(const u8 acl_type); 375 /* Fill in "struct tomoyo_path_info" members. */ 376 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr); 377 /* Run policy loader when /sbin/init starts. */ 378 void tomoyo_load_policy(const char *filename); 379 /* Change "struct tomoyo_domain_info"->flags. */ 380 void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain, 381 const bool is_delete, const u8 flags); 382 383 /* strcmp() for "struct tomoyo_path_info" structure. */ 384 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a, 385 const struct tomoyo_path_info *b) 386 { 387 return a->hash != b->hash || strcmp(a->name, b->name); 388 } 389 390 /* Get type of an ACL entry. */ 391 static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr) 392 { 393 return ptr->type & ~TOMOYO_ACL_DELETED; 394 } 395 396 /* Get type of an ACL entry. */ 397 static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr) 398 { 399 return ptr->type; 400 } 401 402 /** 403 * tomoyo_is_valid - Check whether the character is a valid char. 404 * 405 * @c: The character to check. 406 * 407 * Returns true if @c is a valid character, false otherwise. 408 */ 409 static inline bool tomoyo_is_valid(const unsigned char c) 410 { 411 return c > ' ' && c < 127; 412 } 413 414 /** 415 * tomoyo_is_invalid - Check whether the character is an invalid char. 416 * 417 * @c: The character to check. 418 * 419 * Returns true if @c is an invalid character, false otherwise. 420 */ 421 static inline bool tomoyo_is_invalid(const unsigned char c) 422 { 423 return c && (c <= ' ' || c >= 127); 424 } 425 426 /* The list for "struct tomoyo_domain_info". */ 427 extern struct list_head tomoyo_domain_list; 428 extern struct rw_semaphore tomoyo_domain_list_lock; 429 430 /* Lock for domain->acl_info_list. */ 431 extern struct rw_semaphore tomoyo_domain_acl_info_list_lock; 432 433 /* Has /sbin/init started? */ 434 extern bool tomoyo_policy_loaded; 435 436 /* The kernel's domain. */ 437 extern struct tomoyo_domain_info tomoyo_kernel_domain; 438 439 /** 440 * list_for_each_cookie - iterate over a list with cookie. 441 * @pos: the &struct list_head to use as a loop cursor. 442 * @cookie: the &struct list_head to use as a cookie. 443 * @head: the head for your list. 444 * 445 * Same with list_for_each() except that this primitive uses @cookie 446 * so that we can continue iteration. 447 * @cookie must be NULL when iteration starts, and @cookie will become 448 * NULL when iteration finishes. 449 */ 450 #define list_for_each_cookie(pos, cookie, head) \ 451 for (({ if (!cookie) \ 452 cookie = head; }), \ 453 pos = (cookie)->next; \ 454 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \ 455 (cookie) = pos, pos = pos->next) 456 457 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */ 458