blob: 2367b100cc62daccafa80932e4740385612746e9 [file] [log] [blame]
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001/*
2 * security/tomoyo/file.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takedab69a54e2009-02-05 17:18:14 +09005 */
6
7#include "common.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Kentaro Takedab69a54e2009-02-05 17:18:14 +09009
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090010/*
11 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
12 */
Tetsuo Handa57c25902010-06-03 20:38:44 +090013static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090014 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
15 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
16 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa7c759642011-06-26 23:15:31 +090017 [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa57c25902010-06-03 20:38:44 +090018 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +090019 [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +090020 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
21 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
22 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +090023 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
24 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
25};
26
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090027/*
28 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
29 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090030const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090031 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
32 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
33};
34
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090035/*
36 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
37 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090038const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090039 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
40 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
41 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
42};
43
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090044/*
45 * Mapping table from "enum tomoyo_path_number_acl_index" to
46 * "enum tomoyo_mac_index".
47 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090048const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090049 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
50 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
51 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
52 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
53 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
54 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
55 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
56 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
57};
58
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090059/**
60 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
61 *
62 * @ptr: Pointer to "struct tomoyo_name_union".
63 *
64 * Returns nothing.
65 */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090066void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
67{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090068 tomoyo_put_group(ptr->group);
69 tomoyo_put_name(ptr->filename);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090070}
71
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090072/**
73 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
74 *
75 * @name: Pointer to "struct tomoyo_path_info".
76 * @ptr: Pointer to "struct tomoyo_name_union".
77 *
78 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
79 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090080const struct tomoyo_path_info *
81tomoyo_compare_name_union(const struct tomoyo_path_info *name,
82 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090083{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090084 if (ptr->group)
Tetsuo Handa3f629632010-06-03 20:37:26 +090085 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa484ca792010-07-29 14:29:55 +090086 if (tomoyo_path_matches_pattern(name, ptr->filename))
87 return ptr->filename;
88 return NULL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090089}
90
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090091/**
92 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
93 *
94 * @ptr: Pointer to "struct tomoyo_number_union".
95 *
96 * Returns nothing.
97 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090098void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
99{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900100 tomoyo_put_group(ptr->group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900101}
102
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900103/**
104 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
105 *
106 * @value: Number to check.
107 * @ptr: Pointer to "struct tomoyo_number_union".
108 *
109 * Returns true if @value matches @ptr, false otherwise.
110 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900111bool tomoyo_compare_number_union(const unsigned long value,
112 const struct tomoyo_number_union *ptr)
113{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900114 if (ptr->group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900115 return tomoyo_number_matches_group(value, value, ptr->group);
116 return value >= ptr->values[0] && value <= ptr->values[1];
117}
118
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900119/**
120 * tomoyo_add_slash - Add trailing '/' if needed.
121 *
122 * @buf: Pointer to "struct tomoyo_path_info".
123 *
124 * Returns nothing.
125 *
126 * @buf must be generated by tomoyo_encode() because this function does not
127 * allocate memory for adding '/'.
128 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900129static void tomoyo_add_slash(struct tomoyo_path_info *buf)
130{
131 if (buf->is_dir)
132 return;
133 /*
134 * This is OK because tomoyo_encode() reserves space for appending "/".
135 */
136 strcat((char *) buf->name, "/");
137 tomoyo_fill_path_info(buf);
138}
139
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900140/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900141 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900142 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900143 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900144 * @path: Pointer to "struct path".
145 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900146 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900147 */
Al Viro3f7036a2015-03-08 19:28:30 -0400148static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, const struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900149{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900150 buf->name = tomoyo_realpath_from_path(path);
151 if (buf->name) {
152 tomoyo_fill_path_info(buf);
153 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900154 }
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900155 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900156}
157
Tetsuo Handa99a85252010-06-16 16:22:51 +0900158/**
159 * tomoyo_audit_path_log - Audit path request log.
160 *
161 * @r: Pointer to "struct tomoyo_request_info".
162 *
163 * Returns 0 on success, negative value otherwise.
164 */
165static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
166{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900167 return tomoyo_supervisor(r, "file %s %s\n", tomoyo_path_keyword
168 [r->param.path.operation],
169 r->param.path.filename->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900170}
171
172/**
173 * tomoyo_audit_path2_log - Audit path/path request log.
174 *
175 * @r: Pointer to "struct tomoyo_request_info".
176 *
177 * Returns 0 on success, negative value otherwise.
178 */
179static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
180{
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900181 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
182 [tomoyo_pp2mac[r->param.path2.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900183 r->param.path2.filename1->name,
184 r->param.path2.filename2->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900185}
186
187/**
188 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
189 *
190 * @r: Pointer to "struct tomoyo_request_info".
191 *
192 * Returns 0 on success, negative value otherwise.
193 */
194static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
195{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900196 return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n",
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900197 tomoyo_mac_keywords
198 [tomoyo_pnnn2mac[r->param.mkdev.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900199 r->param.mkdev.filename->name,
200 r->param.mkdev.mode, r->param.mkdev.major,
201 r->param.mkdev.minor);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900202}
203
204/**
205 * tomoyo_audit_path_number_log - Audit path/number request log.
206 *
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900207 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa99a85252010-06-16 16:22:51 +0900208 *
209 * Returns 0 on success, negative value otherwise.
210 */
211static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
212{
213 const u8 type = r->param.path_number.operation;
214 u8 radix;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900215 char buffer[64];
Tetsuo Handa99a85252010-06-16 16:22:51 +0900216 switch (type) {
217 case TOMOYO_TYPE_CREATE:
218 case TOMOYO_TYPE_MKDIR:
219 case TOMOYO_TYPE_MKFIFO:
220 case TOMOYO_TYPE_MKSOCK:
221 case TOMOYO_TYPE_CHMOD:
222 radix = TOMOYO_VALUE_TYPE_OCTAL;
223 break;
224 case TOMOYO_TYPE_IOCTL:
225 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
226 break;
227 default:
228 radix = TOMOYO_VALUE_TYPE_DECIMAL;
229 break;
230 }
231 tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
232 radix);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900233 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
234 [tomoyo_pn2mac[type]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900235 r->param.path_number.filename->name, buffer);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900236}
237
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900238/**
239 * tomoyo_check_path_acl - Check permission for path operation.
240 *
241 * @r: Pointer to "struct tomoyo_request_info".
242 * @ptr: Pointer to "struct tomoyo_acl_info".
243 *
244 * Returns true if granted, false otherwise.
245 *
246 * To be able to use wildcard for domain transition, this function sets
247 * matching entry on success. Since the caller holds tomoyo_read_lock(),
248 * it is safe to set matching entry.
249 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900250static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900251 const struct tomoyo_acl_info *ptr)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900252{
Tetsuo Handa99a85252010-06-16 16:22:51 +0900253 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
254 head);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900255 if (acl->perm & (1 << r->param.path.operation)) {
256 r->param.path.matched_path =
257 tomoyo_compare_name_union(r->param.path.filename,
258 &acl->name);
259 return r->param.path.matched_path != NULL;
260 }
261 return false;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900262}
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900263
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900264/**
265 * tomoyo_check_path_number_acl - Check permission for path number operation.
266 *
267 * @r: Pointer to "struct tomoyo_request_info".
268 * @ptr: Pointer to "struct tomoyo_acl_info".
269 *
270 * Returns true if granted, false otherwise.
271 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900272static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900273 const struct tomoyo_acl_info *ptr)
274{
275 const struct tomoyo_path_number_acl *acl =
276 container_of(ptr, typeof(*acl), head);
277 return (acl->perm & (1 << r->param.path_number.operation)) &&
278 tomoyo_compare_number_union(r->param.path_number.number,
279 &acl->number) &&
280 tomoyo_compare_name_union(r->param.path_number.filename,
281 &acl->name);
282}
283
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900284/**
285 * tomoyo_check_path2_acl - Check permission for path path operation.
286 *
287 * @r: Pointer to "struct tomoyo_request_info".
288 * @ptr: Pointer to "struct tomoyo_acl_info".
289 *
290 * Returns true if granted, false otherwise.
291 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900292static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900293 const struct tomoyo_acl_info *ptr)
294{
295 const struct tomoyo_path2_acl *acl =
296 container_of(ptr, typeof(*acl), head);
297 return (acl->perm & (1 << r->param.path2.operation)) &&
298 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
299 && tomoyo_compare_name_union(r->param.path2.filename2,
300 &acl->name2);
301}
302
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900303/**
304 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
305 *
306 * @r: Pointer to "struct tomoyo_request_info".
307 * @ptr: Pointer to "struct tomoyo_acl_info".
308 *
309 * Returns true if granted, false otherwise.
310 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900311static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900312 const struct tomoyo_acl_info *ptr)
Tetsuo Handa99a85252010-06-16 16:22:51 +0900313{
Tetsuo Handa75093152010-06-16 16:23:55 +0900314 const struct tomoyo_mkdev_acl *acl =
Tetsuo Handa99a85252010-06-16 16:22:51 +0900315 container_of(ptr, typeof(*acl), head);
316 return (acl->perm & (1 << r->param.mkdev.operation)) &&
317 tomoyo_compare_number_union(r->param.mkdev.mode,
318 &acl->mode) &&
319 tomoyo_compare_number_union(r->param.mkdev.major,
320 &acl->major) &&
321 tomoyo_compare_number_union(r->param.mkdev.minor,
322 &acl->minor) &&
323 tomoyo_compare_name_union(r->param.mkdev.filename,
324 &acl->name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900325}
326
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900327/**
328 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
329 *
330 * @a: Pointer to "struct tomoyo_acl_info".
331 * @b: Pointer to "struct tomoyo_acl_info".
332 *
333 * Returns true if @a == @b except permission bits, false otherwise.
334 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900335static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
336 const struct tomoyo_acl_info *b)
337{
338 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
339 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900340 return tomoyo_same_name_union(&p1->name, &p2->name);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900341}
342
Tetsuo Handa7c759642011-06-26 23:15:31 +0900343/**
344 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
345 *
346 * @a: Pointer to "struct tomoyo_acl_info".
347 * @b: Pointer to "struct tomoyo_acl_info".
348 * @is_delete: True for @a &= ~@b, false for @a |= @b.
349 *
350 * Returns true if @a is empty, false otherwise.
351 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900352static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
353 struct tomoyo_acl_info *b,
354 const bool is_delete)
355{
356 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
357 ->perm;
358 u16 perm = *a_perm;
359 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900360 if (is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900361 perm &= ~b_perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900362 else
Tetsuo Handa237ab452010-06-12 20:46:22 +0900363 perm |= b_perm;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900364 *a_perm = perm;
365 return !perm;
366}
367
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900368/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900369 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900370 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900371 * @perm: Permission.
372 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900373 *
374 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900375 *
376 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900377 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900378static int tomoyo_update_path_acl(const u16 perm,
379 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900380{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900381 struct tomoyo_path_acl e = {
382 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900383 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900384 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900385 int error;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900386 if (!tomoyo_parse_name_union(param, &e.name))
387 error = -EINVAL;
388 else
389 error = tomoyo_update_domain(&e.head, sizeof(e), param,
390 tomoyo_same_path_acl,
391 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900392 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900393 return error;
394}
395
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900396/**
397 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
398 *
399 * @a: Pointer to "struct tomoyo_acl_info".
400 * @b: Pointer to "struct tomoyo_acl_info".
401 *
402 * Returns true if @a == @b except permission bits, false otherwise.
403 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900404static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900405 const struct tomoyo_acl_info *b)
406{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900407 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
408 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
409 return tomoyo_same_name_union(&p1->name, &p2->name) &&
410 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
411 tomoyo_same_number_union(&p1->major, &p2->major) &&
412 tomoyo_same_number_union(&p1->minor, &p2->minor);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900413}
414
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900415/**
416 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
417 *
418 * @a: Pointer to "struct tomoyo_acl_info".
419 * @b: Pointer to "struct tomoyo_acl_info".
420 * @is_delete: True for @a &= ~@b, false for @a |= @b.
421 *
422 * Returns true if @a is empty, false otherwise.
423 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900424static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900425 struct tomoyo_acl_info *b,
426 const bool is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900427{
Tetsuo Handa75093152010-06-16 16:23:55 +0900428 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900429 head)->perm;
430 u8 perm = *a_perm;
Tetsuo Handa75093152010-06-16 16:23:55 +0900431 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900432 ->perm;
433 if (is_delete)
434 perm &= ~b_perm;
435 else
436 perm |= b_perm;
437 *a_perm = perm;
438 return !perm;
439}
440
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900441/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900442 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900443 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900444 * @perm: Permission.
445 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900446 *
447 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900448 *
449 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900450 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900451static int tomoyo_update_mkdev_acl(const u8 perm,
452 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900453{
Tetsuo Handa75093152010-06-16 16:23:55 +0900454 struct tomoyo_mkdev_acl e = {
455 .head.type = TOMOYO_TYPE_MKDEV_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900456 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900457 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900458 int error;
459 if (!tomoyo_parse_name_union(param, &e.name) ||
460 !tomoyo_parse_number_union(param, &e.mode) ||
461 !tomoyo_parse_number_union(param, &e.major) ||
462 !tomoyo_parse_number_union(param, &e.minor))
463 error = -EINVAL;
464 else
465 error = tomoyo_update_domain(&e.head, sizeof(e), param,
466 tomoyo_same_mkdev_acl,
467 tomoyo_merge_mkdev_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900468 tomoyo_put_name_union(&e.name);
469 tomoyo_put_number_union(&e.mode);
470 tomoyo_put_number_union(&e.major);
471 tomoyo_put_number_union(&e.minor);
472 return error;
473}
474
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900475/**
476 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
477 *
478 * @a: Pointer to "struct tomoyo_acl_info".
479 * @b: Pointer to "struct tomoyo_acl_info".
480 *
481 * Returns true if @a == @b except permission bits, false otherwise.
482 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900483static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
484 const struct tomoyo_acl_info *b)
485{
486 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
487 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900488 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
489 tomoyo_same_name_union(&p1->name2, &p2->name2);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900490}
491
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900492/**
493 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
494 *
495 * @a: Pointer to "struct tomoyo_acl_info".
496 * @b: Pointer to "struct tomoyo_acl_info".
497 * @is_delete: True for @a &= ~@b, false for @a |= @b.
498 *
499 * Returns true if @a is empty, false otherwise.
500 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900501static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
502 struct tomoyo_acl_info *b,
503 const bool is_delete)
504{
505 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
506 ->perm;
507 u8 perm = *a_perm;
508 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
509 if (is_delete)
510 perm &= ~b_perm;
511 else
512 perm |= b_perm;
513 *a_perm = perm;
514 return !perm;
515}
516
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900517/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900518 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900519 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900520 * @perm: Permission.
521 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900522 *
523 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900524 *
525 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900526 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900527static int tomoyo_update_path2_acl(const u8 perm,
528 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900529{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900530 struct tomoyo_path2_acl e = {
531 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900532 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900533 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900534 int error;
535 if (!tomoyo_parse_name_union(param, &e.name1) ||
536 !tomoyo_parse_name_union(param, &e.name2))
537 error = -EINVAL;
538 else
539 error = tomoyo_update_domain(&e.head, sizeof(e), param,
540 tomoyo_same_path2_acl,
541 tomoyo_merge_path2_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900542 tomoyo_put_name_union(&e.name1);
543 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900544 return error;
545}
546
547/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900548 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900549 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900550 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900551 * @operation: Type of operation.
552 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900553 *
554 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900555 *
556 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900557 */
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900558static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
559 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900560{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900561 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900562
Tetsuo Handa57c25902010-06-03 20:38:44 +0900563 r->type = tomoyo_p2mac[operation];
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900564 r->mode = tomoyo_get_mode(r->domain->ns, r->profile, r->type);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900565 if (r->mode == TOMOYO_CONFIG_DISABLED)
566 return 0;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900567 r->param_type = TOMOYO_TYPE_PATH_ACL;
568 r->param.path.filename = filename;
569 r->param.path.operation = operation;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900570 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900571 tomoyo_check_acl(r, tomoyo_check_path_acl);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900572 error = tomoyo_audit_path_log(r);
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900573 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900574 return error;
575}
576
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900577/**
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900578 * tomoyo_execute_permission - Check permission for execute operation.
579 *
580 * @r: Pointer to "struct tomoyo_request_info".
581 * @filename: Filename to check.
582 *
583 * Returns 0 on success, negative value otherwise.
584 *
585 * Caller holds tomoyo_read_lock().
586 */
587int tomoyo_execute_permission(struct tomoyo_request_info *r,
588 const struct tomoyo_path_info *filename)
589{
590 /*
591 * Unlike other permission checks, this check is done regardless of
592 * profile mode settings in order to check for domain transition
593 * preference.
594 */
595 r->type = TOMOYO_MAC_FILE_EXECUTE;
596 r->mode = tomoyo_get_mode(r->domain->ns, r->profile, r->type);
597 r->param_type = TOMOYO_TYPE_PATH_ACL;
598 r->param.path.filename = filename;
599 r->param.path.operation = TOMOYO_TYPE_EXECUTE;
600 tomoyo_check_acl(r, tomoyo_check_path_acl);
601 r->ee->transition = r->matched_acl && r->matched_acl->cond ?
602 r->matched_acl->cond->transit : NULL;
603 if (r->mode != TOMOYO_CONFIG_DISABLED)
604 return tomoyo_audit_path_log(r);
605 return 0;
606}
607
608/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900609 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
610 *
611 * @a: Pointer to "struct tomoyo_acl_info".
612 * @b: Pointer to "struct tomoyo_acl_info".
613 *
614 * Returns true if @a == @b except permission bits, false otherwise.
615 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900616static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
617 const struct tomoyo_acl_info *b)
618{
619 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
620 head);
621 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
622 head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900623 return tomoyo_same_name_union(&p1->name, &p2->name) &&
624 tomoyo_same_number_union(&p1->number, &p2->number);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900625}
626
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900627/**
628 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
629 *
630 * @a: Pointer to "struct tomoyo_acl_info".
631 * @b: Pointer to "struct tomoyo_acl_info".
632 * @is_delete: True for @a &= ~@b, false for @a |= @b.
633 *
634 * Returns true if @a is empty, false otherwise.
635 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900636static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
637 struct tomoyo_acl_info *b,
638 const bool is_delete)
639{
640 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
641 head)->perm;
642 u8 perm = *a_perm;
643 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
644 ->perm;
645 if (is_delete)
646 perm &= ~b_perm;
647 else
648 perm |= b_perm;
649 *a_perm = perm;
650 return !perm;
651}
652
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900653/**
654 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
655 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900656 * @perm: Permission.
657 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900658 *
659 * Returns 0 on success, negative value otherwise.
660 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900661static int tomoyo_update_path_number_acl(const u8 perm,
662 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900663{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900664 struct tomoyo_path_number_acl e = {
665 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900666 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900667 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900668 int error;
669 if (!tomoyo_parse_name_union(param, &e.name) ||
670 !tomoyo_parse_number_union(param, &e.number))
671 error = -EINVAL;
672 else
673 error = tomoyo_update_domain(&e.head, sizeof(e), param,
674 tomoyo_same_path_number_acl,
675 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900676 tomoyo_put_name_union(&e.name);
677 tomoyo_put_number_union(&e.number);
678 return error;
679}
680
681/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900682 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
683 *
684 * @type: Type of operation.
685 * @path: Pointer to "struct path".
686 * @number: Number.
687 *
688 * Returns 0 on success, negative value otherwise.
689 */
690int tomoyo_path_number_perm(const u8 type, struct path *path,
691 unsigned long number)
692{
693 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900694 struct tomoyo_obj_info obj = {
695 .path1 = *path,
696 };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900697 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900698 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900699 int idx;
700
Tetsuo Handa57c25902010-06-03 20:38:44 +0900701 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900702 == TOMOYO_CONFIG_DISABLED || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900703 return 0;
704 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900705 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900706 goto out;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900707 r.obj = &obj;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900708 if (type == TOMOYO_TYPE_MKDIR)
709 tomoyo_add_slash(&buf);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900710 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
711 r.param.path_number.operation = type;
712 r.param.path_number.filename = &buf;
713 r.param.path_number.number = number;
714 do {
715 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
716 error = tomoyo_audit_path_number_log(&r);
717 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900718 kfree(buf.name);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900719 out:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900720 tomoyo_read_unlock(idx);
721 if (r.mode != TOMOYO_CONFIG_ENFORCING)
722 error = 0;
723 return error;
724}
725
726/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900727 * tomoyo_check_open_permission - Check permission for "read" and "write".
728 *
729 * @domain: Pointer to "struct tomoyo_domain_info".
730 * @path: Pointer to "struct path".
731 * @flag: Flags for open().
732 *
733 * Returns 0 on success, negative value otherwise.
734 */
735int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
736 struct path *path, const int flag)
737{
738 const u8 acc_mode = ACC_MODE(flag);
Tetsuo Handaeae61f32011-03-02 16:54:24 +0900739 int error = 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900740 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900741 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900742 struct tomoyo_obj_info obj = {
743 .path1 = *path,
744 };
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900745 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900746
Tetsuo Handa57c25902010-06-03 20:38:44 +0900747 buf.name = NULL;
748 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900749 idx = tomoyo_read_lock();
Tetsuo Handa7c759642011-06-26 23:15:31 +0900750 if (acc_mode &&
751 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900752 != TOMOYO_CONFIG_DISABLED) {
753 if (!tomoyo_get_realpath(&buf, path)) {
754 error = -ENOMEM;
755 goto out;
756 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900757 r.obj = &obj;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900758 if (acc_mode & MAY_READ)
759 error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900760 &buf);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900761 if (!error && (acc_mode & MAY_WRITE))
762 error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
763 TOMOYO_TYPE_APPEND :
764 TOMOYO_TYPE_WRITE,
765 &buf);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900766 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900767 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900768 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900769 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900770 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900771 error = 0;
772 return error;
773}
774
775/**
Tetsuo Handa7c759642011-06-26 23:15:31 +0900776 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900777 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900778 * @operation: Type of operation.
779 * @path: Pointer to "struct path".
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900780 * @target: Symlink's target if @operation is TOMOYO_TYPE_SYMLINK,
781 * NULL otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900782 *
783 * Returns 0 on success, negative value otherwise.
784 */
Al Viro3f7036a2015-03-08 19:28:30 -0400785int tomoyo_path_perm(const u8 operation, const struct path *path, const char *target)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900786{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900787 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900788 struct tomoyo_obj_info obj = {
789 .path1 = *path,
790 };
Tetsuo Handa7c759642011-06-26 23:15:31 +0900791 int error;
792 struct tomoyo_path_info buf;
793 bool is_enforce;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900794 struct tomoyo_path_info symlink_target;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900795 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796
Tetsuo Handa57c25902010-06-03 20:38:44 +0900797 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
798 == TOMOYO_CONFIG_DISABLED)
799 return 0;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900800 is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
801 error = -ENOMEM;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900802 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900803 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900804 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900805 goto out;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900806 r.obj = &obj;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900807 switch (operation) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900808 case TOMOYO_TYPE_RMDIR:
809 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900810 tomoyo_add_slash(&buf);
811 break;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900812 case TOMOYO_TYPE_SYMLINK:
813 symlink_target.name = tomoyo_encode(target);
814 if (!symlink_target.name)
815 goto out;
816 tomoyo_fill_path_info(&symlink_target);
817 obj.symlink_target = &symlink_target;
818 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900819 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900820 error = tomoyo_path_permission(&r, operation, &buf);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900821 if (operation == TOMOYO_TYPE_SYMLINK)
822 kfree(symlink_target.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900823 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900824 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900825 tomoyo_read_unlock(idx);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900826 if (!is_enforce)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900827 error = 0;
828 return error;
829}
830
831/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900832 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900833 *
834 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
835 * @path: Pointer to "struct path".
836 * @mode: Create mode.
837 * @dev: Device number.
838 *
839 * Returns 0 on success, negative value otherwise.
840 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900841int tomoyo_mkdev_perm(const u8 operation, struct path *path,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900842 const unsigned int mode, unsigned int dev)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900843{
844 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900845 struct tomoyo_obj_info obj = {
846 .path1 = *path,
847 };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900848 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900849 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900850 int idx;
851
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900852 if (tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900853 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900854 return 0;
855 idx = tomoyo_read_lock();
856 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900857 if (tomoyo_get_realpath(&buf, path)) {
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900858 r.obj = &obj;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900859 dev = new_decode_dev(dev);
Tetsuo Handa75093152010-06-16 16:23:55 +0900860 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900861 r.param.mkdev.filename = &buf;
862 r.param.mkdev.operation = operation;
863 r.param.mkdev.mode = mode;
864 r.param.mkdev.major = MAJOR(dev);
865 r.param.mkdev.minor = MINOR(dev);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900866 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
867 error = tomoyo_audit_mkdev_log(&r);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900868 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900869 }
870 tomoyo_read_unlock(idx);
871 if (r.mode != TOMOYO_CONFIG_ENFORCING)
872 error = 0;
873 return error;
874}
875
876/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900877 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900878 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900879 * @operation: Type of operation.
880 * @path1: Pointer to "struct path".
881 * @path2: Pointer to "struct path".
882 *
883 * Returns 0 on success, negative value otherwise.
884 */
Tetsuo Handa97d69312010-02-16 09:46:15 +0900885int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900886 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900887{
888 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900889 struct tomoyo_path_info buf1;
890 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900891 struct tomoyo_request_info r;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900892 struct tomoyo_obj_info obj = {
893 .path1 = *path1,
894 .path2 = *path2,
895 };
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900896 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900897
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900898 if (tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900899 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900900 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900901 buf1.name = NULL;
902 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900903 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900904 if (!tomoyo_get_realpath(&buf1, path1) ||
905 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900906 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900907 switch (operation) {
Tetsuo Handa57c25902010-06-03 20:38:44 +0900908 case TOMOYO_TYPE_RENAME:
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900909 case TOMOYO_TYPE_LINK:
David Howellse656a8e2015-01-29 12:02:32 +0000910 if (!d_is_dir(path1->dentry))
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900911 break;
912 /* fall through */
913 case TOMOYO_TYPE_PIVOT_ROOT:
914 tomoyo_add_slash(&buf1);
915 tomoyo_add_slash(&buf2);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900916 break;
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900917 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900918 r.obj = &obj;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900919 r.param_type = TOMOYO_TYPE_PATH2_ACL;
920 r.param.path2.operation = operation;
921 r.param.path2.filename1 = &buf1;
922 r.param.path2.filename2 = &buf2;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900923 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900924 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
925 error = tomoyo_audit_path2_log(&r);
926 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900927 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900928 kfree(buf1.name);
929 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900930 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900931 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900932 error = 0;
933 return error;
934}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900935
936/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900937 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900938 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900939 * @a: Pointer to "struct tomoyo_acl_info".
940 * @b: Pointer to "struct tomoyo_acl_info".
941 *
942 * Returns true if @a == @b, false otherwise.
943 */
944static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
945 const struct tomoyo_acl_info *b)
946{
947 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
948 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
949 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
950 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
951 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
952 tomoyo_same_number_union(&p1->flags, &p2->flags);
953}
954
955/**
956 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
957 *
958 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900959 *
960 * Returns 0 on success, negative value otherwise.
961 *
962 * Caller holds tomoyo_read_lock().
963 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900964static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900965{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900966 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
967 int error;
968 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
969 !tomoyo_parse_name_union(param, &e.dir_name) ||
970 !tomoyo_parse_name_union(param, &e.fs_type) ||
971 !tomoyo_parse_number_union(param, &e.flags))
972 error = -EINVAL;
973 else
974 error = tomoyo_update_domain(&e.head, sizeof(e), param,
975 tomoyo_same_mount_acl, NULL);
976 tomoyo_put_name_union(&e.dev_name);
977 tomoyo_put_name_union(&e.dir_name);
978 tomoyo_put_name_union(&e.fs_type);
979 tomoyo_put_number_union(&e.flags);
980 return error;
981}
982
983/**
984 * tomoyo_write_file - Update file related list.
985 *
986 * @param: Pointer to "struct tomoyo_acl_param".
987 *
988 * Returns 0 on success, negative value otherwise.
989 *
990 * Caller holds tomoyo_read_lock().
991 */
992int tomoyo_write_file(struct tomoyo_acl_param *param)
993{
994 u16 perm = 0;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900995 u8 type;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900996 const char *operation = tomoyo_read_token(param);
997 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
998 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
999 perm |= 1 << type;
1000 if (perm)
1001 return tomoyo_update_path_acl(perm, param);
1002 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001003 if (tomoyo_permstr(operation,
1004 tomoyo_mac_keywords[tomoyo_pp2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001005 perm |= 1 << type;
1006 if (perm)
1007 return tomoyo_update_path2_acl(perm, param);
1008 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
1009 if (tomoyo_permstr(operation,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001010 tomoyo_mac_keywords[tomoyo_pn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001011 perm |= 1 << type;
1012 if (perm)
1013 return tomoyo_update_path_number_acl(perm, param);
1014 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001015 if (tomoyo_permstr(operation,
1016 tomoyo_mac_keywords[tomoyo_pnnn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001017 perm |= 1 << type;
1018 if (perm)
1019 return tomoyo_update_mkdev_acl(perm, param);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001020 if (tomoyo_permstr(operation,
1021 tomoyo_mac_keywords[TOMOYO_MAC_FILE_MOUNT]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001022 return tomoyo_update_mount_acl(param);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001023 return -EINVAL;
1024}