Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp400345yba; Wed, 3 Apr 2019 10:53:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqzo6Gyp6KxqXiXB/jIT2ssOgLZNc5Fr6ea2q8FST/e1+ze8KXLykQ96GXup9JFmsUnP8B2i X-Received: by 2002:aa7:8c56:: with SMTP id e22mr743810pfd.155.1554313993520; Wed, 03 Apr 2019 10:53:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554313993; cv=none; d=google.com; s=arc-20160816; b=wL8oj+ldVRZfOv54sKs83aLriw60hZYC2XfENGSbQDQUp3TthtJU8eky/3j0BG9trj obwLZOd2BH5n+1w9QKg4QKTtZklzFZ51SX1cY5eKTxEgORtwS3n9iX/4Rq03G2w7gZco QZ2QLomxCXxApsBUR9tGw3tkNDZZQ+7anc5OyT6mH/jhT+cq+E3pF7o0GWjAp0JZtBa0 oCAI/cH375WZa8YWSUdvV41wEubIpDzszYwhvoiXG7zBCz09Uqw9Bvdq9OKaticKlWgE Ir2Z86nxVTRXV4GqQcuWXhhZp8qJhPPEfQ9fcLYbkKFIzyk3wcnT94a+ByDlpqdEsUgx 4EdQ== 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=vQkGjO9jw20ydy3gcwd5bUFX2NOsCskmO88MTqr3CqM=; b=s1wLI6Hn0tJypoNwiusdh8KJ76FARwBRO9bIgs5Pf/4OB0pNcg2DMmkxSyOHiSonsw 1Lh5gCRS6bN3BnYZG/VFm+kCqCdbi+0Sc68uiGxpWi17v+zIUwJ5ZUlt2I/Ozy8yfMFf sAUeN3PkcgqEprtmz2D06r/Mmuv/K1baACbNvbtQhPWFDbnZ0lWkhDSVTp+QEZlK+Vne fSQBKX3p6BtjqsnbmycXVBHadUFDvk85FcLVM6JlChBhvuVz11r2yGo0IY9Q9vIVfAjC yyw36qUxN9HlpLVkIG1TqHoCYqWczt+JGW3Lbts6+61XVFPCWD8+ua8hKzHZPh+VrwTv Ysgw== 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 f21si14201836plr.297.2019.04.03.10.52.58; Wed, 03 Apr 2019 10:53:13 -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 S1726562AbfDCRwN (ORCPT + 99 others); Wed, 3 Apr 2019 13:52:13 -0400 Received: from mga04.intel.com ([192.55.52.120]:57365 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726144AbfDCRwN (ORCPT ); Wed, 3 Apr 2019 13:52:13 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Apr 2019 10:52:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,305,1549958400"; d="scan'208";a="139717984" Received: from oakcakay-mobl2.ger.corp.intel.com (HELO localhost) ([10.249.254.144]) by orsmga003.jf.intel.com with ESMTP; 03 Apr 2019 10:52:08 -0700 Date: Wed, 3 Apr 2019 20:52:07 +0300 From: Jarkko Sakkinen To: "Winkler, Tomas" Cc: Kees Cook , Jason Gunthorpe , James Bottomley , Phil Baker , Craig Robson , Laura Abbott , "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: <20190403175207.GC13396@linux.intel.com> References: <20190401190607.GA23795@beast> <20190401234625.GA29016@linux.intel.com> <20190402164057.GA4544@linux.intel.com> <5B8DA87D05A7694D9FA63FD143655C1B9DAE2759@hasmsx108.ger.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5B8DA87D05A7694D9FA63FD143655C1B9DAE2759@hasmsx108.ger.corp.intel.com> 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 Tue, Apr 02, 2019 at 07:13:52PM +0000, Winkler, Tomas wrote: > > > > On Tue, Apr 02, 2019 at 02:46:25AM +0300, Jarkko Sakkinen wrote: > > > 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 > > > > Applied to my master branch. Jason, Tomas, do you want me to add reviewed- > > by's? > Sure, it fixes my patch. Great, I'll add it. Thank you. Just want to be explicit with these things as I consider them as if I was asking a signature from someone :-) /Jarkko