Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp750246rdh; Thu, 23 Nov 2023 18:03:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFatHpL6Xb3/2bkCjeCVpD89u23Qs5m9ERurJ34Bp5KF9+IyWkWdpk+IqI4v3SjGPf4Eou8 X-Received: by 2002:a05:6a20:9191:b0:187:e3e8:e8d4 with SMTP id v17-20020a056a20919100b00187e3e8e8d4mr1851269pzd.53.1700791432408; Thu, 23 Nov 2023 18:03:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700791432; cv=none; d=google.com; s=arc-20160816; b=bp9JSAtK8y9u6ZyUZsn/26CMVgiHZPTsHYfbmH5PIjM/NC7FzxAVwTagTa56qGZlHL QO3JukP2I220LkDGokvZtefXjSUluRTXuX7HoAyc7d6xs62jKjmje/CS+Y60832ImfNG Iw05nzQQgQrrjoU5t3FpYoT//8W284IFzzi2bzXXeQKjHKdFxm/kOTY/xmOTIPIFs/z4 ff3+gf5Ojyg5bfb5PkwkHRgMIxrHZh7PQ/sVhEx20NQPkQF0YtSrawwADTlK593+6wDy F4Q4zS/s4HUdO6gbmNbFVrXLEovaRYblpY2n3Ht+kq827SYmoVXa8m3iq7E7cDY7tqcT 5iXQ== 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 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id n12-20020a1709026a8c00b001bf1005924csi2199987plk.321.2023.11.23.18.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 18:03:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; 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 23.128.96.35 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 F1A518366251; Thu, 23 Nov 2023 18:03:48 -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 Content-Transfer-Encoding: 8bit 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:49 -0800 (PST) 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 --- 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. -- 2.43.0