Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp561495yba; Mon, 1 Apr 2019 11:53:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqykz/4yvQ+kNzeSSzExlJOyk60GQ0GahB7qcyX4vU9bCsuEltwOktGiOgnrJZGx2W1MYvDw X-Received: by 2002:a63:751d:: with SMTP id q29mr26404784pgc.215.1554144813044; Mon, 01 Apr 2019 11:53:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554144813; cv=none; d=google.com; s=arc-20160816; b=tWuDKcANmPUNkLTUFN3lBa4kFXInNWK9ORKBrEjXSqAbICEFhaRuIykha0D7OT76WP VDmo/u5hjM/UxAzR0uiEOqv/LL8bzIXefds8lR8G5oRZtPtxmb8erVvu30yk9mRsqeEP Bm6fAlK/UGqytsxESr8itZW2Ol8O9+P8xbF5uSMmtZNjLJjDQxQpg8GX0qw26+hzq69Y YdRiyHxE6qMYWEshcFuz42WR8+6WSB6hgsUk40mXkZg6bZWdol1BikE68tO+CebfQy9y 3iqyl/2q3C1eYS092zIHmWmGyk1/saOHCv9j0nLIm0SLlPJt1drWdXxRa+nMnAFCIN9k Am/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=7/IN4AS74kHhnTS76qLjnlyZdJM2U5JLJsmTGlsxxno=; b=DuJc7PwAC5A2DxPeoAcJuI/XwwjeMZmsNZacQV25Corqn+dCp6F2XghHuidHh24KDn 3Kvw9qUYsSVwePy2yg4foHK6SbPag4CtPUCGH+kcFjwX1J/FtLuz1pk9xSZ8OqbjzA/t CYYmb26FvRIKJc8b1U5aNHnZ99J5EPFBar2RjEEb9Pmerz9QlqFEUBIivJapekzm46GB ToBFeKwtZ2XMWMfP8f8+qR/quO6KD/2qvYNP69HOA9x+rMy7JXcSo4jBuw1Sojp0s24P TCIpGqccTXVNlJdrzuRaV9tvI30KOIeG0BjgRnX0zQtFH089DDXQ7DPvc+ippGyUHp9l 4aRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GKSXGJcC; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e10si9240582plt.283.2019.04.01.11.53.14; Mon, 01 Apr 2019 11:53:33 -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=@chromium.org header.s=google header.b=GKSXGJcC; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729045AbfDASwW (ORCPT + 99 others); Mon, 1 Apr 2019 14:52:22 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:32862 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728898AbfDASwW (ORCPT ); Mon, 1 Apr 2019 14:52:22 -0400 Received: by mail-pf1-f194.google.com with SMTP id i19so5023196pfd.0 for ; Mon, 01 Apr 2019 11:52:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=7/IN4AS74kHhnTS76qLjnlyZdJM2U5JLJsmTGlsxxno=; b=GKSXGJcCmgZ6v/2sdp6uP2DxCGILVrLy6q2orPsze5t44OGzOCg9LcaQUuo9hBDBoV ty8CxUPtCM/33XKW5/6R4Gy/FNsBbqk7Q5ogQQC4ywAxDxCnFCNpAiWQVFaD8gn6VX5w TaTCnNn5q3KuxYkXLbMdGsP1eP4G9/iEnkceE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=7/IN4AS74kHhnTS76qLjnlyZdJM2U5JLJsmTGlsxxno=; b=DCe22QCrD2VTwPwtVYwcnP9lQMMjGY8ExnQIAQkbJR0GI1R/4rYWD5wNcENOq8WHH4 lwxxngr4EuEW2uOQUd/UplMwcs7igd31/Af93Xtx7nPmmsx939hdTdX4ob5U1JD5EL/i nTpsmASNexjaVHvGvJ+4chfgNMJ/5RJM1rzRIxb6HZL7XlGEjg+TnoElBe9Y+99nyvZJ 5ns5/2m0kcvN+qg4QnIkos5bs3WgiGgF8fCX9upu5VFjMPjJ2xR9Ubcmj3VGhzE9DnM+ D/A95aCFPQnKLOcjzGd3nC8YuDtCGtU43zpHskpA8pP3nKT2VZK9eL6t/+rKKuTcuTre XfrQ== X-Gm-Message-State: APjAAAV2hXriCmhBgM+8XQ3qKNMYp1I8RxrIEYzohgIxMETKT/R2xiJw C0YGV2dKCrY3vDN+2E3mPSkkPA== X-Received: by 2002:a62:5206:: with SMTP id g6mr48029247pfb.227.1554144740913; Mon, 01 Apr 2019 11:52:20 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id a13sm12786505pgw.91.2019.04.01.11.52.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 01 Apr 2019 11:52:19 -0700 (PDT) Date: Mon, 1 Apr 2019 11:52:19 -0700 From: Kees Cook To: Jarkko Sakkinen Cc: Jason Gunthorpe , Phil Baker , Craig Robson , Laura Abbott , Tomas Winkler , Nayna Jain , linux-kernel@vger.kernel.org, Peter Huewe , Arnd Bergmann , linux-integrity@vger.kernel.org Subject: [PATCH v2] tpm: Actually fail on TPM errors during "get random" Message-ID: <20190401185219.GA22215@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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() and tpm2_get_random() to mask positive TPM errors into -EIO while still passing through any negative errors. [ 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") Fixes: ce63c05b664e ("tpm: migrate tpm2_get_random() to use struct tpm_buf") Cc: Laura Abbott Cc: Tomas Winkler Cc: Jarkko Sakkinen Cc: stable@vger.kernel.org Signed-off-by: Kees Cook --- 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..8e17d4f30aeb 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) { @@ -524,7 +524,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM); if (rc) - return rc; + goto fail; do { tpm_buf_append_u32(&buf, num_bytes); @@ -559,6 +559,9 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) rc = total ? (int)total : -EIO; out: tpm_buf_destroy(&buf); +fail: + if (rc > 0) + rc = -EIO; return rc; } diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index e74c5b7b64bf..dd0677f50f9c 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) { @@ -319,7 +319,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) err = tpm_buf_init(&buf, 0, 0); if (err) - return err; + goto fail; do { tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM); @@ -352,6 +352,9 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) return total ? total : -EIO; out: tpm_buf_destroy(&buf); +fail: + if (err > 0) + erro = -EIO; return err; } -- 2.17.1 -- Kees Cook