2 * security/tomoyo/common.h
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0 2009/04/01
12 #ifndef _SECURITY_TOMOYO_COMMON_H
13 #define _SECURITY_TOMOYO_COMMON_H
15 #include <linux/ctype.h>
16 #include <linux/string.h>
18 #include <linux/file.h>
19 #include <linux/kmod.h>
21 #include <linux/sched.h>
22 #include <linux/namei.h>
23 #include <linux/mount.h>
24 #include <linux/list.h>
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.
36 struct tomoyo_page_buffer
{
41 * tomoyo_path_info is a structure which is used for holding a string data
43 * This structure has several fields for supporting pattern matching.
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
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 "/",
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.
60 struct tomoyo_path_info
{
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) */
69 * This is the max length of a token.
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 \\.
75 #define TOMOYO_MAX_PATHNAME_LEN 4000
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.
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.
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".
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. */
100 * tomoyo_acl_info is a structure which is used for holding
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".
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.
115 struct tomoyo_acl_info
{
116 struct list_head list
;
118 * Type of this ACL entry.
120 * MSB is is_deleted flag.
125 /* This ACL entry is deleted. */
126 #define TOMOYO_ACL_DELETED 0x80
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.
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.
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).
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. */
162 /* Profile number is an integer between 0 and 255. */
163 #define TOMOYO_MAX_PROFILES 256
165 /* Ignore "allow_read" directive in exception policy. */
166 #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
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.
173 #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2
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.
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.
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".
189 struct tomoyo_single_path_acl_record
{
190 struct tomoyo_acl_info head
; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
192 /* Pointer to single pathname. */
193 const struct tomoyo_path_info
*filename
;
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.
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.
206 * Directives held by this structure are "allow_rename" and "allow_link".
208 struct tomoyo_double_path_acl_record
{
209 struct tomoyo_acl_info head
; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
211 /* Pointer to single pathname. */
212 const struct tomoyo_path_info
*filename1
;
213 /* Pointer to single pathname. */
214 const struct tomoyo_path_info
*filename2
;
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)
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
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
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 ).
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
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
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. */
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. */
276 /* Buffer for reading. */
278 /* EOF flag for reading. */
280 /* Read domain ACL of specified PID? */
281 bool read_single_domain
;
282 /* Extra variable for reading. */
284 /* Bytes available for reading. */
286 /* Size of read buffer. */
288 /* Buffer for writing. */
290 /* Bytes available for writing. */
292 /* Size of write buffer. */
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
);
316 * Read "initialize_domain" and "no_initialize_domain" entry
317 * in exception policy.
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
);
341 * Create "initialize_domain" and "no_initialize_domain" entry
342 * in exception policy.
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
);
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.
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 *
370 /* Check mode for specified functionality. */
371 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info
*domain
,
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
);
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
)
387 return a
->hash
!= b
->hash
|| strcmp(a
->name
, b
->name
);
390 /* Get type of an ACL entry. */
391 static inline u8
tomoyo_acl_type1(struct tomoyo_acl_info
*ptr
)
393 return ptr
->type
& ~TOMOYO_ACL_DELETED
;
396 /* Get type of an ACL entry. */
397 static inline u8
tomoyo_acl_type2(struct tomoyo_acl_info
*ptr
)
403 * tomoyo_is_valid - Check whether the character is a valid char.
405 * @c: The character to check.
407 * Returns true if @c is a valid character, false otherwise.
409 static inline bool tomoyo_is_valid(const unsigned char c
)
411 return c
> ' ' && c
< 127;
415 * tomoyo_is_invalid - Check whether the character is an invalid char.
417 * @c: The character to check.
419 * Returns true if @c is an invalid character, false otherwise.
421 static inline bool tomoyo_is_invalid(const unsigned char c
)
423 return c
&& (c
<= ' ' || c
>= 127);
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
;
430 /* Lock for domain->acl_info_list. */
431 extern struct rw_semaphore tomoyo_domain_acl_info_list_lock
;
433 /* Has /sbin/init started? */
434 extern bool tomoyo_policy_loaded
;
436 /* The kernel's domain. */
437 extern struct tomoyo_domain_info tomoyo_kernel_domain
;
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.
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.
450 #define list_for_each_cookie(pos, cookie, head) \
451 for (({ if (!cookie) \
453 pos = (cookie)->next; \
454 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
455 (cookie) = pos, pos = pos->next)
457 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */