blob: beb7d0eb5222d30dc836fc630c83d0fe373381cc [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,
Tetsuo Handa084da352010-02-15 15:10:39 +0900175 TOMOYO_PROCESS_STATUS,
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900176 TOMOYO_STAT,
Tetsuo Handa084da352010-02-15 15:10:39 +0900177 TOMOYO_SELFDOMAIN,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900178 TOMOYO_AUDIT,
Tetsuo Handa084da352010-02-15 15:10:39 +0900179 TOMOYO_VERSION,
180 TOMOYO_PROFILE,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900181 TOMOYO_QUERY,
Tetsuo Handa084da352010-02-15 15:10:39 +0900182 TOMOYO_MANAGER
183};
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900184
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900185/* Index numbers for special mount operations. */
186enum tomoyo_special_mount {
187 TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
188 TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
189 TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
190 TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
191 TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
192 TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
193 TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
194 TOMOYO_MAX_SPECIAL_MOUNT
195};
196
197/* Index numbers for functionality. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900198enum tomoyo_mac_index {
199 TOMOYO_MAC_FILE_EXECUTE,
200 TOMOYO_MAC_FILE_OPEN,
201 TOMOYO_MAC_FILE_CREATE,
202 TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900203 TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900204 TOMOYO_MAC_FILE_MKDIR,
205 TOMOYO_MAC_FILE_RMDIR,
206 TOMOYO_MAC_FILE_MKFIFO,
207 TOMOYO_MAC_FILE_MKSOCK,
208 TOMOYO_MAC_FILE_TRUNCATE,
209 TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900210 TOMOYO_MAC_FILE_MKBLOCK,
211 TOMOYO_MAC_FILE_MKCHAR,
212 TOMOYO_MAC_FILE_LINK,
213 TOMOYO_MAC_FILE_RENAME,
214 TOMOYO_MAC_FILE_CHMOD,
215 TOMOYO_MAC_FILE_CHOWN,
216 TOMOYO_MAC_FILE_CHGRP,
217 TOMOYO_MAC_FILE_IOCTL,
218 TOMOYO_MAC_FILE_CHROOT,
219 TOMOYO_MAC_FILE_MOUNT,
220 TOMOYO_MAC_FILE_UMOUNT,
221 TOMOYO_MAC_FILE_PIVOT_ROOT,
222 TOMOYO_MAX_MAC_INDEX
223};
224
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900225/* Index numbers for category of functionality. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900226enum tomoyo_mac_category_index {
227 TOMOYO_MAC_CATEGORY_FILE,
228 TOMOYO_MAX_MAC_CATEGORY_INDEX
229};
230
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900231/*
232 * Retry this request. Returned by tomoyo_supervisor() if policy violation has
233 * occurred in enforcing mode and the userspace daemon decided to retry.
234 *
235 * We must choose a positive value in order to distinguish "granted" (which is
236 * 0) and "rejected" (which is a negative value) and "retry".
237 */
238#define TOMOYO_RETRY_REQUEST 1
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900239
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900240/* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
241enum tomoyo_policy_stat_type {
242 /* Do not change this order. */
243 TOMOYO_STAT_POLICY_UPDATES,
244 TOMOYO_STAT_POLICY_LEARNING, /* == TOMOYO_CONFIG_LEARNING */
245 TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
246 TOMOYO_STAT_POLICY_ENFORCING, /* == TOMOYO_CONFIG_ENFORCING */
247 TOMOYO_MAX_POLICY_STAT
248};
249
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900250/* Index numbers for profile's PREFERENCE values. */
251enum tomoyo_pref_index {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900252 TOMOYO_PREF_MAX_AUDIT_LOG,
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900253 TOMOYO_PREF_MAX_LEARNING_ENTRY,
254 TOMOYO_MAX_PREF
255};
256
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900257/********** Structure definitions. **********/
Kentaro Takeda95908372009-02-05 17:18:13 +0900258
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900259/* Common header for holding ACL entries. */
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900260struct tomoyo_acl_head {
261 struct list_head list;
262 bool is_deleted;
263} __packed;
264
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900265/* Common header for shared entries. */
266struct tomoyo_shared_acl_head {
267 struct list_head list;
268 atomic_t users;
269} __packed;
270
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900271struct tomoyo_policy_namespace;
272
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900273/* Structure for request info. */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900274struct tomoyo_request_info {
275 struct tomoyo_domain_info *domain;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900276 /* For holding parameters. */
277 union {
278 struct {
279 const struct tomoyo_path_info *filename;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900280 /* For using wildcards at tomoyo_find_next_domain(). */
281 const struct tomoyo_path_info *matched_path;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900282 /* One of values in "enum tomoyo_path_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900283 u8 operation;
284 } path;
285 struct {
286 const struct tomoyo_path_info *filename1;
287 const struct tomoyo_path_info *filename2;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900288 /* One of values in "enum tomoyo_path2_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900289 u8 operation;
290 } path2;
291 struct {
292 const struct tomoyo_path_info *filename;
293 unsigned int mode;
294 unsigned int major;
295 unsigned int minor;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900296 /* One of values in "enum tomoyo_mkdev_acl_index". */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900297 u8 operation;
298 } mkdev;
299 struct {
300 const struct tomoyo_path_info *filename;
301 unsigned long number;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900302 /*
303 * One of values in
304 * "enum tomoyo_path_number_acl_index".
305 */
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900306 u8 operation;
307 } path_number;
308 struct {
309 const struct tomoyo_path_info *type;
310 const struct tomoyo_path_info *dir;
311 const struct tomoyo_path_info *dev;
312 unsigned long flags;
313 int need_dev;
314 } mount;
315 } param;
316 u8 param_type;
317 bool granted;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900318 u8 retry;
319 u8 profile;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900320 u8 mode; /* One of tomoyo_mode_index . */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900321 u8 type;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900322};
323
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900324/* Structure for holding a token. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900325struct tomoyo_path_info {
326 const char *name;
327 u32 hash; /* = full_name_hash(name, strlen(name)) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900328 u16 const_len; /* = tomoyo_const_part_length(name) */
329 bool is_dir; /* = tomoyo_strendswith(name, "/") */
330 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
Kentaro Takeda95908372009-02-05 17:18:13 +0900331};
332
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900333/* Structure for holding string data. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900334struct tomoyo_name {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900335 struct tomoyo_shared_acl_head head;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900336 struct tomoyo_path_info entry;
337};
Kentaro Takeda95908372009-02-05 17:18:13 +0900338
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900339/* Structure for holding a word. */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900340struct tomoyo_name_union {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900341 /* Either @filename or @group is NULL. */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900342 const struct tomoyo_path_info *filename;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900343 struct tomoyo_group *group;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900344};
345
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900346/* Structure for holding a number. */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900347struct tomoyo_number_union {
348 unsigned long values[2];
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900349 struct tomoyo_group *group; /* Maybe NULL. */
350 /* One of values in "enum tomoyo_value_type". */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900351 u8 value_type[2];
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900352};
353
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900354/* Structure for "path_group"/"number_group" directive. */
355struct tomoyo_group {
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900356 struct tomoyo_shared_acl_head head;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900357 const struct tomoyo_path_info *group_name;
358 struct list_head member_list;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900359};
360
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900361/* Structure for "path_group" directive. */
362struct tomoyo_path_group {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900363 struct tomoyo_acl_head head;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900364 const struct tomoyo_path_info *member_name;
365};
366
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900367/* Structure for "number_group" directive. */
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900368struct tomoyo_number_group {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900369 struct tomoyo_acl_head head;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900370 struct tomoyo_number_union number;
371};
372
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900373/* Common header for individual entries. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900374struct tomoyo_acl_info {
375 struct list_head list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900376 bool is_deleted;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900377 u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
Kentaro Takeda95908372009-02-05 17:18:13 +0900378} __packed;
379
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900380/* Structure for domain information. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900381struct tomoyo_domain_info {
382 struct list_head list;
383 struct list_head acl_info_list;
384 /* Name of this domain. Never NULL. */
385 const struct tomoyo_path_info *domainname;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900386 /* Namespace for this domain. Never NULL. */
387 struct tomoyo_policy_namespace *ns;
Kentaro Takeda95908372009-02-05 17:18:13 +0900388 u8 profile; /* Profile number to use. */
Tetsuo Handa32997142011-06-26 23:19:28 +0900389 u8 group; /* Group number to use. */
Tetsuo Handaa0558fc2009-04-06 20:49:14 +0900390 bool is_deleted; /* Delete flag. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900391 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900392 atomic_t users; /* Number of referring credentials. */
Kentaro Takeda95908372009-02-05 17:18:13 +0900393};
394
Kentaro Takeda95908372009-02-05 17:18:13 +0900395/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900396 * Structure for "file execute", "file read", "file write", "file append",
397 * "file unlink", "file getattr", "file rmdir", "file truncate",
398 * "file symlink", "file chroot" and "file unmount" directive.
Kentaro Takeda95908372009-02-05 17:18:13 +0900399 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900400struct tomoyo_path_acl {
401 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900402 u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900403 struct tomoyo_name_union name;
Kentaro Takeda95908372009-02-05 17:18:13 +0900404};
405
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900406/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900407 * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
408 * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900409 */
410struct tomoyo_path_number_acl {
411 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900412 /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900413 u8 perm;
414 struct tomoyo_name_union name;
415 struct tomoyo_number_union number;
416};
417
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900418/* Structure for "file mkblock" and "file mkchar" directive. */
Tetsuo Handa75093152010-06-16 16:23:55 +0900419struct tomoyo_mkdev_acl {
420 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900421 u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900422 struct tomoyo_name_union name;
423 struct tomoyo_number_union mode;
424 struct tomoyo_number_union major;
425 struct tomoyo_number_union minor;
426};
427
428/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900429 * Structure for "file rename", "file link" and "file pivot_root" directive.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900430 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900431struct tomoyo_path2_acl {
432 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900433 u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900434 struct tomoyo_name_union name1;
435 struct tomoyo_name_union name2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900436};
437
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900438/* Structure for "file mount" directive. */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900439struct tomoyo_mount_acl {
440 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900441 struct tomoyo_name_union dev_name;
442 struct tomoyo_name_union dir_name;
443 struct tomoyo_name_union fs_type;
444 struct tomoyo_number_union flags;
445};
446
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900447/* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
448struct tomoyo_acl_param {
449 char *data;
450 struct list_head *list;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900451 struct tomoyo_policy_namespace *ns;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900452 bool is_delete;
453};
454
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900455#define TOMOYO_MAX_IO_READ_QUEUE 64
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900456
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900457/*
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900458 * Structure for reading/writing policy via /sys/kernel/security/tomoyo
459 * interfaces.
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900460 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900461struct tomoyo_io_buffer {
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900462 void (*read) (struct tomoyo_io_buffer *);
Kentaro Takeda95908372009-02-05 17:18:13 +0900463 int (*write) (struct tomoyo_io_buffer *);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900464 int (*poll) (struct file *file, poll_table *wait);
Kentaro Takeda95908372009-02-05 17:18:13 +0900465 /* Exclusive lock for this structure. */
466 struct mutex io_sem;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900467 char __user *read_user_buf;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900468 size_t read_user_buf_avail;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900469 struct {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900470 struct list_head *ns;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900471 struct list_head *domain;
472 struct list_head *group;
473 struct list_head *acl;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900474 size_t avail;
475 unsigned int step;
476 unsigned int query_index;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900477 u16 index;
Tetsuo Handa32997142011-06-26 23:19:28 +0900478 u8 acl_group_index;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900479 u8 bit;
480 u8 w_pos;
481 bool eof;
482 bool print_this_domain_only;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900483 bool print_transition_related_only;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900484 const char *w[TOMOYO_MAX_IO_READ_QUEUE];
485 } r;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900486 struct {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900487 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900488 /* The position currently writing to. */
489 struct tomoyo_domain_info *domain;
490 /* Bytes available for writing. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900491 size_t avail;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900492 bool is_delete;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900493 } w;
Kentaro Takeda95908372009-02-05 17:18:13 +0900494 /* Buffer for reading. */
495 char *read_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900496 /* Size of read buffer. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900497 size_t readbuf_size;
Kentaro Takeda95908372009-02-05 17:18:13 +0900498 /* Buffer for writing. */
499 char *write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900500 /* Size of write buffer. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900501 size_t writebuf_size;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900502 /* Type of this interface. */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900503 enum tomoyo_securityfs_interface_index type;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +0900504 /* Users counter protected by tomoyo_io_buffer_list_lock. */
505 u8 users;
506 /* List for telling GC not to kfree() elements. */
507 struct list_head list;
Kentaro Takeda95908372009-02-05 17:18:13 +0900508};
509
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900510/*
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900511 * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
512 * "no_keep_domain" keyword.
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900513 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900514struct tomoyo_transition_control {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900515 struct tomoyo_acl_head head;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900516 u8 type; /* One of values in "enum tomoyo_transition_type". */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900517 /* True if the domainname is tomoyo_get_last_name(). */
518 bool is_last_name;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900519 const struct tomoyo_path_info *domainname; /* Maybe NULL */
520 const struct tomoyo_path_info *program; /* Maybe NULL */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900521};
522
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900523/* Structure for "aggregator" keyword. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900524struct tomoyo_aggregator {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900525 struct tomoyo_acl_head head;
Tetsuo Handa10843072010-06-03 20:38:03 +0900526 const struct tomoyo_path_info *original_name;
527 const struct tomoyo_path_info *aggregated_name;
Tetsuo Handa10843072010-06-03 20:38:03 +0900528};
529
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900530/* Structure for policy manager. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900531struct tomoyo_manager {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900532 struct tomoyo_acl_head head;
533 bool is_domain; /* True if manager is a domainname. */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900534 /* A path to program or a domainname. */
535 const struct tomoyo_path_info *manager;
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900536};
537
Tetsuo Handa57c25902010-06-03 20:38:44 +0900538struct tomoyo_preference {
539 unsigned int learning_max_entry;
540 bool enforcing_verbose;
541 bool learning_verbose;
542 bool permissive_verbose;
543};
544
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900545/* Structure for /sys/kernel/security/tomnoyo/profile interface. */
Tetsuo Handa57c25902010-06-03 20:38:44 +0900546struct tomoyo_profile {
547 const struct tomoyo_path_info *comment;
548 struct tomoyo_preference *learning;
549 struct tomoyo_preference *permissive;
550 struct tomoyo_preference *enforcing;
551 struct tomoyo_preference preference;
552 u8 default_config;
553 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900554 unsigned int pref[TOMOYO_MAX_PREF];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900555};
556
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900557/* Structure for representing YYYY/MM/DD hh/mm/ss. */
558struct tomoyo_time {
559 u16 year;
560 u8 month;
561 u8 day;
562 u8 hour;
563 u8 min;
564 u8 sec;
565};
566
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900567/* Structure for policy namespace. */
568struct tomoyo_policy_namespace {
569 /* Profile table. Memory is allocated as needed. */
570 struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
571 /* List of "struct tomoyo_group". */
572 struct list_head group_list[TOMOYO_MAX_GROUP];
573 /* List of policy. */
574 struct list_head policy_list[TOMOYO_MAX_POLICY];
575 /* The global ACL referred by "use_group" keyword. */
576 struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
577 /* List for connecting to tomoyo_namespace_list list. */
578 struct list_head namespace_list;
579 /* Profile version. Currently only 20100903 is defined. */
580 unsigned int profile_version;
581 /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
582 const char *name;
583};
584
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900585/********** Function prototypes. **********/
586
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900587bool tomoyo_compare_number_union(const unsigned long value,
588 const struct tomoyo_number_union *ptr);
Tetsuo Handa75093152010-06-16 16:23:55 +0900589bool tomoyo_correct_domain(const unsigned char *domainname);
Tetsuo Handa75093152010-06-16 16:23:55 +0900590bool tomoyo_correct_path(const char *filename);
591bool tomoyo_correct_word(const char *string);
Tetsuo Handa75093152010-06-16 16:23:55 +0900592bool tomoyo_domain_def(const unsigned char *buffer);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900593bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
594bool tomoyo_memory_ok(void *ptr);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900595bool tomoyo_number_matches_group(const unsigned long min,
596 const unsigned long max,
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900597 const struct tomoyo_group *group);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900598bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
599 struct tomoyo_name_union *ptr);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900600bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
601 struct tomoyo_number_union *ptr);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900602bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
603 const struct tomoyo_path_info *pattern);
604bool tomoyo_permstr(const char *string, const char *keyword);
605bool tomoyo_str_starts(char **src, const char *find);
606char *tomoyo_encode(const char *str);
607char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
608 va_list args);
609char *tomoyo_read_token(struct tomoyo_acl_param *param);
610char *tomoyo_realpath_from_path(struct path *path);
611char *tomoyo_realpath_nofollow(const char *pathname);
612const char *tomoyo_get_exe(void);
613const char *tomoyo_yesno(const unsigned int value);
614const struct tomoyo_path_info *tomoyo_compare_name_union
615(const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
616const struct tomoyo_path_info *tomoyo_get_name(const char *name);
617const struct tomoyo_path_info *tomoyo_path_matches_group
618(const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
619int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
620 struct path *path, const int flag);
621int tomoyo_close_control(struct tomoyo_io_buffer *head);
622int tomoyo_find_next_domain(struct linux_binprm *bprm);
623int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
624 const u8 index);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900625int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900626 struct tomoyo_domain_info *domain,
627 const u8 index);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900628int tomoyo_mkdev_perm(const u8 operation, struct path *path,
629 const unsigned int mode, unsigned int dev);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900630int tomoyo_mount_permission(char *dev_name, struct path *path,
631 const char *type, unsigned long flags,
632 void *data_page);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900633int tomoyo_open_control(const u8 type, struct file *file);
Tetsuo Handa97d69312010-02-16 09:46:15 +0900634int tomoyo_path2_perm(const u8 operation, struct path *path1,
635 struct path *path2);
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900636int tomoyo_path_number_perm(const u8 operation, struct path *path,
637 unsigned long number);
638int tomoyo_path_perm(const u8 operation, struct path *path);
639int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
640 const struct tomoyo_path_info *filename);
641int tomoyo_poll_control(struct file *file, poll_table *wait);
642int tomoyo_poll_log(struct file *file, poll_table *wait);
643int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
644 __printf(2, 3);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900645int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900646 struct tomoyo_acl_param *param,
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900647 bool (*check_duplicate)
648 (const struct tomoyo_acl_info *,
649 const struct tomoyo_acl_info *),
650 bool (*merge_duplicate)
651 (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
652 const bool));
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900653int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900654 struct tomoyo_acl_param *param,
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900655 bool (*check_duplicate)
656 (const struct tomoyo_acl_head *,
657 const struct tomoyo_acl_head *));
658int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
659int tomoyo_write_file(struct tomoyo_acl_param *param);
660int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
661int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
662 const u8 type);
663ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
664 const int buffer_len);
665ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
666 const char __user *buffer, const int buffer_len);
667struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
668 const bool transit);
669struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
670struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
671 const u8 idx);
672struct tomoyo_policy_namespace *tomoyo_assign_namespace
673(const char *domainname);
674struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
675 const u8 profile);
676unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
677 const u8 index);
678void *tomoyo_commit_ok(void *data, const unsigned int size);
679void __init tomoyo_load_builtin_policy(void);
680void __init tomoyo_mm_init(void);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900681void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900682 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900683 const struct tomoyo_acl_info *));
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900684void tomoyo_check_profile(void);
685void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp);
686void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
687void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
688void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
689 __printf(2, 3);
690void tomoyo_load_policy(const char *filename);
691void tomoyo_memory_free(void *ptr);
692void tomoyo_normalize_line(unsigned char *buffer);
693void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
694void tomoyo_print_ulong(char *buffer, const int buffer_len,
695 const unsigned long value, const u8 type);
696void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
697void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
698void tomoyo_read_log(struct tomoyo_io_buffer *head);
699void tomoyo_update_stat(const u8 index);
700void tomoyo_warn_oom(const char *function);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900701void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900702 __printf(2, 3);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900703void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
704 va_list args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900705
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900706/********** External variable definitions. **********/
707
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900708extern bool tomoyo_policy_loaded;
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900709extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
710extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
711 + TOMOYO_MAX_MAC_CATEGORY_INDEX];
712extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900713extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
714extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900715extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900716extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
717extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
Tetsuo Handa3ddf17f2011-06-29 14:22:37 +0900718extern struct list_head tomoyo_domain_list;
719extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
720extern struct list_head tomoyo_namespace_list;
721extern struct mutex tomoyo_policy_lock;
722extern struct srcu_struct tomoyo_ss;
723extern struct tomoyo_domain_info tomoyo_kernel_domain;
724extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900725extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
726extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900727
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900728/********** Inlined functions. **********/
729
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900730/**
731 * tomoyo_read_lock - Take lock for protecting policy.
732 *
733 * Returns index number for tomoyo_read_unlock().
734 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900735static inline int tomoyo_read_lock(void)
736{
737 return srcu_read_lock(&tomoyo_ss);
738}
739
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900740/**
741 * tomoyo_read_unlock - Release lock for protecting policy.
742 *
743 * @idx: Index number returned by tomoyo_read_lock().
744 *
745 * Returns nothing.
746 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900747static inline void tomoyo_read_unlock(int idx)
748{
749 srcu_read_unlock(&tomoyo_ss, idx);
750}
751
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900752/**
753 * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
754 *
755 * @a: Pointer to "struct tomoyo_path_info".
756 * @b: Pointer to "struct tomoyo_path_info".
757 *
758 * Returns true if @a == @b, false otherwise.
759 */
Kentaro Takeda95908372009-02-05 17:18:13 +0900760static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
761 const struct tomoyo_path_info *b)
762{
763 return a->hash != b->hash || strcmp(a->name, b->name);
764}
765
Kentaro Takeda95908372009-02-05 17:18:13 +0900766/**
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900767 * tomoyo_put_name - Drop reference on "struct tomoyo_name".
768 *
769 * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
770 *
771 * Returns nothing.
772 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900773static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
774{
775 if (name) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900776 struct tomoyo_name *ptr =
777 container_of(name, typeof(*ptr), entry);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900778 atomic_dec(&ptr->head.users);
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900779 }
780}
Kentaro Takeda95908372009-02-05 17:18:13 +0900781
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900782/**
783 * tomoyo_put_group - Drop reference on "struct tomoyo_group".
784 *
785 * @group: Pointer to "struct tomoyo_group". Maybe NULL.
786 *
787 * Returns nothing.
788 */
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900789static inline void tomoyo_put_group(struct tomoyo_group *group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900790{
791 if (group)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900792 atomic_dec(&group->head.users);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900793}
794
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900795/**
796 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
797 *
798 * Returns pointer to "struct tomoyo_domain_info" for current thread.
799 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900800static inline struct tomoyo_domain_info *tomoyo_domain(void)
801{
802 return current_cred()->security;
803}
Kentaro Takeda95908372009-02-05 17:18:13 +0900804
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900805/**
806 * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
807 *
808 * @task: Pointer to "struct task_struct".
809 *
810 * Returns pointer to "struct tomoyo_security" for specified thread.
811 */
Tetsuo Handa76bb0892010-02-11 09:42:40 +0900812static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
813 *task)
814{
815 return task_cred_xxx(task, security);
816}
Kentaro Takeda95908372009-02-05 17:18:13 +0900817
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900818/**
819 * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
820 *
821 * @a: Pointer to "struct tomoyo_name_union".
822 * @b: Pointer to "struct tomoyo_name_union".
823 *
824 * Returns true if @a == @b, false otherwise.
825 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900826static inline bool tomoyo_same_name_union
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900827(const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900828{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900829 return a->filename == b->filename && a->group == b->group;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900830}
831
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900832/**
833 * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
834 *
835 * @a: Pointer to "struct tomoyo_number_union".
836 * @b: Pointer to "struct tomoyo_number_union".
837 *
838 * Returns true if @a == @b, false otherwise.
839 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900840static inline bool tomoyo_same_number_union
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900841(const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900842{
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900843 return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900844 a->group == b->group && a->value_type[0] == b->value_type[0] &&
845 a->value_type[1] == b->value_type[1];
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900846}
847
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900848/**
849 * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
850 *
851 * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
852 */
853static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
854{
855 return tomoyo_domain()->ns;
856}
857
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900858#if defined(CONFIG_SLOB)
859
860/**
861 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
862 *
863 * @size: Size to be rounded up.
864 *
865 * Returns @size.
866 *
867 * Since SLOB does not round up, this function simply returns @size.
868 */
869static inline int tomoyo_round2(size_t size)
870{
871 return size;
872}
873
874#else
875
876/**
877 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
878 *
879 * @size: Size to be rounded up.
880 *
881 * Returns rounded size.
882 *
883 * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
884 * (e.g.) 128 bytes.
885 */
886static inline int tomoyo_round2(size_t size)
887{
888#if PAGE_SIZE == 4096
889 size_t bsize = 32;
890#else
891 size_t bsize = 64;
892#endif
893 if (!size)
894 return 0;
895 while (size > bsize)
896 bsize <<= 1;
897 return bsize;
898}
899
900#endif
901
Kentaro Takeda95908372009-02-05 17:18:13 +0900902/**
903 * list_for_each_cookie - iterate over a list with cookie.
904 * @pos: the &struct list_head to use as a loop cursor.
Kentaro Takeda95908372009-02-05 17:18:13 +0900905 * @head: the head for your list.
Kentaro Takeda95908372009-02-05 17:18:13 +0900906 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900907#define list_for_each_cookie(pos, head) \
908 if (!pos) \
909 pos = srcu_dereference((head)->next, &tomoyo_ss); \
910 for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900911
Kentaro Takeda95908372009-02-05 17:18:13 +0900912#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */