blob: 86f7d1b90212a9b0f6bb6ce461d8e95b7a164c32 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kentaro Takedab69a54e2009-02-05 17:18:14 +09002/*
3 * security/tomoyo/file.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takedab69a54e2009-02-05 17:18:14 +09006 */
7
8#include "common.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Kentaro Takedab69a54e2009-02-05 17:18:14 +090010
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090011/*
12 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
13 */
Tetsuo Handa57c25902010-06-03 20:38:44 +090014static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090015 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
16 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
17 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa7c759642011-06-26 23:15:31 +090018 [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa57c25902010-06-03 20:38:44 +090019 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +090020 [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +090021 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
22 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
23 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +090024 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
25 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
26};
27
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090028/*
29 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
30 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090031const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090032 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
33 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
34};
35
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090036/*
37 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
38 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090039const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090040 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
41 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
42 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
43};
44
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090045/*
46 * Mapping table from "enum tomoyo_path_number_acl_index" to
47 * "enum tomoyo_mac_index".
48 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090049const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090050 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
51 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
52 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
53 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
54 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
55 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
56 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
57 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
58};
59
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090060/**
61 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
62 *
63 * @ptr: Pointer to "struct tomoyo_name_union".
64 *
65 * Returns nothing.
66 */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090067void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
68{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090069 tomoyo_put_group(ptr->group);
70 tomoyo_put_name(ptr->filename);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090071}
72
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090073/**
74 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
75 *
76 * @name: Pointer to "struct tomoyo_path_info".
77 * @ptr: Pointer to "struct tomoyo_name_union".
78 *
79 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
80 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090081const struct tomoyo_path_info *
82tomoyo_compare_name_union(const struct tomoyo_path_info *name,
83 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090084{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090085 if (ptr->group)
Tetsuo Handa3f629632010-06-03 20:37:26 +090086 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa484ca792010-07-29 14:29:55 +090087 if (tomoyo_path_matches_pattern(name, ptr->filename))
88 return ptr->filename;
89 return NULL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090090}
91
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090092/**
93 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
94 *
95 * @ptr: Pointer to "struct tomoyo_number_union".
96 *
97 * Returns nothing.
98 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090099void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
100{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900101 tomoyo_put_group(ptr->group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900102}
103
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900104/**
105 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
106 *
107 * @value: Number to check.
108 * @ptr: Pointer to "struct tomoyo_number_union".
109 *
110 * Returns true if @value matches @ptr, false otherwise.
111 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900112bool tomoyo_compare_number_union(const unsigned long value,
113 const struct tomoyo_number_union *ptr)
114{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900115 if (ptr->group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900116 return tomoyo_number_matches_group(value, value, ptr->group);
117 return value >= ptr->values[0] && value <= ptr->values[1];
118}
119
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900120/**
121 * tomoyo_add_slash - Add trailing '/' if needed.
122 *
123 * @buf: Pointer to "struct tomoyo_path_info".
124 *
125 * Returns nothing.
126 *
127 * @buf must be generated by tomoyo_encode() because this function does not
128 * allocate memory for adding '/'.
129 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900130static void tomoyo_add_slash(struct tomoyo_path_info *buf)
131{
132 if (buf->is_dir)
133 return;
134 /*
135 * This is OK because tomoyo_encode() reserves space for appending "/".
136 */
137 strcat((char *) buf->name, "/");
138 tomoyo_fill_path_info(buf);
139}
140
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900141/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900142 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900143 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900144 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900145 * @path: Pointer to "struct path".
146 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900147 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900148 */
Al Viro3f7036a2015-03-08 19:28:30 -0400149static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, const struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900150{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900151 buf->name = tomoyo_realpath_from_path(path);
152 if (buf->name) {
153 tomoyo_fill_path_info(buf);
154 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900155 }
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900156 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900157}
158
Tetsuo Handa99a85252010-06-16 16:22:51 +0900159/**
160 * tomoyo_audit_path_log - Audit path request log.
161 *
162 * @r: Pointer to "struct tomoyo_request_info".
163 *
164 * Returns 0 on success, negative value otherwise.
165 */
166static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
167{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900168 return tomoyo_supervisor(r, "file %s %s\n", tomoyo_path_keyword
169 [r->param.path.operation],
170 r->param.path.filename->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900171}
172
173/**
174 * tomoyo_audit_path2_log - Audit path/path request log.
175 *
176 * @r: Pointer to "struct tomoyo_request_info".
177 *
178 * Returns 0 on success, negative value otherwise.
179 */
180static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
181{
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900182 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
183 [tomoyo_pp2mac[r->param.path2.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900184 r->param.path2.filename1->name,
185 r->param.path2.filename2->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900186}
187
188/**
189 * tomoyo_audit_mkdev_log - Audit path/number/number/number 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_mkdev_log(struct tomoyo_request_info *r)
196{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900197 return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n",
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900198 tomoyo_mac_keywords
199 [tomoyo_pnnn2mac[r->param.mkdev.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900200 r->param.mkdev.filename->name,
201 r->param.mkdev.mode, r->param.mkdev.major,
202 r->param.mkdev.minor);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900203}
204
205/**
206 * tomoyo_audit_path_number_log - Audit path/number request log.
207 *
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900208 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa99a85252010-06-16 16:22:51 +0900209 *
210 * Returns 0 on success, negative value otherwise.
211 */
212static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
213{
214 const u8 type = r->param.path_number.operation;
215 u8 radix;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900216 char buffer[64];
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900217
Tetsuo Handa99a85252010-06-16 16:22:51 +0900218 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);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900235 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
236 [tomoyo_pn2mac[type]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900237 r->param.path_number.filename->name, buffer);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900238}
239
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900240/**
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 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900252static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900253 const struct tomoyo_acl_info *ptr)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900254{
Tetsuo Handa99a85252010-06-16 16:22:51 +0900255 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
256 head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900257
Tetsuo Handa484ca792010-07-29 14:29:55 +0900258 if (acl->perm & (1 << r->param.path.operation)) {
259 r->param.path.matched_path =
260 tomoyo_compare_name_union(r->param.path.filename,
261 &acl->name);
262 return r->param.path.matched_path != NULL;
263 }
264 return false;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900265}
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900266
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900267/**
268 * tomoyo_check_path_number_acl - Check permission for path number operation.
269 *
270 * @r: Pointer to "struct tomoyo_request_info".
271 * @ptr: Pointer to "struct tomoyo_acl_info".
272 *
273 * Returns true if granted, false otherwise.
274 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900275static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900276 const struct tomoyo_acl_info *ptr)
277{
278 const struct tomoyo_path_number_acl *acl =
279 container_of(ptr, typeof(*acl), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900280
Tetsuo Handa99a85252010-06-16 16:22:51 +0900281 return (acl->perm & (1 << r->param.path_number.operation)) &&
282 tomoyo_compare_number_union(r->param.path_number.number,
283 &acl->number) &&
284 tomoyo_compare_name_union(r->param.path_number.filename,
285 &acl->name);
286}
287
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900288/**
289 * tomoyo_check_path2_acl - Check permission for path path operation.
290 *
291 * @r: Pointer to "struct tomoyo_request_info".
292 * @ptr: Pointer to "struct tomoyo_acl_info".
293 *
294 * Returns true if granted, false otherwise.
295 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900296static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900297 const struct tomoyo_acl_info *ptr)
298{
299 const struct tomoyo_path2_acl *acl =
300 container_of(ptr, typeof(*acl), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900301
Tetsuo Handa99a85252010-06-16 16:22:51 +0900302 return (acl->perm & (1 << r->param.path2.operation)) &&
303 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
304 && tomoyo_compare_name_union(r->param.path2.filename2,
305 &acl->name2);
306}
307
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900308/**
309 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
310 *
311 * @r: Pointer to "struct tomoyo_request_info".
312 * @ptr: Pointer to "struct tomoyo_acl_info".
313 *
314 * Returns true if granted, false otherwise.
315 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900316static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900317 const struct tomoyo_acl_info *ptr)
Tetsuo Handa99a85252010-06-16 16:22:51 +0900318{
Tetsuo Handa75093152010-06-16 16:23:55 +0900319 const struct tomoyo_mkdev_acl *acl =
Tetsuo Handa99a85252010-06-16 16:22:51 +0900320 container_of(ptr, typeof(*acl), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900321
Tetsuo Handa99a85252010-06-16 16:22:51 +0900322 return (acl->perm & (1 << r->param.mkdev.operation)) &&
323 tomoyo_compare_number_union(r->param.mkdev.mode,
324 &acl->mode) &&
325 tomoyo_compare_number_union(r->param.mkdev.major,
326 &acl->major) &&
327 tomoyo_compare_number_union(r->param.mkdev.minor,
328 &acl->minor) &&
329 tomoyo_compare_name_union(r->param.mkdev.filename,
330 &acl->name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900331}
332
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900333/**
334 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
335 *
336 * @a: Pointer to "struct tomoyo_acl_info".
337 * @b: Pointer to "struct tomoyo_acl_info".
338 *
339 * Returns true if @a == @b except permission bits, false otherwise.
340 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900341static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
342 const struct tomoyo_acl_info *b)
343{
344 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
345 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900346
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900347 return tomoyo_same_name_union(&p1->name, &p2->name);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900348}
349
Tetsuo Handa7c759642011-06-26 23:15:31 +0900350/**
351 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
352 *
353 * @a: Pointer to "struct tomoyo_acl_info".
354 * @b: Pointer to "struct tomoyo_acl_info".
355 * @is_delete: True for @a &= ~@b, false for @a |= @b.
356 *
357 * Returns true if @a is empty, false otherwise.
358 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900359static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
360 struct tomoyo_acl_info *b,
361 const bool is_delete)
362{
363 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
364 ->perm;
365 u16 perm = *a_perm;
366 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900367
Tetsuo Handa7c759642011-06-26 23:15:31 +0900368 if (is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900369 perm &= ~b_perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900370 else
Tetsuo Handa237ab452010-06-12 20:46:22 +0900371 perm |= b_perm;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900372 *a_perm = perm;
373 return !perm;
374}
375
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900376/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900377 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900378 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900379 * @perm: Permission.
380 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900381 *
382 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900383 *
384 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900385 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900386static int tomoyo_update_path_acl(const u16 perm,
387 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900388{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900389 struct tomoyo_path_acl e = {
390 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900391 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900392 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900393 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900394
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900395 if (!tomoyo_parse_name_union(param, &e.name))
396 error = -EINVAL;
397 else
398 error = tomoyo_update_domain(&e.head, sizeof(e), param,
399 tomoyo_same_path_acl,
400 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900401 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900402 return error;
403}
404
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900405/**
406 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
407 *
408 * @a: Pointer to "struct tomoyo_acl_info".
409 * @b: Pointer to "struct tomoyo_acl_info".
410 *
411 * Returns true if @a == @b except permission bits, false otherwise.
412 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900413static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900414 const struct tomoyo_acl_info *b)
415{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900416 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
417 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900418
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900419 return tomoyo_same_name_union(&p1->name, &p2->name) &&
420 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
421 tomoyo_same_number_union(&p1->major, &p2->major) &&
422 tomoyo_same_number_union(&p1->minor, &p2->minor);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900423}
424
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900425/**
426 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
427 *
428 * @a: Pointer to "struct tomoyo_acl_info".
429 * @b: Pointer to "struct tomoyo_acl_info".
430 * @is_delete: True for @a &= ~@b, false for @a |= @b.
431 *
432 * Returns true if @a is empty, false otherwise.
433 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900434static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900435 struct tomoyo_acl_info *b,
436 const bool is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900437{
Tetsuo Handa75093152010-06-16 16:23:55 +0900438 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900439 head)->perm;
440 u8 perm = *a_perm;
Tetsuo Handa75093152010-06-16 16:23:55 +0900441 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900442 ->perm;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900443
Tetsuo Handa237ab452010-06-12 20:46:22 +0900444 if (is_delete)
445 perm &= ~b_perm;
446 else
447 perm |= b_perm;
448 *a_perm = perm;
449 return !perm;
450}
451
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900452/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900453 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900454 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900455 * @perm: Permission.
456 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900457 *
458 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900459 *
460 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900461 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900462static int tomoyo_update_mkdev_acl(const u8 perm,
463 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900464{
Tetsuo Handa75093152010-06-16 16:23:55 +0900465 struct tomoyo_mkdev_acl e = {
466 .head.type = TOMOYO_TYPE_MKDEV_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900467 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900468 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900469 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900470
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900471 if (!tomoyo_parse_name_union(param, &e.name) ||
472 !tomoyo_parse_number_union(param, &e.mode) ||
473 !tomoyo_parse_number_union(param, &e.major) ||
474 !tomoyo_parse_number_union(param, &e.minor))
475 error = -EINVAL;
476 else
477 error = tomoyo_update_domain(&e.head, sizeof(e), param,
478 tomoyo_same_mkdev_acl,
479 tomoyo_merge_mkdev_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900480 tomoyo_put_name_union(&e.name);
481 tomoyo_put_number_union(&e.mode);
482 tomoyo_put_number_union(&e.major);
483 tomoyo_put_number_union(&e.minor);
484 return error;
485}
486
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900487/**
488 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
489 *
490 * @a: Pointer to "struct tomoyo_acl_info".
491 * @b: Pointer to "struct tomoyo_acl_info".
492 *
493 * Returns true if @a == @b except permission bits, false otherwise.
494 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900495static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
496 const struct tomoyo_acl_info *b)
497{
498 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
499 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900500
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900501 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
502 tomoyo_same_name_union(&p1->name2, &p2->name2);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900503}
504
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900505/**
506 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
507 *
508 * @a: Pointer to "struct tomoyo_acl_info".
509 * @b: Pointer to "struct tomoyo_acl_info".
510 * @is_delete: True for @a &= ~@b, false for @a |= @b.
511 *
512 * Returns true if @a is empty, false otherwise.
513 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900514static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
515 struct tomoyo_acl_info *b,
516 const bool is_delete)
517{
518 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
519 ->perm;
520 u8 perm = *a_perm;
521 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900522
Tetsuo Handa237ab452010-06-12 20:46:22 +0900523 if (is_delete)
524 perm &= ~b_perm;
525 else
526 perm |= b_perm;
527 *a_perm = perm;
528 return !perm;
529}
530
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900531/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900532 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900533 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900534 * @perm: Permission.
535 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900536 *
537 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900538 *
539 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900540 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900541static int tomoyo_update_path2_acl(const u8 perm,
542 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900543{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900544 struct tomoyo_path2_acl e = {
545 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900546 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900547 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900548 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900549
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900550 if (!tomoyo_parse_name_union(param, &e.name1) ||
551 !tomoyo_parse_name_union(param, &e.name2))
552 error = -EINVAL;
553 else
554 error = tomoyo_update_domain(&e.head, sizeof(e), param,
555 tomoyo_same_path2_acl,
556 tomoyo_merge_path2_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900557 tomoyo_put_name_union(&e.name1);
558 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900559 return error;
560}
561
562/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900563 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900564 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900565 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900566 * @operation: Type of operation.
567 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900568 *
569 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900570 *
571 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900572 */
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900573static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
574 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900575{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900576 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900577
Tetsuo Handa57c25902010-06-03 20:38:44 +0900578 r->type = tomoyo_p2mac[operation];
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900579 r->mode = tomoyo_get_mode(r->domain->ns, r->profile, r->type);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900580 if (r->mode == TOMOYO_CONFIG_DISABLED)
581 return 0;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900582 r->param_type = TOMOYO_TYPE_PATH_ACL;
583 r->param.path.filename = filename;
584 r->param.path.operation = operation;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900585 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900586 tomoyo_check_acl(r, tomoyo_check_path_acl);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900587 error = tomoyo_audit_path_log(r);
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900588 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900589 return error;
590}
591
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900592/**
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900593 * tomoyo_execute_permission - Check permission for execute operation.
594 *
595 * @r: Pointer to "struct tomoyo_request_info".
596 * @filename: Filename to check.
597 *
598 * Returns 0 on success, negative value otherwise.
599 *
600 * Caller holds tomoyo_read_lock().
601 */
602int tomoyo_execute_permission(struct tomoyo_request_info *r,
603 const struct tomoyo_path_info *filename)
604{
605 /*
606 * Unlike other permission checks, this check is done regardless of
607 * profile mode settings in order to check for domain transition
608 * preference.
609 */
610 r->type = TOMOYO_MAC_FILE_EXECUTE;
611 r->mode = tomoyo_get_mode(r->domain->ns, r->profile, r->type);
612 r->param_type = TOMOYO_TYPE_PATH_ACL;
613 r->param.path.filename = filename;
614 r->param.path.operation = TOMOYO_TYPE_EXECUTE;
615 tomoyo_check_acl(r, tomoyo_check_path_acl);
616 r->ee->transition = r->matched_acl && r->matched_acl->cond ?
617 r->matched_acl->cond->transit : NULL;
618 if (r->mode != TOMOYO_CONFIG_DISABLED)
619 return tomoyo_audit_path_log(r);
620 return 0;
621}
622
623/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900624 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
625 *
626 * @a: Pointer to "struct tomoyo_acl_info".
627 * @b: Pointer to "struct tomoyo_acl_info".
628 *
629 * Returns true if @a == @b except permission bits, false otherwise.
630 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900631static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
632 const struct tomoyo_acl_info *b)
633{
634 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
635 head);
636 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
637 head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900638
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900639 return tomoyo_same_name_union(&p1->name, &p2->name) &&
640 tomoyo_same_number_union(&p1->number, &p2->number);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900641}
642
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900643/**
644 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
645 *
646 * @a: Pointer to "struct tomoyo_acl_info".
647 * @b: Pointer to "struct tomoyo_acl_info".
648 * @is_delete: True for @a &= ~@b, false for @a |= @b.
649 *
650 * Returns true if @a is empty, false otherwise.
651 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900652static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
653 struct tomoyo_acl_info *b,
654 const bool is_delete)
655{
656 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
657 head)->perm;
658 u8 perm = *a_perm;
659 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
660 ->perm;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900661
Tetsuo Handa237ab452010-06-12 20:46:22 +0900662 if (is_delete)
663 perm &= ~b_perm;
664 else
665 perm |= b_perm;
666 *a_perm = perm;
667 return !perm;
668}
669
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900670/**
671 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
672 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900673 * @perm: Permission.
674 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900675 *
676 * Returns 0 on success, negative value otherwise.
677 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900678static int tomoyo_update_path_number_acl(const u8 perm,
679 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900680{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900681 struct tomoyo_path_number_acl e = {
682 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900683 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900684 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900685 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900686
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900687 if (!tomoyo_parse_name_union(param, &e.name) ||
688 !tomoyo_parse_number_union(param, &e.number))
689 error = -EINVAL;
690 else
691 error = tomoyo_update_domain(&e.head, sizeof(e), param,
692 tomoyo_same_path_number_acl,
693 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900694 tomoyo_put_name_union(&e.name);
695 tomoyo_put_number_union(&e.number);
696 return error;
697}
698
699/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900700 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
701 *
702 * @type: Type of operation.
703 * @path: Pointer to "struct path".
704 * @number: Number.
705 *
706 * Returns 0 on success, negative value otherwise.
707 */
Al Viroe6641ed2016-03-25 14:41:28 -0400708int tomoyo_path_number_perm(const u8 type, const struct path *path,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900709 unsigned long number)
710{
711 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900712 struct tomoyo_obj_info obj = {
Kees Cook82917982017-03-29 16:52:58 -0700713 .path1 = { .mnt = path->mnt, .dentry = path->dentry },
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900714 };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900715 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900716 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900717 int idx;
718
Tetsuo Handa57c25902010-06-03 20:38:44 +0900719 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900720 == TOMOYO_CONFIG_DISABLED || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900721 return 0;
722 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900723 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900724 goto out;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900725 r.obj = &obj;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900726 if (type == TOMOYO_TYPE_MKDIR)
727 tomoyo_add_slash(&buf);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900728 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
729 r.param.path_number.operation = type;
730 r.param.path_number.filename = &buf;
731 r.param.path_number.number = number;
732 do {
733 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
734 error = tomoyo_audit_path_number_log(&r);
735 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900736 kfree(buf.name);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900737 out:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900738 tomoyo_read_unlock(idx);
739 if (r.mode != TOMOYO_CONFIG_ENFORCING)
740 error = 0;
741 return error;
742}
743
744/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900745 * tomoyo_check_open_permission - Check permission for "read" and "write".
746 *
747 * @domain: Pointer to "struct tomoyo_domain_info".
748 * @path: Pointer to "struct path".
749 * @flag: Flags for open().
750 *
751 * Returns 0 on success, negative value otherwise.
752 */
753int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
Al Viroe6641ed2016-03-25 14:41:28 -0400754 const struct path *path, const int flag)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900755{
756 const u8 acc_mode = ACC_MODE(flag);
Tetsuo Handaeae61f32011-03-02 16:54:24 +0900757 int error = 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900758 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900759 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900760 struct tomoyo_obj_info obj = {
Kees Cook82917982017-03-29 16:52:58 -0700761 .path1 = { .mnt = path->mnt, .dentry = path->dentry },
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900762 };
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900763 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900764
Tetsuo Handa57c25902010-06-03 20:38:44 +0900765 buf.name = NULL;
766 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900767 idx = tomoyo_read_lock();
Tetsuo Handa7c759642011-06-26 23:15:31 +0900768 if (acc_mode &&
769 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900770 != TOMOYO_CONFIG_DISABLED) {
771 if (!tomoyo_get_realpath(&buf, path)) {
772 error = -ENOMEM;
773 goto out;
774 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900775 r.obj = &obj;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900776 if (acc_mode & MAY_READ)
777 error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900778 &buf);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900779 if (!error && (acc_mode & MAY_WRITE))
780 error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
781 TOMOYO_TYPE_APPEND :
782 TOMOYO_TYPE_WRITE,
783 &buf);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900784 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900785 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900786 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900787 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900788 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900789 error = 0;
790 return error;
791}
792
793/**
Tetsuo Handa7c759642011-06-26 23:15:31 +0900794 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900795 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796 * @operation: Type of operation.
797 * @path: Pointer to "struct path".
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900798 * @target: Symlink's target if @operation is TOMOYO_TYPE_SYMLINK,
799 * NULL otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900800 *
801 * Returns 0 on success, negative value otherwise.
802 */
Al Viro3f7036a2015-03-08 19:28:30 -0400803int tomoyo_path_perm(const u8 operation, const struct path *path, const char *target)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900804{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900805 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900806 struct tomoyo_obj_info obj = {
Kees Cook82917982017-03-29 16:52:58 -0700807 .path1 = { .mnt = path->mnt, .dentry = path->dentry },
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900808 };
Tetsuo Handa7c759642011-06-26 23:15:31 +0900809 int error;
810 struct tomoyo_path_info buf;
811 bool is_enforce;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900812 struct tomoyo_path_info symlink_target;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900813 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900814
Tetsuo Handa57c25902010-06-03 20:38:44 +0900815 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
816 == TOMOYO_CONFIG_DISABLED)
817 return 0;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900818 is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
819 error = -ENOMEM;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900820 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900821 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900822 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900823 goto out;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900824 r.obj = &obj;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900825 switch (operation) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900826 case TOMOYO_TYPE_RMDIR:
827 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900828 tomoyo_add_slash(&buf);
829 break;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900830 case TOMOYO_TYPE_SYMLINK:
831 symlink_target.name = tomoyo_encode(target);
832 if (!symlink_target.name)
833 goto out;
834 tomoyo_fill_path_info(&symlink_target);
835 obj.symlink_target = &symlink_target;
836 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900837 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900838 error = tomoyo_path_permission(&r, operation, &buf);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900839 if (operation == TOMOYO_TYPE_SYMLINK)
840 kfree(symlink_target.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900841 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900842 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900843 tomoyo_read_unlock(idx);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900844 if (!is_enforce)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900845 error = 0;
846 return error;
847}
848
849/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900850 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900851 *
852 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
853 * @path: Pointer to "struct path".
854 * @mode: Create mode.
855 * @dev: Device number.
856 *
857 * Returns 0 on success, negative value otherwise.
858 */
Al Viroe6641ed2016-03-25 14:41:28 -0400859int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900860 const unsigned int mode, unsigned int dev)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900861{
862 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900863 struct tomoyo_obj_info obj = {
Kees Cook82917982017-03-29 16:52:58 -0700864 .path1 = { .mnt = path->mnt, .dentry = path->dentry },
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900865 };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900866 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900867 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900868 int idx;
869
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900870 if (tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900871 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900872 return 0;
873 idx = tomoyo_read_lock();
874 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900875 if (tomoyo_get_realpath(&buf, path)) {
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900876 r.obj = &obj;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900877 dev = new_decode_dev(dev);
Tetsuo Handa75093152010-06-16 16:23:55 +0900878 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900879 r.param.mkdev.filename = &buf;
880 r.param.mkdev.operation = operation;
881 r.param.mkdev.mode = mode;
882 r.param.mkdev.major = MAJOR(dev);
883 r.param.mkdev.minor = MINOR(dev);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900884 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
885 error = tomoyo_audit_mkdev_log(&r);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900886 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900887 }
888 tomoyo_read_unlock(idx);
889 if (r.mode != TOMOYO_CONFIG_ENFORCING)
890 error = 0;
891 return error;
892}
893
894/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900895 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900896 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900897 * @operation: Type of operation.
898 * @path1: Pointer to "struct path".
899 * @path2: Pointer to "struct path".
900 *
901 * Returns 0 on success, negative value otherwise.
902 */
Al Viroe6641ed2016-03-25 14:41:28 -0400903int tomoyo_path2_perm(const u8 operation, const struct path *path1,
904 const struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900905{
906 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900907 struct tomoyo_path_info buf1;
908 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900909 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900910 struct tomoyo_obj_info obj = {
Kees Cook82917982017-03-29 16:52:58 -0700911 .path1 = { .mnt = path1->mnt, .dentry = path1->dentry },
912 .path2 = { .mnt = path2->mnt, .dentry = path2->dentry }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900913 };
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900914 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900915
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900916 if (tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900917 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900918 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900919 buf1.name = NULL;
920 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900921 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900922 if (!tomoyo_get_realpath(&buf1, path1) ||
923 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900924 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900925 switch (operation) {
Tetsuo Handa57c25902010-06-03 20:38:44 +0900926 case TOMOYO_TYPE_RENAME:
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900927 case TOMOYO_TYPE_LINK:
David Howellse656a8e2015-01-29 12:02:32 +0000928 if (!d_is_dir(path1->dentry))
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900929 break;
930 /* fall through */
931 case TOMOYO_TYPE_PIVOT_ROOT:
932 tomoyo_add_slash(&buf1);
933 tomoyo_add_slash(&buf2);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900934 break;
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900935 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900936 r.obj = &obj;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900937 r.param_type = TOMOYO_TYPE_PATH2_ACL;
938 r.param.path2.operation = operation;
939 r.param.path2.filename1 = &buf1;
940 r.param.path2.filename2 = &buf2;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900941 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900942 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
943 error = tomoyo_audit_path2_log(&r);
944 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900945 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900946 kfree(buf1.name);
947 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900948 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900949 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900950 error = 0;
951 return error;
952}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900953
954/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900955 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900956 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900957 * @a: Pointer to "struct tomoyo_acl_info".
958 * @b: Pointer to "struct tomoyo_acl_info".
959 *
960 * Returns true if @a == @b, false otherwise.
961 */
962static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
963 const struct tomoyo_acl_info *b)
964{
965 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
966 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900967
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900968 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
969 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
970 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
971 tomoyo_same_number_union(&p1->flags, &p2->flags);
972}
973
974/**
975 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
976 *
977 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900978 *
979 * Returns 0 on success, negative value otherwise.
980 *
981 * Caller holds tomoyo_read_lock().
982 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900983static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900984{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900985 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
986 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900987
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900988 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
989 !tomoyo_parse_name_union(param, &e.dir_name) ||
990 !tomoyo_parse_name_union(param, &e.fs_type) ||
991 !tomoyo_parse_number_union(param, &e.flags))
992 error = -EINVAL;
993 else
994 error = tomoyo_update_domain(&e.head, sizeof(e), param,
995 tomoyo_same_mount_acl, NULL);
996 tomoyo_put_name_union(&e.dev_name);
997 tomoyo_put_name_union(&e.dir_name);
998 tomoyo_put_name_union(&e.fs_type);
999 tomoyo_put_number_union(&e.flags);
1000 return error;
1001}
1002
1003/**
1004 * tomoyo_write_file - Update file related list.
1005 *
1006 * @param: Pointer to "struct tomoyo_acl_param".
1007 *
1008 * Returns 0 on success, negative value otherwise.
1009 *
1010 * Caller holds tomoyo_read_lock().
1011 */
1012int tomoyo_write_file(struct tomoyo_acl_param *param)
1013{
1014 u16 perm = 0;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001015 u8 type;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001016 const char *operation = tomoyo_read_token(param);
Tetsuo Handacdcf6722019-01-24 18:37:35 +09001017
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001018 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
1019 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
1020 perm |= 1 << type;
1021 if (perm)
1022 return tomoyo_update_path_acl(perm, param);
1023 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001024 if (tomoyo_permstr(operation,
1025 tomoyo_mac_keywords[tomoyo_pp2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001026 perm |= 1 << type;
1027 if (perm)
1028 return tomoyo_update_path2_acl(perm, param);
1029 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
1030 if (tomoyo_permstr(operation,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001031 tomoyo_mac_keywords[tomoyo_pn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001032 perm |= 1 << type;
1033 if (perm)
1034 return tomoyo_update_path_number_acl(perm, param);
1035 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001036 if (tomoyo_permstr(operation,
1037 tomoyo_mac_keywords[tomoyo_pnnn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001038 perm |= 1 << type;
1039 if (perm)
1040 return tomoyo_update_mkdev_acl(perm, param);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001041 if (tomoyo_permstr(operation,
1042 tomoyo_mac_keywords[TOMOYO_MAC_FILE_MOUNT]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001043 return tomoyo_update_mount_acl(param);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001044 return -EINVAL;
1045}