]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/common.h
TOMOYO: Add rest of file operation restrictions.
[net-next-2.6.git] / security / tomoyo / common.h
CommitLineData
9590837b
KT
1/*
2 * security/tomoyo/common.h
3 *
4 * Common functions for TOMOYO.
5 *
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 *
39826a1e 8 * Version: 2.2.0 2009/04/01
9590837b
KT
9 *
10 */
11
12#ifndef _SECURITY_TOMOYO_COMMON_H
13#define _SECURITY_TOMOYO_COMMON_H
14
15#include <linux/ctype.h>
16#include <linux/string.h>
17#include <linux/mm.h>
18#include <linux/file.h>
19#include <linux/kmod.h>
20#include <linux/fs.h>
21#include <linux/sched.h>
22#include <linux/namei.h>
23#include <linux/mount.h>
24#include <linux/list.h>
25
26struct dentry;
27struct vfsmount;
28
c3fa109a
TH
29/*
30 * tomoyo_page_buffer is a structure which is used for holding a pathname
31 * obtained from "struct dentry" and "struct vfsmount" pair.
32 * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
33 * (because TOMOYO escapes non ASCII printable characters using \ooo format),
34 * we will make the buffer larger.
35 */
9590837b
KT
36struct tomoyo_page_buffer {
37 char buffer[4096];
38};
39
c3fa109a
TH
40/*
41 * tomoyo_path_info is a structure which is used for holding a string data
42 * used by TOMOYO.
43 * This structure has several fields for supporting pattern matching.
44 *
45 * (1) "name" is the '\0' terminated string data.
46 * (2) "hash" is full_name_hash(name, strlen(name)).
47 * This allows tomoyo_pathcmp() to compare by hash before actually compare
48 * using strcmp().
49 * (3) "const_len" is the length of the initial segment of "name" which
50 * consists entirely of non wildcard characters. In other words, the length
51 * which we can compare two strings using strncmp().
52 * (4) "is_dir" is a bool which is true if "name" ends with "/",
53 * false otherwise.
54 * TOMOYO distinguishes directory and non-directory. A directory ends with
55 * "/" and non-directory does not end with "/".
56 * (5) "is_patterned" is a bool which is true if "name" contains wildcard
57 * characters, false otherwise. This allows TOMOYO to use "hash" and
58 * strcmp() for string comparison if "is_patterned" is false.
c3fa109a 59 */
9590837b
KT
60struct tomoyo_path_info {
61 const char *name;
62 u32 hash; /* = full_name_hash(name, strlen(name)) */
9590837b
KT
63 u16 const_len; /* = tomoyo_const_part_length(name) */
64 bool is_dir; /* = tomoyo_strendswith(name, "/") */
65 bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
9590837b
KT
66};
67
68/*
69 * This is the max length of a token.
70 *
71 * A token consists of only ASCII printable characters.
72 * Non printable characters in a token is represented in \ooo style
73 * octal string. Thus, \ itself is represented as \\.
74 */
75#define TOMOYO_MAX_PATHNAME_LEN 4000
76
c3fa109a
TH
77/*
78 * tomoyo_path_info_with_data is a structure which is used for holding a
79 * pathname obtained from "struct dentry" and "struct vfsmount" pair.
80 *
81 * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
82 * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
83 * buffer for the pathname only.
84 *
85 * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
86 * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
87 * so that we don't need to return two pointers to the caller. If the caller
88 * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
89 * "struct tomoyo_path_info_with_data".
90 */
9590837b
KT
91struct tomoyo_path_info_with_data {
92 /* Keep "head" first, for this pointer is passed to tomoyo_free(). */
93 struct tomoyo_path_info head;
a106cbfd 94 char barrier1[16]; /* Safeguard for overrun. */
9590837b
KT
95 char body[TOMOYO_MAX_PATHNAME_LEN];
96 char barrier2[16]; /* Safeguard for overrun. */
97};
98
99/*
c3fa109a
TH
100 * tomoyo_acl_info is a structure which is used for holding
101 *
102 * (1) "list" which is linked to the ->acl_info_list of
103 * "struct tomoyo_domain_info"
104 * (2) "type" which tells
105 * (a) type & 0x7F : type of the entry (either
106 * "struct tomoyo_single_path_acl_record" or
107 * "struct tomoyo_double_path_acl_record")
108 * (b) type & 0x80 : whether the entry is marked as "deleted".
9590837b
KT
109 *
110 * Packing "struct tomoyo_acl_info" allows
937bf613 111 * "struct tomoyo_single_path_acl_record" to embed "u8" + "u16" and
9590837b
KT
112 * "struct tomoyo_double_path_acl_record" to embed "u8"
113 * without enlarging their structure size.
114 */
115struct tomoyo_acl_info {
116 struct list_head list;
117 /*
118 * Type of this ACL entry.
119 *
120 * MSB is is_deleted flag.
121 */
122 u8 type;
123} __packed;
124
125/* This ACL entry is deleted. */
126#define TOMOYO_ACL_DELETED 0x80
127
c3fa109a
TH
128/*
129 * tomoyo_domain_info is a structure which is used for holding permissions
130 * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
131 * It has following fields.
132 *
133 * (1) "list" which is linked to tomoyo_domain_list .
134 * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
135 * (3) "domainname" which holds the name of the domain.
136 * (4) "profile" which remembers profile number assigned to this domain.
137 * (5) "is_deleted" is a bool which is true if this domain is marked as
138 * "deleted", false otherwise.
139 * (6) "quota_warned" is a bool which is used for suppressing warning message
140 * when learning mode learned too much entries.
141 * (7) "flags" which remembers this domain's attributes.
142 *
143 * A domain's lifecycle is an analogy of files on / directory.
144 * Multiple domains with the same domainname cannot be created (as with
145 * creating files with the same filename fails with -EEXIST).
146 * If a process reached a domain, that process can reside in that domain after
147 * that domain is marked as "deleted" (as with a process can access an already
148 * open()ed file after that file was unlink()ed).
149 */
9590837b
KT
150struct tomoyo_domain_info {
151 struct list_head list;
152 struct list_head acl_info_list;
153 /* Name of this domain. Never NULL. */
154 const struct tomoyo_path_info *domainname;
155 u8 profile; /* Profile number to use. */
a0558fc3 156 bool is_deleted; /* Delete flag. */
9590837b
KT
157 bool quota_warned; /* Quota warnning flag. */
158 /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */
159 u8 flags;
160};
161
162/* Profile number is an integer between 0 and 255. */
163#define TOMOYO_MAX_PROFILES 256
164
165/* Ignore "allow_read" directive in exception policy. */
166#define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
167/*
168 * This domain was unable to create a new domain at tomoyo_find_next_domain()
169 * because the name of the domain to be created was too long or
170 * it could not allocate memory.
171 * More than one process continued execve() without domain transition.
172 */
173#define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2
174
175/*
c3fa109a
TH
176 * tomoyo_single_path_acl_record is a structure which is used for holding an
177 * entry with one pathname operation (e.g. open(), mkdir()).
178 * It has following fields.
179 *
180 * (1) "head" which is a "struct tomoyo_acl_info".
181 * (2) "perm" which is a bitmask of permitted operations.
182 * (3) "filename" is the pathname.
183 *
184 * Directives held by this structure are "allow_read/write", "allow_execute",
185 * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
186 * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
937bf613
TH
187 * "allow_mkchar", "allow_truncate", "allow_symlink", "allow_rewrite",
188 * "allow_chmod", "allow_chown", "allow_chgrp", "allow_chroot", "allow_mount"
189 * and "allow_unmount".
9590837b
KT
190 */
191struct tomoyo_single_path_acl_record {
192 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
937bf613 193 u8 perm_high;
9590837b
KT
194 u16 perm;
195 /* Pointer to single pathname. */
196 const struct tomoyo_path_info *filename;
197};
198
c3fa109a
TH
199/*
200 * tomoyo_double_path_acl_record is a structure which is used for holding an
937bf613 201 * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
c3fa109a
TH
202 * It has following fields.
203 *
204 * (1) "head" which is a "struct tomoyo_acl_info".
205 * (2) "perm" which is a bitmask of permitted operations.
206 * (3) "filename1" is the source/old pathname.
207 * (4) "filename2" is the destination/new pathname.
208 *
937bf613
TH
209 * Directives held by this structure are "allow_rename", "allow_link" and
210 * "allow_pivot_root".
c3fa109a 211 */
9590837b
KT
212struct tomoyo_double_path_acl_record {
213 struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
214 u8 perm;
215 /* Pointer to single pathname. */
216 const struct tomoyo_path_info *filename1;
217 /* Pointer to single pathname. */
218 const struct tomoyo_path_info *filename2;
219};
220
221/* Keywords for ACLs. */
222#define TOMOYO_KEYWORD_ALIAS "alias "
223#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
224#define TOMOYO_KEYWORD_DELETE "delete "
225#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
226#define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
227#define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
228#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
229#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
230#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
231#define TOMOYO_KEYWORD_SELECT "select "
9590837b
KT
232#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
233#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
234/* A domain definition starts with <kernel>. */
235#define TOMOYO_ROOT_NAME "<kernel>"
236#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
237
238/* Index numbers for Access Controls. */
239#define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */
240#define TOMOYO_MAX_ACCEPT_ENTRY 1
241#define TOMOYO_VERBOSE 2
242#define TOMOYO_MAX_CONTROL_INDEX 3
243
c3fa109a
TH
244/*
245 * tomoyo_io_buffer is a structure which is used for reading and modifying
246 * configuration via /sys/kernel/security/tomoyo/ interface.
247 * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
248 * cursors.
249 *
250 * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
251 * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
252 * entry has a list of "struct tomoyo_acl_info", we need two cursors when
253 * reading (one is for traversing tomoyo_domain_list and the other is for
254 * traversing "struct tomoyo_acl_info"->acl_info_list ).
255 *
256 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
257 * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
258 * domain with the domainname specified by the rest of that line (NULL is set
259 * if seek failed).
260 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
261 * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
262 * line (->write_var1 is set to NULL if a domain was deleted).
263 * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
264 * neither "select " nor "delete ", an entry or a domain specified by that line
265 * is appended.
266 */
9590837b
KT
267struct tomoyo_io_buffer {
268 int (*read) (struct tomoyo_io_buffer *);
269 int (*write) (struct tomoyo_io_buffer *);
270 /* Exclusive lock for this structure. */
271 struct mutex io_sem;
272 /* The position currently reading from. */
273 struct list_head *read_var1;
274 /* Extra variables for reading. */
275 struct list_head *read_var2;
276 /* The position currently writing to. */
277 struct tomoyo_domain_info *write_var1;
278 /* The step for reading. */
279 int read_step;
280 /* Buffer for reading. */
281 char *read_buf;
282 /* EOF flag for reading. */
283 bool read_eof;
284 /* Read domain ACL of specified PID? */
285 bool read_single_domain;
286 /* Extra variable for reading. */
287 u8 read_bit;
288 /* Bytes available for reading. */
289 int read_avail;
290 /* Size of read buffer. */
291 int readbuf_size;
292 /* Buffer for writing. */
293 char *write_buf;
294 /* Bytes available for writing. */
295 int write_avail;
296 /* Size of write buffer. */
297 int writebuf_size;
298};
299
300/* Check whether the domain has too many ACL entries to hold. */
301bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
302/* Transactional sprintf() for policy dump. */
303bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
304 __attribute__ ((format(printf, 2, 3)));
305/* Check whether the domainname is correct. */
306bool tomoyo_is_correct_domain(const unsigned char *domainname,
307 const char *function);
308/* Check whether the token is correct. */
309bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
310 const s8 pattern_type, const s8 end_type,
311 const char *function);
312/* Check whether the token can be a domainname. */
313bool tomoyo_is_domain_def(const unsigned char *buffer);
314/* Check whether the given filename matches the given pattern. */
315bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
316 const struct tomoyo_path_info *pattern);
317/* Read "alias" entry in exception policy. */
318bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
319/*
320 * Read "initialize_domain" and "no_initialize_domain" entry
321 * in exception policy.
322 */
323bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
324/* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
325bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
326/* Read "file_pattern" entry in exception policy. */
327bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
328/* Read "allow_read" entry in exception policy. */
329bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
330/* Read "deny_rewrite" entry in exception policy. */
331bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
332/* Write domain policy violation warning message to console? */
333bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
334/* Convert double path operation to operation name. */
335const char *tomoyo_dp2keyword(const u8 operation);
336/* Get the last component of the given domainname. */
337const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
338/* Get warning message. */
339const char *tomoyo_get_msg(const bool is_enforce);
340/* Convert single path operation to operation name. */
341const char *tomoyo_sp2keyword(const u8 operation);
9590837b
KT
342/* Create "alias" entry in exception policy. */
343int tomoyo_write_alias_policy(char *data, const bool is_delete);
344/*
345 * Create "initialize_domain" and "no_initialize_domain" entry
346 * in exception policy.
347 */
348int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
349 const bool is_delete);
350/* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
351int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
352 const bool is_delete);
353/*
354 * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
355 * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
356 * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
357 * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
358 * "allow_link" entry in domain policy.
359 */
360int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
361 const bool is_delete);
362/* Create "allow_read" entry in exception policy. */
363int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
364/* Create "deny_rewrite" entry in exception policy. */
365int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
366/* Create "file_pattern" entry in exception policy. */
367int tomoyo_write_pattern_policy(char *data, const bool is_delete);
368/* Find a domain by the given name. */
369struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
370/* Find or create a domain by the given name. */
371struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
372 domainname,
373 const u8 profile);
9590837b
KT
374/* Check mode for specified functionality. */
375unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
376 const u8 index);
377/* Allocate memory for structures. */
378void *tomoyo_alloc_acl_element(const u8 acl_type);
379/* Fill in "struct tomoyo_path_info" members. */
380void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
381/* Run policy loader when /sbin/init starts. */
382void tomoyo_load_policy(const char *filename);
383/* Change "struct tomoyo_domain_info"->flags. */
384void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
385 const bool is_delete, const u8 flags);
386
387/* strcmp() for "struct tomoyo_path_info" structure. */
388static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
389 const struct tomoyo_path_info *b)
390{
391 return a->hash != b->hash || strcmp(a->name, b->name);
392}
393
394/* Get type of an ACL entry. */
395static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr)
396{
397 return ptr->type & ~TOMOYO_ACL_DELETED;
398}
399
400/* Get type of an ACL entry. */
401static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr)
402{
403 return ptr->type;
404}
405
406/**
407 * tomoyo_is_valid - Check whether the character is a valid char.
408 *
409 * @c: The character to check.
410 *
411 * Returns true if @c is a valid character, false otherwise.
412 */
413static inline bool tomoyo_is_valid(const unsigned char c)
414{
415 return c > ' ' && c < 127;
416}
417
418/**
419 * tomoyo_is_invalid - Check whether the character is an invalid char.
420 *
421 * @c: The character to check.
422 *
423 * Returns true if @c is an invalid character, false otherwise.
424 */
425static inline bool tomoyo_is_invalid(const unsigned char c)
426{
427 return c && (c <= ' ' || c >= 127);
428}
429
430/* The list for "struct tomoyo_domain_info". */
431extern struct list_head tomoyo_domain_list;
432extern struct rw_semaphore tomoyo_domain_list_lock;
433
434/* Lock for domain->acl_info_list. */
435extern struct rw_semaphore tomoyo_domain_acl_info_list_lock;
436
437/* Has /sbin/init started? */
438extern bool tomoyo_policy_loaded;
439
440/* The kernel's domain. */
441extern struct tomoyo_domain_info tomoyo_kernel_domain;
442
443/**
444 * list_for_each_cookie - iterate over a list with cookie.
445 * @pos: the &struct list_head to use as a loop cursor.
446 * @cookie: the &struct list_head to use as a cookie.
447 * @head: the head for your list.
448 *
449 * Same with list_for_each() except that this primitive uses @cookie
450 * so that we can continue iteration.
451 * @cookie must be NULL when iteration starts, and @cookie will become
452 * NULL when iteration finishes.
453 */
454#define list_for_each_cookie(pos, cookie, head) \
455 for (({ if (!cookie) \
456 cookie = head; }), \
457 pos = (cookie)->next; \
458 prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
459 (cookie) = pos, pos = pos->next)
460
461#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */