Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM Corporation |
| 3 | * Authors: |
| 4 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 5 | * Dave Safford <safford@watson.ibm.com> |
| 6 | * Reiner Sailer <sailer@watson.ibm.com> |
| 7 | * Kylene Hall <kjhall@us.ibm.com> |
| 8 | * |
| 9 | * Copyright (C) 2013 Obsidian Research Corp |
| 10 | * Jason Gunthorpe <jgunthorpe@obsidianresearch.com> |
| 11 | * |
| 12 | * Device file system interface to the TPM |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation, version 2 of the |
| 17 | * License. |
| 18 | * |
| 19 | */ |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include "tpm.h" |
| 23 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 24 | struct file_priv { |
| 25 | struct tpm_chip *chip; |
| 26 | |
| 27 | /* Data passed to and from the tpm via the read/write calls */ |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 28 | size_t data_pending; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 29 | struct mutex buffer_mutex; |
| 30 | |
| 31 | struct timer_list user_read_timer; /* user needs to claim result */ |
| 32 | struct work_struct work; |
| 33 | |
| 34 | u8 data_buffer[TPM_BUFSIZE]; |
| 35 | }; |
| 36 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 37 | static void user_reader_timeout(unsigned long ptr) |
| 38 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 39 | struct file_priv *priv = (struct file_priv *)ptr; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 40 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 41 | schedule_work(&priv->work); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void timeout_work(struct work_struct *work) |
| 45 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 46 | struct file_priv *priv = container_of(work, struct file_priv, work); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 47 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 48 | mutex_lock(&priv->buffer_mutex); |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 49 | priv->data_pending = 0; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 50 | memset(priv->data_buffer, 0, sizeof(priv->data_buffer)); |
| 51 | mutex_unlock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int tpm_open(struct inode *inode, struct file *file) |
| 55 | { |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 56 | struct tpm_chip *chip = |
| 57 | container_of(inode->i_cdev, struct tpm_chip, cdev); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 58 | struct file_priv *priv; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 59 | |
| 60 | /* It's assured that the chip will be opened just once, |
| 61 | * by the check of is_open variable, which is protected |
| 62 | * by driver_lock. */ |
| 63 | if (test_and_set_bit(0, &chip->is_open)) { |
Jason Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 64 | dev_dbg(&chip->dev, "Another process owns this TPM\n"); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 65 | return -EBUSY; |
| 66 | } |
| 67 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 68 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 69 | if (priv == NULL) { |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 70 | clear_bit(0, &chip->is_open); |
| 71 | return -ENOMEM; |
| 72 | } |
| 73 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 74 | priv->chip = chip; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 75 | mutex_init(&priv->buffer_mutex); |
| 76 | setup_timer(&priv->user_read_timer, user_reader_timeout, |
| 77 | (unsigned long)priv); |
| 78 | INIT_WORK(&priv->work, timeout_work); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 79 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 80 | file->private_data = priv; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static ssize_t tpm_read(struct file *file, char __user *buf, |
| 85 | size_t size, loff_t *off) |
| 86 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 87 | struct file_priv *priv = file->private_data; |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 88 | ssize_t ret_size = 0; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 89 | int rc; |
| 90 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 91 | del_singleshot_timer_sync(&priv->user_read_timer); |
| 92 | flush_work(&priv->work); |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 93 | mutex_lock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 94 | |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 95 | if (priv->data_pending) { |
| 96 | ret_size = min_t(ssize_t, size, priv->data_pending); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 97 | rc = copy_to_user(buf, priv->data_buffer, ret_size); |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 98 | memset(priv->data_buffer, 0, priv->data_pending); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 99 | if (rc) |
| 100 | ret_size = -EFAULT; |
| 101 | |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 102 | priv->data_pending = 0; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 105 | mutex_unlock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 106 | return ret_size; |
| 107 | } |
| 108 | |
| 109 | static ssize_t tpm_write(struct file *file, const char __user *buf, |
| 110 | size_t size, loff_t *off) |
| 111 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 112 | struct file_priv *priv = file->private_data; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 113 | size_t in_size = size; |
| 114 | ssize_t out_size; |
| 115 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 116 | if (in_size > TPM_BUFSIZE) |
| 117 | return -E2BIG; |
| 118 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 119 | mutex_lock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 120 | |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 121 | /* Cannot perform a write until the read has cleared either via |
| 122 | * tpm_read or a user_read_timer timeout. This also prevents split |
| 123 | * buffered writes from blocking here. |
| 124 | */ |
| 125 | if (priv->data_pending != 0) { |
| 126 | mutex_unlock(&priv->buffer_mutex); |
| 127 | return -EBUSY; |
| 128 | } |
| 129 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 130 | if (copy_from_user |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 131 | (priv->data_buffer, (void __user *) buf, in_size)) { |
| 132 | mutex_unlock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 133 | return -EFAULT; |
| 134 | } |
| 135 | |
Alexander Steffen | 89f0fb9 | 2017-09-08 17:21:32 +0200 | [diff] [blame] | 136 | if (in_size < 6 || |
| 137 | in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) { |
| 138 | mutex_unlock(&priv->buffer_mutex); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 142 | /* atomic tpm command send and result receive. We only hold the ops |
| 143 | * lock during this period so that the tpm can be unregistered even if |
| 144 | * the char dev is held open. |
| 145 | */ |
| 146 | if (tpm_try_get_ops(priv->chip)) { |
| 147 | mutex_unlock(&priv->buffer_mutex); |
| 148 | return -EPIPE; |
| 149 | } |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 150 | out_size = tpm_transmit(priv->chip, priv->data_buffer, |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 151 | sizeof(priv->data_buffer), 0); |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 152 | |
| 153 | tpm_put_ops(priv->chip); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 154 | if (out_size < 0) { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 155 | mutex_unlock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 156 | return out_size; |
| 157 | } |
| 158 | |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 159 | priv->data_pending = out_size; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 160 | mutex_unlock(&priv->buffer_mutex); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 161 | |
| 162 | /* Set a timeout by which the reader must come claim the result */ |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 163 | mod_timer(&priv->user_read_timer, jiffies + (60 * HZ)); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 164 | |
| 165 | return in_size; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Called on file close |
| 170 | */ |
| 171 | static int tpm_release(struct inode *inode, struct file *file) |
| 172 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 173 | struct file_priv *priv = file->private_data; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 174 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 175 | del_singleshot_timer_sync(&priv->user_read_timer); |
| 176 | flush_work(&priv->work); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 177 | file->private_data = NULL; |
Tadeusz Struk | 1d4167a | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 178 | priv->data_pending = 0; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 179 | clear_bit(0, &priv->chip->is_open); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 180 | kfree(priv); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 184 | const struct file_operations tpm_fops = { |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 185 | .owner = THIS_MODULE, |
| 186 | .llseek = no_llseek, |
| 187 | .open = tpm_open, |
| 188 | .read = tpm_read, |
| 189 | .write = tpm_write, |
| 190 | .release = tpm_release, |
| 191 | }; |
| 192 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 193 | |