blob: 7984a0ed548b483177d8a08d19411f669c9d7705 [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 Handa32997142011-06-26 23:19:28 +090041/* Group number is an integer between 0 and 255. */
42#define TOMOYO_MAX_ACL_GROUPS 256
43
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090044/* Index numbers for operation mode. */
Tetsuo Handacb0abe62010-05-17 10:08:05 +090045enum tomoyo_mode_index {
46 TOMOYO_CONFIG_DISABLED,
47 TOMOYO_CONFIG_LEARNING,
48 TOMOYO_CONFIG_PERMISSIVE,
Tetsuo Handa57c25902010-06-03 20:38:44 +090049 TOMOYO_CONFIG_ENFORCING,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090050 TOMOYO_CONFIG_MAX_MODE,
51 TOMOYO_CONFIG_WANT_REJECT_LOG = 64,
52 TOMOYO_CONFIG_WANT_GRANT_LOG = 128,
53 TOMOYO_CONFIG_USE_DEFAULT = 255,
Tetsuo Handacb0abe62010-05-17 10:08:05 +090054};
55
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090056/* Index numbers for entry type. */
Tetsuo Handaa230f9e2010-06-17 16:53:24 +090057enum tomoyo_policy_id {
58 TOMOYO_ID_GROUP,
59 TOMOYO_ID_PATH_GROUP,
60 TOMOYO_ID_NUMBER_GROUP,
Tetsuo Handa5448ec42010-06-21 11:14:39 +090061 TOMOYO_ID_TRANSITION_CONTROL,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +090062 TOMOYO_ID_AGGREGATOR,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +090063 TOMOYO_ID_MANAGER,
64 TOMOYO_ID_NAME,
65 TOMOYO_ID_ACL,
66 TOMOYO_ID_DOMAIN,
67 TOMOYO_MAX_POLICY
68};
69
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090070/* Index numbers for domain's attributes. */
71enum tomoyo_domain_info_flags_index {
72 /* Quota warnning flag. */
73 TOMOYO_DIF_QUOTA_WARNED,
74 /*
75 * This domain was unable to create a new domain at
76 * tomoyo_find_next_domain() because the name of the domain to be
77 * created was too long or it could not allocate memory.
78 * More than one process continued execve() without domain transition.
79 */
80 TOMOYO_DIF_TRANSITION_FAILED,
81 TOMOYO_MAX_DOMAIN_INFO_FLAGS
82};
83
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090084/* Index numbers for group entries. */
Tetsuo Handaa230f9e2010-06-17 16:53:24 +090085enum tomoyo_group_id {
86 TOMOYO_PATH_GROUP,
87 TOMOYO_NUMBER_GROUP,
88 TOMOYO_MAX_GROUP
89};
90
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090091/* Index numbers for type of numeric values. */
92enum tomoyo_value_type {
93 TOMOYO_VALUE_TYPE_INVALID,
94 TOMOYO_VALUE_TYPE_DECIMAL,
95 TOMOYO_VALUE_TYPE_OCTAL,
96 TOMOYO_VALUE_TYPE_HEXADECIMAL,
97};
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090098
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090099/* Index numbers for domain transition control keywords. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900100enum tomoyo_transition_type {
101 /* Do not change this order, */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900102 TOMOYO_TRANSITION_CONTROL_NO_RESET,
103 TOMOYO_TRANSITION_CONTROL_RESET,
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900104 TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
105 TOMOYO_TRANSITION_CONTROL_INITIALIZE,
106 TOMOYO_TRANSITION_CONTROL_NO_KEEP,
107 TOMOYO_TRANSITION_CONTROL_KEEP,
108 TOMOYO_MAX_TRANSITION_TYPE
109};
110
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900111/* Index numbers for Access Controls. */
Tetsuo Handa084da352010-02-15 15:10:39 +0900112enum tomoyo_acl_entry_type_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900113 TOMOYO_TYPE_PATH_ACL,
114 TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900115 TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handa75093152010-06-16 16:23:55 +0900116 TOMOYO_TYPE_MKDEV_ACL,
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900117 TOMOYO_TYPE_MOUNT_ACL,
Tetsuo Handa084da352010-02-15 15:10:39 +0900118};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900119
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900120/* Index numbers for access controls with one pathname. */
Tetsuo Handa084da352010-02-15 15:10:39 +0900121enum tomoyo_path_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900122 TOMOYO_TYPE_EXECUTE,
123 TOMOYO_TYPE_READ,
124 TOMOYO_TYPE_WRITE,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900125 TOMOYO_TYPE_APPEND,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900126 TOMOYO_TYPE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900127 TOMOYO_TYPE_GETATTR,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900128 TOMOYO_TYPE_RMDIR,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900129 TOMOYO_TYPE_TRUNCATE,
130 TOMOYO_TYPE_SYMLINK,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900131 TOMOYO_TYPE_CHROOT,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900132 TOMOYO_TYPE_UMOUNT,
133 TOMOYO_MAX_PATH_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900134};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900135
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900136/* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900137enum tomoyo_memory_stat_type {
138 TOMOYO_MEMORY_POLICY,
139 TOMOYO_MEMORY_AUDIT,
140 TOMOYO_MEMORY_QUERY,
141 TOMOYO_MAX_MEMORY_STAT
142};
143
Tetsuo Handa75093152010-06-16 16:23:55 +0900144enum tomoyo_mkdev_acl_index {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900145 TOMOYO_TYPE_MKBLOCK,
146 TOMOYO_TYPE_MKCHAR,
Tetsuo Handa75093152010-06-16 16:23:55 +0900147 TOMOYO_MAX_MKDEV_OPERATION
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900148};
149
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900150/* Index numbers for access controls with two pathnames. */
Tetsuo Handa084da352010-02-15 15:10:39 +0900151enum tomoyo_path2_acl_index {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900152 TOMOYO_TYPE_LINK,
153 TOMOYO_TYPE_RENAME,
154 TOMOYO_TYPE_PIVOT_ROOT,
155 TOMOYO_MAX_PATH2_OPERATION
Tetsuo Handa084da352010-02-15 15:10:39 +0900156};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900157
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900158/* Index numbers for access controls with one pathname and one number. */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900159enum tomoyo_path_number_acl_index {
160 TOMOYO_TYPE_CREATE,
161 TOMOYO_TYPE_MKDIR,
162 TOMOYO_TYPE_MKFIFO,
163 TOMOYO_TYPE_MKSOCK,
164 TOMOYO_TYPE_IOCTL,
165 TOMOYO_TYPE_CHMOD,
166 TOMOYO_TYPE_CHOWN,
167 TOMOYO_TYPE_CHGRP,
168 TOMOYO_MAX_PATH_NUMBER_OPERATION
169};
170
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900171/* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
Tetsuo Handa084da352010-02-15 15:10:39 +0900172enum tomoyo_securityfs_interface_index {
173 TOMOYO_DOMAINPOLICY,
174 TOMOYO_EXCEPTIONPOLICY,
175 TOMOYO_DOMAIN_STATUS,
176 TOMOYO_PROCESS_STATUS,
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900177 TOMOYO_STAT,
Tetsuo Handa084da352010-02-15 15:10:39 +0900178 TOMOYO_SELFDOMAIN,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900179 TOMOYO_AUDIT,
Tetsuo Handa084da352010-02-15 15:10:39 +0900180 TOMOYO_VERSION,
181 TOMOYO_PROFILE,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900182 TOMOYO_QUERY,
Tetsuo Handa084da352010-02-15 15:10:39 +0900183 TOMOYO_MANAGER
184};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900185
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900186/* Index numbers for special mount operations. */
187enum tomoyo_special_mount {
188 TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
189 TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
190 TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
191 TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
192 TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
193 TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
194 TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
195 TOMOYO_MAX_SPECIAL_MOUNT
196};
197
198/* Index numbers for functionality. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900199enum tomoyo_mac_index {
200 TOMOYO_MAC_FILE_EXECUTE,
201 TOMOYO_MAC_FILE_OPEN,
202 TOMOYO_MAC_FILE_CREATE,
203 TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900204 TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900205 TOMOYO_MAC_FILE_MKDIR,
206 TOMOYO_MAC_FILE_RMDIR,
207 TOMOYO_MAC_FILE_MKFIFO,
208 TOMOYO_MAC_FILE_MKSOCK,
209 TOMOYO_MAC_FILE_TRUNCATE,
210 TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900211 TOMOYO_MAC_FILE_MKBLOCK,
212 TOMOYO_MAC_FILE_MKCHAR,
213 TOMOYO_MAC_FILE_LINK,
214 TOMOYO_MAC_FILE_RENAME,
215 TOMOYO_MAC_FILE_CHMOD,
216 TOMOYO_MAC_FILE_CHOWN,
217 TOMOYO_MAC_FILE_CHGRP,
218 TOMOYO_MAC_FILE_IOCTL,
219 TOMOYO_MAC_FILE_CHROOT,
220 TOMOYO_MAC_FILE_MOUNT,
221 TOMOYO_MAC_FILE_UMOUNT,
222 TOMOYO_MAC_FILE_PIVOT_ROOT,
223 TOMOYO_MAX_MAC_INDEX
224};
225
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900226/* Index numbers for category of functionality. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900227enum tomoyo_mac_category_index {
228 TOMOYO_MAC_CATEGORY_FILE,
229 TOMOYO_MAX_MAC_CATEGORY_INDEX
230};
231
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900232/*
233 * Retry this request. Returned by tomoyo_supervisor() if policy violation has
234 * occurred in enforcing mode and the userspace daemon decided to retry.
235 *
236 * We must choose a positive value in order to distinguish "granted" (which is
237 * 0) and "rejected" (which is a negative value) and "retry".
238 */
239#define TOMOYO_RETRY_REQUEST 1
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900240
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900241/* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
242enum tomoyo_policy_stat_type {
243 /* Do not change this order. */
244 TOMOYO_STAT_POLICY_UPDATES,
245 TOMOYO_STAT_POLICY_LEARNING, /* == TOMOYO_CONFIG_LEARNING */
246 TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
247 TOMOYO_STAT_POLICY_ENFORCING, /* == TOMOYO_CONFIG_ENFORCING */
248 TOMOYO_MAX_POLICY_STAT
249};
250
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900251/* Index numbers for profile's PREFERENCE values. */
252enum tomoyo_pref_index {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900253 TOMOYO_PREF_MAX_AUDIT_LOG,
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900254 TOMOYO_PREF_MAX_LEARNING_ENTRY,
255 TOMOYO_MAX_PREF
256};
257
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900258/********** Structure definitions. **********/
Kentaro Takeda95908372009-02-05 17:18:13 +0900259
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900260/* Common header for holding ACL entries. */
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900261struct tomoyo_acl_head {
262 struct list_head list;
263 bool is_deleted;
264} __packed;
265
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900266/* Common header for shared entries. */
267struct tomoyo_shared_acl_head {
268 struct list_head list;
269 atomic_t users;
270} __packed;
271
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900272struct tomoyo_policy_namespace;
273
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900274/* Structure for request info. */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900275struct tomoyo_request_info {
276 struct tomoyo_domain_info *domain;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900277 /* For holding parameters. */
278 union {
279 struct {
280 const struct tomoyo_path_info *filename;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900281 /* For using wildcards at tomoyo_find_next_domain(). */
282 const struct tomoyo_path_info *matched_path;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900283 /* One of values in "enum tomoyo_path_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900284 u8 operation;
285 } path;
286 struct {
287 const struct tomoyo_path_info *filename1;
288 const struct tomoyo_path_info *filename2;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900289 /* One of values in "enum tomoyo_path2_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900290 u8 operation;
291 } path2;
292 struct {
293 const struct tomoyo_path_info *filename;
294 unsigned int mode;
295 unsigned int major;
296 unsigned int minor;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900297 /* One of values in "enum tomoyo_mkdev_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900298 u8 operation;
299 } mkdev;
300 struct {
301 const struct tomoyo_path_info *filename;
302 unsigned long number;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900303 /*
304 * One of values in
305 * "enum tomoyo_path_number_acl_index".
306 */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900307 u8 operation;
308 } path_number;
309 struct {
310 const struct tomoyo_path_info *type;
311 const struct tomoyo_path_info *dir;
312 const struct tomoyo_path_info *dev;
313 unsigned long flags;
314 int need_dev;
315 } mount;
316 } param;
317 u8 param_type;
318 bool granted;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900319 u8 retry;
320 u8 profile;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900321 u8 mode; /* One of tomoyo_mode_index . */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900322 u8 type;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900323};
324
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900325/* Structure for holding a token. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900326struct tomoyo_path_info {
327 const char *name;
328 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900329 u16 const_len; /* = tomoyo_const_part_length(name) */
330 bool is_dir; /* = tomoyo_strendswith(name, "/") */
331 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900332};
333
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900334/* Structure for holding string data. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900335struct tomoyo_name {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900336 struct tomoyo_shared_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900337 struct tomoyo_path_info entry;
338};
Kentaro Takeda95908372009-02-05 17:18:13 +0900339
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900340/* Structure for holding a word. */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900341struct tomoyo_name_union {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900342 /* Either @filename or @group is NULL. */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900343 const struct tomoyo_path_info *filename;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900344 struct tomoyo_group *group;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900345};
346
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900347/* Structure for holding a number. */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900348struct tomoyo_number_union {
349 unsigned long values[2];
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900350 struct tomoyo_group *group; /* Maybe NULL. */
351 /* One of values in "enum tomoyo_value_type". */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900352 u8 value_type[2];
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900353};
354
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900355/* Structure for "path_group"/"number_group" directive. */
356struct tomoyo_group {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900357 struct tomoyo_shared_acl_head head;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900358 const struct tomoyo_path_info *group_name;
359 struct list_head member_list;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900360};
361
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900362/* Structure for "path_group" directive. */
363struct tomoyo_path_group {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900364 struct tomoyo_acl_head head;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900365 const struct tomoyo_path_info *member_name;
366};
367
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900368/* Structure for "number_group" directive. */
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900369struct tomoyo_number_group {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900370 struct tomoyo_acl_head head;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900371 struct tomoyo_number_union number;
372};
373
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900374/* Common header for individual entries. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900375struct tomoyo_acl_info {
376 struct list_head list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900377 bool is_deleted;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900378 u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
Kentaro Takeda95908372009-02-05 17:18:13 +0900379} __packed;
380
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900381/* Structure for domain information. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900382struct tomoyo_domain_info {
383 struct list_head list;
384 struct list_head acl_info_list;
385 /* Name of this domain. Never NULL. */
386 const struct tomoyo_path_info *domainname;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900387 /* Namespace for this domain. Never NULL. */
388 struct tomoyo_policy_namespace *ns;
Kentaro Takeda95908372009-02-05 17:18:13 +0900389 u8 profile; /* Profile number to use. */
Tetsuo Handa32997142011-06-26 23:19:28 +0900390 u8 group; /* Group number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900391 bool is_deleted; /* Delete flag. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900392 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900393 atomic_t users; /* Number of referring credentials. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900394};
395
Kentaro Takeda95908372009-02-05 17:18:13 +0900396/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900397 * Structure for "file execute", "file read", "file write", "file append",
398 * "file unlink", "file getattr", "file rmdir", "file truncate",
399 * "file symlink", "file chroot" and "file unmount" directive.
Kentaro Takeda95908372009-02-05 17:18:13 +0900400 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900401struct tomoyo_path_acl {
402 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900403 u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900404 struct tomoyo_name_union name;
Kentaro Takeda95908372009-02-05 17:18:13 +0900405};
406
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900407/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900408 * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
409 * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900410 */
411struct tomoyo_path_number_acl {
412 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900413 /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900414 u8 perm;
415 struct tomoyo_name_union name;
416 struct tomoyo_number_union number;
417};
418
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900419/* Structure for "file mkblock" and "file mkchar" directive. */
Tetsuo Handa75093152010-06-16 16:23:55 +0900420struct tomoyo_mkdev_acl {
421 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900422 u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900423 struct tomoyo_name_union name;
424 struct tomoyo_number_union mode;
425 struct tomoyo_number_union major;
426 struct tomoyo_number_union minor;
427};
428
429/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900430 * Structure for "file rename", "file link" and "file pivot_root" directive.
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 */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900434 u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
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 Handab5bc60b2011-06-26 23:16:03 +0900439/* Structure for "file mount" directive. */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900440struct tomoyo_mount_acl {
441 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900442 struct tomoyo_name_union dev_name;
443 struct tomoyo_name_union dir_name;
444 struct tomoyo_name_union fs_type;
445 struct tomoyo_number_union flags;
446};
447
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900448/* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
449struct tomoyo_acl_param {
450 char *data;
451 struct list_head *list;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900452 struct tomoyo_policy_namespace *ns;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900453 bool is_delete;
454};
455
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900456#define TOMOYO_MAX_IO_READ_QUEUE 64
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900457
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900458/*
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900459 * Structure for reading/writing policy via /sys/kernel/security/tomoyo
460 * interfaces.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900461 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900462struct tomoyo_io_buffer {
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900463 void (*read) (struct tomoyo_io_buffer *);
Kentaro Takeda95908372009-02-05 17:18:13 +0900464 int (*write) (struct tomoyo_io_buffer *);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900465 int (*poll) (struct file *file, poll_table *wait);
Kentaro Takeda95908372009-02-05 17:18:13 +0900466 /* Exclusive lock for this structure. */
467 struct mutex io_sem;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900468 char __user *read_user_buf;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900469 size_t read_user_buf_avail;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900470 struct {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900471 struct list_head *ns;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900472 struct list_head *domain;
473 struct list_head *group;
474 struct list_head *acl;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900475 size_t avail;
476 unsigned int step;
477 unsigned int query_index;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900478 u16 index;
Tetsuo Handa32997142011-06-26 23:19:28 +0900479 u8 acl_group_index;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900480 u8 bit;
481 u8 w_pos;
482 bool eof;
483 bool print_this_domain_only;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900484 bool print_transition_related_only;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900485 const char *w[TOMOYO_MAX_IO_READ_QUEUE];
486 } r;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900487 struct {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900488 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900489 /* The position currently writing to. */
490 struct tomoyo_domain_info *domain;
491 /* Bytes available for writing. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900492 size_t avail;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900493 bool is_delete;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900494 } w;
Kentaro Takeda95908372009-02-05 17:18:13 +0900495 /* Buffer for reading. */
496 char *read_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900497 /* Size of read buffer. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900498 size_t readbuf_size;
Kentaro Takeda95908372009-02-05 17:18:13 +0900499 /* Buffer for writing. */
500 char *write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900501 /* Size of write buffer. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900502 size_t writebuf_size;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900503 /* Type of this interface. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900504 enum tomoyo_securityfs_interface_index type;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +0900505 /* Users counter protected by tomoyo_io_buffer_list_lock. */
506 u8 users;
507 /* List for telling GC not to kfree() elements. */
508 struct list_head list;
Kentaro Takeda95908372009-02-05 17:18:13 +0900509};
510
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900511/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900512 * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
513 * "no_keep_domain" keyword.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900514 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900515struct tomoyo_transition_control {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900516 struct tomoyo_acl_head head;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900517 u8 type; /* One of values in "enum tomoyo_transition_type". */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900518 /* True if the domainname is tomoyo_get_last_name(). */
519 bool is_last_name;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900520 const struct tomoyo_path_info *domainname; /* Maybe NULL */
521 const struct tomoyo_path_info *program; /* Maybe NULL */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900522};
523
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900524/* Structure for "aggregator" keyword. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900525struct tomoyo_aggregator {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900526 struct tomoyo_acl_head head;
Tetsuo Handa10843072010-06-03 20:38:03 +0900527 const struct tomoyo_path_info *original_name;
528 const struct tomoyo_path_info *aggregated_name;
Tetsuo Handa10843072010-06-03 20:38:03 +0900529};
530
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900531/* Structure for policy manager. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900532struct tomoyo_manager {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900533 struct tomoyo_acl_head head;
534 bool is_domain; /* True if manager is a domainname. */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900535 /* A path to program or a domainname. */
536 const struct tomoyo_path_info *manager;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900537};
538
Tetsuo Handa57c25902010-06-03 20:38:44 +0900539struct tomoyo_preference {
540 unsigned int learning_max_entry;
541 bool enforcing_verbose;
542 bool learning_verbose;
543 bool permissive_verbose;
544};
545
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900546/* Structure for /sys/kernel/security/tomnoyo/profile interface. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900547struct tomoyo_profile {
548 const struct tomoyo_path_info *comment;
549 struct tomoyo_preference *learning;
550 struct tomoyo_preference *permissive;
551 struct tomoyo_preference *enforcing;
552 struct tomoyo_preference preference;
553 u8 default_config;
554 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900555 unsigned int pref[TOMOYO_MAX_PREF];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900556};
557
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900558/* Structure for representing YYYY/MM/DD hh/mm/ss. */
559struct tomoyo_time {
560 u16 year;
561 u8 month;
562 u8 day;
563 u8 hour;
564 u8 min;
565 u8 sec;
566};
567
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900568/* Structure for policy namespace. */
569struct tomoyo_policy_namespace {
570 /* Profile table. Memory is allocated as needed. */
571 struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
572 /* List of "struct tomoyo_group". */
573 struct list_head group_list[TOMOYO_MAX_GROUP];
574 /* List of policy. */
575 struct list_head policy_list[TOMOYO_MAX_POLICY];
576 /* The global ACL referred by "use_group" keyword. */
577 struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
578 /* List for connecting to tomoyo_namespace_list list. */
579 struct list_head namespace_list;
580 /* Profile version. Currently only 20100903 is defined. */
581 unsigned int profile_version;
582 /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
583 const char *name;
584};
585
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900586/********** Function prototypes. **********/
587
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900588void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900589bool tomoyo_str_starts(char **src, const char *find);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900590const char *tomoyo_get_exe(void);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900591void tomoyo_normalize_line(unsigned char *buffer);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900592void tomoyo_check_profile(void);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900593int tomoyo_open_control(const u8 type, struct file *file);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900594int tomoyo_close_control(struct tomoyo_io_buffer *head);
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900595int tomoyo_poll_control(struct file *file, poll_table *wait);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900596ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
597 const int buffer_len);
598ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
599 const char __user *buffer, const int buffer_len);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900600bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900601void tomoyo_warn_oom(const char *function);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900602const struct tomoyo_path_info *
603tomoyo_compare_name_union(const struct tomoyo_path_info *name,
604 const struct tomoyo_name_union *ptr);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900605bool tomoyo_compare_number_union(const unsigned long value,
606 const struct tomoyo_number_union *ptr);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900607int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
608 const u8 index);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900609void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
Kentaro Takeda95908372009-02-05 17:18:13 +0900610 __attribute__ ((format(printf, 2, 3)));
Tetsuo Handa75093152010-06-16 16:23:55 +0900611bool tomoyo_correct_domain(const unsigned char *domainname);
Tetsuo Handa75093152010-06-16 16:23:55 +0900612bool tomoyo_correct_path(const char *filename);
613bool tomoyo_correct_word(const char *string);
Tetsuo Handa75093152010-06-16 16:23:55 +0900614bool tomoyo_domain_def(const unsigned char *buffer);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900615bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900616 struct tomoyo_name_union *ptr);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900617const struct tomoyo_path_info *
618tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
619 const struct tomoyo_group *group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900620bool tomoyo_number_matches_group(const unsigned long min,
621 const unsigned long max,
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900622 const struct tomoyo_group *group);
Kentaro Takeda95908372009-02-05 17:18:13 +0900623bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
624 const struct tomoyo_path_info *pattern);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900625bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
626 struct tomoyo_number_union *ptr);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900627bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
Kentaro Takeda95908372009-02-05 17:18:13 +0900628bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900629int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900630 struct tomoyo_domain_info *domain,
631 const u8 index);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900632int tomoyo_mount_permission(char *dev_name, struct path *path,
633 const char *type, unsigned long flags,
634 void *data_page);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900635int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
636int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900637 const u8 type);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900638int tomoyo_write_file(struct tomoyo_acl_param *param);
639int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900640int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
641 __attribute__ ((format(printf, 2, 3)));
Kentaro Takeda95908372009-02-05 17:18:13 +0900642struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900643struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900644 const bool transit);
645struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
646 const u8 profile);
647struct tomoyo_policy_namespace *tomoyo_assign_namespace
648(const char *domainname);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900649struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
650 const u8 idx);
Kentaro Takeda95908372009-02-05 17:18:13 +0900651unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
652 const u8 index);
Kentaro Takeda95908372009-02-05 17:18:13 +0900653void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
Kentaro Takeda95908372009-02-05 17:18:13 +0900654void tomoyo_load_policy(const char *filename);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900655void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900656char *tomoyo_encode(const char *str);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900657char *tomoyo_realpath_nofollow(const char *pathname);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900658char *tomoyo_realpath_from_path(struct path *path);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900659bool tomoyo_memory_ok(void *ptr);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900660void *tomoyo_commit_ok(void *data, const unsigned int size);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900661const struct tomoyo_path_info *tomoyo_get_name(const char *name);
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900662void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp);
663void tomoyo_update_stat(const u8 index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900664void __init tomoyo_mm_init(void);
Tetsuo Handa05336de2010-06-16 16:20:24 +0900665int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900666 const struct tomoyo_path_info *filename);
667int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
668 struct path *path, const int flag);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900669int tomoyo_path_number_perm(const u8 operation, struct path *path,
670 unsigned long number);
Tetsuo Handa75093152010-06-16 16:23:55 +0900671int tomoyo_mkdev_perm(const u8 operation, struct path *path,
672 const unsigned int mode, unsigned int dev);
Tetsuo Handa97d69312010-02-16 09:46:15 +0900673int tomoyo_path_perm(const u8 operation, struct path *path);
674int tomoyo_path2_perm(const u8 operation, struct path *path1,
675 struct path *path2);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900676int tomoyo_find_next_domain(struct linux_binprm *bprm);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900677void tomoyo_print_ulong(char *buffer, const int buffer_len,
678 const unsigned long value, const u8 type);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900679void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
Tetsuo Handa2e503bb2011-06-26 23:20:55 +0900680void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900681void tomoyo_memory_free(void *ptr);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900682int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900683 struct tomoyo_acl_param *param,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900684 bool (*check_duplicate) (const struct tomoyo_acl_info
685 *,
686 const struct tomoyo_acl_info
687 *),
688 bool (*merge_duplicate) (struct tomoyo_acl_info *,
689 struct tomoyo_acl_info *,
690 const bool));
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900691int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900692 struct tomoyo_acl_param *param,
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900693 bool (*check_duplicate) (const struct tomoyo_acl_head
694 *,
695 const struct tomoyo_acl_head
696 *));
Tetsuo Handa99a85252010-06-16 16:22:51 +0900697void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900698 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900699 const struct tomoyo_acl_info *));
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900700char *tomoyo_read_token(struct tomoyo_acl_param *param);
701bool tomoyo_permstr(const char *string, const char *keyword);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900702
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900703const char *tomoyo_yesno(const unsigned int value);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900704void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
705 __attribute__ ((format(printf, 2, 3)));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900706void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
707 va_list args);
708void tomoyo_read_log(struct tomoyo_io_buffer *head);
709int tomoyo_poll_log(struct file *file, poll_table *wait);
710char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
711 va_list args);
712
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900713/********** External variable definitions. **********/
714
715/* Lock for GC. */
716extern struct srcu_struct tomoyo_ss;
717
718/* The list for "struct tomoyo_domain_info". */
719extern struct list_head tomoyo_domain_list;
720
Tetsuo Handa847b1732010-02-11 09:43:54 +0900721extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
Tetsuo Handa847b1732010-02-11 09:43:54 +0900722
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900723/* Lock for protecting policy. */
724extern struct mutex tomoyo_policy_lock;
725
726/* Has /sbin/init started? */
727extern bool tomoyo_policy_loaded;
728
729/* The kernel's domain. */
730extern struct tomoyo_domain_info tomoyo_kernel_domain;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900731extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
732extern struct list_head tomoyo_namespace_list;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900733
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900734extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX +
735 TOMOYO_MAX_MAC_CATEGORY_INDEX];
736extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
737extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
738
Tetsuo Handa71c28232010-06-16 16:26:38 +0900739
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900740extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
741extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
742extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
743
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900744extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900745extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
746extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
747extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900748
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900749/********** Inlined functions. **********/
750
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900751/**
752 * tomoyo_read_lock - Take lock for protecting policy.
753 *
754 * Returns index number for tomoyo_read_unlock().
755 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900756static inline int tomoyo_read_lock(void)
757{
758 return srcu_read_lock(&tomoyo_ss);
759}
760
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900761/**
762 * tomoyo_read_unlock - Release lock for protecting policy.
763 *
764 * @idx: Index number returned by tomoyo_read_lock().
765 *
766 * Returns nothing.
767 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900768static inline void tomoyo_read_unlock(int idx)
769{
770 srcu_read_unlock(&tomoyo_ss, idx);
771}
772
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900773/**
774 * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
775 *
776 * @a: Pointer to "struct tomoyo_path_info".
777 * @b: Pointer to "struct tomoyo_path_info".
778 *
779 * Returns true if @a == @b, false otherwise.
780 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900781static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
782 const struct tomoyo_path_info *b)
783{
784 return a->hash != b->hash || strcmp(a->name, b->name);
785}
786
Kentaro Takeda95908372009-02-05 17:18:13 +0900787/**
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900788 * tomoyo_put_name - Drop reference on "struct tomoyo_name".
789 *
790 * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
791 *
792 * Returns nothing.
793 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900794static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
795{
796 if (name) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900797 struct tomoyo_name *ptr =
798 container_of(name, typeof(*ptr), entry);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900799 atomic_dec(&ptr->head.users);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900800 }
801}
Kentaro Takeda95908372009-02-05 17:18:13 +0900802
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900803/**
804 * tomoyo_put_group - Drop reference on "struct tomoyo_group".
805 *
806 * @group: Pointer to "struct tomoyo_group". Maybe NULL.
807 *
808 * Returns nothing.
809 */
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900810static inline void tomoyo_put_group(struct tomoyo_group *group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900811{
812 if (group)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900813 atomic_dec(&group->head.users);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900814}
815
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900816/**
817 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
818 *
819 * Returns pointer to "struct tomoyo_domain_info" for current thread.
820 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900821static inline struct tomoyo_domain_info *tomoyo_domain(void)
822{
823 return current_cred()->security;
824}
Kentaro Takeda95908372009-02-05 17:18:13 +0900825
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900826/**
827 * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
828 *
829 * @task: Pointer to "struct task_struct".
830 *
831 * Returns pointer to "struct tomoyo_security" for specified thread.
832 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900833static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
834 *task)
835{
836 return task_cred_xxx(task, security);
837}
Kentaro Takeda95908372009-02-05 17:18:13 +0900838
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900839/**
840 * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
841 *
842 * @a: Pointer to "struct tomoyo_name_union".
843 * @b: Pointer to "struct tomoyo_name_union".
844 *
845 * Returns true if @a == @b, false otherwise.
846 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900847static inline bool tomoyo_same_name_union
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900848(const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900849{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900850 return a->filename == b->filename && a->group == b->group;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900851}
852
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900853/**
854 * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
855 *
856 * @a: Pointer to "struct tomoyo_number_union".
857 * @b: Pointer to "struct tomoyo_number_union".
858 *
859 * Returns true if @a == @b, false otherwise.
860 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900861static inline bool tomoyo_same_number_union
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900862(const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900863{
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900864 return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900865 a->group == b->group && a->value_type[0] == b->value_type[0] &&
866 a->value_type[1] == b->value_type[1];
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900867}
868
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900869/**
870 * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
871 *
872 * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
873 */
874static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
875{
876 return tomoyo_domain()->ns;
877}
878
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900879#if defined(CONFIG_SLOB)
880
881/**
882 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
883 *
884 * @size: Size to be rounded up.
885 *
886 * Returns @size.
887 *
888 * Since SLOB does not round up, this function simply returns @size.
889 */
890static inline int tomoyo_round2(size_t size)
891{
892 return size;
893}
894
895#else
896
897/**
898 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
899 *
900 * @size: Size to be rounded up.
901 *
902 * Returns rounded size.
903 *
904 * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
905 * (e.g.) 128 bytes.
906 */
907static inline int tomoyo_round2(size_t size)
908{
909#if PAGE_SIZE == 4096
910 size_t bsize = 32;
911#else
912 size_t bsize = 64;
913#endif
914 if (!size)
915 return 0;
916 while (size > bsize)
917 bsize <<= 1;
918 return bsize;
919}
920
921#endif
922
Kentaro Takeda95908372009-02-05 17:18:13 +0900923/**
924 * list_for_each_cookie - iterate over a list with cookie.
925 * @pos: the &struct list_head to use as a loop cursor.
Kentaro Takeda95908372009-02-05 17:18:13 +0900926 * @head: the head for your list.
Kentaro Takeda95908372009-02-05 17:18:13 +0900927 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900928#define list_for_each_cookie(pos, head) \
929 if (!pos) \
930 pos = srcu_dereference((head)->next, &tomoyo_ss); \
931 for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900932
Kentaro Takeda95908372009-02-05 17:18:13 +0900933#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */