tpm: expose spaces via a device link /dev/tpmrm<n>
authorJames Bottomley <James.Bottomley@HansenPartnership.com>
Tue, 3 Jan 2017 17:07:32 +0000 (09:07 -0800)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Mon, 3 Apr 2017 19:46:02 +0000 (22:46 +0300)
Currently the tpm spaces are not exposed to userspace.  Make this
exposure via a separate device, which can now be opened multiple times
because each read/write transaction goes separately via the space.

Concurrency is protected by the chip->tpm_mutex for each read/write
transaction separately.  The TPM is cleared of all transient objects
by the time the mutex is dropped, so there should be no interference
between the kernel and userspace.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
drivers/char/tpm/Makefile
drivers/char/tpm/tpm-chip.c
drivers/char/tpm/tpm-interface.c
drivers/char/tpm/tpm.h
drivers/char/tpm/tpmrm-dev.c [new file with mode: 0644]

index 10e5827445fce4587c4690da103a3326c2d69011..23681f01f95a214d417b29298660fb3d1d5a5b01 100644 (file)
@@ -3,7 +3,8 @@
 #
 obj-$(CONFIG_TCG_TPM) += tpm.o
 tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o \
-        tpm-dev-common.o tpm1_eventlog.o tpm2_eventlog.o tpm2-space.o
+        tpm-dev-common.o tpmrm-dev.o tpm1_eventlog.o tpm2_eventlog.o \
+         tpm2-space.o
 tpm-$(CONFIG_ACPI) += tpm_ppi.o tpm_acpi.o
 tpm-$(CONFIG_OF) += tpm_of.o
 obj-$(CONFIG_TCG_TIS_CORE) += tpm_tis_core.o
index 993b9ae42876ae336426060ce293969817716b95..187ec04ce9c31bef1b73d184dc3865c8e2ab37a2 100644 (file)
@@ -33,6 +33,7 @@ DEFINE_IDR(dev_nums_idr);
 static DEFINE_MUTEX(idr_lock);
 
 struct class *tpm_class;
+struct class *tpmrm_class;
 dev_t tpm_devt;
 
 /**
@@ -132,6 +133,14 @@ static void tpm_dev_release(struct device *dev)
        kfree(chip);
 }
 
+static void tpm_devs_release(struct device *dev)
+{
+       struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
+
+       /* release the master device reference */
+       put_device(&chip->dev);
+}
+
 /**
  * tpm_chip_alloc() - allocate a new struct tpm_chip instance
  * @pdev: device to which the chip is associated
@@ -168,18 +177,35 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
        chip->dev_num = rc;
 
        device_initialize(&chip->dev);
+       device_initialize(&chip->devs);
 
        chip->dev.class = tpm_class;
        chip->dev.release = tpm_dev_release;
        chip->dev.parent = pdev;
        chip->dev.groups = chip->groups;
 
+       chip->devs.parent = pdev;
+       chip->devs.class = tpmrm_class;
+       chip->devs.release = tpm_devs_release;
+       /* get extra reference on main device to hold on
+        * behalf of devs.  This holds the chip structure
+        * while cdevs is in use.  The corresponding put
+        * is in the tpm_devs_release
+        */
+       get_device(&chip->dev);
+
        if (chip->dev_num == 0)
                chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
        else
                chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
 
+       chip->devs.devt =
+               MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
+
        rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
+       if (rc)
+               goto out;
+       rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
        if (rc)
                goto out;
 
@@ -187,8 +213,11 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
                chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
 
        cdev_init(&chip->cdev, &tpm_fops);
+       cdev_init(&chip->cdevs, &tpmrm_fops);
        chip->cdev.owner = THIS_MODULE;
+       chip->cdevs.owner = THIS_MODULE;
        chip->cdev.kobj.parent = &chip->dev.kobj;
+       chip->cdevs.kobj.parent = &chip->devs.kobj;
 
        chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
        if (!chip->work_space.context_buf) {
@@ -199,6 +228,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
        return chip;
 
 out:
+       put_device(&chip->devs);
        put_device(&chip->dev);
        return ERR_PTR(rc);
 }
@@ -243,7 +273,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
                        "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
                        dev_name(&chip->dev), MAJOR(chip->dev.devt),
                        MINOR(chip->dev.devt), rc);
-
                return rc;
        }
 
@@ -258,6 +287,29 @@ static int tpm_add_char_device(struct tpm_chip *chip)
                return rc;
        }
 
