TOMOYO: Allow using executable's realpath and symlink's target as conditions.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / security / tomoyo / common.h
CommitLineData
9590837b
KT
1/*
2 * security/tomoyo/common.h
3 *
76bb0895 4 * Header file for TOMOYO.
9590837b 5 *
76bb0895 6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9590837b
KT
7 */
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>
76bb0895 22#include <linux/cred.h>
17fcfbd9 23#include <linux/poll.h>
2066a361
TH
24#include <linux/binfmts.h>
25#include <linux/highmem.h>
76bb0895
TH
26
27/********** Constants definitions. **********/
28
29/*
30 * TOMOYO uses this hash only when appending a string into the string
31 * table. Frequency of appending strings is very low. So we don't need
32 * large (e.g. 64k) hash size. 256 will be sufficient.
33 */
34#define TOMOYO_HASH_BITS 8
35#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
36
c8c57e84 37#define TOMOYO_EXEC_TMPSIZE 4096
76bb0895
TH
38
39/* Profile number is an integer between 0 and 255. */
40#define TOMOYO_MAX_PROFILES 256
41
32997144
TH
42/* Group number is an integer between 0 and 255. */
43#define TOMOYO_MAX_ACL_GROUPS 256
44
2066a361
TH
45/* Index numbers for "struct tomoyo_condition". */
46enum tomoyo_conditions_index {
47 TOMOYO_TASK_UID, /* current_uid() */
48 TOMOYO_TASK_EUID, /* current_euid() */
49 TOMOYO_TASK_SUID, /* current_suid() */
50 TOMOYO_TASK_FSUID, /* current_fsuid() */
51 TOMOYO_TASK_GID, /* current_gid() */
52 TOMOYO_TASK_EGID, /* current_egid() */
53 TOMOYO_TASK_SGID, /* current_sgid() */
54 TOMOYO_TASK_FSGID, /* current_fsgid() */
55 TOMOYO_TASK_PID, /* sys_getpid() */
56 TOMOYO_TASK_PPID, /* sys_getppid() */
8761afd4
TH
57 TOMOYO_TYPE_IS_SOCKET, /* S_IFSOCK */
58 TOMOYO_TYPE_IS_SYMLINK, /* S_IFLNK */
59 TOMOYO_TYPE_IS_FILE, /* S_IFREG */
60 TOMOYO_TYPE_IS_BLOCK_DEV, /* S_IFBLK */
61 TOMOYO_TYPE_IS_DIRECTORY, /* S_IFDIR */
62 TOMOYO_TYPE_IS_CHAR_DEV, /* S_IFCHR */
63 TOMOYO_TYPE_IS_FIFO, /* S_IFIFO */
64 TOMOYO_MODE_SETUID, /* S_ISUID */
65 TOMOYO_MODE_SETGID, /* S_ISGID */
66 TOMOYO_MODE_STICKY, /* S_ISVTX */
67 TOMOYO_MODE_OWNER_READ, /* S_IRUSR */
68 TOMOYO_MODE_OWNER_WRITE, /* S_IWUSR */
69 TOMOYO_MODE_OWNER_EXECUTE, /* S_IXUSR */
70 TOMOYO_MODE_GROUP_READ, /* S_IRGRP */
71 TOMOYO_MODE_GROUP_WRITE, /* S_IWGRP */
72 TOMOYO_MODE_GROUP_EXECUTE, /* S_IXGRP */
73 TOMOYO_MODE_OTHERS_READ, /* S_IROTH */
74 TOMOYO_MODE_OTHERS_WRITE, /* S_IWOTH */
75 TOMOYO_MODE_OTHERS_EXECUTE, /* S_IXOTH */
2ca9bf45
TH
76 TOMOYO_EXEC_REALPATH,
77 TOMOYO_SYMLINK_TARGET,
8761afd4
TH
78 TOMOYO_PATH1_UID,
79 TOMOYO_PATH1_GID,
80 TOMOYO_PATH1_INO,
81 TOMOYO_PATH1_MAJOR,
82 TOMOYO_PATH1_MINOR,
83 TOMOYO_PATH1_PERM,
84 TOMOYO_PATH1_TYPE,
85 TOMOYO_PATH1_DEV_MAJOR,
86 TOMOYO_PATH1_DEV_MINOR,
87 TOMOYO_PATH2_UID,
88 TOMOYO_PATH2_GID,
89 TOMOYO_PATH2_INO,
90 TOMOYO_PATH2_MAJOR,
91 TOMOYO_PATH2_MINOR,
92 TOMOYO_PATH2_PERM,
93 TOMOYO_PATH2_TYPE,
94 TOMOYO_PATH2_DEV_MAJOR,
95 TOMOYO_PATH2_DEV_MINOR,
96 TOMOYO_PATH1_PARENT_UID,
97 TOMOYO_PATH1_PARENT_GID,
98 TOMOYO_PATH1_PARENT_INO,
99 TOMOYO_PATH1_PARENT_PERM,
100 TOMOYO_PATH2_PARENT_UID,
101 TOMOYO_PATH2_PARENT_GID,
102 TOMOYO_PATH2_PARENT_INO,
103 TOMOYO_PATH2_PARENT_PERM,
2066a361
TH
104 TOMOYO_MAX_CONDITION_KEYWORD,
105 TOMOYO_NUMBER_UNION,
2ca9bf45 106 TOMOYO_NAME_UNION,
2066a361
TH
107};
108
8761afd4
TH
109
110/* Index numbers for stat(). */
111enum tomoyo_path_stat_index {
112 /* Do not change this order. */
113 TOMOYO_PATH1,
114 TOMOYO_PATH1_PARENT,
115 TOMOYO_PATH2,
116 TOMOYO_PATH2_PARENT,
117 TOMOYO_MAX_PATH_STAT
118};
119
b5bc60b4 120/* Index numbers for operation mode. */
cb0abe6a
TH
121enum tomoyo_mode_index {
122 TOMOYO_CONFIG_DISABLED,
123 TOMOYO_CONFIG_LEARNING,
124 TOMOYO_CONFIG_PERMISSIVE,
57c2590f 125 TOMOYO_CONFIG_ENFORCING,
eadd99cc
TH
126 TOMOYO_CONFIG_MAX_MODE,
127 TOMOYO_CONFIG_WANT_REJECT_LOG = 64,
128 TOMOYO_CONFIG_WANT_GRANT_LOG = 128,
129 TOMOYO_CONFIG_USE_DEFAULT = 255,
cb0abe6a
TH
130};
131
b5bc60b4 132/* Index numbers for entry type. */
a230f9e7
TH
133enum tomoyo_policy_id {
134 TOMOYO_ID_GROUP,
135 TOMOYO_ID_PATH_GROUP,
136 TOMOYO_ID_NUMBER_GROUP,
5448ec4f 137 TOMOYO_ID_TRANSITION_CONTROL,
a230f9e7 138 TOMOYO_ID_AGGREGATOR,
a230f9e7 139 TOMOYO_ID_MANAGER,
2066a361 140 TOMOYO_ID_CONDITION,
a230f9e7
TH
141 TOMOYO_ID_NAME,
142 TOMOYO_ID_ACL,
143 TOMOYO_ID_DOMAIN,
144 TOMOYO_MAX_POLICY
145};
146
2c47ab93
TH
147/* Index numbers for domain's attributes. */
148enum tomoyo_domain_info_flags_index {
149 /* Quota warnning flag. */
150 TOMOYO_DIF_QUOTA_WARNED,
151 /*
152 * This domain was unable to create a new domain at
153 * tomoyo_find_next_domain() because the name of the domain to be
154 * created was too long or it could not allocate memory.
155 * More than one process continued execve() without domain transition.
156 */
157 TOMOYO_DIF_TRANSITION_FAILED,
158 TOMOYO_MAX_DOMAIN_INFO_FLAGS
159};
160
b5bc60b4 161/* Index numbers for group entries. */
a230f9e7
TH
162enum tomoyo_group_id {
163 TOMOYO_PATH_GROUP,
164 TOMOYO_NUMBER_GROUP,
165 TOMOYO_MAX_GROUP
166};
167
b5bc60b4
TH
168/* Index numbers for type of numeric values. */
169enum tomoyo_value_type {
170 TOMOYO_VALUE_TYPE_INVALID,
171 TOMOYO_VALUE_TYPE_DECIMAL,
172 TOMOYO_VALUE_TYPE_OCTAL,
173 TOMOYO_VALUE_TYPE_HEXADECIMAL,
174};
4c3e9e2d 175
b5bc60b4 176/* Index numbers for domain transition control keywords. */
5448ec4f
TH
177enum tomoyo_transition_type {
178 /* Do not change this order, */
bd03a3e4
TH
179 TOMOYO_TRANSITION_CONTROL_NO_RESET,
180 TOMOYO_TRANSITION_CONTROL_RESET,
5448ec4f
TH
181 TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
182 TOMOYO_TRANSITION_CONTROL_INITIALIZE,
183 TOMOYO_TRANSITION_CONTROL_NO_KEEP,
184 TOMOYO_TRANSITION_CONTROL_KEEP,
185 TOMOYO_MAX_TRANSITION_TYPE
186};
187
76bb0895 188/* Index numbers for Access Controls. */
084da356 189enum tomoyo_acl_entry_type_index {
7ef61233
TH
190 TOMOYO_TYPE_PATH_ACL,
191 TOMOYO_TYPE_PATH2_ACL,
a1f9bb6a 192 TOMOYO_TYPE_PATH_NUMBER_ACL,
75093152 193 TOMOYO_TYPE_MKDEV_ACL,
2106ccd9 194 TOMOYO_TYPE_MOUNT_ACL,
084da356 195};
76bb0895 196
b5bc60b4 197/* Index numbers for access controls with one pathname. */
084da356 198enum tomoyo_path_acl_index {
7ef61233
TH
199 TOMOYO_TYPE_EXECUTE,
200 TOMOYO_TYPE_READ,
201 TOMOYO_TYPE_WRITE,
7c75964f 202 TOMOYO_TYPE_APPEND,
7ef61233 203 TOMOYO_TYPE_UNLINK,
7c75964f 204 TOMOYO_TYPE_GETATTR,
7ef61233 205 TOMOYO_TYPE_RMDIR,
7ef61233
TH
206 TOMOYO_TYPE_TRUNCATE,
207 TOMOYO_TYPE_SYMLINK,
7ef61233 208 TOMOYO_TYPE_CHROOT,
7ef61233
TH
209 TOMOYO_TYPE_UMOUNT,
210 TOMOYO_MAX_PATH_OPERATION
084da356
TH
211};
212
b22b8b9f 213/* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
eadd99cc
TH
214enum tomoyo_memory_stat_type {
215 TOMOYO_MEMORY_POLICY,
216 TOMOYO_MEMORY_AUDIT,
217 TOMOYO_MEMORY_QUERY,
218 TOMOYO_MAX_MEMORY_STAT
219};
220
75093152 221enum tomoyo_mkdev_acl_index {
a1f9bb6a
TH
222 TOMOYO_TYPE_MKBLOCK,
223 TOMOYO_TYPE_MKCHAR,
75093152 224 TOMOYO_MAX_MKDEV_OPERATION
a1f9bb6a
TH
225};
226
b5bc60b4 227/* Index numbers for access controls with two pathnames. */
084da356 228enum tomoyo_path2_acl_index {
7ef61233
TH
229 TOMOYO_TYPE_LINK,
230 TOMOYO_TYPE_RENAME,
231 TOMOYO_TYPE_PIVOT_ROOT,
232 TOMOYO_MAX_PATH2_OPERATION
084da356
TH
233};
234
b5bc60b4 235/* Index numbers for access controls with one pathname and one number. */
a1f9bb6a
TH
236enum tomoyo_path_number_acl_index {
237 TOMOYO_TYPE_CREATE,
238 TOMOYO_TYPE_MKDIR,
239 TOMOYO_TYPE_MKFIFO,
240 TOMOYO_TYPE_MKSOCK,
241 TOMOYO_TYPE_IOCTL,
242 TOMOYO_TYPE_CHMOD,
243 TOMOYO_TYPE_CHOWN,
244 TOMOYO_TYPE_CHGRP,
245 TOMOYO_MAX_PATH_NUMBER_OPERATION
246};
247
b5bc60b4 248/* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
084da356
TH
249enum tomoyo_securityfs_interface_index {
250 TOMOYO_DOMAINPOLICY,
251 TOMOYO_EXCEPTIONPOLICY,
084da356 252 TOMOYO_PROCESS_STATUS,
b22b8b9f 253 TOMOYO_STAT,
084da356 254 TOMOYO_SELFDOMAIN,
eadd99cc 255 TOMOYO_AUDIT,
084da356
TH
256 TOMOYO_VERSION,
257 TOMOYO_PROFILE,
17fcfbd9 258 TOMOYO_QUERY,
084da356
TH
259 TOMOYO_MANAGER
260};
9590837b 261
b5bc60b4
TH
262/* Index numbers for special mount operations. */
263enum tomoyo_special_mount {
264 TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
265 TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
266 TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
267 TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
268 TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
269 TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
270 TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
271 TOMOYO_MAX_SPECIAL_MOUNT
272};
273
274/* Index numbers for functionality. */
57c2590f
TH
275enum tomoyo_mac_index {
276 TOMOYO_MAC_FILE_EXECUTE,
277 TOMOYO_MAC_FILE_OPEN,
278 TOMOYO_MAC_FILE_CREATE,
279 TOMOYO_MAC_FILE_UNLINK,
7c75964f 280 TOMOYO_MAC_FILE_GETATTR,
57c2590f
TH
281 TOMOYO_MAC_FILE_MKDIR,
282 TOMOYO_MAC_FILE_RMDIR,
283 TOMOYO_MAC_FILE_MKFIFO,
284 TOMOYO_MAC_FILE_MKSOCK,
285 TOMOYO_MAC_FILE_TRUNCATE,
286 TOMOYO_MAC_FILE_SYMLINK,
57c2590f
TH
287 TOMOYO_MAC_FILE_MKBLOCK,
288 TOMOYO_MAC_FILE_MKCHAR,
289 TOMOYO_MAC_FILE_LINK,
290 TOMOYO_MAC_FILE_RENAME,
291 TOMOYO_MAC_FILE_CHMOD,
292 TOMOYO_MAC_FILE_CHOWN,
293 TOMOYO_MAC_FILE_CHGRP,
294 TOMOYO_MAC_FILE_IOCTL,
295 TOMOYO_MAC_FILE_CHROOT,
296 TOMOYO_MAC_FILE_MOUNT,
297 TOMOYO_MAC_FILE_UMOUNT,
298 TOMOYO_MAC_FILE_PIVOT_ROOT,
299 TOMOYO_MAX_MAC_INDEX
300};
301
b5bc60b4 302/* Index numbers for category of functionality. */
57c2590f
TH
303enum tomoyo_mac_category_index {
304 TOMOYO_MAC_CATEGORY_FILE,
305 TOMOYO_MAX_MAC_CATEGORY_INDEX
306};
307
82e0f001 308/*
b5bc60b4
TH
309 * Retry this request. Returned by tomoyo_supervisor() if policy violation has
310 * occurred in enforcing mode and the userspace daemon decided to retry.
82e0f001 311 *
b5bc60b4
TH
312 * We must choose a positive value in order to distinguish "granted" (which is
313 * 0) and "rejected" (which is a negative value) and "retry".
82e0f001 314 */
b5bc60b4
TH
315#define TOMOYO_RETRY_REQUEST 1
316
b22b8b9f
TH
317/* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
318enum tomoyo_policy_stat_type {
319 /* Do not change this order. */
320 TOMOYO_STAT_POLICY_UPDATES,
321 TOMOYO_STAT_POLICY_LEARNING, /* == TOMOYO_CONFIG_LEARNING */
322 TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
323 TOMOYO_STAT_POLICY_ENFORCING, /* == TOMOYO_CONFIG_ENFORCING */
324 TOMOYO_MAX_POLICY_STAT
325};
326
d5ca1725
TH
327/* Index numbers for profile's PREFERENCE values. */
328enum tomoyo_pref_index {
eadd99cc 329 TOMOYO_PREF_MAX_AUDIT_LOG,
d5ca1725
TH
330 TOMOYO_PREF_MAX_LEARNING_ENTRY,
331 TOMOYO_MAX_PREF
332};
333
b5bc60b4
TH
334/********** Structure definitions. **********/
335
336/* Common header for holding ACL entries. */
82e0f001
TH
337struct tomoyo_acl_head {
338 struct list_head list;
339 bool is_deleted;
340} __packed;
341
0df7e8b8
TH
342/* Common header for shared entries. */
343struct tomoyo_shared_acl_head {
344 struct list_head list;
345 atomic_t users;
346} __packed;
347
bd03a3e4
TH
348struct tomoyo_policy_namespace;
349
b5bc60b4 350/* Structure for request info. */
cb0abe6a 351struct tomoyo_request_info {
8761afd4
TH
352 /*
353 * For holding parameters specific to operations which deal files.
354 * NULL if not dealing files.
355 */
356 struct tomoyo_obj_info *obj;
2ca9bf45
TH
357 /*
358 * For holding parameters specific to execve() request.
359 * NULL if not dealing do_execve().
360 */
361 struct tomoyo_execve *ee;
cb0abe6a 362 struct tomoyo_domain_info *domain;
cf6e9a64
TH
363 /* For holding parameters. */
364 union {
365 struct {
366 const struct tomoyo_path_info *filename;
484ca79c
TH
367 /* For using wildcards at tomoyo_find_next_domain(). */
368 const struct tomoyo_path_info *matched_path;
b5bc60b4 369 /* One of values in "enum tomoyo_path_acl_index". */
cf6e9a64
TH
370 u8 operation;
371 } path;
372 struct {
373 const struct tomoyo_path_info *filename1;
374 const struct tomoyo_path_info *filename2;
b5bc60b4 375 /* One of values in "enum tomoyo_path2_acl_index". */
cf6e9a64
TH
376 u8 operation;
377 } path2;
378 struct {
379 const struct tomoyo_path_info *filename;
380 unsigned int mode;
381 unsigned int major;
382 unsigned int minor;
b5bc60b4 383 /* One of values in "enum tomoyo_mkdev_acl_index". */
cf6e9a64
TH
384 u8 operation;
385 } mkdev;
386 struct {
387 const struct tomoyo_path_info *filename;
388 unsigned long number;
b5bc60b4
TH
389 /*
390 * One of values in
391 * "enum tomoyo_path_number_acl_index".
392 */
cf6e9a64
TH
393 u8 operation;
394 } path_number;
395 struct {
396 const struct tomoyo_path_info *type;
397 const struct tomoyo_path_info *dir;
398 const struct tomoyo_path_info *dev;
399 unsigned long flags;
400 int need_dev;
401 } mount;
402 } param;
403 u8 param_type;
404 bool granted;
17fcfbd9
TH
405 u8 retry;
406 u8 profile;
cb0abe6a 407 u8 mode; /* One of tomoyo_mode_index . */
57c2590f 408 u8 type;
cb0abe6a
TH
409};
410
b5bc60b4 411/* Structure for holding a token. */
9590837b
KT
412struct tomoyo_path_info {
413 const char *name;
414 u32 hash; /* = full_name_hash(name, strlen(name)) */
9590837b
KT
415 u16 const_len; /* = tomoyo_const_part_length(name) */
416 bool is_dir; /* = tomoyo_strendswith(name, "/") */
417 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
9590837b
KT
418};
419
b5bc60b4 420/* Structure for holding string data. */
e2bf6907 421struct tomoyo_name {
0df7e8b8 422 struct tomoyo_shared_acl_head head;
76bb0895
TH
423 struct tomoyo_path_info entry;
424};
9590837b 425
b5bc60b4 426/* Structure for holding a word. */
7762fbff 427struct tomoyo_name_union {
b5bc60b4 428 /* Either @filename or @group is NULL. */
7762fbff 429 const struct tomoyo_path_info *filename;
a98aa4de 430 struct tomoyo_group *group;
7762fbff
TH
431};
432
b5bc60b4 433/* Structure for holding a number. */
4c3e9e2d
TH
434struct tomoyo_number_union {
435 unsigned long values[2];
b5bc60b4
TH
436 struct tomoyo_group *group; /* Maybe NULL. */
437 /* One of values in "enum tomoyo_value_type". */
0df7e8b8 438 u8 value_type[2];
4c3e9e2d
TH
439};
440
a98aa4de
TH
441/* Structure for "path_group"/"number_group" directive. */
442struct tomoyo_group {
0df7e8b8 443 struct tomoyo_shared_acl_head head;
4c3e9e2d
TH
444 const struct tomoyo_path_info *group_name;
445 struct list_head member_list;
4c3e9e2d
TH
446};
447
7762fbff 448/* Structure for "path_group" directive. */
a98aa4de 449struct tomoyo_path_group {
82e0f001 450 struct tomoyo_acl_head head;
7762fbff
TH
451 const struct tomoyo_path_info *member_name;
452};
453
4c3e9e2d 454/* Structure for "number_group" directive. */
a98aa4de 455struct tomoyo_number_group {
82e0f001 456 struct tomoyo_acl_head head;
4c3e9e2d
TH
457 struct tomoyo_number_union number;
458};
459
8761afd4
TH
460/* Subset of "struct stat". Used by conditional ACL and audit logs. */
461struct tomoyo_mini_stat {
462 uid_t uid;
463 gid_t gid;
464 ino_t ino;
465 mode_t mode;
466 dev_t dev;
467 dev_t rdev;
468};
469
470/* Structure for attribute checks in addition to pathname checks. */
471struct tomoyo_obj_info {
472 /*
473 * True if tomoyo_get_attributes() was already called, false otherwise.
474 */
475 bool validate_done;
476 /* True if @stat[] is valid. */
477 bool stat_valid[TOMOYO_MAX_PATH_STAT];
478 /* First pathname. Initialized with { NULL, NULL } if no path. */
479 struct path path1;
480 /* Second pathname. Initialized with { NULL, NULL } if no path. */
481 struct path path2;
482 /*
483 * Information on @path1, @path1's parent directory, @path2, @path2's
484 * parent directory.
485 */
486 struct tomoyo_mini_stat stat[TOMOYO_MAX_PATH_STAT];
2ca9bf45
TH
487 /*
488 * Content of symbolic link to be created. NULL for operations other
489 * than symlink().
490 */
491 struct tomoyo_path_info *symlink_target;
492};
493
494/* Structure for execve() operation. */
495struct tomoyo_execve {
496 struct tomoyo_request_info r;
497 struct tomoyo_obj_info obj;
498 struct linux_binprm *bprm;
499 /* For temporary use. */
500 char *tmp; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
8761afd4
TH
501};
502
2066a361
TH
503/* Structure for entries which follows "struct tomoyo_condition". */
504struct tomoyo_condition_element {
505 /* Left hand operand. */
506 u8 left;
507 /* Right hand operand. */
508 u8 right;
509 /* Equation operator. True if equals or overlaps, false otherwise. */
510 bool equals;
511};
512
513/* Structure for optional arguments. */
514struct tomoyo_condition {
515 struct tomoyo_shared_acl_head head;
516 u32 size; /* Memory size allocated for this entry. */
517 u16 condc; /* Number of conditions in this struct. */
518 u16 numbers_count; /* Number of "struct tomoyo_number_union values". */
2ca9bf45 519 u16 names_count; /* Number of "struct tomoyo_name_union names". */
2066a361
TH
520 /*
521 * struct tomoyo_condition_element condition[condc];
522 * struct tomoyo_number_union values[numbers_count];
2ca9bf45 523 * struct tomoyo_name_union names[names_count];
2066a361
TH
524 */
525};
526
b5bc60b4 527/* Common header for individual entries. */
9590837b
KT
528struct tomoyo_acl_info {
529 struct list_head list;
2066a361 530 struct tomoyo_condition *cond; /* Maybe NULL. */
237ab459 531 bool is_deleted;
b5bc60b4 532 u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
9590837b
KT
533} __packed;
534
b5bc60b4 535/* Structure for domain information. */
9590837b
KT
536struct tomoyo_domain_info {
537 struct list_head list;
538 struct list_head acl_info_list;
539 /* Name of this domain. Never NULL. */
540 const struct tomoyo_path_info *domainname;
bd03a3e4
TH
541 /* Namespace for this domain. Never NULL. */
542 struct tomoyo_policy_namespace *ns;
9590837b 543 u8 profile; /* Profile number to use. */
32997144 544 u8 group; /* Group number to use. */
a0558fc3 545 bool is_deleted; /* Delete flag. */
2c47ab93 546 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
ec8e6a4e 547 atomic_t users; /* Number of referring credentials. */
9590837b
KT
548};
549
9590837b 550/*
b5bc60b4
TH
551 * Structure for "file execute", "file read", "file write", "file append",
552 * "file unlink", "file getattr", "file rmdir", "file truncate",
553 * "file symlink", "file chroot" and "file unmount" directive.
9590837b 554 */
7ef61233
TH
555struct tomoyo_path_acl {
556 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
b5bc60b4 557 u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
7762fbff 558 struct tomoyo_name_union name;
9590837b
KT
559};
560
a1f9bb6a 561/*
b5bc60b4
TH
562 * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
563 * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
a1f9bb6a
TH
564 */
565struct tomoyo_path_number_acl {
566 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
b5bc60b4 567 /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
a1f9bb6a
TH
568 u8 perm;
569 struct tomoyo_name_union name;
570 struct tomoyo_number_union number;
571};
572
b5bc60b4 573/* Structure for "file mkblock" and "file mkchar" directive. */
75093152
TH
574struct tomoyo_mkdev_acl {
575 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
b5bc60b4 576 u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
a1f9bb6a
TH
577 struct tomoyo_name_union name;
578 struct tomoyo_number_union mode;
579 struct tomoyo_number_union major;
580 struct tomoyo_number_union minor;
581};
582
c3fa109a 583/*
b5bc60b4 584 * Structure for "file rename", "file link" and "file pivot_root" directive.
c3fa109a 585 */
7ef61233
TH
586struct tomoyo_path2_acl {
587 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
b5bc60b4 588 u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
7762fbff
TH
589 struct tomoyo_name_union name1;
590 struct tomoyo_name_union name2;
9590837b
KT
591};
592
b5bc60b4 593/* Structure for "file mount" directive. */
2106ccd9
TH
594struct tomoyo_mount_acl {
595 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
2106ccd9
TH
596 struct tomoyo_name_union dev_name;
597 struct tomoyo_name_union dir_name;
598 struct tomoyo_name_union fs_type;
599 struct tomoyo_number_union flags;
600};
601
a238cf5b
TH
602/* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
603struct tomoyo_acl_param {
604 char *data;
605 struct list_head *list;
bd03a3e4 606 struct tomoyo_policy_namespace *ns;
a238cf5b
TH
607 bool is_delete;
608};
609
0d2171d7 610#define TOMOYO_MAX_IO_READ_QUEUE 64
f23571e8 611
c3fa109a 612/*
f23571e8
TH
613 * Structure for reading/writing policy via /sys/kernel/security/tomoyo
614 * interfaces.
c3fa109a 615 */
9590837b 616struct tomoyo_io_buffer {
8fbe71f0 617 void (*read) (struct tomoyo_io_buffer *);
9590837b 618 int (*write) (struct tomoyo_io_buffer *);
17fcfbd9 619 int (*poll) (struct file *file, poll_table *wait);
9590837b
KT
620 /* Exclusive lock for this structure. */
621 struct mutex io_sem;
f23571e8 622 char __user *read_user_buf;
2c47ab93 623 size_t read_user_buf_avail;
f23571e8 624 struct {
bd03a3e4 625 struct list_head *ns;
f23571e8
TH
626 struct list_head *domain;
627 struct list_head *group;
628 struct list_head *acl;
2c47ab93
TH
629 size_t avail;
630 unsigned int step;
631 unsigned int query_index;
f23571e8 632 u16 index;
2066a361 633 u16 cond_index;
32997144 634 u8 acl_group_index;
2066a361 635 u8 cond_step;
f23571e8
TH
636 u8 bit;
637 u8 w_pos;
638 bool eof;
639 bool print_this_domain_only;
bd03a3e4 640 bool print_transition_related_only;
2066a361 641 bool print_cond_part;
f23571e8
TH
642 const char *w[TOMOYO_MAX_IO_READ_QUEUE];
643 } r;
0df7e8b8 644 struct {
bd03a3e4 645 struct tomoyo_policy_namespace *ns;
0df7e8b8
TH
646 /* The position currently writing to. */
647 struct tomoyo_domain_info *domain;
648 /* Bytes available for writing. */
2c47ab93 649 size_t avail;
bd03a3e4 650 bool is_delete;
0df7e8b8 651 } w;
9590837b
KT
652 /* Buffer for reading. */
653 char *read_buf;
9590837b 654 /* Size of read buffer. */
2c47ab93 655 size_t readbuf_size;
9590837b
KT
656 /* Buffer for writing. */
657 char *write_buf;
9590837b 658 /* Size of write buffer. */
2c47ab93 659 size_t writebuf_size;
17fcfbd9 660 /* Type of this interface. */
2c47ab93 661 enum tomoyo_securityfs_interface_index type;
2e503bbb
TH
662 /* Users counter protected by tomoyo_io_buffer_list_lock. */
663 u8 users;
664 /* List for telling GC not to kfree() elements. */
665 struct list_head list;
9590837b
KT
666};
667
76bb0895 668/*
b5bc60b4
TH
669 * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
670 * "no_keep_domain" keyword.
76bb0895 671 */
5448ec4f 672struct tomoyo_transition_control {
82e0f001 673 struct tomoyo_acl_head head;
5448ec4f 674 u8 type; /* One of values in "enum tomoyo_transition_type". */
76bb0895
TH
675 /* True if the domainname is tomoyo_get_last_name(). */
676 bool is_last_name;
5448ec4f
TH
677 const struct tomoyo_path_info *domainname; /* Maybe NULL */
678 const struct tomoyo_path_info *program; /* Maybe NULL */
76bb0895
TH
679};
680
b5bc60b4 681/* Structure for "aggregator" keyword. */
e2bf6907 682struct tomoyo_aggregator {
82e0f001 683 struct tomoyo_acl_head head;
1084307c
TH
684 const struct tomoyo_path_info *original_name;
685 const struct tomoyo_path_info *aggregated_name;
1084307c
TH
686};
687
b5bc60b4 688/* Structure for policy manager. */
e2bf6907 689struct tomoyo_manager {
82e0f001
TH
690 struct tomoyo_acl_head head;
691 bool is_domain; /* True if manager is a domainname. */
76bb0895
TH
692 /* A path to program or a domainname. */
693 const struct tomoyo_path_info *manager;
76bb0895
TH
694};
695
57c2590f
TH
696struct tomoyo_preference {
697 unsigned int learning_max_entry;
698 bool enforcing_verbose;
699 bool learning_verbose;
700 bool permissive_verbose;
701};
702
b5bc60b4 703/* Structure for /sys/kernel/security/tomnoyo/profile interface. */
57c2590f
TH
704struct tomoyo_profile {
705 const struct tomoyo_path_info *comment;
706 struct tomoyo_preference *learning;
707 struct tomoyo_preference *permissive;
708 struct tomoyo_preference *enforcing;
709 struct tomoyo_preference preference;
710 u8 default_config;
711 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
d5ca1725 712 unsigned int pref[TOMOYO_MAX_PREF];
57c2590f
TH
713};
714
eadd99cc
TH
715/* Structure for representing YYYY/MM/DD hh/mm/ss. */
716struct tomoyo_time {
717 u16 year;
718 u8 month;
719 u8 day;
720 u8 hour;
721 u8 min;
722 u8 sec;
723};
724
bd03a3e4
TH
725/* Structure for policy namespace. */
726struct tomoyo_policy_namespace {
727 /* Profile table. Memory is allocated as needed. */
728 struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
729 /* List of "struct tomoyo_group". */
730 struct list_head group_list[TOMOYO_MAX_GROUP];
731 /* List of policy. */
732 struct list_head policy_list[TOMOYO_MAX_POLICY];
733 /* The global ACL referred by "use_group" keyword. */
734 struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
735 /* List for connecting to tomoyo_namespace_list list. */
736 struct list_head namespace_list;
737 /* Profile version. Currently only 20100903 is defined. */
738 unsigned int profile_version;
739 /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
740 const char *name;
741};
742
76bb0895
TH
743/********** Function prototypes. **********/
744
2106ccd9
TH
745bool tomoyo_compare_number_union(const unsigned long value,
746 const struct tomoyo_number_union *ptr);
2066a361
TH
747bool tomoyo_condition(struct tomoyo_request_info *r,
748 const struct tomoyo_condition *cond);
75093152 749bool tomoyo_correct_domain(const unsigned char *domainname);
75093152
TH
750bool tomoyo_correct_path(const char *filename);
751bool tomoyo_correct_word(const char *string);
75093152 752bool tomoyo_domain_def(const unsigned char *buffer);
3ddf17f0
TH
753bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
754bool tomoyo_memory_ok(void *ptr);
4c3e9e2d
TH
755bool tomoyo_number_matches_group(const unsigned long min,
756 const unsigned long max,
a98aa4de 757 const struct tomoyo_group *group);
3ddf17f0
TH
758bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
759 struct tomoyo_name_union *ptr);
a238cf5b
TH
760bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
761 struct tomoyo_number_union *ptr);
3ddf17f0
TH
762bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
763 const struct tomoyo_path_info *pattern);
764bool tomoyo_permstr(const char *string, const char *keyword);
765bool tomoyo_str_starts(char **src, const char *find);
766char *tomoyo_encode(const char *str);
767char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
768 va_list args);
769char *tomoyo_read_token(struct tomoyo_acl_param *param);
770char *tomoyo_realpath_from_path(struct path *path);
771char *tomoyo_realpath_nofollow(const char *pathname);
772const char *tomoyo_get_exe(void);
773const char *tomoyo_yesno(const unsigned int value);
774const struct tomoyo_path_info *tomoyo_compare_name_union
775(const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
776const struct tomoyo_path_info *tomoyo_get_name(const char *name);
777const struct tomoyo_path_info *tomoyo_path_matches_group
778(const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
779int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
780 struct path *path, const int flag);
781int tomoyo_close_control(struct tomoyo_io_buffer *head);
782int tomoyo_find_next_domain(struct linux_binprm *bprm);
783int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
784 const u8 index);
2106ccd9 785int tomoyo_init_request_info(struct tomoyo_request_info *r,
57c2590f
TH
786 struct tomoyo_domain_info *domain,
787 const u8 index);
3ddf17f0
TH
788int tomoyo_mkdev_perm(const u8 operation, struct path *path,
789 const unsigned int mode, unsigned int dev);
b5bc60b4
TH
790int tomoyo_mount_permission(char *dev_name, struct path *path,
791 const char *type, unsigned long flags,
792 void *data_page);
3ddf17f0
TH
793int tomoyo_open_control(const u8 type, struct file *file);
794int tomoyo_path2_perm(const u8 operation, struct path *path1,
795 struct path *path2);
796int tomoyo_path_number_perm(const u8 operation, struct path *path,
797 unsigned long number);
798int tomoyo_path_perm(const u8 operation, struct path *path);
799int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
800 const struct tomoyo_path_info *filename);
801int tomoyo_poll_control(struct file *file, poll_table *wait);
802int tomoyo_poll_log(struct file *file, poll_table *wait);
803int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
804 __printf(2, 3);
805int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
806 struct tomoyo_acl_param *param,
807 bool (*check_duplicate)
808 (const struct tomoyo_acl_info *,
809 const struct tomoyo_acl_info *),
810 bool (*merge_duplicate)
811 (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
812 const bool));
813int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
814 struct tomoyo_acl_param *param,
815 bool (*check_duplicate)
816 (const struct tomoyo_acl_head *,
817 const struct tomoyo_acl_head *));
a238cf5b 818int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
a238cf5b
TH
819int tomoyo_write_file(struct tomoyo_acl_param *param);
820int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
3ddf17f0
TH
821int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
822 const u8 type);
823ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
824 const int buffer_len);
825ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
826 const char __user *buffer, const int buffer_len);
2066a361 827struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
e2bf6907 828struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
bd03a3e4 829 const bool transit);
3ddf17f0 830struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
a238cf5b
TH
831struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
832 const u8 idx);
3ddf17f0
TH
833struct tomoyo_policy_namespace *tomoyo_assign_namespace
834(const char *domainname);
835struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
836 const u8 profile);
9590837b
KT
837unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
838 const u8 index);
2066a361 839u8 tomoyo_parse_ulong(unsigned long *result, char **str);
9e4b50e9 840void *tomoyo_commit_ok(void *data, const unsigned int size);
efe836ab 841void __init tomoyo_load_builtin_policy(void);
3ddf17f0 842void __init tomoyo_mm_init(void);
99a85259 843void tomoyo_check_acl(struct tomoyo_request_info *r,
484ca79c 844 bool (*check_entry) (struct tomoyo_request_info *,
99a85259 845 const struct tomoyo_acl_info *));
3ddf17f0
TH
846void tomoyo_check_profile(void);
847void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp);
2066a361 848void tomoyo_del_condition(struct list_head *element);
3ddf17f0 849void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
8761afd4 850void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
3ddf17f0
TH
851void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
852void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
853 __printf(2, 3);
854void tomoyo_load_policy(const char *filename);
855void tomoyo_memory_free(void *ptr);
856void tomoyo_normalize_line(unsigned char *buffer);
857void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
858void tomoyo_print_ulong(char *buffer, const int buffer_len,
859 const unsigned long value, const u8 type);
860void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
861void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
862void tomoyo_read_log(struct tomoyo_io_buffer *head);
863void tomoyo_update_stat(const u8 index);
864void tomoyo_warn_oom(const char *function);
bd03a3e4 865void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
3ddf17f0 866 __printf(2, 3);
eadd99cc
TH
867void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
868 va_list args);
eadd99cc 869
76bb0895
TH
870/********** External variable definitions. **********/
871
76bb0895 872extern bool tomoyo_policy_loaded;
2066a361
TH
873extern const char * const tomoyo_condition_keyword
874[TOMOYO_MAX_CONDITION_KEYWORD];
3ddf17f0
TH
875extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
876extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
877 + TOMOYO_MAX_MAC_CATEGORY_INDEX];
878extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
2c47ab93
TH
879extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
880extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
3ddf17f0 881extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
0d2171d7
TH
882extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
883extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
2066a361 884extern struct list_head tomoyo_condition_list;
3ddf17f0
TH
885extern struct list_head tomoyo_domain_list;
886extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
887extern struct list_head tomoyo_namespace_list;
888extern struct mutex tomoyo_policy_lock;
889extern struct srcu_struct tomoyo_ss;
890extern struct tomoyo_domain_info tomoyo_kernel_domain;
891extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
eadd99cc
TH
892extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
893extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
17fcfbd9 894
76bb0895
TH
895/********** Inlined functions. **********/
896
b5bc60b4
TH
897/**
898 * tomoyo_read_lock - Take lock for protecting policy.
899 *
900 * Returns index number for tomoyo_read_unlock().
901 */
76bb0895
TH
902static inline int tomoyo_read_lock(void)
903{
904 return srcu_read_lock(&tomoyo_ss);
905}
906
b5bc60b4
TH
907/**
908 * tomoyo_read_unlock - Release lock for protecting policy.
909 *
910 * @idx: Index number returned by tomoyo_read_lock().
911 *
912 * Returns nothing.
913 */
76bb0895
TH
914static inline void tomoyo_read_unlock(int idx)
915{
916 srcu_read_unlock(&tomoyo_ss, idx);
917}
918
2066a361
TH
919/**
920 * tomoyo_sys_getppid - Copy of getppid().
921 *
922 * Returns parent process's PID.
923 *
924 * Alpha does not have getppid() defined. To be able to build this module on
925 * Alpha, I have to copy getppid() from kernel/timer.c.
926 */
927static inline pid_t tomoyo_sys_getppid(void)
928{
929 pid_t pid;
930 rcu_read_lock();
931 pid = task_tgid_vnr(current->real_parent);
932 rcu_read_unlock();
933 return pid;
934}
935
936/**
937 * tomoyo_sys_getpid - Copy of getpid().
938 *
939 * Returns current thread's PID.
940 *
941 * Alpha does not have getpid() defined. To be able to build this module on
942 * Alpha, I have to copy getpid() from kernel/timer.c.
943 */
944static inline pid_t tomoyo_sys_getpid(void)
945{
946 return task_tgid_vnr(current);
947}
948
b5bc60b4
TH
949/**
950 * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
951 *
952 * @a: Pointer to "struct tomoyo_path_info".
953 * @b: Pointer to "struct tomoyo_path_info".
954 *
955 * Returns true if @a == @b, false otherwise.
956 */
9590837b
KT
957static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
958 const struct tomoyo_path_info *b)
959{
960 return a->hash != b->hash || strcmp(a->name, b->name);
961}
962
b5bc60b4
TH
963/**
964 * tomoyo_put_name - Drop reference on "struct tomoyo_name".
965 *
966 * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
967 *
968 * Returns nothing.
969 */
76bb0895
TH
970static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
971{
972 if (name) {
e2bf6907
TH
973 struct tomoyo_name *ptr =
974 container_of(name, typeof(*ptr), entry);
0df7e8b8 975 atomic_dec(&ptr->head.users);
76bb0895
TH
976 }
977}
9590837b 978
2066a361
TH
979/**
980 * tomoyo_put_condition - Drop reference on "struct tomoyo_condition".
981 *
982 * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
983 *
984 * Returns nothing.
985 */
986static inline void tomoyo_put_condition(struct tomoyo_condition *cond)
987{
988 if (cond)
989 atomic_dec(&cond->head.users);
990}
991
b5bc60b4
TH
992/**
993 * tomoyo_put_group - Drop reference on "struct tomoyo_group".
994 *
995 * @group: Pointer to "struct tomoyo_group". Maybe NULL.
996 *
997 * Returns nothing.
998 */
a98aa4de 999static inline void tomoyo_put_group(struct tomoyo_group *group)
4c3e9e2d
TH
1000{
1001 if (group)
0df7e8b8 1002 atomic_dec(&group->head.users);
4c3e9e2d
TH
1003}
1004
b5bc60b4
TH
1005/**
1006 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
1007 *
1008 * Returns pointer to "struct tomoyo_domain_info" for current thread.
1009 */
76bb0895
TH
1010static inline struct tomoyo_domain_info *tomoyo_domain(void)
1011{
1012 return current_cred()->security;
1013}
9590837b 1014
b5bc60b4
TH
1015/**
1016 * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
1017 *
1018 * @task: Pointer to "struct task_struct".
1019 *
1020 * Returns pointer to "struct tomoyo_security" for specified thread.
1021 */
76bb0895
TH
1022static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
1023 *task)
1024{
1025 return task_cred_xxx(task, security);
1026}
9590837b 1027
b5bc60b4
TH
1028/**
1029 * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
1030 *
1031 * @a: Pointer to "struct tomoyo_name_union".
1032 * @b: Pointer to "struct tomoyo_name_union".
1033 *
1034 * Returns true if @a == @b, false otherwise.
1035 */
75093152 1036static inline bool tomoyo_same_name_union
b5bc60b4 1037(const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
7762fbff 1038{
0df7e8b8 1039 return a->filename == b->filename && a->group == b->group;
7762fbff
TH
1040}
1041
b5bc60b4
TH
1042/**
1043 * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
1044 *
1045 * @a: Pointer to "struct tomoyo_number_union".
1046 * @b: Pointer to "struct tomoyo_number_union".
1047 *
1048 * Returns true if @a == @b, false otherwise.
1049 */
75093152 1050static inline bool tomoyo_same_number_union
b5bc60b4 1051(const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
4c3e9e2d 1052{
b5bc60b4 1053 return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
0df7e8b8
TH
1054 a->group == b->group && a->value_type[0] == b->value_type[0] &&
1055 a->value_type[1] == b->value_type[1];
4c3e9e2d
TH
1056}
1057
bd03a3e4
TH
1058/**
1059 * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
1060 *
1061 * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
1062 */
1063static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
1064{
1065 return tomoyo_domain()->ns;
1066}
1067
eadd99cc
TH
1068#if defined(CONFIG_SLOB)
1069
1070/**
1071 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
1072 *
1073 * @size: Size to be rounded up.
1074 *
1075 * Returns @size.
1076 *
1077 * Since SLOB does not round up, this function simply returns @size.
1078 */
1079static inline int tomoyo_round2(size_t size)
1080{
1081 return size;
1082}
1083
1084#else
1085
1086/**
1087 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
1088 *
1089 * @size: Size to be rounded up.
1090 *
1091 * Returns rounded size.
1092 *
1093 * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
1094 * (e.g.) 128 bytes.
1095 */
1096static inline int tomoyo_round2(size_t size)
1097{
1098#if PAGE_SIZE == 4096
1099 size_t bsize = 32;
1100#else
1101 size_t bsize = 64;
1102#endif
1103 if (!size)
1104 return 0;
1105 while (size > bsize)
1106 bsize <<= 1;
1107 return bsize;
1108}
1109
1110#endif
1111
9590837b
KT
1112/**
1113 * list_for_each_cookie - iterate over a list with cookie.
1114 * @pos: the &struct list_head to use as a loop cursor.
9590837b 1115 * @head: the head for your list.
9590837b 1116 */
475e6fa3
TH
1117#define list_for_each_cookie(pos, head) \
1118 if (!pos) \
1119 pos = srcu_dereference((head)->next, &tomoyo_ss); \
1120 for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
fdb8ebb7 1121
9590837b 1122#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */