mm: Add optional close() to struct vm_special_mapping
Subsystems (17)
Michael Ellerman
reviewerPATCH v2 1/4 mm: Add optional close() to struct vm_special_mapping
Add an optional close() callback to struct vm_special_mapping. It will be used, by powerpc at least, to handle unmapping of the VDSO.
Although support for unmapping the VDSO was initially added for CRIU[1], it is not desirable to guard that support behind CONFIG_CHECKPOINT_RESTORE.
There are other known users of unmapping the VDSO which are not related to CRIU, eg. Valgrind [2] and void-ship [3].
The powerpc arch_unmap() hook has been in place for ~9 years, with no ifdef, so there may be other unknown users that have come to rely on unmapping the VDSO. Even if the code was behind an ifdef, major distros enable CHECKPOINT_RESTORE so users may not realise unmapping the VDSO depends on that configuration option.
It's also undesirable to have such core mm behaviour behind a relatively obscure CONFIG option.
Longer term the unmap behaviour should be standardised across architectures, however that is complicated by the fact the VDSO pointer is stored differently across architectures. There was a previous attempt to unify that handling [4], which could be revived.
See [5] for further discussion.
[1]: commit 83d3f0e90c6c ("powerpc/mm: tracking vDSO remap")
[2]: https://sourceware.org/git/?p=valgrind.git;a=commit;h=3a004915a2cbdcdebafc1612427576bf3321eef5
[3]: https://github.com/insanitybit/void-ship
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/linuxppc-dev/shiq5v3jrmyi6ncwke7wgl76ojysgbhrchsk32q4lbx2hadqqc@kzyy2igem256
--- include/linux/mm_types.h | 3 +++ mm/mmap.c | 6 ++++++ 2 files changed, 9 insertions(+)
v2:
- Add some blank lines as requested.
- Expand special_mapping_close() comment.
- Add David's reviewed-by.
- Expand change log to capture review discussion.
include/linux/mm_types.h +3 -0
@@ -1313,6 +1313,9 @@ struct vm_special_mapping {int (*mremap)(const struct vm_special_mapping *sm,struct vm_area_struct *new_vma); + +void (*close)(const struct vm_special_mapping *sm, +struct vm_area_struct *vma); };enum tlb_flush_reason {
mm/mmap.c +6 -0
@@ -3620,10 +3620,16 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)static vm_fault_t special_mapping_fault (struct vm_fault *vmf);/* +* Close hook, called for unmap() and on the old vma for mremap(). +* * Having a close hook prevents vma merging regardless of flags. */ static void special_mapping_close (struct vm_area_struct *vma) { +const struct vm_special_mapping *sm = vma->vm_private_data; + +if (sm->close) + sm->close (sm, vma); }static const char *special_mapping_name (struct vm_area_struct *vma)
Signature · 1 line
2.45.2
Michael Ellerman
reviewerPATCH v2 2/4 powerpc/mm: Handle VDSO unmapping via close() rather than arch_unmap()
Add a close() callback to the VDSO special mapping to handle unmapping of the VDSO. That will make it possible to remove the arch_unmap() hook entirely in a subsequent patch.
--- arch/powerpc/include/asm/mmu_context.h | 4 ---- arch/powerpc/kernel/vdso.c | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-)
v2: Unchanged except for collecting tags.
arch/powerpc/include/asm/mmu_context.h +0 -4
@@ -263,10 +263,6 @@ extern void arch_exit_mmap(struct mm_struct *mm);static inline void arch_unmap (struct mm_struct *mm,unsigned long start,unsigned long end) { -unsigned long vdso_base = (unsigned long )mm->context.vdso; - -if (start <= vdso_base && vdso_base < end) - mm->context.vdso =NULL ; }#ifdef CONFIG_PPC_MEM_KEYS
arch/powerpc/kernel/vdso.c +17 -0
@@ -81,6 +81,21 @@ static int vdso64_mremap(const struct vm_special_mapping *sm, struct vm_area_strreturn vdso_mremap (sm, new_vma, &vdso64_end - &vdso64_start); } +static void vdso_close (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ +struct mm_struct *mm = vma->vm_mm; + +/* +* close() is called for munmap() but also for mremap(). In the mremap() +* case the vdso pointer has already been updated by the mremap() hook +* above, so it must not be set to NULL here. +*/ +if (vma->vm_start != (unsigned long )mm->context.vdso) +return ; + + mm->context.vdso =NULL ; +} +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm,struct vm_area_struct *vma,struct vm_fault *vmf);
@@ -92,11 +107,13 @@ static struct vm_special_mapping vvar_spec __ro_after_init = {static struct vm_special_mapping vdso32_spec__ro_after_init = { .name ="[vdso]" , .mremap = vdso32_mremap, + .close = vdso_close, };static struct vm_special_mapping vdso64_spec__ro_after_init = { .name ="[vdso]" , .mremap = vdso64_mremap, + .close = vdso_close, };#ifdef CONFIG_TIME_NS
Signature · 1 line
2.45.2
Michael Ellerman
reviewerPATCH v2 3/4 mm: Remove arch_unmap()
In reply to Michael EllermanNow that powerpc no longer uses arch_unmap() to handle VDSO unmapping, there are no meaningful implementions left. Drop support for it entirely, and update comments which refer to it.
--- arch/powerpc/include/asm/mmu_context.h | 5 ----- arch/x86/include/asm/mmu_context.h | 5 ----- include/asm-generic/mm_hooks.h | 11 +++-------- mm/mmap.c | 12 +++--------- 4 files changed, 6 insertions(+), 27 deletions(-)
v2: Unchanged except for collecting tags.
arch/powerpc/include/asm/mmu_context.h +0 -5
@@ -260,11 +260,6 @@ static inline void enter_lazy_tlb(struct mm_struct *mm,extern void arch_exit_mmap (struct mm_struct *mm); -static inline void arch_unmap (struct mm_struct *mm, -unsigned long start,unsigned long end) -{ -} -#ifdef CONFIG_PPC_MEM_KEYS bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign);
arch/x86/include/asm/mmu_context.h +0 -5
@@ -232,11 +232,6 @@ static inline bool is_64bit_mm(struct mm_struct *mm) }#endif -static inline void arch_unmap (struct mm_struct *mm,unsigned long start, -unsigned long end) -{ -} -/* * We only want to enforce protection keys on the current process * because we effectively have no access to PKRU for other
include/asm-generic/mm_hooks.h +3 -8
@@ -1,8 +1,8 @@/* SPDX-License-Identifier: GPL-2.0 */ /* -* Define generic no-op hooks for arch_dup_mmap, arch_exit_mmap -* and arch_unmap to be included in asm-FOO/mmu_context.h for any -* arch FOO which doesn't need to hook these. +* Define generic no-op hooks for arch_dup_mmap and arch_exit_mmap +* to be included in asm-FOO/mmu_context.h for any arch FOO which +* doesn't need to hook these. */ #ifndef _ASM_GENERIC_MM_HOOKS_H#define _ASM_GENERIC_MM_HOOKS_H
@@ -17,11 +17,6 @@ static inline void arch_exit_mmap(struct mm_struct *mm) { } -static inline void arch_unmap (struct mm_struct *mm, -unsigned long start,unsigned long end) -{ -} -static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {
mm/mmap.c +3 -9
@@ -2789,7 +2789,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,* * This function takes a @mas that is either pointing to the previous VMA or set * to MA_START and sets it up to remove the mapping(s). The @len will be -* aligned and any arch_unmap work will be preformed. +* aligned. * * Return: 0 on success and drops the lock if so directed, error and leaves the * lock held otherwise.
@@ -2809,16 +2809,12 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,return -EINVAL ;/* -* Check if memory is sealed before arch_unmap. -* Prevent unmapping a sealed VMA. +* Check if memory is sealed, prevent unmapping a sealed VMA. * can_modify_mm assumes we have acquired the lock on MM. */ if (unlikely (!can_modify_mm (mm, start, end)))return -EPERM ; -/* arch_unmap() might do unmaps itself. */ -arch_unmap (mm, start, end); -/* Find the first overlapping VMA */ vma =vma_find (vmi, end);if (!vma) {
@@ -3232,14 +3228,12 @@ int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;/* -* Check if memory is sealed before arch_unmap. -* Prevent unmapping a sealed VMA. +* Check if memory is sealed, prevent unmapping a sealed VMA. * can_modify_mm assumes we have acquired the lock on MM. */ if (unlikely (!can_modify_mm (mm, start, end)))return -EPERM ; -arch_unmap (mm, start, end);return do_vmi_align_munmap (vmi, vma, mm, start, end, uf, unlock); }
Signature · 1 line
2.45.2
Michael Ellerman
reviewerPATCH v2 4/4 powerpc/vdso: Refactor error handling
In reply to Michael EllermanLinus noticed that the error handling in __arch_setup_additional_pages() fails to clear the mm VDSO pointer if _install_special_mapping() fails. In practice there should be no actual bug, because if there's an error the VDSO pointer is cleared later in arch_setup_additional_pages().
However it's no longer necessary to set the pointer before installing the mapping. Commit c1bab64360e6 ("powerpc/vdso: Move to _install_special_mapping() and remove arch_vma_name()") reworked the code so that the VMA name comes from the vm_special_mapping.name, rather than relying on arch_vma_name().
So rework the code to only set the VDSO pointer once the mappings have been installed correctly, and remove the stale comment.
Depends-on: c1bab64360e6 ("powerpc/vdso: Move to _install_special_mapping() and remove arch_vma_name()")
--- arch/powerpc/kernel/vdso.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
v2: Unchanged.
arch/powerpc/kernel/vdso.c +7 -11
@@ -214,13 +214,6 @@ static int __arch_setup_additional_pages(struct linux_binprm *bprm, int uses_int/* Add required alignment. */ vdso_base =ALIGN (vdso_base,VDSO_ALIGNMENT ); -/* -* Put vDSO base into mm struct. We need to do this before calling -* install_special_mapping or the perf counter mmap tracking code -* will fail to recognise it as a vDSO. -*/ - mm->context.vdso = (void __user *)vdso_base + vvar_size; - vma =_install_special_mapping (mm, vdso_base, vvar_size,VM_READ |VM_MAYREAD |VM_IO |VM_DONTDUMP |VM_PFNMAP , &vvar_spec);
@@ -240,10 +233,15 @@ static int __arch_setup_additional_pages(struct linux_binprm *bprm, int uses_int vma =_install_special_mapping (mm, vdso_base + vvar_size, vdso_size,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , vdso_spec); -if (IS_ERR (vma)) +if (IS_ERR (vma)) {do_munmap (mm, vdso_base, vvar_size,NULL ); +return PTR_ERR (vma); + } -return PTR_ERR_OR_ZERO (vma); +// Now that the mappings are in place, set the mm VDSO pointer + mm->context.vdso = (void __user *)vdso_base + vvar_size; + +return 0 ; }int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp)
@@ -257,8 +255,6 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)return -EINTR ; rc =__arch_setup_additional_pages (bprm, uses_interp); -if (rc) - mm->context.vdso =NULL ;mmap_write_unlock (mm);return rc;
Signature · 1 line
2.45.2
Quoted 19 lines · Michael Ellerman
Add an optional
close()callback to structvm_special_mapping. It will be used, by powerpc at least, to handle unmapping of the VDSO.Although support for unmapping the VDSO was initially added for CRIU[1], it is not desirable to guard that support behind
CONFIG_CHECKPOINT_RESTORE.There are other known users of unmapping the VDSO which are not related to CRIU, eg. Valgrind [2] and void-ship [3].
The powerpc
arch_unmap()hook has been in place for ~9 years, with no ifdef, so there may be other unknown users that have come to rely on unmapping the VDSO. Even if the code was behind an ifdef, major distros enableCHECKPOINT_RESTOREso users may not realise unmapping the VDSO depends on that configuration option.It's also undesirable to have such core mm behaviour behind a relatively obscure CONFIG option.
Quoted 6 lines
Longer term the unmap behaviour should be standardised across architectures, however that is complicated by the fact the VDSO pointer is stored differently across architectures. There was a previous attempt to unify that handling [4], which could be revived.
See [5] for further discussion.
Thank you for tackling this issue, it's much improved.
For the whole series:
Quoted 59 lines
[1]: commit
83d3f0e90c6c("powerpc/mm: tracking vDSO remap")
[2]: https://sourceware.org/git/?p=valgrind.git;a=commit;h=3a004915a2cbdcdebafc1612427576bf3321eef5
[3]: https://github.com/insanitybit/void-ship
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/linuxppc-dev/shiq5v3jrmyi6ncwke7wgl76ojysgbhrchsk32q4lbx2hadqqc@kzyy2igem256Suggested-by Linus Torvalds <[email protected]>Signed-off-by Michael Ellerman <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- include/linux/mm_types.h | 3 +++ mm/mmap.c | 6 ++++++ 2 files changed, 9 insertions(+)v2:
- Add some blank lines as requested.
- Expandspecial_mapping_close()comment.
- Add David's reviewed-by.
- Expand change log to capture review discussion.include/linux/mm_types.h +3
-0@@ -1313,6 +1313,9 @@ struct vm_special_mapping {int (*mremap)(const struct vm_special_mapping *sm,struct vm_area_struct *new_vma); + +void (*close)(const struct vm_special_mapping *sm, +struct vm_area_struct *vma); };enum tlb_flush_reason {mm/mmap.c +6
-0@@ -3620,10 +3620,16 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)static vm_fault_t special_mapping_fault (struct vm_fault *vmf);/* +* Close hook, called for unmap() and on the old vma for mremap(). +* * Having a close hook prevents vma merging regardless of flags. */ static void special_mapping_close (struct vm_area_struct *vma) { +const struct vm_special_mapping *sm = vma->vm_private_data; + +if (sm->close) + sm->close (sm, vma); }static const char *special_mapping_name (struct vm_area_struct *vma)Signature · 1 line
2.45.2
Hi Michael,
Quoted 84 lines · Michael Ellerman
Add an optional
close()callback to structvm_special_mapping. It will be used, by powerpc at least, to handle unmapping of the VDSO.Although support for unmapping the VDSO was initially added for CRIU[1], it is not desirable to guard that support behind
CONFIG_CHECKPOINT_RESTORE.There are other known users of unmapping the VDSO which are not related to CRIU, eg. Valgrind [2] and void-ship [3].
The powerpc
arch_unmap()hook has been in place for ~9 years, with no ifdef, so there may be other unknown users that have come to rely on unmapping the VDSO. Even if the code was behind an ifdef, major distros enableCHECKPOINT_RESTOREso users may not realise unmapping the VDSO depends on that configuration option.It's also undesirable to have such core mm behaviour behind a relatively obscure CONFIG option.
Longer term the unmap behaviour should be standardised across architectures, however that is complicated by the fact the VDSO pointer is stored differently across architectures. There was a previous attempt to unify that handling [4], which could be revived.
See [5] for further discussion.
[1]: commit
83d3f0e90c6c("powerpc/mm: tracking vDSO remap")
[2]: https://sourceware.org/git/?p=valgrind.git;a=commit;h=3a004915a2cbdcdebafc1612427576bf3321eef5
[3]: https://github.com/insanitybit/void-ship
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/linuxppc-dev/shiq5v3jrmyi6ncwke7wgl76ojysgbhrchsk32q4lbx2hadqqc@kzyy2igem256Suggested-by Linus Torvalds <[email protected]>Signed-off-by Michael Ellerman <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- include/linux/mm_types.h | 3 +++ mm/mmap.c | 6 ++++++ 2 files changed, 9 insertions(+)v2:
- Add some blank lines as requested.
- Expandspecial_mapping_close()comment.
- Add David's reviewed-by.
- Expand change log to capture review discussion.include/linux/mm_types.h +3
-0@@ -1313,6 +1313,9 @@ struct vm_special_mapping {int (*mremap)(const struct vm_special_mapping *sm,struct vm_area_struct *new_vma); + +void (*close)(const struct vm_special_mapping *sm, +struct vm_area_struct *vma); };enum tlb_flush_reason {mm/mmap.c +6
-0@@ -3620,10 +3620,16 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)static vm_fault_t special_mapping_fault (struct vm_fault *vmf);/* +* Close hook, called for unmap() and on the old vma for mremap(). +* * Having a close hook prevents vma merging regardless of flags. */ static void special_mapping_close (struct vm_area_struct *vma) { +const struct vm_special_mapping *sm = vma->vm_private_data; + +if (sm->close) + sm->close (sm, vma); }static const char *special_mapping_name (struct vm_area_struct *vma)Signature · 1 line
2.45.2
This change is now in -next and I bisected a crash that our CI sees with ARCH=um to it:
$ make -skj"$(nproc)" ARCH=um CROSS_COMPILE=x86_64-linux- defconfig linux
$ ./linux ubd0=$PWD/rootfs.ext4 ... Linux version 6.11.0-rc4-next-20240819 (nathan@thelio-3990X) (x86_64-linux-gcc (GCC) 14.2.0, GNU ld (GNU Binutils) 2.42) #1 Mon Aug 19 11:42:20 MST 2024 ... Run /sbin/init as init process
Modules linked in: Pid: 24, comm: mount Not tainted 6.11.0-rc4-next-20240819 RIP: 0033:0x68006f6c RSP: 000000006c8bfc68 EFLAGS: 00010206 RAX: 0000000068006f6c RBX: 0000000068a0aa18 RCX: 00000000600d8b09 RDX: 0000000000000000 RSI: 0000000068a0aa18 RDI: 0000000068805120 RBP: 000000006c8bfc70 R08: 0000000000000001 R09: 0000000068ae0308 R10: 000000000000000e R11: ffffffffffffffff R12: 0000000000000001 R13: 0000000068a0aa18 R14: 0000000000000015 R15: 0000000068944a88 Kernel panic - not syncing: Segfault with no mm CPU: 0 UID: 0 PID: 24 Comm: mount Not tainted 6.11.0-rc4-next-20240819 #1 Stack: 600caeff 6c8bfc90 600d8b2a 68944a80 00000047 6c8bfda0 600cbfd9 6c8bfd50 68944ad0 68944a88 7f7ffff000 7f7fffffff Call Trace: [<600caeff>] ? special_mapping_close+0x16/0x19 [<600d8b2a>] remove_vma+0x21/0x59 [<600cbfd9>] exit_mmap+0x1f3/0x2bc [<60032a0c>] ? unblock_signals+0x0/0xbd [<600329fd>] ? block_signals+0x0/0xf [<6003831c>] __mmput+0x24/0x94 [<60067262>] ? up_read+0x0/0x2c [<600383a1>] mmput+0x15/0x18 [<6003ce97>] do_exit+0x381/0x9b8 [<600e4b8d>] ? kfree+0x107/0x11b [<6003d752>] sys_exit_group+0x0/0x16 [<6003d768>] pid_child_should_wake+0x0/0x42 [<60022e7a>] handle_syscall+0x79/0xa7 [<600358de>] userspace+0x4d3/0x505 [<60020927>] fork_handler+0x84/0x8b
Passing this through scripts/decode_stacktrace.sh results in
? special_mapping_close (mm/mmap.c:2056)remove_vma (mm/vma.c:144)exit_mmap (include/linux/sched.h:2049 mm/mmap.c:1947)
? unblock_signals (arch/um/os-Linux/signal.c:296)
? block_signals (arch/um/os-Linux/signal.c:282)__mmput (kernel/fork.c:1349)
? up_read (arch/x86/include/asm/atomic64_64.h:79 (discriminator 5) include/linux/atomic/atomic-arch-fallback.h:2749 (discriminator 5) include/linux/atomic/atomic-long.h:184 (discriminator 5) include/linux/atomic/atomic-instrumented.h:3317 (discriminator 5) kernel/locking/rwsem.c:1347 (discriminator 5) kernel/locking/rwsem.c:1622 (discriminator 5)) mmput (kernel/fork.c:1370)
do_exit (arch/um/include/asm/thread_info.h:46 kernel/exit.c:572 kernel/exit.c:926) ? kfree (mm/slub.c:4482 (discriminator 2) mm/slub.c:4522 (discriminator 2) mm/slub.c:4669 (discriminator 2)) sys_exit_group (kernel/exit.c:1099 kernel/exit.c:1097) pid_child_should_wake (kernel/exit.c:1106 kernel/exit.c:1565) handle_syscall (arch/um/kernel/skas/syscall.c:45 (discriminator 1))
userspace (arch/um/os-Linux/skas/process.c:466)fork_handler (arch/um/kernel/process.c:137)
This change seems pretty innocuous but the bisect log does not lie :) I am guessing UML is just special here somehow?
# bad: [367b5c3d53e57d51a5878816804652963da90950] Add linux-next specific files for 20240816
# good: [e724918b3786252b985b0c2764c16a57d1937707] Merge tag 'hardening-v6.11-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
git bisect start '367b5c3d53e57d51a5878816804652963da90950' 'e724918b3786252b985b0c2764c16a57d1937707'
# bad: [b12bdbe2615f5426953ae1e64d74176674618edb] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git
git bisect bad b12bdbe2615f5426953ae1e64d74176674618edb
# bad: [9ad9c8d6eea9063fe7309cdc8e76bd12377cd613] Merge branch 'for-next' of https://github.com/sophgo/linux.git
git bisect bad 9ad9c8d6eea9063fe7309cdc8e76bd12377cd613
# bad: [57c53c832b28ca79eddca47c5b599036be10d347] Merge branch 'perf-tools-next' of git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git
git bisect bad 57c53c832b28ca79eddca47c5b599036be10d347
# bad: [cbaf19e941bcd83cf50f569b3888f7db6dcaccfc] foo
git bisect bad cbaf19e941bcd83cf50f569b3888f7db6dcaccfc
# good: [cdb0e8eb648858f37bbe1d6245c3a3c49f265c1c] fixup! selftests/mm: Add mseal test for no-discard madvise
git bisect good cdb0e8eb648858f37bbe1d6245c3a3c49f265c1c
# bad: [4fdacc9ec44f04a9edc4ddd0c782ab698cd15257] mm: shmem: support large folio allocation for shmem_replace_folio()
git bisect bad 4fdacc9ec44f04a9edc4ddd0c782ab698cd15257
# good: [90f91965eee8256ffad811a6da097bc13b66aa2e] mm: reduce deferred struct page init ifdeffery
git bisect good 90f91965eee8256ffad811a6da097bc13b66aa2e
# good: [5ae759160c5df466f4ae7cb89c05cd963e91cc3c] mm: introduce a pageflag for partially mapped folios
git bisect good 5ae759160c5df466f4ae7cb89c05cd963e91cc3c
# good: [03683572685d2f8febfc022b758fdb4bddf8d783] maple_tree: fix comment typo with corresponding maple_status
git bisect good 03683572685d2f8febfc022b758fdb4bddf8d783
# bad: [74ef5018120b2a441428400a5f92891307d41b82] powerpc/vdso: refactor error handling
git bisect bad 74ef5018120b2a441428400a5f92891307d41b82
# bad: [5077f828c08424b81279341813a18b8923ebd42e] mm: add optional close() to struct vm_special_mapping
git bisect bad 5077f828c08424b81279341813a18b8923ebd42e
# good: [0ebac8817b5dce7b3a1afd6ff7197a75829d50ad] kfence: save freeing stack trace at calling time instead of freeing time
git bisect good 0ebac8817b5dce7b3a1afd6ff7197a75829d50ad
# first bad commit: [5077f828c08424b81279341813a18b8923ebd42e] mm: add optional close() to struct vm_special_mapping
The rootfs is available from [1] in case it matters (x86_64-rootfs.ext4.zst, decompress it with zstd first); it just shuts down the machine on boot.
Cheers,
Nathan
[1]: https://github.com/ClangBuiltLinux/boot-utils/releases/latest
Quoted 11 lines · Nathan Chancellor
Modules linked in: Pid: 24, comm: mount Not tainted 6.11.0-rc4-next-20240819 RIP: 0033:0x68006f6c RSP: 000000006c8bfc68 EFLAGS: 00010206 RAX: 0000000068006f6c RBX: 0000000068a0aa18 RCX: 00000000600d8b09 RDX: 0000000000000000 RSI: 0000000068a0aa18 RDI: 0000000068805120 RBP: 000000006c8bfc70 R08: 0000000000000001 R09: 0000000068ae0308 R10: 000000000000000e R11: ffffffffffffffff R12: 0000000000000001 R13: 0000000068a0aa18 R14: 0000000000000015 R15: 0000000068944a88
Quoted 8 lines
Kernel panic - not syncing: Segfault with no mm CPU: 0 UID: 0 PID: 24 Comm: mount Not tainted 6.11.0-rc4-next-20240819 #1 Stack: 600caeff 6c8bfc90 600d8b2a 68944a80 00000047 6c8bfda0 600cbfd9 6c8bfd50 68944ad0 68944a88 7f7ffff000 7f7fffffff Call Trace: [<600caeff>] ? special_mapping_close+0x16/0x19
Hmm. No "Code:" line? Did you just edit that out, or maybe UML doesn't print one out?
Anyway, for me that special_mapping_close() disassembles to
<+0>: mov %rdi,%rsi <+3>: mov 0x78(%rdi),%rdi <+7>: mov 0x20(%rdi),%rax <+11>: test %rax,%rax <+14>: je 0x600caa11 <special_mapping_close+24> <+16>: push %rbp <+17>: mov %rsp,%rbp <+20>: call *%rax <+22>: pop %rbp
<+23>: ret
<+24>: ret
which may just match yours, because special_mapping_close+0x16 is obviously that +22, and it's the return point for that call.
And your %rax value does match that invalid %rip value of 0x68006f6c.
So it does look like it's jumping off to la-la-land, and the problem is the code
const struct vm_special_mapping *sm = vma->vm_private_data;
if (sm->close)
sm->close(sm, vma);where presumably 'vm_private_data' isn't a "struct vm_special_mapping *" at all.
And I think I see the problem.
When we have that 'legacy_special_mapping_vmops', then the vm_private_data field actually points to 'pages'.
So the 'legacy_special_mapping_vmops' can *only* contain the '.fault' handler, not the other handlers.
IOW, does something like this fix it?
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2095,7 +2095,6 @@ static const struct vm_operations_structspecial_mapping_vmops = {
};
static const struct vm_operations_struct legacy_special_mapping_vmops = {
- .close = special_mapping_close,
.fault = special_mapping_fault,
};and honestly, we should have different 'fault' functions instead of having the same fault function and then making the function dynamically see which vm_operations_struct it was. But that's a separate issue.
And oh Christ, the difference between "_install_special_mapping()" (which installs the modern style special mapping) and "install_special_mapping()" (which installs the legacy special mapping) is truly horrendously disgusting.
And yes, UML uses that legacy crap, which explains why it happens on UML but not on sane architectures.
As does csky, hexagon, and nios2.
We should get rid of the legacy case entirely, and remove that insane difference between _install_special_mapping() and install_special_mapping().
But in the meantime, the one-liner fix *should* fix it. The four architectures that uses the legacy crud don't care about the close function anyway.
What a horrid thing.
Linus
Quoted 21 lines · Linus Torvalds
Quoted 11 lines · Nathan Chancellor
Modules linked in: Pid: 24, comm: mount Not tainted 6.11.0-rc4-next-20240819 RIP: 0033:0x68006f6c RSP: 000000006c8bfc68 EFLAGS: 00010206 RAX: 0000000068006f6c RBX: 0000000068a0aa18 RCX: 00000000600d8b09 RDX: 0000000000000000 RSI: 0000000068a0aa18 RDI: 0000000068805120 RBP: 000000006c8bfc70 R08: 0000000000000001 R09: 0000000068ae0308 R10: 000000000000000e R11: ffffffffffffffff R12: 0000000000000001 R13: 0000000068a0aa18 R14: 0000000000000015 R15: 0000000068944a88Quoted 8 lines
Kernel panic - not syncing: Segfault with no mm CPU: 0 UID: 0 PID: 24 Comm: mount Not tainted 6.11.0-rc4-next-20240819 #1 Stack: 600caeff 6c8bfc90 600d8b2a 68944a80 00000047 6c8bfda0 600cbfd9 6c8bfd50 68944ad0 68944a88 7f7ffff000 7f7fffffff Call Trace: [<600caeff>] ? special_mapping_close+0x16/0x19
Quoted 2 lines
Hmm. No "Code:" line? Did you just edit that out, or maybe UML doesn't print one out?
Nope, no editing, it is straight from my terminal. I guess UML just doesn't print one.
Quoted 12 lines
Anyway, for me that
special_mapping_close()disassembles to<+0>: mov %rdi,%rsi <+3>: mov 0x78(%rdi),%rdi <+7>: mov 0x20(%rdi),%rax <+11>: test %rax,%rax <+14>: je 0x600caa11 <special_mapping_close+24> <+16>: push %rbp <+17>: mov %rsp,%rbp <+20>: call *%rax <+22>: pop %rbp
Quoted 5 lines
<+23>: ret
<+24>: retwhich may just match yours, because
special_mapping_close+0x16is obviously that +22, and it's the return point for that call.
Yeah seems like it, objdump -dr shows:
0000000000000027 <special_mapping_close>:
27: 48 89 fe mov %rdi,%rsi
2a: 48 8b 7f 78 mov 0x78(%rdi),%rdi
2e: 48 8b 47 20 mov 0x20(%rdi),%rax
32: 48 85 c0 test %rax,%rax
35: 74 08 je 3f <special_mapping_close+0x18>
37: 55 push %rbp
38: 48 89 e5 mov %rsp,%rbp
3b: ff d0 call *%rax
3d: 5d pop %rbp
3e: c3 ret
3f: c3 retQuoted 24 lines
And your %rax value does match that invalid %rip value of
0x68006f6c.So it does look like it's jumping off to la-la-land, and the problem is the code
const struct vm_special_mapping *sm = vma->vm_private_data; if (sm->close) sm->close(sm, vma);where presumably '
vm_private_data' isn't a "structvm_special_mapping*" at all.And I think I see the problem.
When we have that '
legacy_special_mapping_vmops', then thevm_private_datafield actually points to 'pages'.So the '
legacy_special_mapping_vmops' can *only* contain the '.fault' handler, not the other handlers.IOW, does something like this fix it?
---
a/mm/mmap.c
+++b/mm/mmap.c
@@ -2095,7 +2095,6 @@ static const structvm_operations_struct
Quoted 7 lines
special_mapping_vmops = { }; static const struct vm_operations_struct legacy_special_mapping_vmops = {- .close =
special_mapping_close,
.fault =special_mapping_fault,
};
Yes, that appears to fix it for me. I don't have much to say about the rest but others might :)
Cheers,
Nathan
Quoted 3 lines · Nathan Chancellor
Yes, that appears to fix it for me. I don't have much to say about the rest but others might :)
Ok, I did a quick hack-job to remove that disgusting install_special_mapping() legacy case.
With this, the old "install_special_mapping()" mess no longer exists, but I haven't even attempted to compile the result, because I don't have cross-compile environments for any of the affected architectures.
Except UML. I did at least build it there, but it's not like I tested it.
Adding architecture maintainers and more architecture lists to the participants. It would be good to actually get this patch tested. Context for newly added people:
https://lore.kernel.org/all/CAHk-=wj9QPhG4CjiX8YLRC1wHj_Qs-T8wJi0WEhkfp0cszvB9w@mail.gmail.com/
NOTE! This patch is against my current tree, not the linux-next changes. But it should entirely remove the case that caused problems in linux-next.
Linus
Quoted 5 lines · Linus Torvalds
Ok, I did a quick hack-job to remove that disgusting
install_special_mapping()legacy case.With this [..]
I forgot to actually attach that "this". Here it is. For real, this time.
Linus
arch/csky/kernel/vdso.c | 28 +++++++++++++++++++++------- arch/hexagon/kernel/vdso.c | 14 ++++++++++---- arch/nios2/mm/init.c | 12 ++++++++---- arch/sh/kernel/vsyscall/vsyscall.c | 14 +++++++++++--- arch/x86/um/vdso/vma.c | 12 ++++++++---- include/linux/mm.h | 4 ---- mm/mmap.c | 32 +++++--------------------------- 7 files changed, 63 insertions(+), 53 deletions(-)
arch/csky/kernel/vdso.c +21 -7
@@ -45,9 +45,16 @@ arch_initcall(vdso_init);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) { +struct vm_area_struct *vma;struct mm_struct *mm = current->mm;unsigned long vdso_base, vdso_len;int ret; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + }; +static struct vm_special_mapping vvar_mapping = { + .name ="[vvar]" , + }; vdso_len = (vdso_pages +1 ) <<PAGE_SHIFT ;
@@ -65,22 +72,29 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,*/ mm->context.vdso = (void *)vdso_base; - ret = -install_special_mapping (mm, vdso_base, vdso_pages <<PAGE_SHIFT , + vdso_mapping.pages = vdso_pagelist; + vma = +_install_special_mapping (mm, vdso_base, vdso_pages <<PAGE_SHIFT , (VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC ), - vdso_pagelist); + &vdso_mapping); -if (unlikely (ret)) { +if (unlikely (IS_ERR (vma))) { + ret =PTR_ERR (vma); mm->context.vdso =NULL ; goto end; } vdso_base += (vdso_pages <<PAGE_SHIFT ); - ret =install_special_mapping (mm, vdso_base,PAGE_SIZE , - (VM_READ |VM_MAYREAD ), &vdso_pagelist[vdso_pages]); + vvar_mapping.pages = &vdso_pagelist[vdso_pages]; + vma =_install_special_mapping (mm, vdso_base,PAGE_SIZE , + (VM_READ |VM_MAYREAD ), &vvar_mapping); -if (unlikely (ret)) +if (unlikely (IS_ERR (vma))) { + ret =PTR_ERR (vma); mm->context.vdso =NULL ; + goto end; + } + ret =0 ; end:mmap_write_unlock (mm);return ret;
arch/hexagon/kernel/vdso.c +10 -4
@@ -51,7 +51,11 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) {int ret;unsigned long vdso_base; +struct vm_area_struct *vma;struct mm_struct *mm = current->mm; +static struct vm_special_mapping vdso_mapping = { + name ="[vdso]" , + };if (mmap_write_lock_killable (mm))return -EINTR ;
@@ -66,16 +70,18 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) }/* MAYWRITE to allow gdb to COW and set breakpoints. */ - ret =install_special_mapping (mm, vdso_base,PAGE_SIZE , + vdso_mapping.pages = &&vdso_page; + vma =_install_special_mapping (mm, vdso_base,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - &vdso_page); + &vdso_mapping); -if (ret) + ret =PTR_ERR (vma); +if (IS_ERR (vma)) goto up_fail; mm->context.vdso = (void *)vdso_base; - + ret =0 ; up_fail:mmap_write_unlock (mm);return ret;
arch/nios2/mm/init.c +8 -4
@@ -82,6 +82,10 @@ void __init mmu_init(void)pgd_t swapper_pg_dir[PTRS_PER_PGD ]__aligned(PAGE_SIZE) ;pte_t invalid_pte_table[PTRS_PER_PTE ]__aligned(PAGE_SIZE) ;static struct page *kuser_page[1 ]; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = kuser_page, +};static int alloc_kuser_page (void ) {
@@ -106,18 +110,18 @@ arch_initcall(alloc_kuser_page);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) {struct mm_struct *mm = current->mm; -int ret; +struct vm_area_struct *vma;mmap_write_lock (mm);/* Map kuser helpers to user space address */ - ret =install_special_mapping (mm,KUSER_BASE ,KUSER_SIZE , + vma =_install_special_mapping (mm,KUSER_BASE ,KUSER_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD | -VM_MAYEXEC , kuser_page); +VM_MAYEXEC , &vdso_mapping);mmap_write_unlock (mm); -return ret; +return IS_ERR (vma) ?PTR_ERR (vma) :0 ; }const char *arch_vma_name (struct vm_area_struct *vma)
arch/sh/kernel/vsyscall/vsyscall.c +11 -3
@@ -36,6 +36,10 @@ __setup("vdso=", vdso_setup);*/ extern const char vsyscall_trapa_start, vsyscall_trapa_end;static struct page *syscall_pages[1 ]; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = syscall_pages; +};int __init vsyscall_init (void ) {
@@ -58,6 +62,7 @@ int __init vsyscall_init(void)int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) {struct mm_struct *mm = current->mm; +struct vm_area_struct *vma;unsigned long addr;int ret;
@@ -70,14 +75,17 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) goto up_fail; } - ret =install_special_mapping (mm, addr,PAGE_SIZE , + vdso_mapping.pages = syscall_pages; + vma =_install_special_mapping (mm, addr,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - syscall_pages); -if (unlikely (ret)) + &vdso_mapping); + ret =PTR_ERR (vma); +if (unlikely (IS_ERR (vma))) goto up_fail; current->mm->context.vdso = (void *)addr; + ret =0 ; up_fail:mmap_write_unlock (mm);
arch/x86/um/vdso/vma.c +8 -4
@@ -52,8 +52,11 @@ subsys_initcall(init_vdso);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) { -int err; +struct vm_area_struct *vma;struct mm_struct *mm = current->mm; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + };if (!vdso_enabled)return 0 ;
@@ -61,12 +64,13 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)if (mmap_write_lock_killable (mm))return -EINTR ; - err =install_special_mapping (mm, um_vdso_addr,PAGE_SIZE , + vdso_mapping.pages = vdsop; + vma =_install_special_mapping (mm, um_vdso_addr,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - vdsop); + &vdso_mapping);mmap_write_unlock (mm); -return err; +return IS_ERR (vma) ?PTR_ERR (vma) :0 ; }
include/linux/mm.h +0 -4
@@ -3381,10 +3381,6 @@ extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, const struct vm_special_mapping *spec); -/* This is an obsolete alternative to _install_special_mapping. */ -extern int install_special_mapping (struct mm_struct *mm, -unsigned long addr,unsigned long len, -unsigned long flags,struct page **pages);unsigned long randomize_stack_top (unsigned long stack_top);unsigned long randomize_page (unsigned long start,unsigned long range);
mm/mmap.c +5 -27
@@ -3665,27 +3665,17 @@ static const struct vm_operations_struct special_mapping_vmops = { .may_split = special_mapping_split, }; -static const struct vm_operations_struct legacy_special_mapping_vmops = { - .close = special_mapping_close, - .fault = special_mapping_fault, -}; -static vm_fault_t special_mapping_fault (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;pgoff_t pgoff;struct page **pages; +struct vm_special_mapping *sm = vma->vm_private_data; -if (vma->vm_ops == &legacy_special_mapping_vmops) { - pages = vma->vm_private_data; - }else { -struct vm_special_mapping *sm = vma->vm_private_data; +if (sm->fault) +return sm->fault (sm, vmf->vma, vmf); -if (sm->fault) -return sm->fault (sm, vmf->vma, vmf); - - pages = sm->pages; - } + pages = sm->pages;for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) pgoff--;
@@ -3740,8 +3730,7 @@ bool vma_is_special_mapping(const struct vm_area_struct *vma, const struct vm_special_mapping *sm) {return vma->vm_private_data == sm && - (vma->vm_ops == &special_mapping_vmops || - vma->vm_ops == &legacy_special_mapping_vmops); + vma->vm_ops == &special_mapping_vmops; }/*
@@ -3762,17 +3751,6 @@ struct vm_area_struct *_install_special_mapping( &special_mapping_vmops); } -int install_special_mapping (struct mm_struct *mm, -unsigned long addr,unsigned long len, -unsigned long vm_flags,struct page **pages) -{ -struct vm_area_struct *vma =__install_special_mapping ( - mm, addr, len, vm_flags, (void *)pages, - &legacy_special_mapping_vmops); - -return PTR_ERR_OR_ZERO (vma); -} -static DEFINE_MUTEX (mm_all_locks_mutex);static void vm_lock_anon_vma (struct mm_struct *mm,struct anon_vma *anon_vma)
Andrew Morton
maintainerQuoted 9 lines · On Mon, 19 Aug 2024 13:16:32 -0700 Linus Torvalds…
Quoted 5 lines · Linus Torvalds
Ok, I did a quick hack-job to remove that disgusting
install_special_mapping()legacy case.With this [..]
I forgot to actually attach that "this". Here it is. For real, this time.
Thanks. Do you think your one-liner remains desirable with this fix in place?
Quoted 6 lines · Andrew Morton
Quoted 3 lines
I forgot to actually attach that "this". Here it is. For real, this time.
Thanks. Do you think your one-liner remains desirable with this fix in place?
That patch actually removes the whole 'legacy_special_mapping_vmops' that my one-liner then removed the '.close' field from, so no - my one-liner just becomes a non-issue.
NOTE! That patch of mine *will* conflict with Michael's patch series, since my patch to remove legacy_special_mapping_vmops was done on top of my current -git tree. But it should be an obvious conflict, in that it just means that the addition of .close never happens.
Linus
Michael Ellerman
reviewerQuoted 20 lines · Linus Torvalds
Quoted 5 lines · Linus Torvalds
Ok, I did a quick hack-job to remove that disgusting
install_special_mapping()legacy case.With this [..]
I forgot to actually attach that "this". Here it is. For real, this time.
Linus
arch/csky/kernel/vdso.c | 28 +++++++++++++++++++++------- arch/hexagon/kernel/vdso.c | 14 ++++++++++---- arch/nios2/mm/init.c | 12 ++++++++---- arch/sh/kernel/vsyscall/vsyscall.c | 14 +++++++++++--- arch/x86/um/vdso/vma.c | 12 ++++++++---- include/linux/mm.h | 4 ---- mm/mmap.c | 32 +++++--------------------------- 7 files changed, 63 insertions(+), 53 deletions(-)
...
arch/sh/kernel/vsyscall/vsyscall.c __setup("vdso=", vdso_setup); · line 41
Quoted 10 lines
index
1bd85a6949c4..5e68ab7a8898100644arch/sh/kernel/vsyscall/vsyscall.c +3
-0@@ -36,6 +36,10 @@ __setup("vdso=", vdso_setup);*/ extern const char vsyscall_trapa_start, vsyscall_trapa_end;static struct page *syscall_pages[1 ]; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = syscall_pages;
^
should be ,
arch/sh/kernel/vsyscall/vsyscall.c __setup("vdso=", vdso_setup); · line 42
Quoted 1 line
arch/sh/kernel/vsyscall/vsyscall.c +1
-0+};
cheers
Quoted 7 lines · Michael Ellerman
arch/sh/kernel/vsyscall/vsyscall.c __setup("vdso=", vdso_setup); · line 41
Quoted 3 lines · Linus Torvalds
arch/sh/kernel/vsyscall/vsyscall.c +3
-0+static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = syscall_pages;^
should be ,
Ack. Changed here locally. But I assume you also don't actually test sh...
It would be good to get acks from the architectures that still used the legacy interface.
Linus
Quoted 10 lines · Linus Torvalds
Quoted 7 lines · Michael Ellerman
arch/sh/kernel/vsyscall/vsyscall.c __setup("vdso=", vdso_setup); · line 41
Quoted 3 lines · Linus Torvalds
arch/sh/kernel/vsyscall/vsyscall.c +3
-0+static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = syscall_pages;^
should be ,Ack. Changed here locally.
Hexagon also has &&vdso_page which I don't understand (but have a toolchain for somewhere to at least smoketest...)
Quoted 1 line
But I assume you also don't actually test sh...
But I do.
Aside: arch/sh smoketests easily under qemu, here's a relocatable binary toolchain:
wget https://landley.net/bin/toolchains/latest/sh4-linux-musl-cross.tar.xz
tar xvf sh4-linux-musl-cross-tar.xz CROSS_COMPILE=$PWD/sh4-linux-musl-cross/bin/sh4-linux-musl-
And https://landley.net/bin/mkroot/latest/sh4.tgz is a tiny qemu-system-sh4 system with kernel + initramfs.cpio.gz + run-qemu.sh with the config used to build it under docs/ and as the first three lines of docs/linux-miniconfig record, the kernel is arch/sh/boot/zImage.
You can keep the initramfs.cpio.gz and run-qemu.sh and swap out the kernel for a quick boot to shell prompt under qemu. Serial console is qemu's stdin/stdout, "exit" to shut down the emulator.
The build script, if you care, is 400 lines of bash:
https://github.com/landley/toybox/blob/master/mkroot/mkroot.sh
Quoted 2 lines
It would be good to get acks from the architectures that still used the legacy interface.
I'll give arch/sh a whirl. Can somebody answer my above hexagon question?
Quoted 1 line
Linus
Rob
Quoted 3 lines · Rob Landley
Hexagon also has &&vdso_page which I don't understand (but have a toolchain for somewhere to at least smoketest...)
The '&&' is just a typo. It should obviously be just a single '&'. As mentioned, the only testing that patch got was a x86-64 UML build test.
Fixed locally.
Linus
Rob Landley
Tested-byQuoted 10 lines · Linus Torvalds
Quoted 3 lines · Rob Landley
Hexagon also has &&vdso_page which I don't understand (but have a toolchain for somewhere to at least smoketest...)
The '&&' is just a typo. It should obviously be just a single '&'. As mentioned, the only testing that patch got was a x86-64 UML build test.
Fixed locally.
I deleted the extra ; and arch/sh4 built and qemu-system-sh4 booted to shell prompt:
Freeing initrd memory: 556K
Freeing unused kernel image (initmem) memory: 132K
This architecture does not have kernel memory protection. Run /init as init process
8139cp 0000:00:02.0 eth0: link up, 100Mbps, full-duplex, lpa 0x05E1 Type exit when done.
# cat /proc/version Linux version 6.11.0-rc4 (landley@driftwood) (sh4-linux-musl-gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.33.1) #1 Tue Aug 20 16:45:25 CDT 2024 # head -n 3 /proc/cpuinfo machine : RTS7751R2D processor : 0 cpu family : sh4
Rob
Linus Torvalds
Tested-by Rob LandleyQuoted 2 lines · Rob Landley
Tested-by Rob Landley <[email protected]>
Ok, here's the patch with the fixes for problems that were pointed out, and with the beginnings of a commit message.
I'm going to be traveling for the next ~10 days, and while I'll have my laptop and internet, I'm unlikely to work on this any more.
Andrew - I think this is good, but there may be other issues lurking. Do with it as you see fit,
Linus
From ef4e84d69c3d669e606b5d386625267a3b9c9573 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 20 Aug 2024 15:14:47 -0700
Subject: [PATCH] mm: remove legacy install_special_mapping() code
All relevant architectures had already been converted to the new interface (which just has an underscore in front of the name - not very imaginative naming), this just force-converts the stragglers.
The modern interface is almost identical to the old one, except instead of the page pointer it takes a "struct vm_special_mapping" that describes the mapping (and contains the page pointer as one member), and it returns the resulting 'vma' instead of just the error code.
Getting rid of the old interface also gets rid of some special casing, which had caused problems with the mremap extensions to "struct vm_special_mapping".
--- arch/csky/kernel/vdso.c | 28 +++++++++++++++++++------- arch/hexagon/kernel/vdso.c | 14 +++++++++---- arch/nios2/mm/init.c | 12 +++++++---- arch/sh/kernel/vsyscall/vsyscall.c | 14 ++++++++++--- arch/x86/um/vdso/vma.c | 12 +++++++---- include/linux/mm.h | 4 ---- mm/mmap.c | 32 +++++------------------------- 7 files changed, 63 insertions(+), 53 deletions(-)
arch/csky/kernel/vdso.c +21 -7
@@ -45,9 +45,16 @@ arch_initcall(vdso_init);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) { +struct vm_area_struct *vma;struct mm_struct *mm = current->mm;unsigned long vdso_base, vdso_len;int ret; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + }; +static struct vm_special_mapping vvar_mapping = { + .name ="[vvar]" , + }; vdso_len = (vdso_pages +1 ) <<PAGE_SHIFT ;
@@ -65,22 +72,29 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,*/ mm->context.vdso = (void *)vdso_base; - ret = -install_special_mapping (mm, vdso_base, vdso_pages <<PAGE_SHIFT , + vdso_mapping.pages = vdso_pagelist; + vma = +_install_special_mapping (mm, vdso_base, vdso_pages <<PAGE_SHIFT , (VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC ), - vdso_pagelist); + &vdso_mapping); -if (unlikely (ret)) { +if (unlikely (IS_ERR (vma))) { + ret =PTR_ERR (vma); mm->context.vdso =NULL ; goto end; } vdso_base += (vdso_pages <<PAGE_SHIFT ); - ret =install_special_mapping (mm, vdso_base,PAGE_SIZE , - (VM_READ |VM_MAYREAD ), &vdso_pagelist[vdso_pages]); + vvar_mapping.pages = &vdso_pagelist[vdso_pages]; + vma =_install_special_mapping (mm, vdso_base,PAGE_SIZE , + (VM_READ |VM_MAYREAD ), &vvar_mapping); -if (unlikely (ret)) +if (unlikely (IS_ERR (vma))) { + ret =PTR_ERR (vma); mm->context.vdso =NULL ; + goto end; + } + ret =0 ; end:mmap_write_unlock (mm);return ret;
arch/hexagon/kernel/vdso.c +10 -4
@@ -51,7 +51,11 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) {int ret;unsigned long vdso_base; +struct vm_area_struct *vma;struct mm_struct *mm = current->mm; +static struct vm_special_mapping vdso_mapping = { + name ="[vdso]" , + };if (mmap_write_lock_killable (mm))return -EINTR ;
@@ -66,16 +70,18 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) }/* MAYWRITE to allow gdb to COW and set breakpoints. */ - ret =install_special_mapping (mm, vdso_base,PAGE_SIZE , + vdso_mapping.pages = &vdso_page; + vma =_install_special_mapping (mm, vdso_base,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - &vdso_page); + &vdso_mapping); -if (ret) + ret =PTR_ERR (vma); +if (IS_ERR (vma)) goto up_fail; mm->context.vdso = (void *)vdso_base; - + ret =0 ; up_fail:mmap_write_unlock (mm);return ret;
arch/nios2/mm/init.c +8 -4
@@ -82,6 +82,10 @@ void __init mmu_init(void)pgd_t swapper_pg_dir[PTRS_PER_PGD ]__aligned(PAGE_SIZE) ;pte_t invalid_pte_table[PTRS_PER_PTE ]__aligned(PAGE_SIZE) ;static struct page *kuser_page[1 ]; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = kuser_page, +};static int alloc_kuser_page (void ) {
@@ -106,18 +110,18 @@ arch_initcall(alloc_kuser_page);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) {struct mm_struct *mm = current->mm; -int ret; +struct vm_area_struct *vma;mmap_write_lock (mm);/* Map kuser helpers to user space address */ - ret =install_special_mapping (mm,KUSER_BASE ,KUSER_SIZE , + vma =_install_special_mapping (mm,KUSER_BASE ,KUSER_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD | -VM_MAYEXEC , kuser_page); +VM_MAYEXEC , &vdso_mapping);mmap_write_unlock (mm); -return ret; +return IS_ERR (vma) ?PTR_ERR (vma) :0 ; }const char *arch_vma_name (struct vm_area_struct *vma)
arch/sh/kernel/vsyscall/vsyscall.c +11 -3
@@ -36,6 +36,10 @@ __setup("vdso=", vdso_setup);*/ extern const char vsyscall_trapa_start, vsyscall_trapa_end;static struct page *syscall_pages[1 ]; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + .pages = syscall_pages, +};int __init vsyscall_init (void ) {
@@ -58,6 +62,7 @@ int __init vsyscall_init(void)int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) {struct mm_struct *mm = current->mm; +struct vm_area_struct *vma;unsigned long addr;int ret;
@@ -70,14 +75,17 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) goto up_fail; } - ret =install_special_mapping (mm, addr,PAGE_SIZE , + vdso_mapping.pages = syscall_pages; + vma =_install_special_mapping (mm, addr,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - syscall_pages); -if (unlikely (ret)) + &vdso_mapping); + ret =PTR_ERR (vma); +if (unlikely (IS_ERR (vma))) goto up_fail; current->mm->context.vdso = (void *)addr; + ret =0 ; up_fail:mmap_write_unlock (mm);
arch/x86/um/vdso/vma.c +8 -4
@@ -52,8 +52,11 @@ subsys_initcall(init_vdso);int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) { -int err; +struct vm_area_struct *vma;struct mm_struct *mm = current->mm; +static struct vm_special_mapping vdso_mapping = { + .name ="[vdso]" , + };if (!vdso_enabled)return 0 ;
@@ -61,12 +64,13 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)if (mmap_write_lock_killable (mm))return -EINTR ; - err =install_special_mapping (mm, um_vdso_addr,PAGE_SIZE , + vdso_mapping.pages = vdsop; + vma =_install_special_mapping (mm, um_vdso_addr,PAGE_SIZE ,VM_READ |VM_EXEC |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC , - vdsop); + &vdso_mapping);mmap_write_unlock (mm); -return err; +return IS_ERR (vma) ?PTR_ERR (vma) :0 ; }
include/linux/mm.h +0 -4
@@ -3381,10 +3381,6 @@ extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, const struct vm_special_mapping *spec); -/* This is an obsolete alternative to _install_special_mapping. */ -extern int install_special_mapping (struct mm_struct *mm, -unsigned long addr,unsigned long len, -unsigned long flags,struct page **pages);unsigned long randomize_stack_top (unsigned long stack_top);unsigned long randomize_page (unsigned long start,unsigned long range);
mm/mmap.c +5 -27
@@ -3665,27 +3665,17 @@ static const struct vm_operations_struct special_mapping_vmops = { .may_split = special_mapping_split, }; -static const struct vm_operations_struct legacy_special_mapping_vmops = { - .close = special_mapping_close, - .fault = special_mapping_fault, -}; -static vm_fault_t special_mapping_fault (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;pgoff_t pgoff;struct page **pages; +struct vm_special_mapping *sm = vma->vm_private_data; -if (vma->vm_ops == &legacy_special_mapping_vmops) { - pages = vma->vm_private_data; - }else { -struct vm_special_mapping *sm = vma->vm_private_data; +if (sm->fault) +return sm->fault (sm, vmf->vma, vmf); -if (sm->fault) -return sm->fault (sm, vmf->vma, vmf); - - pages = sm->pages; - } + pages = sm->pages;for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) pgoff--;
@@ -3740,8 +3730,7 @@ bool vma_is_special_mapping(const struct vm_area_struct *vma, const struct vm_special_mapping *sm) {return vma->vm_private_data == sm && - (vma->vm_ops == &special_mapping_vmops || - vma->vm_ops == &legacy_special_mapping_vmops); + vma->vm_ops == &special_mapping_vmops; }/*
@@ -3762,17 +3751,6 @@ struct vm_area_struct *_install_special_mapping( &special_mapping_vmops); } -int install_special_mapping (struct mm_struct *mm, -unsigned long addr,unsigned long len, -unsigned long vm_flags,struct page **pages) -{ -struct vm_area_struct *vma =__install_special_mapping ( - mm, addr, len, vm_flags, (void *)pages, - &legacy_special_mapping_vmops); - -return PTR_ERR_OR_ZERO (vma); -} -static DEFINE_MUTEX (mm_all_locks_mutex);static void vm_lock_anon_vma (struct mm_struct *mm,struct anon_vma *anon_vma)
Signature · 1 line
2.43.0.493.gbc7ee2e5e1
Andrew Morton
maintainerQuoted 2 lines · On Tue, 20 Aug 2024 16:14:29 -0700 Linus Torvalds…
Andrew - I think this is good, but there may be other issues lurking. Do with it as you see fit,
Hey you know me, I'll merge any old thing if I think it'll help nurture newbies.
Sven Schnelle
reviewerQuoted 83 lines · Nathan Chancellor
Hi Michael,
Quoted 80 lines · Michael Ellerman
Add an optional
close()callback to structvm_special_mapping. It will be used, by powerpc at least, to handle unmapping of the VDSO.Although support for unmapping the VDSO was initially added for CRIU[1], it is not desirable to guard that support behind
CONFIG_CHECKPOINT_RESTORE.There are other known users of unmapping the VDSO which are not related to CRIU, eg. Valgrind [2] and void-ship [3].
The powerpc
arch_unmap()hook has been in place for ~9 years, with no ifdef, so there may be other unknown users that have come to rely on unmapping the VDSO. Even if the code was behind an ifdef, major distros enableCHECKPOINT_RESTOREso users may not realise unmapping the VDSO depends on that configuration option.It's also undesirable to have such core mm behaviour behind a relatively obscure CONFIG option.
Longer term the unmap behaviour should be standardised across architectures, however that is complicated by the fact the VDSO pointer is stored differently across architectures. There was a previous attempt to unify that handling [4], which could be revived.
See [5] for further discussion.
[1]: commit
83d3f0e90c6c("powerpc/mm: tracking vDSO remap")
[2]: https://sourceware.org/git/?p=valgrind.git;a=commit;h=3a004915a2cbdcdebafc1612427576bf3321eef5
[3]: https://github.com/insanitybit/void-ship
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/linuxppc-dev/shiq5v3jrmyi6ncwke7wgl76ojysgbhrchsk32q4lbx2hadqqc@kzyy2igem256Suggested-by Linus Torvalds <[email protected]>Signed-off-by Michael Ellerman <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- include/linux/mm_types.h | 3 +++ mm/mmap.c | 6 ++++++ 2 files changed, 9 insertions(+)v2:
- Add some blank lines as requested.
- Expandspecial_mapping_close()comment.
- Add David's reviewed-by.
- Expand change log to capture review discussion.include/linux/mm_types.h +3
-0@@ -1313,6 +1313,9 @@ struct vm_special_mapping {int (*mremap)(const struct vm_special_mapping *sm,struct vm_area_struct *new_vma); + +void (*close)(const struct vm_special_mapping *sm, +struct vm_area_struct *vma); };enum tlb_flush_reason {mm/mmap.c +6
-0@@ -3620,10 +3620,16 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)static vm_fault_t special_mapping_fault (struct vm_fault *vmf);/* +* Close hook, called for unmap() and on the old vma for mremap(). +* * Having a close hook prevents vma merging regardless of flags. */ static void special_mapping_close (struct vm_area_struct *vma) { +const struct vm_special_mapping *sm = vma->vm_private_data; + +if (sm->close) + sm->close (sm, vma); }
Quoted 7 lines
Quoted 4 lines
mm/mmap.c +0
-1static const char *special_mapping_name (struct vm_area_struct *vma) --2.45.2
This change is now in -next and I bisected a crash that our CI sees with ARCH=um to it:
I see another crash on s390, which seems related, but rather an issue in uprobe. This can be reproduced by
# cd linux-next/tools/testing/selftests/ftrace # ./ftracetest ./test.d/dynevent/add_remove_uprobe.tc
The 'mm: Add optional close() to struct vm_special_mapping' patch just makes it visible. I enabled KASAN, and that shows me:
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384 [ 44.505479] [ 44.505486] CPU: 51 UID: 0 PID: 1384 Comm: sh Not tainted 6.11.0-rc6-next-20240902-dirty #1496 [ 44.505503] Hardware name: IBM 3931 A01 704 (z/VM 7.3.0) [ 44.505508] Call Trace: [ 44.505511] [<000b0324d2f78080>] dump_stack_lvl+0xd0/0x108 [ 44.505521] [<000b0324d2f5435c>] print_address_description.constprop.0+0x34/0x2e0 [ 44.505529] [<000b0324d2f5464c>] print_report+0x44/0x138 [ 44.505536] [<000b0324d1383192>] kasan_report+0xc2/0x140 [ 44.505543] [<000b0324d2f52904>] special_mapping_close+0x9c/0xc8 [ 44.505550] [<000b0324d12c7978>] remove_vma+0x78/0x120 [ 44.505557] [<000b0324d128a2c6>] exit_mmap+0x326/0x750 [ 44.505563] [<000b0324d0ba655a>] __mmput+0x9a/0x370 [ 44.505570] [<000b0324d0bbfbe0>] exit_mm+0x240/0x340 [ 44.505575] [<000b0324d0bc0228>] do_exit+0x548/0xd70 [ 44.505580] [<000b0324d0bc1102>] do_group_exit+0x132/0x390 [ 44.505586] [<000b0324d0bc13b6>] __s390x_sys_exit_group+0x56/0x60 [ 44.505592] [<000b0324d0adcbd6>] do_syscall+0x2f6/0x430 [ 44.505599] [<000b0324d2f78434>] __do_syscall+0xa4/0x170 [ 44.505606] [<000b0324d2f9454c>] system_call+0x74/0x98 [ 44.505614] [ 44.505616] Allocated by task 1384: [ 44.505621] kasan_save_stack+0x40/0x70 [ 44.505630] kasan_save_track+0x28/0x40 [ 44.505636] __kasan_kmalloc+0xa0/0xc0 [ 44.505642] __create_xol_area+0xfa/0x410 [ 44.505648] get_xol_area+0xb0/0xf0 [ 44.505652] uprobe_notify_resume+0x27a/0x470 [ 44.505657] irqentry_exit_to_user_mode+0x15e/0x1d0 [ 44.505664] pgm_check_handler+0x122/0x170 [ 44.505670] [ 44.505672] Freed by task 1384: [ 44.505676] kasan_save_stack+0x40/0x70 [ 44.505682] kasan_save_track+0x28/0x40 [ 44.505687] kasan_save_free_info+0x4a/0x70 [ 44.505693] __kasan_slab_free+0x5a/0x70 [ 44.505698] kfree+0xe8/0x3f0 [ 44.505704] __mmput+0x20/0x370 [ 44.505709] exit_mm+0x240/0x340 [ 44.505713] do_exit+0x548/0xd70 [ 44.505718] do_group_exit+0x132/0x390 [ 44.505722] __s390x_sys_exit_group+0x56/0x60 [ 44.505727] do_syscall+0x2f6/0x430 [ 44.505732] __do_syscall+0xa4/0x170 [ 44.505738] system_call+0x74/0x98
The problem is that uprobe_clear_state() kfree's struct xol_area, which contains struct vm_special_mapping *xol_mapping. This one is passed to _install_special_mapping() in xol_add_vma().
__mput reads:
static inline void __mmput(struct mm_struct *mm)
{
VM_BUG_ON(atomic_read(&mm->mm_users));
uprobe_clear_state(mm);
exit_aio(mm);
ksm_exit(mm);
khugepaged_exit(mm); /* must run before exit_mmap */
exit_mmap(mm);
...
}So uprobe_clear_state() in the beginning free's the memory area containing the vm_special_mapping data, but exit_mmap() uses this address later via vma->vm_private_data (which was set in _install_special_mapping().
The following change fixes this for me, but i'm not sure about any side effects:
kernel/fork.c +1 -1
@@ -1340,11 +1340,11 @@ static inline void __mmput(struct mm_struct *mm) {VM_BUG_ON (atomic_read (&mm->mm_users)); -uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */ exit_mmap (mm); +uprobe_clear_state (mm);mm_put_huge_zero_folio (mm);set_mm_exe_file (mm,NULL );if (!list_empty (&mm->mmlist)) {
Any thoughts?
Andrew Morton
maintainerQuoted 14 lines · On Mon, 02 Sep 2024 21:06:48 +0200 Sven Schnelle <[email protected]> wrote:
So
uprobe_clear_state()in the beginning free's the memory area containing thevm_special_mappingdata, butexit_mmap()uses this address later viavma->vm_private_data(which was set in_install_special_mapping().The following change fixes this for me, but i'm not sure about any side effects:
kernel/fork.c +0
-0@@ -1340,11 +1340,11 @@ static inline void __mmput(struct mm_struct *mm) {VM_BUG_ON (atomic_read (&mm->mm_users));
kernel/fork.c static inline void __mmput(struct mm_struct *mm) · line 1350
Quoted 10 lines
kernel/fork.c +1
-1-uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */ exit_mmap (mm); +uprobe_clear_state (mm);mm_put_huge_zero_folio (mm);set_mm_exe_file (mm,NULL );if (!list_empty (&mm->mmlist)) {
uprobe_clear_state() is a pretty simple low-level thing. Side-effects seem unlikely?
Quoted 3 lines · Andrew Morton
uprobe_clear_state()is a pretty simple low-level thing. Side-effects seem unlikely?
I think uprobe_clear_state() should be removed from fork.c entirely, made 'static', and then we'd have
area->xol_mapping.close = uprobe_clear_state;
in __create_xol_area() instead (ok, the arguments change, instead of looking up "mm->uprobes_state.xol_area", it would get it as the vma argument)
That's how it should always have been, except we didn't have a close() function.
Hmm?
Linus
Sven Schnelle
reviewerQuoted 10 lines · Linus Torvalds
Quoted 3 lines · Andrew Morton
uprobe_clear_state()is a pretty simple low-level thing. Side-effects seem unlikely?I think
uprobe_clear_state()should be removed fromfork.centirely, made 'static', and then we'd havearea->xol_mapping.close = uprobe_clear_state;
Quoted 7 lines
in
__create_xol_area()instead (ok, the arguments change, instead of looking up "mm->uprobes_state.xol_area", it would get it as the vma argument)That's how it should always have been, except we didn't have a
close()function.Hmm?
Indeed, that's much better. I'll prepare a patch.
Thanks!
Sven Schnelle
reviewerPATCH 1/1 uprobes: use vm_special_mapping close() functionality
In reply to Linus TorvaldsThe following KASAN splat was shown:
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384 [ 44.505479] [ 44.505486] CPU: 51 UID: 0 PID: 1384 Comm: sh Not tainted 6.11.0-rc6-next-20240902-dirty #1496 [ 44.505503] Hardware name: IBM 3931 A01 704 (z/VM 7.3.0) [ 44.505508] Call Trace: [ 44.505511] [<000b0324d2f78080>] dump_stack_lvl+0xd0/0x108 [ 44.505521] [<000b0324d2f5435c>] print_address_description.constprop.0+0x34/0x2e0 [ 44.505529] [<000b0324d2f5464c>] print_report+0x44/0x138 [ 44.505536] [<000b0324d1383192>] kasan_report+0xc2/0x140 [ 44.505543] [<000b0324d2f52904>] special_mapping_close+0x9c/0xc8 [ 44.505550] [<000b0324d12c7978>] remove_vma+0x78/0x120 [ 44.505557] [<000b0324d128a2c6>] exit_mmap+0x326/0x750 [ 44.505563] [<000b0324d0ba655a>] __mmput+0x9a/0x370 [ 44.505570] [<000b0324d0bbfbe0>] exit_mm+0x240/0x340 [ 44.505575] [<000b0324d0bc0228>] do_exit+0x548/0xd70 [ 44.505580] [<000b0324d0bc1102>] do_group_exit+0x132/0x390 [ 44.505586] [<000b0324d0bc13b6>] __s390x_sys_exit_group+0x56/0x60 [ 44.505592] [<000b0324d0adcbd6>] do_syscall+0x2f6/0x430 [ 44.505599] [<000b0324d2f78434>] __do_syscall+0xa4/0x170 [ 44.505606] [<000b0324d2f9454c>] system_call+0x74/0x98 [ 44.505614] [ 44.505616] Allocated by task 1384: [ 44.505621] kasan_save_stack+0x40/0x70 [ 44.505630] kasan_save_track+0x28/0x40 [ 44.505636] __kasan_kmalloc+0xa0/0xc0 [ 44.505642] __create_xol_area+0xfa/0x410 [ 44.505648] get_xol_area+0xb0/0xf0 [ 44.505652] uprobe_notify_resume+0x27a/0x470 [ 44.505657] irqentry_exit_to_user_mode+0x15e/0x1d0 [ 44.505664] pgm_check_handler+0x122/0x170 [ 44.505670] [ 44.505672] Freed by task 1384: [ 44.505676] kasan_save_stack+0x40/0x70 [ 44.505682] kasan_save_track+0x28/0x40 [ 44.505687] kasan_save_free_info+0x4a/0x70 [ 44.505693] __kasan_slab_free+0x5a/0x70 [ 44.505698] kfree+0xe8/0x3f0 [ 44.505704] __mmput+0x20/0x370 [ 44.505709] exit_mm+0x240/0x340 [ 44.505713] do_exit+0x548/0xd70 [ 44.505718] do_group_exit+0x132/0x390 [ 44.505722] __s390x_sys_exit_group+0x56/0x60 [ 44.505727] do_syscall+0x2f6/0x430 [ 44.505732] __do_syscall+0xa4/0x170 [ 44.505738] system_call+0x74/0x98
The problem is that uprobe_clear_state() kfree's struct xol_area, which contains struct vm_special_mapping *xol_mapping. This one is passed to _install_special_mapping() in xol_add_vma().__mput reads:
static inline void __mmput(struct mm_struct *mm)
{
VM_BUG_ON(atomic_read(&mm->mm_users));
uprobe_clear_state(mm);
exit_aio(mm);
ksm_exit(mm);
khugepaged_exit(mm); /* must run before exit_mmap */
exit_mmap(mm);
...
}So uprobe_clear_state() in the beginning free's the memory area containing the vm_special_mapping data, but exit_mmap() uses this address later via vma->vm_private_data (which was set in _install_special_mapping().
Fix this by moving uprobe_clear_state() to uprobes.c and use it as close() callback.
--- include/linux/uprobes.h | 1 - kernel/events/uprobes.c | 39 ++++++++++++++++++++------------------- kernel/fork.c | 1 - 3 files changed, 20 insertions(+), 21 deletions(-)
include/linux/uprobes.h +0 -1
@@ -128,7 +128,6 @@ extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);extern void uprobe_notify_resume (struct pt_regs *regs);extern bool uprobe_deny_signal (void );extern bool arch_uprobe_skip_sstep (struct arch_uprobe *aup,struct pt_regs *regs); -extern void uprobe_clear_state (struct mm_struct *mm);extern int arch_uprobe_analyze_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long addr);extern int arch_uprobe_pre_xol (struct arch_uprobe *aup,struct pt_regs *regs);extern int arch_uprobe_post_xol (struct arch_uprobe *aup,struct pt_regs *regs);
kernel/events/uprobes.c +20 -19
@@ -1463,6 +1463,25 @@ void * __weak arch_uprobe_trampoline(unsigned long *psize)return &insn; } +/* +* uprobe_clear_state - Free the area allocated for slots. +*/ +static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;
@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state; area->xol_mapping.pages = area->pages; area->pages[0 ] =alloc_page (GFP_HIGHUSER );if (!area->pages[0 ])
@@ -1526,25 +1546,6 @@ static struct xol_area *get_xol_area(void)return area; } -/* -* uprobe_clear_state - Free the area allocated for slots. -*/ -void uprobe_clear_state (struct mm_struct *mm) -{ -struct xol_area *area = mm->uprobes_state.xol_area; - -mutex_lock (&delayed_uprobe_lock); -delayed_uprobe_remove (NULL , mm); -mutex_unlock (&delayed_uprobe_lock); - -if (!area) -return ; - -put_page (area->pages[0 ]); -kfree (area->bitmap); -kfree (area); -} -void uprobe_start_dup_mmap (void ) {percpu_down_read (&dup_mmap_sem);
kernel/fork.c +0 -1
@@ -1340,7 +1340,6 @@ static inline void __mmput(struct mm_struct *mm) {VM_BUG_ON (atomic_read (&mm->mm_users)); -uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */
Signature · 1 line
2.43.0
Sven Schnelle
reviewerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Hi Michael,
Quoted 49 lines · Sven Schnelle
The following KASAN splat was shown:
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384 [ 44.505479] [ 44.505486] CPU: 51 UID: 0 PID: 1384 Comm: sh Not tainted 6.11.0-rc6-next-20240902-dirty #1496 [ 44.505503] Hardware name: IBM 3931 A01 704 (z/VM 7.3.0) [ 44.505508] Call Trace: [ 44.505511] [<000b0324d2f78080>] dump_stack_lvl+0xd0/0x108 [ 44.505521] [<000b0324d2f5435c>] print_address_description.constprop.0+0x34/0x2e0 [ 44.505529] [<000b0324d2f5464c>] print_report+0x44/0x138 [ 44.505536] [<000b0324d1383192>] kasan_report+0xc2/0x140 [ 44.505543] [<000b0324d2f52904>] special_mapping_close+0x9c/0xc8 [ 44.505550] [<000b0324d12c7978>] remove_vma+0x78/0x120 [ 44.505557] [<000b0324d128a2c6>] exit_mmap+0x326/0x750 [ 44.505563] [<000b0324d0ba655a>] __mmput+0x9a/0x370 [ 44.505570] [<000b0324d0bbfbe0>] exit_mm+0x240/0x340 [ 44.505575] [<000b0324d0bc0228>] do_exit+0x548/0xd70 [ 44.505580] [<000b0324d0bc1102>] do_group_exit+0x132/0x390 [ 44.505586] [<000b0324d0bc13b6>] __s390x_sys_exit_group+0x56/0x60 [ 44.505592] [<000b0324d0adcbd6>] do_syscall+0x2f6/0x430 [ 44.505599] [<000b0324d2f78434>] __do_syscall+0xa4/0x170 [ 44.505606] [<000b0324d2f9454c>] system_call+0x74/0x98 [ 44.505614] [ 44.505616] Allocated by task 1384: [ 44.505621] kasan_save_stack+0x40/0x70 [ 44.505630] kasan_save_track+0x28/0x40 [ 44.505636] __kasan_kmalloc+0xa0/0xc0 [ 44.505642] __create_xol_area+0xfa/0x410 [ 44.505648] get_xol_area+0xb0/0xf0 [ 44.505652] uprobe_notify_resume+0x27a/0x470 [ 44.505657] irqentry_exit_to_user_mode+0x15e/0x1d0 [ 44.505664] pgm_check_handler+0x122/0x170 [ 44.505670] [ 44.505672] Freed by task 1384: [ 44.505676] kasan_save_stack+0x40/0x70 [ 44.505682] kasan_save_track+0x28/0x40 [ 44.505687] kasan_save_free_info+0x4a/0x70 [ 44.505693] __kasan_slab_free+0x5a/0x70 [ 44.505698] kfree+0xe8/0x3f0 [ 44.505704] __mmput+0x20/0x370 [ 44.505709] exit_mm+0x240/0x340 [ 44.505713] do_exit+0x548/0xd70 [ 44.505718] do_group_exit+0x132/0x390 [ 44.505722] __s390x_sys_exit_group+0x56/0x60 [ 44.505727] do_syscall+0x2f6/0x430 [ 44.505732] __do_syscall+0xa4/0x170 [ 44.505738] system_call+0x74/0x98
Quoted 1 line
[..]
As this has a dependency on your special mapping close series, do you want to carry that with your patches?
Michael Ellerman
reviewerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Quoted 1 line · Sven Schnelle
Hi Michael,
Hi Sven,
Quoted 7 lines
Quoted 5 lines · Sven Schnelle
The following KASAN splat was shown:
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384
...
Quoted 4 lines
Quoted 1 line
[..]
As this has a dependency on your special mapping close series, do you want to carry that with your patches?
Andrew has my series in mm-stable, so I think this should go into mm as well. I assume he will pick it up.
cheers
Andrew Morton
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Quoted 12 lines · On Wed, 04 Sep 2024 13:57:13 +1000 Michael Ellerman <[email protected]> wrote:
Quoted 1 line · Sven Schnelle
Hi Michael,
Hi Sven,
Quoted 7 lines
Quoted 5 lines · Sven Schnelle
The following KASAN splat was shown:
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384
Quoted 8 lines
...
Quoted 4 lines
Quoted 1 line
[..]
As this has a dependency on your special mapping close series, do you want to carry that with your patches?
Andrew has my series in mm-stable, so I think this should go into mm as well. I assume he will pick it up.
yup, thanks. Added, with
It appears that peterz is scooping up Sven's "uprobes: use kzalloc to allocate xol area".
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Sven SchnelleQuoted 13 lines · Sven Schnelle
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384 [ 44.505479] [ 44.505486] CPU: 51 UID: 0 PID: 1384 Comm: sh Not tainted 6.11.0-rc6-next-20240902-dirty #1496 [ 44.505503] Hardware name: IBM 3931 A01 704 (z/VM 7.3.0) [ 44.505508] Call Trace: [ 44.505511] [<000b0324d2f78080>] dump_stack_lvl+0xd0/0x108 [ 44.505521] [<000b0324d2f5435c>] print_address_description.constprop.0+0x34/0x2e0 [ 44.505529] [<000b0324d2f5464c>] print_report+0x44/0x138 [ 44.505536] [<000b0324d1383192>] kasan_report+0xc2/0x140 [ 44.505543] [<000b0324d2f52904>] special_mapping_close+0x9c/0xc8
^^^^^^^^^^^^^^^^^^^^^
Caused by
[PATCH v2 1/4] mm: Add optional close() to struct vm_special_mapping https://lore.kernel.org/all/[email protected]/
?
Quoted 20 lines
kernel/events/uprobes.c +16
-0+static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;
kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1503
Quoted 4 lines
kernel/events/uprobes.c +1
-0@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state;
LGTM.
but with or without this fix __create_xol_area() also needs
area->xol_mapping.mremap = NULL;
?
And in the longer term I think we should probably add a single instance of "struct vm_special_mapping uprobe_special_mapping with ->fault != NULL but this is another issue.
Oleg.
Sven Schnelle
reviewerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Quoted 49 lines · Oleg Nesterov
Quoted 13 lines · Sven Schnelle
[ 44.505448] ================================================================== 20:37:27 [3421/145075] [ 44.505455] BUG: KASAN: slab-use-after-free in special_mapping_close+0x9c/0xc8 [ 44.505471] Read of size 8 at addr 00000000868dac48 by task sh/1384 [ 44.505479] [ 44.505486] CPU: 51 UID: 0 PID: 1384 Comm: sh Not tainted 6.11.0-rc6-next-20240902-dirty #1496 [ 44.505503] Hardware name: IBM 3931 A01 704 (z/VM 7.3.0) [ 44.505508] Call Trace: [ 44.505511] [<000b0324d2f78080>] dump_stack_lvl+0xd0/0x108 [ 44.505521] [<000b0324d2f5435c>] print_address_description.constprop.0+0x34/0x2e0 [ 44.505529] [<000b0324d2f5464c>] print_report+0x44/0x138 [ 44.505536] [<000b0324d1383192>] kasan_report+0xc2/0x140 [ 44.505543] [<000b0324d2f52904>] special_mapping_close+0x9c/0xc8^^^^^^^^^^^^^^^^^^^^^Caused by
[PATCH v2 1/4] mm: Add optional close() to struct vm_special_mapping https://lore.kernel.org/all/[email protected]/?
Quoted 20 lines
kernel/events/uprobes.c +16
-0+static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1503
Quoted 4 lines
kernel/events/uprobes.c +1
-0@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state;LGTM.
Quoted 5 lines
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;?
I think the code should just use kzalloc() instead of kmalloc() so all members are cleared. I noticed that when looking into the issue because the new close member of xol_mapping wasn't initialized. My plan was to send that change as a separate patch - because it's not related to this issue.
Re: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Oleg NesterovQuoted 4 lines · Oleg Nesterov
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;
I think the whole thing needs to be zeroed out.
It was always horribly buggy. The close thing just made it more *obviously* buggy, because closing a vma is a lot more common than mremap'ing it.
Either use kzalloc(), or do a proper initializer something like this:
- area->xol_mapping.name = "[uprobes]";
- area->xol_mapping.fault = NULL;
- area->xol_mapping.pages = area->pages;
+ area->xol_mapping = (struct vm_special_mapping) {
+ .name = "[uprobes]",
+ .pages = area->pages,
+ .close = uprobe_clear_state,+ };
which should initialize the struct vm_special_mapping fully.
Linus
Sven Schnelle
reviewerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Hi Linus,
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Oleg Nesterov
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;
Quoted 7 lines
I think the whole thing needs to be zeroed out.
It was always horribly buggy. The close thing just made it more *obviously* buggy, because closing a vma is a lot more common than mremap'ing it.
Either use
kzalloc(), or do a proper initializer something like this:
I sent a patch which does this today:
Re: [PATCH] uprobes: use vm_special_mapping close() functionality
Quoted 4 lines · Sven Schnelle
Quoted 2 lines
Either use
kzalloc(), or do a proper initializer something like this:I sent a patch which does this today:
Ack, looks good.
This should probably be backported independently of the new close() series, since the mremap issue seems to be a longstanding issue.
Linus
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Linus TorvaldsQuoted 8 lines · Linus Torvalds
Quoted 4 lines · Oleg Nesterov
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;I think the whole thing needs to be zeroed out.
Agreed,
Quoted 1 line
Either use
kzalloc(),
This is what Sven did in
[PATCH] uprobes: use kzalloc to allocate xol area https://lore.kernel.org/all/[email protected]/
Oleg.
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Linus TorvaldsI didn't have time to write a full reply yesterday.
Quoted 8 lines · Linus Torvalds
Quoted 4 lines · Oleg Nesterov
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;I think the whole thing needs to be zeroed out.
Again, this is what Sven did and I agree with this fix for now.
Quoted 3 lines
It was always horribly buggy. The close thing just made it more *obviously* buggy, because closing a vma is a lot more common than mremap'ing it.
Well, this code is very old, I don't think it was "always" buggy. But at least today it is horribly ugly, and
Review
Quoted 10 lines
Either use
kzalloc(), or do a proper initializer something like this:(quoted diff) +5
-3- area->xol_mapping.name = "[uprobes]"; - area->xol_mapping.fault = NULL; - area->xol_mapping.pages = area->pages; + area->xol_mapping = (struct vm_special_mapping) { + .name = "[uprobes]", + .pages = area->pages, + .close = uprobe_clear_state, + };
either way the code is still ugly, imo.
How about the (untested) patch below?
I am not going to send this patch right now, it conflicts with the ongoing close/mremap changes, but what do you think?
We do not need to add the instance of vm_special_mapping into mm_struct, a single instance (xol_mapping below) with .fault = xol_fault() is enough. And, with this change xol_area no longer needs "struct page *pages[2]", it can be turned into "struct page *page".
Oleg.
---
kernel/events/uprobes.c +16 -5
@@ -101,7 +101,6 @@ struct xol_area {atomic_t slot_count;/* number of in-use slots */ unsigned long *bitmap;/* 0 = free slot */ -struct vm_special_mapping xol_mapping;struct page *pages[2 ];/* * We keep the vma's vm_start rather than a pointer to the vma
@@ -1453,6 +1452,21 @@ void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned lonset_bit (MMF_RECALC_UPROBES , &vma->vm_mm->flags); } +static vm_fault_t xol_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +struct xol_area *area = vma->vm_mm->uprobes_state.xol_area; + + vmf->page = area->pages[0 ]; +get_page (vmf->page); +return 0 ; +} + +static const struct vm_special_mapping xol_mapping = { + .name ="[uprobes]" , + .fault = xol_fault, +}; +/* Slot allocation for XOL */ static int xol_add_vma (struct mm_struct *mm,struct xol_area *area) {
@@ -1479,7 +1493,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) vma =_install_special_mapping (mm, area->vaddr,PAGE_SIZE ,VM_EXEC |VM_MAYEXEC |VM_DONTCOPY |VM_IO , - &area->xol_mapping); + &xol_mapping);if (IS_ERR (vma)) { ret =PTR_ERR (vma); goto fail;
@@ -1518,9 +1532,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)if (!area->bitmap) goto free_area; - area->xol_mapping.name ="[uprobes]" ; - area->xol_mapping.fault =NULL ; - area->xol_mapping.pages = area->pages; area->pages[0 ] =alloc_page (GFP_HIGHUSER );if (!area->pages[0 ]) goto free_bitmap;
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Linus TorvaldsI didn't have time to write a full reply yesterday.
Quoted 8 lines · Linus Torvalds
Quoted 4 lines · Oleg Nesterov
but with or without this fix
__create_xol_area()also needsarea->xol_mapping.mremap = NULL;I think the whole thing needs to be zeroed out.
Again, this is what Sven did and I agree with this fix for now.
Quoted 3 lines
It was always horribly buggy. The close thing just made it more *obviously* buggy, because closing a vma is a lot more common than mremap'ing it.
Well, this code is very old, I don't think it was "always" buggy. But at least today it is horribly ugly, and
Review
Quoted 10 lines
Either use
kzalloc(), or do a proper initializer something like this:(quoted diff) +5
-3- area->xol_mapping.name = "[uprobes]"; - area->xol_mapping.fault = NULL; - area->xol_mapping.pages = area->pages; + area->xol_mapping = (struct vm_special_mapping) { + .name = "[uprobes]", + .pages = area->pages, + .close = uprobe_clear_state, + };
either way the code is still ugly, imo.
How about the (untested) patch below?
I am not going to send this patch right now, it conflicts with the ongoing xol_mapping.close changes, but what do you think?
We do not need to add the instance of vm_special_mapping into mm_struct, a single instance (xol_mapping below) with .fault = xol_fault() is enough. And, with this change xol_area no longer needs "struct page *pages[2]", it can be turned into "struct page *page".
Oleg.
---
kernel/events/uprobes.c +16 -5
@@ -101,7 +101,6 @@ struct xol_area {atomic_t slot_count;/* number of in-use slots */ unsigned long *bitmap;/* 0 = free slot */ -struct vm_special_mapping xol_mapping;struct page *pages[2 ];/* * We keep the vma's vm_start rather than a pointer to the vma
@@ -1453,6 +1452,21 @@ void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned lonset_bit (MMF_RECALC_UPROBES , &vma->vm_mm->flags); } +static vm_fault_t xol_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +struct xol_area *area = vma->vm_mm->uprobes_state.xol_area; + + vmf->page = area->pages[0 ]; +get_page (vmf->page); +return 0 ; +} + +static const struct vm_special_mapping xol_mapping = { + .name ="[uprobes]" , + .fault = xol_fault, +}; +/* Slot allocation for XOL */ static int xol_add_vma (struct mm_struct *mm,struct xol_area *area) {
@@ -1479,7 +1493,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) vma =_install_special_mapping (mm, area->vaddr,PAGE_SIZE ,VM_EXEC |VM_MAYEXEC |VM_DONTCOPY |VM_IO , - &area->xol_mapping); + &xol_mapping);if (IS_ERR (vma)) { ret =PTR_ERR (vma); goto fail;
@@ -1518,9 +1532,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)if (!area->bitmap) goto free_area; - area->xol_mapping.name ="[uprobes]" ; - area->xol_mapping.fault =NULL ; - area->xol_mapping.pages = area->pages; area->pages[0 ] =alloc_page (GFP_HIGHUSER );if (!area->pages[0 ]) goto free_bitmap;
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
In reply to Sven SchnelleQuoted 21 lines · Sven Schnelle
kernel/events/uprobes.c +16
-0+static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;
kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1503
Quoted 4 lines
kernel/events/uprobes.c +1
-0@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state;
Ah, no, we can't do this :/
A malicious application can munmap() its "[uprobes]" vma and free area/pages/bitmap. If this application hits the uprobe breakpoint after that it will use the freed memory.
And no, "mm->uprobes_state.xol_area = NULL" in uprobe_clear_state() won't help. Say, another thread can sleep on area.wq when munmap() is called.
Sorry, I should have realized that immediately, but I didn't :/
Andrew, this is uprobes-use-vm_special_mapping-close-functionality.patch in mm-stable
Oleg.
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
I guess VM_SEALED could help, but it depends on CONFIG_64BIT
Quoted 43 lines · Oleg Nesterov
Quoted 21 lines · Sven Schnelle
kernel/events/uprobes.c +16
-0+static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1503
Quoted 4 lines
kernel/events/uprobes.c +1
-0@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state;Ah, no, we can't do this :/
A malicious application can
munmap()its "[uprobes]" vma and free area/pages/bitmap. If this application hits the uprobe breakpoint after that it will use the freed memory.And no, "
mm->uprobes_state.xol_area= NULL" inuprobe_clear_state()won't help. Say, another thread can sleep on area.wq whenmunmap()is called.Sorry, I should have realized that immediately, but I didn't :/
Andrew, this is uprobes-use-vm_special_mapping-close-functionality.patch in mm-stable
Oleg.
Oleg Nesterov
maintainerRe: [PATCH] uprobes: use vm_special_mapping close() functionality
Damn, sorry for the spam.
So I am going to send the patch from
https://lore.kernel.org/all/[email protected]/
To me it looks like a good cleanup regardless, and unless I am totally confused it should fix the problem with use-after-free.
Oleg.
Quoted 48 lines · Oleg Nesterov
I guess
VM_SEALEDcould help, but it depends onCONFIG_64BITQuoted 43 lines · Oleg Nesterov
Quoted 21 lines · Sven Schnelle
kernel/events/uprobes.c +16
-0+static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) +{ + struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , vma->vm_mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;kernel/events/uprobes.c static struct xol_area *__create_xol_area(unsigned long vaddr) · line 1503
Quoted 4 lines
kernel/events/uprobes.c +1
-0@@ -1481,6 +1500,7 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; + area->xol_mapping.close = uprobe_clear_state;Ah, no, we can't do this :/
A malicious application can
munmap()its "[uprobes]" vma and free area/pages/bitmap. If this application hits the uprobe breakpoint after that it will use the freed memory.And no, "
mm->uprobes_state.xol_area= NULL" inuprobe_clear_state()won't help. Say, another thread can sleep on area.wq whenmunmap()is called.Sorry, I should have realized that immediately, but I didn't :/
Andrew, this is uprobes-use-vm_special_mapping-close-functionality.patch in mm-stable
Oleg.
Oleg Nesterov
maintainerPATCH 1/3 Revert "uprobes: use vm_special_mapping close() functionality"
In reply to Sven SchnelleThis reverts commit 08e28de1160a712724268fd33d77b32f1bc84d1c.
A malicious application can munmap() its "[uprobes]" vma and in this case xol_mapping.close == uprobe_clear_state() will free the memory which can be used by another thread, or the same thread when it hits the uprobe bp afterwards.
--- include/linux/uprobes.h | 1 + kernel/events/uprobes.c | 36 +++++++++++++++++++----------------- kernel/fork.c | 1 + 3 files changed, 21 insertions(+), 17 deletions(-)
include/linux/uprobes.h +1 -0
@@ -126,6 +126,7 @@ extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);extern void uprobe_notify_resume (struct pt_regs *regs);extern bool uprobe_deny_signal (void );extern bool arch_uprobe_skip_sstep (struct arch_uprobe *aup,struct pt_regs *regs); +extern void uprobe_clear_state (struct mm_struct *mm);extern int arch_uprobe_analyze_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long addr);extern int arch_uprobe_pre_xol (struct arch_uprobe *aup,struct pt_regs *regs);extern int arch_uprobe_post_xol (struct arch_uprobe *aup,struct pt_regs *regs);
kernel/events/uprobes.c +19 -17
@@ -1482,22 +1482,6 @@ void * __weak arch_uprobe_trampoline(unsigned long *psize)return &insn; } -/* -* uprobe_clear_state - Free the area allocated for slots. -*/ -static void uprobe_clear_state (const struct vm_special_mapping *sm,struct vm_area_struct *vma) -{ - struct xol_area *area = container_of(vma->vm_private_data, struct xol_area, xol_mapping); - -mutex_lock (&delayed_uprobe_lock); -delayed_uprobe_remove (NULL , vma->vm_mm); -mutex_unlock (&delayed_uprobe_lock); - -put_page (area->pages[0 ]); -kfree (area->bitmap); -kfree (area); -} -static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm;
@@ -1516,7 +1500,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr) area->xol_mapping.name ="[uprobes]" ; area->xol_mapping.fault =NULL ; - area->xol_mapping.close = uprobe_clear_state; area->xol_mapping.pages = area->pages; area->pages[0 ] =alloc_page (GFP_HIGHUSER );if (!area->pages[0 ])
@@ -1562,6 +1545,25 @@ static struct xol_area *get_xol_area(void)return area; } +/* +* uprobe_clear_state - Free the area allocated for slots. +*/ +void uprobe_clear_state (struct mm_struct *mm) +{ +struct xol_area *area = mm->uprobes_state.xol_area; + +mutex_lock (&delayed_uprobe_lock); +delayed_uprobe_remove (NULL , mm); +mutex_unlock (&delayed_uprobe_lock); + +if (!area) +return ; + +put_page (area->pages[0 ]); +kfree (area->bitmap); +kfree (area); +} +void uprobe_start_dup_mmap (void ) {percpu_down_read (&dup_mmap_sem);
kernel/fork.c +1 -0
@@ -1338,6 +1338,7 @@ static inline void __mmput(struct mm_struct *mm) {VM_BUG_ON (atomic_read (&mm->mm_users)); +uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */
Signature · 1 line
2.25.1.362.g51ebf55
Oleg Nesterov
maintainerPATCH 2/3 uprobes: introduce the global struct vm_special_mapping xol_mapping
Currently each xol_area has its own instance of vm_special_mapping, this is suboptimal and ugly. Kill xol_area->xol_mapping and add a single global instance of vm_special_mapping, the ->fault() method can use area->pages rather than xol_mapping->pages.
As a side effect this fixes the problem introduced by the recent commit 223febc6e557 ("mm: add optional close() to struct vm_special_mapping"), if special_mapping_close() is called from the __mmput() paths, it will use vma->vm_private_data = &area->xol_mapping freed by uprobe_clear_state().
--- kernel/events/uprobes.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
kernel/events/uprobes.c +16 -5
@@ -99,7 +99,6 @@ struct xol_area {atomic_t slot_count;/* number of in-use slots */ unsigned long *bitmap;/* 0 = free slot */ -struct vm_special_mapping xol_mapping;struct page *pages[2 ];/* * We keep the vma's vm_start rather than a pointer to the vma
@@ -1433,6 +1432,21 @@ void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned lonset_bit (MMF_RECALC_UPROBES , &vma->vm_mm->flags); } +static vm_fault_t xol_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +struct xol_area *area = vma->vm_mm->uprobes_state.xol_area; + + vmf->page = area->pages[0 ]; +get_page (vmf->page); +return 0 ; +} + +static const struct vm_special_mapping xol_mapping = { + .name ="[uprobes]" , + .fault = xol_fault, +}; +/* Slot allocation for XOL */ static int xol_add_vma (struct mm_struct *mm,struct xol_area *area) {
@@ -1459,7 +1473,7 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) vma =_install_special_mapping (mm, area->vaddr,PAGE_SIZE ,VM_EXEC |VM_MAYEXEC |VM_DONTCOPY |VM_IO , - &area->xol_mapping); + &xol_mapping);if (IS_ERR (vma)) { ret =PTR_ERR (vma); goto fail;
@@ -1498,9 +1512,6 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)if (!area->bitmap) goto free_area; - area->xol_mapping.name ="[uprobes]" ; - area->xol_mapping.fault =NULL ; - area->xol_mapping.pages = area->pages; area->pages[0 ] =alloc_page (GFP_HIGHUSER );if (!area->pages[0 ]) goto free_bitmap;
Signature · 1 line
2.25.1.362.g51ebf55
Oleg Nesterov
maintainerPATCH 3/3 uprobes: turn xol_area->pages[2] into xol_area->page
In reply to Oleg NesterovNow that xol_mapping has its own ->fault() method we no longer need xol_area->pages[1] == NULL, we need a single page.
--- kernel/events/uprobes.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
kernel/events/uprobes.c +8 -9
@@ -99,7 +99,7 @@ struct xol_area {atomic_t slot_count;/* number of in-use slots */ unsigned long *bitmap;/* 0 = free slot */ -struct page *pages[2 ]; +struct page *page;/* * We keep the vma's vm_start rather than a pointer to the vma * itself. The probed process or a naughty kernel module could make
@@ -1437,7 +1437,7 @@ static vm_fault_t xol_fault(const struct vm_special_mapping *sm, {struct xol_area *area = vma->vm_mm->uprobes_state.xol_area; - vmf->page = area->pages[0 ]; + vmf->page = area->page;get_page (vmf->page);return 0 ; }
@@ -1512,10 +1512,9 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)if (!area->bitmap) goto free_area; - area->pages[0 ] =alloc_page (GFP_HIGHUSER ); -if (!area->pages[0 ]) + area->page =alloc_page (GFP_HIGHUSER ); +if (!area->page) goto free_bitmap; - area->pages[1 ] =NULL ; area->vaddr = vaddr;init_waitqueue_head (&area->wq);
@@ -1523,12 +1522,12 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)set_bit (0 , area->bitmap);atomic_set (&area->slot_count,1 ); insns =arch_uprobe_trampoline (&insns_size); -arch_uprobe_copy_ixol (area->pages[0 ],0 , insns, insns_size); +arch_uprobe_copy_ixol (area->page,0 , insns, insns_size);if (!xol_add_vma (mm, area))return area; -__free_page (area->pages[0 ]); +__free_page (area->page); free_bitmap:kfree (area->bitmap); free_area:
@@ -1570,7 +1569,7 @@ void uprobe_clear_state(struct mm_struct *mm)if (!area)return ; -put_page (area->pages[0 ]); +put_page (area->page);kfree (area->bitmap);kfree (area); }
@@ -1637,7 +1636,7 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)if (unlikely (!xol_vaddr))return 0 ; -arch_uprobe_copy_ixol (area->pages[0 ], xol_vaddr, +arch_uprobe_copy_ixol (area->page, xol_vaddr, &uprobe->arch.ixol,sizeof (uprobe->arch.ixol));return xol_vaddr;
Signature · 1 line
2.25.1.362.g51ebf55