tpm-dev-common.c 3.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2004 IBM Corporation
 * Authors:
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Copyright (C) 2013 Obsidian Research Corp
 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
 *
 * Device file system interface to the TPM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 */
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "tpm.h"
#include "tpm-dev.h"

25
static void user_reader_timeout(struct timer_list *t)
26
{
27
	struct file_priv *priv = from_timer(priv, t, user_read_timer);
28 29 30 31 32 33 34 35 36 37 38 39

	pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
		task_tgid_nr(current));

	schedule_work(&priv->work);
}

static void timeout_work(struct work_struct *work)
{
	struct file_priv *priv = container_of(work, struct file_priv, work);

	mutex_lock(&priv->buffer_mutex);
40
	priv->data_pending = 0;
41 42 43 44 45 46 47 48 49
	memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
	mutex_unlock(&priv->buffer_mutex);
}

void tpm_common_open(struct file *file, struct tpm_chip *chip,
		     struct file_priv *priv)
{
	priv->chip = chip;
	mutex_init(&priv->buffer_mutex);
50
	timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
51 52 53 54 55 56 57 58 59
	INIT_WORK(&priv->work, timeout_work);

	file->private_data = priv;
}

ssize_t tpm_common_read(struct file *file, char __user *buf,
			size_t size, loff_t *off)
{
	struct file_priv *priv = file->private_data;
60
	ssize_t ret_size = 0;
61 62 63 64
	int rc;

	del_singleshot_timer_sync(&priv->user_read_timer);
	flush_work(&priv->work);
65
	mutex_lock(&priv->buffer_mutex);
66

67 68
	if (priv->data_pending) {
		ret_size = min_t(ssize_t, size, priv->data_pending);
69
		rc = copy_to_user(buf, priv->data_buffer, ret_size);
70
		memset(priv->data_buffer, 0, priv->data_pending);
71 72 73
		if (rc)
			ret_size = -EFAULT;

74
		priv->data_pending = 0;
75 76
	}

77
	mutex_unlock(&priv->buffer_mutex);
78 79 80 81 82 83 84 85 86 87
	return ret_size;
}

ssize_t tpm_common_write(struct file *file, const char __user *buf,
			 size_t size, loff_t *off, struct tpm_space *space)
{
	struct file_priv *priv = file->private_data;
	size_t in_size = size;
	ssize_t out_size;

88 89 90 91 92
	if (in_size > TPM_BUFSIZE)
		return -E2BIG;

	mutex_lock(&priv->buffer_mutex);

93 94 95 96
	/* Cannot perform a write until the read has cleared either via
	 * tpm_read or a user_read_timer timeout. This also prevents split
	 * buffered writes from blocking here.
	 */
97 98
	if (priv->data_pending != 0) {
		mutex_unlock(&priv->buffer_mutex);
99
		return -EBUSY;
100
	}
101 102 103 104 105 106 107

	if (copy_from_user
	    (priv->data_buffer, (void __user *) buf, in_size)) {
		mutex_unlock(&priv->buffer_mutex);
		return -EFAULT;
	}

108 109 110 111 112 113
	if (in_size < 6 ||
	    in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
		mutex_unlock(&priv->buffer_mutex);
		return -EINVAL;
	}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	/* atomic tpm command send and result receive. We only hold the ops
	 * lock during this period so that the tpm can be unregistered even if
	 * the char dev is held open.
	 */
	if (tpm_try_get_ops(priv->chip)) {
		mutex_unlock(&priv->buffer_mutex);
		return -EPIPE;
	}
	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
				sizeof(priv->data_buffer), 0);

	tpm_put_ops(priv->chip);
	if (out_size < 0) {
		mutex_unlock(&priv->buffer_mutex);
		return out_size;
	}

131
	priv->data_pending = out_size;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	mutex_unlock(&priv->buffer_mutex);

	/* Set a timeout by which the reader must come claim the result */
	mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));

	return in_size;
}

/*
 * Called on file close
 */
void tpm_common_release(struct file *file, struct file_priv *priv)
{
	del_singleshot_timer_sync(&priv->user_read_timer);
	flush_work(&priv->work);
	file->private_data = NULL;
148
	priv->data_pending = 0;
149
}