Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5175348pxj; Wed, 26 May 2021 04:48:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/CGIeiSGAxH5F/FaSQ39iWvNgPmXXJCINiJsfeG+VSKef2tffvvLGHR597ba7FJMrilJD X-Received: by 2002:a92:50c:: with SMTP id q12mr24783287ile.166.1622029705978; Wed, 26 May 2021 04:48:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622029705; cv=none; d=google.com; s=arc-20160816; b=JMKjBWdOalav1+rQHCrbB3Iz6esWsEEqQ3DfIuse5Ku7key0Ax9JV3g5IQHfdpY2Ax XuPfsA14g0wSTYhZx/2cHQcP14ZVgh9VgI9Gf7c6zC40lg3e7h8bkdis+w8QM27/V1Mc EsvMu2VFG597kEcRSUuhZquRQ02c2L64COHipjbcNEQyMkxVsK/ZEOk0z5btra/1j1Np y+fse7ZW4SDQQlVmbViiYWHCNdgINMR4130Pja/c20kpJzYPlFM+fU5gqHe44XaYGibq F+4KYaKL5GISWBWTkTT7bKuPFpRXjdjlPcSaf83O/muMlSwTpOUWxWybwHJg66fhYbd0 b7FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JJ3fHwP3hVPZvgjaTdlW9K5kXuLoKhJuGEQ2IEH9N0g=; b=Ri0xIB48pexyVxmiv+brkzGFftyuXK/LtKl6oqZ4Lv/+uALcEkH9oFFsn1abeA2l50 arNtGWQ5iBPxiZ5aP/6uIP23FTqQI29FnuPODmdu14/fSMNtChQmGQAGqeZUhYG2X5rZ qnqxO5lyPrS4aStvKL/QH0ihXA1u56iDEnUzB5aLwR+C+B0f6jbJgeRjDBFoSxM+G9Yc 6YBQVExxnVh4/qRj2ytrjwFNI//+3wbOT6311q+vnQvWH3lgujbFmONiMDiZrs5fSqlH xxkDHG7MyVlPQI0xYcr4Xmuavw/fEjRhkftO86J/laRdRTLqwv3IHYarHsY07lBxWhXX NdzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="oYlicS6/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t188si18034096jaa.52.2021.05.26.04.48.11; Wed, 26 May 2021 04:48:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="oYlicS6/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233253AbhEZIF3 (ORCPT + 99 others); Wed, 26 May 2021 04:05:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:40646 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232614AbhEZIF3 (ORCPT ); Wed, 26 May 2021 04:05:29 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C9617613D3; Wed, 26 May 2021 08:03:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622016238; bh=YoSuzmD4HYDIR1nxiMTmGqdnaU88RgVp1FEKUAPIIT8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oYlicS6/iDFBAvmAn9Gu2xJRK7jrGs/rm+lWraQ9PdELYQVhTKlZBjdSFfqnbp01T i1LrrGfaiQ/syIXYrfZixfBFDUfqpsVr9QTsjAhQsjNQnBDnpUvxCq0PgPBGU4JfhC dGkn8yv+9ZGXsvSao/4UwzyhvZiAMNi/JnmIyiUQjXT4QtYMXO10DfJU656KC0Q8Lu y9jo6luKCRV+R3BgYS4xqn+Wtn1qeaDdKwMl8P7KTR067n4JU8DbO6IB1Ng+DZB5Kr tDBaKBczqsb8k/o5KaWffh6hZe1HSA9BeCzbIoG9WJ+tCGf9NmVxml8WIzvtqfaU65 N7va3YPUqeYYQ== From: Masami Hiramatsu To: Steven Rostedt , Ingo Molnar Cc: X86 ML , Masami Hiramatsu , Daniel Xu , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, kuba@kernel.org, mingo@redhat.com, ast@kernel.org, tglx@linutronix.de, kernel-team@fb.com, yhs@fb.com, Josh Poimboeuf , linux-ia64@vger.kernel.org, Abhishek Sagar , Andrii Nakryiko Subject: [PATCH -tip v6 10/13] x86/kprobes: Push a fake return address at kretprobe_trampoline Date: Wed, 26 May 2021 17:03:52 +0900 Message-Id: <162201623241.278331.4061034283741994264.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <162201612941.278331.5293566981784464165.stgit@devnote2> References: <162201612941.278331.5293566981784464165.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This changes x86/kretprobe stack frame on kretprobe_trampoline a bit, which now push the kretprobe_trampoline as a fake return address at the bottom of the stack frame. With this fix, the ORC unwinder will see the kretprobe_trampoline as a return address. Signed-off-by: Masami Hiramatsu Suggested-by: Josh Poimboeuf --- arch/x86/kernel/kprobes/core.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 9a6763fd066e..4f3567a9974f 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -1041,28 +1041,31 @@ asm( ".global kretprobe_trampoline\n" ".type kretprobe_trampoline, @function\n" "kretprobe_trampoline:\n" - /* We don't bother saving the ss register */ #ifdef CONFIG_X86_64 - " pushq %rsp\n" + /* Push fake return address to tell the unwinder it's a kretprobe */ + " pushq $kretprobe_trampoline\n" UNWIND_HINT_FUNC + /* Save the sp-8, this will be fixed later */ + " pushq %rsp\n" " pushfq\n" SAVE_REGS_STRING " movq %rsp, %rdi\n" " call trampoline_handler\n" - /* Replace saved sp with true return address. */ - " movq %rax, 19*8(%rsp)\n" RESTORE_REGS_STRING + " addq $8, %rsp\n" " popfq\n" #else - " pushl %esp\n" + /* Push fake return address to tell the unwinder it's a kretprobe */ + " pushl $kretprobe_trampoline\n" UNWIND_HINT_FUNC + /* Save the sp-4, this will be fixed later */ + " pushl %esp\n" " pushfl\n" SAVE_REGS_STRING " movl %esp, %eax\n" " call trampoline_handler\n" - /* Replace saved sp with true return address. */ - " movl %eax, 15*4(%esp)\n" RESTORE_REGS_STRING + " addl $4, %esp\n" " popfl\n" #endif " ret\n" @@ -1073,8 +1076,10 @@ NOKPROBE_SYMBOL(kretprobe_trampoline); /* * Called from kretprobe_trampoline */ -__used __visible void *trampoline_handler(struct pt_regs *regs) +__used __visible void trampoline_handler(struct pt_regs *regs) { + unsigned long *frame_pointer; + /* fixup registers */ regs->cs = __KERNEL_CS; #ifdef CONFIG_X86_32 @@ -1082,8 +1087,16 @@ __used __visible void *trampoline_handler(struct pt_regs *regs) #endif regs->ip = (unsigned long)&kretprobe_trampoline; regs->orig_ax = ~0UL; + regs->sp += sizeof(long); + frame_pointer = ((unsigned long *)®s->sp) + 1; - return (void *)kretprobe_trampoline_handler(regs, ®s->sp); + /* Replace fake return address with real one. */ + *frame_pointer = kretprobe_trampoline_handler(regs, frame_pointer); + /* + * Move flags to sp so that kretprobe_trapmoline can return + * right after popf. + */ + regs->sp = regs->flags; } NOKPROBE_SYMBOL(trampoline_handler);