Skip to contentSkip to conversation

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8 (v6.13-rc6, PowerMac G4)

DISCUSSION Erhard Furtner 4 participants 14 messages Jan 12, 2025 – Feb 2, 2025 1 message not in the archive PowerPC

lore
Share & tools
lore
#0 lore raw

Greetings!

I am getting this at bootup on my PowerMac G4 with a KASAN-enabled kernel 6.13-rc6:

[...]

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8
Write of size 8 at addr f1000000 by task chronyd/1293

CPU: 0 UID: 123 PID: 1293 Comm: chronyd Tainted: G        W          6.13.0-rc6-PMacG4 #2
Tainted: [W]=WARN
Hardware name: PowerMac3,6 7455 0x80010303 PowerMac
Call Trace:
[c2437590] [c1631a84] dump_stack_lvl+0x70/0x8c (unreliable)
[c24375b0] [c0504998] print_report+0xdc/0x504
[c2437610] [c050475c] kasan_report+0xf8/0x108
[c2437690] [c0505a3c] kasan_check_range+0x24/0x18c
[c24376a0] [c03fb5e4] copy_to_kernel_nofault+0xd8/0x1c8
[c24376c0] [c004c014] patch_instructions+0x15c/0x16c
[c2437710] [c00731a8] bpf_arch_text_copy+0x60/0x7c
[c2437730] [c0281168] bpf_jit_binary_pack_finalize+0x50/0xac
[c2437750] [c0073cf4] bpf_int_jit_compile+0xb30/0xdec
[c2437880] [c0280394] bpf_prog_select_runtime+0x15c/0x478
[c24378d0] [c1263428] bpf_prepare_filter+0xbf8/0xc14
[c2437990] [c12677ec] bpf_prog_create_from_user+0x258/0x2b4
[c24379d0] [c027111c] do_seccomp+0x3dc/0x1890
[c2437ac0] [c001d8e0] system_call_exception+0x2dc/0x420
[c2437f30] [c00281ac] ret_from_syscall+0x0/0x2c
--- interrupt: c00 at 0x5a1274
NIP:  005a1274 LR: 006a3b3c CTR: 005296c8
REGS: c2437f40 TRAP: 0c00   Tainted: G        W           (6.13.0-rc6-PMacG4)
MSR:  0200f932 <VEC,EE,PR,FP,ME,IR,DR,RI>  CR: 24004422  XER: 00000000

GPR00: 00000166 af8f3fa0 a7ee3540 00000001 00000000 013b6500 005a5858 0200f932
GPR08: 00000000 00001fe9 013d5fc8 005296c8 2822244c 00b2fcd8 00000000 af8f4b57
GPR16: 00000000 00000001 00000000 00000000 00000000 00000001 00000000 00000002
GPR24: 00afdbb0 00000000 00000000 00000000 006e0004 013ce060 006e7c1c 00000001 NIP [005a1274] 0x5a1274
LR [006a3b3c] 0x6a3b3c
--- interrupt: c00

The buggy address belongs to the virtual mapping at
 [f1000000, f1002000) created by:
 text_area_cpu_up+0x20/0x190

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:00000000 index:0x0 pfn:0x76e30
flags: 0x80000000(zone=2)
raw: 80000000 00000000 00000122 00000000 00000000 00000000 ffffffff 00000001
raw: 00000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 f0ffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 f0ffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>f1000000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
           ^
 f1000080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
 f1000100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
==================================================================

Disabling lock debugging due to kernel taint

Kernel .config and full dmesg attached. The other 2 warnings in the dmesg, "workqueue: work disable count underflowed" are https://lore.kernel.org/all/CA+G9fYsQmFT3m1J0uGfWGJjci8MQoB7BNcmhBR175PAbxx9fBg@mail.gmail.com/ (offending commit should be reverted in next -rc) and "Missing '#size-cells'" is fixed by https://lore.kernel.org/all/[email protected]/ (not yet merged)

Regards,
Erhard

#1 lore raw
Quoted 3 lines · Erhard Furtner

Greetings!

I am getting this at bootup on my PowerMac G4 with a KASAN-enabled kernel 6.13-rc6:

Sorry for the delayed response,

Are you seeing this only in this kernel or this is the recent kernel you tried to boot?

Maddy

Quoted 63 lines

[...]

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8
Write of size 8 at addr f1000000 by task chronyd/1293

