]> asedeno.scripts.mit.edu Git - linux.git/blob - security/tomoyo/securityfs_if.c
tomoyo: Swicth from cred->security to task_struct->security.
[linux.git] / security / tomoyo / securityfs_if.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * security/tomoyo/securityfs_if.c
4  *
5  * Copyright (C) 2005-2011  NTT DATA CORPORATION
6  */
7
8 #include <linux/security.h>
9 #include "common.h"
10
11 /**
12  * 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  */
19 static 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  */
40 static 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;
47         data = memdup_user_nul(buf, count);
48         if (IS_ERR(data))
49                 return PTR_ERR(data);
50         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 tomoyo_task *s = tomoyo_task(current);
71                                 struct tomoyo_domain_info *old_domain =
72                                         s->domain_info;
73
74                                 s->domain_info = new_domain;
75                                 atomic_inc(&new_domain->users);
76                                 atomic_dec(&old_domain->users);
77                                 error = 0;
78                         }
79                 }
80                 tomoyo_read_unlock(idx);
81         } else
82                 error = -EINVAL;
83         kfree(data);
84         return error ? error : count;
85 }
86
87 /**
88  * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
89  *
90  * @file:  Pointer to "struct file".
91  * @buf:   Domainname which current thread belongs to.
92  * @count: Size of @buf.
93  * @ppos:  Bytes read by now.
94  *
95  * Returns read size on success, negative value otherwise.
96  */
97 static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
98                                 size_t count, loff_t *ppos)
99 {
100         const char *domain = tomoyo_domain()->domainname->name;
101         loff_t len = strlen(domain);
102         loff_t pos = *ppos;
103         if (pos >= len || !count)
104                 return 0;
105         len -= pos;
106         if (count < len)
107                 len = count;
108         if (copy_to_user(buf, domain + pos, len))
109                 return -EFAULT;
110         *ppos += len;
111         return len;
112 }
113
114 /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
115 static const struct file_operations tomoyo_self_operations = {
116         .write = tomoyo_write_self,
117         .read  = tomoyo_read_self,
118 };
119
120 /**
121  * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
122  *
123  * @inode: Pointer to "struct inode".
124  * @file:  Pointer to "struct file".
125  *
126  * Returns 0 on success, negative value otherwise.
127  */
128 static int tomoyo_open(struct inode *inode, struct file *file)
129 {
130         const int key = ((u8 *) file_inode(file)->i_private)
131                 - ((u8 *) NULL);
132         return tomoyo_open_control(key, file);
133 }
134
135 /**
136  * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
137  *
138  * @file:  Pointer to "struct file".
139  *
140  */
141 static int tomoyo_release(struct inode *inode, struct file *file)
142 {
143         tomoyo_close_control(file->private_data);
144         return 0;
145 }
146
147 /**
148  * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
149  *
150  * @file: Pointer to "struct file".
151  * @wait: Pointer to "poll_table". Maybe NULL.
152  *
153  * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write,
154  * EPOLLOUT | EPOLLWRNORM otherwise.
155  */
156 static __poll_t tomoyo_poll(struct file *file, poll_table *wait)
157 {
158         return tomoyo_poll_control(file, wait);
159 }
160
161 /**
162  * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
163  *
164  * @file:  Pointer to "struct file".
165  * @buf:   Pointer to buffer.
166  * @count: Size of @buf.
167  * @ppos:  Unused.
168  *
169  * Returns bytes read on success, negative value otherwise.
170  */
171 static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
172                            loff_t *ppos)
173 {
174         return tomoyo_read_control(file->private_data, buf, count);
175 }
176
177 /**
178  * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
179  *
180  * @file:  Pointer to "struct file".
181  * @buf:   Pointer to buffer.
182  * @count: Size of @buf.
183  * @ppos:  Unused.
184  *
185  * Returns @count on success, negative value otherwise.
186  */
187 static ssize_t tomoyo_write(struct file *file, const char __user *buf,
188                             size_t count, loff_t *ppos)
189 {
190         return tomoyo_write_control(file->private_data, buf, count);
191 }
192
193 /*
194  * tomoyo_operations is a "struct file_operations" which is used for handling
195  * /sys/kernel/security/tomoyo/ interface.
196  *
197  * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
198  * See tomoyo_io_buffer for internals.
199  */
200 static const struct file_operations tomoyo_operations = {
201         .open    = tomoyo_open,
202         .release = tomoyo_release,
203         .poll    = tomoyo_poll,
204         .read    = tomoyo_read,
205         .write   = tomoyo_write,
206         .llseek  = noop_llseek,
207 };
208
209 /**
210  * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
211  *
212  * @name:   The name of the interface file.
213  * @mode:   The permission of the interface file.
214  * @parent: The parent directory.
215  * @key:    Type of interface.
216  *
217  * Returns nothing.
218  */
219 static void __init tomoyo_create_entry(const char *name, const umode_t mode,
220                                        struct dentry *parent, const u8 key)
221 {
222         securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
223                                &tomoyo_operations);
224 }
225
226 /**
227  * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
228  *
229  * Returns 0.
230  */
231 static int __init tomoyo_initerface_init(void)
232 {
233         struct tomoyo_domain_info *domain;
234         struct dentry *tomoyo_dir;
235
236         if (!tomoyo_enabled)
237                 return 0;
238         domain = tomoyo_domain();
239         /* Don't create securityfs entries unless registered. */
240         if (domain != &tomoyo_kernel_domain)
241                 return 0;
242
243         tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
244         tomoyo_create_entry("query",            0600, tomoyo_dir,
245                             TOMOYO_QUERY);
246         tomoyo_create_entry("domain_policy",    0600, tomoyo_dir,
247                             TOMOYO_DOMAINPOLICY);
248         tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
249                             TOMOYO_EXCEPTIONPOLICY);
250         tomoyo_create_entry("audit",            0400, tomoyo_dir,
251                             TOMOYO_AUDIT);
252         tomoyo_create_entry(".process_status",  0600, tomoyo_dir,
253                             TOMOYO_PROCESS_STATUS);
254         tomoyo_create_entry("stat",             0644, tomoyo_dir,
255                             TOMOYO_STAT);
256         tomoyo_create_entry("profile",          0600, tomoyo_dir,
257                             TOMOYO_PROFILE);
258         tomoyo_create_entry("manager",          0600, tomoyo_dir,
259                             TOMOYO_MANAGER);
260         tomoyo_create_entry("version",          0400, tomoyo_dir,
261                             TOMOYO_VERSION);
262         securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
263                                &tomoyo_self_operations);
264         tomoyo_load_builtin_policy();
265         return 0;
266 }
267
268 fs_initcall(tomoyo_initerface_init);