diff mbox

[net] bpf: rather use get_random_int for randomizations

Message ID 24461974e8402395c12b3e36b9ac7935b88ef1c2.1463572456.git.daniel@iogearbox.net
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Daniel Borkmann May 18, 2016, 12:14 p.m. UTC
Start address randomization and blinding in BPF currently use
prandom_u32(). prandom_u32() values are not exposed to unpriviledged
user space to my knowledge, but given other kernel facilities such as
ASLR, stack canaries, etc make use of stronger get_random_int(), we
better make use of it here as well given blinding requests successively
new random values. get_random_int() has minimal entropy pool depletion,
is not cryptographically secure, but doesn't need to be for our use
cases here.

Suggested-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
 kernel/bpf/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Eric Dumazet May 18, 2016, 1:20 p.m. UTC | #1
On Wed, 2016-05-18 at 14:14 +0200, Daniel Borkmann wrote:
> Start address randomization and blinding in BPF currently use
> prandom_u32(). prandom_u32() values are not exposed to unpriviledged
> user space to my knowledge, but given other kernel facilities such as
> ASLR, stack canaries, etc make use of stronger get_random_int(), we
> better make use of it here as well given blinding requests successively
> new random values. get_random_int() has minimal entropy pool depletion,
> is not cryptographically secure, but doesn't need to be for our use
> cases here.

Well, if it is not crypto secure, what is the point using it instead of
prandom_u32() ?

I do not think changing this is fundamentally changing something, it
looks like code churn or magic incantation to me.

There is little amount of entropy since the hole is constrained by
PAGE_SIZE. 

Have you had any report of an actual attack ?

If yes, we seriously need to reconsider this whole schem.
Hannes Frederic Sowa May 18, 2016, 1:28 p.m. UTC | #2
On 18.05.2016 15:20, Eric Dumazet wrote:
> On Wed, 2016-05-18 at 14:14 +0200, Daniel Borkmann wrote:
>> Start address randomization and blinding in BPF currently use
>> prandom_u32(). prandom_u32() values are not exposed to unpriviledged
>> user space to my knowledge, but given other kernel facilities such as
>> ASLR, stack canaries, etc make use of stronger get_random_int(), we
>> better make use of it here as well given blinding requests successively
>> new random values. get_random_int() has minimal entropy pool depletion,
>> is not cryptographically secure, but doesn't need to be for our use
>> cases here.
> 
> Well, if it is not crypto secure, what is the point using it instead of
> prandom_u32() ?

Leaks of prandom_u32 allows an attacker to find the whole sequence of
generated random numbers or the other way around.

> I do not think changing this is fundamentally changing something, it
> looks like code churn or magic incantation to me.

I saw this during the review of the blinding patches. I was afraid, that
by extracting or dumping the ebpf program, a user could find out the
blinding constant and after some retries could find out the coefficients
to the taus rng, thus being able to infer the whole sequence generated
by prandom_u32.

> There is little amount of entropy since the hole is constrained by
> PAGE_SIZE. 

True.

I don't consider this a big thing, I just mentioned that we probably
shouldn't use prandom_u32 if the value somehow could leak to user space
and should be used for security.

Bye,
Hannes
Daniel Borkmann May 18, 2016, 2:14 p.m. UTC | #3
On 05/18/2016 03:28 PM, Hannes Frederic Sowa wrote:
> On 18.05.2016 15:20, Eric Dumazet wrote:
>> On Wed, 2016-05-18 at 14:14 +0200, Daniel Borkmann wrote:
>>> Start address randomization and blinding in BPF currently use
>>> prandom_u32(). prandom_u32() values are not exposed to unpriviledged
>>> user space to my knowledge, but given other kernel facilities such as
>>> ASLR, stack canaries, etc make use of stronger get_random_int(), we
>>> better make use of it here as well given blinding requests successively
>>> new random values. get_random_int() has minimal entropy pool depletion,
>>> is not cryptographically secure, but doesn't need to be for our use
>>> cases here.
>>
>> Well, if it is not crypto secure, what is the point using it instead of
>> prandom_u32() ?
>
> Leaks of prandom_u32 allows an attacker to find the whole sequence of
> generated random numbers or the other way around.
>
>> I do not think changing this is fundamentally changing something, it
>> looks like code churn or magic incantation to me.
>
> I saw this during the review of the blinding patches. I was afraid, that
> by extracting or dumping the ebpf program, a user could find out the
> blinding constant and after some retries could find out the coefficients
> to the taus rng, thus being able to infer the whole sequence generated
> by prandom_u32.

