xref: /openbmc/linux/security/tomoyo/file.c (revision 7ef61233)
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 
14b69a54eeSKentaro Takeda /* Keyword array for single path operations. */
157ef61233STetsuo Handa static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
167ef61233STetsuo Handa 	[TOMOYO_TYPE_READ_WRITE] = "read/write",
177ef61233STetsuo Handa 	[TOMOYO_TYPE_EXECUTE]    = "execute",
187ef61233STetsuo Handa 	[TOMOYO_TYPE_READ]       = "read",
197ef61233STetsuo Handa 	[TOMOYO_TYPE_WRITE]      = "write",
207ef61233STetsuo Handa 	[TOMOYO_TYPE_CREATE]     = "create",
217ef61233STetsuo Handa 	[TOMOYO_TYPE_UNLINK]     = "unlink",
227ef61233STetsuo Handa 	[TOMOYO_TYPE_MKDIR]      = "mkdir",
237ef61233STetsuo Handa 	[TOMOYO_TYPE_RMDIR]      = "rmdir",
247ef61233STetsuo Handa 	[TOMOYO_TYPE_MKFIFO]     = "mkfifo",
257ef61233STetsuo Handa 	[TOMOYO_TYPE_MKSOCK]     = "mksock",
267ef61233STetsuo Handa 	[TOMOYO_TYPE_MKBLOCK]    = "mkblock",
277ef61233STetsuo Handa 	[TOMOYO_TYPE_MKCHAR]     = "mkchar",
287ef61233STetsuo Handa 	[TOMOYO_TYPE_TRUNCATE]   = "truncate",
297ef61233STetsuo Handa 	[TOMOYO_TYPE_SYMLINK]    = "symlink",
307ef61233STetsuo Handa 	[TOMOYO_TYPE_REWRITE]    = "rewrite",
317ef61233STetsuo Handa 	[TOMOYO_TYPE_IOCTL]      = "ioctl",
327ef61233STetsuo Handa 	[TOMOYO_TYPE_CHMOD]      = "chmod",
337ef61233STetsuo Handa 	[TOMOYO_TYPE_CHOWN]      = "chown",
347ef61233STetsuo Handa 	[TOMOYO_TYPE_CHGRP]      = "chgrp",
357ef61233STetsuo Handa 	[TOMOYO_TYPE_CHROOT]     = "chroot",
367ef61233STetsuo Handa 	[TOMOYO_TYPE_MOUNT]      = "mount",
377ef61233STetsuo Handa 	[TOMOYO_TYPE_UMOUNT]     = "unmount",
38b69a54eeSKentaro Takeda };
39b69a54eeSKentaro Takeda 
40b69a54eeSKentaro Takeda /* Keyword array for double path operations. */
417ef61233STetsuo Handa static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
427ef61233STetsuo Handa 	[TOMOYO_TYPE_LINK]    = "link",
437ef61233STetsuo Handa 	[TOMOYO_TYPE_RENAME]  = "rename",
447ef61233STetsuo Handa 	[TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
45b69a54eeSKentaro Takeda };
46b69a54eeSKentaro Takeda 
47b69a54eeSKentaro Takeda /**
487ef61233STetsuo Handa  * tomoyo_path2keyword - Get the name of single path operation.
49b69a54eeSKentaro Takeda  *
50b69a54eeSKentaro Takeda  * @operation: Type of operation.
51b69a54eeSKentaro Takeda  *
52b69a54eeSKentaro Takeda  * Returns the name of single path operation.
53b69a54eeSKentaro Takeda  */
547ef61233STetsuo Handa const char *tomoyo_path2keyword(const u8 operation)
55b69a54eeSKentaro Takeda {
567ef61233STetsuo Handa 	return (operation < TOMOYO_MAX_PATH_OPERATION)
577ef61233STetsuo Handa 		? tomoyo_path_keyword[operation] : NULL;
58b69a54eeSKentaro Takeda }
59b69a54eeSKentaro Takeda 
60b69a54eeSKentaro Takeda /**
617ef61233STetsuo Handa  * tomoyo_path22keyword - Get the name of double path operation.
62b69a54eeSKentaro Takeda  *
63b69a54eeSKentaro Takeda  * @operation: Type of operation.
64b69a54eeSKentaro Takeda  *
65b69a54eeSKentaro Takeda  * Returns the name of double path operation.
66b69a54eeSKentaro Takeda  */
677ef61233STetsuo Handa const char *tomoyo_path22keyword(const u8 operation)
68b69a54eeSKentaro Takeda {
697ef61233STetsuo Handa 	return (operation < TOMOYO_MAX_PATH2_OPERATION)
707ef61233STetsuo Handa 		? tomoyo_path2_keyword[operation] : NULL;
71b69a54eeSKentaro Takeda }
72b69a54eeSKentaro Takeda 
73b69a54eeSKentaro Takeda /**
74b69a54eeSKentaro Takeda  * tomoyo_strendswith - Check whether the token ends with the given token.
75b69a54eeSKentaro Takeda  *
76b69a54eeSKentaro Takeda  * @name: The token to check.
77b69a54eeSKentaro Takeda  * @tail: The token to find.
78b69a54eeSKentaro Takeda  *
79b69a54eeSKentaro Takeda  * Returns true if @name ends with @tail, false otherwise.
80b69a54eeSKentaro Takeda  */
81b69a54eeSKentaro Takeda static bool tomoyo_strendswith(const char *name, const char *tail)
82b69a54eeSKentaro Takeda {
83b69a54eeSKentaro Takeda 	int len;
84b69a54eeSKentaro Takeda 
85b69a54eeSKentaro Takeda 	if (!name || !tail)
86b69a54eeSKentaro Takeda 		return false;
87b69a54eeSKentaro Takeda 	len = strlen(name) - strlen(tail);
88b69a54eeSKentaro Takeda 	return len >= 0 && !strcmp(name + len, tail);
89b69a54eeSKentaro Takeda }
90b69a54eeSKentaro Takeda 
91b69a54eeSKentaro Takeda /**
92b69a54eeSKentaro Takeda  * tomoyo_get_path - Get realpath.
93b69a54eeSKentaro Takeda  *
94b69a54eeSKentaro Takeda  * @path: Pointer to "struct path".
95b69a54eeSKentaro Takeda  *
96b69a54eeSKentaro Takeda  * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
97b69a54eeSKentaro Takeda  */
98b69a54eeSKentaro Takeda static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
99b69a54eeSKentaro Takeda {
100b69a54eeSKentaro Takeda 	int error;
1018e2d39a1STetsuo Handa 	struct tomoyo_path_info_with_data *buf = kzalloc(sizeof(*buf),
1028e2d39a1STetsuo Handa 							 GFP_KERNEL);
103b69a54eeSKentaro Takeda 
104b69a54eeSKentaro Takeda 	if (!buf)
105b69a54eeSKentaro Takeda 		return NULL;
106b69a54eeSKentaro Takeda 	/* Reserve one byte for appending "/". */
107b69a54eeSKentaro Takeda 	error = tomoyo_realpath_from_path2(path, buf->body,
108b69a54eeSKentaro Takeda 					   sizeof(buf->body) - 2);
109b69a54eeSKentaro Takeda 	if (!error) {
110b69a54eeSKentaro Takeda 		buf->head.name = buf->body;
111b69a54eeSKentaro Takeda 		tomoyo_fill_path_info(&buf->head);
112b69a54eeSKentaro Takeda 		return &buf->head;
113b69a54eeSKentaro Takeda 	}
1148e2d39a1STetsuo Handa 	kfree(buf);
115b69a54eeSKentaro Takeda 	return NULL;
116b69a54eeSKentaro Takeda }
117b69a54eeSKentaro Takeda 
1187ef61233STetsuo Handa static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
119b69a54eeSKentaro Takeda 				   const char *filename2,
1207ef61233STetsuo Handa 				   struct tomoyo_domain_info *const domain,
1217ef61233STetsuo Handa 				   const bool is_delete);
1227ef61233STetsuo Handa static int tomoyo_update_path_acl(const u8 type, const char *filename,
1237ef61233STetsuo Handa 				  struct tomoyo_domain_info *const domain,
1247ef61233STetsuo Handa 				  const bool is_delete);
125b69a54eeSKentaro Takeda 
126c3fa109aSTetsuo Handa /*
127c3fa109aSTetsuo Handa  * tomoyo_globally_readable_list is used for holding list of pathnames which
128c3fa109aSTetsuo Handa  * are by default allowed to be open()ed for reading by any process.
129c3fa109aSTetsuo Handa  *
130c3fa109aSTetsuo Handa  * An entry is added by
131c3fa109aSTetsuo Handa  *
132c3fa109aSTetsuo Handa  * # echo 'allow_read /lib/libc-2.5.so' > \
133c3fa109aSTetsuo Handa  *                               /sys/kernel/security/tomoyo/exception_policy
134c3fa109aSTetsuo Handa  *
135c3fa109aSTetsuo Handa  * and is deleted by
136c3fa109aSTetsuo Handa  *
137c3fa109aSTetsuo Handa  * # echo 'delete allow_read /lib/libc-2.5.so' > \
138c3fa109aSTetsuo Handa  *                               /sys/kernel/security/tomoyo/exception_policy
139c3fa109aSTetsuo Handa  *
140c3fa109aSTetsuo Handa  * and all entries are retrieved by
141c3fa109aSTetsuo Handa  *
142c3fa109aSTetsuo Handa  * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
143c3fa109aSTetsuo Handa  *
144c3fa109aSTetsuo Handa  * In the example above, any process is allowed to
145c3fa109aSTetsuo Handa  * open("/lib/libc-2.5.so", O_RDONLY).
146c3fa109aSTetsuo Handa  * One exception is, if the domain which current process belongs to is marked
147c3fa109aSTetsuo Handa  * as "ignore_global_allow_read", current process can't do so unless explicitly
148c3fa109aSTetsuo Handa  * given "allow_read /lib/libc-2.5.so" to the domain which current process
149c3fa109aSTetsuo Handa  * belongs to.
150c3fa109aSTetsuo Handa  */
151847b173eSTetsuo Handa LIST_HEAD(tomoyo_globally_readable_list);
152b69a54eeSKentaro Takeda 
153b69a54eeSKentaro Takeda /**
154b69a54eeSKentaro Takeda  * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
155b69a54eeSKentaro Takeda  *
156b69a54eeSKentaro Takeda  * @filename:  Filename unconditionally permitted to open() for reading.
157b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
158b69a54eeSKentaro Takeda  *
159b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
160fdb8ebb7STetsuo Handa  *
161fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
162b69a54eeSKentaro Takeda  */
163b69a54eeSKentaro Takeda static int tomoyo_update_globally_readable_entry(const char *filename,
164b69a54eeSKentaro Takeda 						 const bool is_delete)
165b69a54eeSKentaro Takeda {
166ca0b7df3STetsuo Handa 	struct tomoyo_globally_readable_file_entry *entry = NULL;
167b69a54eeSKentaro Takeda 	struct tomoyo_globally_readable_file_entry *ptr;
168b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename;
169ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
170b69a54eeSKentaro Takeda 
171b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__))
172b69a54eeSKentaro Takeda 		return -EINVAL;
173bf24fb01STetsuo Handa 	saved_filename = tomoyo_get_name(filename);
174b69a54eeSKentaro Takeda 	if (!saved_filename)
175b69a54eeSKentaro Takeda 		return -ENOMEM;
176ca0b7df3STetsuo Handa 	if (!is_delete)
177ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
178f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
179fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
180b69a54eeSKentaro Takeda 		if (ptr->filename != saved_filename)
181b69a54eeSKentaro Takeda 			continue;
182b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
183b69a54eeSKentaro Takeda 		error = 0;
184ca0b7df3STetsuo Handa 		break;
185b69a54eeSKentaro Takeda 	}
186ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
187ca0b7df3STetsuo Handa 		entry->filename = saved_filename;
188bf24fb01STetsuo Handa 		saved_filename = NULL;
189ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_globally_readable_list);
190ca0b7df3STetsuo Handa 		entry = NULL;
191b69a54eeSKentaro Takeda 		error = 0;
192ca0b7df3STetsuo Handa 	}
193f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
194bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename);
195ca0b7df3STetsuo Handa 	kfree(entry);
196b69a54eeSKentaro Takeda 	return error;
197b69a54eeSKentaro Takeda }
198b69a54eeSKentaro Takeda 
199b69a54eeSKentaro Takeda /**
200b69a54eeSKentaro Takeda  * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
201b69a54eeSKentaro Takeda  *
202b69a54eeSKentaro Takeda  * @filename: The filename to check.
203b69a54eeSKentaro Takeda  *
204b69a54eeSKentaro Takeda  * Returns true if any domain can open @filename for reading, false otherwise.
205fdb8ebb7STetsuo Handa  *
206fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
207b69a54eeSKentaro Takeda  */
208b69a54eeSKentaro Takeda static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
209b69a54eeSKentaro Takeda 					     filename)
210b69a54eeSKentaro Takeda {
211b69a54eeSKentaro Takeda 	struct tomoyo_globally_readable_file_entry *ptr;
212b69a54eeSKentaro Takeda 	bool found = false;
213fdb8ebb7STetsuo Handa 
214fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
215b69a54eeSKentaro Takeda 		if (!ptr->is_deleted &&
216b69a54eeSKentaro Takeda 		    tomoyo_path_matches_pattern(filename, ptr->filename)) {
217b69a54eeSKentaro Takeda 			found = true;
218b69a54eeSKentaro Takeda 			break;
219b69a54eeSKentaro Takeda 		}
220b69a54eeSKentaro Takeda 	}
221b69a54eeSKentaro Takeda 	return found;
222b69a54eeSKentaro Takeda }
223b69a54eeSKentaro Takeda 
224b69a54eeSKentaro Takeda /**
225b69a54eeSKentaro Takeda  * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
226b69a54eeSKentaro Takeda  *
227b69a54eeSKentaro Takeda  * @data:      String to parse.
228b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
229b69a54eeSKentaro Takeda  *
230b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
231fdb8ebb7STetsuo Handa  *
232fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
233b69a54eeSKentaro Takeda  */
234b69a54eeSKentaro Takeda int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
235b69a54eeSKentaro Takeda {
236b69a54eeSKentaro Takeda 	return tomoyo_update_globally_readable_entry(data, is_delete);
237b69a54eeSKentaro Takeda }
238b69a54eeSKentaro Takeda 
239b69a54eeSKentaro Takeda /**
240b69a54eeSKentaro Takeda  * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
241b69a54eeSKentaro Takeda  *
242b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
243b69a54eeSKentaro Takeda  *
244b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
245fdb8ebb7STetsuo Handa  *
246fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
247b69a54eeSKentaro Takeda  */
248b69a54eeSKentaro Takeda bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
249b69a54eeSKentaro Takeda {
250b69a54eeSKentaro Takeda 	struct list_head *pos;
251b69a54eeSKentaro Takeda 	bool done = true;
252b69a54eeSKentaro Takeda 
253b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2,
254b69a54eeSKentaro Takeda 			     &tomoyo_globally_readable_list) {
255b69a54eeSKentaro Takeda 		struct tomoyo_globally_readable_file_entry *ptr;
256b69a54eeSKentaro Takeda 		ptr = list_entry(pos,
257b69a54eeSKentaro Takeda 				 struct tomoyo_globally_readable_file_entry,
258b69a54eeSKentaro Takeda 				 list);
259b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
260b69a54eeSKentaro Takeda 			continue;
2617d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
2627d2948b1STetsuo Handa 					ptr->filename->name);
2637d2948b1STetsuo Handa 		if (!done)
264b69a54eeSKentaro Takeda 			break;
265b69a54eeSKentaro Takeda 	}
266b69a54eeSKentaro Takeda 	return done;
267b69a54eeSKentaro Takeda }
268b69a54eeSKentaro Takeda 
269c3fa109aSTetsuo Handa /* tomoyo_pattern_list is used for holding list of pathnames which are used for
270c3fa109aSTetsuo Handa  * converting pathnames to pathname patterns during learning mode.
271c3fa109aSTetsuo Handa  *
272c3fa109aSTetsuo Handa  * An entry is added by
273c3fa109aSTetsuo Handa  *
274c3fa109aSTetsuo Handa  * # echo 'file_pattern /proc/\$/mounts' > \
275c3fa109aSTetsuo Handa  *                             /sys/kernel/security/tomoyo/exception_policy
276c3fa109aSTetsuo Handa  *
277c3fa109aSTetsuo Handa  * and is deleted by
278c3fa109aSTetsuo Handa  *
279c3fa109aSTetsuo Handa  * # echo 'delete file_pattern /proc/\$/mounts' > \
280c3fa109aSTetsuo Handa  *                             /sys/kernel/security/tomoyo/exception_policy
281c3fa109aSTetsuo Handa  *
282c3fa109aSTetsuo Handa  * and all entries are retrieved by
283c3fa109aSTetsuo Handa  *
284c3fa109aSTetsuo Handa  * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
285c3fa109aSTetsuo Handa  *
286c3fa109aSTetsuo Handa  * In the example above, if a process which belongs to a domain which is in
287c3fa109aSTetsuo Handa  * learning mode requested open("/proc/1/mounts", O_RDONLY),
288c3fa109aSTetsuo Handa  * "allow_read /proc/\$/mounts" is automatically added to the domain which that
289c3fa109aSTetsuo Handa  * process belongs to.
290c3fa109aSTetsuo Handa  *
291c3fa109aSTetsuo Handa  * It is not a desirable behavior that we have to use /proc/\$/ instead of
292c3fa109aSTetsuo Handa  * /proc/self/ when current process needs to access only current process's
293c3fa109aSTetsuo Handa  * information. As of now, LSM version of TOMOYO is using __d_path() for
294c3fa109aSTetsuo Handa  * calculating pathname. Non LSM version of TOMOYO is using its own function
295c3fa109aSTetsuo Handa  * which pretends as if /proc/self/ is not a symlink; so that we can forbid
296c3fa109aSTetsuo Handa  * current process from accessing other process's information.
297c3fa109aSTetsuo Handa  */
298847b173eSTetsuo Handa LIST_HEAD(tomoyo_pattern_list);
299b69a54eeSKentaro Takeda 
300b69a54eeSKentaro Takeda /**
301b69a54eeSKentaro Takeda  * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
302b69a54eeSKentaro Takeda  *
303b69a54eeSKentaro Takeda  * @pattern:   Pathname pattern.
304b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
305b69a54eeSKentaro Takeda  *
306b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
307fdb8ebb7STetsuo Handa  *
308fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
309b69a54eeSKentaro Takeda  */
310b69a54eeSKentaro Takeda static int tomoyo_update_file_pattern_entry(const char *pattern,
311b69a54eeSKentaro Takeda 					    const bool is_delete)
312b69a54eeSKentaro Takeda {
313ca0b7df3STetsuo Handa 	struct tomoyo_pattern_entry *entry = NULL;
314b69a54eeSKentaro Takeda 	struct tomoyo_pattern_entry *ptr;
315b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_pattern;
316ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
317b69a54eeSKentaro Takeda 
318bf24fb01STetsuo Handa 	saved_pattern = tomoyo_get_name(pattern);
319b69a54eeSKentaro Takeda 	if (!saved_pattern)
320ca0b7df3STetsuo Handa 		return error;
321ca0b7df3STetsuo Handa 	if (!saved_pattern->is_patterned)
322ca0b7df3STetsuo Handa 		goto out;
323ca0b7df3STetsuo Handa 	if (!is_delete)
324ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
325f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
326fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
327b69a54eeSKentaro Takeda 		if (saved_pattern != ptr->pattern)
328b69a54eeSKentaro Takeda 			continue;
329b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
330b69a54eeSKentaro Takeda 		error = 0;
331ca0b7df3STetsuo Handa 		break;
332b69a54eeSKentaro Takeda 	}
333ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
334ca0b7df3STetsuo Handa 		entry->pattern = saved_pattern;
335bf24fb01STetsuo Handa 		saved_pattern = NULL;
336ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
337ca0b7df3STetsuo Handa 		entry = NULL;
338b69a54eeSKentaro Takeda 		error = 0;
339ca0b7df3STetsuo Handa 	}
340f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
341ca0b7df3STetsuo Handa  out:
342ca0b7df3STetsuo Handa 	kfree(entry);
343bf24fb01STetsuo Handa 	tomoyo_put_name(saved_pattern);
344b69a54eeSKentaro Takeda 	return error;
345b69a54eeSKentaro Takeda }
346b69a54eeSKentaro Takeda 
347b69a54eeSKentaro Takeda /**
348b69a54eeSKentaro Takeda  * tomoyo_get_file_pattern - Get patterned pathname.
349b69a54eeSKentaro Takeda  *
350b69a54eeSKentaro Takeda  * @filename: The filename to find patterned pathname.
351b69a54eeSKentaro Takeda  *
352b69a54eeSKentaro Takeda  * Returns pointer to pathname pattern if matched, @filename otherwise.
353fdb8ebb7STetsuo Handa  *
354fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
355b69a54eeSKentaro Takeda  */
356b69a54eeSKentaro Takeda static const struct tomoyo_path_info *
357b69a54eeSKentaro Takeda tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
358b69a54eeSKentaro Takeda {
359b69a54eeSKentaro Takeda 	struct tomoyo_pattern_entry *ptr;
360b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *pattern = NULL;
361b69a54eeSKentaro Takeda 
362fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
363b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
364b69a54eeSKentaro Takeda 			continue;
365b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
366b69a54eeSKentaro Takeda 			continue;
367b69a54eeSKentaro Takeda 		pattern = ptr->pattern;
368b69a54eeSKentaro Takeda 		if (tomoyo_strendswith(pattern->name, "/\\*")) {
369b69a54eeSKentaro Takeda 			/* Do nothing. Try to find the better match. */
370b69a54eeSKentaro Takeda 		} else {
371b69a54eeSKentaro Takeda 			/* This would be the better match. Use this. */
372b69a54eeSKentaro Takeda 			break;
373b69a54eeSKentaro Takeda 		}
374b69a54eeSKentaro Takeda 	}
375b69a54eeSKentaro Takeda 	if (pattern)
376b69a54eeSKentaro Takeda 		filename = pattern;
377b69a54eeSKentaro Takeda 	return filename;
378b69a54eeSKentaro Takeda }
379b69a54eeSKentaro Takeda 
380b69a54eeSKentaro Takeda /**
381b69a54eeSKentaro Takeda  * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
382b69a54eeSKentaro Takeda  *
383b69a54eeSKentaro Takeda  * @data:      String to parse.
384b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
385b69a54eeSKentaro Takeda  *
386b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
387fdb8ebb7STetsuo Handa  *
388fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
389b69a54eeSKentaro Takeda  */
390b69a54eeSKentaro Takeda int tomoyo_write_pattern_policy(char *data, const bool is_delete)
391b69a54eeSKentaro Takeda {
392b69a54eeSKentaro Takeda 	return tomoyo_update_file_pattern_entry(data, is_delete);
393b69a54eeSKentaro Takeda }
394b69a54eeSKentaro Takeda 
395b69a54eeSKentaro Takeda /**
396b69a54eeSKentaro Takeda  * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
397b69a54eeSKentaro Takeda  *
398b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
399b69a54eeSKentaro Takeda  *
400b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
401fdb8ebb7STetsuo Handa  *
402fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
403b69a54eeSKentaro Takeda  */
404b69a54eeSKentaro Takeda bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
405b69a54eeSKentaro Takeda {
406b69a54eeSKentaro Takeda 	struct list_head *pos;
407b69a54eeSKentaro Takeda 	bool done = true;
408b69a54eeSKentaro Takeda 
409b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
410b69a54eeSKentaro Takeda 		struct tomoyo_pattern_entry *ptr;
411b69a54eeSKentaro Takeda 		ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
412b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
413b69a54eeSKentaro Takeda 			continue;
4147d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
4157d2948b1STetsuo Handa 					"%s\n", ptr->pattern->name);
4167d2948b1STetsuo Handa 		if (!done)
417b69a54eeSKentaro Takeda 			break;
418b69a54eeSKentaro Takeda 	}
419b69a54eeSKentaro Takeda 	return done;
420b69a54eeSKentaro Takeda }
421b69a54eeSKentaro Takeda 
422c3fa109aSTetsuo Handa /*
423c3fa109aSTetsuo Handa  * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
424c3fa109aSTetsuo Handa  * default forbidden to modify already written content of a file.
425c3fa109aSTetsuo Handa  *
426c3fa109aSTetsuo Handa  * An entry is added by
427c3fa109aSTetsuo Handa  *
428c3fa109aSTetsuo Handa  * # echo 'deny_rewrite /var/log/messages' > \
429c3fa109aSTetsuo Handa  *                              /sys/kernel/security/tomoyo/exception_policy
430c3fa109aSTetsuo Handa  *
431c3fa109aSTetsuo Handa  * and is deleted by
432c3fa109aSTetsuo Handa  *
433c3fa109aSTetsuo Handa  * # echo 'delete deny_rewrite /var/log/messages' > \
434c3fa109aSTetsuo Handa  *                              /sys/kernel/security/tomoyo/exception_policy
435c3fa109aSTetsuo Handa  *
436c3fa109aSTetsuo Handa  * and all entries are retrieved by
437c3fa109aSTetsuo Handa  *
438c3fa109aSTetsuo Handa  * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
439c3fa109aSTetsuo Handa  *
440c3fa109aSTetsuo Handa  * In the example above, if a process requested to rewrite /var/log/messages ,
441c3fa109aSTetsuo Handa  * the process can't rewrite unless the domain which that process belongs to
442c3fa109aSTetsuo Handa  * has "allow_rewrite /var/log/messages" entry.
443c3fa109aSTetsuo Handa  *
444c3fa109aSTetsuo Handa  * It is not a desirable behavior that we have to add "\040(deleted)" suffix
445c3fa109aSTetsuo Handa  * when we want to allow rewriting already unlink()ed file. As of now,
446c3fa109aSTetsuo Handa  * LSM version of TOMOYO is using __d_path() for calculating pathname.
447c3fa109aSTetsuo Handa  * Non LSM version of TOMOYO is using its own function which doesn't append
448c3fa109aSTetsuo Handa  * " (deleted)" suffix if the file is already unlink()ed; so that we don't
449c3fa109aSTetsuo Handa  * need to worry whether the file is already unlink()ed or not.
450c3fa109aSTetsuo Handa  */
451847b173eSTetsuo Handa LIST_HEAD(tomoyo_no_rewrite_list);
452b69a54eeSKentaro Takeda 
453b69a54eeSKentaro Takeda /**
454b69a54eeSKentaro Takeda  * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
455b69a54eeSKentaro Takeda  *
456b69a54eeSKentaro Takeda  * @pattern:   Pathname pattern that are not rewritable by default.
457b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
458b69a54eeSKentaro Takeda  *
459b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
460fdb8ebb7STetsuo Handa  *
461fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
462b69a54eeSKentaro Takeda  */
463b69a54eeSKentaro Takeda static int tomoyo_update_no_rewrite_entry(const char *pattern,
464b69a54eeSKentaro Takeda 					  const bool is_delete)
465b69a54eeSKentaro Takeda {
466ca0b7df3STetsuo Handa 	struct tomoyo_no_rewrite_entry *entry = NULL;
467ca0b7df3STetsuo Handa 	struct tomoyo_no_rewrite_entry *ptr;
468b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_pattern;
469ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
470b69a54eeSKentaro Takeda 
471b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__))
472b69a54eeSKentaro Takeda 		return -EINVAL;
473bf24fb01STetsuo Handa 	saved_pattern = tomoyo_get_name(pattern);
474b69a54eeSKentaro Takeda 	if (!saved_pattern)
475ca0b7df3STetsuo Handa 		return error;
476ca0b7df3STetsuo Handa 	if (!is_delete)
477ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
478f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
479fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
480b69a54eeSKentaro Takeda 		if (ptr->pattern != saved_pattern)
481b69a54eeSKentaro Takeda 			continue;
482b69a54eeSKentaro Takeda 		ptr->is_deleted = is_delete;
483b69a54eeSKentaro Takeda 		error = 0;
484ca0b7df3STetsuo Handa 		break;
485b69a54eeSKentaro Takeda 	}
486ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
487ca0b7df3STetsuo Handa 		entry->pattern = saved_pattern;
488bf24fb01STetsuo Handa 		saved_pattern = NULL;
489ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->list, &tomoyo_no_rewrite_list);
490ca0b7df3STetsuo Handa 		entry = NULL;
491b69a54eeSKentaro Takeda 		error = 0;
492ca0b7df3STetsuo Handa 	}
493f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
494bf24fb01STetsuo Handa 	tomoyo_put_name(saved_pattern);
495ca0b7df3STetsuo Handa 	kfree(entry);
496b69a54eeSKentaro Takeda 	return error;
497b69a54eeSKentaro Takeda }
498b69a54eeSKentaro Takeda 
499b69a54eeSKentaro Takeda /**
500b69a54eeSKentaro Takeda  * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
501b69a54eeSKentaro Takeda  *
502b69a54eeSKentaro Takeda  * @filename: Filename to check.
503b69a54eeSKentaro Takeda  *
504b69a54eeSKentaro Takeda  * Returns true if @filename is specified by "deny_rewrite" directive,
505b69a54eeSKentaro Takeda  * false otherwise.
506fdb8ebb7STetsuo Handa  *
507fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
508b69a54eeSKentaro Takeda  */
509b69a54eeSKentaro Takeda static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
510b69a54eeSKentaro Takeda {
511b69a54eeSKentaro Takeda 	struct tomoyo_no_rewrite_entry *ptr;
512b69a54eeSKentaro Takeda 	bool found = false;
513b69a54eeSKentaro Takeda 
514fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
515b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
516b69a54eeSKentaro Takeda 			continue;
517b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
518b69a54eeSKentaro Takeda 			continue;
519b69a54eeSKentaro Takeda 		found = true;
520b69a54eeSKentaro Takeda 		break;
521b69a54eeSKentaro Takeda 	}
522b69a54eeSKentaro Takeda 	return found;
523b69a54eeSKentaro Takeda }
524b69a54eeSKentaro Takeda 
525b69a54eeSKentaro Takeda /**
526b69a54eeSKentaro Takeda  * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
527b69a54eeSKentaro Takeda  *
528b69a54eeSKentaro Takeda  * @data:      String to parse.
529b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
530b69a54eeSKentaro Takeda  *
531b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
532fdb8ebb7STetsuo Handa  *
533fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
534b69a54eeSKentaro Takeda  */
535b69a54eeSKentaro Takeda int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
536b69a54eeSKentaro Takeda {
537b69a54eeSKentaro Takeda 	return tomoyo_update_no_rewrite_entry(data, is_delete);
538b69a54eeSKentaro Takeda }
539b69a54eeSKentaro Takeda 
540b69a54eeSKentaro Takeda /**
541b69a54eeSKentaro Takeda  * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
542b69a54eeSKentaro Takeda  *
543b69a54eeSKentaro Takeda  * @head: Pointer to "struct tomoyo_io_buffer".
544b69a54eeSKentaro Takeda  *
545b69a54eeSKentaro Takeda  * Returns true on success, false otherwise.
546fdb8ebb7STetsuo Handa  *
547fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
548b69a54eeSKentaro Takeda  */
549b69a54eeSKentaro Takeda bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
550b69a54eeSKentaro Takeda {
551b69a54eeSKentaro Takeda 	struct list_head *pos;
552b69a54eeSKentaro Takeda 	bool done = true;
553b69a54eeSKentaro Takeda 
554b69a54eeSKentaro Takeda 	list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
555b69a54eeSKentaro Takeda 		struct tomoyo_no_rewrite_entry *ptr;
556b69a54eeSKentaro Takeda 		ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
557b69a54eeSKentaro Takeda 		if (ptr->is_deleted)
558b69a54eeSKentaro Takeda 			continue;
5597d2948b1STetsuo Handa 		done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
5607d2948b1STetsuo Handa 					"%s\n", ptr->pattern->name);
5617d2948b1STetsuo Handa 		if (!done)
562b69a54eeSKentaro Takeda 			break;
563b69a54eeSKentaro Takeda 	}
564b69a54eeSKentaro Takeda 	return done;
565b69a54eeSKentaro Takeda }
566b69a54eeSKentaro Takeda 
567b69a54eeSKentaro Takeda /**
568b69a54eeSKentaro Takeda  * tomoyo_update_file_acl - Update file's read/write/execute ACL.
569b69a54eeSKentaro Takeda  *
570b69a54eeSKentaro Takeda  * @filename:  Filename.
571b69a54eeSKentaro Takeda  * @perm:      Permission (between 1 to 7).
572b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
573b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
574b69a54eeSKentaro Takeda  *
575b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
576b69a54eeSKentaro Takeda  *
577b69a54eeSKentaro Takeda  * This is legacy support interface for older policy syntax.
578b69a54eeSKentaro Takeda  * Current policy syntax uses "allow_read/write" instead of "6",
579b69a54eeSKentaro Takeda  * "allow_read" instead of "4", "allow_write" instead of "2",
580b69a54eeSKentaro Takeda  * "allow_execute" instead of "1".
581fdb8ebb7STetsuo Handa  *
582fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
583b69a54eeSKentaro Takeda  */
584b69a54eeSKentaro Takeda static int tomoyo_update_file_acl(const char *filename, u8 perm,
585b69a54eeSKentaro Takeda 				  struct tomoyo_domain_info * const domain,
586b69a54eeSKentaro Takeda 				  const bool is_delete)
587b69a54eeSKentaro Takeda {
588b69a54eeSKentaro Takeda 	if (perm > 7 || !perm) {
589b69a54eeSKentaro Takeda 		printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
590b69a54eeSKentaro Takeda 		       __func__, perm, filename);
591b69a54eeSKentaro Takeda 		return -EINVAL;
592b69a54eeSKentaro Takeda 	}
593b69a54eeSKentaro Takeda 	if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
594b69a54eeSKentaro Takeda 		/*
595b69a54eeSKentaro Takeda 		 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
596b69a54eeSKentaro Takeda 		 * directory permissions.
597b69a54eeSKentaro Takeda 		 */
598b69a54eeSKentaro Takeda 		return 0;
599b69a54eeSKentaro Takeda 	if (perm & 4)
6007ef61233STetsuo Handa 		tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
6017ef61233STetsuo Handa 				       is_delete);
602b69a54eeSKentaro Takeda 	if (perm & 2)
6037ef61233STetsuo Handa 		tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
6047ef61233STetsuo Handa 				       is_delete);
605b69a54eeSKentaro Takeda 	if (perm & 1)
6067ef61233STetsuo Handa 		tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
6077ef61233STetsuo Handa 				       is_delete);
608b69a54eeSKentaro Takeda 	return 0;
609b69a54eeSKentaro Takeda }
610b69a54eeSKentaro Takeda 
611b69a54eeSKentaro Takeda /**
6127ef61233STetsuo Handa  * tomoyo_path_acl2 - Check permission for single path operation.
613b69a54eeSKentaro Takeda  *
614b69a54eeSKentaro Takeda  * @domain:          Pointer to "struct tomoyo_domain_info".
615b69a54eeSKentaro Takeda  * @filename:        Filename to check.
616b69a54eeSKentaro Takeda  * @perm:            Permission.
617b69a54eeSKentaro Takeda  * @may_use_pattern: True if patterned ACL is permitted.
618b69a54eeSKentaro Takeda  *
619b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
620fdb8ebb7STetsuo Handa  *
621fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
622b69a54eeSKentaro Takeda  */
6237ef61233STetsuo Handa static int tomoyo_path_acl2(const struct tomoyo_domain_info *domain,
6247ef61233STetsuo Handa 			    const struct tomoyo_path_info *filename,
6257ef61233STetsuo Handa 			    const u32 perm, const bool may_use_pattern)
626b69a54eeSKentaro Takeda {
627b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
628b69a54eeSKentaro Takeda 	int error = -EPERM;
629b69a54eeSKentaro Takeda 
630fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
6317ef61233STetsuo Handa 		struct tomoyo_path_acl *acl;
6327ef61233STetsuo Handa 		if (ptr->type != TOMOYO_TYPE_PATH_ACL)
633b69a54eeSKentaro Takeda 			continue;
6347ef61233STetsuo Handa 		acl = container_of(ptr, struct tomoyo_path_acl, head);
635937bf613STetsuo Handa 		if (perm <= 0xFFFF) {
636b69a54eeSKentaro Takeda 			if (!(acl->perm & perm))
637b69a54eeSKentaro Takeda 				continue;
638937bf613STetsuo Handa 		} else {
639937bf613STetsuo Handa 			if (!(acl->perm_high & (perm >> 16)))
640937bf613STetsuo Handa 				continue;
641937bf613STetsuo Handa 		}
642b69a54eeSKentaro Takeda 		if (may_use_pattern || !acl->filename->is_patterned) {
643b69a54eeSKentaro Takeda 			if (!tomoyo_path_matches_pattern(filename,
644b69a54eeSKentaro Takeda 							 acl->filename))
645b69a54eeSKentaro Takeda 				continue;
646b69a54eeSKentaro Takeda 		} else {
647b69a54eeSKentaro Takeda 			continue;
648b69a54eeSKentaro Takeda 		}
649b69a54eeSKentaro Takeda 		error = 0;
650b69a54eeSKentaro Takeda 		break;
651b69a54eeSKentaro Takeda 	}
652b69a54eeSKentaro Takeda 	return error;
653b69a54eeSKentaro Takeda }
654b69a54eeSKentaro Takeda 
655b69a54eeSKentaro Takeda /**
656b69a54eeSKentaro Takeda  * tomoyo_check_file_acl - Check permission for opening files.
657b69a54eeSKentaro Takeda  *
658b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
659b69a54eeSKentaro Takeda  * @filename:  Filename to check.
660b69a54eeSKentaro Takeda  * @operation: Mode ("read" or "write" or "read/write" or "execute").
661b69a54eeSKentaro Takeda  *
662b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
663fdb8ebb7STetsuo Handa  *
664fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
665b69a54eeSKentaro Takeda  */
666b69a54eeSKentaro Takeda static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
667b69a54eeSKentaro Takeda 				 const struct tomoyo_path_info *filename,
668b69a54eeSKentaro Takeda 				 const u8 operation)
669b69a54eeSKentaro Takeda {
670937bf613STetsuo Handa 	u32 perm = 0;
671b69a54eeSKentaro Takeda 
672b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
673b69a54eeSKentaro Takeda 		return 0;
674b69a54eeSKentaro Takeda 	if (operation == 6)
6757ef61233STetsuo Handa 		perm = 1 << TOMOYO_TYPE_READ_WRITE;
676b69a54eeSKentaro Takeda 	else if (operation == 4)
6777ef61233STetsuo Handa 		perm = 1 << TOMOYO_TYPE_READ;
678b69a54eeSKentaro Takeda 	else if (operation == 2)
6797ef61233STetsuo Handa 		perm = 1 << TOMOYO_TYPE_WRITE;
680b69a54eeSKentaro Takeda 	else if (operation == 1)
6817ef61233STetsuo Handa 		perm = 1 << TOMOYO_TYPE_EXECUTE;
682b69a54eeSKentaro Takeda 	else
683b69a54eeSKentaro Takeda 		BUG();
6847ef61233STetsuo Handa 	return tomoyo_path_acl2(domain, filename, perm, operation != 1);
685b69a54eeSKentaro Takeda }
686b69a54eeSKentaro Takeda 
687b69a54eeSKentaro Takeda /**
688b69a54eeSKentaro Takeda  * tomoyo_check_file_perm2 - Check permission for opening files.
689b69a54eeSKentaro Takeda  *
690b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
691b69a54eeSKentaro Takeda  * @filename:  Filename to check.
692b69a54eeSKentaro Takeda  * @perm:      Mode ("read" or "write" or "read/write" or "execute").
693b69a54eeSKentaro Takeda  * @operation: Operation name passed used for verbose mode.
694b69a54eeSKentaro Takeda  * @mode:      Access control mode.
695b69a54eeSKentaro Takeda  *
696b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
697fdb8ebb7STetsuo Handa  *
698fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
699b69a54eeSKentaro Takeda  */
700b69a54eeSKentaro Takeda static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain,
701b69a54eeSKentaro Takeda 				   const struct tomoyo_path_info *filename,
702b69a54eeSKentaro Takeda 				   const u8 perm, const char *operation,
703b69a54eeSKentaro Takeda 				   const u8 mode)
704b69a54eeSKentaro Takeda {
705b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
706b69a54eeSKentaro Takeda 	const char *msg = "<unknown>";
707b69a54eeSKentaro Takeda 	int error = 0;
708b69a54eeSKentaro Takeda 
709b69a54eeSKentaro Takeda 	if (!filename)
710b69a54eeSKentaro Takeda 		return 0;
711b69a54eeSKentaro Takeda 	error = tomoyo_check_file_acl(domain, filename, perm);
712ea13ddbaSTetsuo Handa 	if (error && perm == 4 && !domain->ignore_global_allow_read
713b69a54eeSKentaro Takeda 	    && tomoyo_is_globally_readable_file(filename))
714b69a54eeSKentaro Takeda 		error = 0;
715b69a54eeSKentaro Takeda 	if (perm == 6)
7167ef61233STetsuo Handa 		msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
717b69a54eeSKentaro Takeda 	else if (perm == 4)
7187ef61233STetsuo Handa 		msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
719b69a54eeSKentaro Takeda 	else if (perm == 2)
7207ef61233STetsuo Handa 		msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
721b69a54eeSKentaro Takeda 	else if (perm == 1)
7227ef61233STetsuo Handa 		msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
723b69a54eeSKentaro Takeda 	else
724b69a54eeSKentaro Takeda 		BUG();
725b69a54eeSKentaro Takeda 	if (!error)
726b69a54eeSKentaro Takeda 		return 0;
727b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
728b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied "
729b69a54eeSKentaro Takeda 		       "for %s\n", tomoyo_get_msg(is_enforce), msg, operation,
730b69a54eeSKentaro Takeda 		       filename->name, tomoyo_get_last_name(domain));
731b69a54eeSKentaro Takeda 	if (is_enforce)
732b69a54eeSKentaro Takeda 		return error;
733b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
734b69a54eeSKentaro Takeda 		/* Don't use patterns for execute permission. */
735b69a54eeSKentaro Takeda 		const struct tomoyo_path_info *patterned_file = (perm != 1) ?
736b69a54eeSKentaro Takeda 			tomoyo_get_file_pattern(filename) : filename;
737b69a54eeSKentaro Takeda 		tomoyo_update_file_acl(patterned_file->name, perm,
738b69a54eeSKentaro Takeda 				       domain, false);
739b69a54eeSKentaro Takeda 	}
740b69a54eeSKentaro Takeda 	return 0;
741b69a54eeSKentaro Takeda }
742b69a54eeSKentaro Takeda 
743b69a54eeSKentaro Takeda /**
744b69a54eeSKentaro Takeda  * tomoyo_write_file_policy - Update file related list.
745b69a54eeSKentaro Takeda  *
746b69a54eeSKentaro Takeda  * @data:      String to parse.
747b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
748b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
749b69a54eeSKentaro Takeda  *
750b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
751fdb8ebb7STetsuo Handa  *
752fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
753b69a54eeSKentaro Takeda  */
754b69a54eeSKentaro Takeda int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
755b69a54eeSKentaro Takeda 			     const bool is_delete)
756b69a54eeSKentaro Takeda {
757b69a54eeSKentaro Takeda 	char *filename = strchr(data, ' ');
758b69a54eeSKentaro Takeda 	char *filename2;
759b69a54eeSKentaro Takeda 	unsigned int perm;
760b69a54eeSKentaro Takeda 	u8 type;
761b69a54eeSKentaro Takeda 
762b69a54eeSKentaro Takeda 	if (!filename)
763b69a54eeSKentaro Takeda 		return -EINVAL;
764b69a54eeSKentaro Takeda 	*filename++ = '\0';
765b69a54eeSKentaro Takeda 	if (sscanf(data, "%u", &perm) == 1)
766b69a54eeSKentaro Takeda 		return tomoyo_update_file_acl(filename, (u8) perm, domain,
767b69a54eeSKentaro Takeda 					      is_delete);
768b69a54eeSKentaro Takeda 	if (strncmp(data, "allow_", 6))
769b69a54eeSKentaro Takeda 		goto out;
770b69a54eeSKentaro Takeda 	data += 6;
7717ef61233STetsuo Handa 	for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
7727ef61233STetsuo Handa 		if (strcmp(data, tomoyo_path_keyword[type]))
773b69a54eeSKentaro Takeda 			continue;
7747ef61233STetsuo Handa 		return tomoyo_update_path_acl(type, filename, domain,
7757ef61233STetsuo Handa 					      is_delete);
776b69a54eeSKentaro Takeda 	}
777b69a54eeSKentaro Takeda 	filename2 = strchr(filename, ' ');
778b69a54eeSKentaro Takeda 	if (!filename2)
779b69a54eeSKentaro Takeda 		goto out;
780b69a54eeSKentaro Takeda 	*filename2++ = '\0';
7817ef61233STetsuo Handa 	for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
7827ef61233STetsuo Handa 		if (strcmp(data, tomoyo_path2_keyword[type]))
783b69a54eeSKentaro Takeda 			continue;
7847ef61233STetsuo Handa 		return tomoyo_update_path2_acl(type, filename, filename2,
785b69a54eeSKentaro Takeda 					       domain, is_delete);
786b69a54eeSKentaro Takeda 	}
787b69a54eeSKentaro Takeda  out:
788b69a54eeSKentaro Takeda 	return -EINVAL;
789b69a54eeSKentaro Takeda }
790b69a54eeSKentaro Takeda 
791b69a54eeSKentaro Takeda /**
7927ef61233STetsuo Handa  * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
793b69a54eeSKentaro Takeda  *
794b69a54eeSKentaro Takeda  * @type:      Type of operation.
795b69a54eeSKentaro Takeda  * @filename:  Filename.
796b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
797b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
798b69a54eeSKentaro Takeda  *
799b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
800fdb8ebb7STetsuo Handa  *
801fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
802b69a54eeSKentaro Takeda  */
8037ef61233STetsuo Handa static int tomoyo_update_path_acl(const u8 type, const char *filename,
8047ef61233STetsuo Handa 				  struct tomoyo_domain_info *const domain,
8057ef61233STetsuo Handa 				  const bool is_delete)
806b69a54eeSKentaro Takeda {
807937bf613STetsuo Handa 	static const u32 rw_mask =
8087ef61233STetsuo Handa 		(1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
809b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename;
810b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
8117ef61233STetsuo Handa 	struct tomoyo_path_acl *entry = NULL;
812ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
813937bf613STetsuo Handa 	const u32 perm = 1 << type;
814b69a54eeSKentaro Takeda 
815b69a54eeSKentaro Takeda 	if (!domain)
816b69a54eeSKentaro Takeda 		return -EINVAL;
817b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__))
818b69a54eeSKentaro Takeda 		return -EINVAL;
819bf24fb01STetsuo Handa 	saved_filename = tomoyo_get_name(filename);
820b69a54eeSKentaro Takeda 	if (!saved_filename)
821b69a54eeSKentaro Takeda 		return -ENOMEM;
822ca0b7df3STetsuo Handa 	if (!is_delete)
823ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
824f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
825fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
8267ef61233STetsuo Handa 		struct tomoyo_path_acl *acl =
8277ef61233STetsuo Handa 			container_of(ptr, struct tomoyo_path_acl, head);
8287ef61233STetsuo Handa 		if (ptr->type != TOMOYO_TYPE_PATH_ACL)
829b69a54eeSKentaro Takeda 			continue;
830b69a54eeSKentaro Takeda 		if (acl->filename != saved_filename)
831b69a54eeSKentaro Takeda 			continue;
832ca0b7df3STetsuo Handa 		if (is_delete) {
833ca0b7df3STetsuo Handa 			if (perm <= 0xFFFF)
834ca0b7df3STetsuo Handa 				acl->perm &= ~perm;
835ca0b7df3STetsuo Handa 			else
836ca0b7df3STetsuo Handa 				acl->perm_high &= ~(perm >> 16);
837ca0b7df3STetsuo Handa 			if ((acl->perm & rw_mask) != rw_mask)
8387ef61233STetsuo Handa 				acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
8397ef61233STetsuo Handa 			else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
840ca0b7df3STetsuo Handa 				acl->perm &= ~rw_mask;
841ca0b7df3STetsuo Handa 		} else {
842937bf613STetsuo Handa 			if (perm <= 0xFFFF)
843b69a54eeSKentaro Takeda 				acl->perm |= perm;
844937bf613STetsuo Handa 			else
845937bf613STetsuo Handa 				acl->perm_high |= (perm >> 16);
846b69a54eeSKentaro Takeda 			if ((acl->perm & rw_mask) == rw_mask)
8477ef61233STetsuo Handa 				acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
8487ef61233STetsuo Handa 			else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
849b69a54eeSKentaro Takeda 				acl->perm |= rw_mask;
850b69a54eeSKentaro Takeda 		}
851b69a54eeSKentaro Takeda 		error = 0;
852b69a54eeSKentaro Takeda 		break;
853b69a54eeSKentaro Takeda 	}
854ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
8557ef61233STetsuo Handa 		entry->head.type = TOMOYO_TYPE_PATH_ACL;
856ca0b7df3STetsuo Handa 		if (perm <= 0xFFFF)
857ca0b7df3STetsuo Handa 			entry->perm = perm;
858ca0b7df3STetsuo Handa 		else
859ca0b7df3STetsuo Handa 			entry->perm_high = (perm >> 16);
8607ef61233STetsuo Handa 		if (perm == (1 << TOMOYO_TYPE_READ_WRITE))
861ca0b7df3STetsuo Handa 			entry->perm |= rw_mask;
862ca0b7df3STetsuo Handa 		entry->filename = saved_filename;
863bf24fb01STetsuo Handa 		saved_filename = NULL;
864ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
865ca0b7df3STetsuo Handa 		entry = NULL;
866ca0b7df3STetsuo Handa 		error = 0;
867ca0b7df3STetsuo Handa 	}
868f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
869ca0b7df3STetsuo Handa 	kfree(entry);
870bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename);
871b69a54eeSKentaro Takeda 	return error;
872b69a54eeSKentaro Takeda }
873b69a54eeSKentaro Takeda 
874b69a54eeSKentaro Takeda /**
8757ef61233STetsuo Handa  * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
876b69a54eeSKentaro Takeda  *
877b69a54eeSKentaro Takeda  * @type:      Type of operation.
878b69a54eeSKentaro Takeda  * @filename1: First filename.
879b69a54eeSKentaro Takeda  * @filename2: Second filename.
880b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
881b69a54eeSKentaro Takeda  * @is_delete: True if it is a delete request.
882b69a54eeSKentaro Takeda  *
883b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
884fdb8ebb7STetsuo Handa  *
885fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
886b69a54eeSKentaro Takeda  */
8877ef61233STetsuo Handa static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
888b69a54eeSKentaro Takeda 				   const char *filename2,
8897ef61233STetsuo Handa 				   struct tomoyo_domain_info *const domain,
8907ef61233STetsuo Handa 				   const bool is_delete)
891b69a54eeSKentaro Takeda {
892b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename1;
893b69a54eeSKentaro Takeda 	const struct tomoyo_path_info *saved_filename2;
894b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
8957ef61233STetsuo Handa 	struct tomoyo_path2_acl *entry = NULL;
896ca0b7df3STetsuo Handa 	int error = is_delete ? -ENOENT : -ENOMEM;
897b69a54eeSKentaro Takeda 	const u8 perm = 1 << type;
898b69a54eeSKentaro Takeda 
899b69a54eeSKentaro Takeda 	if (!domain)
900b69a54eeSKentaro Takeda 		return -EINVAL;
901b69a54eeSKentaro Takeda 	if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) ||
902b69a54eeSKentaro Takeda 	    !tomoyo_is_correct_path(filename2, 0, 0, 0, __func__))
903b69a54eeSKentaro Takeda 		return -EINVAL;
904bf24fb01STetsuo Handa 	saved_filename1 = tomoyo_get_name(filename1);
905bf24fb01STetsuo Handa 	saved_filename2 = tomoyo_get_name(filename2);
906b69a54eeSKentaro Takeda 	if (!saved_filename1 || !saved_filename2)
907ca0b7df3STetsuo Handa 		goto out;
908ca0b7df3STetsuo Handa 	if (!is_delete)
909ca0b7df3STetsuo Handa 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
910f737d95dSTetsuo Handa 	mutex_lock(&tomoyo_policy_lock);
911ca0b7df3STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
9127ef61233STetsuo Handa 		struct tomoyo_path2_acl *acl =
9137ef61233STetsuo Handa 			container_of(ptr, struct tomoyo_path2_acl, head);
9147ef61233STetsuo Handa 		if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
915ca0b7df3STetsuo Handa 			continue;
916ca0b7df3STetsuo Handa 		if (acl->filename1 != saved_filename1 ||
917ca0b7df3STetsuo Handa 		    acl->filename2 != saved_filename2)
918ca0b7df3STetsuo Handa 			continue;
919b69a54eeSKentaro Takeda 		if (is_delete)
920b69a54eeSKentaro Takeda 			acl->perm &= ~perm;
921ca0b7df3STetsuo Handa 		else
922ca0b7df3STetsuo Handa 			acl->perm |= perm;
923b69a54eeSKentaro Takeda 		error = 0;
924b69a54eeSKentaro Takeda 		break;
925b69a54eeSKentaro Takeda 	}
926ca0b7df3STetsuo Handa 	if (!is_delete && error && tomoyo_memory_ok(entry)) {
9277ef61233STetsuo Handa 		entry->head.type = TOMOYO_TYPE_PATH2_ACL;
928ca0b7df3STetsuo Handa 		entry->perm = perm;
929ca0b7df3STetsuo Handa 		entry->filename1 = saved_filename1;
930bf24fb01STetsuo Handa 		saved_filename1 = NULL;
931ca0b7df3STetsuo Handa 		entry->filename2 = saved_filename2;
932bf24fb01STetsuo Handa 		saved_filename2 = NULL;
933ca0b7df3STetsuo Handa 		list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
934ca0b7df3STetsuo Handa 		entry = NULL;
935ca0b7df3STetsuo Handa 		error = 0;
936ca0b7df3STetsuo Handa 	}
937f737d95dSTetsuo Handa 	mutex_unlock(&tomoyo_policy_lock);
938ca0b7df3STetsuo Handa  out:
939bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename1);
940bf24fb01STetsuo Handa 	tomoyo_put_name(saved_filename2);
941ca0b7df3STetsuo Handa 	kfree(entry);
942b69a54eeSKentaro Takeda 	return error;
943b69a54eeSKentaro Takeda }
944b69a54eeSKentaro Takeda 
945b69a54eeSKentaro Takeda /**
9467ef61233STetsuo Handa  * tomoyo_path_acl - Check permission for single path operation.
947b69a54eeSKentaro Takeda  *
948b69a54eeSKentaro Takeda  * @domain:   Pointer to "struct tomoyo_domain_info".
949b69a54eeSKentaro Takeda  * @type:     Type of operation.
950b69a54eeSKentaro Takeda  * @filename: Filename to check.
951b69a54eeSKentaro Takeda  *
952b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
953fdb8ebb7STetsuo Handa  *
954fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
955b69a54eeSKentaro Takeda  */
9567ef61233STetsuo Handa static int tomoyo_path_acl(struct tomoyo_domain_info *domain, const u8 type,
957b69a54eeSKentaro Takeda 			   const struct tomoyo_path_info *filename)
958b69a54eeSKentaro Takeda {
959b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
960b69a54eeSKentaro Takeda 		return 0;
9617ef61233STetsuo Handa 	return tomoyo_path_acl2(domain, filename, 1 << type, 1);
962b69a54eeSKentaro Takeda }
963b69a54eeSKentaro Takeda 
964b69a54eeSKentaro Takeda /**
9657ef61233STetsuo Handa  * tomoyo_path2_acl - Check permission for double path operation.
966b69a54eeSKentaro Takeda  *
967b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
968b69a54eeSKentaro Takeda  * @type:      Type of operation.
969b69a54eeSKentaro Takeda  * @filename1: First filename to check.
970b69a54eeSKentaro Takeda  * @filename2: Second filename to check.
971b69a54eeSKentaro Takeda  *
972b69a54eeSKentaro Takeda  * Returns 0 on success, -EPERM otherwise.
973fdb8ebb7STetsuo Handa  *
974fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
975b69a54eeSKentaro Takeda  */
9767ef61233STetsuo Handa static int tomoyo_path2_acl(const struct tomoyo_domain_info *domain,
977b69a54eeSKentaro Takeda 			    const u8 type,
9787ef61233STetsuo Handa 			    const struct tomoyo_path_info *filename1,
9797ef61233STetsuo Handa 			    const struct tomoyo_path_info *filename2)
980b69a54eeSKentaro Takeda {
981b69a54eeSKentaro Takeda 	struct tomoyo_acl_info *ptr;
982b69a54eeSKentaro Takeda 	const u8 perm = 1 << type;
983b69a54eeSKentaro Takeda 	int error = -EPERM;
984b69a54eeSKentaro Takeda 
985b69a54eeSKentaro Takeda 	if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
986b69a54eeSKentaro Takeda 		return 0;
987fdb8ebb7STetsuo Handa 	list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
9887ef61233STetsuo Handa 		struct tomoyo_path2_acl *acl;
9897ef61233STetsuo Handa 		if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
990b69a54eeSKentaro Takeda 			continue;
9917ef61233STetsuo Handa 		acl = container_of(ptr, struct tomoyo_path2_acl, head);
992b69a54eeSKentaro Takeda 		if (!(acl->perm & perm))
993b69a54eeSKentaro Takeda 			continue;
994b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename1, acl->filename1))
995b69a54eeSKentaro Takeda 			continue;
996b69a54eeSKentaro Takeda 		if (!tomoyo_path_matches_pattern(filename2, acl->filename2))
997b69a54eeSKentaro Takeda 			continue;
998b69a54eeSKentaro Takeda 		error = 0;
999b69a54eeSKentaro Takeda 		break;
1000b69a54eeSKentaro Takeda 	}
1001b69a54eeSKentaro Takeda 	return error;
1002b69a54eeSKentaro Takeda }
1003b69a54eeSKentaro Takeda 
1004b69a54eeSKentaro Takeda /**
10057ef61233STetsuo Handa  * tomoyo_path_permission2 - Check permission for single path operation.
1006b69a54eeSKentaro Takeda  *
1007b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1008b69a54eeSKentaro Takeda  * @operation: Type of operation.
1009b69a54eeSKentaro Takeda  * @filename:  Filename to check.
1010b69a54eeSKentaro Takeda  * @mode:      Access control mode.
1011b69a54eeSKentaro Takeda  *
1012b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1013fdb8ebb7STetsuo Handa  *
1014fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1015b69a54eeSKentaro Takeda  */
10167ef61233STetsuo Handa static int tomoyo_path_permission2(struct tomoyo_domain_info *const domain,
10177ef61233STetsuo Handa 				   u8 operation,
10187ef61233STetsuo Handa 				   const struct tomoyo_path_info *filename,
10197ef61233STetsuo Handa 				   const u8 mode)
1020b69a54eeSKentaro Takeda {
1021b69a54eeSKentaro Takeda 	const char *msg;
1022b69a54eeSKentaro Takeda 	int error;
1023b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1024b69a54eeSKentaro Takeda 
1025b69a54eeSKentaro Takeda 	if (!mode)
1026b69a54eeSKentaro Takeda 		return 0;
1027b69a54eeSKentaro Takeda  next:
10287ef61233STetsuo Handa 	error = tomoyo_path_acl(domain, operation, filename);
10297ef61233STetsuo Handa 	msg = tomoyo_path2keyword(operation);
1030b69a54eeSKentaro Takeda 	if (!error)
1031b69a54eeSKentaro Takeda 		goto ok;
1032b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
1033b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n",
1034b69a54eeSKentaro Takeda 		       tomoyo_get_msg(is_enforce), msg, filename->name,
1035b69a54eeSKentaro Takeda 		       tomoyo_get_last_name(domain));
1036b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1037b69a54eeSKentaro Takeda 		const char *name = tomoyo_get_file_pattern(filename)->name;
10387ef61233STetsuo Handa 		tomoyo_update_path_acl(operation, name, domain, false);
1039b69a54eeSKentaro Takeda 	}
1040b69a54eeSKentaro Takeda 	if (!is_enforce)
1041b69a54eeSKentaro Takeda 		error = 0;
1042b69a54eeSKentaro Takeda  ok:
1043b69a54eeSKentaro Takeda 	/*
1044b69a54eeSKentaro Takeda 	 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1045b69a54eeSKentaro Takeda 	 * we need to check "allow_rewrite" permission if the filename is
1046b69a54eeSKentaro Takeda 	 * specified by "deny_rewrite" keyword.
1047b69a54eeSKentaro Takeda 	 */
10487ef61233STetsuo Handa 	if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
1049b69a54eeSKentaro Takeda 	    tomoyo_is_no_rewrite_file(filename)) {
10507ef61233STetsuo Handa 		operation = TOMOYO_TYPE_REWRITE;
1051b69a54eeSKentaro Takeda 		goto next;
1052b69a54eeSKentaro Takeda 	}
1053b69a54eeSKentaro Takeda 	return error;
1054b69a54eeSKentaro Takeda }
1055b69a54eeSKentaro Takeda 
1056b69a54eeSKentaro Takeda /**
1057b69a54eeSKentaro Takeda  * tomoyo_check_exec_perm - Check permission for "execute".
1058b69a54eeSKentaro Takeda  *
1059b69a54eeSKentaro Takeda  * @domain:   Pointer to "struct tomoyo_domain_info".
1060b69a54eeSKentaro Takeda  * @filename: Check permission for "execute".
1061b69a54eeSKentaro Takeda  *
1062b69a54eeSKentaro Takeda  * Returns 0 on success, negativevalue otherwise.
1063fdb8ebb7STetsuo Handa  *
1064fdb8ebb7STetsuo Handa  * Caller holds tomoyo_read_lock().
1065b69a54eeSKentaro Takeda  */
1066b69a54eeSKentaro Takeda int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
1067bcb86975STetsuo Handa 			   const struct tomoyo_path_info *filename)
1068b69a54eeSKentaro Takeda {
1069b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1070b69a54eeSKentaro Takeda 
1071b69a54eeSKentaro Takeda 	if (!mode)
1072b69a54eeSKentaro Takeda 		return 0;
1073b69a54eeSKentaro Takeda 	return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode);
1074b69a54eeSKentaro Takeda }
1075b69a54eeSKentaro Takeda 
1076b69a54eeSKentaro Takeda /**
1077b69a54eeSKentaro Takeda  * tomoyo_check_open_permission - Check permission for "read" and "write".
1078b69a54eeSKentaro Takeda  *
1079b69a54eeSKentaro Takeda  * @domain: Pointer to "struct tomoyo_domain_info".
1080b69a54eeSKentaro Takeda  * @path:   Pointer to "struct path".
1081b69a54eeSKentaro Takeda  * @flag:   Flags for open().
1082b69a54eeSKentaro Takeda  *
1083b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1084b69a54eeSKentaro Takeda  */
1085b69a54eeSKentaro Takeda int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
1086b69a54eeSKentaro Takeda 				 struct path *path, const int flag)
1087b69a54eeSKentaro Takeda {
1088b69a54eeSKentaro Takeda 	const u8 acc_mode = ACC_MODE(flag);
1089b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1090b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1091b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1092b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1093fdb8ebb7STetsuo Handa 	int idx;
1094b69a54eeSKentaro Takeda 
1095b69a54eeSKentaro Takeda 	if (!mode || !path->mnt)
1096b69a54eeSKentaro Takeda 		return 0;
1097b69a54eeSKentaro Takeda 	if (acc_mode == 0)
1098b69a54eeSKentaro Takeda 		return 0;
1099b69a54eeSKentaro Takeda 	if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
1100b69a54eeSKentaro Takeda 		/*
1101b69a54eeSKentaro Takeda 		 * I don't check directories here because mkdir() and rmdir()
1102b69a54eeSKentaro Takeda 		 * don't call me.
1103b69a54eeSKentaro Takeda 		 */
1104b69a54eeSKentaro Takeda 		return 0;
1105fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1106b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(path);
1107b69a54eeSKentaro Takeda 	if (!buf)
1108b69a54eeSKentaro Takeda 		goto out;
1109b69a54eeSKentaro Takeda 	error = 0;
1110b69a54eeSKentaro Takeda 	/*
1111b69a54eeSKentaro Takeda 	 * If the filename is specified by "deny_rewrite" keyword,
1112b69a54eeSKentaro Takeda 	 * we need to check "allow_rewrite" permission when the filename is not
1113b69a54eeSKentaro Takeda 	 * opened for append mode or the filename is truncated at open time.
1114b69a54eeSKentaro Takeda 	 */
1115b69a54eeSKentaro Takeda 	if ((acc_mode & MAY_WRITE) &&
1116b69a54eeSKentaro Takeda 	    ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
1117b69a54eeSKentaro Takeda 	    (tomoyo_is_no_rewrite_file(buf))) {
11187ef61233STetsuo Handa 		error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE,
1119b69a54eeSKentaro Takeda 						buf, mode);
1120b69a54eeSKentaro Takeda 	}
1121b69a54eeSKentaro Takeda 	if (!error)
1122b69a54eeSKentaro Takeda 		error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
1123b69a54eeSKentaro Takeda 						mode);
1124b69a54eeSKentaro Takeda 	if (!error && (flag & O_TRUNC))
11257ef61233STetsuo Handa 		error = tomoyo_path_permission2(domain, TOMOYO_TYPE_TRUNCATE,
1126b69a54eeSKentaro Takeda 						buf, mode);
1127b69a54eeSKentaro Takeda  out:
11288e2d39a1STetsuo Handa 	kfree(buf);
1129fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1130b69a54eeSKentaro Takeda 	if (!is_enforce)
1131b69a54eeSKentaro Takeda 		error = 0;
1132b69a54eeSKentaro Takeda 	return error;
1133b69a54eeSKentaro Takeda }
1134b69a54eeSKentaro Takeda 
1135b69a54eeSKentaro Takeda /**
11367ef61233STetsuo Handa  * tomoyo_path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
1137b69a54eeSKentaro Takeda  *
1138b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1139b69a54eeSKentaro Takeda  * @operation: Type of operation.
1140b69a54eeSKentaro Takeda  * @path:      Pointer to "struct path".
1141b69a54eeSKentaro Takeda  *
1142b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1143b69a54eeSKentaro Takeda  */
11447ef61233STetsuo Handa int tomoyo_path_perm(struct tomoyo_domain_info *domain,
1145b69a54eeSKentaro Takeda 		     const u8 operation, struct path *path)
1146b69a54eeSKentaro Takeda {
1147b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1148b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1149b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1150b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1151fdb8ebb7STetsuo Handa 	int idx;
1152b69a54eeSKentaro Takeda 
1153b69a54eeSKentaro Takeda 	if (!mode || !path->mnt)
1154b69a54eeSKentaro Takeda 		return 0;
1155fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1156b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(path);
1157b69a54eeSKentaro Takeda 	if (!buf)
1158b69a54eeSKentaro Takeda 		goto out;
1159b69a54eeSKentaro Takeda 	switch (operation) {
11607ef61233STetsuo Handa 	case TOMOYO_TYPE_MKDIR:
11617ef61233STetsuo Handa 	case TOMOYO_TYPE_RMDIR:
11627ef61233STetsuo Handa 	case TOMOYO_TYPE_CHROOT:
1163b69a54eeSKentaro Takeda 		if (!buf->is_dir) {
1164b69a54eeSKentaro Takeda 			/*
1165b69a54eeSKentaro Takeda 			 * tomoyo_get_path() reserves space for appending "/."
1166b69a54eeSKentaro Takeda 			 */
1167b69a54eeSKentaro Takeda 			strcat((char *) buf->name, "/");
1168b69a54eeSKentaro Takeda 			tomoyo_fill_path_info(buf);
1169b69a54eeSKentaro Takeda 		}
1170b69a54eeSKentaro Takeda 	}
11717ef61233STetsuo Handa 	error = tomoyo_path_permission2(domain, operation, buf, mode);
1172b69a54eeSKentaro Takeda  out:
11738e2d39a1STetsuo Handa 	kfree(buf);
1174fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1175b69a54eeSKentaro Takeda 	if (!is_enforce)
1176b69a54eeSKentaro Takeda 		error = 0;
1177b69a54eeSKentaro Takeda 	return error;
1178b69a54eeSKentaro Takeda }
1179b69a54eeSKentaro Takeda 
1180b69a54eeSKentaro Takeda /**
1181b69a54eeSKentaro Takeda  * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1182b69a54eeSKentaro Takeda  *
1183b69a54eeSKentaro Takeda  * @domain: Pointer to "struct tomoyo_domain_info".
1184b69a54eeSKentaro Takeda  * @filp: Pointer to "struct file".
1185b69a54eeSKentaro Takeda  *
1186b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1187b69a54eeSKentaro Takeda  */
1188b69a54eeSKentaro Takeda int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain,
1189b69a54eeSKentaro Takeda 				    struct file *filp)
1190b69a54eeSKentaro Takeda {
1191b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1192b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1193b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1194b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf;
1195fdb8ebb7STetsuo Handa 	int idx;
1196b69a54eeSKentaro Takeda 
1197b69a54eeSKentaro Takeda 	if (!mode || !filp->f_path.mnt)
1198b69a54eeSKentaro Takeda 		return 0;
1199fdb8ebb7STetsuo Handa 
1200fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1201b69a54eeSKentaro Takeda 	buf = tomoyo_get_path(&filp->f_path);
1202b69a54eeSKentaro Takeda 	if (!buf)
1203b69a54eeSKentaro Takeda 		goto out;
1204b69a54eeSKentaro Takeda 	if (!tomoyo_is_no_rewrite_file(buf)) {
1205b69a54eeSKentaro Takeda 		error = 0;
1206b69a54eeSKentaro Takeda 		goto out;
1207b69a54eeSKentaro Takeda 	}
12087ef61233STetsuo Handa 	error = tomoyo_path_permission2(domain, TOMOYO_TYPE_REWRITE, buf, mode);
1209b69a54eeSKentaro Takeda  out:
12108e2d39a1STetsuo Handa 	kfree(buf);
1211fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1212b69a54eeSKentaro Takeda 	if (!is_enforce)
1213b69a54eeSKentaro Takeda 		error = 0;
1214b69a54eeSKentaro Takeda 	return error;
1215b69a54eeSKentaro Takeda }
1216b69a54eeSKentaro Takeda 
1217b69a54eeSKentaro Takeda /**
12187ef61233STetsuo Handa  * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
1219b69a54eeSKentaro Takeda  *
1220b69a54eeSKentaro Takeda  * @domain:    Pointer to "struct tomoyo_domain_info".
1221b69a54eeSKentaro Takeda  * @operation: Type of operation.
1222b69a54eeSKentaro Takeda  * @path1:      Pointer to "struct path".
1223b69a54eeSKentaro Takeda  * @path2:      Pointer to "struct path".
1224b69a54eeSKentaro Takeda  *
1225b69a54eeSKentaro Takeda  * Returns 0 on success, negative value otherwise.
1226b69a54eeSKentaro Takeda  */
12277ef61233STetsuo Handa int tomoyo_path2_perm(struct tomoyo_domain_info * const domain,
1228b69a54eeSKentaro Takeda 		      const u8 operation, struct path *path1,
1229b69a54eeSKentaro Takeda 		      struct path *path2)
1230b69a54eeSKentaro Takeda {
1231b69a54eeSKentaro Takeda 	int error = -ENOMEM;
1232b69a54eeSKentaro Takeda 	struct tomoyo_path_info *buf1, *buf2;
1233b69a54eeSKentaro Takeda 	const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
1234b69a54eeSKentaro Takeda 	const bool is_enforce = (mode == 3);
1235b69a54eeSKentaro Takeda 	const char *msg;
1236fdb8ebb7STetsuo Handa 	int idx;
1237b69a54eeSKentaro Takeda 
1238b69a54eeSKentaro Takeda 	if (!mode || !path1->mnt || !path2->mnt)
1239b69a54eeSKentaro Takeda 		return 0;
1240fdb8ebb7STetsuo Handa 	idx = tomoyo_read_lock();
1241b69a54eeSKentaro Takeda 	buf1 = tomoyo_get_path(path1);
1242b69a54eeSKentaro Takeda 	buf2 = tomoyo_get_path(path2);
1243b69a54eeSKentaro Takeda 	if (!buf1 || !buf2)
1244b69a54eeSKentaro Takeda 		goto out;
1245b69a54eeSKentaro Takeda 	{
1246b69a54eeSKentaro Takeda 		struct dentry *dentry = path1->dentry;
1247b69a54eeSKentaro Takeda 		if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
1248b69a54eeSKentaro Takeda 			/*
1249b69a54eeSKentaro Takeda 			 * tomoyo_get_path() reserves space for appending "/."
1250b69a54eeSKentaro Takeda 			 */
1251b69a54eeSKentaro Takeda 			if (!buf1->is_dir) {
1252b69a54eeSKentaro Takeda 				strcat((char *) buf1->name, "/");
1253b69a54eeSKentaro Takeda 				tomoyo_fill_path_info(buf1);
1254b69a54eeSKentaro Takeda 			}
1255b69a54eeSKentaro Takeda 			if (!buf2->is_dir) {
1256b69a54eeSKentaro Takeda 				strcat((char *) buf2->name, "/");
1257b69a54eeSKentaro Takeda 				tomoyo_fill_path_info(buf2);
1258b69a54eeSKentaro Takeda 			}
1259b69a54eeSKentaro Takeda 		}
1260b69a54eeSKentaro Takeda 	}
12617ef61233STetsuo Handa 	error = tomoyo_path2_acl(domain, operation, buf1, buf2);
12627ef61233STetsuo Handa 	msg = tomoyo_path22keyword(operation);
1263b69a54eeSKentaro Takeda 	if (!error)
1264b69a54eeSKentaro Takeda 		goto out;
1265b69a54eeSKentaro Takeda 	if (tomoyo_verbose_mode(domain))
1266b69a54eeSKentaro Takeda 		printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' "
1267b69a54eeSKentaro Takeda 		       "denied for %s\n", tomoyo_get_msg(is_enforce),
1268b69a54eeSKentaro Takeda 		       msg, buf1->name, buf2->name,
1269b69a54eeSKentaro Takeda 		       tomoyo_get_last_name(domain));
1270b69a54eeSKentaro Takeda 	if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
1271b69a54eeSKentaro Takeda 		const char *name1 = tomoyo_get_file_pattern(buf1)->name;
1272b69a54eeSKentaro Takeda 		const char *name2 = tomoyo_get_file_pattern(buf2)->name;
12737ef61233STetsuo Handa 		tomoyo_update_path2_acl(operation, name1, name2, domain,
1274b69a54eeSKentaro Takeda 					false);
1275b69a54eeSKentaro Takeda 	}
1276b69a54eeSKentaro Takeda  out:
12778e2d39a1STetsuo Handa 	kfree(buf1);
12788e2d39a1STetsuo Handa 	kfree(buf2);
1279fdb8ebb7STetsuo Handa 	tomoyo_read_unlock(idx);
1280b69a54eeSKentaro Takeda 	if (!is_enforce)
1281b69a54eeSKentaro Takeda 		error = 0;
1282b69a54eeSKentaro Takeda 	return error;
1283b69a54eeSKentaro Takeda }
1284