blob: be03e4a21db0d9586bb0d07a508a0c5e13688751 [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>
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +090023#include <linux/poll.h>
Tetsuo Handa76bb0892010-02-11 09:42:40 +090024struct linux_binprm;
Kentaro Takeda95908372009-02-05 17:18:13 +090025
Tetsuo Handa76bb0892010-02-11 09:42:40 +090026/********** Constants definitions. **********/
27
28/*
29 * TOMOYO uses this hash only when appending a string into the string
30 * table. Frequency of appending strings is very low. So we don't need
31 * large (e.g. 64k) hash size. 256 will be sufficient.
32 */
33#define TOMOYO_HASH_BITS 8
34#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
35
36/*
37 * This is the max length of a token.
38 *
39 * A token consists of only ASCII printable characters.
40 * Non printable characters in a token is represented in \ooo style
41 * octal string. Thus, \ itself is represented as \\.
42 */
43#define TOMOYO_MAX_PATHNAME_LEN 4000
44
45/* Profile number is an integer between 0 and 255. */
46#define TOMOYO_MAX_PROFILES 256
47
Tetsuo Handacb0abe62010-05-17 10:08:05 +090048enum tomoyo_mode_index {
49 TOMOYO_CONFIG_DISABLED,
50 TOMOYO_CONFIG_LEARNING,
51 TOMOYO_CONFIG_PERMISSIVE,
52 TOMOYO_CONFIG_ENFORCING
53};
54
Tetsuo Handa76bb0892010-02-11 09:42:40 +090055/* Keywords for ACLs. */
56#define TOMOYO_KEYWORD_ALIAS "alias "
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090057#define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
Tetsuo Handa76bb0892010-02-11 09:42:40 +090058#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
59#define TOMOYO_KEYWORD_DELETE "delete "
60#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
61#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
62#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
63#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
64#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
65#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090066#define TOMOYO_KEYWORD_PATH_GROUP "path_group "
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090067#define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
Tetsuo Handa76bb0892010-02-11 09:42:40 +090068#define TOMOYO_KEYWORD_SELECT "select "
69#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
70#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
71/* A domain definition starts with <kernel>. */
72#define TOMOYO_ROOT_NAME "<kernel>"
73#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
74
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090075/* Value type definition. */
76#define TOMOYO_VALUE_TYPE_INVALID 0
77#define TOMOYO_VALUE_TYPE_DECIMAL 1
78#define TOMOYO_VALUE_TYPE_OCTAL 2
79#define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
80
Tetsuo Handa76bb0892010-02-11 09:42:40 +090081/* Index numbers for Access Controls. */
Tetsuo Handa084da352010-02-15 15:10:39 +090082enum tomoyo_mac_index {
83 TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
84 TOMOYO_MAX_ACCEPT_ENTRY,
85 TOMOYO_VERBOSE,
86 TOMOYO_MAX_CONTROL_INDEX
87};
Tetsuo Handa76bb0892010-02-11 09:42:40 +090088
89/* Index numbers for Access Controls. */
Tetsuo Handa084da352010-02-15 15:10:39 +090090enum tomoyo_acl_entry_type_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090091 TOMOYO_TYPE_PATH_ACL,
92 TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090093 TOMOYO_TYPE_PATH_NUMBER_ACL,
94 TOMOYO_TYPE_PATH_NUMBER3_ACL,
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090095 TOMOYO_TYPE_MOUNT_ACL,
Tetsuo Handa084da352010-02-15 15:10:39 +090096};
Tetsuo Handa76bb0892010-02-11 09:42:40 +090097
98/* Index numbers for File Controls. */
99
100/*
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900101 * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
102 * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
103 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
104 * TOMOYO_TYPE_READ_WRITE is set.
105 * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
106 * or TOMOYO_TYPE_WRITE is cleared.
107 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
108 * TOMOYO_TYPE_READ_WRITE is cleared.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900109 */
110
Tetsuo Handa084da352010-02-15 15:10:39 +0900111enum tomoyo_path_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900112 TOMOYO_TYPE_READ_WRITE,
113 TOMOYO_TYPE_EXECUTE,
114 TOMOYO_TYPE_READ,
115 TOMOYO_TYPE_WRITE,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900116 TOMOYO_TYPE_UNLINK,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900117 TOMOYO_TYPE_RMDIR,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900118 TOMOYO_TYPE_TRUNCATE,
119 TOMOYO_TYPE_SYMLINK,
120 TOMOYO_TYPE_REWRITE,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900121 TOMOYO_TYPE_CHROOT,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900122 TOMOYO_TYPE_UMOUNT,
123 TOMOYO_MAX_PATH_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900124};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900125
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900126enum tomoyo_path_number3_acl_index {
127 TOMOYO_TYPE_MKBLOCK,
128 TOMOYO_TYPE_MKCHAR,
129 TOMOYO_MAX_PATH_NUMBER3_OPERATION
130};
131
Tetsuo Handa084da352010-02-15 15:10:39 +0900132enum tomoyo_path2_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900133 TOMOYO_TYPE_LINK,
134 TOMOYO_TYPE_RENAME,
135 TOMOYO_TYPE_PIVOT_ROOT,
136 TOMOYO_MAX_PATH2_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900137};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900138
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900139enum tomoyo_path_number_acl_index {
140 TOMOYO_TYPE_CREATE,
141 TOMOYO_TYPE_MKDIR,
142 TOMOYO_TYPE_MKFIFO,
143 TOMOYO_TYPE_MKSOCK,
144 TOMOYO_TYPE_IOCTL,
145 TOMOYO_TYPE_CHMOD,
146 TOMOYO_TYPE_CHOWN,
147 TOMOYO_TYPE_CHGRP,
148 TOMOYO_MAX_PATH_NUMBER_OPERATION
149};
150
Tetsuo Handa084da352010-02-15 15:10:39 +0900151enum tomoyo_securityfs_interface_index {
152 TOMOYO_DOMAINPOLICY,
153 TOMOYO_EXCEPTIONPOLICY,
154 TOMOYO_DOMAIN_STATUS,
155 TOMOYO_PROCESS_STATUS,
156 TOMOYO_MEMINFO,
157 TOMOYO_SELFDOMAIN,
158 TOMOYO_VERSION,
159 TOMOYO_PROFILE,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900160 TOMOYO_QUERY,
Tetsuo Handa084da352010-02-15 15:10:39 +0900161 TOMOYO_MANAGER
162};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900163
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900164#define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
165
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900166/********** Structure definitions. **********/
Kentaro Takeda95908372009-02-05 17:18:13 +0900167
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900168/*
169 * tomoyo_page_buffer is a structure which is used for holding a pathname
170 * obtained from "struct dentry" and "struct vfsmount" pair.
171 * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
172 * (because TOMOYO escapes non ASCII printable characters using \ooo format),
173 * we will make the buffer larger.
174 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900175struct tomoyo_page_buffer {
176 char buffer[4096];
177};
178
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900179/*
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900180 * tomoyo_request_info is a structure which is used for holding
181 *
182 * (1) Domain information of current process.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900183 * (2) How many retries are made for this request.
184 * (3) Profile number used for this request.
185 * (4) Access control mode of the profile.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900186 */
187struct tomoyo_request_info {
188 struct tomoyo_domain_info *domain;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900189 u8 retry;
190 u8 profile;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900191 u8 mode; /* One of tomoyo_mode_index . */
192};
193
194/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900195 * tomoyo_path_info is a structure which is used for holding a string data
196 * used by TOMOYO.
197 * This structure has several fields for supporting pattern matching.
198 *
199 * (1) "name" is the '\0' terminated string data.
200 * (2) "hash" is full_name_hash(name, strlen(name)).
201 * This allows tomoyo_pathcmp() to compare by hash before actually compare
202 * using strcmp().
203 * (3) "const_len" is the length of the initial segment of "name" which
204 * consists entirely of non wildcard characters. In other words, the length
205 * which we can compare two strings using strncmp().
206 * (4) "is_dir" is a bool which is true if "name" ends with "/",
207 * false otherwise.
208 * TOMOYO distinguishes directory and non-directory. A directory ends with
209 * "/" and non-directory does not end with "/".
210 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
211 * characters, false otherwise. This allows TOMOYO to use "hash" and
212 * strcmp() for string comparison if "is_patterned" is false.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900213 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900214struct tomoyo_path_info {
215 const char *name;
216 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900217 u16 const_len; /* = tomoyo_const_part_length(name) */
218 bool is_dir; /* = tomoyo_strendswith(name, "/") */
219 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900220};
221
222/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900223 * tomoyo_name_entry is a structure which is used for linking
224 * "struct tomoyo_path_info" into tomoyo_name_list .
Kentaro Takeda95908372009-02-05 17:18:13 +0900225 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900226struct tomoyo_name_entry {
227 struct list_head list;
228 atomic_t users;
229 struct tomoyo_path_info entry;
230};
Kentaro Takeda95908372009-02-05 17:18:13 +0900231
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900232/*
233 * tomoyo_path_info_with_data is a structure which is used for holding a
234 * pathname obtained from "struct dentry" and "struct vfsmount" pair.
235 *
236 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
237 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
238 * buffer for the pathname only.
239 *
240 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
241 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
242 * so that we don't need to return two pointers to the caller. If the caller
243 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
244 * "struct tomoyo_path_info_with_data".
245 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900246struct tomoyo_path_info_with_data {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900247 /* Keep "head" first, for this pointer is passed to kfree(). */
Kentaro Takeda95908372009-02-05 17:18:13 +0900248 struct tomoyo_path_info head;
Tetsuo Handaa106cbf2009-03-27 13:12:16 +0900249 char barrier1[16]; /* Safeguard for overrun. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900250 char body[TOMOYO_MAX_PATHNAME_LEN];
251 char barrier2[16]; /* Safeguard for overrun. */
252};
253
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900254struct tomoyo_name_union {
255 const struct tomoyo_path_info *filename;
256 struct tomoyo_path_group *group;
257 u8 is_group;
258};
259
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900260struct tomoyo_number_union {
261 unsigned long values[2];
262 struct tomoyo_number_group *group;
263 u8 min_type;
264 u8 max_type;
265 u8 is_group;
266};
267
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900268/* Structure for "path_group" directive. */
269struct tomoyo_path_group {
270 struct list_head list;
271 const struct tomoyo_path_info *group_name;
272 struct list_head member_list;
273 atomic_t users;
274};
275
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900276/* Structure for "number_group" directive. */
277struct tomoyo_number_group {
278 struct list_head list;
279 const struct tomoyo_path_info *group_name;
280 struct list_head member_list;
281 atomic_t users;
282};
283
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900284/* Structure for "path_group" directive. */
285struct tomoyo_path_group_member {
286 struct list_head list;
287 bool is_deleted;
288 const struct tomoyo_path_info *member_name;
289};
290
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900291/* Structure for "number_group" directive. */
292struct tomoyo_number_group_member {
293 struct list_head list;
294 bool is_deleted;
295 struct tomoyo_number_union number;
296};
297
Kentaro Takeda95908372009-02-05 17:18:13 +0900298/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900299 * tomoyo_acl_info is a structure which is used for holding
300 *
301 * (1) "list" which is linked to the ->acl_info_list of
302 * "struct tomoyo_domain_info"
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900303 * (2) "type" which tells type of the entry (either
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900304 * "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
Kentaro Takeda95908372009-02-05 17:18:13 +0900305 *
306 * Packing "struct tomoyo_acl_info" allows
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900307 * "struct tomoyo_path_acl" to embed "u8" + "u16" and
308 * "struct tomoyo_path2_acl" to embed "u8"
Kentaro Takeda95908372009-02-05 17:18:13 +0900309 * without enlarging their structure size.
310 */
311struct tomoyo_acl_info {
312 struct list_head list;
Kentaro Takeda95908372009-02-05 17:18:13 +0900313 u8 type;
314} __packed;
315
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900316/*
317 * tomoyo_domain_info is a structure which is used for holding permissions
318 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
319 * It has following fields.
320 *
321 * (1) "list" which is linked to tomoyo_domain_list .
322 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
323 * (3) "domainname" which holds the name of the domain.
324 * (4) "profile" which remembers profile number assigned to this domain.
325 * (5) "is_deleted" is a bool which is true if this domain is marked as
326 * "deleted", false otherwise.
327 * (6) "quota_warned" is a bool which is used for suppressing warning message
328 * when learning mode learned too much entries.
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900329 * (7) "ignore_global_allow_read" is a bool which is true if this domain
330 * should ignore "allow_read" directive in exception policy.
331 * (8) "transition_failed" is a bool which is set to true when this domain was
332 * unable to create a new domain at tomoyo_find_next_domain() because the
333 * name of the domain to be created was too long or it could not allocate
334 * memory. If set to true, more than one process continued execve()
335 * without domain transition.
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900336 * (9) "users" is an atomic_t that holds how many "struct cred"->security
337 * are referring this "struct tomoyo_domain_info". If is_deleted == true
338 * and users == 0, this struct will be kfree()d upon next garbage
339 * collection.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900340 *
341 * A domain's lifecycle is an analogy of files on / directory.
342 * Multiple domains with the same domainname cannot be created (as with
343 * creating files with the same filename fails with -EEXIST).
344 * If a process reached a domain, that process can reside in that domain after
345 * that domain is marked as "deleted" (as with a process can access an already
346 * open()ed file after that file was unlink()ed).
347 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900348struct tomoyo_domain_info {
349 struct list_head list;
350 struct list_head acl_info_list;
351 /* Name of this domain. Never NULL. */
352 const struct tomoyo_path_info *domainname;
353 u8 profile; /* Profile number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900354 bool is_deleted; /* Delete flag. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900355 bool quota_warned; /* Quota warnning flag. */
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900356 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
357 bool transition_failed; /* Domain transition failed flag. */
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900358 atomic_t users; /* Number of referring credentials. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900359};
360
Kentaro Takeda95908372009-02-05 17:18:13 +0900361/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900362 * tomoyo_path_acl is a structure which is used for holding an
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900363 * entry with one pathname operation (e.g. open(), mkdir()).
364 * It has following fields.
365 *
366 * (1) "head" which is a "struct tomoyo_acl_info".
367 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900368 * (3) "name" is the pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900369 *
370 * Directives held by this structure are "allow_read/write", "allow_execute",
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900371 * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900372 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
373 * "allow_unmount".
Kentaro Takeda95908372009-02-05 17:18:13 +0900374 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900375struct tomoyo_path_acl {
376 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900377 u16 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900378 struct tomoyo_name_union name;
Kentaro Takeda95908372009-02-05 17:18:13 +0900379};
380
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900381/*
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900382 * tomoyo_path_number_acl is a structure which is used for holding an
383 * entry with one pathname and one number operation.
384 * It has following fields.
385 *
386 * (1) "head" which is a "struct tomoyo_acl_info".
387 * (2) "perm" which is a bitmask of permitted operations.
388 * (3) "name" is the pathname.
389 * (4) "number" is the numeric value.
390 *
391 * Directives held by this structure are "allow_create", "allow_mkdir",
392 * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
393 * and "allow_chgrp".
394 *
395 */
396struct tomoyo_path_number_acl {
397 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
398 u8 perm;
399 struct tomoyo_name_union name;
400 struct tomoyo_number_union number;
401};
402
403/*
404 * tomoyo_path_number3_acl is a structure which is used for holding an
405 * entry with one pathname and three numbers operation.
406 * It has following fields.
407 *
408 * (1) "head" which is a "struct tomoyo_acl_info".
409 * (2) "perm" which is a bitmask of permitted operations.
410 * (3) "mode" is the create mode.
411 * (4) "major" is the major number of device node.
412 * (5) "minor" is the minor number of device node.
413 *
414 * Directives held by this structure are "allow_mkchar", "allow_mkblock".
415 *
416 */
417struct tomoyo_path_number3_acl {
418 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
419 u8 perm;
420 struct tomoyo_name_union name;
421 struct tomoyo_number_union mode;
422 struct tomoyo_number_union major;
423 struct tomoyo_number_union minor;
424};
425
426/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900427 * tomoyo_path2_acl is a structure which is used for holding an
Tetsuo Handa937bf612009-12-02 21:09:48 +0900428 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900429 * It has following fields.
430 *
431 * (1) "head" which is a "struct tomoyo_acl_info".
432 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900433 * (3) "name1" is the source/old pathname.
434 * (4) "name2" is the destination/new pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900435 *
Tetsuo Handa937bf612009-12-02 21:09:48 +0900436 * Directives held by this structure are "allow_rename", "allow_link" and
437 * "allow_pivot_root".
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900438 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900439struct tomoyo_path2_acl {
440 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900441 u8 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900442 struct tomoyo_name_union name1;
443 struct tomoyo_name_union name2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900444};
445
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900446/*
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900447 * tomoyo_mount_acl is a structure which is used for holding an
448 * entry for mount operation.
449 * It has following fields.
450 *
451 * (1) "head" which is a "struct tomoyo_acl_info".
452 * (2) "is_deleted" is boolean.
453 * (3) "dev_name" is the device name.
454 * (4) "dir_name" is the mount point.
455 * (5) "flags" is the mount flags.
456 *
457 * Directives held by this structure are "allow_rename", "allow_link" and
458 * "allow_pivot_root".
459 */
460struct tomoyo_mount_acl {
461 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
462 bool is_deleted;
463 struct tomoyo_name_union dev_name;
464 struct tomoyo_name_union dir_name;
465 struct tomoyo_name_union fs_type;
466 struct tomoyo_number_union flags;
467};
468
469/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900470 * tomoyo_io_buffer is a structure which is used for reading and modifying
471 * configuration via /sys/kernel/security/tomoyo/ interface.
472 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
473 * cursors.
474 *
475 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
476 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
477 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
478 * reading (one is for traversing tomoyo_domain_list and the other is for
479 * traversing "struct tomoyo_acl_info"->acl_info_list ).
480 *
481 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
482 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
483 * domain with the domainname specified by the rest of that line (NULL is set
484 * if seek failed).
485 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
486 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
487 * line (->write_var1 is set to NULL if a domain was deleted).
488 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
489 * neither "select " nor "delete ", an entry or a domain specified by that line
490 * is appended.
491 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900492struct tomoyo_io_buffer {
493 int (*read) (struct tomoyo_io_buffer *);
494 int (*write) (struct tomoyo_io_buffer *);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900495 int (*poll) (struct file *file, poll_table *wait);
Kentaro Takeda95908372009-02-05 17:18:13 +0900496 /* Exclusive lock for this structure. */
497 struct mutex io_sem;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900498 /* Index returned by tomoyo_read_lock(). */
499 int reader_idx;
Kentaro Takeda95908372009-02-05 17:18:13 +0900500 /* The position currently reading from. */
501 struct list_head *read_var1;
502 /* Extra variables for reading. */
503 struct list_head *read_var2;
504 /* The position currently writing to. */
505 struct tomoyo_domain_info *write_var1;
506 /* The step for reading. */
507 int read_step;
508 /* Buffer for reading. */
509 char *read_buf;
510 /* EOF flag for reading. */
511 bool read_eof;
512 /* Read domain ACL of specified PID? */
513 bool read_single_domain;
514 /* Extra variable for reading. */
515 u8 read_bit;
516 /* Bytes available for reading. */
517 int read_avail;
518 /* Size of read buffer. */
519 int readbuf_size;
520 /* Buffer for writing. */
521 char *write_buf;
522 /* Bytes available for writing. */
523 int write_avail;
524 /* Size of write buffer. */
525 int writebuf_size;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900526 /* Type of this interface. */
527 u8 type;
Kentaro Takeda95908372009-02-05 17:18:13 +0900528};
529
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900530/*
531 * tomoyo_globally_readable_file_entry is a structure which is used for holding
532 * "allow_read" entries.
533 * It has following fields.
534 *
535 * (1) "list" which is linked to tomoyo_globally_readable_list .
536 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
537 * (3) "is_deleted" is a bool which is true if marked as deleted, false
538 * otherwise.
539 */
540struct tomoyo_globally_readable_file_entry {
541 struct list_head list;
542 const struct tomoyo_path_info *filename;
543 bool is_deleted;
544};
545
546/*
547 * tomoyo_pattern_entry is a structure which is used for holding
548 * "tomoyo_pattern_list" entries.
549 * It has following fields.
550 *
551 * (1) "list" which is linked to tomoyo_pattern_list .
552 * (2) "pattern" is a pathname pattern which is used for converting pathnames
553 * to pathname patterns during learning mode.
554 * (3) "is_deleted" is a bool which is true if marked as deleted, false
555 * otherwise.
556 */
557struct tomoyo_pattern_entry {
558 struct list_head list;
559 const struct tomoyo_path_info *pattern;
560 bool is_deleted;
561};
562
563/*
564 * tomoyo_no_rewrite_entry is a structure which is used for holding
565 * "deny_rewrite" entries.
566 * It has following fields.
567 *
568 * (1) "list" which is linked to tomoyo_no_rewrite_list .
569 * (2) "pattern" is a pathname which is by default not permitted to modify
570 * already existing content.
571 * (3) "is_deleted" is a bool which is true if marked as deleted, false
572 * otherwise.
573 */
574struct tomoyo_no_rewrite_entry {
575 struct list_head list;
576 const struct tomoyo_path_info *pattern;
577 bool is_deleted;
578};
579
580/*
581 * tomoyo_domain_initializer_entry is a structure which is used for holding
582 * "initialize_domain" and "no_initialize_domain" entries.
583 * It has following fields.
584 *
585 * (1) "list" which is linked to tomoyo_domain_initializer_list .
586 * (2) "domainname" which is "a domainname" or "the last component of a
587 * domainname". This field is NULL if "from" clause is not specified.
588 * (3) "program" which is a program's pathname.
589 * (4) "is_deleted" is a bool which is true if marked as deleted, false
590 * otherwise.
591 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
592 * otherwise.
593 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
594 * component of a domainname", false otherwise.
595 */
596struct tomoyo_domain_initializer_entry {
597 struct list_head list;
598 const struct tomoyo_path_info *domainname; /* This may be NULL */
599 const struct tomoyo_path_info *program;
600 bool is_deleted;
601 bool is_not; /* True if this entry is "no_initialize_domain". */
602 /* True if the domainname is tomoyo_get_last_name(). */
603 bool is_last_name;
604};
605
606/*
607 * tomoyo_domain_keeper_entry is a structure which is used for holding
608 * "keep_domain" and "no_keep_domain" entries.
609 * It has following fields.
610 *
611 * (1) "list" which is linked to tomoyo_domain_keeper_list .
612 * (2) "domainname" which is "a domainname" or "the last component of a
613 * domainname".
614 * (3) "program" which is a program's pathname.
615 * This field is NULL if "from" clause is not specified.
616 * (4) "is_deleted" is a bool which is true if marked as deleted, false
617 * otherwise.
618 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
619 * otherwise.
620 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
621 * component of a domainname", false otherwise.
622 */
623struct tomoyo_domain_keeper_entry {
624 struct list_head list;
625 const struct tomoyo_path_info *domainname;
626 const struct tomoyo_path_info *program; /* This may be NULL */
627 bool is_deleted;
628 bool is_not; /* True if this entry is "no_keep_domain". */
629 /* True if the domainname is tomoyo_get_last_name(). */
630 bool is_last_name;
631};
632
633/*
634 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
635 * It has following fields.
636 *
637 * (1) "list" which is linked to tomoyo_alias_list .
638 * (2) "original_name" which is a dereferenced pathname.
639 * (3) "aliased_name" which is a symlink's pathname.
640 * (4) "is_deleted" is a bool which is true if marked as deleted, false
641 * otherwise.
642 */
643struct tomoyo_alias_entry {
644 struct list_head list;
645 const struct tomoyo_path_info *original_name;
646 const struct tomoyo_path_info *aliased_name;
647 bool is_deleted;
648};
649
650/*
651 * tomoyo_policy_manager_entry is a structure which is used for holding list of
652 * domainnames or programs which are permitted to modify configuration via
653 * /sys/kernel/security/tomoyo/ interface.
654 * It has following fields.
655 *
656 * (1) "list" which is linked to tomoyo_policy_manager_list .
657 * (2) "manager" is a domainname or a program's pathname.
658 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
659 * otherwise.
660 * (4) "is_deleted" is a bool which is true if marked as deleted, false
661 * otherwise.
662 */
663struct tomoyo_policy_manager_entry {
664 struct list_head list;
665 /* A path to program or a domainname. */
666 const struct tomoyo_path_info *manager;
667 bool is_domain; /* True if manager is a domainname. */
668 bool is_deleted; /* True if this entry is deleted. */
669};
670
671/********** Function prototypes. **********/
672
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900673extern asmlinkage long sys_getpid(void);
674extern asmlinkage long sys_getppid(void);
675
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900676/* Check whether the given string starts with the given keyword. */
677bool tomoyo_str_starts(char **src, const char *find);
678/* Get tomoyo_realpath() of current process. */
679const char *tomoyo_get_exe(void);
680/* Format string. */
681void tomoyo_normalize_line(unsigned char *buffer);
682/* Print warning or error message on console. */
683void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
684 __attribute__ ((format(printf, 2, 3)));
685/* Check all profiles currently assigned to domains are defined. */
686void tomoyo_check_profile(void);
687/* Open operation for /sys/kernel/security/tomoyo/ interface. */
688int tomoyo_open_control(const u8 type, struct file *file);
689/* Close /sys/kernel/security/tomoyo/ interface. */
690int tomoyo_close_control(struct file *file);
691/* Read operation for /sys/kernel/security/tomoyo/ interface. */
692int tomoyo_read_control(struct file *file, char __user *buffer,
693 const int buffer_len);
694/* Write operation for /sys/kernel/security/tomoyo/ interface. */
695int tomoyo_write_control(struct file *file, const char __user *buffer,
696 const int buffer_len);
697/* Check whether the domain has too many ACL entries to hold. */
698bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
699/* Print out of memory warning message. */
700void tomoyo_warn_oom(const char *function);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900701/* Check whether the given name matches the given name_union. */
702bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
703 const struct tomoyo_name_union *ptr);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900704/* Check whether the given number matches the given number_union. */
705bool tomoyo_compare_number_union(const unsigned long value,
706 const struct tomoyo_number_union *ptr);
Kentaro Takeda95908372009-02-05 17:18:13 +0900707/* Transactional sprintf() for policy dump. */
708bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
709 __attribute__ ((format(printf, 2, 3)));
710/* Check whether the domainname is correct. */
Tetsuo Handa17080002010-02-16 21:14:48 +0900711bool tomoyo_is_correct_domain(const unsigned char *domainname);
Kentaro Takeda95908372009-02-05 17:18:13 +0900712/* Check whether the token is correct. */
713bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
Tetsuo Handa17080002010-02-16 21:14:48 +0900714 const s8 pattern_type, const s8 end_type);
Kentaro Takeda95908372009-02-05 17:18:13 +0900715/* Check whether the token can be a domainname. */
716bool tomoyo_is_domain_def(const unsigned char *buffer);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900717bool tomoyo_parse_name_union(const char *filename,
718 struct tomoyo_name_union *ptr);
719/* Check whether the given filename matches the given path_group. */
720bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
721 const struct tomoyo_path_group *group,
722 const bool may_use_pattern);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900723/* Check whether the given value matches the given number_group. */
724bool tomoyo_number_matches_group(const unsigned long min,
725 const unsigned long max,
726 const struct tomoyo_number_group *group);
Kentaro Takeda95908372009-02-05 17:18:13 +0900727/* Check whether the given filename matches the given pattern. */
728bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
729 const struct tomoyo_path_info *pattern);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900730
731bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
732 const struct tomoyo_number_union *ptr);
733bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
734
Kentaro Takeda95908372009-02-05 17:18:13 +0900735/* Read "alias" entry in exception policy. */
736bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
737/*
738 * Read "initialize_domain" and "no_initialize_domain" entry
739 * in exception policy.
740 */
741bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
742/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
743bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
744/* Read "file_pattern" entry in exception policy. */
745bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900746/* Read "path_group" entry in exception policy. */
747bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900748/* Read "number_group" entry in exception policy. */
749bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900750/* Read "allow_read" entry in exception policy. */
751bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
752/* Read "deny_rewrite" entry in exception policy. */
753bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900754/* Tokenize a line. */
755bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
Kentaro Takeda95908372009-02-05 17:18:13 +0900756/* Write domain policy violation warning message to console? */
757bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
758/* Convert double path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900759const char *tomoyo_path22keyword(const u8 operation);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900760const char *tomoyo_path_number2keyword(const u8 operation);
761const char *tomoyo_path_number32keyword(const u8 operation);
Kentaro Takeda95908372009-02-05 17:18:13 +0900762/* Get the last component of the given domainname. */
763const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
Kentaro Takeda95908372009-02-05 17:18:13 +0900764/* Convert single path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900765const char *tomoyo_path2keyword(const u8 operation);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900766/* Fill "struct tomoyo_request_info". */
767int tomoyo_init_request_info(struct tomoyo_request_info *r,
768 struct tomoyo_domain_info *domain);
769/* Check permission for mount operation. */
770int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
771 unsigned long flags, void *data_page);
Kentaro Takeda95908372009-02-05 17:18:13 +0900772/* Create "alias" entry in exception policy. */
773int tomoyo_write_alias_policy(char *data, const bool is_delete);
774/*
775 * Create "initialize_domain" and "no_initialize_domain" entry
776 * in exception policy.
777 */
778int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
779 const bool is_delete);
780/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
781int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
782 const bool is_delete);
783/*
784 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
785 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
786 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
787 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
788 * "allow_link" entry in domain policy.
789 */
790int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
791 const bool is_delete);
792/* Create "allow_read" entry in exception policy. */
793int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900794/* Create "allow_mount" entry in domain policy. */
795int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
796 const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900797/* Create "deny_rewrite" entry in exception policy. */
798int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
799/* Create "file_pattern" entry in exception policy. */
800int tomoyo_write_pattern_policy(char *data, const bool is_delete);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900801/* Create "path_group" entry in exception policy. */
802int tomoyo_write_path_group_policy(char *data, const bool is_delete);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900803int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
804 __attribute__ ((format(printf, 2, 3)));
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900805/* Create "number_group" entry in exception policy. */
806int tomoyo_write_number_group_policy(char *data, const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900807/* Find a domain by the given name. */
808struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
809/* Find or create a domain by the given name. */
810struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
811 domainname,
812 const u8 profile);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900813/* Allocate memory for "struct tomoyo_path_group". */
814struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900815struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900816
Kentaro Takeda95908372009-02-05 17:18:13 +0900817/* Check mode for specified functionality. */
818unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
819 const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900820/* Fill in "struct tomoyo_path_info" members. */
821void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
822/* Run policy loader when /sbin/init starts. */
823void tomoyo_load_policy(const char *filename);
Kentaro Takeda95908372009-02-05 17:18:13 +0900824
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900825void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
826
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900827/* Convert binary string to ascii string. */
828int tomoyo_encode(char *buffer, int buflen, const char *str);
829
830/* Returns realpath(3) of the given pathname but ignores chroot'ed root. */
831int tomoyo_realpath_from_path2(struct path *path, char *newname,
832 int newname_len);
833
834/*
835 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
836 * These functions use kzalloc(), so the caller must call kfree()
837 * if these functions didn't return NULL.
838 */
839char *tomoyo_realpath(const char *pathname);
840/*
841 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
842 */
843char *tomoyo_realpath_nofollow(const char *pathname);
844/* Same with tomoyo_realpath() except that the pathname is already solved. */
845char *tomoyo_realpath_from_path(struct path *path);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900846/* Get patterned pathname. */
847const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900848
849/* Check memory quota. */
850bool tomoyo_memory_ok(void *ptr);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900851void *tomoyo_commit_ok(void *data, const unsigned int size);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900852
853/*
854 * Keep the given name on the RAM.
855 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
856 */
857const struct tomoyo_path_info *tomoyo_get_name(const char *name);
858
859/* Check for memory usage. */
860int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
861
862/* Set memory quota. */
863int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
864
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900865/* Initialize mm related code. */
866void __init tomoyo_mm_init(void);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900867int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
868 const struct tomoyo_path_info *filename);
869int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
870 struct path *path, const int flag);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900871int tomoyo_path_number_perm(const u8 operation, struct path *path,
872 unsigned long number);
873int tomoyo_path_number3_perm(const u8 operation, struct path *path,
874 const unsigned int mode, unsigned int dev);
Tetsuo Handa97d69312010-02-16 09:46:15 +0900875int tomoyo_path_perm(const u8 operation, struct path *path);
876int tomoyo_path2_perm(const u8 operation, struct path *path1,
877 struct path *path2);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900878int tomoyo_find_next_domain(struct linux_binprm *bprm);
879
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900880void tomoyo_print_ulong(char *buffer, const int buffer_len,
881 const unsigned long value, const u8 type);
882
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900883/* Drop refcount on tomoyo_name_union. */
884void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
885
Tetsuo Handa847b1732010-02-11 09:43:54 +0900886/* Run garbage collector. */
887void tomoyo_run_gc(void);
888
889void tomoyo_memory_free(void *ptr);
890
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900891/********** External variable definitions. **********/
892
893/* Lock for GC. */
894extern struct srcu_struct tomoyo_ss;
895
896/* The list for "struct tomoyo_domain_info". */
897extern struct list_head tomoyo_domain_list;
898
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900899extern struct list_head tomoyo_path_group_list;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900900extern struct list_head tomoyo_number_group_list;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900901extern struct list_head tomoyo_domain_initializer_list;
902extern struct list_head tomoyo_domain_keeper_list;
903extern struct list_head tomoyo_alias_list;
904extern struct list_head tomoyo_globally_readable_list;
905extern struct list_head tomoyo_pattern_list;
906extern struct list_head tomoyo_no_rewrite_list;
907extern struct list_head tomoyo_policy_manager_list;
908extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
Tetsuo Handa847b1732010-02-11 09:43:54 +0900909
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900910/* Lock for protecting policy. */
911extern struct mutex tomoyo_policy_lock;
912
913/* Has /sbin/init started? */
914extern bool tomoyo_policy_loaded;
915
916/* The kernel's domain. */
917extern struct tomoyo_domain_info tomoyo_kernel_domain;
918
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900919extern unsigned int tomoyo_quota_for_query;
920extern unsigned int tomoyo_query_memory_size;
921
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900922/********** Inlined functions. **********/
923
924static inline int tomoyo_read_lock(void)
925{
926 return srcu_read_lock(&tomoyo_ss);
927}
928
929static inline void tomoyo_read_unlock(int idx)
930{
931 srcu_read_unlock(&tomoyo_ss, idx);
932}
933
Kentaro Takeda95908372009-02-05 17:18:13 +0900934/* strcmp() for "struct tomoyo_path_info" structure. */
935static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
936 const struct tomoyo_path_info *b)
937{
938 return a->hash != b->hash || strcmp(a->name, b->name);
939}
940
Kentaro Takeda95908372009-02-05 17:18:13 +0900941/**
942 * tomoyo_is_valid - Check whether the character is a valid char.
943 *
944 * @c: The character to check.
945 *
946 * Returns true if @c is a valid character, false otherwise.
947 */
948static inline bool tomoyo_is_valid(const unsigned char c)
949{
950 return c > ' ' && c < 127;
951}
952
953/**
954 * tomoyo_is_invalid - Check whether the character is an invalid char.
955 *
956 * @c: The character to check.
957 *
958 * Returns true if @c is an invalid character, false otherwise.
959 */
960static inline bool tomoyo_is_invalid(const unsigned char c)
961{
962 return c && (c <= ' ' || c >= 127);
963}
964
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900965static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
966{
967 if (name) {
968 struct tomoyo_name_entry *ptr =
969 container_of(name, struct tomoyo_name_entry, entry);
970 atomic_dec(&ptr->users);
971 }
972}
Kentaro Takeda95908372009-02-05 17:18:13 +0900973
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900974static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
975{
976 if (group)
977 atomic_dec(&group->users);
978}
979
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900980static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
981{
982 if (group)
983 atomic_dec(&group->users);
984}
985
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900986static inline struct tomoyo_domain_info *tomoyo_domain(void)
987{
988 return current_cred()->security;
989}
Kentaro Takeda95908372009-02-05 17:18:13 +0900990
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900991static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
992 *task)
993{
994 return task_cred_xxx(task, security);
995}
Kentaro Takeda95908372009-02-05 17:18:13 +0900996
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900997static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
998 const struct tomoyo_acl_info *p2)
999{
1000 return p1->type == p2->type;
1001}
1002
1003static inline bool tomoyo_is_same_name_union
1004(const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
1005{
1006 return p1->filename == p2->filename && p1->group == p2->group &&
1007 p1->is_group == p2->is_group;
1008}
1009
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001010static inline bool tomoyo_is_same_number_union
1011(const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
1012{
1013 return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
1014 && p1->group == p2->group && p1->min_type == p2->min_type &&
1015 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
1016}
1017
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001018static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
1019 const struct tomoyo_path_acl *p2)
1020{
1021 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1022 tomoyo_is_same_name_union(&p1->name, &p2->name);
1023}
1024
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001025static inline bool tomoyo_is_same_path_number3_acl
1026(const struct tomoyo_path_number3_acl *p1,
1027 const struct tomoyo_path_number3_acl *p2)
1028{
1029 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1030 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1031 && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
1032 && tomoyo_is_same_number_union(&p1->major, &p2->major)
1033 && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
1034}
1035
1036
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001037static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
1038 const struct tomoyo_path2_acl *p2)
1039{
1040 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1041 tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
1042 tomoyo_is_same_name_union(&p1->name2, &p2->name2);
1043}
1044
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001045static inline bool tomoyo_is_same_path_number_acl
1046(const struct tomoyo_path_number_acl *p1,
1047 const struct tomoyo_path_number_acl *p2)
1048{
1049 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1050 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1051 && tomoyo_is_same_number_union(&p1->number, &p2->number);
1052}
1053
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001054static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1055 const struct tomoyo_mount_acl *p2)
1056{
1057 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1058 tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
1059 tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
1060 tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
1061 tomoyo_is_same_number_union(&p1->flags, &p2->flags);
1062}
1063
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +09001064static inline bool tomoyo_is_same_domain_initializer_entry
1065(const struct tomoyo_domain_initializer_entry *p1,
1066 const struct tomoyo_domain_initializer_entry *p2)
1067{
1068 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1069 && p1->domainname == p2->domainname
1070 && p1->program == p2->program;
1071}
1072
1073static inline bool tomoyo_is_same_domain_keeper_entry
1074(const struct tomoyo_domain_keeper_entry *p1,
1075 const struct tomoyo_domain_keeper_entry *p2)
1076{
1077 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1078 && p1->domainname == p2->domainname
1079 && p1->program == p2->program;
1080}
1081
1082static inline bool tomoyo_is_same_alias_entry
1083(const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1084{
1085 return p1->original_name == p2->original_name &&
1086 p1->aliased_name == p2->aliased_name;
1087}
1088
Kentaro Takeda95908372009-02-05 17:18:13 +09001089/**
1090 * list_for_each_cookie - iterate over a list with cookie.
1091 * @pos: the &struct list_head to use as a loop cursor.
1092 * @cookie: the &struct list_head to use as a cookie.
1093 * @head: the head for your list.
1094 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001095 * Same with list_for_each_rcu() except that this primitive uses @cookie
Kentaro Takeda95908372009-02-05 17:18:13 +09001096 * so that we can continue iteration.
1097 * @cookie must be NULL when iteration starts, and @cookie will become
1098 * NULL when iteration finishes.
1099 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001100#define list_for_each_cookie(pos, cookie, head) \
1101 for (({ if (!cookie) \
1102 cookie = head; }), \
1103 pos = rcu_dereference((cookie)->next); \
1104 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
1105 (cookie) = pos, pos = rcu_dereference(pos->next))
1106
Kentaro Takeda95908372009-02-05 17:18:13 +09001107#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */