From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: bpf@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>
Subject: [PATCH] bpf: call get_random_u32() for random integers
Date: Mon, 5 Dec 2022 19:15:34 +0100 [thread overview]
Message-ID: <20221205181534.612702-1-Jason@zx2c4.com> (raw)
Since BPF's bpf_user_rnd_u32() was introduced, there have been three
significant developments in the RNG: 1) get_random_u32() returns the
same types of bytes as /dev/urandom, eliminating the distinction between
"kernel random bytes" and "userspace random bytes", 2) get_random_u32()
operates mostly locklessly over percpu state, 3) get_random_u32() has
become quite fast.
So rather than using the old clunky Tausworthe prandom code, just call
get_random_u32(), which should fit BPF uses perfectly.
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/linux/bpf.h | 1 -
kernel/bpf/core.c | 17 +----------------
kernel/bpf/verifier.c | 2 --
net/core/filter.c | 1 -
4 files changed, 1 insertion(+), 20 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 0566705c1d4e..aae89318789a 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2554,7 +2554,6 @@ const struct bpf_func_proto *tracing_prog_func_proto(
enum bpf_func_id func_id, const struct bpf_prog *prog);
/* Shared helpers among cBPF and eBPF. */
-void bpf_user_rnd_init_once(void);
u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 38159f39e2af..2cc28d63d761 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2579,14 +2579,6 @@ void bpf_prog_free(struct bpf_prog *fp)
}
EXPORT_SYMBOL_GPL(bpf_prog_free);
-/* RNG for unpriviledged user space with separated state from prandom_u32(). */
-static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
-
-void bpf_user_rnd_init_once(void)
-{
- prandom_init_once(&bpf_user_rnd_state);
-}
-
BPF_CALL_0(bpf_user_rnd_u32)
{
/* Should someone ever have the rather unwise idea to use some
@@ -2595,14 +2587,7 @@ BPF_CALL_0(bpf_user_rnd_u32)
* transformations. Register assignments from both sides are
* different, f.e. classic always sets fn(ctx, A, X) here.
*/
- struct rnd_state *state;
- u32 res;
-
- state = &get_cpu_var(bpf_user_rnd_state);
- res = prandom_u32_state(state);
- put_cpu_var(bpf_user_rnd_state);
-
- return res;
+ return get_random_u32();
}
BPF_CALL_0(bpf_get_raw_cpu_id)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 225666307bba..75a1a6526165 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -14045,8 +14045,6 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
if (insn->imm == BPF_FUNC_get_route_realm)
prog->dst_needed = 1;
- if (insn->imm == BPF_FUNC_get_prandom_u32)
- bpf_user_rnd_init_once();
if (insn->imm == BPF_FUNC_override_return)
prog->kprobe_override = 1;
if (insn->imm == BPF_FUNC_tail_call) {
diff --git a/net/core/filter.c b/net/core/filter.c
index bb0136e7a8e4..7a595ac0028d 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -443,7 +443,6 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
break;
case SKF_AD_OFF + SKF_AD_RANDOM:
*insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
- bpf_user_rnd_init_once();
break;
}
break;
--
2.38.1
next reply other threads:[~2022-12-05 18:15 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-05 18:15 Jason A. Donenfeld [this message]
2022-12-05 22:21 ` [PATCH] bpf: call get_random_u32() for random integers Daniel Borkmann
2022-12-05 22:47 ` Jason A. Donenfeld
2022-12-06 12:50 ` Toke Høiland-Jørgensen
2022-12-06 12:59 ` Jason A. Donenfeld
2022-12-06 13:26 ` Toke Høiland-Jørgensen
2022-12-06 13:30 ` Jason A. Donenfeld
2022-12-06 13:53 ` Toke Høiland-Jørgensen
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=20221205181534.612702-1-Jason@zx2c4.com \
--to=jason@zx2c4.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=netdev@vger.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).