From patchwork Tue Oct 31 20:05:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10035351 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 33CB2602B9 for ; Tue, 31 Oct 2017 20:05:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 239ED28A9D for ; Tue, 31 Oct 2017 20:05:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 183DD28AD6; Tue, 31 Oct 2017 20:05:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7FC8B28A9D for ; Tue, 31 Oct 2017 20:05:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932856AbdJaUFJ (ORCPT ); Tue, 31 Oct 2017 16:05:09 -0400 Received: from mail-io0-f193.google.com ([209.85.223.193]:52523 "EHLO mail-io0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932868AbdJaUFG (ORCPT ); Tue, 31 Oct 2017 16:05:06 -0400 Received: by mail-io0-f193.google.com with SMTP id f20so1447812ioj.9 for ; Tue, 31 Oct 2017 13:05:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=3+KsmTWrhP4ue9Hr2vIqys/6AiuKDBn1jcV5DA6KGUY=; b=ca3SqPbMbIlEwtb9AVPSNlZu+GUEiauEuimaeaF40OseJGWTNOHh6D4EGDKV1htAmT z4i9eA2DNkkCOiUDu0aTl7J4/QNAPncKHI/wg8Ewyt0kc3NFry26ZWBZiiPB5folWHsx saLRa61YuUkCrYp8gBhZ+++40t7BY1LXh3ElQdnMZWJxOXV6tUPOqXjk9YHEciHdOkgQ 0wuTQHzCF3y7LtvDM/QJwjxHO6fiKYQoT1kbJxr6A6x7pgFgiR+SHFrjIRsLH9gwEm+6 5PX+hOvlsG1V1CJCUgBseL4Tb/VL8cRXSMxt486A2EPNz9bqtfxggukkhzbeLGak3WR/ XsVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=3+KsmTWrhP4ue9Hr2vIqys/6AiuKDBn1jcV5DA6KGUY=; b=Qg18AqHUE7S+rWYLlmWorZdG+hPDF2XWVXY4fdrli6v/Udf/Z1BP238sFKzSglPftM RiR9m6Ns3ylFEWyNc2dk5Uv7oaKhf7MWE8DSR+7x6O5DfNJU1cbfIS6o9z2sCuvnZVSw Ltg7z0BWIe7h9GBBmolKgA5tRClgrGb0sZAoBmsBSvGTZfD673Svq/UN8gESM3dTWgyM HcsNOazibbgbGO3CJpGT2/qr3d6JV3WR2PLk+twkCG+gzsidhMgqxj+64p5cKdFlM3/j wfYySUWZnS+ffqO6X71gbKokeDMDGLb+syhKDrqhoyaL9gywi6J//NG4KvikrHGdWSoh M/qw== X-Gm-Message-State: AMCzsaXt1QWrvyDA9AcJHv5rWLuF3FQXckKyZ6o+ZgzBGI2bQDhyKl8D 1hBZ8x7595iy+JqyaI1ItUxv6A== X-Google-Smtp-Source: ABhQp+RyvPADdE6HGuAILjy/z959UkI45KuIvWWh3qAvGdy8Cwhszqo4+wgRqjK5AsxNH7ukML6DmQ== X-Received: by 10.107.68.20 with SMTP id r20mr3972705ioa.133.1509480305150; Tue, 31 Oct 2017 13:05:05 -0700 (PDT) Received: from ziepe.ca (S010614cc2056d97f.ed.shawcable.net. [70.74.179.152]) by smtp.gmail.com with ESMTPSA id g79sm1243547itb.43.2017.10.31.13.05.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Oct 2017 13:05:04 -0700 (PDT) Received: from jgg by wakko.ziepe.ca with local (Exim 4.86_2) (envelope-from ) id 1e9cmV-0008U4-Ls; Tue, 31 Oct 2017 14:05:03 -0600 Date: Tue, 31 Oct 2017 14:05:03 -0600 From: Jason Gunthorpe To: Jason Gunthorpe Cc: Jarkko Sakkinen , PrasannaKumar Muralidharan , Stefan Berger , linux-integrity@vger.kernel.org, David Howells , Herbert Xu , Dmitry Kasatkin , open list , "open list:INTEGRITY MEASUREMENT ARCHITECTURE (IMA)" , "open list:KEYS-TRUSTED" , "open list:HARDWARE RANDOM NUMBER GENERATOR CORE" , James Morris , Matt Mackall , David Safford , Mimi Zohar , "Serge E. Hallyn" Subject: [PATCH v2] tpm: Move Linux RNG connection to hwrng Message-ID: <20171031200503.GC18578@ziepe.ca> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The tpm-rng.c approach is completely inconsistent with how the kernel handles hotplug. Instead manage a hwrng device for each TPM. This will cause the kernel to read entropy from the TPM when it is plugged in, and allow access to the TPM rng via /dev/hwrng. Signed-off-by: PrasannaKumar Muralidharan Signed-off-by: Jason Gunthorpe Tested-by: PrasannaKumar Muralidharan Tested-by: Jarkko Sakkinen Reviewed-by: Jarkko Sakkinen --- drivers/char/hw_random/Kconfig | 13 ----------- drivers/char/hw_random/Makefile | 1 - drivers/char/hw_random/tpm-rng.c | 50 ---------------------------------------- drivers/char/tpm/Kconfig | 11 +++++++++ drivers/char/tpm/tpm-chip.c | 41 ++++++++++++++++++++++++++++---- drivers/char/tpm/tpm.h | 4 ++++ 6 files changed, 52 insertions(+), 68 deletions(-) delete mode 100644 drivers/char/hw_random/tpm-rng.c v2 applies against Jarkko's patch "tpm: use struct tpm_chip for tpm_chip_find_get()" diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig index 95a031e9eced07..a20fed182cbcce 100644 --- a/drivers/char/hw_random/Kconfig +++ b/drivers/char/hw_random/Kconfig @@ -306,19 +306,6 @@ config HW_RANDOM_POWERNV If unsure, say Y. -config HW_RANDOM_TPM - tristate "TPM HW Random Number Generator support" - depends on TCG_TPM - default HW_RANDOM - ---help--- - This driver provides kernel-side support for the Random Number - Generator in the Trusted Platform Module - - To compile this driver as a module, choose M here: the - module will be called tpm-rng. - - If unsure, say Y. - config HW_RANDOM_HISI tristate "Hisilicon Random Number Generator support" depends on HW_RANDOM && ARCH_HISI diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile index 39a67defac67cb..91cb8e8213e7c1 100644 --- a/drivers/char/hw_random/Makefile +++ b/drivers/char/hw_random/Makefile @@ -26,7 +26,6 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o obj-$(CONFIG_HW_RANDOM_HISI) += hisi-rng.o -obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o diff --git a/drivers/char/hw_random/tpm-rng.c b/drivers/char/hw_random/tpm-rng.c deleted file mode 100644 index c5e363825af008..00000000000000 --- a/drivers/char/hw_random/tpm-rng.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2012 Kent Yoder IBM Corporation - * - * HWRNG interfaces to pull RNG data from a TPM - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include -#include -#include - -#define MODULE_NAME "tpm-rng" - -static int tpm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) -{ - return tpm_get_random(NULL, data, max); -} - -static struct hwrng tpm_rng = { - .name = MODULE_NAME, - .read = tpm_rng_read, -}; - -static int __init rng_init(void) -{ - return hwrng_register(&tpm_rng); -} -module_init(rng_init); - -static void __exit rng_exit(void) -{ - hwrng_unregister(&tpm_rng); -} -module_exit(rng_exit); - -MODULE_LICENSE("GPL v2"); -MODULE_AUTHOR("Kent Yoder "); -MODULE_DESCRIPTION("RNG driver for TPM devices"); diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index a30352202f1fdc..18c81cbe4704ca 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig @@ -26,6 +26,17 @@ menuconfig TCG_TPM if TCG_TPM +config HW_RANDOM_TPM + bool "TPM HW Random Number Generator support" + depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m) + default y + ---help--- + This setting exposes the TPM's Random Number Generator as a hwrng + device. This allows the kernel to collect randomness from the TPM at + boot, and provides the TPM randomines in /dev/hwrng. + + If unsure, say Y. + config TCG_TIS_CORE tristate ---help--- diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 6faf4c3fa6dbbd..797ab962291333 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "tpm.h" #include "tpm_eventlog.h" @@ -391,6 +392,26 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip) return 0; } + +static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) +{ + struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); + + return tpm_get_random(chip, data, max); +} + +static int tpm_add_hwrng(struct tpm_chip *chip) +{ + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM)) + return 0; + + snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), + "tpm-rng-%d", chip->dev_num); + chip->hwrng.name = chip->hwrng_name; + chip->hwrng.read = tpm_hwrng_read; + return hwrng_register(&chip->hwrng); +} + /* * tpm_chip_register() - create a character device for the TPM chip * @chip: TPM chip to use. @@ -423,11 +444,13 @@ int tpm_chip_register(struct tpm_chip *chip) tpm_add_ppi(chip); + rc = tpm_add_hwrng(chip); + if (rc) + goto out_ppi; + rc = tpm_add_char_device(chip); - if (rc) { - tpm_bios_log_teardown(chip); - return rc; - } + if (rc) + goto out_hwrng; rc = tpm_add_legacy_sysfs(chip); if (rc) { @@ -436,6 +459,14 @@ int tpm_chip_register(struct tpm_chip *chip) } return 0; + +out_hwrng: + if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) + hwrng_unregister(&chip->hwrng); +out_ppi: + tpm_bios_log_teardown(chip); + + return rc; } EXPORT_SYMBOL_GPL(tpm_chip_register); @@ -455,6 +486,8 @@ EXPORT_SYMBOL_GPL(tpm_chip_register); void tpm_chip_unregister(struct tpm_chip *chip) { tpm_del_legacy_sysfs(chip); + if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) + hwrng_unregister(&chip->hwrng); tpm_bios_log_teardown(chip); if (chip->flags & TPM_CHIP_FLAG_TPM2) cdev_device_del(&chip->cdevs, &chip->devs); diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 7424511241031a..efe8ad0d137f74 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -210,6 +211,9 @@ struct tpm_chip { int dev_num; /* /dev/tpm# */ unsigned long is_open; /* only one allowed */ + char hwrng_name[64]; + struct hwrng hwrng; + struct mutex tpm_mutex; /* tpm is processing */ unsigned long timeout_a; /* jiffies */