From: Alexei Starovoitov <ast@kernel.org>
To: <davem@davemloft.net>
Cc: <daniel@iogearbox.net>, <x86@kernel.org>,
<netdev@vger.kernel.org>, <bpf@vger.kernel.org>,
<kernel-team@fb.com>
Subject: [PATCH v4 bpf-next 14/20] bpf: Fix race in btf_resolve_helper_id()
Date: Thu, 14 Nov 2019 10:57:14 -0800 [thread overview]
Message-ID: <20191114185720.1641606-15-ast@kernel.org> (raw)
In-Reply-To: <20191114185720.1641606-1-ast@kernel.org>
btf_resolve_helper_id() caching logic is a bit racy, since under root the
verifier can verify several programs in parallel. Fix it with READ/WRITE_ONCE.
Fix the type as well, since error is also recorded.
Fixes: a7658e1a4164 ("bpf: Check types of arguments passed into helpers")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
---
include/linux/bpf.h | 5 +++--
kernel/bpf/btf.c | 26 +++++++++++++++++++++++++-
kernel/bpf/verifier.c | 8 +++-----
net/core/filter.c | 2 +-
4 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 0d4c5c224d79..cb5a356381f5 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -248,7 +248,7 @@ struct bpf_func_proto {
};
enum bpf_arg_type arg_type[5];
};
- u32 *btf_id; /* BTF ids of arguments */
+ int *btf_id; /* BTF ids of arguments */
};
/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
@@ -881,7 +881,8 @@ int btf_struct_access(struct bpf_verifier_log *log,
const struct btf_type *t, int off, int size,
enum bpf_access_type atype,
u32 *next_btf_id);
-u32 btf_resolve_helper_id(struct bpf_verifier_log *log, void *, int);
+int btf_resolve_helper_id(struct bpf_verifier_log *log,
+ const struct bpf_func_proto *fn, int);
int btf_distill_func_proto(struct bpf_verifier_log *log,
struct btf *btf,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 9e1164e5b429..033d071eb59c 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3721,7 +3721,8 @@ int btf_struct_access(struct bpf_verifier_log *log,
return -EINVAL;
}
-u32 btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, int arg)
+static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
+ int arg)
{
char fnname[KSYM_SYMBOL_LEN + 4] = "btf_";
const struct btf_param *args;
@@ -3789,6 +3790,29 @@ u32 btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, int arg)
return btf_id;
}
+int btf_resolve_helper_id(struct bpf_verifier_log *log,
+ const struct bpf_func_proto *fn, int arg)
+{
+ int *btf_id = &fn->btf_id[arg];
+ int ret;
+
+ if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID)
+ return -EINVAL;
+
+ ret = READ_ONCE(*btf_id);
+ if (ret)
+ return ret;
+ /* ok to race the search. The result is the same */
+ ret = __btf_resolve_helper_id(log, fn->func, arg);
+ if (!ret) {
+ /* Function argument cannot be type 'void' */
+ bpf_log(log, "BTF resolution bug\n");
+ return -EFAULT;
+ }
+ WRITE_ONCE(*btf_id, ret);
+ return ret;
+}
+
static int __get_type_size(struct btf *btf, u32 btf_id,
const struct btf_type **bad_type)
{
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 8f89cfa93e88..e78ec7990767 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -4147,11 +4147,9 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
meta.func_id = func_id;
/* check args */
for (i = 0; i < 5; i++) {
- if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID) {
- if (!fn->btf_id[i])
- fn->btf_id[i] = btf_resolve_helper_id(&env->log, fn->func, i);
- meta.btf_id = fn->btf_id[i];
- }
+ err = btf_resolve_helper_id(&env->log, fn, i);
+ if (err > 0)
+ meta.btf_id = err;
err = check_func_arg(env, BPF_REG_1 + i, fn->arg_type[i], &meta);
if (err)
return err;
diff --git a/net/core/filter.c b/net/core/filter.c
index fc303abec8fa..f72face90659 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3816,7 +3816,7 @@ static const struct bpf_func_proto bpf_skb_event_output_proto = {
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
};
-static u32 bpf_skb_output_btf_ids[5];
+static int bpf_skb_output_btf_ids[5];
const struct bpf_func_proto bpf_skb_output_proto = {
.func = bpf_skb_event_output,
.gpl_only = true,
--
2.23.0
next prev parent reply other threads:[~2019-11-14 18:57 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-14 18:57 [PATCH v4 bpf-next 00/20] Introduce BPF trampoline Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 01/20] x86/alternatives: Teach text_poke_bp() to emulate instructions Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 02/20] bpf: refactor x86 JIT into helpers Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 03/20] bpf: Add bpf_arch_text_poke() helper Alexei Starovoitov
2019-11-15 22:18 ` Andrii Nakryiko
2019-11-14 18:57 ` [PATCH v4 bpf-next 04/20] bpf: Introduce BPF trampoline Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 05/20] libbpf: Introduce btf__find_by_name_kind() Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 06/20] libbpf: Add support to attach to fentry/fexit tracing progs Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 07/20] selftest/bpf: Simple test for fentry/fexit Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 08/20] bpf: Add kernel test functions for fentry testing Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 09/20] selftests/bpf: Add test for BPF trampoline Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 10/20] selftests/bpf: Add fexit tests " Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 11/20] selftests/bpf: Add combined fentry/fexit test Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 12/20] selftests/bpf: Add stress test for maximum number of progs Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 13/20] bpf: Reserve space for BPF trampoline in BPF programs Alexei Starovoitov
2019-11-14 18:57 ` Alexei Starovoitov [this message]
2019-11-14 18:57 ` [PATCH v4 bpf-next 15/20] bpf: Annotate context types Alexei Starovoitov
2019-11-14 22:55 ` Song Liu
2019-11-14 23:01 ` Alexei Starovoitov
2019-11-14 23:19 ` Song Liu
2019-11-14 23:23 ` Song Liu
2019-11-14 18:57 ` [PATCH v4 bpf-next 16/20] bpf: Compare BTF types of functions arguments with actual types Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 17/20] bpf: Support attaching tracing BPF program to other BPF programs Alexei Starovoitov
2019-11-14 23:30 ` Song Liu
2019-11-14 18:57 ` [PATCH v4 bpf-next 18/20] libbpf: Add support for attaching BPF programs " Alexei Starovoitov
2019-11-14 23:39 ` Song Liu
2019-11-14 18:57 ` [PATCH v4 bpf-next 19/20] selftests/bpf: Extend test_pkt_access test Alexei Starovoitov
2019-11-14 18:57 ` [PATCH v4 bpf-next 20/20] selftests/bpf: Add a test for attaching BPF prog to another BPF prog and subprog Alexei Starovoitov
2019-11-16 0:01 ` [PATCH v4 bpf-next 00/20] Introduce BPF trampoline Daniel Borkmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191114185720.1641606-15-ast@kernel.org \
--to=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).