TOMOYO: Add numeric values grouping support.
[linux-block.git] / security / tomoyo / file.c
CommitLineData
b69a54ee
KT
1/*
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 *
39826a1e 8 * Version: 2.2.0 2009/04/01
b69a54ee
KT
9 *
10 */
11
12#include "common.h"
5a0e3ad6 13#include <linux/slab.h>
b69a54ee
KT
14
15/* Keyword array for single path operations. */
7ef61233
TH
16static 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",
b69a54ee
KT
39};
40
41/* Keyword array for double path operations. */
7ef61233
TH
42static 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",
b69a54ee
KT
46};
47
7762fbff
TH
48void 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
4c3e9e2d
TH
79void 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
b69a54ee 93/**
7ef61233 94 * tomoyo_path2keyword - Get the name of single path operation.
b69a54ee
KT
95 *
96 * @operation: Type of operation.
97 *
98 * Returns the name of single path operation.
99 */
7ef61233 100const char *tomoyo_path2keyword(const u8 operation)
b69a54ee 101{
7ef61233
TH
102 return (operation < TOMOYO_MAX_PATH_OPERATION)
103 ? tomoyo_path_keyword[operation] : NULL;
b69a54ee
KT
104}
105
106/**
7ef61233 107 * tomoyo_path22keyword - Get the name of double path operation.
b69a54ee
KT
108 *
109 * @operation: Type of operation.
110 *
111 * Returns the name of double path operation.
112 */
7ef61233 113const char *tomoyo_path22keyword(const u8 operation)
b69a54ee 114{
7ef61233
TH
115 return (operation < TOMOYO_MAX_PATH2_OPERATION)
116 ? tomoyo_path2_keyword[operation] : NULL;
b69a54ee
KT
117}
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;
8e2d39a1 147 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
4e5d6f7e 148 GFP_NOFS);
b69a54ee
KT
149
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 }
8e2d39a1 160 kfree(buf);
b69a54ee
KT
161 return NULL;
162}
163
7ef61233
TH
164static 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);
b69a54ee 171
c3fa109a
TH
172/*
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 */
847b173e 197LIST_HEAD(tomoyo_globally_readable_list);
b69a54ee
KT
198
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.
fdb8ebb7
TH
206 *
207 * Caller holds tomoyo_read_lock().
b69a54ee
KT
208 */
209static int tomoyo_update_globally_readable_entry(const char *filename,
210 const bool is_delete)
211{
b69a54ee 212 struct tomoyo_globally_readable_file_entry *ptr;
9e4b50e9 213 struct tomoyo_globally_readable_file_entry e = { };
ca0b7df3 214 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 215
17080008 216 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
b69a54ee 217 return -EINVAL;
9e4b50e9
TH
218 e.filename = tomoyo_get_name(filename);
219 if (!e.filename)
b69a54ee 220 return -ENOMEM;
29282381
TH
221 if (mutex_lock_interruptible(&tomoyo_policy_lock))
222 goto out;
fdb8ebb7 223 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
9e4b50e9 224 if (ptr->filename != e.filename)
b69a54ee
KT
225 continue;
226 ptr->is_deleted = is_delete;
227 error = 0;
ca0b7df3 228 break;
b69a54ee 229 }
9e4b50e9
TH
230 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 }
b69a54ee 238 }
f737d95d 239 mutex_unlock(&tomoyo_policy_lock);
29282381 240 out:
9e4b50e9 241 tomoyo_put_name(e.filename);
b69a54ee
KT
242 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.
fdb8ebb7
TH
251 *
252 * Caller holds tomoyo_read_lock().
b69a54ee
KT
253 */
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;
fdb8ebb7
TH
259
260 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
b69a54ee
KT
261 if (!ptr->is_deleted &&
262 tomoyo_path_matches_pattern(filename, ptr->filename)) {
263 found = true;
264 break;
265 }
266 }
b69a54ee
KT
267 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.
fdb8ebb7
TH
277 *
278 * Caller holds tomoyo_read_lock().
b69a54ee
KT
279 */
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.
fdb8ebb7
TH
291 *
292 * Caller holds tomoyo_read_lock().
b69a54ee
KT
293 */
294bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
295{
296 struct list_head *pos;
297 bool done = true;
298
b69a54ee
KT
299 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;
7d2948b1
TH
307 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
308 ptr->filename->name);
309 if (!done)
b69a54ee 310 break;
b69a54ee 311 }
b69a54ee
KT
312 return done;
313}
314
c3fa109a
TH
315/* 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 */
847b173e 344LIST_HEAD(tomoyo_pattern_list);
b69a54ee
KT
345
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.
fdb8ebb7
TH
353 *
354 * Caller holds tomoyo_read_lock().
b69a54ee
KT
355 */
356static int tomoyo_update_file_pattern_entry(const char *pattern,
357 const bool is_delete)
358{
b69a54ee 359 struct tomoyo_pattern_entry *ptr;
9e4b50e9 360 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
ca0b7df3 361 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 362
9e4b50e9 363 if (!e.pattern)
ca0b7df3 364 return error;
9e4b50e9 365 if (!e.pattern->is_patterned)
ca0b7df3 366 goto out;
29282381
TH
367 if (mutex_lock_interruptible(&tomoyo_policy_lock))
368 goto out;
fdb8ebb7 369 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
9e4b50e9 370 if (e.pattern != ptr->pattern)
b69a54ee
KT
371 continue;
372 ptr->is_deleted = is_delete;
373 error = 0;
ca0b7df3 374 break;
b69a54ee 375 }
9e4b50e9
TH
376 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 }
b69a54ee 383 }
f737d95d 384 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 385 out:
9e4b50e9 386 tomoyo_put_name(e.pattern);
b69a54ee
KT
387 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.
fdb8ebb7
TH
396 *
397 * Caller holds tomoyo_read_lock().
b69a54ee
KT
398 */
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
fdb8ebb7 405 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
b69a54ee
KT
406 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 }
b69a54ee
KT
418 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.
fdb8ebb7
TH
430 *
431 * Caller holds tomoyo_read_lock().
b69a54ee
KT
432 */
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.
fdb8ebb7
TH
444 *
445 * Caller holds tomoyo_read_lock().
b69a54ee
KT
446 */
447bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
448{
449 struct list_head *pos;
450 bool done = true;
451
b69a54ee
KT
452 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;
7d2948b1
TH
457 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
458 "%s\n", ptr->pattern->name);
459 if (!done)
b69a54ee 460 break;
b69a54ee 461 }
b69a54ee
KT
462 return done;
463}
464
c3fa109a
TH
465/*
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 */
847b173e 494LIST_HEAD(tomoyo_no_rewrite_list);
b69a54ee
KT
495
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.
fdb8ebb7
TH
503 *
504 * Caller holds tomoyo_read_lock().
b69a54ee
KT
505 */
506static int tomoyo_update_no_rewrite_entry(const char *pattern,
507 const bool is_delete)
508{
ca0b7df3 509 struct tomoyo_no_rewrite_entry *ptr;
9e4b50e9 510 struct tomoyo_no_rewrite_entry e = { };
ca0b7df3 511 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 512
17080008 513 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
b69a54ee 514 return -EINVAL;
9e4b50e9
TH
515 e.pattern = tomoyo_get_name(pattern);
516 if (!e.pattern)
ca0b7df3 517 return error;
29282381
TH
518 if (mutex_lock_interruptible(&tomoyo_policy_lock))
519 goto out;
fdb8ebb7 520 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
9e4b50e9 521 if (ptr->pattern != e.pattern)
b69a54ee
KT
522 continue;
523 ptr->is_deleted = is_delete;
524 error = 0;
ca0b7df3 525 break;
b69a54ee 526 }
9e4b50e9
TH
527 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 }
b69a54ee 535 }
f737d95d 536 mutex_unlock(&tomoyo_policy_lock);
29282381 537 out:
9e4b50e9 538 tomoyo_put_name(e.pattern);
b69a54ee
KT
539 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.
fdb8ebb7
TH
549 *
550 * Caller holds tomoyo_read_lock().
b69a54ee
KT
551 */
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
fdb8ebb7 557 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
b69a54ee
KT
558 if (ptr->is_deleted)
559 continue;
560 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
561 continue;
562 found = true;
563 break;
564 }
b69a54ee
KT
565 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.
fdb8ebb7
TH
575 *
576 * Caller holds tomoyo_read_lock().
b69a54ee
KT
577 */
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.
fdb8ebb7
TH
589 *
590 * Caller holds tomoyo_read_lock().
b69a54ee
KT
591 */
592bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
593{
594 struct list_head *pos;
595 bool done = true;
596
b69a54ee
KT
597 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;
7d2948b1
TH
602 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
603 "%s\n", ptr->pattern->name);
604 if (!done)
b69a54ee 605 break;
b69a54ee 606 }
b69a54ee
KT
607 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".
fdb8ebb7
TH
624 *
625 * Caller holds tomoyo_read_lock().
b69a54ee
KT
626 */
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)
7ef61233
TH
643 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
644 is_delete);
b69a54ee 645 if (perm & 2)
7ef61233
TH
646 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
647 is_delete);
b69a54ee 648 if (perm & 1)
7ef61233
TH
649 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
650 is_delete);
b69a54ee
KT
651 return 0;
652}
653
654/**
7ef61233 655 * tomoyo_path_acl2 - Check permission for single path operation.
b69a54ee
KT
656 *
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.
fdb8ebb7
TH
663 *
664 * Caller holds tomoyo_read_lock().
b69a54ee 665 */
7ef61233
TH
666static 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)
b69a54ee
KT
669{
670 struct tomoyo_acl_info *ptr;
671 int error = -EPERM;
672
fdb8ebb7 673 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
674 struct tomoyo_path_acl *acl;
675 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
b69a54ee 676 continue;
7ef61233 677 acl = container_of(ptr, struct tomoyo_path_acl, head);
937bf613
TH
678 if (perm <= 0xFFFF) {
679 if (!(acl->perm & perm))
680 continue;
681 } else {
682 if (!(acl->perm_high & (perm >> 16)))
683 continue;
684 }
7762fbff
TH
685 if (!tomoyo_compare_name_union_pattern(filename, &acl->name,
686 may_use_pattern))
b69a54ee 687 continue;
b69a54ee
KT
688 error = 0;
689 break;
690 }
b69a54ee
KT
691 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.
fdb8ebb7
TH
702 *
703 * Caller holds tomoyo_read_lock().
b69a54ee
KT
704 */
705static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
706 const struct tomoyo_path_info *filename,
707 const u8 operation)
708{
937bf613 709 u32 perm = 0;
b69a54ee
KT
710
711 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
712 return 0;
713 if (operation == 6)
7ef61233 714 perm = 1 << TOMOYO_TYPE_READ_WRITE;
b69a54ee 715 else if (operation == 4)
7ef61233 716 perm = 1 << TOMOYO_TYPE_READ;
b69a54ee 717 else if (operation == 2)
7ef61233 718 perm = 1 << TOMOYO_TYPE_WRITE;
b69a54ee 719 else if (operation == 1)
7ef61233 720 perm = 1 << TOMOYO_TYPE_EXECUTE;
b69a54ee
KT
721 else
722 BUG();
7ef61233 723 return tomoyo_path_acl2(domain, filename, perm, operation != 1);
b69a54ee
KT
724}
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.
fdb8ebb7
TH
736 *
737 * Caller holds tomoyo_read_lock().
b69a54ee
KT
738 */
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);
ea13ddba 751 if (error && perm == 4 && !domain->ignore_global_allow_read
b69a54ee
KT
752 && tomoyo_is_globally_readable_file(filename))
753 error = 0;
754 if (perm == 6)
7ef61233 755 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
b69a54ee 756 else if (perm == 4)
7ef61233 757 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
b69a54ee 758 else if (perm == 2)
7ef61233 759 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
b69a54ee 760 else if (perm == 1)
7ef61233 761 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
b69a54ee
KT
762 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.
fdb8ebb7
TH
790 *
791 * Caller holds tomoyo_read_lock().
b69a54ee
KT
792 */
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;
7ef61233
TH
810 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
811 if (strcmp(data, tomoyo_path_keyword[type]))
b69a54ee 812 continue;
7ef61233
TH
813 return tomoyo_update_path_acl(type, filename, domain,
814 is_delete);
b69a54ee
KT
815 }
816 filename2 = strchr(filename, ' ');
817 if (!filename2)
818 goto out;
819 *filename2++ = '\0';
7ef61233
TH
820 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
821 if (strcmp(data, tomoyo_path2_keyword[type]))
b69a54ee 822 continue;
7ef61233
TH
823 return tomoyo_update_path2_acl(type, filename, filename2,
824 domain, is_delete);
b69a54ee
KT
825 }
826 out:
827 return -EINVAL;
828}
829
830/**
7ef61233 831 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
b69a54ee
KT
832 *
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.
fdb8ebb7
TH
839 *
840 * Caller holds tomoyo_read_lock().
b69a54ee 841 */
7ef61233
TH
842static int tomoyo_update_path_acl(const u8 type, const char *filename,
843 struct tomoyo_domain_info *const domain,
844 const bool is_delete)
b69a54ee 845{
9e4b50e9 846 static const u32 tomoyo_rw_mask =
7ef61233 847 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
9e4b50e9 848 const u32 perm = 1 << type;
b69a54ee 849 struct tomoyo_acl_info *ptr;
9e4b50e9
TH
850 struct tomoyo_path_acl e = {
851 .head.type = TOMOYO_TYPE_PATH_ACL,
852 .perm_high = perm >> 16,
853 .perm = perm
854 };
ca0b7df3 855 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 856
9e4b50e9
TH
857 if (type == TOMOYO_TYPE_READ_WRITE)
858 e.perm |= tomoyo_rw_mask;
b69a54ee
KT
859 if (!domain)
860 return -EINVAL;
7762fbff 861 if (!tomoyo_parse_name_union(filename, &e.name))
b69a54ee 862 return -EINVAL;
29282381
TH
863 if (mutex_lock_interruptible(&tomoyo_policy_lock))
864 goto out;
fdb8ebb7 865 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
866 struct tomoyo_path_acl *acl =
867 container_of(ptr, struct tomoyo_path_acl, head);
7762fbff 868 if (!tomoyo_is_same_path_acl(acl, &e))
b69a54ee 869 continue;
ca0b7df3
TH
870 if (is_delete) {
871 if (perm <= 0xFFFF)
872 acl->perm &= ~perm;
873 else
874 acl->perm_high &= ~(perm >> 16);
9e4b50e9 875 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
7ef61233
TH
876 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
877 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
9e4b50e9 878 acl->perm &= ~tomoyo_rw_mask;
ca0b7df3
TH
879 } else {
880 if (perm <= 0xFFFF)
881 acl->perm |= perm;
882 else
883 acl->perm_high |= (perm >> 16);
9e4b50e9 884 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
7ef61233
TH
885 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
886 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
9e4b50e9 887 acl->perm |= tomoyo_rw_mask;
ca0b7df3 888 }
b69a54ee 889 error = 0;
ca0b7df3 890 break;
cd7bec6a 891 }
9e4b50e9
TH
892 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 }
b69a54ee 900 }
f737d95d 901 mutex_unlock(&tomoyo_policy_lock);
29282381 902 out:
7762fbff 903 tomoyo_put_name_union(&e.name);
b69a54ee
KT
904 return error;
905}
906
907/**
7ef61233 908 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
b69a54ee
KT
909 *
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.
fdb8ebb7
TH
917 *
918 * Caller holds tomoyo_read_lock().
b69a54ee 919 */
7ef61233
TH
920static 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)
b69a54ee 924{
9e4b50e9
TH
925 const u8 perm = 1 << type;
926 struct tomoyo_path2_acl e = {
927 .head.type = TOMOYO_TYPE_PATH2_ACL,
928 .perm = perm
929 };
b69a54ee 930 struct tomoyo_acl_info *ptr;
ca0b7df3 931 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee
KT
932
933 if (!domain)
934 return -EINVAL;
7762fbff
TH
935 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
936 !tomoyo_parse_name_union(filename2, &e.name2))
ca0b7df3 937 goto out;
29282381
TH
938 if (mutex_lock_interruptible(&tomoyo_policy_lock))
939 goto out;
fdb8ebb7 940 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
941 struct tomoyo_path2_acl *acl =
942 container_of(ptr, struct tomoyo_path2_acl, head);
7762fbff 943 if (!tomoyo_is_same_path2_acl(acl, &e))
b69a54ee 944 continue;
ca0b7df3
TH
945 if (is_delete)
946 acl->perm &= ~perm;
947 else
948 acl->perm |= perm;
b69a54ee 949 error = 0;
ca0b7df3 950 break;
cd7bec6a 951 }
9e4b50e9
TH
952 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 }
b69a54ee 960 }
f737d95d 961 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 962 out:
7762fbff
TH
963 tomoyo_put_name_union(&e.name1);
964 tomoyo_put_name_union(&e.name2);
b69a54ee
KT
965 return error;
966}
967
968/**
7ef61233 969 * tomoyo_path_acl - Check permission for single path operation.
b69a54ee
KT
970 *
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.
fdb8ebb7
TH
976 *
977 * Caller holds tomoyo_read_lock().
b69a54ee 978 */
7ef61233
TH
979static int tomoyo_path_acl(struct tomoyo_domain_info *domain, const u8 type,
980 const struct tomoyo_path_info *filename)
b69a54ee
KT
981{
982 if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
983 return 0;
7ef61233 984 return tomoyo_path_acl2(domain, filename, 1 << type, 1);
b69a54ee
KT
985}
986
987/**
7ef61233 988 * tomoyo_path2_acl - Check permission for double path operation.
b69a54ee
KT
989 *
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.
fdb8ebb7
TH
996 *
997 * Caller holds tomoyo_read_lock().
b69a54ee 998 */
7ef61233
TH
999static 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)
b69a54ee
KT
1003{
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;
fdb8ebb7 1010 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
1011 struct tomoyo_path2_acl *acl;
1012 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
b69a54ee 1013 continue;
7ef61233 1014 acl = container_of(ptr, struct tomoyo_path2_acl, head);
b69a54ee
KT
1015 if (!(acl->perm & perm))
1016 continue;
7762fbff 1017 if (!tomoyo_compare_name_union(filename1, &acl->name1))
b69a54ee 1018 continue;
7762fbff 1019 if (!tomoyo_compare_name_union(filename2, &acl->name2))
b69a54ee
KT
1020 continue;
1021 error = 0;
1022 break;
1023 }
b69a54ee
KT
1024 return error;
1025}
1026
1027/**
7ef61233 1028 * tomoyo_path_permission2 - Check permission for single path operation.
b69a54ee
KT
1029 *
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.
fdb8ebb7
TH
1036 *
1037 * Caller holds tomoyo_read_lock().
b69a54ee 1038 */
7ef61233
TH
1039static int tomoyo_path_permission2(struct tomoyo_domain_info *const domain,
1040 u8 operation,
1041 const struct tomoyo_path_info *filename,
1042 const u8 mode)
b69a54ee
KT
1043{
1044 const char *msg;
1045 int error;
1046 const bool is_enforce = (mode == 3);
1047
1048 if (!mode)
1049 return 0;
1050 next:
7ef61233
TH
1051 error = tomoyo_path_acl(domain, operation, filename);
1052 msg = tomoyo_path2keyword(operation);
b69a54ee
KT
1053 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;
7ef61233 1061 tomoyo_update_path_acl(operation, name, domain, false);
b69a54ee
KT
1062 }
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 */
7ef61233 1071 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
b69a54ee 1072 tomoyo_is_no_rewrite_file(filename)) {
7ef61233 1073 operation = TOMOYO_TYPE_REWRITE;
b69a54ee
KT
1074 goto next;
1075 }
1076 return error;
1077}
1078
b69a54ee
KT
1079/**
1080 * tomoyo_check_exec_perm - Check permission for "execute".
1081 *
1082 * @domain: Pointer to "struct tomoyo_domain_info".
1083 * @filename: Check permission for "execute".
b69a54ee
KT
1084 *
1085 * Returns 0 on success, negativevalue otherwise.
fdb8ebb7
TH
1086 *
1087 * Caller holds tomoyo_read_lock().
b69a54ee
KT
1088 */
1089int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
bcb86975 1090 const struct tomoyo_path_info *filename)
b69a54ee
KT
1091{
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);
fdb8ebb7 1116 int idx;
b69a54ee
KT
1117
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;
fdb8ebb7 1128 idx = tomoyo_read_lock();
b69a54ee
KT
1129 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))) {
7ef61233
TH
1141 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE,
1142 buf, mode);
b69a54ee
KT
1143 }
1144 if (!error)
1145 error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
1146 mode);
1147 if (!error && (flag & O_TRUNC))
7ef61233
TH
1148 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_TRUNCATE,
1149 buf, mode);
b69a54ee 1150 out:
8e2d39a1 1151 kfree(buf);
fdb8ebb7 1152 tomoyo_read_unlock(idx);
b69a54ee
KT
1153 if (!is_enforce)
1154 error = 0;
1155 return error;
1156}
1157
1158/**
7ef61233 1159 * tomoyo_path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
b69a54ee 1160 *
b69a54ee
KT
1161 * @operation: Type of operation.
1162 * @path: Pointer to "struct path".
1163 *
1164 * Returns 0 on success, negative value otherwise.
1165 */
97d6931e 1166int tomoyo_path_perm(const u8 operation, struct path *path)
b69a54ee
KT
1167{
1168 int error = -ENOMEM;
1169 struct tomoyo_path_info *buf;
97d6931e 1170 struct tomoyo_domain_info *domain = tomoyo_domain();
b69a54ee
KT
1171 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1172 const bool is_enforce = (mode == 3);
fdb8ebb7 1173 int idx;
b69a54ee
KT
1174
1175 if (!mode || !path->mnt)
1176 return 0;
fdb8ebb7 1177 idx = tomoyo_read_lock();
b69a54ee
KT
1178 buf = tomoyo_get_path(path);
1179 if (!buf)
1180 goto out;
1181 switch (operation) {
7ef61233
TH
1182 case TOMOYO_TYPE_MKDIR:
1183 case TOMOYO_TYPE_RMDIR:
1184 case TOMOYO_TYPE_CHROOT:
b69a54ee
KT
1185 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 }
7ef61233 1193 error = tomoyo_path_permission2(domain, operation, buf, mode);
b69a54ee 1194 out:
8e2d39a1 1195 kfree(buf);
fdb8ebb7 1196 tomoyo_read_unlock(idx);
b69a54ee
KT
1197 if (!is_enforce)
1198 error = 0;
1199 return error;
1200}
1201
1202/**
1203 * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1204 *
b69a54ee
KT
1205 * @filp: Pointer to "struct file".
1206 *
1207 * Returns 0 on success, negative value otherwise.
1208 */
97d6931e 1209int tomoyo_check_rewrite_permission(struct file *filp)
b69a54ee
KT
1210{
1211 int error = -ENOMEM;
97d6931e 1212 struct tomoyo_domain_info *domain = tomoyo_domain();
b69a54ee
KT
1213 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1214 const bool is_enforce = (mode == 3);
1215 struct tomoyo_path_info *buf;
fdb8ebb7 1216 int idx;
b69a54ee
KT
1217
1218 if (!mode || !filp->f_path.mnt)
1219 return 0;
fdb8ebb7
TH
1220
1221 idx = tomoyo_read_lock();
b69a54ee
KT
1222 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 }
7ef61233 1229 error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE, buf, mode);
b69a54ee 1230 out:
8e2d39a1 1231 kfree(buf);
fdb8ebb7 1232 tomoyo_read_unlock(idx);
b69a54ee
KT
1233 if (!is_enforce)
1234 error = 0;
1235 return error;
1236}
1237
1238/**
7ef61233 1239 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
b69a54ee 1240 *
b69a54ee
KT
1241 * @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 */
97d6931e 1247int tomoyo_path2_perm(const u8 operation, struct path *path1,
7ef61233 1248 struct path *path2)
b69a54ee
KT
1249{
1250 int error = -ENOMEM;
1251 struct tomoyo_path_info *buf1, *buf2;
97d6931e 1252 struct tomoyo_domain_info *domain = tomoyo_domain();
b69a54ee
KT
1253 const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1254 const bool is_enforce = (mode == 3);
1255 const char *msg;
fdb8ebb7 1256 int idx;
b69a54ee
KT
1257
1258 if (!mode || !path1->mnt || !path2->mnt)
1259 return 0;
fdb8ebb7 1260 idx = tomoyo_read_lock();
b69a54ee
KT
1261 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 }
7ef61233
TH
1281 error = tomoyo_path2_acl(domain, operation, buf1, buf2);
1282 msg = tomoyo_path22keyword(operation);
b69a54ee
KT
1283 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;
7ef61233
TH
1293 tomoyo_update_path2_acl(operation, name1, name2, domain,
1294 false);
b69a54ee
KT
1295 }
1296 out:
8e2d39a1
TH
1297 kfree(buf1);
1298 kfree(buf2);
fdb8ebb7 1299 tomoyo_read_unlock(idx);
b69a54ee
KT
1300 if (!is_enforce)
1301 error = 0;
1302 return error;
1303}