+       if (chip->flags & TPM_CHIP_FLAG_TPM2)
+               rc = cdev_add(&chip->cdevs, chip->devs.devt, 1);
+       if (rc) {
+               dev_err(&chip->dev,
+                       "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
+                       dev_name(&chip->devs), MAJOR(chip->devs.devt),
+                       MINOR(chip->devs.devt), rc);
+               tpm_del_char_device(chip, true);
+               return rc;
+       }
+
+       if (chip->flags & TPM_CHIP_FLAG_TPM2)
+               rc = device_add(&chip->devs);
+       if (rc) {
+               dev_err(&chip->dev,
+                       "unable to device_register() %s, major %d, minor %d, err=%d\n",
+                       dev_name(&chip->devs), MAJOR(chip->devs.devt),
+                       MINOR(chip->devs.devt), rc);
+               cdev_del(&chip->cdevs);
+               tpm_del_char_device(chip, true);
+               return rc;
+       }
+
        /* Make the chip available. */
        mutex_lock(&idr_lock);
        idr_replace(&dev_nums_idr, chip, chip->dev_num);
@@ -391,6 +443,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
 {
        tpm_del_legacy_sysfs(chip);
        tpm_bios_log_teardown(chip);
+       if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+               cdev_del(&chip->cdevs);
+               device_del(&chip->devs);
+       }
        tpm_del_char_device(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
index d09cf26365c3a236b30193f26623da2b11486645..16abbf9cb53a3f4111d596d5c31ab9152ad25dd9 100644 (file)
@@ -1262,9 +1262,17 @@ static int __init tpm_init(void)
                return PTR_ERR(tpm_class);
        }
 
-       rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
+       tpmrm_class = class_create(THIS_MODULE, "tpmrm");
+       if (IS_ERR(tpmrm_class)) {
+               pr_err("couldn't create tpmrm class\n");
+               class_destroy(tpm_class);
+               return PTR_ERR(tpmrm_class);
+       }
+
+       rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
        if (rc < 0) {
                pr_err("tpm: failed to allocate char dev region\n");
+               class_destroy(tpmrm_class);
                class_destroy(tpm_class);
                return rc;
        }
@@ -1276,7 +1284,8 @@ static void __exit tpm_exit(void)
 {
        idr_destroy(&dev_nums_idr);
        class_destroy(tpm_class);
-       unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
+       class_destroy(tpmrm_class);
+       unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
 }
 
 subsys_initcall(tpm_init);
index 023fc02ad0f663769067a6088f99af360aeb2bd0..16dd207a4542d4b9a13c95440a69254dc2abade9 100644 (file)
@@ -182,7 +182,9 @@ struct tpm_chip_seqops {
 
 struct tpm_chip {
        struct device dev;
+       struct device devs;
        struct cdev cdev;
+       struct cdev cdevs;
 
        /* A driver callback under ops cannot be run unless ops_sem is held
         * (sometimes implicitly, eg for the sysfs code). ops becomes null
@@ -510,8 +512,10 @@ static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
 }
 
 extern struct class *tpm_class;
+extern struct class *tpmrm_class;
 extern dev_t tpm_devt;
 extern const struct file_operations tpm_fops;
+extern const struct file_operations tpmrm_fops;
 extern struct idr dev_nums_idr;
 
 enum tpm_transmit_flags {
diff --git a/drivers/char/tpm/tpmrm-dev.c b/drivers/char/tpm/tpmrm-dev.c
new file mode 100644 (file)
index 0000000..630bddc
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2017 James.Bottomley@HansenPartnership.com
+ *
+ * GPLv2
+ */
+#include <linux/slab.h>
+#include "tpm-dev.h"
+
+struct tpmrm_priv {
+       struct file_priv priv;
+       struct tpm_space space;
+};
+
+static int tpmrm_open(struct inode *inode, struct file *file)
+{
+       struct tpm_chip *chip;
+       struct tpmrm_priv *priv;
+       int rc;
+
+       chip = container_of(inode->i_cdev, struct tpm_chip, cdevs);
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (priv == NULL)
+               return -ENOMEM;
+
+       rc = tpm2_init_space(&priv->space);
+       if (rc) {
+               kfree(priv);
+               return -ENOMEM;
+       }
+
+       tpm_common_open(file, chip, &priv->priv);
+
+       return 0;
+}
+
+static int tpmrm_release(struct inode *inode, struct file *file)
+{
+       struct file_priv *fpriv = file->private_data;
+       struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
+
+       tpm_common_release(file, fpriv);
+       tpm2_del_space(&priv->space);
+       kfree(priv);
+
+       return 0;
+}
+
+ssize_t tpmrm_write(struct file *file, const char __user *buf,
+                  size_t size, loff_t *off)
+{
+       struct file_priv *fpriv = file->private_data;
+       struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
+
+       return tpm_common_write(file, buf, size, off, &priv->space);
+}
+
+const struct file_operations tpmrm_fops = {
+       .owner = THIS_MODULE,
+       .llseek = no_llseek,
+       .open = tpmrm_open,
+       .read = tpm_common_read,
+       .write = tpmrm_write,
+       .release = tpmrm_release,
+};
+