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