]> Pileus Git - ~andy/linux/blob - security/tomoyo/file.c
TOMOYO: Rename directives.
[~andy/linux] / security / tomoyo / file.c
1 /*
2  * security/tomoyo/file.c
3  *
4  * Pathname restriction functions.
5  *
6  * Copyright (C) 2005-2010  NTT DATA CORPORATION
7  */
8
9 #include "common.h"
10 #include <linux/slab.h>
11
12 /* Keyword array for operations with one pathname. */
13 const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
14         [TOMOYO_TYPE_EXECUTE]    = "execute",
15         [TOMOYO_TYPE_READ]       = "read",
16         [TOMOYO_TYPE_WRITE]      = "write",
17         [TOMOYO_TYPE_APPEND]     = "append",
18         [TOMOYO_TYPE_UNLINK]     = "unlink",
19         [TOMOYO_TYPE_GETATTR]    = "getattr",
20         [TOMOYO_TYPE_RMDIR]      = "rmdir",
21         [TOMOYO_TYPE_TRUNCATE]   = "truncate",
22         [TOMOYO_TYPE_SYMLINK]    = "symlink",
23         [TOMOYO_TYPE_CHROOT]     = "chroot",
24         [TOMOYO_TYPE_UMOUNT]     = "unmount",
25 };
26
27 /* Keyword array for operations with one pathname and three numbers. */
28 const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
29         [TOMOYO_TYPE_MKBLOCK]    = "mkblock",
30         [TOMOYO_TYPE_MKCHAR]     = "mkchar",
31 };
32
33 /* Keyword array for operations with two pathnames. */
34 const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
35         [TOMOYO_TYPE_LINK]       = "link",
36         [TOMOYO_TYPE_RENAME]     = "rename",
37         [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
38 };
39
40 /* Keyword array for operations with one pathname and one number. */
41 const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
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
52 /*
53  * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
54  */
55 static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
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_APPEND]     = TOMOYO_MAC_FILE_OPEN,
60         [TOMOYO_TYPE_UNLINK]     = TOMOYO_MAC_FILE_UNLINK,
61         [TOMOYO_TYPE_GETATTR]    = TOMOYO_MAC_FILE_GETATTR,
62         [TOMOYO_TYPE_RMDIR]      = TOMOYO_MAC_FILE_RMDIR,
63         [TOMOYO_TYPE_TRUNCATE]   = TOMOYO_MAC_FILE_TRUNCATE,
64         [TOMOYO_TYPE_SYMLINK]    = TOMOYO_MAC_FILE_SYMLINK,
65         [TOMOYO_TYPE_CHROOT]     = TOMOYO_MAC_FILE_CHROOT,
66         [TOMOYO_TYPE_UMOUNT]     = TOMOYO_MAC_FILE_UMOUNT,
67 };
68
69 /*
70  * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
71  */
72 const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
73         [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
74         [TOMOYO_TYPE_MKCHAR]  = TOMOYO_MAC_FILE_MKCHAR,
75 };
76
77 /*
78  * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
79  */
80 const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
81         [TOMOYO_TYPE_LINK]       = TOMOYO_MAC_FILE_LINK,
82         [TOMOYO_TYPE_RENAME]     = TOMOYO_MAC_FILE_RENAME,
83         [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
84 };
85
86 /*
87  * Mapping table from "enum tomoyo_path_number_acl_index" to
88  * "enum tomoyo_mac_index".
89  */
90 const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
91         [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
92         [TOMOYO_TYPE_MKDIR]  = TOMOYO_MAC_FILE_MKDIR,
93         [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
94         [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
95         [TOMOYO_TYPE_IOCTL]  = TOMOYO_MAC_FILE_IOCTL,
96         [TOMOYO_TYPE_CHMOD]  = TOMOYO_MAC_FILE_CHMOD,
97         [TOMOYO_TYPE_CHOWN]  = TOMOYO_MAC_FILE_CHOWN,
98         [TOMOYO_TYPE_CHGRP]  = TOMOYO_MAC_FILE_CHGRP,
99 };
100
101 /**
102  * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
103  *
104  * @ptr: Pointer to "struct tomoyo_name_union".
105  *
106  * Returns nothing.
107  */
108 void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
109 {
110         tomoyo_put_group(ptr->group);
111         tomoyo_put_name(ptr->filename);
112 }
113
114 /**
115  * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
116  *
117  * @name: Pointer to "struct tomoyo_path_info".
118  * @ptr:  Pointer to "struct tomoyo_name_union".
119  *
120  * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
121  */
122 const struct tomoyo_path_info *
123 tomoyo_compare_name_union(const struct tomoyo_path_info *name,
124                           const struct tomoyo_name_union *ptr)
125 {
126         if (ptr->group)
127                 return tomoyo_path_matches_group(name, ptr->group);
128         if (tomoyo_path_matches_pattern(name, ptr->filename))
129                 return ptr->filename;
130         return NULL;
131 }
132
133 /**
134  * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
135  *
136  * @ptr: Pointer to "struct tomoyo_number_union".
137  *
138  * Returns nothing.
139  */
140 void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
141 {
142         tomoyo_put_group(ptr->group);
143 }
144
145 /**
146  * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
147  *
148  * @value: Number to check.
149  * @ptr:   Pointer to "struct tomoyo_number_union".
150  *
151  * Returns true if @value matches @ptr, false otherwise.
152  */
153 bool tomoyo_compare_number_union(const unsigned long value,
154                                  const struct tomoyo_number_union *ptr)
155 {
156         if (ptr->group)
157                 return tomoyo_number_matches_group(value, value, ptr->group);
158         return value >= ptr->values[0] && value <= ptr->values[1];
159 }
160
161 /**
162  * tomoyo_add_slash - Add trailing '/' if needed.
163  *
164  * @buf: Pointer to "struct tomoyo_path_info".
165  *
166  * Returns nothing.
167  *
168  * @buf must be generated by tomoyo_encode() because this function does not
169  * allocate memory for adding '/'.
170  */
171 static void tomoyo_add_slash(struct tomoyo_path_info *buf)
172 {
173         if (buf->is_dir)
174                 return;
175         /*
176          * This is OK because tomoyo_encode() reserves space for appending "/".
177          */
178         strcat((char *) buf->name, "/");
179         tomoyo_fill_path_info(buf);
180 }
181
182 /**
183  * tomoyo_get_realpath - Get realpath.
184  *
185  * @buf:  Pointer to "struct tomoyo_path_info".
186  * @path: Pointer to "struct path".
187  *
188  * Returns true on success, false otherwise.
189  */
190 static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
191 {
192         buf->name = tomoyo_realpath_from_path(path);
193         if (buf->name) {
194                 tomoyo_fill_path_info(buf);
195                 return true;
196         }
197         return false;
198 }
199
200 /**
201  * tomoyo_audit_path_log - Audit path request log.
202  *
203  * @r: Pointer to "struct tomoyo_request_info".
204  *
205  * Returns 0 on success, negative value otherwise.
206  */
207 static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
208 {
209         const char *operation = tomoyo_path_keyword[r->param.path.operation];
210         const struct tomoyo_path_info *filename = r->param.path.filename;
211         if (r->granted)
212                 return 0;
213         tomoyo_warn_log(r, "%s %s", operation, filename->name);
214         return tomoyo_supervisor(r, "file %s %s\n", operation, filename->name);
215 }
216
217 /**
218  * tomoyo_audit_path2_log - Audit path/path request log.
219  *
220  * @r: Pointer to "struct tomoyo_request_info".
221  *
222  * Returns 0 on success, negative value otherwise.
223  */
224 static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
225 {
226         const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
227         const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
228         const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
229         if (r->granted)
230                 return 0;
231         tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
232                         filename2->name);
233         return tomoyo_supervisor(r, "file %s %s %s\n", operation,
234                                  filename1->name, filename2->name);
235 }
236
237 /**
238  * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
239  *
240  * @r: Pointer to "struct tomoyo_request_info".
241  *
242  * Returns 0 on success, negative value otherwise.
243  */
244 static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
245 {
246         const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
247         const struct tomoyo_path_info *filename = r->param.mkdev.filename;
248         const unsigned int major = r->param.mkdev.major;
249         const unsigned int minor = r->param.mkdev.minor;
250         const unsigned int mode = r->param.mkdev.mode;
251         if (r->granted)
252                 return 0;
253         tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
254                         major, minor);
255         return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n", operation,
256                                  filename->name, mode, major, minor);
257 }
258
259 /**
260  * tomoyo_audit_path_number_log - Audit path/number request log.
261  *
262  * @r: Pointer to "struct tomoyo_request_info".
263  *
264  * Returns 0 on success, negative value otherwise.
265  */
266 static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
267 {
268         const u8 type = r->param.path_number.operation;
269         u8 radix;
270         const struct tomoyo_path_info *filename = r->param.path_number.filename;
271         const char *operation = tomoyo_path_number_keyword[type];
272         char buffer[64];
273         if (r->granted)
274                 return 0;
275         switch (type) {
276         case TOMOYO_TYPE_CREATE:
277         case TOMOYO_TYPE_MKDIR:
278         case TOMOYO_TYPE_MKFIFO:
279         case TOMOYO_TYPE_MKSOCK:
280         case TOMOYO_TYPE_CHMOD:
281                 radix = TOMOYO_VALUE_TYPE_OCTAL;
282                 break;
283         case TOMOYO_TYPE_IOCTL:
284                 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
285                 break;
286         default:
287                 radix = TOMOYO_VALUE_TYPE_DECIMAL;
288                 break;
289         }
290         tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
291                            radix);
292         tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
293         return tomoyo_supervisor(r, "file %s %s %s\n", operation,
294                                  filename->name, buffer);
295 }
296
297 /**
298  * tomoyo_check_path_acl - Check permission for path operation.
299  *
300  * @r:   Pointer to "struct tomoyo_request_info".
301  * @ptr: Pointer to "struct tomoyo_acl_info".
302  *
303  * Returns true if granted, false otherwise.
304  *
305  * To be able to use wildcard for domain transition, this function sets
306  * matching entry on success. Since the caller holds tomoyo_read_lock(),
307  * it is safe to set matching entry.
308  */
309 static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
310                                   const struct tomoyo_acl_info *ptr)
311 {
312         const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
313                                                          head);
314         if (acl->perm & (1 << r->param.path.operation)) {
315                 r->param.path.matched_path =
316                         tomoyo_compare_name_union(r->param.path.filename,
317                                                   &acl->name);
318                 return r->param.path.matched_path != NULL;
319         }
320         return false;
321 }
322
323 /**
324  * tomoyo_check_path_number_acl - Check permission for path number operation.
325  *
326  * @r:   Pointer to "struct tomoyo_request_info".
327  * @ptr: Pointer to "struct tomoyo_acl_info".
328  *
329  * Returns true if granted, false otherwise.
330  */
331 static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
332                                          const struct tomoyo_acl_info *ptr)
333 {
334         const struct tomoyo_path_number_acl *acl =
335                 container_of(ptr, typeof(*acl), head);
336         return (acl->perm & (1 << r->param.path_number.operation)) &&
337                 tomoyo_compare_number_union(r->param.path_number.number,
338                                             &acl->number) &&
339                 tomoyo_compare_name_union(r->param.path_number.filename,
340                                           &acl->name);
341 }
342
343 /**
344  * tomoyo_check_path2_acl - Check permission for path path operation.
345  *
346  * @r:   Pointer to "struct tomoyo_request_info".
347  * @ptr: Pointer to "struct tomoyo_acl_info".
348  *
349  * Returns true if granted, false otherwise.
350  */
351 static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
352                                    const struct tomoyo_acl_info *ptr)
353 {
354         const struct tomoyo_path2_acl *acl =
355                 container_of(ptr, typeof(*acl), head);
356         return (acl->perm & (1 << r->param.path2.operation)) &&
357                 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
358                 && tomoyo_compare_name_union(r->param.path2.filename2,
359                                              &acl->name2);
360 }
361
362 /**
363  * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
364  *
365  * @r:   Pointer to "struct tomoyo_request_info".
366  * @ptr: Pointer to "struct tomoyo_acl_info".
367  *
368  * Returns true if granted, false otherwise.
369  */
370 static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
371                                    const struct tomoyo_acl_info *ptr)
372 {
373         const struct tomoyo_mkdev_acl *acl =
374                 container_of(ptr, typeof(*acl), head);
375         return (acl->perm & (1 << r->param.mkdev.operation)) &&
376                 tomoyo_compare_number_union(r->param.mkdev.mode,
377                                             &acl->mode) &&
378                 tomoyo_compare_number_union(r->param.mkdev.major,
379                                             &acl->major) &&
380                 tomoyo_compare_number_union(r->param.mkdev.minor,
381                                             &acl->minor) &&
382                 tomoyo_compare_name_union(r->param.mkdev.filename,
383                                           &acl->name);
384 }
385
386 /**
387  * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
388  *
389  * @a: Pointer to "struct tomoyo_acl_info".
390  * @b: Pointer to "struct tomoyo_acl_info".
391  *
392  * Returns true if @a == @b except permission bits, false otherwise.
393  */
394 static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
395                                  const struct tomoyo_acl_info *b)
396 {
397         const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
398         const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
399         return tomoyo_same_name_union(&p1->name, &p2->name);
400 }
401
402 /**
403  * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
404  *
405  * @a:         Pointer to "struct tomoyo_acl_info".
406  * @b:         Pointer to "struct tomoyo_acl_info".
407  * @is_delete: True for @a &= ~@b, false for @a |= @b.
408  *
409  * Returns true if @a is empty, false otherwise.
410  */
411 static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
412                                   struct tomoyo_acl_info *b,
413                                   const bool is_delete)
414 {
415         u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
416                 ->perm;
417         u16 perm = *a_perm;
418         const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
419         if (is_delete)
420                 perm &= ~b_perm;
421         else
422                 perm |= b_perm;
423         *a_perm = perm;
424         return !perm;
425 }
426
427 /**
428  * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
429  *
430  * @perm:  Permission.
431  * @param: Pointer to "struct tomoyo_acl_param".
432  *
433  * Returns 0 on success, negative value otherwise.
434  *
435  * Caller holds tomoyo_read_lock().
436  */
437 static int tomoyo_update_path_acl(const u16 perm,
438                                   struct tomoyo_acl_param *param)
439 {
440         struct tomoyo_path_acl e = {
441                 .head.type = TOMOYO_TYPE_PATH_ACL,
442                 .perm = perm
443         };
444         int error;
445         if (!tomoyo_parse_name_union(param, &e.name))
446                 error = -EINVAL;
447         else
448                 error = tomoyo_update_domain(&e.head, sizeof(e), param,
449                                              tomoyo_same_path_acl,
450                                              tomoyo_merge_path_acl);
451         tomoyo_put_name_union(&e.name);
452         return error;
453 }
454
455 /**
456  * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
457  *
458  * @a: Pointer to "struct tomoyo_acl_info".
459  * @b: Pointer to "struct tomoyo_acl_info".
460  *
461  * Returns true if @a == @b except permission bits, false otherwise.
462  */
463 static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
464                                          const struct tomoyo_acl_info *b)
465 {
466         const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
467         const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
468         return tomoyo_same_name_union(&p1->name, &p2->name) &&
469                 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
470                 tomoyo_same_number_union(&p1->major, &p2->major) &&
471                 tomoyo_same_number_union(&p1->minor, &p2->minor);
472 }
473
474 /**
475  * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
476  *
477  * @a:         Pointer to "struct tomoyo_acl_info".
478  * @b:         Pointer to "struct tomoyo_acl_info".
479  * @is_delete: True for @a &= ~@b, false for @a |= @b.
480  *
481  * Returns true if @a is empty, false otherwise.
482  */
483 static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
484                                    struct tomoyo_acl_info *b,
485                                    const bool is_delete)
486 {
487         u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
488                                          head)->perm;
489         u8 perm = *a_perm;
490         const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
491                 ->perm;
492         if (is_delete)
493                 perm &= ~b_perm;
494         else
495                 perm |= b_perm;
496         *a_perm = perm;
497         return !perm;
498 }
499
500 /**
501  * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
502  *
503  * @perm:  Permission.
504  * @param: Pointer to "struct tomoyo_acl_param".
505  *
506  * Returns 0 on success, negative value otherwise.
507  *
508  * Caller holds tomoyo_read_lock().
509  */
510 static int tomoyo_update_mkdev_acl(const u8 perm,
511                                    struct tomoyo_acl_param *param)
512 {
513         struct tomoyo_mkdev_acl e = {
514                 .head.type = TOMOYO_TYPE_MKDEV_ACL,
515                 .perm = perm
516         };
517         int error;
518         if (!tomoyo_parse_name_union(param, &e.name) ||
519             !tomoyo_parse_number_union(param, &e.mode) ||
520             !tomoyo_parse_number_union(param, &e.major) ||
521             !tomoyo_parse_number_union(param, &e.minor))
522                 error = -EINVAL;
523         else
524                 error = tomoyo_update_domain(&e.head, sizeof(e), param,
525                                              tomoyo_same_mkdev_acl,
526                                              tomoyo_merge_mkdev_acl);
527         tomoyo_put_name_union(&e.name);
528         tomoyo_put_number_union(&e.mode);
529         tomoyo_put_number_union(&e.major);
530         tomoyo_put_number_union(&e.minor);
531         return error;
532 }
533
534 /**
535  * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
536  *
537  * @a: Pointer to "struct tomoyo_acl_info".
538  * @b: Pointer to "struct tomoyo_acl_info".
539  *
540  * Returns true if @a == @b except permission bits, false otherwise.
541  */
542 static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
543                                   const struct tomoyo_acl_info *b)
544 {
545         const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
546         const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
547         return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
548                 tomoyo_same_name_union(&p1->name2, &p2->name2);
549 }
550
551 /**
552  * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
553  *
554  * @a:         Pointer to "struct tomoyo_acl_info".
555  * @b:         Pointer to "struct tomoyo_acl_info".
556  * @is_delete: True for @a &= ~@b, false for @a |= @b.
557  *
558  * Returns true if @a is empty, false otherwise.
559  */
560 static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
561                                    struct tomoyo_acl_info *b,
562                                    const bool is_delete)
563 {
564         u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
565                 ->perm;
566         u8 perm = *a_perm;
567         const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
568         if (is_delete)
569                 perm &= ~b_perm;
570         else
571                 perm |= b_perm;
572         *a_perm = perm;
573         return !perm;
574 }
575
576 /**
577  * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
578  *
579  * @perm:  Permission.
580  * @param: Pointer to "struct tomoyo_acl_param".
581  *
582  * Returns 0 on success, negative value otherwise.
583  *
584  * Caller holds tomoyo_read_lock().
585  */
586 static int tomoyo_update_path2_acl(const u8 perm,
587                                    struct tomoyo_acl_param *param)
588 {
589         struct tomoyo_path2_acl e = {
590                 .head.type = TOMOYO_TYPE_PATH2_ACL,
591                 .perm = perm
592         };
593         int error;
594         if (!tomoyo_parse_name_union(param, &e.name1) ||
595             !tomoyo_parse_name_union(param, &e.name2))
596                 error = -EINVAL;
597         else
598                 error = tomoyo_update_domain(&e.head, sizeof(e), param,
599                                              tomoyo_same_path2_acl,
600                                              tomoyo_merge_path2_acl);
601         tomoyo_put_name_union(&e.name1);
602         tomoyo_put_name_union(&e.name2);
603         return error;
604 }
605
606 /**
607  * tomoyo_path_permission - Check permission for single path operation.
608  *
609  * @r:         Pointer to "struct tomoyo_request_info".
610  * @operation: Type of operation.
611  * @filename:  Filename to check.
612  *
613  * Returns 0 on success, negative value otherwise.
614  *
615  * Caller holds tomoyo_read_lock().
616  */
617 int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
618                            const struct tomoyo_path_info *filename)
619 {
620         int error;
621
622         r->type = tomoyo_p2mac[operation];
623         r->mode = tomoyo_get_mode(r->profile, r->type);
624         if (r->mode == TOMOYO_CONFIG_DISABLED)
625                 return 0;
626         r->param_type = TOMOYO_TYPE_PATH_ACL;
627         r->param.path.filename = filename;
628         r->param.path.operation = operation;
629         do {
630                 tomoyo_check_acl(r, tomoyo_check_path_acl);
631                 error = tomoyo_audit_path_log(r);
632                 /*
633                  * Do not retry for execute request, for alias may have
634                  * changed.
635                  */
636         } while (error == TOMOYO_RETRY_REQUEST &&
637                  operation != TOMOYO_TYPE_EXECUTE);
638         return error;
639 }
640
641 /**
642  * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
643  *
644  * @a: Pointer to "struct tomoyo_acl_info".
645  * @b: Pointer to "struct tomoyo_acl_info".
646  *
647  * Returns true if @a == @b except permission bits, false otherwise.
648  */
649 static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
650                                         const struct tomoyo_acl_info *b)
651 {
652         const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
653                                                                head);
654         const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
655                                                                head);
656         return tomoyo_same_name_union(&p1->name, &p2->name) &&
657                 tomoyo_same_number_union(&p1->number, &p2->number);
658 }
659
660 /**
661  * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
662  *
663  * @a:         Pointer to "struct tomoyo_acl_info".
664  * @b:         Pointer to "struct tomoyo_acl_info".
665  * @is_delete: True for @a &= ~@b, false for @a |= @b.
666  *
667  * Returns true if @a is empty, false otherwise.
668  */
669 static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
670                                          struct tomoyo_acl_info *b,
671                                          const bool is_delete)
672 {
673         u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
674                                           head)->perm;
675         u8 perm = *a_perm;
676         const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
677                 ->perm;
678         if (is_delete)
679                 perm &= ~b_perm;
680         else
681                 perm |= b_perm;
682         *a_perm = perm;
683         return !perm;
684 }
685
686 /**
687  * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
688  *
689  * @perm:  Permission.
690  * @param: Pointer to "struct tomoyo_acl_param".
691  *
692  * Returns 0 on success, negative value otherwise.
693  */
694 static int tomoyo_update_path_number_acl(const u8 perm,
695                                          struct tomoyo_acl_param *param)
696 {
697         struct tomoyo_path_number_acl e = {
698                 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
699                 .perm = perm
700         };
701         int error;
702         if (!tomoyo_parse_name_union(param, &e.name) ||
703             !tomoyo_parse_number_union(param, &e.number))
704                 error = -EINVAL;
705         else
706                 error = tomoyo_update_domain(&e.head, sizeof(e), param,
707                                              tomoyo_same_path_number_acl,
708                                              tomoyo_merge_path_number_acl);
709         tomoyo_put_name_union(&e.name);
710         tomoyo_put_number_union(&e.number);
711         return error;
712 }
713
714 /**
715  * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
716  *
717  * @type:   Type of operation.
718  * @path:   Pointer to "struct path".
719  * @number: Number.
720  *
721  * Returns 0 on success, negative value otherwise.
722  */
723 int tomoyo_path_number_perm(const u8 type, struct path *path,
724                             unsigned long number)
725 {
726         struct tomoyo_request_info r;
727         int error = -ENOMEM;
728         struct tomoyo_path_info buf;
729         int idx;
730
731         if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
732             == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
733                 return 0;
734         idx = tomoyo_read_lock();
735         if (!tomoyo_get_realpath(&buf, path))
736                 goto out;
737         if (type == TOMOYO_TYPE_MKDIR)
738                 tomoyo_add_slash(&buf);
739         r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
740         r.param.path_number.operation = type;
741         r.param.path_number.filename = &buf;
742         r.param.path_number.number = number;
743         do {
744                 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
745                 error = tomoyo_audit_path_number_log(&r);
746         } while (error == TOMOYO_RETRY_REQUEST);
747         kfree(buf.name);
748  out:
749         tomoyo_read_unlock(idx);
750         if (r.mode != TOMOYO_CONFIG_ENFORCING)
751                 error = 0;
752         return error;
753 }
754
755 /**
756  * tomoyo_check_open_permission - Check permission for "read" and "write".
757  *
758  * @domain: Pointer to "struct tomoyo_domain_info".
759  * @path:   Pointer to "struct path".
760  * @flag:   Flags for open().
761  *
762  * Returns 0 on success, negative value otherwise.
763  */
764 int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
765                                  struct path *path, const int flag)
766 {
767         const u8 acc_mode = ACC_MODE(flag);
768         int error = 0;
769         struct tomoyo_path_info buf;
770         struct tomoyo_request_info r;
771         int idx;
772
773         if (!path->mnt)
774                 return 0;
775         buf.name = NULL;
776         r.mode = TOMOYO_CONFIG_DISABLED;
777         idx = tomoyo_read_lock();
778         if (acc_mode &&
779             tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
780             != TOMOYO_CONFIG_DISABLED) {
781                 if (!tomoyo_get_realpath(&buf, path)) {
782                         error = -ENOMEM;
783                         goto out;
784                 }
785                 if (acc_mode & MAY_READ)
786                         error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
787                                                        &buf);
788                 if (!error && (acc_mode & MAY_WRITE))
789                         error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
790                                                        TOMOYO_TYPE_APPEND :
791                                                        TOMOYO_TYPE_WRITE,
792                                                        &buf);
793         }
794  out:
795         kfree(buf.name);
796         tomoyo_read_unlock(idx);
797         if (r.mode != TOMOYO_CONFIG_ENFORCING)
798                 error = 0;
799         return error;
800 }
801
802 /**
803  * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
804  *
805  * @operation: Type of operation.
806  * @path:      Pointer to "struct path".
807  *
808  * Returns 0 on success, negative value otherwise.
809  */
810 int tomoyo_path_perm(const u8 operation, struct path *path)
811 {
812         struct tomoyo_request_info r;
813         int error;
814         struct tomoyo_path_info buf;
815         bool is_enforce;
816         int idx;
817
818         if (!path->mnt)
819                 return 0;
820         if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
821             == TOMOYO_CONFIG_DISABLED)
822                 return 0;
823         is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
824         error = -ENOMEM;
825         buf.name = NULL;
826         idx = tomoyo_read_lock();
827         if (!tomoyo_get_realpath(&buf, path))
828                 goto out;
829         switch (operation) {
830         case TOMOYO_TYPE_RMDIR:
831         case TOMOYO_TYPE_CHROOT:
832                 tomoyo_add_slash(&buf);
833                 break;
834         }
835         error = tomoyo_path_permission(&r, operation, &buf);
836  out:
837         kfree(buf.name);
838         tomoyo_read_unlock(idx);
839         if (!is_enforce)
840                 error = 0;
841         return error;
842 }
843
844 /**
845  * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
846  *
847  * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
848  * @path:      Pointer to "struct path".
849  * @mode:      Create mode.
850  * @dev:       Device number.
851  *
852  * Returns 0 on success, negative value otherwise.
853  */
854 int tomoyo_mkdev_perm(const u8 operation, struct path *path,
855                       const unsigned int mode, unsigned int dev)
856 {
857         struct tomoyo_request_info r;
858         int error = -ENOMEM;
859         struct tomoyo_path_info buf;
860         int idx;
861
862         if (!path->mnt ||
863             tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
864             == TOMOYO_CONFIG_DISABLED)
865                 return 0;
866         idx = tomoyo_read_lock();
867         error = -ENOMEM;
868         if (tomoyo_get_realpath(&buf, path)) {
869                 dev = new_decode_dev(dev);
870                 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
871                 r.param.mkdev.filename = &buf;
872                 r.param.mkdev.operation = operation;
873                 r.param.mkdev.mode = mode;
874                 r.param.mkdev.major = MAJOR(dev);
875                 r.param.mkdev.minor = MINOR(dev);
876                 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
877                 error = tomoyo_audit_mkdev_log(&r);
878                 kfree(buf.name);
879         }
880         tomoyo_read_unlock(idx);
881         if (r.mode != TOMOYO_CONFIG_ENFORCING)
882                 error = 0;
883         return error;
884 }
885
886 /**
887  * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
888  *
889  * @operation: Type of operation.
890  * @path1:      Pointer to "struct path".
891  * @path2:      Pointer to "struct path".
892  *
893  * Returns 0 on success, negative value otherwise.
894  */
895 int tomoyo_path2_perm(const u8 operation, struct path *path1,
896                       struct path *path2)
897 {
898         int error = -ENOMEM;
899         struct tomoyo_path_info buf1;
900         struct tomoyo_path_info buf2;
901         struct tomoyo_request_info r;
902         int idx;
903
904         if (!path1->mnt || !path2->mnt ||
905             tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
906             == TOMOYO_CONFIG_DISABLED)
907                 return 0;
908         buf1.name = NULL;
909         buf2.name = NULL;
910         idx = tomoyo_read_lock();
911         if (!tomoyo_get_realpath(&buf1, path1) ||
912             !tomoyo_get_realpath(&buf2, path2))
913                 goto out;
914         switch (operation) {
915                 struct dentry *dentry;
916         case TOMOYO_TYPE_RENAME:
917         case TOMOYO_TYPE_LINK:
918                 dentry = path1->dentry;
919                 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
920                         break;
921                 /* fall through */
922         case TOMOYO_TYPE_PIVOT_ROOT:
923                 tomoyo_add_slash(&buf1);
924                 tomoyo_add_slash(&buf2);
925                 break;
926         }
927         r.param_type = TOMOYO_TYPE_PATH2_ACL;
928         r.param.path2.operation = operation;
929         r.param.path2.filename1 = &buf1;
930         r.param.path2.filename2 = &buf2;
931         do {
932                 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
933                 error = tomoyo_audit_path2_log(&r);
934         } while (error == TOMOYO_RETRY_REQUEST);
935  out:
936         kfree(buf1.name);
937         kfree(buf2.name);
938         tomoyo_read_unlock(idx);
939         if (r.mode != TOMOYO_CONFIG_ENFORCING)
940                 error = 0;
941         return error;
942 }
943
944 /**
945  * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
946  *
947  * @a: Pointer to "struct tomoyo_acl_info".
948  * @b: Pointer to "struct tomoyo_acl_info".
949  *
950  * Returns true if @a == @b, false otherwise.
951  */
952 static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
953                                   const struct tomoyo_acl_info *b)
954 {
955         const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
956         const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
957         return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
958                 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
959                 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
960                 tomoyo_same_number_union(&p1->flags, &p2->flags);
961 }
962
963 /**
964  * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
965  *
966  * @param: Pointer to "struct tomoyo_acl_param".
967  *
968  * Returns 0 on success, negative value otherwise.
969  *
970  * Caller holds tomoyo_read_lock().
971  */
972 static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
973 {
974         struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
975         int error;
976         if (!tomoyo_parse_name_union(param, &e.dev_name) ||
977             !tomoyo_parse_name_union(param, &e.dir_name) ||
978             !tomoyo_parse_name_union(param, &e.fs_type) ||
979             !tomoyo_parse_number_union(param, &e.flags))
980                 error = -EINVAL;
981         else
982                 error = tomoyo_update_domain(&e.head, sizeof(e), param,
983                                              tomoyo_same_mount_acl, NULL);
984         tomoyo_put_name_union(&e.dev_name);
985         tomoyo_put_name_union(&e.dir_name);
986         tomoyo_put_name_union(&e.fs_type);
987         tomoyo_put_number_union(&e.flags);
988         return error;
989 }
990
991 /**
992  * tomoyo_write_file - Update file related list.
993  *
994  * @param: Pointer to "struct tomoyo_acl_param".
995  *
996  * Returns 0 on success, negative value otherwise.
997  *
998  * Caller holds tomoyo_read_lock().
999  */
1000 int tomoyo_write_file(struct tomoyo_acl_param *param)
1001 {
1002         u16 perm = 0;
1003         u8 type;
1004         const char *operation = tomoyo_read_token(param);
1005         for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
1006                 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
1007                         perm |= 1 << type;
1008         if (perm)
1009                 return tomoyo_update_path_acl(perm, param);
1010         for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
1011                 if (tomoyo_permstr(operation, tomoyo_path2_keyword[type]))
1012                         perm |= 1 << type;
1013         if (perm)
1014                 return tomoyo_update_path2_acl(perm, param);
1015         for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
1016                 if (tomoyo_permstr(operation,
1017                                    tomoyo_path_number_keyword[type]))
1018                         perm |= 1 << type;
1019         if (perm)
1020                 return tomoyo_update_path_number_acl(perm, param);
1021         for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
1022                 if (tomoyo_permstr(operation, tomoyo_mkdev_keyword[type]))
1023                         perm |= 1 << type;
1024         if (perm)
1025                 return tomoyo_update_mkdev_acl(perm, param);
1026         if (tomoyo_permstr(operation, "mount"))
1027                 return tomoyo_update_mount_acl(param);
1028         return -EINVAL;
1029 }