TOMOYO: Cleanup part 4.
[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 /* Group number is an integer between 0 and 255. */
42 #define TOMOYO_MAX_ACL_GROUPS 256
43
44 /* Index numbers for operation mode. */
45 enum tomoyo_mode_index {
46 TOMOYO_CONFIG_DISABLED,
47 TOMOYO_CONFIG_LEARNING,
48 TOMOYO_CONFIG_PERMISSIVE,
49 TOMOYO_CONFIG_ENFORCING,
50 TOMOYO_CONFIG_MAX_MODE,
51 TOMOYO_CONFIG_WANT_REJECT_LOG = 64,
52 TOMOYO_CONFIG_WANT_GRANT_LOG = 128,
53 TOMOYO_CONFIG_USE_DEFAULT = 255,
54 };
55
56 /* Index numbers for entry type. */
57 enum tomoyo_policy_id {
58 TOMOYO_ID_GROUP,
59 TOMOYO_ID_PATH_GROUP,
60 TOMOYO_ID_NUMBER_GROUP,
61 TOMOYO_ID_TRANSITION_CONTROL,
62 TOMOYO_ID_AGGREGATOR,
63 TOMOYO_ID_MANAGER,
64 TOMOYO_ID_NAME,
65 TOMOYO_ID_ACL,
66 TOMOYO_ID_DOMAIN,
67 TOMOYO_MAX_POLICY
68 };
69
70 /* Index numbers for domain's attributes. */
71 enum tomoyo_domain_info_flags_index {
72 /* Quota warnning flag. */
73 TOMOYO_DIF_QUOTA_WARNED,
74 /*
75 * This domain was unable to create a new domain at
76 * tomoyo_find_next_domain() because the name of the domain to be
77 * created was too long or it could not allocate memory.
78 * More than one process continued execve() without domain transition.
79 */
80 TOMOYO_DIF_TRANSITION_FAILED,
81 TOMOYO_MAX_DOMAIN_INFO_FLAGS
82 };
83
84 /* Index numbers for group entries. */
85 enum tomoyo_group_id {
86 TOMOYO_PATH_GROUP,
87 TOMOYO_NUMBER_GROUP,
88 TOMOYO_MAX_GROUP
89 };
90
91 /* Index numbers for type of numeric values. */
92 enum tomoyo_value_type {
93 TOMOYO_VALUE_TYPE_INVALID,
94 TOMOYO_VALUE_TYPE_DECIMAL,
95 TOMOYO_VALUE_TYPE_OCTAL,
96 TOMOYO_VALUE_TYPE_HEXADECIMAL,
97 };
98
99 /* Index numbers for domain transition control keywords. */
100 enum tomoyo_transition_type {
101 /* Do not change this order, */
102 TOMOYO_TRANSITION_CONTROL_NO_RESET,
103 TOMOYO_TRANSITION_CONTROL_RESET,
104 TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
105 TOMOYO_TRANSITION_CONTROL_INITIALIZE,
106 TOMOYO_TRANSITION_CONTROL_NO_KEEP,
107 TOMOYO_TRANSITION_CONTROL_KEEP,
108 TOMOYO_MAX_TRANSITION_TYPE
109 };
110
111 /* Index numbers for Access Controls. */
112 enum tomoyo_acl_entry_type_index {
113 TOMOYO_TYPE_PATH_ACL,
114 TOMOYO_TYPE_PATH2_ACL,
115 TOMOYO_TYPE_PATH_NUMBER_ACL,
116 TOMOYO_TYPE_MKDEV_ACL,
117 TOMOYO_TYPE_MOUNT_ACL,
118 };
119
120 /* Index numbers for access controls with one pathname. */
121 enum tomoyo_path_acl_index {
122 TOMOYO_TYPE_EXECUTE,
123 TOMOYO_TYPE_READ,
124 TOMOYO_TYPE_WRITE,
125 TOMOYO_TYPE_APPEND,
126 TOMOYO_TYPE_UNLINK,
127 TOMOYO_TYPE_GETATTR,
128 TOMOYO_TYPE_RMDIR,
129 TOMOYO_TYPE_TRUNCATE,
130 TOMOYO_TYPE_SYMLINK,
131 TOMOYO_TYPE_CHROOT,
132 TOMOYO_TYPE_UMOUNT,
133 TOMOYO_MAX_PATH_OPERATION
134 };
135
136 enum tomoyo_memory_stat_type {
137 TOMOYO_MEMORY_POLICY,
138 TOMOYO_MEMORY_AUDIT,
139 TOMOYO_MEMORY_QUERY,
140 TOMOYO_MAX_MEMORY_STAT
141 };
142
143 enum tomoyo_mkdev_acl_index {
144 TOMOYO_TYPE_MKBLOCK,
145 TOMOYO_TYPE_MKCHAR,
146 TOMOYO_MAX_MKDEV_OPERATION
147 };
148
149 /* Index numbers for access controls with two pathnames. */
150 enum tomoyo_path2_acl_index {
151 TOMOYO_TYPE_LINK,
152 TOMOYO_TYPE_RENAME,
153 TOMOYO_TYPE_PIVOT_ROOT,
154 TOMOYO_MAX_PATH2_OPERATION
155 };
156
157 /* Index numbers for access controls with one pathname and one number. */
158 enum tomoyo_path_number_acl_index {
159 TOMOYO_TYPE_CREATE,
160 TOMOYO_TYPE_MKDIR,
161 TOMOYO_TYPE_MKFIFO,
162 TOMOYO_TYPE_MKSOCK,
163 TOMOYO_TYPE_IOCTL,
164 TOMOYO_TYPE_CHMOD,
165 TOMOYO_TYPE_CHOWN,
166 TOMOYO_TYPE_CHGRP,
167 TOMOYO_MAX_PATH_NUMBER_OPERATION
168 };
169
170 /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
171 enum tomoyo_securityfs_interface_index {
172 TOMOYO_DOMAINPOLICY,
173 TOMOYO_EXCEPTIONPOLICY,
174 TOMOYO_DOMAIN_STATUS,
175 TOMOYO_PROCESS_STATUS,
176 TOMOYO_MEMINFO,
177 TOMOYO_SELFDOMAIN,
178 TOMOYO_AUDIT,
179 TOMOYO_VERSION,
180 TOMOYO_PROFILE,
181 TOMOYO_QUERY,
182 TOMOYO_MANAGER
183 };
184
185 /* Index numbers for special mount operations. */
186 enum tomoyo_special_mount {
187 TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
188 TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
189 TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
190 TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
191 TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
192 TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
193 TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
194 TOMOYO_MAX_SPECIAL_MOUNT
195 };
196
197 /* Index numbers for functionality. */
198 enum tomoyo_mac_index {
199 TOMOYO_MAC_FILE_EXECUTE,
200 TOMOYO_MAC_FILE_OPEN,
201 TOMOYO_MAC_FILE_CREATE,
202 TOMOYO_MAC_FILE_UNLINK,
203 TOMOYO_MAC_FILE_GETATTR,
204 TOMOYO_MAC_FILE_MKDIR,
205 TOMOYO_MAC_FILE_RMDIR,
206 TOMOYO_MAC_FILE_MKFIFO,
207 TOMOYO_MAC_FILE_MKSOCK,
208 TOMOYO_MAC_FILE_TRUNCATE,
209 TOMOYO_MAC_FILE_SYMLINK,
210 TOMOYO_MAC_FILE_MKBLOCK,
211 TOMOYO_MAC_FILE_MKCHAR,
212 TOMOYO_MAC_FILE_LINK,
213 TOMOYO_MAC_FILE_RENAME,
214 TOMOYO_MAC_FILE_CHMOD,
215 TOMOYO_MAC_FILE_CHOWN,
216 TOMOYO_MAC_FILE_CHGRP,
217 TOMOYO_MAC_FILE_IOCTL,
218 TOMOYO_MAC_FILE_CHROOT,
219 TOMOYO_MAC_FILE_MOUNT,
220 TOMOYO_MAC_FILE_UMOUNT,
221 TOMOYO_MAC_FILE_PIVOT_ROOT,
222 TOMOYO_MAX_MAC_INDEX
223 };
224
225 /* Index numbers for category of functionality. */
226 enum tomoyo_mac_category_index {
227 TOMOYO_MAC_CATEGORY_FILE,
228 TOMOYO_MAX_MAC_CATEGORY_INDEX
229 };
230
231 /*
232 * Retry this request. Returned by tomoyo_supervisor() if policy violation has
233 * occurred in enforcing mode and the userspace daemon decided to retry.
234 *
235 * We must choose a positive value in order to distinguish "granted" (which is
236 * 0) and "rejected" (which is a negative value) and "retry".
237 */
238 #define TOMOYO_RETRY_REQUEST 1
239
240 /* Index numbers for profile's PREFERENCE values. */
241 enum tomoyo_pref_index {
242 TOMOYO_PREF_MAX_AUDIT_LOG,
243 TOMOYO_PREF_MAX_LEARNING_ENTRY,
244 TOMOYO_MAX_PREF
245 };
246
247 /********** Structure definitions. **********/
248
249 /* Common header for holding ACL entries. */
250 struct tomoyo_acl_head {
251 struct list_head list;
252 bool is_deleted;
253 } __packed;
254
255 /* Common header for shared entries. */
256 struct tomoyo_shared_acl_head {
257 struct list_head list;
258 atomic_t users;
259 } __packed;
260
261 struct tomoyo_policy_namespace;
262
263 /* Structure for request info. */
264 struct tomoyo_request_info {
265 struct tomoyo_domain_info *domain;
266 /* For holding parameters. */
267 union {
268 struct {
269 const struct tomoyo_path_info *filename;
270 /* For using wildcards at tomoyo_find_next_domain(). */
271 const struct tomoyo_path_info *matched_path;
272 /* One of values in "enum tomoyo_path_acl_index". */
273 u8 operation;
274 } path;
275 struct {
276 const struct tomoyo_path_info *filename1;
277 const struct tomoyo_path_info *filename2;
278 /* One of values in "enum tomoyo_path2_acl_index". */
279 u8 operation;
280 } path2;
281 struct {
282 const struct tomoyo_path_info *filename;
283 unsigned int mode;
284 unsigned int major;
285 unsigned int minor;
286 /* One of values in "enum tomoyo_mkdev_acl_index". */
287 u8 operation;
288 } mkdev;
289 struct {
290 const struct tomoyo_path_info *filename;
291 unsigned long number;
292 /*
293 * One of values in
294 * "enum tomoyo_path_number_acl_index".
295 */
296 u8 operation;
297 } path_number;
298 struct {
299 const struct tomoyo_path_info *type;
300 const struct tomoyo_path_info *dir;
301 const struct tomoyo_path_info *dev;
302 unsigned long flags;
303 int need_dev;
304 } mount;
305 } param;
306 u8 param_type;
307 bool granted;
308 u8 retry;
309 u8 profile;
310 u8 mode; /* One of tomoyo_mode_index . */
311 u8 type;
312 };
313
314 /* Structure for holding a token. */
315 struct tomoyo_path_info {
316 const char *name;
317 u32 hash; /* = full_name_hash(name, strlen(name)) */
318 u16 const_len; /* = tomoyo_const_part_length(name) */
319 bool is_dir; /* = tomoyo_strendswith(name, "/") */
320 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
321 };
322
323 /* Structure for holding string data. */
324 struct tomoyo_name {
325 struct tomoyo_shared_acl_head head;
326 struct tomoyo_path_info entry;
327 };
328
329 /* Structure for holding a word. */
330 struct tomoyo_name_union {
331 /* Either @filename or @group is NULL. */
332 const struct tomoyo_path_info *filename;
333 struct tomoyo_group *group;
334 };
335
336 /* Structure for holding a number. */
337 struct tomoyo_number_union {
338 unsigned long values[2];
339 struct tomoyo_group *group; /* Maybe NULL. */
340 /* One of values in "enum tomoyo_value_type". */
341 u8 value_type[2];
342 };
343
344 /* Structure for "path_group"/"number_group" directive. */
345 struct tomoyo_group {
346 struct tomoyo_shared_acl_head head;
347 const struct tomoyo_path_info *group_name;
348 struct list_head member_list;
349 };
350
351 /* Structure for "path_group" directive. */
352 struct tomoyo_path_group {
353 struct tomoyo_acl_head head;
354 const struct tomoyo_path_info *member_name;
355 };
356
357 /* Structure for "number_group" directive. */
358 struct tomoyo_number_group {
359 struct tomoyo_acl_head head;
360 struct tomoyo_number_union number;
361 };
362
363 /* Common header for individual entries. */
364 struct tomoyo_acl_info {
365 struct list_head list;
366 bool is_deleted;
367 u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
368 } __packed;
369
370 /* Structure for domain information. */
371 struct tomoyo_domain_info {
372 struct list_head list;
373 struct list_head acl_info_list;
374 /* Name of this domain. Never NULL. */
375 const struct tomoyo_path_info *domainname;
376 /* Namespace for this domain. Never NULL. */
377 struct tomoyo_policy_namespace *ns;
378 u8 profile; /* Profile number to use. */
379 u8 group; /* Group number to use. */
380 bool is_deleted; /* Delete flag. */
381 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
382 atomic_t users; /* Number of referring credentials. */
383 };
384
385 /*
386 * Structure for "file execute", "file read", "file write", "file append",
387 * "file unlink", "file getattr", "file rmdir", "file truncate",
388 * "file symlink", "file chroot" and "file unmount" directive.
389 */
390 struct tomoyo_path_acl {
391 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
392 u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
393 struct tomoyo_name_union name;
394 };
395
396 /*
397 * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
398 * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
399 */
400 struct tomoyo_path_number_acl {
401 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
402 /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
403 u8 perm;
404 struct tomoyo_name_union name;
405 struct tomoyo_number_union number;
406 };
407
408 /* Structure for "file mkblock" and "file mkchar" directive. */
409 struct tomoyo_mkdev_acl {
410 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
411 u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
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
418 /*
419 * Structure for "file rename", "file link" and "file pivot_root" directive.
420 */
421 struct tomoyo_path2_acl {
422 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
423 u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
424 struct tomoyo_name_union name1;
425 struct tomoyo_name_union name2;
426 };
427
428 /* Structure for "file mount" directive. */
429 struct tomoyo_mount_acl {
430 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
431 struct tomoyo_name_union dev_name;
432 struct tomoyo_name_union dir_name;
433 struct tomoyo_name_union fs_type;
434 struct tomoyo_number_union flags;
435 };
436
437 /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
438 struct tomoyo_acl_param {
439 char *data;
440 struct list_head *list;
441 struct tomoyo_policy_namespace *ns;
442 bool is_delete;
443 };
444
445 #define TOMOYO_MAX_IO_READ_QUEUE 64
446
447 /*
448 * Structure for reading/writing policy via /sys/kernel/security/tomoyo
449 * interfaces.
450 */
451 struct tomoyo_io_buffer {
452 void (*read) (struct tomoyo_io_buffer *);
453 int (*write) (struct tomoyo_io_buffer *);
454 int (*poll) (struct file *file, poll_table *wait);
455 /* Exclusive lock for this structure. */
456 struct mutex io_sem;
457 char __user *read_user_buf;
458 size_t read_user_buf_avail;
459 struct {
460 struct list_head *ns;
461 struct list_head *domain;
462 struct list_head *group;
463 struct list_head *acl;
464 size_t avail;
465 unsigned int step;
466 unsigned int query_index;
467 u16 index;
468 u8 acl_group_index;
469 u8 bit;
470 u8 w_pos;
471 bool eof;
472 bool print_this_domain_only;
473 bool print_transition_related_only;
474 const char *w[TOMOYO_MAX_IO_READ_QUEUE];
475 } r;
476 struct {
477 struct tomoyo_policy_namespace *ns;
478 /* The position currently writing to. */
479 struct tomoyo_domain_info *domain;
480 /* Bytes available for writing. */
481 size_t avail;
482 bool is_delete;
483 } w;
484 /* Buffer for reading. */
485 char *read_buf;
486 /* Size of read buffer. */
487 size_t readbuf_size;
488 /* Buffer for writing. */
489 char *write_buf;
490 /* Size of write buffer. */
491 size_t writebuf_size;
492 /* Type of this interface. */
493 enum tomoyo_securityfs_interface_index type;
494 /* Users counter protected by tomoyo_io_buffer_list_lock. */
495 u8 users;
496 /* List for telling GC not to kfree() elements. */
497 struct list_head list;
498 };
499
500 /*
501 * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
502 * "no_keep_domain" keyword.
503 */
504 struct tomoyo_transition_control {
505 struct tomoyo_acl_head head;
506 u8 type; /* One of values in "enum tomoyo_transition_type". */
507 /* True if the domainname is tomoyo_get_last_name(). */
508 bool is_last_name;
509 const struct tomoyo_path_info *domainname; /* Maybe NULL */
510 const struct tomoyo_path_info *program; /* Maybe NULL */
511 };
512
513 /* Structure for "aggregator" keyword. */
514 struct tomoyo_aggregator {
515 struct tomoyo_acl_head head;
516 const struct tomoyo_path_info *original_name;
517 const struct tomoyo_path_info *aggregated_name;
518 };
519
520 /* Structure for policy manager. */
521 struct tomoyo_manager {
522 struct tomoyo_acl_head head;
523 bool is_domain; /* True if manager is a domainname. */
524 /* A path to program or a domainname. */
525 const struct tomoyo_path_info *manager;
526 };
527
528 struct tomoyo_preference {
529 unsigned int learning_max_entry;
530 bool enforcing_verbose;
531 bool learning_verbose;
532 bool permissive_verbose;
533 };
534
535 /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
536 struct tomoyo_profile {
537 const struct tomoyo_path_info *comment;
538 struct tomoyo_preference *learning;
539 struct tomoyo_preference *permissive;
540 struct tomoyo_preference *enforcing;
541 struct tomoyo_preference preference;
542 u8 default_config;
543 u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
544 unsigned int pref[TOMOYO_MAX_PREF];
545 };
546
547 /* Structure for representing YYYY/MM/DD hh/mm/ss. */
548 struct tomoyo_time {
549 u16 year;
550 u8 month;
551 u8 day;
552 u8 hour;
553 u8 min;
554 u8 sec;
555 };
556
557 /* Structure for policy namespace. */
558 struct tomoyo_policy_namespace {
559 /* Profile table. Memory is allocated as needed. */
560 struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
561 /* List of "struct tomoyo_group". */
562 struct list_head group_list[TOMOYO_MAX_GROUP];
563 /* List of policy. */
564 struct list_head policy_list[TOMOYO_MAX_POLICY];
565 /* The global ACL referred by "use_group" keyword. */
566 struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
567 /* List for connecting to tomoyo_namespace_list list. */
568 struct list_head namespace_list;
569 /* Profile version. Currently only 20100903 is defined. */
570 unsigned int profile_version;
571 /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
572 const char *name;
573 };
574
575 /********** Function prototypes. **********/
576
577 void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
578 bool tomoyo_str_starts(char **src, const char *find);
579 const char *tomoyo_get_exe(void);
580 void tomoyo_normalize_line(unsigned char *buffer);
581 void tomoyo_check_profile(void);
582 int tomoyo_open_control(const u8 type, struct file *file);
583 int tomoyo_close_control(struct tomoyo_io_buffer *head);
584 int tomoyo_poll_control(struct file *file, poll_table *wait);
585 ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
586 const int buffer_len);
587 ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
588 const char __user *buffer, const int buffer_len);
589 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
590 void tomoyo_warn_oom(const char *function);
591 const struct tomoyo_path_info *
592 tomoyo_compare_name_union(const struct tomoyo_path_info *name,
593 const struct tomoyo_name_union *ptr);
594 bool tomoyo_compare_number_union(const unsigned long value,
595 const struct tomoyo_number_union *ptr);
596 int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
597 const u8 index);
598 void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
599 __attribute__ ((format(printf, 2, 3)));
600 bool tomoyo_correct_domain(const unsigned char *domainname);
601 bool tomoyo_correct_path(const char *filename);
602 bool tomoyo_correct_word(const char *string);
603 bool tomoyo_domain_def(const unsigned char *buffer);
604 bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
605 struct tomoyo_name_union *ptr);
606 const struct tomoyo_path_info *
607 tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
608 const struct tomoyo_group *group);
609 bool tomoyo_number_matches_group(const unsigned long min,
610 const unsigned long max,
611 const struct tomoyo_group *group);
612 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
613 const struct tomoyo_path_info *pattern);
614 bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
615 struct tomoyo_number_union *ptr);
616 bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
617 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
618 int tomoyo_init_request_info(struct tomoyo_request_info *r,
619 struct tomoyo_domain_info *domain,
620 const u8 index);
621 int tomoyo_mount_permission(char *dev_name, struct path *path,
622 const char *type, unsigned long flags,
623 void *data_page);
624 int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
625 int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
626 const u8 type);
627 int tomoyo_write_file(struct tomoyo_acl_param *param);
628 int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
629 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
630 __attribute__ ((format(printf, 2, 3)));
631 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
632 struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
633 const bool transit);
634 struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
635 const u8 profile);
636 struct tomoyo_policy_namespace *tomoyo_assign_namespace
637 (const char *domainname);
638 struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
639 const u8 idx);
640 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
641 const u8 index);
642 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
643 void tomoyo_load_policy(const char *filename);
644 void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
645 char *tomoyo_encode(const char *str);
646 char *tomoyo_realpath_nofollow(const char *pathname);
647 char *tomoyo_realpath_from_path(struct path *path);
648 bool tomoyo_memory_ok(void *ptr);
649 void *tomoyo_commit_ok(void *data, const unsigned int size);
650 const struct tomoyo_path_info *tomoyo_get_name(const char *name);
651 void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
652 int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
653 void __init tomoyo_mm_init(void);
654 int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
655 const struct tomoyo_path_info *filename);
656 int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
657 struct path *path, const int flag);
658 int tomoyo_path_number_perm(const u8 operation, struct path *path,
659 unsigned long number);
660 int tomoyo_mkdev_perm(const u8 operation, struct path *path,
661 const unsigned int mode, unsigned int dev);
662 int tomoyo_path_perm(const u8 operation, struct path *path);
663 int tomoyo_path2_perm(const u8 operation, struct path *path1,
664 struct path *path2);
665 int tomoyo_find_next_domain(struct linux_binprm *bprm);
666 void tomoyo_print_ulong(char *buffer, const int buffer_len,
667 const unsigned long value, const u8 type);
668 void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
669 void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
670 void tomoyo_memory_free(void *ptr);
671 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
672 struct tomoyo_acl_param *param,
673 bool (*check_duplicate) (const struct tomoyo_acl_info
674 *,
675 const struct tomoyo_acl_info
676 *),
677 bool (*merge_duplicate) (struct tomoyo_acl_info *,
678 struct tomoyo_acl_info *,
679 const bool));
680 int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
681 struct tomoyo_acl_param *param,
682 bool (*check_duplicate) (const struct tomoyo_acl_head
683 *,
684 const struct tomoyo_acl_head
685 *));
686 void tomoyo_check_acl(struct tomoyo_request_info *r,
687 bool (*check_entry) (struct tomoyo_request_info *,
688 const struct tomoyo_acl_info *));
689 char *tomoyo_read_token(struct tomoyo_acl_param *param);
690 bool tomoyo_permstr(const char *string, const char *keyword);
691
692 const char *tomoyo_yesno(const unsigned int value);
693 void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
694 __attribute__ ((format(printf, 2, 3)));
695 void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
696 va_list args);
697 void tomoyo_read_log(struct tomoyo_io_buffer *head);
698 int tomoyo_poll_log(struct file *file, poll_table *wait);
699 char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
700 va_list args);
701
702 /********** External variable definitions. **********/
703
704 /* Lock for GC. */
705 extern struct srcu_struct tomoyo_ss;
706
707 /* The list for "struct tomoyo_domain_info". */
708 extern struct list_head tomoyo_domain_list;
709
710 extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
711
712 /* Lock for protecting policy. */
713 extern struct mutex tomoyo_policy_lock;
714
715 /* Has /sbin/init started? */
716 extern bool tomoyo_policy_loaded;
717
718 /* The kernel's domain. */
719 extern struct tomoyo_domain_info tomoyo_kernel_domain;
720 extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
721 extern struct list_head tomoyo_namespace_list;
722
723 extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX +
724 TOMOYO_MAX_MAC_CATEGORY_INDEX];
725 extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
726 extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
727
728
729 extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
730 extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
731 extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
732
733 extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
734 extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
735 extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
736 extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
737
738 /********** Inlined functions. **********/
739
740 /**
741 * tomoyo_read_lock - Take lock for protecting policy.
742 *
743 * Returns index number for tomoyo_read_unlock().
744 */
745 static inline int tomoyo_read_lock(void)
746 {
747 return srcu_read_lock(&tomoyo_ss);
748 }
749
750 /**
751 * tomoyo_read_unlock - Release lock for protecting policy.
752 *
753 * @idx: Index number returned by tomoyo_read_lock().
754 *
755 * Returns nothing.
756 */
757 static inline void tomoyo_read_unlock(int idx)
758 {
759 srcu_read_unlock(&tomoyo_ss, idx);
760 }
761
762 /**
763 * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
764 *
765 * @a: Pointer to "struct tomoyo_path_info".
766 * @b: Pointer to "struct tomoyo_path_info".
767 *
768 * Returns true if @a == @b, false otherwise.
769 */
770 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
771 const struct tomoyo_path_info *b)
772 {
773 return a->hash != b->hash || strcmp(a->name, b->name);
774 }
775
776 /**
777 * tomoyo_put_name - Drop reference on "struct tomoyo_name".
778 *
779 * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
780 *
781 * Returns nothing.
782 */
783 static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
784 {
785 if (name) {
786 struct tomoyo_name *ptr =
787 container_of(name, typeof(*ptr), entry);
788 atomic_dec(&ptr->head.users);
789 }
790 }
791
792 /**
793 * tomoyo_put_group - Drop reference on "struct tomoyo_group".
794 *
795 * @group: Pointer to "struct tomoyo_group". Maybe NULL.
796 *
797 * Returns nothing.
798 */
799 static inline void tomoyo_put_group(struct tomoyo_group *group)
800 {
801 if (group)
802 atomic_dec(&group->head.users);
803 }
804
805 /**
806 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
807 *
808 * Returns pointer to "struct tomoyo_domain_info" for current thread.
809 */
810 static inline struct tomoyo_domain_info *tomoyo_domain(void)
811 {
812 return current_cred()->security;
813 }
814
815 /**
816 * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
817 *
818 * @task: Pointer to "struct task_struct".
819 *
820 * Returns pointer to "struct tomoyo_security" for specified thread.
821 */
822 static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
823 *task)
824 {
825 return task_cred_xxx(task, security);
826 }
827
828 /**
829 * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
830 *
831 * @a: Pointer to "struct tomoyo_name_union".
832 * @b: Pointer to "struct tomoyo_name_union".
833 *
834 * Returns true if @a == @b, false otherwise.
835 */
836 static inline bool tomoyo_same_name_union
837 (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
838 {
839 return a->filename == b->filename && a->group == b->group;
840 }
841
842 /**
843 * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
844 *
845 * @a: Pointer to "struct tomoyo_number_union".
846 * @b: Pointer to "struct tomoyo_number_union".
847 *
848 * Returns true if @a == @b, false otherwise.
849 */
850 static inline bool tomoyo_same_number_union
851 (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
852 {
853 return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
854 a->group == b->group && a->value_type[0] == b->value_type[0] &&
855 a->value_type[1] == b->value_type[1];
856 }
857
858 /**
859 * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
860 *
861 * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
862 */
863 static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
864 {
865 return tomoyo_domain()->ns;
866 }
867
868 #if defined(CONFIG_SLOB)
869
870 /**
871 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
872 *
873 * @size: Size to be rounded up.
874 *
875 * Returns @size.
876 *
877 * Since SLOB does not round up, this function simply returns @size.
878 */
879 static inline int tomoyo_round2(size_t size)
880 {
881 return size;
882 }
883
884 #else
885
886 /**
887 * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
888 *
889 * @size: Size to be rounded up.
890 *
891 * Returns rounded size.
892 *
893 * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
894 * (e.g.) 128 bytes.
895 */
896 static inline int tomoyo_round2(size_t size)
897 {
898 #if PAGE_SIZE == 4096
899 size_t bsize = 32;
900 #else
901 size_t bsize = 64;
902 #endif
903 if (!size)
904 return 0;
905 while (size > bsize)
906 bsize <<= 1;
907 return bsize;
908 }
909
910 #endif
911
912 /**
913 * list_for_each_cookie - iterate over a list with cookie.
914 * @pos: the &struct list_head to use as a loop cursor.
915 * @head: the head for your list.
916 */
917 #define list_for_each_cookie(pos, head) \
918 if (!pos) \
919 pos = srcu_dereference((head)->next, &tomoyo_ss); \
920 for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
921
922 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */
This page took 0.049894 seconds and 6 git commands to generate.