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