blob: c13806937dc66978fd8af5cc1275f18d7e9283b4 [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
151/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900152 * tomoyo_strendswith - Check whether the token ends with the given token.
153 *
154 * @name: The token to check.
155 * @tail: The token to find.
156 *
157 * Returns true if @name ends with @tail, false otherwise.
158 */
159static bool tomoyo_strendswith(const char *name, const char *tail)
160{
161 int len;
162
163 if (!name || !tail)
164 return false;
165 len = strlen(name) - strlen(tail);
166 return len >= 0 && !strcmp(name + len, tail);
167}
168
169/**
170 * tomoyo_get_path - Get realpath.
171 *
172 * @path: Pointer to "struct path".
173 *
174 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
175 */
176static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
177{
178 int error;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900179 struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +0900180 GFP_NOFS);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900181
182 if (!buf)
183 return NULL;
184 /* Reserve one byte for appending "/". */
185 error = tomoyo_realpath_from_path2(path, buf->body,
186 sizeof(buf->body) - 2);
187 if (!error) {
188 buf->head.name = buf->body;
189 tomoyo_fill_path_info(&buf->head);
190 return &buf->head;
191 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900192 kfree(buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900193 return NULL;
194}
195
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900196static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
197 const char *filename2,
198 struct tomoyo_domain_info *const domain,
199 const bool is_delete);
200static int tomoyo_update_path_acl(const u8 type, const char *filename,
201 struct tomoyo_domain_info *const domain,
202 const bool is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900203
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900204/*
205 * tomoyo_globally_readable_list is used for holding list of pathnames which
206 * are by default allowed to be open()ed for reading by any process.
207 *
208 * An entry is added by
209 *
210 * # echo 'allow_read /lib/libc-2.5.so' > \
211 * /sys/kernel/security/tomoyo/exception_policy
212 *
213 * and is deleted by
214 *
215 * # echo 'delete allow_read /lib/libc-2.5.so' > \
216 * /sys/kernel/security/tomoyo/exception_policy
217 *
218 * and all entries are retrieved by
219 *
220 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
221 *
222 * In the example above, any process is allowed to
223 * open("/lib/libc-2.5.so", O_RDONLY).
224 * One exception is, if the domain which current process belongs to is marked
225 * as "ignore_global_allow_read", current process can't do so unless explicitly
226 * given "allow_read /lib/libc-2.5.so" to the domain which current process
227 * belongs to.
228 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900229LIST_HEAD(tomoyo_globally_readable_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900230
231/**
232 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
233 *
234 * @filename: Filename unconditionally permitted to open() for reading.
235 * @is_delete: True if it is a delete request.
236 *
237 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900238 *
239 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900240 */
241static int tomoyo_update_globally_readable_entry(const char *filename,
242 const bool is_delete)
243{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900244 struct tomoyo_globally_readable_file_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900245 struct tomoyo_globally_readable_file_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900246 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900247
Tetsuo Handa17080002010-02-16 21:14:48 +0900248 if (!tomoyo_is_correct_path(filename, 1, 0, -1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900249 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900250 e.filename = tomoyo_get_name(filename);
251 if (!e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900252 return -ENOMEM;
Tetsuo Handa29282382010-05-06 00:18:15 +0900253 if (mutex_lock_interruptible(&tomoyo_policy_lock))
254 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900255 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900256 if (ptr->filename != e.filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900257 continue;
258 ptr->is_deleted = is_delete;
259 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900260 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900261 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900262 if (!is_delete && error) {
263 struct tomoyo_globally_readable_file_entry *entry =
264 tomoyo_commit_ok(&e, sizeof(e));
265 if (entry) {
266 list_add_tail_rcu(&entry->list,
267 &tomoyo_globally_readable_list);
268 error = 0;
269 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900270 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900271 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900272 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900273 tomoyo_put_name(e.filename);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900274 return error;
275}
276
277/**
278 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
279 *
280 * @filename: The filename to check.
281 *
282 * Returns true if any domain can open @filename for reading, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900283 *
284 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900285 */
286static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
287 filename)
288{
289 struct tomoyo_globally_readable_file_entry *ptr;
290 bool found = false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900291
292 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900293 if (!ptr->is_deleted &&
294 tomoyo_path_matches_pattern(filename, ptr->filename)) {
295 found = true;
296 break;
297 }
298 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900299 return found;
300}
301
302/**
303 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
304 *
305 * @data: String to parse.
306 * @is_delete: True if it is a delete request.
307 *
308 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900309 *
310 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900311 */
312int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
313{
314 return tomoyo_update_globally_readable_entry(data, is_delete);
315}
316
317/**
318 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
319 *
320 * @head: Pointer to "struct tomoyo_io_buffer".
321 *
322 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900323 *
324 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900325 */
326bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
327{
328 struct list_head *pos;
329 bool done = true;
330
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900331 list_for_each_cookie(pos, head->read_var2,
332 &tomoyo_globally_readable_list) {
333 struct tomoyo_globally_readable_file_entry *ptr;
334 ptr = list_entry(pos,
335 struct tomoyo_globally_readable_file_entry,
336 list);
337 if (ptr->is_deleted)
338 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900339 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
340 ptr->filename->name);
341 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900342 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900343 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900344 return done;
345}
346
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900347/* tomoyo_pattern_list is used for holding list of pathnames which are used for
348 * converting pathnames to pathname patterns during learning mode.
349 *
350 * An entry is added by
351 *
352 * # echo 'file_pattern /proc/\$/mounts' > \
353 * /sys/kernel/security/tomoyo/exception_policy
354 *
355 * and is deleted by
356 *
357 * # echo 'delete file_pattern /proc/\$/mounts' > \
358 * /sys/kernel/security/tomoyo/exception_policy
359 *
360 * and all entries are retrieved by
361 *
362 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
363 *
364 * In the example above, if a process which belongs to a domain which is in
365 * learning mode requested open("/proc/1/mounts", O_RDONLY),
366 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
367 * process belongs to.
368 *
369 * It is not a desirable behavior that we have to use /proc/\$/ instead of
370 * /proc/self/ when current process needs to access only current process's
371 * information. As of now, LSM version of TOMOYO is using __d_path() for
372 * calculating pathname. Non LSM version of TOMOYO is using its own function
373 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
374 * current process from accessing other process's information.
375 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900376LIST_HEAD(tomoyo_pattern_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900377
378/**
379 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
380 *
381 * @pattern: Pathname pattern.
382 * @is_delete: True if it is a delete request.
383 *
384 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900385 *
386 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900387 */
388static int tomoyo_update_file_pattern_entry(const char *pattern,
389 const bool is_delete)
390{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900391 struct tomoyo_pattern_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900392 struct tomoyo_pattern_entry e = { .pattern = tomoyo_get_name(pattern) };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900393 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900394
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900395 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900396 return error;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900397 if (!e.pattern->is_patterned)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900398 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900399 if (mutex_lock_interruptible(&tomoyo_policy_lock))
400 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900401 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900402 if (e.pattern != ptr->pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900403 continue;
404 ptr->is_deleted = is_delete;
405 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900406 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900407 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900408 if (!is_delete && error) {
409 struct tomoyo_pattern_entry *entry =
410 tomoyo_commit_ok(&e, sizeof(e));
411 if (entry) {
412 list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
413 error = 0;
414 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900415 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900416 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900417 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900418 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900419 return error;
420}
421
422/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900423 * tomoyo_file_pattern - Get patterned pathname.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900424 *
425 * @filename: The filename to find patterned pathname.
426 *
427 * Returns pointer to pathname pattern if matched, @filename otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900428 *
429 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900430 */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900431const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900432{
433 struct tomoyo_pattern_entry *ptr;
434 const struct tomoyo_path_info *pattern = NULL;
435
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900436 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900437 if (ptr->is_deleted)
438 continue;
439 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
440 continue;
441 pattern = ptr->pattern;
442 if (tomoyo_strendswith(pattern->name, "/\\*")) {
443 /* Do nothing. Try to find the better match. */
444 } else {
445 /* This would be the better match. Use this. */
446 break;
447 }
448 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900449 if (pattern)
450 filename = pattern;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900451 return filename->name;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900452}
453
454/**
455 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
456 *
457 * @data: String to parse.
458 * @is_delete: True if it is a delete request.
459 *
460 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900461 *
462 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900463 */
464int tomoyo_write_pattern_policy(char *data, const bool is_delete)
465{
466 return tomoyo_update_file_pattern_entry(data, is_delete);
467}
468
469/**
470 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
471 *
472 * @head: Pointer to "struct tomoyo_io_buffer".
473 *
474 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900475 *
476 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900477 */
478bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
479{
480 struct list_head *pos;
481 bool done = true;
482
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900483 list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
484 struct tomoyo_pattern_entry *ptr;
485 ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
486 if (ptr->is_deleted)
487 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900488 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
489 "%s\n", ptr->pattern->name);
490 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900491 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900492 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900493 return done;
494}
495
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900496/*
497 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
498 * default forbidden to modify already written content of a file.
499 *
500 * An entry is added by
501 *
502 * # echo 'deny_rewrite /var/log/messages' > \
503 * /sys/kernel/security/tomoyo/exception_policy
504 *
505 * and is deleted by
506 *
507 * # echo 'delete deny_rewrite /var/log/messages' > \
508 * /sys/kernel/security/tomoyo/exception_policy
509 *
510 * and all entries are retrieved by
511 *
512 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
513 *
514 * In the example above, if a process requested to rewrite /var/log/messages ,
515 * the process can't rewrite unless the domain which that process belongs to
516 * has "allow_rewrite /var/log/messages" entry.
517 *
518 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
519 * when we want to allow rewriting already unlink()ed file. As of now,
520 * LSM version of TOMOYO is using __d_path() for calculating pathname.
521 * Non LSM version of TOMOYO is using its own function which doesn't append
522 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
523 * need to worry whether the file is already unlink()ed or not.
524 */
Tetsuo Handa847b1732010-02-11 09:43:54 +0900525LIST_HEAD(tomoyo_no_rewrite_list);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900526
527/**
528 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
529 *
530 * @pattern: Pathname pattern that are not rewritable by default.
531 * @is_delete: True if it is a delete request.
532 *
533 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900534 *
535 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900536 */
537static int tomoyo_update_no_rewrite_entry(const char *pattern,
538 const bool is_delete)
539{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900540 struct tomoyo_no_rewrite_entry *ptr;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900541 struct tomoyo_no_rewrite_entry e = { };
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900542 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900543
Tetsuo Handa17080002010-02-16 21:14:48 +0900544 if (!tomoyo_is_correct_path(pattern, 0, 0, 0))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900545 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900546 e.pattern = tomoyo_get_name(pattern);
547 if (!e.pattern)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900548 return error;
Tetsuo Handa29282382010-05-06 00:18:15 +0900549 if (mutex_lock_interruptible(&tomoyo_policy_lock))
550 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900551 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900552 if (ptr->pattern != e.pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900553 continue;
554 ptr->is_deleted = is_delete;
555 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900556 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900557 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900558 if (!is_delete && error) {
559 struct tomoyo_no_rewrite_entry *entry =
560 tomoyo_commit_ok(&e, sizeof(e));
561 if (entry) {
562 list_add_tail_rcu(&entry->list,
563 &tomoyo_no_rewrite_list);
564 error = 0;
565 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900566 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900567 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900568 out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900569 tomoyo_put_name(e.pattern);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900570 return error;
571}
572
573/**
574 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
575 *
576 * @filename: Filename to check.
577 *
578 * Returns true if @filename is specified by "deny_rewrite" directive,
579 * false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900580 *
581 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900582 */
583static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
584{
585 struct tomoyo_no_rewrite_entry *ptr;
586 bool found = false;
587
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900588 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900589 if (ptr->is_deleted)
590 continue;
591 if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
592 continue;
593 found = true;
594 break;
595 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900596 return found;
597}
598
599/**
600 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
601 *
602 * @data: String to parse.
603 * @is_delete: True if it is a delete request.
604 *
605 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900606 *
607 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900608 */
609int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
610{
611 return tomoyo_update_no_rewrite_entry(data, is_delete);
612}
613
614/**
615 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
616 *
617 * @head: Pointer to "struct tomoyo_io_buffer".
618 *
619 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900620 *
621 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900622 */
623bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
624{
625 struct list_head *pos;
626 bool done = true;
627
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900628 list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
629 struct tomoyo_no_rewrite_entry *ptr;
630 ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
631 if (ptr->is_deleted)
632 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +0900633 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
634 "%s\n", ptr->pattern->name);
635 if (!done)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900636 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900637 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900638 return done;
639}
640
641/**
642 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
643 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900644 * @perm: Permission (between 1 to 7).
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900645 * @filename: Filename.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900646 * @domain: Pointer to "struct tomoyo_domain_info".
647 * @is_delete: True if it is a delete request.
648 *
649 * Returns 0 on success, negative value otherwise.
650 *
651 * This is legacy support interface for older policy syntax.
652 * Current policy syntax uses "allow_read/write" instead of "6",
653 * "allow_read" instead of "4", "allow_write" instead of "2",
654 * "allow_execute" instead of "1".
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900655 *
656 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900657 */
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900658static int tomoyo_update_file_acl(u8 perm, const char *filename,
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900659 struct tomoyo_domain_info * const domain,
660 const bool is_delete)
661{
662 if (perm > 7 || !perm) {
663 printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
664 __func__, perm, filename);
665 return -EINVAL;
666 }
667 if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
668 /*
669 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
670 * directory permissions.
671 */
672 return 0;
673 if (perm & 4)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900674 tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
675 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900676 if (perm & 2)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900677 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
678 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900679 if (perm & 1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900680 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
681 is_delete);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900682 return 0;
683}
684
685/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900686 * tomoyo_path_acl - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900687 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900688 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900689 * @filename: Filename to check.
690 * @perm: Permission.
691 * @may_use_pattern: True if patterned ACL is permitted.
692 *
693 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900694 *
695 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900696 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900697static int tomoyo_path_acl(const struct tomoyo_request_info *r,
698 const struct tomoyo_path_info *filename,
699 const u32 perm, const bool may_use_pattern)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900700{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900701 struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900702 struct tomoyo_acl_info *ptr;
703 int error = -EPERM;
704
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900705 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900706 struct tomoyo_path_acl *acl;
707 if (ptr->type != TOMOYO_TYPE_PATH_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900708 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900709 acl = container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900710 if (!(acl->perm & perm) ||
711 !tomoyo_compare_name_union_pattern(filename, &acl->name,
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900712 may_use_pattern))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900713 continue;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900714 error = 0;
715 break;
716 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900717 return error;
718}
719
720/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900721 * tomoyo_file_perm - Check permission for opening files.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900722 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900723 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900724 * @filename: Filename to check.
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900725 * @mode: Mode ("read" or "write" or "read/write" or "execute").
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900726 *
727 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900728 *
729 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900730 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900731static int tomoyo_file_perm(struct tomoyo_request_info *r,
732 const struct tomoyo_path_info *filename,
733 const u8 mode)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900734{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900735 const char *msg = "<unknown>";
736 int error = 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900737 u32 perm = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900738
739 if (!filename)
740 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900741
742 if (mode == 6) {
743 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
744 perm = 1 << TOMOYO_TYPE_READ_WRITE;
745 } else if (mode == 4) {
746 msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
747 perm = 1 << TOMOYO_TYPE_READ;
748 } else if (mode == 2) {
749 msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
750 perm = 1 << TOMOYO_TYPE_WRITE;
751 } else if (mode == 1) {
752 msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
753 perm = 1 << TOMOYO_TYPE_EXECUTE;
754 } else
755 BUG();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900756 do {
757 error = tomoyo_path_acl(r, filename, perm, mode != 1);
758 if (error && mode == 4 && !r->domain->ignore_global_allow_read
759 && tomoyo_is_globally_readable_file(filename))
760 error = 0;
761 if (!error)
762 break;
763 tomoyo_warn_log(r, "%s %s", msg, filename->name);
764 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
765 mode == 1 ? filename->name :
766 tomoyo_file_pattern(filename));
767 /*
768 * Do not retry for execute request, for alias may have
769 * changed.
770 */
771 } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
772 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900773 error = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900774 return error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900775}
776
777/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900778 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900779 *
780 * @type: Type of operation.
781 * @filename: Filename.
782 * @domain: Pointer to "struct tomoyo_domain_info".
783 * @is_delete: True if it is a delete request.
784 *
785 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900786 *
787 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900788 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900789static int tomoyo_update_path_acl(const u8 type, const char *filename,
790 struct tomoyo_domain_info *const domain,
791 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900792{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900793 static const u16 tomoyo_rw_mask =
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900794 (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900795 const u16 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900796 struct tomoyo_acl_info *ptr;
797 struct tomoyo_path_acl e = {
798 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900799 .perm = perm
800 };
801 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900802
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900803 if (type == TOMOYO_TYPE_READ_WRITE)
804 e.perm |= tomoyo_rw_mask;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900805 if (!domain)
806 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900807 if (!tomoyo_parse_name_union(filename, &e.name))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900808 return -EINVAL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900809 if (mutex_lock_interruptible(&tomoyo_policy_lock))
810 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900811 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900812 struct tomoyo_path_acl *acl =
813 container_of(ptr, struct tomoyo_path_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900814 if (!tomoyo_is_same_path_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900815 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900816 if (is_delete) {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900817 acl->perm &= ~perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900818 if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900819 acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
820 else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900821 acl->perm &= ~tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900822 } else {
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900823 acl->perm |= perm;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900824 if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900825 acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
826 else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900827 acl->perm |= tomoyo_rw_mask;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900828 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900829 error = 0;
830 break;
831 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900832 if (!is_delete && error) {
833 struct tomoyo_path_acl *entry =
834 tomoyo_commit_ok(&e, sizeof(e));
835 if (entry) {
836 list_add_tail_rcu(&entry->head.list,
837 &domain->acl_info_list);
838 error = 0;
839 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900840 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900841 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa29282382010-05-06 00:18:15 +0900842 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900843 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900844 return error;
845}
846
847/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900848 * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
849 *
850 * @type: Type of operation.
851 * @filename: Filename.
852 * @mode: Create mode.
853 * @major: Device major number.
854 * @minor: Device minor number.
855 * @domain: Pointer to "struct tomoyo_domain_info".
856 * @is_delete: True if it is a delete request.
857 *
858 * Returns 0 on success, negative value otherwise.
859 */
860static inline int tomoyo_update_path_number3_acl(const u8 type,
861 const char *filename,
862 char *mode,
863 char *major, char *minor,
864 struct tomoyo_domain_info *
865 const domain,
866 const bool is_delete)
867{
868 const u8 perm = 1 << type;
869 struct tomoyo_acl_info *ptr;
870 struct tomoyo_path_number3_acl e = {
871 .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
872 .perm = perm
873 };
874 int error = is_delete ? -ENOENT : -ENOMEM;
875 if (!tomoyo_parse_name_union(filename, &e.name) ||
876 !tomoyo_parse_number_union(mode, &e.mode) ||
877 !tomoyo_parse_number_union(major, &e.major) ||
878 !tomoyo_parse_number_union(minor, &e.minor))
879 goto out;
880 if (mutex_lock_interruptible(&tomoyo_policy_lock))
881 goto out;
882 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
883 struct tomoyo_path_number3_acl *acl =
884 container_of(ptr, struct tomoyo_path_number3_acl, head);
885 if (!tomoyo_is_same_path_number3_acl(acl, &e))
886 continue;
887 if (is_delete)
888 acl->perm &= ~perm;
889 else
890 acl->perm |= perm;
891 error = 0;
892 break;
893 }
894 if (!is_delete && error) {
895 struct tomoyo_path_number3_acl *entry =
896 tomoyo_commit_ok(&e, sizeof(e));
897 if (entry) {
898 list_add_tail_rcu(&entry->head.list,
899 &domain->acl_info_list);
900 error = 0;
901 }
902 }
903 mutex_unlock(&tomoyo_policy_lock);
904 out:
905 tomoyo_put_name_union(&e.name);
906 tomoyo_put_number_union(&e.mode);
907 tomoyo_put_number_union(&e.major);
908 tomoyo_put_number_union(&e.minor);
909 return error;
910}
911
912/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900913 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900914 *
915 * @type: Type of operation.
916 * @filename1: First filename.
917 * @filename2: Second filename.
918 * @domain: Pointer to "struct tomoyo_domain_info".
919 * @is_delete: True if it is a delete request.
920 *
921 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900922 *
923 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900924 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900925static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
926 const char *filename2,
927 struct tomoyo_domain_info *const domain,
928 const bool is_delete)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900929{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900930 const u8 perm = 1 << type;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900931 struct tomoyo_path2_acl e = {
932 .head.type = TOMOYO_TYPE_PATH2_ACL,
933 .perm = perm
934 };
935 struct tomoyo_acl_info *ptr;
936 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900937
938 if (!domain)
939 return -EINVAL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900940 if (!tomoyo_parse_name_union(filename1, &e.name1) ||
941 !tomoyo_parse_name_union(filename2, &e.name2))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900942 goto out;
Tetsuo Handa29282382010-05-06 00:18:15 +0900943 if (mutex_lock_interruptible(&tomoyo_policy_lock))
944 goto out;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900945 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900946 struct tomoyo_path2_acl *acl =
947 container_of(ptr, struct tomoyo_path2_acl, head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900948 if (!tomoyo_is_same_path2_acl(acl, &e))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900949 continue;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900950 if (is_delete)
951 acl->perm &= ~perm;
952 else
953 acl->perm |= perm;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900954 error = 0;
955 break;
956 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900957 if (!is_delete && error) {
958 struct tomoyo_path2_acl *entry =
959 tomoyo_commit_ok(&e, sizeof(e));
960 if (entry) {
961 list_add_tail_rcu(&entry->head.list,
962 &domain->acl_info_list);
963 error = 0;
964 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900965 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900966 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900967 out:
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900968 tomoyo_put_name_union(&e.name1);
969 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900970 return error;
971}
972
973/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900974 * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
975 *
976 * @r: Pointer to "struct tomoyo_request_info".
977 * @filename: Filename to check.
978 * @perm: Permission.
979 * @mode: Create mode.
980 * @major: Device major number.
981 * @minor: Device minor number.
982 *
983 * Returns 0 on success, -EPERM otherwise.
984 *
985 * Caller holds tomoyo_read_lock().
986 */
987static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
988 const struct tomoyo_path_info *filename,
989 const u16 perm, const unsigned int mode,
990 const unsigned int major,
991 const unsigned int minor)
992{
993 struct tomoyo_domain_info *domain = r->domain;
994 struct tomoyo_acl_info *ptr;
995 int error = -EPERM;
996 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
997 struct tomoyo_path_number3_acl *acl;
998 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
999 continue;
1000 acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
1001 if (!tomoyo_compare_number_union(mode, &acl->mode))
1002 continue;
1003 if (!tomoyo_compare_number_union(major, &acl->major))
1004 continue;
1005 if (!tomoyo_compare_number_union(minor, &acl->minor))
1006 continue;
1007 if (!(acl->perm & perm))
1008 continue;
1009 if (!tomoyo_compare_name_union(filename, &acl->name))
1010 continue;
1011 error = 0;
1012 break;
1013 }
1014 return error;
1015}
1016
1017/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001018 * tomoyo_path2_acl - Check permission for double path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001019 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001020 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001021 * @type: Type of operation.
1022 * @filename1: First filename to check.
1023 * @filename2: Second filename to check.
1024 *
1025 * Returns 0 on success, -EPERM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001026 *
1027 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001028 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001029static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001030 const struct tomoyo_path_info *filename1,
1031 const struct tomoyo_path_info *filename2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001032{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001033 const struct tomoyo_domain_info *domain = r->domain;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001034 struct tomoyo_acl_info *ptr;
1035 const u8 perm = 1 << type;
1036 int error = -EPERM;
1037
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001038 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001039 struct tomoyo_path2_acl *acl;
1040 if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001041 continue;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001042 acl = container_of(ptr, struct tomoyo_path2_acl, head);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001043 if (!(acl->perm & perm))
1044 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001045 if (!tomoyo_compare_name_union(filename1, &acl->name1))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001046 continue;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +09001047 if (!tomoyo_compare_name_union(filename2, &acl->name2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001048 continue;
1049 error = 0;
1050 break;
1051 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001052 return error;
1053}
1054
1055/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001056 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001057 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001058 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001059 * @operation: Type of operation.
1060 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001061 *
1062 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001063 *
1064 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001065 */
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001066static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
1067 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001068{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001069 const char *msg;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001070 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001071
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001072 next:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001073 do {
1074 error = tomoyo_path_acl(r, filename, 1 << operation, 1);
1075 if (!error)
1076 break;
1077 msg = tomoyo_path2keyword(operation);
1078 tomoyo_warn_log(r, "%s %s", msg, filename->name);
1079 error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
1080 tomoyo_file_pattern(filename));
1081 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001082 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001083 error = 0;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001084 /*
1085 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1086 * we need to check "allow_rewrite" permission if the filename is
1087 * specified by "deny_rewrite" keyword.
1088 */
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001089 if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001090 tomoyo_is_no_rewrite_file(filename)) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001091 operation = TOMOYO_TYPE_REWRITE;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001092 goto next;
1093 }
1094 return error;
1095}
1096
1097/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001098 * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
1099 *
1100 * @r: Pointer to "struct tomoyo_request_info".
1101 * @type: Operation.
1102 * @filename: Filename to check.
1103 * @number: Number.
1104 *
1105 * Returns 0 on success, -EPERM otherwise.
1106 *
1107 * Caller holds tomoyo_read_lock().
1108 */
1109static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
1110 const struct tomoyo_path_info *filename,
1111 const unsigned long number)
1112{
1113 struct tomoyo_domain_info *domain = r->domain;
1114 struct tomoyo_acl_info *ptr;
1115 const u8 perm = 1 << type;
1116 int error = -EPERM;
1117 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1118 struct tomoyo_path_number_acl *acl;
1119 if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
1120 continue;
1121 acl = container_of(ptr, struct tomoyo_path_number_acl,
1122 head);
1123 if (!(acl->perm & perm) ||
1124 !tomoyo_compare_number_union(number, &acl->number) ||
1125 !tomoyo_compare_name_union(filename, &acl->name))
1126 continue;
1127 error = 0;
1128 break;
1129 }
1130 return error;
1131}
1132
1133/**
1134 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
1135 *
1136 * @type: Type of operation.
1137 * @filename: Filename.
1138 * @number: Number.
1139 * @domain: Pointer to "struct tomoyo_domain_info".
1140 * @is_delete: True if it is a delete request.
1141 *
1142 * Returns 0 on success, negative value otherwise.
1143 */
1144static inline int tomoyo_update_path_number_acl(const u8 type,
1145 const char *filename,
1146 char *number,
1147 struct tomoyo_domain_info *
1148 const domain,
1149 const bool is_delete)
1150{
1151 const u8 perm = 1 << type;
1152 struct tomoyo_acl_info *ptr;
1153 struct tomoyo_path_number_acl e = {
1154 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
1155 .perm = perm
1156 };
1157 int error = is_delete ? -ENOENT : -ENOMEM;
1158 if (!domain)
1159 return -EINVAL;
1160 if (!tomoyo_parse_name_union(filename, &e.name))
1161 return -EINVAL;
1162 if (!tomoyo_parse_number_union(number, &e.number))
1163 goto out;
1164 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1165 goto out;
1166 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1167 struct tomoyo_path_number_acl *acl =
1168 container_of(ptr, struct tomoyo_path_number_acl, head);
1169 if (!tomoyo_is_same_path_number_acl(acl, &e))
1170 continue;
1171 if (is_delete)
1172 acl->perm &= ~perm;
1173 else
1174 acl->perm |= perm;
1175 error = 0;
1176 break;
1177 }
1178 if (!is_delete && error) {
1179 struct tomoyo_path_number_acl *entry =
1180 tomoyo_commit_ok(&e, sizeof(e));
1181 if (entry) {
1182 list_add_tail_rcu(&entry->head.list,
1183 &domain->acl_info_list);
1184 error = 0;
1185 }
1186 }
1187 mutex_unlock(&tomoyo_policy_lock);
1188 out:
1189 tomoyo_put_name_union(&e.name);
1190 tomoyo_put_number_union(&e.number);
1191 return error;
1192}
1193
1194/**
1195 * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1196 *
1197 * @r: Pointer to "strct tomoyo_request_info".
1198 * @filename: Filename to check.
1199 * @number: Number.
1200 *
1201 * Returns 0 on success, negative value otherwise.
1202 *
1203 * Caller holds tomoyo_read_lock().
1204 */
1205static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
1206 const u8 type,
1207 const struct tomoyo_path_info *filename,
1208 const unsigned long number)
1209{
1210 char buffer[64];
1211 int error;
1212 u8 radix;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001213 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001214
1215 if (!filename)
1216 return 0;
1217 switch (type) {
1218 case TOMOYO_TYPE_CREATE:
1219 case TOMOYO_TYPE_MKDIR:
1220 case TOMOYO_TYPE_MKFIFO:
1221 case TOMOYO_TYPE_MKSOCK:
1222 case TOMOYO_TYPE_CHMOD:
1223 radix = TOMOYO_VALUE_TYPE_OCTAL;
1224 break;
1225 case TOMOYO_TYPE_IOCTL:
1226 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
1227 break;
1228 default:
1229 radix = TOMOYO_VALUE_TYPE_DECIMAL;
1230 break;
1231 }
1232 tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001233 do {
1234 error = tomoyo_path_number_acl(r, type, filename, number);
1235 if (!error)
1236 break;
1237 msg = tomoyo_path_number2keyword(type);
1238 tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
1239 error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
1240 tomoyo_file_pattern(filename),
1241 buffer);
1242 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001243 if (r->mode != TOMOYO_CONFIG_ENFORCING)
1244 error = 0;
1245 return error;
1246}
1247
1248/**
1249 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
1250 *
1251 * @type: Type of operation.
1252 * @path: Pointer to "struct path".
1253 * @number: Number.
1254 *
1255 * Returns 0 on success, negative value otherwise.
1256 */
1257int tomoyo_path_number_perm(const u8 type, struct path *path,
1258 unsigned long number)
1259{
1260 struct tomoyo_request_info r;
1261 int error = -ENOMEM;
1262 struct tomoyo_path_info *buf;
1263 int idx;
1264
1265 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1266 !path->mnt || !path->dentry)
1267 return 0;
1268 idx = tomoyo_read_lock();
1269 buf = tomoyo_get_path(path);
1270 if (!buf)
1271 goto out;
1272 if (type == TOMOYO_TYPE_MKDIR && !buf->is_dir) {
1273 /*
1274 * tomoyo_get_path() reserves space for appending "/."
1275 */
1276 strcat((char *) buf->name, "/");
1277 tomoyo_fill_path_info(buf);
1278 }
1279 error = tomoyo_path_number_perm2(&r, type, buf, number);
1280 out:
1281 kfree(buf);
1282 tomoyo_read_unlock(idx);
1283 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1284 error = 0;
1285 return error;
1286}
1287
1288/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001289 * tomoyo_check_exec_perm - Check permission for "execute".
1290 *
1291 * @domain: Pointer to "struct tomoyo_domain_info".
1292 * @filename: Check permission for "execute".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001293 *
1294 * Returns 0 on success, negativevalue otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001295 *
1296 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001297 */
1298int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
Tetsuo Handabcb86972009-06-04 15:14:34 +09001299 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001300{
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001301 struct tomoyo_request_info r;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001302
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001303 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001304 return 0;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001305 return tomoyo_file_perm(&r, filename, 1);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001306}
1307
1308/**
1309 * tomoyo_check_open_permission - Check permission for "read" and "write".
1310 *
1311 * @domain: Pointer to "struct tomoyo_domain_info".
1312 * @path: Pointer to "struct path".
1313 * @flag: Flags for open().
1314 *
1315 * Returns 0 on success, negative value otherwise.
1316 */
1317int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1318 struct path *path, const int flag)
1319{
1320 const u8 acc_mode = ACC_MODE(flag);
1321 int error = -ENOMEM;
1322 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001323 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001324 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001325
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001326 if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
1327 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001328 return 0;
1329 if (acc_mode == 0)
1330 return 0;
1331 if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1332 /*
1333 * I don't check directories here because mkdir() and rmdir()
1334 * don't call me.
1335 */
1336 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001337 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001338 buf = tomoyo_get_path(path);
1339 if (!buf)
1340 goto out;
1341 error = 0;
1342 /*
1343 * If the filename is specified by "deny_rewrite" keyword,
1344 * we need to check "allow_rewrite" permission when the filename is not
1345 * opened for append mode or the filename is truncated at open time.
1346 */
1347 if ((acc_mode & MAY_WRITE) &&
1348 ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1349 (tomoyo_is_no_rewrite_file(buf))) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001350 error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001351 }
1352 if (!error)
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001353 error = tomoyo_file_perm(&r, buf, acc_mode);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001354 if (!error && (flag & O_TRUNC))
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001355 error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001356 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001357 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001358 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001359 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001360 error = 0;
1361 return error;
1362}
1363
1364/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001365 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001366 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001367 * @operation: Type of operation.
1368 * @path: Pointer to "struct path".
1369 *
1370 * Returns 0 on success, negative value otherwise.
1371 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001372int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001373{
1374 int error = -ENOMEM;
1375 struct tomoyo_path_info *buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001376 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001377 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001378
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001379 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1380 !path->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001381 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001382 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001383 buf = tomoyo_get_path(path);
1384 if (!buf)
1385 goto out;
1386 switch (operation) {
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001387 case TOMOYO_TYPE_REWRITE:
1388 if (!tomoyo_is_no_rewrite_file(buf)) {
1389 error = 0;
1390 goto out;
1391 }
1392 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001393 case TOMOYO_TYPE_RMDIR:
1394 case TOMOYO_TYPE_CHROOT:
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001395 if (!buf->is_dir) {
1396 /*
1397 * tomoyo_get_path() reserves space for appending "/."
1398 */
1399 strcat((char *) buf->name, "/");
1400 tomoyo_fill_path_info(buf);
1401 }
1402 }
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001403 error = tomoyo_path_permission(&r, operation, buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001404 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001405 kfree(buf);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001406 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001407 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001408 error = 0;
1409 return error;
1410}
1411
1412/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001413 * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
1414 *
1415 * @r: Pointer to "struct tomoyo_request_info".
1416 * @operation: Type of operation.
1417 * @filename: Filename to check.
1418 * @mode: Create mode.
1419 * @dev: Device number.
1420 *
1421 * Returns 0 on success, negative value otherwise.
1422 *
1423 * Caller holds tomoyo_read_lock().
1424 */
1425static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
1426 const u8 operation,
1427 const struct tomoyo_path_info *filename,
1428 const unsigned int mode,
1429 const unsigned int dev)
1430{
1431 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001432 const char *msg;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001433 const unsigned int major = MAJOR(dev);
1434 const unsigned int minor = MINOR(dev);
1435
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001436 do {
1437 error = tomoyo_path_number3_acl(r, filename, 1 << operation,
1438 mode, major, minor);
1439 if (!error)
1440 break;
1441 msg = tomoyo_path_number32keyword(operation);
1442 tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
1443 mode, major, minor);
1444 error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
1445 tomoyo_file_pattern(filename), mode,
1446 major, minor);
1447 } while (error == TOMOYO_RETRY_REQUEST);
1448 if (r->mode != TOMOYO_CONFIG_ENFORCING)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001449 error = 0;
1450 return error;
1451}
1452
1453/**
1454 * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
1455 *
1456 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
1457 * @path: Pointer to "struct path".
1458 * @mode: Create mode.
1459 * @dev: Device number.
1460 *
1461 * Returns 0 on success, negative value otherwise.
1462 */
1463int tomoyo_path_number3_perm(const u8 operation, struct path *path,
1464 const unsigned int mode, unsigned int dev)
1465{
1466 struct tomoyo_request_info r;
1467 int error = -ENOMEM;
1468 struct tomoyo_path_info *buf;
1469 int idx;
1470
1471 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1472 !path->mnt)
1473 return 0;
1474 idx = tomoyo_read_lock();
1475 error = -ENOMEM;
1476 buf = tomoyo_get_path(path);
1477 if (buf) {
1478 error = tomoyo_path_number3_perm2(&r, operation, buf, mode,
1479 new_decode_dev(dev));
1480 kfree(buf);
1481 }
1482 tomoyo_read_unlock(idx);
1483 if (r.mode != TOMOYO_CONFIG_ENFORCING)
1484 error = 0;
1485 return error;
1486}
1487
1488/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001489 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001490 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001491 * @operation: Type of operation.
1492 * @path1: Pointer to "struct path".
1493 * @path2: Pointer to "struct path".
1494 *
1495 * Returns 0 on success, negative value otherwise.
1496 */
Tetsuo Handa97d69312010-02-16 09:46:15 +09001497int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +09001498 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001499{
1500 int error = -ENOMEM;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001501 const char *msg;
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001502 struct tomoyo_path_info *buf1;
1503 struct tomoyo_path_info *buf2;
1504 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001505 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001506
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001507 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
1508 !path1->mnt || !path2->mnt)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001509 return 0;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001510 idx = tomoyo_read_lock();
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001511 buf1 = tomoyo_get_path(path1);
1512 buf2 = tomoyo_get_path(path2);
1513 if (!buf1 || !buf2)
1514 goto out;
1515 {
1516 struct dentry *dentry = path1->dentry;
1517 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1518 /*
1519 * tomoyo_get_path() reserves space for appending "/."
1520 */
1521 if (!buf1->is_dir) {
1522 strcat((char *) buf1->name, "/");
1523 tomoyo_fill_path_info(buf1);
1524 }
1525 if (!buf2->is_dir) {
1526 strcat((char *) buf2->name, "/");
1527 tomoyo_fill_path_info(buf2);
1528 }
1529 }
1530 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001531 do {
1532 error = tomoyo_path2_acl(&r, operation, buf1, buf2);
1533 if (!error)
1534 break;
1535 msg = tomoyo_path22keyword(operation);
1536 tomoyo_warn_log(&r, "%s %s %s", msg, buf1->name, buf2->name);
1537 error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
1538 tomoyo_file_pattern(buf1),
1539 tomoyo_file_pattern(buf2));
1540 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001541 out:
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001542 kfree(buf1);
1543 kfree(buf2);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001544 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +09001545 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001546 error = 0;
1547 return error;
1548}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +09001549
1550/**
1551 * tomoyo_write_file_policy - Update file related list.
1552 *
1553 * @data: String to parse.
1554 * @domain: Pointer to "struct tomoyo_domain_info".
1555 * @is_delete: True if it is a delete request.
1556 *
1557 * Returns 0 on success, negative value otherwise.
1558 *
1559 * Caller holds tomoyo_read_lock().
1560 */
1561int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
1562 const bool is_delete)
1563{
1564 char *w[5];
1565 u8 type;
1566 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
1567 return -EINVAL;
1568 if (strncmp(w[0], "allow_", 6)) {
1569 unsigned int perm;
1570 if (sscanf(w[0], "%u", &perm) == 1)
1571 return tomoyo_update_file_acl((u8) perm, w[1], domain,
1572 is_delete);
1573 goto out;
1574 }
1575 w[0] += 6;
1576 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
1577 if (strcmp(w[0], tomoyo_path_keyword[type]))
1578 continue;
1579 return tomoyo_update_path_acl(type, w[1], domain, is_delete);
1580 }
1581 if (!w[2][0])
1582 goto out;
1583 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
1584 if (strcmp(w[0], tomoyo_path2_keyword[type]))
1585 continue;
1586 return tomoyo_update_path2_acl(type, w[1], w[2], domain,
1587 is_delete);
1588 }
1589 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
1590 if (strcmp(w[0], tomoyo_path_number_keyword[type]))
1591 continue;
1592 return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
1593 is_delete);
1594 }
1595 if (!w[3][0] || !w[4][0])
1596 goto out;
1597 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
1598 if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
1599 continue;
1600 return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
1601 w[4], domain, is_delete);
1602 }
1603 out:
1604 return -EINVAL;
1605}