]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/file.c
TOMOYO: Rename symbols.
[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. */
71c28236 13const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
7ef61233
TH
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 27/* Keyword array for operations with one pathname and three numbers. */
71c28236 28const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
a1f9bb6a
TH
29 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
30 [TOMOYO_TYPE_MKCHAR] = "mkchar",
31};
32
33/* Keyword array for operations with two pathnames. */
71c28236 34const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
a1f9bb6a
TH
35 [TOMOYO_TYPE_LINK] = "link",
36 [TOMOYO_TYPE_RENAME] = "rename",
7ef61233 37 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
b69a54ee
KT
38};
39
a1f9bb6a 40/* Keyword array for operations with one pathname and one number. */
71c28236 41const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
a1f9bb6a
TH
42 [TOMOYO_TYPE_CREATE] = "create",
43 [TOMOYO_TYPE_MKDIR] = "mkdir",
44 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
45 [TOMOYO_TYPE_MKSOCK] = "mksock",
46 [TOMOYO_TYPE_IOCTL] = "ioctl",
47 [TOMOYO_TYPE_CHMOD] = "chmod",
48 [TOMOYO_TYPE_CHOWN] = "chown",
49 [TOMOYO_TYPE_CHGRP] = "chgrp",
50};
51
57c2590f
TH
52static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
53 [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
54 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
55 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
56 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
57 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
58 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
59 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
60 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
61 [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
62 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
63 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
64};
65
75093152 66static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
57c2590f
TH
67 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
68 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
69};
70
71static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
72 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
73 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
74 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
75};
76
77static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
78 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
79 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
80 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
81 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
82 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
83 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
84 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
85 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
86};
87
7762fbff
TH
88void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
89{
90 if (!ptr)
91 return;
92 if (ptr->is_group)
a98aa4de 93 tomoyo_put_group(ptr->group);
7762fbff
TH
94 else
95 tomoyo_put_name(ptr->filename);
96}
97
98bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
99 const struct tomoyo_name_union *ptr)
100{
101 if (ptr->is_group)
3f629636 102 return tomoyo_path_matches_group(name, ptr->group);
7762fbff
TH
103 return tomoyo_path_matches_pattern(name, ptr->filename);
104}
105
4c3e9e2d
TH
106void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
107{
108 if (ptr && ptr->is_group)
a98aa4de 109 tomoyo_put_group(ptr->group);
4c3e9e2d
TH
110}
111
112bool tomoyo_compare_number_union(const unsigned long value,
113 const struct tomoyo_number_union *ptr)
114{
115 if (ptr->is_group)
116 return tomoyo_number_matches_group(value, value, ptr->group);
117 return value >= ptr->values[0] && value <= ptr->values[1];
118}
119
c8c57e84
TH
120static void tomoyo_add_slash(struct tomoyo_path_info *buf)
121{
122 if (buf->is_dir)
123 return;
124 /*
125 * This is OK because tomoyo_encode() reserves space for appending "/".
126 */
127 strcat((char *) buf->name, "/");
128 tomoyo_fill_path_info(buf);
129}
130
b69a54ee
KT
131/**
132 * tomoyo_strendswith - Check whether the token ends with the given token.
133 *
134 * @name: The token to check.
135 * @tail: The token to find.
136 *
137 * Returns true if @name ends with @tail, false otherwise.
138 */
139static bool tomoyo_strendswith(const char *name, const char *tail)
140{
141 int len;
142
143 if (!name || !tail)
144 return false;
145 len = strlen(name) - strlen(tail);
146 return len >= 0 && !strcmp(name + len, tail);
147}
148
149/**
c8c57e84 150 * tomoyo_get_realpath - Get realpath.
b69a54ee 151 *
c8c57e84 152 * @buf: Pointer to "struct tomoyo_path_info".
b69a54ee
KT
153 * @path: Pointer to "struct path".
154 *
c8c57e84 155 * Returns true on success, false otherwise.
b69a54ee 156 */
c8c57e84 157static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
b69a54ee 158{
c8c57e84
TH
159 buf->name = tomoyo_realpath_from_path(path);
160 if (buf->name) {
161 tomoyo_fill_path_info(buf);
162 return true;
b69a54ee 163 }
c8c57e84 164 return false;
b69a54ee
KT
165}
166
99a85259
TH
167/**
168 * tomoyo_audit_path_log - Audit path request log.
169 *
170 * @r: Pointer to "struct tomoyo_request_info".
171 *
172 * Returns 0 on success, negative value otherwise.
173 */
174static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
175{
176 const char *operation = tomoyo_path_keyword[r->param.path.operation];
177 const struct tomoyo_path_info *filename = r->param.path.filename;
178 if (r->granted)
179 return 0;
180 tomoyo_warn_log(r, "%s %s", operation, filename->name);
181 return tomoyo_supervisor(r, "allow_%s %s\n", operation,
e2bf6907 182 tomoyo_pattern(filename));
99a85259
TH
183}
184
185/**
186 * tomoyo_audit_path2_log - Audit path/path request log.
187 *
188 * @r: Pointer to "struct tomoyo_request_info".
189 *
190 * Returns 0 on success, negative value otherwise.
191 */
192static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
193{
194 const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
195 const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
196 const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
197 if (r->granted)
198 return 0;
199 tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
200 filename2->name);
201 return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
e2bf6907
TH
202 tomoyo_pattern(filename1),
203 tomoyo_pattern(filename2));
99a85259
TH
204}
205
206/**
207 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
208 *
209 * @r: Pointer to "struct tomoyo_request_info".
210 *
211 * Returns 0 on success, negative value otherwise.
212 */
213static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
214{
71c28236 215 const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
99a85259
TH
216 const struct tomoyo_path_info *filename = r->param.mkdev.filename;
217 const unsigned int major = r->param.mkdev.major;
218 const unsigned int minor = r->param.mkdev.minor;
219 const unsigned int mode = r->param.mkdev.mode;
220 if (r->granted)
221 return 0;
222 tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
223 major, minor);
224 return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation,
e2bf6907 225 tomoyo_pattern(filename), mode, major, minor);
99a85259
TH
226}
227
228/**
229 * tomoyo_audit_path_number_log - Audit path/number request log.
230 *
231 * @r: Pointer to "struct tomoyo_request_info".
232 * @error: Error code.
233 *
234 * Returns 0 on success, negative value otherwise.
235 */
236static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
237{
238 const u8 type = r->param.path_number.operation;
239 u8 radix;
240 const struct tomoyo_path_info *filename = r->param.path_number.filename;
241 const char *operation = tomoyo_path_number_keyword[type];
242 char buffer[64];
243 if (r->granted)
244 return 0;
245 switch (type) {
246 case TOMOYO_TYPE_CREATE:
247 case TOMOYO_TYPE_MKDIR:
248 case TOMOYO_TYPE_MKFIFO:
249 case TOMOYO_TYPE_MKSOCK:
250 case TOMOYO_TYPE_CHMOD:
251 radix = TOMOYO_VALUE_TYPE_OCTAL;
252 break;
253 case TOMOYO_TYPE_IOCTL:
254 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
255 break;
256 default:
257 radix = TOMOYO_VALUE_TYPE_DECIMAL;
258 break;
259 }
260 tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
261 radix);
262 tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
263 return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
e2bf6907 264 tomoyo_pattern(filename), buffer);
99a85259
TH
265}
266
36f5e1ff
TH
267static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a,
268 const struct tomoyo_acl_head *b)
269{
e2bf6907 270 return container_of(a, struct tomoyo_readable_file,
36f5e1ff 271 head)->filename ==
e2bf6907 272 container_of(b, struct tomoyo_readable_file,
36f5e1ff
TH
273 head)->filename;
274}
275
b69a54ee 276/**
e2bf6907 277 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_readable_file" list.
b69a54ee
KT
278 *
279 * @filename: Filename unconditionally permitted to open() for reading.
280 * @is_delete: True if it is a delete request.
281 *
282 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
283 *
284 * Caller holds tomoyo_read_lock().
b69a54ee
KT
285 */
286static int tomoyo_update_globally_readable_entry(const char *filename,
287 const bool is_delete)
288{
e2bf6907 289 struct tomoyo_readable_file e = { };
36f5e1ff 290 int error;
b69a54ee 291
75093152 292 if (!tomoyo_correct_word(filename))
b69a54ee 293 return -EINVAL;
9e4b50e9
TH
294 e.filename = tomoyo_get_name(filename);
295 if (!e.filename)
b69a54ee 296 return -ENOMEM;
36f5e1ff 297 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7
TH
298 &tomoyo_policy_list
299 [TOMOYO_ID_GLOBALLY_READABLE],
36f5e1ff 300 tomoyo_same_globally_readable);
9e4b50e9 301 tomoyo_put_name(e.filename);
b69a54ee
KT
302 return error;
303}
304
305/**
75093152 306 * tomoyo_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
b69a54ee
KT
307 *
308 * @filename: The filename to check.
309 *
310 * Returns true if any domain can open @filename for reading, false otherwise.
fdb8ebb7
TH
311 *
312 * Caller holds tomoyo_read_lock().
b69a54ee 313 */
75093152 314static bool tomoyo_globally_readable_file(const struct tomoyo_path_info *
b69a54ee
KT
315 filename)
316{
e2bf6907 317 struct tomoyo_readable_file *ptr;
b69a54ee 318 bool found = false;
fdb8ebb7 319
a230f9e7
TH
320 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
321 [TOMOYO_ID_GLOBALLY_READABLE], head.list) {
82e0f001 322 if (!ptr->head.is_deleted &&
b69a54ee
KT
323 tomoyo_path_matches_pattern(filename, ptr->filename)) {
324 found = true;
325 break;
326 }
327 }
b69a54ee
KT
328 return found;
329}
330
331/**
e2bf6907 332 * tomoyo_write_globally_readable - Write "struct tomoyo_readable_file" list.
b69a54ee
KT
333 *
334 * @data: String to parse.
335 * @is_delete: True if it is a delete request.
336 *
337 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
338 *
339 * Caller holds tomoyo_read_lock().
b69a54ee 340 */
e2bf6907 341int tomoyo_write_globally_readable(char *data, const bool is_delete)
b69a54ee
KT
342{
343 return tomoyo_update_globally_readable_entry(data, is_delete);
344}
345
36f5e1ff
TH
346static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a,
347 const struct tomoyo_acl_head *b)
348{
e2bf6907
TH
349 return container_of(a, struct tomoyo_no_pattern, head)->pattern ==
350 container_of(b, struct tomoyo_no_pattern, head)->pattern;
36f5e1ff
TH
351}
352
b69a54ee 353/**
e2bf6907 354 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_no_pattern" list.
b69a54ee
KT
355 *
356 * @pattern: Pathname pattern.
357 * @is_delete: True if it is a delete request.
358 *
359 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
360 *
361 * Caller holds tomoyo_read_lock().
b69a54ee
KT
362 */
363static int tomoyo_update_file_pattern_entry(const char *pattern,
364 const bool is_delete)
365{
e2bf6907 366 struct tomoyo_no_pattern e = { };
36f5e1ff 367 int error;
b69a54ee 368
75093152 369 if (!tomoyo_correct_word(pattern))
3f629636
TH
370 return -EINVAL;
371 e.pattern = tomoyo_get_name(pattern);
9e4b50e9 372 if (!e.pattern)
36f5e1ff
TH
373 return -ENOMEM;
374 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7 375 &tomoyo_policy_list[TOMOYO_ID_PATTERN],
36f5e1ff 376 tomoyo_same_pattern);
9e4b50e9 377 tomoyo_put_name(e.pattern);
b69a54ee
KT
378 return error;
379}
380
381/**
e2bf6907 382 * tomoyo_pattern - Get patterned pathname.
b69a54ee
KT
383 *
384 * @filename: The filename to find patterned pathname.
385 *
386 * Returns pointer to pathname pattern if matched, @filename otherwise.
fdb8ebb7
TH
387 *
388 * Caller holds tomoyo_read_lock().
b69a54ee 389 */
e2bf6907 390const char *tomoyo_pattern(const struct tomoyo_path_info *filename)
b69a54ee 391{
e2bf6907 392 struct tomoyo_no_pattern *ptr;
b69a54ee
KT
393 const struct tomoyo_path_info *pattern = NULL;
394
a230f9e7
TH
395 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_PATTERN],
396 head.list) {
82e0f001 397 if (ptr->head.is_deleted)
b69a54ee
KT
398 continue;
399 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
400 continue;
401 pattern = ptr->pattern;
402 if (tomoyo_strendswith(pattern->name, "/\\*")) {
403 /* Do nothing. Try to find the better match. */
404 } else {
405 /* This would be the better match. Use this. */
406 break;
407 }
408 }
b69a54ee
KT
409 if (pattern)
410 filename = pattern;
17fcfbd9 411 return filename->name;
b69a54ee
KT
412}
413
414/**
e2bf6907 415 * tomoyo_write_pattern - Write "struct tomoyo_no_pattern" list.
b69a54ee
KT
416 *
417 * @data: String to parse.
418 * @is_delete: True if it is a delete request.
419 *
420 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
421 *
422 * Caller holds tomoyo_read_lock().
b69a54ee 423 */
e2bf6907 424int tomoyo_write_pattern(char *data, const bool is_delete)
b69a54ee
KT
425{
426 return tomoyo_update_file_pattern_entry(data, is_delete);
427}
428
36f5e1ff
TH
429static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a,
430 const struct tomoyo_acl_head *b)
431{
e2bf6907
TH
432 return container_of(a, struct tomoyo_no_rewrite, head)->pattern
433 == container_of(b, struct tomoyo_no_rewrite, head)
36f5e1ff
TH
434 ->pattern;
435}
436
b69a54ee 437/**
e2bf6907 438 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite" list.
b69a54ee
KT
439 *
440 * @pattern: Pathname pattern that are not rewritable by default.
441 * @is_delete: True if it is a delete request.
442 *
443 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
444 *
445 * Caller holds tomoyo_read_lock().
b69a54ee
KT
446 */
447static int tomoyo_update_no_rewrite_entry(const char *pattern,
448 const bool is_delete)
449{
e2bf6907 450 struct tomoyo_no_rewrite e = { };
36f5e1ff 451 int error;
b69a54ee 452
75093152 453 if (!tomoyo_correct_word(pattern))
b69a54ee 454 return -EINVAL;
9e4b50e9
TH
455 e.pattern = tomoyo_get_name(pattern);
456 if (!e.pattern)
36f5e1ff
TH
457 return -ENOMEM;
458 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7 459 &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE],
36f5e1ff 460 tomoyo_same_no_rewrite);
9e4b50e9 461 tomoyo_put_name(e.pattern);
b69a54ee
KT
462 return error;
463}
464
465/**
75093152 466 * tomoyo_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
b69a54ee
KT
467 *
468 * @filename: Filename to check.
469 *
470 * Returns true if @filename is specified by "deny_rewrite" directive,
471 * false otherwise.
fdb8ebb7
TH
472 *
473 * Caller holds tomoyo_read_lock().
b69a54ee 474 */
75093152 475static bool tomoyo_no_rewrite_file(const struct tomoyo_path_info *filename)
b69a54ee 476{
e2bf6907 477 struct tomoyo_no_rewrite *ptr;
b69a54ee
KT
478 bool found = false;
479
a230f9e7
TH
480 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE],
481 head.list) {
82e0f001 482 if (ptr->head.is_deleted)
b69a54ee
KT
483 continue;
484 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
485 continue;
486 found = true;
487 break;
488 }
b69a54ee
KT
489 return found;
490}
491
492/**
e2bf6907 493 * tomoyo_write_no_rewrite - Write "struct tomoyo_no_rewrite" list.
b69a54ee
KT
494 *
495 * @data: String to parse.
496 * @is_delete: True if it is a delete request.
497 *
498 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
499 *
500 * Caller holds tomoyo_read_lock().
b69a54ee 501 */
e2bf6907 502int tomoyo_write_no_rewrite(char *data, const bool is_delete)
b69a54ee
KT
503{
504 return tomoyo_update_no_rewrite_entry(data, is_delete);
505}
506
99a85259
TH
507static bool tomoyo_check_path_acl(const struct tomoyo_request_info *r,
508 const struct tomoyo_acl_info *ptr)
b69a54ee 509{
99a85259
TH
510 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
511 head);
512 return (acl->perm & (1 << r->param.path.operation)) &&
513 tomoyo_compare_name_union(r->param.path.filename, &acl->name);
514}
b69a54ee 515
99a85259
TH
516static bool tomoyo_check_path_number_acl(const struct tomoyo_request_info *r,
517 const struct tomoyo_acl_info *ptr)
518{
519 const struct tomoyo_path_number_acl *acl =
520 container_of(ptr, typeof(*acl), head);
521 return (acl->perm & (1 << r->param.path_number.operation)) &&
522 tomoyo_compare_number_union(r->param.path_number.number,
523 &acl->number) &&
524 tomoyo_compare_name_union(r->param.path_number.filename,
525 &acl->name);
526}
527
528static bool tomoyo_check_path2_acl(const struct tomoyo_request_info *r,
529 const struct tomoyo_acl_info *ptr)
530{
531 const struct tomoyo_path2_acl *acl =
532 container_of(ptr, typeof(*acl), head);
533 return (acl->perm & (1 << r->param.path2.operation)) &&
534 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
535 && tomoyo_compare_name_union(r->param.path2.filename2,
536 &acl->name2);
537}
538
539static bool tomoyo_check_mkdev_acl(const struct tomoyo_request_info *r,
540 const struct tomoyo_acl_info *ptr)
541{
75093152 542 const struct tomoyo_mkdev_acl *acl =
99a85259
TH
543 container_of(ptr, typeof(*acl), head);
544 return (acl->perm & (1 << r->param.mkdev.operation)) &&
545 tomoyo_compare_number_union(r->param.mkdev.mode,
546 &acl->mode) &&
547 tomoyo_compare_number_union(r->param.mkdev.major,
548 &acl->major) &&
549 tomoyo_compare_number_union(r->param.mkdev.minor,
550 &acl->minor) &&
551 tomoyo_compare_name_union(r->param.mkdev.filename,
552 &acl->name);
b69a54ee
KT
553}
554
237ab459
TH
555static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
556 const struct tomoyo_acl_info *b)
557{
558 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
559 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
75093152
TH
560 return tomoyo_same_acl_head(&p1->head, &p2->head) &&
561 tomoyo_same_name_union(&p1->name, &p2->name);
237ab459
TH
562}
563
564static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
565 struct tomoyo_acl_info *b,
566 const bool is_delete)
567{
568 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
569 ->perm;
570 u16 perm = *a_perm;
571 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
572 if (is_delete) {
573 perm &= ~b_perm;
574 if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
575 perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
576 else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
577 perm &= ~TOMOYO_RW_MASK;
578 } else {
579 perm |= b_perm;
580 if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
581 perm |= (1 << TOMOYO_TYPE_READ_WRITE);
582 else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
583 perm |= TOMOYO_RW_MASK;
584 }
585 *a_perm = perm;
586 return !perm;
587}
588
b69a54ee 589/**
7ef61233 590 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
b69a54ee
KT
591 *
592 * @type: Type of operation.
593 * @filename: Filename.
594 * @domain: Pointer to "struct tomoyo_domain_info".
595 * @is_delete: True if it is a delete request.
596 *
597 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
598 *
599 * Caller holds tomoyo_read_lock().
b69a54ee 600 */
7ef61233 601static int tomoyo_update_path_acl(const u8 type, const char *filename,
237ab459 602 struct tomoyo_domain_info * const domain,
7ef61233 603 const bool is_delete)
b69a54ee 604{
9e4b50e9
TH
605 struct tomoyo_path_acl e = {
606 .head.type = TOMOYO_TYPE_PATH_ACL,
237ab459 607 .perm = 1 << type
9e4b50e9 608 };
237ab459
TH
609 int error;
610 if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
611 e.perm |= TOMOYO_RW_MASK;
7762fbff 612 if (!tomoyo_parse_name_union(filename, &e.name))
b69a54ee 613 return -EINVAL;
237ab459
TH
614 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
615 tomoyo_same_path_acl,
616 tomoyo_merge_path_acl);
7762fbff 617 tomoyo_put_name_union(&e.name);
b69a54ee
KT
618 return error;
619}
620
75093152 621static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
237ab459
TH
622 const struct tomoyo_acl_info *b)
623{
75093152 624 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1),
237ab459 625 head);
75093152 626 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2),
237ab459 627 head);
75093152
TH
628 return tomoyo_same_acl_head(&p1->head, &p2->head)
629 && tomoyo_same_name_union(&p1->name, &p2->name)
630 && tomoyo_same_number_union(&p1->mode, &p2->mode)
631 && tomoyo_same_number_union(&p1->major, &p2->major)
632 && tomoyo_same_number_union(&p1->minor, &p2->minor);
237ab459
TH
633}
634
75093152 635static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
237ab459
TH
636 struct tomoyo_acl_info *b,
637 const bool is_delete)
638{
75093152 639 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
237ab459
TH
640 head)->perm;
641 u8 perm = *a_perm;
75093152 642 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
237ab459
TH
643 ->perm;
644 if (is_delete)
645 perm &= ~b_perm;
646 else
647 perm |= b_perm;
648 *a_perm = perm;
649 return !perm;
650}
651
a1f9bb6a 652/**
75093152 653 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
a1f9bb6a
TH
654 *
655 * @type: Type of operation.
656 * @filename: Filename.
657 * @mode: Create mode.
658 * @major: Device major number.
659 * @minor: Device minor number.
660 * @domain: Pointer to "struct tomoyo_domain_info".
661 * @is_delete: True if it is a delete request.
662 *
663 * Returns 0 on success, negative value otherwise.
237ab459
TH
664 *
665 * Caller holds tomoyo_read_lock().
a1f9bb6a 666 */
75093152 667static int tomoyo_update_mkdev_acl(const u8 type, const char *filename,
237ab459
TH
668 char *mode, char *major, char *minor,
669 struct tomoyo_domain_info * const
670 domain, const bool is_delete)
a1f9bb6a 671{
75093152
TH
672 struct tomoyo_mkdev_acl e = {
673 .head.type = TOMOYO_TYPE_MKDEV_ACL,
237ab459 674 .perm = 1 << type
a1f9bb6a
TH
675 };
676 int error = is_delete ? -ENOENT : -ENOMEM;
677 if (!tomoyo_parse_name_union(filename, &e.name) ||
678 !tomoyo_parse_number_union(mode, &e.mode) ||
679 !tomoyo_parse_number_union(major, &e.major) ||
680 !tomoyo_parse_number_union(minor, &e.minor))
681 goto out;
237ab459 682 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
75093152
TH
683 tomoyo_same_mkdev_acl,
684 tomoyo_merge_mkdev_acl);
a1f9bb6a
TH
685 out:
686 tomoyo_put_name_union(&e.name);
687 tomoyo_put_number_union(&e.mode);
688 tomoyo_put_number_union(&e.major);
689 tomoyo_put_number_union(&e.minor);
690 return error;
691}
692
237ab459
TH
693static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
694 const struct tomoyo_acl_info *b)
695{
696 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
697 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
75093152
TH
698 return tomoyo_same_acl_head(&p1->head, &p2->head)
699 && tomoyo_same_name_union(&p1->name1, &p2->name1)
700 && tomoyo_same_name_union(&p1->name2, &p2->name2);
237ab459
TH
701}
702
703static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
704 struct tomoyo_acl_info *b,
705 const bool is_delete)
706{
707 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
708 ->perm;
709 u8 perm = *a_perm;
710 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
711 if (is_delete)
712 perm &= ~b_perm;
713 else
714 perm |= b_perm;
715 *a_perm = perm;
716 return !perm;
717}
718
b69a54ee 719/**
7ef61233 720 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
b69a54ee
KT
721 *
722 * @type: Type of operation.
723 * @filename1: First filename.
724 * @filename2: Second filename.
725 * @domain: Pointer to "struct tomoyo_domain_info".
726 * @is_delete: True if it is a delete request.
727 *
728 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
729 *
730 * Caller holds tomoyo_read_lock().
b69a54ee 731 */
7ef61233
TH
732static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
733 const char *filename2,
237ab459 734 struct tomoyo_domain_info * const domain,
7ef61233 735 const bool is_delete)
b69a54ee 736{
9e4b50e9
TH
737 struct tomoyo_path2_acl e = {
738 .head.type = TOMOYO_TYPE_PATH2_ACL,
237ab459 739 .perm = 1 << type
9e4b50e9 740 };
ca0b7df3 741 int error = is_delete ? -ENOENT : -ENOMEM;
7762fbff
TH
742 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
743 !tomoyo_parse_name_union(filename2, &e.name2))
ca0b7df3 744 goto out;
237ab459
TH
745 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
746 tomoyo_same_path2_acl,
747 tomoyo_merge_path2_acl);
ca0b7df3 748 out:
7762fbff
TH
749 tomoyo_put_name_union(&e.name1);
750 tomoyo_put_name_union(&e.name2);
b69a54ee
KT
751 return error;
752}
753
b69a54ee 754/**
cb0abe6a 755 * tomoyo_path_permission - Check permission for single path operation.
b69a54ee 756 *
cb0abe6a 757 * @r: Pointer to "struct tomoyo_request_info".
b69a54ee
KT
758 * @operation: Type of operation.
759 * @filename: Filename to check.
b69a54ee
KT
760 *
761 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
762 *
763 * Caller holds tomoyo_read_lock().
b69a54ee 764 */
05336dee
TH
765int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
766 const struct tomoyo_path_info *filename)
b69a54ee 767{
b69a54ee 768 int error;
b69a54ee 769
b69a54ee 770 next:
57c2590f
TH
771 r->type = tomoyo_p2mac[operation];
772 r->mode = tomoyo_get_mode(r->profile, r->type);
773 if (r->mode == TOMOYO_CONFIG_DISABLED)
774 return 0;
cf6e9a64
TH
775 r->param_type = TOMOYO_TYPE_PATH_ACL;
776 r->param.path.filename = filename;
777 r->param.path.operation = operation;
17fcfbd9 778 do {
99a85259
TH
779 tomoyo_check_acl(r, tomoyo_check_path_acl);
780 if (!r->granted && operation == TOMOYO_TYPE_READ &&
05336dee 781 !r->domain->ignore_global_allow_read &&
75093152 782 tomoyo_globally_readable_file(filename))
99a85259
TH
783 r->granted = true;
784 error = tomoyo_audit_path_log(r);
05336dee
TH
785 /*
786 * Do not retry for execute request, for alias may have
787 * changed.
788 */
789 } while (error == TOMOYO_RETRY_REQUEST &&
790 operation != TOMOYO_TYPE_EXECUTE);
b69a54ee
KT
791 /*
792 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
793 * we need to check "allow_rewrite" permission if the filename is
794 * specified by "deny_rewrite" keyword.
795 */
7ef61233 796 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
75093152 797 tomoyo_no_rewrite_file(filename)) {
7ef61233 798 operation = TOMOYO_TYPE_REWRITE;
b69a54ee
KT
799 goto next;
800 }
801 return error;
802}
803
237ab459
TH
804static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
805 const struct tomoyo_acl_info *b)
806{
807 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
808 head);
809 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
810 head);
75093152
TH
811 return tomoyo_same_acl_head(&p1->head, &p2->head)
812 && tomoyo_same_name_union(&p1->name, &p2->name)
813 && tomoyo_same_number_union(&p1->number, &p2->number);
237ab459
TH
814}
815
816static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
817 struct tomoyo_acl_info *b,
818 const bool is_delete)
819{
820 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
821 head)->perm;
822 u8 perm = *a_perm;
823 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
824 ->perm;
825 if (is_delete)
826 perm &= ~b_perm;
827 else
828 perm |= b_perm;
829 *a_perm = perm;
830 return !perm;
831}
832
a1f9bb6a
TH
833/**
834 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
835 *
836 * @type: Type of operation.
837 * @filename: Filename.
838 * @number: Number.
839 * @domain: Pointer to "struct tomoyo_domain_info".
840 * @is_delete: True if it is a delete request.
841 *
842 * Returns 0 on success, negative value otherwise.
843 */
237ab459
TH
844static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
845 char *number,
846 struct tomoyo_domain_info * const
847 domain,
848 const bool is_delete)
a1f9bb6a 849{
a1f9bb6a
TH
850 struct tomoyo_path_number_acl e = {
851 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
237ab459 852 .perm = 1 << type
a1f9bb6a
TH
853 };
854 int error = is_delete ? -ENOENT : -ENOMEM;
a1f9bb6a
TH
855 if (!tomoyo_parse_name_union(filename, &e.name))
856 return -EINVAL;
857 if (!tomoyo_parse_number_union(number, &e.number))
858 goto out;
237ab459
TH
859 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
860 tomoyo_same_path_number_acl,
861 tomoyo_merge_path_number_acl);
a1f9bb6a
TH
862 out:
863 tomoyo_put_name_union(&e.name);
864 tomoyo_put_number_union(&e.number);
865 return error;
866}
867
a1f9bb6a
TH
868/**
869 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
870 *
871 * @type: Type of operation.
872 * @path: Pointer to "struct path".
873 * @number: Number.
874 *
875 * Returns 0 on success, negative value otherwise.
876 */
877int tomoyo_path_number_perm(const u8 type, struct path *path,
878 unsigned long number)
879{
880 struct tomoyo_request_info r;
881 int error = -ENOMEM;
c8c57e84 882 struct tomoyo_path_info buf;
a1f9bb6a
TH
883 int idx;
884
57c2590f
TH
885 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
886 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
a1f9bb6a
TH
887 return 0;
888 idx = tomoyo_read_lock();
c8c57e84 889 if (!tomoyo_get_realpath(&buf, path))
a1f9bb6a 890 goto out;
c8c57e84
TH
891 if (type == TOMOYO_TYPE_MKDIR)
892 tomoyo_add_slash(&buf);
cb917cf5
TH
893 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
894 r.param.path_number.operation = type;
895 r.param.path_number.filename = &buf;
896 r.param.path_number.number = number;
897 do {
898 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
899 error = tomoyo_audit_path_number_log(&r);
900 } while (error == TOMOYO_RETRY_REQUEST);
c8c57e84 901 kfree(buf.name);
cb917cf5 902 out:
a1f9bb6a
TH
903 tomoyo_read_unlock(idx);
904 if (r.mode != TOMOYO_CONFIG_ENFORCING)
905 error = 0;
906 return error;
907}
908
b69a54ee
KT
909/**
910 * tomoyo_check_open_permission - Check permission for "read" and "write".
911 *
912 * @domain: Pointer to "struct tomoyo_domain_info".
913 * @path: Pointer to "struct path".
914 * @flag: Flags for open().
915 *
916 * Returns 0 on success, negative value otherwise.
917 */
918int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
919 struct path *path, const int flag)
920{
921 const u8 acc_mode = ACC_MODE(flag);
922 int error = -ENOMEM;
c8c57e84 923 struct tomoyo_path_info buf;
cb0abe6a 924 struct tomoyo_request_info r;
fdb8ebb7 925 int idx;
b69a54ee 926
57c2590f
TH
927 if (!path->mnt ||
928 (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
b69a54ee 929 return 0;
57c2590f
TH
930 buf.name = NULL;
931 r.mode = TOMOYO_CONFIG_DISABLED;
fdb8ebb7 932 idx = tomoyo_read_lock();
c8c57e84 933 if (!tomoyo_get_realpath(&buf, path))
b69a54ee
KT
934 goto out;
935 error = 0;
936 /*
937 * If the filename is specified by "deny_rewrite" keyword,
938 * we need to check "allow_rewrite" permission when the filename is not
939 * opened for append mode or the filename is truncated at open time.
940 */
57c2590f
TH
941 if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
942 && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
943 != TOMOYO_CONFIG_DISABLED) {
944 if (!tomoyo_get_realpath(&buf, path)) {
945 error = -ENOMEM;
946 goto out;
947 }
75093152 948 if (tomoyo_no_rewrite_file(&buf))
57c2590f
TH
949 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
950 &buf);
b69a54ee 951 }
57c2590f
TH
952 if (!error && acc_mode &&
953 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
954 != TOMOYO_CONFIG_DISABLED) {
05336dee 955 u8 operation;
57c2590f
TH
956 if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
957 error = -ENOMEM;
958 goto out;
959 }
05336dee
TH
960 if (acc_mode == (MAY_READ | MAY_WRITE))
961 operation = TOMOYO_TYPE_READ_WRITE;
962 else if (acc_mode == MAY_READ)
963 operation = TOMOYO_TYPE_READ;
964 else
965 operation = TOMOYO_TYPE_WRITE;
966 error = tomoyo_path_permission(&r, operation, &buf);
57c2590f 967 }
b69a54ee 968 out:
c8c57e84 969 kfree(buf.name);
fdb8ebb7 970 tomoyo_read_unlock(idx);
cb0abe6a 971 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
972 error = 0;
973 return error;
974}
975
976/**
2106ccd9 977 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
b69a54ee 978 *
b69a54ee
KT
979 * @operation: Type of operation.
980 * @path: Pointer to "struct path".
981 *
982 * Returns 0 on success, negative value otherwise.
983 */
97d6931e 984int tomoyo_path_perm(const u8 operation, struct path *path)
b69a54ee
KT
985{
986 int error = -ENOMEM;
c8c57e84 987 struct tomoyo_path_info buf;
cb0abe6a 988 struct tomoyo_request_info r;
fdb8ebb7 989 int idx;
b69a54ee 990
57c2590f
TH
991 if (!path->mnt)
992 return 0;
993 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
994 == TOMOYO_CONFIG_DISABLED)
b69a54ee 995 return 0;
57c2590f 996 buf.name = NULL;
fdb8ebb7 997 idx = tomoyo_read_lock();
c8c57e84 998 if (!tomoyo_get_realpath(&buf, path))
b69a54ee
KT
999 goto out;
1000 switch (operation) {
cb0abe6a 1001 case TOMOYO_TYPE_REWRITE:
75093152 1002 if (!tomoyo_no_rewrite_file(&buf)) {
cb0abe6a
TH
1003 error = 0;
1004 goto out;
1005 }
1006 break;
7ef61233
TH
1007 case TOMOYO_TYPE_RMDIR:
1008 case TOMOYO_TYPE_CHROOT:
57c2590f 1009 case TOMOYO_TYPE_UMOUNT:
c8c57e84
TH
1010 tomoyo_add_slash(&buf);
1011 break;
b69a54ee 1012 }
c8c57e84 1013 error = tomoyo_path_permission(&r, operation, &buf);
b69a54ee 1014 out:
c8c57e84 1015 kfree(buf.name);
fdb8ebb7 1016 tomoyo_read_unlock(idx);
cb0abe6a 1017 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
1018 error = 0;
1019 return error;
1020}
1021
a1f9bb6a 1022/**
75093152 1023 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
a1f9bb6a
TH
1024 *
1025 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1026 * @path: Pointer to "struct path".
1027 * @mode: Create mode.
1028 * @dev: Device number.
1029 *
1030 * Returns 0 on success, negative value otherwise.
1031 */
75093152 1032int tomoyo_mkdev_perm(const u8 operation, struct path *path,
a1f9bb6a
TH
1033 const unsigned int mode, unsigned int dev)
1034{
1035 struct tomoyo_request_info r;
1036 int error = -ENOMEM;
c8c57e84 1037 struct tomoyo_path_info buf;
a1f9bb6a
TH
1038 int idx;
1039
57c2590f
TH
1040 if (!path->mnt ||
1041 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
1042 == TOMOYO_CONFIG_DISABLED)
a1f9bb6a
TH
1043 return 0;
1044 idx = tomoyo_read_lock();
1045 error = -ENOMEM;
c8c57e84 1046 if (tomoyo_get_realpath(&buf, path)) {
cf6e9a64 1047 dev = new_decode_dev(dev);
75093152 1048 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
cf6e9a64
TH
1049 r.param.mkdev.filename = &buf;
1050 r.param.mkdev.operation = operation;
1051 r.param.mkdev.mode = mode;
1052 r.param.mkdev.major = MAJOR(dev);
1053 r.param.mkdev.minor = MINOR(dev);
99a85259
TH
1054 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
1055 error = tomoyo_audit_mkdev_log(&r);
c8c57e84 1056 kfree(buf.name);
a1f9bb6a
TH
1057 }
1058 tomoyo_read_unlock(idx);
1059 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1060 error = 0;
1061 return error;
1062}
1063
b69a54ee 1064/**
7ef61233 1065 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
b69a54ee 1066 *
b69a54ee
KT
1067 * @operation: Type of operation.
1068 * @path1: Pointer to "struct path".
1069 * @path2: Pointer to "struct path".
1070 *
1071 * Returns 0 on success, negative value otherwise.
1072 */
97d6931e 1073int tomoyo_path2_perm(const u8 operation, struct path *path1,
7ef61233 1074 struct path *path2)
b69a54ee
KT
1075{
1076 int error = -ENOMEM;
c8c57e84
TH
1077 struct tomoyo_path_info buf1;
1078 struct tomoyo_path_info buf2;
cb0abe6a 1079 struct tomoyo_request_info r;
fdb8ebb7 1080 int idx;
b69a54ee 1081
57c2590f
TH
1082 if (!path1->mnt || !path2->mnt ||
1083 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
1084 == TOMOYO_CONFIG_DISABLED)
b69a54ee 1085 return 0;
c8c57e84
TH
1086 buf1.name = NULL;
1087 buf2.name = NULL;
fdb8ebb7 1088 idx = tomoyo_read_lock();
c8c57e84
TH
1089 if (!tomoyo_get_realpath(&buf1, path1) ||
1090 !tomoyo_get_realpath(&buf2, path2))
b69a54ee 1091 goto out;
57c2590f
TH
1092 switch (operation) {
1093 struct dentry *dentry;
1094 case TOMOYO_TYPE_RENAME:
1095 case TOMOYO_TYPE_LINK:
1096 dentry = path1->dentry;
1097 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
1098 break;
1099 /* fall through */
1100 case TOMOYO_TYPE_PIVOT_ROOT:
1101 tomoyo_add_slash(&buf1);
1102 tomoyo_add_slash(&buf2);
1103 break;
1104 }
cf6e9a64
TH
1105 r.param_type = TOMOYO_TYPE_PATH2_ACL;
1106 r.param.path2.operation = operation;
1107 r.param.path2.filename1 = &buf1;
1108 r.param.path2.filename2 = &buf2;
17fcfbd9 1109 do {
99a85259
TH
1110 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
1111 error = tomoyo_audit_path2_log(&r);
1112 } while (error == TOMOYO_RETRY_REQUEST);
b69a54ee 1113 out:
c8c57e84
TH
1114 kfree(buf1.name);
1115 kfree(buf2.name);
fdb8ebb7 1116 tomoyo_read_unlock(idx);
cb0abe6a 1117 if (r.mode != TOMOYO_CONFIG_ENFORCING)
b69a54ee
KT
1118 error = 0;
1119 return error;
1120}
a1f9bb6a
TH
1121
1122/**
e2bf6907 1123 * tomoyo_write_file - Update file related list.
a1f9bb6a
TH
1124 *
1125 * @data: String to parse.
1126 * @domain: Pointer to "struct tomoyo_domain_info".
1127 * @is_delete: True if it is a delete request.
1128 *
1129 * Returns 0 on success, negative value otherwise.
1130 *
1131 * Caller holds tomoyo_read_lock().
1132 */
e2bf6907
TH
1133int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain,
1134 const bool is_delete)
a1f9bb6a
TH
1135{
1136 char *w[5];
1137 u8 type;
1138 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1139 return -EINVAL;
237ab459 1140 if (strncmp(w[0], "allow_", 6))
a1f9bb6a 1141 goto out;
a1f9bb6a
TH
1142 w[0] += 6;
1143 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1144 if (strcmp(w[0], tomoyo_path_keyword[type]))
1145 continue;
1146 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1147 }
1148 if (!w[2][0])
1149 goto out;
1150 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1151 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1152 continue;
1153 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1154 is_delete);
1155 }
1156 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1157 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1158 continue;
1159 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1160 is_delete);
1161 }
1162 if (!w[3][0] || !w[4][0])
1163 goto out;
75093152
TH
1164 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) {
1165 if (strcmp(w[0], tomoyo_mkdev_keyword[type]))
a1f9bb6a 1166 continue;
75093152
TH
1167 return tomoyo_update_mkdev_acl(type, w[1], w[2], w[3],
1168 w[4], domain, is_delete);
a1f9bb6a
TH
1169 }
1170 out:
1171 return -EINVAL;
1172}