Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp767397yba; Mon, 1 Apr 2019 16:48:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3PzVDUmNytHmrj6k9fAEKbDETQ0vQLlbAFOcUS/pLvGhDLdx0CnxBXmnE378LyiSEH8U4 X-Received: by 2002:a65:6205:: with SMTP id d5mr8125585pgv.61.1554162507391; Mon, 01 Apr 2019 16:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554162507; cv=none; d=google.com; s=arc-20160816; b=ga9Kx6WqN0Fl7jw+7HDiXBmZxcx8ZIcJhMQ3pfhYu6ub35XfROj6VJtliB/h/wAQ/Y HnjeaER8zA+zvcl40VHB7r/UHSQtRbVsl+X67En6s7GZEILDcgh9SHyB333f4bsfdRK4 wO5G2HL+YkWDuDvk7twW5mgRKX0jAKkRjVca556mfIoeS1iv1/BKTiBgiSOaUw9hUUZ7 NM2w60DFX/tEV148y8Xd2WjQpci4PAjuPrYn0yEkhYo4+tnkFYhwcOcKOhH/NXkiocxa 8MH0ZYOeizcDphszeCIJVBHYyg8/LIjd/OrqMDfmqfoEZ0V7BMSNbZQdMuaC96+/LiL4 F27Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=KIsUjqupUsg6q9qosl2pZAGc5zq2pHaCX5DePb9ty84=; b=pkE5LGWFh2BE5UCjbkHc8kMOCjg1qe/IAh0eBHCu3z6WYFYSMEvSPM/zKwCVcCcBPw 4M+qDBU4lwxK7f2zmocfAoHn8lWXaFFcIiAqVSoBXYHHyLkbWUnUIIqQUAo1IThSDahR o81rOaZzNxpCSdx4cl4GPoIy5kYBeFaS55e2fkE5xTOPyGahU6Osqfk5NZYmTJRS7W6X 9IxoHmcax2scNccGIX4dRrGtKtEbOop+F8FDPdDDqliCmTrR6FkdqloZVfU9lseBb5D/ 7zko7qw/FUN2AwzljolbPJcx6TteeshO6zeduoob53ahTtpJkYyC27ah1Vxt5TzVLTd9 GkYQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id z13si9760261pgv.508.2019.04.01.16.47.58; Mon, 01 Apr 2019 16:48:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726738AbfDAXqd (ORCPT + 99 others); Mon, 1 Apr 2019 19:46:33 -0400 Received: from mga14.intel.com ([192.55.52.115]:27624 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725893AbfDAXqd (ORCPT ); Mon, 1 Apr 2019 19:46:33 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Apr 2019 16:46:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,298,1549958400"; d="scan'208";a="147173511" Received: from unknown (HELO localhost) ([10.249.254.142]) by orsmga002.jf.intel.com with ESMTP; 01 Apr 2019 16:46:27 -0700 Date: Tue, 2 Apr 2019 02:46:25 +0300 From: Jarkko Sakkinen To: Kees Cook Cc: Jason Gunthorpe , James Bottomley , Phil Baker , Craig Robson , Laura Abbott , Tomas Winkler , linux-kernel@vger.kernel.org, Peter Huewe , Arnd Bergmann , linux-integrity@vger.kernel.org Subject: Re: [PATCH v3] tpm: Actually fail on TPM errors during "get random" Message-ID: <20190401234625.GA29016@linux.intel.com> References: <20190401190607.GA23795@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190401190607.GA23795@beast> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 01, 2019 at 12:06:07PM -0700, Kees Cook wrote: > A "get random" may fail with a TPM error, but those codes were returned > as-is to the caller, which assumed the result was the number of bytes > that had been written to the target buffer, which could lead to a kernel > heap memory exposure and over-read. > > This fixes tpm1_get_random() to mask positive TPM errors into -EIO, as > before. > > [ 18.092103] tpm tpm0: A TPM error (379) occurred attempting get random > [ 18.092106] usercopy: Kernel memory exposure attempt detected from SLUB object 'kmalloc-64' (offset 0, size 379)! > > Link: https://bugzilla.redhat.com/show_bug.cgi?id=1650989 > Reported-by: Phil Baker > Reported-by: Craig Robson > Fixes: 7aee9c52d7ac ("tpm: tpm1: rewrite tpm1_get_random() using tpm_buf structure") > Cc: Laura Abbott > Cc: Tomas Winkler > Cc: Jarkko Sakkinen > Cc: stable@vger.kernel.org > Signed-off-by: Kees Cook > --- > v3: fix never-succeed, limit checks to tpm cmd return (James, Jason) > v2: also fix tpm2 implementation (Jason Gunthorpe) > --- > drivers/char/tpm/tpm1-cmd.c | 7 +++++-- > drivers/char/tpm/tpm2-cmd.c | 7 +++++-- > 2 files changed, 10 insertions(+), 4 deletions(-) > > diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c > index 85dcf2654d11..faacbe1ffa1a 100644 > --- a/drivers/char/tpm/tpm1-cmd.c > +++ b/drivers/char/tpm/tpm1-cmd.c > @@ -510,7 +510,7 @@ struct tpm1_get_random_out { > * > * Return: > * * number of bytes read > - * * -errno or a TPM return code otherwise > + * * -errno (positive TPM return codes are masked to -EIO) > */ > int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) > { > @@ -531,8 +531,11 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) > > rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len), > "attempting get random"); > - if (rc) > + if (rc) { > + if (rc > 0) > + rc = -EIO; > goto out; > + } > > out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE]; > > diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c > index e74c5b7b64bf..8ffa6af61580 100644 > --- a/drivers/char/tpm/tpm2-cmd.c > +++ b/drivers/char/tpm/tpm2-cmd.c > @@ -301,7 +301,7 @@ struct tpm2_get_random_out { > * > * Return: > * size of the buffer on success, > - * -errno otherwise > + * -errno otherwise ((positive TPM return codes are masked to -EIO) > */ > int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) > { > @@ -328,8 +328,11 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) > offsetof(struct tpm2_get_random_out, > buffer), > "attempting get random"); > - if (err) > + if (err) { > + if (err > 0) > + err = -EIO; > goto out; > + } > > out = (struct tpm2_get_random_out *) > &buf.data[TPM_HEADER_SIZE]; > -- > 2.17.1 > > > -- > Kees Cook Reviewed-by: Jarkko Sakkinen /Jarkko