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