blob: 2cd9c3f44407885e0fa1927eb531eb9f6685dbad [file] [log] [blame]
Michael Halcrow8bf2deb2008-04-29 00:59:50 -07001/**
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 Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Michael Halcrow8bf2deb2008-04-29 00:59:50 -070028#include <linux/wait.h>
29#include <linux/module.h>
30#include "ecryptfs_kernel.h"
31
32static 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 */
41static unsigned int
42ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
43{
44 struct ecryptfs_daemon *daemon;
45 unsigned int mask = 0;
David Howells4eea0352008-11-14 10:38:49 +110046 uid_t euid = current_euid();
Michael Halcrow8bf2deb2008-04-29 00:59:50 -070047 int rc;
48
49 mutex_lock(&ecryptfs_daemon_hash_mux);
50 /* TODO: Just use file->private_data? */
Serge Hallyn18b6e042008-10-15 16:38:45 -050051 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
Tyler Hicks6dfa5302012-06-11 09:24:11 -070052 if (rc || !daemon) {
53 mutex_unlock(&ecryptfs_daemon_hash_mux);
54 return -EINVAL;
55 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -070056 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;
73out_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 */
86static int
87ecryptfs_miscdev_open(struct inode *inode, struct file *file)
88{
89 struct ecryptfs_daemon *daemon = NULL;
David Howells4eea0352008-11-14 10:38:49 +110090 uid_t euid = current_euid();
Michael Halcrow8bf2deb2008-04-29 00:59:50 -070091 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 Hallyn18b6e042008-10-15 16:38:45 -0500101 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700102 if (rc || !daemon) {
Serge Hallyn18b6e042008-10-15 16:38:45 -0500103 rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700104 task_pid(current));
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700105 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 Halcrow6a3fd922008-04-29 00:59:52 -0700112 if (daemon->pid != task_pid(current)) {
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700113 rc = -EINVAL;
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700114 printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], "
115 "but pid [0x%p] has attempted to open the handle "
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700116 "instead\n", __func__, daemon->pid, daemon->euid,
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700117 task_pid(current));
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700118 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 Halcrow6a3fd922008-04-29 00:59:52 -0700123 "opened once per daemon; pid [0x%p] already has this "
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700124 "handle open\n", __func__, daemon->pid);
125 goto out_unlock_daemon;
126 }
127 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
Tyler Hicks6dfa5302012-06-11 09:24:11 -0700128 file->private_data = daemon;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700129 atomic_inc(&ecryptfs_num_miscdev_opens);
130out_unlock_daemon:
131 mutex_unlock(&daemon->mux);
132out_module_put_unlock_daemon_list:
133 if (rc)
134 module_put(THIS_MODULE);
135out_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 */
150static int
151ecryptfs_miscdev_release(struct inode *inode, struct file *file)
152{
153 struct ecryptfs_daemon *daemon = NULL;
David Howells4eea0352008-11-14 10:38:49 +1100154 uid_t euid = current_euid();
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700155 int rc;
156
157 mutex_lock(&ecryptfs_daemon_hash_mux);
Serge Hallyn18b6e042008-10-15 16:38:45 -0500158 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
Tyler Hicks6dfa5302012-06-11 09:24:11 -0700159 if (rc || !daemon)
160 daemon = file->private_data;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700161 mutex_lock(&daemon->mux);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700162 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 */
194int 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{
198 int rc = 0;
199
200 mutex_lock(&msg_ctx->mux);
Tyler Hicks57ea34d2009-03-15 14:17:01 -0500201 msg_ctx->msg = kmalloc((sizeof(*msg_ctx->msg) + data_size),
202 GFP_KERNEL);
203 if (!msg_ctx->msg) {
204 rc = -ENOMEM;
205 printk(KERN_ERR "%s: Out of memory whilst attempting "
206 "to kmalloc(%zd, GFP_KERNEL)\n", __func__,
207 (sizeof(*msg_ctx->msg) + data_size));
208 goto out_unlock;
209 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700210 msg_ctx->msg->index = msg_ctx->index;
211 msg_ctx->msg->data_len = data_size;
212 msg_ctx->type = msg_type;
Tyler Hicks57ea34d2009-03-15 14:17:01 -0500213 memcpy(msg_ctx->msg->data, data, data_size);
214 msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700215 mutex_lock(&daemon->mux);
216 list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
217 daemon->num_queued_msg_ctx++;
218 wake_up_interruptible(&daemon->wait);
219 mutex_unlock(&daemon->mux);
220out_unlock:
221 mutex_unlock(&msg_ctx->mux);
222 return rc;
223}
224
Tyler Hicks48399c02012-01-14 16:46:46 +0100225/*
226 * miscdevfs packet format:
227 * Octet 0: Type
228 * Octets 1-4: network byte order msg_ctx->counter
229 * Octets 5-N0: Size of struct ecryptfs_message to follow
230 * Octets N0-N1: struct ecryptfs_message (including data)
231 *
232 * Octets 5-N1 not written if the packet type does not include a message
233 */
234#define PKT_TYPE_SIZE 1
235#define PKT_CTR_SIZE 4
236#define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
237#define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
238 + ECRYPTFS_MIN_PKT_LEN_SIZE)
239/* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
240#define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
241 + ECRYPTFS_MAX_PKT_LEN_SIZE \
242 + sizeof(struct ecryptfs_message) \
243 + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
244#define PKT_TYPE_OFFSET 0
245#define PKT_CTR_OFFSET PKT_TYPE_SIZE
246#define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
247
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700248/**
249 * ecryptfs_miscdev_read - format and send message from queue
250 * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
251 * @buf: User buffer into which to copy the next message on the daemon queue
252 * @count: Amount of space available in @buf
253 * @ppos: Offset in file (ignored)
254 *
255 * Pulls the most recent message from the daemon queue, formats it for
256 * being sent via a miscdevfs handle, and copies it into @buf
257 *
258 * Returns the number of bytes copied into the user buffer
259 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700260static ssize_t
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700261ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
262 loff_t *ppos)
263{
264 struct ecryptfs_daemon *daemon;
265 struct ecryptfs_msg_ctx *msg_ctx;
266 size_t packet_length_size;
Tyler Hicks48399c02012-01-14 16:46:46 +0100267 char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700268 size_t i;
269 size_t total_length;
David Howells4eea0352008-11-14 10:38:49 +1100270 uid_t euid = current_euid();
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700271 int rc;
272
273 mutex_lock(&ecryptfs_daemon_hash_mux);
274 /* TODO: Just use file->private_data? */
Serge Hallyn18b6e042008-10-15 16:38:45 -0500275 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
Tyler Hicks6dfa5302012-06-11 09:24:11 -0700276 if (rc || !daemon) {
277 mutex_unlock(&ecryptfs_daemon_hash_mux);
278 return -EINVAL;
279 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700280 mutex_lock(&daemon->mux);
Tyler Hicks6dfa5302012-06-11 09:24:11 -0700281 if (task_pid(current) != daemon->pid) {
282 mutex_unlock(&daemon->mux);
283 mutex_unlock(&ecryptfs_daemon_hash_mux);
284 return -EPERM;
285 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700286 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
287 rc = 0;
Cyrill Gorcunov43f14d82008-05-12 14:02:40 -0700288 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700289 printk(KERN_WARNING "%s: Attempt to read from zombified "
290 "daemon\n", __func__);
291 goto out_unlock_daemon;
292 }
293 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
294 rc = 0;
Cyrill Gorcunov43f14d82008-05-12 14:02:40 -0700295 mutex_unlock(&ecryptfs_daemon_hash_mux);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700296 goto out_unlock_daemon;
297 }
298 /* This daemon will not go away so long as this flag is set */
299 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
300 mutex_unlock(&ecryptfs_daemon_hash_mux);
301check_list:
302 if (list_empty(&daemon->msg_ctx_out_queue)) {
303 mutex_unlock(&daemon->mux);
304 rc = wait_event_interruptible(
305 daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
306 mutex_lock(&daemon->mux);
307 if (rc < 0) {
308 rc = 0;
309 goto out_unlock_daemon;
310 }
311 }
312 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
313 rc = 0;
314 goto out_unlock_daemon;
315 }
316 if (list_empty(&daemon->msg_ctx_out_queue)) {
317 /* Something else jumped in since the
318 * wait_event_interruptable() and removed the
319 * message from the queue; try again */
320 goto check_list;
321 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700322 msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
323 struct ecryptfs_msg_ctx, daemon_out_list);
324 BUG_ON(!msg_ctx);
325 mutex_lock(&msg_ctx->mux);
326 if (msg_ctx->msg) {
327 rc = ecryptfs_write_packet_length(packet_length,
328 msg_ctx->msg_size,
329 &packet_length_size);
330 if (rc) {
331 rc = 0;
332 printk(KERN_WARNING "%s: Error writing packet length; "
333 "rc = [%d]\n", __func__, rc);
334 goto out_unlock_msg_ctx;
335 }
336 } else {
337 packet_length_size = 0;
338 msg_ctx->msg_size = 0;
339 }
Tyler Hicks48399c02012-01-14 16:46:46 +0100340 total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
341 + msg_ctx->msg_size);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700342 if (count < total_length) {
343 rc = 0;
344 printk(KERN_WARNING "%s: Only given user buffer of "
Michael Halcrowdf261c52009-01-06 14:42:02 -0800345 "size [%zd], but we need [%zd] to read the "
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700346 "pending message\n", __func__, count, total_length);
347 goto out_unlock_msg_ctx;
348 }
Al Viro79bc12a2008-05-21 06:32:11 +0100349 rc = -EFAULT;
350 if (put_user(msg_ctx->type, buf))
351 goto out_unlock_msg_ctx;
Tyler Hicks48399c02012-01-14 16:46:46 +0100352 if (put_user(cpu_to_be32(msg_ctx->counter),
353 (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
Al Viro79bc12a2008-05-21 06:32:11 +0100354 goto out_unlock_msg_ctx;
Tyler Hicks48399c02012-01-14 16:46:46 +0100355 i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700356 if (msg_ctx->msg) {
Al Viro79bc12a2008-05-21 06:32:11 +0100357 if (copy_to_user(&buf[i], packet_length, packet_length_size))
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700358 goto out_unlock_msg_ctx;
Al Viro79bc12a2008-05-21 06:32:11 +0100359 i += packet_length_size;
360 if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
361 goto out_unlock_msg_ctx;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700362 i += msg_ctx->msg_size;
363 }
364 rc = i;
365 list_del(&msg_ctx->daemon_out_list);
366 kfree(msg_ctx->msg);
367 msg_ctx->msg = NULL;
368 /* We do not expect a reply from the userspace daemon for any
369 * message type other than ECRYPTFS_MSG_REQUEST */
370 if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
371 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
372out_unlock_msg_ctx:
373 mutex_unlock(&msg_ctx->mux);
374out_unlock_daemon:
375 daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
376 mutex_unlock(&daemon->mux);
377 return rc;
378}
379
380/**
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700381 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
382 * @data: Bytes comprising struct ecryptfs_message
383 * @data_size: sizeof(struct ecryptfs_message) + data len
384 * @euid: Effective user id of miscdevess sending the miscdev response
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700385 * @user_ns: The namespace in which @euid applies
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700386 * @pid: Miscdevess id of miscdevess sending the miscdev response
387 * @seq: Sequence number for miscdev response packet
388 *
389 * Returns zero on success; non-zero otherwise
390 */
391static int ecryptfs_miscdev_response(char *data, size_t data_size,
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700392 uid_t euid, struct user_namespace *user_ns,
393 struct pid *pid, u32 seq)
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700394{
395 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
396 int rc;
397
398 if ((sizeof(*msg) + msg->data_len) != data_size) {
399 printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
Michael Halcrowdf261c52009-01-06 14:42:02 -0800400 "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700401 (sizeof(*msg) + msg->data_len), data_size);
402 rc = -EINVAL;
403 goto out;
404 }
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700405 rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700406 if (rc)
407 printk(KERN_ERR
408 "Error processing response message; rc = [%d]\n", rc);
409out:
410 return rc;
411}
412
413/**
414 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
415 * @file: File for misc dev handle (ignored)
416 * @buf: Buffer containing user data
417 * @count: Amount of data in @buf
418 * @ppos: Pointer to offset in file (ignored)
419 *
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700420 * Returns the number of bytes read from @buf
421 */
422static ssize_t
423ecryptfs_miscdev_write(struct file *file, const char __user *buf,
424 size_t count, loff_t *ppos)
425{
Al Viro79bc12a2008-05-21 06:32:11 +0100426 __be32 counter_nbo;
427 u32 seq;
Tyler Hicks48399c02012-01-14 16:46:46 +0100428 size_t packet_size, packet_size_length;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700429 char *data;
David Howells4eea0352008-11-14 10:38:49 +1100430 uid_t euid = current_euid();
Tyler Hicks48399c02012-01-14 16:46:46 +0100431 unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
Tyler Hicks7f133502012-01-14 15:51:37 +0100432 ssize_t rc;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700433
Tyler Hicksdb10e552012-01-12 11:30:44 +0100434 if (count == 0) {
Tyler Hicks7f133502012-01-14 15:51:37 +0100435 return 0;
Tyler Hicks48399c02012-01-14 16:46:46 +0100436 } else if (count == MIN_NON_MSG_PKT_SIZE) {
Tyler Hicksdb10e552012-01-12 11:30:44 +0100437 /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
438 goto memdup;
Tyler Hicks48399c02012-01-14 16:46:46 +0100439 } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
Tyler Hicksdb10e552012-01-12 11:30:44 +0100440 printk(KERN_WARNING "%s: Acceptable packet size range is "
Randy Dunlap164974a2012-02-28 16:31:12 -0800441 "[%d-%zu], but amount of data written is [%zu].",
Tyler Hicks48399c02012-01-14 16:46:46 +0100442 __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
Tyler Hicksdb10e552012-01-12 11:30:44 +0100443 return -EINVAL;
444 }
Li Zefanfd56d242009-04-08 15:09:29 +0800445
Tyler Hicks48399c02012-01-14 16:46:46 +0100446 if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
Tyler Hicksdb10e552012-01-12 11:30:44 +0100447 sizeof(packet_size_peek))) {
448 printk(KERN_WARNING "%s: Error while inspecting packet size\n",
449 __func__);
450 return -EFAULT;
451 }
452
453 rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
454 &packet_size_length);
455 if (rc) {
456 printk(KERN_WARNING "%s: Error parsing packet length; "
Tyler Hicks7f133502012-01-14 15:51:37 +0100457 "rc = [%zd]\n", __func__, rc);
Tyler Hicksdb10e552012-01-12 11:30:44 +0100458 return rc;
459 }
460
Tyler Hicks48399c02012-01-14 16:46:46 +0100461 if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
462 != count) {
Tyler Hicksdb10e552012-01-12 11:30:44 +0100463 printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
464 packet_size);
465 return -EINVAL;
466 }
467
468memdup:
Li Zefanfd56d242009-04-08 15:09:29 +0800469 data = memdup_user(buf, count);
470 if (IS_ERR(data)) {
471 printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
472 __func__, PTR_ERR(data));
Tyler Hicks7f133502012-01-14 15:51:37 +0100473 return PTR_ERR(data);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700474 }
Tyler Hicks48399c02012-01-14 16:46:46 +0100475 switch (data[PKT_TYPE_OFFSET]) {
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700476 case ECRYPTFS_MSG_RESPONSE:
Tyler Hicks48399c02012-01-14 16:46:46 +0100477 if (count < (MIN_MSG_PKT_SIZE
478 + sizeof(struct ecryptfs_message))) {
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700479 printk(KERN_WARNING "%s: Minimum acceptable packet "
Michael Halcrowdf261c52009-01-06 14:42:02 -0800480 "size is [%zd], but amount of data written is "
481 "only [%zd]. Discarding response packet.\n",
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700482 __func__,
Tyler Hicks48399c02012-01-14 16:46:46 +0100483 (MIN_MSG_PKT_SIZE
484 + sizeof(struct ecryptfs_message)), count);
Tyler Hicks7f133502012-01-14 15:51:37 +0100485 rc = -EINVAL;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700486 goto out_free;
487 }
Tyler Hicks48399c02012-01-14 16:46:46 +0100488 memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700489 seq = be32_to_cpu(counter_nbo);
Tyler Hicks48399c02012-01-14 16:46:46 +0100490 rc = ecryptfs_miscdev_response(
491 &data[PKT_LEN_OFFSET + packet_size_length],
492 packet_size, euid, current_user_ns(),
493 task_pid(current), seq);
Tyler Hicks7f133502012-01-14 15:51:37 +0100494 if (rc) {
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700495 printk(KERN_WARNING "%s: Failed to deliver miscdev "
Tyler Hicks7f133502012-01-14 15:51:37 +0100496 "response to requesting operation; rc = [%zd]\n",
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700497 __func__, rc);
Tyler Hicks7f133502012-01-14 15:51:37 +0100498 goto out_free;
499 }
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700500 break;
501 case ECRYPTFS_MSG_HELO:
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700502 case ECRYPTFS_MSG_QUIT:
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700503 break;
504 default:
505 ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
506 "message of unrecognized type [%d]\n",
507 data[0]);
Tyler Hicks7f133502012-01-14 15:51:37 +0100508 rc = -EINVAL;
509 goto out_free;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700510 }
Tyler Hicks7f133502012-01-14 15:51:37 +0100511 rc = count;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700512out_free:
513 kfree(data);
Tyler Hicks7f133502012-01-14 15:51:37 +0100514 return rc;
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700515}
516
517
518static const struct file_operations ecryptfs_miscdev_fops = {
519 .open = ecryptfs_miscdev_open,
520 .poll = ecryptfs_miscdev_poll,
521 .read = ecryptfs_miscdev_read,
522 .write = ecryptfs_miscdev_write,
523 .release = ecryptfs_miscdev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200524 .llseek = noop_llseek,
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700525};
526
527static struct miscdevice ecryptfs_miscdev = {
528 .minor = MISC_DYNAMIC_MINOR,
529 .name = "ecryptfs",
530 .fops = &ecryptfs_miscdev_fops
531};
532
533/**
534 * ecryptfs_init_ecryptfs_miscdev
535 *
536 * Messages sent to the userspace daemon from the kernel are placed on
537 * a queue associated with the daemon. The next read against the
538 * miscdev handle by that daemon will return the oldest message placed
539 * on the message queue for the daemon.
540 *
541 * Returns zero on success; non-zero otherwise
542 */
Jerome Marchand7371a382010-08-17 17:24:05 +0200543int __init ecryptfs_init_ecryptfs_miscdev(void)
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700544{
545 int rc;
546
547 atomic_set(&ecryptfs_num_miscdev_opens, 0);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700548 rc = misc_register(&ecryptfs_miscdev);
549 if (rc)
550 printk(KERN_ERR "%s: Failed to register miscellaneous device "
551 "for communications with userspace daemons; rc = [%d]\n",
552 __func__, rc);
Michael Halcrow8bf2deb2008-04-29 00:59:50 -0700553 return rc;
554}
555
556/**
557 * ecryptfs_destroy_ecryptfs_miscdev
558 *
559 * All of the daemons must be exorcised prior to calling this
560 * function.
561 */
562void ecryptfs_destroy_ecryptfs_miscdev(void)
563{
564 BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
565 misc_deregister(&ecryptfs_miscdev);
566}