From patchwork Fri Jun 9 00:10:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krister Johansen X-Patchwork-Id: 691031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92F15C7EE25 for ; Fri, 9 Jun 2023 00:11:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237546AbjFIALU (ORCPT ); Thu, 8 Jun 2023 20:11:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237175AbjFIALT (ORCPT ); Thu, 8 Jun 2023 20:11:19 -0400 Received: from antelope.pear.relay.mailchannels.net (antelope.pear.relay.mailchannels.net [23.83.216.4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29F972733 for ; Thu, 8 Jun 2023 17:11:14 -0700 (PDT) X-Sender-Id: dreamhost|x-authsender|kjlx@templeofstupid.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id E02066411C4 for ; Fri, 9 Jun 2023 00:11:05 +0000 (UTC) Received: from pdx1-sub0-mail-a313.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 7F311640EA9 for ; Fri, 9 Jun 2023 00:11:05 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1686269465; a=rsa-sha256; cv=none; b=1cliiNdK3/rs7W8X9KsdEpAaduikx/zZQWQ1BabLyAW/2nWZhTo2NRfy4COxjKu+m7XOG7 Rypmyn2fFgzjZqCmmgMqGyr7jO1wTYaXMlboZgVcZftYPF2Q6DNtzCMtw7+JFMwLeo6tIf AgN5yA8k3xy2psYGzvN9B+PaIjnYbu0O2YQYqphX9hPbQWowq3Hp2YIufWmOZUtm1lKrK/ EVy60JS7JCV4a/VyOHWCxB6uSxs4gQxGAyDNU0aaeK1lx6AeWx5iMTY1vi8wLvoBsVQ3gn 7VsJG8jtM7FgR/H4uvJQoyRrbJBRNOsNhh2/g29RvLY5PYaH6dG6AejQZFVO6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1686269465; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RLbI/q+jNwtC3CHXt0I4xW5xpj8BgznW9VU/aHNxfd0=; b=HexMjbvxpHyuLkF9YWQYL3PchkOqrPDgjfzd60e4OjmFxbBjfSBLfSm1AWO9msnw2Vbmqw byUbrTYQF1Glr224+Wn19BfEYytth0Ne6dRMpuHdwfn4WrDYTGfLV8hq/XhsIfq2i5I2Zf a6XiKpYaM3uZ1jZxbuOp3Pptt3FT3Oe282Hc755v4qG/YnqI5wV4HE5AeEdFEQXxlKMzVE N7jebehfiiCWIsAQUP4zSJIg1d8/Yyn+JwE/Qmhpu08Yf+zOB6Ug0ch5x9EFhUdH24xW5B 2/5WSFrCtyFmPbIBGDaq/UO2EuDkIjRkiKDFn17Ou/zD1eRYQ77baZCiWBXRHg== ARC-Authentication-Results: i=1; rspamd-6f5cfd578c-jwvbw; auth=pass smtp.auth=dreamhost smtp.mailfrom=kjlx@templeofstupid.com X-Sender-Id: dreamhost|x-authsender|kjlx@templeofstupid.com X-MC-Relay: Good X-MailChannels-SenderId: dreamhost|x-authsender|kjlx@templeofstupid.com X-MailChannels-Auth-Id: dreamhost X-Soft-Broad: 13f2a5f569096219_1686269465749_424654662 X-MC-Loop-Signature: 1686269465749:1977246838 X-MC-Ingress-Time: 1686269465748 Received: from pdx1-sub0-mail-a313.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.126.30.49 (trex/6.8.1); Fri, 09 Jun 2023 00:11:05 +0000 Received: from kmjvbox (c-73-93-64-36.hsd1.ca.comcast.net [73.93.64.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: kjlx@templeofstupid.com) by pdx1-sub0-mail-a313.dreamhost.com (Postfix) with ESMTPSA id 4QchLq44TGz1qH for ; Thu, 8 Jun 2023 17:10:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=templeofstupid.com; s=dreamhost; t=1686269459; bh=RLbI/q+jNwtC3CHXt0I4xW5xpj8BgznW9VU/aHNxfd0=; h=Date:From:To:Cc:Subject:Content-Type; b=ZV0VvWueE8CSb7L2iDzEP4wufwbaN5u4R5zhLLdIi03X5t1BN8Pzu1tuw3tEoZ3f8 77ENKM53Zmq9z1rOh4wgY5Mc22JDFdBNEvO3Tj4bqkCB60f8RfYKgcPhayh1EsGh1V yyjlFVGp+pigziGilygQhjOMvqfaBlUMXHQbtyrs= Received: from johansen (uid 1000) (envelope-from kjlx@templeofstupid.com) id e0042 by kmjvbox (DragonFly Mail Agent v0.12); Thu, 08 Jun 2023 17:10:58 -0700 Date: Thu, 8 Jun 2023 17:10:58 -0700 From: Krister Johansen To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 1/2] bpf: ensure main program has an extable Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When subprograms are in use, the main program is not jit'd after the subprograms because jit_subprogs sets a value for prog->bpf_func upon success. Subsequent calls to the JIT are bypassed when this value is non-NULL. This leads to a situation where the main program and its func[0] counterpart are both in the bpf kallsyms tree, but only func[0] has an extable. Extables are only created during JIT. Now there are two nearly identical program ksym entries in the tree, but only one has an extable. Depending upon how the entries are placed, there's a chance that a fault will call search_extable on the aux with the NULL entry. Since jit_subprogs already copies state from func[0] to the main program, include the extable pointer in this state duplication. Additionally, ensure that the copy of the main program in func[0] is not added to the bpf_prog_kallsyms table. Instead, let the main program get added later in bpf_prog_load(). This ensures there is only a single copy of the main program in the kallsyms table, and that its tag matches the tag observed by tooling like bpftool. Cc: stable@vger.kernel.org Fixes: 1c2a088a6626 ("bpf: x64: add JIT support for multi-function programs") Signed-off-by: Krister Johansen Acked-by: Yonghong Song --- kernel/bpf/verifier.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 5871aa78d01a..b62d1fc0f92b 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17214,9 +17214,10 @@ static int jit_subprogs(struct bpf_verifier_env *env) } /* finally lock prog and jit images for all functions and - * populate kallsysm + * populate kallsysm. Begin at the first subprogram, since + * bpf_prog_load will add the kallsyms for the main program. */ - for (i = 0; i < env->subprog_cnt; i++) { + for (i = 1; i < env->subprog_cnt; i++) { bpf_prog_lock_ro(func[i]); bpf_prog_kallsyms_add(func[i]); } @@ -17242,6 +17243,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) prog->jited = 1; prog->bpf_func = func[0]->bpf_func; prog->jited_len = func[0]->jited_len; + prog->aux->extable = func[0]->aux->extable; prog->aux->func = func; prog->aux->func_cnt = env->subprog_cnt; bpf_prog_jit_attempt_done(prog); From patchwork Fri Jun 9 00:11:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krister Johansen X-Patchwork-Id: 691590 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06CDAC7EE25 for ; Fri, 9 Jun 2023 00:11:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237714AbjFIALh (ORCPT ); Thu, 8 Jun 2023 20:11:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237175AbjFIALg (ORCPT ); Thu, 8 Jun 2023 20:11:36 -0400 X-Greylist: delayed 97629 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 08 Jun 2023 17:11:35 PDT Received: from grey.apple.relay.mailchannels.net (grey.apple.relay.mailchannels.net [23.83.208.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FBCF2733 for ; Thu, 8 Jun 2023 17:11:35 -0700 (PDT) X-Sender-Id: dreamhost|x-authsender|kjlx@templeofstupid.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 8607A761DAC for ; Fri, 9 Jun 2023 00:11:34 +0000 (UTC) Received: from pdx1-sub0-mail-a313.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 2716E761E5A for ; Fri, 9 Jun 2023 00:11:34 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1686269494; a=rsa-sha256; cv=none; b=xb62SB+Z6itwq5yqKRc8ZZCGrcz4QkYRFRVU27vH3SGrGgd45NIV80y4HRXsZp7hjYUgqj Qu/S9BXemPH71mW/qPhmvbHHSF7UzybbKjU05usBXx+IRjSwYZbwirJDQzmdvs+/7ehqAE xBgSfDnXcNNdng76EjybRyIwSViLhFrmWP7OBUfnZAkCK0hYgbNIYhOLlDor2CYBxiS8VP NI82jB2IX2c8+edfrNgzqpa35iXD/p9FZ3ApDLTPflNUPQvm1uOu+EJH2hR9iqIT6Qa8SY wEUOhIehIGpFvCzxrHvSnxb0LJKG7e8Rg0P/2u+HNKfk4YKVYRc6OEoYDsAqlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1686269494; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F8KPdDJs/fafqK8befrWeYqM6/tlnupScz2H0Ba6c8I=; b=002Celn2SQNn+gaExPaMbicv+D07wNk4E2TlpzoFNdC3CIN4pWD2haBE6e+spyFgzIss7j ChwPEDFJ9xkeHtMxTh3NoQpIVI8Md2uR0UkGeP9CwHwmvzzcA0PvJND5UGKDrAgFSrrA88 MerLjCIWo9q6zpaTb33eOWeZgvlZsiada3ZwdsW0VYzmG5FmJbJ+Ww4rm2sFo9OkrsmKTc 5es88CsguDBeA0Sg0T2dHZAtKOnRt3Ghgq5sPqtqeoou+wG2JUZYK21F846GZEj5kWtxv0 TfUE6YsNdirNVpaYmkvdiROK8Bm7MLwqdCxbEwzn7IX0vXOu/h7TKrcmTBQqjw== ARC-Authentication-Results: i=1; rspamd-6f5cfd578c-lcvt2; auth=pass smtp.auth=dreamhost smtp.mailfrom=kjlx@templeofstupid.com X-Sender-Id: dreamhost|x-authsender|kjlx@templeofstupid.com X-MC-Relay: Good X-MailChannels-SenderId: dreamhost|x-authsender|kjlx@templeofstupid.com X-MailChannels-Auth-Id: dreamhost X-Average-Shelf: 0439237d02c690e5_1686269494372_2842711401 X-MC-Loop-Signature: 1686269494372:2304675126 X-MC-Ingress-Time: 1686269494372 Received: from pdx1-sub0-mail-a313.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.116.217.228 (trex/6.8.1); Fri, 09 Jun 2023 00:11:34 +0000 Received: from kmjvbox (c-73-93-64-36.hsd1.ca.comcast.net [73.93.64.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: kjlx@templeofstupid.com) by pdx1-sub0-mail-a313.dreamhost.com (Postfix) with ESMTPSA id 4QchMS6nRBz1qT for ; Thu, 8 Jun 2023 17:11:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=templeofstupid.com; s=dreamhost; t=1686269493; bh=F8KPdDJs/fafqK8befrWeYqM6/tlnupScz2H0Ba6c8I=; h=Date:From:To:Cc:Subject:Content-Type; b=TXEaB1btLsjRXcmw0020YGDMNtCdMGH1/YMEqiFvR2URzu4xCtonXa+J5TayPvZjc rYoI7NRg9IqKSiDx3fcqjyBaKesd54Bvzb8ZhIylBY7DD1dXsO2uvEBovuS7nTDnjI 7SxmsQjiKVX3p45ilpYXsbrINRR5tLj8NMGF4BLs= Received: from johansen (uid 1000) (envelope-from kjlx@templeofstupid.com) id e0042 by kmjvbox (DragonFly Mail Agent v0.12); Thu, 08 Jun 2023 17:11:31 -0700 Date: Thu, 8 Jun 2023 17:11:31 -0700 From: Krister Johansen To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 2/2] selftests/bpf: add a test for subprogram extables Message-ID: <9e3041e182a75f558f1132f915ddf2ee7e859c6e.1686268304.git.kjlx@templeofstupid.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org In certain situations a program with subprograms may have a NULL extable entry. This should not happen, and when it does, it turns a single trap into multiple. Add a test case for further debugging and to prevent regressions. N.b: without any other patches this can panic or oops a kernel. Signed-off-by: Krister Johansen Acked-by: Ilya Leoshkevich Tested-by: Ilya Leoshkevich --- .../bpf/prog_tests/subprogs_extable.c | 31 +++++++++++++ .../bpf/progs/test_subprogs_extable.c | 46 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/subprogs_extable.c create mode 100644 tools/testing/selftests/bpf/progs/test_subprogs_extable.c diff --git a/tools/testing/selftests/bpf/prog_tests/subprogs_extable.c b/tools/testing/selftests/bpf/prog_tests/subprogs_extable.c new file mode 100644 index 000000000000..2201988274a4 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/subprogs_extable.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "test_subprogs_extable.skel.h" + +void test_subprogs_extable(void) +{ + const int READ_SZ = 456; + struct test_subprogs_extable *skel; + int err; + + skel = test_subprogs_extable__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + err = test_subprogs_extable__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + err = test_subprogs_extable__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto cleanup; + + /* trigger tracepoint */ + ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read"); + + test_subprogs_extable__detach(skel); + +cleanup: + test_subprogs_extable__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_subprogs_extable.c b/tools/testing/selftests/bpf/progs/test_subprogs_extable.c new file mode 100644 index 000000000000..c3ff66bf4cbe --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subprogs_extable.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 8); + __type(key, __u32); + __type(value, __u64); +} test_array SEC(".maps"); + +static __u64 test_cb(struct bpf_map *map, __u32 *key, __u64 *val, void *data) +{ + return 1; +} + +SEC("fexit/bpf_testmod_return_ptr") +int BPF_PROG(handle_fexit_ret_subprogs, int arg, struct file *ret) +{ + *(volatile long *)ret; + *(volatile int *)&ret->f_mode; + bpf_for_each_map_elem(&test_array, test_cb, NULL, 0); + return 0; +} + +SEC("fexit/bpf_testmod_return_ptr") +int BPF_PROG(handle_fexit_ret_subprogs2, int arg, struct file *ret) +{ + *(volatile long *)ret; + *(volatile int *)&ret->f_mode; + bpf_for_each_map_elem(&test_array, test_cb, NULL, 0); + return 0; +} + +SEC("fexit/bpf_testmod_return_ptr") +int BPF_PROG(handle_fexit_ret_subprogs3, int arg, struct file *ret) +{ + *(volatile long *)ret; + *(volatile int *)&ret->f_mode; + bpf_for_each_map_elem(&test_array, test_cb, NULL, 0); + return 0; +} + +char _license[] SEC("license") = "GPL";