blob: 2dffe07309180cbe85cb08e479aa822b30676067 [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 Handa7ef61232010-02-16 08:03:30 +090094 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +090095 *
96 * @operation: Type of operation.
97 *
98 * Returns the name of single path operation.
99 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900100const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900101{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900102 return (operation < TOMOYO_MAX_PATH_OPERATION)
103 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900104}
105
106/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900107 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900108 *
109 * @operation: Type of operation.
110 *
111 * Returns the name of double path operation.
112 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900113const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900114{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900115 return (operation < TOMOYO_MAX_PATH2_OPERATION)
116 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900117}
118
119/**
120 * tomoyo_strendswith - Check whether the token ends with the given token.
121 *
122 * @name: The token to check.
123 * @tail: The token to find.
124 *
125 * Returns true if @name ends with @tail, false otherwise.
126 */
127static bool tomoyo_strendswith(const char *name, const char *tail)
128{
129 int len;
130
131 if (!name || !tail)
132 return false;
133 len = strlen(name) - strlen(tail);
134 return len >= 0 && !strcmp(name + len, tail);
135}
136
137/**
138 * tomoyo_get_path - Get realpath.
139 *
140 * @path: Pointer to "struct path".
141 *
142 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
143 */
144static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
145{
146 int error;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900147 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900148 GFP_NOFS);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900149
150 if (!buf)
151 return NULL;
152 /* Reserve one byte for appending "/". */
153 error = tomoyo_realpath_from_path2(path, buf->body,
154 sizeof(buf->body) - 2);
155 if (!error) {
156 buf->head.name = buf->body;
157 tomoyo_fill_path_info(&buf->head);
158 return &buf->head;
159 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900160 kfree(buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900161 return NULL;
162}
163
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900164static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
165 const char *filename2,
166 struct tomoyo_domain_info *const domain,
167 const bool is_delete);
168static int tomoyo_update_path_acl(const u8 type, const char *filename,
169 struct tomoyo_domain_info *const domain,
170 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900171
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900172/*
173 * tomoyo_globally_readable_list is used for holding list of pathnames which
174 * are by default allowed to be open()ed for reading by any process.
175 *
176 * An entry is added by
177 *
178 * # echo 'allow_read /lib/libc-2.5.so' > \
179 * /sys/kernel/security/tomoyo/exception_policy
180 *
181 * and is deleted by
182 *
183 * # echo 'delete allow_read /lib/libc-2.5.so' > \
184 * /sys/kernel/security/tomoyo/exception_policy
185 *
186 * and all entries are retrieved by
187 *
188 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
189 *
190 * In the example above, any process is allowed to
191 * open("/lib/libc-2.5.so", O_RDONLY).
192 * One exception is, if the domain which current process belongs to is marked
193 * as "ignore_global_allow_read", current process can't do so unless explicitly
194 * given "allow_read /lib/libc-2.5.so" to the domain which current process
195 * belongs to.
196 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900197LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900198
199/**
200 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
201 *
202 * @filename: Filename unconditionally permitted to open() for reading.
203 * @is_delete: True if it is a delete request.
204 *
205 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900206 *
207 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900208 */
209static int tomoyo_update_globally_readable_entry(const char *filename,
210 const bool is_delete)
211{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900212 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900213 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900214 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900215
Tetsuo Handa17080002010-02-16 21:14:48 +0900216 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900217 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900218 e.filename = tomoyo_get_name(filename);
219 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900220 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900221 if (mutex_lock_interruptible(&tomoyo_policy_lock))
222 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900223 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900224 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900225 continue;
226 ptr->is_deleted = is_delete;
227 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900228 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900229 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900230 if (!is_delete && error) {
231 struct tomoyo_globally_readable_file_entry *entry =
232 tomoyo_commit_ok(&e, sizeof(e));
233 if (entry) {
234 list_add_tail_rcu(&entry->list,
235 &tomoyo_globally_readable_list);
236 error = 0;
237 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900238 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900239 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900240 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900241 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900242 return error;
243}
244
245/**
246 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
247 *
248 * @filename: The filename to check.
249 *
250 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900251 *
252 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900253 */
254static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
255 filename)
256{
257 struct tomoyo_globally_readable_file_entry *ptr;
258 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900259
260 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900261 if (!ptr->is_deleted &&
262 tomoyo_path_matches_pattern(filename, ptr->filename)) {
263 found = true;
264 break;
265 }
266 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900267 return found;
268}
269
270/**
271 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
272 *
273 * @data: String to parse.
274 * @is_delete: True if it is a delete request.
275 *
276 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900277 *
278 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900279 */
280int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
281{
282 return tomoyo_update_globally_readable_entry(data, is_delete);
283}
284
285/**
286 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
287 *
288 * @head: Pointer to "struct tomoyo_io_buffer".
289 *
290 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900291 *
292 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900293 */
294bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
295{
296 struct list_head *pos;
297 bool done = true;
298
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900299 list_for_each_cookie(pos, head->read_var2,
300 &tomoyo_globally_readable_list) {
301 struct tomoyo_globally_readable_file_entry *ptr;
302 ptr = list_entry(pos,
303 struct tomoyo_globally_readable_file_entry,
304 list);
305 if (ptr->is_deleted)
306 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900307 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
308 ptr->filename->name);
309 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900310 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900311 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900312 return done;
313}
314
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900315/* tomoyo_pattern_list is used for holding list of pathnames which are used for
316 * converting pathnames to pathname patterns during learning mode.
317 *
318 * An entry is added by
319 *
320 * # echo 'file_pattern /proc/\$/mounts' > \
321 * /sys/kernel/security/tomoyo/exception_policy
322 *
323 * and is deleted by
324 *
325 * # echo 'delete file_pattern /proc/\$/mounts' > \
326 * /sys/kernel/security/tomoyo/exception_policy
327 *
328 * and all entries are retrieved by
329 *
330 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
331 *
332 * In the example above, if a process which belongs to a domain which is in
333 * learning mode requested open("/proc/1/mounts", O_RDONLY),
334 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
335 * process belongs to.
336 *
337 * It is not a desirable behavior that we have to use /proc/\$/ instead of
338 * /proc/self/ when current process needs to access only current process's
339 * information. As of now, LSM version of TOMOYO is using __d_path() for
340 * calculating pathname. Non LSM version of TOMOYO is using its own function
341 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
342 * current process from accessing other process's information.
343 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900344LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900345
346/**
347 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
348 *
349 * @pattern: Pathname pattern.
350 * @is_delete: True if it is a delete request.
351 *
352 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900353 *
354 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900355 */
356static int tomoyo_update_file_pattern_entry(const char *pattern,
357 const bool is_delete)
358{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900359 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900360 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900361 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900362
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900363 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900364 return error;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900365 if (!e.pattern->is_patterned)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900366 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900367 if (mutex_lock_interruptible(&tomoyo_policy_lock))
368 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900369 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900370 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900371 continue;
372 ptr->is_deleted = is_delete;
373 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900374 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900375 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900376 if (!is_delete && error) {
377 struct tomoyo_pattern_entry *entry =
378 tomoyo_commit_ok(&e, sizeof(e));
379 if (entry) {
380 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
381 error = 0;
382 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900383 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900384 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900385 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900386 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900387 return error;
388}
389
390/**
391 * tomoyo_get_file_pattern - Get patterned pathname.
392 *
393 * @filename: The filename to find patterned pathname.
394 *
395 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900396 *
397 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900398 */
399static const struct tomoyo_path_info *
400tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
401{
402 struct tomoyo_pattern_entry *ptr;
403 const struct tomoyo_path_info *pattern = NULL;
404
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900405 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900406 if (ptr->is_deleted)
407 continue;
408 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
409 continue;
410 pattern = ptr->pattern;
411 if (tomoyo_strendswith(pattern->name, "/\\*")) {
412 /* Do nothing. Try to find the better match. */
413 } else {
414 /* This would be the better match. Use this. */
415 break;
416 }
417 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900418 if (pattern)
419 filename = pattern;
420 return filename;
421}
422
423/**
424 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
425 *
426 * @data: String to parse.
427 * @is_delete: True if it is a delete request.
428 *
429 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900430 *
431 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900432 */
433int tomoyo_write_pattern_policy(char *data, const bool is_delete)
434{
435 return tomoyo_update_file_pattern_entry(data, is_delete);
436}
437
438/**
439 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
440 *
441 * @head: Pointer to "struct tomoyo_io_buffer".
442 *
443 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900444 *
445 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900446 */
447bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
448{
449 struct list_head *pos;
450 bool done = true;
451
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900452 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
453 struct tomoyo_pattern_entry *ptr;
454 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
455 if (ptr->is_deleted)
456 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900457 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
458 "%s\n", ptr->pattern->name);
459 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900460 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900461 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900462 return done;
463}
464
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900465/*
466 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
467 * default forbidden to modify already written content of a file.
468 *
469 * An entry is added by
470 *
471 * # echo 'deny_rewrite /var/log/messages' > \
472 * /sys/kernel/security/tomoyo/exception_policy
473 *
474 * and is deleted by
475 *
476 * # echo 'delete deny_rewrite /var/log/messages' > \
477 * /sys/kernel/security/tomoyo/exception_policy
478 *
479 * and all entries are retrieved by
480 *
481 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
482 *
483 * In the example above, if a process requested to rewrite /var/log/messages ,
484 * the process can't rewrite unless the domain which that process belongs to
485 * has "allow_rewrite /var/log/messages" entry.
486 *
487 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
488 * when we want to allow rewriting already unlink()ed file. As of now,
489 * LSM version of TOMOYO is using __d_path() for calculating pathname.
490 * Non LSM version of TOMOYO is using its own function which doesn't append
491 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
492 * need to worry whether the file is already unlink()ed or not.
493 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900494LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900495
496/**
497 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
498 *
499 * @pattern: Pathname pattern that are not rewritable by default.
500 * @is_delete: True if it is a delete request.
501 *
502 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900503 *
504 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900505 */
506static int tomoyo_update_no_rewrite_entry(const char *pattern,
507 const bool is_delete)
508{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900509 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900510 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900511 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900512
Tetsuo Handa17080002010-02-16 21:14:48 +0900513 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900514 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900515 e.pattern = tomoyo_get_name(pattern);
516 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900517 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900518 if (mutex_lock_interruptible(&tomoyo_policy_lock))
519 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900520 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900521 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900522 continue;
523 ptr->is_deleted = is_delete;
524 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900525 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900526 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900527 if (!is_delete && error) {
528 struct tomoyo_no_rewrite_entry *entry =
529 tomoyo_commit_ok(&e, sizeof(e));
530 if (entry) {
531 list_add_tail_rcu(&entry->list,
532 &tomoyo_no_rewrite_list);
533 error = 0;
534 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900535 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900536 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900537 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900538 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900539 return error;
540}
541
542/**
543 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
544 *
545 * @filename: Filename to check.
546 *
547 * Returns true if @filename is specified by "deny_rewrite" directive,
548 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900549 *
550 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900551 */
552static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
553{
554 struct tomoyo_no_rewrite_entry *ptr;
555 bool found = false;
556
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900557 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900558 if (ptr->is_deleted)
559 continue;
560 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
561 continue;
562 found = true;
563 break;
564 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900565 return found;
566}
567
568/**
569 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
570 *
571 * @data: String to parse.
572 * @is_delete: True if it is a delete request.
573 *
574 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900575 *
576 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900577 */
578int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
579{
580 return tomoyo_update_no_rewrite_entry(data, is_delete);
581}
582
583/**
584 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
585 *
586 * @head: Pointer to "struct tomoyo_io_buffer".
587 *
588 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900589 *
590 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900591 */
592bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
593{
594 struct list_head *pos;
595 bool done = true;
596
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900597 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
598 struct tomoyo_no_rewrite_entry *ptr;
599 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
600 if (ptr->is_deleted)
601 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900602 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
603 "%s\n", ptr->pattern->name);
604 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900605 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900606 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900607 return done;
608}
609
610/**
611 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
612 *
613 * @filename: Filename.
614 * @perm: Permission (between 1 to 7).
615 * @domain: Pointer to "struct tomoyo_domain_info".
616 * @is_delete: True if it is a delete request.
617 *
618 * Returns 0 on success, negative value otherwise.
619 *
620 * This is legacy support interface for older policy syntax.
621 * Current policy syntax uses "allow_read/write" instead of "6",
622 * "allow_read" instead of "4", "allow_write" instead of "2",
623 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900624 *
625 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900626 */
627static int tomoyo_update_file_acl(const char *filename, u8 perm,
628 struct tomoyo_domain_info * const domain,
629 const bool is_delete)
630{
631 if (perm > 7 || !perm) {
632 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
633 __func__, perm, filename);
634 return -EINVAL;
635 }
636 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
637 /*
638 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
639 * directory permissions.
640 */
641 return 0;
642 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900643 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
644 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900645 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900646 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
647 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900648 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900649 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
650 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900651 return 0;
652}
653
654/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900655 * tomoyo_path_acl2 - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900656 *
657 * @domain: Pointer to "struct tomoyo_domain_info".
658 * @filename: Filename to check.
659 * @perm: Permission.
660 * @may_use_pattern: True if patterned ACL is permitted.
661 *
662 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900663 *
664 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900665 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900666static int tomoyo_path_acl2(const struct tomoyo_domain_info *domain,
667 const struct tomoyo_path_info *filename,
668 const u32 perm, const bool may_use_pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900669{
670 struct tomoyo_acl_info *ptr;
671 int error = -EPERM;
672
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900673 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900674 struct tomoyo_path_acl *acl;
675 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900676 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900677 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900678 if (perm <= 0xFFFF) {
679 if (!(acl->perm & perm))
680 continue;
681 } else {
682 if (!(acl->perm_high & (perm >> 16)))
683 continue;
684 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900685 if (!tomoyo_compare_name_union_pattern(filename, &acl->name,
686 may_use_pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900687 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900688 error = 0;
689 break;
690 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900691 return error;
692}
693
694/**
695 * tomoyo_check_file_acl - Check permission for opening files.
696 *
697 * @domain: Pointer to "struct tomoyo_domain_info".
698 * @filename: Filename to check.
699 * @operation: Mode ("read" or "write" or "read/write" or "execute").
700 *
701 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900702 *
703 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900704 */
705static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
706 const struct tomoyo_path_info *filename,
707 const u8 operation)
708{
Tetsuo Handa937bf612009-12-02 21:09:48 +0900709 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900710
711 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
712 return 0;
713 if (operation == 6)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900714 perm = 1 << TOMOYO_TYPE_READ_WRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900715 else if (operation == 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900716 perm = 1 << TOMOYO_TYPE_READ;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900717 else if (operation == 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900718 perm = 1 << TOMOYO_TYPE_WRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900719 else if (operation == 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900720 perm = 1 << TOMOYO_TYPE_EXECUTE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900721 else
722 BUG();
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900723 return tomoyo_path_acl2(domain, filename, perm, operation != 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900724}
725
726/**
727 * tomoyo_check_file_perm2 - Check permission for opening files.
728 *
729 * @domain: Pointer to "struct tomoyo_domain_info".
730 * @filename: Filename to check.
731 * @perm: Mode ("read" or "write" or "read/write" or "execute").
732 * @operation: Operation name passed used for verbose mode.
733 * @mode: Access control mode.
734 *
735 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900736 *
737 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900738 */
739static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain,
740 const struct tomoyo_path_info *filename,
741 const u8 perm, const char *operation,
742 const u8 mode)
743{
744 const bool is_enforce = (mode == 3);
745 const char *msg = "<unknown>";
746 int error = 0;
747
748 if (!filename)
749 return 0;
750 error = tomoyo_check_file_acl(domain, filename, perm);
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900751 if (error && perm == 4 && !domain->ignore_global_allow_read
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900752 && tomoyo_is_globally_readable_file(filename))
753 error = 0;
754 if (perm == 6)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900755 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900756 else if (perm == 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900757 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900758 else if (perm == 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900759 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900760 else if (perm == 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900761 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900762 else
763 BUG();
764 if (!error)
765 return 0;
766 if (tomoyo_verbose_mode(domain))
767 printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied "
768 "for %s\n", tomoyo_get_msg(is_enforce), msg, operation,
769 filename->name, tomoyo_get_last_name(domain));
770 if (is_enforce)
771 return error;
772 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
773 /* Don't use patterns for execute permission. */
774 const struct tomoyo_path_info *patterned_file = (perm != 1) ?
775 tomoyo_get_file_pattern(filename) : filename;
776 tomoyo_update_file_acl(patterned_file->name, perm,
777 domain, false);
778 }
779 return 0;
780}
781
782/**
783 * tomoyo_write_file_policy - Update file related list.
784 *
785 * @data: String to parse.
786 * @domain: Pointer to "struct tomoyo_domain_info".
787 * @is_delete: True if it is a delete request.
788 *
789 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900790 *
791 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900792 */
793int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
794 const bool is_delete)
795{
796 char *filename = strchr(data, ' ');
797 char *filename2;
798 unsigned int perm;
799 u8 type;
800
801 if (!filename)
802 return -EINVAL;
803 *filename++ = '\0';
804 if (sscanf(data, "%u", &perm) == 1)
805 return tomoyo_update_file_acl(filename, (u8) perm, domain,
806 is_delete);
807 if (strncmp(data, "allow_", 6))
808 goto out;
809 data += 6;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900810 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
811 if (strcmp(data, tomoyo_path_keyword[type]))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900812 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900813 return tomoyo_update_path_acl(type, filename, domain,
814 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900815 }
816 filename2 = strchr(filename, ' ');
817 if (!filename2)
818 goto out;
819 *filename2++ = '\0';
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900820 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
821 if (strcmp(data, tomoyo_path2_keyword[type]))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900822 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900823 return tomoyo_update_path2_acl(type, filename, filename2,
824 domain, is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900825 }
826 out:
827 return -EINVAL;
828}
829
830/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900831 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900832 *
833 * @type: Type of operation.
834 * @filename: Filename.
835 * @domain: Pointer to "struct tomoyo_domain_info".
836 * @is_delete: True if it is a delete request.
837 *
838 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900839 *
840 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900841 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900842static int tomoyo_update_path_acl(const u8 type, const char *filename,
843 struct tomoyo_domain_info *const domain,
844 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900845{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900846 static const u32 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900847 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900848 const u32 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900849 struct tomoyo_acl_info *ptr;
850 struct tomoyo_path_acl e = {
851 .head.type = TOMOYO_TYPE_PATH_ACL,
852 .perm_high = perm >> 16,
853 .perm = perm
854 };
855 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900856
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900857 if (type == TOMOYO_TYPE_READ_WRITE)
858 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900859 if (!domain)
860 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900861 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900862 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900863 if (mutex_lock_interruptible(&tomoyo_policy_lock))
864 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900865 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900866 struct tomoyo_path_acl *acl =
867 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900868 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900869 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900870 if (is_delete) {
871 if (perm <= 0xFFFF)
872 acl->perm &= ~perm;
873 else
874 acl->perm_high &= ~(perm >> 16);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900875 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900876 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
877 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900878 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900879 } else {
880 if (perm <= 0xFFFF)
881 acl->perm |= perm;
882 else
883 acl->perm_high |= (perm >> 16);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900884 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900885 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
886 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900887 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900888 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900889 error = 0;
890 break;
891 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900892 if (!is_delete && error) {
893 struct tomoyo_path_acl *entry =
894 tomoyo_commit_ok(&e, sizeof(e));
895 if (entry) {
896 list_add_tail_rcu(&entry->head.list,
897 &domain->acl_info_list);
898 error = 0;
899 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900900 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900901 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900902 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900903 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900904 return error;
905}
906
907/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900908 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900909 *
910 * @type: Type of operation.
911 * @filename1: First filename.
912 * @filename2: Second filename.
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.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900917 *
918 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900919 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900920static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
921 const char *filename2,
922 struct tomoyo_domain_info *const domain,
923 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900924{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900925 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900926 struct tomoyo_path2_acl e = {
927 .head.type = TOMOYO_TYPE_PATH2_ACL,
928 .perm = perm
929 };
930 struct tomoyo_acl_info *ptr;
931 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900932
933 if (!domain)
934 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900935 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
936 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900937 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900938 if (mutex_lock_interruptible(&tomoyo_policy_lock))
939 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900940 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900941 struct tomoyo_path2_acl *acl =
942 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900943 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900944 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900945 if (is_delete)
946 acl->perm &= ~perm;
947 else
948 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900949 error = 0;
950 break;
951 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900952 if (!is_delete && error) {
953 struct tomoyo_path2_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 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900960 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900961 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900962 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900963 tomoyo_put_name_union(&e.name1);
964 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900965 return error;
966}
967
968/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900969 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900970 *
971 * @domain: Pointer to "struct tomoyo_domain_info".
972 * @type: Type of operation.
973 * @filename: Filename to check.
974 *
975 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900976 *
977 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900978 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900979static int tomoyo_path_acl(struct tomoyo_domain_info *domain, const u8 type,
980 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900981{
982 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
983 return 0;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900984 return tomoyo_path_acl2(domain, filename, 1 << type, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900985}
986
987/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900988 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900989 *
990 * @domain: Pointer to "struct tomoyo_domain_info".
991 * @type: Type of operation.
992 * @filename1: First filename to check.
993 * @filename2: Second filename to check.
994 *
995 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900996 *
997 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900998 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900999static int tomoyo_path2_acl(const struct tomoyo_domain_info *domain,
1000 const u8 type,
1001 const struct tomoyo_path_info *filename1,
1002 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001003{
1004 struct tomoyo_acl_info *ptr;
1005 const u8 perm = 1 << type;
1006 int error = -EPERM;
1007
1008 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
1009 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001010 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001011 struct tomoyo_path2_acl *acl;
1012 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001013 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001014 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001015 if (!(acl->perm & perm))
1016 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001017 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001018 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001019 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001020 continue;
1021 error = 0;
1022 break;
1023 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001024 return error;
1025}
1026
1027/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001028 * tomoyo_path_permission2 - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001029 *
1030 * @domain: Pointer to "struct tomoyo_domain_info".
1031 * @operation: Type of operation.
1032 * @filename: Filename to check.
1033 * @mode: Access control mode.
1034 *
1035 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001036 *
1037 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001038 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001039static int tomoyo_path_permission2(struct tomoyo_domain_info *const domain,
1040 u8 operation,
1041 const struct tomoyo_path_info *filename,
1042 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043{
1044 const char *msg;
1045 int error;
1046 const bool is_enforce = (mode == 3);
1047
1048 if (!mode)
1049 return 0;
1050 next:
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001051 error = tomoyo_path_acl(domain, operation, filename);
1052 msg = tomoyo_path2keyword(operation);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001053 if (!error)
1054 goto ok;
1055 if (tomoyo_verbose_mode(domain))
1056 printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n",
1057 tomoyo_get_msg(is_enforce), msg, filename->name,
1058 tomoyo_get_last_name(domain));
1059 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1060 const char *name = tomoyo_get_file_pattern(filename)->name;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001061 tomoyo_update_path_acl(operation, name, domain, false);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001062 }
1063 if (!is_enforce)
1064 error = 0;
1065 ok:
1066 /*
1067 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1068 * we need to check "allow_rewrite" permission if the filename is
1069 * specified by "deny_rewrite" keyword.
1070 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001071 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001072 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001073 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001074 goto next;
1075 }
1076 return error;
1077}
1078
1079/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001080 * tomoyo_check_exec_perm - Check permission for "execute".
1081 *
1082 * @domain: Pointer to "struct tomoyo_domain_info".
1083 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001084 *
1085 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001086 *
1087 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001088 */
1089int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001090 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001091{
1092 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1093
1094 if (!mode)
1095 return 0;
1096 return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode);
1097}
1098
1099/**
1100 * tomoyo_check_open_permission - Check permission for "read" and "write".
1101 *
1102 * @domain: Pointer to "struct tomoyo_domain_info".
1103 * @path: Pointer to "struct path".
1104 * @flag: Flags for open().
1105 *
1106 * Returns 0 on success, negative value otherwise.
1107 */
1108int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1109 struct path *path, const int flag)
1110{
1111 const u8 acc_mode = ACC_MODE(flag);
1112 int error = -ENOMEM;
1113 struct tomoyo_path_info *buf;
1114 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1115 const bool is_enforce = (mode == 3);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001116 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001117
1118 if (!mode || !path->mnt)
1119 return 0;
1120 if (acc_mode == 0)
1121 return 0;
1122 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1123 /*
1124 * I don't check directories here because mkdir() and rmdir()
1125 * don't call me.
1126 */
1127 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001128 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001129 buf = tomoyo_get_path(path);
1130 if (!buf)
1131 goto out;
1132 error = 0;
1133 /*
1134 * If the filename is specified by "deny_rewrite" keyword,
1135 * we need to check "allow_rewrite" permission when the filename is not
1136 * opened for append mode or the filename is truncated at open time.
1137 */
1138 if ((acc_mode & MAY_WRITE) &&
1139 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1140 (tomoyo_is_no_rewrite_file(buf))) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001141 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE,
1142 buf, mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001143 }
1144 if (!error)
1145 error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
1146 mode);
1147 if (!error && (flag & O_TRUNC))
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001148 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_TRUNCATE,
1149 buf, mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001150 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001151 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001152 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001153 if (!is_enforce)
1154 error = 0;
1155 return error;
1156}
1157
1158/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001159 * tomoyo_path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001160 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001161 * @operation: Type of operation.
1162 * @path: Pointer to "struct path".
1163 *
1164 * Returns 0 on success, negative value otherwise.
1165 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001166int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001167{
1168 int error = -ENOMEM;
1169 struct tomoyo_path_info *buf;
Tetsuo Handa97d69312010-02-16 09:46:15 +09001170 struct tomoyo_domain_info *domain = tomoyo_domain();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001171 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1172 const bool is_enforce = (mode == 3);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001173 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001174
1175 if (!mode || !path->mnt)
1176 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001177 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001178 buf = tomoyo_get_path(path);
1179 if (!buf)
1180 goto out;
1181 switch (operation) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001182 case TOMOYO_TYPE_MKDIR:
1183 case TOMOYO_TYPE_RMDIR:
1184 case TOMOYO_TYPE_CHROOT:
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001185 if (!buf->is_dir) {
1186 /*
1187 * tomoyo_get_path() reserves space for appending "/."
1188 */
1189 strcat((char *) buf->name, "/");
1190 tomoyo_fill_path_info(buf);
1191 }
1192 }
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001193 error = tomoyo_path_permission2(domain, operation, buf, mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001194 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001195 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001196 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001197 if (!is_enforce)
1198 error = 0;
1199 return error;
1200}
1201
1202/**
1203 * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1204 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001205 * @filp: Pointer to "struct file".
1206 *
1207 * Returns 0 on success, negative value otherwise.
1208 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001209int tomoyo_check_rewrite_permission(struct file *filp)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001210{
1211 int error = -ENOMEM;
Tetsuo Handa97d69312010-02-16 09:46:15 +09001212 struct tomoyo_domain_info *domain = tomoyo_domain();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001213 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1214 const bool is_enforce = (mode == 3);
1215 struct tomoyo_path_info *buf;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001216 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001217
1218 if (!mode || !filp->f_path.mnt)
1219 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001220
1221 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001222 buf = tomoyo_get_path(&filp->f_path);
1223 if (!buf)
1224 goto out;
1225 if (!tomoyo_is_no_rewrite_file(buf)) {
1226 error = 0;
1227 goto out;
1228 }
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001229 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE, buf, mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001230 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001231 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001232 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001233 if (!is_enforce)
1234 error = 0;
1235 return error;
1236}
1237
1238/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001239 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001240 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001241 * @operation: Type of operation.
1242 * @path1: Pointer to "struct path".
1243 * @path2: Pointer to "struct path".
1244 *
1245 * Returns 0 on success, negative value otherwise.
1246 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001247int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001248 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001249{
1250 int error = -ENOMEM;
1251 struct tomoyo_path_info *buf1, *buf2;
Tetsuo Handa97d69312010-02-16 09:46:15 +09001252 struct tomoyo_domain_info *domain = tomoyo_domain();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001253 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1254 const bool is_enforce = (mode == 3);
1255 const char *msg;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001256 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001257
1258 if (!mode || !path1->mnt || !path2->mnt)
1259 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001260 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001261 buf1 = tomoyo_get_path(path1);
1262 buf2 = tomoyo_get_path(path2);
1263 if (!buf1 || !buf2)
1264 goto out;
1265 {
1266 struct dentry *dentry = path1->dentry;
1267 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1268 /*
1269 * tomoyo_get_path() reserves space for appending "/."
1270 */
1271 if (!buf1->is_dir) {
1272 strcat((char *) buf1->name, "/");
1273 tomoyo_fill_path_info(buf1);
1274 }
1275 if (!buf2->is_dir) {
1276 strcat((char *) buf2->name, "/");
1277 tomoyo_fill_path_info(buf2);
1278 }
1279 }
1280 }
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001281 error = tomoyo_path2_acl(domain, operation, buf1, buf2);
1282 msg = tomoyo_path22keyword(operation);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001283 if (!error)
1284 goto out;
1285 if (tomoyo_verbose_mode(domain))
1286 printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' "
1287 "denied for %s\n", tomoyo_get_msg(is_enforce),
1288 msg, buf1->name, buf2->name,
1289 tomoyo_get_last_name(domain));
1290 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1291 const char *name1 = tomoyo_get_file_pattern(buf1)->name;
1292 const char *name2 = tomoyo_get_file_pattern(buf2)->name;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001293 tomoyo_update_path2_acl(operation, name1, name2, domain,
1294 false);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001295 }
1296 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001297 kfree(buf1);
1298 kfree(buf2);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001299 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001300 if (!is_enforce)
1301 error = 0;
1302 return error;
1303}