]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/common.h
TOMOYO: Add mount restriction.
[net-next-2.6.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
TH
22#include <linux/cred.h>
23struct linux_binprm;
24
25/********** Constants definitions. **********/
26
27/*
28 * TOMOYO uses this hash only when appending a string into the string
29 * table. Frequency of appending strings is very low. So we don't need
30 * large (e.g. 64k) hash size. 256 will be sufficient.
31 */
32#define TOMOYO_HASH_BITS 8
33#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
34
35/*
36 * This is the max length of a token.
37 *
38 * A token consists of only ASCII printable characters.
39 * Non printable characters in a token is represented in \ooo style
40 * octal string. Thus, \ itself is represented as \\.
41 */
42#define TOMOYO_MAX_PATHNAME_LEN 4000
43
44/* Profile number is an integer between 0 and 255. */
45#define TOMOYO_MAX_PROFILES 256
46
cb0abe6a
TH
47enum tomoyo_mode_index {
48 TOMOYO_CONFIG_DISABLED,
49 TOMOYO_CONFIG_LEARNING,
50 TOMOYO_CONFIG_PERMISSIVE,
51 TOMOYO_CONFIG_ENFORCING
52};
53
76bb0895
TH
54/* Keywords for ACLs. */
55#define TOMOYO_KEYWORD_ALIAS "alias "
2106ccd9 56#define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
76bb0895
TH
57#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
58#define TOMOYO_KEYWORD_DELETE "delete "
59#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
60#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
61#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
62#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
63#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
64#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
7762fbff 65#define TOMOYO_KEYWORD_PATH_GROUP "path_group "
4c3e9e2d 66#define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
76bb0895
TH
67#define TOMOYO_KEYWORD_SELECT "select "
68#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
69#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
70/* A domain definition starts with <kernel>. */
71#define TOMOYO_ROOT_NAME "<kernel>"
72#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
73
4c3e9e2d
TH
74/* Value type definition. */
75#define TOMOYO_VALUE_TYPE_INVALID 0
76#define TOMOYO_VALUE_TYPE_DECIMAL 1
77#define TOMOYO_VALUE_TYPE_OCTAL 2
78#define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
79
76bb0895 80/* Index numbers for Access Controls. */
084da356
TH
81enum tomoyo_mac_index {
82 TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
83 TOMOYO_MAX_ACCEPT_ENTRY,
84 TOMOYO_VERBOSE,
85 TOMOYO_MAX_CONTROL_INDEX
86};
76bb0895
TH
87
88/* Index numbers for Access Controls. */
084da356 89enum tomoyo_acl_entry_type_index {
7ef61233
TH
90 TOMOYO_TYPE_PATH_ACL,
91 TOMOYO_TYPE_PATH2_ACL,
a1f9bb6a
TH
92 TOMOYO_TYPE_PATH_NUMBER_ACL,
93 TOMOYO_TYPE_PATH_NUMBER3_ACL,
2106ccd9 94 TOMOYO_TYPE_MOUNT_ACL,
084da356 95};
76bb0895
TH
96
97/* Index numbers for File Controls. */
98
99/*
a1f9bb6a
TH
100 * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
101 * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
102 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
103 * TOMOYO_TYPE_READ_WRITE is set.
104 * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
105 * or TOMOYO_TYPE_WRITE is cleared.
106 * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
107 * TOMOYO_TYPE_READ_WRITE is cleared.
76bb0895
TH
108 */
109
084da356 110enum tomoyo_path_acl_index {
7ef61233
TH
111 TOMOYO_TYPE_READ_WRITE,
112 TOMOYO_TYPE_EXECUTE,
113 TOMOYO_TYPE_READ,
114 TOMOYO_TYPE_WRITE,
7ef61233 115 TOMOYO_TYPE_UNLINK,
7ef61233 116 TOMOYO_TYPE_RMDIR,
7ef61233
TH
117 TOMOYO_TYPE_TRUNCATE,
118 TOMOYO_TYPE_SYMLINK,
119 TOMOYO_TYPE_REWRITE,
7ef61233 120 TOMOYO_TYPE_CHROOT,
7ef61233
TH
121 TOMOYO_TYPE_UMOUNT,
122 TOMOYO_MAX_PATH_OPERATION
084da356
TH
123};
124
a1f9bb6a
TH
125enum tomoyo_path_number3_acl_index {
126 TOMOYO_TYPE_MKBLOCK,
127 TOMOYO_TYPE_MKCHAR,
128 TOMOYO_MAX_PATH_NUMBER3_OPERATION
129};
130
084da356 131enum tomoyo_path2_acl_index {
7ef61233
TH
132 TOMOYO_TYPE_LINK,
133 TOMOYO_TYPE_RENAME,
134 TOMOYO_TYPE_PIVOT_ROOT,
135 TOMOYO_MAX_PATH2_OPERATION
084da356
TH
136};
137
a1f9bb6a
TH
138enum tomoyo_path_number_acl_index {
139 TOMOYO_TYPE_CREATE,
140 TOMOYO_TYPE_MKDIR,
141 TOMOYO_TYPE_MKFIFO,
142 TOMOYO_TYPE_MKSOCK,
143 TOMOYO_TYPE_IOCTL,
144 TOMOYO_TYPE_CHMOD,
145 TOMOYO_TYPE_CHOWN,
146 TOMOYO_TYPE_CHGRP,
147 TOMOYO_MAX_PATH_NUMBER_OPERATION
148};
149
084da356
TH
150enum tomoyo_securityfs_interface_index {
151 TOMOYO_DOMAINPOLICY,
152 TOMOYO_EXCEPTIONPOLICY,
153 TOMOYO_DOMAIN_STATUS,
154 TOMOYO_PROCESS_STATUS,
155 TOMOYO_MEMINFO,
156 TOMOYO_SELFDOMAIN,
157 TOMOYO_VERSION,
158 TOMOYO_PROFILE,
159 TOMOYO_MANAGER
160};
9590837b 161
76bb0895 162/********** Structure definitions. **********/
9590837b 163
c3fa109a
TH
164/*
165 * tomoyo_page_buffer is a structure which is used for holding a pathname
166 * obtained from "struct dentry" and "struct vfsmount" pair.
167 * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
168 * (because TOMOYO escapes non ASCII printable characters using \ooo format),
169 * we will make the buffer larger.
170 */
9590837b
KT
171struct tomoyo_page_buffer {
172 char buffer[4096];
173};
174
cb0abe6a
TH
175/*
176 * tomoyo_request_info is a structure which is used for holding
177 *
178 * (1) Domain information of current process.
179 * (2) Access control mode of the profile.
180 */
181struct tomoyo_request_info {
182 struct tomoyo_domain_info *domain;
183 u8 mode; /* One of tomoyo_mode_index . */
184};
185
c3fa109a
TH
186/*
187 * tomoyo_path_info is a structure which is used for holding a string data
188 * used by TOMOYO.
189 * This structure has several fields for supporting pattern matching.
190 *
191 * (1) "name" is the '\0' terminated string data.
192 * (2) "hash" is full_name_hash(name, strlen(name)).
193 * This allows tomoyo_pathcmp() to compare by hash before actually compare
194 * using strcmp().
195 * (3) "const_len" is the length of the initial segment of "name" which
196 * consists entirely of non wildcard characters. In other words, the length
197 * which we can compare two strings using strncmp().
198 * (4) "is_dir" is a bool which is true if "name" ends with "/",
199 * false otherwise.
200 * TOMOYO distinguishes directory and non-directory. A directory ends with
201 * "/" and non-directory does not end with "/".
202 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
203 * characters, false otherwise. This allows TOMOYO to use "hash" and
204 * strcmp() for string comparison if "is_patterned" is false.
c3fa109a 205 */
9590837b
KT
206struct tomoyo_path_info {
207 const char *name;
208 u32 hash; /* = full_name_hash(name, strlen(name)) */
9590837b
KT
209 u16 const_len; /* = tomoyo_const_part_length(name) */
210 bool is_dir; /* = tomoyo_strendswith(name, "/") */
211 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
9590837b
KT
212};
213
214/*
76bb0895
TH
215 * tomoyo_name_entry is a structure which is used for linking
216 * "struct tomoyo_path_info" into tomoyo_name_list .
9590837b 217 */
76bb0895
TH
218struct tomoyo_name_entry {
219 struct list_head list;
220 atomic_t users;
221 struct tomoyo_path_info entry;
222};
9590837b 223
c3fa109a
TH
224/*
225 * tomoyo_path_info_with_data is a structure which is used for holding a
226 * pathname obtained from "struct dentry" and "struct vfsmount" pair.
227 *
228 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
229 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
230 * buffer for the pathname only.
231 *
232 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
233 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
234 * so that we don't need to return two pointers to the caller. If the caller
235 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
236 * "struct tomoyo_path_info_with_data".
237 */
9590837b 238struct tomoyo_path_info_with_data {
8e2d39a1 239 /* Keep "head" first, for this pointer is passed to kfree(). */
9590837b 240 struct tomoyo_path_info head;
a106cbfd 241 char barrier1[16]; /* Safeguard for overrun. */
9590837b
KT
242 char body[TOMOYO_MAX_PATHNAME_LEN];
243 char barrier2[16]; /* Safeguard for overrun. */
244};
245
7762fbff
TH
246struct tomoyo_name_union {
247 const struct tomoyo_path_info *filename;
248 struct tomoyo_path_group *group;
249 u8 is_group;
250};
251
4c3e9e2d
TH
252struct tomoyo_number_union {
253 unsigned long values[2];
254 struct tomoyo_number_group *group;
255 u8 min_type;
256 u8 max_type;
257 u8 is_group;
258};
259
7762fbff
TH
260/* Structure for "path_group" directive. */
261struct tomoyo_path_group {
262 struct list_head list;
263 const struct tomoyo_path_info *group_name;
264 struct list_head member_list;
265 atomic_t users;
266};
267
4c3e9e2d
TH
268/* Structure for "number_group" directive. */
269struct tomoyo_number_group {
270 struct list_head list;
271 const struct tomoyo_path_info *group_name;
272 struct list_head member_list;
273 atomic_t users;
274};
275
7762fbff
TH
276/* Structure for "path_group" directive. */
277struct tomoyo_path_group_member {
278 struct list_head list;
279 bool is_deleted;
280 const struct tomoyo_path_info *member_name;
281};
282
4c3e9e2d
TH
283/* Structure for "number_group" directive. */
284struct tomoyo_number_group_member {
285 struct list_head list;
286 bool is_deleted;
287 struct tomoyo_number_union number;
288};
289
9590837b 290/*
c3fa109a
TH
291 * tomoyo_acl_info is a structure which is used for holding
292 *
293 * (1) "list" which is linked to the ->acl_info_list of
294 * "struct tomoyo_domain_info"
ea13ddba 295 * (2) "type" which tells type of the entry (either
7ef61233 296 * "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
9590837b
KT
297 *
298 * Packing "struct tomoyo_acl_info" allows
7ef61233
TH
299 * "struct tomoyo_path_acl" to embed "u8" + "u16" and
300 * "struct tomoyo_path2_acl" to embed "u8"
9590837b
KT
301 * without enlarging their structure size.
302 */
303struct tomoyo_acl_info {
304 struct list_head list;
9590837b
KT
305 u8 type;
306} __packed;
307
c3fa109a
TH
308/*
309 * tomoyo_domain_info is a structure which is used for holding permissions
310 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
311 * It has following fields.
312 *
313 * (1) "list" which is linked to tomoyo_domain_list .
314 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
315 * (3) "domainname" which holds the name of the domain.
316 * (4) "profile" which remembers profile number assigned to this domain.
317 * (5) "is_deleted" is a bool which is true if this domain is marked as
318 * "deleted", false otherwise.
319 * (6) "quota_warned" is a bool which is used for suppressing warning message
320 * when learning mode learned too much entries.
ea13ddba
TH
321 * (7) "ignore_global_allow_read" is a bool which is true if this domain
322 * should ignore "allow_read" directive in exception policy.
323 * (8) "transition_failed" is a bool which is set to true when this domain was
324 * unable to create a new domain at tomoyo_find_next_domain() because the
325 * name of the domain to be created was too long or it could not allocate
326 * memory. If set to true, more than one process continued execve()
327 * without domain transition.
ec8e6a4e
TH
328 * (9) "users" is an atomic_t that holds how many "struct cred"->security
329 * are referring this "struct tomoyo_domain_info". If is_deleted == true
330 * and users == 0, this struct will be kfree()d upon next garbage
331 * collection.
c3fa109a
TH
332 *
333 * A domain's lifecycle is an analogy of files on / directory.
334 * Multiple domains with the same domainname cannot be created (as with
335 * creating files with the same filename fails with -EEXIST).
336 * If a process reached a domain, that process can reside in that domain after
337 * that domain is marked as "deleted" (as with a process can access an already
338 * open()ed file after that file was unlink()ed).
339 */
9590837b
KT
340struct tomoyo_domain_info {
341 struct list_head list;
342 struct list_head acl_info_list;
343 /* Name of this domain. Never NULL. */
344 const struct tomoyo_path_info *domainname;
345 u8 profile; /* Profile number to use. */
a0558fc3 346 bool is_deleted; /* Delete flag. */
9590837b 347 bool quota_warned; /* Quota warnning flag. */
ea13ddba
TH
348 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
349 bool transition_failed; /* Domain transition failed flag. */
ec8e6a4e 350 atomic_t users; /* Number of referring credentials. */
9590837b
KT
351};
352
9590837b 353/*
7ef61233 354 * tomoyo_path_acl is a structure which is used for holding an
c3fa109a
TH
355 * entry with one pathname operation (e.g. open(), mkdir()).
356 * It has following fields.
357 *
358 * (1) "head" which is a "struct tomoyo_acl_info".
359 * (2) "perm" which is a bitmask of permitted operations.
7762fbff 360 * (3) "name" is the pathname.
c3fa109a
TH
361 *
362 * Directives held by this structure are "allow_read/write", "allow_execute",
a1f9bb6a 363 * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
2106ccd9
TH
364 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
365 * "allow_unmount".
9590837b 366 */
7ef61233
TH
367struct tomoyo_path_acl {
368 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
9590837b 369 u16 perm;
7762fbff 370 struct tomoyo_name_union name;
9590837b
KT
371};
372
a1f9bb6a
TH
373/*
374 * tomoyo_path_number_acl is a structure which is used for holding an
375 * entry with one pathname and one number operation.
376 * It has following fields.
377 *
378 * (1) "head" which is a "struct tomoyo_acl_info".
379 * (2) "perm" which is a bitmask of permitted operations.
380 * (3) "name" is the pathname.
381 * (4) "number" is the numeric value.
382 *
383 * Directives held by this structure are "allow_create", "allow_mkdir",
384 * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
385 * and "allow_chgrp".
386 *
387 */
388struct tomoyo_path_number_acl {
389 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
390 u8 perm;
391 struct tomoyo_name_union name;
392 struct tomoyo_number_union number;
393};
394
395/*
396 * tomoyo_path_number3_acl is a structure which is used for holding an
397 * entry with one pathname and three numbers operation.
398 * It has following fields.
399 *
400 * (1) "head" which is a "struct tomoyo_acl_info".
401 * (2) "perm" which is a bitmask of permitted operations.
402 * (3) "mode" is the create mode.
403 * (4) "major" is the major number of device node.
404 * (5) "minor" is the minor number of device node.
405 *
406 * Directives held by this structure are "allow_mkchar", "allow_mkblock".
407 *
408 */
409struct tomoyo_path_number3_acl {
410 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
411 u8 perm;
412 struct tomoyo_name_union name;
413 struct tomoyo_number_union mode;
414 struct tomoyo_number_union major;
415 struct tomoyo_number_union minor;
416};
417
c3fa109a 418/*
7ef61233 419 * tomoyo_path2_acl is a structure which is used for holding an
937bf613 420 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
c3fa109a
TH
421 * It has following fields.
422 *
423 * (1) "head" which is a "struct tomoyo_acl_info".
424 * (2) "perm" which is a bitmask of permitted operations.
7762fbff
TH
425 * (3) "name1" is the source/old pathname.
426 * (4) "name2" is the destination/new pathname.
c3fa109a 427 *
937bf613
TH
428 * Directives held by this structure are "allow_rename", "allow_link" and
429 * "allow_pivot_root".
c3fa109a 430 */
7ef61233
TH
431struct tomoyo_path2_acl {
432 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
9590837b 433 u8 perm;
7762fbff
TH
434 struct tomoyo_name_union name1;
435 struct tomoyo_name_union name2;
9590837b
KT
436};
437
2106ccd9
TH
438/*
439 * tomoyo_mount_acl is a structure which is used for holding an
440 * entry for mount operation.
441 * It has following fields.
442 *
443 * (1) "head" which is a "struct tomoyo_acl_info".
444 * (2) "is_deleted" is boolean.
445 * (3) "dev_name" is the device name.
446 * (4) "dir_name" is the mount point.
447 * (5) "flags" is the mount flags.
448 *
449 * Directives held by this structure are "allow_rename", "allow_link" and
450 * "allow_pivot_root".
451 */
452struct tomoyo_mount_acl {
453 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
454 bool is_deleted;
455 struct tomoyo_name_union dev_name;
456 struct tomoyo_name_union dir_name;
457 struct tomoyo_name_union fs_type;
458 struct tomoyo_number_union flags;
459};
460
c3fa109a
TH
461/*
462 * tomoyo_io_buffer is a structure which is used for reading and modifying
463 * configuration via /sys/kernel/security/tomoyo/ interface.
464 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
465 * cursors.
466 *
467 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
468 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
469 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
470 * reading (one is for traversing tomoyo_domain_list and the other is for
471 * traversing "struct tomoyo_acl_info"->acl_info_list ).
472 *
473 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
474 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
475 * domain with the domainname specified by the rest of that line (NULL is set
476 * if seek failed).
477 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
478 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
479 * line (->write_var1 is set to NULL if a domain was deleted).
480 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
481 * neither "select " nor "delete ", an entry or a domain specified by that line
482 * is appended.
483 */
9590837b
KT
484struct tomoyo_io_buffer {
485 int (*read) (struct tomoyo_io_buffer *);
486 int (*write) (struct tomoyo_io_buffer *);
487 /* Exclusive lock for this structure. */
488 struct mutex io_sem;
fdb8ebb7
TH
489 /* Index returned by tomoyo_read_lock(). */
490 int reader_idx;
9590837b
KT
491 /* The position currently reading from. */
492 struct list_head *read_var1;
493 /* Extra variables for reading. */
494 struct list_head *read_var2;
495 /* The position currently writing to. */
496 struct tomoyo_domain_info *write_var1;
497 /* The step for reading. */
498 int read_step;
499 /* Buffer for reading. */
500 char *read_buf;
501 /* EOF flag for reading. */
502 bool read_eof;
503 /* Read domain ACL of specified PID? */
504 bool read_single_domain;
505 /* Extra variable for reading. */
506 u8 read_bit;
507 /* Bytes available for reading. */
508 int read_avail;
509 /* Size of read buffer. */
510 int readbuf_size;
511 /* Buffer for writing. */
512 char *write_buf;
513 /* Bytes available for writing. */
514 int write_avail;
515 /* Size of write buffer. */
516 int writebuf_size;
517};
518
76bb0895
TH
519/*
520 * tomoyo_globally_readable_file_entry is a structure which is used for holding
521 * "allow_read" entries.
522 * It has following fields.
523 *
524 * (1) "list" which is linked to tomoyo_globally_readable_list .
525 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
526 * (3) "is_deleted" is a bool which is true if marked as deleted, false
527 * otherwise.
528 */
529struct tomoyo_globally_readable_file_entry {
530 struct list_head list;
531 const struct tomoyo_path_info *filename;
532 bool is_deleted;
533};
534
535/*
536 * tomoyo_pattern_entry is a structure which is used for holding
537 * "tomoyo_pattern_list" entries.
538 * It has following fields.
539 *
540 * (1) "list" which is linked to tomoyo_pattern_list .
541 * (2) "pattern" is a pathname pattern which is used for converting pathnames
542 * to pathname patterns during learning mode.
543 * (3) "is_deleted" is a bool which is true if marked as deleted, false
544 * otherwise.
545 */
546struct tomoyo_pattern_entry {
547 struct list_head list;
548 const struct tomoyo_path_info *pattern;
549 bool is_deleted;
550};
551
552/*
553 * tomoyo_no_rewrite_entry is a structure which is used for holding
554 * "deny_rewrite" entries.
555 * It has following fields.
556 *
557 * (1) "list" which is linked to tomoyo_no_rewrite_list .
558 * (2) "pattern" is a pathname which is by default not permitted to modify
559 * already existing content.
560 * (3) "is_deleted" is a bool which is true if marked as deleted, false
561 * otherwise.
562 */
563struct tomoyo_no_rewrite_entry {
564 struct list_head list;
565 const struct tomoyo_path_info *pattern;
566 bool is_deleted;
567};
568
569/*
570 * tomoyo_domain_initializer_entry is a structure which is used for holding
571 * "initialize_domain" and "no_initialize_domain" entries.
572 * It has following fields.
573 *
574 * (1) "list" which is linked to tomoyo_domain_initializer_list .
575 * (2) "domainname" which is "a domainname" or "the last component of a
576 * domainname". This field is NULL if "from" clause is not specified.
577 * (3) "program" which is a program's pathname.
578 * (4) "is_deleted" is a bool which is true if marked as deleted, false
579 * otherwise.
580 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
581 * otherwise.
582 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
583 * component of a domainname", false otherwise.
584 */
585struct tomoyo_domain_initializer_entry {
586 struct list_head list;
587 const struct tomoyo_path_info *domainname; /* This may be NULL */
588 const struct tomoyo_path_info *program;
589 bool is_deleted;
590 bool is_not; /* True if this entry is "no_initialize_domain". */
591 /* True if the domainname is tomoyo_get_last_name(). */
592 bool is_last_name;
593};
594
595/*
596 * tomoyo_domain_keeper_entry is a structure which is used for holding
597 * "keep_domain" and "no_keep_domain" entries.
598 * It has following fields.
599 *
600 * (1) "list" which is linked to tomoyo_domain_keeper_list .
601 * (2) "domainname" which is "a domainname" or "the last component of a
602 * domainname".
603 * (3) "program" which is a program's pathname.
604 * This field is NULL if "from" clause is not specified.
605 * (4) "is_deleted" is a bool which is true if marked as deleted, false
606 * otherwise.
607 * (5) "is_not" is a bool which is true if "no_initialize_domain", false
608 * otherwise.
609 * (6) "is_last_name" is a bool which is true if "domainname" is "the last
610 * component of a domainname", false otherwise.
611 */
612struct tomoyo_domain_keeper_entry {
613 struct list_head list;
614 const struct tomoyo_path_info *domainname;
615 const struct tomoyo_path_info *program; /* This may be NULL */
616 bool is_deleted;
617 bool is_not; /* True if this entry is "no_keep_domain". */
618 /* True if the domainname is tomoyo_get_last_name(). */
619 bool is_last_name;
620};
621
622/*
623 * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
624 * It has following fields.
625 *
626 * (1) "list" which is linked to tomoyo_alias_list .
627 * (2) "original_name" which is a dereferenced pathname.
628 * (3) "aliased_name" which is a symlink's pathname.
629 * (4) "is_deleted" is a bool which is true if marked as deleted, false
630 * otherwise.
631 */
632struct tomoyo_alias_entry {
633 struct list_head list;
634 const struct tomoyo_path_info *original_name;
635 const struct tomoyo_path_info *aliased_name;
636 bool is_deleted;
637};
638
639/*
640 * tomoyo_policy_manager_entry is a structure which is used for holding list of
641 * domainnames or programs which are permitted to modify configuration via
642 * /sys/kernel/security/tomoyo/ interface.
643 * It has following fields.
644 *
645 * (1) "list" which is linked to tomoyo_policy_manager_list .
646 * (2) "manager" is a domainname or a program's pathname.
647 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
648 * otherwise.
649 * (4) "is_deleted" is a bool which is true if marked as deleted, false
650 * otherwise.
651 */
652struct tomoyo_policy_manager_entry {
653 struct list_head list;
654 /* A path to program or a domainname. */
655 const struct tomoyo_path_info *manager;
656 bool is_domain; /* True if manager is a domainname. */
657 bool is_deleted; /* True if this entry is deleted. */
658};
659
660/********** Function prototypes. **********/
661
7762fbff
TH
662/* Check whether the given name matches the given name_union. */
663bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
664 const struct tomoyo_name_union *ptr);
2106ccd9
TH
665/* Check whether the given number matches the given number_union. */
666bool tomoyo_compare_number_union(const unsigned long value,
667 const struct tomoyo_number_union *ptr);
9590837b 668/* Check whether the domain has too many ACL entries to hold. */
cb0abe6a 669bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
9590837b
KT
670/* Transactional sprintf() for policy dump. */
671bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
672 __attribute__ ((format(printf, 2, 3)));
673/* Check whether the domainname is correct. */
17080008 674bool tomoyo_is_correct_domain(const unsigned char *domainname);
9590837b
KT
675/* Check whether the token is correct. */
676bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
17080008 677 const s8 pattern_type, const s8 end_type);
9590837b
KT
678/* Check whether the token can be a domainname. */
679bool tomoyo_is_domain_def(const unsigned char *buffer);
7762fbff
TH
680bool tomoyo_parse_name_union(const char *filename,
681 struct tomoyo_name_union *ptr);
682/* Check whether the given filename matches the given path_group. */
683bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
684 const struct tomoyo_path_group *group,
685 const bool may_use_pattern);
4c3e9e2d
TH
686/* Check whether the given value matches the given number_group. */
687bool tomoyo_number_matches_group(const unsigned long min,
688 const unsigned long max,
689 const struct tomoyo_number_group *group);
9590837b
KT
690/* Check whether the given filename matches the given pattern. */
691bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
692 const struct tomoyo_path_info *pattern);
4c3e9e2d
TH
693
694bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
695 const struct tomoyo_number_union *ptr);
696bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
697
9590837b
KT
698/* Read "alias" entry in exception policy. */
699bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
700/*
701 * Read "initialize_domain" and "no_initialize_domain" entry
702 * in exception policy.
703 */
704bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
705/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
706bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
707/* Read "file_pattern" entry in exception policy. */
708bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
7762fbff
TH
709/* Read "path_group" entry in exception policy. */
710bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
4c3e9e2d
TH
711/* Read "number_group" entry in exception policy. */
712bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
9590837b
KT
713/* Read "allow_read" entry in exception policy. */
714bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
715/* Read "deny_rewrite" entry in exception policy. */
716bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
7762fbff
TH
717/* Tokenize a line. */
718bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
9590837b
KT
719/* Write domain policy violation warning message to console? */
720bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
721/* Convert double path operation to operation name. */
7ef61233 722const char *tomoyo_path22keyword(const u8 operation);
a1f9bb6a
TH
723const char *tomoyo_path_number2keyword(const u8 operation);
724const char *tomoyo_path_number32keyword(const u8 operation);
9590837b
KT
725/* Get the last component of the given domainname. */
726const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
9590837b 727/* Convert single path operation to operation name. */
7ef61233 728const char *tomoyo_path2keyword(const u8 operation);
2106ccd9
TH
729/* Fill "struct tomoyo_request_info". */
730int tomoyo_init_request_info(struct tomoyo_request_info *r,
731 struct tomoyo_domain_info *domain);
732/* Check permission for mount operation. */
733int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
734 unsigned long flags, void *data_page);
9590837b
KT
735/* Create "alias" entry in exception policy. */
736int tomoyo_write_alias_policy(char *data, const bool is_delete);
737/*
738 * Create "initialize_domain" and "no_initialize_domain" entry
739 * in exception policy.
740 */
741int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
742 const bool is_delete);
743/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
744int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
745 const bool is_delete);
746/*
747 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
748 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
749 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
750 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
751 * "allow_link" entry in domain policy.
752 */
753int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
754 const bool is_delete);
755/* Create "allow_read" entry in exception policy. */
756int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
2106ccd9
TH
757/* Create "allow_mount" entry in domain policy. */
758int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
759 const bool is_delete);
9590837b
KT
760/* Create "deny_rewrite" entry in exception policy. */
761int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
762/* Create "file_pattern" entry in exception policy. */
763int tomoyo_write_pattern_policy(char *data, const bool is_delete);
7762fbff
TH
764/* Create "path_group" entry in exception policy. */
765int tomoyo_write_path_group_policy(char *data, const bool is_delete);
4c3e9e2d
TH
766/* Create "number_group" entry in exception policy. */
767int tomoyo_write_number_group_policy(char *data, const bool is_delete);
9590837b
KT
768/* Find a domain by the given name. */
769struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
770/* Find or create a domain by the given name. */
771struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
772 domainname,
773 const u8 profile);
2106ccd9
TH
774/* Get patterned pathname. */
775const struct tomoyo_path_info *
776tomoyo_get_file_pattern(const struct tomoyo_path_info *filename);
7762fbff
TH
777/* Allocate memory for "struct tomoyo_path_group". */
778struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
4c3e9e2d 779struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
7762fbff 780
9590837b
KT
781/* Check mode for specified functionality. */
782unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
783 const u8 index);
9590837b
KT
784/* Fill in "struct tomoyo_path_info" members. */
785void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
786/* Run policy loader when /sbin/init starts. */
787void tomoyo_load_policy(const char *filename);
9590837b 788
4c3e9e2d
TH
789void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
790
76bb0895
TH
791/* Convert binary string to ascii string. */
792int tomoyo_encode(char *buffer, int buflen, const char *str);
793
794/* Returns realpath(3) of the given pathname but ignores chroot'ed root. */
795int tomoyo_realpath_from_path2(struct path *path, char *newname,
796 int newname_len);
797
798/*
799 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
800 * These functions use kzalloc(), so the caller must call kfree()
801 * if these functions didn't return NULL.
802 */
803char *tomoyo_realpath(const char *pathname);
804/*
805 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
806 */
807char *tomoyo_realpath_nofollow(const char *pathname);
808/* Same with tomoyo_realpath() except that the pathname is already solved. */
809char *tomoyo_realpath_from_path(struct path *path);
810
811/* Check memory quota. */
812bool tomoyo_memory_ok(void *ptr);
9e4b50e9 813void *tomoyo_commit_ok(void *data, const unsigned int size);
76bb0895
TH
814
815/*
816 * Keep the given name on the RAM.
817 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
818 */
819const struct tomoyo_path_info *tomoyo_get_name(const char *name);
820
821/* Check for memory usage. */
822int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
823
824/* Set memory quota. */
825int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
826
827/* Initialize realpath related code. */
828void __init tomoyo_realpath_init(void);
829int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
830 const struct tomoyo_path_info *filename);
831int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
832 struct path *path, const int flag);
a1f9bb6a
TH
833int tomoyo_path_number_perm(const u8 operation, struct path *path,
834 unsigned long number);
835int tomoyo_path_number3_perm(const u8 operation, struct path *path,
836 const unsigned int mode, unsigned int dev);
97d6931e
TH
837int tomoyo_path_perm(const u8 operation, struct path *path);
838int tomoyo_path2_perm(const u8 operation, struct path *path1,
839 struct path *path2);
76bb0895
TH
840int tomoyo_find_next_domain(struct linux_binprm *bprm);
841
a1f9bb6a
TH
842void tomoyo_print_ulong(char *buffer, const int buffer_len,
843 const unsigned long value, const u8 type);
844
7762fbff
TH
845/* Drop refcount on tomoyo_name_union. */
846void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
847
847b173e
TH
848/* Run garbage collector. */
849void tomoyo_run_gc(void);
850
851void tomoyo_memory_free(void *ptr);
852
76bb0895
TH
853/********** External variable definitions. **********/
854
855/* Lock for GC. */
856extern struct srcu_struct tomoyo_ss;
857
858/* The list for "struct tomoyo_domain_info". */
859extern struct list_head tomoyo_domain_list;
860
7762fbff 861extern struct list_head tomoyo_path_group_list;
4c3e9e2d 862extern struct list_head tomoyo_number_group_list;
847b173e
TH
863extern struct list_head tomoyo_domain_initializer_list;
864extern struct list_head tomoyo_domain_keeper_list;
865extern struct list_head tomoyo_alias_list;
866extern struct list_head tomoyo_globally_readable_list;
867extern struct list_head tomoyo_pattern_list;
868extern struct list_head tomoyo_no_rewrite_list;
869extern struct list_head tomoyo_policy_manager_list;
870extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
847b173e 871
76bb0895
TH
872/* Lock for protecting policy. */
873extern struct mutex tomoyo_policy_lock;
874
875/* Has /sbin/init started? */
876extern bool tomoyo_policy_loaded;
877
878/* The kernel's domain. */
879extern struct tomoyo_domain_info tomoyo_kernel_domain;
880
881/********** Inlined functions. **********/
882
883static inline int tomoyo_read_lock(void)
884{
885 return srcu_read_lock(&tomoyo_ss);
886}
887
888static inline void tomoyo_read_unlock(int idx)
889{
890 srcu_read_unlock(&tomoyo_ss, idx);
891}
892
9590837b
KT
893/* strcmp() for "struct tomoyo_path_info" structure. */
894static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
895 const struct tomoyo_path_info *b)
896{
897 return a->hash != b->hash || strcmp(a->name, b->name);
898}
899
9590837b
KT
900/**
901 * tomoyo_is_valid - Check whether the character is a valid char.
902 *
903 * @c: The character to check.
904 *
905 * Returns true if @c is a valid character, false otherwise.
906 */
907static inline bool tomoyo_is_valid(const unsigned char c)
908{
909 return c > ' ' && c < 127;
910}
911
912/**
913 * tomoyo_is_invalid - Check whether the character is an invalid char.
914 *
915 * @c: The character to check.
916 *
917 * Returns true if @c is an invalid character, false otherwise.
918 */
919static inline bool tomoyo_is_invalid(const unsigned char c)
920{
921 return c && (c <= ' ' || c >= 127);
922}
923
76bb0895
TH
924static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
925{
926 if (name) {
927 struct tomoyo_name_entry *ptr =
928 container_of(name, struct tomoyo_name_entry, entry);
929 atomic_dec(&ptr->users);
930 }
931}
9590837b 932
7762fbff
TH
933static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
934{
935 if (group)
936 atomic_dec(&group->users);
937}
938
4c3e9e2d
TH
939static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
940{
941 if (group)
942 atomic_dec(&group->users);
943}
944
76bb0895
TH
945static inline struct tomoyo_domain_info *tomoyo_domain(void)
946{
947 return current_cred()->security;
948}
9590837b 949
76bb0895
TH
950static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
951 *task)
952{
953 return task_cred_xxx(task, security);
954}
9590837b 955
7762fbff
TH
956static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
957 const struct tomoyo_acl_info *p2)
958{
959 return p1->type == p2->type;
960}
961
962static inline bool tomoyo_is_same_name_union
963(const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
964{
965 return p1->filename == p2->filename && p1->group == p2->group &&
966 p1->is_group == p2->is_group;
967}
968
4c3e9e2d
TH
969static inline bool tomoyo_is_same_number_union
970(const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
971{
972 return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
973 && p1->group == p2->group && p1->min_type == p2->min_type &&
974 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
975}
976
7762fbff
TH
977static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
978 const struct tomoyo_path_acl *p2)
979{
980 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
981 tomoyo_is_same_name_union(&p1->name, &p2->name);
982}
983
a1f9bb6a
TH
984static inline bool tomoyo_is_same_path_number3_acl
985(const struct tomoyo_path_number3_acl *p1,
986 const struct tomoyo_path_number3_acl *p2)
987{
988 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
989 && tomoyo_is_same_name_union(&p1->name, &p2->name)
990 && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
991 && tomoyo_is_same_number_union(&p1->major, &p2->major)
992 && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
993}
994
995
7762fbff
TH
996static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
997 const struct tomoyo_path2_acl *p2)
998{
999 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1000 tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
1001 tomoyo_is_same_name_union(&p1->name2, &p2->name2);
1002}
1003
a1f9bb6a
TH
1004static inline bool tomoyo_is_same_path_number_acl
1005(const struct tomoyo_path_number_acl *p1,
1006 const struct tomoyo_path_number_acl *p2)
1007{
1008 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1009 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1010 && tomoyo_is_same_number_union(&p1->number, &p2->number);
1011}
1012
2106ccd9
TH
1013static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1014 const struct tomoyo_mount_acl *p2)
1015{
1016 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1017 tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
1018 tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
1019 tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
1020 tomoyo_is_same_number_union(&p1->flags, &p2->flags);
1021}
1022
9e4b50e9
TH
1023static inline bool tomoyo_is_same_domain_initializer_entry
1024(const struct tomoyo_domain_initializer_entry *p1,
1025 const struct tomoyo_domain_initializer_entry *p2)
1026{
1027 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1028 && p1->domainname == p2->domainname
1029 && p1->program == p2->program;
1030}
1031
1032static inline bool tomoyo_is_same_domain_keeper_entry
1033(const struct tomoyo_domain_keeper_entry *p1,
1034 const struct tomoyo_domain_keeper_entry *p2)
1035{
1036 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1037 && p1->domainname == p2->domainname
1038 && p1->program == p2->program;
1039}
1040
1041static inline bool tomoyo_is_same_alias_entry
1042(const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1043{
1044 return p1->original_name == p2->original_name &&
1045 p1->aliased_name == p2->aliased_name;
1046}
1047
9590837b
KT
1048/**
1049 * list_for_each_cookie - iterate over a list with cookie.
1050 * @pos: the &struct list_head to use as a loop cursor.
1051 * @cookie: the &struct list_head to use as a cookie.
1052 * @head: the head for your list.
1053 *
fdb8ebb7 1054 * Same with list_for_each_rcu() except that this primitive uses @cookie
9590837b
KT
1055 * so that we can continue iteration.
1056 * @cookie must be NULL when iteration starts, and @cookie will become
1057 * NULL when iteration finishes.
1058 */
fdb8ebb7
TH
1059#define list_for_each_cookie(pos, cookie, head) \
1060 for (({ if (!cookie) \
1061 cookie = head; }), \
1062 pos = rcu_dereference((cookie)->next); \
1063 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
1064 (cookie) = pos, pos = rcu_dereference(pos->next))
1065
9590837b 1066#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */