Preserve object size through PTR_ALIGN()
Subsystems (13)
cover Preserve object size through PTR_ALIGN()
PTR_ALIGN() rounds a pointer up by casting it to unsigned long and back, which makes the compiler lose track of the object the pointer belongs to. __builtin_object_size() and __builtin_dynamic_object_size() then return "unknown" for the result and FORTIFY_SOURCE cannot bound accesses made through it. This is KSPP issue 191:
https://github.com/KSPP/linux/issues/191
Patch 7 changes PTR_ALIGN() to add the padding to the original pointer instead, which is the approach the October triage note on that issue suggests. Patch 8 adds a KUnit test. Patches 1 to 6 prepare callers.
Patch 1 changes two crypto templates that compute an address starting one past the end of a struct member. That only escaped a FORTIFY warning because of the integer cast in PTR_ALIGN().
Patches 2 to 6 convert the ten callers that pass a dma_addr_t to PTR_ALIGN() over to ALIGN(), one patch per subsystem. Patches 3, 4 and 6 are needed before patch 7: without them it adds cast warnings on 32-bit kernels with a 64-bit dma_addr_t. Patches 2 and 5 are
for drivers that depend on 64BIT, where nothing would warn, and are only there so that no caller passes an integer to a macro meant for pointers.
PTR_ALIGN_DOWN() is not changed. Its result is often meant to point before the object its argument points into (the start of a page or of a block of page table entries), so it needs its own look at the 25 users. Patch 7 has the details.
I do not know which tree this should go through. The driver patches are independent of each other and can be picked up by their maintainers. If it is easier, the whole series could go through the hardening tree with acks.
Testing, on mainline at 946d9ed1aa02 (v7.4-rc4 plus a few merges), all with the full series applied unless noted:
- lib/tests/fortify_kunit and the crypto self-tests (CRYPTO_SELFTESTS_FULL) under QEMU on x86-64, built with GCC 13.3 and with Clang 18, with and without KASAN: 26 of 26 fortify cases pass and there is no FORTIFY or KASAN report. With the old macro the new test case fails on both compilers and the other 25 pass. Without patch 1 the GCC boot prints the warning quoted in patch 1.
- The same boot with Clang 18, UBSAN_BOUNDS and UBSAN_TRAP, which turns on -fsanitize=local-bounds, with and without the series: no trap.
- allmodconfig builds with W=1 of the C files that use PTR_ALIGN(), before and after: x86-64 and i386 with GCC, and x86-64, arm64, arm, riscv and powerpc with Clang. No new diagnostics in any file that builds. arch/x86/kernel/unwind_frame.c is not part of that configuration and fails to compile with and without the series. 125 of the 136 files build in at least one of these. Six more build with the series applied on s390, loongarch and mips with Clang, without a diagnostic from the macro. The other five did not build in any configuration I tried.
- sparse (built from git) on the callers that align __user and __iomem pointers on x86-64: no new warnings, also with -Wcast-to-as.
- The compile-time scan described in patch 7, which is how I looked for callers that would get a new FORTIFY report.
Not tested: any of the drivers on real hardware, GCC other than 13.3, Clang other than 18, LTO builds, and sparse on the arm64 __user caller. linux-next has four new PTR_ALIGN() users in lib/crypto/tests/ that the scan did not cover. They align pointers into buffers allocated with room for the padding.
Use of AI tools, as Documentation/process/generated-content.rst asks:
the code, the test and the changelogs in this series were written by an AI coding assistant (Anthropic's Claude, run in Claude Code). I asked it to pick one open KSPP issue and produce a fix that could be merged, and then had separate Claude sessions review the result with the instruction to find reasons to reject it. Those reviews are where the caller changes in patches 1 to 6, the compile-time scan and the UBSAN boot came from. The assistant also ran every build, boot and scan listed above, on my machines. I have read the series and the test results, and I am responsible for it.
Jack Wallner (8):
crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request
context
crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses
misc: pci_endpoint_test: Use ALIGN() on DMA addresses
be2net: Use ALIGN() on a DMA address
octeontx2-pf: Use ALIGN() on a DMA address
scsi: be2iscsi: Use ALIGN() on a DMA address vdso/align: Preserve object size through PTR_ALIGN() kunit/fortify: Add PTR_ALIGN() object size test
crypto/ccm.c | 2 +- crypto/gcm.c | 2 +- drivers/crypto/cavium/nitrox/nitrox_lib.c | 2 +- .../crypto/marvell/octeontx/otx_cptpf_ucode.c | 2 +- drivers/crypto/marvell/octeontx2/otx2_cptlf.h | 3 +- drivers/misc/pci_endpoint_test.c | 8 +-- drivers/net/ethernet/emulex/benet/be_main.c | 2 +- .../marvell/octeontx2/nic/cn10k_ipsec.c | 2 +- drivers/scsi/be2iscsi/be_main.c | 2 +- include/vdso/align.h | 23 ++++++- lib/tests/fortify_kunit.c | 63 +++++++++++++++++++ 11 files changed, 97 insertions(+), 14 deletions(-)
base-commit: 946d9ed1aa021d572467a1cdcdc2991f89b788a1
Signature · 1 line
2.43.0
PATCH 1/8 crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context
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.
--- crypto/ccm.c | 2 +- crypto/gcm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
crypto/ccm.c +1 -1
@@ -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' */
crypto/gcm.c +1 -1
@@ -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 );
Signature · 1 line
2.43.0
PATCH 2/8 crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses
In reply to Jack WallnerPTR_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.
--- 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(-)
drivers/crypto/cavium/nitrox/nitrox_lib.c +1 -1
@@ -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 ;
drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c +1 -1
@@ -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);
drivers/crypto/marvell/octeontx2/otx2_cptlf.h +1 -2
@@ -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 ;
Signature · 1 line
2.43.0
PATCH 3/8 misc: pci_endpoint_test: Use ALIGN() on DMA addresses
In reply to Jack WallnerPTR_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.
--- drivers/misc/pci_endpoint_test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/misc/pci_endpoint_test.c +4 -4
@@ -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 {
Signature · 1 line
2.43.0
PATCH 4/8 be2net: Use ALIGN() on a DMA address
In reply to Jack WallnerPTR_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.
--- drivers/net/ethernet/emulex/benet/be_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/emulex/benet/be_main.c +1 -1
@@ -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,
Signature · 1 line
2.43.0
PATCH 5/8 octeontx2-pf: Use ALIGN() on a DMA address
In reply to Jack WallnerPTR_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.
--- drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/cn10k_ipsec.c +1 -1
@@ -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 ; }
Signature · 1 line
2.43.0
PATCH 6/8 scsi: be2iscsi: Use ALIGN() on a DMA address
In reply to Jack WallnerPTR_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.
--- drivers/scsi/be2iscsi/be_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/be2iscsi/be_main.c +1 -1
@@ -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);
Signature · 1 line
2.43.0
PATCH 7/8 vdso/align: Preserve object size through PTR_ALIGN()
In reply to Jack WallnerPTR_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.
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.
--- include/vdso/align.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
include/vdso/align.h +22 -1
@@ -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)
Signature · 1 line
2.43.0
Re: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN()
Hi Jack,
Quoted 106 lines
Date: 2026-11-29 00:56:09-0800
From: Jack Wallner <[email protected]>
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_SOURCEthen cannot bound amemcpy()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)isSIZE_MAXbefore this patch and the number of bytes left in the allocation after it (136 ifkmalloc()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__forceso that sparse stays quiet for the callers that align__userand__iomempointers.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)), andFORTIFY_SOURCEwarns about amemcpy()through it that runs past the end of the member. Two callers incrypto/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, whichUBSAN_BOUNDSwithUBSAN_TRAPenables):
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_BOUNDSandUBSAN_TRAPpasses 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 usePTR_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, anddrivers/dma/ste_dma40.c(2),drivers/i2c/busses/i2c-ismt.c(2) anddrivers/net/ethernet/broadcom/bnxt/bnxt.c(1), where the member is the buffer being aligned. Of those three drivers onlyi2c-ismt.cpasses 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.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 <[email protected]>--- include/vdso/align.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)include/vdso/align.h +7
-1@@ -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
include/vdso/align.h line 27
Quoted 10 lines
include/vdso/align.h +10
-0+* 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); \
This 'typeof(p)' could be replaced with just 'auto', right?
include/vdso/align.h line 30
Quoted 3 lines
include/vdso/align.h +3
-0+unsigned long __ptr_align_v = (unsigned long )__ptr_align_p; \ + \ + (__force typeof(p))((void __force *)__ptr_align_p + \
The attribute makes more sense before the type, not in between. I see both uses already existing in the kernel, but attributes shouldn't go there.
(__force void *)
Have a lovely day!
Alex
Quoted 9 lines
include/vdso/align.h +2
-0+ (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)Signature · 1 line
2.43.0
Signature · 1 line
<https://www.alejandro-colomar.es>
PATCH 8/8 kunit/fortify: Add PTR_ALIGN() object size test
In reply to Jack WallnerCheck 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.
--- lib/tests/fortify_kunit.c | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
lib/tests/fortify_kunit.c +63 -0
@@ -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), {} };
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this 8-patch series and found no issues.
Sashiko has reviewed this 8-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]