Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp939585pxb; Tue, 3 Nov 2020 17:20:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPmGTjGtkvfAyz5Oy3xuUQBNKYE5nghojDah0p0ZFQ4/YVbEIXl2RW92R0QLr47Wx615zu X-Received: by 2002:a17:906:4d93:: with SMTP id s19mr18736911eju.271.1604452807864; Tue, 03 Nov 2020 17:20:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604452807; cv=none; d=google.com; s=arc-20160816; b=Wf2lmJG3O2HRXK7OAV6cJJeum7XU9YUvKaqDoFdgBmGlWzKWmzJCCTNHol4DmWyy7N 2IBqDuLONnAOxdxLd/CCnuspY4ZO9MNnGJu5xu6LnhhV9lDMkYU7gpgOn4Sfr5qknHWi X1dHS7C34qcrjMi1xnPglpjco/qrrVo+fdhmw6kR1vueh1WT4ib/hHL2tFS0xRGJgqB/ xmfCCzRMqvblc+Ys8lw0o9fyzXd0tpeg9/g4ThjbkQuKo/cUXnD3j7/wsUDjKwiwzZ/v AIZk/16AhplP5N2a303jpJuQCUlD8WJsTOLy6yUYXZJxRPSPqTwjN0RkNVKVc5rav5bW jk6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=/R0Ay56nYLnuVFL0f7VYMxs0+VRMV1FUu6U6s9GSR2k=; b=VDm7u721EYeX8MQot5PE4cA0Kxx16Oap+7N8MWGUwLMyFRsSFWbiJxZfSLTFwKNf3C NQ2UhEXBx1vg6TZpPRkMoTQU1bVzogdyFSphi7UlfNK53XPg1DFETU2+ErlOhctgFdRH 8fgm5hCg6984ohh2xOoK5bdK1yfbq6glhDsuNXuNX+mk30ohqIlT+h6AWJEisF5iANER V+H5F9Cw5NrjHBApZ8/mvrtX1NQrQ4D+VuCV0HKTYgBjV0E7yVSI+YprS1Wrk9mdXqJX RL+q+kxhLWLauj16RZSdN9w5D5nSzBOORyF7RD09O/Pb9Ofi0Y1h1JCvNWNz0MJu5tHV zqFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si380678ejj.231.2020.11.03.17.19.44; Tue, 03 Nov 2020 17:20:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730335AbgKDBSY (ORCPT + 99 others); Tue, 3 Nov 2020 20:18:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:49732 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728157AbgKDBSY (ORCPT ); Tue, 3 Nov 2020 20:18:24 -0500 Received: from kernel.org (83-245-197-237.elisa-laajakaista.fi [83.245.197.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7D068223EA; Wed, 4 Nov 2020 01:18:20 +0000 (UTC) Date: Wed, 4 Nov 2020 03:18:17 +0200 From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: David Howells , Mimi Zohar , James Bottomley , Jarkko Sakkinen , stable@vger.kernel.org, "James E.J. Bottomley" , Kent Yoder , James Morris , "Serge E. Hallyn" , "H. Peter Anvin" , David Safford , "open list:KEYS-TRUSTED" , "open list:SECURITY SUBSYSTEM" , open list Subject: [PATCH v4 1/3,RESEND 2] KEYS: trusted: Fix incorrect handling of tpm_get_random() Message-ID: <20201104011817.GB20387@kernel.org> References: <20201013025156.111305-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201013025156.111305-1-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When tpm_get_random() was introduced, it defined the following API for the return value: 1. A positive value tells how many bytes of random data was generated. 2. A negative value on error. However, in the call sites the API was used incorrectly, i.e. as it would only return negative values and otherwise zero. Returning he positive read counts to the user space does not make any possible sense. Fix this by returning -EIO when tpm_get_random() returns a positive value. Fixes: 41ab999c80f1 ("tpm: Move tpm_get_random api into the TPM device driver") Cc: stable@vger.kernel.org Cc: Mimi Zohar Cc: "James E.J. Bottomley" Cc: David Howells Cc: Kent Yoder Signed-off-by: Jarkko Sakkinen --- security/keys/trusted-keys/trusted_tpm1.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c index b9fe02e5f84f..c7b1701cdac5 100644 --- a/security/keys/trusted-keys/trusted_tpm1.c +++ b/security/keys/trusted-keys/trusted_tpm1.c @@ -403,9 +403,12 @@ static int osap(struct tpm_buf *tb, struct osapsess *s, int ret; ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE); - if (ret != TPM_NONCE_SIZE) + if (ret < 0) return ret; + if (ret != TPM_NONCE_SIZE) + return -EIO; + tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP); tpm_buf_append_u16(tb, type); tpm_buf_append_u32(tb, handle); @@ -496,8 +499,12 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype, goto out; ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE); + if (ret < 0) + return ret; + if (ret != TPM_NONCE_SIZE) - goto out; + return -EIO; + ordinal = htonl(TPM_ORD_SEAL); datsize = htonl(datalen); pcrsize = htonl(pcrinfosize); @@ -601,9 +608,12 @@ static int tpm_unseal(struct tpm_buf *tb, ordinal = htonl(TPM_ORD_UNSEAL); ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE); + if (ret < 0) + return ret; + if (ret != TPM_NONCE_SIZE) { pr_info("trusted_key: tpm_get_random failed (%d)\n", ret); - return ret; + return -EIO; } ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE, enonce1, nonceodd, cont, sizeof(uint32_t), @@ -1013,8 +1023,12 @@ static int trusted_instantiate(struct key *key, case Opt_new: key_len = payload->key_len; ret = tpm_get_random(chip, payload->key, key_len); + if (ret < 0) + goto out; + if (ret != key_len) { pr_info("trusted_key: key_create failed (%d)\n", ret); + ret = -EIO; goto out; } if (tpm2) -- 2.25.1