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