Patch "bpf: prevent leaking pointer via xadd on unpriviledged" has been added to the 4.4-stable tree
Subsystems (3)
This is a note to let you know that I've just added the patch titled
bpf: prevent leaking pointer via xadd on unpriviledged
to the 4.4-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
bpf-prevent-leaking-pointer-via-xadd-on-unpriviledged.patch
and it can be found in the queue-4.4 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
Quoted 1 line
From
6bdf6abc56b53103324dfd270a86580306e1a232Mon Sep 17 00:00:00 2001
From: Daniel Borkmann <[email protected]>
Date: Thu, 29 Jun 2017 03:04:59 +0200
Subject: bpf: prevent leaking pointer via xadd on unpriviledged
From: Daniel Borkmann <[email protected]>
commit 6bdf6abc56b53103324dfd270a86580306e1a232 upstream.
Leaking kernel addresses on unpriviledged is generally disallowed, for example, verifier rejects the following:
0: (b7) r0 = 0
1: (18) r2 = 0xffff897e82304400
3: (7b) *(u64 *)(r1 +48) = r2
R2 leaks addr into ctx
Doing pointer arithmetic on them is also forbidden, so that they don't turn into unknown value and then get leaked out. However, there's xadd as a special case, where we don't check the src reg for being a pointer register, e.g. the following will pass:
0: (b7) r0 = 0
1: (7b) *(u64 *)(r1 +48) = r0
2: (18) r2 = 0xffff897e82304400 ; map
4: (db) lock *(u64 *)(r1 +48) += r2
5: (95) exit
We could store the pointer into skb->cb, loose the type context, and then read it out from there again to leak it eventually out of a map value. Or more easily in a different variant, too:
0: (bf) r6 = r1
1: (7a) *(u64 *)(r10 -8) = 0
2: (bf) r2 = r10
3: (07) r2 += -8
4: (18) r1 = 0x0
6: (85) call bpf_map_lookup_elem#1
7: (15) if r0 == 0x0 goto pc+3 R0=map_value(ks=8,vs=8,id=0),min_value=0,max_value=0 R6=ctx R10=fp 8: (b7) r3 = 0
9: (7b) *(u64 *)(r0 +0) = r3
10: (db) lock *(u64 *)(r0 +0) += r6
11: (b7) r0 = 0
12: (95) exit
from 7 to 11: R0=inv,min_value=0,max_value=0 R6=ctx R10=fp 11: (b7) r0 = 0
12: (95) exit
Prevent this by checking xadd src reg for pointer types. Also add a couple of test cases related to this.
--- kernel/bpf/verifier.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/bpf/verifier.c +5 -0
@@ -754,6 +754,11 @@ static int check_xadd(struct verifier_enif (err)return err; +if (is_pointer_value (env, insn->src_reg)) { +verbose ("R%d leaks addr into mem\n" , insn->src_reg); +return -EACCES ; + } +/* check whether atomic_add can read the memory */ err =check_mem_access (env, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_READ ,-1 );
Patches currently in stable-queue which might be from [email protected] are
queue-4.4/bpf-prevent-leaking-pointer-via-xadd-on-unpriviledged.patch