Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

uprobes/x86: Make arch_uretprobe_is_alive(RP_CHECK_CALL) more clever

The previous change documents that cleanup_return_instances()
can't always detect the dead frames, the stack can grow. But
there is one special case which imho worth fixing:
arch_uretprobe_is_alive() can return true when the stack didn't
actually grow, but the next "call" insn uses the already
invalidated frame.

Test-case:

#include <stdio.h>
#include <setjmp.h>

jmp_buf jmp;
int nr = 1024;

void func_2(void)
{
if (--nr == 0)
return;
longjmp(jmp, 1);
}

void func_1(void)
{
setjmp(jmp);
func_2();
}

int main(void)
{
func_1();
return 0;
}

If you ret-probe func_1() and func_2() prepare_uretprobe() hits
the MAX_URETPROBE_DEPTH limit and "return" from func_2() is not
reported.

When we know that the new call is not chained, we can do the
more strict check. In this case "sp" points to the new ret-addr,
so every frame which uses the same "sp" must be dead. The only
complication is that arch_uretprobe_is_alive() needs to know was
it chained or not, so we add the new RP_CHECK_CHAIN_CALL enum
and change prepare_uretprobe() to pass RP_CHECK_CALL only if
!chained.

Note: arch_uretprobe_is_alive() could also re-read *sp and check
if this word is still trampoline_vaddr. This could obviously
improve the logic, but I would like to avoid another
copy_from_user() especially in the case when we can't avoid the
false "alive == T" positives.

Tested-by: Pratyush Anand <panand@redhat.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Acked-by: Anton Arapov <arapov@gmail.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/20150721134028.GA4786@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Oleg Nesterov and committed by
Ingo Molnar
db087ef6 86dcb702

+12 -8
+4 -1
arch/x86/kernel/uprobes.c
··· 989 989 bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, 990 990 struct pt_regs *regs) 991 991 { 992 - return regs->sp <= ret->stack; 992 + if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */ 993 + return regs->sp < ret->stack; 994 + else 995 + return regs->sp <= ret->stack; 993 996 }
+1
include/linux/uprobes.h
··· 104 104 105 105 enum rp_check { 106 106 RP_CHECK_CALL, 107 + RP_CHECK_CHAIN_CALL, 107 108 RP_CHECK_RET, 108 109 }; 109 110
+7 -7
kernel/events/uprobes.c
··· 1511 1511 return trampoline_vaddr; 1512 1512 } 1513 1513 1514 - static void cleanup_return_instances(struct uprobe_task *utask, struct pt_regs *regs) 1514 + static void cleanup_return_instances(struct uprobe_task *utask, bool chained, 1515 + struct pt_regs *regs) 1515 1516 { 1516 1517 struct return_instance *ri = utask->return_instances; 1517 - enum rp_check ctx = RP_CHECK_CALL; 1518 + enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL; 1518 1519 1519 1520 while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) { 1520 1521 ri = free_ret_instance(ri); ··· 1529 1528 struct return_instance *ri; 1530 1529 struct uprobe_task *utask; 1531 1530 unsigned long orig_ret_vaddr, trampoline_vaddr; 1532 - bool chained = false; 1531 + bool chained; 1533 1532 1534 1533 if (!get_xol_area()) 1535 1534 return; ··· 1555 1554 goto fail; 1556 1555 1557 1556 /* drop the entries invalidated by longjmp() */ 1558 - cleanup_return_instances(utask, regs); 1557 + chained = (orig_ret_vaddr == trampoline_vaddr); 1558 + cleanup_return_instances(utask, chained, regs); 1559 1559 1560 1560 /* 1561 1561 * We don't want to keep trampoline address in stack, rather keep the 1562 1562 * original return address of first caller thru all the consequent 1563 1563 * instances. This also makes breakpoint unwrapping easier. 1564 1564 */ 1565 - if (orig_ret_vaddr == trampoline_vaddr) { 1565 + if (chained) { 1566 1566 if (!utask->return_instances) { 1567 1567 /* 1568 1568 * This situation is not possible. Likely we have an ··· 1572 1570 uprobe_warn(current, "handle tail call"); 1573 1571 goto fail; 1574 1572 } 1575 - 1576 - chained = true; 1577 1573 orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; 1578 1574 } 1579 1575