Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp547659yba; Mon, 1 Apr 2019 11:33:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqwSBgars1+Gjxn+VZUn5iKaniRX2vHaYaYU5bt/WP82saDwQ5hTO3LwsEWWBHn73nPIktYw X-Received: by 2002:a62:4553:: with SMTP id s80mr63314330pfa.141.1554143600417; Mon, 01 Apr 2019 11:33:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554143600; cv=none; d=google.com; s=arc-20160816; b=O6Ek8OvJtr9jSwG7Q0tc5uNk3HQmFUfgVrFcqm7OFeosVFMlEcT/TMHNlLwWc5wlfa YlMgcD8hwMr+47oBdum1PH8y/q/f4Hj+4rvPkxYpCDFp1q7uKG5OWtMvnnk3X/3HFFsn bZx4RK/WIqKrYgClhODP2xkac+zYlYqL/g9wQLgHKsMZUcq3olWgEpvkyv+M0i0y73Mf 0Omxe1iKKspVOw9LRR2AE9Aa/SjDOkLALet6PbYBd+gTwEdh3VOKH7+wOrzLglvnwpb3 ph8ghkykdL486iMJEDTpsnc6sChyaJt4ONlGgkcIGtdcWYimgY94Q2pNiW2byI4YY2XK 0+gQ== 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=MQ0O1mmB+oNGhDova//RYIhWLAO6da/Vm26gjBguPdg=; b=nkxZa5estN5XwJ4zt9BzGyoE3eH1epJrfhoT4l5BhBg0Q3NsAilfwr24uh3QOew+vF 2oMtVm/lm7hf5Ov3hvf4itHiwPoTD52fyH5OLTIK5LupbuQFQA/SQ0eW/Je1wnRU0VQE FVOeZSi+B7PHC4lnpOGaFATcE7S03Kj1vLE0m21vbDwiIf7RV0Tttj347Xt+ZVGvFavH GiOZOsuDBhrQ36O30fNSb0HJ8t2Y/rqRTzOD5vhMNGzWx93hfIojlvh+1WQjNQrYiWLd VfUtX963KU2w5fOB1srUmKLhCLUq3VpvF3xrd6I6y/tnehSriN524hcMVhwsrbxL5KCJ 4Nog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nqGC2Lb4; 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 k185si9769462pgc.450.2019.04.01.11.33.05; Mon, 01 Apr 2019 11:33:20 -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=nqGC2Lb4; 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 S1728998AbfDAScP (ORCPT + 99 others); Mon, 1 Apr 2019 14:32:15 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:45633 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728941AbfDAScO (ORCPT ); Mon, 1 Apr 2019 14:32:14 -0400 Received: by mail-pf1-f196.google.com with SMTP id e24so4976262pfi.12 for ; Mon, 01 Apr 2019 11:32:14 -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=MQ0O1mmB+oNGhDova//RYIhWLAO6da/Vm26gjBguPdg=; b=nqGC2Lb4Pu4vnyS8A2Wzh7NqHsmc/DUcZ+y6kRoiUsBeB+qNJI33YSr8nUlHRfz3+5 l3Bu1m5E4j+Aqs8rxoWBjpTBiVnWAe6NnqlPv5BBUhvKALIIzOeklfg4UHQ5KtBMId89 ohOFs13LMOn3R/dXFxH5lFrBMbswBaN++jSvc= 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=MQ0O1mmB+oNGhDova//RYIhWLAO6da/Vm26gjBguPdg=; b=ZFDUp5dGZk61SI85MFaPyVUuOkxR46KuWNsG/ZHiC1wYoP3NlEg6lgQtvEUS7yjb2g PI6dhm7o9WS38l88r1ZtrP4ODs0CJ0RS/yQzzh87EsdgSoWl7jk+O31F530uFtGK9LLz cSjcaU99QiWyqj1RUJc94KVWsuJ6aSaTpg+q5ow6Y+19IW1f7s/2GgxlWag9pNMMw41h Uokcgur0TvTs3Un9A6bQ6qEfkDL/DJQaI/FqHCRJu3lZspWjJPOpIhtQQW0tHzI4+lzg qUxwLJAnp/JGjjDSwWUdfRfqDe79rF3svwihmzNY2dRMjqiEXU7qUkdNIp7nEvBVCMUP +9/A== X-Gm-Message-State: APjAAAUPsydnNX/0myBqq2zkNrvOVkHtahrg9dX6SFHIU6aC+4jsOOmE +U3tsJKsf1Qru9WwCTTq1muu2Q== X-Received: by 2002:a63:6786:: with SMTP id b128mr52832993pgc.318.1554143534143; Mon, 01 Apr 2019 11:32:14 -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 71sm22703762pgc.23.2019.04.01.11.32.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 01 Apr 2019 11:32:12 -0700 (PDT) Date: Mon, 1 Apr 2019 11:32:11 -0700 From: Kees Cook To: Jarkko Sakkinen , Tomas Winkler Cc: Phil Baker , Craig Robson , Laura Abbott , linux-kernel@vger.kernel.org, Peter Huewe , Jason Gunthorpe , Arnd Bergmann , linux-integrity@vger.kernel.org Subject: [PATCH] tpm: Actually fail on TPM errors during "get random" Message-ID: <20190401183211.GA21228@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 an 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 --- drivers/char/tpm/tpm1-cmd.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c index 85dcf2654d11..faeb78ecf960 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,7 +559,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) rc = total ? (int)total : -EIO; out: tpm_buf_destroy(&buf); - return rc; +fail: + if (rc < 0) + return rc; + return -EIO; } #define TPM_ORD_PCRREAD 21 -- 2.17.1 -- Kees Cook