Note that dumping/extraction is normally not possible: cBPF progs hold the
original insns before they were even transformed into eBPF, and eBPF cannot
be dumped back today, so the rewrites stay kernel internal. Only when an
admin is not careful and accidentally sets things like bpf_jit_enable > 1,
where we have JIT debug mode and the generated image goes to dmesg (I don't
consider this the case normally, but what do I know).

>> There is little amount of entropy since the hole is constrained by
>> PAGE_SIZE.
>
> True.
>
> I don't consider this a big thing, I just mentioned that we probably
> shouldn't use prandom_u32 if the value somehow could leak to user space
> and should be used for security.

Agree here which is why I decided to send it.

Thanks,
Daniel
Eric Dumazet May 18, 2016, 2:17 p.m. UTC | #4
On Wed, 2016-05-18 at 15:28 +0200, Hannes Frederic Sowa wrote:

> I don't consider this a big thing, I just mentioned that we probably
> shouldn't use prandom_u32 if the value somehow could leak to user space
> and should be used for security.

Yes, I was mostly trying to understand if you had real security issues
there or some general concerns ;)
Alexei Starovoitov May 18, 2016, 9:18 p.m. UTC | #5
On Wed, May 18, 2016 at 07:17:48AM -0700, Eric Dumazet wrote:
> On Wed, 2016-05-18 at 15:28 +0200, Hannes Frederic Sowa wrote:
> 
> > I don't consider this a big thing, I just mentioned that we probably
> > shouldn't use prandom_u32 if the value somehow could leak to user space
> > and should be used for security.
> 
> Yes, I was mostly trying to understand if you had real security issues
> there or some general concerns ;)

agree with Eric. Frankly I wouldn't do it, but since it's a trivial
patch and if it makes security folks less worried then why not.
Acked-by: Alexei Starovoitov <ast@kernel.org>
David Miller May 20, 2016, 6:24 p.m. UTC | #6
From: Daniel Borkmann <daniel@iogearbox.net>
Date: Wed, 18 May 2016 14:14:28 +0200

> Start address randomization and blinding in BPF currently use
> prandom_u32(). prandom_u32() values are not exposed to unpriviledged
> user space to my knowledge, but given other kernel facilities such as
> ASLR, stack canaries, etc make use of stronger get_random_int(), we
> better make use of it here as well given blinding requests successively
> new random values. get_random_int() has minimal entropy pool depletion,
> is not cryptographically secure, but doesn't need to be for our use
> cases here.
> 
> Suggested-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

Ok, applied, thanks Daniel.
diff mbox

Patch

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f1e8a0d..b94a365 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -231,7 +231,7 @@  bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
 	hdr->pages = size / PAGE_SIZE;
 	hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
 		     PAGE_SIZE - sizeof(*hdr));
-	start = (prandom_u32() % hole) & ~(alignment - 1);
+	start = (get_random_int() % hole) & ~(alignment - 1);
 
 	/* Leave a random number of instructions before BPF code. */
 	*image_ptr = &hdr->image[start];
@@ -251,7 +251,7 @@  static int bpf_jit_blind_insn(const struct bpf_insn *from,
 			      struct bpf_insn *to_buff)
 {
 	struct bpf_insn *to = to_buff;
-	u32 imm_rnd = prandom_u32();
+	u32 imm_rnd = get_random_int();
 	s16 off;
 
 	BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);