blob: a49e18cc7bc22ff524e36c5a8c11c0c4b10cff84 [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"
13#include "tomoyo.h"
14#include "realpath.h"
Kentaro Takedab69a54e2009-02-05 17:18:14 +090015
Tetsuo Handac3fa1092009-06-08 12:37:39 +090016/*
17 * tomoyo_globally_readable_file_entry is a structure which is used for holding
18 * "allow_read" entries.
19 * It has following fields.
20 *
21 * (1) "list" which is linked to tomoyo_globally_readable_list .
22 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
23 * (3) "is_deleted" is a bool which is true if marked as deleted, false
24 * otherwise.
25 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +090026struct tomoyo_globally_readable_file_entry {
27 struct list_head list;
28 const struct tomoyo_path_info *filename;
29 bool is_deleted;
30};
31
Tetsuo Handac3fa1092009-06-08 12:37:39 +090032/*
33 * tomoyo_pattern_entry is a structure which is used for holding
34 * "tomoyo_pattern_list" entries.
35 * It has following fields.
36 *
37 * (1) "list" which is linked to tomoyo_pattern_list .
38 * (2) "pattern" is a pathname pattern which is used for converting pathnames
39 * to pathname patterns during learning mode.
40 * (3) "is_deleted" is a bool which is true if marked as deleted, false
41 * otherwise.
42 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +090043struct tomoyo_pattern_entry {
44 struct list_head list;
45 const struct tomoyo_path_info *pattern;
46 bool is_deleted;
47};
48
Tetsuo Handac3fa1092009-06-08 12:37:39 +090049/*
50 * tomoyo_no_rewrite_entry is a structure which is used for holding
51 * "deny_rewrite" entries.
52 * It has following fields.
53 *
54 * (1) "list" which is linked to tomoyo_no_rewrite_list .
55 * (2) "pattern" is a pathname which is by default not permitted to modify
56 * already existing content.
57 * (3) "is_deleted" is a bool which is true if marked as deleted, false
58 * otherwise.
59 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +090060struct tomoyo_no_rewrite_entry {
61 struct list_head list;
62 const struct tomoyo_path_info *pattern;
63 bool is_deleted;
64};
65
66/* Keyword array for single path operations. */
67static const char *tomoyo_sp_keyword[TOMOYO_MAX_SINGLE_PATH_OPERATION] = {
68 [TOMOYO_TYPE_READ_WRITE_ACL] = "read/write",
69 [TOMOYO_TYPE_EXECUTE_ACL] = "execute",
70 [TOMOYO_TYPE_READ_ACL] = "read",
71 [TOMOYO_TYPE_WRITE_ACL] = "write",
72 [TOMOYO_TYPE_CREATE_ACL] = "create",
73 [TOMOYO_TYPE_UNLINK_ACL] = "unlink",
74 [TOMOYO_TYPE_MKDIR_ACL] = "mkdir",
75 [TOMOYO_TYPE_RMDIR_ACL] = "rmdir",
76 [TOMOYO_TYPE_MKFIFO_ACL] = "mkfifo",
77 [TOMOYO_TYPE_MKSOCK_ACL] = "mksock",
78 [TOMOYO_TYPE_MKBLOCK_ACL] = "mkblock",
79 [TOMOYO_TYPE_MKCHAR_ACL] = "mkchar",
80 [TOMOYO_TYPE_TRUNCATE_ACL] = "truncate",
81 [TOMOYO_TYPE_SYMLINK_ACL] = "symlink",
82 [TOMOYO_TYPE_REWRITE_ACL] = "rewrite",
Tetsuo Handa937bf612009-12-02 21:09:48 +090083 [TOMOYO_TYPE_IOCTL_ACL] = "ioctl",
84 [TOMOYO_TYPE_CHMOD_ACL] = "chmod",
85 [TOMOYO_TYPE_CHOWN_ACL] = "chown",
86 [TOMOYO_TYPE_CHGRP_ACL] = "chgrp",
87 [TOMOYO_TYPE_CHROOT_ACL] = "chroot",
88 [TOMOYO_TYPE_MOUNT_ACL] = "mount",
89 [TOMOYO_TYPE_UMOUNT_ACL] = "unmount",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090090};
91
92/* Keyword array for double path operations. */
93static const char *tomoyo_dp_keyword[TOMOYO_MAX_DOUBLE_PATH_OPERATION] = {
94 [TOMOYO_TYPE_LINK_ACL] = "link",
95 [TOMOYO_TYPE_RENAME_ACL] = "rename",
Tetsuo Handa937bf612009-12-02 21:09:48 +090096 [TOMOYO_TYPE_PIVOT_ROOT_ACL] = "pivot_root",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090097};
98
99/**
100 * tomoyo_sp2keyword - Get the name of single path operation.
101 *
102 * @operation: Type of operation.
103 *
104 * Returns the name of single path operation.
105 */
106const char *tomoyo_sp2keyword(const u8 operation)
107{
108 return (operation < TOMOYO_MAX_SINGLE_PATH_OPERATION)
109 ? tomoyo_sp_keyword[operation] : NULL;
110}
111
112/**
113 * tomoyo_dp2keyword - Get the name of double path operation.
114 *
115 * @operation: Type of operation.
116 *
117 * Returns the name of double path operation.
118 */
119const char *tomoyo_dp2keyword(const u8 operation)
120{
121 return (operation < TOMOYO_MAX_DOUBLE_PATH_OPERATION)
122 ? tomoyo_dp_keyword[operation] : NULL;
123}
124
125/**
126 * tomoyo_strendswith - Check whether the token ends with the given token.
127 *
128 * @name: The token to check.
129 * @tail: The token to find.
130 *
131 * Returns true if @name ends with @tail, false otherwise.
132 */
133static bool tomoyo_strendswith(const char *name, const char *tail)
134{
135 int len;
136
137 if (!name || !tail)
138 return false;
139 len = strlen(name) - strlen(tail);
140 return len >= 0 && !strcmp(name + len, tail);
141}
142
143/**
144 * tomoyo_get_path - Get realpath.
145 *
146 * @path: Pointer to "struct path".
147 *
148 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
149 */
150static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
151{
152 int error;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900153 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
154 GFP_KERNEL);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900155
156 if (!buf)
157 return NULL;
158 /* Reserve one byte for appending "/". */
159 error = tomoyo_realpath_from_path2(path, buf->body,
160 sizeof(buf->body) - 2);
161 if (!error) {
162 buf->head.name = buf->body;
163 tomoyo_fill_path_info(&buf->head);
164 return &buf->head;
165 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900166 kfree(buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900167 return NULL;
168}
169
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900170static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
171 const char *filename2,
172 struct tomoyo_domain_info *
173 const domain, const bool is_delete);
174static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
175 struct tomoyo_domain_info *
176 const domain, const bool is_delete);
177
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900178/*
179 * tomoyo_globally_readable_list is used for holding list of pathnames which
180 * are by default allowed to be open()ed for reading by any process.
181 *
182 * An entry is added by
183 *
184 * # echo 'allow_read /lib/libc-2.5.so' > \
185 * /sys/kernel/security/tomoyo/exception_policy
186 *
187 * and is deleted by
188 *
189 * # echo 'delete allow_read /lib/libc-2.5.so' > \
190 * /sys/kernel/security/tomoyo/exception_policy
191 *
192 * and all entries are retrieved by
193 *
194 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
195 *
196 * In the example above, any process is allowed to
197 * open("/lib/libc-2.5.so", O_RDONLY).
198 * One exception is, if the domain which current process belongs to is marked
199 * as "ignore_global_allow_read", current process can't do so unless explicitly
200 * given "allow_read /lib/libc-2.5.so" to the domain which current process
201 * belongs to.
202 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900203static LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900204
205/**
206 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
207 *
208 * @filename: Filename unconditionally permitted to open() for reading.
209 * @is_delete: True if it is a delete request.
210 *
211 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900212 *
213 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900214 */
215static int tomoyo_update_globally_readable_entry(const char *filename,
216 const bool is_delete)
217{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900218 struct tomoyo_globally_readable_file_entry *entry = NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900219 struct tomoyo_globally_readable_file_entry *ptr;
220 const struct tomoyo_path_info *saved_filename;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900221 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900222
223 if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__))
224 return -EINVAL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900225 saved_filename = tomoyo_get_name(filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900226 if (!saved_filename)
227 return -ENOMEM;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900228 if (!is_delete)
229 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +0900230 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900231 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900232 if (ptr->filename != saved_filename)
233 continue;
234 ptr->is_deleted = is_delete;
235 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900236 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900237 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900238 if (!is_delete && error && tomoyo_memory_ok(entry)) {
239 entry->filename = saved_filename;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900240 saved_filename = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900241 list_add_tail_rcu(&entry->list, &tomoyo_globally_readable_list);
242 entry = NULL;
243 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900244 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900245 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900246 tomoyo_put_name(saved_filename);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900247 kfree(entry);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900248 return error;
249}
250
251/**
252 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
253 *
254 * @filename: The filename to check.
255 *
256 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900257 *
258 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900259 */
260static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
261 filename)
262{
263 struct tomoyo_globally_readable_file_entry *ptr;
264 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900265
266 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900267 if (!ptr->is_deleted &&
268 tomoyo_path_matches_pattern(filename, ptr->filename)) {
269 found = true;
270 break;
271 }
272 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900273 return found;
274}
275
276/**
277 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
278 *
279 * @data: String to parse.
280 * @is_delete: True if it is a delete request.
281 *
282 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900283 *
284 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900285 */
286int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
287{
288 return tomoyo_update_globally_readable_entry(data, is_delete);
289}
290
291/**
292 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
293 *
294 * @head: Pointer to "struct tomoyo_io_buffer".
295 *
296 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900297 *
298 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900299 */
300bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
301{
302 struct list_head *pos;
303 bool done = true;
304
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900305 list_for_each_cookie(pos, head->read_var2,
306 &tomoyo_globally_readable_list) {
307 struct tomoyo_globally_readable_file_entry *ptr;
308 ptr = list_entry(pos,
309 struct tomoyo_globally_readable_file_entry,
310 list);
311 if (ptr->is_deleted)
312 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900313 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
314 ptr->filename->name);
315 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900316 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900317 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900318 return done;
319}
320
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900321/* tomoyo_pattern_list is used for holding list of pathnames which are used for
322 * converting pathnames to pathname patterns during learning mode.
323 *
324 * An entry is added by
325 *
326 * # echo 'file_pattern /proc/\$/mounts' > \
327 * /sys/kernel/security/tomoyo/exception_policy
328 *
329 * and is deleted by
330 *
331 * # echo 'delete file_pattern /proc/\$/mounts' > \
332 * /sys/kernel/security/tomoyo/exception_policy
333 *
334 * and all entries are retrieved by
335 *
336 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
337 *
338 * In the example above, if a process which belongs to a domain which is in
339 * learning mode requested open("/proc/1/mounts", O_RDONLY),
340 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
341 * process belongs to.
342 *
343 * It is not a desirable behavior that we have to use /proc/\$/ instead of
344 * /proc/self/ when current process needs to access only current process's
345 * information. As of now, LSM version of TOMOYO is using __d_path() for
346 * calculating pathname. Non LSM version of TOMOYO is using its own function
347 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
348 * current process from accessing other process's information.
349 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900350static LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900351
352/**
353 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
354 *
355 * @pattern: Pathname pattern.
356 * @is_delete: True if it is a delete request.
357 *
358 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900359 *
360 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900361 */
362static int tomoyo_update_file_pattern_entry(const char *pattern,
363 const bool is_delete)
364{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900365 struct tomoyo_pattern_entry *entry = NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900366 struct tomoyo_pattern_entry *ptr;
367 const struct tomoyo_path_info *saved_pattern;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900368 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900369
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900370 saved_pattern = tomoyo_get_name(pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900371 if (!saved_pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900372 return error;
373 if (!saved_pattern->is_patterned)
374 goto out;
375 if (!is_delete)
376 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +0900377 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900378 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900379 if (saved_pattern != ptr->pattern)
380 continue;
381 ptr->is_deleted = is_delete;
382 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900383 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900384 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900385 if (!is_delete && error && tomoyo_memory_ok(entry)) {
386 entry->pattern = saved_pattern;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900387 saved_pattern = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900388 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
389 entry = NULL;
390 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900391 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900392 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900393 out:
394 kfree(entry);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900395 tomoyo_put_name(saved_pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900396 return error;
397}
398
399/**
400 * tomoyo_get_file_pattern - Get patterned pathname.
401 *
402 * @filename: The filename to find patterned pathname.
403 *
404 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900405 *
406 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900407 */
408static const struct tomoyo_path_info *
409tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
410{
411 struct tomoyo_pattern_entry *ptr;
412 const struct tomoyo_path_info *pattern = NULL;
413
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900414 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900415 if (ptr->is_deleted)
416 continue;
417 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
418 continue;
419 pattern = ptr->pattern;
420 if (tomoyo_strendswith(pattern->name, "/\\*")) {
421 /* Do nothing. Try to find the better match. */
422 } else {
423 /* This would be the better match. Use this. */
424 break;
425 }
426 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900427 if (pattern)
428 filename = pattern;
429 return filename;
430}
431
432/**
433 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
434 *
435 * @data: String to parse.
436 * @is_delete: True if it is a delete request.
437 *
438 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900439 *
440 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900441 */
442int tomoyo_write_pattern_policy(char *data, const bool is_delete)
443{
444 return tomoyo_update_file_pattern_entry(data, is_delete);
445}
446
447/**
448 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
449 *
450 * @head: Pointer to "struct tomoyo_io_buffer".
451 *
452 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900453 *
454 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900455 */
456bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
457{
458 struct list_head *pos;
459 bool done = true;
460
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900461 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
462 struct tomoyo_pattern_entry *ptr;
463 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
464 if (ptr->is_deleted)
465 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900466 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
467 "%s\n", ptr->pattern->name);
468 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900469 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900470 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900471 return done;
472}
473
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900474/*
475 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
476 * default forbidden to modify already written content of a file.
477 *
478 * An entry is added by
479 *
480 * # echo 'deny_rewrite /var/log/messages' > \
481 * /sys/kernel/security/tomoyo/exception_policy
482 *
483 * and is deleted by
484 *
485 * # echo 'delete deny_rewrite /var/log/messages' > \
486 * /sys/kernel/security/tomoyo/exception_policy
487 *
488 * and all entries are retrieved by
489 *
490 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
491 *
492 * In the example above, if a process requested to rewrite /var/log/messages ,
493 * the process can't rewrite unless the domain which that process belongs to
494 * has "allow_rewrite /var/log/messages" entry.
495 *
496 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
497 * when we want to allow rewriting already unlink()ed file. As of now,
498 * LSM version of TOMOYO is using __d_path() for calculating pathname.
499 * Non LSM version of TOMOYO is using its own function which doesn't append
500 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
501 * need to worry whether the file is already unlink()ed or not.
502 */
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900503static LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900504
505/**
506 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
507 *
508 * @pattern: Pathname pattern that are not rewritable by default.
509 * @is_delete: True if it is a delete request.
510 *
511 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900512 *
513 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900514 */
515static int tomoyo_update_no_rewrite_entry(const char *pattern,
516 const bool is_delete)
517{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900518 struct tomoyo_no_rewrite_entry *entry = NULL;
519 struct tomoyo_no_rewrite_entry *ptr;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900520 const struct tomoyo_path_info *saved_pattern;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900521 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900522
523 if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__))
524 return -EINVAL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900525 saved_pattern = tomoyo_get_name(pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900526 if (!saved_pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900527 return error;
528 if (!is_delete)
529 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +0900530 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900531 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900532 if (ptr->pattern != saved_pattern)
533 continue;
534 ptr->is_deleted = is_delete;
535 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900536 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900537 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900538 if (!is_delete && error && tomoyo_memory_ok(entry)) {
539 entry->pattern = saved_pattern;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900540 saved_pattern = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900541 list_add_tail_rcu(&entry->list, &tomoyo_no_rewrite_list);
542 entry = NULL;
543 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900544 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900545 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900546 tomoyo_put_name(saved_pattern);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900547 kfree(entry);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900548 return error;
549}
550
551/**
552 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
553 *
554 * @filename: Filename to check.
555 *
556 * Returns true if @filename is specified by "deny_rewrite" directive,
557 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900558 *
559 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900560 */
561static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
562{
563 struct tomoyo_no_rewrite_entry *ptr;
564 bool found = false;
565
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900566 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900567 if (ptr->is_deleted)
568 continue;
569 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
570 continue;
571 found = true;
572 break;
573 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900574 return found;
575}
576
577/**
578 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
579 *
580 * @data: String to parse.
581 * @is_delete: True if it is a delete request.
582 *
583 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900584 *
585 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900586 */
587int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
588{
589 return tomoyo_update_no_rewrite_entry(data, is_delete);
590}
591
592/**
593 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
594 *
595 * @head: Pointer to "struct tomoyo_io_buffer".
596 *
597 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900598 *
599 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900600 */
601bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
602{
603 struct list_head *pos;
604 bool done = true;
605
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900606 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
607 struct tomoyo_no_rewrite_entry *ptr;
608 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
609 if (ptr->is_deleted)
610 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900611 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
612 "%s\n", ptr->pattern->name);
613 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900614 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900615 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900616 return done;
617}
618
619/**
620 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
621 *
622 * @filename: Filename.
623 * @perm: Permission (between 1 to 7).
624 * @domain: Pointer to "struct tomoyo_domain_info".
625 * @is_delete: True if it is a delete request.
626 *
627 * Returns 0 on success, negative value otherwise.
628 *
629 * This is legacy support interface for older policy syntax.
630 * Current policy syntax uses "allow_read/write" instead of "6",
631 * "allow_read" instead of "4", "allow_write" instead of "2",
632 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900633 *
634 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900635 */
636static int tomoyo_update_file_acl(const char *filename, u8 perm,
637 struct tomoyo_domain_info * const domain,
638 const bool is_delete)
639{
640 if (perm > 7 || !perm) {
641 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
642 __func__, perm, filename);
643 return -EINVAL;
644 }
645 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
646 /*
647 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
648 * directory permissions.
649 */
650 return 0;
651 if (perm & 4)
652 tomoyo_update_single_path_acl(TOMOYO_TYPE_READ_ACL, filename,
653 domain, is_delete);
654 if (perm & 2)
655 tomoyo_update_single_path_acl(TOMOYO_TYPE_WRITE_ACL, filename,
656 domain, is_delete);
657 if (perm & 1)
658 tomoyo_update_single_path_acl(TOMOYO_TYPE_EXECUTE_ACL,
659 filename, domain, is_delete);
660 return 0;
661}
662
663/**
664 * tomoyo_check_single_path_acl2 - Check permission for single path operation.
665 *
666 * @domain: Pointer to "struct tomoyo_domain_info".
667 * @filename: Filename to check.
668 * @perm: Permission.
669 * @may_use_pattern: True if patterned ACL is permitted.
670 *
671 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900672 *
673 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900674 */
675static int tomoyo_check_single_path_acl2(const struct tomoyo_domain_info *
676 domain,
677 const struct tomoyo_path_info *
678 filename,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900679 const u32 perm,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900680 const bool may_use_pattern)
681{
682 struct tomoyo_acl_info *ptr;
683 int error = -EPERM;
684
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900685 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900686 struct tomoyo_single_path_acl_record *acl;
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900687 if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900688 continue;
689 acl = container_of(ptr, struct tomoyo_single_path_acl_record,
690 head);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900691 if (perm <= 0xFFFF) {
692 if (!(acl->perm & perm))
693 continue;
694 } else {
695 if (!(acl->perm_high & (perm >> 16)))
696 continue;
697 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900698 if (may_use_pattern || !acl->filename->is_patterned) {
699 if (!tomoyo_path_matches_pattern(filename,
700 acl->filename))
701 continue;
702 } else {
703 continue;
704 }
705 error = 0;
706 break;
707 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900708 return error;
709}
710
711/**
712 * tomoyo_check_file_acl - Check permission for opening files.
713 *
714 * @domain: Pointer to "struct tomoyo_domain_info".
715 * @filename: Filename to check.
716 * @operation: Mode ("read" or "write" or "read/write" or "execute").
717 *
718 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900719 *
720 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900721 */
722static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
723 const struct tomoyo_path_info *filename,
724 const u8 operation)
725{
Tetsuo Handa937bf612009-12-02 21:09:48 +0900726 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900727
728 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
729 return 0;
730 if (operation == 6)
731 perm = 1 << TOMOYO_TYPE_READ_WRITE_ACL;
732 else if (operation == 4)
733 perm = 1 << TOMOYO_TYPE_READ_ACL;
734 else if (operation == 2)
735 perm = 1 << TOMOYO_TYPE_WRITE_ACL;
736 else if (operation == 1)
737 perm = 1 << TOMOYO_TYPE_EXECUTE_ACL;
738 else
739 BUG();
740 return tomoyo_check_single_path_acl2(domain, filename, perm,
741 operation != 1);
742}
743
744/**
745 * tomoyo_check_file_perm2 - Check permission for opening files.
746 *
747 * @domain: Pointer to "struct tomoyo_domain_info".
748 * @filename: Filename to check.
749 * @perm: Mode ("read" or "write" or "read/write" or "execute").
750 * @operation: Operation name passed used for verbose mode.
751 * @mode: Access control mode.
752 *
753 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900754 *
755 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900756 */
757static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain,
758 const struct tomoyo_path_info *filename,
759 const u8 perm, const char *operation,
760 const u8 mode)
761{
762 const bool is_enforce = (mode == 3);
763 const char *msg = "<unknown>";
764 int error = 0;
765
766 if (!filename)
767 return 0;
768 error = tomoyo_check_file_acl(domain, filename, perm);
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900769 if (error && perm == 4 && !domain->ignore_global_allow_read
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900770 && tomoyo_is_globally_readable_file(filename))
771 error = 0;
772 if (perm == 6)
773 msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_WRITE_ACL);
774 else if (perm == 4)
775 msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_ACL);
776 else if (perm == 2)
777 msg = tomoyo_sp2keyword(TOMOYO_TYPE_WRITE_ACL);
778 else if (perm == 1)
779 msg = tomoyo_sp2keyword(TOMOYO_TYPE_EXECUTE_ACL);
780 else
781 BUG();
782 if (!error)
783 return 0;
784 if (tomoyo_verbose_mode(domain))
785 printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied "
786 "for %s\n", tomoyo_get_msg(is_enforce), msg, operation,
787 filename->name, tomoyo_get_last_name(domain));
788 if (is_enforce)
789 return error;
790 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
791 /* Don't use patterns for execute permission. */
792 const struct tomoyo_path_info *patterned_file = (perm != 1) ?
793 tomoyo_get_file_pattern(filename) : filename;
794 tomoyo_update_file_acl(patterned_file->name, perm,
795 domain, false);
796 }
797 return 0;
798}
799
800/**
801 * tomoyo_write_file_policy - Update file related list.
802 *
803 * @data: String to parse.
804 * @domain: Pointer to "struct tomoyo_domain_info".
805 * @is_delete: True if it is a delete request.
806 *
807 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900808 *
809 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900810 */
811int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
812 const bool is_delete)
813{
814 char *filename = strchr(data, ' ');
815 char *filename2;
816 unsigned int perm;
817 u8 type;
818
819 if (!filename)
820 return -EINVAL;
821 *filename++ = '\0';
822 if (sscanf(data, "%u", &perm) == 1)
823 return tomoyo_update_file_acl(filename, (u8) perm, domain,
824 is_delete);
825 if (strncmp(data, "allow_", 6))
826 goto out;
827 data += 6;
828 for (type = 0; type < TOMOYO_MAX_SINGLE_PATH_OPERATION; type++) {
829 if (strcmp(data, tomoyo_sp_keyword[type]))
830 continue;
831 return tomoyo_update_single_path_acl(type, filename,
832 domain, is_delete);
833 }
834 filename2 = strchr(filename, ' ');
835 if (!filename2)
836 goto out;
837 *filename2++ = '\0';
838 for (type = 0; type < TOMOYO_MAX_DOUBLE_PATH_OPERATION; type++) {
839 if (strcmp(data, tomoyo_dp_keyword[type]))
840 continue;
841 return tomoyo_update_double_path_acl(type, filename, filename2,
842 domain, is_delete);
843 }
844 out:
845 return -EINVAL;
846}
847
848/**
849 * tomoyo_update_single_path_acl - Update "struct tomoyo_single_path_acl_record" list.
850 *
851 * @type: Type of operation.
852 * @filename: Filename.
853 * @domain: Pointer to "struct tomoyo_domain_info".
854 * @is_delete: True if it is a delete request.
855 *
856 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900857 *
858 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900859 */
860static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
861 struct tomoyo_domain_info *
862 const domain, const bool is_delete)
863{
Tetsuo Handa937bf612009-12-02 21:09:48 +0900864 static const u32 rw_mask =
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900865 (1 << TOMOYO_TYPE_READ_ACL) | (1 << TOMOYO_TYPE_WRITE_ACL);
866 const struct tomoyo_path_info *saved_filename;
867 struct tomoyo_acl_info *ptr;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900868 struct tomoyo_single_path_acl_record *entry = NULL;
869 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa937bf612009-12-02 21:09:48 +0900870 const u32 perm = 1 << type;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900871
872 if (!domain)
873 return -EINVAL;
874 if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__))
875 return -EINVAL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900876 saved_filename = tomoyo_get_name(filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900877 if (!saved_filename)
878 return -ENOMEM;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900879 if (!is_delete)
880 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +0900881 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900882 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900883 struct tomoyo_single_path_acl_record *acl =
884 container_of(ptr, struct tomoyo_single_path_acl_record,
885 head);
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900886 if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900887 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900888 if (acl->filename != saved_filename)
889 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900890 if (is_delete) {
891 if (perm <= 0xFFFF)
892 acl->perm &= ~perm;
893 else
894 acl->perm_high &= ~(perm >> 16);
895 if ((acl->perm & rw_mask) != rw_mask)
896 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE_ACL);
897 else if (!(acl->perm &
898 (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
899 acl->perm &= ~rw_mask;
900 } else {
901 if (perm <= 0xFFFF)
902 acl->perm |= perm;
903 else
904 acl->perm_high |= (perm >> 16);
905 if ((acl->perm & rw_mask) == rw_mask)
906 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE_ACL;
907 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
908 acl->perm |= rw_mask;
909 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900910 error = 0;
911 break;
912 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900913 if (!is_delete && error && tomoyo_memory_ok(entry)) {
914 entry->head.type = TOMOYO_TYPE_SINGLE_PATH_ACL;
915 if (perm <= 0xFFFF)
916 entry->perm = perm;
917 else
918 entry->perm_high = (perm >> 16);
919 if (perm == (1 << TOMOYO_TYPE_READ_WRITE_ACL))
920 entry->perm |= rw_mask;
921 entry->filename = saved_filename;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900922 saved_filename = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900923 list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
924 entry = NULL;
925 error = 0;
926 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900927 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900928 kfree(entry);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900929 tomoyo_put_name(saved_filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900930 return error;
931}
932
933/**
934 * tomoyo_update_double_path_acl - Update "struct tomoyo_double_path_acl_record" list.
935 *
936 * @type: Type of operation.
937 * @filename1: First filename.
938 * @filename2: Second filename.
939 * @domain: Pointer to "struct tomoyo_domain_info".
940 * @is_delete: True if it is a delete request.
941 *
942 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900943 *
944 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900945 */
946static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
947 const char *filename2,
948 struct tomoyo_domain_info *
949 const domain, const bool is_delete)
950{
951 const struct tomoyo_path_info *saved_filename1;
952 const struct tomoyo_path_info *saved_filename2;
953 struct tomoyo_acl_info *ptr;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900954 struct tomoyo_double_path_acl_record *entry = NULL;
955 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900956 const u8 perm = 1 << type;
957
958 if (!domain)
959 return -EINVAL;
960 if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) ||
961 !tomoyo_is_correct_path(filename2, 0, 0, 0, __func__))
962 return -EINVAL;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900963 saved_filename1 = tomoyo_get_name(filename1);
964 saved_filename2 = tomoyo_get_name(filename2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900965 if (!saved_filename1 || !saved_filename2)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900966 goto out;
967 if (!is_delete)
968 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +0900969 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900970 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900971 struct tomoyo_double_path_acl_record *acl =
972 container_of(ptr, struct tomoyo_double_path_acl_record,
973 head);
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900974 if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900975 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900976 if (acl->filename1 != saved_filename1 ||
977 acl->filename2 != saved_filename2)
978 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900979 if (is_delete)
980 acl->perm &= ~perm;
981 else
982 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900983 error = 0;
984 break;
985 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900986 if (!is_delete && error && tomoyo_memory_ok(entry)) {
987 entry->head.type = TOMOYO_TYPE_DOUBLE_PATH_ACL;
988 entry->perm = perm;
989 entry->filename1 = saved_filename1;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900990 saved_filename1 = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900991 entry->filename2 = saved_filename2;
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900992 saved_filename2 = NULL;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900993 list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
994 entry = NULL;
995 error = 0;
996 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900997 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900998 out:
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900999 tomoyo_put_name(saved_filename1);
1000 tomoyo_put_name(saved_filename2);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001001 kfree(entry);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001002 return error;
1003}
1004
1005/**
1006 * tomoyo_check_single_path_acl - Check permission for single path operation.
1007 *
1008 * @domain: Pointer to "struct tomoyo_domain_info".
1009 * @type: Type of operation.
1010 * @filename: Filename to check.
1011 *
1012 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001013 *
1014 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001015 */
1016static int tomoyo_check_single_path_acl(struct tomoyo_domain_info *domain,
1017 const u8 type,
1018 const struct tomoyo_path_info *filename)
1019{
1020 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
1021 return 0;
1022 return tomoyo_check_single_path_acl2(domain, filename, 1 << type, 1);
1023}
1024
1025/**
1026 * tomoyo_check_double_path_acl - Check permission for double path operation.
1027 *
1028 * @domain: Pointer to "struct tomoyo_domain_info".
1029 * @type: Type of operation.
1030 * @filename1: First filename to check.
1031 * @filename2: Second filename to check.
1032 *
1033 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001034 *
1035 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001036 */
1037static int tomoyo_check_double_path_acl(const struct tomoyo_domain_info *domain,
1038 const u8 type,
1039 const struct tomoyo_path_info *
1040 filename1,
1041 const struct tomoyo_path_info *
1042 filename2)
1043{
1044 struct tomoyo_acl_info *ptr;
1045 const u8 perm = 1 << type;
1046 int error = -EPERM;
1047
1048 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
1049 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001050 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001051 struct tomoyo_double_path_acl_record *acl;
Tetsuo Handaea13ddb2010-02-03 06:43:06 +09001052 if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001053 continue;
1054 acl = container_of(ptr, struct tomoyo_double_path_acl_record,
1055 head);
1056 if (!(acl->perm & perm))
1057 continue;
1058 if (!tomoyo_path_matches_pattern(filename1, acl->filename1))
1059 continue;
1060 if (!tomoyo_path_matches_pattern(filename2, acl->filename2))
1061 continue;
1062 error = 0;
1063 break;
1064 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001065 return error;
1066}
1067
1068/**
1069 * tomoyo_check_single_path_permission2 - Check permission for single path operation.
1070 *
1071 * @domain: Pointer to "struct tomoyo_domain_info".
1072 * @operation: Type of operation.
1073 * @filename: Filename to check.
1074 * @mode: Access control mode.
1075 *
1076 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001077 *
1078 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001079 */
1080static int tomoyo_check_single_path_permission2(struct tomoyo_domain_info *
1081 const domain, u8 operation,
1082 const struct tomoyo_path_info *
1083 filename, const u8 mode)
1084{
1085 const char *msg;
1086 int error;
1087 const bool is_enforce = (mode == 3);
1088
1089 if (!mode)
1090 return 0;
1091 next:
1092 error = tomoyo_check_single_path_acl(domain, operation, filename);
1093 msg = tomoyo_sp2keyword(operation);
1094 if (!error)
1095 goto ok;
1096 if (tomoyo_verbose_mode(domain))
1097 printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n",
1098 tomoyo_get_msg(is_enforce), msg, filename->name,
1099 tomoyo_get_last_name(domain));
1100 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1101 const char *name = tomoyo_get_file_pattern(filename)->name;
1102 tomoyo_update_single_path_acl(operation, name, domain, false);
1103 }
1104 if (!is_enforce)
1105 error = 0;
1106 ok:
1107 /*
1108 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1109 * we need to check "allow_rewrite" permission if the filename is
1110 * specified by "deny_rewrite" keyword.
1111 */
1112 if (!error && operation == TOMOYO_TYPE_TRUNCATE_ACL &&
1113 tomoyo_is_no_rewrite_file(filename)) {
1114 operation = TOMOYO_TYPE_REWRITE_ACL;
1115 goto next;
1116 }
1117 return error;
1118}
1119
1120/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001121 * tomoyo_check_exec_perm - Check permission for "execute".
1122 *
1123 * @domain: Pointer to "struct tomoyo_domain_info".
1124 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001125 *
1126 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001127 *
1128 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001129 */
1130int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001131 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001132{
1133 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1134
1135 if (!mode)
1136 return 0;
1137 return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode);
1138}
1139
1140/**
1141 * tomoyo_check_open_permission - Check permission for "read" and "write".
1142 *
1143 * @domain: Pointer to "struct tomoyo_domain_info".
1144 * @path: Pointer to "struct path".
1145 * @flag: Flags for open().
1146 *
1147 * Returns 0 on success, negative value otherwise.
1148 */
1149int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1150 struct path *path, const int flag)
1151{
1152 const u8 acc_mode = ACC_MODE(flag);
1153 int error = -ENOMEM;
1154 struct tomoyo_path_info *buf;
1155 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1156 const bool is_enforce = (mode == 3);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001157 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001158
1159 if (!mode || !path->mnt)
1160 return 0;
1161 if (acc_mode == 0)
1162 return 0;
1163 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1164 /*
1165 * I don't check directories here because mkdir() and rmdir()
1166 * don't call me.
1167 */
1168 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001169 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001170 buf = tomoyo_get_path(path);
1171 if (!buf)
1172 goto out;
1173 error = 0;
1174 /*
1175 * If the filename is specified by "deny_rewrite" keyword,
1176 * we need to check "allow_rewrite" permission when the filename is not
1177 * opened for append mode or the filename is truncated at open time.
1178 */
1179 if ((acc_mode & MAY_WRITE) &&
1180 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1181 (tomoyo_is_no_rewrite_file(buf))) {
1182 error = tomoyo_check_single_path_permission2(domain,
1183 TOMOYO_TYPE_REWRITE_ACL,
1184 buf, mode);
1185 }
1186 if (!error)
1187 error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
1188 mode);
1189 if (!error && (flag & O_TRUNC))
1190 error = tomoyo_check_single_path_permission2(domain,
1191 TOMOYO_TYPE_TRUNCATE_ACL,
1192 buf, mode);
1193 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001194 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001195 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001196 if (!is_enforce)
1197 error = 0;
1198 return error;
1199}
1200
1201/**
Tetsuo Handa937bf612009-12-02 21:09:48 +09001202 * tomoyo_check_1path_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 +09001203 *
1204 * @domain: Pointer to "struct tomoyo_domain_info".
1205 * @operation: Type of operation.
1206 * @path: Pointer to "struct path".
1207 *
1208 * Returns 0 on success, negative value otherwise.
1209 */
1210int tomoyo_check_1path_perm(struct tomoyo_domain_info *domain,
1211 const u8 operation, struct path *path)
1212{
1213 int error = -ENOMEM;
1214 struct tomoyo_path_info *buf;
1215 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1216 const bool is_enforce = (mode == 3);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001217 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001218
1219 if (!mode || !path->mnt)
1220 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001221 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001222 buf = tomoyo_get_path(path);
1223 if (!buf)
1224 goto out;
1225 switch (operation) {
1226 case TOMOYO_TYPE_MKDIR_ACL:
1227 case TOMOYO_TYPE_RMDIR_ACL:
Tetsuo Handa937bf612009-12-02 21:09:48 +09001228 case TOMOYO_TYPE_CHROOT_ACL:
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001229 if (!buf->is_dir) {
1230 /*
1231 * tomoyo_get_path() reserves space for appending "/."
1232 */
1233 strcat((char *) buf->name, "/");
1234 tomoyo_fill_path_info(buf);
1235 }
1236 }
1237 error = tomoyo_check_single_path_permission2(domain, operation, buf,
1238 mode);
1239 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001240 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001241 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001242 if (!is_enforce)
1243 error = 0;
1244 return error;
1245}
1246
1247/**
1248 * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1249 *
1250 * @domain: Pointer to "struct tomoyo_domain_info".
1251 * @filp: Pointer to "struct file".
1252 *
1253 * Returns 0 on success, negative value otherwise.
1254 */
1255int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain,
1256 struct file *filp)
1257{
1258 int error = -ENOMEM;
1259 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1260 const bool is_enforce = (mode == 3);
1261 struct tomoyo_path_info *buf;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001262 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001263
1264 if (!mode || !filp->f_path.mnt)
1265 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001266
1267 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001268 buf = tomoyo_get_path(&filp->f_path);
1269 if (!buf)
1270 goto out;
1271 if (!tomoyo_is_no_rewrite_file(buf)) {
1272 error = 0;
1273 goto out;
1274 }
1275 error = tomoyo_check_single_path_permission2(domain,
1276 TOMOYO_TYPE_REWRITE_ACL,
1277 buf, mode);
1278 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001279 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001280 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001281 if (!is_enforce)
1282 error = 0;
1283 return error;
1284}
1285
1286/**
Tetsuo Handa937bf612009-12-02 21:09:48 +09001287 * tomoyo_check_2path_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001288 *
1289 * @domain: Pointer to "struct tomoyo_domain_info".
1290 * @operation: Type of operation.
1291 * @path1: Pointer to "struct path".
1292 * @path2: Pointer to "struct path".
1293 *
1294 * Returns 0 on success, negative value otherwise.
1295 */
1296int tomoyo_check_2path_perm(struct tomoyo_domain_info * const domain,
1297 const u8 operation, struct path *path1,
1298 struct path *path2)
1299{
1300 int error = -ENOMEM;
1301 struct tomoyo_path_info *buf1, *buf2;
1302 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1303 const bool is_enforce = (mode == 3);
1304 const char *msg;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001305 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001306
1307 if (!mode || !path1->mnt || !path2->mnt)
1308 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001309 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001310 buf1 = tomoyo_get_path(path1);
1311 buf2 = tomoyo_get_path(path2);
1312 if (!buf1 || !buf2)
1313 goto out;
1314 {
1315 struct dentry *dentry = path1->dentry;
1316 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1317 /*
1318 * tomoyo_get_path() reserves space for appending "/."
1319 */
1320 if (!buf1->is_dir) {
1321 strcat((char *) buf1->name, "/");
1322 tomoyo_fill_path_info(buf1);
1323 }
1324 if (!buf2->is_dir) {
1325 strcat((char *) buf2->name, "/");
1326 tomoyo_fill_path_info(buf2);
1327 }
1328 }
1329 }
1330 error = tomoyo_check_double_path_acl(domain, operation, buf1, buf2);
1331 msg = tomoyo_dp2keyword(operation);
1332 if (!error)
1333 goto out;
1334 if (tomoyo_verbose_mode(domain))
1335 printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' "
1336 "denied for %s\n", tomoyo_get_msg(is_enforce),
1337 msg, buf1->name, buf2->name,
1338 tomoyo_get_last_name(domain));
1339 if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1340 const char *name1 = tomoyo_get_file_pattern(buf1)->name;
1341 const char *name2 = tomoyo_get_file_pattern(buf2)->name;
1342 tomoyo_update_double_path_acl(operation, name1, name2, domain,
1343 false);
1344 }
1345 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001346 kfree(buf1);
1347 kfree(buf2);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001348 tomoyo_read_unlock(idx);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001349 if (!is_enforce)
1350 error = 0;
1351 return error;
1352}