Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 2008 International Business Machines Corp. |
| 5 | * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/hash.h> |
| 24 | #include <linux/random.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 28 | #include <linux/wait.h> |
| 29 | #include <linux/module.h> |
| 30 | #include "ecryptfs_kernel.h" |
| 31 | |
| 32 | static atomic_t ecryptfs_num_miscdev_opens; |
| 33 | |
| 34 | /** |
| 35 | * ecryptfs_miscdev_poll |
| 36 | * @file: dev file (ignored) |
| 37 | * @pt: dev poll table (ignored) |
| 38 | * |
| 39 | * Returns the poll mask |
| 40 | */ |
| 41 | static unsigned int |
| 42 | ecryptfs_miscdev_poll(struct file *file, poll_table *pt) |
| 43 | { |
| 44 | struct ecryptfs_daemon *daemon; |
| 45 | unsigned int mask = 0; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 46 | uid_t euid = current_euid(); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 47 | int rc; |
| 48 | |
| 49 | mutex_lock(&ecryptfs_daemon_hash_mux); |
| 50 | /* TODO: Just use file->private_data? */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 51 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); |
Tyler Hicks | 8dc6780 | 2012-06-11 09:24:11 -0700 | [diff] [blame^] | 52 | if (rc || !daemon) { |
| 53 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 54 | return -EINVAL; |
| 55 | } |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 56 | mutex_lock(&daemon->mux); |
| 57 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 58 | if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { |
| 59 | printk(KERN_WARNING "%s: Attempt to poll on zombified " |
| 60 | "daemon\n", __func__); |
| 61 | goto out_unlock_daemon; |
| 62 | } |
| 63 | if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) |
| 64 | goto out_unlock_daemon; |
| 65 | if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL) |
| 66 | goto out_unlock_daemon; |
| 67 | daemon->flags |= ECRYPTFS_DAEMON_IN_POLL; |
| 68 | mutex_unlock(&daemon->mux); |
| 69 | poll_wait(file, &daemon->wait, pt); |
| 70 | mutex_lock(&daemon->mux); |
| 71 | if (!list_empty(&daemon->msg_ctx_out_queue)) |
| 72 | mask |= POLLIN | POLLRDNORM; |
| 73 | out_unlock_daemon: |
| 74 | daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL; |
| 75 | mutex_unlock(&daemon->mux); |
| 76 | return mask; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * ecryptfs_miscdev_open |
| 81 | * @inode: inode of miscdev handle (ignored) |
| 82 | * @file: file for miscdev handle (ignored) |
| 83 | * |
| 84 | * Returns zero on success; non-zero otherwise |
| 85 | */ |
| 86 | static int |
| 87 | ecryptfs_miscdev_open(struct inode *inode, struct file *file) |
| 88 | { |
| 89 | struct ecryptfs_daemon *daemon = NULL; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 90 | uid_t euid = current_euid(); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 91 | int rc; |
| 92 | |
| 93 | mutex_lock(&ecryptfs_daemon_hash_mux); |
| 94 | rc = try_module_get(THIS_MODULE); |
| 95 | if (rc == 0) { |
| 96 | rc = -EIO; |
| 97 | printk(KERN_ERR "%s: Error attempting to increment module use " |
| 98 | "count; rc = [%d]\n", __func__, rc); |
| 99 | goto out_unlock_daemon_list; |
| 100 | } |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 101 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 102 | if (rc || !daemon) { |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 103 | rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(), |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 104 | task_pid(current)); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 105 | if (rc) { |
| 106 | printk(KERN_ERR "%s: Error attempting to spawn daemon; " |
| 107 | "rc = [%d]\n", __func__, rc); |
| 108 | goto out_module_put_unlock_daemon_list; |
| 109 | } |
| 110 | } |
| 111 | mutex_lock(&daemon->mux); |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 112 | if (daemon->pid != task_pid(current)) { |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 113 | rc = -EINVAL; |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 114 | printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], " |
| 115 | "but pid [0x%p] has attempted to open the handle " |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 116 | "instead\n", __func__, daemon->pid, daemon->euid, |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 117 | task_pid(current)); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 118 | goto out_unlock_daemon; |
| 119 | } |
| 120 | if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) { |
| 121 | rc = -EBUSY; |
| 122 | printk(KERN_ERR "%s: Miscellaneous device handle may only be " |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 123 | "opened once per daemon; pid [0x%p] already has this " |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 124 | "handle open\n", __func__, daemon->pid); |
| 125 | goto out_unlock_daemon; |
| 126 | } |
| 127 | daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN; |
Tyler Hicks | 8dc6780 | 2012-06-11 09:24:11 -0700 | [diff] [blame^] | 128 | file->private_data = daemon; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 129 | atomic_inc(&ecryptfs_num_miscdev_opens); |
| 130 | out_unlock_daemon: |
| 131 | mutex_unlock(&daemon->mux); |
| 132 | out_module_put_unlock_daemon_list: |
| 133 | if (rc) |
| 134 | module_put(THIS_MODULE); |
| 135 | out_unlock_daemon_list: |
| 136 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 137 | return rc; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * ecryptfs_miscdev_release |
| 142 | * @inode: inode of fs/ecryptfs/euid handle (ignored) |
| 143 | * @file: file for fs/ecryptfs/euid handle (ignored) |
| 144 | * |
| 145 | * This keeps the daemon registered until the daemon sends another |
| 146 | * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters. |
| 147 | * |
| 148 | * Returns zero on success; non-zero otherwise |
| 149 | */ |
| 150 | static int |
| 151 | ecryptfs_miscdev_release(struct inode *inode, struct file *file) |
| 152 | { |
| 153 | struct ecryptfs_daemon *daemon = NULL; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 154 | uid_t euid = current_euid(); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 155 | int rc; |
| 156 | |
| 157 | mutex_lock(&ecryptfs_daemon_hash_mux); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 158 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); |
Tyler Hicks | 8dc6780 | 2012-06-11 09:24:11 -0700 | [diff] [blame^] | 159 | if (rc || !daemon) |
| 160 | daemon = file->private_data; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 161 | mutex_lock(&daemon->mux); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 162 | BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN)); |
| 163 | daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN; |
| 164 | atomic_dec(&ecryptfs_num_miscdev_opens); |
| 165 | mutex_unlock(&daemon->mux); |
| 166 | rc = ecryptfs_exorcise_daemon(daemon); |
| 167 | if (rc) { |
| 168 | printk(KERN_CRIT "%s: Fatal error whilst attempting to " |
| 169 | "shut down daemon; rc = [%d]. Please report this " |
| 170 | "bug.\n", __func__, rc); |
| 171 | BUG(); |
| 172 | } |
| 173 | module_put(THIS_MODULE); |
| 174 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * ecryptfs_send_miscdev |
| 180 | * @data: Data to send to daemon; may be NULL |
| 181 | * @data_size: Amount of data to send to daemon |
| 182 | * @msg_ctx: Message context, which is used to handle the reply. If |
| 183 | * this is NULL, then we do not expect a reply. |
| 184 | * @msg_type: Type of message |
| 185 | * @msg_flags: Flags for message |
| 186 | * @daemon: eCryptfs daemon object |
| 187 | * |
| 188 | * Add msg_ctx to queue and then, if it exists, notify the blocked |
| 189 | * miscdevess about the data being available. Must be called with |
| 190 | * ecryptfs_daemon_hash_mux held. |
| 191 | * |
| 192 | * Returns zero on success; non-zero otherwise |
| 193 | */ |
| 194 | int ecryptfs_send_miscdev(char *data, size_t data_size, |
| 195 | struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type, |
| 196 | u16 msg_flags, struct ecryptfs_daemon *daemon) |
| 197 | { |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 198 | struct ecryptfs_message *msg; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 199 | |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 200 | msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL); |
| 201 | if (!msg) { |
Tyler Hicks | 57ea34d | 2009-03-15 14:17:01 -0500 | [diff] [blame] | 202 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
| 203 | "to kmalloc(%zd, GFP_KERNEL)\n", __func__, |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 204 | (sizeof(*msg) + data_size)); |
| 205 | return -ENOMEM; |
Tyler Hicks | 57ea34d | 2009-03-15 14:17:01 -0500 | [diff] [blame] | 206 | } |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 207 | |
| 208 | mutex_lock(&msg_ctx->mux); |
| 209 | msg_ctx->msg = msg; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 210 | msg_ctx->msg->index = msg_ctx->index; |
| 211 | msg_ctx->msg->data_len = data_size; |
| 212 | msg_ctx->type = msg_type; |
Tyler Hicks | 57ea34d | 2009-03-15 14:17:01 -0500 | [diff] [blame] | 213 | memcpy(msg_ctx->msg->data, data, data_size); |
| 214 | msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 215 | list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue); |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 216 | mutex_unlock(&msg_ctx->mux); |
| 217 | |
| 218 | mutex_lock(&daemon->mux); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 219 | daemon->num_queued_msg_ctx++; |
| 220 | wake_up_interruptible(&daemon->wait); |
| 221 | mutex_unlock(&daemon->mux); |
Tyler Hicks | 60d65f1 | 2012-06-11 10:21:34 -0700 | [diff] [blame] | 222 | |
| 223 | return 0; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 226 | /* |
| 227 | * miscdevfs packet format: |
| 228 | * Octet 0: Type |
| 229 | * Octets 1-4: network byte order msg_ctx->counter |
| 230 | * Octets 5-N0: Size of struct ecryptfs_message to follow |
| 231 | * Octets N0-N1: struct ecryptfs_message (including data) |
| 232 | * |
| 233 | * Octets 5-N1 not written if the packet type does not include a message |
| 234 | */ |
| 235 | #define PKT_TYPE_SIZE 1 |
| 236 | #define PKT_CTR_SIZE 4 |
| 237 | #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE) |
| 238 | #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \ |
| 239 | + ECRYPTFS_MIN_PKT_LEN_SIZE) |
| 240 | /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */ |
| 241 | #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \ |
| 242 | + ECRYPTFS_MAX_PKT_LEN_SIZE \ |
| 243 | + sizeof(struct ecryptfs_message) \ |
| 244 | + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) |
| 245 | #define PKT_TYPE_OFFSET 0 |
| 246 | #define PKT_CTR_OFFSET PKT_TYPE_SIZE |
| 247 | #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE) |
| 248 | |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 249 | /** |
| 250 | * ecryptfs_miscdev_read - format and send message from queue |
| 251 | * @file: fs/ecryptfs/euid miscdevfs handle (ignored) |
| 252 | * @buf: User buffer into which to copy the next message on the daemon queue |
| 253 | * @count: Amount of space available in @buf |
| 254 | * @ppos: Offset in file (ignored) |
| 255 | * |
| 256 | * Pulls the most recent message from the daemon queue, formats it for |
| 257 | * being sent via a miscdevfs handle, and copies it into @buf |
| 258 | * |
| 259 | * Returns the number of bytes copied into the user buffer |
| 260 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 261 | static ssize_t |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 262 | ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count, |
| 263 | loff_t *ppos) |
| 264 | { |
| 265 | struct ecryptfs_daemon *daemon; |
| 266 | struct ecryptfs_msg_ctx *msg_ctx; |
| 267 | size_t packet_length_size; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 268 | char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE]; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 269 | size_t i; |
| 270 | size_t total_length; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 271 | uid_t euid = current_euid(); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 272 | int rc; |
| 273 | |
| 274 | mutex_lock(&ecryptfs_daemon_hash_mux); |
| 275 | /* TODO: Just use file->private_data? */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 276 | rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); |
Tyler Hicks | 8dc6780 | 2012-06-11 09:24:11 -0700 | [diff] [blame^] | 277 | if (rc || !daemon) { |
| 278 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 279 | return -EINVAL; |
| 280 | } |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 281 | mutex_lock(&daemon->mux); |
Tyler Hicks | 8dc6780 | 2012-06-11 09:24:11 -0700 | [diff] [blame^] | 282 | if (task_pid(current) != daemon->pid) { |
| 283 | mutex_unlock(&daemon->mux); |
| 284 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 285 | return -EPERM; |
| 286 | } |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 287 | if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { |
| 288 | rc = 0; |
Cyrill Gorcunov | 43f14d8 | 2008-05-12 14:02:40 -0700 | [diff] [blame] | 289 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 290 | printk(KERN_WARNING "%s: Attempt to read from zombified " |
| 291 | "daemon\n", __func__); |
| 292 | goto out_unlock_daemon; |
| 293 | } |
| 294 | if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) { |
| 295 | rc = 0; |
Cyrill Gorcunov | 43f14d8 | 2008-05-12 14:02:40 -0700 | [diff] [blame] | 296 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 297 | goto out_unlock_daemon; |
| 298 | } |
| 299 | /* This daemon will not go away so long as this flag is set */ |
| 300 | daemon->flags |= ECRYPTFS_DAEMON_IN_READ; |
| 301 | mutex_unlock(&ecryptfs_daemon_hash_mux); |
| 302 | check_list: |
| 303 | if (list_empty(&daemon->msg_ctx_out_queue)) { |
| 304 | mutex_unlock(&daemon->mux); |
| 305 | rc = wait_event_interruptible( |
| 306 | daemon->wait, !list_empty(&daemon->msg_ctx_out_queue)); |
| 307 | mutex_lock(&daemon->mux); |
| 308 | if (rc < 0) { |
| 309 | rc = 0; |
| 310 | goto out_unlock_daemon; |
| 311 | } |
| 312 | } |
| 313 | if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { |
| 314 | rc = 0; |
| 315 | goto out_unlock_daemon; |
| 316 | } |
| 317 | if (list_empty(&daemon->msg_ctx_out_queue)) { |
| 318 | /* Something else jumped in since the |
| 319 | * wait_event_interruptable() and removed the |
| 320 | * message from the queue; try again */ |
| 321 | goto check_list; |
| 322 | } |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 323 | msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue, |
| 324 | struct ecryptfs_msg_ctx, daemon_out_list); |
| 325 | BUG_ON(!msg_ctx); |
| 326 | mutex_lock(&msg_ctx->mux); |
| 327 | if (msg_ctx->msg) { |
| 328 | rc = ecryptfs_write_packet_length(packet_length, |
| 329 | msg_ctx->msg_size, |
| 330 | &packet_length_size); |
| 331 | if (rc) { |
| 332 | rc = 0; |
| 333 | printk(KERN_WARNING "%s: Error writing packet length; " |
| 334 | "rc = [%d]\n", __func__, rc); |
| 335 | goto out_unlock_msg_ctx; |
| 336 | } |
| 337 | } else { |
| 338 | packet_length_size = 0; |
| 339 | msg_ctx->msg_size = 0; |
| 340 | } |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 341 | total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size |
| 342 | + msg_ctx->msg_size); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 343 | if (count < total_length) { |
| 344 | rc = 0; |
| 345 | printk(KERN_WARNING "%s: Only given user buffer of " |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 346 | "size [%zd], but we need [%zd] to read the " |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 347 | "pending message\n", __func__, count, total_length); |
| 348 | goto out_unlock_msg_ctx; |
| 349 | } |
Al Viro | 79bc12a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 350 | rc = -EFAULT; |
| 351 | if (put_user(msg_ctx->type, buf)) |
| 352 | goto out_unlock_msg_ctx; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 353 | if (put_user(cpu_to_be32(msg_ctx->counter), |
| 354 | (__be32 __user *)(&buf[PKT_CTR_OFFSET]))) |
Al Viro | 79bc12a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 355 | goto out_unlock_msg_ctx; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 356 | i = PKT_TYPE_SIZE + PKT_CTR_SIZE; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 357 | if (msg_ctx->msg) { |
Al Viro | 79bc12a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 358 | if (copy_to_user(&buf[i], packet_length, packet_length_size)) |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 359 | goto out_unlock_msg_ctx; |
Al Viro | 79bc12a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 360 | i += packet_length_size; |
| 361 | if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size)) |
| 362 | goto out_unlock_msg_ctx; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 363 | i += msg_ctx->msg_size; |
| 364 | } |
| 365 | rc = i; |
| 366 | list_del(&msg_ctx->daemon_out_list); |
| 367 | kfree(msg_ctx->msg); |
| 368 | msg_ctx->msg = NULL; |
| 369 | /* We do not expect a reply from the userspace daemon for any |
| 370 | * message type other than ECRYPTFS_MSG_REQUEST */ |
| 371 | if (msg_ctx->type != ECRYPTFS_MSG_REQUEST) |
| 372 | ecryptfs_msg_ctx_alloc_to_free(msg_ctx); |
| 373 | out_unlock_msg_ctx: |
| 374 | mutex_unlock(&msg_ctx->mux); |
| 375 | out_unlock_daemon: |
| 376 | daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ; |
| 377 | mutex_unlock(&daemon->mux); |
| 378 | return rc; |
| 379 | } |
| 380 | |
| 381 | /** |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 382 | * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon |
| 383 | * @data: Bytes comprising struct ecryptfs_message |
| 384 | * @data_size: sizeof(struct ecryptfs_message) + data len |
| 385 | * @euid: Effective user id of miscdevess sending the miscdev response |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 386 | * @user_ns: The namespace in which @euid applies |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 387 | * @pid: Miscdevess id of miscdevess sending the miscdev response |
| 388 | * @seq: Sequence number for miscdev response packet |
| 389 | * |
| 390 | * Returns zero on success; non-zero otherwise |
| 391 | */ |
| 392 | static int ecryptfs_miscdev_response(char *data, size_t data_size, |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 393 | uid_t euid, struct user_namespace *user_ns, |
| 394 | struct pid *pid, u32 seq) |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 395 | { |
| 396 | struct ecryptfs_message *msg = (struct ecryptfs_message *)data; |
| 397 | int rc; |
| 398 | |
| 399 | if ((sizeof(*msg) + msg->data_len) != data_size) { |
| 400 | printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = " |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 401 | "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__, |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 402 | (sizeof(*msg) + msg->data_len), data_size); |
| 403 | rc = -EINVAL; |
| 404 | goto out; |
| 405 | } |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 406 | rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 407 | if (rc) |
| 408 | printk(KERN_ERR |
| 409 | "Error processing response message; rc = [%d]\n", rc); |
| 410 | out: |
| 411 | return rc; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * ecryptfs_miscdev_write - handle write to daemon miscdev handle |
| 416 | * @file: File for misc dev handle (ignored) |
| 417 | * @buf: Buffer containing user data |
| 418 | * @count: Amount of data in @buf |
| 419 | * @ppos: Pointer to offset in file (ignored) |
| 420 | * |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 421 | * Returns the number of bytes read from @buf |
| 422 | */ |
| 423 | static ssize_t |
| 424 | ecryptfs_miscdev_write(struct file *file, const char __user *buf, |
| 425 | size_t count, loff_t *ppos) |
| 426 | { |
Al Viro | 79bc12a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 427 | __be32 counter_nbo; |
| 428 | u32 seq; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 429 | size_t packet_size, packet_size_length; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 430 | char *data; |
David Howells | 4eea035 | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 431 | uid_t euid = current_euid(); |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 432 | unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE]; |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 433 | ssize_t rc; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 434 | |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 435 | if (count == 0) { |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 436 | return 0; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 437 | } else if (count == MIN_NON_MSG_PKT_SIZE) { |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 438 | /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */ |
| 439 | goto memdup; |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 440 | } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) { |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 441 | printk(KERN_WARNING "%s: Acceptable packet size range is " |
Randy Dunlap | 164974a | 2012-02-28 16:31:12 -0800 | [diff] [blame] | 442 | "[%d-%zu], but amount of data written is [%zu].", |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 443 | __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count); |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 444 | return -EINVAL; |
| 445 | } |
Li Zefan | fd56d24 | 2009-04-08 15:09:29 +0800 | [diff] [blame] | 446 | |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 447 | if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET], |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 448 | sizeof(packet_size_peek))) { |
| 449 | printk(KERN_WARNING "%s: Error while inspecting packet size\n", |
| 450 | __func__); |
| 451 | return -EFAULT; |
| 452 | } |
| 453 | |
| 454 | rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size, |
| 455 | &packet_size_length); |
| 456 | if (rc) { |
| 457 | printk(KERN_WARNING "%s: Error parsing packet length; " |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 458 | "rc = [%zd]\n", __func__, rc); |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 459 | return rc; |
| 460 | } |
| 461 | |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 462 | if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size) |
| 463 | != count) { |
Tyler Hicks | db10e55 | 2012-01-12 11:30:44 +0100 | [diff] [blame] | 464 | printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__, |
| 465 | packet_size); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | |
| 469 | memdup: |
Li Zefan | fd56d24 | 2009-04-08 15:09:29 +0800 | [diff] [blame] | 470 | data = memdup_user(buf, count); |
| 471 | if (IS_ERR(data)) { |
| 472 | printk(KERN_ERR "%s: memdup_user returned error [%ld]\n", |
| 473 | __func__, PTR_ERR(data)); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 474 | return PTR_ERR(data); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 475 | } |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 476 | switch (data[PKT_TYPE_OFFSET]) { |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 477 | case ECRYPTFS_MSG_RESPONSE: |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 478 | if (count < (MIN_MSG_PKT_SIZE |
| 479 | + sizeof(struct ecryptfs_message))) { |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 480 | printk(KERN_WARNING "%s: Minimum acceptable packet " |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 481 | "size is [%zd], but amount of data written is " |
| 482 | "only [%zd]. Discarding response packet.\n", |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 483 | __func__, |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 484 | (MIN_MSG_PKT_SIZE |
| 485 | + sizeof(struct ecryptfs_message)), count); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 486 | rc = -EINVAL; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 487 | goto out_free; |
| 488 | } |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 489 | memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 490 | seq = be32_to_cpu(counter_nbo); |
Tyler Hicks | 48399c0 | 2012-01-14 16:46:46 +0100 | [diff] [blame] | 491 | rc = ecryptfs_miscdev_response( |
| 492 | &data[PKT_LEN_OFFSET + packet_size_length], |
| 493 | packet_size, euid, current_user_ns(), |
| 494 | task_pid(current), seq); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 495 | if (rc) { |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 496 | printk(KERN_WARNING "%s: Failed to deliver miscdev " |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 497 | "response to requesting operation; rc = [%zd]\n", |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 498 | __func__, rc); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 499 | goto out_free; |
| 500 | } |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 501 | break; |
| 502 | case ECRYPTFS_MSG_HELO: |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 503 | case ECRYPTFS_MSG_QUIT: |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 504 | break; |
| 505 | default: |
| 506 | ecryptfs_printk(KERN_WARNING, "Dropping miscdev " |
| 507 | "message of unrecognized type [%d]\n", |
| 508 | data[0]); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 509 | rc = -EINVAL; |
| 510 | goto out_free; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 511 | } |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 512 | rc = count; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 513 | out_free: |
| 514 | kfree(data); |
Tyler Hicks | 7f13350 | 2012-01-14 15:51:37 +0100 | [diff] [blame] | 515 | return rc; |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | |
| 519 | static const struct file_operations ecryptfs_miscdev_fops = { |
| 520 | .open = ecryptfs_miscdev_open, |
| 521 | .poll = ecryptfs_miscdev_poll, |
| 522 | .read = ecryptfs_miscdev_read, |
| 523 | .write = ecryptfs_miscdev_write, |
| 524 | .release = ecryptfs_miscdev_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 525 | .llseek = noop_llseek, |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | static struct miscdevice ecryptfs_miscdev = { |
| 529 | .minor = MISC_DYNAMIC_MINOR, |
| 530 | .name = "ecryptfs", |
| 531 | .fops = &ecryptfs_miscdev_fops |
| 532 | }; |
| 533 | |
| 534 | /** |
| 535 | * ecryptfs_init_ecryptfs_miscdev |
| 536 | * |
| 537 | * Messages sent to the userspace daemon from the kernel are placed on |
| 538 | * a queue associated with the daemon. The next read against the |
| 539 | * miscdev handle by that daemon will return the oldest message placed |
| 540 | * on the message queue for the daemon. |
| 541 | * |
| 542 | * Returns zero on success; non-zero otherwise |
| 543 | */ |
Jerome Marchand | 7371a38 | 2010-08-17 17:24:05 +0200 | [diff] [blame] | 544 | int __init ecryptfs_init_ecryptfs_miscdev(void) |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 545 | { |
| 546 | int rc; |
| 547 | |
| 548 | atomic_set(&ecryptfs_num_miscdev_opens, 0); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 549 | rc = misc_register(&ecryptfs_miscdev); |
| 550 | if (rc) |
| 551 | printk(KERN_ERR "%s: Failed to register miscellaneous device " |
| 552 | "for communications with userspace daemons; rc = [%d]\n", |
| 553 | __func__, rc); |
Michael Halcrow | 8bf2deb | 2008-04-29 00:59:50 -0700 | [diff] [blame] | 554 | return rc; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * ecryptfs_destroy_ecryptfs_miscdev |
| 559 | * |
| 560 | * All of the daemons must be exorcised prior to calling this |
| 561 | * function. |
| 562 | */ |
| 563 | void ecryptfs_destroy_ecryptfs_miscdev(void) |
| 564 | { |
| 565 | BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0); |
| 566 | misc_deregister(&ecryptfs_miscdev); |
| 567 | } |