blob: 546281c5b233afb4a91968d524bffceeba9f96d4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002/*
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09003 * security/tomoyo/securityfs_if.c
Tetsuo Handac3ef1502010-05-17 10:12:46 +09004 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 */
7
8#include <linux/security.h>
9#include "common.h"
10
11/**
Tetsuo Handa731d37a2011-09-10 15:25:58 +090012 * tomoyo_check_task_acl - Check permission for task operation.
13 *
14 * @r: Pointer to "struct tomoyo_request_info".
15 * @ptr: Pointer to "struct tomoyo_acl_info".
16 *
17 * Returns true if granted, false otherwise.
18 */
19static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
20 const struct tomoyo_acl_info *ptr)
21{
22 const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
23 head);
Tetsuo Handacdcf6722019-01-24 18:37:35 +090024
Tetsuo Handa731d37a2011-09-10 15:25:58 +090025 return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
26}
27
28/**
29 * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
30 *
31 * @file: Pointer to "struct file".
32 * @buf: Domainname to transit to.
33 * @count: Size of @buf.
34 * @ppos: Unused.
35 *
36 * Returns @count on success, negative value otherwise.
37 *
38 * If domain transition was permitted but the domain transition failed, this
39 * function returns error rather than terminating current thread with SIGKILL.
40 */
41static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
42 size_t count, loff_t *ppos)
43{
44 char *data;
45 int error;
Tetsuo Handacdcf6722019-01-24 18:37:35 +090046
Tetsuo Handa731d37a2011-09-10 15:25:58 +090047 if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
48 return -ENOMEM;
Al Viro16e5c1f2015-12-24 00:06:05 -050049 data = memdup_user_nul(buf, count);
50 if (IS_ERR(data))
51 return PTR_ERR(data);
Tetsuo Handa731d37a2011-09-10 15:25:58 +090052 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;
Tetsuo Handacdcf6722019-01-24 18:37:35 +090057
Tetsuo Handa731d37a2011-09-10 15:25:58 +090058 name.name = data;
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);
65 if (!r.granted)
66 error = -EPERM;
67 else {
68 struct tomoyo_domain_info *new_domain =
69 tomoyo_assign_domain(data, true);
70 if (!new_domain) {
71 error = -ENOENT;
72 } else {
Tetsuo Handa8c6cb982019-01-19 23:11:40 +090073 struct tomoyo_task *s = tomoyo_task(current);
74 struct tomoyo_domain_info *old_domain =
75 s->domain_info;
Casey Schaufler43fc4602018-09-21 17:18:07 -070076
Tetsuo Handa8c6cb982019-01-19 23:11:40 +090077 s->domain_info = new_domain;
78 atomic_inc(&new_domain->users);
79 atomic_dec(&old_domain->users);
80 error = 0;
Tetsuo Handa731d37a2011-09-10 15:25:58 +090081 }
82 }
83 tomoyo_read_unlock(idx);
84 } else
85 error = -EINVAL;
Tetsuo Handa731d37a2011-09-10 15:25:58 +090086 kfree(data);
87 return error ? error : count;
88}
89
90/**
91 * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
92 *
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.
97 *
98 * Returns read size on success, negative value otherwise.
99 */
100static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
101 size_t count, loff_t *ppos)
102{
103 const char *domain = tomoyo_domain()->domainname->name;
104 loff_t len = strlen(domain);
105 loff_t pos = *ppos;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900106
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900107 if (pos >= len || !count)
108 return 0;
109 len -= pos;
110 if (count < len)
111 len = count;
112 if (copy_to_user(buf, domain + pos, len))
113 return -EFAULT;
114 *ppos += len;
115 return len;
116}
117
118/* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
119static const struct file_operations tomoyo_self_operations = {
120 .write = tomoyo_write_self,
121 .read = tomoyo_read_self,
122};
123
124/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900125 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
126 *
127 * @inode: Pointer to "struct inode".
128 * @file: Pointer to "struct file".
129 *
130 * Returns 0 on success, negative value otherwise.
131 */
132static int tomoyo_open(struct inode *inode, struct file *file)
133{
Al Viro496ad9a2013-01-23 17:07:38 -0500134 const int key = ((u8 *) file_inode(file)->i_private)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900135 - ((u8 *) NULL);
136 return tomoyo_open_control(key, file);
137}
138
139/**
140 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
141 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900142 * @file: Pointer to "struct file".
143 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900144 */
145static int tomoyo_release(struct inode *inode, struct file *file)
146{
Al Viroe53cfda2013-04-14 16:59:00 -0400147 tomoyo_close_control(file->private_data);
148 return 0;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900149}
150
151/**
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900152 * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900153 *
154 * @file: Pointer to "struct file".
Tetsuo Handa6041e832012-03-14 18:27:49 +0900155 * @wait: Pointer to "poll_table". Maybe NULL.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900156 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800157 * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
158 * EPOLLOUT | EPOLLWRNORM otherwise.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900159 */
Al Viroc0d4be22017-07-02 23:32:02 -0400160static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900161{
162 return tomoyo_poll_control(file, wait);
163}
164
165/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900166 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
167 *
168 * @file: Pointer to "struct file".
169 * @buf: Pointer to buffer.
170 * @count: Size of @buf.
171 * @ppos: Unused.
172 *
173 * Returns bytes read on success, negative value otherwise.
174 */
175static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
176 loff_t *ppos)
177{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900178 return tomoyo_read_control(file->private_data, buf, count);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900179}
180
181/**
182 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
183 *
184 * @file: Pointer to "struct file".
185 * @buf: Pointer to buffer.
186 * @count: Size of @buf.
187 * @ppos: Unused.
188 *
189 * Returns @count on success, negative value otherwise.
190 */
191static ssize_t tomoyo_write(struct file *file, const char __user *buf,
192 size_t count, loff_t *ppos)
193{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900194 return tomoyo_write_control(file->private_data, buf, count);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900195}
196
197/*
198 * tomoyo_operations is a "struct file_operations" which is used for handling
199 * /sys/kernel/security/tomoyo/ interface.
200 *
201 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
202 * See tomoyo_io_buffer for internals.
203 */
204static const struct file_operations tomoyo_operations = {
205 .open = tomoyo_open,
206 .release = tomoyo_release,
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900207 .poll = tomoyo_poll,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900208 .read = tomoyo_read,
209 .write = tomoyo_write,
Tetsuo Handa7e2deb72010-07-08 21:57:41 +0900210 .llseek = noop_llseek,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900211};
212
213/**
214 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
215 *
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.
220 *
221 * Returns nothing.
222 */
Al Viro52ef0c02011-07-26 04:30:04 -0400223static void __init tomoyo_create_entry(const char *name, const umode_t mode,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900224 struct dentry *parent, const u8 key)
225{
226 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
227 &tomoyo_operations);
228}
229
230/**
231 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
232 *
233 * Returns 0.
234 */
235static int __init tomoyo_initerface_init(void)
236{
Casey Schaufler43fc4602018-09-21 17:18:07 -0700237 struct tomoyo_domain_info *domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900238 struct dentry *tomoyo_dir;
239
Casey Schaufler43fc4602018-09-21 17:18:07 -0700240 if (!tomoyo_enabled)
241 return 0;
242 domain = tomoyo_domain();
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900243 /* Don't create securityfs entries unless registered. */
Casey Schaufler43fc4602018-09-21 17:18:07 -0700244 if (domain != &tomoyo_kernel_domain)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900245 return 0;
246
247 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
248 tomoyo_create_entry("query", 0600, tomoyo_dir,
249 TOMOYO_QUERY);
250 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
251 TOMOYO_DOMAINPOLICY);
252 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
253 TOMOYO_EXCEPTIONPOLICY);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900254 tomoyo_create_entry("audit", 0400, tomoyo_dir,
255 TOMOYO_AUDIT);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900256 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
257 TOMOYO_PROCESS_STATUS);
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900258 tomoyo_create_entry("stat", 0644, tomoyo_dir,
259 TOMOYO_STAT);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900260 tomoyo_create_entry("profile", 0600, tomoyo_dir,
261 TOMOYO_PROFILE);
262 tomoyo_create_entry("manager", 0600, tomoyo_dir,
263 TOMOYO_MANAGER);
264 tomoyo_create_entry("version", 0400, tomoyo_dir,
265 TOMOYO_VERSION);
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900266 securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
267 &tomoyo_self_operations);
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900268 tomoyo_load_builtin_policy();
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900269 return 0;
270}
271
272fs_initcall(tomoyo_initerface_init);