arm64: compat: Keep alignment address arithmetic 32-bit
PATCH 1/1 arm64: compat: Keep alignment address arithmetic 32-bit
The compat alignment emulator inherited unsigned long data addresses from the 32-bit ARM implementation. On arm64, negating the unsigned int transfer size wraps it at 32 bits before it is added to a 64-bit address. A decrementing LDM or STM therefore adds nearly 4 GiB instead of subtracting its transfer size. The resulting address lies outside the compat task's address space, so the access fails and the process gets a spurious SIGBUS instead of the fixup.
Using 64-bit addresses also prevents transfer and writeback arithmetic from wrapping at the AArch32 address-space boundary.
Use 32-bit types for emulated data addresses and offsets, and convert them with compat_ptr() at the uaccess boundary. This preserves AArch32 modulo-2^32 address generation for every supported transfer form.
---
Tested:
Built a static AArch32 alignment test. It deliberately executes unaligned multiword instructions. A 64-bit BusyBox initramfs runs it on arm64 QEMU and Pi 400, exercising compat_alignment.c.
On arm64 QEMU and Pi 400, the old kernel gets SIGBUS for ARM LDMDA, STMDA, LDMDB, STMDB, Thumb-2 LDMDB/STMDB, and Thumb-1 PUSH. With this change, all 18 cases pass.
The LDRD/STRD register-offset wrap test also passes on both arm64 targets.
Native ARM32 QEMU: all runnable cases pass. The high-address wrap probes are skipped because of the 3G/1G user/kernel split.
arch/arm64/kernel/compat_alignment.c | 30 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 13 deletions(-)
arch/arm64/kernel/compat_alignment.c +17 -13
@@ -2,6 +2,7 @@// based on arch/arm/mm/alignment.c #include <linux/compiler.h> +#include <linux/compat.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/init.h>
@@ -41,8 +42,8 @@ (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))union offset_union { -unsigned long un; -signed long sn; +u32 un; +s32 sn; };#define TYPE_ERROR 0
@@ -51,7 +52,7 @@ union offset_union {#define TYPE_DONE 3static void -do_alignment_finish_ldst (unsigned long addr,u32 instr,struct pt_regs *regs, +do_alignment_finish_ldst (u32 addr,u32 instr,struct pt_regs *regs,union offset_union offset) {if (!LDST_U_BIT (instr))
@@ -65,7 +66,7 @@ do_alignment_finish_ldst(unsigned long addr, u32 instr, struct pt_regs *regs, }static int -do_alignment_ldrdstrd (unsigned long addr,u32 instr,struct pt_regs *regs) +do_alignment_ldrdstrd (u32 addr,u32 instr,struct pt_regs *regs) {unsigned int rd =RD_BITS (instr);unsigned int rd2;
@@ -85,14 +86,15 @@ do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs)if (load) {unsigned int val, val2; -if (get_user (val, (u32 __user *)addr) || -get_user (val2, (u32 __user *)(addr +4 ))) +if (get_user (val, (u32 __user *)compat_ptr (addr)) || +get_user (val2, (u32 __user *)compat_ptr (addr +4 )))return TYPE_FAULT ; regs->regs[rd] = val; regs->regs[rd2] = val2; }else { -if (put_user (regs->regs[rd], (u32 __user *)addr) || -put_user (regs->regs[rd2], (u32 __user *)(addr +4 ))) +if (put_user (regs->regs[rd], (u32 __user *)compat_ptr (addr)) || +put_user (regs->regs[rd2], + (u32 __user *)compat_ptr (addr +4 )))return TYPE_FAULT ; }return TYPE_LDST ;
@@ -112,10 +114,10 @@ do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs)* PU = 10 A B */ static int -do_alignment_ldmstm (unsigned long addr,u32 instr,struct pt_regs *regs) +do_alignment_ldmstm (u32 addr,u32 instr,struct pt_regs *regs) {unsigned int rd, rn, nr_regs, regbits; -unsigned long eaddr, newaddr; +u32 eaddr, newaddr;unsigned int val;/* count the number of registers in the mask to be transferred */
@@ -137,7 +139,8 @@ do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs) regbits >>= 1, rd += 1)if (regbits &1 ) {if (LDST_L_BIT (instr)) { -if (get_user (val, (u32 __user *)eaddr)) +if (get_user (val, + (u32 __user *)compat_ptr (eaddr)))return TYPE_FAULT ;if (rd <15 ) regs->regs[rd] = val;
@@ -152,7 +155,8 @@ do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs)* to refer to PC, just add 8 here. */ val = (rd <15 ) ? regs->regs[rd] : regs->pc +8 ; -if (put_user (val, (u32 __user *)eaddr)) +if (put_user (val, + (u32 __user *)compat_ptr (eaddr)))return TYPE_FAULT ; } eaddr +=4 ;
@@ -311,7 +315,7 @@ int do_compat_alignment_fixup(unsigned long addr, struct pt_regs *regs) {union offset_union offset;unsigned long instrptr; -int (*handler)(unsigned long addr,u32 instr,struct pt_regs *regs); +int (*handler)(u32 addr,u32 instr,struct pt_regs *regs);unsigned int type;u32 instr =0 ;int isize =4 ;
Signature · 1 line
2.53.0
Quoted 10 lines · Karl Mehltretter
The compat alignment emulator inherited unsigned long data addresses from the 32-bit ARM implementation. On arm64, negating the unsigned int transfer size wraps it at 32 bits before it is added to a 64-bit address. A decrementing LDM or STM therefore adds nearly 4 GiB instead of subtracting its transfer size. The resulting address lies outside the compat task's address space, so the access fails and the process gets a spurious SIGBUS instead of the fixup.
Using 64-bit addresses also prevents transfer and writeback arithmetic from wrapping at the AArch32 address-space boundary.
Hi Karl,
Nice find! How did you come across this?
Your patch looks correct to me, but it took me a bit to understand it, as I found the use of compat_ptr() and changing the addressing to 32-bit a little confusing at first.
arch/arm64/kernel/compat_alignment.c do_alignment_ldrdstrd(unsigned long addr, u32 instr, struct pt_regs *regs) · line 121
Quoted 4 lines
arch/arm64/kernel/compat_alignment.c +1
-1unsigned int rd, rn, nr_regs, regbits; -unsigned long eaddr, newaddr; +u32 eaddr, newaddr;unsigned int val;
As I understand it, the underlying problem here is the 32-bit overflow of nr_regs. Wouldn't it be sufficient to just turn nr_regs into an 'unsigned long' or 'size_t' in both instances?
arch/arm64/kernel/compat_alignment.c do_alignment_ldmstm(unsigned long addr, u32 instr, struct pt_regs *regs) · line 143
Quoted 3 lines
arch/arm64/kernel/compat_alignment.c +2
-1-if (get_user (val, (u32 __user *)eaddr)) +if (get_user (val, + (u32 __user *)compat_ptr (eaddr)))
The individual compat_ptr() in each access looks like it would have been sufficient as well, by avoiding the effect of the overflow, and it also makes the address wrap back to zero at the end of the address space. What's a bit confusing here is that accessing an unaligned set of words at the end of the address space will still read a couple of bytes beyond the end of the 32-bit space.
Again, none of this is wrong, just wondering whether a simpler change would make this easier to understand and keep the code closer to the original arm32 version.
Arnd
Quoted 1 line · Arnd Bergmann
Nice find! How did you come across this?
Hi Arnd,
This didn't start as a real world bug hunt. I only recently became aware of the compat alignment fixups and wanted to see how the implementation differed from the arm32 one.
This is the only issue I found that was a regression from the arm32 implementation, and it looks like this can happen in real usage.
Quoted 3 lines
As I understand it, the underlying problem here is the 32-bit overflow of
nr_regs. Wouldn't it be sufficient to just turnnr_regsinto an 'unsigned long' or 'size_t'
You are right that simply widening nr_regs fixes the incorrect address arithmetic for decrementing LDM/STM transfers.
The 4 GiB wraparound case is possible with a rather unusual arm64 kernel configuration, and correctly handling an individual word crossing the boundary would require byte accesses. That seems too contrived to justify the extra complexity here.
Just to be sure by "both instances" do you mean the nr_regs declarations in both arch/arm64/kernel/compat_alignment.c and arch/arm/mm/alignment.c?
Changing nr_regs in arm32 alignment.c would not change the generated code and I guess probably not be backported, so that would create a divergence with stable kernels.
Would you prefer changing both files, or only the arm64 implementation?
Thanks,
Karl
Quoted 1 line · Karl Mehltretter
On Mon, Aug 17, 2026 at 11:15:33AM +0100, Arnd Bergmann wrote:
<
Quoted 4 lines
The 4 GiB wraparound case is possible with a rather unusual arm64 kernel configuration, and correctly handling an individual word crossing the boundary would require byte accesses. That seems too contrived to justify the extra complexity here.
I don't understand, what is special about the configuration? Isn't this exactly the case you were trying to address with the compat_ptr() hack?
Quoted 3 lines
Just to be sure by "both instances" do you mean the
nr_regsdeclarations in botharch/arm64/kernel/compat_alignment.candarch/arm/mm/alignment.c?
Yes
Quoted 5 lines
Changing
nr_regsin arm32alignment.cwould not change the generated code and I guess probably not be backported, so that would create a divergence with stable kernels.Would you prefer changing both files, or only the arm64 implementation?
I would suggest doing only a small change to arm64.
Arnd
Quoted 12 lines · Arnd Bergmann
Quoted 1 line · Karl Mehltretter
On Mon, Aug 17, 2026 at 11:15:33AM +0100, Arnd Bergmann wrote:
<
Quoted 4 lines
The 4 GiB wraparound case is possible with a rather unusual arm64 kernel configuration, and correctly handling an individual word crossing the boundary would require byte accesses. That seems too contrived to justify the extra complexity here.
I don't understand, what is special about the configuration? Isn't this exactly the case you were trying to address with the
compat_ptr()hack?
Your first reply also made me realize my patch was too complicated for the original bug.
I now used:
CONFIG_EXPERT=y
CONFIG_ARM64_64K_PAGES=y
CONFIG_COMPAT=y
CONFIG_KUSER_HELPERS=y
CONFIG_COMPAT_ALIGNMENT_FIXUPS=y
CONFIG_DEFAULT_MMAP_MIN_ADDR=0
With 64K pages and KUSER_HELPERS, compat TASK_SIZE is 4 GiB. The last 64K is the kuser mapping. The test maps the first 64K at zero, so memory exists on both sides of the wrap.
This does not work with the normal 4K-page config because the top page is outside the compat user space. EXPERT is needed for COMPAT with 64K pages.
On native ARM32, the kernel is mapped at the top of the address space. Userspace cannot reach the 4 GiB wrap.
I tested three versions:
v1: with u32 addresses and compat_ptr() (this patch) v1+: v1 plus byte reads for a word crossing 4 GiB
v2: only make nr_regs unsigned long (as you suggested)
The results on the Pi 400 were:
v1 v1+ v2 ordinary decrementing LDMDB PASS PASS PASS hardware LDR at 0xfffffffd PASS PASS PASS hardware LDRD at 0xfffffffc PASS PASS PASS emulated LDRD word straddles 4 GiB SIGBUS PASS SIGBUS emulated LDMIA word straddles 4 GiB SIGBUS PASS SIGBUS emulated LDMDB underflow and straddle SIGBUS PASS SIGBUS LDRD post-index writeback wrap PASS PASS PASS
QEMU gives the same results.
The hardware LDR test does not use compat_alignment.c. The Cortex-A72 runs the LDR at 0xfffffffd directly. It reads 0xfffffffd, 0xfffffffe, 0xffffffff and zero successfully.
For example, LDMDB with two registers and a base of 5 starts at:
5 - 8 modulo 2^32 = 0xfffffffd
v1 calculates this address. v2 calculates 0xfffffffffffffffd instead.
compat_ptr() fixes the start address, but not the whole access. A get_user(u32) at 0xfffffffd still tries to read four continuous bytes up to 0x100000000, so access_ok() rejects it.
This is why v1 also gets SIGBUS. It has the correct start address, but still uses one get_user(u32). v1+ reads the four bytes separately and wraps the last byte to zero.
The LDRD at 0xfffffffc wraps between its two words. But it is 4-byte aligned and runs directly, so it does not test the alignment handler.
Any multiword access going through the handler is not 4-byte aligned. If it crosses 4 GiB, at least one u32 access also crosses it. So v1 gets the address calculation right, but get_user(u32) or put_user(u32) still fails.
I am not sure who would use an unaligned multiword access wrapping at 4 GiB and expect the compat alignment handler to fix it.
v2 still keeps the normal fixup path. Successful fixups are counted by the perf alignment-faults event. If a fixup fails, the task gets SIGBUS and the normal arm64 fault logging can report it.
So v2 is enough for the original decrementing LDM/STM bug. v1 does not add a working wrap case by itself. I think the small v2 fix makes more sense. v1+ would be the theoretically complete version for AArch32 wrap handling...
Am I getting this right now, or am I still missing a case?
Karl
Quoted 11 lines · Karl Mehltretter
Quoted 1 line · Arnd Bergmann
On Tue, Aug 18, 2026, at 05:22, Karl Mehltretter wrote:
I now used:
CONFIG_EXPERT=y
CONFIG_ARM64_64K_PAGES=y
CONFIG_COMPAT=y
CONFIG_KUSER_HELPERS=y
CONFIG_COMPAT_ALIGNMENT_FIXUPS=y
CONFIG_DEFAULT_MMAP_MIN_ADDR=0
Quoted 3 lines
With 64K pages and
KUSER_HELPERS, compatTASK_SIZEis 4 GiB. The last 64K is the kuser mapping. The test maps the first 64K at zero, so memory exists on both sides of the wrap.
Hopefully nobody actually uses CONFIG_DEFAULT_MMAP_MIN_ADDR=0.
I don't see yet why 64K_PAGES makes a difference. I had expected TASK_SIZE to be 4GiB regardless of the page size, and the top page (but not the bottom page) to be reachable from normal userspace.
Quoted 2 lines
On native ARM32, the kernel is mapped at the top of the address space. Userspace cannot reach the 4 GiB wrap.
right
Quoted 6 lines
So v2 is enough for the original decrementing LDM/STM bug. v1 does not add a working wrap case by itself. I think the small v2 fix makes more sense. v1+ would be the theoretically complete version for AArch32 wrap handling...
Am I getting this right now, or am I still missing a case?
That sounds good, v2 should be all we need then.
Arnd
Quoted 5 lines · Arnd Bergmann
I don't see yet why 64K_PAGES makes a difference. I had expected
TASK_SIZEto be 4GiB regardless of the page size, and the top page (but not the bottom page) to be reachable from normal userspace.
There is a special case in arch/arm64/include/asm/processor.h:
#if defined(CONFIG_ARM64_64K_PAGES) && defined(CONFIG_KUSER_HELPERS)
#define TASK_SIZE_32 UL(0x100000000)
#else
#define TASK_SIZE_32 (UL(0x100000000) - PAGE_SIZE)
#endif
Only ARM64_64K_PAGES with KUSER_HELPERS gives compat tasks the full 4 GiB. The 64K kuser mapping then occupies the last page.
I'll send a v2 soon.
Karl
Quoted 15 lines · Karl Mehltretter
Quoted 5 lines · Arnd Bergmann
I don't see yet why 64K_PAGES makes a difference. I had expected
TASK_SIZEto be 4GiB regardless of the page size, and the top page (but not the bottom page) to be reachable from normal userspace.There is a special case in
arch/arm64/include/asm/processor.h:#if
defined(CONFIG_ARM64_64K_PAGES)&&defined(CONFIG_KUSER_HELPERS)
#defineTASK_SIZE_32UL(0x100000000)
#else
#defineTASK_SIZE_32(UL(0x100000000) -PAGE_SIZE)
#endif
Quoted 3 lines
Only
ARM64_64K_PAGESwithKUSER_HELPERSgives compat tasks the full 4 GiB. The 64K kuser mapping then occupies the last page.
Ok, I wasn't aware of this bit at all. I think you can just skip explaining it in the changelog then and not spend too much time worrying about the case of userspace intentionally overflowing the 32-bit address space, as we clearly try hard to make that impossible in all kinds of ways.
Just explain the important bit with the decrement
causing the compat mode to be completely wrong.
Arnd