Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp840345ybi; Fri, 12 Jul 2019 05:31:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqwKq/nryVQJl/rxBqlmoawTe0I3+/fu8giraIH7ihxLxp83YEwajKENIznPdyW0MZ3lX/pM X-Received: by 2002:a17:902:5a2:: with SMTP id f31mr11126584plf.72.1562934706528; Fri, 12 Jul 2019 05:31:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562934706; cv=none; d=google.com; s=arc-20160816; b=iJseZlDkQCzPwfjq/UrMml2X5EbP8aspDe/YJtbINVVi5NTa0/GrdYpSB01WTcTRLC vBuI1sSx0fTkCTGSwSyUo52L9J/rAHIEh/iML++teFq0luWq/OnT3Eyh7QOhPm6YfGUz n1IRc9OuvlI2wxWCxLbpmmqs7IUlq0ORra56Td4S9Unm25279mk7fz3ois1ipo7Tgpxu K5R40zldMn82gjRNCK7PasGaRKr9gQhgOByKDy5ywLf6BjlYKwpxJ8/SrJ6qnuulNY5C g/0IHTUy+9UWboV7VXSiAKOcUKmf75IHJ2E7CV2Y+N8gYsV9+9doouMH462/3s7QWNbW Jw2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tIE2AUJ8oPCODSGrOSdDqlLIQD+ZZ4xVg0MSluL/NvI=; b=oQEJuWmbmywBRz4OIf/28DEJCMtJULXynFTD9Dlb2f/dQCSzuouD9+Fmh8qIa2rAAz SF1s05OSmQo++wN54pNlILopIzW61egdPTTCFAB5RqnKLKNvby7Lvsud7fiV3Z+6SMAa LmV56sGP8C4UkqO0h6V4FIgEGiPWw4pqDW/9bHExkEI2EPe3IjBRYbvjOqrmQsL/+3ZC nxjud65jOkxwd/PwDX5e3ibhGoM0D96q3v+vTgy4UWjMiazzK2DFO7+S+Dj69Gh+JkEX M3uFs3IL7eN8tHEMbG6ZSOR4VjXGDqiEJo9UNRo5Z5kVT6xvHLQeOaWWItOU/J0Jno9q 4SOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BsxSPPPr; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t125si8098164pgt.344.2019.07.12.05.31.30; Fri, 12 Jul 2019 05:31:46 -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; dkim=pass header.i=@kernel.org header.s=default header.b=BsxSPPPr; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728616AbfGLM3R (ORCPT + 99 others); Fri, 12 Jul 2019 08:29:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:43958 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728945AbfGLM3N (ORCPT ); Fri, 12 Jul 2019 08:29:13 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 D4910208E4; Fri, 12 Jul 2019 12:29:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562934552; bh=A7j9TLWrHUaatr6N1wsFWMM8pMp46I6Ryah3bqxuhbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BsxSPPPrn1C24nvk7YwofiVhJGkKqApzLVq8YKx1SPa6LCa6TDwIkdz2VV1Wp6yAP wQAgovObheh/T9oPnDJogsnIq+aLgXM98Gaj73EIkjLuLrqhifqTo86KjTOoWXycDe krpg8wrrvuIfhg52ga2ViM9GXDbKM+eXo4iYfw9Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Phil Baker , Craig Robson , Laura Abbott , Tomas Winkler , Jarkko Sakkinen , Kees Cook , Bartosz Szczepanek Subject: [PATCH 5.1 093/138] tpm: Actually fail on TPM errors during "get random" Date: Fri, 12 Jul 2019 14:19:17 +0200 Message-Id: <20190712121632.331674959@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190712121628.731888964@linuxfoundation.org> References: <20190712121628.731888964@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook commit 782779b60faa2fc7ff609ac8ef938260fd792c0f upstream. 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 Reviewed-by: Tomas Winkler Tested-by: Bartosz Szczepanek Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Greg Kroah-Hartman --- drivers/char/tpm/tpm1-cmd.c | 7 +++++-- drivers/char/tpm/tpm2-cmd.c | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) --- 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 *chi 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]; --- 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 *chi 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];