get_unmapped_area && in_ia32_syscall (Was: [PATCH] uprobes/x86: fix detection of 32-bit user mode)
From: Oleg Nesterov
Date: Tue Aug 27 2019 - 10:01:02 EST
Sorry for delay, vacation.
On 08/24, Thomas Gleixner wrote:
>
> And sadly this was already mentioned here:
>
> 8faaed1b9f50 ("uprobes/x86: Introduce sizeof_long(), cleanup adjust_ret_addr() and arch_uretprobe_hijack_return_addr()")
Yes, and I even posted a similar fix but forgot to send it officially ...
Thanks Sebastian! I am sure it was not easy to debug this problem.
But to remind, there is another problem with in_ia32_syscall() && uprobes.
get_unmapped_area() paths use in_ia32_syscall() and this is wrong in case
when the caller is xol_add_vma(), in this case TS_COMPAT won't be set.
Usually the addr = TASK_SIZE - PAGE_SIZE passed to get_unmapped_area() should
work, mm->get_unmapped_area() won't be even called. But if this addr is already
occupied get_area() can return addr > TASK_SIZE.
Test-case:
#include <sys/mman.h>
void func(void)
{
}
int main(void)
{
// 0xffffd000 == TASK_SIZE - PAGE_SIZE
mmap((void*)0xffffd000, 4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1,0);
func();
return 0;
}
$ cc -m32 -Wall -g T.c -o ./t
$ perf probe -x ./t func+1 # +1 to avoid push_emulate_op()
$ perf record -e probe_t:func -aR ./t
perf-record "hangs" because ./t endlessly restarts the probed insn while
get_xol_area() can't succeed.
I verified that the "patch" below fixes the problem, any idea how to fix
it properly?
Oleg.
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1387,6 +1387,8 @@ void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned lon
set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
}
+#include <asm/mmu_context.h>
+
/* Slot allocation for XOL */
static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
{
@@ -1402,9 +1404,13 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
}
if (!area->vaddr) {
+ if(!is_64bit_mm(mm))
+ current_thread_info()->status |= TS_COMPAT;
/* Try to map as high as possible, this is only a hint. */
area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
PAGE_SIZE, 0, 0);
+ if(!is_64bit_mm(mm))
+ current_thread_info()->status &= ~TS_COMPAT;;
if (area->vaddr & ~PAGE_MASK) {
ret = area->vaddr;
goto fail;