CPU: 0 UID: 123 PID: 1293 Comm: chronyd Tainted: G        W          6.13.0-rc6-PMacG4 #2
Tainted: [W]=WARN
Hardware name: PowerMac3,6 7455 0x80010303 PowerMac
Call Trace:
[c2437590] [c1631a84] dump_stack_lvl+0x70/0x8c (unreliable)
[c24375b0] [c0504998] print_report+0xdc/0x504
[c2437610] [c050475c] kasan_report+0xf8/0x108
[c2437690] [c0505a3c] kasan_check_range+0x24/0x18c
[c24376a0] [c03fb5e4] copy_to_kernel_nofault+0xd8/0x1c8
[c24376c0] [c004c014] patch_instructions+0x15c/0x16c
[c2437710] [c00731a8] bpf_arch_text_copy+0x60/0x7c
[c2437730] [c0281168] bpf_jit_binary_pack_finalize+0x50/0xac
[c2437750] [c0073cf4] bpf_int_jit_compile+0xb30/0xdec
[c2437880] [c0280394] bpf_prog_select_runtime+0x15c/0x478
[c24378d0] [c1263428] bpf_prepare_filter+0xbf8/0xc14
[c2437990] [c12677ec] bpf_prog_create_from_user+0x258/0x2b4
[c24379d0] [c027111c] do_seccomp+0x3dc/0x1890
[c2437ac0] [c001d8e0] system_call_exception+0x2dc/0x420
[c2437f30] [c00281ac] ret_from_syscall+0x0/0x2c
--- interrupt: c00 at 0x5a1274
NIP:  005a1274 LR: 006a3b3c CTR: 005296c8
REGS: c2437f40 TRAP: 0c00   Tainted: G        W           (6.13.0-rc6-PMacG4)
MSR:  0200f932 <VEC,EE,PR,FP,ME,IR,DR,RI>  CR: 24004422  XER: 00000000

GPR00: 00000166 af8f3fa0 a7ee3540 00000001 00000000 013b6500 005a5858 0200f932
GPR08: 00000000 00001fe9 013d5fc8 005296c8 2822244c 00b2fcd8 00000000 af8f4b57
GPR16: 00000000 00000001 00000000 00000000 00000000 00000001 00000000 00000002
GPR24: 00afdbb0 00000000 00000000 00000000 006e0004 013ce060 006e7c1c 00000001 NIP [005a1274] 0x5a1274
LR [006a3b3c] 0x6a3b3c
--- interrupt: c00

The buggy address belongs to the virtual mapping at
 [f1000000, f1002000) created by:
 text_area_cpu_up+0x20/0x190

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:00000000 index:0x0 pfn:0x76e30
flags: 0x80000000(zone=2)
raw: 80000000 00000000 00000122 00000000 00000000 00000000 ffffffff 00000001
raw: 00000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 f0ffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 f0ffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Quoted 1 line

f1000000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8

           ^

f1000080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
f1000100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
==================================================================
Disabling lock debugging due to kernel taint

Kernel .config and full dmesg attached. The other 2 warnings in the dmesg, "workqueue: work disable count underflowed" are https://lore.kernel.org/all/CA+G9fYsQmFT3m1J0uGfWGJjci8MQoB7BNcmhBR175PAbxx9fBg@mail.gmail.com/ (offending commit should be reverted in next -rc) and "Missing '#size-cells'" is fixed by https://lore.kernel.org/all/[email protected]/ (not yet merged)

Regards,
Erhard

#2 lore raw
Quoted 9 lines · On Sun, 19 Jan 2025 22:06:42 +0530 Madhavan Srinivasan <[email protected]>…
Quoted 3 lines · Erhard Furtner

Greetings!

I am getting this at bootup on my PowerMac G4 with a KASAN-enabled kernel 6.13-rc6:

Sorry for the delayed response,

Are you seeing this only in this kernel or this is the recent kernel you tried to boot?

I think I didn't run a KASAN enabled 6.11 or 6.12 kernel but the reported KASAN hit is still in currently released 6.13:

[...]
==================================================================

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8
Write of size 8 at addr f1014000 by task chronyd/1301

CPU: 1 UID: 123 PID: 1301 Comm: chronyd Tainted: G        W          6.13.0-PMacG4 #3
Tainted: [W]=WARN
Hardware name: PowerMac3,6 7455 0x80010303 PowerMac
Call Trace:
[c7bdf690] [c16327c8] dump_stack_lvl+0x70/0x8c (unreliable)
[c7bdf6b0] [c0504b78] print_report+0xdc/0x504
[c7bdf710] [c050493c] kasan_report+0xf8/0x108
[c7bdf790] [c0505c1c] kasan_check_range+0x24/0x18c
[c7bdf7a0] [c03fb668] copy_to_kernel_nofault+0xd8/0x1c8
[c7bdf7c0] [c004c014] patch_instructions+0x15c/0x16c
[c7bdf810] [c00731a8] bpf_arch_text_copy+0x60/0x7c
[c7bdf830] [c02811f4] bpf_jit_binary_pack_finalize+0x50/0xac
[c7bdf850] [c0073cf4] bpf_int_jit_compile+0xb30/0xdec
[c7bdf980] [c0280420] bpf_prog_select_runtime+0x15c/0x478
[c7bdf9d0] [c1264100] bpf_prepare_filter+0xbf8/0xc14
[c7bdfa90] [c12684c4] bpf_prog_create_from_user+0x258/0x2b4
[c7bdfad0] [c02711a8] do_seccomp+0x3dc/0x1890
[c7bdfbc0] [c001d8e0] system_call_exception+0x2dc/0x420
[c7bdff30] [c00281ac] ret_from_syscall+0x0/0x2c
--- interrupt: c00 at 0x591274
NIP:  00591274 LR: 00693b3c CTR: 005196c8
REGS: c7bdff40 TRAP: 0c00   Tainted: G        W           (6.13.0-PMacG4)
MSR:  0200f932 <VEC,EE,PR,FP,ME,IR,DR,RI>  CR: 24004422  XER: 00000000

