TOMOYO: Support longer pathname.
[deliverable/linux.git] / security / tomoyo / common.h
1 /*
2 * security/tomoyo/common.h
3 *
4 * Header file for TOMOYO.
5 *
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
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>
22 #include <linux/cred.h>
23 #include <linux/poll.h>
24 struct 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
36 #define TOMOYO_EXEC_TMPSIZE 4096
37
38 /* Profile number is an integer between 0 and 255. */
39 #define TOMOYO_MAX_PROFILES 256
40
41 enum tomoyo_mode_index {
42 TOMOYO_CONFIG_DISABLED,
43 TOMOYO_CONFIG_LEARNING,
44 TOMOYO_CONFIG_PERMISSIVE,
45 TOMOYO_CONFIG_ENFORCING
46 };
47
48 /* Keywords for ACLs. */
49 #define TOMOYO_KEYWORD_ALIAS "alias "
50 #define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
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 "
59 #define TOMOYO_KEYWORD_PATH_GROUP "path_group "
60 #define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
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"
64 #define TOMOYO_KEYWORD_QUOTA_EXCEEDED "quota_exceeded"
65 #define TOMOYO_KEYWORD_TRANSITION_FAILED "transition_failed"
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
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
76 /* Index numbers for Access Controls. */
77 enum 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 };
83
84 /* Index numbers for Access Controls. */
85 enum tomoyo_acl_entry_type_index {
86 TOMOYO_TYPE_PATH_ACL,
87 TOMOYO_TYPE_PATH2_ACL,
88 TOMOYO_TYPE_PATH_NUMBER_ACL,
89 TOMOYO_TYPE_PATH_NUMBER3_ACL,
90 TOMOYO_TYPE_MOUNT_ACL,
91 };
92
93 /* Index numbers for File Controls. */
94
95 /*
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.
104 */
105
106 enum tomoyo_path_acl_index {
107 TOMOYO_TYPE_READ_WRITE,
108 TOMOYO_TYPE_EXECUTE,
109 TOMOYO_TYPE_READ,
110 TOMOYO_TYPE_WRITE,
111 TOMOYO_TYPE_UNLINK,
112 TOMOYO_TYPE_RMDIR,
113 TOMOYO_TYPE_TRUNCATE,
114 TOMOYO_TYPE_SYMLINK,
115 TOMOYO_TYPE_REWRITE,
116 TOMOYO_TYPE_CHROOT,
117 TOMOYO_TYPE_UMOUNT,
118 TOMOYO_MAX_PATH_OPERATION
119 };
120
121 enum tomoyo_path_number3_acl_index {
122 TOMOYO_TYPE_MKBLOCK,
123 TOMOYO_TYPE_MKCHAR,
124 TOMOYO_MAX_PATH_NUMBER3_OPERATION
125 };
126
127 enum tomoyo_path2_acl_index {
128 TOMOYO_TYPE_LINK,
129 TOMOYO_TYPE_RENAME,
130 TOMOYO_TYPE_PIVOT_ROOT,
131 TOMOYO_MAX_PATH2_OPERATION
132 };
133
134 enum 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
146 enum 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,
155 TOMOYO_QUERY,
156 TOMOYO_MANAGER
157 };
158
159 #define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
160
161 /********** Structure definitions. **********/
162
163 /*
164 * tomoyo_request_info is a structure which is used for holding
165 *
166 * (1) Domain information of current process.
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.
170 */
171 struct tomoyo_request_info {
172 struct tomoyo_domain_info *domain;
173 u8 retry;
174 u8 profile;
175 u8 mode; /* One of tomoyo_mode_index . */
176 };
177
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.
197 */
198 struct tomoyo_path_info {
199 const char *name;
200 u32 hash; /* = full_name_hash(name, strlen(name)) */
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) */
204 };
205
206 /*
207 * tomoyo_name_entry is a structure which is used for linking
208 * "struct tomoyo_path_info" into tomoyo_name_list .
209 */
210 struct tomoyo_name_entry {
211 struct list_head list;
212 atomic_t users;
213 struct tomoyo_path_info entry;
214 };
215
216 struct tomoyo_name_union {
217 const struct tomoyo_path_info *filename;
218 struct tomoyo_path_group *group;
219 u8 is_group;
220 };
221
222 struct 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
230 /* Structure for "path_group" directive. */
231 struct 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
238 /* Structure for "number_group" directive. */
239 struct 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
246 /* Structure for "path_group" directive. */
247 struct tomoyo_path_group_member {
248 struct list_head list;
249 bool is_deleted;
250 const struct tomoyo_path_info *member_name;
251 };
252
253 /* Structure for "number_group" directive. */
254 struct tomoyo_number_group_member {
255 struct list_head list;
256 bool is_deleted;
257 struct tomoyo_number_union number;
258 };
259
260 /*
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"
265 * (2) "type" which tells type of the entry (either
266 * "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
267 *
268 * Packing "struct tomoyo_acl_info" allows
269 * "struct tomoyo_path_acl" to embed "u8" + "u16" and
270 * "struct tomoyo_path2_acl" to embed "u8"
271 * without enlarging their structure size.
272 */
273 struct tomoyo_acl_info {
274 struct list_head list;
275 u8 type;
276 } __packed;
277
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.
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.
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.
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 */
310 struct 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. */
316 bool is_deleted; /* Delete flag. */
317 bool quota_warned; /* Quota warnning flag. */
318 bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
319 bool transition_failed; /* Domain transition failed flag. */
320 atomic_t users; /* Number of referring credentials. */
321 };
322
323 /*
324 * tomoyo_path_acl is a structure which is used for holding an
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.
330 * (3) "name" is the pathname.
331 *
332 * Directives held by this structure are "allow_read/write", "allow_execute",
333 * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
334 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
335 * "allow_unmount".
336 */
337 struct tomoyo_path_acl {
338 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
339 u16 perm;
340 struct tomoyo_name_union name;
341 };
342
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 */
358 struct 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 */
379 struct 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
388 /*
389 * tomoyo_path2_acl is a structure which is used for holding an
390 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
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.
395 * (3) "name1" is the source/old pathname.
396 * (4) "name2" is the destination/new pathname.
397 *
398 * Directives held by this structure are "allow_rename", "allow_link" and
399 * "allow_pivot_root".
400 */
401 struct tomoyo_path2_acl {
402 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
403 u8 perm;
404 struct tomoyo_name_union name1;
405 struct tomoyo_name_union name2;
406 };
407
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 */
422 struct 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
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 */
454 struct tomoyo_io_buffer {
455 int (*read) (struct tomoyo_io_buffer *);
456 int (*write) (struct tomoyo_io_buffer *);
457 int (*poll) (struct file *file, poll_table *wait);
458 /* Exclusive lock for this structure. */
459 struct mutex io_sem;
460 /* Index returned by tomoyo_read_lock(). */
461 int reader_idx;
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;
488 /* Type of this interface. */
489 u8 type;
490 };
491
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 */
502 struct 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 */
519 struct 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 */
536 struct 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 */
558 struct 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 */
585 struct 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 */
605 struct 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 */
625 struct 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
635 extern asmlinkage long sys_getpid(void);
636 extern asmlinkage long sys_getppid(void);
637
638 /* Check whether the given string starts with the given keyword. */
639 bool tomoyo_str_starts(char **src, const char *find);
640 /* Get tomoyo_realpath() of current process. */
641 const char *tomoyo_get_exe(void);
642 /* Format string. */
643 void tomoyo_normalize_line(unsigned char *buffer);
644 /* Print warning or error message on console. */
645 void 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. */
648 void tomoyo_check_profile(void);
649 /* Open operation for /sys/kernel/security/tomoyo/ interface. */
650 int tomoyo_open_control(const u8 type, struct file *file);
651 /* Close /sys/kernel/security/tomoyo/ interface. */
652 int tomoyo_close_control(struct file *file);
653 /* Read operation for /sys/kernel/security/tomoyo/ interface. */
654 int tomoyo_read_control(struct file *file, char __user *buffer,
655 const int buffer_len);
656 /* Write operation for /sys/kernel/security/tomoyo/ interface. */
657 int 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. */
660 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
661 /* Print out of memory warning message. */
662 void tomoyo_warn_oom(const char *function);
663 /* Check whether the given name matches the given name_union. */
664 bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
665 const struct tomoyo_name_union *ptr);
666 /* Check whether the given number matches the given number_union. */
667 bool tomoyo_compare_number_union(const unsigned long value,
668 const struct tomoyo_number_union *ptr);
669 /* Transactional sprintf() for policy dump. */
670 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
671 __attribute__ ((format(printf, 2, 3)));
672 /* Check whether the domainname is correct. */
673 bool tomoyo_is_correct_domain(const unsigned char *domainname);
674 /* Check whether the token is correct. */
675 bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
676 const s8 pattern_type, const s8 end_type);
677 /* Check whether the token can be a domainname. */
678 bool tomoyo_is_domain_def(const unsigned char *buffer);
679 bool tomoyo_parse_name_union(const char *filename,
680 struct tomoyo_name_union *ptr);
681 /* Check whether the given filename matches the given path_group. */
682 bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
683 const struct tomoyo_path_group *group,
684 const bool may_use_pattern);
685 /* Check whether the given value matches the given number_group. */
686 bool tomoyo_number_matches_group(const unsigned long min,
687 const unsigned long max,
688 const struct tomoyo_number_group *group);
689 /* Check whether the given filename matches the given pattern. */
690 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
691 const struct tomoyo_path_info *pattern);
692
693 bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
694 const struct tomoyo_number_union *ptr);
695 bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
696
697 /* Read "alias" entry in exception policy. */
698 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
699 /*
700 * Read "initialize_domain" and "no_initialize_domain" entry
701 * in exception policy.
702 */
703 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
704 /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
705 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
706 /* Read "file_pattern" entry in exception policy. */
707 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
708 /* Read "path_group" entry in exception policy. */
709 bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
710 /* Read "number_group" entry in exception policy. */
711 bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
712 /* Read "allow_read" entry in exception policy. */
713 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
714 /* Read "deny_rewrite" entry in exception policy. */
715 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
716 /* Tokenize a line. */
717 bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
718 /* Write domain policy violation warning message to console? */
719 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
720 /* Convert double path operation to operation name. */
721 const char *tomoyo_path22keyword(const u8 operation);
722 const char *tomoyo_path_number2keyword(const u8 operation);
723 const char *tomoyo_path_number32keyword(const u8 operation);
724 /* Get the last component of the given domainname. */
725 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
726 /* Convert single path operation to operation name. */
727 const char *tomoyo_path2keyword(const u8 operation);
728 /* Fill "struct tomoyo_request_info". */
729 int tomoyo_init_request_info(struct tomoyo_request_info *r,
730 struct tomoyo_domain_info *domain);
731 /* Check permission for mount operation. */
732 int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
733 unsigned long flags, void *data_page);
734 /* Create "alias" entry in exception policy. */
735 int tomoyo_write_alias_policy(char *data, const bool is_delete);
736 /*
737 * Create "initialize_domain" and "no_initialize_domain" entry
738 * in exception policy.
739 */
740 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
741 const bool is_delete);
742 /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
743 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
744 const bool is_delete);
745 /*
746 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
747 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
748 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
749 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
750 * "allow_link" entry in domain policy.
751 */
752 int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
753 const bool is_delete);
754 /* Create "allow_read" entry in exception policy. */
755 int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
756 /* Create "allow_mount" entry in domain policy. */
757 int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
758 const bool is_delete);
759 /* Create "deny_rewrite" entry in exception policy. */
760 int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
761 /* Create "file_pattern" entry in exception policy. */
762 int tomoyo_write_pattern_policy(char *data, const bool is_delete);
763 /* Create "path_group" entry in exception policy. */
764 int tomoyo_write_path_group_policy(char *data, const bool is_delete);
765 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
766 __attribute__ ((format(printf, 2, 3)));
767 /* Create "number_group" entry in exception policy. */
768 int tomoyo_write_number_group_policy(char *data, const bool is_delete);
769 /* Find a domain by the given name. */
770 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
771 /* Find or create a domain by the given name. */
772 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
773 domainname,
774 const u8 profile);
775 /* Allocate memory for "struct tomoyo_path_group". */
776 struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
777 struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
778
779 /* Check mode for specified functionality. */
780 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
781 const u8 index);
782 /* Fill in "struct tomoyo_path_info" members. */
783 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
784 /* Run policy loader when /sbin/init starts. */
785 void tomoyo_load_policy(const char *filename);
786
787 void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
788
789 /* Convert binary string to ascii string. */
790 char *tomoyo_encode(const char *str);
791
792 /*
793 * Returns realpath(3) of the given pathname but ignores chroot'ed root.
794 * These functions use kzalloc(), so the caller must call kfree()
795 * if these functions didn't return NULL.
796 */
797 char *tomoyo_realpath(const char *pathname);
798 /*
799 * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
800 */
801 char *tomoyo_realpath_nofollow(const char *pathname);
802 /* Same with tomoyo_realpath() except that the pathname is already solved. */
803 char *tomoyo_realpath_from_path(struct path *path);
804 /* Get patterned pathname. */
805 const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
806
807 /* Check memory quota. */
808 bool tomoyo_memory_ok(void *ptr);
809 void *tomoyo_commit_ok(void *data, const unsigned int size);
810
811 /*
812 * Keep the given name on the RAM.
813 * The RAM is shared, so NEVER try to modify or kfree() the returned name.
814 */
815 const struct tomoyo_path_info *tomoyo_get_name(const char *name);
816
817 /* Check for memory usage. */
818 int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
819
820 /* Set memory quota. */
821 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
822
823 /* Initialize mm related code. */
824 void __init tomoyo_mm_init(void);
825 int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
826 const struct tomoyo_path_info *filename);
827 int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
828 struct path *path, const int flag);
829 int tomoyo_path_number_perm(const u8 operation, struct path *path,
830 unsigned long number);
831 int tomoyo_path_number3_perm(const u8 operation, struct path *path,
832 const unsigned int mode, unsigned int dev);
833 int tomoyo_path_perm(const u8 operation, struct path *path);
834 int tomoyo_path2_perm(const u8 operation, struct path *path1,
835 struct path *path2);
836 int tomoyo_find_next_domain(struct linux_binprm *bprm);
837
838 void tomoyo_print_ulong(char *buffer, const int buffer_len,
839 const unsigned long value, const u8 type);
840
841 /* Drop refcount on tomoyo_name_union. */
842 void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
843
844 /* Run garbage collector. */
845 void tomoyo_run_gc(void);
846
847 void tomoyo_memory_free(void *ptr);
848
849 /********** External variable definitions. **********/
850
851 /* Lock for GC. */
852 extern struct srcu_struct tomoyo_ss;
853
854 /* The list for "struct tomoyo_domain_info". */
855 extern struct list_head tomoyo_domain_list;
856
857 extern struct list_head tomoyo_path_group_list;
858 extern struct list_head tomoyo_number_group_list;
859 extern struct list_head tomoyo_domain_initializer_list;
860 extern struct list_head tomoyo_domain_keeper_list;
861 extern struct list_head tomoyo_alias_list;
862 extern struct list_head tomoyo_globally_readable_list;
863 extern struct list_head tomoyo_pattern_list;
864 extern struct list_head tomoyo_no_rewrite_list;
865 extern struct list_head tomoyo_policy_manager_list;
866 extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
867
868 /* Lock for protecting policy. */
869 extern struct mutex tomoyo_policy_lock;
870
871 /* Has /sbin/init started? */
872 extern bool tomoyo_policy_loaded;
873
874 /* The kernel's domain. */
875 extern struct tomoyo_domain_info tomoyo_kernel_domain;
876
877 extern unsigned int tomoyo_quota_for_query;
878 extern unsigned int tomoyo_query_memory_size;
879
880 /********** Inlined functions. **********/
881
882 static inline int tomoyo_read_lock(void)
883 {
884 return srcu_read_lock(&tomoyo_ss);
885 }
886
887 static inline void tomoyo_read_unlock(int idx)
888 {
889 srcu_read_unlock(&tomoyo_ss, idx);
890 }
891
892 /* strcmp() for "struct tomoyo_path_info" structure. */
893 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
894 const struct tomoyo_path_info *b)
895 {
896 return a->hash != b->hash || strcmp(a->name, b->name);
897 }
898
899 /**
900 * tomoyo_is_valid - Check whether the character is a valid char.
901 *
902 * @c: The character to check.
903 *
904 * Returns true if @c is a valid character, false otherwise.
905 */
906 static inline bool tomoyo_is_valid(const unsigned char c)
907 {
908 return c > ' ' && c < 127;
909 }
910
911 /**
912 * tomoyo_is_invalid - Check whether the character is an invalid char.
913 *
914 * @c: The character to check.
915 *
916 * Returns true if @c is an invalid character, false otherwise.
917 */
918 static inline bool tomoyo_is_invalid(const unsigned char c)
919 {
920 return c && (c <= ' ' || c >= 127);
921 }
922
923 static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
924 {
925 if (name) {
926 struct tomoyo_name_entry *ptr =
927 container_of(name, struct tomoyo_name_entry, entry);
928 atomic_dec(&ptr->users);
929 }
930 }
931
932 static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
933 {
934 if (group)
935 atomic_dec(&group->users);
936 }
937
938 static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
939 {
940 if (group)
941 atomic_dec(&group->users);
942 }
943
944 static inline struct tomoyo_domain_info *tomoyo_domain(void)
945 {
946 return current_cred()->security;
947 }
948
949 static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
950 *task)
951 {
952 return task_cred_xxx(task, security);
953 }
954
955 static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
956 const struct tomoyo_acl_info *p2)
957 {
958 return p1->type == p2->type;
959 }
960
961 static inline bool tomoyo_is_same_name_union
962 (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
963 {
964 return p1->filename == p2->filename && p1->group == p2->group &&
965 p1->is_group == p2->is_group;
966 }
967
968 static inline bool tomoyo_is_same_number_union
969 (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
970 {
971 return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
972 && p1->group == p2->group && p1->min_type == p2->min_type &&
973 p1->max_type == p2->max_type && p1->is_group == p2->is_group;
974 }
975
976 static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
977 const struct tomoyo_path_acl *p2)
978 {
979 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
980 tomoyo_is_same_name_union(&p1->name, &p2->name);
981 }
982
983 static inline bool tomoyo_is_same_path_number3_acl
984 (const struct tomoyo_path_number3_acl *p1,
985 const struct tomoyo_path_number3_acl *p2)
986 {
987 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
988 && tomoyo_is_same_name_union(&p1->name, &p2->name)
989 && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
990 && tomoyo_is_same_number_union(&p1->major, &p2->major)
991 && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
992 }
993
994
995 static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
996 const struct tomoyo_path2_acl *p2)
997 {
998 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
999 tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
1000 tomoyo_is_same_name_union(&p1->name2, &p2->name2);
1001 }
1002
1003 static inline bool tomoyo_is_same_path_number_acl
1004 (const struct tomoyo_path_number_acl *p1,
1005 const struct tomoyo_path_number_acl *p2)
1006 {
1007 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1008 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1009 && tomoyo_is_same_number_union(&p1->number, &p2->number);
1010 }
1011
1012 static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1013 const struct tomoyo_mount_acl *p2)
1014 {
1015 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1016 tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
1017 tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
1018 tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
1019 tomoyo_is_same_number_union(&p1->flags, &p2->flags);
1020 }
1021
1022 static inline bool tomoyo_is_same_domain_initializer_entry
1023 (const struct tomoyo_domain_initializer_entry *p1,
1024 const struct tomoyo_domain_initializer_entry *p2)
1025 {
1026 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1027 && p1->domainname == p2->domainname
1028 && p1->program == p2->program;
1029 }
1030
1031 static inline bool tomoyo_is_same_domain_keeper_entry
1032 (const struct tomoyo_domain_keeper_entry *p1,
1033 const struct tomoyo_domain_keeper_entry *p2)
1034 {
1035 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1036 && p1->domainname == p2->domainname
1037 && p1->program == p2->program;
1038 }
1039
1040 static inline bool tomoyo_is_same_alias_entry
1041 (const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1042 {
1043 return p1->original_name == p2->original_name &&
1044 p1->aliased_name == p2->aliased_name;
1045 }
1046
1047 /**
1048 * list_for_each_cookie - iterate over a list with cookie.
1049 * @pos: the &struct list_head to use as a loop cursor.
1050 * @cookie: the &struct list_head to use as a cookie.
1051 * @head: the head for your list.
1052 *
1053 * Same with list_for_each_rcu() except that this primitive uses @cookie
1054 * so that we can continue iteration.
1055 * @cookie must be NULL when iteration starts, and @cookie will become
1056 * NULL when iteration finishes.
1057 */
1058 #define list_for_each_cookie(pos, cookie, head) \
1059 for (({ if (!cookie) \
1060 cookie = head; }), \
1061 pos = rcu_dereference((cookie)->next); \
1062 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
1063 (cookie) = pos, pos = rcu_dereference(pos->next))
1064
1065 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
This page took 0.05551 seconds and 6 git commands to generate.