]> bbs.cooldavid.org Git - net-next-2.6.git/blob - security/tomoyo/common.h
TOMOYO: Remove unused field.
[net-next-2.6.git] / security / tomoyo / common.h
1 /*
2  * security/tomoyo/common.h
3  *
4  * Common functions for TOMOYO.
5  *
6  * Copyright (C) 2005-2009  NTT DATA CORPORATION
7  *
8  * Version: 2.2.0   2009/04/01
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
26 struct dentry;
27 struct vfsmount;
28
29 /* Temporary buffer for holding pathnames. */
30 struct tomoyo_page_buffer {
31         char buffer[4096];
32 };
33
34 /* Structure for holding a token. */
35 struct tomoyo_path_info {
36         const char *name;
37         u32 hash;          /* = full_name_hash(name, strlen(name)) */
38         u16 const_len;     /* = tomoyo_const_part_length(name)     */
39         bool is_dir;       /* = tomoyo_strendswith(name, "/")      */
40         bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
41         u16 depth;         /* = tomoyo_path_depth(name)            */
42 };
43
44 /*
45  * This is the max length of a token.
46  *
47  * A token consists of only ASCII printable characters.
48  * Non printable characters in a token is represented in \ooo style
49  * octal string. Thus, \ itself is represented as \\.
50  */
51 #define TOMOYO_MAX_PATHNAME_LEN 4000
52
53 /* Structure for holding requested pathname. */
54 struct tomoyo_path_info_with_data {
55         /* Keep "head" first, for this pointer is passed to tomoyo_free(). */
56         struct tomoyo_path_info head;
57         char barrier1[16]; /* Safeguard for overrun. */
58         char body[TOMOYO_MAX_PATHNAME_LEN];
59         char barrier2[16]; /* Safeguard for overrun. */
60 };
61
62 /*
63  * Common header for holding ACL entries.
64  *
65  * Packing "struct tomoyo_acl_info" allows
66  * "struct tomoyo_single_path_acl_record" to embed "u16" and
67  * "struct tomoyo_double_path_acl_record" to embed "u8"
68  * without enlarging their structure size.
69  */
70 struct tomoyo_acl_info {
71         struct list_head list;
72         /*
73          * Type of this ACL entry.
74          *
75          * MSB is is_deleted flag.
76          */
77         u8 type;
78 } __packed;
79
80 /* This ACL entry is deleted.           */
81 #define TOMOYO_ACL_DELETED        0x80
82
83 /* Structure for domain information. */
84 struct tomoyo_domain_info {
85         struct list_head list;
86         struct list_head acl_info_list;
87         /* Name of this domain. Never NULL.          */
88         const struct tomoyo_path_info *domainname;
89         u8 profile;        /* Profile number to use. */
90         bool is_deleted;   /* Delete flag.           */
91         bool quota_warned; /* Quota warnning flag.   */
92         /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */
93         u8 flags;
94 };
95
96 /* Profile number is an integer between 0 and 255. */
97 #define TOMOYO_MAX_PROFILES 256
98
99 /* Ignore "allow_read" directive in exception policy. */
100 #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
101 /*
102  * This domain was unable to create a new domain at tomoyo_find_next_domain()
103  * because the name of the domain to be created was too long or
104  * it could not allocate memory.
105  * More than one process continued execve() without domain transition.
106  */
107 #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED        2
108
109 /*
110  * Structure for "allow_read/write", "allow_execute", "allow_read",
111  * "allow_write", "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
112  * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
113  * "allow_truncate", "allow_symlink" and "allow_rewrite" directive.
114  */
115 struct tomoyo_single_path_acl_record {
116         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
117         u16 perm;
118         /* Pointer to single pathname. */
119         const struct tomoyo_path_info *filename;
120 };
121
122 /* Structure for "allow_rename" and "allow_link" directive. */
123 struct tomoyo_double_path_acl_record {
124         struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
125         u8 perm;
126         /* Pointer to single pathname. */
127         const struct tomoyo_path_info *filename1;
128         /* Pointer to single pathname. */
129         const struct tomoyo_path_info *filename2;
130 };
131
132 /* Keywords for ACLs. */
133 #define TOMOYO_KEYWORD_ALIAS                     "alias "
134 #define TOMOYO_KEYWORD_ALLOW_READ                "allow_read "
135 #define TOMOYO_KEYWORD_DELETE                    "delete "
136 #define TOMOYO_KEYWORD_DENY_REWRITE              "deny_rewrite "
137 #define TOMOYO_KEYWORD_FILE_PATTERN              "file_pattern "
138 #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN         "initialize_domain "
139 #define TOMOYO_KEYWORD_KEEP_DOMAIN               "keep_domain "
140 #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN      "no_initialize_domain "
141 #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN            "no_keep_domain "
142 #define TOMOYO_KEYWORD_SELECT                    "select "
143 #define TOMOYO_KEYWORD_USE_PROFILE               "use_profile "
144 #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ  "ignore_global_allow_read"
145 /* A domain definition starts with <kernel>. */
146 #define TOMOYO_ROOT_NAME                         "<kernel>"
147 #define TOMOYO_ROOT_NAME_LEN                     (sizeof(TOMOYO_ROOT_NAME) - 1)
148
149 /* Index numbers for Access Controls. */
150 #define TOMOYO_MAC_FOR_FILE                  0  /* domain_policy.conf */
151 #define TOMOYO_MAX_ACCEPT_ENTRY              1
152 #define TOMOYO_VERBOSE                       2
153 #define TOMOYO_MAX_CONTROL_INDEX             3
154
155 /* Structure for reading/writing policy via securityfs interfaces. */
156 struct tomoyo_io_buffer {
157         int (*read) (struct tomoyo_io_buffer *);
158         int (*write) (struct tomoyo_io_buffer *);
159         /* Exclusive lock for this structure.   */
160         struct mutex io_sem;
161         /* The position currently reading from. */
162         struct list_head *read_var1;
163         /* Extra variables for reading.         */
164         struct list_head *read_var2;
165         /* The position currently writing to.   */
166         struct tomoyo_domain_info *write_var1;
167         /* The step for reading.                */
168         int read_step;
169         /* Buffer for reading.                  */
170         char *read_buf;
171         /* EOF flag for reading.                */
172         bool read_eof;
173         /* Read domain ACL of specified PID?    */
174         bool read_single_domain;
175         /* Extra variable for reading.          */
176         u8 read_bit;
177         /* Bytes available for reading.         */
178         int read_avail;
179         /* Size of read buffer.                 */
180         int readbuf_size;
181         /* Buffer for writing.                  */
182         char *write_buf;
183         /* Bytes available for writing.         */
184         int write_avail;
185         /* Size of write buffer.                */
186         int writebuf_size;
187 };
188
189 /* Check whether the domain has too many ACL entries to hold. */
190 bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
191 /* Transactional sprintf() for policy dump. */
192 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
193         __attribute__ ((format(printf, 2, 3)));
194 /* Check whether the domainname is correct. */
195 bool tomoyo_is_correct_domain(const unsigned char *domainname,
196                               const char *function);
197 /* Check whether the token is correct. */
198 bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
199                             const s8 pattern_type, const s8 end_type,
200                             const char *function);
201 /* Check whether the token can be a domainname. */
202 bool tomoyo_is_domain_def(const unsigned char *buffer);
203 /* Check whether the given filename matches the given pattern. */
204 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
205                                  const struct tomoyo_path_info *pattern);
206 /* Read "alias" entry in exception policy. */
207 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
208 /*
209  * Read "initialize_domain" and "no_initialize_domain" entry
210  * in exception policy.
211  */
212 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
213 /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
214 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
215 /* Read "file_pattern" entry in exception policy. */
216 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
217 /* Read "allow_read" entry in exception policy. */
218 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
219 /* Read "deny_rewrite" entry in exception policy. */
220 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
221 /* Write domain policy violation warning message to console? */
222 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
223 /* Convert double path operation to operation name. */
224 const char *tomoyo_dp2keyword(const u8 operation);
225 /* Get the last component of the given domainname. */
226 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
227 /* Get warning message. */
228 const char *tomoyo_get_msg(const bool is_enforce);
229 /* Convert single path operation to operation name. */
230 const char *tomoyo_sp2keyword(const u8 operation);
231 /* Delete a domain. */
232 int tomoyo_delete_domain(char *data);
233 /* Create "alias" entry in exception policy. */
234 int tomoyo_write_alias_policy(char *data, const bool is_delete);
235 /*
236  * Create "initialize_domain" and "no_initialize_domain" entry
237  * in exception policy.
238  */
239 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
240                                            const bool is_delete);
241 /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
242 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
243                                       const bool is_delete);
244 /*
245  * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
246  * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
247  * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
248  * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
249  * "allow_link" entry in domain policy.
250  */
251 int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
252                              const bool is_delete);
253 /* Create "allow_read" entry in exception policy. */
254 int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
255 /* Create "deny_rewrite" entry in exception policy. */
256 int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
257 /* Create "file_pattern" entry in exception policy. */
258 int tomoyo_write_pattern_policy(char *data, const bool is_delete);
259 /* Find a domain by the given name. */
260 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
261 /* Find or create a domain by the given name. */
262 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
263                                                             domainname,
264                                                             const u8 profile);
265 /* Check mode for specified functionality. */
266 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
267                                 const u8 index);
268 /* Allocate memory for structures. */
269 void *tomoyo_alloc_acl_element(const u8 acl_type);
270 /* Fill in "struct tomoyo_path_info" members. */
271 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
272 /* Run policy loader when /sbin/init starts. */
273 void tomoyo_load_policy(const char *filename);
274 /* Change "struct tomoyo_domain_info"->flags. */
275 void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
276                             const bool is_delete, const u8 flags);
277
278 /* strcmp() for "struct tomoyo_path_info" structure. */
279 static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
280                                   const struct tomoyo_path_info *b)
281 {
282         return a->hash != b->hash || strcmp(a->name, b->name);
283 }
284
285 /* Get type of an ACL entry. */
286 static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr)
287 {
288         return ptr->type & ~TOMOYO_ACL_DELETED;
289 }
290
291 /* Get type of an ACL entry. */
292 static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr)
293 {
294         return ptr->type;
295 }
296
297 /**
298  * tomoyo_is_valid - Check whether the character is a valid char.
299  *
300  * @c: The character to check.
301  *
302  * Returns true if @c is a valid character, false otherwise.
303  */
304 static inline bool tomoyo_is_valid(const unsigned char c)
305 {
306         return c > ' ' && c < 127;
307 }
308
309 /**
310  * tomoyo_is_invalid - Check whether the character is an invalid char.
311  *
312  * @c: The character to check.
313  *
314  * Returns true if @c is an invalid character, false otherwise.
315  */
316 static inline bool tomoyo_is_invalid(const unsigned char c)
317 {
318         return c && (c <= ' ' || c >= 127);
319 }
320
321 /* The list for "struct tomoyo_domain_info". */
322 extern struct list_head tomoyo_domain_list;
323 extern struct rw_semaphore tomoyo_domain_list_lock;
324
325 /* Lock for domain->acl_info_list. */
326 extern struct rw_semaphore tomoyo_domain_acl_info_list_lock;
327
328 /* Has /sbin/init started? */
329 extern bool tomoyo_policy_loaded;
330
331 /* The kernel's domain. */
332 extern struct tomoyo_domain_info tomoyo_kernel_domain;
333
334 /**
335  * list_for_each_cookie - iterate over a list with cookie.
336  * @pos:        the &struct list_head to use as a loop cursor.
337  * @cookie:     the &struct list_head to use as a cookie.
338  * @head:       the head for your list.
339  *
340  * Same with list_for_each() except that this primitive uses @cookie
341  * so that we can continue iteration.
342  * @cookie must be NULL when iteration starts, and @cookie will become
343  * NULL when iteration finishes.
344  */
345 #define list_for_each_cookie(pos, cookie, head)                       \
346         for (({ if (!cookie)                                          \
347                                      cookie = head; }),               \
348              pos = (cookie)->next;                                    \
349              prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
350              (cookie) = pos, pos = pos->next)
351
352 #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */