The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jack Wallner <jackwallner@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org
Subject: [PATCH 1/8] crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context
Date: Sun, 29 Nov 2026 00:56:03 -0800	[thread overview]
Message-ID: <20261129085612.22266-2-jackwallner@gmail.com> (raw)
In-Reply-To: <20261129085612.22266-1-jackwallner@gmail.com>

crypto_rfc4106_crypt() and crypto_rfc4309_crypt() keep the IV for the
child request in the space that follows the child's request context.
They find that space by starting from "subreq + 1", where subreq points
to the subreq member of the request context structure.

"subreq + 1" is one past the end of that member, so as far as the
compiler can tell, everything stored through a pointer derived from it
lies outside the member: with GCC,
__builtin_dynamic_object_size(iv, 1) is 0. The memcpy() calls that fill
in the IV are spared a FORTIFY_SOURCE "field-spanning write" warning
only because PTR_ALIGN() casts the pointer to an integer and back, which
makes the compiler forget where it came from.

A later patch in this series makes PTR_ALIGN() keep the object size.
With it, a kernel built with GCC 13.3 reports this from the crypto
self-tests, which is how I found these two callers:

  memcpy: detected field-spanning write (size 4) of single field "iv" at crypto/gcm.c:483 (size 0)
  memcpy: detected field-spanning write (size 8) of single field "iv + 4" at crypto/gcm.c:484 (size 0)

Start from "rctx + 1" instead, as crypto_rfc4543_crypt() in gcm.c already
does. crypto_rfc4106_init_tfm() and crypto_rfc4309_init_tfm() size the
request from sizeof() of the request context structure, so "rctx + 1"
is also the address that calculation is based on. subreq is the last
member of both structures and nothing follows it, so the address does
not change. I checked that with a temporary build-time assertion of
sizeof() against offsetofend() of subreq, on x86-64, i386, arm, arm64,
riscv and powerpc.

No functional change. With this patch and the PTR_ALIGN() one applied,
the self-tests run without the warning. Nothing in my test boot
instantiates rfc4309, so for ccm.c I have no runtime report, only the
same construct and the same compile-time finding.

The change and this changelog were written by an AI coding assistant
(Claude, run in Claude Code) at my direction. See the cover letter for
how it was used and tested.

Signed-off-by: Jack Wallner <jackwallner@gmail.com>
---
 crypto/ccm.c | 2 +-
 crypto/gcm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/crypto/ccm.c b/crypto/ccm.c
index 916441e4f..f03629982 100644
--- a/crypto/ccm.c
+++ b/crypto/ccm.c
@@ -603,7 +603,7 @@ static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
 	struct crypto_aead *child = ctx->child;
 	struct scatterlist *sg;
-	u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
+	u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(child),
 			   crypto_aead_alignmask(child) + 1);
 
 	/* L' */
diff --git a/crypto/gcm.c b/crypto/gcm.c
index 5f16b237b..4ec24ff27 100644
--- a/crypto/gcm.c
+++ b/crypto/gcm.c
@@ -475,7 +475,7 @@ static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
 	struct aead_request *subreq = &rctx->subreq;
 	struct crypto_aead *child = ctx->child;
 	struct scatterlist *sg;
-	u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
+	u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(child),
 			   crypto_aead_alignmask(child) + 1);
 
 	scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
-- 
2.43.0


  reply	other threads:[~2026-11-29  8:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29  8:56 ` Jack Wallner [this message]
2026-11-29  8:56 ` [PATCH 2/8] crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses Jack Wallner
2026-11-29  8:56 ` [PATCH 3/8] misc: pci_endpoint_test: " Jack Wallner
2026-11-29  8:56 ` [PATCH 4/8] be2net: Use ALIGN() on a DMA address Jack Wallner
2026-11-29  8:56 ` [PATCH 5/8] octeontx2-pf: " Jack Wallner
2026-11-29  8:56 ` [PATCH 6/8] scsi: be2iscsi: " Jack Wallner
2026-11-29  8:56 ` [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29  8:56 ` [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test Jack Wallner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129085612.22266-2-jackwallner@gmail.com \
    --to=jackwallner@gmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=kees@kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox