blob: cef685415df1c587f86d19c4f83e9bb1664d8f3e [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)
68 return tomoyo_path_matches_group(name, ptr->group, 1);
69 return tomoyo_path_matches_pattern(name, ptr->filename);
70}
71
72static bool tomoyo_compare_name_union_pattern(const struct tomoyo_path_info
73 *name,
74 const struct tomoyo_name_union
75 *ptr, const bool may_use_pattern)
76{
77 if (ptr->is_group)
78 return tomoyo_path_matches_group(name, ptr->group,
79 may_use_pattern);
80 if (may_use_pattern || !ptr->filename->is_patterned)
81 return tomoyo_path_matches_pattern(name, ptr->filename);
82 return false;
83}
84
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090085void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
86{
87 if (ptr && ptr->is_group)
88 tomoyo_put_number_group(ptr->group);
89}
90
91bool tomoyo_compare_number_union(const unsigned long value,
92 const struct tomoyo_number_union *ptr)
93{
94 if (ptr->is_group)
95 return tomoyo_number_matches_group(value, value, ptr->group);
96 return value >= ptr->values[0] && value <= ptr->values[1];
97}
98
Kentaro Takedab69a54e2009-02-05 17:18:14 +090099/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900100 * tomoyo_path2keyword - Get the name of single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900101 *
102 * @operation: Type of operation.
103 *
104 * Returns the name of single path operation.
105 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900106const char *tomoyo_path2keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900107{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900108 return (operation < TOMOYO_MAX_PATH_OPERATION)
109 ? tomoyo_path_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900110}
111
112/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900113 * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
114 *
115 * @operation: Type of operation.
116 *
117 * Returns the name of path/number/number/number operation.
118 */
119const char *tomoyo_path_number32keyword(const u8 operation)
120{
121 return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
122 ? tomoyo_path_number3_keyword[operation] : NULL;
123}
124
125/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900126 * tomoyo_path22keyword - Get the name of double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900127 *
128 * @operation: Type of operation.
129 *
130 * Returns the name of double path operation.
131 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900132const char *tomoyo_path22keyword(const u8 operation)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900133{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900134 return (operation < TOMOYO_MAX_PATH2_OPERATION)
135 ? tomoyo_path2_keyword[operation] : NULL;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900136}
137
138/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900139 * tomoyo_path_number2keyword - Get the name of path/number operations.
140 *
141 * @operation: Type of operation.
142 *
143 * Returns the name of path/number operation.
144 */
145const char *tomoyo_path_number2keyword(const u8 operation)
146{
147 return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
148 ? tomoyo_path_number_keyword[operation] : NULL;
149}
150
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900151static void tomoyo_add_slash(struct tomoyo_path_info *buf)
152{
153 if (buf->is_dir)
154 return;
155 /*
156 * This is OK because tomoyo_encode() reserves space for appending "/".
157 */
158 strcat((char *) buf->name, "/");
159 tomoyo_fill_path_info(buf);
160}
161
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900162/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900163 * tomoyo_strendswith - Check whether the token ends with the given token.
164 *
165 * @name: The token to check.
166 * @tail: The token to find.
167 *
168 * Returns true if @name ends with @tail, false otherwise.
169 */
170static bool tomoyo_strendswith(const char *name, const char *tail)
171{
172 int len;
173
174 if (!name || !tail)
175 return false;
176 len = strlen(name) - strlen(tail);
177 return len >= 0 && !strcmp(name + len, tail);
178}
179
180/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900181 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900182 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900183 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900184 * @path: Pointer to "struct path".
185 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900186 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900187 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900188static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900189{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900190 buf->name = tomoyo_realpath_from_path(path);
191 if (buf->name) {
192 tomoyo_fill_path_info(buf);
193 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900194 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900195 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900196}
197
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900198static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
199 const char *filename2,
200 struct tomoyo_domain_info *const domain,
201 const bool is_delete);
202static int tomoyo_update_path_acl(const u8 type, const char *filename,
203 struct tomoyo_domain_info *const domain,
204 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900205
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900206/*
207 * tomoyo_globally_readable_list is used for holding list of pathnames which
208 * are by default allowed to be open()ed for reading by any process.
209 *
210 * An entry is added by
211 *
212 * # echo 'allow_read /lib/libc-2.5.so' > \
213 * /sys/kernel/security/tomoyo/exception_policy
214 *
215 * and is deleted by
216 *
217 * # echo 'delete allow_read /lib/libc-2.5.so' > \
218 * /sys/kernel/security/tomoyo/exception_policy
219 *
220 * and all entries are retrieved by
221 *
222 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
223 *
224 * In the example above, any process is allowed to
225 * open("/lib/libc-2.5.so", O_RDONLY).
226 * One exception is, if the domain which current process belongs to is marked
227 * as "ignore_global_allow_read", current process can't do so unless explicitly
228 * given "allow_read /lib/libc-2.5.so" to the domain which current process
229 * belongs to.
230 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900231LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900232
233/**
234 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
235 *
236 * @filename: Filename unconditionally permitted to open() for reading.
237 * @is_delete: True if it is a delete request.
238 *
239 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900240 *
241 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900242 */
243static int tomoyo_update_globally_readable_entry(const char *filename,
244 const bool is_delete)
245{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900246 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900247 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900248 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900249
Tetsuo Handa17080002010-02-16 21:14:48 +0900250 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900251 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900252 e.filename = tomoyo_get_name(filename);
253 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900254 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900255 if (mutex_lock_interruptible(&tomoyo_policy_lock))
256 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900257 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900258 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900259 continue;
260 ptr->is_deleted = is_delete;
261 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900262 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900263 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900264 if (!is_delete && error) {
265 struct tomoyo_globally_readable_file_entry *entry =
266 tomoyo_commit_ok(&e, sizeof(e));
267 if (entry) {
268 list_add_tail_rcu(&entry->list,
269 &tomoyo_globally_readable_list);
270 error = 0;
271 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900272 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900273 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900274 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900275 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900276 return error;
277}
278
279/**
280 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
281 *
282 * @filename: The filename to check.
283 *
284 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900285 *
286 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900287 */
288static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
289 filename)
290{
291 struct tomoyo_globally_readable_file_entry *ptr;
292 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900293
294 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900295 if (!ptr->is_deleted &&
296 tomoyo_path_matches_pattern(filename, ptr->filename)) {
297 found = true;
298 break;
299 }
300 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900301 return found;
302}
303
304/**
305 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
306 *
307 * @data: String to parse.
308 * @is_delete: True if it is a delete request.
309 *
310 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900311 *
312 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900313 */
314int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
315{
316 return tomoyo_update_globally_readable_entry(data, is_delete);
317}
318
319/**
320 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
321 *
322 * @head: Pointer to "struct tomoyo_io_buffer".
323 *
324 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900325 *
326 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900327 */
328bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
329{
330 struct list_head *pos;
331 bool done = true;
332
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900333 list_for_each_cookie(pos, head->read_var2,
334 &tomoyo_globally_readable_list) {
335 struct tomoyo_globally_readable_file_entry *ptr;
336 ptr = list_entry(pos,
337 struct tomoyo_globally_readable_file_entry,
338 list);
339 if (ptr->is_deleted)
340 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900341 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
342 ptr->filename->name);
343 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900344 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900345 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900346 return done;
347}
348
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900349/* tomoyo_pattern_list is used for holding list of pathnames which are used for
350 * converting pathnames to pathname patterns during learning mode.
351 *
352 * An entry is added by
353 *
354 * # echo 'file_pattern /proc/\$/mounts' > \
355 * /sys/kernel/security/tomoyo/exception_policy
356 *
357 * and is deleted by
358 *
359 * # echo 'delete file_pattern /proc/\$/mounts' > \
360 * /sys/kernel/security/tomoyo/exception_policy
361 *
362 * and all entries are retrieved by
363 *
364 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
365 *
366 * In the example above, if a process which belongs to a domain which is in
367 * learning mode requested open("/proc/1/mounts", O_RDONLY),
368 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
369 * process belongs to.
370 *
371 * It is not a desirable behavior that we have to use /proc/\$/ instead of
372 * /proc/self/ when current process needs to access only current process's
373 * information. As of now, LSM version of TOMOYO is using __d_path() for
374 * calculating pathname. Non LSM version of TOMOYO is using its own function
375 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
376 * current process from accessing other process's information.
377 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900378LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900379
380/**
381 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
382 *
383 * @pattern: Pathname pattern.
384 * @is_delete: True if it is a delete request.
385 *
386 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900387 *
388 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900389 */
390static int tomoyo_update_file_pattern_entry(const char *pattern,
391 const bool is_delete)
392{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900393 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900394 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900395 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900396
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900397 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900398 return error;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900399 if (!e.pattern->is_patterned)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900400 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900401 if (mutex_lock_interruptible(&tomoyo_policy_lock))
402 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900403 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900404 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900405 continue;
406 ptr->is_deleted = is_delete;
407 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900408 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900409 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900410 if (!is_delete && error) {
411 struct tomoyo_pattern_entry *entry =
412 tomoyo_commit_ok(&e, sizeof(e));
413 if (entry) {
414 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
415 error = 0;
416 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900417 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900418 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900419 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900420 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900421 return error;
422}
423
424/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900425 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900426 *
427 * @filename: The filename to find patterned pathname.
428 *
429 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900430 *
431 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900432 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900433const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900434{
435 struct tomoyo_pattern_entry *ptr;
436 const struct tomoyo_path_info *pattern = NULL;
437
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900438 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900439 if (ptr->is_deleted)
440 continue;
441 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
442 continue;
443 pattern = ptr->pattern;
444 if (tomoyo_strendswith(pattern->name, "/\\*")) {
445 /* Do nothing. Try to find the better match. */
446 } else {
447 /* This would be the better match. Use this. */
448 break;
449 }
450 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900451 if (pattern)
452 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900453 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900454}
455
456/**
457 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
458 *
459 * @data: String to parse.
460 * @is_delete: True if it is a delete request.
461 *
462 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900463 *
464 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900465 */
466int tomoyo_write_pattern_policy(char *data, const bool is_delete)
467{
468 return tomoyo_update_file_pattern_entry(data, is_delete);
469}
470
471/**
472 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
473 *
474 * @head: Pointer to "struct tomoyo_io_buffer".
475 *
476 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900477 *
478 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900479 */
480bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
481{
482 struct list_head *pos;
483 bool done = true;
484
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900485 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
486 struct tomoyo_pattern_entry *ptr;
487 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
488 if (ptr->is_deleted)
489 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900490 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
491 "%s\n", ptr->pattern->name);
492 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900493 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900494 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900495 return done;
496}
497
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900498/*
499 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
500 * default forbidden to modify already written content of a file.
501 *
502 * An entry is added by
503 *
504 * # echo 'deny_rewrite /var/log/messages' > \
505 * /sys/kernel/security/tomoyo/exception_policy
506 *
507 * and is deleted by
508 *
509 * # echo 'delete deny_rewrite /var/log/messages' > \
510 * /sys/kernel/security/tomoyo/exception_policy
511 *
512 * and all entries are retrieved by
513 *
514 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
515 *
516 * In the example above, if a process requested to rewrite /var/log/messages ,
517 * the process can't rewrite unless the domain which that process belongs to
518 * has "allow_rewrite /var/log/messages" entry.
519 *
520 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
521 * when we want to allow rewriting already unlink()ed file. As of now,
522 * LSM version of TOMOYO is using __d_path() for calculating pathname.
523 * Non LSM version of TOMOYO is using its own function which doesn't append
524 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
525 * need to worry whether the file is already unlink()ed or not.
526 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900527LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900528
529/**
530 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
531 *
532 * @pattern: Pathname pattern that are not rewritable by default.
533 * @is_delete: True if it is a delete request.
534 *
535 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900536 *
537 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900538 */
539static int tomoyo_update_no_rewrite_entry(const char *pattern,
540 const bool is_delete)
541{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900542 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900543 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900544 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900545
Tetsuo Handa17080002010-02-16 21:14:48 +0900546 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900547 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900548 e.pattern = tomoyo_get_name(pattern);
549 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900550 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900551 if (mutex_lock_interruptible(&tomoyo_policy_lock))
552 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900553 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900554 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900555 continue;
556 ptr->is_deleted = is_delete;
557 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900558 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900559 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900560 if (!is_delete && error) {
561 struct tomoyo_no_rewrite_entry *entry =
562 tomoyo_commit_ok(&e, sizeof(e));
563 if (entry) {
564 list_add_tail_rcu(&entry->list,
565 &tomoyo_no_rewrite_list);
566 error = 0;
567 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900568 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900569 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900570 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900571 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900572 return error;
573}
574
575/**
576 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
577 *
578 * @filename: Filename to check.
579 *
580 * Returns true if @filename is specified by "deny_rewrite" directive,
581 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900582 *
583 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900584 */
585static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
586{
587 struct tomoyo_no_rewrite_entry *ptr;
588 bool found = false;
589
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900590 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900591 if (ptr->is_deleted)
592 continue;
593 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
594 continue;
595 found = true;
596 break;
597 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900598 return found;
599}
600
601/**
602 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
603 *
604 * @data: String to parse.
605 * @is_delete: True if it is a delete request.
606 *
607 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900608 *
609 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900610 */
611int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
612{
613 return tomoyo_update_no_rewrite_entry(data, is_delete);
614}
615
616/**
617 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
618 *
619 * @head: Pointer to "struct tomoyo_io_buffer".
620 *
621 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900622 *
623 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900624 */
625bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
626{
627 struct list_head *pos;
628 bool done = true;
629
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900630 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
631 struct tomoyo_no_rewrite_entry *ptr;
632 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
633 if (ptr->is_deleted)
634 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900635 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
636 "%s\n", ptr->pattern->name);
637 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900638 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900639 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900640 return done;
641}
642
643/**
644 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
645 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900646 * @perm: Permission (between 1 to 7).
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900647 * @filename: Filename.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900648 * @domain: Pointer to "struct tomoyo_domain_info".
649 * @is_delete: True if it is a delete request.
650 *
651 * Returns 0 on success, negative value otherwise.
652 *
653 * This is legacy support interface for older policy syntax.
654 * Current policy syntax uses "allow_read/write" instead of "6",
655 * "allow_read" instead of "4", "allow_write" instead of "2",
656 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900657 *
658 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900659 */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900660static int tomoyo_update_file_acl(u8 perm, const char *filename,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900661 struct tomoyo_domain_info * const domain,
662 const bool is_delete)
663{
664 if (perm > 7 || !perm) {
665 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
666 __func__, perm, filename);
667 return -EINVAL;
668 }
669 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
670 /*
671 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
672 * directory permissions.
673 */
674 return 0;
675 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900676 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
677 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900678 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900679 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
680 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900681 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900682 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
683 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900684 return 0;
685}
686
687/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900688 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900689 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900690 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900691 * @filename: Filename to check.
692 * @perm: Permission.
693 * @may_use_pattern: True if patterned ACL is permitted.
694 *
695 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900696 *
697 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900698 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900699static int tomoyo_path_acl(const struct tomoyo_request_info *r,
700 const struct tomoyo_path_info *filename,
701 const u32 perm, const bool may_use_pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900702{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900703 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900704 struct tomoyo_acl_info *ptr;
705 int error = -EPERM;
706
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900707 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900708 struct tomoyo_path_acl *acl;
709 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900710 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900711 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900712 if (!(acl->perm & perm) ||
713 !tomoyo_compare_name_union_pattern(filename, &acl->name,
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900714 may_use_pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900715 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900716 error = 0;
717 break;
718 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900719 return error;
720}
721
722/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900723 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900724 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900725 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900726 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900727 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900728 *
729 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900730 *
731 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900732 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900733static int tomoyo_file_perm(struct tomoyo_request_info *r,
734 const struct tomoyo_path_info *filename,
735 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900736{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900737 const char *msg = "<unknown>";
738 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900739 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900740
741 if (!filename)
742 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900743
744 if (mode == 6) {
745 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
746 perm = 1 << TOMOYO_TYPE_READ_WRITE;
747 } else if (mode == 4) {
748 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
749 perm = 1 << TOMOYO_TYPE_READ;
750 } else if (mode == 2) {
751 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
752 perm = 1 << TOMOYO_TYPE_WRITE;
753 } else if (mode == 1) {
754 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
755 perm = 1 << TOMOYO_TYPE_EXECUTE;
756 } else
757 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900758 do {
759 error = tomoyo_path_acl(r, filename, perm, mode != 1);
760 if (error && mode == 4 && !r->domain->ignore_global_allow_read
761 && tomoyo_is_globally_readable_file(filename))
762 error = 0;
763 if (!error)
764 break;
765 tomoyo_warn_log(r, "%s %s", msg, filename->name);
766 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
767 mode == 1 ? filename->name :
768 tomoyo_file_pattern(filename));
769 /*
770 * Do not retry for execute request, for alias may have
771 * changed.
772 */
773 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
774 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900775 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900776 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900777}
778
779/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900780 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900781 *
782 * @type: Type of operation.
783 * @filename: Filename.
784 * @domain: Pointer to "struct tomoyo_domain_info".
785 * @is_delete: True if it is a delete request.
786 *
787 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900788 *
789 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900790 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900791static int tomoyo_update_path_acl(const u8 type, const char *filename,
792 struct tomoyo_domain_info *const domain,
793 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900794{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900795 static const u16 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900796 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900797 const u16 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900798 struct tomoyo_acl_info *ptr;
799 struct tomoyo_path_acl e = {
800 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900801 .perm = perm
802 };
803 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900804
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900805 if (type == TOMOYO_TYPE_READ_WRITE)
806 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900807 if (!domain)
808 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900809 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900810 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900811 if (mutex_lock_interruptible(&tomoyo_policy_lock))
812 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900813 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900814 struct tomoyo_path_acl *acl =
815 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900816 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900817 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900818 if (is_delete) {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900819 acl->perm &= ~perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900820 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900821 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
822 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900823 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900824 } else {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900825 acl->perm |= perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900826 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900827 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
828 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900829 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900830 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900831 error = 0;
832 break;
833 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900834 if (!is_delete && error) {
835 struct tomoyo_path_acl *entry =
836 tomoyo_commit_ok(&e, sizeof(e));
837 if (entry) {
838 list_add_tail_rcu(&entry->head.list,
839 &domain->acl_info_list);
840 error = 0;
841 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900842 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900843 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900844 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900845 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900846 return error;
847}
848
849/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900850 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
851 *
852 * @type: Type of operation.
853 * @filename: Filename.
854 * @mode: Create mode.
855 * @major: Device major number.
856 * @minor: Device minor number.
857 * @domain: Pointer to "struct tomoyo_domain_info".
858 * @is_delete: True if it is a delete request.
859 *
860 * Returns 0 on success, negative value otherwise.
861 */
862static inline int tomoyo_update_path_number3_acl(const u8 type,
863 const char *filename,
864 char *mode,
865 char *major, char *minor,
866 struct tomoyo_domain_info *
867 const domain,
868 const bool is_delete)
869{
870 const u8 perm = 1 << type;
871 struct tomoyo_acl_info *ptr;
872 struct tomoyo_path_number3_acl e = {
873 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
874 .perm = perm
875 };
876 int error = is_delete ? -ENOENT : -ENOMEM;
877 if (!tomoyo_parse_name_union(filename, &e.name) ||
878 !tomoyo_parse_number_union(mode, &e.mode) ||
879 !tomoyo_parse_number_union(major, &e.major) ||
880 !tomoyo_parse_number_union(minor, &e.minor))
881 goto out;
882 if (mutex_lock_interruptible(&tomoyo_policy_lock))
883 goto out;
884 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
885 struct tomoyo_path_number3_acl *acl =
886 container_of(ptr, struct tomoyo_path_number3_acl, head);
887 if (!tomoyo_is_same_path_number3_acl(acl, &e))
888 continue;
889 if (is_delete)
890 acl->perm &= ~perm;
891 else
892 acl->perm |= perm;
893 error = 0;
894 break;
895 }
896 if (!is_delete && error) {
897 struct tomoyo_path_number3_acl *entry =
898 tomoyo_commit_ok(&e, sizeof(e));
899 if (entry) {
900 list_add_tail_rcu(&entry->head.list,
901 &domain->acl_info_list);
902 error = 0;
903 }
904 }
905 mutex_unlock(&tomoyo_policy_lock);
906 out:
907 tomoyo_put_name_union(&e.name);
908 tomoyo_put_number_union(&e.mode);
909 tomoyo_put_number_union(&e.major);
910 tomoyo_put_number_union(&e.minor);
911 return error;
912}
913
914/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900915 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900916 *
917 * @type: Type of operation.
918 * @filename1: First filename.
919 * @filename2: Second filename.
920 * @domain: Pointer to "struct tomoyo_domain_info".
921 * @is_delete: True if it is a delete request.
922 *
923 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900924 *
925 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900926 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900927static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
928 const char *filename2,
929 struct tomoyo_domain_info *const domain,
930 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900931{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900932 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900933 struct tomoyo_path2_acl e = {
934 .head.type = TOMOYO_TYPE_PATH2_ACL,
935 .perm = perm
936 };
937 struct tomoyo_acl_info *ptr;
938 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900939
940 if (!domain)
941 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900942 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
943 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900944 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900945 if (mutex_lock_interruptible(&tomoyo_policy_lock))
946 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900947 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900948 struct tomoyo_path2_acl *acl =
949 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900950 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900951 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900952 if (is_delete)
953 acl->perm &= ~perm;
954 else
955 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900956 error = 0;
957 break;
958 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900959 if (!is_delete && error) {
960 struct tomoyo_path2_acl *entry =
961 tomoyo_commit_ok(&e, sizeof(e));
962 if (entry) {
963 list_add_tail_rcu(&entry->head.list,
964 &domain->acl_info_list);
965 error = 0;
966 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900967 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900968 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900969 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900970 tomoyo_put_name_union(&e.name1);
971 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900972 return error;
973}
974
975/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900976 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
977 *
978 * @r: Pointer to "struct tomoyo_request_info".
979 * @filename: Filename to check.
980 * @perm: Permission.
981 * @mode: Create mode.
982 * @major: Device major number.
983 * @minor: Device minor number.
984 *
985 * Returns 0 on success, -EPERM otherwise.
986 *
987 * Caller holds tomoyo_read_lock().
988 */
989static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
990 const struct tomoyo_path_info *filename,
991 const u16 perm, const unsigned int mode,
992 const unsigned int major,
993 const unsigned int minor)
994{
995 struct tomoyo_domain_info *domain = r->domain;
996 struct tomoyo_acl_info *ptr;
997 int error = -EPERM;
998 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
999 struct tomoyo_path_number3_acl *acl;
1000 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
1001 continue;
1002 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
1003 if (!tomoyo_compare_number_union(mode, &acl->mode))
1004 continue;
1005 if (!tomoyo_compare_number_union(major, &acl->major))
1006 continue;
1007 if (!tomoyo_compare_number_union(minor, &acl->minor))
1008 continue;
1009 if (!(acl->perm & perm))
1010 continue;
1011 if (!tomoyo_compare_name_union(filename, &acl->name))
1012 continue;
1013 error = 0;
1014 break;
1015 }
1016 return error;
1017}
1018
1019/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001020 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001021 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001022 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001023 * @type: Type of operation.
1024 * @filename1: First filename to check.
1025 * @filename2: Second filename to check.
1026 *
1027 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001028 *
1029 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001030 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001031static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001032 const struct tomoyo_path_info *filename1,
1033 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001034{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001035 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001036 struct tomoyo_acl_info *ptr;
1037 const u8 perm = 1 << type;
1038 int error = -EPERM;
1039
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001040 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001041 struct tomoyo_path2_acl *acl;
1042 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001044 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001045 if (!(acl->perm & perm))
1046 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001047 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001048 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001049 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001050 continue;
1051 error = 0;
1052 break;
1053 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001054 return error;
1055}
1056
1057/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001058 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001059 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001060 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001061 * @operation: Type of operation.
1062 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001063 *
1064 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001065 *
1066 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001067 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001068static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1069 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001070{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001071 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001072 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001073
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001074 next:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001075 do {
1076 error = tomoyo_path_acl(r, filename, 1 << operation, 1);
1077 if (!error)
1078 break;
1079 msg = tomoyo_path2keyword(operation);
1080 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1081 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1082 tomoyo_file_pattern(filename));
1083 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001084 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001085 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001086 /*
1087 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1088 * we need to check "allow_rewrite" permission if the filename is
1089 * specified by "deny_rewrite" keyword.
1090 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001091 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001092 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001093 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001094 goto next;
1095 }
1096 return error;
1097}
1098
1099/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001100 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1101 *
1102 * @r: Pointer to "struct tomoyo_request_info".
1103 * @type: Operation.
1104 * @filename: Filename to check.
1105 * @number: Number.
1106 *
1107 * Returns 0 on success, -EPERM otherwise.
1108 *
1109 * Caller holds tomoyo_read_lock().
1110 */
1111static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1112 const struct tomoyo_path_info *filename,
1113 const unsigned long number)
1114{
1115 struct tomoyo_domain_info *domain = r->domain;
1116 struct tomoyo_acl_info *ptr;
1117 const u8 perm = 1 << type;
1118 int error = -EPERM;
1119 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1120 struct tomoyo_path_number_acl *acl;
1121 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1122 continue;
1123 acl = container_of(ptr, struct tomoyo_path_number_acl,
1124 head);
1125 if (!(acl->perm & perm) ||
1126 !tomoyo_compare_number_union(number, &acl->number) ||
1127 !tomoyo_compare_name_union(filename, &acl->name))
1128 continue;
1129 error = 0;
1130 break;
1131 }
1132 return error;
1133}
1134
1135/**
1136 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1137 *
1138 * @type: Type of operation.
1139 * @filename: Filename.
1140 * @number: Number.
1141 * @domain: Pointer to "struct tomoyo_domain_info".
1142 * @is_delete: True if it is a delete request.
1143 *
1144 * Returns 0 on success, negative value otherwise.
1145 */
1146static inline int tomoyo_update_path_number_acl(const u8 type,
1147 const char *filename,
1148 char *number,
1149 struct tomoyo_domain_info *
1150 const domain,
1151 const bool is_delete)
1152{
1153 const u8 perm = 1 << type;
1154 struct tomoyo_acl_info *ptr;
1155 struct tomoyo_path_number_acl e = {
1156 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1157 .perm = perm
1158 };
1159 int error = is_delete ? -ENOENT : -ENOMEM;
1160 if (!domain)
1161 return -EINVAL;
1162 if (!tomoyo_parse_name_union(filename, &e.name))
1163 return -EINVAL;
1164 if (!tomoyo_parse_number_union(number, &e.number))
1165 goto out;
1166 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1167 goto out;
1168 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1169 struct tomoyo_path_number_acl *acl =
1170 container_of(ptr, struct tomoyo_path_number_acl, head);
1171 if (!tomoyo_is_same_path_number_acl(acl, &e))
1172 continue;
1173 if (is_delete)
1174 acl->perm &= ~perm;
1175 else
1176 acl->perm |= perm;
1177 error = 0;
1178 break;
1179 }
1180 if (!is_delete && error) {
1181 struct tomoyo_path_number_acl *entry =
1182 tomoyo_commit_ok(&e, sizeof(e));
1183 if (entry) {
1184 list_add_tail_rcu(&entry->head.list,
1185 &domain->acl_info_list);
1186 error = 0;
1187 }
1188 }
1189 mutex_unlock(&tomoyo_policy_lock);
1190 out:
1191 tomoyo_put_name_union(&e.name);
1192 tomoyo_put_number_union(&e.number);
1193 return error;
1194}
1195
1196/**
1197 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1198 *
1199 * @r: Pointer to "strct tomoyo_request_info".
1200 * @filename: Filename to check.
1201 * @number: Number.
1202 *
1203 * Returns 0 on success, negative value otherwise.
1204 *
1205 * Caller holds tomoyo_read_lock().
1206 */
1207static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1208 const u8 type,
1209 const struct tomoyo_path_info *filename,
1210 const unsigned long number)
1211{
1212 char buffer[64];
1213 int error;
1214 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001215 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001216
1217 if (!filename)
1218 return 0;
1219 switch (type) {
1220 case TOMOYO_TYPE_CREATE:
1221 case TOMOYO_TYPE_MKDIR:
1222 case TOMOYO_TYPE_MKFIFO:
1223 case TOMOYO_TYPE_MKSOCK:
1224 case TOMOYO_TYPE_CHMOD:
1225 radix = TOMOYO_VALUE_TYPE_OCTAL;
1226 break;
1227 case TOMOYO_TYPE_IOCTL:
1228 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1229 break;
1230 default:
1231 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1232 break;
1233 }
1234 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001235 do {
1236 error = tomoyo_path_number_acl(r, type, filename, number);
1237 if (!error)
1238 break;
1239 msg = tomoyo_path_number2keyword(type);
1240 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1241 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1242 tomoyo_file_pattern(filename),
1243 buffer);
1244 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001245 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1246 error = 0;
1247 return error;
1248}
1249
1250/**
1251 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1252 *
1253 * @type: Type of operation.
1254 * @path: Pointer to "struct path".
1255 * @number: Number.
1256 *
1257 * Returns 0 on success, negative value otherwise.
1258 */
1259int tomoyo_path_number_perm(const u8 type, struct path *path,
1260 unsigned long number)
1261{
1262 struct tomoyo_request_info r;
1263 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001264 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001265 int idx;
1266
1267 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1268 !path->mnt || !path->dentry)
1269 return 0;
1270 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001271 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001272 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001273 if (type == TOMOYO_TYPE_MKDIR)
1274 tomoyo_add_slash(&buf);
1275 error = tomoyo_path_number_perm2(&r, type, &buf, number);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001276 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001277 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001278 tomoyo_read_unlock(idx);
1279 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1280 error = 0;
1281 return error;
1282}
1283
1284/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001285 * tomoyo_check_exec_perm - Check permission for "execute".
1286 *
1287 * @domain: Pointer to "struct tomoyo_domain_info".
1288 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001289 *
1290 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001291 *
1292 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001293 */
1294int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001295 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001296{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001297 struct tomoyo_request_info r;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001298
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001299 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001300 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001301 return tomoyo_file_perm(&r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001302}
1303
1304/**
1305 * tomoyo_check_open_permission - Check permission for "read" and "write".
1306 *
1307 * @domain: Pointer to "struct tomoyo_domain_info".
1308 * @path: Pointer to "struct path".
1309 * @flag: Flags for open().
1310 *
1311 * Returns 0 on success, negative value otherwise.
1312 */
1313int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1314 struct path *path, const int flag)
1315{
1316 const u8 acc_mode = ACC_MODE(flag);
1317 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001318 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001319 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001320 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001321
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001322 if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
1323 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001324 return 0;
1325 if (acc_mode == 0)
1326 return 0;
1327 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1328 /*
1329 * I don't check directories here because mkdir() and rmdir()
1330 * don't call me.
1331 */
1332 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001333 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001334 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001335 goto out;
1336 error = 0;
1337 /*
1338 * If the filename is specified by "deny_rewrite" keyword,
1339 * we need to check "allow_rewrite" permission when the filename is not
1340 * opened for append mode or the filename is truncated at open time.
1341 */
1342 if ((acc_mode & MAY_WRITE) &&
1343 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001344 (tomoyo_is_no_rewrite_file(&buf))) {
1345 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001346 }
1347 if (!error)
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001348 error = tomoyo_file_perm(&r, &buf, acc_mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001349 if (!error && (flag & O_TRUNC))
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001350 error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001351 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001352 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001353 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001354 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001355 error = 0;
1356 return error;
1357}
1358
1359/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001360 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001361 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001362 * @operation: Type of operation.
1363 * @path: Pointer to "struct path".
1364 *
1365 * Returns 0 on success, negative value otherwise.
1366 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001367int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001368{
1369 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001370 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001371 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001372 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001373
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001374 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1375 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001376 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001377 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001378 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001379 goto out;
1380 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001381 case TOMOYO_TYPE_REWRITE:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001382 if (!tomoyo_is_no_rewrite_file(&buf)) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001383 error = 0;
1384 goto out;
1385 }
1386 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001387 case TOMOYO_TYPE_RMDIR:
1388 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001389 tomoyo_add_slash(&buf);
1390 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001391 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001392 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001393 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001394 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001395 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001396 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001397 error = 0;
1398 return error;
1399}
1400
1401/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001402 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1403 *
1404 * @r: Pointer to "struct tomoyo_request_info".
1405 * @operation: Type of operation.
1406 * @filename: Filename to check.
1407 * @mode: Create mode.
1408 * @dev: Device number.
1409 *
1410 * Returns 0 on success, negative value otherwise.
1411 *
1412 * Caller holds tomoyo_read_lock().
1413 */
1414static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1415 const u8 operation,
1416 const struct tomoyo_path_info *filename,
1417 const unsigned int mode,
1418 const unsigned int dev)
1419{
1420 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001421 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001422 const unsigned int major = MAJOR(dev);
1423 const unsigned int minor = MINOR(dev);
1424
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001425 do {
1426 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1427 mode, major, minor);
1428 if (!error)
1429 break;
1430 msg = tomoyo_path_number32keyword(operation);
1431 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1432 mode, major, minor);
1433 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1434 tomoyo_file_pattern(filename), mode,
1435 major, minor);
1436 } while (error == TOMOYO_RETRY_REQUEST);
1437 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001438 error = 0;
1439 return error;
1440}
1441
1442/**
1443 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1444 *
1445 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1446 * @path: Pointer to "struct path".
1447 * @mode: Create mode.
1448 * @dev: Device number.
1449 *
1450 * Returns 0 on success, negative value otherwise.
1451 */
1452int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1453 const unsigned int mode, unsigned int dev)
1454{
1455 struct tomoyo_request_info r;
1456 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001457 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001458 int idx;
1459
1460 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1461 !path->mnt)
1462 return 0;
1463 idx = tomoyo_read_lock();
1464 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001465 if (tomoyo_get_realpath(&buf, path)) {
1466 error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001467 new_decode_dev(dev));
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001468 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001469 }
1470 tomoyo_read_unlock(idx);
1471 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1472 error = 0;
1473 return error;
1474}
1475
1476/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001477 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001478 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001479 * @operation: Type of operation.
1480 * @path1: Pointer to "struct path".
1481 * @path2: Pointer to "struct path".
1482 *
1483 * Returns 0 on success, negative value otherwise.
1484 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001485int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001486 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001487{
1488 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001489 const char *msg;
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001490 struct tomoyo_path_info buf1;
1491 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001492 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001493 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001494
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001495 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1496 !path1->mnt || !path2->mnt)
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;
1504 {
1505 struct dentry *dentry = path1->dentry;
1506 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001507 tomoyo_add_slash(&buf1);
1508 tomoyo_add_slash(&buf2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001509 }
1510 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001511 do {
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001512 error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001513 if (!error)
1514 break;
1515 msg = tomoyo_path22keyword(operation);
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001516 tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001517 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001518 tomoyo_file_pattern(&buf1),
1519 tomoyo_file_pattern(&buf2));
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001520 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001521 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +09001522 kfree(buf1.name);
1523 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001524 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001525 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001526 error = 0;
1527 return error;
1528}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001529
1530/**
1531 * tomoyo_write_file_policy - Update file related list.
1532 *
1533 * @data: String to parse.
1534 * @domain: Pointer to "struct tomoyo_domain_info".
1535 * @is_delete: True if it is a delete request.
1536 *
1537 * Returns 0 on success, negative value otherwise.
1538 *
1539 * Caller holds tomoyo_read_lock().
1540 */
1541int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1542 const bool is_delete)
1543{
1544 char *w[5];
1545 u8 type;
1546 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1547 return -EINVAL;
1548 if (strncmp(w[0], "allow_", 6)) {
1549 unsigned int perm;
1550 if (sscanf(w[0], "%u", &perm) == 1)
1551 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1552 is_delete);
1553 goto out;
1554 }
1555 w[0] += 6;
1556 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1557 if (strcmp(w[0], tomoyo_path_keyword[type]))
1558 continue;
1559 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1560 }
1561 if (!w[2][0])
1562 goto out;
1563 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1564 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1565 continue;
1566 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1567 is_delete);
1568 }
1569 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1570 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1571 continue;
1572 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1573 is_delete);
1574 }
1575 if (!w[3][0] || !w[4][0])
1576 goto out;
1577 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1578 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1579 continue;
1580 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1581 w[4], domain, is_delete);
1582 }
1583 out:
1584 return -EINVAL;
1585}