diff mbox series

[bpf,4/9] bpf: restrict map value pointer arithmetic for unpriviledged

Message ID 20190101204925.4584-5-daniel@iogearbox.net
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series bpf fix to prevent oob under speculation | expand

Commit Message

Daniel Borkmann Jan. 1, 2019, 8:49 p.m. UTC
Restrict map value pointer arithmetic for unpriviledged users in that
arithmetic itself must not go out of bounds as opposed to the actual
access later on. Therefore after each adjust_ptr_min_max_vals() with a
map value pointer as a destination it will simulate a check_map_access()
of 1 byte on the destination and once that fails the program is rejected
for unpriviledged program loads. We use this later on for masking any
pointer arithmetic with the remainder of the map value space. The
likelihood of breaking any existing real-world unpriviledged eBPF
program is very small for this corner case.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
 kernel/bpf/verifier.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series


diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index afa8515..4da8c73 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -3249,6 +3249,17 @@  static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
+	/* For unprivileged we require that resulting offset must be in bounds
+	 * in order to be able to sanitize access later on.
+	 */
+	if (!env->allow_ptr_leaks && dst_reg->type == PTR_TO_MAP_VALUE &&
+	    check_map_access(env, dst, dst_reg->off, 1, false)) {
+		verbose(env, "R%d pointer arithmetic of map value goes out of range, prohibited for !root\n",
+			dst);
+		return -EACCES;
+	}
 	return 0;