blob: f1d2adfd33bccc2574883b8d6a26c8ca281e3d0e [file] [log] [blame]
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001/*
2 * security/tomoyo/file.c
3 *
4 * Implementation of the Domain-Based Mandatory Access Control.
5 *
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 *
Tetsuo Handa39826a12009-04-08 22:31:28 +09008 * Version: 2.2.0 2009/04/01
Kentaro Takedab69a54e2009-02-05 17:18:14 +09009 *
10 */
11
12#include "common.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Kentaro Takedab69a54e2009-02-05 17:18:14 +090014
15/* Keyword array for single path operations. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +090016static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
17 [TOMOYO_TYPE_READ_WRITE] = "read/write",
18 [TOMOYO_TYPE_EXECUTE] = "execute",
19 [TOMOYO_TYPE_READ] = "read",
20 [TOMOYO_TYPE_WRITE] = "write",
21 [TOMOYO_TYPE_CREATE] = "create",
22 [TOMOYO_TYPE_UNLINK] = "unlink",
23 [TOMOYO_TYPE_MKDIR] = "mkdir",
24 [TOMOYO_TYPE_RMDIR] = "rmdir",
25 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
26 [TOMOYO_TYPE_MKSOCK] = "mksock",
27 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
28 [TOMOYO_TYPE_MKCHAR] = "mkchar",
29 [TOMOYO_TYPE_TRUNCATE] = "truncate",
30 [TOMOYO_TYPE_SYMLINK] = "symlink",
31 [TOMOYO_TYPE_REWRITE] = "rewrite",
32 [TOMOYO_TYPE_IOCTL] = "ioctl",
33 [TOMOYO_TYPE_CHMOD] = "chmod",
34 [TOMOYO_TYPE_CHOWN] = "chown",
35 [TOMOYO_TYPE_CHGRP] = "chgrp",
36 [TOMOYO_TYPE_CHROOT] = "chroot",
37 [TOMOYO_TYPE_MOUNT] = "mount",
38 [TOMOYO_TYPE_UMOUNT] = "unmount",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090039};
40
41/* Keyword array for double path operations. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +090042static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
43 [TOMOYO_TYPE_LINK] = "link",
44 [TOMOYO_TYPE_RENAME] = "rename",
45 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090046};
47
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090048void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
49{
50 if (!ptr)
51 return;
52 if (ptr->is_group)
53 tomoyo_put_path_group(ptr->group);
54 else
55 tomoyo_put_name(ptr->filename);
56}
57
58bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
59 const struct tomoyo_name_union *ptr)
60{
61 if (ptr->is_group)
62 return tomoyo_path_matches_group(name, ptr->group, 1);
63 return tomoyo_path_matches_pattern(name, ptr->filename);
64}
65
66static bool tomoyo_compare_name_union_pattern(const struct tomoyo_path_info
67 *name,
68 const struct tomoyo_name_union
69 *ptr, const bool may_use_pattern)
70{
71 if (ptr->is_group)
72 return tomoyo_path_matches_group(name, ptr->group,
73 may_use_pattern);
74 if (may_use_pattern || !ptr->filename->is_patterned)
75 return tomoyo_path_matches_pattern(name, ptr->filename);
76 return false;
77}
78
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090079void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
80{
81 if (ptr && ptr->is_group)
82 tomoyo_put_number_group(ptr->group);
83}
84
85bool tomoyo_compare_number_union(const unsigned long value,
86 const struct tomoyo_number_union *ptr)
87{
88 if (ptr->is_group)
89 return tomoyo_number_matches_group(value, value, ptr->group);
90 return value >= ptr->values[0] && value <= ptr->values[1];
91}
92
Kentaro Takedab69a54e2009-02-05 17:18:14 +090093/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +090094 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
95 *
96 * @r: Pointer to "struct tomoyo_request_info" to initialize.
97 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
98 *
99 * Returns mode.
100 */
101static int tomoyo_init_request_info(struct tomoyo_request_info *r,
102 struct tomoyo_domain_info *domain)
103{
104 memset(r, 0, sizeof(*r));
105 if (!domain)
106 domain = tomoyo_domain();
107 r->domain = domain;
108 r->mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
109 return r->mode;
110}
111
112static void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
113 __attribute__ ((format(printf, 2, 3)));
114/**
115 * tomoyo_warn_log - Print warning or error message on console.
116 *
117 * @r: Pointer to "struct tomoyo_request_info".
118 * @fmt: The printf()'s format string, followed by parameters.
119 */
120static void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
121{
122 int len = PAGE_SIZE;
123 va_list args;
124 char *buffer;
125 if (!tomoyo_verbose_mode(r->domain))
126 return;
127 while (1) {
128 int len2;
129 buffer = kmalloc(len, GFP_NOFS);
130 if (!buffer)
131 return;
132 va_start(args, fmt);
133 len2 = vsnprintf(buffer, len - 1, fmt, args);
134 va_end(args);
135 if (len2 <= len - 1) {
136 buffer[len2] = '\0';
137 break;
138 }
139 len = len2 + 1;
140 kfree(buffer);
141 }
142 printk(KERN_WARNING "TOMOYO-%s: Access %s denied for %s\n",
143 r->mode == TOMOYO_CONFIG_ENFORCING ? "ERROR" : "WARNING",
144 buffer, tomoyo_get_last_name(r->domain));
145 kfree(buffer);
146}
147
148/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900149 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900150 *
151 * @operation: Type of operation.
152 *
153 * Returns the name of single path operation.
154 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900155const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900156{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900157 return (operation < TOMOYO_MAX_PATH_OPERATION)
158 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900159}
160
161/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900162 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900163 *
164 * @operation: Type of operation.
165 *
166 * Returns the name of double path operation.
167 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900168const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900169{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900170 return (operation < TOMOYO_MAX_PATH2_OPERATION)
171 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900172}
173
174/**
175 * tomoyo_strendswith - Check whether the token ends with the given token.
176 *
177 * @name: The token to check.
178 * @tail: The token to find.
179 *
180 * Returns true if @name ends with @tail, false otherwise.
181 */
182static bool tomoyo_strendswith(const char *name, const char *tail)
183{
184 int len;
185
186 if (!name || !tail)
187 return false;
188 len = strlen(name) - strlen(tail);
189 return len >= 0 && !strcmp(name + len, tail);
190}
191
192/**
193 * tomoyo_get_path - Get realpath.
194 *
195 * @path: Pointer to "struct path".
196 *
197 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
198 */
199static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
200{
201 int error;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900202 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900203 GFP_NOFS);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900204
205 if (!buf)
206 return NULL;
207 /* Reserve one byte for appending "/". */
208 error = tomoyo_realpath_from_path2(path, buf->body,
209 sizeof(buf->body) - 2);
210 if (!error) {
211 buf->head.name = buf->body;
212 tomoyo_fill_path_info(&buf->head);
213 return &buf->head;
214 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900215 kfree(buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900216 return NULL;
217}
218
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900219static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
220 const char *filename2,
221 struct tomoyo_domain_info *const domain,
222 const bool is_delete);
223static int tomoyo_update_path_acl(const u8 type, const char *filename,
224 struct tomoyo_domain_info *const domain,
225 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900226
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900227/*
228 * tomoyo_globally_readable_list is used for holding list of pathnames which
229 * are by default allowed to be open()ed for reading by any process.
230 *
231 * An entry is added by
232 *
233 * # echo 'allow_read /lib/libc-2.5.so' > \
234 * /sys/kernel/security/tomoyo/exception_policy
235 *
236 * and is deleted by
237 *
238 * # echo 'delete allow_read /lib/libc-2.5.so' > \
239 * /sys/kernel/security/tomoyo/exception_policy
240 *
241 * and all entries are retrieved by
242 *
243 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
244 *
245 * In the example above, any process is allowed to
246 * open("/lib/libc-2.5.so", O_RDONLY).
247 * One exception is, if the domain which current process belongs to is marked
248 * as "ignore_global_allow_read", current process can't do so unless explicitly
249 * given "allow_read /lib/libc-2.5.so" to the domain which current process
250 * belongs to.
251 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900252LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900253
254/**
255 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
256 *
257 * @filename: Filename unconditionally permitted to open() for reading.
258 * @is_delete: True if it is a delete request.
259 *
260 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900261 *
262 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900263 */
264static int tomoyo_update_globally_readable_entry(const char *filename,
265 const bool is_delete)
266{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900267 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900268 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900269 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900270
Tetsuo Handa17080002010-02-16 21:14:48 +0900271 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900272 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900273 e.filename = tomoyo_get_name(filename);
274 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900275 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900276 if (mutex_lock_interruptible(&tomoyo_policy_lock))
277 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900278 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900279 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900280 continue;
281 ptr->is_deleted = is_delete;
282 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900283 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900284 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900285 if (!is_delete && error) {
286 struct tomoyo_globally_readable_file_entry *entry =
287 tomoyo_commit_ok(&e, sizeof(e));
288 if (entry) {
289 list_add_tail_rcu(&entry->list,
290 &tomoyo_globally_readable_list);
291 error = 0;
292 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900293 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900294 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900295 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900296 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900297 return error;
298}
299
300/**
301 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
302 *
303 * @filename: The filename to check.
304 *
305 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900306 *
307 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900308 */
309static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
310 filename)
311{
312 struct tomoyo_globally_readable_file_entry *ptr;
313 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900314
315 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900316 if (!ptr->is_deleted &&
317 tomoyo_path_matches_pattern(filename, ptr->filename)) {
318 found = true;
319 break;
320 }
321 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900322 return found;
323}
324
325/**
326 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
327 *
328 * @data: String to parse.
329 * @is_delete: True if it is a delete request.
330 *
331 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900332 *
333 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900334 */
335int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
336{
337 return tomoyo_update_globally_readable_entry(data, is_delete);
338}
339
340/**
341 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
342 *
343 * @head: Pointer to "struct tomoyo_io_buffer".
344 *
345 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900346 *
347 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900348 */
349bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
350{
351 struct list_head *pos;
352 bool done = true;
353
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900354 list_for_each_cookie(pos, head->read_var2,
355 &tomoyo_globally_readable_list) {
356 struct tomoyo_globally_readable_file_entry *ptr;
357 ptr = list_entry(pos,
358 struct tomoyo_globally_readable_file_entry,
359 list);
360 if (ptr->is_deleted)
361 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900362 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
363 ptr->filename->name);
364 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900365 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900366 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900367 return done;
368}
369
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900370/* tomoyo_pattern_list is used for holding list of pathnames which are used for
371 * converting pathnames to pathname patterns during learning mode.
372 *
373 * An entry is added by
374 *
375 * # echo 'file_pattern /proc/\$/mounts' > \
376 * /sys/kernel/security/tomoyo/exception_policy
377 *
378 * and is deleted by
379 *
380 * # echo 'delete file_pattern /proc/\$/mounts' > \
381 * /sys/kernel/security/tomoyo/exception_policy
382 *
383 * and all entries are retrieved by
384 *
385 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
386 *
387 * In the example above, if a process which belongs to a domain which is in
388 * learning mode requested open("/proc/1/mounts", O_RDONLY),
389 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
390 * process belongs to.
391 *
392 * It is not a desirable behavior that we have to use /proc/\$/ instead of
393 * /proc/self/ when current process needs to access only current process's
394 * information. As of now, LSM version of TOMOYO is using __d_path() for
395 * calculating pathname. Non LSM version of TOMOYO is using its own function
396 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
397 * current process from accessing other process's information.
398 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900399LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900400
401/**
402 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
403 *
404 * @pattern: Pathname pattern.
405 * @is_delete: True if it is a delete request.
406 *
407 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900408 *
409 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900410 */
411static int tomoyo_update_file_pattern_entry(const char *pattern,
412 const bool is_delete)
413{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900414 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900415 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900416 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900417
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900418 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900419 return error;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900420 if (!e.pattern->is_patterned)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900421 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900422 if (mutex_lock_interruptible(&tomoyo_policy_lock))
423 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900424 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900425 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900426 continue;
427 ptr->is_deleted = is_delete;
428 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900429 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900430 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900431 if (!is_delete && error) {
432 struct tomoyo_pattern_entry *entry =
433 tomoyo_commit_ok(&e, sizeof(e));
434 if (entry) {
435 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
436 error = 0;
437 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900438 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900439 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900440 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900441 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900442 return error;
443}
444
445/**
446 * tomoyo_get_file_pattern - Get patterned pathname.
447 *
448 * @filename: The filename to find patterned pathname.
449 *
450 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900451 *
452 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900453 */
454static const struct tomoyo_path_info *
455tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
456{
457 struct tomoyo_pattern_entry *ptr;
458 const struct tomoyo_path_info *pattern = NULL;
459
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900460 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900461 if (ptr->is_deleted)
462 continue;
463 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
464 continue;
465 pattern = ptr->pattern;
466 if (tomoyo_strendswith(pattern->name, "/\\*")) {
467 /* Do nothing. Try to find the better match. */
468 } else {
469 /* This would be the better match. Use this. */
470 break;
471 }
472 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900473 if (pattern)
474 filename = pattern;
475 return filename;
476}
477
478/**
479 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
480 *
481 * @data: String to parse.
482 * @is_delete: True if it is a delete request.
483 *
484 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900485 *
486 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900487 */
488int tomoyo_write_pattern_policy(char *data, const bool is_delete)
489{
490 return tomoyo_update_file_pattern_entry(data, is_delete);
491}
492
493/**
494 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
495 *
496 * @head: Pointer to "struct tomoyo_io_buffer".
497 *
498 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900499 *
500 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900501 */
502bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
503{
504 struct list_head *pos;
505 bool done = true;
506
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900507 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
508 struct tomoyo_pattern_entry *ptr;
509 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
510 if (ptr->is_deleted)
511 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900512 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
513 "%s\n", ptr->pattern->name);
514 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900515 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900516 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900517 return done;
518}
519
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900520/*
521 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
522 * default forbidden to modify already written content of a file.
523 *
524 * An entry is added by
525 *
526 * # echo 'deny_rewrite /var/log/messages' > \
527 * /sys/kernel/security/tomoyo/exception_policy
528 *
529 * and is deleted by
530 *
531 * # echo 'delete deny_rewrite /var/log/messages' > \
532 * /sys/kernel/security/tomoyo/exception_policy
533 *
534 * and all entries are retrieved by
535 *
536 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
537 *
538 * In the example above, if a process requested to rewrite /var/log/messages ,
539 * the process can't rewrite unless the domain which that process belongs to
540 * has "allow_rewrite /var/log/messages" entry.
541 *
542 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
543 * when we want to allow rewriting already unlink()ed file. As of now,
544 * LSM version of TOMOYO is using __d_path() for calculating pathname.
545 * Non LSM version of TOMOYO is using its own function which doesn't append
546 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
547 * need to worry whether the file is already unlink()ed or not.
548 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900549LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900550
551/**
552 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
553 *
554 * @pattern: Pathname pattern that are not rewritable by default.
555 * @is_delete: True if it is a delete request.
556 *
557 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900558 *
559 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900560 */
561static int tomoyo_update_no_rewrite_entry(const char *pattern,
562 const bool is_delete)
563{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900564 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900565 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900566 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900567
Tetsuo Handa17080002010-02-16 21:14:48 +0900568 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900569 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900570 e.pattern = tomoyo_get_name(pattern);
571 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900572 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900573 if (mutex_lock_interruptible(&tomoyo_policy_lock))
574 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900575 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900576 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900577 continue;
578 ptr->is_deleted = is_delete;
579 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900580 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900581 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900582 if (!is_delete && error) {
583 struct tomoyo_no_rewrite_entry *entry =
584 tomoyo_commit_ok(&e, sizeof(e));
585 if (entry) {
586 list_add_tail_rcu(&entry->list,
587 &tomoyo_no_rewrite_list);
588 error = 0;
589 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900590 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900591 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900592 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900593 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900594 return error;
595}
596
597/**
598 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
599 *
600 * @filename: Filename to check.
601 *
602 * Returns true if @filename is specified by "deny_rewrite" directive,
603 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900604 *
605 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900606 */
607static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
608{
609 struct tomoyo_no_rewrite_entry *ptr;
610 bool found = false;
611
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900612 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900613 if (ptr->is_deleted)
614 continue;
615 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
616 continue;
617 found = true;
618 break;
619 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900620 return found;
621}
622
623/**
624 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
625 *
626 * @data: String to parse.
627 * @is_delete: True if it is a delete request.
628 *
629 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900630 *
631 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900632 */
633int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
634{
635 return tomoyo_update_no_rewrite_entry(data, is_delete);
636}
637
638/**
639 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
640 *
641 * @head: Pointer to "struct tomoyo_io_buffer".
642 *
643 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900644 *
645 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900646 */
647bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
648{
649 struct list_head *pos;
650 bool done = true;
651
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900652 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
653 struct tomoyo_no_rewrite_entry *ptr;
654 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
655 if (ptr->is_deleted)
656 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900657 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
658 "%s\n", ptr->pattern->name);
659 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900660 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900661 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900662 return done;
663}
664
665/**
666 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
667 *
668 * @filename: Filename.
669 * @perm: Permission (between 1 to 7).
670 * @domain: Pointer to "struct tomoyo_domain_info".
671 * @is_delete: True if it is a delete request.
672 *
673 * Returns 0 on success, negative value otherwise.
674 *
675 * This is legacy support interface for older policy syntax.
676 * Current policy syntax uses "allow_read/write" instead of "6",
677 * "allow_read" instead of "4", "allow_write" instead of "2",
678 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900679 *
680 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900681 */
682static int tomoyo_update_file_acl(const char *filename, u8 perm,
683 struct tomoyo_domain_info * const domain,
684 const bool is_delete)
685{
686 if (perm > 7 || !perm) {
687 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
688 __func__, perm, filename);
689 return -EINVAL;
690 }
691 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
692 /*
693 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
694 * directory permissions.
695 */
696 return 0;
697 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900698 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
699 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900700 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900701 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
702 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900703 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900704 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
705 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900706 return 0;
707}
708
709/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900710 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900711 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900712 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900713 * @filename: Filename to check.
714 * @perm: Permission.
715 * @may_use_pattern: True if patterned ACL is permitted.
716 *
717 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900718 *
719 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900720 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900721static int tomoyo_path_acl(const struct tomoyo_request_info *r,
722 const struct tomoyo_path_info *filename,
723 const u32 perm, const bool may_use_pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900724{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900725 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900726 struct tomoyo_acl_info *ptr;
727 int error = -EPERM;
728
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900729 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900730 struct tomoyo_path_acl *acl;
731 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900732 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900733 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900734 if (perm <= 0xFFFF) {
735 if (!(acl->perm & perm))
736 continue;
737 } else {
738 if (!(acl->perm_high & (perm >> 16)))
739 continue;
740 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900741 if (!tomoyo_compare_name_union_pattern(filename, &acl->name,
742 may_use_pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900743 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900744 error = 0;
745 break;
746 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900747 return error;
748}
749
750/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900751 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900752 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900753 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900754 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900755 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900756 *
757 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900758 *
759 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900760 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900761static int tomoyo_file_perm(struct tomoyo_request_info *r,
762 const struct tomoyo_path_info *filename,
763 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900764{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900765 const char *msg = "<unknown>";
766 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900767 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900768
769 if (!filename)
770 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900771
772 if (mode == 6) {
773 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
774 perm = 1 << TOMOYO_TYPE_READ_WRITE;
775 } else if (mode == 4) {
776 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
777 perm = 1 << TOMOYO_TYPE_READ;
778 } else if (mode == 2) {
779 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
780 perm = 1 << TOMOYO_TYPE_WRITE;
781 } else if (mode == 1) {
782 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
783 perm = 1 << TOMOYO_TYPE_EXECUTE;
784 } else
785 BUG();
786 error = tomoyo_path_acl(r, filename, perm, mode != 1);
787 if (error && mode == 4 && !r->domain->ignore_global_allow_read
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900788 && tomoyo_is_globally_readable_file(filename))
789 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900790 if (!error)
791 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900792 tomoyo_warn_log(r, "%s %s", msg, filename->name);
793 if (r->mode == TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900794 return error;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900795 if (tomoyo_domain_quota_is_ok(r)) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796 /* Don't use patterns for execute permission. */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900797 const struct tomoyo_path_info *patterned_file = (mode != 1) ?
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900798 tomoyo_get_file_pattern(filename) : filename;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900799 tomoyo_update_file_acl(patterned_file->name, mode,
800 r->domain, false);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900801 }
802 return 0;
803}
804
805/**
806 * tomoyo_write_file_policy - Update file related list.
807 *
808 * @data: String to parse.
809 * @domain: Pointer to "struct tomoyo_domain_info".
810 * @is_delete: True if it is a delete request.
811 *
812 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900813 *
814 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900815 */
816int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
817 const bool is_delete)
818{
819 char *filename = strchr(data, ' ');
820 char *filename2;
821 unsigned int perm;
822 u8 type;
823
824 if (!filename)
825 return -EINVAL;
826 *filename++ = '\0';
827 if (sscanf(data, "%u", &perm) == 1)
828 return tomoyo_update_file_acl(filename, (u8) perm, domain,
829 is_delete);
830 if (strncmp(data, "allow_", 6))
831 goto out;
832 data += 6;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900833 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
834 if (strcmp(data, tomoyo_path_keyword[type]))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900835 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900836 return tomoyo_update_path_acl(type, filename, domain,
837 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900838 }
839 filename2 = strchr(filename, ' ');
840 if (!filename2)
841 goto out;
842 *filename2++ = '\0';
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900843 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
844 if (strcmp(data, tomoyo_path2_keyword[type]))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900845 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900846 return tomoyo_update_path2_acl(type, filename, filename2,
847 domain, is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900848 }
849 out:
850 return -EINVAL;
851}
852
853/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900854 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900855 *
856 * @type: Type of operation.
857 * @filename: Filename.
858 * @domain: Pointer to "struct tomoyo_domain_info".
859 * @is_delete: True if it is a delete request.
860 *
861 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900862 *
863 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900864 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900865static int tomoyo_update_path_acl(const u8 type, const char *filename,
866 struct tomoyo_domain_info *const domain,
867 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900868{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900869 static const u32 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900870 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900871 const u32 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900872 struct tomoyo_acl_info *ptr;
873 struct tomoyo_path_acl e = {
874 .head.type = TOMOYO_TYPE_PATH_ACL,
875 .perm_high = perm >> 16,
876 .perm = perm
877 };
878 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900879
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900880 if (type == TOMOYO_TYPE_READ_WRITE)
881 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900882 if (!domain)
883 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900884 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900885 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900886 if (mutex_lock_interruptible(&tomoyo_policy_lock))
887 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900888 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900889 struct tomoyo_path_acl *acl =
890 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900891 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900892 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900893 if (is_delete) {
894 if (perm <= 0xFFFF)
895 acl->perm &= ~perm;
896 else
897 acl->perm_high &= ~(perm >> 16);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900898 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900899 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
900 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900901 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900902 } else {
903 if (perm <= 0xFFFF)
904 acl->perm |= perm;
905 else
906 acl->perm_high |= (perm >> 16);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900907 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900908 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
909 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900910 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900911 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900912 error = 0;
913 break;
914 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900915 if (!is_delete && error) {
916 struct tomoyo_path_acl *entry =
917 tomoyo_commit_ok(&e, sizeof(e));
918 if (entry) {
919 list_add_tail_rcu(&entry->head.list,
920 &domain->acl_info_list);
921 error = 0;
922 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900923 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900924 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900925 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900926 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900927 return error;
928}
929
930/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900931 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900932 *
933 * @type: Type of operation.
934 * @filename1: First filename.
935 * @filename2: Second filename.
936 * @domain: Pointer to "struct tomoyo_domain_info".
937 * @is_delete: True if it is a delete request.
938 *
939 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900940 *
941 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900942 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900943static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
944 const char *filename2,
945 struct tomoyo_domain_info *const domain,
946 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900947{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900948 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900949 struct tomoyo_path2_acl e = {
950 .head.type = TOMOYO_TYPE_PATH2_ACL,
951 .perm = perm
952 };
953 struct tomoyo_acl_info *ptr;
954 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900955
956 if (!domain)
957 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900958 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
959 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900960 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900961 if (mutex_lock_interruptible(&tomoyo_policy_lock))
962 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900963 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900964 struct tomoyo_path2_acl *acl =
965 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900966 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900967 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900968 if (is_delete)
969 acl->perm &= ~perm;
970 else
971 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900972 error = 0;
973 break;
974 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900975 if (!is_delete && error) {
976 struct tomoyo_path2_acl *entry =
977 tomoyo_commit_ok(&e, sizeof(e));
978 if (entry) {
979 list_add_tail_rcu(&entry->head.list,
980 &domain->acl_info_list);
981 error = 0;
982 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900983 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900984 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900985 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900986 tomoyo_put_name_union(&e.name1);
987 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900988 return error;
989}
990
991/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900992 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900993 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900994 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900995 * @type: Type of operation.
996 * @filename1: First filename to check.
997 * @filename2: Second filename to check.
998 *
999 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001000 *
1001 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001002 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001003static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001004 const struct tomoyo_path_info *filename1,
1005 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001006{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001007 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001008 struct tomoyo_acl_info *ptr;
1009 const u8 perm = 1 << type;
1010 int error = -EPERM;
1011
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001012 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001013 struct tomoyo_path2_acl *acl;
1014 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001015 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001016 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001017 if (!(acl->perm & perm))
1018 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001019 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001020 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001021 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001022 continue;
1023 error = 0;
1024 break;
1025 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001026 return error;
1027}
1028
1029/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001030 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001031 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001032 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001033 * @operation: Type of operation.
1034 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001035 *
1036 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001037 *
1038 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001039 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001040static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1041 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001042{
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001044
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001045 next:
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001046 error = tomoyo_path_acl(r, filename, 1 << operation, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001047 if (!error)
1048 goto ok;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001049 tomoyo_warn_log(r, "%s %s", tomoyo_path2keyword(operation),
1050 filename->name);
1051 if (tomoyo_domain_quota_is_ok(r)) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001052 const char *name = tomoyo_get_file_pattern(filename)->name;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001053 tomoyo_update_path_acl(operation, name, r->domain, false);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001054 }
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001055 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001056 error = 0;
1057 ok:
1058 /*
1059 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1060 * we need to check "allow_rewrite" permission if the filename is
1061 * specified by "deny_rewrite" keyword.
1062 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001063 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001064 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001065 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001066 goto next;
1067 }
1068 return error;
1069}
1070
1071/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001072 * tomoyo_check_exec_perm - Check permission for "execute".
1073 *
1074 * @domain: Pointer to "struct tomoyo_domain_info".
1075 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001076 *
1077 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001078 *
1079 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001080 */
1081int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001082 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001083{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001084 struct tomoyo_request_info r;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001085
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001086 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001087 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001088 return tomoyo_file_perm(&r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001089}
1090
1091/**
1092 * tomoyo_check_open_permission - Check permission for "read" and "write".
1093 *
1094 * @domain: Pointer to "struct tomoyo_domain_info".
1095 * @path: Pointer to "struct path".
1096 * @flag: Flags for open().
1097 *
1098 * Returns 0 on success, negative value otherwise.
1099 */
1100int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1101 struct path *path, const int flag)
1102{
1103 const u8 acc_mode = ACC_MODE(flag);
1104 int error = -ENOMEM;
1105 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001106 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001107 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001108
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001109 if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
1110 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001111 return 0;
1112 if (acc_mode == 0)
1113 return 0;
1114 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1115 /*
1116 * I don't check directories here because mkdir() and rmdir()
1117 * don't call me.
1118 */
1119 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001120 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001121 buf = tomoyo_get_path(path);
1122 if (!buf)
1123 goto out;
1124 error = 0;
1125 /*
1126 * If the filename is specified by "deny_rewrite" keyword,
1127 * we need to check "allow_rewrite" permission when the filename is not
1128 * opened for append mode or the filename is truncated at open time.
1129 */
1130 if ((acc_mode & MAY_WRITE) &&
1131 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1132 (tomoyo_is_no_rewrite_file(buf))) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001133 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001134 }
1135 if (!error)
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001136 error = tomoyo_file_perm(&r, buf, acc_mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001137 if (!error && (flag & O_TRUNC))
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001138 error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001139 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001140 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001141 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001142 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001143 error = 0;
1144 return error;
1145}
1146
1147/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001148 * tomoyo_path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "rewrite", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001149 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001150 * @operation: Type of operation.
1151 * @path: Pointer to "struct path".
1152 *
1153 * Returns 0 on success, negative value otherwise.
1154 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001155int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001156{
1157 int error = -ENOMEM;
1158 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001159 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001160 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001161
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001162 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1163 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001164 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001165 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001166 buf = tomoyo_get_path(path);
1167 if (!buf)
1168 goto out;
1169 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001170 case TOMOYO_TYPE_REWRITE:
1171 if (!tomoyo_is_no_rewrite_file(buf)) {
1172 error = 0;
1173 goto out;
1174 }
1175 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001176 case TOMOYO_TYPE_MKDIR:
1177 case TOMOYO_TYPE_RMDIR:
1178 case TOMOYO_TYPE_CHROOT:
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001179 if (!buf->is_dir) {
1180 /*
1181 * tomoyo_get_path() reserves space for appending "/."
1182 */
1183 strcat((char *) buf->name, "/");
1184 tomoyo_fill_path_info(buf);
1185 }
1186 }
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001187 error = tomoyo_path_permission(&r, operation, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001188 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001189 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001190 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001191 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001192 error = 0;
1193 return error;
1194}
1195
1196/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001197 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001198 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001199 * @operation: Type of operation.
1200 * @path1: Pointer to "struct path".
1201 * @path2: Pointer to "struct path".
1202 *
1203 * Returns 0 on success, negative value otherwise.
1204 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001205int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001206 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001207{
1208 int error = -ENOMEM;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001209 struct tomoyo_path_info *buf1;
1210 struct tomoyo_path_info *buf2;
1211 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001212 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001213
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001214 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1215 !path1->mnt || !path2->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001216 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001217 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001218 buf1 = tomoyo_get_path(path1);
1219 buf2 = tomoyo_get_path(path2);
1220 if (!buf1 || !buf2)
1221 goto out;
1222 {
1223 struct dentry *dentry = path1->dentry;
1224 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1225 /*
1226 * tomoyo_get_path() reserves space for appending "/."
1227 */
1228 if (!buf1->is_dir) {
1229 strcat((char *) buf1->name, "/");
1230 tomoyo_fill_path_info(buf1);
1231 }
1232 if (!buf2->is_dir) {
1233 strcat((char *) buf2->name, "/");
1234 tomoyo_fill_path_info(buf2);
1235 }
1236 }
1237 }
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001238 error = tomoyo_path2_acl(&r, operation, buf1, buf2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001239 if (!error)
1240 goto out;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001241 tomoyo_warn_log(&r, "%s %s %s", tomoyo_path22keyword(operation),
1242 buf1->name, buf2->name);
1243 if (tomoyo_domain_quota_is_ok(&r)) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001244 const char *name1 = tomoyo_get_file_pattern(buf1)->name;
1245 const char *name2 = tomoyo_get_file_pattern(buf2)->name;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001246 tomoyo_update_path2_acl(operation, name1, name2, r.domain,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001247 false);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001248 }
1249 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001250 kfree(buf1);
1251 kfree(buf2);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001252 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001253 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001254 error = 0;
1255 return error;
1256}