xref: /openbmc/linux/security/tomoyo/file.c (revision bf24fb01)
1b69a54eeSKentaro Takeda /*
2b69a54eeSKentaro Takeda  * security/tomoyo/file.c
3b69a54eeSKentaro Takeda  *
4b69a54eeSKentaro Takeda  * Implementation of the Domain-Based Mandatory Access Control.
5b69a54eeSKentaro Takeda  *
6b69a54eeSKentaro Takeda  * Copyright (C) 2005-2009  NTT DATA CORPORATION
7b69a54eeSKentaro Takeda  *
839826a1eSTetsuo Handa  * Version: 2.2.0   2009/04/01
9b69a54eeSKentaro Takeda  *
10b69a54eeSKentaro Takeda  */
11b69a54eeSKentaro Takeda 
12b69a54eeSKentaro Takeda #include "common.h"
13b69a54eeSKentaro Takeda #include "tomoyo.h"
14b69a54eeSKentaro Takeda #include "realpath.h"
15b69a54eeSKentaro Takeda 
16c3fa109aSTetsuo Handa /*
17c3fa109aSTetsuo Handa  * tomoyo_globally_readable_file_entry is a structure which is used for holding
18c3fa109aSTetsuo Handa  * "allow_read" entries.
19c3fa109aSTetsuo Handa  * It has following fields.
20c3fa109aSTetsuo Handa  *
21c3fa109aSTetsuo Handa  *  (1) "list" which is linked to tomoyo_globally_readable_list .
22c3fa109aSTetsuo Handa  *  (2) "filename" is a pathname which is allowed to open(O_RDONLY).
23c3fa109aSTetsuo Handa  *  (3) "is_deleted" is a bool which is true if marked as deleted, false
24c3fa109aSTetsuo Handa  *      otherwise.
25c3fa109aSTetsuo Handa  */
26b69a54eeSKentaro Takeda struct tomoyo_globally_readable_file_entry {
27b69a54eeSKentaro Takeda 	struct list_head list;
28b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *filename;
29b69a54eeSKentaro Takeda 	bool is_deleted;
30b69a54eeSKentaro Takeda };
31b69a54eeSKentaro Takeda 
32c3fa109aSTetsuo Handa /*
33c3fa109aSTetsuo Handa  * tomoyo_pattern_entry is a structure which is used for holding
34c3fa109aSTetsuo Handa  * "tomoyo_pattern_list" entries.
35c3fa109aSTetsuo Handa  * It has following fields.
36c3fa109aSTetsuo Handa  *
37c3fa109aSTetsuo Handa  *  (1) "list" which is linked to tomoyo_pattern_list .
38c3fa109aSTetsuo Handa  *  (2) "pattern" is a pathname pattern which is used for converting pathnames
39c3fa109aSTetsuo Handa  *      to pathname patterns during learning mode.
40c3fa109aSTetsuo Handa  *  (3) "is_deleted" is a bool which is true if marked as deleted, false
41c3fa109aSTetsuo Handa  *      otherwise.
42c3fa109aSTetsuo Handa  */
43b69a54eeSKentaro Takeda struct tomoyo_pattern_entry {
44b69a54eeSKentaro Takeda 	struct list_head list;
45b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *pattern;
46b69a54eeSKentaro Takeda 	bool is_deleted;
47b69a54eeSKentaro Takeda };
48b69a54eeSKentaro Takeda 
49c3fa109aSTetsuo Handa /*
50c3fa109aSTetsuo Handa  * tomoyo_no_rewrite_entry is a structure which is used for holding
51c3fa109aSTetsuo Handa  * "deny_rewrite" entries.
52c3fa109aSTetsuo Handa  * It has following fields.
53c3fa109aSTetsuo Handa  *
54c3fa109aSTetsuo Handa  *  (1) "list" which is linked to tomoyo_no_rewrite_list .
55c3fa109aSTetsuo Handa  *  (2) "pattern" is a pathname which is by default not permitted to modify
56c3fa109aSTetsuo Handa  *      already existing content.
57c3fa109aSTetsuo Handa  *  (3) "is_deleted" is a bool which is true if marked as deleted, false
58c3fa109aSTetsuo Handa  *      otherwise.
59c3fa109aSTetsuo Handa  */
60b69a54eeSKentaro Takeda struct tomoyo_no_rewrite_entry {
61b69a54eeSKentaro Takeda 	struct list_head list;
62b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *pattern;
63b69a54eeSKentaro Takeda 	bool is_deleted;
64b69a54eeSKentaro Takeda };
65b69a54eeSKentaro Takeda 
66b69a54eeSKentaro Takeda /* Keyword array for single path operations. */
67b69a54eeSKentaro Takeda static const char *tomoyo_sp_keyword[TOMOYO_MAX_SINGLE_PATH_OPERATION] = {
68b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_READ_WRITE_ACL] = "read/write",
69b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_EXECUTE_ACL]    = "execute",
70b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_READ_ACL]       = "read",
71b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_WRITE_ACL]      = "write",
72b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_CREATE_ACL]     = "create",
73b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_UNLINK_ACL]     = "unlink",
74b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_MKDIR_ACL]      = "mkdir",
75b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_RMDIR_ACL]      = "rmdir",
76b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_MKFIFO_ACL]     = "mkfifo",
77b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_MKSOCK_ACL]     = "mksock",
78b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_MKBLOCK_ACL]    = "mkblock",
79b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_MKCHAR_ACL]     = "mkchar",
80b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_TRUNCATE_ACL]   = "truncate",
81b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_SYMLINK_ACL]    = "symlink",
82b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_REWRITE_ACL]    = "rewrite",
83937bf613STetsuo Handa 	[TOMOYO_TYPE_IOCTL_ACL]      = "ioctl",
84937bf613STetsuo Handa 	[TOMOYO_TYPE_CHMOD_ACL]      = "chmod",
85937bf613STetsuo Handa 	[TOMOYO_TYPE_CHOWN_ACL]      = "chown",
86937bf613STetsuo Handa 	[TOMOYO_TYPE_CHGRP_ACL]      = "chgrp",
87937bf613STetsuo Handa 	[TOMOYO_TYPE_CHROOT_ACL]     = "chroot",
88937bf613STetsuo Handa 	[TOMOYO_TYPE_MOUNT_ACL]      = "mount",
89937bf613STetsuo Handa 	[TOMOYO_TYPE_UMOUNT_ACL]     = "unmount",
90b69a54eeSKentaro Takeda };
91b69a54eeSKentaro Takeda 
92b69a54eeSKentaro Takeda /* Keyword array for double path operations. */
93b69a54eeSKentaro Takeda static const char *tomoyo_dp_keyword[TOMOYO_MAX_DOUBLE_PATH_OPERATION] = {
94b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_LINK_ACL]    = "link",
95b69a54eeSKentaro Takeda 	[TOMOYO_TYPE_RENAME_ACL]  = "rename",
96937bf613STetsuo Handa 	[TOMOYO_TYPE_PIVOT_ROOT_ACL] = "pivot_root",
97b69a54eeSKentaro Takeda };
98b69a54eeSKentaro Takeda 
99b69a54eeSKentaro Takeda /**
100b69a54eeSKentaro Takeda  * tomoyo_sp2keyword - Get the name of single path operation.
101b69a54eeSKentaro Takeda  *
102b69a54eeSKentaro Takeda  * @operation: Type of operation.
103b69a54eeSKentaro Takeda  *
104b69a54eeSKentaro Takeda  * Returns the name of single path operation.
105b69a54eeSKentaro Takeda  */
106b69a54eeSKentaro Takeda const char *tomoyo_sp2keyword(const u8 operation)
107b69a54eeSKentaro Takeda {
108b69a54eeSKentaro Takeda 	return (operation < TOMOYO_MAX_SINGLE_PATH_OPERATION)
109b69a54eeSKentaro Takeda 		? tomoyo_sp_keyword[operation] : NULL;
110b69a54eeSKentaro Takeda }
111b69a54eeSKentaro Takeda 
112b69a54eeSKentaro Takeda /**
113b69a54eeSKentaro Takeda  * tomoyo_dp2keyword - Get the name of double path operation.
114b69a54eeSKentaro Takeda  *
115b69a54eeSKentaro Takeda  * @operation: Type of operation.
116b69a54eeSKentaro Takeda  *
117b69a54eeSKentaro Takeda  * Returns the name of double path operation.
118b69a54eeSKentaro Takeda  */
119b69a54eeSKentaro Takeda const char *tomoyo_dp2keyword(const u8 operation)
120b69a54eeSKentaro Takeda {
121b69a54eeSKentaro Takeda 	return (operation < TOMOYO_MAX_DOUBLE_PATH_OPERATION)
122b69a54eeSKentaro Takeda 		? tomoyo_dp_keyword[operation] : NULL;
123b69a54eeSKentaro Takeda }
124b69a54eeSKentaro Takeda 
125b69a54eeSKentaro Takeda /**
126b69a54eeSKentaro Takeda  * tomoyo_strendswith - Check whether the token ends with the given token.
127b69a54eeSKentaro Takeda  *
128b69a54eeSKentaro Takeda  * @name: The token to check.
129b69a54eeSKentaro Takeda  * @tail: The token to find.
130b69a54eeSKentaro Takeda  *
131b69a54eeSKentaro Takeda  * Returns true if @name ends with @tail, false otherwise.
132b69a54eeSKentaro Takeda  */
133b69a54eeSKentaro Takeda static bool tomoyo_strendswith(const char *name, const char *tail)
134b69a54eeSKentaro Takeda {
135b69a54eeSKentaro Takeda 	int len;
136b69a54eeSKentaro Takeda 
137b69a54eeSKentaro Takeda 	if (!name || !tail)
138b69a54eeSKentaro Takeda 		return false;
139b69a54eeSKentaro Takeda 	len = strlen(name) - strlen(tail);
140b69a54eeSKentaro Takeda 	return len >= 0 && !strcmp(name + len, tail);
141b69a54eeSKentaro Takeda }
142b69a54eeSKentaro Takeda 
143b69a54eeSKentaro Takeda /**
144b69a54eeSKentaro Takeda  * tomoyo_get_path - Get realpath.
145b69a54eeSKentaro Takeda  *
146b69a54eeSKentaro Takeda  * @path: Pointer to "struct path".
147b69a54eeSKentaro Takeda  *
148b69a54eeSKentaro Takeda  * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
149b69a54eeSKentaro Takeda  */
150b69a54eeSKentaro Takeda static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
151b69a54eeSKentaro Takeda {
152b69a54eeSKentaro Takeda 	int error;
1538e2d39a1STetsuo Handa 	struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
1548e2d39a1STetsuo Handa 							 GFP_KERNEL);
155b69a54eeSKentaro Takeda 
156b69a54eeSKentaro Takeda 	if (!buf)
157b69a54eeSKentaro Takeda 		return NULL;
158b69a54eeSKentaro Takeda 	/* Reserve one byte for appending "/". */
159b69a54eeSKentaro Takeda 	error = tomoyo_realpath_from_path2(path, buf->body,
160b69a54eeSKentaro Takeda 					   sizeof(buf->body) - 2);
161b69a54eeSKentaro Takeda 	if (!error) {
162b69a54eeSKentaro Takeda 		buf->head.name = buf->body;
163b69a54eeSKentaro Takeda 		tomoyo_fill_path_info(&buf->head);
164b69a54eeSKentaro Takeda 		return &buf->head;
165b69a54eeSKentaro Takeda 	}
1668e2d39a1STetsuo Handa 	kfree(buf);
167b69a54eeSKentaro Takeda 	return NULL;
168b69a54eeSKentaro Takeda }
169b69a54eeSKentaro Takeda 
170b69a54eeSKentaro Takeda static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
171b69a54eeSKentaro Takeda 					 const char *filename2,
172b69a54eeSKentaro Takeda 					 struct tomoyo_domain_info *
173b69a54eeSKentaro Takeda 					 const domain, const bool is_delete);
174b69a54eeSKentaro Takeda static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
175b69a54eeSKentaro Takeda 					 struct tomoyo_domain_info *
176b69a54eeSKentaro Takeda 					 const domain, const bool is_delete);
177b69a54eeSKentaro Takeda 
178c3fa109aSTetsuo Handa /*
179c3fa109aSTetsuo Handa  * tomoyo_globally_readable_list is used for holding list of pathnames which
180c3fa109aSTetsuo Handa  * are by default allowed to be open()ed for reading by any process.
181c3fa109aSTetsuo Handa  *
182c3fa109aSTetsuo Handa  * An entry is added by
183c3fa109aSTetsuo Handa  *
184c3fa109aSTetsuo Handa  * # echo 'allow_read /lib/libc-2.5.so' > \
185c3fa109aSTetsuo Handa  *                               /sys/kernel/security/tomoyo/exception_policy
186c3fa109aSTetsuo Handa  *
187c3fa109aSTetsuo Handa  * and is deleted by
188c3fa109aSTetsuo Handa  *
189c3fa109aSTetsuo Handa  * # echo 'delete allow_read /lib/libc-2.5.so' > \
190c3fa109aSTetsuo Handa  *                               /sys/kernel/security/tomoyo/exception_policy
191c3fa109aSTetsuo Handa  *
192c3fa109aSTetsuo Handa  * and all entries are retrieved by
193c3fa109aSTetsuo Handa  *
194c3fa109aSTetsuo Handa  * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
195c3fa109aSTetsuo Handa  *
196c3fa109aSTetsuo Handa  * In the example above, any process is allowed to
197c3fa109aSTetsuo Handa  * open("/lib/libc-2.5.so", O_RDONLY).
198c3fa109aSTetsuo Handa  * One exception is, if the domain which current process belongs to is marked
199c3fa109aSTetsuo Handa  * as "ignore_global_allow_read", current process can't do so unless explicitly
200c3fa109aSTetsuo Handa  * given "allow_read /lib/libc-2.5.so" to the domain which current process
201c3fa109aSTetsuo Handa  * belongs to.
202c3fa109aSTetsuo Handa  */
203b69a54eeSKentaro Takeda static LIST_HEAD(tomoyo_globally_readable_list);
204b69a54eeSKentaro Takeda 
205b69a54eeSKentaro Takeda /**
206b69a54eeSKentaro Takeda  * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
207b69a54eeSKentaro Takeda  *
208b69a54eeSKentaro Takeda  * @filename:  Filename unconditionally permitted to open() for reading.
209b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
210b69a54eeSKentaro Takeda  *
211b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
212fdb8ebb7STetsuo Handa  *
213fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
214b69a54eeSKentaro Takeda  */
215b69a54eeSKentaro Takeda static int tomoyo_update_globally_readable_entry(const char *filename,
216b69a54eeSKentaro Takeda 						 const bool is_delete)
217b69a54eeSKentaro Takeda {
218ca0b7df3STetsuo Handa 	struct tomoyo_globally_readable_file_entry *entry = NULL;
219b69a54eeSKentaro Takeda 	struct tomoyo_globally_readable_file_entry *ptr;
220b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename;
221ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
222b69a54eeSKentaro Takeda 
223b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__))
224b69a54eeSKentaro Takeda 		return -EINVAL;
225bf24fb01STetsuo Handa 	saved_filename = tomoyo_get_name(filename);
226b69a54eeSKentaro Takeda 	if (!saved_filename)
227b69a54eeSKentaro Takeda 		return -ENOMEM;
228ca0b7df3STetsuo Handa 	if (!is_delete)
229ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
230f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
231fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
232b69a54eeSKentaro Takeda 		if (ptr->filename != saved_filename)
233b69a54eeSKentaro Takeda 			continue;
234b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
235b69a54eeSKentaro Takeda 		error = 0;
236ca0b7df3STetsuo Handa 		break;
237b69a54eeSKentaro Takeda 	}
238ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
239ca0b7df3STetsuo Handa 		entry->filename = saved_filename;
240bf24fb01STetsuo Handa 		saved_filename = NULL;
241ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_globally_readable_list);
242ca0b7df3STetsuo Handa 		entry = NULL;
243b69a54eeSKentaro Takeda 		error = 0;
244ca0b7df3STetsuo Handa 	}
245f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
246bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename);
247ca0b7df3STetsuo Handa 	kfree(entry);
248b69a54eeSKentaro Takeda 	return error;
249b69a54eeSKentaro Takeda }
250b69a54eeSKentaro Takeda 
251b69a54eeSKentaro Takeda /**
252b69a54eeSKentaro Takeda  * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
253b69a54eeSKentaro Takeda  *
254b69a54eeSKentaro Takeda  * @filename: The filename to check.
255b69a54eeSKentaro Takeda  *
256b69a54eeSKentaro Takeda  * Returns true if any domain can open @filename for reading, false otherwise.
257fdb8ebb7STetsuo Handa  *
258fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
259b69a54eeSKentaro Takeda  */
260b69a54eeSKentaro Takeda static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
261b69a54eeSKentaro Takeda 					     filename)
262b69a54eeSKentaro Takeda {
263b69a54eeSKentaro Takeda 	struct tomoyo_globally_readable_file_entry *ptr;
264b69a54eeSKentaro Takeda 	bool found = false;
265fdb8ebb7STetsuo Handa 
266fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
267b69a54eeSKentaro Takeda 		if (!ptr->is_deleted &&
268b69a54eeSKentaro Takeda 		    tomoyo_path_matches_pattern(filename, ptr->filename)) {
269b69a54eeSKentaro Takeda 			found = true;
270b69a54eeSKentaro Takeda 			break;
271b69a54eeSKentaro Takeda 		}
272b69a54eeSKentaro Takeda 	}
273b69a54eeSKentaro Takeda 	return found;
274b69a54eeSKentaro Takeda }
275b69a54eeSKentaro Takeda 
276b69a54eeSKentaro Takeda /**
277b69a54eeSKentaro Takeda  * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
278b69a54eeSKentaro Takeda  *
279b69a54eeSKentaro Takeda  * @data:      String to parse.
280b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
281b69a54eeSKentaro Takeda  *
282b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
283fdb8ebb7STetsuo Handa  *
284fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
285b69a54eeSKentaro Takeda  */
286b69a54eeSKentaro Takeda int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
287b69a54eeSKentaro Takeda {
288b69a54eeSKentaro Takeda 	return tomoyo_update_globally_readable_entry(data, is_delete);
289b69a54eeSKentaro Takeda }
290b69a54eeSKentaro Takeda 
291b69a54eeSKentaro Takeda /**
292b69a54eeSKentaro Takeda  * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
293b69a54eeSKentaro Takeda  *
294b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
295b69a54eeSKentaro Takeda  *
296b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
297fdb8ebb7STetsuo Handa  *
298fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
299b69a54eeSKentaro Takeda  */
300b69a54eeSKentaro Takeda bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
301b69a54eeSKentaro Takeda {
302b69a54eeSKentaro Takeda 	struct list_head *pos;
303b69a54eeSKentaro Takeda 	bool done = true;
304b69a54eeSKentaro Takeda 
305b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2,
306b69a54eeSKentaro Takeda 			     &tomoyo_globally_readable_list) {
307b69a54eeSKentaro Takeda 		struct tomoyo_globally_readable_file_entry *ptr;
308b69a54eeSKentaro Takeda 		ptr = list_entry(pos,
309b69a54eeSKentaro Takeda 				 struct tomoyo_globally_readable_file_entry,
310b69a54eeSKentaro Takeda 				 list);
311b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
312b69a54eeSKentaro Takeda 			continue;
3137d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
3147d2948b1STetsuo Handa 					ptr->filename->name);
3157d2948b1STetsuo Handa 		if (!done)
316b69a54eeSKentaro Takeda 			break;
317b69a54eeSKentaro Takeda 	}
318b69a54eeSKentaro Takeda 	return done;
319b69a54eeSKentaro Takeda }
320b69a54eeSKentaro Takeda 
321c3fa109aSTetsuo Handa /* tomoyo_pattern_list is used for holding list of pathnames which are used for
322c3fa109aSTetsuo Handa  * converting pathnames to pathname patterns during learning mode.
323c3fa109aSTetsuo Handa  *
324c3fa109aSTetsuo Handa  * An entry is added by
325c3fa109aSTetsuo Handa  *
326c3fa109aSTetsuo Handa  * # echo 'file_pattern /proc/\$/mounts' > \
327c3fa109aSTetsuo Handa  *                             /sys/kernel/security/tomoyo/exception_policy
328c3fa109aSTetsuo Handa  *
329c3fa109aSTetsuo Handa  * and is deleted by
330c3fa109aSTetsuo Handa  *
331c3fa109aSTetsuo Handa  * # echo 'delete file_pattern /proc/\$/mounts' > \
332c3fa109aSTetsuo Handa  *                             /sys/kernel/security/tomoyo/exception_policy
333c3fa109aSTetsuo Handa  *
334c3fa109aSTetsuo Handa  * and all entries are retrieved by
335c3fa109aSTetsuo Handa  *
336c3fa109aSTetsuo Handa  * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
337c3fa109aSTetsuo Handa  *
338c3fa109aSTetsuo Handa  * In the example above, if a process which belongs to a domain which is in
339c3fa109aSTetsuo Handa  * learning mode requested open("/proc/1/mounts", O_RDONLY),
340c3fa109aSTetsuo Handa  * "allow_read /proc/\$/mounts" is automatically added to the domain which that
341c3fa109aSTetsuo Handa  * process belongs to.
342c3fa109aSTetsuo Handa  *
343c3fa109aSTetsuo Handa  * It is not a desirable behavior that we have to use /proc/\$/ instead of
344c3fa109aSTetsuo Handa  * /proc/self/ when current process needs to access only current process's
345c3fa109aSTetsuo Handa  * information. As of now, LSM version of TOMOYO is using __d_path() for
346c3fa109aSTetsuo Handa  * calculating pathname. Non LSM version of TOMOYO is using its own function
347c3fa109aSTetsuo Handa  * which pretends as if /proc/self/ is not a symlink; so that we can forbid
348c3fa109aSTetsuo Handa  * current process from accessing other process's information.
349c3fa109aSTetsuo Handa  */
350b69a54eeSKentaro Takeda static LIST_HEAD(tomoyo_pattern_list);
351b69a54eeSKentaro Takeda 
352b69a54eeSKentaro Takeda /**
353b69a54eeSKentaro Takeda  * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
354b69a54eeSKentaro Takeda  *
355b69a54eeSKentaro Takeda  * @pattern:   Pathname pattern.
356b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
357b69a54eeSKentaro Takeda  *
358b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
359fdb8ebb7STetsuo Handa  *
360fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
361b69a54eeSKentaro Takeda  */
362b69a54eeSKentaro Takeda static int tomoyo_update_file_pattern_entry(const char *pattern,
363b69a54eeSKentaro Takeda 					    const bool is_delete)
364b69a54eeSKentaro Takeda {
365ca0b7df3STetsuo Handa 	struct tomoyo_pattern_entry *entry = NULL;
366b69a54eeSKentaro Takeda 	struct tomoyo_pattern_entry *ptr;
367b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_pattern;
368ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
369b69a54eeSKentaro Takeda 
370bf24fb01STetsuo Handa 	saved_pattern = tomoyo_get_name(pattern);
371b69a54eeSKentaro Takeda 	if (!saved_pattern)
372ca0b7df3STetsuo Handa 		return error;
373ca0b7df3STetsuo Handa 	if (!saved_pattern->is_patterned)
374ca0b7df3STetsuo Handa 		goto out;
375ca0b7df3STetsuo Handa 	if (!is_delete)
376ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
377f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
378fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
379b69a54eeSKentaro Takeda 		if (saved_pattern != ptr->pattern)
380b69a54eeSKentaro Takeda 			continue;
381b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
382b69a54eeSKentaro Takeda 		error = 0;
383ca0b7df3STetsuo Handa 		break;
384b69a54eeSKentaro Takeda 	}
385ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
386ca0b7df3STetsuo Handa 		entry->pattern = saved_pattern;
387bf24fb01STetsuo Handa 		saved_pattern = NULL;
388ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
389ca0b7df3STetsuo Handa 		entry = NULL;
390b69a54eeSKentaro Takeda 		error = 0;
391ca0b7df3STetsuo Handa 	}
392f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
393ca0b7df3STetsuo Handa  out:
394ca0b7df3STetsuo Handa 	kfree(entry);
395bf24fb01STetsuo Handa 	tomoyo_put_name(saved_pattern);
396b69a54eeSKentaro Takeda 	return error;
397b69a54eeSKentaro Takeda }
398b69a54eeSKentaro Takeda 
399b69a54eeSKentaro Takeda /**
400b69a54eeSKentaro Takeda  * tomoyo_get_file_pattern - Get patterned pathname.
401b69a54eeSKentaro Takeda  *
402b69a54eeSKentaro Takeda  * @filename: The filename to find patterned pathname.
403b69a54eeSKentaro Takeda  *
404b69a54eeSKentaro Takeda  * Returns pointer to pathname pattern if matched, @filename otherwise.
405fdb8ebb7STetsuo Handa  *
406fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
407b69a54eeSKentaro Takeda  */
408b69a54eeSKentaro Takeda static const struct tomoyo_path_info *
409b69a54eeSKentaro Takeda tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
410b69a54eeSKentaro Takeda {
411b69a54eeSKentaro Takeda 	struct tomoyo_pattern_entry *ptr;
412b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *pattern = NULL;
413b69a54eeSKentaro Takeda 
414fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
415b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
416b69a54eeSKentaro Takeda 			continue;
417b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
418b69a54eeSKentaro Takeda 			continue;
419b69a54eeSKentaro Takeda 		pattern = ptr->pattern;
420b69a54eeSKentaro Takeda 		if (tomoyo_strendswith(pattern->name, "/\\*")) {
421b69a54eeSKentaro Takeda 			/* Do nothing. Try to find the better match. */
422b69a54eeSKentaro Takeda 		} else {
423b69a54eeSKentaro Takeda 			/* This would be the better match. Use this. */
424b69a54eeSKentaro Takeda 			break;
425b69a54eeSKentaro Takeda 		}
426b69a54eeSKentaro Takeda 	}
427b69a54eeSKentaro Takeda 	if (pattern)
428b69a54eeSKentaro Takeda 		filename = pattern;
429b69a54eeSKentaro Takeda 	return filename;
430b69a54eeSKentaro Takeda }
431b69a54eeSKentaro Takeda 
432b69a54eeSKentaro Takeda /**
433b69a54eeSKentaro Takeda  * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
434b69a54eeSKentaro Takeda  *
435b69a54eeSKentaro Takeda  * @data:      String to parse.
436b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
437b69a54eeSKentaro Takeda  *
438b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
439fdb8ebb7STetsuo Handa  *
440fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
441b69a54eeSKentaro Takeda  */
442b69a54eeSKentaro Takeda int tomoyo_write_pattern_policy(char *data, const bool is_delete)
443b69a54eeSKentaro Takeda {
444b69a54eeSKentaro Takeda 	return tomoyo_update_file_pattern_entry(data, is_delete);
445b69a54eeSKentaro Takeda }
446b69a54eeSKentaro Takeda 
447b69a54eeSKentaro Takeda /**
448b69a54eeSKentaro Takeda  * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
449b69a54eeSKentaro Takeda  *
450b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
451b69a54eeSKentaro Takeda  *
452b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
453fdb8ebb7STetsuo Handa  *
454fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
455b69a54eeSKentaro Takeda  */
456b69a54eeSKentaro Takeda bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
457b69a54eeSKentaro Takeda {
458b69a54eeSKentaro Takeda 	struct list_head *pos;
459b69a54eeSKentaro Takeda 	bool done = true;
460b69a54eeSKentaro Takeda 
461b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
462b69a54eeSKentaro Takeda 		struct tomoyo_pattern_entry *ptr;
463b69a54eeSKentaro Takeda 		ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
464b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
465b69a54eeSKentaro Takeda 			continue;
4667d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
4677d2948b1STetsuo Handa 					"%s\n", ptr->pattern->name);
4687d2948b1STetsuo Handa 		if (!done)
469b69a54eeSKentaro Takeda 			break;
470b69a54eeSKentaro Takeda 	}
471b69a54eeSKentaro Takeda 	return done;
472b69a54eeSKentaro Takeda }
473b69a54eeSKentaro Takeda 
474c3fa109aSTetsuo Handa /*
475c3fa109aSTetsuo Handa  * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
476c3fa109aSTetsuo Handa  * default forbidden to modify already written content of a file.
477c3fa109aSTetsuo Handa  *
478c3fa109aSTetsuo Handa  * An entry is added by
479c3fa109aSTetsuo Handa  *
480c3fa109aSTetsuo Handa  * # echo 'deny_rewrite /var/log/messages' > \
481c3fa109aSTetsuo Handa  *                              /sys/kernel/security/tomoyo/exception_policy
482c3fa109aSTetsuo Handa  *
483c3fa109aSTetsuo Handa  * and is deleted by
484c3fa109aSTetsuo Handa  *
485c3fa109aSTetsuo Handa  * # echo 'delete deny_rewrite /var/log/messages' > \
486c3fa109aSTetsuo Handa  *                              /sys/kernel/security/tomoyo/exception_policy
487c3fa109aSTetsuo Handa  *
488c3fa109aSTetsuo Handa  * and all entries are retrieved by
489c3fa109aSTetsuo Handa  *
490c3fa109aSTetsuo Handa  * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
491c3fa109aSTetsuo Handa  *
492c3fa109aSTetsuo Handa  * In the example above, if a process requested to rewrite /var/log/messages ,
493c3fa109aSTetsuo Handa  * the process can't rewrite unless the domain which that process belongs to
494c3fa109aSTetsuo Handa  * has "allow_rewrite /var/log/messages" entry.
495c3fa109aSTetsuo Handa  *
496c3fa109aSTetsuo Handa  * It is not a desirable behavior that we have to add "\040(deleted)" suffix
497c3fa109aSTetsuo Handa  * when we want to allow rewriting already unlink()ed file. As of now,
498c3fa109aSTetsuo Handa  * LSM version of TOMOYO is using __d_path() for calculating pathname.
499c3fa109aSTetsuo Handa  * Non LSM version of TOMOYO is using its own function which doesn't append
500c3fa109aSTetsuo Handa  * " (deleted)" suffix if the file is already unlink()ed; so that we don't
501c3fa109aSTetsuo Handa  * need to worry whether the file is already unlink()ed or not.
502c3fa109aSTetsuo Handa  */
503b69a54eeSKentaro Takeda static LIST_HEAD(tomoyo_no_rewrite_list);
504b69a54eeSKentaro Takeda 
505b69a54eeSKentaro Takeda /**
506b69a54eeSKentaro Takeda  * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
507b69a54eeSKentaro Takeda  *
508b69a54eeSKentaro Takeda  * @pattern:   Pathname pattern that are not rewritable by default.
509b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
510b69a54eeSKentaro Takeda  *
511b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
512fdb8ebb7STetsuo Handa  *
513fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
514b69a54eeSKentaro Takeda  */
515b69a54eeSKentaro Takeda static int tomoyo_update_no_rewrite_entry(const char *pattern,
516b69a54eeSKentaro Takeda 					  const bool is_delete)
517b69a54eeSKentaro Takeda {
518ca0b7df3STetsuo Handa 	struct tomoyo_no_rewrite_entry *entry = NULL;
519ca0b7df3STetsuo Handa 	struct tomoyo_no_rewrite_entry *ptr;
520b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_pattern;
521ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
522b69a54eeSKentaro Takeda 
523b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__))
524b69a54eeSKentaro Takeda 		return -EINVAL;
525bf24fb01STetsuo Handa 	saved_pattern = tomoyo_get_name(pattern);
526b69a54eeSKentaro Takeda 	if (!saved_pattern)
527ca0b7df3STetsuo Handa 		return error;
528ca0b7df3STetsuo Handa 	if (!is_delete)
529ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
530f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
531fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
532b69a54eeSKentaro Takeda 		if (ptr->pattern != saved_pattern)
533b69a54eeSKentaro Takeda 			continue;
534b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
535b69a54eeSKentaro Takeda 		error = 0;
536ca0b7df3STetsuo Handa 		break;
537b69a54eeSKentaro Takeda 	}
538ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
539ca0b7df3STetsuo Handa 		entry->pattern = saved_pattern;
540bf24fb01STetsuo Handa 		saved_pattern = NULL;
541ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_no_rewrite_list);
542ca0b7df3STetsuo Handa 		entry = NULL;
543b69a54eeSKentaro Takeda 		error = 0;
544ca0b7df3STetsuo Handa 	}
545f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
546bf24fb01STetsuo Handa 	tomoyo_put_name(saved_pattern);
547ca0b7df3STetsuo Handa 	kfree(entry);
548b69a54eeSKentaro Takeda 	return error;
549b69a54eeSKentaro Takeda }
550b69a54eeSKentaro Takeda 
551b69a54eeSKentaro Takeda /**
552b69a54eeSKentaro Takeda  * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
553b69a54eeSKentaro Takeda  *
554b69a54eeSKentaro Takeda  * @filename: Filename to check.
555b69a54eeSKentaro Takeda  *
556b69a54eeSKentaro Takeda  * Returns true if @filename is specified by "deny_rewrite" directive,
557b69a54eeSKentaro Takeda  * false otherwise.
558fdb8ebb7STetsuo Handa  *
559fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
560b69a54eeSKentaro Takeda  */
561b69a54eeSKentaro Takeda static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
562b69a54eeSKentaro Takeda {
563b69a54eeSKentaro Takeda 	struct tomoyo_no_rewrite_entry *ptr;
564b69a54eeSKentaro Takeda 	bool found = false;
565b69a54eeSKentaro Takeda 
566fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
567b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
568b69a54eeSKentaro Takeda 			continue;
569b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
570b69a54eeSKentaro Takeda 			continue;
571b69a54eeSKentaro Takeda 		found = true;
572b69a54eeSKentaro Takeda 		break;
573b69a54eeSKentaro Takeda 	}
574b69a54eeSKentaro Takeda 	return found;
575b69a54eeSKentaro Takeda }
576b69a54eeSKentaro Takeda 
577b69a54eeSKentaro Takeda /**
578b69a54eeSKentaro Takeda  * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
579b69a54eeSKentaro Takeda  *
580b69a54eeSKentaro Takeda  * @data:      String to parse.
581b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
582b69a54eeSKentaro Takeda  *
583b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
584fdb8ebb7STetsuo Handa  *
585fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
586b69a54eeSKentaro Takeda  */
587b69a54eeSKentaro Takeda int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
588b69a54eeSKentaro Takeda {
589b69a54eeSKentaro Takeda 	return tomoyo_update_no_rewrite_entry(data, is_delete);
590b69a54eeSKentaro Takeda }
591b69a54eeSKentaro Takeda 
592b69a54eeSKentaro Takeda /**
593b69a54eeSKentaro Takeda  * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
594b69a54eeSKentaro Takeda  *
595b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
596b69a54eeSKentaro Takeda  *
597b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
598fdb8ebb7STetsuo Handa  *
599fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
600b69a54eeSKentaro Takeda  */
601b69a54eeSKentaro Takeda bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
602b69a54eeSKentaro Takeda {
603b69a54eeSKentaro Takeda 	struct list_head *pos;
604b69a54eeSKentaro Takeda 	bool done = true;
605b69a54eeSKentaro Takeda 
606b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
607b69a54eeSKentaro Takeda 		struct tomoyo_no_rewrite_entry *ptr;
608b69a54eeSKentaro Takeda 		ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
609b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
610b69a54eeSKentaro Takeda 			continue;
6117d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
6127d2948b1STetsuo Handa 					"%s\n", ptr->pattern->name);
6137d2948b1STetsuo Handa 		if (!done)
614b69a54eeSKentaro Takeda 			break;
615b69a54eeSKentaro Takeda 	}
616b69a54eeSKentaro Takeda 	return done;
617b69a54eeSKentaro Takeda }
618b69a54eeSKentaro Takeda 
619b69a54eeSKentaro Takeda /**
620b69a54eeSKentaro Takeda  * tomoyo_update_file_acl - Update file's read/write/execute ACL.
621b69a54eeSKentaro Takeda  *
622b69a54eeSKentaro Takeda  * @filename:  Filename.
623b69a54eeSKentaro Takeda  * @perm:      Permission (between 1 to 7).
624b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
625b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
626b69a54eeSKentaro Takeda  *
627b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
628b69a54eeSKentaro Takeda  *
629b69a54eeSKentaro Takeda  * This is legacy support interface for older policy syntax.
630b69a54eeSKentaro Takeda  * Current policy syntax uses "allow_read/write" instead of "6",
631b69a54eeSKentaro Takeda  * "allow_read" instead of "4", "allow_write" instead of "2",
632b69a54eeSKentaro Takeda  * "allow_execute" instead of "1".
633fdb8ebb7STetsuo Handa  *
634fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
635b69a54eeSKentaro Takeda  */
636b69a54eeSKentaro Takeda static int tomoyo_update_file_acl(const char *filename, u8 perm,
637b69a54eeSKentaro Takeda 				  struct tomoyo_domain_info * const domain,
638b69a54eeSKentaro Takeda 				  const bool is_delete)
639b69a54eeSKentaro Takeda {
640b69a54eeSKentaro Takeda 	if (perm > 7 || !perm) {
641b69a54eeSKentaro Takeda 		printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
642b69a54eeSKentaro Takeda 		       __func__, perm, filename);
643b69a54eeSKentaro Takeda 		return -EINVAL;
644b69a54eeSKentaro Takeda 	}
645b69a54eeSKentaro Takeda 	if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
646b69a54eeSKentaro Takeda 		/*
647b69a54eeSKentaro Takeda 		 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
648b69a54eeSKentaro Takeda 		 * directory permissions.
649b69a54eeSKentaro Takeda 		 */
650b69a54eeSKentaro Takeda 		return 0;
651b69a54eeSKentaro Takeda 	if (perm & 4)
652b69a54eeSKentaro Takeda 		tomoyo_update_single_path_acl(TOMOYO_TYPE_READ_ACL, filename,
653b69a54eeSKentaro Takeda 					      domain, is_delete);
654b69a54eeSKentaro Takeda 	if (perm & 2)
655b69a54eeSKentaro Takeda 		tomoyo_update_single_path_acl(TOMOYO_TYPE_WRITE_ACL, filename,
656b69a54eeSKentaro Takeda 					      domain, is_delete);
657b69a54eeSKentaro Takeda 	if (perm & 1)
658b69a54eeSKentaro Takeda 		tomoyo_update_single_path_acl(TOMOYO_TYPE_EXECUTE_ACL,
659b69a54eeSKentaro Takeda 					      filename, domain, is_delete);
660b69a54eeSKentaro Takeda 	return 0;
661b69a54eeSKentaro Takeda }
662b69a54eeSKentaro Takeda 
663b69a54eeSKentaro Takeda /**
664b69a54eeSKentaro Takeda  * tomoyo_check_single_path_acl2 - Check permission for single path operation.
665b69a54eeSKentaro Takeda  *
666b69a54eeSKentaro Takeda  * @domain:          Pointer to "struct tomoyo_domain_info".
667b69a54eeSKentaro Takeda  * @filename:        Filename to check.
668b69a54eeSKentaro Takeda  * @perm:            Permission.
669b69a54eeSKentaro Takeda  * @may_use_pattern: True if patterned ACL is permitted.
670b69a54eeSKentaro Takeda  *
671b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
672fdb8ebb7STetsuo Handa  *
673fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
674b69a54eeSKentaro Takeda  */
675b69a54eeSKentaro Takeda static int tomoyo_check_single_path_acl2(const struct tomoyo_domain_info *
676b69a54eeSKentaro Takeda 					 domain,
677b69a54eeSKentaro Takeda 					 const struct tomoyo_path_info *
678b69a54eeSKentaro Takeda 					 filename,
679937bf613STetsuo Handa 					 const u32 perm,
680b69a54eeSKentaro Takeda 					 const bool may_use_pattern)
681b69a54eeSKentaro Takeda {
682b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
683b69a54eeSKentaro Takeda 	int error = -EPERM;
684b69a54eeSKentaro Takeda 
685fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
686b69a54eeSKentaro Takeda 		struct tomoyo_single_path_acl_record *acl;
687ea13ddbaSTetsuo Handa 		if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL)
688b69a54eeSKentaro Takeda 			continue;
689b69a54eeSKentaro Takeda 		acl = container_of(ptr, struct tomoyo_single_path_acl_record,
690b69a54eeSKentaro Takeda 				   head);
691937bf613STetsuo Handa 		if (perm <= 0xFFFF) {
692b69a54eeSKentaro Takeda 			if (!(acl->perm & perm))
693b69a54eeSKentaro Takeda 				continue;
694937bf613STetsuo Handa 		} else {
695937bf613STetsuo Handa 			if (!(acl->perm_high & (perm >> 16)))
696937bf613STetsuo Handa 				continue;
697937bf613STetsuo Handa 		}
698b69a54eeSKentaro Takeda 		if (may_use_pattern || !acl->filename->is_patterned) {
699b69a54eeSKentaro Takeda 			if (!tomoyo_path_matches_pattern(filename,
700b69a54eeSKentaro Takeda 							 acl->filename))
701b69a54eeSKentaro Takeda 				continue;
702b69a54eeSKentaro Takeda 		} else {
703b69a54eeSKentaro Takeda 			continue;
704b69a54eeSKentaro Takeda 		}
705b69a54eeSKentaro Takeda 		error = 0;
706b69a54eeSKentaro Takeda 		break;
707b69a54eeSKentaro Takeda 	}
708b69a54eeSKentaro Takeda 	return error;
709b69a54eeSKentaro Takeda }
710b69a54eeSKentaro Takeda 
711b69a54eeSKentaro Takeda /**
712b69a54eeSKentaro Takeda  * tomoyo_check_file_acl - Check permission for opening files.
713b69a54eeSKentaro Takeda  *
714b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
715b69a54eeSKentaro Takeda  * @filename:  Filename to check.
716b69a54eeSKentaro Takeda  * @operation: Mode ("read" or "write" or "read/write" or "execute").
717b69a54eeSKentaro Takeda  *
718b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
719fdb8ebb7STetsuo Handa  *
720fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
721b69a54eeSKentaro Takeda  */
722b69a54eeSKentaro Takeda static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
723b69a54eeSKentaro Takeda 				 const struct tomoyo_path_info *filename,
724b69a54eeSKentaro Takeda 				 const u8 operation)
725b69a54eeSKentaro Takeda {
726937bf613STetsuo Handa 	u32 perm = 0;
727b69a54eeSKentaro Takeda 
728b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
729b69a54eeSKentaro Takeda 		return 0;
730b69a54eeSKentaro Takeda 	if (operation == 6)
731b69a54eeSKentaro Takeda 		perm = 1 << TOMOYO_TYPE_READ_WRITE_ACL;
732b69a54eeSKentaro Takeda 	else if (operation == 4)
733b69a54eeSKentaro Takeda 		perm = 1 << TOMOYO_TYPE_READ_ACL;
734b69a54eeSKentaro Takeda 	else if (operation == 2)
735b69a54eeSKentaro Takeda 		perm = 1 << TOMOYO_TYPE_WRITE_ACL;
736b69a54eeSKentaro Takeda 	else if (operation == 1)
737b69a54eeSKentaro Takeda 		perm = 1 << TOMOYO_TYPE_EXECUTE_ACL;
738b69a54eeSKentaro Takeda 	else
739b69a54eeSKentaro Takeda 		BUG();
740b69a54eeSKentaro Takeda 	return tomoyo_check_single_path_acl2(domain, filename, perm,
741b69a54eeSKentaro Takeda 					     operation != 1);
742b69a54eeSKentaro Takeda }
743b69a54eeSKentaro Takeda 
744b69a54eeSKentaro Takeda /**
745b69a54eeSKentaro Takeda  * tomoyo_check_file_perm2 - Check permission for opening files.
746b69a54eeSKentaro Takeda  *
747b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
748b69a54eeSKentaro Takeda  * @filename:  Filename to check.
749b69a54eeSKentaro Takeda  * @perm:      Mode ("read" or "write" or "read/write" or "execute").
750b69a54eeSKentaro Takeda  * @operation: Operation name passed used for verbose mode.
751b69a54eeSKentaro Takeda  * @mode:      Access control mode.
752b69a54eeSKentaro Takeda  *
753b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
754fdb8ebb7STetsuo Handa  *
755fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
756b69a54eeSKentaro Takeda  */
757b69a54eeSKentaro Takeda static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain,
758b69a54eeSKentaro Takeda 				   const struct tomoyo_path_info *filename,
759b69a54eeSKentaro Takeda 				   const u8 perm, const char *operation,
760b69a54eeSKentaro Takeda 				   const u8 mode)
761b69a54eeSKentaro Takeda {
762b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
763b69a54eeSKentaro Takeda 	const char *msg = "<unknown>";
764b69a54eeSKentaro Takeda 	int error = 0;
765b69a54eeSKentaro Takeda 
766b69a54eeSKentaro Takeda 	if (!filename)
767b69a54eeSKentaro Takeda 		return 0;
768b69a54eeSKentaro Takeda 	error = tomoyo_check_file_acl(domain, filename, perm);
769ea13ddbaSTetsuo Handa 	if (error && perm == 4 && !domain->ignore_global_allow_read
770b69a54eeSKentaro Takeda 	    && tomoyo_is_globally_readable_file(filename))
771b69a54eeSKentaro Takeda 		error = 0;
772b69a54eeSKentaro Takeda 	if (perm == 6)
773b69a54eeSKentaro Takeda 		msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_WRITE_ACL);
774b69a54eeSKentaro Takeda 	else if (perm == 4)
775b69a54eeSKentaro Takeda 		msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_ACL);
776b69a54eeSKentaro Takeda 	else if (perm == 2)
777b69a54eeSKentaro Takeda 		msg = tomoyo_sp2keyword(TOMOYO_TYPE_WRITE_ACL);
778b69a54eeSKentaro Takeda 	else if (perm == 1)
779b69a54eeSKentaro Takeda 		msg = tomoyo_sp2keyword(TOMOYO_TYPE_EXECUTE_ACL);
780b69a54eeSKentaro Takeda 	else
781b69a54eeSKentaro Takeda 		BUG();
782b69a54eeSKentaro Takeda 	if (!error)
783b69a54eeSKentaro Takeda 		return 0;
784b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
785b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied "
786b69a54eeSKentaro Takeda 		       "for %s\n", tomoyo_get_msg(is_enforce), msg, operation,
787b69a54eeSKentaro Takeda 		       filename->name, tomoyo_get_last_name(domain));
788b69a54eeSKentaro Takeda 	if (is_enforce)
789b69a54eeSKentaro Takeda 		return error;
790b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
791b69a54eeSKentaro Takeda 		/* Don't use patterns for execute permission. */
792b69a54eeSKentaro Takeda 		const struct tomoyo_path_info *patterned_file = (perm != 1) ?
793b69a54eeSKentaro Takeda 			tomoyo_get_file_pattern(filename) : filename;
794b69a54eeSKentaro Takeda 		tomoyo_update_file_acl(patterned_file->name, perm,
795b69a54eeSKentaro Takeda 				       domain, false);
796b69a54eeSKentaro Takeda 	}
797b69a54eeSKentaro Takeda 	return 0;
798b69a54eeSKentaro Takeda }
799b69a54eeSKentaro Takeda 
800b69a54eeSKentaro Takeda /**
801b69a54eeSKentaro Takeda  * tomoyo_write_file_policy - Update file related list.
802b69a54eeSKentaro Takeda  *
803b69a54eeSKentaro Takeda  * @data:      String to parse.
804b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
805b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
806b69a54eeSKentaro Takeda  *
807b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
808fdb8ebb7STetsuo Handa  *
809fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
810b69a54eeSKentaro Takeda  */
811b69a54eeSKentaro Takeda int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
812b69a54eeSKentaro Takeda 			     const bool is_delete)
813b69a54eeSKentaro Takeda {
814b69a54eeSKentaro Takeda 	char *filename = strchr(data, ' ');
815b69a54eeSKentaro Takeda 	char *filename2;
816b69a54eeSKentaro Takeda 	unsigned int perm;
817b69a54eeSKentaro Takeda 	u8 type;
818b69a54eeSKentaro Takeda 
819b69a54eeSKentaro Takeda 	if (!filename)
820b69a54eeSKentaro Takeda 		return -EINVAL;
821b69a54eeSKentaro Takeda 	*filename++ = '\0';
822b69a54eeSKentaro Takeda 	if (sscanf(data, "%u", &perm) == 1)
823b69a54eeSKentaro Takeda 		return tomoyo_update_file_acl(filename, (u8) perm, domain,
824b69a54eeSKentaro Takeda 					      is_delete);
825b69a54eeSKentaro Takeda 	if (strncmp(data, "allow_", 6))
826b69a54eeSKentaro Takeda 		goto out;
827b69a54eeSKentaro Takeda 	data += 6;
828b69a54eeSKentaro Takeda 	for (type = 0; type < TOMOYO_MAX_SINGLE_PATH_OPERATION; type++) {
829b69a54eeSKentaro Takeda 		if (strcmp(data, tomoyo_sp_keyword[type]))
830b69a54eeSKentaro Takeda 			continue;
831b69a54eeSKentaro Takeda 		return tomoyo_update_single_path_acl(type, filename,
832b69a54eeSKentaro Takeda 						     domain, is_delete);
833b69a54eeSKentaro Takeda 	}
834b69a54eeSKentaro Takeda 	filename2 = strchr(filename, ' ');
835b69a54eeSKentaro Takeda 	if (!filename2)
836b69a54eeSKentaro Takeda 		goto out;
837b69a54eeSKentaro Takeda 	*filename2++ = '\0';
838b69a54eeSKentaro Takeda 	for (type = 0; type < TOMOYO_MAX_DOUBLE_PATH_OPERATION; type++) {
839b69a54eeSKentaro Takeda 		if (strcmp(data, tomoyo_dp_keyword[type]))
840b69a54eeSKentaro Takeda 			continue;
841b69a54eeSKentaro Takeda 		return tomoyo_update_double_path_acl(type, filename, filename2,
842b69a54eeSKentaro Takeda 						     domain, is_delete);
843b69a54eeSKentaro Takeda 	}
844b69a54eeSKentaro Takeda  out:
845b69a54eeSKentaro Takeda 	return -EINVAL;
846b69a54eeSKentaro Takeda }
847b69a54eeSKentaro Takeda 
848b69a54eeSKentaro Takeda /**
849b69a54eeSKentaro Takeda  * tomoyo_update_single_path_acl - Update "struct tomoyo_single_path_acl_record" list.
850b69a54eeSKentaro Takeda  *
851b69a54eeSKentaro Takeda  * @type:      Type of operation.
852b69a54eeSKentaro Takeda  * @filename:  Filename.
853b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
854b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
855b69a54eeSKentaro Takeda  *
856b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
857fdb8ebb7STetsuo Handa  *
858fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
859b69a54eeSKentaro Takeda  */
860b69a54eeSKentaro Takeda static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
861b69a54eeSKentaro Takeda 					 struct tomoyo_domain_info *
862b69a54eeSKentaro Takeda 					 const domain, const bool is_delete)
863b69a54eeSKentaro Takeda {
864937bf613STetsuo Handa 	static const u32 rw_mask =
865b69a54eeSKentaro Takeda 		(1 << TOMOYO_TYPE_READ_ACL) | (1 << TOMOYO_TYPE_WRITE_ACL);
866b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename;
867b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
868ca0b7df3STetsuo Handa 	struct tomoyo_single_path_acl_record *entry = NULL;
869ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
870937bf613STetsuo Handa 	const u32 perm = 1 << type;
871b69a54eeSKentaro Takeda 
872b69a54eeSKentaro Takeda 	if (!domain)
873b69a54eeSKentaro Takeda 		return -EINVAL;
874b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__))
875b69a54eeSKentaro Takeda 		return -EINVAL;
876bf24fb01STetsuo Handa 	saved_filename = tomoyo_get_name(filename);
877b69a54eeSKentaro Takeda 	if (!saved_filename)
878b69a54eeSKentaro Takeda 		return -ENOMEM;
879ca0b7df3STetsuo Handa 	if (!is_delete)
880ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
881f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
882fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
883ca0b7df3STetsuo Handa 		struct tomoyo_single_path_acl_record *acl =
884ca0b7df3STetsuo Handa 			container_of(ptr, struct tomoyo_single_path_acl_record,
885ca0b7df3STetsuo Handa 				     head);
886ea13ddbaSTetsuo Handa 		if (ptr->type != TOMOYO_TYPE_SINGLE_PATH_ACL)
887b69a54eeSKentaro Takeda 			continue;
888b69a54eeSKentaro Takeda 		if (acl->filename != saved_filename)
889b69a54eeSKentaro Takeda 			continue;
890ca0b7df3STetsuo Handa 		if (is_delete) {
891ca0b7df3STetsuo Handa 			if (perm <= 0xFFFF)
892ca0b7df3STetsuo Handa 				acl->perm &= ~perm;
893ca0b7df3STetsuo Handa 			else
894ca0b7df3STetsuo Handa 				acl->perm_high &= ~(perm >> 16);
895ca0b7df3STetsuo Handa 			if ((acl->perm & rw_mask) != rw_mask)
896ca0b7df3STetsuo Handa 				acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE_ACL);
897ca0b7df3STetsuo Handa 			else if (!(acl->perm &
898ca0b7df3STetsuo Handa 				   (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
899ca0b7df3STetsuo Handa 				acl->perm &= ~rw_mask;
900ca0b7df3STetsuo Handa 		} else {
901937bf613STetsuo Handa 			if (perm <= 0xFFFF)
902b69a54eeSKentaro Takeda 				acl->perm |= perm;
903937bf613STetsuo Handa 			else
904937bf613STetsuo Handa 				acl->perm_high |= (perm >> 16);
905b69a54eeSKentaro Takeda 			if ((acl->perm & rw_mask) == rw_mask)
906b69a54eeSKentaro Takeda 				acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE_ACL;
907b69a54eeSKentaro Takeda 			else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
908b69a54eeSKentaro Takeda 				acl->perm |= rw_mask;
909b69a54eeSKentaro Takeda 		}
910b69a54eeSKentaro Takeda 		error = 0;
911b69a54eeSKentaro Takeda 		break;
912b69a54eeSKentaro Takeda 	}
913ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
914ca0b7df3STetsuo Handa 		entry->head.type = TOMOYO_TYPE_SINGLE_PATH_ACL;
915ca0b7df3STetsuo Handa 		if (perm <= 0xFFFF)
916ca0b7df3STetsuo Handa 			entry->perm = perm;
917ca0b7df3STetsuo Handa 		else
918ca0b7df3STetsuo Handa 			entry->perm_high = (perm >> 16);
919ca0b7df3STetsuo Handa 		if (perm == (1 << TOMOYO_TYPE_READ_WRITE_ACL))
920ca0b7df3STetsuo Handa 			entry->perm |= rw_mask;
921ca0b7df3STetsuo Handa 		entry->filename = saved_filename;
922bf24fb01STetsuo Handa 		saved_filename = NULL;
923ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
924ca0b7df3STetsuo Handa 		entry = NULL;
925ca0b7df3STetsuo Handa 		error = 0;
926ca0b7df3STetsuo Handa 	}
927f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
928ca0b7df3STetsuo Handa 	kfree(entry);
929bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename);
930b69a54eeSKentaro Takeda 	return error;
931b69a54eeSKentaro Takeda }
932b69a54eeSKentaro Takeda 
933b69a54eeSKentaro Takeda /**
934b69a54eeSKentaro Takeda  * tomoyo_update_double_path_acl - Update "struct tomoyo_double_path_acl_record" list.
935b69a54eeSKentaro Takeda  *
936b69a54eeSKentaro Takeda  * @type:      Type of operation.
937b69a54eeSKentaro Takeda  * @filename1: First filename.
938b69a54eeSKentaro Takeda  * @filename2: Second filename.
939b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
940b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
941b69a54eeSKentaro Takeda  *
942b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
943fdb8ebb7STetsuo Handa  *
944fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
945b69a54eeSKentaro Takeda  */
946b69a54eeSKentaro Takeda static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
947b69a54eeSKentaro Takeda 					 const char *filename2,
948b69a54eeSKentaro Takeda 					 struct tomoyo_domain_info *
949b69a54eeSKentaro Takeda 					 const domain, const bool is_delete)
950b69a54eeSKentaro Takeda {
951b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename1;
952b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename2;
953b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
954ca0b7df3STetsuo Handa 	struct tomoyo_double_path_acl_record *entry = NULL;
955ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
956b69a54eeSKentaro Takeda 	const u8 perm = 1 << type;
957b69a54eeSKentaro Takeda 
958b69a54eeSKentaro Takeda 	if (!domain)
959b69a54eeSKentaro Takeda 		return -EINVAL;
960b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) ||
961b69a54eeSKentaro Takeda 	    !tomoyo_is_correct_path(filename2, 0, 0, 0, __func__))
962b69a54eeSKentaro Takeda 		return -EINVAL;
963bf24fb01STetsuo Handa 	saved_filename1 = tomoyo_get_name(filename1);
964bf24fb01STetsuo Handa 	saved_filename2 = tomoyo_get_name(filename2);
965b69a54eeSKentaro Takeda 	if (!saved_filename1 || !saved_filename2)
966ca0b7df3STetsuo Handa 		goto out;
967ca0b7df3STetsuo Handa 	if (!is_delete)
968ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
969f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
970ca0b7df3STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
971ca0b7df3STetsuo Handa 		struct tomoyo_double_path_acl_record *acl =
972ca0b7df3STetsuo Handa 			container_of(ptr, struct tomoyo_double_path_acl_record,
973ca0b7df3STetsuo Handa 				     head);
974ca0b7df3STetsuo Handa 		if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL)
975ca0b7df3STetsuo Handa 			continue;
976ca0b7df3STetsuo Handa 		if (acl->filename1 != saved_filename1 ||
977ca0b7df3STetsuo Handa 		    acl->filename2 != saved_filename2)
978ca0b7df3STetsuo Handa 			continue;
979b69a54eeSKentaro Takeda 		if (is_delete)
980b69a54eeSKentaro Takeda 			acl->perm &= ~perm;
981ca0b7df3STetsuo Handa 		else
982ca0b7df3STetsuo Handa 			acl->perm |= perm;
983b69a54eeSKentaro Takeda 		error = 0;
984b69a54eeSKentaro Takeda 		break;
985b69a54eeSKentaro Takeda 	}
986ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
987ca0b7df3STetsuo Handa 		entry->head.type = TOMOYO_TYPE_DOUBLE_PATH_ACL;
988ca0b7df3STetsuo Handa 		entry->perm = perm;
989ca0b7df3STetsuo Handa 		entry->filename1 = saved_filename1;
990bf24fb01STetsuo Handa 		saved_filename1 = NULL;
991ca0b7df3STetsuo Handa 		entry->filename2 = saved_filename2;
992bf24fb01STetsuo Handa 		saved_filename2 = NULL;
993ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
994ca0b7df3STetsuo Handa 		entry = NULL;
995ca0b7df3STetsuo Handa 		error = 0;
996ca0b7df3STetsuo Handa 	}
997f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
998ca0b7df3STetsuo Handa  out:
999bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename1);
1000bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename2);
1001ca0b7df3STetsuo Handa 	kfree(entry);
1002b69a54eeSKentaro Takeda 	return error;
1003b69a54eeSKentaro Takeda }
1004b69a54eeSKentaro Takeda 
1005b69a54eeSKentaro Takeda /**
1006b69a54eeSKentaro Takeda  * tomoyo_check_single_path_acl - Check permission for single path operation.
1007b69a54eeSKentaro Takeda  *
1008b69a54eeSKentaro Takeda  * @domain:   Pointer to "struct tomoyo_domain_info".
1009b69a54eeSKentaro Takeda  * @type:     Type of operation.
1010b69a54eeSKentaro Takeda  * @filename: Filename to check.
1011b69a54eeSKentaro Takeda  *
1012b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1013fdb8ebb7STetsuo Handa  *
1014fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1015b69a54eeSKentaro Takeda  */
1016b69a54eeSKentaro Takeda static int tomoyo_check_single_path_acl(struct tomoyo_domain_info *domain,
1017b69a54eeSKentaro Takeda 					const u8 type,
1018b69a54eeSKentaro Takeda 					const struct tomoyo_path_info *filename)
1019b69a54eeSKentaro Takeda {
1020b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
1021b69a54eeSKentaro Takeda 		return 0;
1022b69a54eeSKentaro Takeda 	return tomoyo_check_single_path_acl2(domain, filename, 1 << type, 1);
1023b69a54eeSKentaro Takeda }
1024b69a54eeSKentaro Takeda 
1025b69a54eeSKentaro Takeda /**
1026b69a54eeSKentaro Takeda  * tomoyo_check_double_path_acl - Check permission for double path operation.
1027b69a54eeSKentaro Takeda  *
1028b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1029b69a54eeSKentaro Takeda  * @type:      Type of operation.
1030b69a54eeSKentaro Takeda  * @filename1: First filename to check.
1031b69a54eeSKentaro Takeda  * @filename2: Second filename to check.
1032b69a54eeSKentaro Takeda  *
1033b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
1034fdb8ebb7STetsuo Handa  *
1035fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1036b69a54eeSKentaro Takeda  */
1037b69a54eeSKentaro Takeda static int tomoyo_check_double_path_acl(const struct tomoyo_domain_info *domain,
1038b69a54eeSKentaro Takeda 					const u8 type,
1039b69a54eeSKentaro Takeda 					const struct tomoyo_path_info *
1040b69a54eeSKentaro Takeda 					filename1,
1041b69a54eeSKentaro Takeda 					const struct tomoyo_path_info *
1042b69a54eeSKentaro Takeda 					filename2)
1043b69a54eeSKentaro Takeda {
1044b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
1045b69a54eeSKentaro Takeda 	const u8 perm = 1 << type;
1046b69a54eeSKentaro Takeda 	int error = -EPERM;
1047b69a54eeSKentaro Takeda 
1048b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
1049b69a54eeSKentaro Takeda 		return 0;
1050fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1051b69a54eeSKentaro Takeda 		struct tomoyo_double_path_acl_record *acl;
1052ea13ddbaSTetsuo Handa 		if (ptr->type != TOMOYO_TYPE_DOUBLE_PATH_ACL)
1053b69a54eeSKentaro Takeda 			continue;
1054b69a54eeSKentaro Takeda 		acl = container_of(ptr, struct tomoyo_double_path_acl_record,
1055b69a54eeSKentaro Takeda 				   head);
1056b69a54eeSKentaro Takeda 		if (!(acl->perm & perm))
1057b69a54eeSKentaro Takeda 			continue;
1058b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename1, acl->filename1))
1059b69a54eeSKentaro Takeda 			continue;
1060b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename2, acl->filename2))
1061b69a54eeSKentaro Takeda 			continue;
1062b69a54eeSKentaro Takeda 		error = 0;
1063b69a54eeSKentaro Takeda 		break;
1064b69a54eeSKentaro Takeda 	}
1065b69a54eeSKentaro Takeda 	return error;
1066b69a54eeSKentaro Takeda }
1067b69a54eeSKentaro Takeda 
1068b69a54eeSKentaro Takeda /**
1069b69a54eeSKentaro Takeda  * tomoyo_check_single_path_permission2 - Check permission for single path operation.
1070b69a54eeSKentaro Takeda  *
1071b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1072b69a54eeSKentaro Takeda  * @operation: Type of operation.
1073b69a54eeSKentaro Takeda  * @filename:  Filename to check.
1074b69a54eeSKentaro Takeda  * @mode:      Access control mode.
1075b69a54eeSKentaro Takeda  *
1076b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1077fdb8ebb7STetsuo Handa  *
1078fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1079b69a54eeSKentaro Takeda  */
1080b69a54eeSKentaro Takeda static int tomoyo_check_single_path_permission2(struct tomoyo_domain_info *
1081b69a54eeSKentaro Takeda 						const domain, u8 operation,
1082b69a54eeSKentaro Takeda 						const struct tomoyo_path_info *
1083b69a54eeSKentaro Takeda 						filename, const u8 mode)
1084b69a54eeSKentaro Takeda {
1085b69a54eeSKentaro Takeda 	const char *msg;
1086b69a54eeSKentaro Takeda 	int error;
1087b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1088b69a54eeSKentaro Takeda 
1089b69a54eeSKentaro Takeda 	if (!mode)
1090b69a54eeSKentaro Takeda 		return 0;
1091b69a54eeSKentaro Takeda  next:
1092b69a54eeSKentaro Takeda 	error = tomoyo_check_single_path_acl(domain, operation, filename);
1093b69a54eeSKentaro Takeda 	msg = tomoyo_sp2keyword(operation);
1094b69a54eeSKentaro Takeda 	if (!error)
1095b69a54eeSKentaro Takeda 		goto ok;
1096b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
1097b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n",
1098b69a54eeSKentaro Takeda 		       tomoyo_get_msg(is_enforce), msg, filename->name,
1099b69a54eeSKentaro Takeda 		       tomoyo_get_last_name(domain));
1100b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1101b69a54eeSKentaro Takeda 		const char *name = tomoyo_get_file_pattern(filename)->name;
1102b69a54eeSKentaro Takeda 		tomoyo_update_single_path_acl(operation, name, domain, false);
1103b69a54eeSKentaro Takeda 	}
1104b69a54eeSKentaro Takeda 	if (!is_enforce)
1105b69a54eeSKentaro Takeda 		error = 0;
1106b69a54eeSKentaro Takeda  ok:
1107b69a54eeSKentaro Takeda 	/*
1108b69a54eeSKentaro Takeda 	 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1109b69a54eeSKentaro Takeda 	 * we need to check "allow_rewrite" permission if the filename is
1110b69a54eeSKentaro Takeda 	 * specified by "deny_rewrite" keyword.
1111b69a54eeSKentaro Takeda 	 */
1112b69a54eeSKentaro Takeda 	if (!error && operation == TOMOYO_TYPE_TRUNCATE_ACL &&
1113b69a54eeSKentaro Takeda 	    tomoyo_is_no_rewrite_file(filename)) {
1114b69a54eeSKentaro Takeda 		operation = TOMOYO_TYPE_REWRITE_ACL;
1115b69a54eeSKentaro Takeda 		goto next;
1116b69a54eeSKentaro Takeda 	}
1117b69a54eeSKentaro Takeda 	return error;
1118b69a54eeSKentaro Takeda }
1119b69a54eeSKentaro Takeda 
1120b69a54eeSKentaro Takeda /**
1121b69a54eeSKentaro Takeda  * tomoyo_check_exec_perm - Check permission for "execute".
1122b69a54eeSKentaro Takeda  *
1123b69a54eeSKentaro Takeda  * @domain:   Pointer to "struct tomoyo_domain_info".
1124b69a54eeSKentaro Takeda  * @filename: Check permission for "execute".
1125b69a54eeSKentaro Takeda  *
1126b69a54eeSKentaro Takeda  * Returns 0 on success, negativevalue otherwise.
1127fdb8ebb7STetsuo Handa  *
1128fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1129b69a54eeSKentaro Takeda  */
1130b69a54eeSKentaro Takeda int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
1131bcb86975STetsuo Handa 			   const struct tomoyo_path_info *filename)
1132b69a54eeSKentaro Takeda {
1133b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1134b69a54eeSKentaro Takeda 
1135b69a54eeSKentaro Takeda 	if (!mode)
1136b69a54eeSKentaro Takeda 		return 0;
1137b69a54eeSKentaro Takeda 	return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode);
1138b69a54eeSKentaro Takeda }
1139b69a54eeSKentaro Takeda 
1140b69a54eeSKentaro Takeda /**
1141b69a54eeSKentaro Takeda  * tomoyo_check_open_permission - Check permission for "read" and "write".
1142b69a54eeSKentaro Takeda  *
1143b69a54eeSKentaro Takeda  * @domain: Pointer to "struct tomoyo_domain_info".
1144b69a54eeSKentaro Takeda  * @path:   Pointer to "struct path".
1145b69a54eeSKentaro Takeda  * @flag:   Flags for open().
1146b69a54eeSKentaro Takeda  *
1147b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1148b69a54eeSKentaro Takeda  */
1149b69a54eeSKentaro Takeda int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1150b69a54eeSKentaro Takeda 				 struct path *path, const int flag)
1151b69a54eeSKentaro Takeda {
1152b69a54eeSKentaro Takeda 	const u8 acc_mode = ACC_MODE(flag);
1153b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1154b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1155b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1156b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1157fdb8ebb7STetsuo Handa 	int idx;
1158b69a54eeSKentaro Takeda 
1159b69a54eeSKentaro Takeda 	if (!mode || !path->mnt)
1160b69a54eeSKentaro Takeda 		return 0;
1161b69a54eeSKentaro Takeda 	if (acc_mode == 0)
1162b69a54eeSKentaro Takeda 		return 0;
1163b69a54eeSKentaro Takeda 	if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1164b69a54eeSKentaro Takeda 		/*
1165b69a54eeSKentaro Takeda 		 * I don't check directories here because mkdir() and rmdir()
1166b69a54eeSKentaro Takeda 		 * don't call me.
1167b69a54eeSKentaro Takeda 		 */
1168b69a54eeSKentaro Takeda 		return 0;
1169fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1170b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(path);
1171b69a54eeSKentaro Takeda 	if (!buf)
1172b69a54eeSKentaro Takeda 		goto out;
1173b69a54eeSKentaro Takeda 	error = 0;
1174b69a54eeSKentaro Takeda 	/*
1175b69a54eeSKentaro Takeda 	 * If the filename is specified by "deny_rewrite" keyword,
1176b69a54eeSKentaro Takeda 	 * we need to check "allow_rewrite" permission when the filename is not
1177b69a54eeSKentaro Takeda 	 * opened for append mode or the filename is truncated at open time.
1178b69a54eeSKentaro Takeda 	 */
1179b69a54eeSKentaro Takeda 	if ((acc_mode & MAY_WRITE) &&
1180b69a54eeSKentaro Takeda 	    ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1181b69a54eeSKentaro Takeda 	    (tomoyo_is_no_rewrite_file(buf))) {
1182b69a54eeSKentaro Takeda 		error = tomoyo_check_single_path_permission2(domain,
1183b69a54eeSKentaro Takeda 						     TOMOYO_TYPE_REWRITE_ACL,
1184b69a54eeSKentaro Takeda 							     buf, mode);
1185b69a54eeSKentaro Takeda 	}
1186b69a54eeSKentaro Takeda 	if (!error)
1187b69a54eeSKentaro Takeda 		error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
1188b69a54eeSKentaro Takeda 						mode);
1189b69a54eeSKentaro Takeda 	if (!error && (flag & O_TRUNC))
1190b69a54eeSKentaro Takeda 		error = tomoyo_check_single_path_permission2(domain,
1191b69a54eeSKentaro Takeda 						     TOMOYO_TYPE_TRUNCATE_ACL,
1192b69a54eeSKentaro Takeda 							     buf, mode);
1193b69a54eeSKentaro Takeda  out:
11948e2d39a1STetsuo Handa 	kfree(buf);
1195fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1196b69a54eeSKentaro Takeda 	if (!is_enforce)
1197b69a54eeSKentaro Takeda 		error = 0;
1198b69a54eeSKentaro Takeda 	return error;
1199b69a54eeSKentaro Takeda }
1200b69a54eeSKentaro Takeda 
1201b69a54eeSKentaro Takeda /**
1202937bf613STetsuo Handa  * tomoyo_check_1path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
1203b69a54eeSKentaro Takeda  *
1204b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1205b69a54eeSKentaro Takeda  * @operation: Type of operation.
1206b69a54eeSKentaro Takeda  * @path:      Pointer to "struct path".
1207b69a54eeSKentaro Takeda  *
1208b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1209b69a54eeSKentaro Takeda  */
1210b69a54eeSKentaro Takeda int tomoyo_check_1path_perm(struct tomoyo_domain_info *domain,
1211b69a54eeSKentaro Takeda 			    const u8 operation, struct path *path)
1212b69a54eeSKentaro Takeda {
1213b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1214b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1215b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1216b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1217fdb8ebb7STetsuo Handa 	int idx;
1218b69a54eeSKentaro Takeda 
1219b69a54eeSKentaro Takeda 	if (!mode || !path->mnt)
1220b69a54eeSKentaro Takeda 		return 0;
1221fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1222b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(path);
1223b69a54eeSKentaro Takeda 	if (!buf)
1224b69a54eeSKentaro Takeda 		goto out;
1225b69a54eeSKentaro Takeda 	switch (operation) {
1226b69a54eeSKentaro Takeda 	case TOMOYO_TYPE_MKDIR_ACL:
1227b69a54eeSKentaro Takeda 	case TOMOYO_TYPE_RMDIR_ACL:
1228937bf613STetsuo Handa 	case TOMOYO_TYPE_CHROOT_ACL:
1229b69a54eeSKentaro Takeda 		if (!buf->is_dir) {
1230b69a54eeSKentaro Takeda 			/*
1231b69a54eeSKentaro Takeda 			 * tomoyo_get_path() reserves space for appending "/."
1232b69a54eeSKentaro Takeda 			 */
1233b69a54eeSKentaro Takeda 			strcat((char *) buf->name, "/");
1234b69a54eeSKentaro Takeda 			tomoyo_fill_path_info(buf);
1235b69a54eeSKentaro Takeda 		}
1236b69a54eeSKentaro Takeda 	}
1237b69a54eeSKentaro Takeda 	error = tomoyo_check_single_path_permission2(domain, operation, buf,
1238b69a54eeSKentaro Takeda 						     mode);
1239b69a54eeSKentaro Takeda  out:
12408e2d39a1STetsuo Handa 	kfree(buf);
1241fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1242b69a54eeSKentaro Takeda 	if (!is_enforce)
1243b69a54eeSKentaro Takeda 		error = 0;
1244b69a54eeSKentaro Takeda 	return error;
1245b69a54eeSKentaro Takeda }
1246b69a54eeSKentaro Takeda 
1247b69a54eeSKentaro Takeda /**
1248b69a54eeSKentaro Takeda  * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1249b69a54eeSKentaro Takeda  *
1250b69a54eeSKentaro Takeda  * @domain: Pointer to "struct tomoyo_domain_info".
1251b69a54eeSKentaro Takeda  * @filp: Pointer to "struct file".
1252b69a54eeSKentaro Takeda  *
1253b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1254b69a54eeSKentaro Takeda  */
1255b69a54eeSKentaro Takeda int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain,
1256b69a54eeSKentaro Takeda 				    struct file *filp)
1257b69a54eeSKentaro Takeda {
1258b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1259b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1260b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1261b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1262fdb8ebb7STetsuo Handa 	int idx;
1263b69a54eeSKentaro Takeda 
1264b69a54eeSKentaro Takeda 	if (!mode || !filp->f_path.mnt)
1265b69a54eeSKentaro Takeda 		return 0;
1266fdb8ebb7STetsuo Handa 
1267fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1268b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(&filp->f_path);
1269b69a54eeSKentaro Takeda 	if (!buf)
1270b69a54eeSKentaro Takeda 		goto out;
1271b69a54eeSKentaro Takeda 	if (!tomoyo_is_no_rewrite_file(buf)) {
1272b69a54eeSKentaro Takeda 		error = 0;
1273b69a54eeSKentaro Takeda 		goto out;
1274b69a54eeSKentaro Takeda 	}
1275b69a54eeSKentaro Takeda 	error = tomoyo_check_single_path_permission2(domain,
1276b69a54eeSKentaro Takeda 						     TOMOYO_TYPE_REWRITE_ACL,
1277b69a54eeSKentaro Takeda 						     buf, mode);
1278b69a54eeSKentaro Takeda  out:
12798e2d39a1STetsuo Handa 	kfree(buf);
1280fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1281b69a54eeSKentaro Takeda 	if (!is_enforce)
1282b69a54eeSKentaro Takeda 		error = 0;
1283b69a54eeSKentaro Takeda 	return error;
1284b69a54eeSKentaro Takeda }
1285b69a54eeSKentaro Takeda 
1286b69a54eeSKentaro Takeda /**
1287937bf613STetsuo Handa  * tomoyo_check_2path_perm - Check permission for "rename", "link" and "pivot_root".
1288b69a54eeSKentaro Takeda  *
1289b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1290b69a54eeSKentaro Takeda  * @operation: Type of operation.
1291b69a54eeSKentaro Takeda  * @path1:      Pointer to "struct path".
1292b69a54eeSKentaro Takeda  * @path2:      Pointer to "struct path".
1293b69a54eeSKentaro Takeda  *
1294b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1295b69a54eeSKentaro Takeda  */
1296b69a54eeSKentaro Takeda int tomoyo_check_2path_perm(struct tomoyo_domain_info * const domain,
1297b69a54eeSKentaro Takeda 			    const u8 operation, struct path *path1,
1298b69a54eeSKentaro Takeda 			    struct path *path2)
1299b69a54eeSKentaro Takeda {
1300b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1301b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf1, *buf2;
1302b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1303b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1304b69a54eeSKentaro Takeda 	const char *msg;
1305fdb8ebb7STetsuo Handa 	int idx;
1306b69a54eeSKentaro Takeda 
1307b69a54eeSKentaro Takeda 	if (!mode || !path1->mnt || !path2->mnt)
1308b69a54eeSKentaro Takeda 		return 0;
1309fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1310b69a54eeSKentaro Takeda 	buf1 = tomoyo_get_path(path1);
1311b69a54eeSKentaro Takeda 	buf2 = tomoyo_get_path(path2);
1312b69a54eeSKentaro Takeda 	if (!buf1 || !buf2)
1313b69a54eeSKentaro Takeda 		goto out;
1314b69a54eeSKentaro Takeda 	{
1315b69a54eeSKentaro Takeda 		struct dentry *dentry = path1->dentry;
1316b69a54eeSKentaro Takeda 		if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1317b69a54eeSKentaro Takeda 			/*
1318b69a54eeSKentaro Takeda 			 * tomoyo_get_path() reserves space for appending "/."
1319b69a54eeSKentaro Takeda 			 */
1320b69a54eeSKentaro Takeda 			if (!buf1->is_dir) {
1321b69a54eeSKentaro Takeda 				strcat((char *) buf1->name, "/");
1322b69a54eeSKentaro Takeda 				tomoyo_fill_path_info(buf1);
1323b69a54eeSKentaro Takeda 			}
1324b69a54eeSKentaro Takeda 			if (!buf2->is_dir) {
1325b69a54eeSKentaro Takeda 				strcat((char *) buf2->name, "/");
1326b69a54eeSKentaro Takeda 				tomoyo_fill_path_info(buf2);
1327b69a54eeSKentaro Takeda 			}
1328b69a54eeSKentaro Takeda 		}
1329b69a54eeSKentaro Takeda 	}
1330b69a54eeSKentaro Takeda 	error = tomoyo_check_double_path_acl(domain, operation, buf1, buf2);
1331b69a54eeSKentaro Takeda 	msg = tomoyo_dp2keyword(operation);
1332b69a54eeSKentaro Takeda 	if (!error)
1333b69a54eeSKentaro Takeda 		goto out;
1334b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
1335b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' "
1336b69a54eeSKentaro Takeda 		       "denied for %s\n", tomoyo_get_msg(is_enforce),
1337b69a54eeSKentaro Takeda 		       msg, buf1->name, buf2->name,
1338b69a54eeSKentaro Takeda 		       tomoyo_get_last_name(domain));
1339b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1340b69a54eeSKentaro Takeda 		const char *name1 = tomoyo_get_file_pattern(buf1)->name;
1341b69a54eeSKentaro Takeda 		const char *name2 = tomoyo_get_file_pattern(buf2)->name;
1342b69a54eeSKentaro Takeda 		tomoyo_update_double_path_acl(operation, name1, name2, domain,
1343b69a54eeSKentaro Takeda 					      false);
1344b69a54eeSKentaro Takeda 	}
1345b69a54eeSKentaro Takeda  out:
13468e2d39a1STetsuo Handa 	kfree(buf1);
13478e2d39a1STetsuo Handa 	kfree(buf2);
1348fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1349b69a54eeSKentaro Takeda 	if (!is_enforce)
1350b69a54eeSKentaro Takeda 		error = 0;
1351b69a54eeSKentaro Takeda 	return error;
1352b69a54eeSKentaro Takeda }
1353