From patchwork Sat Oct 22 07:27:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7626 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1107496wrr; Sat, 22 Oct 2022 01:41:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6lPFpGa9tDqX34aSXN3+B2IejjrBMZow8eZPQ5stAz4Z6hoic+x15EWKv3sqbAqSBSe6jJ X-Received: by 2002:a17:907:a06b:b0:78d:d25f:b726 with SMTP id ia11-20020a170907a06b00b0078dd25fb726mr19318473ejc.203.1666428066715; Sat, 22 Oct 2022 01:41:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666428066; cv=none; d=google.com; s=arc-20160816; b=RhvY6Gu6/MEgYmRkAA5Pgv/NBnBUrb7D2G6J+63URa0GXxkJ2Lxi1BCr8nRDukHZB+ wTVFAV1UEvlwblvhGzaC47nBl8qjthASyB7eTjADcs/iz6pFW/P8Ur2KJWJNb+hdzWHb oppfuhqFDmVsdw5QxRKYa3DbglXdQuVcz8lmkO25uHSwm0RUlheLJ2kLXLtSxZKzEsml 9mL59a6nC/Bjz+NYWU/c/9OR2yc0ipme0YlbEWzoXLSw50xzMd3WDZeS+rTyFiCqNigj qxzHsjPnyS/AgDm8F05qoLjlWeq8HnZYfo/Ag8ZOH4+2wcl8U0cnLaWrFl/1jqH+2d5Y znsQ== 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=sCVYOWXp+0BSF9HTNK4AnTPesPRqj5RUvsuMbTRptno=; b=Dt4ds+i4mMH46cgUhLcOop2gTE/9azkJ16LdIXImxwhleqpRP8w8couFsplIYNH2kc 1/rbX1DwEOJQgVNqihx1clwUFROt4BQVGdfpIo1nrtiC34YiUn8OZ+wOH/YsPCkPw9C6 ey0/IPzsQ7npPHakJRjaxji3vlt8wVIB+6sHjScfqqWXYAmGxjoFU5+GVhN72Ns730c/ tod9qI5YtNkTjCxlI2Dmb6I+9dsSwdYbKU/qykcbqwJZYGoLQG5NqfCm3BGB6Sngv5lh 5h93JTeSxb7SYSh4NjM+Q9Xtw66zeyPjUSo2JWgM9h5/l4UyiPOcpKKFVvPihU5orvyA LduA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YZaEH6RT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i9-20020a0564020f0900b0046074707fc5si7134665eda.163.2022.10.22.01.40.38; Sat, 22 Oct 2022 01:41:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YZaEH6RT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234384AbiJVIhs (ORCPT + 99 others); Sat, 22 Oct 2022 04:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233959AbiJVIb7 (ORCPT ); Sat, 22 Oct 2022 04:31:59 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 937782D1285; Sat, 22 Oct 2022 01:03:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B8AD9B82DEE; Sat, 22 Oct 2022 08:03:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAB11C433D6; Sat, 22 Oct 2022 08:03:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666425807; bh=73g0JsSReowuNUCn5pgQKkzbNbSYaz9WNv4ymMmfb/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YZaEH6RTyOT9DOpgoaD2qImb/ekLbMtp1bYxspTVrpDaKcjQ4cO6K0GjHsAdeg4Sa sSbE/buFdZjj0ZG4W6P8eDcUE98/2EN/smaPQvuKJrcIviUmmehbwX2KpZ5s/OZBp0 uLpChOal7ZFW3CnK51Q2xL+jXA1eKye6Ha0Bh/Dk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Zijlstra , Martynas Pumputis , Andrii Nakryiko , Jiri Olsa , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.19 593/717] bpf: Adjust kprobe_multi entry_ip for CONFIG_X86_KERNEL_IBT Date: Sat, 22 Oct 2022 09:27:52 +0200 Message-Id: <20221022072524.669631658@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747376476471423963?= X-GMAIL-MSGID: =?utf-8?q?1747376476471423963?= From: Jiri Olsa [ Upstream commit c09eb2e578eb1668bbc84dc07e8d8bd6f04b9a02 ] Martynas reported bpf_get_func_ip returning +4 address when CONFIG_X86_KERNEL_IBT option is enabled. When CONFIG_X86_KERNEL_IBT is enabled we'll have endbr instruction at the function entry, which screws return value of bpf_get_func_ip() helper that should return the function address. There's short term workaround for kprobe_multi bpf program made by Alexei [1], but we need this fixup also for bpf_get_attach_cookie, that returns cookie based on the entry_ip value. Moving the fixup in the fprobe handler, so both bpf_get_func_ip and bpf_get_attach_cookie get expected function address when CONFIG_X86_KERNEL_IBT option is enabled. Also renaming kprobe_multi_link_handler entry_ip argument to fentry_ip so it's clearer this is an ftrace __fentry__ ip. [1] commit 7f0059b58f02 ("selftests/bpf: Fix kprobe_multi test.") Cc: Peter Zijlstra Reported-by: Martynas Pumputis Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa Link: https://lore.kernel.org/r/20220926153340.1621984-5-jolsa@kernel.org Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/trace/bpf_trace.c | 20 +++++++++++++++++-- .../selftests/bpf/progs/kprobe_multi.c | 4 +--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 88589d74a892..af13fdf1d86c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1026,6 +1026,22 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { .arg1_type = ARG_PTR_TO_CTX, }; +#ifdef CONFIG_X86_KERNEL_IBT +static unsigned long get_entry_ip(unsigned long fentry_ip) +{ + u32 instr; + + /* Being extra safe in here in case entry ip is on the page-edge. */ + if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1)) + return fentry_ip; + if (is_endbr(instr)) + fentry_ip -= ENDBR_INSN_SIZE; + return fentry_ip; +} +#else +#define get_entry_ip(fentry_ip) fentry_ip +#endif + BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs) { struct kprobe *kp = kprobe_running(); @@ -2414,13 +2430,13 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, } static void -kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip, +kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, struct pt_regs *regs) { struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - kprobe_multi_link_prog_run(link, entry_ip, regs); + kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs); } static int symbols_cmp_r(const void *a, const void *b, const void *priv) diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi.c b/tools/testing/selftests/bpf/progs/kprobe_multi.c index 08f95a8155d1..98c3399e15c0 100644 --- a/tools/testing/selftests/bpf/progs/kprobe_multi.c +++ b/tools/testing/selftests/bpf/progs/kprobe_multi.c @@ -36,15 +36,13 @@ __u64 kretprobe_test6_result = 0; __u64 kretprobe_test7_result = 0; __u64 kretprobe_test8_result = 0; -extern bool CONFIG_X86_KERNEL_IBT __kconfig __weak; - static void kprobe_multi_check(void *ctx, bool is_return) { if (bpf_get_current_pid_tgid() >> 32 != pid) return; __u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0; - __u64 addr = bpf_get_func_ip(ctx) - (CONFIG_X86_KERNEL_IBT ? 4 : 0); + __u64 addr = bpf_get_func_ip(ctx); #define SET(__var, __addr, __cookie) ({ \ if (((const void *) addr == __addr) && \