1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c3ef1500STetsuo Handa /*
30f2a55d5STetsuo Handa * security/tomoyo/securityfs_if.c
4c3ef1500STetsuo Handa *
50f2a55d5STetsuo Handa * Copyright (C) 2005-2011 NTT DATA CORPORATION
6c3ef1500STetsuo Handa */
7c3ef1500STetsuo Handa
8c3ef1500STetsuo Handa #include <linux/security.h>
9c3ef1500STetsuo Handa #include "common.h"
10c3ef1500STetsuo Handa
11c3ef1500STetsuo Handa /**
12731d37aaSTetsuo Handa * tomoyo_check_task_acl - Check permission for task operation.
13731d37aaSTetsuo Handa *
14731d37aaSTetsuo Handa * @r: Pointer to "struct tomoyo_request_info".
15731d37aaSTetsuo Handa * @ptr: Pointer to "struct tomoyo_acl_info".
16731d37aaSTetsuo Handa *
17731d37aaSTetsuo Handa * Returns true if granted, false otherwise.
18731d37aaSTetsuo Handa */
tomoyo_check_task_acl(struct tomoyo_request_info * r,const struct tomoyo_acl_info * ptr)19731d37aaSTetsuo Handa static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
20731d37aaSTetsuo Handa const struct tomoyo_acl_info *ptr)
21731d37aaSTetsuo Handa {
22731d37aaSTetsuo Handa const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
23731d37aaSTetsuo Handa head);
24cdcf6723STetsuo Handa
25731d37aaSTetsuo Handa return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
26731d37aaSTetsuo Handa }
27731d37aaSTetsuo Handa
28731d37aaSTetsuo Handa /**
29731d37aaSTetsuo Handa * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
30731d37aaSTetsuo Handa *
31731d37aaSTetsuo Handa * @file: Pointer to "struct file".
32731d37aaSTetsuo Handa * @buf: Domainname to transit to.
33731d37aaSTetsuo Handa * @count: Size of @buf.
34731d37aaSTetsuo Handa * @ppos: Unused.
35731d37aaSTetsuo Handa *
36731d37aaSTetsuo Handa * Returns @count on success, negative value otherwise.
37731d37aaSTetsuo Handa *
38731d37aaSTetsuo Handa * If domain transition was permitted but the domain transition failed, this
39731d37aaSTetsuo Handa * function returns error rather than terminating current thread with SIGKILL.
40731d37aaSTetsuo Handa */
tomoyo_write_self(struct file * file,const char __user * buf,size_t count,loff_t * ppos)41731d37aaSTetsuo Handa static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
42731d37aaSTetsuo Handa size_t count, loff_t *ppos)
43731d37aaSTetsuo Handa {
44731d37aaSTetsuo Handa char *data;
45731d37aaSTetsuo Handa int error;
46cdcf6723STetsuo Handa
47731d37aaSTetsuo Handa if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
48731d37aaSTetsuo Handa return -ENOMEM;
4916e5c1fcSAl Viro data = memdup_user_nul(buf, count);
5016e5c1fcSAl Viro if (IS_ERR(data))
5116e5c1fcSAl Viro return PTR_ERR(data);
52731d37aaSTetsuo Handa tomoyo_normalize_line(data);
53731d37aaSTetsuo Handa if (tomoyo_correct_domain(data)) {
54731d37aaSTetsuo Handa const int idx = tomoyo_read_lock();
55731d37aaSTetsuo Handa struct tomoyo_path_info name;
56731d37aaSTetsuo Handa struct tomoyo_request_info r;
57cdcf6723STetsuo Handa
58731d37aaSTetsuo Handa name.name = data;
59731d37aaSTetsuo Handa tomoyo_fill_path_info(&name);
60731d37aaSTetsuo Handa /* Check "task manual_domain_transition" permission. */
61731d37aaSTetsuo Handa tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
62731d37aaSTetsuo Handa r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
63731d37aaSTetsuo Handa r.param.task.domainname = &name;
64731d37aaSTetsuo Handa tomoyo_check_acl(&r, tomoyo_check_task_acl);
65731d37aaSTetsuo Handa if (!r.granted)
66731d37aaSTetsuo Handa error = -EPERM;
67731d37aaSTetsuo Handa else {
68731d37aaSTetsuo Handa struct tomoyo_domain_info *new_domain =
69731d37aaSTetsuo Handa tomoyo_assign_domain(data, true);
70731d37aaSTetsuo Handa if (!new_domain) {
71731d37aaSTetsuo Handa error = -ENOENT;
72731d37aaSTetsuo Handa } else {
738c6cb983STetsuo Handa struct tomoyo_task *s = tomoyo_task(current);
748c6cb983STetsuo Handa struct tomoyo_domain_info *old_domain =
758c6cb983STetsuo Handa s->domain_info;
7643fc4609SCasey Schaufler
778c6cb983STetsuo Handa s->domain_info = new_domain;
78731d37aaSTetsuo Handa atomic_inc(&new_domain->users);
79731d37aaSTetsuo Handa atomic_dec(&old_domain->users);
80731d37aaSTetsuo Handa error = 0;
81731d37aaSTetsuo Handa }
82731d37aaSTetsuo Handa }
83731d37aaSTetsuo Handa tomoyo_read_unlock(idx);
84731d37aaSTetsuo Handa } else
85731d37aaSTetsuo Handa error = -EINVAL;
86731d37aaSTetsuo Handa kfree(data);
87731d37aaSTetsuo Handa return error ? error : count;
88731d37aaSTetsuo Handa }
89731d37aaSTetsuo Handa
90731d37aaSTetsuo Handa /**
91731d37aaSTetsuo Handa * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
92731d37aaSTetsuo Handa *
93731d37aaSTetsuo Handa * @file: Pointer to "struct file".
94731d37aaSTetsuo Handa * @buf: Domainname which current thread belongs to.
95731d37aaSTetsuo Handa * @count: Size of @buf.
96731d37aaSTetsuo Handa * @ppos: Bytes read by now.
97731d37aaSTetsuo Handa *
98731d37aaSTetsuo Handa * Returns read size on success, negative value otherwise.
99731d37aaSTetsuo Handa */
tomoyo_read_self(struct file * file,char __user * buf,size_t count,loff_t * ppos)100731d37aaSTetsuo Handa static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
101731d37aaSTetsuo Handa size_t count, loff_t *ppos)
102731d37aaSTetsuo Handa {
103731d37aaSTetsuo Handa const char *domain = tomoyo_domain()->domainname->name;
104731d37aaSTetsuo Handa loff_t len = strlen(domain);
105731d37aaSTetsuo Handa loff_t pos = *ppos;
106cdcf6723STetsuo Handa
107731d37aaSTetsuo Handa if (pos >= len || !count)
108731d37aaSTetsuo Handa return 0;
109731d37aaSTetsuo Handa len -= pos;
110731d37aaSTetsuo Handa if (count < len)
111731d37aaSTetsuo Handa len = count;
112731d37aaSTetsuo Handa if (copy_to_user(buf, domain + pos, len))
113731d37aaSTetsuo Handa return -EFAULT;
114731d37aaSTetsuo Handa *ppos += len;
115731d37aaSTetsuo Handa return len;
116731d37aaSTetsuo Handa }
117731d37aaSTetsuo Handa
118731d37aaSTetsuo Handa /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
119731d37aaSTetsuo Handa static const struct file_operations tomoyo_self_operations = {
120731d37aaSTetsuo Handa .write = tomoyo_write_self,
121731d37aaSTetsuo Handa .read = tomoyo_read_self,
122731d37aaSTetsuo Handa };
123731d37aaSTetsuo Handa
124731d37aaSTetsuo Handa /**
125c3ef1500STetsuo Handa * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
126c3ef1500STetsuo Handa *
127c3ef1500STetsuo Handa * @inode: Pointer to "struct inode".
128c3ef1500STetsuo Handa * @file: Pointer to "struct file".
129c3ef1500STetsuo Handa *
130c3ef1500STetsuo Handa * Returns 0 on success, negative value otherwise.
131c3ef1500STetsuo Handa */
tomoyo_open(struct inode * inode,struct file * file)132c3ef1500STetsuo Handa static int tomoyo_open(struct inode *inode, struct file *file)
133c3ef1500STetsuo Handa {
134d9594e04SArnd Bergmann const u8 key = (uintptr_t) file_inode(file)->i_private;
135d9594e04SArnd Bergmann
136c3ef1500STetsuo Handa return tomoyo_open_control(key, file);
137c3ef1500STetsuo Handa }
138c3ef1500STetsuo Handa
139c3ef1500STetsuo Handa /**
140c3ef1500STetsuo Handa * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
141c3ef1500STetsuo Handa *
142*98eaa63eSChenXiaoSong * @inode: Pointer to "struct inode".
143c3ef1500STetsuo Handa * @file: Pointer to "struct file".
144c3ef1500STetsuo Handa *
145c3ef1500STetsuo Handa */
tomoyo_release(struct inode * inode,struct file * file)146c3ef1500STetsuo Handa static int tomoyo_release(struct inode *inode, struct file *file)
147c3ef1500STetsuo Handa {
148e53cfda5SAl Viro tomoyo_close_control(file->private_data);
149e53cfda5SAl Viro return 0;
150c3ef1500STetsuo Handa }
151c3ef1500STetsuo Handa
152c3ef1500STetsuo Handa /**
153b5bc60b4STetsuo Handa * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
1540849e3baSTetsuo Handa *
1550849e3baSTetsuo Handa * @file: Pointer to "struct file".
1566041e834STetsuo Handa * @wait: Pointer to "poll_table". Maybe NULL.
1570849e3baSTetsuo Handa *
158a9a08845SLinus Torvalds * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
159a9a08845SLinus Torvalds * EPOLLOUT | EPOLLWRNORM otherwise.
1600849e3baSTetsuo Handa */
tomoyo_poll(struct file * file,poll_table * wait)161c0d4be28SAl Viro static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
1620849e3baSTetsuo Handa {
1630849e3baSTetsuo Handa return tomoyo_poll_control(file, wait);
1640849e3baSTetsuo Handa }
1650849e3baSTetsuo Handa
1660849e3baSTetsuo Handa /**
167c3ef1500STetsuo Handa * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
168c3ef1500STetsuo Handa *
169c3ef1500STetsuo Handa * @file: Pointer to "struct file".
170c3ef1500STetsuo Handa * @buf: Pointer to buffer.
171c3ef1500STetsuo Handa * @count: Size of @buf.
172c3ef1500STetsuo Handa * @ppos: Unused.
173c3ef1500STetsuo Handa *
174c3ef1500STetsuo Handa * Returns bytes read on success, negative value otherwise.
175c3ef1500STetsuo Handa */
tomoyo_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)176c3ef1500STetsuo Handa static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
177c3ef1500STetsuo Handa loff_t *ppos)
178c3ef1500STetsuo Handa {
1790df7e8b8STetsuo Handa return tomoyo_read_control(file->private_data, buf, count);
180c3ef1500STetsuo Handa }
181c3ef1500STetsuo Handa
182c3ef1500STetsuo Handa /**
183c3ef1500STetsuo Handa * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
184c3ef1500STetsuo Handa *
185c3ef1500STetsuo Handa * @file: Pointer to "struct file".
186c3ef1500STetsuo Handa * @buf: Pointer to buffer.
187c3ef1500STetsuo Handa * @count: Size of @buf.
188c3ef1500STetsuo Handa * @ppos: Unused.
189c3ef1500STetsuo Handa *
190c3ef1500STetsuo Handa * Returns @count on success, negative value otherwise.
191c3ef1500STetsuo Handa */
tomoyo_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)192c3ef1500STetsuo Handa static ssize_t tomoyo_write(struct file *file, const char __user *buf,
193c3ef1500STetsuo Handa size_t count, loff_t *ppos)
194c3ef1500STetsuo Handa {
1950df7e8b8STetsuo Handa return tomoyo_write_control(file->private_data, buf, count);
196c3ef1500STetsuo Handa }
197c3ef1500STetsuo Handa
198c3ef1500STetsuo Handa /*
199c3ef1500STetsuo Handa * tomoyo_operations is a "struct file_operations" which is used for handling
200c3ef1500STetsuo Handa * /sys/kernel/security/tomoyo/ interface.
201c3ef1500STetsuo Handa *
202c3ef1500STetsuo Handa * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
203c3ef1500STetsuo Handa * See tomoyo_io_buffer for internals.
204c3ef1500STetsuo Handa */
205c3ef1500STetsuo Handa static const struct file_operations tomoyo_operations = {
206c3ef1500STetsuo Handa .open = tomoyo_open,
207c3ef1500STetsuo Handa .release = tomoyo_release,
2080849e3baSTetsuo Handa .poll = tomoyo_poll,
209c3ef1500STetsuo Handa .read = tomoyo_read,
210c3ef1500STetsuo Handa .write = tomoyo_write,
2117e2deb7cSTetsuo Handa .llseek = noop_llseek,
212c3ef1500STetsuo Handa };
213c3ef1500STetsuo Handa
214c3ef1500STetsuo Handa /**
215c3ef1500STetsuo Handa * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
216c3ef1500STetsuo Handa *
217c3ef1500STetsuo Handa * @name: The name of the interface file.
218c3ef1500STetsuo Handa * @mode: The permission of the interface file.
219c3ef1500STetsuo Handa * @parent: The parent directory.
220c3ef1500STetsuo Handa * @key: Type of interface.
221c3ef1500STetsuo Handa *
222c3ef1500STetsuo Handa * Returns nothing.
223c3ef1500STetsuo Handa */
tomoyo_create_entry(const char * name,const umode_t mode,struct dentry * parent,const u8 key)22452ef0c04SAl Viro static void __init tomoyo_create_entry(const char *name, const umode_t mode,
225c3ef1500STetsuo Handa struct dentry *parent, const u8 key)
226c3ef1500STetsuo Handa {
227d9594e04SArnd Bergmann securityfs_create_file(name, mode, parent, (void *) (uintptr_t) key,
228c3ef1500STetsuo Handa &tomoyo_operations);
229c3ef1500STetsuo Handa }
230c3ef1500STetsuo Handa
231c3ef1500STetsuo Handa /**
232c3ef1500STetsuo Handa * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
233c3ef1500STetsuo Handa *
234c3ef1500STetsuo Handa * Returns 0.
235c3ef1500STetsuo Handa */
tomoyo_initerface_init(void)236c3ef1500STetsuo Handa static int __init tomoyo_initerface_init(void)
237c3ef1500STetsuo Handa {
23843fc4609SCasey Schaufler struct tomoyo_domain_info *domain;
239c3ef1500STetsuo Handa struct dentry *tomoyo_dir;
240c3ef1500STetsuo Handa
24143fc4609SCasey Schaufler if (!tomoyo_enabled)
24243fc4609SCasey Schaufler return 0;
24343fc4609SCasey Schaufler domain = tomoyo_domain();
244c3ef1500STetsuo Handa /* Don't create securityfs entries unless registered. */
24543fc4609SCasey Schaufler if (domain != &tomoyo_kernel_domain)
246c3ef1500STetsuo Handa return 0;
247c3ef1500STetsuo Handa
248c3ef1500STetsuo Handa tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
249c3ef1500STetsuo Handa tomoyo_create_entry("query", 0600, tomoyo_dir,
250c3ef1500STetsuo Handa TOMOYO_QUERY);
251c3ef1500STetsuo Handa tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
252c3ef1500STetsuo Handa TOMOYO_DOMAINPOLICY);
253c3ef1500STetsuo Handa tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
254c3ef1500STetsuo Handa TOMOYO_EXCEPTIONPOLICY);
255eadd99ccSTetsuo Handa tomoyo_create_entry("audit", 0400, tomoyo_dir,
256eadd99ccSTetsuo Handa TOMOYO_AUDIT);
257c3ef1500STetsuo Handa tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
258c3ef1500STetsuo Handa TOMOYO_PROCESS_STATUS);
259b22b8b9fSTetsuo Handa tomoyo_create_entry("stat", 0644, tomoyo_dir,
260b22b8b9fSTetsuo Handa TOMOYO_STAT);
261c3ef1500STetsuo Handa tomoyo_create_entry("profile", 0600, tomoyo_dir,
262c3ef1500STetsuo Handa TOMOYO_PROFILE);
263c3ef1500STetsuo Handa tomoyo_create_entry("manager", 0600, tomoyo_dir,
264c3ef1500STetsuo Handa TOMOYO_MANAGER);
265c3ef1500STetsuo Handa tomoyo_create_entry("version", 0400, tomoyo_dir,
266c3ef1500STetsuo Handa TOMOYO_VERSION);
267731d37aaSTetsuo Handa securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
268731d37aaSTetsuo Handa &tomoyo_self_operations);
269778c4a4dSTetsuo Handa tomoyo_load_builtin_policy();
270c3ef1500STetsuo Handa return 0;
271c3ef1500STetsuo Handa }
272c3ef1500STetsuo Handa
273c3ef1500STetsuo Handa fs_initcall(tomoyo_initerface_init);
274