GPR00: 00000166 aff4aad0 a77fb540 00000001 00000000 01fe2500 00595858 0200f932
GPR08: 00000000 00001fe9 02001fc8 005196c8 2822244c 00b1fcd8 00000000 aff4bb57
GPR16: 00000000 00000001 00000000 00000000 00000000 00000001 00000000 00000002
GPR24: 00aedbb0 00000000 00000000 00000000 006d0004 01ffa060 006d7c1c 00000001 NIP [00591274] 0x591274
LR [00693b3c] 0x693b3c
--- interrupt: c00

The buggy address belongs to the virtual mapping at
 [f1014000, f1016000) created by:
 text_area_cpu_up+0x20/0x190

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:00000000 index:0x0 pfn:0x77116
flags: 0x80000000(zone=2)
raw: 80000000 00000000 00000122 00000000 00000000 00000000 ffffffff 00000001
raw: 00000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 f1013f00: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
 f1013f80: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
>f1014000: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
           ^
 f1014080: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
 f1014100: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
==================================================================

Disabling lock debugging due to kernel taint

Regards,
Erhard

#3 lore raw
In reply to Madhavan Srinivasan
Quoted 9 lines · On Sun, 19 Jan 2025 22:06:42 +0530 Madhavan Srinivasan <[email protected]>…
Quoted 3 lines · Erhard Furtner

Greetings!

I am getting this at bootup on my PowerMac G4 with a KASAN-enabled kernel 6.13-rc6:

Sorry for the delayed response,

Are you seeing this only in this kernel or this is the recent kernel you tried to boot?

Meanwhile I bisected the bug. Offending commit is:

 # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

futex: fix user access on powerpc

The powerpc user access code is special, and unlike other architectures distinguishes between user access for reading and writing.

And commit 43a43faf5376 ("futex: improve user space accesses") messed that up. It went undetected elsewhere, but caused ppc32 to fail early during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".

Note that the address-masking user access helpers don't even have that read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it.

[ Make sure to also do it for the EFAULT case, as pointed out by
Christophe Leroy ]

 kernel/futex/futex.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

Kernel .config and bisect.log attached.

Regards,
Erhard

git bisect start
# Status: warte auf guten und schlechten Commit
# good: [adc218676eef25575469234709c2d87185ca223a] Linux 6.12
git bisect good adc218676eef25575469234709c2d87185ca223a
# Status: warte auf schlechten Commit, 1 guter Commit bekannt
# bad: [ffd294d346d185b70e28b1a28abe367bbfe53c04] Linux 6.13
git bisect bad ffd294d346d185b70e28b1a28abe367bbfe53c04
# good: [28eb75e178d389d325f1666e422bc13bbbb9804c] Merge tag 'drm-next-2024-11-21' of https://gitlab.freedesktop.org/drm/kernel
git bisect good 28eb75e178d389d325f1666e422bc13bbbb9804c
# good: [e33a6d83e1786d5e310ae746c67f5f4e2f93ba35] Merge tag 'usb-6.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
git bisect good e33a6d83e1786d5e310ae746c67f5f4e2f93ba35
# good: [553c89ec31746ff96fc5562943fe5b1c9b1e9276] Merge tag 'mm-hotfixes-stable-2024-12-07-22-39' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
git bisect good 553c89ec31746ff96fc5562943fe5b1c9b1e9276
# bad: [059dd502b263d8a4e2a84809cf1068d6a3905e6f] Merge tag 'block-6.13-20241228' of git://git.kernel.dk/linux
git bisect bad 059dd502b263d8a4e2a84809cf1068d6a3905e6f
# bad: [dccbe2047a5b0859de24bf463dae9eeea8e01c1e] Merge tag 'edac_urgent_for_v6.13_rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/ras/ras
git bisect bad dccbe2047a5b0859de24bf463dae9eeea8e01c1e
# bad: [de20dc2b9604f5130f62d19905cbfae7453fae80] Merge tag 'sound-6.13-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
git bisect bad de20dc2b9604f5130f62d19905cbfae7453fae80
# good: [ad913dfd8bfacdf1d2232fe9f49ccb025885ef22] Merge tag 'for-net-2024-12-12' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth
git bisect good ad913dfd8bfacdf1d2232fe9f49ccb025885ef22
# bad: [becb337c237f9c8030fb976a1d643c0347408cf8] Merge tag 'linux_kselftest-fixes-6.13-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
git bisect bad becb337c237f9c8030fb976a1d643c0347408cf8
# bad: [aeb68937614f4aeceaaa762bd7f0212ce842b797] x86: Fix build regression with CONFIG_KEXEC_JUMP enabled
git bisect bad aeb68937614f4aeceaaa762bd7f0212ce842b797
# good: [c25ca0c2e42c77e0241411d374d44c41e253b3f5] Merge tag 'timers_urgent_for_v6.13_rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect good c25ca0c2e42c77e0241411d374d44c41e253b3f5
# good: [eadaac4dd2e07292181d35858047bdb177d5194f] Merge tag 'irq_urgent_for_v6.13_rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect good eadaac4dd2e07292181d35858047bdb177d5194f
# good: [0b6809a75ac026d4b569815744ad095b5d5955e1] Merge tag 'kbuild-fixes-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild
git bisect good 0b6809a75ac026d4b569815744ad095b5d5955e1
# bad: [32913f348229c9f72dda45fc2c08c6d9dfcd3d6d] futex: fix user access on powerpc
git bisect bad 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
# good: [fac04efc5c793dccbd07e2d59af9f90b7fc0dca4] Linux 6.13-rc2
git bisect good fac04efc5c793dccbd07e2d59af9f90b7fc0dca4

