blob: 0673a69b1320f0d49831f7f3dca95f3df3e4ba7e [file] [log] [blame]
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001/*
2 * security/tomoyo/file.c
3 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09004 * Pathname restriction functions.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09005 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takedab69a54e2009-02-05 17:18:14 +09007 */
8
9#include "common.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kentaro Takedab69a54e2009-02-05 17:18:14 +090011
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090012/* Keyword array for operations with one pathname. */
Tetsuo Handa71c28232010-06-16 16:26:38 +090013const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090014 [TOMOYO_TYPE_EXECUTE] = "execute",
15 [TOMOYO_TYPE_READ] = "read",
16 [TOMOYO_TYPE_WRITE] = "write",
Tetsuo Handa7c759642011-06-26 23:15:31 +090017 [TOMOYO_TYPE_APPEND] = "append",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090018 [TOMOYO_TYPE_UNLINK] = "unlink",
Tetsuo Handa7c759642011-06-26 23:15:31 +090019 [TOMOYO_TYPE_GETATTR] = "getattr",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090020 [TOMOYO_TYPE_RMDIR] = "rmdir",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090021 [TOMOYO_TYPE_TRUNCATE] = "truncate",
22 [TOMOYO_TYPE_SYMLINK] = "symlink",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090023 [TOMOYO_TYPE_CHROOT] = "chroot",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090024 [TOMOYO_TYPE_UMOUNT] = "unmount",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090025};
26
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090027/* Keyword array for operations with one pathname and three numbers. */
Tetsuo Handa71c28232010-06-16 16:26:38 +090028const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090029 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
30 [TOMOYO_TYPE_MKCHAR] = "mkchar",
31};
32
33/* Keyword array for operations with two pathnames. */
Tetsuo Handa71c28232010-06-16 16:26:38 +090034const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090035 [TOMOYO_TYPE_LINK] = "link",
36 [TOMOYO_TYPE_RENAME] = "rename",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090037 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090038};
39
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090040/* Keyword array for operations with one pathname and one number. */
Tetsuo Handa71c28232010-06-16 16:26:38 +090041const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090042 [TOMOYO_TYPE_CREATE] = "create",
43 [TOMOYO_TYPE_MKDIR] = "mkdir",
44 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
45 [TOMOYO_TYPE_MKSOCK] = "mksock",
46 [TOMOYO_TYPE_IOCTL] = "ioctl",
47 [TOMOYO_TYPE_CHMOD] = "chmod",
48 [TOMOYO_TYPE_CHOWN] = "chown",
49 [TOMOYO_TYPE_CHGRP] = "chgrp",
50};
51
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090052/*
53 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
54 */
Tetsuo Handa57c25902010-06-03 20:38:44 +090055static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090056 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
57 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
58 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa7c759642011-06-26 23:15:31 +090059 [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa57c25902010-06-03 20:38:44 +090060 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +090061 [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +090062 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
63 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
64 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +090065 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
66 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
67};
68
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090069/*
70 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
71 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090072const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090073 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
74 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
75};
76
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090077/*
78 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
79 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090080const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090081 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
82 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
83 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
84};
85
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090086/*
87 * Mapping table from "enum tomoyo_path_number_acl_index" to
88 * "enum tomoyo_mac_index".
89 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090090const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090091 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
92 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
93 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
94 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
95 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
96 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
97 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
98 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
99};
100
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900101/**
102 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
103 *
104 * @ptr: Pointer to "struct tomoyo_name_union".
105 *
106 * Returns nothing.
107 */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900108void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
109{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900110 tomoyo_put_group(ptr->group);
111 tomoyo_put_name(ptr->filename);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900112}
113
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900114/**
115 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
116 *
117 * @name: Pointer to "struct tomoyo_path_info".
118 * @ptr: Pointer to "struct tomoyo_name_union".
119 *
120 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
121 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900122const struct tomoyo_path_info *
123tomoyo_compare_name_union(const struct tomoyo_path_info *name,
124 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900125{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900126 if (ptr->group)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900127 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900128 if (tomoyo_path_matches_pattern(name, ptr->filename))
129 return ptr->filename;
130 return NULL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900131}
132
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900133/**
134 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
135 *
136 * @ptr: Pointer to "struct tomoyo_number_union".
137 *
138 * Returns nothing.
139 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900140void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
141{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900142 tomoyo_put_group(ptr->group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900143}
144
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900145/**
146 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
147 *
148 * @value: Number to check.
149 * @ptr: Pointer to "struct tomoyo_number_union".
150 *
151 * Returns true if @value matches @ptr, false otherwise.
152 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900153bool tomoyo_compare_number_union(const unsigned long value,
154 const struct tomoyo_number_union *ptr)
155{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900156 if (ptr->group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900157 return tomoyo_number_matches_group(value, value, ptr->group);
158 return value >= ptr->values[0] && value <= ptr->values[1];
159}
160
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900161/**
162 * tomoyo_add_slash - Add trailing '/' if needed.
163 *
164 * @buf: Pointer to "struct tomoyo_path_info".
165 *
166 * Returns nothing.
167 *
168 * @buf must be generated by tomoyo_encode() because this function does not
169 * allocate memory for adding '/'.
170 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900171static void tomoyo_add_slash(struct tomoyo_path_info *buf)
172{
173 if (buf->is_dir)
174 return;
175 /*
176 * This is OK because tomoyo_encode() reserves space for appending "/".
177 */
178 strcat((char *) buf->name, "/");
179 tomoyo_fill_path_info(buf);
180}
181
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900182/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900183 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900184 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900185 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900186 * @path: Pointer to "struct path".
187 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900188 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900189 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900190static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900191{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900192 buf->name = tomoyo_realpath_from_path(path);
193 if (buf->name) {
194 tomoyo_fill_path_info(buf);
195 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900196 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900197 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900198}
199
Tetsuo Handa99a85252010-06-16 16:22:51 +0900200/**
201 * tomoyo_audit_path_log - Audit path request log.
202 *
203 * @r: Pointer to "struct tomoyo_request_info".
204 *
205 * Returns 0 on success, negative value otherwise.
206 */
207static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
208{
209 const char *operation = tomoyo_path_keyword[r->param.path.operation];
210 const struct tomoyo_path_info *filename = r->param.path.filename;
211 if (r->granted)
212 return 0;
213 tomoyo_warn_log(r, "%s %s", operation, filename->name);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900214 return tomoyo_supervisor(r, "file %s %s\n", operation, filename->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900215}
216
217/**
218 * tomoyo_audit_path2_log - Audit path/path request log.
219 *
220 * @r: Pointer to "struct tomoyo_request_info".
221 *
222 * Returns 0 on success, negative value otherwise.
223 */
224static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
225{
226 const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
227 const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
228 const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
229 if (r->granted)
230 return 0;
231 tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
232 filename2->name);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900233 return tomoyo_supervisor(r, "file %s %s %s\n", operation,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900234 filename1->name, filename2->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900235}
236
237/**
238 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
239 *
240 * @r: Pointer to "struct tomoyo_request_info".
241 *
242 * Returns 0 on success, negative value otherwise.
243 */
244static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
245{
Tetsuo Handa71c28232010-06-16 16:26:38 +0900246 const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
Tetsuo Handa99a85252010-06-16 16:22:51 +0900247 const struct tomoyo_path_info *filename = r->param.mkdev.filename;
248 const unsigned int major = r->param.mkdev.major;
249 const unsigned int minor = r->param.mkdev.minor;
250 const unsigned int mode = r->param.mkdev.mode;
251 if (r->granted)
252 return 0;
253 tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
254 major, minor);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900255 return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n", operation,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900256 filename->name, mode, major, minor);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900257}
258
259/**
260 * tomoyo_audit_path_number_log - Audit path/number request log.
261 *
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900262 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa99a85252010-06-16 16:22:51 +0900263 *
264 * Returns 0 on success, negative value otherwise.
265 */
266static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
267{
268 const u8 type = r->param.path_number.operation;
269 u8 radix;
270 const struct tomoyo_path_info *filename = r->param.path_number.filename;
271 const char *operation = tomoyo_path_number_keyword[type];
272 char buffer[64];
273 if (r->granted)
274 return 0;
275 switch (type) {
276 case TOMOYO_TYPE_CREATE:
277 case TOMOYO_TYPE_MKDIR:
278 case TOMOYO_TYPE_MKFIFO:
279 case TOMOYO_TYPE_MKSOCK:
280 case TOMOYO_TYPE_CHMOD:
281 radix = TOMOYO_VALUE_TYPE_OCTAL;
282 break;
283 case TOMOYO_TYPE_IOCTL:
284 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
285 break;
286 default:
287 radix = TOMOYO_VALUE_TYPE_DECIMAL;
288 break;
289 }
290 tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
291 radix);
292 tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900293 return tomoyo_supervisor(r, "file %s %s %s\n", operation,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900294 filename->name, buffer);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900295}
296
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900297/**
298 * tomoyo_check_path_acl - Check permission for path operation.
299 *
300 * @r: Pointer to "struct tomoyo_request_info".
301 * @ptr: Pointer to "struct tomoyo_acl_info".
302 *
303 * Returns true if granted, false otherwise.
304 *
305 * To be able to use wildcard for domain transition, this function sets
306 * matching entry on success. Since the caller holds tomoyo_read_lock(),
307 * it is safe to set matching entry.
308 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900309static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900310 const struct tomoyo_acl_info *ptr)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900311{
Tetsuo Handa99a85252010-06-16 16:22:51 +0900312 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
313 head);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900314 if (acl->perm & (1 << r->param.path.operation)) {
315 r->param.path.matched_path =
316 tomoyo_compare_name_union(r->param.path.filename,
317 &acl->name);
318 return r->param.path.matched_path != NULL;
319 }
320 return false;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900321}
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900322
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900323/**
324 * tomoyo_check_path_number_acl - Check permission for path number operation.
325 *
326 * @r: Pointer to "struct tomoyo_request_info".
327 * @ptr: Pointer to "struct tomoyo_acl_info".
328 *
329 * Returns true if granted, false otherwise.
330 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900331static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900332 const struct tomoyo_acl_info *ptr)
333{
334 const struct tomoyo_path_number_acl *acl =
335 container_of(ptr, typeof(*acl), head);
336 return (acl->perm & (1 << r->param.path_number.operation)) &&
337 tomoyo_compare_number_union(r->param.path_number.number,
338 &acl->number) &&
339 tomoyo_compare_name_union(r->param.path_number.filename,
340 &acl->name);
341}
342
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900343/**
344 * tomoyo_check_path2_acl - Check permission for path path operation.
345 *
346 * @r: Pointer to "struct tomoyo_request_info".
347 * @ptr: Pointer to "struct tomoyo_acl_info".
348 *
349 * Returns true if granted, false otherwise.
350 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900351static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900352 const struct tomoyo_acl_info *ptr)
353{
354 const struct tomoyo_path2_acl *acl =
355 container_of(ptr, typeof(*acl), head);
356 return (acl->perm & (1 << r->param.path2.operation)) &&
357 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
358 && tomoyo_compare_name_union(r->param.path2.filename2,
359 &acl->name2);
360}
361
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900362/**
363 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
364 *
365 * @r: Pointer to "struct tomoyo_request_info".
366 * @ptr: Pointer to "struct tomoyo_acl_info".
367 *
368 * Returns true if granted, false otherwise.
369 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900370static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900371 const struct tomoyo_acl_info *ptr)
Tetsuo Handa99a85252010-06-16 16:22:51 +0900372{
Tetsuo Handa75093152010-06-16 16:23:55 +0900373 const struct tomoyo_mkdev_acl *acl =
Tetsuo Handa99a85252010-06-16 16:22:51 +0900374 container_of(ptr, typeof(*acl), head);
375 return (acl->perm & (1 << r->param.mkdev.operation)) &&
376 tomoyo_compare_number_union(r->param.mkdev.mode,
377 &acl->mode) &&
378 tomoyo_compare_number_union(r->param.mkdev.major,
379 &acl->major) &&
380 tomoyo_compare_number_union(r->param.mkdev.minor,
381 &acl->minor) &&
382 tomoyo_compare_name_union(r->param.mkdev.filename,
383 &acl->name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900384}
385
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900386/**
387 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
388 *
389 * @a: Pointer to "struct tomoyo_acl_info".
390 * @b: Pointer to "struct tomoyo_acl_info".
391 *
392 * Returns true if @a == @b except permission bits, false otherwise.
393 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900394static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
395 const struct tomoyo_acl_info *b)
396{
397 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
398 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900399 return tomoyo_same_name_union(&p1->name, &p2->name);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900400}
401
Tetsuo Handa7c759642011-06-26 23:15:31 +0900402/**
403 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
404 *
405 * @a: Pointer to "struct tomoyo_acl_info".
406 * @b: Pointer to "struct tomoyo_acl_info".
407 * @is_delete: True for @a &= ~@b, false for @a |= @b.
408 *
409 * Returns true if @a is empty, false otherwise.
410 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900411static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
412 struct tomoyo_acl_info *b,
413 const bool is_delete)
414{
415 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
416 ->perm;
417 u16 perm = *a_perm;
418 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900419 if (is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900420 perm &= ~b_perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900421 else
Tetsuo Handa237ab452010-06-12 20:46:22 +0900422 perm |= b_perm;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900423 *a_perm = perm;
424 return !perm;
425}
426
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900427/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900428 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900429 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900430 * @perm: Permission.
431 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900432 *
433 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900434 *
435 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900436 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900437static int tomoyo_update_path_acl(const u16 perm,
438 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900439{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900440 struct tomoyo_path_acl e = {
441 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900442 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900443 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900444 int error;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900445 if (!tomoyo_parse_name_union(param, &e.name))
446 error = -EINVAL;
447 else
448 error = tomoyo_update_domain(&e.head, sizeof(e), param,
449 tomoyo_same_path_acl,
450 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900451 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900452 return error;
453}
454
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900455/**
456 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
457 *
458 * @a: Pointer to "struct tomoyo_acl_info".
459 * @b: Pointer to "struct tomoyo_acl_info".
460 *
461 * Returns true if @a == @b except permission bits, false otherwise.
462 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900463static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900464 const struct tomoyo_acl_info *b)
465{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900466 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
467 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
468 return tomoyo_same_name_union(&p1->name, &p2->name) &&
469 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
470 tomoyo_same_number_union(&p1->major, &p2->major) &&
471 tomoyo_same_number_union(&p1->minor, &p2->minor);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900472}
473
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900474/**
475 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
476 *
477 * @a: Pointer to "struct tomoyo_acl_info".
478 * @b: Pointer to "struct tomoyo_acl_info".
479 * @is_delete: True for @a &= ~@b, false for @a |= @b.
480 *
481 * Returns true if @a is empty, false otherwise.
482 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900483static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900484 struct tomoyo_acl_info *b,
485 const bool is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900486{
Tetsuo Handa75093152010-06-16 16:23:55 +0900487 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900488 head)->perm;
489 u8 perm = *a_perm;
Tetsuo Handa75093152010-06-16 16:23:55 +0900490 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900491 ->perm;
492 if (is_delete)
493 perm &= ~b_perm;
494 else
495 perm |= b_perm;
496 *a_perm = perm;
497 return !perm;
498}
499
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900500/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900501 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900502 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900503 * @perm: Permission.
504 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900505 *
506 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900507 *
508 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900509 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900510static int tomoyo_update_mkdev_acl(const u8 perm,
511 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900512{
Tetsuo Handa75093152010-06-16 16:23:55 +0900513 struct tomoyo_mkdev_acl e = {
514 .head.type = TOMOYO_TYPE_MKDEV_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900515 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900516 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900517 int error;
518 if (!tomoyo_parse_name_union(param, &e.name) ||
519 !tomoyo_parse_number_union(param, &e.mode) ||
520 !tomoyo_parse_number_union(param, &e.major) ||
521 !tomoyo_parse_number_union(param, &e.minor))
522 error = -EINVAL;
523 else
524 error = tomoyo_update_domain(&e.head, sizeof(e), param,
525 tomoyo_same_mkdev_acl,
526 tomoyo_merge_mkdev_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900527 tomoyo_put_name_union(&e.name);
528 tomoyo_put_number_union(&e.mode);
529 tomoyo_put_number_union(&e.major);
530 tomoyo_put_number_union(&e.minor);
531 return error;
532}
533
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900534/**
535 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
536 *
537 * @a: Pointer to "struct tomoyo_acl_info".
538 * @b: Pointer to "struct tomoyo_acl_info".
539 *
540 * Returns true if @a == @b except permission bits, false otherwise.
541 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900542static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
543 const struct tomoyo_acl_info *b)
544{
545 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
546 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900547 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
548 tomoyo_same_name_union(&p1->name2, &p2->name2);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900549}
550
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900551/**
552 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
553 *
554 * @a: Pointer to "struct tomoyo_acl_info".
555 * @b: Pointer to "struct tomoyo_acl_info".
556 * @is_delete: True for @a &= ~@b, false for @a |= @b.
557 *
558 * Returns true if @a is empty, false otherwise.
559 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900560static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
561 struct tomoyo_acl_info *b,
562 const bool is_delete)
563{
564 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
565 ->perm;
566 u8 perm = *a_perm;
567 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
568 if (is_delete)
569 perm &= ~b_perm;
570 else
571 perm |= b_perm;
572 *a_perm = perm;
573 return !perm;
574}
575
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900576/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900577 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900578 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900579 * @perm: Permission.
580 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900581 *
582 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900583 *
584 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900585 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900586static int tomoyo_update_path2_acl(const u8 perm,
587 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900588{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900589 struct tomoyo_path2_acl e = {
590 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900591 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900592 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900593 int error;
594 if (!tomoyo_parse_name_union(param, &e.name1) ||
595 !tomoyo_parse_name_union(param, &e.name2))
596 error = -EINVAL;
597 else
598 error = tomoyo_update_domain(&e.head, sizeof(e), param,
599 tomoyo_same_path2_acl,
600 tomoyo_merge_path2_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900601 tomoyo_put_name_union(&e.name1);
602 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900603 return error;
604}
605
606/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900607 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900608 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900609 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900610 * @operation: Type of operation.
611 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900612 *
613 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900614 *
615 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900616 */
Tetsuo Handa05336de2010-06-16 16:20:24 +0900617int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
618 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900619{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900620 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900621
Tetsuo Handa57c25902010-06-03 20:38:44 +0900622 r->type = tomoyo_p2mac[operation];
623 r->mode = tomoyo_get_mode(r->profile, r->type);
624 if (r->mode == TOMOYO_CONFIG_DISABLED)
625 return 0;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900626 r->param_type = TOMOYO_TYPE_PATH_ACL;
627 r->param.path.filename = filename;
628 r->param.path.operation = operation;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900629 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900630 tomoyo_check_acl(r, tomoyo_check_path_acl);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900631 error = tomoyo_audit_path_log(r);
Tetsuo Handa05336de2010-06-16 16:20:24 +0900632 /*
633 * Do not retry for execute request, for alias may have
634 * changed.
635 */
636 } while (error == TOMOYO_RETRY_REQUEST &&
637 operation != TOMOYO_TYPE_EXECUTE);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900638 return error;
639}
640
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900641/**
642 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
643 *
644 * @a: Pointer to "struct tomoyo_acl_info".
645 * @b: Pointer to "struct tomoyo_acl_info".
646 *
647 * Returns true if @a == @b except permission bits, false otherwise.
648 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900649static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
650 const struct tomoyo_acl_info *b)
651{
652 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
653 head);
654 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
655 head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900656 return tomoyo_same_name_union(&p1->name, &p2->name) &&
657 tomoyo_same_number_union(&p1->number, &p2->number);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900658}
659
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900660/**
661 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
662 *
663 * @a: Pointer to "struct tomoyo_acl_info".
664 * @b: Pointer to "struct tomoyo_acl_info".
665 * @is_delete: True for @a &= ~@b, false for @a |= @b.
666 *
667 * Returns true if @a is empty, false otherwise.
668 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900669static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
670 struct tomoyo_acl_info *b,
671 const bool is_delete)
672{
673 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
674 head)->perm;
675 u8 perm = *a_perm;
676 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
677 ->perm;
678 if (is_delete)
679 perm &= ~b_perm;
680 else
681 perm |= b_perm;
682 *a_perm = perm;
683 return !perm;
684}
685
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900686/**
687 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
688 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900689 * @perm: Permission.
690 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900691 *
692 * Returns 0 on success, negative value otherwise.
693 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900694static int tomoyo_update_path_number_acl(const u8 perm,
695 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900696{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900697 struct tomoyo_path_number_acl e = {
698 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900699 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900700 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900701 int error;
702 if (!tomoyo_parse_name_union(param, &e.name) ||
703 !tomoyo_parse_number_union(param, &e.number))
704 error = -EINVAL;
705 else
706 error = tomoyo_update_domain(&e.head, sizeof(e), param,
707 tomoyo_same_path_number_acl,
708 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900709 tomoyo_put_name_union(&e.name);
710 tomoyo_put_number_union(&e.number);
711 return error;
712}
713
714/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900715 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
716 *
717 * @type: Type of operation.
718 * @path: Pointer to "struct path".
719 * @number: Number.
720 *
721 * Returns 0 on success, negative value otherwise.
722 */
723int tomoyo_path_number_perm(const u8 type, struct path *path,
724 unsigned long number)
725{
726 struct tomoyo_request_info r;
727 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900728 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900729 int idx;
730
Tetsuo Handa57c25902010-06-03 20:38:44 +0900731 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
732 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900733 return 0;
734 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900735 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900736 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900737 if (type == TOMOYO_TYPE_MKDIR)
738 tomoyo_add_slash(&buf);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900739 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
740 r.param.path_number.operation = type;
741 r.param.path_number.filename = &buf;
742 r.param.path_number.number = number;
743 do {
744 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
745 error = tomoyo_audit_path_number_log(&r);
746 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900747 kfree(buf.name);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900748 out:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900749 tomoyo_read_unlock(idx);
750 if (r.mode != TOMOYO_CONFIG_ENFORCING)
751 error = 0;
752 return error;
753}
754
755/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900756 * tomoyo_check_open_permission - Check permission for "read" and "write".
757 *
758 * @domain: Pointer to "struct tomoyo_domain_info".
759 * @path: Pointer to "struct path".
760 * @flag: Flags for open().
761 *
762 * Returns 0 on success, negative value otherwise.
763 */
764int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
765 struct path *path, const int flag)
766{
767 const u8 acc_mode = ACC_MODE(flag);
Tetsuo Handaeae61f32011-03-02 16:54:24 +0900768 int error = 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900769 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900770 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900771 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900772
Tetsuo Handa7c759642011-06-26 23:15:31 +0900773 if (!path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900774 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900775 buf.name = NULL;
776 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900777 idx = tomoyo_read_lock();
Tetsuo Handa7c759642011-06-26 23:15:31 +0900778 if (acc_mode &&
779 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900780 != TOMOYO_CONFIG_DISABLED) {
781 if (!tomoyo_get_realpath(&buf, path)) {
782 error = -ENOMEM;
783 goto out;
784 }
Tetsuo Handa7c759642011-06-26 23:15:31 +0900785 if (acc_mode & MAY_READ)
786 error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900787 &buf);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900788 if (!error && (acc_mode & MAY_WRITE))
789 error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
790 TOMOYO_TYPE_APPEND :
791 TOMOYO_TYPE_WRITE,
792 &buf);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900793 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900794 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900795 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900796 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900797 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900798 error = 0;
799 return error;
800}
801
802/**
Tetsuo Handa7c759642011-06-26 23:15:31 +0900803 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900804 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900805 * @operation: Type of operation.
806 * @path: Pointer to "struct path".
807 *
808 * Returns 0 on success, negative value otherwise.
809 */
Tetsuo Handa97d69312010-02-16 09:46:15 +0900810int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900811{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900812 struct tomoyo_request_info r;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900813 int error;
814 struct tomoyo_path_info buf;
815 bool is_enforce;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900816 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900817
Tetsuo Handa57c25902010-06-03 20:38:44 +0900818 if (!path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900819 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900820 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
821 == TOMOYO_CONFIG_DISABLED)
822 return 0;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900823 is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
824 error = -ENOMEM;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900825 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900826 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900827 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900828 goto out;
829 switch (operation) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900830 case TOMOYO_TYPE_RMDIR:
831 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900832 tomoyo_add_slash(&buf);
833 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900834 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900835 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900836 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900837 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900838 tomoyo_read_unlock(idx);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900839 if (!is_enforce)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900840 error = 0;
841 return error;
842}
843
844/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900845 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900846 *
847 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
848 * @path: Pointer to "struct path".
849 * @mode: Create mode.
850 * @dev: Device number.
851 *
852 * Returns 0 on success, negative value otherwise.
853 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900854int tomoyo_mkdev_perm(const u8 operation, struct path *path,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900855 const unsigned int mode, unsigned int dev)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900856{
857 struct tomoyo_request_info r;
858 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900859 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900860 int idx;
861
Tetsuo Handa57c25902010-06-03 20:38:44 +0900862 if (!path->mnt ||
863 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
864 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900865 return 0;
866 idx = tomoyo_read_lock();
867 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900868 if (tomoyo_get_realpath(&buf, path)) {
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900869 dev = new_decode_dev(dev);
Tetsuo Handa75093152010-06-16 16:23:55 +0900870 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900871 r.param.mkdev.filename = &buf;
872 r.param.mkdev.operation = operation;
873 r.param.mkdev.mode = mode;
874 r.param.mkdev.major = MAJOR(dev);
875 r.param.mkdev.minor = MINOR(dev);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900876 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
877 error = tomoyo_audit_mkdev_log(&r);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900878 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900879 }
880 tomoyo_read_unlock(idx);
881 if (r.mode != TOMOYO_CONFIG_ENFORCING)
882 error = 0;
883 return error;
884}
885
886/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900887 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900888 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900889 * @operation: Type of operation.
890 * @path1: Pointer to "struct path".
891 * @path2: Pointer to "struct path".
892 *
893 * Returns 0 on success, negative value otherwise.
894 */
Tetsuo Handa97d69312010-02-16 09:46:15 +0900895int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900896 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900897{
898 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900899 struct tomoyo_path_info buf1;
900 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900901 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900902 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900903
Tetsuo Handa57c25902010-06-03 20:38:44 +0900904 if (!path1->mnt || !path2->mnt ||
905 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
906 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900907 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900908 buf1.name = NULL;
909 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900910 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900911 if (!tomoyo_get_realpath(&buf1, path1) ||
912 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900913 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900914 switch (operation) {
915 struct dentry *dentry;
916 case TOMOYO_TYPE_RENAME:
917 case TOMOYO_TYPE_LINK:
918 dentry = path1->dentry;
919 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
920 break;
921 /* fall through */
922 case TOMOYO_TYPE_PIVOT_ROOT:
923 tomoyo_add_slash(&buf1);
924 tomoyo_add_slash(&buf2);
925 break;
926 }
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900927 r.param_type = TOMOYO_TYPE_PATH2_ACL;
928 r.param.path2.operation = operation;
929 r.param.path2.filename1 = &buf1;
930 r.param.path2.filename2 = &buf2;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900931 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900932 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
933 error = tomoyo_audit_path2_log(&r);
934 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900935 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900936 kfree(buf1.name);
937 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900938 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900939 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900940 error = 0;
941 return error;
942}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900943
944/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900945 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900946 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900947 * @a: Pointer to "struct tomoyo_acl_info".
948 * @b: Pointer to "struct tomoyo_acl_info".
949 *
950 * Returns true if @a == @b, false otherwise.
951 */
952static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
953 const struct tomoyo_acl_info *b)
954{
955 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
956 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
957 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
958 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
959 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
960 tomoyo_same_number_union(&p1->flags, &p2->flags);
961}
962
963/**
964 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
965 *
966 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900967 *
968 * Returns 0 on success, negative value otherwise.
969 *
970 * Caller holds tomoyo_read_lock().
971 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900972static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900973{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900974 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
975 int error;
976 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
977 !tomoyo_parse_name_union(param, &e.dir_name) ||
978 !tomoyo_parse_name_union(param, &e.fs_type) ||
979 !tomoyo_parse_number_union(param, &e.flags))
980 error = -EINVAL;
981 else
982 error = tomoyo_update_domain(&e.head, sizeof(e), param,
983 tomoyo_same_mount_acl, NULL);
984 tomoyo_put_name_union(&e.dev_name);
985 tomoyo_put_name_union(&e.dir_name);
986 tomoyo_put_name_union(&e.fs_type);
987 tomoyo_put_number_union(&e.flags);
988 return error;
989}
990
991/**
992 * tomoyo_write_file - Update file related list.
993 *
994 * @param: Pointer to "struct tomoyo_acl_param".
995 *
996 * Returns 0 on success, negative value otherwise.
997 *
998 * Caller holds tomoyo_read_lock().
999 */
1000int tomoyo_write_file(struct tomoyo_acl_param *param)
1001{
1002 u16 perm = 0;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001003 u8 type;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001004 const char *operation = tomoyo_read_token(param);
1005 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
1006 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
1007 perm |= 1 << type;
1008 if (perm)
1009 return tomoyo_update_path_acl(perm, param);
1010 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
1011 if (tomoyo_permstr(operation, tomoyo_path2_keyword[type]))
1012 perm |= 1 << type;
1013 if (perm)
1014 return tomoyo_update_path2_acl(perm, param);
1015 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
1016 if (tomoyo_permstr(operation,
1017 tomoyo_path_number_keyword[type]))
1018 perm |= 1 << type;
1019 if (perm)
1020 return tomoyo_update_path_number_acl(perm, param);
1021 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
1022 if (tomoyo_permstr(operation, tomoyo_mkdev_keyword[type]))
1023 perm |= 1 << type;
1024 if (perm)
1025 return tomoyo_update_mkdev_acl(perm, param);
1026 if (tomoyo_permstr(operation, "mount"))
1027 return tomoyo_update_mount_acl(param);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001028 return -EINVAL;
1029}