* [PATCH 1/8] crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 2/8] crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses Jack Wallner
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Herbert Xu, David S. Miller,
linux-crypto
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 2/8] crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29 8:56 ` [PATCH 1/8] crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 3/8] misc: pci_endpoint_test: " Jack Wallner
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Herbert Xu, David S. Miller,
Srujana Challa, Bharat Bhushan, linux-crypto
PTR_ALIGN() is meant for pointers. nitrox_cmdq_init(),
copy_ucode_to_dma_mem() and otx2_cpt_alloc_instruction_queues() pass it
a dma_addr_t. That compiles because PTR_ALIGN() casts its argument to
unsigned long and back.
A later patch in this series makes PTR_ALIGN() do pointer arithmetic on
its argument so that the compiler can keep track of the object size,
and documents that the argument has to be a pointer. Use ALIGN() for
these addresses, which works in the type of its argument.
No functional change: all three drivers depend on 64BIT, where both
macros give the same value.
Compile tested only.
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>
---
drivers/crypto/cavium/nitrox/nitrox_lib.c | 2 +-
drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c | 2 +-
drivers/crypto/marvell/octeontx2/otx2_cptlf.h | 3 +--
3 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/drivers/crypto/cavium/nitrox/nitrox_lib.c b/drivers/crypto/cavium/nitrox/nitrox_lib.c
index d35303857..e40fcebb3 100644
--- a/drivers/crypto/cavium/nitrox/nitrox_lib.c
+++ b/drivers/crypto/cavium/nitrox/nitrox_lib.c
@@ -33,7 +33,7 @@ static int nitrox_cmdq_init(struct nitrox_cmdq *cmdq, int align_bytes)
if (!cmdq->unalign_base)
return -ENOMEM;
- cmdq->dma = PTR_ALIGN(cmdq->unalign_dma, align_bytes);
+ cmdq->dma = ALIGN(cmdq->unalign_dma, align_bytes);
cmdq->base = cmdq->unalign_base + (cmdq->dma - cmdq->unalign_dma);
cmdq->write_idx = 0;
diff --git a/drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c b/drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c
index c66cd3229..0cac7f104 100644
--- a/drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c
+++ b/drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c
@@ -853,7 +853,7 @@ static int copy_ucode_to_dma_mem(struct device *dev,
return -ENOMEM;
}
ucode->align_va = PTR_ALIGN(ucode->va, OTX_CPT_UCODE_ALIGNMENT);
- ucode->align_dma = PTR_ALIGN(ucode->dma, OTX_CPT_UCODE_ALIGNMENT);
+ ucode->align_dma = ALIGN(ucode->dma, OTX_CPT_UCODE_ALIGNMENT);
memcpy((void *) ucode->align_va, (void *) ucode_data +
sizeof(struct otx_cpt_ucode_hdr), ucode->size);
diff --git a/drivers/crypto/marvell/octeontx2/otx2_cptlf.h b/drivers/crypto/marvell/octeontx2/otx2_cptlf.h
index 1b9f75214..c71647c46 100644
--- a/drivers/crypto/marvell/octeontx2/otx2_cptlf.h
+++ b/drivers/crypto/marvell/octeontx2/otx2_cptlf.h
@@ -178,8 +178,7 @@ static inline int otx2_cpt_alloc_instruction_queues(
/* Align pointers */
iq->vaddr = PTR_ALIGN(iq->vaddr, OTX2_CPT_INST_Q_ALIGNMENT);
- iq->dma_addr = PTR_ALIGN(iq->dma_addr,
- OTX2_CPT_INST_Q_ALIGNMENT);
+ iq->dma_addr = ALIGN(iq->dma_addr, OTX2_CPT_INST_Q_ALIGNMENT);
}
return 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 3/8] misc: pci_endpoint_test: Use ALIGN() on DMA addresses
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29 8:56 ` [PATCH 1/8] crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context Jack Wallner
2026-11-29 8:56 ` [PATCH 2/8] crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 4/8] be2net: Use ALIGN() on a DMA address Jack Wallner
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Manivannan Sadhasivam,
Krzysztof Wilczyński, Kishon Vijay Abraham I, Frank Li,
Arnd Bergmann, Greg Kroah-Hartman, linux-pci
PTR_ALIGN() is meant for pointers. The copy, write and read tests pass
it a dma_addr_t in four places. That compiles because PTR_ALIGN() casts
its argument to unsigned long and back.
A later patch in this series makes PTR_ALIGN() do pointer arithmetic on
its argument so that the compiler can keep track of the object size.
With that change these four lines get -Wint-to-pointer-cast and
-Wpointer-to-int-cast warnings on 32-bit kernels with a 64-bit
dma_addr_t (x86 with PAE, for example).
Use ALIGN(), which works in the type of its argument. There is no change
on 64-bit kernels. On a 32-bit kernel with a 64-bit dma_addr_t the old
form would drop the upper 32 bits of the address. I have not seen that
happen and did not try to trigger it: this is a cleanup and a
prerequisite for the PTR_ALIGN() change, not a fix for a reported
problem.
Compile tested only, including i386 allmodconfig, which has a 64-bit
dma_addr_t.
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>
---
drivers/misc/pci_endpoint_test.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
index dd49ca7f4..17d53375e 100644
--- a/drivers/misc/pci_endpoint_test.c
+++ b/drivers/misc/pci_endpoint_test.c
@@ -750,7 +750,7 @@ static int pci_endpoint_test_copy(struct pci_endpoint_test *test,
}
if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
- src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
+ src_phys_addr = ALIGN(orig_src_phys_addr, alignment);
offset = src_phys_addr - orig_src_phys_addr;
src_addr = orig_src_addr + offset;
} else {
@@ -782,7 +782,7 @@ static int pci_endpoint_test_copy(struct pci_endpoint_test *test,
}
if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
- dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
+ dst_phys_addr = ALIGN(orig_dst_phys_addr, alignment);
offset = dst_phys_addr - orig_dst_phys_addr;
dst_addr = orig_dst_addr + offset;
} else {
@@ -884,7 +884,7 @@ static int pci_endpoint_test_write(struct pci_endpoint_test *test,
}
if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
- phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
+ phys_addr = ALIGN(orig_phys_addr, alignment);
offset = phys_addr - orig_phys_addr;
addr = orig_addr + offset;
} else {
@@ -979,7 +979,7 @@ static int pci_endpoint_test_read(struct pci_endpoint_test *test,
}
if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
- phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
+ phys_addr = ALIGN(orig_phys_addr, alignment);
offset = phys_addr - orig_phys_addr;
addr = orig_addr + offset;
} else {
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/8] be2net: Use ALIGN() on a DMA address
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
` (2 preceding siblings ...)
2026-11-29 8:56 ` [PATCH 3/8] misc: pci_endpoint_test: " Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 5/8] octeontx2-pf: " Jack Wallner
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Ajit Khaparde,
Sriharsha Basavapatna, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, netdev
PTR_ALIGN() is meant for pointers. be_drv_init() passes it the DMA
address of the mailbox, a dma_addr_t. That compiles because PTR_ALIGN()
casts its argument to unsigned long and back.
A later patch in this series makes PTR_ALIGN() do pointer arithmetic on
its argument so that the compiler can keep track of the object size.
With that change this line gets -Wint-to-pointer-cast and
-Wpointer-to-int-cast warnings on 32-bit kernels with a 64-bit
dma_addr_t (x86 with PAE, for example). I confirmed that with an i386
allmodconfig build.
Use ALIGN(), which works in the type of its argument. There is no change
on 64-bit kernels. On a 32-bit kernel with a 64-bit dma_addr_t the old
form would drop the upper 32 bits of the address. I have not seen that
happen and did not try to trigger it: this is a cleanup and a
prerequisite for the PTR_ALIGN() change, not a fix for a reported
problem.
Compile tested only, including i386 allmodconfig, which has a 64-bit
dma_addr_t.
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>
---
drivers/net/ethernet/emulex/benet/be_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/emulex/benet/be_main.c b/drivers/net/ethernet/emulex/benet/be_main.c
index ac09fd3a4..b2544de63 100644
--- a/drivers/net/ethernet/emulex/benet/be_main.c
+++ b/drivers/net/ethernet/emulex/benet/be_main.c
@@ -5639,7 +5639,7 @@ static int be_drv_init(struct be_adapter *adapter)
mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
- mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
+ mbox_mem_align->dma = ALIGN(mbox_mem_alloc->dma, 16);
rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
rx_filter->va = dma_alloc_coherent(dev, rx_filter->size,
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5/8] octeontx2-pf: Use ALIGN() on a DMA address
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
` (3 preceding siblings ...)
2026-11-29 8:56 ` [PATCH 4/8] be2net: Use ALIGN() on a DMA address Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 6/8] scsi: be2iscsi: " Jack Wallner
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Sunil Goutham, Geetha sowjanya,
Ratheesh Kannoth, Subbaraya Sundeep, Bharat Bhushan, Andrew Lunn,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
netdev
PTR_ALIGN() is meant for pointers. cn10k_outb_cptlf_iq_alloc() passes it
a dma_addr_t. That compiles because PTR_ALIGN() casts its argument to
unsigned long and back.
A later patch in this series makes PTR_ALIGN() do pointer arithmetic on
its argument so that the compiler can keep track of the object size,
and documents that the argument has to be a pointer. Use ALIGN() for
this address, which works in the type of its argument.
No functional change: the driver depends on 64BIT, where both macros
give the same value.
Compile tested only.
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>
---
drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c b/drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c
index 443435608..7ba0f4a28 100644
--- a/drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c
+++ b/drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c
@@ -248,7 +248,7 @@ static int cn10k_outb_cptlf_iq_alloc(struct otx2_nic *pf)
/* Align pointers */
iq->vaddr = PTR_ALIGN(iq->vaddr, OTX2_ALIGN);
- iq->dma_addr = PTR_ALIGN(iq->dma_addr, OTX2_ALIGN);
+ iq->dma_addr = ALIGN(iq->dma_addr, OTX2_ALIGN);
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 6/8] scsi: be2iscsi: Use ALIGN() on a DMA address
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
` (4 preceding siblings ...)
2026-11-29 8:56 ` [PATCH 5/8] octeontx2-pf: " Jack Wallner
@ 2026-11-29 8:56 ` 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
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Ketan Mukadam,
James E.J. Bottomley, Martin K. Petersen, linux-scsi
PTR_ALIGN() is meant for pointers. be_ctrl_init() passes it the DMA
address of the mailbox, a dma_addr_t. That compiles because PTR_ALIGN()
casts its argument to unsigned long and back.
A later patch in this series makes PTR_ALIGN() do pointer arithmetic on
its argument so that the compiler can keep track of the object size.
With that change this line gets -Wint-to-pointer-cast and
-Wpointer-to-int-cast warnings on 32-bit kernels with a 64-bit
dma_addr_t (x86 with PAE, for example).
Use ALIGN(), which works in the type of its argument. There is no change
on 64-bit kernels. On a 32-bit kernel with a 64-bit dma_addr_t the old
form would drop the upper 32 bits of the address. I have not seen that
happen and did not try to trigger it: this is a cleanup and a
prerequisite for the PTR_ALIGN() change, not a fix for a reported
problem.
Compile tested only, including i386 allmodconfig, which has a 64-bit
dma_addr_t.
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>
---
drivers/scsi/be2iscsi/be_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/scsi/be2iscsi/be_main.c b/drivers/scsi/be2iscsi/be_main.c
index cadcaefaa..4e1387a39 100644
--- a/drivers/scsi/be2iscsi/be_main.c
+++ b/drivers/scsi/be2iscsi/be_main.c
@@ -561,7 +561,7 @@ static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
- mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
+ mbox_mem_align->dma = ALIGN(mbox_mem_alloc->dma, 16);
memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
mutex_init(&ctrl->mbox_lock);
spin_lock_init(&phba->ctrl.mcc_lock);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN()
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
` (5 preceding siblings ...)
2026-11-29 8:56 ` [PATCH 6/8] scsi: be2iscsi: " Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test Jack Wallner
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook
Cc: linux-hardening, linux-kernel, Andy Lutomirski, Thomas Gleixner,
Vincenzo Frascino
PTR_ALIGN() rounds a pointer up by casting it to unsigned long, aligning
the integer and casting the result back. Compilers do not track which
object a pointer points into across that round trip, so
__builtin_object_size() and __builtin_dynamic_object_size() report
"unknown" for the result. FORTIFY_SOURCE then cannot bound a memcpy() or
similar call made through an aligned pointer.
Compute only the padding as an integer and add it to the original
pointer instead. The resulting address is the same, and both GCC and
Clang keep the object size. With
char *heap = kmalloc(200, GFP_KERNEL);
char *p = PTR_ALIGN(heap + 3, 64);
__builtin_dynamic_object_size(p, 0) is SIZE_MAX before this patch and
the number of bytes left in the allocation after it (136 if kmalloc()
returned a 64 byte aligned address), on GCC 13.3 and on Clang 18. The
same holds for pointers into stack objects and into struct members.
The argument has to be evaluated once and used twice, so PTR_ALIGN()
becomes a statement expression. Every current user is inside a function.
The casts use __force so that sparse stays quiet for the callers that
align __user and __iomem pointers.
The alignment itself compiles to the same instructions as before: at
-O2, GCC 13.3 on x86-64 and Clang 18 on x86-64, i386, arm, arm64,
riscv64 and powerpc64le emit identical code for the old and the new
form. In allmodconfig builds the text of the roughly 100 objects that
use PTR_ALIGN() grows by 0.09 to 0.15 percent in total on x86-64, i386,
arm64 and powerpc and by 0.35 to 0.5 percent on arm and riscv, the new
test case included. I attribute that to the compiler now carrying an
object size to more call sites.
PTR_ALIGN_DOWN() is left alone on purpose. Rounding down commonly gives
a pointer to before the start of the object the argument points into,
such as the start of the page holding it, and that pointer must not be
bounds checked against the smaller object. It has 25 users, most of them
page table and page start arithmetic, and can be looked at separately.
One thing does change for callers. Where GCC can see that the argument
points into a member of a structure, the result is now bounded by that
member as well (__builtin_dynamic_object_size(p, 1)), and FORTIFY_SOURCE
warns about a memcpy() through it that runs past the end of the member.
Two callers in crypto/ stepped past a member on purpose. The first patch
of this series changes them.
The same goes for Clang's -fsanitize=local-bounds
(CONFIG_UBSAN_LOCAL_BOUNDS, which UBSAN_BOUNDS with UBSAN_TRAP enables):
an access through the result into an object whose size Clang can see is
now bounds checked, where the cast used to hide it. A kernel built with
Clang 18, UBSAN_BOUNDS and UBSAN_TRAP passes the fortify KUnit suite and
the crypto self-tests with and without this series. I did not test that
configuration beyond that.
To find the callers that could get a new FORTIFY report I had the
compiler list them. I built allmodconfig with a temporary PTR_ALIGN()
that calls a function declared with __attribute__((warning)) whenever
__builtin_dynamic_object_size() of the result is known, which reports
each call site after inlining. That covered the C files that use
PTR_ALIGN() and the directories that reach it through header helpers
(crypto/, lib/crypto/, drivers/crypto/, block/, drivers/md/, fs/crypto/,
security/keys/ and the net and driver directories that use those
helpers), with GCC 13.3 for x86-64, i386 and arm64 and with Clang 18 for
x86-64 and arm64. Code that builds only for other architectures is not
covered.
A size is visible at 69 call sites. At 62 of them it is the size of the
whole allocation or stack buffer being aligned, so a report there would
be a real overflow. At 7, with GCC only, it is the size of a struct
member: the two crypto callers above, and drivers/dma/ste_dma40.c (2),
drivers/i2c/busses/i2c-ismt.c (2) and
drivers/net/ethernet/broadcom/bnxt/bnxt.c (1), where the member is the
buffer being aligned. Of those three drivers only i2c-ismt.c passes the
result to a fortified function, and its copies fit: at most 32 bytes at
offset 1 of a buffer with at least 33 bytes left after alignment.
Link: https://github.com/KSPP/linux/issues/191
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>
---
include/vdso/align.h | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/include/vdso/align.h b/include/vdso/align.h
index 02dd8626b..0b9fbac98 100644
--- a/include/vdso/align.h
+++ b/include/vdso/align.h
@@ -8,7 +8,28 @@
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))
#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
-#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
+
+/*
+ * Only the distance to the boundary is computed as an integer. Applying it
+ * to the original pointer, instead of casting the aligned integer back to a
+ * pointer, lets the compiler keep track of the object being pointed into, so
+ * __builtin_object_size() and __builtin_dynamic_object_size() still work on
+ * the result. @p must be a pointer: use ALIGN() for integers such as
+ * dma_addr_t.
+ *
+ * PTR_ALIGN_DOWN() keeps the integer round trip on purpose: its result often
+ * points before the start of the object that @p points into (the start of
+ * the page holding it, for example), which must not be bounds checked
+ * against that object.
+ */
+#define PTR_ALIGN(p, a) \
+({ \
+ typeof(p) __ptr_align_p = (p); \
+ unsigned long __ptr_align_v = (unsigned long)__ptr_align_p; \
+ \
+ (__force typeof(p))((void __force *)__ptr_align_p + \
+ (ALIGN(__ptr_align_v, (a)) - __ptr_align_v)); \
+})
#define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
` (6 preceding siblings ...)
2026-11-29 8:56 ` [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Jack Wallner
@ 2026-11-29 8:56 ` Jack Wallner
7 siblings, 0 replies; 9+ messages in thread
From: Jack Wallner @ 2026-11-29 8:56 UTC (permalink / raw)
To: Kees Cook; +Cc: linux-hardening, linux-kernel
Check that the compiler still knows the size of the object behind a
pointer after PTR_ALIGN(), for a member of a stack object and for a
kmalloc() allocation, and that an out of bounds read through the aligned
pointer is caught.
The test fails without "vdso/align: Preserve object size through
PTR_ALIGN()" and is skipped on compilers without
__builtin_dynamic_object_size().
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>
---
lib/tests/fortify_kunit.c | 63 +++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/lib/tests/fortify_kunit.c b/lib/tests/fortify_kunit.c
index 413cdbf3d..4df55eed3 100644
--- a/lib/tests/fortify_kunit.c
+++ b/lib/tests/fortify_kunit.c
@@ -43,6 +43,7 @@ void fortify_add_kunit_error(int write);
#include <kunit/device.h>
#include <kunit/test.h>
#include <kunit/test-bug.h>
+#include <linux/align.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/string.h>
@@ -1011,6 +1012,67 @@ static void fortify_test_kmemdup(struct kunit *test)
kfree(copy);
}
+struct fortify_ptr_align {
+ unsigned long bytes_before;
+ char buf[40];
+ unsigned long bytes_after;
+};
+
+/*
+ * PTR_ALIGN() must not hide which object a pointer points into, or the
+ * bounds checking loses track of it.
+ */
+static void fortify_test_ptr_align(struct kunit *test)
+{
+#if !__has_builtin(__builtin_dynamic_object_size)
+ kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n");
+#else
+ struct fortify_ptr_align pad = { };
+ size_t size = unknown_size;
+ size_t offset = 11;
+ char *heap, *up, *copy;
+ size_t avail;
+
+ /* Keep the compiler from knowing the starting offset. */
+ OPTIMIZER_HIDE_VAR(offset);
+
+ /* Into a member of a stack object. */
+ up = PTR_ALIGN(&pad.buf[offset], 8);
+ KUNIT_EXPECT_TRUE(test, IS_ALIGNED((unsigned long)up, 8));
+ KUNIT_EXPECT_LT(test, up - &pad.buf[offset], 8);
+ KUNIT_EXPECT_EQ(test, __struct_size(up),
+ sizeof(pad) - (up - (char *)&pad));
+ /*
+ * Compilers differ in whether they track the member (buf) or only
+ * the object (pad) here, so accept either bound.
+ */
+ KUNIT_EXPECT_GE(test, __member_size(up),
+ sizeof(pad.buf) - (up - pad.buf));
+ KUNIT_EXPECT_LE(test, __member_size(up), __struct_size(up));
+
+ /* Into a heap object. */
+ heap = kzalloc(size, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, heap);
+ up = PTR_ALIGN(heap + offset, 8);
+ avail = size - (up - heap);
+ KUNIT_EXPECT_EQ(test, __struct_size(up), avail);
+
+ /* Reading what is left after aligning is within bounds. */
+ copy = kmemdup(up, avail, GFP_KERNEL);
+ KUNIT_EXPECT_NOT_NULL(test, copy);
+ KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
+ kfree(copy);
+
+ /* Out of bounds by 1 byte is caught, and nothing is read. */
+ copy = kmemdup(up, avail + 1, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
+ KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
+ kfree(copy);
+
+ kfree(heap);
+#endif
+}
+
static int fortify_test_init(struct kunit *test)
{
if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE))
@@ -1054,6 +1116,7 @@ static struct kunit_case fortify_test_cases[] = {
KUNIT_CASE(fortify_test_memchr_inv),
KUNIT_CASE(fortify_test_memcmp),
KUNIT_CASE(fortify_test_kmemdup),
+ KUNIT_CASE(fortify_test_ptr_align),
{}
};
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread