blob: f6aff59b08859046c122a171f1c5ce19f11cddc7 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.h
3 *
Tetsuo Handa76bb0892010-02-11 09:42:40 +09004 * Header file for TOMOYO.
Kentaro Takeda95908372009-02-05 17:18:13 +09005 *
Tetsuo Handa76bb0892010-02-11 09:42:40 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda95908372009-02-05 17:18:13 +09007 */
8
9#ifndef _SECURITY_TOMOYO_COMMON_H
10#define _SECURITY_TOMOYO_COMMON_H
11
12#include <linux/ctype.h>
13#include <linux/string.h>
14#include <linux/mm.h>
15#include <linux/file.h>
16#include <linux/kmod.h>
17#include <linux/fs.h>
18#include <linux/sched.h>
19#include <linux/namei.h>
20#include <linux/mount.h>
21#include <linux/list.h>
Tetsuo Handa76bb0892010-02-11 09:42:40 +090022#include <linux/cred.h>
23struct linux_binprm;
Kentaro Takeda95908372009-02-05 17:18:13 +090024
Tetsuo Handa76bb0892010-02-11 09:42:40 +090025/********** Constants definitions. **********/
26
27/*
28 * TOMOYO uses this hash only when appending a string into the string
29 * table. Frequency of appending strings is very low. So we don't need
30 * large (e.g. 64k) hash size. 256 will be sufficient.
31 */
32#define TOMOYO_HASH_BITS 8
33#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
34
35/*
36 * This is the max length of a token.
37 *
38 * A token consists of only ASCII printable characters.
39 * Non printable characters in a token is represented in \ooo style
40 * octal string. Thus, \ itself is represented as \\.
41 */
42#define TOMOYO_MAX_PATHNAME_LEN 4000
43
44/* Profile number is an integer between 0 and 255. */
45#define TOMOYO_MAX_PROFILES 256
46
47/* Keywords for ACLs. */
48#define TOMOYO_KEYWORD_ALIAS "alias "
49#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
50#define TOMOYO_KEYWORD_DELETE "delete "
51#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
52#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
53#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
54#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
55#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
56#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
57#define TOMOYO_KEYWORD_SELECT "select "
58#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
59#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
60/* A domain definition starts with <kernel>. */
61#define TOMOYO_ROOT_NAME "<kernel>"
62#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
63
64/* Index numbers for Access Controls. */
65#define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */
66#define TOMOYO_MAX_ACCEPT_ENTRY 1
67#define TOMOYO_VERBOSE 2
68#define TOMOYO_MAX_CONTROL_INDEX 3
69
70/* Index numbers for Access Controls. */
71
72#define TOMOYO_TYPE_SINGLE_PATH_ACL 0
73#define TOMOYO_TYPE_DOUBLE_PATH_ACL 1
74
75/* Index numbers for File Controls. */
76
77/*
78 * TYPE_READ_WRITE_ACL is special. TYPE_READ_WRITE_ACL is automatically set
79 * if both TYPE_READ_ACL and TYPE_WRITE_ACL are set. Both TYPE_READ_ACL and
80 * TYPE_WRITE_ACL are automatically set if TYPE_READ_WRITE_ACL is set.
81 * TYPE_READ_WRITE_ACL is automatically cleared if either TYPE_READ_ACL or
82 * TYPE_WRITE_ACL is cleared. Both TYPE_READ_ACL and TYPE_WRITE_ACL are
83 * automatically cleared if TYPE_READ_WRITE_ACL is cleared.
84 */
85
86#define TOMOYO_TYPE_READ_WRITE_ACL 0
87#define TOMOYO_TYPE_EXECUTE_ACL 1
88#define TOMOYO_TYPE_READ_ACL 2
89#define TOMOYO_TYPE_WRITE_ACL 3
90#define TOMOYO_TYPE_CREATE_ACL 4
91#define TOMOYO_TYPE_UNLINK_ACL 5
92#define TOMOYO_TYPE_MKDIR_ACL 6
93#define TOMOYO_TYPE_RMDIR_ACL 7
94#define TOMOYO_TYPE_MKFIFO_ACL 8
95#define TOMOYO_TYPE_MKSOCK_ACL 9
96#define TOMOYO_TYPE_MKBLOCK_ACL 10
97#define TOMOYO_TYPE_MKCHAR_ACL 11
98#define TOMOYO_TYPE_TRUNCATE_ACL 12
99#define TOMOYO_TYPE_SYMLINK_ACL 13
100#define TOMOYO_TYPE_REWRITE_ACL 14
101#define TOMOYO_TYPE_IOCTL_ACL 15
102#define TOMOYO_TYPE_CHMOD_ACL 16
103#define TOMOYO_TYPE_CHOWN_ACL 17
104#define TOMOYO_TYPE_CHGRP_ACL 18
105#define TOMOYO_TYPE_CHROOT_ACL 19
106#define TOMOYO_TYPE_MOUNT_ACL 20
107#define TOMOYO_TYPE_UMOUNT_ACL 21
108#define TOMOYO_MAX_SINGLE_PATH_OPERATION 22
109
110#define TOMOYO_TYPE_LINK_ACL 0
111#define TOMOYO_TYPE_RENAME_ACL 1
112#define TOMOYO_TYPE_PIVOT_ROOT_ACL 2
113#define TOMOYO_MAX_DOUBLE_PATH_OPERATION 3
114
115#define TOMOYO_DOMAINPOLICY 0
116#define TOMOYO_EXCEPTIONPOLICY 1
117#define TOMOYO_DOMAIN_STATUS 2
118#define TOMOYO_PROCESS_STATUS 3
119#define TOMOYO_MEMINFO 4
120#define TOMOYO_SELFDOMAIN 5
121#define TOMOYO_VERSION 6
122#define TOMOYO_PROFILE 7
123#define TOMOYO_MANAGER 8
124
125/********** Structure definitions. **********/
Kentaro Takeda95908372009-02-05 17:18:13 +0900126
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900127/*
128 * tomoyo_page_buffer is a structure which is used for holding a pathname
129 * obtained from "struct dentry" and "struct vfsmount" pair.
130 * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
131 * (because TOMOYO escapes non ASCII printable characters using \ooo format),
132 * we will make the buffer larger.
133 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900134struct tomoyo_page_buffer {
135 char buffer[4096];
136};
137
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900138/*
139 * tomoyo_path_info is a structure which is used for holding a string data
140 * used by TOMOYO.
141 * This structure has several fields for supporting pattern matching.
142 *
143 * (1) "name" is the '\0' terminated string data.
144 * (2) "hash" is full_name_hash(name, strlen(name)).
145 * This allows tomoyo_pathcmp() to compare by hash before actually compare
146 * using strcmp().
147 * (3) "const_len" is the length of the initial segment of "name" which
148 * consists entirely of non wildcard characters. In other words, the length
149 * which we can compare two strings using strncmp().
150 * (4) "is_dir" is a bool which is true if "name" ends with "/",
151 * false otherwise.
152 * TOMOYO distinguishes directory and non-directory. A directory ends with
153 * "/" and non-directory does not end with "/".
154 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
155 * characters, false otherwise. This allows TOMOYO to use "hash" and
156 * strcmp() for string comparison if "is_patterned" is false.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900157 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900158struct tomoyo_path_info {
159 const char *name;
160 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900161 u16 const_len; /* = tomoyo_const_part_length(name) */
162 bool is_dir; /* = tomoyo_strendswith(name, "/") */
163 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900164};
165
166/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900167 * tomoyo_name_entry is a structure which is used for linking
168 * "struct tomoyo_path_info" into tomoyo_name_list .
Kentaro Takeda95908372009-02-05 17:18:13 +0900169 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900170struct tomoyo_name_entry {
171 struct list_head list;
172 atomic_t users;
173 struct tomoyo_path_info entry;
174};
Kentaro Takeda95908372009-02-05 17:18:13 +0900175
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900176/*
177 * tomoyo_path_info_with_data is a structure which is used for holding a
178 * pathname obtained from "struct dentry" and "struct vfsmount" pair.
179 *
180 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
181 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
182 * buffer for the pathname only.
183 *
184 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
185 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
186 * so that we don't need to return two pointers to the caller. If the caller
187 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
188 * "struct tomoyo_path_info_with_data".
189 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900190struct tomoyo_path_info_with_data {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900191 /* Keep "head" first, for this pointer is passed to kfree(). */
Kentaro Takeda95908372009-02-05 17:18:13 +0900192 struct tomoyo_path_info head;
Tetsuo Handaa106cbf2009-03-27 13:12:16 +0900193 char barrier1[16]; /* Safeguard for overrun. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900194 char body[TOMOYO_MAX_PATHNAME_LEN];
195 char barrier2[16]; /* Safeguard for overrun. */
196};
197
198/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900199 * tomoyo_acl_info is a structure which is used for holding
200 *
201 * (1) "list" which is linked to the ->acl_info_list of
202 * "struct tomoyo_domain_info"
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900203 * (2) "type" which tells type of the entry (either
204 * "struct tomoyo_single_path_acl_record" or
205 * "struct tomoyo_double_path_acl_record").
Kentaro Takeda95908372009-02-05 17:18:13 +0900206 *
207 * Packing "struct tomoyo_acl_info" allows
Tetsuo Handa937bf612009-12-02 21:09:48 +0900208 * "struct tomoyo_single_path_acl_record" to embed "u8" + "u16" and
Kentaro Takeda95908372009-02-05 17:18:13 +0900209 * "struct tomoyo_double_path_acl_record" to embed "u8"
210 * without enlarging their structure size.
211 */
212struct tomoyo_acl_info {
213 struct list_head list;
Kentaro Takeda95908372009-02-05 17:18:13 +0900214 u8 type;
215} __packed;
216
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900217/*
218 * tomoyo_domain_info is a structure which is used for holding permissions
219 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
220 * It has following fields.
221 *
222 * (1) "list" which is linked to tomoyo_domain_list .
223 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
224 * (3) "domainname" which holds the name of the domain.
225 * (4) "profile" which remembers profile number assigned to this domain.
226 * (5) "is_deleted" is a bool which is true if this domain is marked as
227 * "deleted", false otherwise.
228 * (6) "quota_warned" is a bool which is used for suppressing warning message
229 * when learning mode learned too much entries.
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900230 * (7) "ignore_global_allow_read" is a bool which is true if this domain
231 * should ignore "allow_read" directive in exception policy.
232 * (8) "transition_failed" is a bool which is set to true when this domain was
233 * unable to create a new domain at tomoyo_find_next_domain() because the
234 * name of the domain to be created was too long or it could not allocate
235 * memory. If set to true, more than one process continued execve()
236 * without domain transition.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900237 *
238 * A domain's lifecycle is an analogy of files on / directory.
239 * Multiple domains with the same domainname cannot be created (as with
240 * creating files with the same filename fails with -EEXIST).
241 * If a process reached a domain, that process can reside in that domain after
242 * that domain is marked as "deleted" (as with a process can access an already
243 * open()ed file after that file was unlink()ed).
244 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900245struct tomoyo_domain_info {
246 struct list_head list;
247 struct list_head acl_info_list;
248 /* Name of this domain. Never NULL. */
249 const struct tomoyo_path_info *domainname;
250 u8 profile; /* Profile number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900251 bool is_deleted; /* Delete flag. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900252 bool quota_warned; /* Quota warnning flag. */
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900253 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
254 bool transition_failed; /* Domain transition failed flag. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900255};
256
Kentaro Takeda95908372009-02-05 17:18:13 +0900257/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900258 * tomoyo_single_path_acl_record is a structure which is used for holding an
259 * entry with one pathname operation (e.g. open(), mkdir()).
260 * It has following fields.
261 *
262 * (1) "head" which is a "struct tomoyo_acl_info".
263 * (2) "perm" which is a bitmask of permitted operations.
264 * (3) "filename" is the pathname.
265 *
266 * Directives held by this structure are "allow_read/write", "allow_execute",
267 * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
268 * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
Tetsuo Handa937bf612009-12-02 21:09:48 +0900269 * "allow_mkchar", "allow_truncate", "allow_symlink", "allow_rewrite",
270 * "allow_chmod", "allow_chown", "allow_chgrp", "allow_chroot", "allow_mount"
271 * and "allow_unmount".
Kentaro Takeda95908372009-02-05 17:18:13 +0900272 */
273struct tomoyo_single_path_acl_record {
274 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
Tetsuo Handa937bf612009-12-02 21:09:48 +0900275 u8 perm_high;
Kentaro Takeda95908372009-02-05 17:18:13 +0900276 u16 perm;
277 /* Pointer to single pathname. */
278 const struct tomoyo_path_info *filename;
279};
280
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900281/*
282 * tomoyo_double_path_acl_record is a structure which is used for holding an
Tetsuo Handa937bf612009-12-02 21:09:48 +0900283 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900284 * It has following fields.
285 *
286 * (1) "head" which is a "struct tomoyo_acl_info".
287 * (2) "perm" which is a bitmask of permitted operations.
288 * (3) "filename1" is the source/old pathname.
289 * (4) "filename2" is the destination/new pathname.
290 *
Tetsuo Handa937bf612009-12-02 21:09:48 +0900291 * Directives held by this structure are "allow_rename", "allow_link" and
292 * "allow_pivot_root".
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900293 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900294struct tomoyo_double_path_acl_record {
295 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
296 u8 perm;
297 /* Pointer to single pathname. */
298 const struct tomoyo_path_info *filename1;
299 /* Pointer to single pathname. */
300 const struct tomoyo_path_info *filename2;
301};
302
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900303/*
304 * tomoyo_io_buffer is a structure which is used for reading and modifying
305 * configuration via /sys/kernel/security/tomoyo/ interface.
306 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
307 * cursors.
308 *
309 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
310 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
311 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
312 * reading (one is for traversing tomoyo_domain_list and the other is for
313 * traversing "struct tomoyo_acl_info"->acl_info_list ).
314 *
315 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
316 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
317 * domain with the domainname specified by the rest of that line (NULL is set
318 * if seek failed).
319 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
320 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
321 * line (->write_var1 is set to NULL if a domain was deleted).
322 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
323 * neither "select " nor "delete ", an entry or a domain specified by that line
324 * is appended.
325 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900326struct tomoyo_io_buffer {
327 int (*read) (struct tomoyo_io_buffer *);
328 int (*write) (struct tomoyo_io_buffer *);
329 /* Exclusive lock for this structure. */
330 struct mutex io_sem;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900331 /* Index returned by tomoyo_read_lock(). */
332 int reader_idx;
Kentaro Takeda95908372009-02-05 17:18:13 +0900333 /* The position currently reading from. */
334 struct list_head *read_var1;
335 /* Extra variables for reading. */
336 struct list_head *read_var2;
337 /* The position currently writing to. */
338 struct tomoyo_domain_info *write_var1;
339 /* The step for reading. */
340 int read_step;
341 /* Buffer for reading. */
342 char *read_buf;
343 /* EOF flag for reading. */
344 bool read_eof;
345 /* Read domain ACL of specified PID? */
346 bool read_single_domain;
347 /* Extra variable for reading. */
348 u8 read_bit;
349 /* Bytes available for reading. */
350 int read_avail;
351 /* Size of read buffer. */
352 int readbuf_size;
353 /* Buffer for writing. */
354 char *write_buf;
355 /* Bytes available for writing. */
356 int write_avail;
357 /* Size of write buffer. */
358 int writebuf_size;
359};
360
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900361/*
362 * tomoyo_globally_readable_file_entry is a structure which is used for holding
363 * "allow_read" entries.
364 * It has following fields.
365 *
366 * (1) "list" which is linked to tomoyo_globally_readable_list .
367 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
368 * (3) "is_deleted" is a bool which is true if marked as deleted, false
369 * otherwise.
370 */
371struct tomoyo_globally_readable_file_entry {
372 struct list_head list;
373 const struct tomoyo_path_info *filename;
374 bool is_deleted;
375};
376
377/*
378 * tomoyo_pattern_entry is a structure which is used for holding
379 * "tomoyo_pattern_list" entries.
380 * It has following fields.
381 *
382 * (1) "list" which is linked to tomoyo_pattern_list .
383 * (2) "pattern" is a pathname pattern which is used for converting pathnames
384 * to pathname patterns during learning mode.
385 * (3) "is_deleted" is a bool which is true if marked as deleted, false
386 * otherwise.
387 */
388struct tomoyo_pattern_entry {
389 struct list_head list;
390 const struct tomoyo_path_info *pattern;
391 bool is_deleted;
392};
393
394/*
395 * tomoyo_no_rewrite_entry is a structure which is used for holding
396 * "deny_rewrite" entries.
397 * It has following fields.
398 *
399 * (1) "list" which is linked to tomoyo_no_rewrite_list .
400 * (2) "pattern" is a pathname which is by default not permitted to modify
401 * already existing content.
402 * (3) "is_deleted" is a bool which is true if marked as deleted, false
403 * otherwise.
404 */
405struct tomoyo_no_rewrite_entry {
406 struct list_head list;
407 const struct tomoyo_path_info *pattern;
408 bool is_deleted;
409};
410
411/*
412 * tomoyo_domain_initializer_entry is a structure which is used for holding
413 * "initialize_domain" and "no_initialize_domain" entries.
414 * It has following fields.
415 *
416 * (1) "list" which is linked to tomoyo_domain_initializer_list .
417 * (2) "domainname" which is "a domainname" or "the last component of a
418 * domainname". This field is NULL if "from" clause is not specified.
419 * (3) "program" which is a program's pathname.
420 * (4) "is_deleted" is a bool which is true if marked as deleted, false
421 * otherwise.
422 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
423 * otherwise.
424 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
425 * component of a domainname", false otherwise.
426 */
427struct tomoyo_domain_initializer_entry {
428 struct list_head list;
429 const struct tomoyo_path_info *domainname; /* This may be NULL */
430 const struct tomoyo_path_info *program;
431 bool is_deleted;
432 bool is_not; /* True if this entry is "no_initialize_domain". */
433 /* True if the domainname is tomoyo_get_last_name(). */
434 bool is_last_name;
435};
436
437/*
438 * tomoyo_domain_keeper_entry is a structure which is used for holding
439 * "keep_domain" and "no_keep_domain" entries.
440 * It has following fields.
441 *
442 * (1) "list" which is linked to tomoyo_domain_keeper_list .
443 * (2) "domainname" which is "a domainname" or "the last component of a
444 * domainname".
445 * (3) "program" which is a program's pathname.
446 * This field is NULL if "from" clause is not specified.
447 * (4) "is_deleted" is a bool which is true if marked as deleted, false
448 * otherwise.
449 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
450 * otherwise.
451 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
452 * component of a domainname", false otherwise.
453 */
454struct tomoyo_domain_keeper_entry {
455 struct list_head list;
456 const struct tomoyo_path_info *domainname;
457 const struct tomoyo_path_info *program; /* This may be NULL */
458 bool is_deleted;
459 bool is_not; /* True if this entry is "no_keep_domain". */
460 /* True if the domainname is tomoyo_get_last_name(). */
461 bool is_last_name;
462};
463
464/*
465 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
466 * It has following fields.
467 *
468 * (1) "list" which is linked to tomoyo_alias_list .
469 * (2) "original_name" which is a dereferenced pathname.
470 * (3) "aliased_name" which is a symlink's pathname.
471 * (4) "is_deleted" is a bool which is true if marked as deleted, false
472 * otherwise.
473 */
474struct tomoyo_alias_entry {
475 struct list_head list;
476 const struct tomoyo_path_info *original_name;
477 const struct tomoyo_path_info *aliased_name;
478 bool is_deleted;
479};
480
481/*
482 * tomoyo_policy_manager_entry is a structure which is used for holding list of
483 * domainnames or programs which are permitted to modify configuration via
484 * /sys/kernel/security/tomoyo/ interface.
485 * It has following fields.
486 *
487 * (1) "list" which is linked to tomoyo_policy_manager_list .
488 * (2) "manager" is a domainname or a program's pathname.
489 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
490 * otherwise.
491 * (4) "is_deleted" is a bool which is true if marked as deleted, false
492 * otherwise.
493 */
494struct tomoyo_policy_manager_entry {
495 struct list_head list;
496 /* A path to program or a domainname. */
497 const struct tomoyo_path_info *manager;
498 bool is_domain; /* True if manager is a domainname. */
499 bool is_deleted; /* True if this entry is deleted. */
500};
501
502/********** Function prototypes. **********/
503
Kentaro Takeda95908372009-02-05 17:18:13 +0900504/* Check whether the domain has too many ACL entries to hold. */
505bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
506/* Transactional sprintf() for policy dump. */
507bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
508 __attribute__ ((format(printf, 2, 3)));
509/* Check whether the domainname is correct. */
510bool tomoyo_is_correct_domain(const unsigned char *domainname,
511 const char *function);
512/* Check whether the token is correct. */
513bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
514 const s8 pattern_type, const s8 end_type,
515 const char *function);
516/* Check whether the token can be a domainname. */
517bool tomoyo_is_domain_def(const unsigned char *buffer);
518/* Check whether the given filename matches the given pattern. */
519bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
520 const struct tomoyo_path_info *pattern);
521/* Read "alias" entry in exception policy. */
522bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
523/*
524 * Read "initialize_domain" and "no_initialize_domain" entry
525 * in exception policy.
526 */
527bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
528/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
529bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
530/* Read "file_pattern" entry in exception policy. */
531bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
532/* Read "allow_read" entry in exception policy. */
533bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
534/* Read "deny_rewrite" entry in exception policy. */
535bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
536/* Write domain policy violation warning message to console? */
537bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
538/* Convert double path operation to operation name. */
539const char *tomoyo_dp2keyword(const u8 operation);
540/* Get the last component of the given domainname. */
541const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
542/* Get warning message. */
543const char *tomoyo_get_msg(const bool is_enforce);
544/* Convert single path operation to operation name. */
545const char *tomoyo_sp2keyword(const u8 operation);
Kentaro Takeda95908372009-02-05 17:18:13 +0900546/* Create "alias" entry in exception policy. */
547int tomoyo_write_alias_policy(char *data, const bool is_delete);
548/*
549 * Create "initialize_domain" and "no_initialize_domain" entry
550 * in exception policy.
551 */
552int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
553 const bool is_delete);
554/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
555int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
556 const bool is_delete);
557/*
558 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
559 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
560 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
561 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
562 * "allow_link" entry in domain policy.
563 */
564int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
565 const bool is_delete);
566/* Create "allow_read" entry in exception policy. */
567int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
568/* Create "deny_rewrite" entry in exception policy. */
569int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
570/* Create "file_pattern" entry in exception policy. */
571int tomoyo_write_pattern_policy(char *data, const bool is_delete);
572/* Find a domain by the given name. */
573struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
574/* Find or create a domain by the given name. */
575struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
576 domainname,
577 const u8 profile);
Kentaro Takeda95908372009-02-05 17:18:13 +0900578/* Check mode for specified functionality. */
579unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
580 const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900581/* Fill in "struct tomoyo_path_info" members. */
582void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
583/* Run policy loader when /sbin/init starts. */
584void tomoyo_load_policy(const char *filename);
Kentaro Takeda95908372009-02-05 17:18:13 +0900585
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900586/* Convert binary string to ascii string. */
587int tomoyo_encode(char *buffer, int buflen, const char *str);
588
589/* Returns realpath(3) of the given pathname but ignores chroot'ed root. */
590int tomoyo_realpath_from_path2(struct path *path, char *newname,
591 int newname_len);
592
593/*
594 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
595 * These functions use kzalloc(), so the caller must call kfree()
596 * if these functions didn't return NULL.
597 */
598char *tomoyo_realpath(const char *pathname);
599/*
600 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
601 */
602char *tomoyo_realpath_nofollow(const char *pathname);
603/* Same with tomoyo_realpath() except that the pathname is already solved. */
604char *tomoyo_realpath_from_path(struct path *path);
605
606/* Check memory quota. */
607bool tomoyo_memory_ok(void *ptr);
608
609/*
610 * Keep the given name on the RAM.
611 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
612 */
613const struct tomoyo_path_info *tomoyo_get_name(const char *name);
614
615/* Check for memory usage. */
616int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
617
618/* Set memory quota. */
619int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
620
621/* Initialize realpath related code. */
622void __init tomoyo_realpath_init(void);
623int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
624 const struct tomoyo_path_info *filename);
625int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
626 struct path *path, const int flag);
627int tomoyo_check_1path_perm(struct tomoyo_domain_info *domain,
628 const u8 operation, struct path *path);
629int tomoyo_check_2path_perm(struct tomoyo_domain_info *domain,
630 const u8 operation, struct path *path1,
631 struct path *path2);
632int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain,
633 struct file *filp);
634int tomoyo_find_next_domain(struct linux_binprm *bprm);
635
636/********** External variable definitions. **********/
637
638/* Lock for GC. */
639extern struct srcu_struct tomoyo_ss;
640
641/* The list for "struct tomoyo_domain_info". */
642extern struct list_head tomoyo_domain_list;
643
644/* Lock for protecting policy. */
645extern struct mutex tomoyo_policy_lock;
646
647/* Has /sbin/init started? */
648extern bool tomoyo_policy_loaded;
649
650/* The kernel's domain. */
651extern struct tomoyo_domain_info tomoyo_kernel_domain;
652
653/********** Inlined functions. **********/
654
655static inline int tomoyo_read_lock(void)
656{
657 return srcu_read_lock(&tomoyo_ss);
658}
659
660static inline void tomoyo_read_unlock(int idx)
661{
662 srcu_read_unlock(&tomoyo_ss, idx);
663}
664
Kentaro Takeda95908372009-02-05 17:18:13 +0900665/* strcmp() for "struct tomoyo_path_info" structure. */
666static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
667 const struct tomoyo_path_info *b)
668{
669 return a->hash != b->hash || strcmp(a->name, b->name);
670}
671
Kentaro Takeda95908372009-02-05 17:18:13 +0900672/**
673 * tomoyo_is_valid - Check whether the character is a valid char.
674 *
675 * @c: The character to check.
676 *
677 * Returns true if @c is a valid character, false otherwise.
678 */
679static inline bool tomoyo_is_valid(const unsigned char c)
680{
681 return c > ' ' && c < 127;
682}
683
684/**
685 * tomoyo_is_invalid - Check whether the character is an invalid char.
686 *
687 * @c: The character to check.
688 *
689 * Returns true if @c is an invalid character, false otherwise.
690 */
691static inline bool tomoyo_is_invalid(const unsigned char c)
692{
693 return c && (c <= ' ' || c >= 127);
694}
695
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900696static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
697{
698 if (name) {
699 struct tomoyo_name_entry *ptr =
700 container_of(name, struct tomoyo_name_entry, entry);
701 atomic_dec(&ptr->users);
702 }
703}
Kentaro Takeda95908372009-02-05 17:18:13 +0900704
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900705static inline struct tomoyo_domain_info *tomoyo_domain(void)
706{
707 return current_cred()->security;
708}
Kentaro Takeda95908372009-02-05 17:18:13 +0900709
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900710static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
711 *task)
712{
713 return task_cred_xxx(task, security);
714}
Kentaro Takeda95908372009-02-05 17:18:13 +0900715
716/**
717 * list_for_each_cookie - iterate over a list with cookie.
718 * @pos: the &struct list_head to use as a loop cursor.
719 * @cookie: the &struct list_head to use as a cookie.
720 * @head: the head for your list.
721 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900722 * Same with list_for_each_rcu() except that this primitive uses @cookie
Kentaro Takeda95908372009-02-05 17:18:13 +0900723 * so that we can continue iteration.
724 * @cookie must be NULL when iteration starts, and @cookie will become
725 * NULL when iteration finishes.
726 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900727#define list_for_each_cookie(pos, cookie, head) \
728 for (({ if (!cookie) \
729 cookie = head; }), \
730 pos = rcu_dereference((cookie)->next); \
731 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
732 (cookie) = pos, pos = rcu_dereference(pos->next))
733
Kentaro Takeda95908372009-02-05 17:18:13 +0900734#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */