blob: 768dff9608b1def07757a24baefa3884e5040564 [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);
24 return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
25}
26
27/**
28 * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
29 *
30 * @file: Pointer to "struct file".
31 * @buf: Domainname to transit to.
32 * @count: Size of @buf.
33 * @ppos: Unused.
34 *
35 * Returns @count on success, negative value otherwise.
36 *
37 * If domain transition was permitted but the domain transition failed, this
38 * function returns error rather than terminating current thread with SIGKILL.
39 */
40static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
41 size_t count, loff_t *ppos)
42{
43 char *data;
44 int error;
45 if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
46 return -ENOMEM;
Al Viro16e5c1f2015-12-24 00:06:05 -050047 data = memdup_user_nul(buf, count);
48 if (IS_ERR(data))
49 return PTR_ERR(data);
Tetsuo Handa731d37a2011-09-10 15:25:58 +090050 tomoyo_normalize_line(data);
51 if (tomoyo_correct_domain(data)) {
52 const int idx = tomoyo_read_lock();
53 struct tomoyo_path_info name;
54 struct tomoyo_request_info r;
55 name.name = data;
56 tomoyo_fill_path_info(&name);
57 /* Check "task manual_domain_transition" permission. */
58 tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
59 r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
60 r.param.task.domainname = &name;
61 tomoyo_check_acl(&r, tomoyo_check_task_acl);
62 if (!r.granted)
63 error = -EPERM;
64 else {
65 struct tomoyo_domain_info *new_domain =
66 tomoyo_assign_domain(data, true);
67 if (!new_domain) {
68 error = -ENOENT;
69 } else {
70 struct cred *cred = prepare_creds();
71 if (!cred) {
72 error = -ENOMEM;
73 } else {
Casey Schaufler43fc4602018-09-21 17:18:07 -070074 struct tomoyo_domain_info **blob;
75 struct tomoyo_domain_info *old_domain;
76
77 blob = tomoyo_cred(cred);
78 old_domain = *blob;
79 *blob = new_domain;
Tetsuo Handa731d37a2011-09-10 15:25:58 +090080 atomic_inc(&new_domain->users);
81 atomic_dec(&old_domain->users);
82 commit_creds(cred);
83 error = 0;
84 }
85 }
86 }
87 tomoyo_read_unlock(idx);
88 } else
89 error = -EINVAL;
Tetsuo Handa731d37a2011-09-10 15:25:58 +090090 kfree(data);
91 return error ? error : count;
92}
93
94/**
95 * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
96 *
97 * @file: Pointer to "struct file".
98 * @buf: Domainname which current thread belongs to.
99 * @count: Size of @buf.
100 * @ppos: Bytes read by now.
101 *
102 * Returns read size on success, negative value otherwise.
103 */
104static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
106{
107 const char *domain = tomoyo_domain()->domainname->name;
108 loff_t len = strlen(domain);
109 loff_t pos = *ppos;
110 if (pos >= len || !count)
111 return 0;
112 len -= pos;
113 if (count < len)
114 len = count;
115 if (copy_to_user(buf, domain + pos, len))
116 return -EFAULT;
117 *ppos += len;
118 return len;
119}
120
121/* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
122static const struct file_operations tomoyo_self_operations = {
123 .write = tomoyo_write_self,
124 .read = tomoyo_read_self,
125};
126
127/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900128 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
129 *
130 * @inode: Pointer to "struct inode".
131 * @file: Pointer to "struct file".
132 *
133 * Returns 0 on success, negative value otherwise.
134 */
135static int tomoyo_open(struct inode *inode, struct file *file)
136{
Al Viro496ad9a2013-01-23 17:07:38 -0500137 const int key = ((u8 *) file_inode(file)->i_private)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900138 - ((u8 *) NULL);
139 return tomoyo_open_control(key, file);
140}
141
142/**
143 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
144 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900145 * @file: Pointer to "struct file".
146 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900147 */
148static int tomoyo_release(struct inode *inode, struct file *file)
149{
Al Viroe53cfda2013-04-14 16:59:00 -0400150 tomoyo_close_control(file->private_data);
151 return 0;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900152}
153
154/**
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900155 * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900156 *
157 * @file: Pointer to "struct file".
Tetsuo Handa6041e832012-03-14 18:27:49 +0900158 * @wait: Pointer to "poll_table". Maybe NULL.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900159 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800160 * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
161 * EPOLLOUT | EPOLLWRNORM otherwise.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900162 */
Al Viroc0d4be22017-07-02 23:32:02 -0400163static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900164{
165 return tomoyo_poll_control(file, wait);
166}
167
168/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900169 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
170 *
171 * @file: Pointer to "struct file".
172 * @buf: Pointer to buffer.
173 * @count: Size of @buf.
174 * @ppos: Unused.
175 *
176 * Returns bytes read on success, negative value otherwise.
177 */
178static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
179 loff_t *ppos)
180{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900181 return tomoyo_read_control(file->private_data, buf, count);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900182}
183
184/**
185 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
186 *
187 * @file: Pointer to "struct file".
188 * @buf: Pointer to buffer.
189 * @count: Size of @buf.
190 * @ppos: Unused.
191 *
192 * Returns @count on success, negative value otherwise.
193 */
194static ssize_t tomoyo_write(struct file *file, const char __user *buf,
195 size_t count, loff_t *ppos)
196{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900197 return tomoyo_write_control(file->private_data, buf, count);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900198}
199
200/*
201 * tomoyo_operations is a "struct file_operations" which is used for handling
202 * /sys/kernel/security/tomoyo/ interface.
203 *
204 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
205 * See tomoyo_io_buffer for internals.
206 */
207static const struct file_operations tomoyo_operations = {
208 .open = tomoyo_open,
209 .release = tomoyo_release,
Tetsuo Handa0849e3b2010-06-25 12:22:09 +0900210 .poll = tomoyo_poll,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900211 .read = tomoyo_read,
212 .write = tomoyo_write,
Tetsuo Handa7e2deb72010-07-08 21:57:41 +0900213 .llseek = noop_llseek,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900214};
215
216/**
217 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
218 *
219 * @name: The name of the interface file.
220 * @mode: The permission of the interface file.
221 * @parent: The parent directory.
222 * @key: Type of interface.
223 *
224 * Returns nothing.
225 */
Al Viro52ef0c02011-07-26 04:30:04 -0400226static void __init tomoyo_create_entry(const char *name, const umode_t mode,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900227 struct dentry *parent, const u8 key)
228{
229 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
230 &tomoyo_operations);
231}
232
233/**
234 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
235 *
236 * Returns 0.
237 */
238static int __init tomoyo_initerface_init(void)
239{
Casey Schaufler43fc4602018-09-21 17:18:07 -0700240 struct tomoyo_domain_info *domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900241 struct dentry *tomoyo_dir;
242
Casey Schaufler43fc4602018-09-21 17:18:07 -0700243 if (!tomoyo_enabled)
244 return 0;
245 domain = tomoyo_domain();
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900246 /* Don't create securityfs entries unless registered. */
Casey Schaufler43fc4602018-09-21 17:18:07 -0700247 if (domain != &tomoyo_kernel_domain)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900248 return 0;
249
250 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
251 tomoyo_create_entry("query", 0600, tomoyo_dir,
252 TOMOYO_QUERY);
253 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
254 TOMOYO_DOMAINPOLICY);
255 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
256 TOMOYO_EXCEPTIONPOLICY);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900257 tomoyo_create_entry("audit", 0400, tomoyo_dir,
258 TOMOYO_AUDIT);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900259 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
260 TOMOYO_PROCESS_STATUS);
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900261 tomoyo_create_entry("stat", 0644, tomoyo_dir,
262 TOMOYO_STAT);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900263 tomoyo_create_entry("profile", 0600, tomoyo_dir,
264 TOMOYO_PROFILE);
265 tomoyo_create_entry("manager", 0600, tomoyo_dir,
266 TOMOYO_MANAGER);
267 tomoyo_create_entry("version", 0400, tomoyo_dir,
268 TOMOYO_VERSION);
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900269 securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
270 &tomoyo_self_operations);
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900271 tomoyo_load_builtin_policy();
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900272 return 0;
273}
274
275fs_initcall(tomoyo_initerface_init);