blob: c629cb4e2c66813f2ff149bacaf536c877a53120 [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
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090015/* Keyword array for operations with one pathname. */
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",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090021 [TOMOYO_TYPE_UNLINK] = "unlink",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090022 [TOMOYO_TYPE_RMDIR] = "rmdir",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090023 [TOMOYO_TYPE_TRUNCATE] = "truncate",
24 [TOMOYO_TYPE_SYMLINK] = "symlink",
25 [TOMOYO_TYPE_REWRITE] = "rewrite",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090026 [TOMOYO_TYPE_CHROOT] = "chroot",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090027 [TOMOYO_TYPE_UMOUNT] = "unmount",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090028};
29
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090030/* Keyword array for operations with one pathname and three numbers. */
31static const char *tomoyo_path_number3_keyword
32[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
33 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
34 [TOMOYO_TYPE_MKCHAR] = "mkchar",
35};
36
37/* Keyword array for operations with two pathnames. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +090038static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090039 [TOMOYO_TYPE_LINK] = "link",
40 [TOMOYO_TYPE_RENAME] = "rename",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090041 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090042};
43
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090044/* Keyword array for operations with one pathname and one number. */
45static const char *tomoyo_path_number_keyword
46[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
47 [TOMOYO_TYPE_CREATE] = "create",
48 [TOMOYO_TYPE_MKDIR] = "mkdir",
49 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
50 [TOMOYO_TYPE_MKSOCK] = "mksock",
51 [TOMOYO_TYPE_IOCTL] = "ioctl",
52 [TOMOYO_TYPE_CHMOD] = "chmod",
53 [TOMOYO_TYPE_CHOWN] = "chown",
54 [TOMOYO_TYPE_CHGRP] = "chgrp",
55};
56
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090057void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
58{
59 if (!ptr)
60 return;
61 if (ptr->is_group)
62 tomoyo_put_path_group(ptr->group);
63 else
64 tomoyo_put_name(ptr->filename);
65}
66
67bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
68 const struct tomoyo_name_union *ptr)
69{
70 if (ptr->is_group)
71 return tomoyo_path_matches_group(name, ptr->group, 1);
72 return tomoyo_path_matches_pattern(name, ptr->filename);
73}
74
75static bool tomoyo_compare_name_union_pattern(const struct tomoyo_path_info
76 *name,
77 const struct tomoyo_name_union
78 *ptr, const bool may_use_pattern)
79{
80 if (ptr->is_group)
81 return tomoyo_path_matches_group(name, ptr->group,
82 may_use_pattern);
83 if (may_use_pattern || !ptr->filename->is_patterned)
84 return tomoyo_path_matches_pattern(name, ptr->filename);
85 return false;
86}
87
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090088void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
89{
90 if (ptr && ptr->is_group)
91 tomoyo_put_number_group(ptr->group);
92}
93
94bool tomoyo_compare_number_union(const unsigned long value,
95 const struct tomoyo_number_union *ptr)
96{
97 if (ptr->is_group)
98 return tomoyo_number_matches_group(value, value, ptr->group);
99 return value >= ptr->values[0] && value <= ptr->values[1];
100}
101
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900102/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900103 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
104 *
105 * @r: Pointer to "struct tomoyo_request_info" to initialize.
106 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
107 *
108 * Returns mode.
109 */
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900110int tomoyo_init_request_info(struct tomoyo_request_info *r,
111 struct tomoyo_domain_info *domain)
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900112{
113 memset(r, 0, sizeof(*r));
114 if (!domain)
115 domain = tomoyo_domain();
116 r->domain = domain;
117 r->mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
118 return r->mode;
119}
120
121static void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
122 __attribute__ ((format(printf, 2, 3)));
123/**
124 * tomoyo_warn_log - Print warning or error message on console.
125 *
126 * @r: Pointer to "struct tomoyo_request_info".
127 * @fmt: The printf()'s format string, followed by parameters.
128 */
129static void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
130{
131 int len = PAGE_SIZE;
132 va_list args;
133 char *buffer;
134 if (!tomoyo_verbose_mode(r->domain))
135 return;
136 while (1) {
137 int len2;
138 buffer = kmalloc(len, GFP_NOFS);
139 if (!buffer)
140 return;
141 va_start(args, fmt);
142 len2 = vsnprintf(buffer, len - 1, fmt, args);
143 va_end(args);
144 if (len2 <= len - 1) {
145 buffer[len2] = '\0';
146 break;
147 }
148 len = len2 + 1;
149 kfree(buffer);
150 }
151 printk(KERN_WARNING "TOMOYO-%s: Access %s denied for %s\n",
152 r->mode == TOMOYO_CONFIG_ENFORCING ? "ERROR" : "WARNING",
153 buffer, tomoyo_get_last_name(r->domain));
154 kfree(buffer);
155}
156
157/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900158 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900159 *
160 * @operation: Type of operation.
161 *
162 * Returns the name of single path operation.
163 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900164const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900165{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900166 return (operation < TOMOYO_MAX_PATH_OPERATION)
167 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900168}
169
170/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900171 * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
172 *
173 * @operation: Type of operation.
174 *
175 * Returns the name of path/number/number/number operation.
176 */
177const char *tomoyo_path_number32keyword(const u8 operation)
178{
179 return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
180 ? tomoyo_path_number3_keyword[operation] : NULL;
181}
182
183/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900184 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900185 *
186 * @operation: Type of operation.
187 *
188 * Returns the name of double path operation.
189 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900190const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900191{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900192 return (operation < TOMOYO_MAX_PATH2_OPERATION)
193 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900194}
195
196/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900197 * tomoyo_path_number2keyword - Get the name of path/number operations.
198 *
199 * @operation: Type of operation.
200 *
201 * Returns the name of path/number operation.
202 */
203const char *tomoyo_path_number2keyword(const u8 operation)
204{
205 return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
206 ? tomoyo_path_number_keyword[operation] : NULL;
207}
208
209/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900210 * tomoyo_strendswith - Check whether the token ends with the given token.
211 *
212 * @name: The token to check.
213 * @tail: The token to find.
214 *
215 * Returns true if @name ends with @tail, false otherwise.
216 */
217static bool tomoyo_strendswith(const char *name, const char *tail)
218{
219 int len;
220
221 if (!name || !tail)
222 return false;
223 len = strlen(name) - strlen(tail);
224 return len >= 0 && !strcmp(name + len, tail);
225}
226
227/**
228 * tomoyo_get_path - Get realpath.
229 *
230 * @path: Pointer to "struct path".
231 *
232 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
233 */
234static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
235{
236 int error;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900237 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900238 GFP_NOFS);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900239
240 if (!buf)
241 return NULL;
242 /* Reserve one byte for appending "/". */
243 error = tomoyo_realpath_from_path2(path, buf->body,
244 sizeof(buf->body) - 2);
245 if (!error) {
246 buf->head.name = buf->body;
247 tomoyo_fill_path_info(&buf->head);
248 return &buf->head;
249 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900250 kfree(buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900251 return NULL;
252}
253
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900254static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
255 const char *filename2,
256 struct tomoyo_domain_info *const domain,
257 const bool is_delete);
258static int tomoyo_update_path_acl(const u8 type, const char *filename,
259 struct tomoyo_domain_info *const domain,
260 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900261
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900262/*
263 * tomoyo_globally_readable_list is used for holding list of pathnames which
264 * are by default allowed to be open()ed for reading by any process.
265 *
266 * An entry is added by
267 *
268 * # echo 'allow_read /lib/libc-2.5.so' > \
269 * /sys/kernel/security/tomoyo/exception_policy
270 *
271 * and is deleted by
272 *
273 * # echo 'delete allow_read /lib/libc-2.5.so' > \
274 * /sys/kernel/security/tomoyo/exception_policy
275 *
276 * and all entries are retrieved by
277 *
278 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
279 *
280 * In the example above, any process is allowed to
281 * open("/lib/libc-2.5.so", O_RDONLY).
282 * One exception is, if the domain which current process belongs to is marked
283 * as "ignore_global_allow_read", current process can't do so unless explicitly
284 * given "allow_read /lib/libc-2.5.so" to the domain which current process
285 * belongs to.
286 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900287LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900288
289/**
290 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
291 *
292 * @filename: Filename unconditionally permitted to open() for reading.
293 * @is_delete: True if it is a delete request.
294 *
295 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900296 *
297 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900298 */
299static int tomoyo_update_globally_readable_entry(const char *filename,
300 const bool is_delete)
301{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900302 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900303 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900304 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900305
Tetsuo Handa17080002010-02-16 21:14:48 +0900306 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900307 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900308 e.filename = tomoyo_get_name(filename);
309 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900310 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900311 if (mutex_lock_interruptible(&tomoyo_policy_lock))
312 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900313 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900314 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900315 continue;
316 ptr->is_deleted = is_delete;
317 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900318 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900319 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900320 if (!is_delete && error) {
321 struct tomoyo_globally_readable_file_entry *entry =
322 tomoyo_commit_ok(&e, sizeof(e));
323 if (entry) {
324 list_add_tail_rcu(&entry->list,
325 &tomoyo_globally_readable_list);
326 error = 0;
327 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900328 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900329 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900330 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900331 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900332 return error;
333}
334
335/**
336 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
337 *
338 * @filename: The filename to check.
339 *
340 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900341 *
342 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900343 */
344static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
345 filename)
346{
347 struct tomoyo_globally_readable_file_entry *ptr;
348 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900349
350 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900351 if (!ptr->is_deleted &&
352 tomoyo_path_matches_pattern(filename, ptr->filename)) {
353 found = true;
354 break;
355 }
356 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900357 return found;
358}
359
360/**
361 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
362 *
363 * @data: String to parse.
364 * @is_delete: True if it is a delete request.
365 *
366 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900367 *
368 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900369 */
370int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
371{
372 return tomoyo_update_globally_readable_entry(data, is_delete);
373}
374
375/**
376 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
377 *
378 * @head: Pointer to "struct tomoyo_io_buffer".
379 *
380 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900381 *
382 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900383 */
384bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
385{
386 struct list_head *pos;
387 bool done = true;
388
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900389 list_for_each_cookie(pos, head->read_var2,
390 &tomoyo_globally_readable_list) {
391 struct tomoyo_globally_readable_file_entry *ptr;
392 ptr = list_entry(pos,
393 struct tomoyo_globally_readable_file_entry,
394 list);
395 if (ptr->is_deleted)
396 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900397 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
398 ptr->filename->name);
399 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900400 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900401 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900402 return done;
403}
404
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900405/* tomoyo_pattern_list is used for holding list of pathnames which are used for
406 * converting pathnames to pathname patterns during learning mode.
407 *
408 * An entry is added by
409 *
410 * # echo 'file_pattern /proc/\$/mounts' > \
411 * /sys/kernel/security/tomoyo/exception_policy
412 *
413 * and is deleted by
414 *
415 * # echo 'delete file_pattern /proc/\$/mounts' > \
416 * /sys/kernel/security/tomoyo/exception_policy
417 *
418 * and all entries are retrieved by
419 *
420 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
421 *
422 * In the example above, if a process which belongs to a domain which is in
423 * learning mode requested open("/proc/1/mounts", O_RDONLY),
424 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
425 * process belongs to.
426 *
427 * It is not a desirable behavior that we have to use /proc/\$/ instead of
428 * /proc/self/ when current process needs to access only current process's
429 * information. As of now, LSM version of TOMOYO is using __d_path() for
430 * calculating pathname. Non LSM version of TOMOYO is using its own function
431 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
432 * current process from accessing other process's information.
433 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900434LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900435
436/**
437 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
438 *
439 * @pattern: Pathname pattern.
440 * @is_delete: True if it is a delete request.
441 *
442 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900443 *
444 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900445 */
446static int tomoyo_update_file_pattern_entry(const char *pattern,
447 const bool is_delete)
448{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900449 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900450 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900451 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900452
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900453 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900454 return error;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900455 if (!e.pattern->is_patterned)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900456 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900457 if (mutex_lock_interruptible(&tomoyo_policy_lock))
458 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900459 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900460 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900461 continue;
462 ptr->is_deleted = is_delete;
463 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900464 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900465 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900466 if (!is_delete && error) {
467 struct tomoyo_pattern_entry *entry =
468 tomoyo_commit_ok(&e, sizeof(e));
469 if (entry) {
470 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
471 error = 0;
472 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900473 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900474 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900475 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900476 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900477 return error;
478}
479
480/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900481 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900482 *
483 * @filename: The filename to find patterned pathname.
484 *
485 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900486 *
487 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900488 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900489const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900490{
491 struct tomoyo_pattern_entry *ptr;
492 const struct tomoyo_path_info *pattern = NULL;
493
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900494 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900495 if (ptr->is_deleted)
496 continue;
497 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
498 continue;
499 pattern = ptr->pattern;
500 if (tomoyo_strendswith(pattern->name, "/\\*")) {
501 /* Do nothing. Try to find the better match. */
502 } else {
503 /* This would be the better match. Use this. */
504 break;
505 }
506 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900507 if (pattern)
508 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900509 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900510}
511
512/**
513 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
514 *
515 * @data: String to parse.
516 * @is_delete: True if it is a delete request.
517 *
518 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900519 *
520 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900521 */
522int tomoyo_write_pattern_policy(char *data, const bool is_delete)
523{
524 return tomoyo_update_file_pattern_entry(data, is_delete);
525}
526
527/**
528 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
529 *
530 * @head: Pointer to "struct tomoyo_io_buffer".
531 *
532 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900533 *
534 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900535 */
536bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
537{
538 struct list_head *pos;
539 bool done = true;
540
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900541 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
542 struct tomoyo_pattern_entry *ptr;
543 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
544 if (ptr->is_deleted)
545 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900546 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
547 "%s\n", ptr->pattern->name);
548 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900549 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900550 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900551 return done;
552}
553
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900554/*
555 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
556 * default forbidden to modify already written content of a file.
557 *
558 * An entry is added by
559 *
560 * # echo 'deny_rewrite /var/log/messages' > \
561 * /sys/kernel/security/tomoyo/exception_policy
562 *
563 * and is deleted by
564 *
565 * # echo 'delete deny_rewrite /var/log/messages' > \
566 * /sys/kernel/security/tomoyo/exception_policy
567 *
568 * and all entries are retrieved by
569 *
570 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
571 *
572 * In the example above, if a process requested to rewrite /var/log/messages ,
573 * the process can't rewrite unless the domain which that process belongs to
574 * has "allow_rewrite /var/log/messages" entry.
575 *
576 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
577 * when we want to allow rewriting already unlink()ed file. As of now,
578 * LSM version of TOMOYO is using __d_path() for calculating pathname.
579 * Non LSM version of TOMOYO is using its own function which doesn't append
580 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
581 * need to worry whether the file is already unlink()ed or not.
582 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900583LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900584
585/**
586 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
587 *
588 * @pattern: Pathname pattern that are not rewritable by default.
589 * @is_delete: True if it is a delete request.
590 *
591 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900592 *
593 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900594 */
595static int tomoyo_update_no_rewrite_entry(const char *pattern,
596 const bool is_delete)
597{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900598 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900599 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900600 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900601
Tetsuo Handa17080002010-02-16 21:14:48 +0900602 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900603 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900604 e.pattern = tomoyo_get_name(pattern);
605 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900606 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900607 if (mutex_lock_interruptible(&tomoyo_policy_lock))
608 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900609 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900610 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900611 continue;
612 ptr->is_deleted = is_delete;
613 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900614 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900615 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900616 if (!is_delete && error) {
617 struct tomoyo_no_rewrite_entry *entry =
618 tomoyo_commit_ok(&e, sizeof(e));
619 if (entry) {
620 list_add_tail_rcu(&entry->list,
621 &tomoyo_no_rewrite_list);
622 error = 0;
623 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900624 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900625 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900626 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900627 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900628 return error;
629}
630
631/**
632 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
633 *
634 * @filename: Filename to check.
635 *
636 * Returns true if @filename is specified by "deny_rewrite" directive,
637 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900638 *
639 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900640 */
641static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
642{
643 struct tomoyo_no_rewrite_entry *ptr;
644 bool found = false;
645
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900646 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900647 if (ptr->is_deleted)
648 continue;
649 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
650 continue;
651 found = true;
652 break;
653 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900654 return found;
655}
656
657/**
658 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
659 *
660 * @data: String to parse.
661 * @is_delete: True if it is a delete request.
662 *
663 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900664 *
665 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900666 */
667int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
668{
669 return tomoyo_update_no_rewrite_entry(data, is_delete);
670}
671
672/**
673 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
674 *
675 * @head: Pointer to "struct tomoyo_io_buffer".
676 *
677 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900678 *
679 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900680 */
681bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
682{
683 struct list_head *pos;
684 bool done = true;
685
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900686 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
687 struct tomoyo_no_rewrite_entry *ptr;
688 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
689 if (ptr->is_deleted)
690 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900691 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
692 "%s\n", ptr->pattern->name);
693 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900694 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900695 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900696 return done;
697}
698
699/**
700 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
701 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900702 * @perm: Permission (between 1 to 7).
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900703 * @filename: Filename.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900704 * @domain: Pointer to "struct tomoyo_domain_info".
705 * @is_delete: True if it is a delete request.
706 *
707 * Returns 0 on success, negative value otherwise.
708 *
709 * This is legacy support interface for older policy syntax.
710 * Current policy syntax uses "allow_read/write" instead of "6",
711 * "allow_read" instead of "4", "allow_write" instead of "2",
712 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900713 *
714 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900715 */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900716static int tomoyo_update_file_acl(u8 perm, const char *filename,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900717 struct tomoyo_domain_info * const domain,
718 const bool is_delete)
719{
720 if (perm > 7 || !perm) {
721 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
722 __func__, perm, filename);
723 return -EINVAL;
724 }
725 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
726 /*
727 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
728 * directory permissions.
729 */
730 return 0;
731 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900732 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
733 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900734 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900735 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
736 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900737 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900738 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
739 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900740 return 0;
741}
742
743/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900744 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900745 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900746 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900747 * @filename: Filename to check.
748 * @perm: Permission.
749 * @may_use_pattern: True if patterned ACL is permitted.
750 *
751 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900752 *
753 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900754 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900755static int tomoyo_path_acl(const struct tomoyo_request_info *r,
756 const struct tomoyo_path_info *filename,
757 const u32 perm, const bool may_use_pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900758{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900759 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900760 struct tomoyo_acl_info *ptr;
761 int error = -EPERM;
762
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900763 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900764 struct tomoyo_path_acl *acl;
765 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900766 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900767 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900768 if (!(acl->perm & perm) ||
769 !tomoyo_compare_name_union_pattern(filename, &acl->name,
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900770 may_use_pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900771 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900772 error = 0;
773 break;
774 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900775 return error;
776}
777
778/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900779 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900780 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900781 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900782 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900783 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900784 *
785 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900786 *
787 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900788 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900789static int tomoyo_file_perm(struct tomoyo_request_info *r,
790 const struct tomoyo_path_info *filename,
791 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900792{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900793 const char *msg = "<unknown>";
794 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900795 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796
797 if (!filename)
798 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900799
800 if (mode == 6) {
801 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
802 perm = 1 << TOMOYO_TYPE_READ_WRITE;
803 } else if (mode == 4) {
804 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
805 perm = 1 << TOMOYO_TYPE_READ;
806 } else if (mode == 2) {
807 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
808 perm = 1 << TOMOYO_TYPE_WRITE;
809 } else if (mode == 1) {
810 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
811 perm = 1 << TOMOYO_TYPE_EXECUTE;
812 } else
813 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900814 do {
815 error = tomoyo_path_acl(r, filename, perm, mode != 1);
816 if (error && mode == 4 && !r->domain->ignore_global_allow_read
817 && tomoyo_is_globally_readable_file(filename))
818 error = 0;
819 if (!error)
820 break;
821 tomoyo_warn_log(r, "%s %s", msg, filename->name);
822 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
823 mode == 1 ? filename->name :
824 tomoyo_file_pattern(filename));
825 /*
826 * Do not retry for execute request, for alias may have
827 * changed.
828 */
829 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
830 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900831 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900832 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900833}
834
835/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900836 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900837 *
838 * @type: Type of operation.
839 * @filename: Filename.
840 * @domain: Pointer to "struct tomoyo_domain_info".
841 * @is_delete: True if it is a delete request.
842 *
843 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900844 *
845 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900846 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900847static int tomoyo_update_path_acl(const u8 type, const char *filename,
848 struct tomoyo_domain_info *const domain,
849 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900850{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900851 static const u16 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900852 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900853 const u16 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900854 struct tomoyo_acl_info *ptr;
855 struct tomoyo_path_acl e = {
856 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900857 .perm = perm
858 };
859 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900860
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900861 if (type == TOMOYO_TYPE_READ_WRITE)
862 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900863 if (!domain)
864 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900865 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900866 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900867 if (mutex_lock_interruptible(&tomoyo_policy_lock))
868 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900869 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900870 struct tomoyo_path_acl *acl =
871 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900872 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900873 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900874 if (is_delete) {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900875 acl->perm &= ~perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900876 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900877 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
878 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900879 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900880 } else {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900881 acl->perm |= perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900882 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900883 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
884 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900885 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900886 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900887 error = 0;
888 break;
889 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900890 if (!is_delete && error) {
891 struct tomoyo_path_acl *entry =
892 tomoyo_commit_ok(&e, sizeof(e));
893 if (entry) {
894 list_add_tail_rcu(&entry->head.list,
895 &domain->acl_info_list);
896 error = 0;
897 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900898 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900899 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900900 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900901 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900902 return error;
903}
904
905/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900906 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
907 *
908 * @type: Type of operation.
909 * @filename: Filename.
910 * @mode: Create mode.
911 * @major: Device major number.
912 * @minor: Device minor number.
913 * @domain: Pointer to "struct tomoyo_domain_info".
914 * @is_delete: True if it is a delete request.
915 *
916 * Returns 0 on success, negative value otherwise.
917 */
918static inline int tomoyo_update_path_number3_acl(const u8 type,
919 const char *filename,
920 char *mode,
921 char *major, char *minor,
922 struct tomoyo_domain_info *
923 const domain,
924 const bool is_delete)
925{
926 const u8 perm = 1 << type;
927 struct tomoyo_acl_info *ptr;
928 struct tomoyo_path_number3_acl e = {
929 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
930 .perm = perm
931 };
932 int error = is_delete ? -ENOENT : -ENOMEM;
933 if (!tomoyo_parse_name_union(filename, &e.name) ||
934 !tomoyo_parse_number_union(mode, &e.mode) ||
935 !tomoyo_parse_number_union(major, &e.major) ||
936 !tomoyo_parse_number_union(minor, &e.minor))
937 goto out;
938 if (mutex_lock_interruptible(&tomoyo_policy_lock))
939 goto out;
940 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
941 struct tomoyo_path_number3_acl *acl =
942 container_of(ptr, struct tomoyo_path_number3_acl, head);
943 if (!tomoyo_is_same_path_number3_acl(acl, &e))
944 continue;
945 if (is_delete)
946 acl->perm &= ~perm;
947 else
948 acl->perm |= perm;
949 error = 0;
950 break;
951 }
952 if (!is_delete && error) {
953 struct tomoyo_path_number3_acl *entry =
954 tomoyo_commit_ok(&e, sizeof(e));
955 if (entry) {
956 list_add_tail_rcu(&entry->head.list,
957 &domain->acl_info_list);
958 error = 0;
959 }
960 }
961 mutex_unlock(&tomoyo_policy_lock);
962 out:
963 tomoyo_put_name_union(&e.name);
964 tomoyo_put_number_union(&e.mode);
965 tomoyo_put_number_union(&e.major);
966 tomoyo_put_number_union(&e.minor);
967 return error;
968}
969
970/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900971 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900972 *
973 * @type: Type of operation.
974 * @filename1: First filename.
975 * @filename2: Second filename.
976 * @domain: Pointer to "struct tomoyo_domain_info".
977 * @is_delete: True if it is a delete request.
978 *
979 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900980 *
981 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900982 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900983static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
984 const char *filename2,
985 struct tomoyo_domain_info *const domain,
986 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900987{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900988 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900989 struct tomoyo_path2_acl e = {
990 .head.type = TOMOYO_TYPE_PATH2_ACL,
991 .perm = perm
992 };
993 struct tomoyo_acl_info *ptr;
994 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900995
996 if (!domain)
997 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900998 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
999 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001000 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +09001001 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1002 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001003 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001004 struct tomoyo_path2_acl *acl =
1005 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001006 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001007 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001008 if (is_delete)
1009 acl->perm &= ~perm;
1010 else
1011 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001012 error = 0;
1013 break;
1014 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +09001015 if (!is_delete && error) {
1016 struct tomoyo_path2_acl *entry =
1017 tomoyo_commit_ok(&e, sizeof(e));
1018 if (entry) {
1019 list_add_tail_rcu(&entry->head.list,
1020 &domain->acl_info_list);
1021 error = 0;
1022 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001023 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001024 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001025 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001026 tomoyo_put_name_union(&e.name1);
1027 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001028 return error;
1029}
1030
1031/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001032 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
1033 *
1034 * @r: Pointer to "struct tomoyo_request_info".
1035 * @filename: Filename to check.
1036 * @perm: Permission.
1037 * @mode: Create mode.
1038 * @major: Device major number.
1039 * @minor: Device minor number.
1040 *
1041 * Returns 0 on success, -EPERM otherwise.
1042 *
1043 * Caller holds tomoyo_read_lock().
1044 */
1045static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
1046 const struct tomoyo_path_info *filename,
1047 const u16 perm, const unsigned int mode,
1048 const unsigned int major,
1049 const unsigned int minor)
1050{
1051 struct tomoyo_domain_info *domain = r->domain;
1052 struct tomoyo_acl_info *ptr;
1053 int error = -EPERM;
1054 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1055 struct tomoyo_path_number3_acl *acl;
1056 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
1057 continue;
1058 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
1059 if (!tomoyo_compare_number_union(mode, &acl->mode))
1060 continue;
1061 if (!tomoyo_compare_number_union(major, &acl->major))
1062 continue;
1063 if (!tomoyo_compare_number_union(minor, &acl->minor))
1064 continue;
1065 if (!(acl->perm & perm))
1066 continue;
1067 if (!tomoyo_compare_name_union(filename, &acl->name))
1068 continue;
1069 error = 0;
1070 break;
1071 }
1072 return error;
1073}
1074
1075/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001076 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001077 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001078 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001079 * @type: Type of operation.
1080 * @filename1: First filename to check.
1081 * @filename2: Second filename to check.
1082 *
1083 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001084 *
1085 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001086 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001087static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001088 const struct tomoyo_path_info *filename1,
1089 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001090{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001091 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001092 struct tomoyo_acl_info *ptr;
1093 const u8 perm = 1 << type;
1094 int error = -EPERM;
1095
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001096 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001097 struct tomoyo_path2_acl *acl;
1098 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001099 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001100 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001101 if (!(acl->perm & perm))
1102 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001103 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001104 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001105 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001106 continue;
1107 error = 0;
1108 break;
1109 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001110 return error;
1111}
1112
1113/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001114 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001115 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001116 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001117 * @operation: Type of operation.
1118 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001119 *
1120 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001121 *
1122 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001123 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001124static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1125 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001126{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001127 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001128 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001129
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001130 next:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001131 do {
1132 error = tomoyo_path_acl(r, filename, 1 << operation, 1);
1133 if (!error)
1134 break;
1135 msg = tomoyo_path2keyword(operation);
1136 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1137 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1138 tomoyo_file_pattern(filename));
1139 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001140 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001141 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001142 /*
1143 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1144 * we need to check "allow_rewrite" permission if the filename is
1145 * specified by "deny_rewrite" keyword.
1146 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001147 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001148 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001149 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001150 goto next;
1151 }
1152 return error;
1153}
1154
1155/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001156 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1157 *
1158 * @r: Pointer to "struct tomoyo_request_info".
1159 * @type: Operation.
1160 * @filename: Filename to check.
1161 * @number: Number.
1162 *
1163 * Returns 0 on success, -EPERM otherwise.
1164 *
1165 * Caller holds tomoyo_read_lock().
1166 */
1167static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1168 const struct tomoyo_path_info *filename,
1169 const unsigned long number)
1170{
1171 struct tomoyo_domain_info *domain = r->domain;
1172 struct tomoyo_acl_info *ptr;
1173 const u8 perm = 1 << type;
1174 int error = -EPERM;
1175 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1176 struct tomoyo_path_number_acl *acl;
1177 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1178 continue;
1179 acl = container_of(ptr, struct tomoyo_path_number_acl,
1180 head);
1181 if (!(acl->perm & perm) ||
1182 !tomoyo_compare_number_union(number, &acl->number) ||
1183 !tomoyo_compare_name_union(filename, &acl->name))
1184 continue;
1185 error = 0;
1186 break;
1187 }
1188 return error;
1189}
1190
1191/**
1192 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1193 *
1194 * @type: Type of operation.
1195 * @filename: Filename.
1196 * @number: Number.
1197 * @domain: Pointer to "struct tomoyo_domain_info".
1198 * @is_delete: True if it is a delete request.
1199 *
1200 * Returns 0 on success, negative value otherwise.
1201 */
1202static inline int tomoyo_update_path_number_acl(const u8 type,
1203 const char *filename,
1204 char *number,
1205 struct tomoyo_domain_info *
1206 const domain,
1207 const bool is_delete)
1208{
1209 const u8 perm = 1 << type;
1210 struct tomoyo_acl_info *ptr;
1211 struct tomoyo_path_number_acl e = {
1212 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1213 .perm = perm
1214 };
1215 int error = is_delete ? -ENOENT : -ENOMEM;
1216 if (!domain)
1217 return -EINVAL;
1218 if (!tomoyo_parse_name_union(filename, &e.name))
1219 return -EINVAL;
1220 if (!tomoyo_parse_number_union(number, &e.number))
1221 goto out;
1222 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1223 goto out;
1224 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1225 struct tomoyo_path_number_acl *acl =
1226 container_of(ptr, struct tomoyo_path_number_acl, head);
1227 if (!tomoyo_is_same_path_number_acl(acl, &e))
1228 continue;
1229 if (is_delete)
1230 acl->perm &= ~perm;
1231 else
1232 acl->perm |= perm;
1233 error = 0;
1234 break;
1235 }
1236 if (!is_delete && error) {
1237 struct tomoyo_path_number_acl *entry =
1238 tomoyo_commit_ok(&e, sizeof(e));
1239 if (entry) {
1240 list_add_tail_rcu(&entry->head.list,
1241 &domain->acl_info_list);
1242 error = 0;
1243 }
1244 }
1245 mutex_unlock(&tomoyo_policy_lock);
1246 out:
1247 tomoyo_put_name_union(&e.name);
1248 tomoyo_put_number_union(&e.number);
1249 return error;
1250}
1251
1252/**
1253 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1254 *
1255 * @r: Pointer to "strct tomoyo_request_info".
1256 * @filename: Filename to check.
1257 * @number: Number.
1258 *
1259 * Returns 0 on success, negative value otherwise.
1260 *
1261 * Caller holds tomoyo_read_lock().
1262 */
1263static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1264 const u8 type,
1265 const struct tomoyo_path_info *filename,
1266 const unsigned long number)
1267{
1268 char buffer[64];
1269 int error;
1270 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001271 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001272
1273 if (!filename)
1274 return 0;
1275 switch (type) {
1276 case TOMOYO_TYPE_CREATE:
1277 case TOMOYO_TYPE_MKDIR:
1278 case TOMOYO_TYPE_MKFIFO:
1279 case TOMOYO_TYPE_MKSOCK:
1280 case TOMOYO_TYPE_CHMOD:
1281 radix = TOMOYO_VALUE_TYPE_OCTAL;
1282 break;
1283 case TOMOYO_TYPE_IOCTL:
1284 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1285 break;
1286 default:
1287 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1288 break;
1289 }
1290 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001291 do {
1292 error = tomoyo_path_number_acl(r, type, filename, number);
1293 if (!error)
1294 break;
1295 msg = tomoyo_path_number2keyword(type);
1296 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1297 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1298 tomoyo_file_pattern(filename),
1299 buffer);
1300 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001301 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1302 error = 0;
1303 return error;
1304}
1305
1306/**
1307 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1308 *
1309 * @type: Type of operation.
1310 * @path: Pointer to "struct path".
1311 * @number: Number.
1312 *
1313 * Returns 0 on success, negative value otherwise.
1314 */
1315int tomoyo_path_number_perm(const u8 type, struct path *path,
1316 unsigned long number)
1317{
1318 struct tomoyo_request_info r;
1319 int error = -ENOMEM;
1320 struct tomoyo_path_info *buf;
1321 int idx;
1322
1323 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1324 !path->mnt || !path->dentry)
1325 return 0;
1326 idx = tomoyo_read_lock();
1327 buf = tomoyo_get_path(path);
1328 if (!buf)
1329 goto out;
1330 if (type == TOMOYO_TYPE_MKDIR && !buf->is_dir) {
1331 /*
1332 * tomoyo_get_path() reserves space for appending "/."
1333 */
1334 strcat((char *) buf->name, "/");
1335 tomoyo_fill_path_info(buf);
1336 }
1337 error = tomoyo_path_number_perm2(&r, type, buf, number);
1338 out:
1339 kfree(buf);
1340 tomoyo_read_unlock(idx);
1341 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1342 error = 0;
1343 return error;
1344}
1345
1346/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001347 * tomoyo_check_exec_perm - Check permission for "execute".
1348 *
1349 * @domain: Pointer to "struct tomoyo_domain_info".
1350 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001351 *
1352 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001353 *
1354 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001355 */
1356int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001357 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001358{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001359 struct tomoyo_request_info r;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001360
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001361 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001362 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001363 return tomoyo_file_perm(&r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001364}
1365
1366/**
1367 * tomoyo_check_open_permission - Check permission for "read" and "write".
1368 *
1369 * @domain: Pointer to "struct tomoyo_domain_info".
1370 * @path: Pointer to "struct path".
1371 * @flag: Flags for open().
1372 *
1373 * Returns 0 on success, negative value otherwise.
1374 */
1375int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1376 struct path *path, const int flag)
1377{
1378 const u8 acc_mode = ACC_MODE(flag);
1379 int error = -ENOMEM;
1380 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001381 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001382 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001383
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001384 if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
1385 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001386 return 0;
1387 if (acc_mode == 0)
1388 return 0;
1389 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1390 /*
1391 * I don't check directories here because mkdir() and rmdir()
1392 * don't call me.
1393 */
1394 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001395 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001396 buf = tomoyo_get_path(path);
1397 if (!buf)
1398 goto out;
1399 error = 0;
1400 /*
1401 * If the filename is specified by "deny_rewrite" keyword,
1402 * we need to check "allow_rewrite" permission when the filename is not
1403 * opened for append mode or the filename is truncated at open time.
1404 */
1405 if ((acc_mode & MAY_WRITE) &&
1406 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1407 (tomoyo_is_no_rewrite_file(buf))) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001408 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001409 }
1410 if (!error)
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001411 error = tomoyo_file_perm(&r, buf, acc_mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001412 if (!error && (flag & O_TRUNC))
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001413 error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001414 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001415 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001416 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001417 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001418 error = 0;
1419 return error;
1420}
1421
1422/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001423 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001424 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001425 * @operation: Type of operation.
1426 * @path: Pointer to "struct path".
1427 *
1428 * Returns 0 on success, negative value otherwise.
1429 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001430int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001431{
1432 int error = -ENOMEM;
1433 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001434 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001435 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001436
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001437 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1438 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001439 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001440 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001441 buf = tomoyo_get_path(path);
1442 if (!buf)
1443 goto out;
1444 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001445 case TOMOYO_TYPE_REWRITE:
1446 if (!tomoyo_is_no_rewrite_file(buf)) {
1447 error = 0;
1448 goto out;
1449 }
1450 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001451 case TOMOYO_TYPE_RMDIR:
1452 case TOMOYO_TYPE_CHROOT:
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001453 if (!buf->is_dir) {
1454 /*
1455 * tomoyo_get_path() reserves space for appending "/."
1456 */
1457 strcat((char *) buf->name, "/");
1458 tomoyo_fill_path_info(buf);
1459 }
1460 }
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001461 error = tomoyo_path_permission(&r, operation, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001462 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001463 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001464 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001465 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001466 error = 0;
1467 return error;
1468}
1469
1470/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001471 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1472 *
1473 * @r: Pointer to "struct tomoyo_request_info".
1474 * @operation: Type of operation.
1475 * @filename: Filename to check.
1476 * @mode: Create mode.
1477 * @dev: Device number.
1478 *
1479 * Returns 0 on success, negative value otherwise.
1480 *
1481 * Caller holds tomoyo_read_lock().
1482 */
1483static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1484 const u8 operation,
1485 const struct tomoyo_path_info *filename,
1486 const unsigned int mode,
1487 const unsigned int dev)
1488{
1489 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001490 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001491 const unsigned int major = MAJOR(dev);
1492 const unsigned int minor = MINOR(dev);
1493
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001494 do {
1495 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1496 mode, major, minor);
1497 if (!error)
1498 break;
1499 msg = tomoyo_path_number32keyword(operation);
1500 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1501 mode, major, minor);
1502 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1503 tomoyo_file_pattern(filename), mode,
1504 major, minor);
1505 } while (error == TOMOYO_RETRY_REQUEST);
1506 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001507 error = 0;
1508 return error;
1509}
1510
1511/**
1512 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1513 *
1514 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1515 * @path: Pointer to "struct path".
1516 * @mode: Create mode.
1517 * @dev: Device number.
1518 *
1519 * Returns 0 on success, negative value otherwise.
1520 */
1521int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1522 const unsigned int mode, unsigned int dev)
1523{
1524 struct tomoyo_request_info r;
1525 int error = -ENOMEM;
1526 struct tomoyo_path_info *buf;
1527 int idx;
1528
1529 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1530 !path->mnt)
1531 return 0;
1532 idx = tomoyo_read_lock();
1533 error = -ENOMEM;
1534 buf = tomoyo_get_path(path);
1535 if (buf) {
1536 error = tomoyo_path_number3_perm2(&r, operation, buf, mode,
1537 new_decode_dev(dev));
1538 kfree(buf);
1539 }
1540 tomoyo_read_unlock(idx);
1541 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1542 error = 0;
1543 return error;
1544}
1545
1546/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001547 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001548 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001549 * @operation: Type of operation.
1550 * @path1: Pointer to "struct path".
1551 * @path2: Pointer to "struct path".
1552 *
1553 * Returns 0 on success, negative value otherwise.
1554 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001555int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001556 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001557{
1558 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001559 const char *msg;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001560 struct tomoyo_path_info *buf1;
1561 struct tomoyo_path_info *buf2;
1562 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001563 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001564
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001565 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1566 !path1->mnt || !path2->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001567 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001568 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001569 buf1 = tomoyo_get_path(path1);
1570 buf2 = tomoyo_get_path(path2);
1571 if (!buf1 || !buf2)
1572 goto out;
1573 {
1574 struct dentry *dentry = path1->dentry;
1575 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1576 /*
1577 * tomoyo_get_path() reserves space for appending "/."
1578 */
1579 if (!buf1->is_dir) {
1580 strcat((char *) buf1->name, "/");
1581 tomoyo_fill_path_info(buf1);
1582 }
1583 if (!buf2->is_dir) {
1584 strcat((char *) buf2->name, "/");
1585 tomoyo_fill_path_info(buf2);
1586 }
1587 }
1588 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001589 do {
1590 error = tomoyo_path2_acl(&r, operation, buf1, buf2);
1591 if (!error)
1592 break;
1593 msg = tomoyo_path22keyword(operation);
1594 tomoyo_warn_log(&r, "%s %s %s", msg, buf1->name, buf2->name);
1595 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
1596 tomoyo_file_pattern(buf1),
1597 tomoyo_file_pattern(buf2));
1598 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001599 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001600 kfree(buf1);
1601 kfree(buf2);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001602 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001603 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001604 error = 0;
1605 return error;
1606}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001607
1608/**
1609 * tomoyo_write_file_policy - Update file related list.
1610 *
1611 * @data: String to parse.
1612 * @domain: Pointer to "struct tomoyo_domain_info".
1613 * @is_delete: True if it is a delete request.
1614 *
1615 * Returns 0 on success, negative value otherwise.
1616 *
1617 * Caller holds tomoyo_read_lock().
1618 */
1619int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1620 const bool is_delete)
1621{
1622 char *w[5];
1623 u8 type;
1624 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1625 return -EINVAL;
1626 if (strncmp(w[0], "allow_", 6)) {
1627 unsigned int perm;
1628 if (sscanf(w[0], "%u", &perm) == 1)
1629 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1630 is_delete);
1631 goto out;
1632 }
1633 w[0] += 6;
1634 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1635 if (strcmp(w[0], tomoyo_path_keyword[type]))
1636 continue;
1637 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1638 }
1639 if (!w[2][0])
1640 goto out;
1641 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1642 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1643 continue;
1644 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1645 is_delete);
1646 }
1647 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1648 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1649 continue;
1650 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1651 is_delete);
1652 }
1653 if (!w[3][0] || !w[4][0])
1654 goto out;
1655 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1656 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1657 continue;
1658 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1659 w[4], domain, is_delete);
1660 }
1661 out:
1662 return -EINVAL;
1663}