# first bad commit: [32913f348229c9f72dda45fc2c08c6d9dfcd3d6d] futex: fix user access on powerpc
#4 lore raw

Le 21/01/2025 à 22:00, Erhard Furtner a écrit :

Quoted 44 lines
Quoted 9 lines · On Sun, 19 Jan 2025 22:06:42 +0530 Madhavan Srinivasan <[email protected]>…
Quoted 3 lines · Erhard Furtner

Greetings!

I am getting this at bootup on my PowerMac G4 with a KASAN-enabled kernel 6.13-rc6:

Sorry for the delayed response,

Are you seeing this only in this kernel or this is the recent kernel you tried to boot?

Meanwhile I bisected the bug. Offending commit is:

  # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

futex: fix user access on powerpc

The powerpc user access code is special, and unlike other architectures distinguishes between user access for reading and writing.

And commit 43a43faf5376 ("futex: improve user space accesses") messed that up. It went undetected elsewhere, but caused ppc32 to fail early during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".

Note that the address-masking user access helpers don't even have that read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it.

[ Make sure to also do it for the EFAULT case, as pointed out by
Christophe Leroy ]

Quoted 5 lines
  kernel/futex/futex.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Christophe

#5 lore raw
Quoted 54 lines · On Tue, 21 Jan 2025 23:07:25 +0100 Christophe Leroy…
Quoted 31 lines

Meanwhile I bisected the bug. Offending commit is:

  # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

futex: fix user access on powerpc

The powerpc user access code is special, and unlike other architectures distinguishes between user access for reading and writing.

And commit 43a43faf5376 ("futex: improve user space accesses") messed that up. It went undetected elsewhere, but caused ppc32 to fail early during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".

Note that the address-masking user access helpers don't even have that read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it.

[ Make sure to also do it for the EFAULT case, as pointed out by
Christophe Leroy ]

Quoted 5 lines
  kernel/futex/futex.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Quoted 2 lines

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Attached v6.13 .config plays nicely with qemu ttyS0 (forgot to disable SERIAL_8250 and set SERIAL_PMACZILOG + SERIAL_PMACZILOG_CONSOLE instead as I prefer the PCI Serial card in my G4).

The KASAN hit also shows up on qemu 8.2.7 via via:
qemu-system-ppc -machine mac99,via=pmu -cpu 7450 -m 2G -nographic -append console=ttyS0 -kernel vmlinux-6.13.0-PMacG4 -hda Debian-VM_g4.img

Regards,
Erhard

#6 lore raw

Le 22/01/2025 à 00:21, Erhard Furtner a écrit :

Quoted 57 lines
Quoted 54 lines · On Tue, 21 Jan 2025 23:07:25 +0100 Christophe Leroy…
Quoted 31 lines

Meanwhile I bisected the bug. Offending commit is:

   # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

futex: fix user access on powerpc

The powerpc user access code is special, and unlike other architectures distinguishes between user access for reading and writing.

And commit 43a43faf5376 ("futex: improve user space accesses") messed that up. It went undetected elsewhere, but caused ppc32 to fail early during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".

Note that the address-masking user access helpers don't even have that read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it.

[ Make sure to also do it for the EFAULT case, as pointed out by
Christophe Leroy ]

Quoted 5 lines
   kernel/futex/futex.h | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Quoted 8 lines
Quoted 2 lines

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Attached v6.13 .config plays nicely with qemu ttyS0 (forgot to disable SERIAL_8250 and set SERIAL_PMACZILOG + SERIAL_PMACZILOG_CONSOLE instead as I prefer the PCI Serial card in my G4).

The KASAN hit also shows up on qemu 8.2.7 via via:
qemu-system-ppc -machine mac99,via=pmu -cpu 7450 -m 2G -nographic -append console=ttyS0 -kernel vmlinux-6.13.0-PMacG4 -hda Debian-VM_g4.img

I was able to reproduce it with v6.13 with QEMU when loading test_bpf module.

On my side, the problem doesn't disappear when reverting of commit 32913f348229 ("futex: fix user access on powerpc")

I bisected it to commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault"), which makes a lot more sense to me.

It might be a problem in the way patch_instruction() is implemented on powerpc, to be investigated.

Christophe

#7 lore raw

Le 22/01/2025 à 16:32, Christophe Leroy a écrit :

Quoted 64 lines

Le 22/01/2025 à 00:21, Erhard Furtner a écrit :

Quoted 52 lines
Quoted 49 lines · On Tue, 21 Jan 2025 23:07:25 +0100 Christophe Leroy…
Quoted 26 lines

Meanwhile I bisected the bug. Offending commit is:

   # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

      futex: fix user access on powerpc
      The powerpc user access code is special, and unlike other architectures distinguishes between user access for reading and writing. And commit 43a43faf5376 ("futex: improve user space accesses") messed that up.  It went undetected elsewhere, but caused ppc32 to fail early during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".
      Note that the address-masking user access helpers don't even have that read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it.
      [ Make sure to also do it for the EFAULT case, as pointed out by Christophe Leroy ]

