blob: 83fa17a1113a0d67f01d83047216532b4e43c82e [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 Handa7762fbf2010-05-10 17:30:26 +090054void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
55{
56 if (!ptr)
57 return;
58 if (ptr->is_group)
59 tomoyo_put_path_group(ptr->group);
60 else
61 tomoyo_put_name(ptr->filename);
62}
63
64bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
65 const struct tomoyo_name_union *ptr)
66{
67 if (ptr->is_group)
Tetsuo Handa3f629632010-06-03 20:37:26 +090068 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090069 return tomoyo_path_matches_pattern(name, ptr->filename);
70}
71
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090072void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
73{
74 if (ptr && ptr->is_group)
75 tomoyo_put_number_group(ptr->group);
76}
77
78bool tomoyo_compare_number_union(const unsigned long value,
79 const struct tomoyo_number_union *ptr)
80{
81 if (ptr->is_group)
82 return tomoyo_number_matches_group(value, value, ptr->group);
83 return value >= ptr->values[0] && value <= ptr->values[1];
84}
85
Kentaro Takedab69a54e2009-02-05 17:18:14 +090086/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +090087 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +090088 *
89 * @operation: Type of operation.
90 *
91 * Returns the name of single path operation.
92 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +090093const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +090094{
Tetsuo Handa7ef61232010-02-16 08:03:30 +090095 return (operation < TOMOYO_MAX_PATH_OPERATION)
96 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +090097}
98
99/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900100 * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
101 *
102 * @operation: Type of operation.
103 *
104 * Returns the name of path/number/number/number operation.
105 */
106const char *tomoyo_path_number32keyword(const u8 operation)
107{
108 return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
109 ? tomoyo_path_number3_keyword[operation] : NULL;
110}
111
112/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900113 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900114 *
115 * @operation: Type of operation.
116 *
117 * Returns the name of double path operation.
118 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900119const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900120{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900121 return (operation < TOMOYO_MAX_PATH2_OPERATION)
122 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900123}
124
125/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900126 * tomoyo_path_number2keyword - Get the name of path/number operations.
127 *
128 * @operation: Type of operation.
129 *
130 * Returns the name of path/number operation.
131 */
132const char *tomoyo_path_number2keyword(const u8 operation)
133{
134 return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
135 ? tomoyo_path_number_keyword[operation] : NULL;
136}
137
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900138static void tomoyo_add_slash(struct tomoyo_path_info *buf)
139{
140 if (buf->is_dir)
141 return;
142 /*
143 * This is OK because tomoyo_encode() reserves space for appending "/".
144 */
145 strcat((char *) buf->name, "/");
146 tomoyo_fill_path_info(buf);
147}
148
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900149/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900150 * tomoyo_strendswith - Check whether the token ends with the given token.
151 *
152 * @name: The token to check.
153 * @tail: The token to find.
154 *
155 * Returns true if @name ends with @tail, false otherwise.
156 */
157static bool tomoyo_strendswith(const char *name, const char *tail)
158{
159 int len;
160
161 if (!name || !tail)
162 return false;
163 len = strlen(name) - strlen(tail);
164 return len >= 0 && !strcmp(name + len, tail);
165}
166
167/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900168 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900169 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900170 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900171 * @path: Pointer to "struct path".
172 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900173 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900174 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900175static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900176{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900177 buf->name = tomoyo_realpath_from_path(path);
178 if (buf->name) {
179 tomoyo_fill_path_info(buf);
180 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900181 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900182 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900183}
184
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900185static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
186 const char *filename2,
187 struct tomoyo_domain_info *const domain,
188 const bool is_delete);
189static int tomoyo_update_path_acl(const u8 type, const char *filename,
190 struct tomoyo_domain_info *const domain,
191 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900192
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900193/*
194 * tomoyo_globally_readable_list is used for holding list of pathnames which
195 * are by default allowed to be open()ed for reading by any process.
196 *
197 * An entry is added by
198 *
199 * # echo 'allow_read /lib/libc-2.5.so' > \
200 * /sys/kernel/security/tomoyo/exception_policy
201 *
202 * and is deleted by
203 *
204 * # echo 'delete allow_read /lib/libc-2.5.so' > \
205 * /sys/kernel/security/tomoyo/exception_policy
206 *
207 * and all entries are retrieved by
208 *
209 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
210 *
211 * In the example above, any process is allowed to
212 * open("/lib/libc-2.5.so", O_RDONLY).
213 * One exception is, if the domain which current process belongs to is marked
214 * as "ignore_global_allow_read", current process can't do so unless explicitly
215 * given "allow_read /lib/libc-2.5.so" to the domain which current process
216 * belongs to.
217 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900218LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900219
220/**
221 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
222 *
223 * @filename: Filename unconditionally permitted to open() for reading.
224 * @is_delete: True if it is a delete request.
225 *
226 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900227 *
228 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900229 */
230static int tomoyo_update_globally_readable_entry(const char *filename,
231 const bool is_delete)
232{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900233 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900234 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900235 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900236
Tetsuo Handa3f629632010-06-03 20:37:26 +0900237 if (!tomoyo_is_correct_word(filename))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900238 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900239 e.filename = tomoyo_get_name(filename);
240 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900241 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900242 if (mutex_lock_interruptible(&tomoyo_policy_lock))
243 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900244 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900245 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900246 continue;
247 ptr->is_deleted = is_delete;
248 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900249 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900250 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900251 if (!is_delete && error) {
252 struct tomoyo_globally_readable_file_entry *entry =
253 tomoyo_commit_ok(&e, sizeof(e));
254 if (entry) {
255 list_add_tail_rcu(&entry->list,
256 &tomoyo_globally_readable_list);
257 error = 0;
258 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900259 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900260 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900261 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900262 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900263 return error;
264}
265
266/**
267 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
268 *
269 * @filename: The filename to check.
270 *
271 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900272 *
273 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900274 */
275static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
276 filename)
277{
278 struct tomoyo_globally_readable_file_entry *ptr;
279 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900280
281 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900282 if (!ptr->is_deleted &&
283 tomoyo_path_matches_pattern(filename, ptr->filename)) {
284 found = true;
285 break;
286 }
287 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900288 return found;
289}
290
291/**
292 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
293 *
294 * @data: String to parse.
295 * @is_delete: True if it is a delete request.
296 *
297 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900298 *
299 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900300 */
301int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
302{
303 return tomoyo_update_globally_readable_entry(data, is_delete);
304}
305
306/**
307 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
308 *
309 * @head: Pointer to "struct tomoyo_io_buffer".
310 *
311 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900312 *
313 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900314 */
315bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
316{
317 struct list_head *pos;
318 bool done = true;
319
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900320 list_for_each_cookie(pos, head->read_var2,
321 &tomoyo_globally_readable_list) {
322 struct tomoyo_globally_readable_file_entry *ptr;
323 ptr = list_entry(pos,
324 struct tomoyo_globally_readable_file_entry,
325 list);
326 if (ptr->is_deleted)
327 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900328 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
329 ptr->filename->name);
330 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900331 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900332 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900333 return done;
334}
335
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900336/* tomoyo_pattern_list is used for holding list of pathnames which are used for
337 * converting pathnames to pathname patterns during learning mode.
338 *
339 * An entry is added by
340 *
341 * # echo 'file_pattern /proc/\$/mounts' > \
342 * /sys/kernel/security/tomoyo/exception_policy
343 *
344 * and is deleted by
345 *
346 * # echo 'delete file_pattern /proc/\$/mounts' > \
347 * /sys/kernel/security/tomoyo/exception_policy
348 *
349 * and all entries are retrieved by
350 *
351 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
352 *
353 * In the example above, if a process which belongs to a domain which is in
354 * learning mode requested open("/proc/1/mounts", O_RDONLY),
355 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
356 * process belongs to.
357 *
358 * It is not a desirable behavior that we have to use /proc/\$/ instead of
359 * /proc/self/ when current process needs to access only current process's
360 * information. As of now, LSM version of TOMOYO is using __d_path() for
361 * calculating pathname. Non LSM version of TOMOYO is using its own function
362 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
363 * current process from accessing other process's information.
364 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900365LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900366
367/**
368 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
369 *
370 * @pattern: Pathname pattern.
371 * @is_delete: True if it is a delete request.
372 *
373 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900374 *
375 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900376 */
377static int tomoyo_update_file_pattern_entry(const char *pattern,
378 const bool is_delete)
379{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900380 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900381 struct tomoyo_pattern_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900382 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900383
Tetsuo Handa3f629632010-06-03 20:37:26 +0900384 if (!tomoyo_is_correct_word(pattern))
385 return -EINVAL;
386 e.pattern = tomoyo_get_name(pattern);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900387 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900388 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900389 if (mutex_lock_interruptible(&tomoyo_policy_lock))
390 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900391 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900392 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900393 continue;
394 ptr->is_deleted = is_delete;
395 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900396 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900397 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900398 if (!is_delete && error) {
399 struct tomoyo_pattern_entry *entry =
400 tomoyo_commit_ok(&e, sizeof(e));
401 if (entry) {
402 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
403 error = 0;
404 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900405 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900406 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900407 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900408 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900409 return error;
410}
411
412/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900413 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900414 *
415 * @filename: The filename to find patterned pathname.
416 *
417 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900418 *
419 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900420 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900421const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900422{
423 struct tomoyo_pattern_entry *ptr;
424 const struct tomoyo_path_info *pattern = NULL;
425
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900426 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900427 if (ptr->is_deleted)
428 continue;
429 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
430 continue;
431 pattern = ptr->pattern;
432 if (tomoyo_strendswith(pattern->name, "/\\*")) {
433 /* Do nothing. Try to find the better match. */
434 } else {
435 /* This would be the better match. Use this. */
436 break;
437 }
438 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900439 if (pattern)
440 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900441 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900442}
443
444/**
445 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
446 *
447 * @data: String to parse.
448 * @is_delete: True if it is a delete request.
449 *
450 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900451 *
452 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900453 */
454int tomoyo_write_pattern_policy(char *data, const bool is_delete)
455{
456 return tomoyo_update_file_pattern_entry(data, is_delete);
457}
458
459/**
460 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
461 *
462 * @head: Pointer to "struct tomoyo_io_buffer".
463 *
464 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900465 *
466 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900467 */
468bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
469{
470 struct list_head *pos;
471 bool done = true;
472
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900473 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
474 struct tomoyo_pattern_entry *ptr;
475 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
476 if (ptr->is_deleted)
477 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900478 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
479 "%s\n", ptr->pattern->name);
480 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900481 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900482 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900483 return done;
484}
485
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900486/*
487 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
488 * default forbidden to modify already written content of a file.
489 *
490 * An entry is added by
491 *
492 * # echo 'deny_rewrite /var/log/messages' > \
493 * /sys/kernel/security/tomoyo/exception_policy
494 *
495 * and is deleted by
496 *
497 * # echo 'delete deny_rewrite /var/log/messages' > \
498 * /sys/kernel/security/tomoyo/exception_policy
499 *
500 * and all entries are retrieved by
501 *
502 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
503 *
504 * In the example above, if a process requested to rewrite /var/log/messages ,
505 * the process can't rewrite unless the domain which that process belongs to
506 * has "allow_rewrite /var/log/messages" entry.
507 *
508 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
509 * when we want to allow rewriting already unlink()ed file. As of now,
510 * LSM version of TOMOYO is using __d_path() for calculating pathname.
511 * Non LSM version of TOMOYO is using its own function which doesn't append
512 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
513 * need to worry whether the file is already unlink()ed or not.
514 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900515LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900516
517/**
518 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
519 *
520 * @pattern: Pathname pattern that are not rewritable by default.
521 * @is_delete: True if it is a delete request.
522 *
523 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900524 *
525 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900526 */
527static int tomoyo_update_no_rewrite_entry(const char *pattern,
528 const bool is_delete)
529{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900530 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900531 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900532 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900533
Tetsuo Handa3f629632010-06-03 20:37:26 +0900534 if (!tomoyo_is_correct_word(pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900535 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900536 e.pattern = tomoyo_get_name(pattern);
537 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900538 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900539 if (mutex_lock_interruptible(&tomoyo_policy_lock))
540 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900541 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900542 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900543 continue;
544 ptr->is_deleted = is_delete;
545 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900546 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900547 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900548 if (!is_delete && error) {
549 struct tomoyo_no_rewrite_entry *entry =
550 tomoyo_commit_ok(&e, sizeof(e));
551 if (entry) {
552 list_add_tail_rcu(&entry->list,
553 &tomoyo_no_rewrite_list);
554 error = 0;
555 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900556 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900557 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900558 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900559 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900560 return error;
561}
562
563/**
564 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
565 *
566 * @filename: Filename to check.
567 *
568 * Returns true if @filename is specified by "deny_rewrite" directive,
569 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900570 *
571 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900572 */
573static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
574{
575 struct tomoyo_no_rewrite_entry *ptr;
576 bool found = false;
577
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900578 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900579 if (ptr->is_deleted)
580 continue;
581 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
582 continue;
583 found = true;
584 break;
585 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900586 return found;
587}
588
589/**
590 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
591 *
592 * @data: String to parse.
593 * @is_delete: True if it is a delete request.
594 *
595 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900596 *
597 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900598 */
599int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
600{
601 return tomoyo_update_no_rewrite_entry(data, is_delete);
602}
603
604/**
605 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
606 *
607 * @head: Pointer to "struct tomoyo_io_buffer".
608 *
609 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900610 *
611 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900612 */
613bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
614{
615 struct list_head *pos;
616 bool done = true;
617
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900618 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
619 struct tomoyo_no_rewrite_entry *ptr;
620 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
621 if (ptr->is_deleted)
622 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900623 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
624 "%s\n", ptr->pattern->name);
625 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900626 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900627 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900628 return done;
629}
630
631/**
632 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
633 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900634 * @perm: Permission (between 1 to 7).
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900635 * @filename: Filename.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900636 * @domain: Pointer to "struct tomoyo_domain_info".
637 * @is_delete: True if it is a delete request.
638 *
639 * Returns 0 on success, negative value otherwise.
640 *
641 * This is legacy support interface for older policy syntax.
642 * Current policy syntax uses "allow_read/write" instead of "6",
643 * "allow_read" instead of "4", "allow_write" instead of "2",
644 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900645 *
646 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900647 */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900648static int tomoyo_update_file_acl(u8 perm, const char *filename,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900649 struct tomoyo_domain_info * const domain,
650 const bool is_delete)
651{
652 if (perm > 7 || !perm) {
653 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
654 __func__, perm, filename);
655 return -EINVAL;
656 }
657 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
658 /*
659 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
660 * directory permissions.
661 */
662 return 0;
663 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900664 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
665 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900666 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900667 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
668 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900669 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900670 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
671 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900672 return 0;
673}
674
675/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900676 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900677 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900678 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900679 * @filename: Filename to check.
680 * @perm: Permission.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900681 *
682 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900683 *
684 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900685 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900686static int tomoyo_path_acl(const struct tomoyo_request_info *r,
687 const struct tomoyo_path_info *filename,
Tetsuo Handa3f629632010-06-03 20:37:26 +0900688 const u32 perm)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900689{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900690 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900691 struct tomoyo_acl_info *ptr;
692 int error = -EPERM;
693
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900694 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900695 struct tomoyo_path_acl *acl;
696 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900697 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900698 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900699 if (!(acl->perm & perm) ||
Tetsuo Handa3f629632010-06-03 20:37:26 +0900700 !tomoyo_compare_name_union(filename, &acl->name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900701 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900702 error = 0;
703 break;
704 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900705 return error;
706}
707
708/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900709 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900710 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900711 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900712 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900713 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900714 *
715 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900716 *
717 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900718 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900719static int tomoyo_file_perm(struct tomoyo_request_info *r,
720 const struct tomoyo_path_info *filename,
721 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900722{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900723 const char *msg = "<unknown>";
724 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900725 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900726
727 if (!filename)
728 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900729
730 if (mode == 6) {
731 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
732 perm = 1 << TOMOYO_TYPE_READ_WRITE;
733 } else if (mode == 4) {
734 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
735 perm = 1 << TOMOYO_TYPE_READ;
736 } else if (mode == 2) {
737 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
738 perm = 1 << TOMOYO_TYPE_WRITE;
739 } else if (mode == 1) {
740 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
741 perm = 1 << TOMOYO_TYPE_EXECUTE;
742 } else
743 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900744 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900745 error = tomoyo_path_acl(r, filename, perm);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900746 if (error && mode == 4 && !r->domain->ignore_global_allow_read
747 && tomoyo_is_globally_readable_file(filename))
748 error = 0;
749 if (!error)
750 break;
751 tomoyo_warn_log(r, "%s %s", msg, filename->name);
752 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900753 tomoyo_file_pattern(filename));
754 /*
755 * Do not retry for execute request, for alias may have
756 * changed.
757 */
758 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
759 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900760 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900761 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900762}
763
764/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900765 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900766 *
767 * @type: Type of operation.
768 * @filename: Filename.
769 * @domain: Pointer to "struct tomoyo_domain_info".
770 * @is_delete: True if it is a delete request.
771 *
772 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900773 *
774 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900775 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900776static int tomoyo_update_path_acl(const u8 type, const char *filename,
777 struct tomoyo_domain_info *const domain,
778 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900779{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900780 static const u16 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900781 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900782 const u16 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900783 struct tomoyo_acl_info *ptr;
784 struct tomoyo_path_acl e = {
785 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900786 .perm = perm
787 };
788 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900789
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900790 if (type == TOMOYO_TYPE_READ_WRITE)
791 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900792 if (!domain)
793 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900794 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900795 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900796 if (mutex_lock_interruptible(&tomoyo_policy_lock))
797 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900798 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900799 struct tomoyo_path_acl *acl =
800 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900801 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900802 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900803 if (is_delete) {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900804 acl->perm &= ~perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900805 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900806 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
807 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900808 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900809 } else {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900810 acl->perm |= perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900811 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900812 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
813 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900814 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900815 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900816 error = 0;
817 break;
818 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900819 if (!is_delete && error) {
820 struct tomoyo_path_acl *entry =
821 tomoyo_commit_ok(&e, sizeof(e));
822 if (entry) {
823 list_add_tail_rcu(&entry->head.list,
824 &domain->acl_info_list);
825 error = 0;
826 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900827 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900828 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900829 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900830 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900831 return error;
832}
833
834/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900835 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
836 *
837 * @type: Type of operation.
838 * @filename: Filename.
839 * @mode: Create mode.
840 * @major: Device major number.
841 * @minor: Device minor number.
842 * @domain: Pointer to "struct tomoyo_domain_info".
843 * @is_delete: True if it is a delete request.
844 *
845 * Returns 0 on success, negative value otherwise.
846 */
847static inline int tomoyo_update_path_number3_acl(const u8 type,
848 const char *filename,
849 char *mode,
850 char *major, char *minor,
851 struct tomoyo_domain_info *
852 const domain,
853 const bool is_delete)
854{
855 const u8 perm = 1 << type;
856 struct tomoyo_acl_info *ptr;
857 struct tomoyo_path_number3_acl e = {
858 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
859 .perm = perm
860 };
861 int error = is_delete ? -ENOENT : -ENOMEM;
862 if (!tomoyo_parse_name_union(filename, &e.name) ||
863 !tomoyo_parse_number_union(mode, &e.mode) ||
864 !tomoyo_parse_number_union(major, &e.major) ||
865 !tomoyo_parse_number_union(minor, &e.minor))
866 goto out;
867 if (mutex_lock_interruptible(&tomoyo_policy_lock))
868 goto out;
869 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
870 struct tomoyo_path_number3_acl *acl =
871 container_of(ptr, struct tomoyo_path_number3_acl, head);
872 if (!tomoyo_is_same_path_number3_acl(acl, &e))
873 continue;
874 if (is_delete)
875 acl->perm &= ~perm;
876 else
877 acl->perm |= perm;
878 error = 0;
879 break;
880 }
881 if (!is_delete && error) {
882 struct tomoyo_path_number3_acl *entry =
883 tomoyo_commit_ok(&e, sizeof(e));
884 if (entry) {
885 list_add_tail_rcu(&entry->head.list,
886 &domain->acl_info_list);
887 error = 0;
888 }
889 }
890 mutex_unlock(&tomoyo_policy_lock);
891 out:
892 tomoyo_put_name_union(&e.name);
893 tomoyo_put_number_union(&e.mode);
894 tomoyo_put_number_union(&e.major);
895 tomoyo_put_number_union(&e.minor);
896 return error;
897}
898
899/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900900 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900901 *
902 * @type: Type of operation.
903 * @filename1: First filename.
904 * @filename2: Second filename.
905 * @domain: Pointer to "struct tomoyo_domain_info".
906 * @is_delete: True if it is a delete request.
907 *
908 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900909 *
910 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900911 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900912static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
913 const char *filename2,
914 struct tomoyo_domain_info *const domain,
915 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900916{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900917 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900918 struct tomoyo_path2_acl e = {
919 .head.type = TOMOYO_TYPE_PATH2_ACL,
920 .perm = perm
921 };
922 struct tomoyo_acl_info *ptr;
923 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900924
925 if (!domain)
926 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900927 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
928 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900929 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900930 if (mutex_lock_interruptible(&tomoyo_policy_lock))
931 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900932 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900933 struct tomoyo_path2_acl *acl =
934 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900935 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900936 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900937 if (is_delete)
938 acl->perm &= ~perm;
939 else
940 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900941 error = 0;
942 break;
943 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900944 if (!is_delete && error) {
945 struct tomoyo_path2_acl *entry =
946 tomoyo_commit_ok(&e, sizeof(e));
947 if (entry) {
948 list_add_tail_rcu(&entry->head.list,
949 &domain->acl_info_list);
950 error = 0;
951 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900952 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900953 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900954 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900955 tomoyo_put_name_union(&e.name1);
956 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900957 return error;
958}
959
960/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900961 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
962 *
963 * @r: Pointer to "struct tomoyo_request_info".
964 * @filename: Filename to check.
965 * @perm: Permission.
966 * @mode: Create mode.
967 * @major: Device major number.
968 * @minor: Device minor number.
969 *
970 * Returns 0 on success, -EPERM otherwise.
971 *
972 * Caller holds tomoyo_read_lock().
973 */
974static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
975 const struct tomoyo_path_info *filename,
976 const u16 perm, const unsigned int mode,
977 const unsigned int major,
978 const unsigned int minor)
979{
980 struct tomoyo_domain_info *domain = r->domain;
981 struct tomoyo_acl_info *ptr;
982 int error = -EPERM;
983 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
984 struct tomoyo_path_number3_acl *acl;
985 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
986 continue;
987 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
988 if (!tomoyo_compare_number_union(mode, &acl->mode))
989 continue;
990 if (!tomoyo_compare_number_union(major, &acl->major))
991 continue;
992 if (!tomoyo_compare_number_union(minor, &acl->minor))
993 continue;
994 if (!(acl->perm & perm))
995 continue;
996 if (!tomoyo_compare_name_union(filename, &acl->name))
997 continue;
998 error = 0;
999 break;
1000 }
1001 return error;
1002}
1003
1004/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001005 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001006 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001007 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001008 * @type: Type of operation.
1009 * @filename1: First filename to check.
1010 * @filename2: Second filename to check.
1011 *
1012 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001013 *
1014 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001015 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001016static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001017 const struct tomoyo_path_info *filename1,
1018 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001019{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001020 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001021 struct tomoyo_acl_info *ptr;
1022 const u8 perm = 1 << type;
1023 int error = -EPERM;
1024
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001025 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001026 struct tomoyo_path2_acl *acl;
1027 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001028 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001029 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001030 if (!(acl->perm & perm))
1031 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001032 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001033 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001034 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001035 continue;
1036 error = 0;
1037 break;
1038 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001039 return error;
1040}
1041
1042/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001043 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001044 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001045 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001046 * @operation: Type of operation.
1047 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001048 *
1049 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001050 *
1051 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001052 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001053static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1054 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001055{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001056 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001057 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001058
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001059 next:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001060 do {
Tetsuo Handa3f629632010-06-03 20:37:26 +09001061 error = tomoyo_path_acl(r, filename, 1 << operation);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001062 if (!error)
1063 break;
1064 msg = tomoyo_path2keyword(operation);
1065 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1066 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1067 tomoyo_file_pattern(filename));
1068 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001069 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001070 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001071 /*
1072 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1073 * we need to check "allow_rewrite" permission if the filename is
1074 * specified by "deny_rewrite" keyword.
1075 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001076 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001077 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001078 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001079 goto next;
1080 }
1081 return error;
1082}
1083
1084/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001085 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1086 *
1087 * @r: Pointer to "struct tomoyo_request_info".
1088 * @type: Operation.
1089 * @filename: Filename to check.
1090 * @number: Number.
1091 *
1092 * Returns 0 on success, -EPERM otherwise.
1093 *
1094 * Caller holds tomoyo_read_lock().
1095 */
1096static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1097 const struct tomoyo_path_info *filename,
1098 const unsigned long number)
1099{
1100 struct tomoyo_domain_info *domain = r->domain;
1101 struct tomoyo_acl_info *ptr;
1102 const u8 perm = 1 << type;
1103 int error = -EPERM;
1104 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1105 struct tomoyo_path_number_acl *acl;
1106 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1107 continue;
1108 acl = container_of(ptr, struct tomoyo_path_number_acl,
1109 head);
1110 if (!(acl->perm & perm) ||
1111 !tomoyo_compare_number_union(number, &acl->number) ||
1112 !tomoyo_compare_name_union(filename, &acl->name))
1113 continue;
1114 error = 0;
1115 break;
1116 }
1117 return error;
1118}
1119
1120/**
1121 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1122 *
1123 * @type: Type of operation.
1124 * @filename: Filename.
1125 * @number: Number.
1126 * @domain: Pointer to "struct tomoyo_domain_info".
1127 * @is_delete: True if it is a delete request.
1128 *
1129 * Returns 0 on success, negative value otherwise.
1130 */
1131static inline int tomoyo_update_path_number_acl(const u8 type,
1132 const char *filename,
1133 char *number,
1134 struct tomoyo_domain_info *
1135 const domain,
1136 const bool is_delete)
1137{
1138 const u8 perm = 1 << type;
1139 struct tomoyo_acl_info *ptr;
1140 struct tomoyo_path_number_acl e = {
1141 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1142 .perm = perm
1143 };
1144 int error = is_delete ? -ENOENT : -ENOMEM;
1145 if (!domain)
1146 return -EINVAL;
1147 if (!tomoyo_parse_name_union(filename, &e.name))
1148 return -EINVAL;
1149 if (!tomoyo_parse_number_union(number, &e.number))
1150 goto out;
1151 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1152 goto out;
1153 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1154 struct tomoyo_path_number_acl *acl =
1155 container_of(ptr, struct tomoyo_path_number_acl, head);
1156 if (!tomoyo_is_same_path_number_acl(acl, &e))
1157 continue;
1158 if (is_delete)
1159 acl->perm &= ~perm;
1160 else
1161 acl->perm |= perm;
1162 error = 0;
1163 break;
1164 }
1165 if (!is_delete && error) {
1166 struct tomoyo_path_number_acl *entry =
1167 tomoyo_commit_ok(&e, sizeof(e));
1168 if (entry) {
1169 list_add_tail_rcu(&entry->head.list,
1170 &domain->acl_info_list);
1171 error = 0;
1172 }
1173 }
1174 mutex_unlock(&tomoyo_policy_lock);
1175 out:
1176 tomoyo_put_name_union(&e.name);
1177 tomoyo_put_number_union(&e.number);
1178 return error;
1179}
1180
1181/**
1182 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1183 *
1184 * @r: Pointer to "strct tomoyo_request_info".
1185 * @filename: Filename to check.
1186 * @number: Number.
1187 *
1188 * Returns 0 on success, negative value otherwise.
1189 *
1190 * Caller holds tomoyo_read_lock().
1191 */
1192static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1193 const u8 type,
1194 const struct tomoyo_path_info *filename,
1195 const unsigned long number)
1196{
1197 char buffer[64];
1198 int error;
1199 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001200 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001201
1202 if (!filename)
1203 return 0;
1204 switch (type) {
1205 case TOMOYO_TYPE_CREATE:
1206 case TOMOYO_TYPE_MKDIR:
1207 case TOMOYO_TYPE_MKFIFO:
1208 case TOMOYO_TYPE_MKSOCK:
1209 case TOMOYO_TYPE_CHMOD:
1210 radix = TOMOYO_VALUE_TYPE_OCTAL;
1211 break;
1212 case TOMOYO_TYPE_IOCTL:
1213 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1214 break;
1215 default:
1216 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1217 break;
1218 }
1219 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001220 do {
1221 error = tomoyo_path_number_acl(r, type, filename, number);
1222 if (!error)
1223 break;
1224 msg = tomoyo_path_number2keyword(type);
1225 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1226 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1227 tomoyo_file_pattern(filename),
1228 buffer);
1229 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001230 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1231 error = 0;
1232 return error;
1233}
1234
1235/**
1236 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1237 *
1238 * @type: Type of operation.
1239 * @path: Pointer to "struct path".
1240 * @number: Number.
1241 *
1242 * Returns 0 on success, negative value otherwise.
1243 */
1244int tomoyo_path_number_perm(const u8 type, struct path *path,
1245 unsigned long number)
1246{
1247 struct tomoyo_request_info r;
1248 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001249 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001250 int idx;
1251
1252 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1253 !path->mnt || !path->dentry)
1254 return 0;
1255 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001256 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001257 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001258 if (type == TOMOYO_TYPE_MKDIR)
1259 tomoyo_add_slash(&buf);
1260 error = tomoyo_path_number_perm2(&r, type, &buf, number);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001261 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001262 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001263 tomoyo_read_unlock(idx);
1264 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1265 error = 0;
1266 return error;
1267}
1268
1269/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001270 * tomoyo_check_exec_perm - Check permission for "execute".
1271 *
1272 * @domain: Pointer to "struct tomoyo_domain_info".
1273 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001274 *
1275 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001276 *
1277 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001278 */
1279int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001280 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001281{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001282 struct tomoyo_request_info r;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001283
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001284 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001285 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001286 return tomoyo_file_perm(&r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001287}
1288
1289/**
1290 * tomoyo_check_open_permission - Check permission for "read" and "write".
1291 *
1292 * @domain: Pointer to "struct tomoyo_domain_info".
1293 * @path: Pointer to "struct path".
1294 * @flag: Flags for open().
1295 *
1296 * Returns 0 on success, negative value otherwise.
1297 */
1298int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1299 struct path *path, const int flag)
1300{
1301 const u8 acc_mode = ACC_MODE(flag);
1302 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001303 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001304 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001305 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001306
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001307 if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
1308 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001309 return 0;
1310 if (acc_mode == 0)
1311 return 0;
1312 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1313 /*
1314 * I don't check directories here because mkdir() and rmdir()
1315 * don't call me.
1316 */
1317 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001318 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001319 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001320 goto out;
1321 error = 0;
1322 /*
1323 * If the filename is specified by "deny_rewrite" keyword,
1324 * we need to check "allow_rewrite" permission when the filename is not
1325 * opened for append mode or the filename is truncated at open time.
1326 */
1327 if ((acc_mode & MAY_WRITE) &&
1328 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001329 (tomoyo_is_no_rewrite_file(&buf))) {
1330 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001331 }
1332 if (!error)
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001333 error = tomoyo_file_perm(&r, &buf, acc_mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001334 if (!error && (flag & O_TRUNC))
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001335 error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001336 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001337 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001338 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001339 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001340 error = 0;
1341 return error;
1342}
1343
1344/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001345 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001346 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001347 * @operation: Type of operation.
1348 * @path: Pointer to "struct path".
1349 *
1350 * Returns 0 on success, negative value otherwise.
1351 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001352int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001353{
1354 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001355 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001356 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001357 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001358
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001359 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1360 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001361 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001362 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001363 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001364 goto out;
1365 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001366 case TOMOYO_TYPE_REWRITE:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001367 if (!tomoyo_is_no_rewrite_file(&buf)) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001368 error = 0;
1369 goto out;
1370 }
1371 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001372 case TOMOYO_TYPE_RMDIR:
1373 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001374 tomoyo_add_slash(&buf);
1375 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001376 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001377 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001378 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001379 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001380 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001381 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001382 error = 0;
1383 return error;
1384}
1385
1386/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001387 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1388 *
1389 * @r: Pointer to "struct tomoyo_request_info".
1390 * @operation: Type of operation.
1391 * @filename: Filename to check.
1392 * @mode: Create mode.
1393 * @dev: Device number.
1394 *
1395 * Returns 0 on success, negative value otherwise.
1396 *
1397 * Caller holds tomoyo_read_lock().
1398 */
1399static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1400 const u8 operation,
1401 const struct tomoyo_path_info *filename,
1402 const unsigned int mode,
1403 const unsigned int dev)
1404{
1405 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001406 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001407 const unsigned int major = MAJOR(dev);
1408 const unsigned int minor = MINOR(dev);
1409
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001410 do {
1411 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1412 mode, major, minor);
1413 if (!error)
1414 break;
1415 msg = tomoyo_path_number32keyword(operation);
1416 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1417 mode, major, minor);
1418 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1419 tomoyo_file_pattern(filename), mode,
1420 major, minor);
1421 } while (error == TOMOYO_RETRY_REQUEST);
1422 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001423 error = 0;
1424 return error;
1425}
1426
1427/**
1428 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1429 *
1430 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1431 * @path: Pointer to "struct path".
1432 * @mode: Create mode.
1433 * @dev: Device number.
1434 *
1435 * Returns 0 on success, negative value otherwise.
1436 */
1437int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1438 const unsigned int mode, unsigned int dev)
1439{
1440 struct tomoyo_request_info r;
1441 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001442 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001443 int idx;
1444
1445 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1446 !path->mnt)
1447 return 0;
1448 idx = tomoyo_read_lock();
1449 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001450 if (tomoyo_get_realpath(&buf, path)) {
1451 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001452 new_decode_dev(dev));
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001453 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001454 }
1455 tomoyo_read_unlock(idx);
1456 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1457 error = 0;
1458 return error;
1459}
1460
1461/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001462 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001463 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001464 * @operation: Type of operation.
1465 * @path1: Pointer to "struct path".
1466 * @path2: Pointer to "struct path".
1467 *
1468 * Returns 0 on success, negative value otherwise.
1469 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001470int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001471 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001472{
1473 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001474 const char *msg;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001475 struct tomoyo_path_info buf1;
1476 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001477 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001478 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001479
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001480 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1481 !path1->mnt || !path2->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001482 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001483 buf1.name = NULL;
1484 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001485 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001486 if (!tomoyo_get_realpath(&buf1, path1) ||
1487 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001488 goto out;
1489 {
1490 struct dentry *dentry = path1->dentry;
1491 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001492 tomoyo_add_slash(&buf1);
1493 tomoyo_add_slash(&buf2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001494 }
1495 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001496 do {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001497 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001498 if (!error)
1499 break;
1500 msg = tomoyo_path22keyword(operation);
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001501 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001502 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001503 tomoyo_file_pattern(&buf1),
1504 tomoyo_file_pattern(&buf2));
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001505 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001506 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001507 kfree(buf1.name);
1508 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001509 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001510 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001511 error = 0;
1512 return error;
1513}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001514
1515/**
1516 * tomoyo_write_file_policy - Update file related list.
1517 *
1518 * @data: String to parse.
1519 * @domain: Pointer to "struct tomoyo_domain_info".
1520 * @is_delete: True if it is a delete request.
1521 *
1522 * Returns 0 on success, negative value otherwise.
1523 *
1524 * Caller holds tomoyo_read_lock().
1525 */
1526int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1527 const bool is_delete)
1528{
1529 char *w[5];
1530 u8 type;
1531 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1532 return -EINVAL;
1533 if (strncmp(w[0], "allow_", 6)) {
1534 unsigned int perm;
1535 if (sscanf(w[0], "%u", &perm) == 1)
1536 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1537 is_delete);
1538 goto out;
1539 }
1540 w[0] += 6;
1541 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1542 if (strcmp(w[0], tomoyo_path_keyword[type]))
1543 continue;
1544 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1545 }
1546 if (!w[2][0])
1547 goto out;
1548 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1549 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1550 continue;
1551 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1552 is_delete);
1553 }
1554 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1555 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1556 continue;
1557 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1558 is_delete);
1559 }
1560 if (!w[3][0] || !w[4][0])
1561 goto out;
1562 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1563 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1564 continue;
1565 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1566 w[4], domain, is_delete);
1567 }
1568 out:
1569 return -EINVAL;
1570}