Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp845478ybi; Fri, 12 Jul 2019 05:36:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqw5F5v7aaFw3EZOICSAAvCJ3FQGyeeOzq/LM6srXkK5j+HYUJYiXBRxr5aXxAln4AaMuuWm X-Received: by 2002:a17:902:4501:: with SMTP id m1mr11294925pld.111.1562934992370; Fri, 12 Jul 2019 05:36:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562934992; cv=none; d=google.com; s=arc-20160816; b=0RcO9G7pyaLmwiPGX8zolXZJBhcYdakuqEJigyZPbdsYqVFQARhcKela3EZaElrlky bpe5FrkQEwoYhbp9aRFkSTxwsqhH5S5py8IvIwfpVIUl88kS7OuIe2d6oG/nR1DRm27r JxaMnk0kiRND1YqgCV/KpB7cg+xU93LlfCQWrMxm5mtZ5cqp5q60gZmuk2+TjBi4BTrg jdXHfhr4ixfAKwnLdW+PHytOKwY3HilPpz7X6jk5A+6Oe0JTH6d3Dugrj8j+B0cm7/mq bwyyggsSCUQPbTcNnybDMwfKpXUEydmamsvIW0Sobqi217QAjGVlEXHHrCuVB3EeKufE jVxw== 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=oXN/shU2yoNyolw0p4bNRWPTyt+xEX4FH5v6qYKJZwc=; b=stiiKWSKpWkLligPSveZ1eyAc5WN30Xti+qcLeZubGMI/83axxKGA/MJiOBRMGSHv4 r8DXbMLhMNm83V6uBYqPrCSwpycVQbmeaBjxnVfoS1r1V2hlL+msktN91xSlEQNey3PW rD4eClsduETO+L5Ok1n40ghR7iYw9Jh+kFEF9KNQpIQh4ZJhdin7fgL4qTiTHt1aXQzG EO/TRFWR1HEswcmsDCxsn6zFAuTChHnFMVEB8K0mgFr7cuFAdzf8KhUCCxUREE/nli1f gFuMYyE/MIthzjd15MSg+CaRYtCWmxTMma5NSZGPcY2If6z50uxrF573YeUIzaZjZGik wXDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Asx7jd17; 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 a21si8437321pfo.249.2019.07.12.05.36.17; Fri, 12 Jul 2019 05:36:32 -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=Asx7jd17; 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 S1729705AbfGLMeI (ORCPT + 99 others); Fri, 12 Jul 2019 08:34:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:53766 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728132AbfGLMeG (ORCPT ); Fri, 12 Jul 2019 08:34:06 -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 83F9F216C4; Fri, 12 Jul 2019 12:34:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562934845; bh=KDB7BkkpHiBg9CX+rLZM9phDdX5FbUHbfnLEUuAj8Zc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Asx7jd17k9Zue098gWiwKiGJEn8uKO81jXiAR2OQse9eY/4KR8sA6oboiLuLivdah p3/Dle9eb/uxa0cca9OPciEzNZRN25tYzkKkz7yXKfsfQLX2g/h+xQH7J0KSgY0u+z gATGh9IdY9RKdZBvzaSsuWFdQBQdo2pbWhAsqq9A= 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.2 09/61] tpm: Actually fail on TPM errors during "get random" Date: Fri, 12 Jul 2019 14:19:22 +0200 Message-Id: <20190712121621.125477736@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190712121620.632595223@linuxfoundation.org> References: <20190712121620.632595223@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 @@ -297,7 +297,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) { @@ -324,8 +324,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];