blob: 8015719926d5e48cd3ed5a15fd362a29a4c22f5d [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 Handa7ef61232010-02-16 08:03:30 +090013static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
14 [TOMOYO_TYPE_READ_WRITE] = "read/write",
15 [TOMOYO_TYPE_EXECUTE] = "execute",
16 [TOMOYO_TYPE_READ] = "read",
17 [TOMOYO_TYPE_WRITE] = "write",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090018 [TOMOYO_TYPE_UNLINK] = "unlink",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090019 [TOMOYO_TYPE_RMDIR] = "rmdir",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090020 [TOMOYO_TYPE_TRUNCATE] = "truncate",
21 [TOMOYO_TYPE_SYMLINK] = "symlink",
22 [TOMOYO_TYPE_REWRITE] = "rewrite",
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. */
28static const char *tomoyo_path_number3_keyword
29[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
30 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
31 [TOMOYO_TYPE_MKCHAR] = "mkchar",
32};
33
34/* Keyword array for operations with two pathnames. */
Tetsuo Handa7ef61232010-02-16 08:03:30 +090035static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090036 [TOMOYO_TYPE_LINK] = "link",
37 [TOMOYO_TYPE_RENAME] = "rename",
Tetsuo Handa7ef61232010-02-16 08:03:30 +090038 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
Kentaro Takedab69a54e2009-02-05 17:18:14 +090039};
40
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090041/* Keyword array for operations with one pathname and one number. */
42static const char *tomoyo_path_number_keyword
43[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
44 [TOMOYO_TYPE_CREATE] = "create",
45 [TOMOYO_TYPE_MKDIR] = "mkdir",
46 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
47 [TOMOYO_TYPE_MKSOCK] = "mksock",
48 [TOMOYO_TYPE_IOCTL] = "ioctl",
49 [TOMOYO_TYPE_CHMOD] = "chmod",
50 [TOMOYO_TYPE_CHOWN] = "chown",
51 [TOMOYO_TYPE_CHGRP] = "chgrp",
52};
53
Tetsuo Handa57c25902010-06-03 20:38:44 +090054static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
55 [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
56 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
57 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
58 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
59 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
60 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
61 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
62 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
63 [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
64 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
65 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
66};
67
68static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
69 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
70 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
71};
72
73static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
74 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
75 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
76 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
77};
78
79static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
80 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
81 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
82 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
83 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
84 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
85 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
86 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
87 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
88};
89
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090090void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
91{
92 if (!ptr)
93 return;
94 if (ptr->is_group)
95 tomoyo_put_path_group(ptr->group);
96 else
97 tomoyo_put_name(ptr->filename);
98}
99
100bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
101 const struct tomoyo_name_union *ptr)
102{
103 if (ptr->is_group)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900104 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900105 return tomoyo_path_matches_pattern(name, ptr->filename);
106}
107
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900108void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
109{
110 if (ptr && ptr->is_group)
111 tomoyo_put_number_group(ptr->group);
112}
113
114bool tomoyo_compare_number_union(const unsigned long value,
115 const struct tomoyo_number_union *ptr)
116{
117 if (ptr->is_group)
118 return tomoyo_number_matches_group(value, value, ptr->group);
119 return value >= ptr->values[0] && value <= ptr->values[1];
120}
121
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900122/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900123 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900124 *
125 * @operation: Type of operation.
126 *
127 * Returns the name of single path operation.
128 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900129const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900130{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900131 return (operation < TOMOYO_MAX_PATH_OPERATION)
132 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900133}
134
135/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900136 * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
137 *
138 * @operation: Type of operation.
139 *
140 * Returns the name of path/number/number/number operation.
141 */
142const char *tomoyo_path_number32keyword(const u8 operation)
143{
144 return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
145 ? tomoyo_path_number3_keyword[operation] : NULL;
146}
147
148/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900149 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900150 *
151 * @operation: Type of operation.
152 *
153 * Returns the name of double path operation.
154 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900155const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900156{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900157 return (operation < TOMOYO_MAX_PATH2_OPERATION)
158 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900159}
160
161/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900162 * tomoyo_path_number2keyword - Get the name of path/number operations.
163 *
164 * @operation: Type of operation.
165 *
166 * Returns the name of path/number operation.
167 */
168const char *tomoyo_path_number2keyword(const u8 operation)
169{
170 return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
171 ? tomoyo_path_number_keyword[operation] : NULL;
172}
173
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900174static void tomoyo_add_slash(struct tomoyo_path_info *buf)
175{
176 if (buf->is_dir)
177 return;
178 /*
179 * This is OK because tomoyo_encode() reserves space for appending "/".
180 */
181 strcat((char *) buf->name, "/");
182 tomoyo_fill_path_info(buf);
183}
184
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900185/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900186 * tomoyo_strendswith - Check whether the token ends with the given token.
187 *
188 * @name: The token to check.
189 * @tail: The token to find.
190 *
191 * Returns true if @name ends with @tail, false otherwise.
192 */
193static bool tomoyo_strendswith(const char *name, const char *tail)
194{
195 int len;
196
197 if (!name || !tail)
198 return false;
199 len = strlen(name) - strlen(tail);
200 return len >= 0 && !strcmp(name + len, tail);
201}
202
203/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900204 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900205 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900206 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900207 * @path: Pointer to "struct path".
208 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900209 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900210 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900211static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900212{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900213 buf->name = tomoyo_realpath_from_path(path);
214 if (buf->name) {
215 tomoyo_fill_path_info(buf);
216 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900217 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900218 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900219}
220
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900221static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
222 const char *filename2,
223 struct tomoyo_domain_info *const domain,
224 const bool is_delete);
225static int tomoyo_update_path_acl(const u8 type, const char *filename,
226 struct tomoyo_domain_info *const domain,
227 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900228
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900229/*
230 * tomoyo_globally_readable_list is used for holding list of pathnames which
231 * are by default allowed to be open()ed for reading by any process.
232 *
233 * An entry is added by
234 *
235 * # echo 'allow_read /lib/libc-2.5.so' > \
236 * /sys/kernel/security/tomoyo/exception_policy
237 *
238 * and is deleted by
239 *
240 * # echo 'delete allow_read /lib/libc-2.5.so' > \
241 * /sys/kernel/security/tomoyo/exception_policy
242 *
243 * and all entries are retrieved by
244 *
245 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
246 *
247 * In the example above, any process is allowed to
248 * open("/lib/libc-2.5.so", O_RDONLY).
249 * One exception is, if the domain which current process belongs to is marked
250 * as "ignore_global_allow_read", current process can't do so unless explicitly
251 * given "allow_read /lib/libc-2.5.so" to the domain which current process
252 * belongs to.
253 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900254LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900255
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900256static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a,
257 const struct tomoyo_acl_head *b)
258{
259 return container_of(a, struct tomoyo_globally_readable_file_entry,
260 head)->filename ==
261 container_of(b, struct tomoyo_globally_readable_file_entry,
262 head)->filename;
263}
264
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900265/**
266 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
267 *
268 * @filename: Filename unconditionally permitted to open() for reading.
269 * @is_delete: True if it is a delete request.
270 *
271 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900272 *
273 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900274 */
275static int tomoyo_update_globally_readable_entry(const char *filename,
276 const bool is_delete)
277{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900278 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900279 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900280
Tetsuo Handa3f629632010-06-03 20:37:26 +0900281 if (!tomoyo_is_correct_word(filename))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900282 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900283 e.filename = tomoyo_get_name(filename);
284 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900285 return -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900286 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
287 &tomoyo_globally_readable_list,
288 tomoyo_same_globally_readable);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900289 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900290 return error;
291}
292
293/**
294 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
295 *
296 * @filename: The filename to check.
297 *
298 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900299 *
300 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900301 */
302static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
303 filename)
304{
305 struct tomoyo_globally_readable_file_entry *ptr;
306 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900307
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900308 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
309 head.list) {
310 if (!ptr->head.is_deleted &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900311 tomoyo_path_matches_pattern(filename, ptr->filename)) {
312 found = true;
313 break;
314 }
315 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900316 return found;
317}
318
319/**
320 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
321 *
322 * @data: String to parse.
323 * @is_delete: True if it is a delete request.
324 *
325 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900326 *
327 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900328 */
329int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
330{
331 return tomoyo_update_globally_readable_entry(data, is_delete);
332}
333
334/**
335 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
336 *
337 * @head: Pointer to "struct tomoyo_io_buffer".
338 *
339 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900340 *
341 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900342 */
343bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
344{
345 struct list_head *pos;
346 bool done = true;
347
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900348 list_for_each_cookie(pos, head->read_var2,
349 &tomoyo_globally_readable_list) {
350 struct tomoyo_globally_readable_file_entry *ptr;
351 ptr = list_entry(pos,
352 struct tomoyo_globally_readable_file_entry,
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900353 head.list);
354 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900355 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900356 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
357 ptr->filename->name);
358 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900359 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900360 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900361 return done;
362}
363
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900364/* tomoyo_pattern_list is used for holding list of pathnames which are used for
365 * converting pathnames to pathname patterns during learning mode.
366 *
367 * An entry is added by
368 *
369 * # echo 'file_pattern /proc/\$/mounts' > \
370 * /sys/kernel/security/tomoyo/exception_policy
371 *
372 * and is deleted by
373 *
374 * # echo 'delete file_pattern /proc/\$/mounts' > \
375 * /sys/kernel/security/tomoyo/exception_policy
376 *
377 * and all entries are retrieved by
378 *
379 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
380 *
381 * In the example above, if a process which belongs to a domain which is in
382 * learning mode requested open("/proc/1/mounts", O_RDONLY),
383 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
384 * process belongs to.
385 *
386 * It is not a desirable behavior that we have to use /proc/\$/ instead of
387 * /proc/self/ when current process needs to access only current process's
388 * information. As of now, LSM version of TOMOYO is using __d_path() for
389 * calculating pathname. Non LSM version of TOMOYO is using its own function
390 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
391 * current process from accessing other process's information.
392 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900393LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900394
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900395static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a,
396 const struct tomoyo_acl_head *b)
397{
398 return container_of(a, struct tomoyo_pattern_entry, head)->pattern ==
399 container_of(b, struct tomoyo_pattern_entry, head)->pattern;
400}
401
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900402/**
403 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
404 *
405 * @pattern: Pathname pattern.
406 * @is_delete: True if it is a delete request.
407 *
408 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900409 *
410 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900411 */
412static int tomoyo_update_file_pattern_entry(const char *pattern,
413 const bool is_delete)
414{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900415 struct tomoyo_pattern_entry e = { };
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900416 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900417
Tetsuo Handa3f629632010-06-03 20:37:26 +0900418 if (!tomoyo_is_correct_word(pattern))
419 return -EINVAL;
420 e.pattern = tomoyo_get_name(pattern);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900421 if (!e.pattern)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900422 return -ENOMEM;
423 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
424 &tomoyo_pattern_list,
425 tomoyo_same_pattern);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900426 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900427 return error;
428}
429
430/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900431 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900432 *
433 * @filename: The filename to find patterned pathname.
434 *
435 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900436 *
437 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900438 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900439const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900440{
441 struct tomoyo_pattern_entry *ptr;
442 const struct tomoyo_path_info *pattern = NULL;
443
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900444 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, head.list) {
445 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900446 continue;
447 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
448 continue;
449 pattern = ptr->pattern;
450 if (tomoyo_strendswith(pattern->name, "/\\*")) {
451 /* Do nothing. Try to find the better match. */
452 } else {
453 /* This would be the better match. Use this. */
454 break;
455 }
456 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900457 if (pattern)
458 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900459 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900460}
461
462/**
463 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
464 *
465 * @data: String to parse.
466 * @is_delete: True if it is a delete request.
467 *
468 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900469 *
470 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900471 */
472int tomoyo_write_pattern_policy(char *data, const bool is_delete)
473{
474 return tomoyo_update_file_pattern_entry(data, is_delete);
475}
476
477/**
478 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
479 *
480 * @head: Pointer to "struct tomoyo_io_buffer".
481 *
482 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900483 *
484 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900485 */
486bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
487{
488 struct list_head *pos;
489 bool done = true;
490
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900491 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
492 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900493 ptr = list_entry(pos, struct tomoyo_pattern_entry, head.list);
494 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900495 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900496 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
497 "%s\n", ptr->pattern->name);
498 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900499 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900500 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900501 return done;
502}
503
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900504/*
505 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
506 * default forbidden to modify already written content of a file.
507 *
508 * An entry is added by
509 *
510 * # echo 'deny_rewrite /var/log/messages' > \
511 * /sys/kernel/security/tomoyo/exception_policy
512 *
513 * and is deleted by
514 *
515 * # echo 'delete deny_rewrite /var/log/messages' > \
516 * /sys/kernel/security/tomoyo/exception_policy
517 *
518 * and all entries are retrieved by
519 *
520 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
521 *
522 * In the example above, if a process requested to rewrite /var/log/messages ,
523 * the process can't rewrite unless the domain which that process belongs to
524 * has "allow_rewrite /var/log/messages" entry.
525 *
526 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
527 * when we want to allow rewriting already unlink()ed file. As of now,
528 * LSM version of TOMOYO is using __d_path() for calculating pathname.
529 * Non LSM version of TOMOYO is using its own function which doesn't append
530 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
531 * need to worry whether the file is already unlink()ed or not.
532 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900533LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900534
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900535static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a,
536 const struct tomoyo_acl_head *b)
537{
538 return container_of(a, struct tomoyo_no_rewrite_entry, head)->pattern
539 == container_of(b, struct tomoyo_no_rewrite_entry, head)
540 ->pattern;
541}
542
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900543/**
544 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
545 *
546 * @pattern: Pathname pattern that are not rewritable by default.
547 * @is_delete: True if it is a delete request.
548 *
549 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900550 *
551 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900552 */
553static int tomoyo_update_no_rewrite_entry(const char *pattern,
554 const bool is_delete)
555{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900556 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900557 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900558
Tetsuo Handa3f629632010-06-03 20:37:26 +0900559 if (!tomoyo_is_correct_word(pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900560 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900561 e.pattern = tomoyo_get_name(pattern);
562 if (!e.pattern)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900563 return -ENOMEM;
564 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
565 &tomoyo_no_rewrite_list,
566 tomoyo_same_no_rewrite);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900567 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900568 return error;
569}
570
571/**
572 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
573 *
574 * @filename: Filename to check.
575 *
576 * Returns true if @filename is specified by "deny_rewrite" directive,
577 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900578 *
579 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900580 */
581static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
582{
583 struct tomoyo_no_rewrite_entry *ptr;
584 bool found = false;
585
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900586 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, head.list) {
587 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900588 continue;
589 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
590 continue;
591 found = true;
592 break;
593 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900594 return found;
595}
596
597/**
598 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
599 *
600 * @data: String to parse.
601 * @is_delete: True if it is a delete request.
602 *
603 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900604 *
605 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900606 */
607int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
608{
609 return tomoyo_update_no_rewrite_entry(data, is_delete);
610}
611
612/**
613 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
614 *
615 * @head: Pointer to "struct tomoyo_io_buffer".
616 *
617 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900618 *
619 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900620 */
621bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
622{
623 struct list_head *pos;
624 bool done = true;
625
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900626 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
627 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900628 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry,
629 head.list);
630 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900631 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900632 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
633 "%s\n", ptr->pattern->name);
634 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900635 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900636 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900637 return done;
638}
639
640/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900641 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900642 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900643 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900644 * @filename: Filename to check.
645 * @perm: Permission.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900646 *
647 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900648 *
649 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900650 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900651static int tomoyo_path_acl(const struct tomoyo_request_info *r,
652 const struct tomoyo_path_info *filename,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900653 const u32 perm)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900654{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900655 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900656 struct tomoyo_acl_info *ptr;
657 int error = -EPERM;
658
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900659 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900660 struct tomoyo_path_acl *acl;
661 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900662 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900663 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900664 if (!(acl->perm & perm) ||
Tetsuo Handa3f629632010-06-03 20:37:26 +0900665 !tomoyo_compare_name_union(filename, &acl->name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900666 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900667 error = 0;
668 break;
669 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900670 return error;
671}
672
673/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900674 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900675 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900676 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900677 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900678 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900679 *
680 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900681 *
682 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900683 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900684static int tomoyo_file_perm(struct tomoyo_request_info *r,
685 const struct tomoyo_path_info *filename,
686 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900687{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900688 const char *msg = "<unknown>";
689 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900690 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900691
692 if (!filename)
693 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900694
695 if (mode == 6) {
696 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
697 perm = 1 << TOMOYO_TYPE_READ_WRITE;
698 } else if (mode == 4) {
699 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
700 perm = 1 << TOMOYO_TYPE_READ;
701 } else if (mode == 2) {
702 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
703 perm = 1 << TOMOYO_TYPE_WRITE;
704 } else if (mode == 1) {
705 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
706 perm = 1 << TOMOYO_TYPE_EXECUTE;
707 } else
708 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900709 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900710 error = tomoyo_path_acl(r, filename, perm);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900711 if (error && mode == 4 && !r->domain->ignore_global_allow_read
712 && tomoyo_is_globally_readable_file(filename))
713 error = 0;
714 if (!error)
715 break;
716 tomoyo_warn_log(r, "%s %s", msg, filename->name);
717 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900718 tomoyo_file_pattern(filename));
719 /*
720 * Do not retry for execute request, for alias may have
721 * changed.
722 */
723 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
724 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900725 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900726 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900727}
728
Tetsuo Handa237ab452010-06-12 20:46:22 +0900729static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
730 const struct tomoyo_acl_info *b)
731{
732 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
733 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
734 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
735 tomoyo_is_same_name_union(&p1->name, &p2->name);
736}
737
738static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
739 struct tomoyo_acl_info *b,
740 const bool is_delete)
741{
742 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
743 ->perm;
744 u16 perm = *a_perm;
745 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
746 if (is_delete) {
747 perm &= ~b_perm;
748 if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
749 perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
750 else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
751 perm &= ~TOMOYO_RW_MASK;
752 } else {
753 perm |= b_perm;
754 if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
755 perm |= (1 << TOMOYO_TYPE_READ_WRITE);
756 else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
757 perm |= TOMOYO_RW_MASK;
758 }
759 *a_perm = perm;
760 return !perm;
761}
762
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900763/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900764 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900765 *
766 * @type: Type of operation.
767 * @filename: Filename.
768 * @domain: Pointer to "struct tomoyo_domain_info".
769 * @is_delete: True if it is a delete request.
770 *
771 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900772 *
773 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900774 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900775static int tomoyo_update_path_acl(const u8 type, const char *filename,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900776 struct tomoyo_domain_info * const domain,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900777 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900778{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900779 struct tomoyo_path_acl e = {
780 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900781 .perm = 1 << type
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900782 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900783 int error;
784 if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
785 e.perm |= TOMOYO_RW_MASK;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900786 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900787 return -EINVAL;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900788 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
789 tomoyo_same_path_acl,
790 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900791 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900792 return error;
793}
794
Tetsuo Handa237ab452010-06-12 20:46:22 +0900795static bool tomoyo_same_path_number3_acl(const struct tomoyo_acl_info *a,
796 const struct tomoyo_acl_info *b)
797{
798 const struct tomoyo_path_number3_acl *p1 = container_of(a, typeof(*p1),
799 head);
800 const struct tomoyo_path_number3_acl *p2 = container_of(b, typeof(*p2),
801 head);
802 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
803 && tomoyo_is_same_name_union(&p1->name, &p2->name)
804 && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
805 && tomoyo_is_same_number_union(&p1->major, &p2->major)
806 && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
807}
808
809static bool tomoyo_merge_path_number3_acl(struct tomoyo_acl_info *a,
810 struct tomoyo_acl_info *b,
811 const bool is_delete)
812{
813 u8 *const a_perm = &container_of(a, struct tomoyo_path_number3_acl,
814 head)->perm;
815 u8 perm = *a_perm;
816 const u8 b_perm = container_of(b, struct tomoyo_path_number3_acl, head)
817 ->perm;
818 if (is_delete)
819 perm &= ~b_perm;
820 else
821 perm |= b_perm;
822 *a_perm = perm;
823 return !perm;
824}
825
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900826/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900827 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
828 *
829 * @type: Type of operation.
830 * @filename: Filename.
831 * @mode: Create mode.
832 * @major: Device major number.
833 * @minor: Device minor number.
834 * @domain: Pointer to "struct tomoyo_domain_info".
835 * @is_delete: True if it is a delete request.
836 *
837 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900838 *
839 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900840 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900841static int tomoyo_update_path_number3_acl(const u8 type, const char *filename,
842 char *mode, char *major, char *minor,
843 struct tomoyo_domain_info * const
844 domain, const bool is_delete)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900845{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900846 struct tomoyo_path_number3_acl e = {
847 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900848 .perm = 1 << type
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900849 };
850 int error = is_delete ? -ENOENT : -ENOMEM;
851 if (!tomoyo_parse_name_union(filename, &e.name) ||
852 !tomoyo_parse_number_union(mode, &e.mode) ||
853 !tomoyo_parse_number_union(major, &e.major) ||
854 !tomoyo_parse_number_union(minor, &e.minor))
855 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900856 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
857 tomoyo_same_path_number3_acl,
858 tomoyo_merge_path_number3_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900859 out:
860 tomoyo_put_name_union(&e.name);
861 tomoyo_put_number_union(&e.mode);
862 tomoyo_put_number_union(&e.major);
863 tomoyo_put_number_union(&e.minor);
864 return error;
865}
866
Tetsuo Handa237ab452010-06-12 20:46:22 +0900867static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
868 const struct tomoyo_acl_info *b)
869{
870 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
871 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
872 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
873 && tomoyo_is_same_name_union(&p1->name1, &p2->name1)
874 && tomoyo_is_same_name_union(&p1->name2, &p2->name2);
875}
876
877static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
878 struct tomoyo_acl_info *b,
879 const bool is_delete)
880{
881 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
882 ->perm;
883 u8 perm = *a_perm;
884 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
885 if (is_delete)
886 perm &= ~b_perm;
887 else
888 perm |= b_perm;
889 *a_perm = perm;
890 return !perm;
891}
892
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900893/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900894 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900895 *
896 * @type: Type of operation.
897 * @filename1: First filename.
898 * @filename2: Second filename.
899 * @domain: Pointer to "struct tomoyo_domain_info".
900 * @is_delete: True if it is a delete request.
901 *
902 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900903 *
904 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900905 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900906static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
907 const char *filename2,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900908 struct tomoyo_domain_info * const domain,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900909 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900910{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900911 struct tomoyo_path2_acl e = {
912 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900913 .perm = 1 << type
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900914 };
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900915 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900916 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
917 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900918 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900919 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
920 tomoyo_same_path2_acl,
921 tomoyo_merge_path2_acl);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900922 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900923 tomoyo_put_name_union(&e.name1);
924 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900925 return error;
926}
927
928/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900929 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
930 *
931 * @r: Pointer to "struct tomoyo_request_info".
932 * @filename: Filename to check.
933 * @perm: Permission.
934 * @mode: Create mode.
935 * @major: Device major number.
936 * @minor: Device minor number.
937 *
938 * Returns 0 on success, -EPERM otherwise.
939 *
940 * Caller holds tomoyo_read_lock().
941 */
942static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
943 const struct tomoyo_path_info *filename,
944 const u16 perm, const unsigned int mode,
945 const unsigned int major,
946 const unsigned int minor)
947{
948 struct tomoyo_domain_info *domain = r->domain;
949 struct tomoyo_acl_info *ptr;
950 int error = -EPERM;
951 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
952 struct tomoyo_path_number3_acl *acl;
953 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
954 continue;
955 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
956 if (!tomoyo_compare_number_union(mode, &acl->mode))
957 continue;
958 if (!tomoyo_compare_number_union(major, &acl->major))
959 continue;
960 if (!tomoyo_compare_number_union(minor, &acl->minor))
961 continue;
962 if (!(acl->perm & perm))
963 continue;
964 if (!tomoyo_compare_name_union(filename, &acl->name))
965 continue;
966 error = 0;
967 break;
968 }
969 return error;
970}
971
972/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900973 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900974 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900975 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900976 * @type: Type of operation.
977 * @filename1: First filename to check.
978 * @filename2: Second filename to check.
979 *
980 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900981 *
982 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900983 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900984static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900985 const struct tomoyo_path_info *filename1,
986 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900987{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900988 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900989 struct tomoyo_acl_info *ptr;
990 const u8 perm = 1 << type;
991 int error = -EPERM;
992
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900993 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900994 struct tomoyo_path2_acl *acl;
995 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900996 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900997 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900998 if (!(acl->perm & perm))
999 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001000 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001001 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001002 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001003 continue;
1004 error = 0;
1005 break;
1006 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001007 return error;
1008}
1009
1010/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001011 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001012 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001013 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001014 * @operation: Type of operation.
1015 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001016 *
1017 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001018 *
1019 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001020 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001021static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1022 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001023{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001024 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001025 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001026
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001027 next:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001028 r->type = tomoyo_p2mac[operation];
1029 r->mode = tomoyo_get_mode(r->profile, r->type);
1030 if (r->mode == TOMOYO_CONFIG_DISABLED)
1031 return 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001032 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +09001033 error = tomoyo_path_acl(r, filename, 1 << operation);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001034 if (!error)
1035 break;
1036 msg = tomoyo_path2keyword(operation);
1037 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1038 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1039 tomoyo_file_pattern(filename));
1040 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001041 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001042 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043 /*
1044 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1045 * we need to check "allow_rewrite" permission if the filename is
1046 * specified by "deny_rewrite" keyword.
1047 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001048 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001049 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001050 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001051 goto next;
1052 }
1053 return error;
1054}
1055
1056/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001057 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1058 *
1059 * @r: Pointer to "struct tomoyo_request_info".
1060 * @type: Operation.
1061 * @filename: Filename to check.
1062 * @number: Number.
1063 *
1064 * Returns 0 on success, -EPERM otherwise.
1065 *
1066 * Caller holds tomoyo_read_lock().
1067 */
1068static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1069 const struct tomoyo_path_info *filename,
1070 const unsigned long number)
1071{
1072 struct tomoyo_domain_info *domain = r->domain;
1073 struct tomoyo_acl_info *ptr;
1074 const u8 perm = 1 << type;
1075 int error = -EPERM;
1076 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1077 struct tomoyo_path_number_acl *acl;
1078 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1079 continue;
1080 acl = container_of(ptr, struct tomoyo_path_number_acl,
1081 head);
1082 if (!(acl->perm & perm) ||
1083 !tomoyo_compare_number_union(number, &acl->number) ||
1084 !tomoyo_compare_name_union(filename, &acl->name))
1085 continue;
1086 error = 0;
1087 break;
1088 }
1089 return error;
1090}
1091
Tetsuo Handa237ab452010-06-12 20:46:22 +09001092static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
1093 const struct tomoyo_acl_info *b)
1094{
1095 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
1096 head);
1097 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
1098 head);
1099 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1100 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1101 && tomoyo_is_same_number_union(&p1->number, &p2->number);
1102}
1103
1104static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
1105 struct tomoyo_acl_info *b,
1106 const bool is_delete)
1107{
1108 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
1109 head)->perm;
1110 u8 perm = *a_perm;
1111 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
1112 ->perm;
1113 if (is_delete)
1114 perm &= ~b_perm;
1115 else
1116 perm |= b_perm;
1117 *a_perm = perm;
1118 return !perm;
1119}
1120
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001121/**
1122 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1123 *
1124 * @type: Type of operation.
1125 * @filename: Filename.
1126 * @number: Number.
1127 * @domain: Pointer to "struct tomoyo_domain_info".
1128 * @is_delete: True if it is a delete request.
1129 *
1130 * Returns 0 on success, negative value otherwise.
1131 */
Tetsuo Handa237ab452010-06-12 20:46:22 +09001132static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
1133 char *number,
1134 struct tomoyo_domain_info * const
1135 domain,
1136 const bool is_delete)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001137{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001138 struct tomoyo_path_number_acl e = {
1139 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +09001140 .perm = 1 << type
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001141 };
1142 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001143 if (!tomoyo_parse_name_union(filename, &e.name))
1144 return -EINVAL;
1145 if (!tomoyo_parse_number_union(number, &e.number))
1146 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001147 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
1148 tomoyo_same_path_number_acl,
1149 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001150 out:
1151 tomoyo_put_name_union(&e.name);
1152 tomoyo_put_number_union(&e.number);
1153 return error;
1154}
1155
1156/**
1157 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1158 *
1159 * @r: Pointer to "strct tomoyo_request_info".
1160 * @filename: Filename to check.
1161 * @number: Number.
1162 *
1163 * Returns 0 on success, negative value otherwise.
1164 *
1165 * Caller holds tomoyo_read_lock().
1166 */
1167static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1168 const u8 type,
1169 const struct tomoyo_path_info *filename,
1170 const unsigned long number)
1171{
1172 char buffer[64];
1173 int error;
1174 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001175 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001176
1177 if (!filename)
1178 return 0;
1179 switch (type) {
1180 case TOMOYO_TYPE_CREATE:
1181 case TOMOYO_TYPE_MKDIR:
1182 case TOMOYO_TYPE_MKFIFO:
1183 case TOMOYO_TYPE_MKSOCK:
1184 case TOMOYO_TYPE_CHMOD:
1185 radix = TOMOYO_VALUE_TYPE_OCTAL;
1186 break;
1187 case TOMOYO_TYPE_IOCTL:
1188 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1189 break;
1190 default:
1191 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1192 break;
1193 }
1194 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001195 do {
1196 error = tomoyo_path_number_acl(r, type, filename, number);
1197 if (!error)
1198 break;
1199 msg = tomoyo_path_number2keyword(type);
1200 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1201 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1202 tomoyo_file_pattern(filename),
1203 buffer);
1204 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001205 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1206 error = 0;
1207 return error;
1208}
1209
1210/**
1211 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1212 *
1213 * @type: Type of operation.
1214 * @path: Pointer to "struct path".
1215 * @number: Number.
1216 *
1217 * Returns 0 on success, negative value otherwise.
1218 */
1219int tomoyo_path_number_perm(const u8 type, struct path *path,
1220 unsigned long number)
1221{
1222 struct tomoyo_request_info r;
1223 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001224 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001225 int idx;
1226
Tetsuo Handa57c25902010-06-03 20:38:44 +09001227 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
1228 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001229 return 0;
1230 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001231 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001232 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001233 if (type == TOMOYO_TYPE_MKDIR)
1234 tomoyo_add_slash(&buf);
1235 error = tomoyo_path_number_perm2(&r, type, &buf, number);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001236 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001237 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001238 tomoyo_read_unlock(idx);
1239 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1240 error = 0;
1241 return error;
1242}
1243
1244/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001245 * tomoyo_check_exec_perm - Check permission for "execute".
1246 *
Tetsuo Handa57c25902010-06-03 20:38:44 +09001247 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001248 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001249 *
1250 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001251 *
1252 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001253 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001254int tomoyo_check_exec_perm(struct tomoyo_request_info *r,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001255 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001256{
Tetsuo Handa57c25902010-06-03 20:38:44 +09001257 if (r->mode == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001258 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001259 return tomoyo_file_perm(r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001260}
1261
1262/**
1263 * tomoyo_check_open_permission - Check permission for "read" and "write".
1264 *
1265 * @domain: Pointer to "struct tomoyo_domain_info".
1266 * @path: Pointer to "struct path".
1267 * @flag: Flags for open().
1268 *
1269 * Returns 0 on success, negative value otherwise.
1270 */
1271int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1272 struct path *path, const int flag)
1273{
1274 const u8 acc_mode = ACC_MODE(flag);
1275 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001276 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001277 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001278 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001279
Tetsuo Handa57c25902010-06-03 20:38:44 +09001280 if (!path->mnt ||
1281 (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001282 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001283 buf.name = NULL;
1284 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001285 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001286 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001287 goto out;
1288 error = 0;
1289 /*
1290 * If the filename is specified by "deny_rewrite" keyword,
1291 * we need to check "allow_rewrite" permission when the filename is not
1292 * opened for append mode or the filename is truncated at open time.
1293 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001294 if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
1295 && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
1296 != TOMOYO_CONFIG_DISABLED) {
1297 if (!tomoyo_get_realpath(&buf, path)) {
1298 error = -ENOMEM;
1299 goto out;
1300 }
1301 if (tomoyo_is_no_rewrite_file(&buf))
1302 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
1303 &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001304 }
Tetsuo Handa57c25902010-06-03 20:38:44 +09001305 if (!error && acc_mode &&
1306 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
1307 != TOMOYO_CONFIG_DISABLED) {
1308 if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
1309 error = -ENOMEM;
1310 goto out;
1311 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001312 error = tomoyo_file_perm(&r, &buf, acc_mode);
Tetsuo Handa57c25902010-06-03 20:38:44 +09001313 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001314 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001315 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001316 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001317 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001318 error = 0;
1319 return error;
1320}
1321
1322/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001323 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001324 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001325 * @operation: Type of operation.
1326 * @path: Pointer to "struct path".
1327 *
1328 * Returns 0 on success, negative value otherwise.
1329 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001330int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001331{
1332 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001333 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001334 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001335 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001336
Tetsuo Handa57c25902010-06-03 20:38:44 +09001337 if (!path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001338 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001339 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
1340 == TOMOYO_CONFIG_DISABLED)
1341 return 0;
1342 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001343 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001344 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001345 goto out;
1346 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001347 case TOMOYO_TYPE_REWRITE:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001348 if (!tomoyo_is_no_rewrite_file(&buf)) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001349 error = 0;
1350 goto out;
1351 }
1352 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001353 case TOMOYO_TYPE_RMDIR:
1354 case TOMOYO_TYPE_CHROOT:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001355 case TOMOYO_TYPE_UMOUNT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001356 tomoyo_add_slash(&buf);
1357 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001358 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001359 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001360 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001361 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001362 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001363 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001364 error = 0;
1365 return error;
1366}
1367
1368/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001369 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1370 *
1371 * @r: Pointer to "struct tomoyo_request_info".
1372 * @operation: Type of operation.
1373 * @filename: Filename to check.
1374 * @mode: Create mode.
1375 * @dev: Device number.
1376 *
1377 * Returns 0 on success, negative value otherwise.
1378 *
1379 * Caller holds tomoyo_read_lock().
1380 */
1381static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1382 const u8 operation,
1383 const struct tomoyo_path_info *filename,
1384 const unsigned int mode,
1385 const unsigned int dev)
1386{
1387 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001388 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001389 const unsigned int major = MAJOR(dev);
1390 const unsigned int minor = MINOR(dev);
1391
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001392 do {
1393 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1394 mode, major, minor);
1395 if (!error)
1396 break;
1397 msg = tomoyo_path_number32keyword(operation);
1398 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1399 mode, major, minor);
1400 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1401 tomoyo_file_pattern(filename), mode,
1402 major, minor);
1403 } while (error == TOMOYO_RETRY_REQUEST);
1404 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001405 error = 0;
1406 return error;
1407}
1408
1409/**
1410 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1411 *
1412 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1413 * @path: Pointer to "struct path".
1414 * @mode: Create mode.
1415 * @dev: Device number.
1416 *
1417 * Returns 0 on success, negative value otherwise.
1418 */
1419int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1420 const unsigned int mode, unsigned int dev)
1421{
1422 struct tomoyo_request_info r;
1423 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001424 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001425 int idx;
1426
Tetsuo Handa57c25902010-06-03 20:38:44 +09001427 if (!path->mnt ||
1428 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
1429 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001430 return 0;
1431 idx = tomoyo_read_lock();
1432 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001433 if (tomoyo_get_realpath(&buf, path)) {
1434 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001435 new_decode_dev(dev));
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001436 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001437 }
1438 tomoyo_read_unlock(idx);
1439 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1440 error = 0;
1441 return error;
1442}
1443
1444/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001445 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001446 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001447 * @operation: Type of operation.
1448 * @path1: Pointer to "struct path".
1449 * @path2: Pointer to "struct path".
1450 *
1451 * Returns 0 on success, negative value otherwise.
1452 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001453int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001454 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001455{
1456 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001457 const char *msg;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001458 struct tomoyo_path_info buf1;
1459 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001460 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001461 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001462
Tetsuo Handa57c25902010-06-03 20:38:44 +09001463 if (!path1->mnt || !path2->mnt ||
1464 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
1465 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001466 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001467 buf1.name = NULL;
1468 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001469 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001470 if (!tomoyo_get_realpath(&buf1, path1) ||
1471 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001472 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001473 switch (operation) {
1474 struct dentry *dentry;
1475 case TOMOYO_TYPE_RENAME:
1476 case TOMOYO_TYPE_LINK:
1477 dentry = path1->dentry;
1478 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
1479 break;
1480 /* fall through */
1481 case TOMOYO_TYPE_PIVOT_ROOT:
1482 tomoyo_add_slash(&buf1);
1483 tomoyo_add_slash(&buf2);
1484 break;
1485 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001486 do {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001487 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001488 if (!error)
1489 break;
1490 msg = tomoyo_path22keyword(operation);
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001491 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001492 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001493 tomoyo_file_pattern(&buf1),
1494 tomoyo_file_pattern(&buf2));
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001495 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001496 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001497 kfree(buf1.name);
1498 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001499 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001500 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001501 error = 0;
1502 return error;
1503}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001504
1505/**
1506 * tomoyo_write_file_policy - Update file related list.
1507 *
1508 * @data: String to parse.
1509 * @domain: Pointer to "struct tomoyo_domain_info".
1510 * @is_delete: True if it is a delete request.
1511 *
1512 * Returns 0 on success, negative value otherwise.
1513 *
1514 * Caller holds tomoyo_read_lock().
1515 */
1516int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1517 const bool is_delete)
1518{
1519 char *w[5];
1520 u8 type;
1521 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1522 return -EINVAL;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001523 if (strncmp(w[0], "allow_", 6))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001524 goto out;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001525 w[0] += 6;
1526 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1527 if (strcmp(w[0], tomoyo_path_keyword[type]))
1528 continue;
1529 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1530 }
1531 if (!w[2][0])
1532 goto out;
1533 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1534 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1535 continue;
1536 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1537 is_delete);
1538 }
1539 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1540 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1541 continue;
1542 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1543 is_delete);
1544 }
1545 if (!w[3][0] || !w[4][0])
1546 goto out;
1547 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1548 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1549 continue;
1550 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1551 w[4], domain, is_delete);
1552 }
1553 out:
1554 return -EINVAL;
1555}