1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/securityfs_if.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/security.h>
12 * tomoyo_check_task_acl - Check permission for task operation.
14 * @r: Pointer to "struct tomoyo_request_info".
15 * @ptr: Pointer to "struct tomoyo_acl_info".
17 * Returns true if granted, false otherwise.
19 static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
20 const struct tomoyo_acl_info *ptr)
22 const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
25 return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
29 * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
31 * @file: Pointer to "struct file".
32 * @buf: Domainname to transit to.
33 * @count: Size of @buf.
36 * Returns @count on success, negative value otherwise.
38 * If domain transition was permitted but the domain transition failed, this
39 * function returns error rather than terminating current thread with SIGKILL.
41 static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
42 size_t count, loff_t *ppos)
47 if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
49 data = memdup_user_nul(buf, count);
52 tomoyo_normalize_line(data);
53 if (tomoyo_correct_domain(data)) {
54 const int idx = tomoyo_read_lock();
55 struct tomoyo_path_info name;
56 struct tomoyo_request_info r;
59 tomoyo_fill_path_info(&name);
60 /* Check "task manual_domain_transition" permission. */
61 tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
62 r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
63 r.param.task.domainname = &name;
64 tomoyo_check_acl(&r, tomoyo_check_task_acl);
68 struct tomoyo_domain_info *new_domain =
69 tomoyo_assign_domain(data, true);
73 struct tomoyo_task *s = tomoyo_task(current);
74 struct tomoyo_domain_info *old_domain =
77 s->domain_info = new_domain;
78 atomic_inc(&new_domain->users);
79 atomic_dec(&old_domain->users);
83 tomoyo_read_unlock(idx);
87 return error ? error : count;
91 * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
93 * @file: Pointer to "struct file".
94 * @buf: Domainname which current thread belongs to.
95 * @count: Size of @buf.
96 * @ppos: Bytes read by now.
98 * Returns read size on success, negative value otherwise.
100 static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
101 size_t count, loff_t *ppos)
103 const char *domain = tomoyo_domain()->domainname->name;
104 loff_t len = strlen(domain);
107 if (pos >= len || !count)
112 if (copy_to_user(buf, domain + pos, len))
118 /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
119 static const struct file_operations tomoyo_self_operations = {
120 .write = tomoyo_write_self,
121 .read = tomoyo_read_self,
125 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
127 * @inode: Pointer to "struct inode".
128 * @file: Pointer to "struct file".
130 * Returns 0 on success, negative value otherwise.
132 static int tomoyo_open(struct inode *inode, struct file *file)
134 const int key = ((u8 *) file_inode(file)->i_private)
136 return tomoyo_open_control(key, file);
140 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
142 * @file: Pointer to "struct file".
145 static int tomoyo_release(struct inode *inode, struct file *file)
147 tomoyo_close_control(file->private_data);
152 * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
154 * @file: Pointer to "struct file".
155 * @wait: Pointer to "poll_table". Maybe NULL.
157 * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
158 * EPOLLOUT | EPOLLWRNORM otherwise.
160 static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
162 return tomoyo_poll_control(file, wait);
166 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
168 * @file: Pointer to "struct file".
169 * @buf: Pointer to buffer.
170 * @count: Size of @buf.
173 * Returns bytes read on success, negative value otherwise.
175 static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
178 return tomoyo_read_control(file->private_data, buf, count);
182 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
184 * @file: Pointer to "struct file".
185 * @buf: Pointer to buffer.
186 * @count: Size of @buf.
189 * Returns @count on success, negative value otherwise.
191 static ssize_t tomoyo_write(struct file *file, const char __user *buf,
192 size_t count, loff_t *ppos)
194 return tomoyo_write_control(file->private_data, buf, count);
198 * tomoyo_operations is a "struct file_operations" which is used for handling
199 * /sys/kernel/security/tomoyo/ interface.
201 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
202 * See tomoyo_io_buffer for internals.
204 static const struct file_operations tomoyo_operations = {
206 .release = tomoyo_release,
209 .write = tomoyo_write,
210 .llseek = noop_llseek,
214 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
216 * @name: The name of the interface file.
217 * @mode: The permission of the interface file.
218 * @parent: The parent directory.
219 * @key: Type of interface.
223 static void __init tomoyo_create_entry(const char *name, const umode_t mode,
224 struct dentry *parent, const u8 key)
226 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
231 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
235 static int __init tomoyo_initerface_init(void)
237 struct tomoyo_domain_info *domain;
238 struct dentry *tomoyo_dir;
242 domain = tomoyo_domain();
243 /* Don't create securityfs entries unless registered. */
244 if (domain != &tomoyo_kernel_domain)
247 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
248 tomoyo_create_entry("query", 0600, tomoyo_dir,
250 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
251 TOMOYO_DOMAINPOLICY);
252 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
253 TOMOYO_EXCEPTIONPOLICY);
254 tomoyo_create_entry("audit", 0400, tomoyo_dir,
256 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
257 TOMOYO_PROCESS_STATUS);
258 tomoyo_create_entry("stat", 0644, tomoyo_dir,
260 tomoyo_create_entry("profile", 0600, tomoyo_dir,
262 tomoyo_create_entry("manager", 0600, tomoyo_dir,
264 tomoyo_create_entry("version", 0400, tomoyo_dir,
266 securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
267 &tomoyo_self_operations);
268 tomoyo_load_builtin_policy();
272 fs_initcall(tomoyo_initerface_init);