Cc Christophe Leroy <[email protected]>
Link https://eur01.safelinks.protection.outlook.com/? url=https%3A%2F%2Flore.kernel.org%2Fall%2F87bjxl6b0i.fsf%40igel.home%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cb4c1dc7184f54a410a0e08dd3a7270b6%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638730985407902881%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=E5Yp9jopCPE1NFuBM8rs%2B1jXZ%2FXAaKvBGpcEP%2BaMyz0%3D&reserved=0
Quoted 5 lines

   kernel/futex/futex.h | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Quoted 8 lines
Quoted 2 lines

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Attached v6.13 .config plays nicely with qemu ttyS0 (forgot to disable SERIAL_8250 and set SERIAL_PMACZILOG + SERIAL_PMACZILOG_CONSOLE instead as I prefer the PCI Serial card in my G4).

The KASAN hit also shows up on qemu 8.2.7 via via:
qemu-system-ppc -machine mac99,via=pmu -cpu 7450 -m 2G -nographic - append console=ttyS0 -kernel vmlinux-6.13.0-PMacG4 -hda Debian-VM_g4.img

Quoted 7 lines

I was able to reproduce it with v6.13 with QEMU when loading test_bpf module.

On my side, the problem doesn't disappear when reverting of commit 32913f348229 ("futex: fix user access on powerpc")

I bisected it to commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault"), which makes a lot more sense to me.

It might be a problem in the way patch_instruction() is implemented on powerpc, to be investigated.

I think the problem is commit 37bc3e5fd764 ("powerpc/lib/code-patching: Use alternate map for patch_instruction()")

Can you try the change below:

arch/powerpc/lib/code-patching.c +1 -1
index af97fbb3c257..8a378fc19074 100644
@@ -108,7 +108,7 @@ static int text_area_cpu_up(unsigned int cpu)
 	unsigned long addr;
 	int err;
 
-	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
+	area = get_vm_area(PAGE_SIZE, 0);
 	if (!area) {
 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
 			cpu);
#8 lore raw
Quoted 97 lines · On Wed, 22 Jan 2025 19:23:00 +0100 Christophe Leroy…

Le 22/01/2025 à 16:32, Christophe Leroy a écrit :

Quoted 76 lines

Le 22/01/2025 à 00:21, Erhard Furtner a écrit :

Quoted 61 lines
Quoted 55 lines · On Tue, 21 Jan 2025 23:07:25 +0100 Christophe Leroy…
Quoted 29 lines

Meanwhile I bisected the bug. Offending commit is:

   # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

      futex: fix user access on powerpc
      The powerpc user access code is special, and unlike other architectures
      distinguishes between user access for reading and writing. And commit 43a43faf5376 ("futex: improve user space accesses") messed
      that up.  It went undetected elsewhere, but caused ppc32 to fail early
      during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".
      Note that the address-masking user access helpers don't even have that
      read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it. [ Make sure to also do it for the EFAULT case, as pointed out by Christophe Leroy ]

url=https%3A%2F%2Flore.kernel.org%2Fall%2F87bjxl6b0i.fsf%40igel.home%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cb4c1dc7184f54a410a0e08dd3a7270b6%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638730985407902881%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=E5Yp9jopCPE1NFuBM8rs%2B1jXZ%2FXAaKvBGpcEP%2BaMyz0%3D&reserved=0

Quoted 8 lines

   kernel/futex/futex.h | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Quoted 2 lines

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Quoted 8 lines

Attached v6.13 .config plays nicely with qemu ttyS0 (forgot to disable SERIAL_8250 and set SERIAL_PMACZILOG + SERIAL_PMACZILOG_CONSOLE instead as I prefer the PCI Serial card in my G4).

The KASAN hit also shows up on qemu 8.2.7 via via:
qemu-system-ppc -machine mac99,via=pmu -cpu 7450 -m 2G -nographic - append console=ttyS0 -kernel vmlinux-6.13.0-PMacG4 -hda Debian-VM_g4.img

I was able to reproduce it with v6.13 with QEMU when loading test_bpf module.

Quoted 8 lines

On my side, the problem doesn't disappear when reverting of commit 32913f348229 ("futex: fix user access on powerpc")

I bisected it to commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault"), which makes a lot more sense to me.

It might be a problem in the way patch_instruction() is implemented on powerpc, to be investigated.

