blob: 36b027460ea6cb2dab23af476993f216d8a75f5e [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 Handa82e0f002010-06-15 09:22:42 +0900192 * tomoyo_acl_head is a structure which is used for holding elements not in
193 * domain policy.
194 * It has following fields.
195 *
196 * (1) "list" which is linked to tomoyo_policy_list[] .
197 * (2) "is_deleted" is a bool which is true if marked as deleted, false
198 * otherwise.
199 */
200struct tomoyo_acl_head {
201 struct list_head list;
202 bool is_deleted;
203} __packed;
204
205/*
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900206 * tomoyo_request_info is a structure which is used for holding
207 *
208 * (1) Domain information of current process.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900209 * (2) How many retries are made for this request.
210 * (3) Profile number used for this request.
211 * (4) Access control mode of the profile.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900212 */
213struct tomoyo_request_info {
214 struct tomoyo_domain_info *domain;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900215 /* For holding parameters. */
216 union {
217 struct {
218 const struct tomoyo_path_info *filename;
219 u8 operation;
220 } path;
221 struct {
222 const struct tomoyo_path_info *filename1;
223 const struct tomoyo_path_info *filename2;
224 u8 operation;
225 } path2;
226 struct {
227 const struct tomoyo_path_info *filename;
228 unsigned int mode;
229 unsigned int major;
230 unsigned int minor;
231 u8 operation;
232 } mkdev;
233 struct {
234 const struct tomoyo_path_info *filename;
235 unsigned long number;
236 u8 operation;
237 } path_number;
238 struct {
239 const struct tomoyo_path_info *type;
240 const struct tomoyo_path_info *dir;
241 const struct tomoyo_path_info *dev;
242 unsigned long flags;
243 int need_dev;
244 } mount;
245 } param;
246 u8 param_type;
247 bool granted;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900248 u8 retry;
249 u8 profile;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900250 u8 mode; /* One of tomoyo_mode_index . */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900251 u8 type;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900252};
253
254/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900255 * tomoyo_path_info is a structure which is used for holding a string data
256 * used by TOMOYO.
257 * This structure has several fields for supporting pattern matching.
258 *
259 * (1) "name" is the '\0' terminated string data.
260 * (2) "hash" is full_name_hash(name, strlen(name)).
261 * This allows tomoyo_pathcmp() to compare by hash before actually compare
262 * using strcmp().
263 * (3) "const_len" is the length of the initial segment of "name" which
264 * consists entirely of non wildcard characters. In other words, the length
265 * which we can compare two strings using strncmp().
266 * (4) "is_dir" is a bool which is true if "name" ends with "/",
267 * false otherwise.
268 * TOMOYO distinguishes directory and non-directory. A directory ends with
269 * "/" and non-directory does not end with "/".
270 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
271 * characters, false otherwise. This allows TOMOYO to use "hash" and
272 * strcmp() for string comparison if "is_patterned" is false.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900273 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900274struct tomoyo_path_info {
275 const char *name;
276 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900277 u16 const_len; /* = tomoyo_const_part_length(name) */
278 bool is_dir; /* = tomoyo_strendswith(name, "/") */
279 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900280};
281
282/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900283 * tomoyo_name_entry is a structure which is used for linking
284 * "struct tomoyo_path_info" into tomoyo_name_list .
Kentaro Takeda95908372009-02-05 17:18:13 +0900285 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900286struct tomoyo_name_entry {
287 struct list_head list;
288 atomic_t users;
289 struct tomoyo_path_info entry;
290};
Kentaro Takeda95908372009-02-05 17:18:13 +0900291
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900292struct tomoyo_name_union {
293 const struct tomoyo_path_info *filename;
294 struct tomoyo_path_group *group;
295 u8 is_group;
296};
297
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900298struct tomoyo_number_union {
299 unsigned long values[2];
300 struct tomoyo_number_group *group;
301 u8 min_type;
302 u8 max_type;
303 u8 is_group;
304};
305
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900306/* Structure for "path_group" directive. */
307struct tomoyo_path_group {
308 struct list_head list;
309 const struct tomoyo_path_info *group_name;
310 struct list_head member_list;
311 atomic_t users;
312};
313
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900314/* Structure for "number_group" directive. */
315struct tomoyo_number_group {
316 struct list_head list;
317 const struct tomoyo_path_info *group_name;
318 struct list_head member_list;
319 atomic_t users;
320};
321
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900322/* Structure for "path_group" directive. */
323struct tomoyo_path_group_member {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900324 struct tomoyo_acl_head head;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900325 const struct tomoyo_path_info *member_name;
326};
327
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900328/* Structure for "number_group" directive. */
329struct tomoyo_number_group_member {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900330 struct tomoyo_acl_head head;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900331 struct tomoyo_number_union number;
332};
333
Kentaro Takeda95908372009-02-05 17:18:13 +0900334/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900335 * tomoyo_acl_info is a structure which is used for holding
336 *
337 * (1) "list" which is linked to the ->acl_info_list of
338 * "struct tomoyo_domain_info"
Tetsuo Handa237ab452010-06-12 20:46:22 +0900339 * (2) "is_deleted" is a bool which is true if this domain is marked as
340 * "deleted", false otherwise.
341 * (3) "type" which tells type of the entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900342 *
343 * Packing "struct tomoyo_acl_info" allows
Tetsuo Handa237ab452010-06-12 20:46:22 +0900344 * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
345 * "struct tomoyo_path_number_acl" "struct tomoyo_path_number3_acl" to embed
346 * "u8" without enlarging their structure size.
Kentaro Takeda95908372009-02-05 17:18:13 +0900347 */
348struct tomoyo_acl_info {
349 struct list_head list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900350 bool is_deleted;
351 u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
Kentaro Takeda95908372009-02-05 17:18:13 +0900352} __packed;
353
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900354/*
355 * tomoyo_domain_info is a structure which is used for holding permissions
356 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
357 * It has following fields.
358 *
359 * (1) "list" which is linked to tomoyo_domain_list .
360 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
361 * (3) "domainname" which holds the name of the domain.
362 * (4) "profile" which remembers profile number assigned to this domain.
363 * (5) "is_deleted" is a bool which is true if this domain is marked as
364 * "deleted", false otherwise.
365 * (6) "quota_warned" is a bool which is used for suppressing warning message
366 * when learning mode learned too much entries.
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900367 * (7) "ignore_global_allow_read" is a bool which is true if this domain
368 * should ignore "allow_read" directive in exception policy.
369 * (8) "transition_failed" is a bool which is set to true when this domain was
370 * unable to create a new domain at tomoyo_find_next_domain() because the
371 * name of the domain to be created was too long or it could not allocate
372 * memory. If set to true, more than one process continued execve()
373 * without domain transition.
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900374 * (9) "users" is an atomic_t that holds how many "struct cred"->security
375 * are referring this "struct tomoyo_domain_info". If is_deleted == true
376 * and users == 0, this struct will be kfree()d upon next garbage
377 * collection.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900378 *
379 * A domain's lifecycle is an analogy of files on / directory.
380 * Multiple domains with the same domainname cannot be created (as with
381 * creating files with the same filename fails with -EEXIST).
382 * If a process reached a domain, that process can reside in that domain after
383 * that domain is marked as "deleted" (as with a process can access an already
384 * open()ed file after that file was unlink()ed).
385 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900386struct tomoyo_domain_info {
387 struct list_head list;
388 struct list_head acl_info_list;
389 /* Name of this domain. Never NULL. */
390 const struct tomoyo_path_info *domainname;
391 u8 profile; /* Profile number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900392 bool is_deleted; /* Delete flag. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900393 bool quota_warned; /* Quota warnning flag. */
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900394 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
395 bool transition_failed; /* Domain transition failed flag. */
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900396 atomic_t users; /* Number of referring credentials. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900397};
398
Kentaro Takeda95908372009-02-05 17:18:13 +0900399/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900400 * tomoyo_path_acl is a structure which is used for holding an
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900401 * entry with one pathname operation (e.g. open(), mkdir()).
402 * It has following fields.
403 *
404 * (1) "head" which is a "struct tomoyo_acl_info".
405 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900406 * (3) "name" is the pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900407 *
408 * Directives held by this structure are "allow_read/write", "allow_execute",
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900409 * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900410 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
411 * "allow_unmount".
Kentaro Takeda95908372009-02-05 17:18:13 +0900412 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900413struct tomoyo_path_acl {
414 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900415 u16 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900416 struct tomoyo_name_union name;
Kentaro Takeda95908372009-02-05 17:18:13 +0900417};
418
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900419/*
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900420 * tomoyo_path_number_acl is a structure which is used for holding an
421 * entry with one pathname and one number operation.
422 * 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.
426 * (3) "name" is the pathname.
427 * (4) "number" is the numeric value.
428 *
429 * Directives held by this structure are "allow_create", "allow_mkdir",
430 * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
431 * and "allow_chgrp".
432 *
433 */
434struct tomoyo_path_number_acl {
435 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
436 u8 perm;
437 struct tomoyo_name_union name;
438 struct tomoyo_number_union number;
439};
440
441/*
442 * tomoyo_path_number3_acl is a structure which is used for holding an
443 * entry with one pathname and three numbers operation.
444 * It has following fields.
445 *
446 * (1) "head" which is a "struct tomoyo_acl_info".
447 * (2) "perm" which is a bitmask of permitted operations.
448 * (3) "mode" is the create mode.
449 * (4) "major" is the major number of device node.
450 * (5) "minor" is the minor number of device node.
451 *
452 * Directives held by this structure are "allow_mkchar", "allow_mkblock".
453 *
454 */
455struct tomoyo_path_number3_acl {
456 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
457 u8 perm;
458 struct tomoyo_name_union name;
459 struct tomoyo_number_union mode;
460 struct tomoyo_number_union major;
461 struct tomoyo_number_union minor;
462};
463
464/*
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900465 * tomoyo_path2_acl is a structure which is used for holding an
Tetsuo Handa937bf612009-12-02 21:09:48 +0900466 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900467 * It has following fields.
468 *
469 * (1) "head" which is a "struct tomoyo_acl_info".
470 * (2) "perm" which is a bitmask of permitted operations.
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900471 * (3) "name1" is the source/old pathname.
472 * (4) "name2" is the destination/new pathname.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900473 *
Tetsuo Handa937bf612009-12-02 21:09:48 +0900474 * Directives held by this structure are "allow_rename", "allow_link" and
475 * "allow_pivot_root".
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900476 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900477struct tomoyo_path2_acl {
478 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
Kentaro Takeda95908372009-02-05 17:18:13 +0900479 u8 perm;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900480 struct tomoyo_name_union name1;
481 struct tomoyo_name_union name2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900482};
483
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900484/*
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900485 * tomoyo_mount_acl is a structure which is used for holding an
486 * entry for mount operation.
487 * It has following fields.
488 *
489 * (1) "head" which is a "struct tomoyo_acl_info".
Tetsuo Handa237ab452010-06-12 20:46:22 +0900490 * (2) "dev_name" is the device name.
491 * (3) "dir_name" is the mount point.
492 * (4) "fs_type" is the filesystem type.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900493 * (5) "flags" is the mount flags.
494 *
Tetsuo Handa237ab452010-06-12 20:46:22 +0900495 * Directive held by this structure is "allow_mount".
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900496 */
497struct tomoyo_mount_acl {
498 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900499 struct tomoyo_name_union dev_name;
500 struct tomoyo_name_union dir_name;
501 struct tomoyo_name_union fs_type;
502 struct tomoyo_number_union flags;
503};
504
505/*
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900506 * tomoyo_io_buffer is a structure which is used for reading and modifying
507 * configuration via /sys/kernel/security/tomoyo/ interface.
508 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
509 * cursors.
510 *
511 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
512 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
513 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
514 * reading (one is for traversing tomoyo_domain_list and the other is for
515 * traversing "struct tomoyo_acl_info"->acl_info_list ).
516 *
517 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
518 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
519 * domain with the domainname specified by the rest of that line (NULL is set
520 * if seek failed).
521 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
522 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
523 * line (->write_var1 is set to NULL if a domain was deleted).
524 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
525 * neither "select " nor "delete ", an entry or a domain specified by that line
526 * is appended.
527 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900528struct tomoyo_io_buffer {
529 int (*read) (struct tomoyo_io_buffer *);
530 int (*write) (struct tomoyo_io_buffer *);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900531 int (*poll) (struct file *file, poll_table *wait);
Kentaro Takeda95908372009-02-05 17:18:13 +0900532 /* Exclusive lock for this structure. */
533 struct mutex io_sem;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900534 /* Index returned by tomoyo_read_lock(). */
535 int reader_idx;
Kentaro Takeda95908372009-02-05 17:18:13 +0900536 /* The position currently reading from. */
537 struct list_head *read_var1;
538 /* Extra variables for reading. */
539 struct list_head *read_var2;
540 /* The position currently writing to. */
541 struct tomoyo_domain_info *write_var1;
542 /* The step for reading. */
543 int read_step;
544 /* Buffer for reading. */
545 char *read_buf;
546 /* EOF flag for reading. */
547 bool read_eof;
548 /* Read domain ACL of specified PID? */
549 bool read_single_domain;
550 /* Extra variable for reading. */
551 u8 read_bit;
552 /* Bytes available for reading. */
553 int read_avail;
554 /* Size of read buffer. */
555 int readbuf_size;
556 /* Buffer for writing. */
557 char *write_buf;
558 /* Bytes available for writing. */
559 int write_avail;
560 /* Size of write buffer. */
561 int writebuf_size;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900562 /* Type of this interface. */
563 u8 type;
Kentaro Takeda95908372009-02-05 17:18:13 +0900564};
565
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900566/*
567 * tomoyo_globally_readable_file_entry is a structure which is used for holding
568 * "allow_read" entries.
569 * It has following fields.
570 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900571 * (1) "head" is "struct tomoyo_acl_head".
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900572 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900573 */
574struct tomoyo_globally_readable_file_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900575 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900576 const struct tomoyo_path_info *filename;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900577};
578
579/*
580 * tomoyo_pattern_entry is a structure which is used for holding
581 * "tomoyo_pattern_list" entries.
582 * It has following fields.
583 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900584 * (1) "head" is "struct tomoyo_acl_head".
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900585 * (2) "pattern" is a pathname pattern which is used for converting pathnames
586 * to pathname patterns during learning mode.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900587 */
588struct tomoyo_pattern_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900589 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900590 const struct tomoyo_path_info *pattern;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900591};
592
593/*
594 * tomoyo_no_rewrite_entry is a structure which is used for holding
595 * "deny_rewrite" entries.
596 * It has following fields.
597 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900598 * (1) "head" is "struct tomoyo_acl_head".
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900599 * (2) "pattern" is a pathname which is by default not permitted to modify
600 * already existing content.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900601 */
602struct tomoyo_no_rewrite_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900603 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900604 const struct tomoyo_path_info *pattern;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900605};
606
607/*
608 * tomoyo_domain_initializer_entry is a structure which is used for holding
609 * "initialize_domain" and "no_initialize_domain" entries.
610 * It has following fields.
611 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900612 * (1) "head" is "struct tomoyo_acl_head".
613 * (2) "is_not" is a bool which is true if "no_initialize_domain", false
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900614 * otherwise.
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900615 * (3) "is_last_name" is a bool which is true if "domainname" is "the last
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900616 * component of a domainname", false otherwise.
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900617 * (4) "domainname" which is "a domainname" or "the last component of a
618 * domainname". This field is NULL if "from" clause is not specified.
619 * (5) "program" which is a program's pathname.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900620 */
621struct tomoyo_domain_initializer_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900622 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900623 bool is_not; /* True if this entry is "no_initialize_domain". */
624 /* True if the domainname is tomoyo_get_last_name(). */
625 bool is_last_name;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900626 const struct tomoyo_path_info *domainname; /* This may be NULL */
627 const struct tomoyo_path_info *program;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900628};
629
630/*
631 * tomoyo_domain_keeper_entry is a structure which is used for holding
632 * "keep_domain" and "no_keep_domain" entries.
633 * It has following fields.
634 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900635 * (1) "head" is "struct tomoyo_acl_head".
636 * (2) "is_not" is a bool which is true if "no_initialize_domain", false
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900637 * otherwise.
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900638 * (3) "is_last_name" is a bool which is true if "domainname" is "the last
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900639 * component of a domainname", false otherwise.
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900640 * (4) "domainname" which is "a domainname" or "the last component of a
641 * domainname".
642 * (5) "program" which is a program's pathname.
643 * This field is NULL if "from" clause is not specified.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900644 */
645struct tomoyo_domain_keeper_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900646 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900647 bool is_not; /* True if this entry is "no_keep_domain". */
648 /* True if the domainname is tomoyo_get_last_name(). */
649 bool is_last_name;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900650 const struct tomoyo_path_info *domainname;
651 const struct tomoyo_path_info *program; /* This may be NULL */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900652};
653
654/*
Tetsuo Handa10843072010-06-03 20:38:03 +0900655 * tomoyo_aggregator_entry is a structure which is used for holding
656 * "aggregator" entries.
657 * It has following fields.
658 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900659 * (1) "head" is "struct tomoyo_acl_head".
Tetsuo Handa10843072010-06-03 20:38:03 +0900660 * (2) "original_name" which is originally requested name.
661 * (3) "aggregated_name" which is name to rewrite.
Tetsuo Handa10843072010-06-03 20:38:03 +0900662 */
663struct tomoyo_aggregator_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900664 struct tomoyo_acl_head head;
Tetsuo Handa10843072010-06-03 20:38:03 +0900665 const struct tomoyo_path_info *original_name;
666 const struct tomoyo_path_info *aggregated_name;
Tetsuo Handa10843072010-06-03 20:38:03 +0900667};
668
669/*
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900670 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
671 * It has following fields.
672 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900673 * (1) "head" is "struct tomoyo_acl_head".
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900674 * (2) "original_name" which is a dereferenced pathname.
675 * (3) "aliased_name" which is a symlink's pathname.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900676 */
677struct tomoyo_alias_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900678 struct tomoyo_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900679 const struct tomoyo_path_info *original_name;
680 const struct tomoyo_path_info *aliased_name;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900681};
682
683/*
684 * tomoyo_policy_manager_entry is a structure which is used for holding list of
685 * domainnames or programs which are permitted to modify configuration via
686 * /sys/kernel/security/tomoyo/ interface.
687 * It has following fields.
688 *
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900689 * (1) "head" is "struct tomoyo_acl_head".
690 * (2) "is_domain" is a bool which is true if "manager" is a domainname, false
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900691 * otherwise.
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900692 * (3) "manager" is a domainname or a program's pathname.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900693 */
694struct tomoyo_policy_manager_entry {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900695 struct tomoyo_acl_head head;
696 bool is_domain; /* True if manager is a domainname. */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900697 /* A path to program or a domainname. */
698 const struct tomoyo_path_info *manager;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900699};
700
Tetsuo Handa57c25902010-06-03 20:38:44 +0900701struct tomoyo_preference {
702 unsigned int learning_max_entry;
703 bool enforcing_verbose;
704 bool learning_verbose;
705 bool permissive_verbose;
706};
707
708struct tomoyo_profile {
709 const struct tomoyo_path_info *comment;
710 struct tomoyo_preference *learning;
711 struct tomoyo_preference *permissive;
712 struct tomoyo_preference *enforcing;
713 struct tomoyo_preference preference;
714 u8 default_config;
715 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
716};
717
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900718/********** Function prototypes. **********/
719
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900720extern asmlinkage long sys_getpid(void);
721extern asmlinkage long sys_getppid(void);
722
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900723/* Check whether the given string starts with the given keyword. */
724bool tomoyo_str_starts(char **src, const char *find);
725/* Get tomoyo_realpath() of current process. */
726const char *tomoyo_get_exe(void);
727/* Format string. */
728void tomoyo_normalize_line(unsigned char *buffer);
729/* Print warning or error message on console. */
730void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
731 __attribute__ ((format(printf, 2, 3)));
732/* Check all profiles currently assigned to domains are defined. */
733void tomoyo_check_profile(void);
734/* Open operation for /sys/kernel/security/tomoyo/ interface. */
735int tomoyo_open_control(const u8 type, struct file *file);
736/* Close /sys/kernel/security/tomoyo/ interface. */
737int tomoyo_close_control(struct file *file);
738/* Read operation for /sys/kernel/security/tomoyo/ interface. */
739int tomoyo_read_control(struct file *file, char __user *buffer,
740 const int buffer_len);
741/* Write operation for /sys/kernel/security/tomoyo/ interface. */
742int tomoyo_write_control(struct file *file, const char __user *buffer,
743 const int buffer_len);
744/* Check whether the domain has too many ACL entries to hold. */
745bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
746/* Print out of memory warning message. */
747void tomoyo_warn_oom(const char *function);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900748/* Check whether the given name matches the given name_union. */
749bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
750 const struct tomoyo_name_union *ptr);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900751/* Check whether the given number matches the given number_union. */
752bool tomoyo_compare_number_union(const unsigned long value,
753 const struct tomoyo_number_union *ptr);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900754int tomoyo_get_mode(const u8 profile, const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900755/* Transactional sprintf() for policy dump. */
756bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
757 __attribute__ ((format(printf, 2, 3)));
758/* Check whether the domainname is correct. */
Tetsuo Handa17080002010-02-16 21:14:48 +0900759bool tomoyo_is_correct_domain(const unsigned char *domainname);
Kentaro Takeda95908372009-02-05 17:18:13 +0900760/* Check whether the token is correct. */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900761bool tomoyo_is_correct_path(const char *filename);
762bool tomoyo_is_correct_word(const char *string);
Kentaro Takeda95908372009-02-05 17:18:13 +0900763/* Check whether the token can be a domainname. */
764bool tomoyo_is_domain_def(const unsigned char *buffer);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900765bool tomoyo_parse_name_union(const char *filename,
766 struct tomoyo_name_union *ptr);
767/* Check whether the given filename matches the given path_group. */
768bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900769 const struct tomoyo_path_group *group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900770/* Check whether the given value matches the given number_group. */
771bool tomoyo_number_matches_group(const unsigned long min,
772 const unsigned long max,
773 const struct tomoyo_number_group *group);
Kentaro Takeda95908372009-02-05 17:18:13 +0900774/* Check whether the given filename matches the given pattern. */
775bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
776 const struct tomoyo_path_info *pattern);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900777
778bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
779 const struct tomoyo_number_union *ptr);
780bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
781
Tetsuo Handa10843072010-06-03 20:38:03 +0900782/* Read "aggregator" entry in exception policy. */
783bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900784/* Read "alias" entry in exception policy. */
785bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
786/*
787 * Read "initialize_domain" and "no_initialize_domain" entry
788 * in exception policy.
789 */
790bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
791/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
792bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
793/* Read "file_pattern" entry in exception policy. */
794bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900795/* Read "path_group" entry in exception policy. */
796bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900797/* Read "number_group" entry in exception policy. */
798bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900799/* Read "allow_read" entry in exception policy. */
800bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
801/* Read "deny_rewrite" entry in exception policy. */
802bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900803/* Tokenize a line. */
804bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
Kentaro Takeda95908372009-02-05 17:18:13 +0900805/* Write domain policy violation warning message to console? */
806bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
807/* Convert double path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900808const char *tomoyo_path22keyword(const u8 operation);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900809const char *tomoyo_path_number2keyword(const u8 operation);
810const char *tomoyo_path_number32keyword(const u8 operation);
Kentaro Takeda95908372009-02-05 17:18:13 +0900811/* Get the last component of the given domainname. */
812const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
Kentaro Takeda95908372009-02-05 17:18:13 +0900813/* Convert single path operation to operation name. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900814const char *tomoyo_path2keyword(const u8 operation);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900815/* Fill "struct tomoyo_request_info". */
816int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900817 struct tomoyo_domain_info *domain,
818 const u8 index);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900819/* Check permission for mount operation. */
820int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
821 unsigned long flags, void *data_page);
Tetsuo Handa10843072010-06-03 20:38:03 +0900822/* Create "aggregator" entry in exception policy. */
823int tomoyo_write_aggregator_policy(char *data, const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900824/* Create "alias" entry in exception policy. */
825int tomoyo_write_alias_policy(char *data, const bool is_delete);
826/*
827 * Create "initialize_domain" and "no_initialize_domain" entry
828 * in exception policy.
829 */
830int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
831 const bool is_delete);
832/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
833int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
834 const bool is_delete);
835/*
836 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
837 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
838 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
839 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
840 * "allow_link" entry in domain policy.
841 */
842int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
843 const bool is_delete);
844/* Create "allow_read" entry in exception policy. */
845int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900846/* Create "allow_mount" entry in domain policy. */
847int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
848 const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900849/* Create "deny_rewrite" entry in exception policy. */
850int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
851/* Create "file_pattern" entry in exception policy. */
852int tomoyo_write_pattern_policy(char *data, const bool is_delete);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900853/* Create "path_group" entry in exception policy. */
854int tomoyo_write_path_group_policy(char *data, const bool is_delete);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900855int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
856 __attribute__ ((format(printf, 2, 3)));
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900857/* Create "number_group" entry in exception policy. */
858int tomoyo_write_number_group_policy(char *data, const bool is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900859/* Find a domain by the given name. */
860struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
861/* Find or create a domain by the given name. */
862struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
863 domainname,
864 const u8 profile);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900865struct tomoyo_profile *tomoyo_profile(const u8 profile);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900866/* Allocate memory for "struct tomoyo_path_group". */
867struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900868struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900869
Kentaro Takeda95908372009-02-05 17:18:13 +0900870/* Check mode for specified functionality. */
871unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
872 const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900873/* Fill in "struct tomoyo_path_info" members. */
874void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
875/* Run policy loader when /sbin/init starts. */
876void tomoyo_load_policy(const char *filename);
Kentaro Takeda95908372009-02-05 17:18:13 +0900877
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900878void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
879
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900880/* Convert binary string to ascii string. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900881char *tomoyo_encode(const char *str);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900882
883/*
884 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
885 * These functions use kzalloc(), so the caller must call kfree()
886 * if these functions didn't return NULL.
887 */
888char *tomoyo_realpath(const char *pathname);
889/*
890 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
891 */
892char *tomoyo_realpath_nofollow(const char *pathname);
893/* Same with tomoyo_realpath() except that the pathname is already solved. */
894char *tomoyo_realpath_from_path(struct path *path);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900895/* Get patterned pathname. */
896const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900897
898/* Check memory quota. */
899bool tomoyo_memory_ok(void *ptr);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900900void *tomoyo_commit_ok(void *data, const unsigned int size);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900901
902/*
903 * Keep the given name on the RAM.
904 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
905 */
906const struct tomoyo_path_info *tomoyo_get_name(const char *name);
907
908/* Check for memory usage. */
909int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
910
911/* Set memory quota. */
912int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
913
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900914/* Initialize mm related code. */
915void __init tomoyo_mm_init(void);
Tetsuo Handa05336de2010-06-16 16:20:24 +0900916int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900917 const struct tomoyo_path_info *filename);
918int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
919 struct path *path, const int flag);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900920int tomoyo_path_number_perm(const u8 operation, struct path *path,
921 unsigned long number);
922int tomoyo_path_number3_perm(const u8 operation, struct path *path,
923 const unsigned int mode, unsigned int dev);
Tetsuo Handa97d69312010-02-16 09:46:15 +0900924int tomoyo_path_perm(const u8 operation, struct path *path);
925int tomoyo_path2_perm(const u8 operation, struct path *path1,
926 struct path *path2);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900927int tomoyo_find_next_domain(struct linux_binprm *bprm);
928
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900929void tomoyo_print_ulong(char *buffer, const int buffer_len,
930 const unsigned long value, const u8 type);
931
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900932/* Drop refcount on tomoyo_name_union. */
933void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
934
Tetsuo Handa847b1732010-02-11 09:43:54 +0900935/* Run garbage collector. */
936void tomoyo_run_gc(void);
937
938void tomoyo_memory_free(void *ptr);
939
Tetsuo Handa237ab452010-06-12 20:46:22 +0900940int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
941 bool is_delete, struct tomoyo_domain_info *domain,
942 bool (*check_duplicate) (const struct tomoyo_acl_info
943 *,
944 const struct tomoyo_acl_info
945 *),
946 bool (*merge_duplicate) (struct tomoyo_acl_info *,
947 struct tomoyo_acl_info *,
948 const bool));
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900949int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
950 bool is_delete, struct list_head *list,
951 bool (*check_duplicate) (const struct tomoyo_acl_head
952 *,
953 const struct tomoyo_acl_head
954 *));
Tetsuo Handa99a85252010-06-16 16:22:51 +0900955void tomoyo_check_acl(struct tomoyo_request_info *r,
956 bool (*check_entry) (const struct tomoyo_request_info *,
957 const struct tomoyo_acl_info *));
Tetsuo Handa237ab452010-06-12 20:46:22 +0900958
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900959/********** External variable definitions. **********/
960
961/* Lock for GC. */
962extern struct srcu_struct tomoyo_ss;
963
964/* The list for "struct tomoyo_domain_info". */
965extern struct list_head tomoyo_domain_list;
966
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900967extern struct list_head tomoyo_path_group_list;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900968extern struct list_head tomoyo_number_group_list;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900969extern struct list_head tomoyo_domain_initializer_list;
970extern struct list_head tomoyo_domain_keeper_list;
Tetsuo Handa10843072010-06-03 20:38:03 +0900971extern struct list_head tomoyo_aggregator_list;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900972extern struct list_head tomoyo_alias_list;
973extern struct list_head tomoyo_globally_readable_list;
974extern struct list_head tomoyo_pattern_list;
975extern struct list_head tomoyo_no_rewrite_list;
976extern struct list_head tomoyo_policy_manager_list;
977extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
Tetsuo Handa847b1732010-02-11 09:43:54 +0900978
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900979/* Lock for protecting policy. */
980extern struct mutex tomoyo_policy_lock;
981
982/* Has /sbin/init started? */
983extern bool tomoyo_policy_loaded;
984
985/* The kernel's domain. */
986extern struct tomoyo_domain_info tomoyo_kernel_domain;
987
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900988extern unsigned int tomoyo_quota_for_query;
989extern unsigned int tomoyo_query_memory_size;
990
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900991/********** Inlined functions. **********/
992
993static inline int tomoyo_read_lock(void)
994{
995 return srcu_read_lock(&tomoyo_ss);
996}
997
998static inline void tomoyo_read_unlock(int idx)
999{
1000 srcu_read_unlock(&tomoyo_ss, idx);
1001}
1002
Kentaro Takeda95908372009-02-05 17:18:13 +09001003/* strcmp() for "struct tomoyo_path_info" structure. */
1004static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
1005 const struct tomoyo_path_info *b)
1006{
1007 return a->hash != b->hash || strcmp(a->name, b->name);
1008}
1009
Kentaro Takeda95908372009-02-05 17:18:13 +09001010/**
1011 * tomoyo_is_valid - Check whether the character is a valid char.
1012 *
1013 * @c: The character to check.
1014 *
1015 * Returns true if @c is a valid character, false otherwise.
1016 */
1017static inline bool tomoyo_is_valid(const unsigned char c)
1018{
1019 return c > ' ' && c < 127;
1020}
1021
1022/**
1023 * tomoyo_is_invalid - Check whether the character is an invalid char.
1024 *
1025 * @c: The character to check.
1026 *
1027 * Returns true if @c is an invalid character, false otherwise.
1028 */
1029static inline bool tomoyo_is_invalid(const unsigned char c)
1030{
1031 return c && (c <= ' ' || c >= 127);
1032}
1033
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001034static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
1035{
1036 if (name) {
1037 struct tomoyo_name_entry *ptr =
1038 container_of(name, struct tomoyo_name_entry, entry);
1039 atomic_dec(&ptr->users);
1040 }
1041}
Kentaro Takeda95908372009-02-05 17:18:13 +09001042
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001043static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
1044{
1045 if (group)
1046 atomic_dec(&group->users);
1047}
1048
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001049static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
1050{
1051 if (group)
1052 atomic_dec(&group->users);
1053}
1054
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001055static inline struct tomoyo_domain_info *tomoyo_domain(void)
1056{
1057 return current_cred()->security;
1058}
Kentaro Takeda95908372009-02-05 17:18:13 +09001059
Tetsuo Handa76bb0892010-02-11 09:42:40 +09001060static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
1061 *task)
1062{
1063 return task_cred_xxx(task, security);
1064}
Kentaro Takeda95908372009-02-05 17:18:13 +09001065
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001066static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
1067 const struct tomoyo_acl_info *p2)
1068{
1069 return p1->type == p2->type;
1070}
1071
1072static inline bool tomoyo_is_same_name_union
1073(const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
1074{
1075 return p1->filename == p2->filename && p1->group == p2->group &&
1076 p1->is_group == p2->is_group;
1077}
1078
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +09001079static inline bool tomoyo_is_same_number_union
1080(const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
1081{
1082 return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
1083 && p1->group == p2->group && p1->min_type == p2->min_type &&
1084 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
1085}
1086
Kentaro Takeda95908372009-02-05 17:18:13 +09001087/**
1088 * list_for_each_cookie - iterate over a list with cookie.
1089 * @pos: the &struct list_head to use as a loop cursor.
1090 * @cookie: the &struct list_head to use as a cookie.
1091 * @head: the head for your list.
1092 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001093 * Same with list_for_each_rcu() except that this primitive uses @cookie
Kentaro Takeda95908372009-02-05 17:18:13 +09001094 * so that we can continue iteration.
1095 * @cookie must be NULL when iteration starts, and @cookie will become
1096 * NULL when iteration finishes.
1097 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001098#define list_for_each_cookie(pos, cookie, head) \
1099 for (({ if (!cookie) \
1100 cookie = head; }), \
1101 pos = rcu_dereference((cookie)->next); \
1102 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
1103 (cookie) = pos, pos = rcu_dereference(pos->next))
1104
Kentaro Takeda95908372009-02-05 17:18:13 +09001105#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */