davinci: DA850/OMAP-L138 EVM: simplify configuration of emac in MII/RMII mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / tomoyo / common.h
blob31df541911f76f3d5cc39c92508eb37a25fb8545
1 /*
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>
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>
26 struct dentry;
27 struct vfsmount;
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 {
37 char buffer[4096];
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.
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 * (6) "depth" is calculated using the number of "/" characters in "name".
60 * This allows TOMOYO to avoid comparing two pathnames which never match
61 * (e.g. whether "/var/www/html/index.html" matches "/tmp/sh-thd-\$").
63 struct tomoyo_path_info {
64 const char *name;
65 u32 hash; /* = full_name_hash(name, strlen(name)) */
66 u16 const_len; /* = tomoyo_const_part_length(name) */
67 bool is_dir; /* = tomoyo_strendswith(name, "/") */
68 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
69 u16 depth; /* = tomoyo_path_depth(name) */
73 * This is the max length of a token.
75 * A token consists of only ASCII printable characters.
76 * Non printable characters in a token is represented in \ooo style
77 * octal string. Thus, \ itself is represented as \\.
79 #define TOMOYO_MAX_PATHNAME_LEN 4000
82 * tomoyo_path_info_with_data is a structure which is used for holding a
83 * pathname obtained from "struct dentry" and "struct vfsmount" pair.
85 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
86 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
87 * buffer for the pathname only.
89 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
90 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
91 * so that we don't need to return two pointers to the caller. If the caller
92 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
93 * "struct tomoyo_path_info_with_data".
95 struct tomoyo_path_info_with_data {
96 /* Keep "head" first, for this pointer is passed to tomoyo_free(). */
97 struct tomoyo_path_info head;
98 char barrier1[16]; /* Safeguard for overrun. */
99 char body[TOMOYO_MAX_PATHNAME_LEN];
100 char barrier2[16]; /* Safeguard for overrun. */
104 * tomoyo_acl_info is a structure which is used for holding
106 * (1) "list" which is linked to the ->acl_info_list of
107 * "struct tomoyo_domain_info"
108 * (2) "type" which tells
109 * (a) type & 0x7F : type of the entry (either
110 * "struct tomoyo_single_path_acl_record" or
111 * "struct tomoyo_double_path_acl_record")
112 * (b) type & 0x80 : whether the entry is marked as "deleted".
114 * Packing "struct tomoyo_acl_info" allows
115 * "struct tomoyo_single_path_acl_record" to embed "u16" and
116 * "struct tomoyo_double_path_acl_record" to embed "u8"
117 * without enlarging their structure size.
119 struct tomoyo_acl_info {
120 struct list_head list;
122 * Type of this ACL entry.
124 * MSB is is_deleted flag.
126 u8 type;
127 } __packed;
129 /* This ACL entry is deleted. */
130 #define TOMOYO_ACL_DELETED 0x80
133 * tomoyo_domain_info is a structure which is used for holding permissions
134 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
135 * It has following fields.
137 * (1) "list" which is linked to tomoyo_domain_list .
138 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
139 * (3) "domainname" which holds the name of the domain.
140 * (4) "profile" which remembers profile number assigned to this domain.
141 * (5) "is_deleted" is a bool which is true if this domain is marked as
142 * "deleted", false otherwise.
143 * (6) "quota_warned" is a bool which is used for suppressing warning message
144 * when learning mode learned too much entries.
145 * (7) "flags" which remembers this domain's attributes.
147 * A domain's lifecycle is an analogy of files on / directory.
148 * Multiple domains with the same domainname cannot be created (as with
149 * creating files with the same filename fails with -EEXIST).
150 * If a process reached a domain, that process can reside in that domain after
151 * that domain is marked as "deleted" (as with a process can access an already
152 * open()ed file after that file was unlink()ed).
154 struct tomoyo_domain_info {
155 struct list_head list;
156 struct list_head acl_info_list;
157 /* Name of this domain. Never NULL. */
158 const struct tomoyo_path_info *domainname;
159 u8 profile; /* Profile number to use. */
160 bool is_deleted; /* Delete flag. */
161 bool quota_warned; /* Quota warnning flag. */
162 /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */
163 u8 flags;
166 /* Profile number is an integer between 0 and 255. */
167 #define TOMOYO_MAX_PROFILES 256
169 /* Ignore "allow_read" directive in exception policy. */
170 #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
172 * This domain was unable to create a new domain at tomoyo_find_next_domain()
173 * because the name of the domain to be created was too long or
174 * it could not allocate memory.
175 * More than one process continued execve() without domain transition.
177 #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2
180 * tomoyo_single_path_acl_record is a structure which is used for holding an
181 * entry with one pathname operation (e.g. open(), mkdir()).
182 * It has following fields.
184 * (1) "head" which is a "struct tomoyo_acl_info".
185 * (2) "perm" which is a bitmask of permitted operations.
186 * (3) "filename" is the pathname.
188 * Directives held by this structure are "allow_read/write", "allow_execute",
189 * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
190 * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
191 * "allow_mkchar", "allow_truncate", "allow_symlink" and "allow_rewrite".
193 struct tomoyo_single_path_acl_record {
194 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
195 u16 perm;
196 /* Pointer to single pathname. */
197 const struct tomoyo_path_info *filename;
201 * tomoyo_double_path_acl_record is a structure which is used for holding an
202 * entry with two pathnames operation (i.e. link() and rename()).
203 * It has following fields.
205 * (1) "head" which is a "struct tomoyo_acl_info".
206 * (2) "perm" which is a bitmask of permitted operations.
207 * (3) "filename1" is the source/old pathname.
208 * (4) "filename2" is the destination/new pathname.
210 * Directives held by this structure are "allow_rename" and "allow_link".
212 struct 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;
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)
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
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.
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 ).
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.
267 struct 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 /* The position currently reading from. */
273 struct list_head *read_var1;
274 /* Extra variables for reading. */
275 struct list_head *read_var2;
276 /* The position currently writing to. */
277 struct tomoyo_domain_info *write_var1;
278 /* The step for reading. */
279 int read_step;
280 /* Buffer for reading. */
281 char *read_buf;
282 /* EOF flag for reading. */
283 bool read_eof;
284 /* Read domain ACL of specified PID? */
285 bool read_single_domain;
286 /* Extra variable for reading. */
287 u8 read_bit;
288 /* Bytes available for reading. */
289 int read_avail;
290 /* Size of read buffer. */
291 int readbuf_size;
292 /* Buffer for writing. */
293 char *write_buf;
294 /* Bytes available for writing. */
295 int write_avail;
296 /* Size of write buffer. */
297 int writebuf_size;
300 /* Check whether the domain has too many ACL entries to hold. */
301 bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
302 /* Transactional sprintf() for policy dump. */
303 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
304 __attribute__ ((format(printf, 2, 3)));
305 /* Check whether the domainname is correct. */
306 bool tomoyo_is_correct_domain(const unsigned char *domainname,
307 const char *function);
308 /* Check whether the token is correct. */
309 bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
310 const s8 pattern_type, const s8 end_type,
311 const char *function);
312 /* Check whether the token can be a domainname. */
313 bool tomoyo_is_domain_def(const unsigned char *buffer);
314 /* Check whether the given filename matches the given pattern. */
315 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
316 const struct tomoyo_path_info *pattern);
317 /* Read "alias" entry in exception policy. */
318 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
320 * Read "initialize_domain" and "no_initialize_domain" entry
321 * in exception policy.
323 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
324 /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
325 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
326 /* Read "file_pattern" entry in exception policy. */
327 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
328 /* Read "allow_read" entry in exception policy. */
329 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
330 /* Read "deny_rewrite" entry in exception policy. */
331 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
332 /* Write domain policy violation warning message to console? */
333 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
334 /* Convert double path operation to operation name. */
335 const char *tomoyo_dp2keyword(const u8 operation);
336 /* Get the last component of the given domainname. */
337 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
338 /* Get warning message. */
339 const char *tomoyo_get_msg(const bool is_enforce);
340 /* Convert single path operation to operation name. */
341 const char *tomoyo_sp2keyword(const u8 operation);
342 /* Create "alias" entry in exception policy. */
343 int tomoyo_write_alias_policy(char *data, const bool is_delete);
345 * Create "initialize_domain" and "no_initialize_domain" entry
346 * in exception policy.
348 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
349 const bool is_delete);
350 /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
351 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
352 const bool is_delete);
354 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
355 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
356 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
357 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
358 * "allow_link" entry in domain policy.
360 int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
361 const bool is_delete);
362 /* Create "allow_read" entry in exception policy. */
363 int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
364 /* Create "deny_rewrite" entry in exception policy. */
365 int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
366 /* Create "file_pattern" entry in exception policy. */
367 int tomoyo_write_pattern_policy(char *data, const bool is_delete);
368 /* Find a domain by the given name. */
369 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
370 /* Find or create a domain by the given name. */
371 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
372 domainname,
373 const u8 profile);
374 /* Check mode for specified functionality. */
375 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
376 const u8 index);
377 /* Allocate memory for structures. */
378 void *tomoyo_alloc_acl_element(const u8 acl_type);
379 /* Fill in "struct tomoyo_path_info" members. */
380 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
381 /* Run policy loader when /sbin/init starts. */
382 void tomoyo_load_policy(const char *filename);
383 /* Change "struct tomoyo_domain_info"->flags. */
384 void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
385 const bool is_delete, const u8 flags);
387 /* strcmp() for "struct tomoyo_path_info" structure. */
388 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
389 const struct tomoyo_path_info *b)
391 return a->hash != b->hash || strcmp(a->name, b->name);
394 /* Get type of an ACL entry. */
395 static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr)
397 return ptr->type & ~TOMOYO_ACL_DELETED;
400 /* Get type of an ACL entry. */
401 static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr)
403 return ptr->type;
407 * tomoyo_is_valid - Check whether the character is a valid char.
409 * @c: The character to check.
411 * Returns true if @c is a valid character, false otherwise.
413 static inline bool tomoyo_is_valid(const unsigned char c)
415 return c > ' ' && c < 127;
419 * tomoyo_is_invalid - Check whether the character is an invalid char.
421 * @c: The character to check.
423 * Returns true if @c is an invalid character, false otherwise.
425 static inline bool tomoyo_is_invalid(const unsigned char c)
427 return c && (c <= ' ' || c >= 127);
430 /* The list for "struct tomoyo_domain_info". */
431 extern struct list_head tomoyo_domain_list;
432 extern struct rw_semaphore tomoyo_domain_list_lock;
434 /* Lock for domain->acl_info_list. */
435 extern struct rw_semaphore tomoyo_domain_acl_info_list_lock;
437 /* Has /sbin/init started? */
438 extern bool tomoyo_policy_loaded;
440 /* The kernel's domain. */
441 extern struct tomoyo_domain_info tomoyo_kernel_domain;
444 * list_for_each_cookie - iterate over a list with cookie.
445 * @pos: the &struct list_head to use as a loop cursor.
446 * @cookie: the &struct list_head to use as a cookie.
447 * @head: the head for your list.
449 * Same with list_for_each() except that this primitive uses @cookie
450 * so that we can continue iteration.
451 * @cookie must be NULL when iteration starts, and @cookie will become
452 * NULL when iteration finishes.
454 #define list_for_each_cookie(pos, cookie, head) \
455 for (({ if (!cookie) \
456 cookie = head; }), \
457 pos = (cookie)->next; \
458 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
459 (cookie) = pos, pos = pos->next)
461 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */