blob: 8e51348d022ec4ef08b1469d5bcecf9e7c5b5083 [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
256/**
257 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
258 *
259 * @filename: Filename unconditionally permitted to open() for reading.
260 * @is_delete: True if it is a delete request.
261 *
262 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900263 *
264 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900265 */
266static int tomoyo_update_globally_readable_entry(const char *filename,
267 const bool is_delete)
268{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900269 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900270 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900271 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900272
Tetsuo Handa3f629632010-06-03 20:37:26 +0900273 if (!tomoyo_is_correct_word(filename))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900274 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900275 e.filename = tomoyo_get_name(filename);
276 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900277 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900278 if (mutex_lock_interruptible(&tomoyo_policy_lock))
279 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900280 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900281 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900282 continue;
283 ptr->is_deleted = is_delete;
284 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900285 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900286 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900287 if (!is_delete && error) {
288 struct tomoyo_globally_readable_file_entry *entry =
289 tomoyo_commit_ok(&e, sizeof(e));
290 if (entry) {
291 list_add_tail_rcu(&entry->list,
292 &tomoyo_globally_readable_list);
293 error = 0;
294 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900295 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900296 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900297 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900298 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900299 return error;
300}
301
302/**
303 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
304 *
305 * @filename: The filename to check.
306 *
307 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900308 *
309 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900310 */
311static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
312 filename)
313{
314 struct tomoyo_globally_readable_file_entry *ptr;
315 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900316
317 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900318 if (!ptr->is_deleted &&
319 tomoyo_path_matches_pattern(filename, ptr->filename)) {
320 found = true;
321 break;
322 }
323 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900324 return found;
325}
326
327/**
328 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
329 *
330 * @data: String to parse.
331 * @is_delete: True if it is a delete request.
332 *
333 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900334 *
335 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900336 */
337int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
338{
339 return tomoyo_update_globally_readable_entry(data, is_delete);
340}
341
342/**
343 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
344 *
345 * @head: Pointer to "struct tomoyo_io_buffer".
346 *
347 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900348 *
349 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900350 */
351bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
352{
353 struct list_head *pos;
354 bool done = true;
355
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900356 list_for_each_cookie(pos, head->read_var2,
357 &tomoyo_globally_readable_list) {
358 struct tomoyo_globally_readable_file_entry *ptr;
359 ptr = list_entry(pos,
360 struct tomoyo_globally_readable_file_entry,
361 list);
362 if (ptr->is_deleted)
363 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900364 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
365 ptr->filename->name);
366 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900367 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900368 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900369 return done;
370}
371
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900372/* tomoyo_pattern_list is used for holding list of pathnames which are used for
373 * converting pathnames to pathname patterns during learning mode.
374 *
375 * An entry is added by
376 *
377 * # echo 'file_pattern /proc/\$/mounts' > \
378 * /sys/kernel/security/tomoyo/exception_policy
379 *
380 * and is deleted by
381 *
382 * # echo 'delete file_pattern /proc/\$/mounts' > \
383 * /sys/kernel/security/tomoyo/exception_policy
384 *
385 * and all entries are retrieved by
386 *
387 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
388 *
389 * In the example above, if a process which belongs to a domain which is in
390 * learning mode requested open("/proc/1/mounts", O_RDONLY),
391 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
392 * process belongs to.
393 *
394 * It is not a desirable behavior that we have to use /proc/\$/ instead of
395 * /proc/self/ when current process needs to access only current process's
396 * information. As of now, LSM version of TOMOYO is using __d_path() for
397 * calculating pathname. Non LSM version of TOMOYO is using its own function
398 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
399 * current process from accessing other process's information.
400 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900401LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900402
403/**
404 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
405 *
406 * @pattern: Pathname pattern.
407 * @is_delete: True if it is a delete request.
408 *
409 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900410 *
411 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900412 */
413static int tomoyo_update_file_pattern_entry(const char *pattern,
414 const bool is_delete)
415{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900416 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900417 struct tomoyo_pattern_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900418 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900419
Tetsuo Handa3f629632010-06-03 20:37:26 +0900420 if (!tomoyo_is_correct_word(pattern))
421 return -EINVAL;
422 e.pattern = tomoyo_get_name(pattern);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900423 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900424 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900425 if (mutex_lock_interruptible(&tomoyo_policy_lock))
426 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900427 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900428 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900429 continue;
430 ptr->is_deleted = is_delete;
431 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900432 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900433 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900434 if (!is_delete && error) {
435 struct tomoyo_pattern_entry *entry =
436 tomoyo_commit_ok(&e, sizeof(e));
437 if (entry) {
438 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
439 error = 0;
440 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900441 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900442 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900443 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900444 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900445 return error;
446}
447
448/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900449 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900450 *
451 * @filename: The filename to find patterned pathname.
452 *
453 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900454 *
455 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900456 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900457const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900458{
459 struct tomoyo_pattern_entry *ptr;
460 const struct tomoyo_path_info *pattern = NULL;
461
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900462 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900463 if (ptr->is_deleted)
464 continue;
465 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
466 continue;
467 pattern = ptr->pattern;
468 if (tomoyo_strendswith(pattern->name, "/\\*")) {
469 /* Do nothing. Try to find the better match. */
470 } else {
471 /* This would be the better match. Use this. */
472 break;
473 }
474 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900475 if (pattern)
476 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900477 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900478}
479
480/**
481 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
482 *
483 * @data: String to parse.
484 * @is_delete: True if it is a delete request.
485 *
486 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900487 *
488 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900489 */
490int tomoyo_write_pattern_policy(char *data, const bool is_delete)
491{
492 return tomoyo_update_file_pattern_entry(data, is_delete);
493}
494
495/**
496 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
497 *
498 * @head: Pointer to "struct tomoyo_io_buffer".
499 *
500 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900501 *
502 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900503 */
504bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
505{
506 struct list_head *pos;
507 bool done = true;
508
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900509 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
510 struct tomoyo_pattern_entry *ptr;
511 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
512 if (ptr->is_deleted)
513 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900514 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
515 "%s\n", ptr->pattern->name);
516 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900517 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900518 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900519 return done;
520}
521
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900522/*
523 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
524 * default forbidden to modify already written content of a file.
525 *
526 * An entry is added by
527 *
528 * # echo 'deny_rewrite /var/log/messages' > \
529 * /sys/kernel/security/tomoyo/exception_policy
530 *
531 * and is deleted by
532 *
533 * # echo 'delete deny_rewrite /var/log/messages' > \
534 * /sys/kernel/security/tomoyo/exception_policy
535 *
536 * and all entries are retrieved by
537 *
538 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
539 *
540 * In the example above, if a process requested to rewrite /var/log/messages ,
541 * the process can't rewrite unless the domain which that process belongs to
542 * has "allow_rewrite /var/log/messages" entry.
543 *
544 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
545 * when we want to allow rewriting already unlink()ed file. As of now,
546 * LSM version of TOMOYO is using __d_path() for calculating pathname.
547 * Non LSM version of TOMOYO is using its own function which doesn't append
548 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
549 * need to worry whether the file is already unlink()ed or not.
550 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900551LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900552
553/**
554 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
555 *
556 * @pattern: Pathname pattern that are not rewritable by default.
557 * @is_delete: True if it is a delete request.
558 *
559 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900560 *
561 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900562 */
563static int tomoyo_update_no_rewrite_entry(const char *pattern,
564 const bool is_delete)
565{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900566 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900567 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900568 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900569
Tetsuo Handa3f629632010-06-03 20:37:26 +0900570 if (!tomoyo_is_correct_word(pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900571 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900572 e.pattern = tomoyo_get_name(pattern);
573 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900574 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900575 if (mutex_lock_interruptible(&tomoyo_policy_lock))
576 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900577 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900578 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900579 continue;
580 ptr->is_deleted = is_delete;
581 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900582 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900583 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900584 if (!is_delete && error) {
585 struct tomoyo_no_rewrite_entry *entry =
586 tomoyo_commit_ok(&e, sizeof(e));
587 if (entry) {
588 list_add_tail_rcu(&entry->list,
589 &tomoyo_no_rewrite_list);
590 error = 0;
591 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900592 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900593 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900594 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900595 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900596 return error;
597}
598
599/**
600 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
601 *
602 * @filename: Filename to check.
603 *
604 * Returns true if @filename is specified by "deny_rewrite" directive,
605 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900606 *
607 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900608 */
609static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
610{
611 struct tomoyo_no_rewrite_entry *ptr;
612 bool found = false;
613
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900614 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900615 if (ptr->is_deleted)
616 continue;
617 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
618 continue;
619 found = true;
620 break;
621 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900622 return found;
623}
624
625/**
626 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
627 *
628 * @data: String to parse.
629 * @is_delete: True if it is a delete request.
630 *
631 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900632 *
633 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900634 */
635int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
636{
637 return tomoyo_update_no_rewrite_entry(data, is_delete);
638}
639
640/**
641 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
642 *
643 * @head: Pointer to "struct tomoyo_io_buffer".
644 *
645 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900646 *
647 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900648 */
649bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
650{
651 struct list_head *pos;
652 bool done = true;
653
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900654 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
655 struct tomoyo_no_rewrite_entry *ptr;
656 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
657 if (ptr->is_deleted)
658 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900659 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
660 "%s\n", ptr->pattern->name);
661 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900662 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900663 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900664 return done;
665}
666
667/**
668 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
669 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900670 * @perm: Permission (between 1 to 7).
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900671 * @filename: Filename.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900672 * @domain: Pointer to "struct tomoyo_domain_info".
673 * @is_delete: True if it is a delete request.
674 *
675 * Returns 0 on success, negative value otherwise.
676 *
677 * This is legacy support interface for older policy syntax.
678 * Current policy syntax uses "allow_read/write" instead of "6",
679 * "allow_read" instead of "4", "allow_write" instead of "2",
680 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900681 *
682 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900683 */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900684static int tomoyo_update_file_acl(u8 perm, const char *filename,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900685 struct tomoyo_domain_info * const domain,
686 const bool is_delete)
687{
688 if (perm > 7 || !perm) {
689 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
690 __func__, perm, filename);
691 return -EINVAL;
692 }
693 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
694 /*
695 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
696 * directory permissions.
697 */
698 return 0;
699 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900700 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
701 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900702 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900703 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
704 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900705 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900706 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
707 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900708 return 0;
709}
710
711/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900712 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900713 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900714 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900715 * @filename: Filename to check.
716 * @perm: Permission.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900717 *
718 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900719 *
720 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900721 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900722static int tomoyo_path_acl(const struct tomoyo_request_info *r,
723 const struct tomoyo_path_info *filename,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900724 const u32 perm)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900725{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900726 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900727 struct tomoyo_acl_info *ptr;
728 int error = -EPERM;
729
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900730 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900731 struct tomoyo_path_acl *acl;
732 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900733 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900734 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900735 if (!(acl->perm & perm) ||
Tetsuo Handa3f629632010-06-03 20:37:26 +0900736 !tomoyo_compare_name_union(filename, &acl->name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900737 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900738 error = 0;
739 break;
740 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900741 return error;
742}
743
744/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900745 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900746 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900747 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900748 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900749 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900750 *
751 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900752 *
753 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900754 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900755static int tomoyo_file_perm(struct tomoyo_request_info *r,
756 const struct tomoyo_path_info *filename,
757 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900758{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900759 const char *msg = "<unknown>";
760 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900761 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900762
763 if (!filename)
764 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900765
766 if (mode == 6) {
767 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
768 perm = 1 << TOMOYO_TYPE_READ_WRITE;
769 } else if (mode == 4) {
770 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
771 perm = 1 << TOMOYO_TYPE_READ;
772 } else if (mode == 2) {
773 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
774 perm = 1 << TOMOYO_TYPE_WRITE;
775 } else if (mode == 1) {
776 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
777 perm = 1 << TOMOYO_TYPE_EXECUTE;
778 } else
779 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900780 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900781 error = tomoyo_path_acl(r, filename, perm);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900782 if (error && mode == 4 && !r->domain->ignore_global_allow_read
783 && tomoyo_is_globally_readable_file(filename))
784 error = 0;
785 if (!error)
786 break;
787 tomoyo_warn_log(r, "%s %s", msg, filename->name);
788 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900789 tomoyo_file_pattern(filename));
790 /*
791 * Do not retry for execute request, for alias may have
792 * changed.
793 */
794 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
795 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900797 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900798}
799
800/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900801 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900802 *
803 * @type: Type of operation.
804 * @filename: Filename.
805 * @domain: Pointer to "struct tomoyo_domain_info".
806 * @is_delete: True if it is a delete request.
807 *
808 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900809 *
810 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900811 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900812static int tomoyo_update_path_acl(const u8 type, const char *filename,
813 struct tomoyo_domain_info *const domain,
814 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900815{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900816 static const u16 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900817 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900818 const u16 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900819 struct tomoyo_acl_info *ptr;
820 struct tomoyo_path_acl e = {
821 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900822 .perm = perm
823 };
824 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900825
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900826 if (type == TOMOYO_TYPE_READ_WRITE)
827 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900828 if (!domain)
829 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900830 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900831 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900832 if (mutex_lock_interruptible(&tomoyo_policy_lock))
833 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900834 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900835 struct tomoyo_path_acl *acl =
836 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900837 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900838 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900839 if (is_delete) {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900840 acl->perm &= ~perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900841 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900842 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
843 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900844 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900845 } else {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900846 acl->perm |= perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900847 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900848 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
849 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900850 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900851 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900852 error = 0;
853 break;
854 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900855 if (!is_delete && error) {
856 struct tomoyo_path_acl *entry =
857 tomoyo_commit_ok(&e, sizeof(e));
858 if (entry) {
859 list_add_tail_rcu(&entry->head.list,
860 &domain->acl_info_list);
861 error = 0;
862 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900863 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900864 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900865 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900866 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900867 return error;
868}
869
870/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900871 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
872 *
873 * @type: Type of operation.
874 * @filename: Filename.
875 * @mode: Create mode.
876 * @major: Device major number.
877 * @minor: Device minor number.
878 * @domain: Pointer to "struct tomoyo_domain_info".
879 * @is_delete: True if it is a delete request.
880 *
881 * Returns 0 on success, negative value otherwise.
882 */
883static inline int tomoyo_update_path_number3_acl(const u8 type,
884 const char *filename,
885 char *mode,
886 char *major, char *minor,
887 struct tomoyo_domain_info *
888 const domain,
889 const bool is_delete)
890{
891 const u8 perm = 1 << type;
892 struct tomoyo_acl_info *ptr;
893 struct tomoyo_path_number3_acl e = {
894 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
895 .perm = perm
896 };
897 int error = is_delete ? -ENOENT : -ENOMEM;
898 if (!tomoyo_parse_name_union(filename, &e.name) ||
899 !tomoyo_parse_number_union(mode, &e.mode) ||
900 !tomoyo_parse_number_union(major, &e.major) ||
901 !tomoyo_parse_number_union(minor, &e.minor))
902 goto out;
903 if (mutex_lock_interruptible(&tomoyo_policy_lock))
904 goto out;
905 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
906 struct tomoyo_path_number3_acl *acl =
907 container_of(ptr, struct tomoyo_path_number3_acl, head);
908 if (!tomoyo_is_same_path_number3_acl(acl, &e))
909 continue;
910 if (is_delete)
911 acl->perm &= ~perm;
912 else
913 acl->perm |= perm;
914 error = 0;
915 break;
916 }
917 if (!is_delete && error) {
918 struct tomoyo_path_number3_acl *entry =
919 tomoyo_commit_ok(&e, sizeof(e));
920 if (entry) {
921 list_add_tail_rcu(&entry->head.list,
922 &domain->acl_info_list);
923 error = 0;
924 }
925 }
926 mutex_unlock(&tomoyo_policy_lock);
927 out:
928 tomoyo_put_name_union(&e.name);
929 tomoyo_put_number_union(&e.mode);
930 tomoyo_put_number_union(&e.major);
931 tomoyo_put_number_union(&e.minor);
932 return error;
933}
934
935/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900936 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900937 *
938 * @type: Type of operation.
939 * @filename1: First filename.
940 * @filename2: Second filename.
941 * @domain: Pointer to "struct tomoyo_domain_info".
942 * @is_delete: True if it is a delete request.
943 *
944 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900945 *
946 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900947 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900948static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
949 const char *filename2,
950 struct tomoyo_domain_info *const domain,
951 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900952{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900953 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900954 struct tomoyo_path2_acl e = {
955 .head.type = TOMOYO_TYPE_PATH2_ACL,
956 .perm = perm
957 };
958 struct tomoyo_acl_info *ptr;
959 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900960
961 if (!domain)
962 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900963 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
964 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900965 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900966 if (mutex_lock_interruptible(&tomoyo_policy_lock))
967 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900968 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900969 struct tomoyo_path2_acl *acl =
970 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900971 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900972 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900973 if (is_delete)
974 acl->perm &= ~perm;
975 else
976 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900977 error = 0;
978 break;
979 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900980 if (!is_delete && error) {
981 struct tomoyo_path2_acl *entry =
982 tomoyo_commit_ok(&e, sizeof(e));
983 if (entry) {
984 list_add_tail_rcu(&entry->head.list,
985 &domain->acl_info_list);
986 error = 0;
987 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900988 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900989 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900990 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900991 tomoyo_put_name_union(&e.name1);
992 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900993 return error;
994}
995
996/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900997 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
998 *
999 * @r: Pointer to "struct tomoyo_request_info".
1000 * @filename: Filename to check.
1001 * @perm: Permission.
1002 * @mode: Create mode.
1003 * @major: Device major number.
1004 * @minor: Device minor number.
1005 *
1006 * Returns 0 on success, -EPERM otherwise.
1007 *
1008 * Caller holds tomoyo_read_lock().
1009 */
1010static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
1011 const struct tomoyo_path_info *filename,
1012 const u16 perm, const unsigned int mode,
1013 const unsigned int major,
1014 const unsigned int minor)
1015{
1016 struct tomoyo_domain_info *domain = r->domain;
1017 struct tomoyo_acl_info *ptr;
1018 int error = -EPERM;
1019 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1020 struct tomoyo_path_number3_acl *acl;
1021 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
1022 continue;
1023 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
1024 if (!tomoyo_compare_number_union(mode, &acl->mode))
1025 continue;
1026 if (!tomoyo_compare_number_union(major, &acl->major))
1027 continue;
1028 if (!tomoyo_compare_number_union(minor, &acl->minor))
1029 continue;
1030 if (!(acl->perm & perm))
1031 continue;
1032 if (!tomoyo_compare_name_union(filename, &acl->name))
1033 continue;
1034 error = 0;
1035 break;
1036 }
1037 return error;
1038}
1039
1040/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001041 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001042 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001043 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001044 * @type: Type of operation.
1045 * @filename1: First filename to check.
1046 * @filename2: Second filename to check.
1047 *
1048 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001049 *
1050 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001051 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001052static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001053 const struct tomoyo_path_info *filename1,
1054 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001055{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001056 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001057 struct tomoyo_acl_info *ptr;
1058 const u8 perm = 1 << type;
1059 int error = -EPERM;
1060
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001061 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001062 struct tomoyo_path2_acl *acl;
1063 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001064 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001065 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001066 if (!(acl->perm & perm))
1067 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001068 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001069 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001070 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001071 continue;
1072 error = 0;
1073 break;
1074 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001075 return error;
1076}
1077
1078/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001079 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001080 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001081 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001082 * @operation: Type of operation.
1083 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001084 *
1085 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001086 *
1087 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001088 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001089static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1090 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001091{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001092 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001093 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001094
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001095 next:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001096 r->type = tomoyo_p2mac[operation];
1097 r->mode = tomoyo_get_mode(r->profile, r->type);
1098 if (r->mode == TOMOYO_CONFIG_DISABLED)
1099 return 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001100 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +09001101 error = tomoyo_path_acl(r, filename, 1 << operation);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001102 if (!error)
1103 break;
1104 msg = tomoyo_path2keyword(operation);
1105 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1106 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1107 tomoyo_file_pattern(filename));
1108 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001109 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001110 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001111 /*
1112 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1113 * we need to check "allow_rewrite" permission if the filename is
1114 * specified by "deny_rewrite" keyword.
1115 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001116 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001117 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001118 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001119 goto next;
1120 }
1121 return error;
1122}
1123
1124/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001125 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1126 *
1127 * @r: Pointer to "struct tomoyo_request_info".
1128 * @type: Operation.
1129 * @filename: Filename to check.
1130 * @number: Number.
1131 *
1132 * Returns 0 on success, -EPERM otherwise.
1133 *
1134 * Caller holds tomoyo_read_lock().
1135 */
1136static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1137 const struct tomoyo_path_info *filename,
1138 const unsigned long number)
1139{
1140 struct tomoyo_domain_info *domain = r->domain;
1141 struct tomoyo_acl_info *ptr;
1142 const u8 perm = 1 << type;
1143 int error = -EPERM;
1144 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1145 struct tomoyo_path_number_acl *acl;
1146 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1147 continue;
1148 acl = container_of(ptr, struct tomoyo_path_number_acl,
1149 head);
1150 if (!(acl->perm & perm) ||
1151 !tomoyo_compare_number_union(number, &acl->number) ||
1152 !tomoyo_compare_name_union(filename, &acl->name))
1153 continue;
1154 error = 0;
1155 break;
1156 }
1157 return error;
1158}
1159
1160/**
1161 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1162 *
1163 * @type: Type of operation.
1164 * @filename: Filename.
1165 * @number: Number.
1166 * @domain: Pointer to "struct tomoyo_domain_info".
1167 * @is_delete: True if it is a delete request.
1168 *
1169 * Returns 0 on success, negative value otherwise.
1170 */
1171static inline int tomoyo_update_path_number_acl(const u8 type,
1172 const char *filename,
1173 char *number,
1174 struct tomoyo_domain_info *
1175 const domain,
1176 const bool is_delete)
1177{
1178 const u8 perm = 1 << type;
1179 struct tomoyo_acl_info *ptr;
1180 struct tomoyo_path_number_acl e = {
1181 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1182 .perm = perm
1183 };
1184 int error = is_delete ? -ENOENT : -ENOMEM;
1185 if (!domain)
1186 return -EINVAL;
1187 if (!tomoyo_parse_name_union(filename, &e.name))
1188 return -EINVAL;
1189 if (!tomoyo_parse_number_union(number, &e.number))
1190 goto out;
1191 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1192 goto out;
1193 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1194 struct tomoyo_path_number_acl *acl =
1195 container_of(ptr, struct tomoyo_path_number_acl, head);
1196 if (!tomoyo_is_same_path_number_acl(acl, &e))
1197 continue;
1198 if (is_delete)
1199 acl->perm &= ~perm;
1200 else
1201 acl->perm |= perm;
1202 error = 0;
1203 break;
1204 }
1205 if (!is_delete && error) {
1206 struct tomoyo_path_number_acl *entry =
1207 tomoyo_commit_ok(&e, sizeof(e));
1208 if (entry) {
1209 list_add_tail_rcu(&entry->head.list,
1210 &domain->acl_info_list);
1211 error = 0;
1212 }
1213 }
1214 mutex_unlock(&tomoyo_policy_lock);
1215 out:
1216 tomoyo_put_name_union(&e.name);
1217 tomoyo_put_number_union(&e.number);
1218 return error;
1219}
1220
1221/**
1222 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1223 *
1224 * @r: Pointer to "strct tomoyo_request_info".
1225 * @filename: Filename to check.
1226 * @number: Number.
1227 *
1228 * Returns 0 on success, negative value otherwise.
1229 *
1230 * Caller holds tomoyo_read_lock().
1231 */
1232static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1233 const u8 type,
1234 const struct tomoyo_path_info *filename,
1235 const unsigned long number)
1236{
1237 char buffer[64];
1238 int error;
1239 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001240 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001241
1242 if (!filename)
1243 return 0;
1244 switch (type) {
1245 case TOMOYO_TYPE_CREATE:
1246 case TOMOYO_TYPE_MKDIR:
1247 case TOMOYO_TYPE_MKFIFO:
1248 case TOMOYO_TYPE_MKSOCK:
1249 case TOMOYO_TYPE_CHMOD:
1250 radix = TOMOYO_VALUE_TYPE_OCTAL;
1251 break;
1252 case TOMOYO_TYPE_IOCTL:
1253 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1254 break;
1255 default:
1256 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1257 break;
1258 }
1259 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001260 do {
1261 error = tomoyo_path_number_acl(r, type, filename, number);
1262 if (!error)
1263 break;
1264 msg = tomoyo_path_number2keyword(type);
1265 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1266 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1267 tomoyo_file_pattern(filename),
1268 buffer);
1269 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001270 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1271 error = 0;
1272 return error;
1273}
1274
1275/**
1276 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1277 *
1278 * @type: Type of operation.
1279 * @path: Pointer to "struct path".
1280 * @number: Number.
1281 *
1282 * Returns 0 on success, negative value otherwise.
1283 */
1284int tomoyo_path_number_perm(const u8 type, struct path *path,
1285 unsigned long number)
1286{
1287 struct tomoyo_request_info r;
1288 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001289 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001290 int idx;
1291
Tetsuo Handa57c25902010-06-03 20:38:44 +09001292 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
1293 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001294 return 0;
1295 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001296 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001297 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001298 if (type == TOMOYO_TYPE_MKDIR)
1299 tomoyo_add_slash(&buf);
1300 error = tomoyo_path_number_perm2(&r, type, &buf, number);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001301 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001302 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001303 tomoyo_read_unlock(idx);
1304 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1305 error = 0;
1306 return error;
1307}
1308
1309/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001310 * tomoyo_check_exec_perm - Check permission for "execute".
1311 *
Tetsuo Handa57c25902010-06-03 20:38:44 +09001312 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001313 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001314 *
1315 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001316 *
1317 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001318 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001319int tomoyo_check_exec_perm(struct tomoyo_request_info *r,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001320 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001321{
Tetsuo Handa57c25902010-06-03 20:38:44 +09001322 if (r->mode == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001323 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001324 return tomoyo_file_perm(r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001325}
1326
1327/**
1328 * tomoyo_check_open_permission - Check permission for "read" and "write".
1329 *
1330 * @domain: Pointer to "struct tomoyo_domain_info".
1331 * @path: Pointer to "struct path".
1332 * @flag: Flags for open().
1333 *
1334 * Returns 0 on success, negative value otherwise.
1335 */
1336int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1337 struct path *path, const int flag)
1338{
1339 const u8 acc_mode = ACC_MODE(flag);
1340 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001341 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001342 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001343 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001344
Tetsuo Handa57c25902010-06-03 20:38:44 +09001345 if (!path->mnt ||
1346 (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001347 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001348 buf.name = NULL;
1349 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001350 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001351 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001352 goto out;
1353 error = 0;
1354 /*
1355 * If the filename is specified by "deny_rewrite" keyword,
1356 * we need to check "allow_rewrite" permission when the filename is not
1357 * opened for append mode or the filename is truncated at open time.
1358 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001359 if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
1360 && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
1361 != TOMOYO_CONFIG_DISABLED) {
1362 if (!tomoyo_get_realpath(&buf, path)) {
1363 error = -ENOMEM;
1364 goto out;
1365 }
1366 if (tomoyo_is_no_rewrite_file(&buf))
1367 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
1368 &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001369 }
Tetsuo Handa57c25902010-06-03 20:38:44 +09001370 if (!error && acc_mode &&
1371 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
1372 != TOMOYO_CONFIG_DISABLED) {
1373 if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
1374 error = -ENOMEM;
1375 goto out;
1376 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001377 error = tomoyo_file_perm(&r, &buf, acc_mode);
Tetsuo Handa57c25902010-06-03 20:38:44 +09001378 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001379 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001380 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001381 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001382 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001383 error = 0;
1384 return error;
1385}
1386
1387/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001388 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001389 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001390 * @operation: Type of operation.
1391 * @path: Pointer to "struct path".
1392 *
1393 * Returns 0 on success, negative value otherwise.
1394 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001395int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001396{
1397 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001398 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001399 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001400 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001401
Tetsuo Handa57c25902010-06-03 20:38:44 +09001402 if (!path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001403 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001404 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
1405 == TOMOYO_CONFIG_DISABLED)
1406 return 0;
1407 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001408 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001409 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001410 goto out;
1411 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001412 case TOMOYO_TYPE_REWRITE:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001413 if (!tomoyo_is_no_rewrite_file(&buf)) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001414 error = 0;
1415 goto out;
1416 }
1417 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001418 case TOMOYO_TYPE_RMDIR:
1419 case TOMOYO_TYPE_CHROOT:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001420 case TOMOYO_TYPE_UMOUNT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001421 tomoyo_add_slash(&buf);
1422 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001423 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001424 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001425 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001426 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001427 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001428 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001429 error = 0;
1430 return error;
1431}
1432
1433/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001434 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1435 *
1436 * @r: Pointer to "struct tomoyo_request_info".
1437 * @operation: Type of operation.
1438 * @filename: Filename to check.
1439 * @mode: Create mode.
1440 * @dev: Device number.
1441 *
1442 * Returns 0 on success, negative value otherwise.
1443 *
1444 * Caller holds tomoyo_read_lock().
1445 */
1446static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1447 const u8 operation,
1448 const struct tomoyo_path_info *filename,
1449 const unsigned int mode,
1450 const unsigned int dev)
1451{
1452 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001453 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001454 const unsigned int major = MAJOR(dev);
1455 const unsigned int minor = MINOR(dev);
1456
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001457 do {
1458 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1459 mode, major, minor);
1460 if (!error)
1461 break;
1462 msg = tomoyo_path_number32keyword(operation);
1463 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1464 mode, major, minor);
1465 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1466 tomoyo_file_pattern(filename), mode,
1467 major, minor);
1468 } while (error == TOMOYO_RETRY_REQUEST);
1469 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001470 error = 0;
1471 return error;
1472}
1473
1474/**
1475 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1476 *
1477 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1478 * @path: Pointer to "struct path".
1479 * @mode: Create mode.
1480 * @dev: Device number.
1481 *
1482 * Returns 0 on success, negative value otherwise.
1483 */
1484int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1485 const unsigned int mode, unsigned int dev)
1486{
1487 struct tomoyo_request_info r;
1488 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001489 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001490 int idx;
1491
Tetsuo Handa57c25902010-06-03 20:38:44 +09001492 if (!path->mnt ||
1493 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
1494 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001495 return 0;
1496 idx = tomoyo_read_lock();
1497 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001498 if (tomoyo_get_realpath(&buf, path)) {
1499 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001500 new_decode_dev(dev));
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001501 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001502 }
1503 tomoyo_read_unlock(idx);
1504 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1505 error = 0;
1506 return error;
1507}
1508
1509/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001510 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001511 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001512 * @operation: Type of operation.
1513 * @path1: Pointer to "struct path".
1514 * @path2: Pointer to "struct path".
1515 *
1516 * Returns 0 on success, negative value otherwise.
1517 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001518int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001519 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001520{
1521 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001522 const char *msg;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001523 struct tomoyo_path_info buf1;
1524 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001525 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001526 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001527
Tetsuo Handa57c25902010-06-03 20:38:44 +09001528 if (!path1->mnt || !path2->mnt ||
1529 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
1530 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001531 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001532 buf1.name = NULL;
1533 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001534 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001535 if (!tomoyo_get_realpath(&buf1, path1) ||
1536 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001537 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001538 switch (operation) {
1539 struct dentry *dentry;
1540 case TOMOYO_TYPE_RENAME:
1541 case TOMOYO_TYPE_LINK:
1542 dentry = path1->dentry;
1543 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
1544 break;
1545 /* fall through */
1546 case TOMOYO_TYPE_PIVOT_ROOT:
1547 tomoyo_add_slash(&buf1);
1548 tomoyo_add_slash(&buf2);
1549 break;
1550 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001551 do {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001552 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001553 if (!error)
1554 break;
1555 msg = tomoyo_path22keyword(operation);
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001556 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001557 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001558 tomoyo_file_pattern(&buf1),
1559 tomoyo_file_pattern(&buf2));
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001560 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001561 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001562 kfree(buf1.name);
1563 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001564 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001565 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001566 error = 0;
1567 return error;
1568}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001569
1570/**
1571 * tomoyo_write_file_policy - Update file related list.
1572 *
1573 * @data: String to parse.
1574 * @domain: Pointer to "struct tomoyo_domain_info".
1575 * @is_delete: True if it is a delete request.
1576 *
1577 * Returns 0 on success, negative value otherwise.
1578 *
1579 * Caller holds tomoyo_read_lock().
1580 */
1581int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1582 const bool is_delete)
1583{
1584 char *w[5];
1585 u8 type;
1586 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1587 return -EINVAL;
1588 if (strncmp(w[0], "allow_", 6)) {
1589 unsigned int perm;
1590 if (sscanf(w[0], "%u", &perm) == 1)
1591 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1592 is_delete);
1593 goto out;
1594 }
1595 w[0] += 6;
1596 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1597 if (strcmp(w[0], tomoyo_path_keyword[type]))
1598 continue;
1599 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1600 }
1601 if (!w[2][0])
1602 goto out;
1603 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1604 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1605 continue;
1606 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1607 is_delete);
1608 }
1609 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1610 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1611 continue;
1612 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1613 is_delete);
1614 }
1615 if (!w[3][0] || !w[4][0])
1616 goto out;
1617 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1618 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1619 continue;
1620 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1621 w[4], domain, is_delete);
1622 }
1623 out:
1624 return -EINVAL;
1625}