Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * eCryptfs: Linux filesystem encryption layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2008 International Business Machines Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "ecryptfs_kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static atomic_t ecryptfs_num_miscdev_opens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * ecryptfs_miscdev_poll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @file: dev file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @pt: dev poll table (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Returns the poll mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static __poll_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct ecryptfs_daemon *daemon = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		printk(KERN_WARNING "%s: Attempt to poll on zombified "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		       "daemon\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	poll_wait(file, &daemon->wait, pt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (!list_empty(&daemon->msg_ctx_out_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) out_unlock_daemon:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * ecryptfs_miscdev_open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * @inode: inode of miscdev handle (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @file: file for miscdev handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) ecryptfs_miscdev_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct ecryptfs_daemon *daemon = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	mutex_lock(&ecryptfs_daemon_hash_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	rc = ecryptfs_find_daemon_by_euid(&daemon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		goto out_unlock_daemon_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	rc = ecryptfs_spawn_daemon(&daemon, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		printk(KERN_ERR "%s: Error attempting to spawn daemon; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		       "rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto out_unlock_daemon_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	file->private_data = daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	atomic_inc(&ecryptfs_num_miscdev_opens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) out_unlock_daemon:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) out_unlock_daemon_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	mutex_unlock(&ecryptfs_daemon_hash_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * ecryptfs_miscdev_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @inode: inode of fs/ecryptfs/euid handle (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @file: file for fs/ecryptfs/euid handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * This keeps the daemon registered until the daemon sends another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ecryptfs_miscdev_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct ecryptfs_daemon *daemon = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	atomic_dec(&ecryptfs_num_miscdev_opens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	mutex_lock(&ecryptfs_daemon_hash_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	rc = ecryptfs_exorcise_daemon(daemon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	mutex_unlock(&ecryptfs_daemon_hash_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		printk(KERN_CRIT "%s: Fatal error whilst attempting to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		       "shut down daemon; rc = [%d]. Please report this "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		       "bug.\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * ecryptfs_send_miscdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * @data: Data to send to daemon; may be NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * @data_size: Amount of data to send to daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * @msg_ctx: Message context, which is used to handle the reply. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  *           this is NULL, then we do not expect a reply.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * @msg_type: Type of message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * @msg_flags: Flags for message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * @daemon: eCryptfs daemon object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * Add msg_ctx to queue and then, if it exists, notify the blocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * miscdevess about the data being available. Must be called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * ecryptfs_daemon_hash_mux held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int ecryptfs_send_miscdev(char *data, size_t data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			  struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			  u16 msg_flags, struct ecryptfs_daemon *daemon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct ecryptfs_message *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	mutex_lock(&msg_ctx->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	msg_ctx->msg = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	msg_ctx->msg->index = msg_ctx->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	msg_ctx->msg->data_len = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	msg_ctx->type = msg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	memcpy(msg_ctx->msg->data, data, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mutex_unlock(&msg_ctx->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	daemon->num_queued_msg_ctx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	wake_up_interruptible(&daemon->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * miscdevfs packet format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  *  Octet 0: Type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  *  Octets 1-4: network byte order msg_ctx->counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *  Octets 5-N0: Size of struct ecryptfs_message to follow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  *  Octets N0-N1: struct ecryptfs_message (including data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  *  Octets 5-N1 not written if the packet type does not include a message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define PKT_TYPE_SIZE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #define PKT_CTR_SIZE		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #define MIN_NON_MSG_PKT_SIZE	(PKT_TYPE_SIZE + PKT_CTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define MIN_MSG_PKT_SIZE	(PKT_TYPE_SIZE + PKT_CTR_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				 + ECRYPTFS_MIN_PKT_LEN_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #define MAX_MSG_PKT_SIZE	(PKT_TYPE_SIZE + PKT_CTR_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				 + ECRYPTFS_MAX_PKT_LEN_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				 + sizeof(struct ecryptfs_message) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				 + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #define PKT_TYPE_OFFSET		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #define PKT_CTR_OFFSET		PKT_TYPE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #define PKT_LEN_OFFSET		(PKT_TYPE_SIZE + PKT_CTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * ecryptfs_miscdev_read - format and send message from queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * @file: miscdevfs handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * @buf: User buffer into which to copy the next message on the daemon queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * @count: Amount of space available in @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * @ppos: Offset in file (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * Pulls the most recent message from the daemon queue, formats it for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * being sent via a miscdevfs handle, and copies it into @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * Returns the number of bytes copied into the user buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		      loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct ecryptfs_daemon *daemon = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct ecryptfs_msg_ctx *msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	size_t packet_length_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	size_t total_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		printk(KERN_WARNING "%s: Attempt to read from zombified "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		       "daemon\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* This daemon will not go away so long as this flag is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) check_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (list_empty(&daemon->msg_ctx_out_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		rc = wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		mutex_lock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		goto out_unlock_daemon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (list_empty(&daemon->msg_ctx_out_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		/* Something else jumped in since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		 * wait_event_interruptable() and removed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		 * message from the queue; try again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		goto check_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				   struct ecryptfs_msg_ctx, daemon_out_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	BUG_ON(!msg_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_lock(&msg_ctx->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (msg_ctx->msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		rc = ecryptfs_write_packet_length(packet_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 						  msg_ctx->msg_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 						  &packet_length_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			printk(KERN_WARNING "%s: Error writing packet length; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			       "rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		packet_length_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		msg_ctx->msg_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			+ msg_ctx->msg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (count < total_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		printk(KERN_WARNING "%s: Only given user buffer of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		       "size [%zd], but we need [%zd] to read the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		       "pending message\n", __func__, count, total_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (put_user(msg_ctx->type, buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (put_user(cpu_to_be32(msg_ctx->counter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		     (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (msg_ctx->msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		if (copy_to_user(&buf[i], packet_length, packet_length_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		i += packet_length_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			goto out_unlock_msg_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		i += msg_ctx->msg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	rc = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	list_del(&msg_ctx->daemon_out_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	kfree(msg_ctx->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	msg_ctx->msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/* We do not expect a reply from the userspace daemon for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * message type other than ECRYPTFS_MSG_REQUEST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) out_unlock_msg_ctx:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	mutex_unlock(&msg_ctx->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) out_unlock_daemon:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	mutex_unlock(&daemon->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * @data: Bytes comprising struct ecryptfs_message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * @data_size: sizeof(struct ecryptfs_message) + data len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * @seq: Sequence number for miscdev response packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int ecryptfs_miscdev_response(struct ecryptfs_daemon *daemon, char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				     size_t data_size, u32 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if ((sizeof(*msg) + msg->data_len) != data_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		       "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		       (sizeof(*msg) + msg->data_len), data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	rc = ecryptfs_process_response(daemon, msg, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		       "Error processing response message; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * ecryptfs_miscdev_write - handle write to daemon miscdev handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * @file: File for misc dev handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * @buf: Buffer containing user data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * @count: Amount of data in @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * @ppos: Pointer to offset in file (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * Returns the number of bytes read from @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ecryptfs_miscdev_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		       size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	__be32 counter_nbo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	u32 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	size_t packet_size, packet_size_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	} else if (count == MIN_NON_MSG_PKT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		/* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		goto memdup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	} else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		printk(KERN_WARNING "%s: Acceptable packet size range is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		       "[%d-%zu], but amount of data written is [%zu].\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		       __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			   sizeof(packet_size_peek))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		printk(KERN_WARNING "%s: Error while inspecting packet size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		       __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 					  &packet_size_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		printk(KERN_WARNING "%s: Error parsing packet length; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		       "rc = [%zd]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	    != count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		       packet_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) memdup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	data = memdup_user(buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (IS_ERR(data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		       __func__, PTR_ERR(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	switch (data[PKT_TYPE_OFFSET]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	case ECRYPTFS_MSG_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (count < (MIN_MSG_PKT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			     + sizeof(struct ecryptfs_message))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			printk(KERN_WARNING "%s: Minimum acceptable packet "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			       "size is [%zd], but amount of data written is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			       "only [%zd]. Discarding response packet.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			       __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			       (MIN_MSG_PKT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				+ sizeof(struct ecryptfs_message)), count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		seq = be32_to_cpu(counter_nbo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		rc = ecryptfs_miscdev_response(file->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 				&data[PKT_LEN_OFFSET + packet_size_length],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				packet_size, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			printk(KERN_WARNING "%s: Failed to deliver miscdev "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			       "response to requesting operation; rc = [%zd]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			       __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	case ECRYPTFS_MSG_HELO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	case ECRYPTFS_MSG_QUIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 				"message of unrecognized type [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	rc = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static const struct file_operations ecryptfs_miscdev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	.owner   = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	.open    = ecryptfs_miscdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	.poll    = ecryptfs_miscdev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	.read    = ecryptfs_miscdev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	.write   = ecryptfs_miscdev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	.release = ecryptfs_miscdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	.llseek  = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static struct miscdevice ecryptfs_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	.minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	.name  = "ecryptfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	.fops  = &ecryptfs_miscdev_fops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  * ecryptfs_init_ecryptfs_miscdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)  * Messages sent to the userspace daemon from the kernel are placed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)  * a queue associated with the daemon. The next read against the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)  * miscdev handle by that daemon will return the oldest message placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  * on the message queue for the daemon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int __init ecryptfs_init_ecryptfs_miscdev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	atomic_set(&ecryptfs_num_miscdev_opens, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	rc = misc_register(&ecryptfs_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		printk(KERN_ERR "%s: Failed to register miscellaneous device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		       "for communications with userspace daemons; rc = [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		       __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * ecryptfs_destroy_ecryptfs_miscdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * All of the daemons must be exorcised prior to calling this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) void ecryptfs_destroy_ecryptfs_miscdev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	misc_deregister(&ecryptfs_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }