From patchwork Fri Nov 24 02:02:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 169150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp841403vqx; Thu, 23 Nov 2023 18:03:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeFRO0yi6EvtGW42xN0YlUv/3xGNJuO2KfuT3oIfI23E4G80MWSeOVTkK4xmliNApjf3te X-Received: by 2002:a05:6a00:1d06:b0:6cb:8c70:4792 with SMTP id a6-20020a056a001d0600b006cb8c704792mr1423398pfx.4.1700791425724; Thu, 23 Nov 2023 18:03:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700791425; cv=none; d=google.com; s=arc-20160816; b=Ra8B+ez19Xvz13z6SGV1rn91WBZBXGnaU8EYgmfqofvlbgUNb8luu+ZlaUjy0LVbXk ZCq0yG6S3CY5FGnDD0UJYHeSTOo4DEtfz+Dks/tugF1EwE/G3WwZCu5s02qWwgcjxY80 KGamFUGM+2TcgIeVGjcdtBWwq2suLjzewhf3zkmlI1dLk9lbzhP2dtwzcDr4sx97UQQC vwlSi9wCTm1RLz8P0ehs04GOu+ggP3AKE21SbXU/JdYv8a2NzEzAjAm4mNNg9mtGMc4k 4vFP1MxlAW9W5N8LCcWzqlffIut9xPaUqCmIdHxXM8Npt5A6nTpyPBeL9iup+b72MmnF iOKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WEOo26I0ckUi7GsIQYnUEBicS3zZ7mt6mMoQ5fT8EOY=; fh=RBX0EO7BCt4n4lnvPBYFN24o6jFly+IlKRm41n8yo0s=; b=H2+x0mwNlAgdokQ8X7qlFHCaTq/kyyztkqw3MPu9bgrF5lhLGFOXI5BR3rwS/hFRef ZdwIUq25Yxy9HEue91d55bDW/uoHybVDi22TRG16eHvU6gxl3yxCcceUkr2GA3BHu21w a1vmNZS/dz5e2T58o0cdxNLEzw5nTdtSWEm6/HRmdv1Lg3HA8mWOTxdz2mS7kO9wX+tQ pB58j7uWgX8Mc8P39ozoNxD4tpVbPjrVUNDl9ASWmttFxReOWZypOhUpymANYruNlAbu meHqSA8Z18CQMTWwsT7dKvzJQ7sOuwn3Leoffwi3ZXzLNeENef63uueFAfMgqlyRi3yc X1Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ASsI15md; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id d13-20020a056a00244d00b006cb4f312136si2477327pfj.200.2023.11.23.18.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 18:03:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ASsI15md; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id E8C9F8366256; Thu, 23 Nov 2023 18:03:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230284AbjKXCDa (ORCPT + 99 others); Thu, 23 Nov 2023 21:03:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232787AbjKXCDL (ORCPT ); Thu, 23 Nov 2023 21:03:11 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E53E19AC for ; Thu, 23 Nov 2023 18:03:03 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2814C433C9; Fri, 24 Nov 2023 02:03:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700791383; bh=QxqRnwnwf/AxgHt8+bTTK2G02f45I3yxIt0Ytvx6QdM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ASsI15mdzvCNaBq5rXvzB4Nd8ICJz7E0gaH/EKZ7qXU0hKbX9KxWNVfTyjj3iC4O4 qHgMbd8RubYG5DwMKrwY9kiYVH2AEKZDbOdxBQCNQHKV1fD44zIdRb3KvXVFz/iPYn B2vgDQDEpOABh7Nr48fy1cCXLW2foYinIXr0TlNzhHrtf9yGcLAITvXdyq8g2d7o6p INqwlQ0HmxqYZ/ejCwWiXjnD157iNb1tjazhGrjid1Fz7Hk6oywNYHhREWftqcu8Fa u+YglqCEaHbmU2uh6MwkJyd2A36J8WpgAO0cLdY+p27iAPDS0N4g+zDC+eJzpbbAiW nvvfZkmyLW9+g== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , James Bottomley , William Roberts , Stefan Berger , David Howells , Jason Gunthorpe , Mimi Zohar , Mario Limonciello , Jerry Snitselaar Subject: [PATCH v6 7/8] tpm: Add tpm_buf_read_{u8,u16,u32} Date: Fri, 24 Nov 2023 04:02:36 +0200 Message-ID: <20231124020237.27116-8-jarkko@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231124020237.27116-1-jarkko@kernel.org> References: <20231124020237.27116-1-jarkko@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 23 Nov 2023 18:03:42 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783409070146037763 X-GMAIL-MSGID: 1783409070146037763 Declare reader functions for the instances of struct tpm_buf. If the read goes out of boundary, TPM_BUF_BOUNDARY_ERROR is set, and subsequent read will do nothing. Signed-off-by: Jarkko Sakkinen Reviewed-by: Stefan Berger --- v5 [2023-11-24]: Fixed off-by-one error in the boundary check. v4 [2023-11-21]: Address James Bottomley's feedback for v2 of this patch, i.e. offset pointer was not correctly dereferenced. v3 [2023-11-21]: Add possibility to check for boundary error to the as response to the feedback from Mario Limenciello: https://lore.kernel.org/linux-integrity/3f9086f6-935f-48a7-889b-c71398422fa1@amd.com/ --- drivers/char/tpm/tpm-buf.c | 79 +++++++++++++++++++++++++++++++++++++- include/linux/tpm.h | 5 +++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index 099b4a56c5d5..32619e9ab4fa 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -107,7 +107,7 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length) return; if ((buf->length + new_length) > PAGE_SIZE) { - WARN(1, "tpm_buf: overflow\n"); + WARN(1, "tpm_buf: write overflow\n"); buf->flags |= TPM_BUF_OVERFLOW; return; } @@ -143,3 +143,80 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value) tpm_buf_append(buf, (u8 *)&value2, 4); } EXPORT_SYMBOL_GPL(tpm_buf_append_u32); + +/** + * tpm_buf_read() - Read from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * @count: the number of bytes to read + * @output: the output buffer + */ +static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output) +{ + off_t next_offset; + + /* Return silently if overflow has already happened. */ + if (buf->flags & TPM_BUF_BOUNDARY_ERROR) + return; + + next_offset = *offset + count; + if (next_offset > buf->length) { + WARN(1, "tpm_buf: read out of boundary\n"); + buf->flags |= TPM_BUF_BOUNDARY_ERROR; + return; + } + + memcpy(output, &buf->data[*offset], count); + *offset = next_offset; +} + +/** + * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 8-bit word + */ +u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset) +{ + u8 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return value; +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u8); + +/** + * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 16-bit word + */ +u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset) +{ + u16 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return be16_to_cpu(value); +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u16); + +/** + * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 32-bit word + */ +u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset) +{ + u32 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return be32_to_cpu(value); +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u32); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 715db4a91c1f..e8172f81c562 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -302,6 +302,8 @@ enum tpm_buf_flags { TPM_BUF_OVERFLOW = BIT(0), /* TPM2B format: */ TPM_BUF_TPM2B = BIT(1), + /* read out of boundary: */ + TPM_BUF_BOUNDARY_ERROR = BIT(2), }; /* @@ -338,6 +340,9 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length); void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value); void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value); void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value); +u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset); +u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset); +u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset); /* * Check if TPM device is in the firmware upgrade mode.