Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3918140rwi; Sat, 22 Oct 2022 02:11:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5kVwzeJHjkGha7NYqeuVsdefBQ0y6Tf1/0ofMfGYR4OyM9BMU9MlQUvwMNaCUMcOz3wD95 X-Received: by 2002:a17:90b:1b4d:b0:20d:7bbf:46f0 with SMTP id nv13-20020a17090b1b4d00b0020d7bbf46f0mr27009472pjb.77.1666429867669; Sat, 22 Oct 2022 02:11:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666429867; cv=none; d=google.com; s=arc-20160816; b=PHZcajGa2lPTf00qPFJ297eRytFc9wz1z3csk4JkkrgH1Uspo4YqWa3b/1FlYv4L0c wPoHKvgx8aAtWADsZX0VKeubgl/pmVe8p2hLUuES1pI3iV0XqfGgnTCo5w0sh+PeePp0 Wmioj3LiH+APylF2nDmCXT48jCphQgzNuZOssvVeHwAGjbklkC2x6XhwRSIFrCJknPLc GrFtKNW/MlVWP6zvwvVqiO+KPP2I8RtLPl7cMh+njLbuoSFQxlExK2wvyrBQV5ZEI9KC ZPogL4yJ94KmssviPJPVuflPt3NVPUKj9Qvn88sPxazPGyAJgHh9vd2NiuIIPSqxtuk0 w6rA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a170902748a00b001865c55049dsi9728251pll.570.2022.10.22.02.10.55; Sat, 22 Oct 2022 02:11:07 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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) && \ -- 2.35.1