Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2409151lqb; Mon, 27 May 2024 20:52:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUE3J1NLeKOS5xd0WwseQ49rNYhrjtmi28iLDLH4Nt8uPwLf/nePgZODjWTESrC8kvlOc8uT7+CJB8LxZlSmwQVCtdA8CbVKm8hEsIJRA== X-Google-Smtp-Source: AGHT+IHswqt3AuvgkddOWDTeqHJFQ7yl+DobDDJoCej2u3j+ULxf1CA5B/qlfm3my2ioXCSMxYZr X-Received: by 2002:a05:6a20:1592:b0:1b0:119c:7587 with SMTP id adf61e73a8af0-1b212f720a1mr12062528637.51.1716868325934; Mon, 27 May 2024 20:52:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716868325; cv=pass; d=google.com; s=arc-20160816; b=GR72uq9WKvbN15fAmUsUk2zmMmRRIqa25IgSrpLC4toIqUVmd3FTKuY/fP2fC5bEqM vSrgz1rIUY+1Xde/1TvhNGItTuIuUD254LqHiKsKaLaL/cPhVdMNjV9rkY0gAtHCyKSo QCRaB117eM59Vpo5kriN6t14+4N4x3D0L7mEV/yGkH8fSDH01//kMVUrww3vScgFxnKG nMTHtWjYgLGu5Y1X0JdG8Z5uh5qhD/TOoPSKaIu8LUDwjqHxKxAKgwdamoOz+lOX/CLr WcQpypDZj4RmfLy1svyZaQNJ2jfEOy+2+7jgc/Tu6f710GGAM5ipgWk4Amy1ABr131ln H3OQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mwcHRLunRzbaIOVg61pkMXTILixwK4oVtavuP9tJUns=; fh=rHYxINyg2KqfqYnhz31NpIsmhHrV9yXqodVKTTS70dQ=; b=ho1/GStPkDhdYnrj3+tuVL8IGfy1Xd0Fd8tnsxXC8nqVXvlx+3srFas65suAPmrP1m QFQ68cJkBen1S72hmTnl2pj+7CzgowRvnDw4QHtW/bnz7clnWT9nLKDdlXXec0QQU07Z nA4/FnUmfFrZ7/M/DlUCLPCcywUVGz76/r1pNuG4G7PDwKEcJ0KZEQfd73NLZ3KWRKCY xALqbNax2SnsrlI4rUAmawhgjFmhNsQ6sX4F8kNHH8h5mkxhwIfEh/BzC0HqALpW9bcm idpO63J4nXjmzLLWFL7YTmgX/xAtpd9dCR0lScNs/LepLNMcO9N8GaemlBeb+4KHXFrv RCjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EWz2qLEs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4433-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4433-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c970ef2si45662405ad.244.2024.05.27.20.52.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 20:52:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto+bounces-4433-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EWz2qLEs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4433-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4433-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8C98E2845CB for ; Tue, 28 May 2024 03:52:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 409FD1BC4B; Tue, 28 May 2024 03:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EWz2qLEs" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E80201B969; Tue, 28 May 2024 03:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716868312; cv=none; b=QhE2k6s7I2c1q248CC1XbbuzayK7fuRrI7fAYRuJnUK+cMteqzSFg7/4HvbZ7YkLfZGSKpJ1EqouZeXqWF2SvyJzsqhKSgWeorysIafO6GnyjnxpRAxZCfVlCZs0+cC7d3oJBZocbzaB4wLFkCZMUVRxdKSH1xcFNsRBC+g7wJw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716868312; c=relaxed/simple; bh=ZgvWBP6tE4s9/snimxcOGEFivWgLx0Ri9cZOylfpUtU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PwVoLlUxz1PTFbgsNPxOJ78WoXpL8QuK77UmIzV7ZIHxRIhnXYWRnHNYnV7ARMOfdy+z1k3stZAJbPrWE6Ot65SZysmd72oBoWmNWuk1SNbnsyVUo2WOFPNreq726B8wE8NgrR6wOJ1MAyGMVfB8L04ueZbyFhYvMIMbBmhU3n8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EWz2qLEs; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18704C32782; Tue, 28 May 2024 03:51:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716868311; bh=ZgvWBP6tE4s9/snimxcOGEFivWgLx0Ri9cZOylfpUtU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EWz2qLEsuPGoqRilvjGGhFYLpoa49Vdz9h5ql48FBVCCK1E2lO2ZWnYMTOxZjNeMj WqbZUful+bX0pH00nrSNekDdxmdvjDfL3j/AAXP09tL3krHhUkesz2/eve6LtOFsCz pdzwOUB3pMN+3XNn6WUm1wZ9HhIUpQJMtNpMdLWmaAOmNlreq40dgF3z0xO3KKsUI6 rPbrOSFHVrT/lXkWJUFT3a3ej3CS5nx7rOeC0XOvpb0mx/r6hktfk+DtS8VrxudufD 0o/5j126Kcyg2X5KQhESSjN9ZDUvNKrYzgiSH1ivPnRwKDx+VV979sZp1Xiq06fEN3 hdRmRedKZufRQ== From: Jarkko Sakkinen To: Herbert Xu Cc: linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, Andreas.Fuchs@infineon.com, James Prestwood , David Woodhouse , Eric Biggers , James Bottomley , linux-crypto@vger.kernel.org, Stefan Berger , Jarkko Sakkinen , "David S. Miller" , linux-kernel@vger.kernel.org (open list), Peter Huewe , Jason Gunthorpe , James Bottomley , Ard Biesheuvel , Mario Limonciello Subject: [PATCH v6 1/6] tpm: Open code tpm_buf_parameters() Date: Tue, 28 May 2024 06:51:16 +0300 Message-ID: <20240528035136.11464-2-jarkko@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240528035136.11464-1-jarkko@kernel.org> References: <20240528035136.11464-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit With only single call site, this makes zero sense (slipped out of the radar during the review). Open code and document the action directly to the site, to make it more readable. Fixes: 1b6d7f9eb150 ("tpm: add session encryption protection to tpm2_get_random()") Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-buf.c | 26 -------------------------- drivers/char/tpm/tpm2-cmd.c | 10 +++++++++- include/linux/tpm.h | 2 -- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index 647c6ca92ac3..cad0048bcc3c 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -223,30 +223,4 @@ u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset) } EXPORT_SYMBOL_GPL(tpm_buf_read_u32); -static u16 tpm_buf_tag(struct tpm_buf *buf) -{ - struct tpm_header *head = (struct tpm_header *)buf->data; - - return be16_to_cpu(head->tag); -} - -/** - * tpm_buf_parameters - return the TPM response parameters area of the tpm_buf - * @buf: tpm_buf to use - * - * Where the parameters are located depends on the tag of a TPM - * command (it's immediately after the header for TPM_ST_NO_SESSIONS - * or 4 bytes after for TPM_ST_SESSIONS). Evaluate this and return a - * pointer to the first byte of the parameters area. - * - * @return: pointer to parameters area - */ -u8 *tpm_buf_parameters(struct tpm_buf *buf) -{ - int offset = TPM_HEADER_SIZE; - - if (tpm_buf_tag(buf) == TPM2_ST_SESSIONS) - offset += 4; - return &buf->data[offset]; -} diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 0cdf892ec2a7..1e856259219e 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -281,6 +281,7 @@ struct tpm2_get_random_out { int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) { struct tpm2_get_random_out *out; + struct tpm_header *head; struct tpm_buf buf; u32 recd; u32 num_bytes = max; @@ -288,6 +289,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) int total = 0; int retries = 5; u8 *dest_ptr = dest; + off_t offset; if (!num_bytes || max > TPM_MAX_RNG_DATA) return -EINVAL; @@ -320,7 +322,13 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) goto out; } - out = (struct tpm2_get_random_out *)tpm_buf_parameters(&buf); + head = (struct tpm_header *)buf.data; + offset = TPM_HEADER_SIZE; + /* Skip the parameter size field: */ + if (be16_to_cpu(head->tag) == TPM2_ST_SESSIONS) + offset += 4; + + out = (struct tpm2_get_random_out *)&buf.data[offset]; recd = min_t(u32, be16_to_cpu(out->size), num_bytes); if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + diff --git a/include/linux/tpm.h b/include/linux/tpm.h index c17e4efbb2e5..b3217200df28 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -437,8 +437,6 @@ 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); -u8 *tpm_buf_parameters(struct tpm_buf *buf); - /* * Check if TPM device is in the firmware upgrade mode. */ -- 2.45.1