From patchwork Mon Apr 1 18:52:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10880501 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 129231575 for ; Mon, 1 Apr 2019 18:52:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F13EC27F4B for ; Mon, 1 Apr 2019 18:52:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4D6528384; Mon, 1 Apr 2019 18:52:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7018F27F4B for ; Mon, 1 Apr 2019 18:52:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728939AbfDASwW (ORCPT ); Mon, 1 Apr 2019 14:52:22 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:46629 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728880AbfDASwV (ORCPT ); Mon, 1 Apr 2019 14:52:21 -0400 Received: by mail-pf1-f194.google.com with SMTP id 9so5001111pfj.13 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=GWK/KcWspavx57M674rF4K5UjC0bAkLttBFPOxvhV3Ozg/vJVHJuJz4k5hTgoFX7In V+HOybQ/md0TZzFR/y/th2U51uAPuJTkVRvbxl59uS//oXNlShuHg3KefdSJoo6YFtzl vE22huwGj3+9vAnv4Ef35X4pw71x0OXVDfzRod5NOSl/OJkc0QlPnWdL7cEm/RIqa/HQ xPiCQqJXd/O2MdkjIfOcwkbiQEysfTVG08Oz9hf2d0DQFva1GIJnTkgXWVfNvybjJP1X aSHBxYaavnf3Vhj7hqDY8zim+wW+fFw6LZs1JDA5Jea3ChsHjj7eX8I1nkMrulZqwlmQ rWJQ== X-Gm-Message-State: APjAAAUP/CseYkF44GegetlAdRBmtyefwp24wHm+767oe3YwlcRvKjmR ChgOtW4D/vn2EKV4+Wvxs1eJNw== X-Google-Smtp-Source: APXvYqzqsuzgYdZfQFWOvucA5+eop/VVSZDvOnoDJ/pHdfFsFfXDajhLhcRiVdBDhvIhF59PLMfISw== 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-Disposition: inline Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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; }