blob: 09436d11f298ff7d770110cfcf22429ea20d359a [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 Handa82e0f002010-06-15 09:22:42 +0900280 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
281 head.list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900282 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900283 continue;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900284 ptr->head.is_deleted = is_delete;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900285 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900286 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900287 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900288 if (!is_delete && error) {
289 struct tomoyo_globally_readable_file_entry *entry =
290 tomoyo_commit_ok(&e, sizeof(e));
291 if (entry) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900292 list_add_tail_rcu(&entry->head.list,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900293 &tomoyo_globally_readable_list);
294 error = 0;
295 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900296 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900297 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900298 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900299 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900300 return error;
301}
302
303/**
304 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
305 *
306 * @filename: The filename to check.
307 *
308 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900309 *
310 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900311 */
312static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
313 filename)
314{
315 struct tomoyo_globally_readable_file_entry *ptr;
316 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900317
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900318 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
319 head.list) {
320 if (!ptr->head.is_deleted &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900321 tomoyo_path_matches_pattern(filename, ptr->filename)) {
322 found = true;
323 break;
324 }
325 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900326 return found;
327}
328
329/**
330 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
331 *
332 * @data: String to parse.
333 * @is_delete: True if it is a delete request.
334 *
335 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900336 *
337 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900338 */
339int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
340{
341 return tomoyo_update_globally_readable_entry(data, is_delete);
342}
343
344/**
345 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
346 *
347 * @head: Pointer to "struct tomoyo_io_buffer".
348 *
349 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900350 *
351 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900352 */
353bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
354{
355 struct list_head *pos;
356 bool done = true;
357
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900358 list_for_each_cookie(pos, head->read_var2,
359 &tomoyo_globally_readable_list) {
360 struct tomoyo_globally_readable_file_entry *ptr;
361 ptr = list_entry(pos,
362 struct tomoyo_globally_readable_file_entry,
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900363 head.list);
364 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900365 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900366 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
367 ptr->filename->name);
368 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900369 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900370 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900371 return done;
372}
373
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900374/* tomoyo_pattern_list is used for holding list of pathnames which are used for
375 * converting pathnames to pathname patterns during learning mode.
376 *
377 * An entry is added by
378 *
379 * # echo 'file_pattern /proc/\$/mounts' > \
380 * /sys/kernel/security/tomoyo/exception_policy
381 *
382 * and is deleted by
383 *
384 * # echo 'delete file_pattern /proc/\$/mounts' > \
385 * /sys/kernel/security/tomoyo/exception_policy
386 *
387 * and all entries are retrieved by
388 *
389 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
390 *
391 * In the example above, if a process which belongs to a domain which is in
392 * learning mode requested open("/proc/1/mounts", O_RDONLY),
393 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
394 * process belongs to.
395 *
396 * It is not a desirable behavior that we have to use /proc/\$/ instead of
397 * /proc/self/ when current process needs to access only current process's
398 * information. As of now, LSM version of TOMOYO is using __d_path() for
399 * calculating pathname. Non LSM version of TOMOYO is using its own function
400 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
401 * current process from accessing other process's information.
402 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900403LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900404
405/**
406 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
407 *
408 * @pattern: Pathname pattern.
409 * @is_delete: True if it is a delete request.
410 *
411 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900412 *
413 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900414 */
415static int tomoyo_update_file_pattern_entry(const char *pattern,
416 const bool is_delete)
417{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900418 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900419 struct tomoyo_pattern_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900420 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900421
Tetsuo Handa3f629632010-06-03 20:37:26 +0900422 if (!tomoyo_is_correct_word(pattern))
423 return -EINVAL;
424 e.pattern = tomoyo_get_name(pattern);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900425 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900426 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900427 if (mutex_lock_interruptible(&tomoyo_policy_lock))
428 goto out;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900429 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, head.list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900430 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900431 continue;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900432 ptr->head.is_deleted = is_delete;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900433 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900434 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900435 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900436 if (!is_delete && error) {
437 struct tomoyo_pattern_entry *entry =
438 tomoyo_commit_ok(&e, sizeof(e));
439 if (entry) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900440 list_add_tail_rcu(&entry->head.list,
441 &tomoyo_pattern_list);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900442 error = 0;
443 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900444 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900445 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900446 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900447 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900448 return error;
449}
450
451/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900452 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900453 *
454 * @filename: The filename to find patterned pathname.
455 *
456 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900457 *
458 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900459 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900460const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900461{
462 struct tomoyo_pattern_entry *ptr;
463 const struct tomoyo_path_info *pattern = NULL;
464
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900465 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, head.list) {
466 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900467 continue;
468 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
469 continue;
470 pattern = ptr->pattern;
471 if (tomoyo_strendswith(pattern->name, "/\\*")) {
472 /* Do nothing. Try to find the better match. */
473 } else {
474 /* This would be the better match. Use this. */
475 break;
476 }
477 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900478 if (pattern)
479 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900480 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900481}
482
483/**
484 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
485 *
486 * @data: String to parse.
487 * @is_delete: True if it is a delete request.
488 *
489 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900490 *
491 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900492 */
493int tomoyo_write_pattern_policy(char *data, const bool is_delete)
494{
495 return tomoyo_update_file_pattern_entry(data, is_delete);
496}
497
498/**
499 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
500 *
501 * @head: Pointer to "struct tomoyo_io_buffer".
502 *
503 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900504 *
505 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900506 */
507bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
508{
509 struct list_head *pos;
510 bool done = true;
511
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900512 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
513 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900514 ptr = list_entry(pos, struct tomoyo_pattern_entry, head.list);
515 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900516 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900517 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
518 "%s\n", ptr->pattern->name);
519 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900520 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900521 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900522 return done;
523}
524
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900525/*
526 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
527 * default forbidden to modify already written content of a file.
528 *
529 * An entry is added by
530 *
531 * # echo 'deny_rewrite /var/log/messages' > \
532 * /sys/kernel/security/tomoyo/exception_policy
533 *
534 * and is deleted by
535 *
536 * # echo 'delete deny_rewrite /var/log/messages' > \
537 * /sys/kernel/security/tomoyo/exception_policy
538 *
539 * and all entries are retrieved by
540 *
541 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
542 *
543 * In the example above, if a process requested to rewrite /var/log/messages ,
544 * the process can't rewrite unless the domain which that process belongs to
545 * has "allow_rewrite /var/log/messages" entry.
546 *
547 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
548 * when we want to allow rewriting already unlink()ed file. As of now,
549 * LSM version of TOMOYO is using __d_path() for calculating pathname.
550 * Non LSM version of TOMOYO is using its own function which doesn't append
551 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
552 * need to worry whether the file is already unlink()ed or not.
553 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900554LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900555
556/**
557 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
558 *
559 * @pattern: Pathname pattern that are not rewritable by default.
560 * @is_delete: True if it is a delete request.
561 *
562 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900563 *
564 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900565 */
566static int tomoyo_update_no_rewrite_entry(const char *pattern,
567 const bool is_delete)
568{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900569 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900570 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900571 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900572
Tetsuo Handa3f629632010-06-03 20:37:26 +0900573 if (!tomoyo_is_correct_word(pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900574 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900575 e.pattern = tomoyo_get_name(pattern);
576 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900577 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900578 if (mutex_lock_interruptible(&tomoyo_policy_lock))
579 goto out;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900580 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, head.list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900581 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900582 continue;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900583 ptr->head.is_deleted = is_delete;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900584 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900585 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900586 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900587 if (!is_delete && error) {
588 struct tomoyo_no_rewrite_entry *entry =
589 tomoyo_commit_ok(&e, sizeof(e));
590 if (entry) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900591 list_add_tail_rcu(&entry->head.list,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900592 &tomoyo_no_rewrite_list);
593 error = 0;
594 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900595 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900596 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900597 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900598 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900599 return error;
600}
601
602/**
603 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
604 *
605 * @filename: Filename to check.
606 *
607 * Returns true if @filename is specified by "deny_rewrite" directive,
608 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900609 *
610 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900611 */
612static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
613{
614 struct tomoyo_no_rewrite_entry *ptr;
615 bool found = false;
616
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900617 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, head.list) {
618 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900619 continue;
620 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
621 continue;
622 found = true;
623 break;
624 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900625 return found;
626}
627
628/**
629 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
630 *
631 * @data: String to parse.
632 * @is_delete: True if it is a delete request.
633 *
634 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900635 *
636 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900637 */
638int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
639{
640 return tomoyo_update_no_rewrite_entry(data, is_delete);
641}
642
643/**
644 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
645 *
646 * @head: Pointer to "struct tomoyo_io_buffer".
647 *
648 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900649 *
650 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900651 */
652bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
653{
654 struct list_head *pos;
655 bool done = true;
656
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900657 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
658 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900659 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry,
660 head.list);
661 if (ptr->head.is_deleted)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900662 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900663 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
664 "%s\n", ptr->pattern->name);
665 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900666 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900667 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900668 return done;
669}
670
671/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900672 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900673 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900674 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900675 * @filename: Filename to check.
676 * @perm: Permission.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900677 *
678 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900679 *
680 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900681 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900682static int tomoyo_path_acl(const struct tomoyo_request_info *r,
683 const struct tomoyo_path_info *filename,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900684 const u32 perm)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900685{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900686 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900687 struct tomoyo_acl_info *ptr;
688 int error = -EPERM;
689
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900690 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900691 struct tomoyo_path_acl *acl;
692 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900693 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900694 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900695 if (!(acl->perm & perm) ||
Tetsuo Handa3f629632010-06-03 20:37:26 +0900696 !tomoyo_compare_name_union(filename, &acl->name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900697 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900698 error = 0;
699 break;
700 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900701 return error;
702}
703
704/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900705 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900706 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900707 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900708 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900709 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900710 *
711 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900712 *
713 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900714 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900715static int tomoyo_file_perm(struct tomoyo_request_info *r,
716 const struct tomoyo_path_info *filename,
717 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900718{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900719 const char *msg = "<unknown>";
720 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900721 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900722
723 if (!filename)
724 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900725
726 if (mode == 6) {
727 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
728 perm = 1 << TOMOYO_TYPE_READ_WRITE;
729 } else if (mode == 4) {
730 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
731 perm = 1 << TOMOYO_TYPE_READ;
732 } else if (mode == 2) {
733 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
734 perm = 1 << TOMOYO_TYPE_WRITE;
735 } else if (mode == 1) {
736 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
737 perm = 1 << TOMOYO_TYPE_EXECUTE;
738 } else
739 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900740 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900741 error = tomoyo_path_acl(r, filename, perm);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900742 if (error && mode == 4 && !r->domain->ignore_global_allow_read
743 && tomoyo_is_globally_readable_file(filename))
744 error = 0;
745 if (!error)
746 break;
747 tomoyo_warn_log(r, "%s %s", msg, filename->name);
748 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900749 tomoyo_file_pattern(filename));
750 /*
751 * Do not retry for execute request, for alias may have
752 * changed.
753 */
754 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
755 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900756 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900757 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900758}
759
Tetsuo Handa237ab452010-06-12 20:46:22 +0900760static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
761 const struct tomoyo_acl_info *b)
762{
763 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
764 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
765 return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
766 tomoyo_is_same_name_union(&p1->name, &p2->name);
767}
768
769static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
770 struct tomoyo_acl_info *b,
771 const bool is_delete)
772{
773 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
774 ->perm;
775 u16 perm = *a_perm;
776 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
777 if (is_delete) {
778 perm &= ~b_perm;
779 if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
780 perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
781 else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
782 perm &= ~TOMOYO_RW_MASK;
783 } else {
784 perm |= b_perm;
785 if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
786 perm |= (1 << TOMOYO_TYPE_READ_WRITE);
787 else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
788 perm |= TOMOYO_RW_MASK;
789 }
790 *a_perm = perm;
791 return !perm;
792}
793
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900794/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900795 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900796 *
797 * @type: Type of operation.
798 * @filename: Filename.
799 * @domain: Pointer to "struct tomoyo_domain_info".
800 * @is_delete: True if it is a delete request.
801 *
802 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900803 *
804 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900805 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900806static int tomoyo_update_path_acl(const u8 type, const char *filename,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900807 struct tomoyo_domain_info * const domain,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900808 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900809{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900810 struct tomoyo_path_acl e = {
811 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900812 .perm = 1 << type
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900813 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900814 int error;
815 if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
816 e.perm |= TOMOYO_RW_MASK;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900817 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900818 return -EINVAL;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900819 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
820 tomoyo_same_path_acl,
821 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900822 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900823 return error;
824}
825
Tetsuo Handa237ab452010-06-12 20:46:22 +0900826static bool tomoyo_same_path_number3_acl(const struct tomoyo_acl_info *a,
827 const struct tomoyo_acl_info *b)
828{
829 const struct tomoyo_path_number3_acl *p1 = container_of(a, typeof(*p1),
830 head);
831 const struct tomoyo_path_number3_acl *p2 = container_of(b, typeof(*p2),
832 head);
833 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
834 && tomoyo_is_same_name_union(&p1->name, &p2->name)
835 && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
836 && tomoyo_is_same_number_union(&p1->major, &p2->major)
837 && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
838}
839
840static bool tomoyo_merge_path_number3_acl(struct tomoyo_acl_info *a,
841 struct tomoyo_acl_info *b,
842 const bool is_delete)
843{
844 u8 *const a_perm = &container_of(a, struct tomoyo_path_number3_acl,
845 head)->perm;
846 u8 perm = *a_perm;
847 const u8 b_perm = container_of(b, struct tomoyo_path_number3_acl, head)
848 ->perm;
849 if (is_delete)
850 perm &= ~b_perm;
851 else
852 perm |= b_perm;
853 *a_perm = perm;
854 return !perm;
855}
856
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900857/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900858 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
859 *
860 * @type: Type of operation.
861 * @filename: Filename.
862 * @mode: Create mode.
863 * @major: Device major number.
864 * @minor: Device minor number.
865 * @domain: Pointer to "struct tomoyo_domain_info".
866 * @is_delete: True if it is a delete request.
867 *
868 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900869 *
870 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900871 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900872static int tomoyo_update_path_number3_acl(const u8 type, const char *filename,
873 char *mode, char *major, char *minor,
874 struct tomoyo_domain_info * const
875 domain, const bool is_delete)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900876{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900877 struct tomoyo_path_number3_acl e = {
878 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900879 .perm = 1 << type
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900880 };
881 int error = is_delete ? -ENOENT : -ENOMEM;
882 if (!tomoyo_parse_name_union(filename, &e.name) ||
883 !tomoyo_parse_number_union(mode, &e.mode) ||
884 !tomoyo_parse_number_union(major, &e.major) ||
885 !tomoyo_parse_number_union(minor, &e.minor))
886 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900887 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
888 tomoyo_same_path_number3_acl,
889 tomoyo_merge_path_number3_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900890 out:
891 tomoyo_put_name_union(&e.name);
892 tomoyo_put_number_union(&e.mode);
893 tomoyo_put_number_union(&e.major);
894 tomoyo_put_number_union(&e.minor);
895 return error;
896}
897
Tetsuo Handa237ab452010-06-12 20:46:22 +0900898static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
899 const struct tomoyo_acl_info *b)
900{
901 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
902 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
903 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
904 && tomoyo_is_same_name_union(&p1->name1, &p2->name1)
905 && tomoyo_is_same_name_union(&p1->name2, &p2->name2);
906}
907
908static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
909 struct tomoyo_acl_info *b,
910 const bool is_delete)
911{
912 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
913 ->perm;
914 u8 perm = *a_perm;
915 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
916 if (is_delete)
917 perm &= ~b_perm;
918 else
919 perm |= b_perm;
920 *a_perm = perm;
921 return !perm;
922}
923
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900924/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900925 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900926 *
927 * @type: Type of operation.
928 * @filename1: First filename.
929 * @filename2: Second filename.
930 * @domain: Pointer to "struct tomoyo_domain_info".
931 * @is_delete: True if it is a delete request.
932 *
933 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900934 *
935 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900936 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900937static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
938 const char *filename2,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900939 struct tomoyo_domain_info * const domain,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900940 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900941{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900942 struct tomoyo_path2_acl e = {
943 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900944 .perm = 1 << type
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900945 };
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900946 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900947 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
948 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900949 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900950 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
951 tomoyo_same_path2_acl,
952 tomoyo_merge_path2_acl);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900953 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900954 tomoyo_put_name_union(&e.name1);
955 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900956 return error;
957}
958
959/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900960 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
961 *
962 * @r: Pointer to "struct tomoyo_request_info".
963 * @filename: Filename to check.
964 * @perm: Permission.
965 * @mode: Create mode.
966 * @major: Device major number.
967 * @minor: Device minor number.
968 *
969 * Returns 0 on success, -EPERM otherwise.
970 *
971 * Caller holds tomoyo_read_lock().
972 */
973static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
974 const struct tomoyo_path_info *filename,
975 const u16 perm, const unsigned int mode,
976 const unsigned int major,
977 const unsigned int minor)
978{
979 struct tomoyo_domain_info *domain = r->domain;
980 struct tomoyo_acl_info *ptr;
981 int error = -EPERM;
982 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
983 struct tomoyo_path_number3_acl *acl;
984 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
985 continue;
986 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
987 if (!tomoyo_compare_number_union(mode, &acl->mode))
988 continue;
989 if (!tomoyo_compare_number_union(major, &acl->major))
990 continue;
991 if (!tomoyo_compare_number_union(minor, &acl->minor))
992 continue;
993 if (!(acl->perm & perm))
994 continue;
995 if (!tomoyo_compare_name_union(filename, &acl->name))
996 continue;
997 error = 0;
998 break;
999 }
1000 return error;
1001}
1002
1003/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001004 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001005 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001006 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001007 * @type: Type of operation.
1008 * @filename1: First filename to check.
1009 * @filename2: Second filename to check.
1010 *
1011 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001012 *
1013 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001014 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001015static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001016 const struct tomoyo_path_info *filename1,
1017 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001018{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001019 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001020 struct tomoyo_acl_info *ptr;
1021 const u8 perm = 1 << type;
1022 int error = -EPERM;
1023
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001024 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001025 struct tomoyo_path2_acl *acl;
1026 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001027 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001028 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001029 if (!(acl->perm & perm))
1030 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001031 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001032 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001033 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001034 continue;
1035 error = 0;
1036 break;
1037 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001038 return error;
1039}
1040
1041/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001042 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001044 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001045 * @operation: Type of operation.
1046 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001047 *
1048 * Returns 0 on success, negative value 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_path_permission(struct tomoyo_request_info *r, u8 operation,
1053 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001054{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001055 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001056 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001057
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001058 next:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001059 r->type = tomoyo_p2mac[operation];
1060 r->mode = tomoyo_get_mode(r->profile, r->type);
1061 if (r->mode == TOMOYO_CONFIG_DISABLED)
1062 return 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001063 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +09001064 error = tomoyo_path_acl(r, filename, 1 << operation);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001065 if (!error)
1066 break;
1067 msg = tomoyo_path2keyword(operation);
1068 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1069 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1070 tomoyo_file_pattern(filename));
1071 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001072 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001073 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001074 /*
1075 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1076 * we need to check "allow_rewrite" permission if the filename is
1077 * specified by "deny_rewrite" keyword.
1078 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001079 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001080 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001081 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001082 goto next;
1083 }
1084 return error;
1085}
1086
1087/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001088 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1089 *
1090 * @r: Pointer to "struct tomoyo_request_info".
1091 * @type: Operation.
1092 * @filename: Filename to check.
1093 * @number: Number.
1094 *
1095 * Returns 0 on success, -EPERM otherwise.
1096 *
1097 * Caller holds tomoyo_read_lock().
1098 */
1099static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1100 const struct tomoyo_path_info *filename,
1101 const unsigned long number)
1102{
1103 struct tomoyo_domain_info *domain = r->domain;
1104 struct tomoyo_acl_info *ptr;
1105 const u8 perm = 1 << type;
1106 int error = -EPERM;
1107 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1108 struct tomoyo_path_number_acl *acl;
1109 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1110 continue;
1111 acl = container_of(ptr, struct tomoyo_path_number_acl,
1112 head);
1113 if (!(acl->perm & perm) ||
1114 !tomoyo_compare_number_union(number, &acl->number) ||
1115 !tomoyo_compare_name_union(filename, &acl->name))
1116 continue;
1117 error = 0;
1118 break;
1119 }
1120 return error;
1121}
1122
Tetsuo Handa237ab452010-06-12 20:46:22 +09001123static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
1124 const struct tomoyo_acl_info *b)
1125{
1126 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
1127 head);
1128 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
1129 head);
1130 return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1131 && tomoyo_is_same_name_union(&p1->name, &p2->name)
1132 && tomoyo_is_same_number_union(&p1->number, &p2->number);
1133}
1134
1135static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
1136 struct tomoyo_acl_info *b,
1137 const bool is_delete)
1138{
1139 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
1140 head)->perm;
1141 u8 perm = *a_perm;
1142 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
1143 ->perm;
1144 if (is_delete)
1145 perm &= ~b_perm;
1146 else
1147 perm |= b_perm;
1148 *a_perm = perm;
1149 return !perm;
1150}
1151
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001152/**
1153 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1154 *
1155 * @type: Type of operation.
1156 * @filename: Filename.
1157 * @number: Number.
1158 * @domain: Pointer to "struct tomoyo_domain_info".
1159 * @is_delete: True if it is a delete request.
1160 *
1161 * Returns 0 on success, negative value otherwise.
1162 */
Tetsuo Handa237ab452010-06-12 20:46:22 +09001163static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
1164 char *number,
1165 struct tomoyo_domain_info * const
1166 domain,
1167 const bool is_delete)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001168{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001169 struct tomoyo_path_number_acl e = {
1170 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handa237ab452010-06-12 20:46:22 +09001171 .perm = 1 << type
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001172 };
1173 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001174 if (!tomoyo_parse_name_union(filename, &e.name))
1175 return -EINVAL;
1176 if (!tomoyo_parse_number_union(number, &e.number))
1177 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001178 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
1179 tomoyo_same_path_number_acl,
1180 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001181 out:
1182 tomoyo_put_name_union(&e.name);
1183 tomoyo_put_number_union(&e.number);
1184 return error;
1185}
1186
1187/**
1188 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1189 *
1190 * @r: Pointer to "strct tomoyo_request_info".
1191 * @filename: Filename to check.
1192 * @number: Number.
1193 *
1194 * Returns 0 on success, negative value otherwise.
1195 *
1196 * Caller holds tomoyo_read_lock().
1197 */
1198static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1199 const u8 type,
1200 const struct tomoyo_path_info *filename,
1201 const unsigned long number)
1202{
1203 char buffer[64];
1204 int error;
1205 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001206 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001207
1208 if (!filename)
1209 return 0;
1210 switch (type) {
1211 case TOMOYO_TYPE_CREATE:
1212 case TOMOYO_TYPE_MKDIR:
1213 case TOMOYO_TYPE_MKFIFO:
1214 case TOMOYO_TYPE_MKSOCK:
1215 case TOMOYO_TYPE_CHMOD:
1216 radix = TOMOYO_VALUE_TYPE_OCTAL;
1217 break;
1218 case TOMOYO_TYPE_IOCTL:
1219 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1220 break;
1221 default:
1222 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1223 break;
1224 }
1225 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001226 do {
1227 error = tomoyo_path_number_acl(r, type, filename, number);
1228 if (!error)
1229 break;
1230 msg = tomoyo_path_number2keyword(type);
1231 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1232 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1233 tomoyo_file_pattern(filename),
1234 buffer);
1235 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001236 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1237 error = 0;
1238 return error;
1239}
1240
1241/**
1242 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1243 *
1244 * @type: Type of operation.
1245 * @path: Pointer to "struct path".
1246 * @number: Number.
1247 *
1248 * Returns 0 on success, negative value otherwise.
1249 */
1250int tomoyo_path_number_perm(const u8 type, struct path *path,
1251 unsigned long number)
1252{
1253 struct tomoyo_request_info r;
1254 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001255 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001256 int idx;
1257
Tetsuo Handa57c25902010-06-03 20:38:44 +09001258 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
1259 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001260 return 0;
1261 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001262 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001263 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001264 if (type == TOMOYO_TYPE_MKDIR)
1265 tomoyo_add_slash(&buf);
1266 error = tomoyo_path_number_perm2(&r, type, &buf, number);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001267 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001268 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001269 tomoyo_read_unlock(idx);
1270 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1271 error = 0;
1272 return error;
1273}
1274
1275/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001276 * tomoyo_check_exec_perm - Check permission for "execute".
1277 *
Tetsuo Handa57c25902010-06-03 20:38:44 +09001278 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001279 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001280 *
1281 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001282 *
1283 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001284 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001285int tomoyo_check_exec_perm(struct tomoyo_request_info *r,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001286 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001287{
Tetsuo Handa57c25902010-06-03 20:38:44 +09001288 if (r->mode == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001289 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001290 return tomoyo_file_perm(r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001291}
1292
1293/**
1294 * tomoyo_check_open_permission - Check permission for "read" and "write".
1295 *
1296 * @domain: Pointer to "struct tomoyo_domain_info".
1297 * @path: Pointer to "struct path".
1298 * @flag: Flags for open().
1299 *
1300 * Returns 0 on success, negative value otherwise.
1301 */
1302int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1303 struct path *path, const int flag)
1304{
1305 const u8 acc_mode = ACC_MODE(flag);
1306 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001307 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001308 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001309 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001310
Tetsuo Handa57c25902010-06-03 20:38:44 +09001311 if (!path->mnt ||
1312 (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001313 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001314 buf.name = NULL;
1315 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001316 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001317 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001318 goto out;
1319 error = 0;
1320 /*
1321 * If the filename is specified by "deny_rewrite" keyword,
1322 * we need to check "allow_rewrite" permission when the filename is not
1323 * opened for append mode or the filename is truncated at open time.
1324 */
Tetsuo Handa57c25902010-06-03 20:38:44 +09001325 if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
1326 && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
1327 != TOMOYO_CONFIG_DISABLED) {
1328 if (!tomoyo_get_realpath(&buf, path)) {
1329 error = -ENOMEM;
1330 goto out;
1331 }
1332 if (tomoyo_is_no_rewrite_file(&buf))
1333 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
1334 &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001335 }
Tetsuo Handa57c25902010-06-03 20:38:44 +09001336 if (!error && acc_mode &&
1337 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
1338 != TOMOYO_CONFIG_DISABLED) {
1339 if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
1340 error = -ENOMEM;
1341 goto out;
1342 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001343 error = tomoyo_file_perm(&r, &buf, acc_mode);
Tetsuo Handa57c25902010-06-03 20:38:44 +09001344 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001345 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001346 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001347 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001348 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001349 error = 0;
1350 return error;
1351}
1352
1353/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001354 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001355 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001356 * @operation: Type of operation.
1357 * @path: Pointer to "struct path".
1358 *
1359 * Returns 0 on success, negative value otherwise.
1360 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001361int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001362{
1363 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001364 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001365 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001366 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001367
Tetsuo Handa57c25902010-06-03 20:38:44 +09001368 if (!path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001369 return 0;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001370 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
1371 == TOMOYO_CONFIG_DISABLED)
1372 return 0;
1373 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001374 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001375 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001376 goto out;
1377 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001378 case TOMOYO_TYPE_REWRITE:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001379 if (!tomoyo_is_no_rewrite_file(&buf)) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001380 error = 0;
1381 goto out;
1382 }
1383 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001384 case TOMOYO_TYPE_RMDIR:
1385 case TOMOYO_TYPE_CHROOT:
Tetsuo Handa57c25902010-06-03 20:38:44 +09001386 case TOMOYO_TYPE_UMOUNT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001387 tomoyo_add_slash(&buf);
1388 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001389 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001390 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001391 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001392 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001393 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001394 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001395 error = 0;
1396 return error;
1397}
1398
1399/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001400 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1401 *
1402 * @r: Pointer to "struct tomoyo_request_info".
1403 * @operation: Type of operation.
1404 * @filename: Filename to check.
1405 * @mode: Create mode.
1406 * @dev: Device number.
1407 *
1408 * Returns 0 on success, negative value otherwise.
1409 *
1410 * Caller holds tomoyo_read_lock().
1411 */
1412static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1413 const u8 operation,
1414 const struct tomoyo_path_info *filename,
1415 const unsigned int mode,
1416 const unsigned int dev)
1417{
1418 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001419 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001420 const unsigned int major = MAJOR(dev);
1421 const unsigned int minor = MINOR(dev);
1422
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001423 do {
1424 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1425 mode, major, minor);
1426 if (!error)
1427 break;
1428 msg = tomoyo_path_number32keyword(operation);
1429 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1430 mode, major, minor);
1431 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1432 tomoyo_file_pattern(filename), mode,
1433 major, minor);
1434 } while (error == TOMOYO_RETRY_REQUEST);
1435 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001436 error = 0;
1437 return error;
1438}
1439
1440/**
1441 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1442 *
1443 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1444 * @path: Pointer to "struct path".
1445 * @mode: Create mode.
1446 * @dev: Device number.
1447 *
1448 * Returns 0 on success, negative value otherwise.
1449 */
1450int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1451 const unsigned int mode, unsigned int dev)
1452{
1453 struct tomoyo_request_info r;
1454 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001455 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001456 int idx;
1457
Tetsuo Handa57c25902010-06-03 20:38:44 +09001458 if (!path->mnt ||
1459 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
1460 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001461 return 0;
1462 idx = tomoyo_read_lock();
1463 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001464 if (tomoyo_get_realpath(&buf, path)) {
1465 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001466 new_decode_dev(dev));
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001467 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001468 }
1469 tomoyo_read_unlock(idx);
1470 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1471 error = 0;
1472 return error;
1473}
1474
1475/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001476 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001477 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001478 * @operation: Type of operation.
1479 * @path1: Pointer to "struct path".
1480 * @path2: Pointer to "struct path".
1481 *
1482 * Returns 0 on success, negative value otherwise.
1483 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001484int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001485 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001486{
1487 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001488 const char *msg;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001489 struct tomoyo_path_info buf1;
1490 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001491 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001492 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001493
Tetsuo Handa57c25902010-06-03 20:38:44 +09001494 if (!path1->mnt || !path2->mnt ||
1495 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
1496 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001497 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001498 buf1.name = NULL;
1499 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001500 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001501 if (!tomoyo_get_realpath(&buf1, path1) ||
1502 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001503 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001504 switch (operation) {
1505 struct dentry *dentry;
1506 case TOMOYO_TYPE_RENAME:
1507 case TOMOYO_TYPE_LINK:
1508 dentry = path1->dentry;
1509 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
1510 break;
1511 /* fall through */
1512 case TOMOYO_TYPE_PIVOT_ROOT:
1513 tomoyo_add_slash(&buf1);
1514 tomoyo_add_slash(&buf2);
1515 break;
1516 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001517 do {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001518 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001519 if (!error)
1520 break;
1521 msg = tomoyo_path22keyword(operation);
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001522 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001523 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001524 tomoyo_file_pattern(&buf1),
1525 tomoyo_file_pattern(&buf2));
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001526 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001527 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001528 kfree(buf1.name);
1529 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001530 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001531 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001532 error = 0;
1533 return error;
1534}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001535
1536/**
1537 * tomoyo_write_file_policy - Update file related list.
1538 *
1539 * @data: String to parse.
1540 * @domain: Pointer to "struct tomoyo_domain_info".
1541 * @is_delete: True if it is a delete request.
1542 *
1543 * Returns 0 on success, negative value otherwise.
1544 *
1545 * Caller holds tomoyo_read_lock().
1546 */
1547int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1548 const bool is_delete)
1549{
1550 char *w[5];
1551 u8 type;
1552 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1553 return -EINVAL;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001554 if (strncmp(w[0], "allow_", 6))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001555 goto out;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001556 w[0] += 6;
1557 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1558 if (strcmp(w[0], tomoyo_path_keyword[type]))
1559 continue;
1560 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1561 }
1562 if (!w[2][0])
1563 goto out;
1564 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1565 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1566 continue;
1567 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1568 is_delete);
1569 }
1570 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1571 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1572 continue;
1573 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1574 is_delete);
1575 }
1576 if (!w[3][0] || !w[4][0])
1577 goto out;
1578 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1579 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1580 continue;
1581 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1582 w[4], domain, is_delete);
1583 }
1584 out:
1585 return -EINVAL;
1586}