]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/file.c
TOMOYO: Update profile structure.
[net-next-2.6.git] / security / tomoyo / file.c
CommitLineData
b69a54ee
KT
1/*
2 * security/tomoyo/file.c
3 *
c3ef1500 4 * Pathname restriction functions.
b69a54ee 5 *
c3ef1500 6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
b69a54ee
KT
7 */
8
9#include "common.h"
5a0e3ad6 10#include <linux/slab.h>
b69a54ee 11
a1f9bb6a 12/* Keyword array for operations with one pathname. */
7ef61233
TH
13static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
14 [TOMOYO_TYPE_READ_WRITE] = "read/write",
15 [TOMOYO_TYPE_EXECUTE] = "execute",
16 [TOMOYO_TYPE_READ] = "read",
17 [TOMOYO_TYPE_WRITE] = "write",
7ef61233 18 [TOMOYO_TYPE_UNLINK] = "unlink",
7ef61233 19 [TOMOYO_TYPE_RMDIR] = "rmdir",
7ef61233
TH
20 [TOMOYO_TYPE_TRUNCATE] = "truncate",
21 [TOMOYO_TYPE_SYMLINK] = "symlink",
22 [TOMOYO_TYPE_REWRITE] = "rewrite",
7ef61233 23 [TOMOYO_TYPE_CHROOT] = "chroot",
7ef61233 24 [TOMOYO_TYPE_UMOUNT] = "unmount",
b69a54ee
KT
25};
26
a1f9bb6a
TH
27/* Keyword array for operations with one pathname and three numbers. */
28static const char *tomoyo_path_number3_keyword
29[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
30 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
31 [TOMOYO_TYPE_MKCHAR] = "mkchar",
32};
33
34/* Keyword array for operations with two pathnames. */
7ef61233 35static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
a1f9bb6a
TH
36 [TOMOYO_TYPE_LINK] = "link",
37 [TOMOYO_TYPE_RENAME] = "rename",
7ef61233 38 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
b69a54ee
KT
39};
40
a1f9bb6a
TH
41/* Keyword array for operations with one pathname and one number. */
42static const char *tomoyo_path_number_keyword
43[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
44 [TOMOYO_TYPE_CREATE] = "create",
45 [TOMOYO_TYPE_MKDIR] = "mkdir",
46 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
47 [TOMOYO_TYPE_MKSOCK] = "mksock",
48 [TOMOYO_TYPE_IOCTL] = "ioctl",
49 [TOMOYO_TYPE_CHMOD] = "chmod",
50 [TOMOYO_TYPE_CHOWN] = "chown",
51 [TOMOYO_TYPE_CHGRP] = "chgrp",
52};
53
57c2590f
TH
54static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
55 [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
56 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
57 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
58 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
59 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
60 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
61 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
62 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
63 [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
64 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
65 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
66};
67
68static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
69 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
70 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
71};
72
73static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
74 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
75 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
76 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
77};
78
79static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
80 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
81 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
82 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
83 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
84 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
85 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
86 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
87 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
88};
89
7762fbff
TH
90void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
91{
92 if (!ptr)
93 return;
94 if (ptr->is_group)
95 tomoyo_put_path_group(ptr->group);
96 else
97 tomoyo_put_name(ptr->filename);
98}
99
100bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
101 const struct tomoyo_name_union *ptr)
102{
103 if (ptr->is_group)
3f629636 104 return tomoyo_path_matches_group(name, ptr->group);
7762fbff
TH
105 return tomoyo_path_matches_pattern(name, ptr->filename);
106}
107
4c3e9e2d
TH
108void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
109{
110 if (ptr && ptr->is_group)
111 tomoyo_put_number_group(ptr->group);
112}
113
114bool tomoyo_compare_number_union(const unsigned long value,
115 const struct tomoyo_number_union *ptr)
116{
117 if (ptr->is_group)
118 return tomoyo_number_matches_group(value, value, ptr->group);
119 return value >= ptr->values[0] && value <= ptr->values[1];
120}
121
b69a54ee 122/**
7ef61233 123 * tomoyo_path2keyword - Get the name of single path operation.
b69a54ee
KT
124 *
125 * @operation: Type of operation.
126 *
127 * Returns the name of single path operation.
128 */
7ef61233 129const char *tomoyo_path2keyword(const u8 operation)
b69a54ee 130{
7ef61233
TH
131 return (operation < TOMOYO_MAX_PATH_OPERATION)
132 ? tomoyo_path_keyword[operation] : NULL;
b69a54ee
KT
133}
134
a1f9bb6a
TH
135/**
136 * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
137 *
138 * @operation: Type of operation.
139 *
140 * Returns the name of path/number/number/number operation.
141 */
142const char *tomoyo_path_number32keyword(const u8 operation)
143{
144 return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
145 ? tomoyo_path_number3_keyword[operation] : NULL;
146}
147
b69a54ee 148/**
7ef61233 149 * tomoyo_path22keyword - Get the name of double path operation.
b69a54ee
KT
150 *
151 * @operation: Type of operation.
152 *
153 * Returns the name of double path operation.
154 */
7ef61233 155const char *tomoyo_path22keyword(const u8 operation)
b69a54ee 156{
7ef61233
TH
157 return (operation < TOMOYO_MAX_PATH2_OPERATION)
158 ? tomoyo_path2_keyword[operation] : NULL;
b69a54ee
KT
159}
160
a1f9bb6a
TH
161/**
162 * tomoyo_path_number2keyword - Get the name of path/number operations.
163 *
164 * @operation: Type of operation.
165 *
166 * Returns the name of path/number operation.
167 */
168const char *tomoyo_path_number2keyword(const u8 operation)
169{
170 return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
171 ? tomoyo_path_number_keyword[operation] : NULL;
172}
173
c8c57e84
TH
174static void tomoyo_add_slash(struct tomoyo_path_info *buf)
175{
176 if (buf->is_dir)
177 return;
178 /*
179 * This is OK because tomoyo_encode() reserves space for appending "/".
180 */
181 strcat((char *) buf->name, "/");
182 tomoyo_fill_path_info(buf);
183}
184
b69a54ee
KT
185/**
186 * tomoyo_strendswith - Check whether the token ends with the given token.
187 *
188 * @name: The token to check.
189 * @tail: The token to find.
190 *
191 * Returns true if @name ends with @tail, false otherwise.
192 */
193static bool tomoyo_strendswith(const char *name, const char *tail)
194{
195 int len;
196
197 if (!name || !tail)
198 return false;
199 len = strlen(name) - strlen(tail);
200 return len >= 0 && !strcmp(name + len, tail);
201}
202
203/**
c8c57e84 204 * tomoyo_get_realpath - Get realpath.
b69a54ee 205 *
c8c57e84 206 * @buf: Pointer to "struct tomoyo_path_info".
b69a54ee
KT
207 * @path: Pointer to "struct path".
208 *
c8c57e84 209 * Returns true on success, false otherwise.
b69a54ee 210 */
c8c57e84 211static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
b69a54ee 212{
c8c57e84
TH
213 buf->name = tomoyo_realpath_from_path(path);
214 if (buf->name) {
215 tomoyo_fill_path_info(buf);
216 return true;
b69a54ee 217 }
c8c57e84 218 return false;
b69a54ee
KT
219}
220
7ef61233
TH
221static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
222 const char *filename2,
223 struct tomoyo_domain_info *const domain,
224 const bool is_delete);
225static int tomoyo_update_path_acl(const u8 type, const char *filename,
226 struct tomoyo_domain_info *const domain,
227 const bool is_delete);
b69a54ee 228
c3fa109a
TH
229/*
230 * tomoyo_globally_readable_list is used for holding list of pathnames which
231 * are by default allowed to be open()ed for reading by any process.
232 *
233 * An entry is added by
234 *
235 * # echo 'allow_read /lib/libc-2.5.so' > \
236 * /sys/kernel/security/tomoyo/exception_policy
237 *
238 * and is deleted by
239 *
240 * # echo 'delete allow_read /lib/libc-2.5.so' > \
241 * /sys/kernel/security/tomoyo/exception_policy
242 *
243 * and all entries are retrieved by
244 *
245 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
246 *
247 * In the example above, any process is allowed to
248 * open("/lib/libc-2.5.so", O_RDONLY).
249 * One exception is, if the domain which current process belongs to is marked
250 * as "ignore_global_allow_read", current process can't do so unless explicitly
251 * given "allow_read /lib/libc-2.5.so" to the domain which current process
252 * belongs to.
253 */
847b173e 254LIST_HEAD(tomoyo_globally_readable_list);
b69a54ee
KT
255
256/**
257 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
258 *
259 * @filename: Filename unconditionally permitted to open() for reading.
260 * @is_delete: True if it is a delete request.
261 *
262 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
263 *
264 * Caller holds tomoyo_read_lock().
b69a54ee
KT
265 */
266static int tomoyo_update_globally_readable_entry(const char *filename,
267 const bool is_delete)
268{
b69a54ee 269 struct tomoyo_globally_readable_file_entry *ptr;
9e4b50e9 270 struct tomoyo_globally_readable_file_entry e = { };
ca0b7df3 271 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 272
3f629636 273 if (!tomoyo_is_correct_word(filename))
b69a54ee 274 return -EINVAL;
9e4b50e9
TH
275 e.filename = tomoyo_get_name(filename);
276 if (!e.filename)
b69a54ee 277 return -ENOMEM;
29282381
TH
278 if (mutex_lock_interruptible(&tomoyo_policy_lock))
279 goto out;
fdb8ebb7 280 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
9e4b50e9 281 if (ptr->filename != e.filename)
b69a54ee
KT
282 continue;
283 ptr->is_deleted = is_delete;
284 error = 0;
ca0b7df3 285 break;
b69a54ee 286 }
9e4b50e9
TH
287 if (!is_delete && error) {
288 struct tomoyo_globally_readable_file_entry *entry =
289 tomoyo_commit_ok(&e, sizeof(e));
290 if (entry) {
291 list_add_tail_rcu(&entry->list,
292 &tomoyo_globally_readable_list);
293 error = 0;
294 }
b69a54ee 295 }
f737d95d 296 mutex_unlock(&tomoyo_policy_lock);
29282381 297 out:
9e4b50e9 298 tomoyo_put_name(e.filename);
b69a54ee
KT
299 return error;
300}
301
302/**
303 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
304 *
305 * @filename: The filename to check.
306 *
307 * Returns true if any domain can open @filename for reading, false otherwise.
fdb8ebb7
TH
308 *
309 * Caller holds tomoyo_read_lock().
b69a54ee
KT
310 */
311static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
312 filename)
313{
314 struct tomoyo_globally_readable_file_entry *ptr;
315 bool found = false;
fdb8ebb7
TH
316
317 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
b69a54ee
KT
318 if (!ptr->is_deleted &&
319 tomoyo_path_matches_pattern(filename, ptr->filename)) {
320 found = true;
321 break;
322 }
323 }
b69a54ee
KT
324 return found;
325}
326
327/**
328 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
329 *
330 * @data: String to parse.
331 * @is_delete: True if it is a delete request.
332 *
333 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
334 *
335 * Caller holds tomoyo_read_lock().
b69a54ee
KT
336 */
337int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
338{
339 return tomoyo_update_globally_readable_entry(data, is_delete);
340}
341
342/**
343 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
344 *
345 * @head: Pointer to "struct tomoyo_io_buffer".
346 *
347 * Returns true on success, false otherwise.
fdb8ebb7
TH
348 *
349 * Caller holds tomoyo_read_lock().
b69a54ee
KT
350 */
351bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
352{
353 struct list_head *pos;
354 bool done = true;
355
b69a54ee
KT
356 list_for_each_cookie(pos, head->read_var2,
357 &tomoyo_globally_readable_list) {
358 struct tomoyo_globally_readable_file_entry *ptr;
359 ptr = list_entry(pos,
360 struct tomoyo_globally_readable_file_entry,
361 list);
362 if (ptr->is_deleted)
363 continue;
7d2948b1
TH
364 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
365 ptr->filename->name);
366 if (!done)
b69a54ee 367 break;
b69a54ee 368 }
b69a54ee
KT
369 return done;
370}
371
c3fa109a
TH
372/* tomoyo_pattern_list is used for holding list of pathnames which are used for
373 * converting pathnames to pathname patterns during learning mode.
374 *
375 * An entry is added by
376 *
377 * # echo 'file_pattern /proc/\$/mounts' > \
378 * /sys/kernel/security/tomoyo/exception_policy
379 *
380 * and is deleted by
381 *
382 * # echo 'delete file_pattern /proc/\$/mounts' > \
383 * /sys/kernel/security/tomoyo/exception_policy
384 *
385 * and all entries are retrieved by
386 *
387 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
388 *
389 * In the example above, if a process which belongs to a domain which is in
390 * learning mode requested open("/proc/1/mounts", O_RDONLY),
391 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
392 * process belongs to.
393 *
394 * It is not a desirable behavior that we have to use /proc/\$/ instead of
395 * /proc/self/ when current process needs to access only current process's
396 * information. As of now, LSM version of TOMOYO is using __d_path() for
397 * calculating pathname. Non LSM version of TOMOYO is using its own function
398 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
399 * current process from accessing other process's information.
400 */
847b173e 401LIST_HEAD(tomoyo_pattern_list);
b69a54ee
KT
402
403/**
404 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
405 *
406 * @pattern: Pathname pattern.
407 * @is_delete: True if it is a delete request.
408 *
409 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
410 *
411 * Caller holds tomoyo_read_lock().
b69a54ee
KT
412 */
413static int tomoyo_update_file_pattern_entry(const char *pattern,
414 const bool is_delete)
415{
b69a54ee 416 struct tomoyo_pattern_entry *ptr;
3f629636 417 struct tomoyo_pattern_entry e = { };
ca0b7df3 418 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 419
3f629636
TH
420 if (!tomoyo_is_correct_word(pattern))
421 return -EINVAL;
422 e.pattern = tomoyo_get_name(pattern);
9e4b50e9 423 if (!e.pattern)
ca0b7df3 424 return error;
29282381
TH
425 if (mutex_lock_interruptible(&tomoyo_policy_lock))
426 goto out;
fdb8ebb7 427 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
9e4b50e9 428 if (e.pattern != ptr->pattern)
b69a54ee
KT
429 continue;
430 ptr->is_deleted = is_delete;
431 error = 0;
ca0b7df3 432 break;
b69a54ee 433 }
9e4b50e9
TH
434 if (!is_delete && error) {
435 struct tomoyo_pattern_entry *entry =
436 tomoyo_commit_ok(&e, sizeof(e));
437 if (entry) {
438 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
439 error = 0;
440 }
b69a54ee 441 }
f737d95d 442 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 443 out:
9e4b50e9 444 tomoyo_put_name(e.pattern);
b69a54ee
KT
445 return error;
446}
447
448/**
17fcfbd9 449 * tomoyo_file_pattern - Get patterned pathname.
b69a54ee
KT
450 *
451 * @filename: The filename to find patterned pathname.
452 *
453 * Returns pointer to pathname pattern if matched, @filename otherwise.
fdb8ebb7
TH
454 *
455 * Caller holds tomoyo_read_lock().
b69a54ee 456 */
17fcfbd9 457const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
b69a54ee
KT
458{
459 struct tomoyo_pattern_entry *ptr;
460 const struct tomoyo_path_info *pattern = NULL;
461
fdb8ebb7 462 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
b69a54ee
KT
463 if (ptr->is_deleted)
464 continue;
465 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
466 continue;
467 pattern = ptr->pattern;
468 if (tomoyo_strendswith(pattern->name, "/\\*")) {
469 /* Do nothing. Try to find the better match. */
470 } else {
471 /* This would be the better match. Use this. */
472 break;
473 }
474 }
b69a54ee
KT
475 if (pattern)
476 filename = pattern;
17fcfbd9 477 return filename->name;
b69a54ee
KT
478}
479
480/**
481 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
482 *
483 * @data: String to parse.
484 * @is_delete: True if it is a delete request.
485 *
486 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
487 *
488 * Caller holds tomoyo_read_lock().
b69a54ee
KT
489 */
490int tomoyo_write_pattern_policy(char *data, const bool is_delete)
491{
492 return tomoyo_update_file_pattern_entry(data, is_delete);
493}
494
495/**
496 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
497 *
498 * @head: Pointer to "struct tomoyo_io_buffer".
499 *
500 * Returns true on success, false otherwise.
fdb8ebb7
TH
501 *
502 * Caller holds tomoyo_read_lock().
b69a54ee
KT
503 */
504bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
505{
506 struct list_head *pos;
507 bool done = true;
508
b69a54ee
KT
509 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
510 struct tomoyo_pattern_entry *ptr;
511 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
512 if (ptr->is_deleted)
513 continue;
7d2948b1
TH
514 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
515 "%s\n", ptr->pattern->name);
516 if (!done)
b69a54ee 517 break;
b69a54ee 518 }
b69a54ee
KT
519 return done;
520}
521
c3fa109a
TH
522/*
523 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
524 * default forbidden to modify already written content of a file.
525 *
526 * An entry is added by
527 *
528 * # echo 'deny_rewrite /var/log/messages' > \
529 * /sys/kernel/security/tomoyo/exception_policy
530 *
531 * and is deleted by
532 *
533 * # echo 'delete deny_rewrite /var/log/messages' > \
534 * /sys/kernel/security/tomoyo/exception_policy
535 *
536 * and all entries are retrieved by
537 *
538 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
539 *
540 * In the example above, if a process requested to rewrite /var/log/messages ,
541 * the process can't rewrite unless the domain which that process belongs to
542 * has "allow_rewrite /var/log/messages" entry.
543 *
544 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
545 * when we want to allow rewriting already unlink()ed file. As of now,
546 * LSM version of TOMOYO is using __d_path() for calculating pathname.
547 * Non LSM version of TOMOYO is using its own function which doesn't append
548 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
549 * need to worry whether the file is already unlink()ed or not.
550 */
847b173e 551LIST_HEAD(tomoyo_no_rewrite_list);
b69a54ee
KT
552
553/**
554 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
555 *
556 * @pattern: Pathname pattern that are not rewritable by default.
557 * @is_delete: True if it is a delete request.
558 *
559 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
560 *
561 * Caller holds tomoyo_read_lock().
b69a54ee
KT
562 */
563static int tomoyo_update_no_rewrite_entry(const char *pattern,
564 const bool is_delete)
565{
ca0b7df3 566 struct tomoyo_no_rewrite_entry *ptr;
9e4b50e9 567 struct tomoyo_no_rewrite_entry e = { };
ca0b7df3 568 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 569
3f629636 570 if (!tomoyo_is_correct_word(pattern))
b69a54ee 571 return -EINVAL;
9e4b50e9
TH
572 e.pattern = tomoyo_get_name(pattern);
573 if (!e.pattern)
ca0b7df3 574 return error;
29282381
TH
575 if (mutex_lock_interruptible(&tomoyo_policy_lock))
576 goto out;
fdb8ebb7 577 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
9e4b50e9 578 if (ptr->pattern != e.pattern)
b69a54ee
KT
579 continue;
580 ptr->is_deleted = is_delete;
581 error = 0;
ca0b7df3 582 break;
b69a54ee 583 }
9e4b50e9
TH
584 if (!is_delete && error) {
585 struct tomoyo_no_rewrite_entry *entry =
586 tomoyo_commit_ok(&e, sizeof(e));
587 if (entry) {
588 list_add_tail_rcu(&entry->list,
589 &tomoyo_no_rewrite_list);
590 error = 0;
591 }
b69a54ee 592 }
f737d95d 593 mutex_unlock(&tomoyo_policy_lock);
29282381 594 out:
9e4b50e9 595 tomoyo_put_name(e.pattern);
b69a54ee
KT
596 return error;
597}
598
599/**
600 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
601 *
602 * @filename: Filename to check.
603 *
604 * Returns true if @filename is specified by "deny_rewrite" directive,
605 * false otherwise.
fdb8ebb7
TH
606 *
607 * Caller holds tomoyo_read_lock().
b69a54ee
KT
608 */
609static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
610{
611 struct tomoyo_no_rewrite_entry *ptr;
612 bool found = false;
613
fdb8ebb7 614 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
b69a54ee
KT
615 if (ptr->is_deleted)
616 continue;
617 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
618 continue;
619 found = true;
620 break;
621 }
b69a54ee
KT
622 return found;
623}
624
625/**
626 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
627 *
628 * @data: String to parse.
629 * @is_delete: True if it is a delete request.
630 *
631 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
632 *
633 * Caller holds tomoyo_read_lock().
b69a54ee
KT
634 */
635int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
636{
637 return tomoyo_update_no_rewrite_entry(data, is_delete);
638}
639
640/**
641 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
642 *
643 * @head: Pointer to "struct tomoyo_io_buffer".
644 *
645 * Returns true on success, false otherwise.
fdb8ebb7
TH
646 *
647 * Caller holds tomoyo_read_lock().
b69a54ee
KT
648 */
649bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
650{
651 struct list_head *pos;
652 bool done = true;
653
b69a54ee
KT
654 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
655 struct tomoyo_no_rewrite_entry *ptr;
656 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
657 if (ptr->is_deleted)
658 continue;
7d2948b1
TH
659 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
660 "%s\n", ptr->pattern->name);
661 if (!done)
b69a54ee 662 break;
b69a54ee 663 }
b69a54ee
KT
664 return done;
665}
666
667/**
668 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
669 *
b69a54ee 670 * @perm: Permission (between 1 to 7).
a1f9bb6a 671 * @filename: Filename.
b69a54ee
KT
672 * @domain: Pointer to "struct tomoyo_domain_info".
673 * @is_delete: True if it is a delete request.
674 *
675 * Returns 0 on success, negative value otherwise.
676 *
677 * This is legacy support interface for older policy syntax.
678 * Current policy syntax uses "allow_read/write" instead of "6",
679 * "allow_read" instead of "4", "allow_write" instead of "2",
680 * "allow_execute" instead of "1".
fdb8ebb7
TH
681 *
682 * Caller holds tomoyo_read_lock().
b69a54ee 683 */
a1f9bb6a 684static int tomoyo_update_file_acl(u8 perm, const char *filename,
b69a54ee
KT
685 struct tomoyo_domain_info * const domain,
686 const bool is_delete)
687{
688 if (perm > 7 || !perm) {
689 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
690 __func__, perm, filename);
691 return -EINVAL;
692 }
693 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
694 /*
695 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
696 * directory permissions.
697 */
698 return 0;
699 if (perm & 4)
7ef61233
TH
700 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
701 is_delete);
b69a54ee 702 if (perm & 2)
7ef61233
TH
703 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
704 is_delete);
b69a54ee 705 if (perm & 1)
7ef61233
TH
706 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
707 is_delete);
b69a54ee
KT
708 return 0;
709}
710
711/**
cb0abe6a 712 * tomoyo_path_acl - Check permission for single path operation.
b69a54ee 713 *
cb0abe6a 714 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee
KT
715 * @filename: Filename to check.
716 * @perm: Permission.
b69a54ee
KT
717 *
718 * Returns 0 on success, -EPERM otherwise.
fdb8ebb7
TH
719 *
720 * Caller holds tomoyo_read_lock().
b69a54ee 721 */
cb0abe6a
TH
722static int tomoyo_path_acl(const struct tomoyo_request_info *r,
723 const struct tomoyo_path_info *filename,
3f629636 724 const u32 perm)
b69a54ee 725{
cb0abe6a 726 struct tomoyo_domain_info *domain = r->domain;
b69a54ee
KT
727 struct tomoyo_acl_info *ptr;
728 int error = -EPERM;
729
fdb8ebb7 730 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
731 struct tomoyo_path_acl *acl;
732 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
b69a54ee 733 continue;
7ef61233 734 acl = container_of(ptr, struct tomoyo_path_acl, head);
a1f9bb6a 735 if (!(acl->perm & perm) ||
3f629636 736 !tomoyo_compare_name_union(filename, &acl->name))
b69a54ee 737 continue;
b69a54ee
KT
738 error = 0;
739 break;
740 }
b69a54ee
KT
741 return error;
742}
743
744/**
cb0abe6a 745 * tomoyo_file_perm - Check permission for opening files.
b69a54ee 746 *
cb0abe6a 747 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee 748 * @filename: Filename to check.
cb0abe6a 749 * @mode: Mode ("read" or "write" or "read/write" or "execute").
b69a54ee
KT
750 *
751 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
752 *
753 * Caller holds tomoyo_read_lock().
b69a54ee 754 */
cb0abe6a
TH
755static int tomoyo_file_perm(struct tomoyo_request_info *r,
756 const struct tomoyo_path_info *filename,
757 const u8 mode)
b69a54ee 758{
b69a54ee
KT
759 const char *msg = "<unknown>";
760 int error = 0;
cb0abe6a 761 u32 perm = 0;
b69a54ee
KT
762
763 if (!filename)
764 return 0;
cb0abe6a
TH
765
766 if (mode == 6) {
7ef61233 767 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
cb0abe6a
TH
768 perm = 1 << TOMOYO_TYPE_READ_WRITE;
769 } else if (mode == 4) {
7ef61233 770 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
cb0abe6a
TH
771 perm = 1 << TOMOYO_TYPE_READ;
772 } else if (mode == 2) {
7ef61233 773 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
cb0abe6a
TH
774 perm = 1 << TOMOYO_TYPE_WRITE;
775 } else if (mode == 1) {
7ef61233 776 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
cb0abe6a
TH
777 perm = 1 << TOMOYO_TYPE_EXECUTE;
778 } else
b69a54ee 779 BUG();
17fcfbd9 780 do {
3f629636 781 error = tomoyo_path_acl(r, filename, perm);
17fcfbd9
TH
782 if (error && mode == 4 && !r->domain->ignore_global_allow_read
783 && tomoyo_is_globally_readable_file(filename))
784 error = 0;
785 if (!error)
786 break;
787 tomoyo_warn_log(r, "%s %s", msg, filename->name);
788 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
17fcfbd9
TH
789 tomoyo_file_pattern(filename));
790 /*
791 * Do not retry for execute request, for alias may have
792 * changed.
793 */
794 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
795 if (r->mode != TOMOYO_CONFIG_ENFORCING)
cb0abe6a 796 error = 0;
17fcfbd9 797 return error;
b69a54ee
KT
798}
799
b69a54ee 800/**
7ef61233 801 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
b69a54ee
KT
802 *
803 * @type: Type of operation.
804 * @filename: Filename.
805 * @domain: Pointer to "struct tomoyo_domain_info".
806 * @is_delete: True if it is a delete request.
807 *
808 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
809 *
810 * Caller holds tomoyo_read_lock().
b69a54ee 811 */
7ef61233
TH
812static int tomoyo_update_path_acl(const u8 type, const char *filename,
813 struct tomoyo_domain_info *const domain,
814 const bool is_delete)
b69a54ee 815{
a1f9bb6a 816 static const u16 tomoyo_rw_mask =
7ef61233 817 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
a1f9bb6a 818 const u16 perm = 1 << type;
b69a54ee 819 struct tomoyo_acl_info *ptr;
9e4b50e9
TH
820 struct tomoyo_path_acl e = {
821 .head.type = TOMOYO_TYPE_PATH_ACL,
9e4b50e9
TH
822 .perm = perm
823 };
ca0b7df3 824 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee 825
9e4b50e9
TH
826 if (type == TOMOYO_TYPE_READ_WRITE)
827 e.perm |= tomoyo_rw_mask;
b69a54ee
KT
828 if (!domain)
829 return -EINVAL;
7762fbff 830 if (!tomoyo_parse_name_union(filename, &e.name))
b69a54ee 831 return -EINVAL;
29282381
TH
832 if (mutex_lock_interruptible(&tomoyo_policy_lock))
833 goto out;
fdb8ebb7 834 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
835 struct tomoyo_path_acl *acl =
836 container_of(ptr, struct tomoyo_path_acl, head);
7762fbff 837 if (!tomoyo_is_same_path_acl(acl, &e))
b69a54ee 838 continue;
ca0b7df3 839 if (is_delete) {
a1f9bb6a 840 acl->perm &= ~perm;
9e4b50e9 841 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
7ef61233
TH
842 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
843 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
9e4b50e9 844 acl->perm &= ~tomoyo_rw_mask;
ca0b7df3 845 } else {
a1f9bb6a 846 acl->perm |= perm;
9e4b50e9 847 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
7ef61233
TH
848 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
849 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
9e4b50e9 850 acl->perm |= tomoyo_rw_mask;
ca0b7df3 851 }
b69a54ee 852 error = 0;
ca0b7df3 853 break;
cd7bec6a 854 }
9e4b50e9
TH
855 if (!is_delete && error) {
856 struct tomoyo_path_acl *entry =
857 tomoyo_commit_ok(&e, sizeof(e));
858 if (entry) {
859 list_add_tail_rcu(&entry->head.list,
860 &domain->acl_info_list);
861 error = 0;
862 }
b69a54ee 863 }
f737d95d 864 mutex_unlock(&tomoyo_policy_lock);
29282381 865 out:
7762fbff 866 tomoyo_put_name_union(&e.name);
b69a54ee
KT
867 return error;
868}
869
a1f9bb6a
TH
870/**
871 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
872 *
873 * @type: Type of operation.
874 * @filename: Filename.
875 * @mode: Create mode.
876 * @major: Device major number.
877 * @minor: Device minor number.
878 * @domain: Pointer to "struct tomoyo_domain_info".
879 * @is_delete: True if it is a delete request.
880 *
881 * Returns 0 on success, negative value otherwise.
882 */
883static inline int tomoyo_update_path_number3_acl(const u8 type,
884 const char *filename,
885 char *mode,
886 char *major, char *minor,
887 struct tomoyo_domain_info *
888 const domain,
889 const bool is_delete)
890{
891 const u8 perm = 1 << type;
892 struct tomoyo_acl_info *ptr;
893 struct tomoyo_path_number3_acl e = {
894 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
895 .perm = perm
896 };
897 int error = is_delete ? -ENOENT : -ENOMEM;
898 if (!tomoyo_parse_name_union(filename, &e.name) ||
899 !tomoyo_parse_number_union(mode, &e.mode) ||
900 !tomoyo_parse_number_union(major, &e.major) ||
901 !tomoyo_parse_number_union(minor, &e.minor))
902 goto out;
903 if (mutex_lock_interruptible(&tomoyo_policy_lock))
904 goto out;
905 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
906 struct tomoyo_path_number3_acl *acl =
907 container_of(ptr, struct tomoyo_path_number3_acl, head);
908 if (!tomoyo_is_same_path_number3_acl(acl, &e))
909 continue;
910 if (is_delete)
911 acl->perm &= ~perm;
912 else
913 acl->perm |= perm;
914 error = 0;
915 break;
916 }
917 if (!is_delete && error) {
918 struct tomoyo_path_number3_acl *entry =
919 tomoyo_commit_ok(&e, sizeof(e));
920 if (entry) {
921 list_add_tail_rcu(&entry->head.list,
922 &domain->acl_info_list);
923 error = 0;
924 }
925 }
926 mutex_unlock(&tomoyo_policy_lock);
927 out:
928 tomoyo_put_name_union(&e.name);
929 tomoyo_put_number_union(&e.mode);
930 tomoyo_put_number_union(&e.major);
931 tomoyo_put_number_union(&e.minor);
932 return error;
933}
934
b69a54ee 935/**
7ef61233 936 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
b69a54ee
KT
937 *
938 * @type: Type of operation.
939 * @filename1: First filename.
940 * @filename2: Second filename.
941 * @domain: Pointer to "struct tomoyo_domain_info".
942 * @is_delete: True if it is a delete request.
943 *
944 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
945 *
946 * Caller holds tomoyo_read_lock().
b69a54ee 947 */
7ef61233
TH
948static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
949 const char *filename2,
950 struct tomoyo_domain_info *const domain,
951 const bool is_delete)
b69a54ee 952{
9e4b50e9
TH
953 const u8 perm = 1 << type;
954 struct tomoyo_path2_acl e = {
955 .head.type = TOMOYO_TYPE_PATH2_ACL,
956 .perm = perm
957 };
b69a54ee 958 struct tomoyo_acl_info *ptr;
ca0b7df3 959 int error = is_delete ? -ENOENT : -ENOMEM;
b69a54ee
KT
960
961 if (!domain)
962 return -EINVAL;
7762fbff
TH
963 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
964 !tomoyo_parse_name_union(filename2, &e.name2))
ca0b7df3 965 goto out;
29282381
TH
966 if (mutex_lock_interruptible(&tomoyo_policy_lock))
967 goto out;
fdb8ebb7 968 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
969 struct tomoyo_path2_acl *acl =
970 container_of(ptr, struct tomoyo_path2_acl, head);
7762fbff 971 if (!tomoyo_is_same_path2_acl(acl, &e))
b69a54ee 972 continue;
ca0b7df3
TH
973 if (is_delete)
974 acl->perm &= ~perm;
975 else
976 acl->perm |= perm;
b69a54ee 977 error = 0;
ca0b7df3 978 break;
cd7bec6a 979 }
9e4b50e9
TH
980 if (!is_delete && error) {
981 struct tomoyo_path2_acl *entry =
982 tomoyo_commit_ok(&e, sizeof(e));
983 if (entry) {
984 list_add_tail_rcu(&entry->head.list,
985 &domain->acl_info_list);
986 error = 0;
987 }
b69a54ee 988 }
f737d95d 989 mutex_unlock(&tomoyo_policy_lock);
ca0b7df3 990 out:
7762fbff
TH
991 tomoyo_put_name_union(&e.name1);
992 tomoyo_put_name_union(&e.name2);
b69a54ee
KT
993 return error;
994}
995
a1f9bb6a
TH
996/**
997 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
998 *
999 * @r: Pointer to "struct tomoyo_request_info".
1000 * @filename: Filename to check.
1001 * @perm: Permission.
1002 * @mode: Create mode.
1003 * @major: Device major number.
1004 * @minor: Device minor number.
1005 *
1006 * Returns 0 on success, -EPERM otherwise.
1007 *
1008 * Caller holds tomoyo_read_lock().
1009 */
1010static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
1011 const struct tomoyo_path_info *filename,
1012 const u16 perm, const unsigned int mode,
1013 const unsigned int major,
1014 const unsigned int minor)
1015{
1016 struct tomoyo_domain_info *domain = r->domain;
1017 struct tomoyo_acl_info *ptr;
1018 int error = -EPERM;
1019 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1020 struct tomoyo_path_number3_acl *acl;
1021 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
1022 continue;
1023 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
1024 if (!tomoyo_compare_number_union(mode, &acl->mode))
1025 continue;
1026 if (!tomoyo_compare_number_union(major, &acl->major))
1027 continue;
1028 if (!tomoyo_compare_number_union(minor, &acl->minor))
1029 continue;
1030 if (!(acl->perm & perm))
1031 continue;
1032 if (!tomoyo_compare_name_union(filename, &acl->name))
1033 continue;
1034 error = 0;
1035 break;
1036 }
1037 return error;
1038}
1039
b69a54ee 1040/**
7ef61233 1041 * tomoyo_path2_acl - Check permission for double path operation.
b69a54ee 1042 *
cb0abe6a 1043 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee
KT
1044 * @type: Type of operation.
1045 * @filename1: First filename to check.
1046 * @filename2: Second filename to check.
1047 *
1048 * Returns 0 on success, -EPERM otherwise.
fdb8ebb7
TH
1049 *
1050 * Caller holds tomoyo_read_lock().
b69a54ee 1051 */
cb0abe6a 1052static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
7ef61233
TH
1053 const struct tomoyo_path_info *filename1,
1054 const struct tomoyo_path_info *filename2)
b69a54ee 1055{
cb0abe6a 1056 const struct tomoyo_domain_info *domain = r->domain;
b69a54ee
KT
1057 struct tomoyo_acl_info *ptr;
1058 const u8 perm = 1 << type;
1059 int error = -EPERM;
1060
fdb8ebb7 1061 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7ef61233
TH
1062 struct tomoyo_path2_acl *acl;
1063 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
b69a54ee 1064 continue;
7ef61233 1065 acl = container_of(ptr, struct tomoyo_path2_acl, head);
b69a54ee
KT
1066 if (!(acl->perm & perm))
1067 continue;
7762fbff 1068 if (!tomoyo_compare_name_union(filename1, &acl->name1))
b69a54ee 1069 continue;
7762fbff 1070 if (!tomoyo_compare_name_union(filename2, &acl->name2))
b69a54ee
KT
1071 continue;
1072 error = 0;
1073 break;
1074 }
b69a54ee
KT
1075 return error;
1076}
1077
1078/**
cb0abe6a 1079 * tomoyo_path_permission - Check permission for single path operation.
b69a54ee 1080 *
cb0abe6a 1081 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee
KT
1082 * @operation: Type of operation.
1083 * @filename: Filename to check.
b69a54ee
KT
1084 *
1085 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
1086 *
1087 * Caller holds tomoyo_read_lock().
b69a54ee 1088 */
cb0abe6a
TH
1089static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1090 const struct tomoyo_path_info *filename)
b69a54ee 1091{
17fcfbd9 1092 const char *msg;
b69a54ee 1093 int error;
b69a54ee 1094
b69a54ee 1095 next:
57c2590f
TH
1096 r->type = tomoyo_p2mac[operation];
1097 r->mode = tomoyo_get_mode(r->profile, r->type);
1098 if (r->mode == TOMOYO_CONFIG_DISABLED)
1099 return 0;
17fcfbd9 1100 do {
3f629636 1101 error = tomoyo_path_acl(r, filename, 1 << operation);
17fcfbd9
TH
1102 if (!error)
1103 break;
1104 msg = tomoyo_path2keyword(operation);
1105 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1106 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1107 tomoyo_file_pattern(filename));
1108 } while (error == TOMOYO_RETRY_REQUEST);
cb0abe6a 1109 if (r->mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee 1110 error = 0;
b69a54ee
KT
1111 /*
1112 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1113 * we need to check "allow_rewrite" permission if the filename is
1114 * specified by "deny_rewrite" keyword.
1115 */
7ef61233 1116 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
b69a54ee 1117 tomoyo_is_no_rewrite_file(filename)) {
7ef61233 1118 operation = TOMOYO_TYPE_REWRITE;
b69a54ee
KT
1119 goto next;
1120 }
1121 return error;
1122}
1123
a1f9bb6a
TH
1124/**
1125 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1126 *
1127 * @r: Pointer to "struct tomoyo_request_info".
1128 * @type: Operation.
1129 * @filename: Filename to check.
1130 * @number: Number.
1131 *
1132 * Returns 0 on success, -EPERM otherwise.
1133 *
1134 * Caller holds tomoyo_read_lock().
1135 */
1136static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1137 const struct tomoyo_path_info *filename,
1138 const unsigned long number)
1139{
1140 struct tomoyo_domain_info *domain = r->domain;
1141 struct tomoyo_acl_info *ptr;
1142 const u8 perm = 1 << type;
1143 int error = -EPERM;
1144 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1145 struct tomoyo_path_number_acl *acl;
1146 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1147 continue;
1148 acl = container_of(ptr, struct tomoyo_path_number_acl,
1149 head);
1150 if (!(acl->perm & perm) ||
1151 !tomoyo_compare_number_union(number, &acl->number) ||
1152 !tomoyo_compare_name_union(filename, &acl->name))
1153 continue;
1154 error = 0;
1155 break;
1156 }
1157 return error;
1158}
1159
1160/**
1161 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1162 *
1163 * @type: Type of operation.
1164 * @filename: Filename.
1165 * @number: Number.
1166 * @domain: Pointer to "struct tomoyo_domain_info".
1167 * @is_delete: True if it is a delete request.
1168 *
1169 * Returns 0 on success, negative value otherwise.
1170 */
1171static inline int tomoyo_update_path_number_acl(const u8 type,
1172 const char *filename,
1173 char *number,
1174 struct tomoyo_domain_info *
1175 const domain,
1176 const bool is_delete)
1177{
1178 const u8 perm = 1 << type;
1179 struct tomoyo_acl_info *ptr;
1180 struct tomoyo_path_number_acl e = {
1181 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1182 .perm = perm
1183 };
1184 int error = is_delete ? -ENOENT : -ENOMEM;
1185 if (!domain)
1186 return -EINVAL;
1187 if (!tomoyo_parse_name_union(filename, &e.name))
1188 return -EINVAL;
1189 if (!tomoyo_parse_number_union(number, &e.number))
1190 goto out;
1191 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1192 goto out;
1193 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1194 struct tomoyo_path_number_acl *acl =
1195 container_of(ptr, struct tomoyo_path_number_acl, head);
1196 if (!tomoyo_is_same_path_number_acl(acl, &e))
1197 continue;
1198 if (is_delete)
1199 acl->perm &= ~perm;
1200 else
1201 acl->perm |= perm;
1202 error = 0;
1203 break;
1204 }
1205 if (!is_delete && error) {
1206 struct tomoyo_path_number_acl *entry =
1207 tomoyo_commit_ok(&e, sizeof(e));
1208 if (entry) {
1209 list_add_tail_rcu(&entry->head.list,
1210 &domain->acl_info_list);
1211 error = 0;
1212 }
1213 }
1214 mutex_unlock(&tomoyo_policy_lock);
1215 out:
1216 tomoyo_put_name_union(&e.name);
1217 tomoyo_put_number_union(&e.number);
1218 return error;
1219}
1220
1221/**
1222 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1223 *
1224 * @r: Pointer to "strct tomoyo_request_info".
1225 * @filename: Filename to check.
1226 * @number: Number.
1227 *
1228 * Returns 0 on success, negative value otherwise.
1229 *
1230 * Caller holds tomoyo_read_lock().
1231 */
1232static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1233 const u8 type,
1234 const struct tomoyo_path_info *filename,
1235 const unsigned long number)
1236{
1237 char buffer[64];
1238 int error;
1239 u8 radix;
17fcfbd9 1240 const char *msg;
a1f9bb6a
TH
1241
1242 if (!filename)
1243 return 0;
1244 switch (type) {
1245 case TOMOYO_TYPE_CREATE:
1246 case TOMOYO_TYPE_MKDIR:
1247 case TOMOYO_TYPE_MKFIFO:
1248 case TOMOYO_TYPE_MKSOCK:
1249 case TOMOYO_TYPE_CHMOD:
1250 radix = TOMOYO_VALUE_TYPE_OCTAL;
1251 break;
1252 case TOMOYO_TYPE_IOCTL:
1253 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1254 break;
1255 default:
1256 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1257 break;
1258 }
1259 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
17fcfbd9
TH
1260 do {
1261 error = tomoyo_path_number_acl(r, type, filename, number);
1262 if (!error)
1263 break;
1264 msg = tomoyo_path_number2keyword(type);
1265 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1266 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1267 tomoyo_file_pattern(filename),
1268 buffer);
1269 } while (error == TOMOYO_RETRY_REQUEST);
a1f9bb6a
TH
1270 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1271 error = 0;
1272 return error;
1273}
1274
1275/**
1276 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1277 *
1278 * @type: Type of operation.
1279 * @path: Pointer to "struct path".
1280 * @number: Number.
1281 *
1282 * Returns 0 on success, negative value otherwise.
1283 */
1284int tomoyo_path_number_perm(const u8 type, struct path *path,
1285 unsigned long number)
1286{
1287 struct tomoyo_request_info r;
1288 int error = -ENOMEM;
c8c57e84 1289 struct tomoyo_path_info buf;
a1f9bb6a
TH
1290 int idx;
1291
57c2590f
TH
1292 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
1293 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
a1f9bb6a
TH
1294 return 0;
1295 idx = tomoyo_read_lock();
c8c57e84 1296 if (!tomoyo_get_realpath(&buf, path))
a1f9bb6a 1297 goto out;
c8c57e84
TH
1298 if (type == TOMOYO_TYPE_MKDIR)
1299 tomoyo_add_slash(&buf);
1300 error = tomoyo_path_number_perm2(&r, type, &buf, number);
a1f9bb6a 1301 out:
c8c57e84 1302 kfree(buf.name);
a1f9bb6a
TH
1303 tomoyo_read_unlock(idx);
1304 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1305 error = 0;
1306 return error;
1307}
1308
b69a54ee
KT
1309/**
1310 * tomoyo_check_exec_perm - Check permission for "execute".
1311 *
57c2590f 1312 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee 1313 * @filename: Check permission for "execute".
b69a54ee
KT
1314 *
1315 * Returns 0 on success, negativevalue otherwise.
fdb8ebb7
TH
1316 *
1317 * Caller holds tomoyo_read_lock().
b69a54ee 1318 */
57c2590f 1319int tomoyo_check_exec_perm(struct tomoyo_request_info *r,
bcb86975 1320 const struct tomoyo_path_info *filename)
b69a54ee 1321{
57c2590f 1322 if (r->mode == TOMOYO_CONFIG_DISABLED)
b69a54ee 1323 return 0;
57c2590f 1324 return tomoyo_file_perm(r, filename, 1);
b69a54ee
KT
1325}
1326
1327/**
1328 * tomoyo_check_open_permission - Check permission for "read" and "write".
1329 *
1330 * @domain: Pointer to "struct tomoyo_domain_info".
1331 * @path: Pointer to "struct path".
1332 * @flag: Flags for open().
1333 *
1334 * Returns 0 on success, negative value otherwise.
1335 */
1336int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1337 struct path *path, const int flag)
1338{
1339 const u8 acc_mode = ACC_MODE(flag);
1340 int error = -ENOMEM;
c8c57e84 1341 struct tomoyo_path_info buf;
cb0abe6a 1342 struct tomoyo_request_info r;
fdb8ebb7 1343 int idx;
b69a54ee 1344
57c2590f
TH
1345 if (!path->mnt ||
1346 (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
b69a54ee 1347 return 0;
57c2590f
TH
1348 buf.name = NULL;
1349 r.mode = TOMOYO_CONFIG_DISABLED;
fdb8ebb7 1350 idx = tomoyo_read_lock();
c8c57e84 1351 if (!tomoyo_get_realpath(&buf, path))
b69a54ee
KT
1352 goto out;
1353 error = 0;
1354 /*
1355 * If the filename is specified by "deny_rewrite" keyword,
1356 * we need to check "allow_rewrite" permission when the filename is not
1357 * opened for append mode or the filename is truncated at open time.
1358 */
57c2590f
TH
1359 if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
1360 && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
1361 != TOMOYO_CONFIG_DISABLED) {
1362 if (!tomoyo_get_realpath(&buf, path)) {
1363 error = -ENOMEM;
1364 goto out;
1365 }
1366 if (tomoyo_is_no_rewrite_file(&buf))
1367 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
1368 &buf);
b69a54ee 1369 }
57c2590f
TH
1370 if (!error && acc_mode &&
1371 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
1372 != TOMOYO_CONFIG_DISABLED) {
1373 if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
1374 error = -ENOMEM;
1375 goto out;
1376 }
c8c57e84 1377 error = tomoyo_file_perm(&r, &buf, acc_mode);
57c2590f 1378 }
b69a54ee 1379 out:
c8c57e84 1380 kfree(buf.name);
fdb8ebb7 1381 tomoyo_read_unlock(idx);
cb0abe6a 1382 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
1383 error = 0;
1384 return error;
1385}
1386
1387/**
2106ccd9 1388 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
b69a54ee 1389 *
b69a54ee
KT
1390 * @operation: Type of operation.
1391 * @path: Pointer to "struct path".
1392 *
1393 * Returns 0 on success, negative value otherwise.
1394 */
97d6931e 1395int tomoyo_path_perm(const u8 operation, struct path *path)
b69a54ee
KT
1396{
1397 int error = -ENOMEM;
c8c57e84 1398 struct tomoyo_path_info buf;
cb0abe6a 1399 struct tomoyo_request_info r;
fdb8ebb7 1400 int idx;
b69a54ee 1401
57c2590f
TH
1402 if (!path->mnt)
1403 return 0;
1404 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
1405 == TOMOYO_CONFIG_DISABLED)
b69a54ee 1406 return 0;
57c2590f 1407 buf.name = NULL;
fdb8ebb7 1408 idx = tomoyo_read_lock();
c8c57e84 1409 if (!tomoyo_get_realpath(&buf, path))
b69a54ee
KT
1410 goto out;
1411 switch (operation) {
cb0abe6a 1412 case TOMOYO_TYPE_REWRITE:
c8c57e84 1413 if (!tomoyo_is_no_rewrite_file(&buf)) {
cb0abe6a
TH
1414 error = 0;
1415 goto out;
1416 }
1417 break;
7ef61233
TH
1418 case TOMOYO_TYPE_RMDIR:
1419 case TOMOYO_TYPE_CHROOT:
57c2590f 1420 case TOMOYO_TYPE_UMOUNT:
c8c57e84
TH
1421 tomoyo_add_slash(&buf);
1422 break;
b69a54ee 1423 }
c8c57e84 1424 error = tomoyo_path_permission(&r, operation, &buf);
b69a54ee 1425 out:
c8c57e84 1426 kfree(buf.name);
fdb8ebb7 1427 tomoyo_read_unlock(idx);
cb0abe6a 1428 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
1429 error = 0;
1430 return error;
1431}
1432
a1f9bb6a
TH
1433/**
1434 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1435 *
1436 * @r: Pointer to "struct tomoyo_request_info".
1437 * @operation: Type of operation.
1438 * @filename: Filename to check.
1439 * @mode: Create mode.
1440 * @dev: Device number.
1441 *
1442 * Returns 0 on success, negative value otherwise.
1443 *
1444 * Caller holds tomoyo_read_lock().
1445 */
1446static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1447 const u8 operation,
1448 const struct tomoyo_path_info *filename,
1449 const unsigned int mode,
1450 const unsigned int dev)
1451{
1452 int error;
17fcfbd9 1453 const char *msg;
a1f9bb6a
TH
1454 const unsigned int major = MAJOR(dev);
1455 const unsigned int minor = MINOR(dev);
1456
17fcfbd9
TH
1457 do {
1458 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1459 mode, major, minor);
1460 if (!error)
1461 break;
1462 msg = tomoyo_path_number32keyword(operation);
1463 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1464 mode, major, minor);
1465 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1466 tomoyo_file_pattern(filename), mode,
1467 major, minor);
1468 } while (error == TOMOYO_RETRY_REQUEST);
1469 if (r->mode != TOMOYO_CONFIG_ENFORCING)
a1f9bb6a
TH
1470 error = 0;
1471 return error;
1472}
1473
1474/**
1475 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1476 *
1477 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1478 * @path: Pointer to "struct path".
1479 * @mode: Create mode.
1480 * @dev: Device number.
1481 *
1482 * Returns 0 on success, negative value otherwise.
1483 */
1484int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1485 const unsigned int mode, unsigned int dev)
1486{
1487 struct tomoyo_request_info r;
1488 int error = -ENOMEM;
c8c57e84 1489 struct tomoyo_path_info buf;
a1f9bb6a
TH
1490 int idx;
1491
57c2590f
TH
1492 if (!path->mnt ||
1493 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
1494 == TOMOYO_CONFIG_DISABLED)
a1f9bb6a
TH
1495 return 0;
1496 idx = tomoyo_read_lock();
1497 error = -ENOMEM;
c8c57e84
TH
1498 if (tomoyo_get_realpath(&buf, path)) {
1499 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
a1f9bb6a 1500 new_decode_dev(dev));
c8c57e84 1501 kfree(buf.name);
a1f9bb6a
TH
1502 }
1503 tomoyo_read_unlock(idx);
1504 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1505 error = 0;
1506 return error;
1507}
1508
b69a54ee 1509/**
7ef61233 1510 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
b69a54ee 1511 *
b69a54ee
KT
1512 * @operation: Type of operation.
1513 * @path1: Pointer to "struct path".
1514 * @path2: Pointer to "struct path".
1515 *
1516 * Returns 0 on success, negative value otherwise.
1517 */
97d6931e 1518int tomoyo_path2_perm(const u8 operation, struct path *path1,
7ef61233 1519 struct path *path2)
b69a54ee
KT
1520{
1521 int error = -ENOMEM;
17fcfbd9 1522 const char *msg;
c8c57e84
TH
1523 struct tomoyo_path_info buf1;
1524 struct tomoyo_path_info buf2;
cb0abe6a 1525 struct tomoyo_request_info r;
fdb8ebb7 1526 int idx;
b69a54ee 1527
57c2590f
TH
1528 if (!path1->mnt || !path2->mnt ||
1529 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
1530 == TOMOYO_CONFIG_DISABLED)
b69a54ee 1531 return 0;
c8c57e84
TH
1532 buf1.name = NULL;
1533 buf2.name = NULL;
fdb8ebb7 1534 idx = tomoyo_read_lock();
c8c57e84
TH
1535 if (!tomoyo_get_realpath(&buf1, path1) ||
1536 !tomoyo_get_realpath(&buf2, path2))
b69a54ee 1537 goto out;
57c2590f
TH
1538 switch (operation) {
1539 struct dentry *dentry;
1540 case TOMOYO_TYPE_RENAME:
1541 case TOMOYO_TYPE_LINK:
1542 dentry = path1->dentry;
1543 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
1544 break;
1545 /* fall through */
1546 case TOMOYO_TYPE_PIVOT_ROOT:
1547 tomoyo_add_slash(&buf1);
1548 tomoyo_add_slash(&buf2);
1549 break;
1550 }
17fcfbd9 1551 do {
c8c57e84 1552 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
17fcfbd9
TH
1553 if (!error)
1554 break;
1555 msg = tomoyo_path22keyword(operation);
c8c57e84 1556 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
17fcfbd9 1557 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
c8c57e84
TH
1558 tomoyo_file_pattern(&buf1),
1559 tomoyo_file_pattern(&buf2));
17fcfbd9 1560 } while (error == TOMOYO_RETRY_REQUEST);
b69a54ee 1561 out:
c8c57e84
TH
1562 kfree(buf1.name);
1563 kfree(buf2.name);
fdb8ebb7 1564 tomoyo_read_unlock(idx);
cb0abe6a 1565 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
1566 error = 0;
1567 return error;
1568}
a1f9bb6a
TH
1569
1570/**
1571 * tomoyo_write_file_policy - Update file related list.
1572 *
1573 * @data: String to parse.
1574 * @domain: Pointer to "struct tomoyo_domain_info".
1575 * @is_delete: True if it is a delete request.
1576 *
1577 * Returns 0 on success, negative value otherwise.
1578 *
1579 * Caller holds tomoyo_read_lock().
1580 */
1581int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1582 const bool is_delete)
1583{
1584 char *w[5];
1585 u8 type;
1586 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1587 return -EINVAL;
1588 if (strncmp(w[0], "allow_", 6)) {
1589 unsigned int perm;
1590 if (sscanf(w[0], "%u", &perm) == 1)
1591 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1592 is_delete);
1593 goto out;
1594 }
1595 w[0] += 6;
1596 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1597 if (strcmp(w[0], tomoyo_path_keyword[type]))
1598 continue;
1599 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1600 }
1601 if (!w[2][0])
1602 goto out;
1603 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1604 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1605 continue;
1606 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1607 is_delete);
1608 }
1609 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1610 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1611 continue;
1612 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1613 is_delete);
1614 }
1615 if (!w[3][0] || !w[4][0])
1616 goto out;
1617 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1618 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1619 continue;
1620 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1621 w[4], domain, is_delete);
1622 }
1623 out:
1624 return -EINVAL;
1625}