Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp6130318pxv; Thu, 29 Jul 2021 07:13:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4/+Lx3PPaFv/LQpl94gEEXFM7muzsBHFdiuvXDMyE7ZNDo0a04NyJfT8ynacffeoTqfSr X-Received: by 2002:a92:6805:: with SMTP id d5mr3745763ilc.286.1627568014213; Thu, 29 Jul 2021 07:13:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627568014; cv=none; d=google.com; s=arc-20160816; b=HCdc7HPbe5Yk5eAkoyU2rmpB8HsZle8gUkSwoDa+NsQVBmZ8BbHWKHsfWQXYVn1YQW ebOACR5hCm4krciYEt9rEo32aDBQ1/YTlbervJBXjDC2isvtGwOgXIFe7NrafkSUbXBC RuK7Pl/EImWTyeQ6Nxl1N4uh53ml+EvzSnHtAsFDh7l12z6OO/Mqin9jprh08Mxakb7I k6s93cIkHK0MC51Mph7MaPK6Nz8HeLtl3xwsQ5KMOnAjw7l6AJwxR8ENhCiGBem+HYRe 3C3x+cptjw1wTaDM1k+bBSKjXti8wyac548r4Hf7Cwq6YQcWJBz6y9qQtRchs6IZZ+pR P6gw== 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=75dB2kP9VJT1ttDw+5f0w5jB8ebpPVC0rGe96BUkvPY=; b=vF/LdV/BDryUHfz4+9xH+zRs8ndIQ7/XW0nqj0Wb5ia953qhb3f0qglTzHpz4tK+Sl 7G2mipVgvKGHy7fClj3jyfnQhHUmsgUIPSd4B6gNcKQ8eCI5piHz0Fs0HA+pMERJVz4l qnxFQjZvHRo6fhqrH9ngkv5sL9LQRl/1XgNuJm4ni4VI/xI0GQ6iLuVFJyjrEJHQW21q 9YagXBS7bsDuckHwDrLqBYjfey5AySqc0ldd/CugLdM+sa1CKru2EYG3WlNVvvy0NrIf 1eqt+Wz/gK5MZ0NTUTT/YdGt7iS451npa0hpN1w2q9177xpoiiepXXWm7Rp5D3Yu07Fz Bcqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=F6jIp4PS; 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 v5si3809268jan.37.2021.07.29.07.13.22; Thu, 29 Jul 2021 07:13:34 -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=F6jIp4PS; 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 S239094AbhG2OKH (ORCPT + 99 others); Thu, 29 Jul 2021 10:10:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:57374 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238877AbhG2OIF (ORCPT ); Thu, 29 Jul 2021 10:08:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BB5B160EBD; Thu, 29 Jul 2021 14:07:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627567682; bh=b5f2P/y2shO+S47LUkQKIDybO3DPdanNyRLlemEbyJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F6jIp4PSzC0dAghIWd5eNkMSkrKI9BWHTnI/eidIH/dRdhqlrInpzCbBCTH5Ghzjp xdg9Pthx75BSCXcJ/JtZs5aKd5VXPX/l2jqdn+gyuL60ZIkA1Vr326u9/JtryhnWCM vTDFOtFRzLoLgoao++F1nR90CpWmnzfOijJJGm2sb7LdNBbTQCrY8fQPxk7LlSmsH4 0p/3wVaCDRmFWmmDoV0Y9sxFvcQacRuBsUmFF0TLTUzs00Gn9H/8O/SU6Ss1wnwijO 0JuibN1Cdaa7mLHPX57nug8y8VdaMO5WLIeq6du4DYl0GrfAovDkUieWZOc1wL0iRA uxXdo12iiNTCw== From: Masami Hiramatsu To: Steven Rostedt , Josh Poimboeuf , 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, Thomas Gleixner , Borislav Petkov , Peter Zijlstra , kernel-team@fb.com, yhs@fb.com, linux-ia64@vger.kernel.org, Abhishek Sagar , Andrii Nakryiko Subject: [PATCH -tip v10 13/16] x86/kprobes: Push a fake return address at kretprobe_trampoline Date: Thu, 29 Jul 2021 23:07:58 +0900 Message-Id: <162756767828.301564.16335053369674561101.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <162756755600.301564.4957591913842010341.stgit@devnote2> References: <162756755600.301564.4957591913842010341.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 Change __kretprobe_trampoline() to push the address of the __kretprobe_trampoline() as a fake return address at the bottom of the stack frame. This fake return address will be replaced with the correct return address in the trampoline_handler(). With this change, the ORC unwinder can check whether the return address is modified by kretprobes or not. Signed-off-by: Masami Hiramatsu Suggested-by: Josh Poimboeuf Tested-by: Andrii Nakryiko Acked-by: Josh Poimboeuf --- Changes in v9: - Update changelog and comment. - Remove unneeded type casting. --- arch/x86/kernel/kprobes/core.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index d1436d7463fd..7e1111c19605 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -1022,28 +1022,33 @@ 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 a 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 + /* In trampoline_handler(), 'regs->flags' is copied to 'regs->sp'. */ + " addq $8, %rsp\n" " popfq\n" #else - " pushl %esp\n" + /* Push a 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 + /* In trampoline_handler(), 'regs->flags' is copied to 'regs->sp'. */ + " addl $4, %esp\n" " popfl\n" #endif " ret\n" @@ -1063,8 +1068,10 @@ STACK_FRAME_NON_STANDARD_FP(__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 @@ -1072,8 +1079,17 @@ __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 = ®s->sp + 1; + + /* Replace fake return address with real one. */ + *frame_pointer = kretprobe_trampoline_handler(regs, frame_pointer); - return (void *)kretprobe_trampoline_handler(regs, ®s->sp); + /* + * Copy FLAGS to 'pt_regs::sp' so that __kretprobe_trapmoline() + * can do RET right after POPF. + */ + regs->sp = regs->flags; } NOKPROBE_SYMBOL(trampoline_handler);