I think the problem is commit 37bc3e5fd764 ("powerpc/lib/code-patching:
Use alternate map for patch_instruction()")

Can you try the change below:

arch/powerpc/lib/code-patching.c +0 -0
index af97fbb3c257..8a378fc19074 100644

arch/powerpc/lib/code-patching.c static int text_area_cpu_up(unsigned int cpu) · line 114

Quoted 8 lines
arch/powerpc/lib/code-patching.c +1 -1
@@ -108,7 +108,7 @@ static int text_area_cpu_up(unsigned int cpu)
  	unsigned long addr;
  	int err;
 
-	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
+	area = get_vm_area(PAGE_SIZE, 0);
  	if (!area) {
  		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
  			cpu);

Patch applies on v6.13 and fixes the KASAN hit on QEMU and on my PowerMac G4 DP. Thanks Christophe!

Regards,
Erhard

#9 lore raw
In reply to Christophe Leroy
Quoted 98 lines · On Wed, 22 Jan 2025 19:23:00 +0100 Christophe Leroy…

Le 22/01/2025 à 16:32, Christophe Leroy a écrit :

Quoted 76 lines

Le 22/01/2025 à 00:21, Erhard Furtner a écrit :

Quoted 61 lines
Quoted 55 lines · On Tue, 21 Jan 2025 23:07:25 +0100 Christophe Leroy…
Quoted 29 lines

Meanwhile I bisected the bug. Offending commit is:

   # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

      futex: fix user access on powerpc
      The powerpc user access code is special, and unlike other architectures
      distinguishes between user access for reading and writing. And commit 43a43faf5376 ("futex: improve user space accesses") messed
      that up.  It went undetected elsewhere, but caused ppc32 to fail early
      during boot, because the user access had been started with user_read_access_begin(), but then finished off with just a plain "user_access_end()".
      Note that the address-masking user access helpers don't even have that
      read-vs-write distinction, so if powerpc ever wants to do address masking tricks, we'll have to do some extra work for it. [ Make sure to also do it for the EFAULT case, as pointed out by Christophe Leroy ]

url=https%3A%2F%2Flore.kernel.org%2Fall%2F87bjxl6b0i.fsf%40igel.home%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cb4c1dc7184f54a410a0e08dd3a7270b6%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638730985407902881%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=E5Yp9jopCPE1NFuBM8rs%2B1jXZ%2FXAaKvBGpcEP%2BaMyz0%3D&reserved=0

Quoted 8 lines

   kernel/futex/futex.h | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)

Indeed, reverting 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d on top of v6.13 makes the KASAN hit disappear.

That looks terribly odd.

On G4, user_read_access_begin() and user_read_access_end() are no-op because book3s/32 can only protect user access by kernel against write. Read is always granted.

So the bug must be an indirect side effect of what user_access_end() does. user_access_end() does a sync. Would the lack of sync (once replaced user_access_end() by user_read_access_end() ) lead to some odd re-ordering ? Or another possibility is that user_access_end() is called on some kernel address (I see in the description of commit 43a43faf5376 ("futex: improve user space accesses") that the replaced __get_user() was expected to work on kernel adresses) ? Calling user_access_begin() and user_access_end() is unexpected and there is no guard so it could lead to strange segment settings which hides a KASAN hit. But once the fix the issue the KASAN resurfaces ? Could this be the problem ?

Quoted 2 lines

Do you have a way to reproduce the bug on QEMU ? It would enable me to investigate it further.

Quoted 8 lines

Attached v6.13 .config plays nicely with qemu ttyS0 (forgot to disable SERIAL_8250 and set SERIAL_PMACZILOG + SERIAL_PMACZILOG_CONSOLE instead as I prefer the PCI Serial card in my G4).

The KASAN hit also shows up on qemu 8.2.7 via via:
qemu-system-ppc -machine mac99,via=pmu -cpu 7450 -m 2G -nographic - append console=ttyS0 -kernel vmlinux-6.13.0-PMacG4 -hda Debian-VM_g4.img

I was able to reproduce it with v6.13 with QEMU when loading test_bpf module.

Quoted 8 lines

On my side, the problem doesn't disappear when reverting of commit 32913f348229 ("futex: fix user access on powerpc")

I bisected it to commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault"), which makes a lot more sense to me.

It might be a problem in the way patch_instruction() is implemented on powerpc, to be investigated.

