1 // SPDX-License-Identifier: GPL-2.0-only
3 * eCryptfs: Linux filesystem encryption layer
5 * Copyright (C) 2008 International Business Machines Corp.
6 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
10 #include <linux/hash.h>
11 #include <linux/random.h>
12 #include <linux/miscdevice.h>
13 #include <linux/poll.h>
14 #include <linux/slab.h>
15 #include <linux/wait.h>
16 #include <linux/module.h>
17 #include "ecryptfs_kernel.h"
19 static atomic_t ecryptfs_num_miscdev_opens;
22 * ecryptfs_miscdev_poll
24 * @pt: dev poll table (ignored)
26 * Returns the poll mask
29 ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
31 struct ecryptfs_daemon *daemon = file->private_data;
34 mutex_lock(&daemon->mux);
35 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
36 printk(KERN_WARNING "%s: Attempt to poll on zombified "
37 "daemon\n", __func__);
38 goto out_unlock_daemon;
40 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
41 goto out_unlock_daemon;
42 if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
43 goto out_unlock_daemon;
44 daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
45 mutex_unlock(&daemon->mux);
46 poll_wait(file, &daemon->wait, pt);
47 mutex_lock(&daemon->mux);
48 if (!list_empty(&daemon->msg_ctx_out_queue))
49 mask |= EPOLLIN | EPOLLRDNORM;
51 daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
52 mutex_unlock(&daemon->mux);
57 * ecryptfs_miscdev_open
58 * @inode: inode of miscdev handle (ignored)
59 * @file: file for miscdev handle
61 * Returns zero on success; non-zero otherwise
64 ecryptfs_miscdev_open(struct inode *inode, struct file *file)
66 struct ecryptfs_daemon *daemon = NULL;
69 mutex_lock(&ecryptfs_daemon_hash_mux);
70 rc = ecryptfs_find_daemon_by_euid(&daemon);
73 goto out_unlock_daemon_list;
75 rc = ecryptfs_spawn_daemon(&daemon, file);
77 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
78 "rc = [%d]\n", __func__, rc);
79 goto out_unlock_daemon_list;
81 mutex_lock(&daemon->mux);
82 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
84 goto out_unlock_daemon;
86 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
87 file->private_data = daemon;
88 atomic_inc(&ecryptfs_num_miscdev_opens);
90 mutex_unlock(&daemon->mux);
91 out_unlock_daemon_list:
92 mutex_unlock(&ecryptfs_daemon_hash_mux);
97 * ecryptfs_miscdev_release
98 * @inode: inode of fs/ecryptfs/euid handle (ignored)
99 * @file: file for fs/ecryptfs/euid handle
101 * This keeps the daemon registered until the daemon sends another
102 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
104 * Returns zero on success; non-zero otherwise
107 ecryptfs_miscdev_release(struct inode *inode, struct file *file)
109 struct ecryptfs_daemon *daemon = file->private_data;
112 mutex_lock(&daemon->mux);
113 BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
114 daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
115 atomic_dec(&ecryptfs_num_miscdev_opens);
116 mutex_unlock(&daemon->mux);
118 mutex_lock(&ecryptfs_daemon_hash_mux);
119 rc = ecryptfs_exorcise_daemon(daemon);
120 mutex_unlock(&ecryptfs_daemon_hash_mux);
122 printk(KERN_CRIT "%s: Fatal error whilst attempting to "
123 "shut down daemon; rc = [%d]. Please report this "
124 "bug.\n", __func__, rc);
131 * ecryptfs_send_miscdev
132 * @data: Data to send to daemon; may be NULL
133 * @data_size: Amount of data to send to daemon
134 * @msg_ctx: Message context, which is used to handle the reply. If
135 * this is NULL, then we do not expect a reply.
136 * @msg_type: Type of message
137 * @msg_flags: Flags for message
138 * @daemon: eCryptfs daemon object
140 * Add msg_ctx to queue and then, if it exists, notify the blocked
141 * miscdevess about the data being available. Must be called with
142 * ecryptfs_daemon_hash_mux held.
144 * Returns zero on success; non-zero otherwise
146 int ecryptfs_send_miscdev(char *data, size_t data_size,
147 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
148 u16 msg_flags, struct ecryptfs_daemon *daemon)
150 struct ecryptfs_message *msg;
152 msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
156 mutex_lock(&msg_ctx->mux);
158 msg_ctx->msg->index = msg_ctx->index;
159 msg_ctx->msg->data_len = data_size;
160 msg_ctx->type = msg_type;
161 memcpy(msg_ctx->msg->data, data, data_size);
162 msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
163 list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
164 mutex_unlock(&msg_ctx->mux);
166 mutex_lock(&daemon->mux);
167 daemon->num_queued_msg_ctx++;
168 wake_up_interruptible(&daemon->wait);
169 mutex_unlock(&daemon->mux);
175 * miscdevfs packet format:
177 * Octets 1-4: network byte order msg_ctx->counter
178 * Octets 5-N0: Size of struct ecryptfs_message to follow
179 * Octets N0-N1: struct ecryptfs_message (including data)
181 * Octets 5-N1 not written if the packet type does not include a message
183 #define PKT_TYPE_SIZE 1
184 #define PKT_CTR_SIZE 4
185 #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
186 #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
187 + ECRYPTFS_MIN_PKT_LEN_SIZE)
188 /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
189 #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
190 + ECRYPTFS_MAX_PKT_LEN_SIZE \
191 + sizeof(struct ecryptfs_message) \
192 + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
193 #define PKT_TYPE_OFFSET 0
194 #define PKT_CTR_OFFSET PKT_TYPE_SIZE
195 #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
198 * ecryptfs_miscdev_read - format and send message from queue
199 * @file: miscdevfs handle
200 * @buf: User buffer into which to copy the next message on the daemon queue
201 * @count: Amount of space available in @buf
202 * @ppos: Offset in file (ignored)
204 * Pulls the most recent message from the daemon queue, formats it for
205 * being sent via a miscdevfs handle, and copies it into @buf
207 * Returns the number of bytes copied into the user buffer
210 ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
213 struct ecryptfs_daemon *daemon = file->private_data;
214 struct ecryptfs_msg_ctx *msg_ctx;
215 size_t packet_length_size;
216 char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
221 mutex_lock(&daemon->mux);
222 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
224 printk(KERN_WARNING "%s: Attempt to read from zombified "
225 "daemon\n", __func__);
226 goto out_unlock_daemon;
228 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
230 goto out_unlock_daemon;
232 /* This daemon will not go away so long as this flag is set */
233 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
235 if (list_empty(&daemon->msg_ctx_out_queue)) {
236 mutex_unlock(&daemon->mux);
237 rc = wait_event_interruptible(
238 daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
239 mutex_lock(&daemon->mux);
242 goto out_unlock_daemon;
245 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
247 goto out_unlock_daemon;
249 if (list_empty(&daemon->msg_ctx_out_queue)) {
250 /* Something else jumped in since the
251 * wait_event_interruptable() and removed the
252 * message from the queue; try again */
255 msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
256 struct ecryptfs_msg_ctx, daemon_out_list);
258 mutex_lock(&msg_ctx->mux);
260 rc = ecryptfs_write_packet_length(packet_length,
262 &packet_length_size);
265 printk(KERN_WARNING "%s: Error writing packet length; "
266 "rc = [%d]\n", __func__, rc);
267 goto out_unlock_msg_ctx;
270 packet_length_size = 0;
271 msg_ctx->msg_size = 0;
273 total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
274 + msg_ctx->msg_size);
275 if (count < total_length) {
277 printk(KERN_WARNING "%s: Only given user buffer of "
278 "size [%zd], but we need [%zd] to read the "
279 "pending message\n", __func__, count, total_length);
280 goto out_unlock_msg_ctx;
283 if (put_user(msg_ctx->type, buf))
284 goto out_unlock_msg_ctx;
285 if (put_user(cpu_to_be32(msg_ctx->counter),
286 (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
287 goto out_unlock_msg_ctx;
288 i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
290 if (copy_to_user(&buf[i], packet_length, packet_length_size))
291 goto out_unlock_msg_ctx;
292 i += packet_length_size;
293 if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
294 goto out_unlock_msg_ctx;
295 i += msg_ctx->msg_size;
298 list_del(&msg_ctx->daemon_out_list);
301 /* We do not expect a reply from the userspace daemon for any
302 * message type other than ECRYPTFS_MSG_REQUEST */
303 if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
304 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
306 mutex_unlock(&msg_ctx->mux);
308 daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
309 mutex_unlock(&daemon->mux);
314 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
315 * @daemon: eCryptfs daemon object
316 * @data: Bytes comprising struct ecryptfs_message
317 * @data_size: sizeof(struct ecryptfs_message) + data len
318 * @seq: Sequence number for miscdev response packet
320 * Returns zero on success; non-zero otherwise
322 static int ecryptfs_miscdev_response(struct ecryptfs_daemon *daemon, char *data,
323 size_t data_size, u32 seq)
325 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
328 if ((sizeof(*msg) + msg->data_len) != data_size) {
329 printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
330 "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
331 (sizeof(*msg) + msg->data_len), data_size);
335 rc = ecryptfs_process_response(daemon, msg, seq);
338 "Error processing response message; rc = [%d]\n", rc);
344 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
345 * @file: File for misc dev handle
346 * @buf: Buffer containing user data
347 * @count: Amount of data in @buf
348 * @ppos: Pointer to offset in file (ignored)
350 * Returns the number of bytes read from @buf
353 ecryptfs_miscdev_write(struct file *file, const char __user *buf,
354 size_t count, loff_t *ppos)
358 size_t packet_size, packet_size_length;
360 unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
365 } else if (count == MIN_NON_MSG_PKT_SIZE) {
366 /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
368 } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
369 printk(KERN_WARNING "%s: Acceptable packet size range is "
370 "[%d-%zu], but amount of data written is [%zu].\n",
371 __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
375 if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
376 sizeof(packet_size_peek))) {
377 printk(KERN_WARNING "%s: Error while inspecting packet size\n",
382 rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
383 &packet_size_length);
385 printk(KERN_WARNING "%s: Error parsing packet length; "
386 "rc = [%zd]\n", __func__, rc);
390 if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
392 printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
398 data = memdup_user(buf, count);
400 printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
401 __func__, PTR_ERR(data));
402 return PTR_ERR(data);
404 switch (data[PKT_TYPE_OFFSET]) {
405 case ECRYPTFS_MSG_RESPONSE:
406 if (count < (MIN_MSG_PKT_SIZE
407 + sizeof(struct ecryptfs_message))) {
408 printk(KERN_WARNING "%s: Minimum acceptable packet "
409 "size is [%zd], but amount of data written is "
410 "only [%zd]. Discarding response packet.\n",
413 + sizeof(struct ecryptfs_message)), count);
417 memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
418 seq = be32_to_cpu(counter_nbo);
419 rc = ecryptfs_miscdev_response(file->private_data,
420 &data[PKT_LEN_OFFSET + packet_size_length],
423 printk(KERN_WARNING "%s: Failed to deliver miscdev "
424 "response to requesting operation; rc = [%zd]\n",
429 case ECRYPTFS_MSG_HELO:
430 case ECRYPTFS_MSG_QUIT:
433 ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
434 "message of unrecognized type [%d]\n",
446 static const struct file_operations ecryptfs_miscdev_fops = {
447 .owner = THIS_MODULE,
448 .open = ecryptfs_miscdev_open,
449 .poll = ecryptfs_miscdev_poll,
450 .read = ecryptfs_miscdev_read,
451 .write = ecryptfs_miscdev_write,
452 .release = ecryptfs_miscdev_release,
453 .llseek = noop_llseek,
456 static struct miscdevice ecryptfs_miscdev = {
457 .minor = MISC_DYNAMIC_MINOR,
459 .fops = &ecryptfs_miscdev_fops
463 * ecryptfs_init_ecryptfs_miscdev
465 * Messages sent to the userspace daemon from the kernel are placed on
466 * a queue associated with the daemon. The next read against the
467 * miscdev handle by that daemon will return the oldest message placed
468 * on the message queue for the daemon.
470 * Returns zero on success; non-zero otherwise
472 int __init ecryptfs_init_ecryptfs_miscdev(void)
476 atomic_set(&ecryptfs_num_miscdev_opens, 0);
477 rc = misc_register(&ecryptfs_miscdev);
479 printk(KERN_ERR "%s: Failed to register miscellaneous device "
480 "for communications with userspace daemons; rc = [%d]\n",
486 * ecryptfs_destroy_ecryptfs_miscdev
488 * All of the daemons must be exorcised prior to calling this
491 void ecryptfs_destroy_ecryptfs_miscdev(void)
493 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
494 misc_deregister(&ecryptfs_miscdev);