blob: 539b9a28b7396900e602a8d6bcda563e784338a7 [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
Tetsuo Handac8c57e82010-06-03 20:36:43 +090036#define TOMOYO_EXEC_TMPSIZE 4096
Tetsuo Handa76bb0892010-02-11 09:42:40 +090037
38/* Profile number is an integer between 0 and 255. */
39#define TOMOYO_MAX_PROFILES 256
40
Tetsuo Handacb0abe62010-05-17 10:08:05 +090041enum tomoyo_mode_index {
42 TOMOYO_CONFIG_DISABLED,
43 TOMOYO_CONFIG_LEARNING,
44 TOMOYO_CONFIG_PERMISSIVE,
Tetsuo Handa57c25902010-06-03 20:38:44 +090045 TOMOYO_CONFIG_ENFORCING,
46 TOMOYO_CONFIG_USE_DEFAULT = 255
Tetsuo Handacb0abe62010-05-17 10:08:05 +090047};
48
Tetsuo Handa76bb0892010-02-11 09:42:40 +090049/* Keywords for ACLs. */
Tetsuo Handa10843072010-06-03 20:38:03 +090050#define TOMOYO_KEYWORD_AGGREGATOR "aggregator "
Tetsuo Handa76bb0892010-02-11 09:42:40 +090051#define TOMOYO_KEYWORD_ALIAS "alias "
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090052#define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
Tetsuo Handa76bb0892010-02-11 09:42:40 +090053#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
54#define TOMOYO_KEYWORD_DELETE "delete "
55#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
56#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
57#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
58#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
59#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
60#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090061#define TOMOYO_KEYWORD_PATH_GROUP "path_group "
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090062#define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
Tetsuo Handa76bb0892010-02-11 09:42:40 +090063#define TOMOYO_KEYWORD_SELECT "select "
64#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
65#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
Tetsuo Handa9b244372010-06-03 20:35:53 +090066#define TOMOYO_KEYWORD_QUOTA_EXCEEDED "quota_exceeded"
67#define TOMOYO_KEYWORD_TRANSITION_FAILED "transition_failed"
Tetsuo Handa76bb0892010-02-11 09:42:40 +090068/* A domain definition starts with <kernel>. */
69#define TOMOYO_ROOT_NAME "<kernel>"
70#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
71
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090072/* Value type definition. */
73#define TOMOYO_VALUE_TYPE_INVALID 0
74#define TOMOYO_VALUE_TYPE_DECIMAL 1
75#define TOMOYO_VALUE_TYPE_OCTAL 2
76#define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
77
Tetsuo Handa76bb0892010-02-11 09:42:40 +090078/* Index numbers for Access Controls. */
Tetsuo Handa084da352010-02-15 15:10:39 +090079enum tomoyo_acl_entry_type_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090080 TOMOYO_TYPE_PATH_ACL,
81 TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090082 TOMOYO_TYPE_PATH_NUMBER_ACL,
83 TOMOYO_TYPE_PATH_NUMBER3_ACL,
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090084 TOMOYO_TYPE_MOUNT_ACL,
Tetsuo Handa084da352010-02-15 15:10:39 +090085};
Tetsuo Handa76bb0892010-02-11 09:42:40 +090086
87/* Index numbers for File Controls. */
88
89/*
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090090 * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
91 * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
92 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
93 * TOMOYO_TYPE_READ_WRITE is set.
94 * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
95 * or TOMOYO_TYPE_WRITE is cleared.
96 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
97 * TOMOYO_TYPE_READ_WRITE is cleared.
Tetsuo Handa76bb0892010-02-11 09:42:40 +090098 */
99
Tetsuo Handa084da352010-02-15 15:10:39 +0900100enum tomoyo_path_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900101 TOMOYO_TYPE_READ_WRITE,
102 TOMOYO_TYPE_EXECUTE,
103 TOMOYO_TYPE_READ,
104 TOMOYO_TYPE_WRITE,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900105 TOMOYO_TYPE_UNLINK,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900106 TOMOYO_TYPE_RMDIR,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900107 TOMOYO_TYPE_TRUNCATE,
108 TOMOYO_TYPE_SYMLINK,
109 TOMOYO_TYPE_REWRITE,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900110 TOMOYO_TYPE_CHROOT,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900111 TOMOYO_TYPE_UMOUNT,
112 TOMOYO_MAX_PATH_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900113};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900114
Tetsuo Handa237ab452010-06-12 20:46:22 +0900115#define TOMOYO_RW_MASK ((1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE))
116
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900117enum tomoyo_path_number3_acl_index {
118 TOMOYO_TYPE_MKBLOCK,
119 TOMOYO_TYPE_MKCHAR,
120 TOMOYO_MAX_PATH_NUMBER3_OPERATION
121};
122
Tetsuo Handa084da352010-02-15 15:10:39 +0900123enum tomoyo_path2_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900124 TOMOYO_TYPE_LINK,
125 TOMOYO_TYPE_RENAME,
126 TOMOYO_TYPE_PIVOT_ROOT,
127 TOMOYO_MAX_PATH2_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900128};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900129
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900130enum tomoyo_path_number_acl_index {
131 TOMOYO_TYPE_CREATE,
132 TOMOYO_TYPE_MKDIR,
133 TOMOYO_TYPE_MKFIFO,
134 TOMOYO_TYPE_MKSOCK,
135 TOMOYO_TYPE_IOCTL,
136 TOMOYO_TYPE_CHMOD,
137 TOMOYO_TYPE_CHOWN,
138 TOMOYO_TYPE_CHGRP,
139 TOMOYO_MAX_PATH_NUMBER_OPERATION
140};
141
Tetsuo Handa084da352010-02-15 15:10:39 +0900142enum tomoyo_securityfs_interface_index {
143 TOMOYO_DOMAINPOLICY,
144 TOMOYO_EXCEPTIONPOLICY,
145 TOMOYO_DOMAIN_STATUS,
146 TOMOYO_PROCESS_STATUS,
147 TOMOYO_MEMINFO,
148 TOMOYO_SELFDOMAIN,
149 TOMOYO_VERSION,
150 TOMOYO_PROFILE,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900151 TOMOYO_QUERY,
Tetsuo Handa084da352010-02-15 15:10:39 +0900152 TOMOYO_MANAGER
153};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900154
Tetsuo Handa57c25902010-06-03 20:38:44 +0900155enum tomoyo_mac_index {
156 TOMOYO_MAC_FILE_EXECUTE,
157 TOMOYO_MAC_FILE_OPEN,
158 TOMOYO_MAC_FILE_CREATE,
159 TOMOYO_MAC_FILE_UNLINK,
160 TOMOYO_MAC_FILE_MKDIR,
161 TOMOYO_MAC_FILE_RMDIR,
162 TOMOYO_MAC_FILE_MKFIFO,
163 TOMOYO_MAC_FILE_MKSOCK,
164 TOMOYO_MAC_FILE_TRUNCATE,
165 TOMOYO_MAC_FILE_SYMLINK,
166 TOMOYO_MAC_FILE_REWRITE,
167 TOMOYO_MAC_FILE_MKBLOCK,
168 TOMOYO_MAC_FILE_MKCHAR,
169 TOMOYO_MAC_FILE_LINK,
170 TOMOYO_MAC_FILE_RENAME,
171 TOMOYO_MAC_FILE_CHMOD,
172 TOMOYO_MAC_FILE_CHOWN,
173 TOMOYO_MAC_FILE_CHGRP,
174 TOMOYO_MAC_FILE_IOCTL,
175 TOMOYO_MAC_FILE_CHROOT,
176 TOMOYO_MAC_FILE_MOUNT,
177 TOMOYO_MAC_FILE_UMOUNT,
178 TOMOYO_MAC_FILE_PIVOT_ROOT,
179 TOMOYO_MAX_MAC_INDEX
180};
181
182enum tomoyo_mac_category_index {
183 TOMOYO_MAC_CATEGORY_FILE,
184 TOMOYO_MAX_MAC_CATEGORY_INDEX
185};
186
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900187#define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
188
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900189/********** Structure definitions. **********/
Kentaro Takeda95908372009-02-05 17:18:13 +0900190
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900191/*
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900192 * tomoyo_request_info is a structure which is used for holding
193 *
194 * (1) Domain information of current process.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900195 * (2) How many retries are made for this request.
196 * (3) Profile number used for this request.
197 * (4) Access control mode of the profile.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900198 */
199struct tomoyo_request_info {
200 struct tomoyo_domain_info *domain;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900201 u8 retry;
202 u8 profile;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900203 u8 mode; /* One of tomoyo_mode_index . */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900204 u8 type;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900205};
206
207/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900208 * tomoyo_path_info is a structure which is used for holding a string data
209 * used by TOMOYO.
210 * This structure has several fields for supporting pattern matching.
211 *
212 * (1) "name" is the '\0' terminated string data.
213 * (2) "hash" is full_name_hash(name, strlen(name)).
214 * This allows tomoyo_pathcmp() to compare by hash before actually compare
215 * using strcmp().
216 * (3) "const_len" is the length of the initial segment of "name" which
217 * consists entirely of non wildcard characters. In other words, the length
218 * which we can compare two strings using strncmp().
219 * (4) "is_dir" is a bool which is true if "name" ends with "/",
220 * false otherwise.
221 * TOMOYO distinguishes directory and non-directory. A directory ends with
222 * "/" and non-directory does not end with "/".
223 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
224 * characters, false otherwise. This allows TOMOYO to use "hash" and
225 * strcmp() for string comparison if "is_patterned" is false.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900226 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900227struct tomoyo_path_info {
228 const char *name;
229 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900230 u16 const_len; /* = tomoyo_const_part_length(name) */
231 bool is_dir; /* = tomoyo_strendswith(name, "/") */
232 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900233};
234
235/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900236 * tomoyo_name_entry is a structure which is used for linking
237 * "struct tomoyo_path_info" into tomoyo_name_list .
Kentaro Takeda95908372009-02-05 17:18:13 +0900238 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900239struct tomoyo_name_entry {
240 struct list_head list;
241 atomic_t users;
242 struct tomoyo_path_info entry;
243};
Kentaro Takeda95908372009-02-05 17:18:13 +0900244
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900245struct tomoyo_name_union {
246 const struct tomoyo_path_info *filename;
247 struct tomoyo_path_group *group;
248 u8 is_group;
249};
250
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900251struct tomoyo_number_union {
252 unsigned long values[2];
253 struct tomoyo_number_group *group;
254 u8 min_type;
255 u8 max_type;
256 u8 is_group;
257};
258
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900259/* Structure for "path_group" directive. */
260struct tomoyo_path_group {
261 struct list_head list;
262 const struct tomoyo_path_info *group_name;
263 struct list_head member_list;
264 atomic_t users;
265};
266
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900267/* Structure for "number_group" directive. */
268struct tomoyo_number_group {
269 struct list_head list;
270 const struct tomoyo_path_info *group_name;
271 struct list_head member_list;
272 atomic_t users;
273};
274
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900275/* Structure for "path_group" directive. */
276struct tomoyo_path_group_member {
277 struct list_head list;
278 bool is_deleted;
279 const struct tomoyo_path_info *member_name;
280};
281
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900282/* Structure for "number_group" directive. */
283struct tomoyo_number_group_member {
284 struct list_head list;
285 bool is_deleted;
286 struct tomoyo_number_union number;
287};
288
Kentaro Takeda95908372009-02-05 17:18:13 +0900289/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900290 * tomoyo_acl_info is a structure which is used for holding
291 *
292 * (1) "list" which is linked to the ->acl_info_list of
293 * "struct tomoyo_domain_info"
Tetsuo Handa237ab452010-06-12 20:46:22 +0900294 * (2) "is_deleted" is a bool which is true if this domain is marked as
295 * "deleted", false otherwise.
296 * (3) "type" which tells type of the entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900297 *
298 * Packing "struct tomoyo_acl_info" allows
Tetsuo Handa237ab452010-06-12 20:46:22 +0900299 * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
300 * "struct tomoyo_path_number_acl" "struct tomoyo_path_number3_acl" to embed
301 * "u8" without enlarging their structure size.
Kentaro Takeda95908372009-02-05 17:18:13 +0900302 */
303struct tomoyo_acl_info {
304 struct list_head list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900305 bool is_deleted;
306 u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
Kentaro Takeda95908372009-02-05 17:18:13 +0900307} __packed;
308
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900309/*
310 * tomoyo_domain_info is a structure which is used for holding permissions
311 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
312 * It has following fields.
313 *
314 * (1) "list" which is linked to tomoyo_domain_list .
315 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
316 * (3) "domainname" which holds the name of the domain.
317 * (4) "profile" which remembers profile number assigned to this domain.
318 * (5) "is_deleted" is a bool which is true if this domain is marked as
319 * "deleted", false otherwise.
320 * (6) "quota_warned" is a bool which is used for suppressing warning message
321 * when learning mode learned too much entries.
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900322 * (7) "ignore_global_allow_read" is a bool which is true if this domain
323 * should ignore "allow_read" directive in exception policy.
324 * (8) "transition_failed" is a bool which is set to true when this domain was
325 * unable to create a new domain at tomoyo_find_next_domain() because the
326 * name of the domain to be created was too long or it could not allocate
327 * memory. If set to true, more than one process continued execve()
328 * without domain transition.
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900329 * (9) "users" is an atomic_t that holds how many "struct cred"->security
330 * are referring this "struct tomoyo_domain_info". If is_deleted == true
331 * and users == 0, this struct will be kfree()d upon next garbage
332 * collection.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900333 *
334 * A domain's lifecycle is an analogy of files on / directory.
335 * Multiple domains with the same domainname cannot be created (as with
336 * creating files with the same filename fails with -EEXIST).
337 * If a process reached a domain, that process can reside in that domain after
338 * that domain is marked as "deleted" (as with a process can access an already
339 * open()ed file after that file was unlink()ed).
340 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900341struct tomoyo_domain_info {
342 struct list_head list;
343 struct list_head acl_info_list;
344 /* Name of this domain. Never NULL. */
345 const struct tomoyo_path_info *domainname;
346 u8 profile; /* Profile number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900347 bool is_deleted; /* Delete flag. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900348 bool quota_warned; /* Quota warnning flag. */
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900349 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
350 bool transition_failed; /* Domain transition failed flag. */
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900351 atomic_t users; /* Number of referring credentials. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900352};
353
Kentaro Takeda95908372009-02-05 17:18:13 +0900354/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900355 * tomoyo_path_acl is a structure which is used for holding an
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900356 * entry with one pathname operation (e.g. open(), mkdir()).
357 * It has following fields.
358 *
359 * (1) "head" which is a "struct tomoyo_acl_info".
360 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900361 * (3) "name" is the pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900362 *
363 * Directives held by this structure are "allow_read/write", "allow_execute",
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900364 * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900365 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
366 * "allow_unmount".
Kentaro Takeda95908372009-02-05 17:18:13 +0900367 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900368struct tomoyo_path_acl {
369 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900370 u16 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900371 struct tomoyo_name_union name;
Kentaro Takeda95908372009-02-05 17:18:13 +0900372};
373
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900374/*
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900375 * tomoyo_path_number_acl is a structure which is used for holding an
376 * entry with one pathname and one number operation.
377 * It has following fields.
378 *
379 * (1) "head" which is a "struct tomoyo_acl_info".
380 * (2) "perm" which is a bitmask of permitted operations.
381 * (3) "name" is the pathname.
382 * (4) "number" is the numeric value.
383 *
384 * Directives held by this structure are "allow_create", "allow_mkdir",
385 * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
386 * and "allow_chgrp".
387 *
388 */
389struct tomoyo_path_number_acl {
390 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
391 u8 perm;
392 struct tomoyo_name_union name;
393 struct tomoyo_number_union number;
394};
395
396/*
397 * tomoyo_path_number3_acl is a structure which is used for holding an
398 * entry with one pathname and three numbers operation.
399 * It has following fields.
400 *
401 * (1) "head" which is a "struct tomoyo_acl_info".
402 * (2) "perm" which is a bitmask of permitted operations.
403 * (3) "mode" is the create mode.
404 * (4) "major" is the major number of device node.
405 * (5) "minor" is the minor number of device node.
406 *
407 * Directives held by this structure are "allow_mkchar", "allow_mkblock".
408 *
409 */
410struct tomoyo_path_number3_acl {
411 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
412 u8 perm;
413 struct tomoyo_name_union name;
414 struct tomoyo_number_union mode;
415 struct tomoyo_number_union major;
416 struct tomoyo_number_union minor;
417};
418
419/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900420 * tomoyo_path2_acl is a structure which is used for holding an
Tetsuo Handa937bf612009-12-02 21:09:48 +0900421 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900422 * It has following fields.
423 *
424 * (1) "head" which is a "struct tomoyo_acl_info".
425 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900426 * (3) "name1" is the source/old pathname.
427 * (4) "name2" is the destination/new pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900428 *
Tetsuo Handa937bf612009-12-02 21:09:48 +0900429 * Directives held by this structure are "allow_rename", "allow_link" and
430 * "allow_pivot_root".
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900431 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900432struct tomoyo_path2_acl {
433 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900434 u8 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900435 struct tomoyo_name_union name1;
436 struct tomoyo_name_union name2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900437};
438
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900439/*
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900440 * tomoyo_mount_acl is a structure which is used for holding an
441 * entry for mount operation.
442 * It has following fields.
443 *
444 * (1) "head" which is a "struct tomoyo_acl_info".
Tetsuo Handa237ab452010-06-12 20:46:22 +0900445 * (2) "dev_name" is the device name.
446 * (3) "dir_name" is the mount point.
447 * (4) "fs_type" is the filesystem type.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900448 * (5) "flags" is the mount flags.
449 *
Tetsuo Handa237ab452010-06-12 20:46:22 +0900450 * Directive held by this structure is "allow_mount".
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900451 */
452struct tomoyo_mount_acl {
453 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900454 struct tomoyo_name_union dev_name;
455 struct tomoyo_name_union dir_name;
456 struct tomoyo_name_union fs_type;
457 struct tomoyo_number_union flags;
458};
459
460/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900461 * tomoyo_io_buffer is a structure which is used for reading and modifying
462 * configuration via /sys/kernel/security/tomoyo/ interface.
463 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
464 * cursors.
465 *
466 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
467 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
468 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
469 * reading (one is for traversing tomoyo_domain_list and the other is for
470 * traversing "struct tomoyo_acl_info"->acl_info_list ).
471 *
472 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
473 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
474 * domain with the domainname specified by the rest of that line (NULL is set
475 * if seek failed).
476 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
477 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
478 * line (->write_var1 is set to NULL if a domain was deleted).
479 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
480 * neither "select " nor "delete ", an entry or a domain specified by that line
481 * is appended.
482 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900483struct tomoyo_io_buffer {
484 int (*read) (struct tomoyo_io_buffer *);
485 int (*write) (struct tomoyo_io_buffer *);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900486 int (*poll) (struct file *file, poll_table *wait);
Kentaro Takeda95908372009-02-05 17:18:13 +0900487 /* Exclusive lock for this structure. */
488 struct mutex io_sem;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900489 /* Index returned by tomoyo_read_lock(). */
490 int reader_idx;
Kentaro Takeda95908372009-02-05 17:18:13 +0900491 /* The position currently reading from. */
492 struct list_head *read_var1;
493 /* Extra variables for reading. */
494 struct list_head *read_var2;
495 /* The position currently writing to. */
496 struct tomoyo_domain_info *write_var1;
497 /* The step for reading. */
498 int read_step;
499 /* Buffer for reading. */
500 char *read_buf;
501 /* EOF flag for reading. */
502 bool read_eof;
503 /* Read domain ACL of specified PID? */
504 bool read_single_domain;
505 /* Extra variable for reading. */
506 u8 read_bit;
507 /* Bytes available for reading. */
508 int read_avail;
509 /* Size of read buffer. */
510 int readbuf_size;
511 /* Buffer for writing. */
512 char *write_buf;
513 /* Bytes available for writing. */
514 int write_avail;
515 /* Size of write buffer. */
516 int writebuf_size;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900517 /* Type of this interface. */
518 u8 type;
Kentaro Takeda95908372009-02-05 17:18:13 +0900519};
520
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900521/*
522 * tomoyo_globally_readable_file_entry is a structure which is used for holding
523 * "allow_read" entries.
524 * It has following fields.
525 *
526 * (1) "list" which is linked to tomoyo_globally_readable_list .
527 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
528 * (3) "is_deleted" is a bool which is true if marked as deleted, false
529 * otherwise.
530 */
531struct tomoyo_globally_readable_file_entry {
532 struct list_head list;
533 const struct tomoyo_path_info *filename;
534 bool is_deleted;
535};
536
537/*
538 * tomoyo_pattern_entry is a structure which is used for holding
539 * "tomoyo_pattern_list" entries.
540 * It has following fields.
541 *
542 * (1) "list" which is linked to tomoyo_pattern_list .
543 * (2) "pattern" is a pathname pattern which is used for converting pathnames
544 * to pathname patterns during learning mode.
545 * (3) "is_deleted" is a bool which is true if marked as deleted, false
546 * otherwise.
547 */
548struct tomoyo_pattern_entry {
549 struct list_head list;
550 const struct tomoyo_path_info *pattern;
551 bool is_deleted;
552};
553
554/*
555 * tomoyo_no_rewrite_entry is a structure which is used for holding
556 * "deny_rewrite" entries.
557 * It has following fields.
558 *
559 * (1) "list" which is linked to tomoyo_no_rewrite_list .
560 * (2) "pattern" is a pathname which is by default not permitted to modify
561 * already existing content.
562 * (3) "is_deleted" is a bool which is true if marked as deleted, false
563 * otherwise.
564 */
565struct tomoyo_no_rewrite_entry {
566 struct list_head list;
567 const struct tomoyo_path_info *pattern;
568 bool is_deleted;
569};
570
571/*
572 * tomoyo_domain_initializer_entry is a structure which is used for holding
573 * "initialize_domain" and "no_initialize_domain" entries.
574 * It has following fields.
575 *
576 * (1) "list" which is linked to tomoyo_domain_initializer_list .
577 * (2) "domainname" which is "a domainname" or "the last component of a
578 * domainname". This field is NULL if "from" clause is not specified.
579 * (3) "program" which is a program's pathname.
580 * (4) "is_deleted" is a bool which is true if marked as deleted, false
581 * otherwise.
582 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
583 * otherwise.
584 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
585 * component of a domainname", false otherwise.
586 */
587struct tomoyo_domain_initializer_entry {
588 struct list_head list;
589 const struct tomoyo_path_info *domainname; /* This may be NULL */
590 const struct tomoyo_path_info *program;
591 bool is_deleted;
592 bool is_not; /* True if this entry is "no_initialize_domain". */
593 /* True if the domainname is tomoyo_get_last_name(). */
594 bool is_last_name;
595};
596
597/*
598 * tomoyo_domain_keeper_entry is a structure which is used for holding
599 * "keep_domain" and "no_keep_domain" entries.
600 * It has following fields.
601 *
602 * (1) "list" which is linked to tomoyo_domain_keeper_list .
603 * (2) "domainname" which is "a domainname" or "the last component of a
604 * domainname".
605 * (3) "program" which is a program's pathname.
606 * This field is NULL if "from" clause is not specified.
607 * (4) "is_deleted" is a bool which is true if marked as deleted, false
608 * otherwise.
609 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
610 * otherwise.
611 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
612 * component of a domainname", false otherwise.
613 */
614struct tomoyo_domain_keeper_entry {
615 struct list_head list;
616 const struct tomoyo_path_info *domainname;
617 const struct tomoyo_path_info *program; /* This may be NULL */
618 bool is_deleted;
619 bool is_not; /* True if this entry is "no_keep_domain". */
620 /* True if the domainname is tomoyo_get_last_name(). */
621 bool is_last_name;
622};
623
624/*
Tetsuo Handa10843072010-06-03 20:38:03 +0900625 * tomoyo_aggregator_entry is a structure which is used for holding
626 * "aggregator" entries.
627 * It has following fields.
628 *
629 * (1) "list" which is linked to tomoyo_aggregator_list .
630 * (2) "original_name" which is originally requested name.
631 * (3) "aggregated_name" which is name to rewrite.
632 * (4) "is_deleted" is a bool which is true if marked as deleted, false
633 * otherwise.
634 */
635struct tomoyo_aggregator_entry {
636 struct list_head list;
637 const struct tomoyo_path_info *original_name;
638 const struct tomoyo_path_info *aggregated_name;
639 bool is_deleted;
640};
641
642/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900643 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
644 * It has following fields.
645 *
646 * (1) "list" which is linked to tomoyo_alias_list .
647 * (2) "original_name" which is a dereferenced pathname.
648 * (3) "aliased_name" which is a symlink's pathname.
649 * (4) "is_deleted" is a bool which is true if marked as deleted, false
650 * otherwise.
651 */
652struct tomoyo_alias_entry {
653 struct list_head list;
654 const struct tomoyo_path_info *original_name;
655 const struct tomoyo_path_info *aliased_name;
656 bool is_deleted;
657};
658
659/*
660 * tomoyo_policy_manager_entry is a structure which is used for holding list of
661 * domainnames or programs which are permitted to modify configuration via
662 * /sys/kernel/security/tomoyo/ interface.
663 * It has following fields.
664 *
665 * (1) "list" which is linked to tomoyo_policy_manager_list .
666 * (2) "manager" is a domainname or a program's pathname.
667 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
668 * otherwise.
669 * (4) "is_deleted" is a bool which is true if marked as deleted, false
670 * otherwise.
671 */
672struct tomoyo_policy_manager_entry {
673 struct list_head list;
674 /* A path to program or a domainname. */
675 const struct tomoyo_path_info *manager;
676 bool is_domain; /* True if manager is a domainname. */
677 bool is_deleted; /* True if this entry is deleted. */
678};
679
Tetsuo Handa57c25902010-06-03 20:38:44 +0900680struct tomoyo_preference {
681 unsigned int learning_max_entry;
682 bool enforcing_verbose;
683 bool learning_verbose;
684 bool permissive_verbose;
685};
686
687struct tomoyo_profile {
688 const struct tomoyo_path_info *comment;
689 struct tomoyo_preference *learning;
690 struct tomoyo_preference *permissive;
691 struct tomoyo_preference *enforcing;
692 struct tomoyo_preference preference;
693 u8 default_config;
694 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
695};
696
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900697/********** Function prototypes. **********/
698
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900699extern asmlinkage long sys_getpid(void);
700extern asmlinkage long sys_getppid(void);
701
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900702/* Check whether the given string starts with the given keyword. */
703bool tomoyo_str_starts(char **src, const char *find);
704/* Get tomoyo_realpath() of current process. */
705const char *tomoyo_get_exe(void);
706/* Format string. */
707void tomoyo_normalize_line(unsigned char *buffer);
708/* Print warning or error message on console. */
709void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
710 __attribute__ ((format(printf, 2, 3)));
711/* Check all profiles currently assigned to domains are defined. */
712void tomoyo_check_profile(void);
713/* Open operation for /sys/kernel/security/tomoyo/ interface. */
714int tomoyo_open_control(const u8 type, struct file *file);
715/* Close /sys/kernel/security/tomoyo/ interface. */
716int tomoyo_close_control(struct file *file);
717/* Read operation for /sys/kernel/security/tomoyo/ interface. */
718int tomoyo_read_control(struct file *file, char __user *buffer,
719 const int buffer_len);
720/* Write operation for /sys/kernel/security/tomoyo/ interface. */
721int tomoyo_write_control(struct file *file, const char __user *buffer,
722 const int buffer_len);
723/* Check whether the domain has too many ACL entries to hold. */
724bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
725/* Print out of memory warning message. */
726void tomoyo_warn_oom(const char *function);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900727/* Check whether the given name matches the given name_union. */
728bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
729 const struct tomoyo_name_union *ptr);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900730/* Check whether the given number matches the given number_union. */
731bool tomoyo_compare_number_union(const unsigned long value,
732 const struct tomoyo_number_union *ptr);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900733int tomoyo_get_mode(const u8 profile, const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900734/* Transactional sprintf() for policy dump. */
735bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
736 __attribute__ ((format(printf, 2, 3)));
737/* Check whether the domainname is correct. */
Tetsuo Handa17080002010-02-16 21:14:48 +0900738bool tomoyo_is_correct_domain(const unsigned char *domainname);
Kentaro Takeda95908372009-02-05 17:18:13 +0900739/* Check whether the token is correct. */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900740bool tomoyo_is_correct_path(const char *filename);
741bool tomoyo_is_correct_word(const char *string);
Kentaro Takeda95908372009-02-05 17:18:13 +0900742/* Check whether the token can be a domainname. */
743bool tomoyo_is_domain_def(const unsigned char *buffer);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900744bool tomoyo_parse_name_union(const char *filename,
745 struct tomoyo_name_union *ptr);
746/* Check whether the given filename matches the given path_group. */
747bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900748 const struct tomoyo_path_group *group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900749/* Check whether the given value matches the given number_group. */
750bool tomoyo_number_matches_group(const unsigned long min,
751 const unsigned long max,
752 const struct tomoyo_number_group *group);
Kentaro Takeda95908372009-02-05 17:18:13 +0900753/* Check whether the given filename matches the given pattern. */
754bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
755 const struct tomoyo_path_info *pattern);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900756
757bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
758 const struct tomoyo_number_union *ptr);
759bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
760
Tetsuo Handa10843072010-06-03 20:38:03 +0900761/* Read "aggregator" entry in exception policy. */
762bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900763/* Read "alias" entry in exception policy. */
764bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
765/*
766 * Read "initialize_domain" and "no_initialize_domain" entry
767 * in exception policy.
768 */
769bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
770/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
771bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
772/* Read "file_pattern" entry in exception policy. */
773bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900774/* Read "path_group" entry in exception policy. */
775bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900776/* Read "number_group" entry in exception policy. */
777bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900778/* Read "allow_read" entry in exception policy. */
779bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
780/* Read "deny_rewrite" entry in exception policy. */
781bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900782/* Tokenize a line. */
783bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
Kentaro Takeda95908372009-02-05 17:18:13 +0900784/* Write domain policy violation warning message to console? */
785bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
786/* Convert double path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900787const char *tomoyo_path22keyword(const u8 operation);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900788const char *tomoyo_path_number2keyword(const u8 operation);
789const char *tomoyo_path_number32keyword(const u8 operation);
Kentaro Takeda95908372009-02-05 17:18:13 +0900790/* Get the last component of the given domainname. */
791const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
Kentaro Takeda95908372009-02-05 17:18:13 +0900792/* Convert single path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900793const char *tomoyo_path2keyword(const u8 operation);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900794/* Fill "struct tomoyo_request_info". */
795int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900796 struct tomoyo_domain_info *domain,
797 const u8 index);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900798/* Check permission for mount operation. */
799int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
800 unsigned long flags, void *data_page);
Tetsuo Handa10843072010-06-03 20:38:03 +0900801/* Create "aggregator" entry in exception policy. */
802int tomoyo_write_aggregator_policy(char *data, const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900803/* Create "alias" entry in exception policy. */
804int tomoyo_write_alias_policy(char *data, const bool is_delete);
805/*
806 * Create "initialize_domain" and "no_initialize_domain" entry
807 * in exception policy.
808 */
809int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
810 const bool is_delete);
811/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
812int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
813 const bool is_delete);
814/*
815 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
816 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
817 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
818 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
819 * "allow_link" entry in domain policy.
820 */
821int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
822 const bool is_delete);
823/* Create "allow_read" entry in exception policy. */
824int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900825/* Create "allow_mount" entry in domain policy. */
826int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
827 const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900828/* Create "deny_rewrite" entry in exception policy. */
829int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
830/* Create "file_pattern" entry in exception policy. */
831int tomoyo_write_pattern_policy(char *data, const bool is_delete);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900832/* Create "path_group" entry in exception policy. */
833int tomoyo_write_path_group_policy(char *data, const bool is_delete);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900834int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
835 __attribute__ ((format(printf, 2, 3)));
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900836/* Create "number_group" entry in exception policy. */
837int tomoyo_write_number_group_policy(char *data, const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900838/* Find a domain by the given name. */
839struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
840/* Find or create a domain by the given name. */
841struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
842 domainname,
843 const u8 profile);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900844struct tomoyo_profile *tomoyo_profile(const u8 profile);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900845/* Allocate memory for "struct tomoyo_path_group". */
846struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900847struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900848
Kentaro Takeda95908372009-02-05 17:18:13 +0900849/* Check mode for specified functionality. */
850unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
851 const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900852/* Fill in "struct tomoyo_path_info" members. */
853void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
854/* Run policy loader when /sbin/init starts. */
855void tomoyo_load_policy(const char *filename);
Kentaro Takeda95908372009-02-05 17:18:13 +0900856
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900857void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
858
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900859/* Convert binary string to ascii string. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900860char *tomoyo_encode(const char *str);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900861
862/*
863 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
864 * These functions use kzalloc(), so the caller must call kfree()
865 * if these functions didn't return NULL.
866 */
867char *tomoyo_realpath(const char *pathname);
868/*
869 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
870 */
871char *tomoyo_realpath_nofollow(const char *pathname);
872/* Same with tomoyo_realpath() except that the pathname is already solved. */
873char *tomoyo_realpath_from_path(struct path *path);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900874/* Get patterned pathname. */
875const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900876
877/* Check memory quota. */
878bool tomoyo_memory_ok(void *ptr);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900879void *tomoyo_commit_ok(void *data, const unsigned int size);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900880
881/*
882 * Keep the given name on the RAM.
883 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
884 */
885const struct tomoyo_path_info *tomoyo_get_name(const char *name);
886
887/* Check for memory usage. */
888int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
889
890/* Set memory quota. */
891int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
892
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900893/* Initialize mm related code. */
894void __init tomoyo_mm_init(void);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900895int tomoyo_check_exec_perm(struct tomoyo_request_info *r,
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900896 const struct tomoyo_path_info *filename);
897int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
898 struct path *path, const int flag);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900899int tomoyo_path_number_perm(const u8 operation, struct path *path,
900 unsigned long number);
901int tomoyo_path_number3_perm(const u8 operation, struct path *path,
902 const unsigned int mode, unsigned int dev);
Tetsuo Handa97d69312010-02-16 09:46:15 +0900903int tomoyo_path_perm(const u8 operation, struct path *path);
904int tomoyo_path2_perm(const u8 operation, struct path *path1,
905 struct path *path2);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900906int tomoyo_find_next_domain(struct linux_binprm *bprm);
907
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900908void tomoyo_print_ulong(char *buffer, const int buffer_len,
909 const unsigned long value, const u8 type);
910
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900911/* Drop refcount on tomoyo_name_union. */
912void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
913
Tetsuo Handa847b1732010-02-11 09:43:54 +0900914/* Run garbage collector. */
915void tomoyo_run_gc(void);
916
917void tomoyo_memory_free(void *ptr);
918
Tetsuo Handa237ab452010-06-12 20:46:22 +0900919int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
920 bool is_delete, struct tomoyo_domain_info *domain,
921 bool (*check_duplicate) (const struct tomoyo_acl_info
922 *,
923 const struct tomoyo_acl_info
924 *),
925 bool (*merge_duplicate) (struct tomoyo_acl_info *,
926 struct tomoyo_acl_info *,
927 const bool));
928
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900929/********** External variable definitions. **********/
930
931/* Lock for GC. */
932extern struct srcu_struct tomoyo_ss;
933
934/* The list for "struct tomoyo_domain_info". */
935extern struct list_head tomoyo_domain_list;
936
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900937extern struct list_head tomoyo_path_group_list;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900938extern struct list_head tomoyo_number_group_list;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900939extern struct list_head tomoyo_domain_initializer_list;
940extern struct list_head tomoyo_domain_keeper_list;
Tetsuo Handa10843072010-06-03 20:38:03 +0900941extern struct list_head tomoyo_aggregator_list;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900942extern struct list_head tomoyo_alias_list;
943extern struct list_head tomoyo_globally_readable_list;
944extern struct list_head tomoyo_pattern_list;
945extern struct list_head tomoyo_no_rewrite_list;
946extern struct list_head tomoyo_policy_manager_list;
947extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
Tetsuo Handa847b1732010-02-11 09:43:54 +0900948
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900949/* Lock for protecting policy. */
950extern struct mutex tomoyo_policy_lock;
951
952/* Has /sbin/init started? */
953extern bool tomoyo_policy_loaded;
954
955/* The kernel's domain. */
956extern struct tomoyo_domain_info tomoyo_kernel_domain;
957
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900958extern unsigned int tomoyo_quota_for_query;
959extern unsigned int tomoyo_query_memory_size;
960
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900961/********** Inlined functions. **********/
962
963static inline int tomoyo_read_lock(void)
964{
965 return srcu_read_lock(&tomoyo_ss);
966}
967
968static inline void tomoyo_read_unlock(int idx)
969{
970 srcu_read_unlock(&tomoyo_ss, idx);
971}
972
Kentaro Takeda95908372009-02-05 17:18:13 +0900973/* strcmp() for "struct tomoyo_path_info" structure. */
974static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
975 const struct tomoyo_path_info *b)
976{
977 return a->hash != b->hash || strcmp(a->name, b->name);
978}
979
Kentaro Takeda95908372009-02-05 17:18:13 +0900980/**
981 * tomoyo_is_valid - Check whether the character is a valid char.
982 *
983 * @c: The character to check.
984 *
985 * Returns true if @c is a valid character, false otherwise.
986 */
987static inline bool tomoyo_is_valid(const unsigned char c)
988{
989 return c > ' ' && c < 127;
990}
991
992/**
993 * tomoyo_is_invalid - Check whether the character is an invalid char.
994 *
995 * @c: The character to check.
996 *
997 * Returns true if @c is an invalid character, false otherwise.
998 */
999static inline bool tomoyo_is_invalid(const unsigned char c)
1000{
1001 return c && (c <= ' ' || c >= 127);
1002}
1003
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001004static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
1005{
1006 if (name) {
1007 struct tomoyo_name_entry *ptr =
1008 container_of(name, struct tomoyo_name_entry, entry);
1009 atomic_dec(&ptr->users);
1010 }
1011}
Kentaro Takeda95908372009-02-05 17:18:13 +09001012
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001013static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
1014{
1015 if (group)
1016 atomic_dec(&group->users);
1017}
1018
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001019static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
1020{
1021 if (group)
1022 atomic_dec(&group->users);
1023}
1024
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001025static inline struct tomoyo_domain_info *tomoyo_domain(void)
1026{
1027 return current_cred()->security;
1028}
Kentaro Takeda95908372009-02-05 17:18:13 +09001029
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001030static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
1031 *task)
1032{
1033 return task_cred_xxx(task, security);
1034}
Kentaro Takeda95908372009-02-05 17:18:13 +09001035
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001036static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
1037 const struct tomoyo_acl_info *p2)
1038{
1039 return p1->type == p2->type;
1040}
1041
1042static inline bool tomoyo_is_same_name_union
1043(const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
1044{
1045 return p1->filename == p2->filename && p1->group == p2->group &&
1046 p1->is_group == p2->is_group;
1047}
1048
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001049static inline bool tomoyo_is_same_number_union
1050(const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
1051{
1052 return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
1053 && p1->group == p2->group && p1->min_type == p2->min_type &&
1054 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
1055}
1056
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +09001057static inline bool tomoyo_is_same_domain_initializer_entry
1058(const struct tomoyo_domain_initializer_entry *p1,
1059 const struct tomoyo_domain_initializer_entry *p2)
1060{
1061 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1062 && p1->domainname == p2->domainname
1063 && p1->program == p2->program;
1064}
1065
1066static inline bool tomoyo_is_same_domain_keeper_entry
1067(const struct tomoyo_domain_keeper_entry *p1,
1068 const struct tomoyo_domain_keeper_entry *p2)
1069{
1070 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1071 && p1->domainname == p2->domainname
1072 && p1->program == p2->program;
1073}
1074
Tetsuo Handa10843072010-06-03 20:38:03 +09001075static inline bool tomoyo_is_same_aggregator_entry
1076(const struct tomoyo_aggregator_entry *p1,
1077 const struct tomoyo_aggregator_entry *p2)
1078{
1079 return p1->original_name == p2->original_name &&
1080 p1->aggregated_name == p2->aggregated_name;
1081}
1082
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +09001083static inline bool tomoyo_is_same_alias_entry
1084(const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1085{
1086 return p1->original_name == p2->original_name &&
1087 p1->aliased_name == p2->aliased_name;
1088}
1089
Kentaro Takeda95908372009-02-05 17:18:13 +09001090/**
1091 * list_for_each_cookie - iterate over a list with cookie.
1092 * @pos: the &struct list_head to use as a loop cursor.
1093 * @cookie: the &struct list_head to use as a cookie.
1094 * @head: the head for your list.
1095 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001096 * Same with list_for_each_rcu() except that this primitive uses @cookie
Kentaro Takeda95908372009-02-05 17:18:13 +09001097 * so that we can continue iteration.
1098 * @cookie must be NULL when iteration starts, and @cookie will become
1099 * NULL when iteration finishes.
1100 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001101#define list_for_each_cookie(pos, cookie, head) \
1102 for (({ if (!cookie) \
1103 cookie = head; }), \
1104 pos = rcu_dereference((cookie)->next); \
1105 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
1106 (cookie) = pos, pos = rcu_dereference(pos->next))
1107
Kentaro Takeda95908372009-02-05 17:18:13 +09001108#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */