Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 2 | /* |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 3 | * security/tomoyo/securityfs_if.c |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/security.h> |
| 9 | #include "common.h" |
| 10 | |
| 11 | /** |
Tetsuo Handa | 731d37a | 2011-09-10 15:25:58 +0900 | [diff] [blame] | 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; |
Al Viro | 16e5c1f | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 47 | data = memdup_user_nul(buf, count); |
| 48 | if (IS_ERR(data)) |
| 49 | return PTR_ERR(data); |
Tetsuo Handa | 731d37a | 2011-09-10 15:25:58 +0900 | [diff] [blame] | 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 cred *cred = prepare_creds(); |
| 71 | if (!cred) { |
| 72 | error = -ENOMEM; |
| 73 | } else { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame^] | 74 | 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 Handa | 731d37a | 2011-09-10 15:25:58 +0900 | [diff] [blame] | 80 | 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 Handa | 731d37a | 2011-09-10 15:25:58 +0900 | [diff] [blame] | 90 | 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 | */ |
| 104 | static 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. */ |
| 122 | static const struct file_operations tomoyo_self_operations = { |
| 123 | .write = tomoyo_write_self, |
| 124 | .read = tomoyo_read_self, |
| 125 | }; |
| 126 | |
| 127 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 128 | * 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 | */ |
| 135 | static int tomoyo_open(struct inode *inode, struct file *file) |
| 136 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 137 | const int key = ((u8 *) file_inode(file)->i_private) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 138 | - ((u8 *) NULL); |
| 139 | return tomoyo_open_control(key, file); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface. |
| 144 | * |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 145 | * @file: Pointer to "struct file". |
| 146 | * |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 147 | */ |
| 148 | static int tomoyo_release(struct inode *inode, struct file *file) |
| 149 | { |
Al Viro | e53cfda | 2013-04-14 16:59:00 -0400 | [diff] [blame] | 150 | tomoyo_close_control(file->private_data); |
| 151 | return 0; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 155 | * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface. |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 156 | * |
| 157 | * @file: Pointer to "struct file". |
Tetsuo Handa | 6041e83 | 2012-03-14 18:27:49 +0900 | [diff] [blame] | 158 | * @wait: Pointer to "poll_table". Maybe NULL. |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 159 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 160 | * Returns EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM if ready to read/write, |
| 161 | * EPOLLOUT | EPOLLWRNORM otherwise. |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 162 | */ |
Al Viro | c0d4be2 | 2017-07-02 23:32:02 -0400 | [diff] [blame] | 163 | static __poll_t tomoyo_poll(struct file *file, poll_table *wait) |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 164 | { |
| 165 | return tomoyo_poll_control(file, wait); |
| 166 | } |
| 167 | |
| 168 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 169 | * 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 | */ |
| 178 | static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count, |
| 179 | loff_t *ppos) |
| 180 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 181 | return tomoyo_read_control(file->private_data, buf, count); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 182 | } |
| 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 | */ |
| 194 | static ssize_t tomoyo_write(struct file *file, const char __user *buf, |
| 195 | size_t count, loff_t *ppos) |
| 196 | { |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 197 | return tomoyo_write_control(file->private_data, buf, count); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 198 | } |
| 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 | */ |
| 207 | static const struct file_operations tomoyo_operations = { |
| 208 | .open = tomoyo_open, |
| 209 | .release = tomoyo_release, |
Tetsuo Handa | 0849e3b | 2010-06-25 12:22:09 +0900 | [diff] [blame] | 210 | .poll = tomoyo_poll, |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 211 | .read = tomoyo_read, |
| 212 | .write = tomoyo_write, |
Tetsuo Handa | 7e2deb7 | 2010-07-08 21:57:41 +0900 | [diff] [blame] | 213 | .llseek = noop_llseek, |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 214 | }; |
| 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 Viro | 52ef0c0 | 2011-07-26 04:30:04 -0400 | [diff] [blame] | 226 | static void __init tomoyo_create_entry(const char *name, const umode_t mode, |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 227 | 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 | */ |
| 238 | static int __init tomoyo_initerface_init(void) |
| 239 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame^] | 240 | struct tomoyo_domain_info *domain; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 241 | struct dentry *tomoyo_dir; |
| 242 | |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame^] | 243 | if (!tomoyo_enabled) |
| 244 | return 0; |
| 245 | domain = tomoyo_domain(); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 246 | /* Don't create securityfs entries unless registered. */ |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame^] | 247 | if (domain != &tomoyo_kernel_domain) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 248 | 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 Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 257 | tomoyo_create_entry("audit", 0400, tomoyo_dir, |
| 258 | TOMOYO_AUDIT); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 259 | tomoyo_create_entry(".process_status", 0600, tomoyo_dir, |
| 260 | TOMOYO_PROCESS_STATUS); |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame] | 261 | tomoyo_create_entry("stat", 0644, tomoyo_dir, |
| 262 | TOMOYO_STAT); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 263 | 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 Handa | 731d37a | 2011-09-10 15:25:58 +0900 | [diff] [blame] | 269 | securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL, |
| 270 | &tomoyo_self_operations); |
Tetsuo Handa | 778c4a4 | 2011-09-25 17:49:09 +0900 | [diff] [blame] | 271 | tomoyo_load_builtin_policy(); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | fs_initcall(tomoyo_initerface_init); |