Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 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-pre 2009/02/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 | /* Temporary buffer for holding pathnames. */ |
| 30 | struct tomoyo_page_buffer { |
| 31 | char buffer[4096]; |
| 32 | }; |
| 33 | |
| 34 | /* Structure for holding a token. */ |
| 35 | struct tomoyo_path_info { |
| 36 | const char *name; |
| 37 | u32 hash; /* = full_name_hash(name, strlen(name)) */ |
| 38 | u16 total_len; /* = strlen(name) */ |
| 39 | u16 const_len; /* = tomoyo_const_part_length(name) */ |
| 40 | bool is_dir; /* = tomoyo_strendswith(name, "/") */ |
| 41 | bool is_patterned; /* = tomoyo_path_contains_pattern(name) */ |
| 42 | u16 depth; /* = tomoyo_path_depth(name) */ |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * This is the max length of a token. |
| 47 | * |
| 48 | * A token consists of only ASCII printable characters. |
| 49 | * Non printable characters in a token is represented in \ooo style |
| 50 | * octal string. Thus, \ itself is represented as \\. |
| 51 | */ |
| 52 | #define TOMOYO_MAX_PATHNAME_LEN 4000 |
| 53 | |
| 54 | /* Structure for holding requested pathname. */ |
| 55 | struct tomoyo_path_info_with_data { |
| 56 | /* Keep "head" first, for this pointer is passed to tomoyo_free(). */ |
| 57 | struct tomoyo_path_info head; |
Tetsuo Handa | a106cbf | 2009-03-27 13:12:16 +0900 | [diff] [blame] | 58 | char barrier1[16]; /* Safeguard for overrun. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 59 | char body[TOMOYO_MAX_PATHNAME_LEN]; |
| 60 | char barrier2[16]; /* Safeguard for overrun. */ |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Common header for holding ACL entries. |
| 65 | * |
| 66 | * Packing "struct tomoyo_acl_info" allows |
| 67 | * "struct tomoyo_single_path_acl_record" to embed "u16" and |
| 68 | * "struct tomoyo_double_path_acl_record" to embed "u8" |
| 69 | * without enlarging their structure size. |
| 70 | */ |
| 71 | struct tomoyo_acl_info { |
| 72 | struct list_head list; |
| 73 | /* |
| 74 | * Type of this ACL entry. |
| 75 | * |
| 76 | * MSB is is_deleted flag. |
| 77 | */ |
| 78 | u8 type; |
| 79 | } __packed; |
| 80 | |
| 81 | /* This ACL entry is deleted. */ |
| 82 | #define TOMOYO_ACL_DELETED 0x80 |
| 83 | |
| 84 | /* Structure for domain information. */ |
| 85 | struct tomoyo_domain_info { |
| 86 | struct list_head list; |
| 87 | struct list_head acl_info_list; |
| 88 | /* Name of this domain. Never NULL. */ |
| 89 | const struct tomoyo_path_info *domainname; |
| 90 | u8 profile; /* Profile number to use. */ |
Tetsuo Handa | 7ba5779 | 2009-04-06 20:49:14 +0900 | [diff] [blame^] | 91 | bool is_deleted; /* Delete flag. */ |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 92 | bool quota_warned; /* Quota warnning flag. */ |
| 93 | /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */ |
| 94 | u8 flags; |
| 95 | }; |
| 96 | |
| 97 | /* Profile number is an integer between 0 and 255. */ |
| 98 | #define TOMOYO_MAX_PROFILES 256 |
| 99 | |
| 100 | /* Ignore "allow_read" directive in exception policy. */ |
| 101 | #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1 |
| 102 | /* |
| 103 | * This domain was unable to create a new domain at tomoyo_find_next_domain() |
| 104 | * because the name of the domain to be created was too long or |
| 105 | * it could not allocate memory. |
| 106 | * More than one process continued execve() without domain transition. |
| 107 | */ |
| 108 | #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2 |
| 109 | |
| 110 | /* |
| 111 | * Structure for "allow_read/write", "allow_execute", "allow_read", |
| 112 | * "allow_write", "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir", |
| 113 | * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar", |
| 114 | * "allow_truncate", "allow_symlink" and "allow_rewrite" directive. |
| 115 | */ |
| 116 | struct tomoyo_single_path_acl_record { |
| 117 | struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */ |
| 118 | u16 perm; |
| 119 | /* Pointer to single pathname. */ |
| 120 | const struct tomoyo_path_info *filename; |
| 121 | }; |
| 122 | |
| 123 | /* Structure for "allow_rename" and "allow_link" directive. */ |
| 124 | struct tomoyo_double_path_acl_record { |
| 125 | struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */ |
| 126 | u8 perm; |
| 127 | /* Pointer to single pathname. */ |
| 128 | const struct tomoyo_path_info *filename1; |
| 129 | /* Pointer to single pathname. */ |
| 130 | const struct tomoyo_path_info *filename2; |
| 131 | }; |
| 132 | |
| 133 | /* Keywords for ACLs. */ |
| 134 | #define TOMOYO_KEYWORD_ALIAS "alias " |
| 135 | #define TOMOYO_KEYWORD_ALLOW_READ "allow_read " |
| 136 | #define TOMOYO_KEYWORD_DELETE "delete " |
| 137 | #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite " |
| 138 | #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern " |
| 139 | #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain " |
| 140 | #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain " |
| 141 | #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain " |
| 142 | #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain " |
| 143 | #define TOMOYO_KEYWORD_SELECT "select " |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 144 | #define TOMOYO_KEYWORD_USE_PROFILE "use_profile " |
| 145 | #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read" |
| 146 | /* A domain definition starts with <kernel>. */ |
| 147 | #define TOMOYO_ROOT_NAME "<kernel>" |
| 148 | #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1) |
| 149 | |
| 150 | /* Index numbers for Access Controls. */ |
| 151 | #define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */ |
| 152 | #define TOMOYO_MAX_ACCEPT_ENTRY 1 |
| 153 | #define TOMOYO_VERBOSE 2 |
| 154 | #define TOMOYO_MAX_CONTROL_INDEX 3 |
| 155 | |
| 156 | /* Structure for reading/writing policy via securityfs interfaces. */ |
| 157 | struct tomoyo_io_buffer { |
| 158 | int (*read) (struct tomoyo_io_buffer *); |
| 159 | int (*write) (struct tomoyo_io_buffer *); |
| 160 | /* Exclusive lock for this structure. */ |
| 161 | struct mutex io_sem; |
| 162 | /* The position currently reading from. */ |
| 163 | struct list_head *read_var1; |
| 164 | /* Extra variables for reading. */ |
| 165 | struct list_head *read_var2; |
| 166 | /* The position currently writing to. */ |
| 167 | struct tomoyo_domain_info *write_var1; |
| 168 | /* The step for reading. */ |
| 169 | int read_step; |
| 170 | /* Buffer for reading. */ |
| 171 | char *read_buf; |
| 172 | /* EOF flag for reading. */ |
| 173 | bool read_eof; |
| 174 | /* Read domain ACL of specified PID? */ |
| 175 | bool read_single_domain; |
| 176 | /* Extra variable for reading. */ |
| 177 | u8 read_bit; |
| 178 | /* Bytes available for reading. */ |
| 179 | int read_avail; |
| 180 | /* Size of read buffer. */ |
| 181 | int readbuf_size; |
| 182 | /* Buffer for writing. */ |
| 183 | char *write_buf; |
| 184 | /* Bytes available for writing. */ |
| 185 | int write_avail; |
| 186 | /* Size of write buffer. */ |
| 187 | int writebuf_size; |
| 188 | }; |
| 189 | |
| 190 | /* Check whether the domain has too many ACL entries to hold. */ |
| 191 | bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain); |
| 192 | /* Transactional sprintf() for policy dump. */ |
| 193 | bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) |
| 194 | __attribute__ ((format(printf, 2, 3))); |
| 195 | /* Check whether the domainname is correct. */ |
| 196 | bool tomoyo_is_correct_domain(const unsigned char *domainname, |
| 197 | const char *function); |
| 198 | /* Check whether the token is correct. */ |
| 199 | bool tomoyo_is_correct_path(const char *filename, const s8 start_type, |
| 200 | const s8 pattern_type, const s8 end_type, |
| 201 | const char *function); |
| 202 | /* Check whether the token can be a domainname. */ |
| 203 | bool tomoyo_is_domain_def(const unsigned char *buffer); |
| 204 | /* Check whether the given filename matches the given pattern. */ |
| 205 | bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename, |
| 206 | const struct tomoyo_path_info *pattern); |
| 207 | /* Read "alias" entry in exception policy. */ |
| 208 | bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head); |
| 209 | /* |
| 210 | * Read "initialize_domain" and "no_initialize_domain" entry |
| 211 | * in exception policy. |
| 212 | */ |
| 213 | bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head); |
| 214 | /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */ |
| 215 | bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head); |
| 216 | /* Read "file_pattern" entry in exception policy. */ |
| 217 | bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head); |
| 218 | /* Read "allow_read" entry in exception policy. */ |
| 219 | bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head); |
| 220 | /* Read "deny_rewrite" entry in exception policy. */ |
| 221 | bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head); |
| 222 | /* Write domain policy violation warning message to console? */ |
| 223 | bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain); |
| 224 | /* Convert double path operation to operation name. */ |
| 225 | const char *tomoyo_dp2keyword(const u8 operation); |
| 226 | /* Get the last component of the given domainname. */ |
| 227 | const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain); |
| 228 | /* Get warning message. */ |
| 229 | const char *tomoyo_get_msg(const bool is_enforce); |
| 230 | /* Convert single path operation to operation name. */ |
| 231 | const char *tomoyo_sp2keyword(const u8 operation); |
| 232 | /* Delete a domain. */ |
| 233 | int tomoyo_delete_domain(char *data); |
| 234 | /* Create "alias" entry in exception policy. */ |
| 235 | int tomoyo_write_alias_policy(char *data, const bool is_delete); |
| 236 | /* |
| 237 | * Create "initialize_domain" and "no_initialize_domain" entry |
| 238 | * in exception policy. |
| 239 | */ |
| 240 | int tomoyo_write_domain_initializer_policy(char *data, const bool is_not, |
| 241 | const bool is_delete); |
| 242 | /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */ |
| 243 | int tomoyo_write_domain_keeper_policy(char *data, const bool is_not, |
| 244 | const bool is_delete); |
| 245 | /* |
| 246 | * Create "allow_read/write", "allow_execute", "allow_read", "allow_write", |
| 247 | * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir", |
| 248 | * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar", |
| 249 | * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and |
| 250 | * "allow_link" entry in domain policy. |
| 251 | */ |
| 252 | int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain, |
| 253 | const bool is_delete); |
| 254 | /* Create "allow_read" entry in exception policy. */ |
| 255 | int tomoyo_write_globally_readable_policy(char *data, const bool is_delete); |
| 256 | /* Create "deny_rewrite" entry in exception policy. */ |
| 257 | int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete); |
| 258 | /* Create "file_pattern" entry in exception policy. */ |
| 259 | int tomoyo_write_pattern_policy(char *data, const bool is_delete); |
| 260 | /* Find a domain by the given name. */ |
| 261 | struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname); |
| 262 | /* Find or create a domain by the given name. */ |
| 263 | struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char * |
| 264 | domainname, |
| 265 | const u8 profile); |
Kentaro Takeda | 9590837 | 2009-02-05 17:18:13 +0900 | [diff] [blame] | 266 | /* Check mode for specified functionality. */ |
| 267 | unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain, |
| 268 | const u8 index); |
| 269 | /* Allocate memory for structures. */ |
| 270 | void *tomoyo_alloc_acl_element(const u8 acl_type); |
| 271 | /* Fill in "struct tomoyo_path_info" members. */ |
| 272 | void tomoyo_fill_path_info(struct tomoyo_path_info *ptr); |
| 273 | /* Run policy loader when /sbin/init starts. */ |
| 274 | void tomoyo_load_policy(const char *filename); |
| 275 | /* Change "struct tomoyo_domain_info"->flags. */ |
| 276 | void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain, |
| 277 | const bool is_delete, const u8 flags); |
| 278 | |
| 279 | /* strcmp() for "struct tomoyo_path_info" structure. */ |
| 280 | static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a, |
| 281 | const struct tomoyo_path_info *b) |
| 282 | { |
| 283 | return a->hash != b->hash || strcmp(a->name, b->name); |
| 284 | } |
| 285 | |
| 286 | /* Get type of an ACL entry. */ |
| 287 | static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr) |
| 288 | { |
| 289 | return ptr->type & ~TOMOYO_ACL_DELETED; |
| 290 | } |
| 291 | |
| 292 | /* Get type of an ACL entry. */ |
| 293 | static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr) |
| 294 | { |
| 295 | return ptr->type; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * tomoyo_is_valid - Check whether the character is a valid char. |
| 300 | * |
| 301 | * @c: The character to check. |
| 302 | * |
| 303 | * Returns true if @c is a valid character, false otherwise. |
| 304 | */ |
| 305 | static inline bool tomoyo_is_valid(const unsigned char c) |
| 306 | { |
| 307 | return c > ' ' && c < 127; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * tomoyo_is_invalid - Check whether the character is an invalid char. |
| 312 | * |
| 313 | * @c: The character to check. |
| 314 | * |
| 315 | * Returns true if @c is an invalid character, false otherwise. |
| 316 | */ |
| 317 | static inline bool tomoyo_is_invalid(const unsigned char c) |
| 318 | { |
| 319 | return c && (c <= ' ' || c >= 127); |
| 320 | } |
| 321 | |
| 322 | /* The list for "struct tomoyo_domain_info". */ |
| 323 | extern struct list_head tomoyo_domain_list; |
| 324 | extern struct rw_semaphore tomoyo_domain_list_lock; |
| 325 | |
| 326 | /* Lock for domain->acl_info_list. */ |
| 327 | extern struct rw_semaphore tomoyo_domain_acl_info_list_lock; |
| 328 | |
| 329 | /* Has /sbin/init started? */ |
| 330 | extern bool tomoyo_policy_loaded; |
| 331 | |
| 332 | /* The kernel's domain. */ |
| 333 | extern struct tomoyo_domain_info tomoyo_kernel_domain; |
| 334 | |
| 335 | /** |
| 336 | * list_for_each_cookie - iterate over a list with cookie. |
| 337 | * @pos: the &struct list_head to use as a loop cursor. |
| 338 | * @cookie: the &struct list_head to use as a cookie. |
| 339 | * @head: the head for your list. |
| 340 | * |
| 341 | * Same with list_for_each() except that this primitive uses @cookie |
| 342 | * so that we can continue iteration. |
| 343 | * @cookie must be NULL when iteration starts, and @cookie will become |
| 344 | * NULL when iteration finishes. |
| 345 | */ |
| 346 | #define list_for_each_cookie(pos, cookie, head) \ |
| 347 | for (({ if (!cookie) \ |
| 348 | cookie = head; }), \ |
| 349 | pos = (cookie)->next; \ |
| 350 | prefetch(pos->next), pos != (head) || ((cookie) = NULL); \ |
| 351 | (cookie) = pos, pos = pos->next) |
| 352 | |
| 353 | #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */ |