From patchwork Fri Mar 22 18:01:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 10866387 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1AC431708 for ; Fri, 22 Mar 2019 18:04:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F16CB2A79B for ; Fri, 22 Mar 2019 18:04:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E60522A7B4; Fri, 22 Mar 2019 18:04:37 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 799052A79B for ; Fri, 22 Mar 2019 18:04:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 71A6B211E8310; Fri, 22 Mar 2019 11:04:37 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=185.176.76.210; helo=huawei.com; envelope-from=roberto.sassu@huawei.com; receiver=linux-nvdimm@lists.01.org Received: from huawei.com (lhrrgout.huawei.com [185.176.76.210]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B43FA211E8300 for ; Fri, 22 Mar 2019 11:04:36 -0700 (PDT) Received: from lhreml708-cah.china.huawei.com (unknown [172.18.7.108]) by Forcepoint Email with ESMTP id E0BA87EBD14F17CE7D47; Fri, 22 Mar 2019 18:04:34 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.154) by smtpsuk.huawei.com (10.201.108.49) with Microsoft SMTP Server (TLS) id 14.3.408.0; Fri, 22 Mar 2019 18:04:24 +0000 From: Roberto Sassu To: , , , Subject: [PATCH] KEYS: trusted: defer execution of TPM-specific code until key instantiate Date: Fri, 22 Mar 2019 19:01:39 +0100 Message-ID: <20190322180139.18856-1-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.204.65.154] X-CFilter-Loop: Reflected X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: silviu.vlasceanu@huawei.com, linux-nvdimm@lists.01.org, jejb@linux.ibm.com, Roberto Sassu , linux-kernel@vger.kernel.org, david.safford@ge.com, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, stable@vger.kernel.org, linux-integrity@vger.kernel.org Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP Commit 240730437deb ("KEYS: trusted: explicitly use tpm_chip structure from tpm_default_chip()") changed the tpm_chip argument of every TPM function from NULL to a pointer that is retrieved at module initialization time. Unlike before this patch, the trusted module cannot be loaded if no TPM is available. Unfortunately, this causes a dependency problem because the encrypted key type requires the 'key_type_trusted' symbol when CONFIG_TRUSTED_KEYS is defined. This patch fixes the issue by deferring the execution of TPM-specific code until a new trusted key is instantiated: init_tpm(), to obtain a tpm_chip pointer; init_digests(), introduced by commit 0b6cf6b97b7e ("tpm: pass an array of tpm_extend_digest structures to tpm_pcr_extend()"), to get random bytes from the TPM to lock a PCR. Cc: stable@vger.kernel.org Fixes: 240730437deb ("KEYS: trusted: explicitly use tpm_chip structure from tpm_default_chip()") Reported-by: Dan Williams Signed-off-by: Roberto Sassu Tested-by: Dan Williams --- security/keys/trusted.c | 89 +++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/security/keys/trusted.c b/security/keys/trusted.c index ecec672d3a77..c5162ca9c944 100644 --- a/security/keys/trusted.c +++ b/security/keys/trusted.c @@ -946,6 +946,44 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key) return p; } +static int init_tpm(void) +{ + if (chip) + return 0; + + chip = tpm_default_chip(); + if (!chip) + return -ENODEV; + + return 0; +} + +static int init_digests(void) +{ + u8 digest[TPM_MAX_DIGEST_SIZE]; + int ret; + int i; + + if (digests) + return 0; + + ret = tpm_get_random(chip, digest, TPM_MAX_DIGEST_SIZE); + if (ret < 0) + return ret; + if (ret < TPM_MAX_DIGEST_SIZE) + return -EFAULT; + + digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests), + GFP_KERNEL); + if (!digests) + return -ENOMEM; + + for (i = 0; i < chip->nr_allocated_banks; i++) + memcpy(digests[i].digest, digest, TPM_MAX_DIGEST_SIZE); + + return 0; +} + /* * trusted_instantiate - create a new trusted key * @@ -967,6 +1005,14 @@ static int trusted_instantiate(struct key *key, size_t key_len; int tpm2; + ret = init_tpm(); + if (ret < 0) + return ret; + + ret = init_digests(); + if (ret < 0) + return ret; + tpm2 = tpm_is_tpm2(chip); if (tpm2 < 0) return tpm2; @@ -1218,58 +1264,23 @@ static int __init trusted_shash_alloc(void) return ret; } -static int __init init_digests(void) -{ - u8 digest[TPM_MAX_DIGEST_SIZE]; - int ret; - int i; - - ret = tpm_get_random(chip, digest, TPM_MAX_DIGEST_SIZE); - if (ret < 0) - return ret; - if (ret < TPM_MAX_DIGEST_SIZE) - return -EFAULT; - - digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests), - GFP_KERNEL); - if (!digests) - return -ENOMEM; - - for (i = 0; i < chip->nr_allocated_banks; i++) - memcpy(digests[i].digest, digest, TPM_MAX_DIGEST_SIZE); - - return 0; -} - static int __init init_trusted(void) { int ret; - chip = tpm_default_chip(); - if (!chip) - return -ENOENT; - ret = init_digests(); - if (ret < 0) - goto err_put; ret = trusted_shash_alloc(); if (ret < 0) - goto err_free; + return ret; ret = register_key_type(&key_type_trusted); if (ret < 0) - goto err_release; - return 0; -err_release: - trusted_shash_release(); -err_free: - kfree(digests); -err_put: - put_device(&chip->dev); + trusted_shash_release(); return ret; } static void __exit cleanup_trusted(void) { - put_device(&chip->dev); + if (chip) + put_device(&chip->dev); kfree(digests); trusted_shash_release(); unregister_key_type(&key_type_trusted);