I think the problem is commit 37bc3e5fd764 ("powerpc/lib/code-patching:
Use alternate map for patch_instruction()")

Can you try the change below:

arch/powerpc/lib/code-patching.c +0 -0
index af97fbb3c257..8a378fc19074 100644

arch/powerpc/lib/code-patching.c static int text_area_cpu_up(unsigned int cpu) · line 114

Quoted 8 lines
arch/powerpc/lib/code-patching.c +1 -1
@@ -108,7 +108,7 @@ static int text_area_cpu_up(unsigned int cpu)
  	unsigned long addr;
  	int err;
 
-	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
+	area = get_vm_area(PAGE_SIZE, 0);
  	if (!area) {
  		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
  			cpu);

Checked on my Talos II (POWER9) too, to see whether ppc64 is also affected and here I still see the KASAN hit despite Christophes patch applied:

BUG: KASAN: user-memory-access in copy_to_kernel_nofault+0x8c/0x1a0
Write of size 8 at addr 0000187e458f2000 by task systemd/1

CPU: 87 UID: 0 PID: 1 Comm: systemd Tainted: G                T  6.13.0-P9-dirty #3
Tainted: [T]=RANDSTRUCT
Hardware name: T2P9D01 REV 1.01 POWER9 0x4e1202 opal:skiboot-bc106a0 PowerNV
Call Trace:
[c000000008dbf3b0] [c000000001656a48] dump_stack_lvl+0xbc/0x110 (unreliable)
[c000000008dbf3f0] [c0000000006e2fc8] print_report+0x6b0/0x708
[c000000008dbf4e0] [c0000000006e2454] kasan_report+0x164/0x300
[c000000008dbf600] [c0000000006e54d4] kasan_check_range+0x314/0x370
[c000000008dbf640] [c0000000006e6310] __kasan_check_write+0x20/0x40
[c000000008dbf660] [c000000000578e8c] copy_to_kernel_nofault+0x8c/0x1a0
[c000000008dbf6a0] [c0000000000acfe4] __patch_instructions+0x194/0x210
[c000000008dbf6e0] [c0000000000ade80] patch_instructions+0x150/0x590
[c000000008dbf7c0] [c0000000001159bc] bpf_arch_text_copy+0x6c/0xe0
[c000000008dbf800] [c000000000424250] bpf_jit_binary_pack_finalize+0x40/0xc0
[c000000008dbf830] [c000000000115dec] bpf_int_jit_compile+0x3bc/0x930
[c000000008dbf990] [c000000000423720] bpf_prog_select_runtime+0x1f0/0x280
[c000000008dbfa00] [c000000000434b18] bpf_prog_load+0xbb8/0x1370
[c000000008dbfb70] [c000000000436ebc] __sys_bpf+0x5ac/0x2e00
[c000000008dbfd00] [c00000000043a228] sys_bpf+0x28/0x40
[c000000008dbfd20] [c000000000038eb4] system_call_exception+0x334/0x610
[c000000008dbfe50] [c00000000000c270] system_call_vectored_common+0xf0/0x280
--- interrupt: 3000 at 0x3fff82f5cfa8
NIP:  00003fff82f5cfa8 LR: 00003fff82f5cfa8 CTR: 0000000000000000
REGS: c000000008dbfe80 TRAP: 3000   Tainted: G                T   (6.13.0-P9-dirty)
MSR:  900000000280f032 <SF,HV,VEC,VSX,EE,PR,FP,ME,IR,DR,RI>  CR: 42004848  XER: 00000000
IRQMASK: 0 
GPR00: 0000000000000169 00003fffdcf789a0 00003fff83067100 0000000000000005 
GPR04: 00003fffdcf78a98 0000000000000090 0000000000000000 0000000000000008 
GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
GPR12: 0000000000000000 00003fff836ff7e0 c000000000010678 0000000000000000 
GPR16: 0000000000000000 0000000000000000 00003fffdcf78f28 00003fffdcf78f90 
GPR20: 0000000000000000 0000000000000000 0000000000000000 00003fffdcf78f80 
GPR24: 00003fffdcf78f70 00003fffdcf78d10 00003fff835c7239 00003fffdcf78bd8 
GPR28: 00003fffdcf78a98 0000000000000000 0000000000000000 000000011f547580 

NIP [00003fff82f5cfa8] 0x3fff82f5cfa8
LR [00003fff82f5cfa8] 0x3fff82f5cfa8
--- interrupt: 3000

Full dmesg and kernel .config attached.

Regards,
Erhard

#10 lore raw

Le 01/02/2025 à 15:14, Erhard Furtner a écrit :

Quoted 38 lines
Quoted 25 lines · On Wed, 22 Jan 2025 19:23:00 +0100 Christophe Leroy…
Quoted 4 lines

I was able to reproduce it with v6.13 with QEMU when loading test_bpf module.

Quoted 8 lines

On my side, the problem doesn't disappear when reverting of commit 32913f348229 ("futex: fix user access on powerpc")

I bisected it to commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault"), which makes a lot more sense to me.

It might be a problem in the way patch_instruction() is implemented on powerpc, to be investigated.

I think the problem is commit 37bc3e5fd764 ("powerpc/lib/code-patching:
Use alternate map for patch_instruction()")

Can you try the change below:

arch/powerpc/lib/code-patching.c +0 -0
index af97fbb3c257..8a378fc19074 100644

arch/powerpc/lib/code-patching.c static int text_area_cpu_up(unsigned int cpu) · line 114

Quoted 8 lines
arch/powerpc/lib/code-patching.c +1 -1
@@ -108,7 +108,7 @@ static int text_area_cpu_up(unsigned int cpu)
   	unsigned long addr;
   	int err;
 
-	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
+	area = get_vm_area(PAGE_SIZE, 0);
   	if (!area) {
   		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
   			cpu);

Checked on my Talos II (POWER9) too, to see whether ppc64 is also affected and here I still see the KASAN hit despite Christophes patch applied:

Quoted 2 lines
BUG: KASAN: user-memory-access in copy_to_kernel_nofault+0x8c/0x1a0
Write of size 8 at addr 0000187e458f2000 by task systemd/1

Thanks for the report.

That's something different. Previous report was:

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8

This is what my patch fixes.

New report is:

BUG: KASAN: user-memory-access in copy_to_kernel_nofault+0x8c/0x1a0

Christophe

A message that is not in the archive (sent off-list or not yet received): 20250201165416.71e00c43@yea

#12 lore raw

Le 01/02/2025 à 16:54, Erhard Furtner a écrit :

Quoted 13 lines
Quoted 10 lines · On Sat, 1 Feb 2025 16:14:04 +0100 Christophe Leroy…

Thanks for the report.

That's something different. Previous report was:

BUG: KASAN: vmalloc-out-of-bounds in copy_to_kernel_nofault+0xd8/0x1c8

This is what my patch fixes.

New report is:

Quoted 8 lines
Quoted 3 lines
BUG: KASAN: user-memory-access in copy_to_kernel_nofault+0x8c/0x1a0

Christophe

Ah, sorry... I have not been very thoroughly it seems!

The dmesg looked similarly so I thought it was the same issue.

This time the problem is a mixture of commit 465cabc97b42 ("powerpc/code-patching: introduce patch_instructions()") and commit c28c15b6d28a ("powerpc/code-patching: Use temporary mm for Radix MMU") which is revealed by commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault")

Commit c28c15b6d28a is inspired by commit b3fd8e83ada0 ("x86/alternatives: Use temporary mm for text poking") but misses the kasan_disable_current() / kasan_enable_current() sequence.

Was not necessary because __patch_mem() is not instrumented. But commit 465cabc97b42 added use of copy_to_kernel_nofault() which is now instrumented.

The problem is that commit c28c15b6d28a makes use of a special memory area which is not kernel memory and it doesn't have any matching KASAN shadow area. And because it is located below TASK_SIZE, in addition kasan sees it as user memory.

Can you try the change below ?

arch/powerpc/lib/code-patching.c +2 -0
index 8a378fc19074..f84e0337cc02 100644
@@ -493,7 +493,9 @@ static int __do_patch_instructions_mm(u32 *addr, u32 *code, size_t len, bool rep
 
 	orig_mm = start_using_temp_mm(patching_mm);
 
+	kasan_disable_current();
 	err = __patch_instructions(patch_addr, code, len, repeat_instr);
+	kasan_enable_current();
 
 	/* context synchronisation performed by __patch_instructions */
 	stop_using_temp_mm(patching_mm, orig_mm);
#13 lore raw
Quoted 36 lines · On Sun, 2 Feb 2025 09:44:20 +0100 Christophe Leroy…

This time the problem is a mixture of commit 465cabc97b42 ("powerpc/code-patching: introduce patch_instructions()") and commit c28c15b6d28a ("powerpc/code-patching: Use temporary mm for Radix MMU") which is revealed by commit e4137f08816b ("mm, kasan, kmsan: instrument copy_from/to_kernel_nofault")

Commit c28c15b6d28a is inspired by commit b3fd8e83ada0 ("x86/alternatives: Use temporary mm for text poking") but misses the kasan_disable_current() / kasan_enable_current() sequence.

Was not necessary because __patch_mem() is not instrumented. But commit 465cabc97b42 added use of copy_to_kernel_nofault() which is now instrumented.

The problem is that commit c28c15b6d28a makes use of a special memory area which is not kernel memory and it doesn't have any matching KASAN shadow area. And because it is located below TASK_SIZE, in addition kasan sees it as user memory.

Can you try the change below ?

arch/powerpc/lib/code-patching.c +2 -0
index 8a378fc19074..f84e0337cc02 100644
@@ -493,7 +493,9 @@ static int __do_patch_instructions_mm(u32 *addr, u32 
 *code, size_t len, bool rep
 
  	orig_mm = start_using_temp_mm(patching_mm);
 
+	kasan_disable_current();
  	err = __patch_instructions(patch_addr, code, len, repeat_instr);
+	kasan_enable_current();
 
  	/* context synchronisation performed by __patch_instructions */
Quoted 2 lines
  	stop_using_temp_mm(patching_mm, orig_mm);

Thanks! With this patch applied the KASAN hit is gone and I got no further KASAN hits on my Talos II during boot. Applied both patches on top of v6.13.1.

Regards,
Erhard

#14 lore raw
In reply to Erhard Furtner
Quoted 11 lines · Erhard Furtner

Meanwhile I bisected the bug. Offending commit is:

 # git bisect good
32913f348229c9f72dda45fc2c08c6d9dfcd3d6d is the first bad commit
commit 32913f348229c9f72dda45fc2c08c6d9dfcd3d6d
Author: Linus Torvalds <[email protected]>
Date:   Mon Dec 9 10:00:25 2024 -0800

futex: fix user access on powerpc

Heh. As Christophe said - very funky. All that commit does is fix the user_access_end() to be a user_read_access_end() so that it pairs correctly with the user_read_access_begin().

On other architectures is a complete no-op, but even on powerpc that literally just changes a

        prevent_user_access(KUAP_READ_WRITE);

into a

        prevent_user_access(KUAP_READ);

unless I'm missing something. So it's really not a big change even on powerpc.

If I'm parsing your config file correctly, this is the 32-bit "book3" case, so we're talking about the prevent_user_access() in

arch/powerpc/include/asm/book3s/32/kup.h

and those things end up being no-ops for non-write cases:

        if (!(dir & KUAP_WRITE))
                return;

but it *used* to do - before that commit - this:

        current->thread.kuap = KUAP_NONE;
        uaccess_end_32s(kuap);

but this all pairs with the user_read_access_begin(), which does

        allow_read_from_user(ptr, len);

which in turn is

        allow_user_access(NULL, from, size, KUAP_READ);

which again is a no-op in that book3s/32/kup.h case because KUAP_WRITE isn't set and it returns early.

As Christophe says, there's a few 'sync()' calls sprinkled around. So maybe the inlining and some odd compiler issue is causing problems?

Or maybe the old (incorrect) uaccess_end_32s() ended up hiding some other bug.

Linus