1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * security/tomoyo/securityfs_if.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2005-2011 NTT DATA CORPORATION
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/security.h>
9*4882a593Smuzhiyun #include "common.h"
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /**
12*4882a593Smuzhiyun * tomoyo_check_task_acl - Check permission for task operation.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * @r: Pointer to "struct tomoyo_request_info".
15*4882a593Smuzhiyun * @ptr: Pointer to "struct tomoyo_acl_info".
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Returns true if granted, false otherwise.
18*4882a593Smuzhiyun */
tomoyo_check_task_acl(struct tomoyo_request_info * r,const struct tomoyo_acl_info * ptr)19*4882a593Smuzhiyun static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
20*4882a593Smuzhiyun const struct tomoyo_acl_info *ptr)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
23*4882a593Smuzhiyun head);
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * @file: Pointer to "struct file".
32*4882a593Smuzhiyun * @buf: Domainname to transit to.
33*4882a593Smuzhiyun * @count: Size of @buf.
34*4882a593Smuzhiyun * @ppos: Unused.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * Returns @count on success, negative value otherwise.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * If domain transition was permitted but the domain transition failed, this
39*4882a593Smuzhiyun * function returns error rather than terminating current thread with SIGKILL.
40*4882a593Smuzhiyun */
tomoyo_write_self(struct file * file,const char __user * buf,size_t count,loff_t * ppos)41*4882a593Smuzhiyun static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
42*4882a593Smuzhiyun size_t count, loff_t *ppos)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun char *data;
45*4882a593Smuzhiyun int error;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
48*4882a593Smuzhiyun return -ENOMEM;
49*4882a593Smuzhiyun data = memdup_user_nul(buf, count);
50*4882a593Smuzhiyun if (IS_ERR(data))
51*4882a593Smuzhiyun return PTR_ERR(data);
52*4882a593Smuzhiyun tomoyo_normalize_line(data);
53*4882a593Smuzhiyun if (tomoyo_correct_domain(data)) {
54*4882a593Smuzhiyun const int idx = tomoyo_read_lock();
55*4882a593Smuzhiyun struct tomoyo_path_info name;
56*4882a593Smuzhiyun struct tomoyo_request_info r;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun name.name = data;
59*4882a593Smuzhiyun tomoyo_fill_path_info(&name);
60*4882a593Smuzhiyun /* Check "task manual_domain_transition" permission. */
61*4882a593Smuzhiyun tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
62*4882a593Smuzhiyun r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
63*4882a593Smuzhiyun r.param.task.domainname = &name;
64*4882a593Smuzhiyun tomoyo_check_acl(&r, tomoyo_check_task_acl);
65*4882a593Smuzhiyun if (!r.granted)
66*4882a593Smuzhiyun error = -EPERM;
67*4882a593Smuzhiyun else {
68*4882a593Smuzhiyun struct tomoyo_domain_info *new_domain =
69*4882a593Smuzhiyun tomoyo_assign_domain(data, true);
70*4882a593Smuzhiyun if (!new_domain) {
71*4882a593Smuzhiyun error = -ENOENT;
72*4882a593Smuzhiyun } else {
73*4882a593Smuzhiyun struct tomoyo_task *s = tomoyo_task(current);
74*4882a593Smuzhiyun struct tomoyo_domain_info *old_domain =
75*4882a593Smuzhiyun s->domain_info;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun s->domain_info = new_domain;
78*4882a593Smuzhiyun atomic_inc(&new_domain->users);
79*4882a593Smuzhiyun atomic_dec(&old_domain->users);
80*4882a593Smuzhiyun error = 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun tomoyo_read_unlock(idx);
84*4882a593Smuzhiyun } else
85*4882a593Smuzhiyun error = -EINVAL;
86*4882a593Smuzhiyun kfree(data);
87*4882a593Smuzhiyun return error ? error : count;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /**
91*4882a593Smuzhiyun * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * @file: Pointer to "struct file".
94*4882a593Smuzhiyun * @buf: Domainname which current thread belongs to.
95*4882a593Smuzhiyun * @count: Size of @buf.
96*4882a593Smuzhiyun * @ppos: Bytes read by now.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Returns read size on success, negative value otherwise.
99*4882a593Smuzhiyun */
tomoyo_read_self(struct file * file,char __user * buf,size_t count,loff_t * ppos)100*4882a593Smuzhiyun static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
101*4882a593Smuzhiyun size_t count, loff_t *ppos)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun const char *domain = tomoyo_domain()->domainname->name;
104*4882a593Smuzhiyun loff_t len = strlen(domain);
105*4882a593Smuzhiyun loff_t pos = *ppos;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (pos >= len || !count)
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun len -= pos;
110*4882a593Smuzhiyun if (count < len)
111*4882a593Smuzhiyun len = count;
112*4882a593Smuzhiyun if (copy_to_user(buf, domain + pos, len))
113*4882a593Smuzhiyun return -EFAULT;
114*4882a593Smuzhiyun *ppos += len;
115*4882a593Smuzhiyun return len;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
119*4882a593Smuzhiyun static const struct file_operations tomoyo_self_operations = {
120*4882a593Smuzhiyun .write = tomoyo_write_self,
121*4882a593Smuzhiyun .read = tomoyo_read_self,
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /**
125*4882a593Smuzhiyun * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * @inode: Pointer to "struct inode".
128*4882a593Smuzhiyun * @file: Pointer to "struct file".
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * Returns 0 on success, negative value otherwise.
131*4882a593Smuzhiyun */
tomoyo_open(struct inode * inode,struct file * file)132*4882a593Smuzhiyun static int tomoyo_open(struct inode *inode, struct file *file)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun const int key = ((u8 *) file_inode(file)->i_private)
135*4882a593Smuzhiyun - ((u8 *) NULL);
136*4882a593Smuzhiyun return tomoyo_open_control(key, file);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * @file: Pointer to "struct file".
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun */
tomoyo_release(struct inode * inode,struct file * file)145*4882a593Smuzhiyun static int tomoyo_release(struct inode *inode, struct file *file)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun tomoyo_close_control(file->private_data);
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * @file: Pointer to "struct file".
155*4882a593Smuzhiyun * @wait: Pointer to "poll_table". Maybe NULL.
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
158*4882a593Smuzhiyun * EPOLLOUT | EPOLLWRNORM otherwise.
159*4882a593Smuzhiyun */
tomoyo_poll(struct file * file,poll_table * wait)160*4882a593Smuzhiyun static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun return tomoyo_poll_control(file, wait);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /**
166*4882a593Smuzhiyun * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * @file: Pointer to "struct file".
169*4882a593Smuzhiyun * @buf: Pointer to buffer.
170*4882a593Smuzhiyun * @count: Size of @buf.
171*4882a593Smuzhiyun * @ppos: Unused.
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Returns bytes read on success, negative value otherwise.
174*4882a593Smuzhiyun */
tomoyo_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)175*4882a593Smuzhiyun static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
176*4882a593Smuzhiyun loff_t *ppos)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun return tomoyo_read_control(file->private_data, buf, count);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
183*4882a593Smuzhiyun *
184*4882a593Smuzhiyun * @file: Pointer to "struct file".
185*4882a593Smuzhiyun * @buf: Pointer to buffer.
186*4882a593Smuzhiyun * @count: Size of @buf.
187*4882a593Smuzhiyun * @ppos: Unused.
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * Returns @count on success, negative value otherwise.
190*4882a593Smuzhiyun */
tomoyo_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)191*4882a593Smuzhiyun static ssize_t tomoyo_write(struct file *file, const char __user *buf,
192*4882a593Smuzhiyun size_t count, loff_t *ppos)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun return tomoyo_write_control(file->private_data, buf, count);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun * tomoyo_operations is a "struct file_operations" which is used for handling
199*4882a593Smuzhiyun * /sys/kernel/security/tomoyo/ interface.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
202*4882a593Smuzhiyun * See tomoyo_io_buffer for internals.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun static const struct file_operations tomoyo_operations = {
205*4882a593Smuzhiyun .open = tomoyo_open,
206*4882a593Smuzhiyun .release = tomoyo_release,
207*4882a593Smuzhiyun .poll = tomoyo_poll,
208*4882a593Smuzhiyun .read = tomoyo_read,
209*4882a593Smuzhiyun .write = tomoyo_write,
210*4882a593Smuzhiyun .llseek = noop_llseek,
211*4882a593Smuzhiyun };
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /**
214*4882a593Smuzhiyun * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * @name: The name of the interface file.
217*4882a593Smuzhiyun * @mode: The permission of the interface file.
218*4882a593Smuzhiyun * @parent: The parent directory.
219*4882a593Smuzhiyun * @key: Type of interface.
220*4882a593Smuzhiyun *
221*4882a593Smuzhiyun * Returns nothing.
222*4882a593Smuzhiyun */
tomoyo_create_entry(const char * name,const umode_t mode,struct dentry * parent,const u8 key)223*4882a593Smuzhiyun static void __init tomoyo_create_entry(const char *name, const umode_t mode,
224*4882a593Smuzhiyun struct dentry *parent, const u8 key)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
227*4882a593Smuzhiyun &tomoyo_operations);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * Returns 0.
234*4882a593Smuzhiyun */
tomoyo_initerface_init(void)235*4882a593Smuzhiyun static int __init tomoyo_initerface_init(void)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun struct tomoyo_domain_info *domain;
238*4882a593Smuzhiyun struct dentry *tomoyo_dir;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (!tomoyo_enabled)
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun domain = tomoyo_domain();
243*4882a593Smuzhiyun /* Don't create securityfs entries unless registered. */
244*4882a593Smuzhiyun if (domain != &tomoyo_kernel_domain)
245*4882a593Smuzhiyun return 0;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
248*4882a593Smuzhiyun tomoyo_create_entry("query", 0600, tomoyo_dir,
249*4882a593Smuzhiyun TOMOYO_QUERY);
250*4882a593Smuzhiyun tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
251*4882a593Smuzhiyun TOMOYO_DOMAINPOLICY);
252*4882a593Smuzhiyun tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
253*4882a593Smuzhiyun TOMOYO_EXCEPTIONPOLICY);
254*4882a593Smuzhiyun tomoyo_create_entry("audit", 0400, tomoyo_dir,
255*4882a593Smuzhiyun TOMOYO_AUDIT);
256*4882a593Smuzhiyun tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
257*4882a593Smuzhiyun TOMOYO_PROCESS_STATUS);
258*4882a593Smuzhiyun tomoyo_create_entry("stat", 0644, tomoyo_dir,
259*4882a593Smuzhiyun TOMOYO_STAT);
260*4882a593Smuzhiyun tomoyo_create_entry("profile", 0600, tomoyo_dir,
261*4882a593Smuzhiyun TOMOYO_PROFILE);
262*4882a593Smuzhiyun tomoyo_create_entry("manager", 0600, tomoyo_dir,
263*4882a593Smuzhiyun TOMOYO_MANAGER);
264*4882a593Smuzhiyun tomoyo_create_entry("version", 0400, tomoyo_dir,
265*4882a593Smuzhiyun TOMOYO_VERSION);
266*4882a593Smuzhiyun securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
267*4882a593Smuzhiyun &tomoyo_self_operations);
268*4882a593Smuzhiyun tomoyo_load_builtin_policy();
269*4882a593Smuzhiyun return 0;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun fs_initcall(tomoyo_initerface_init);
273