Skip to contentSkip to conversation

ebpf: Allow dereferences of PTR_TO_STACK registers

PATCH Applied Alex Gartrell 3 participants 3 messages Jul 23, 2015 – Jul 27, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ebpf: Allow dereferences of PTR_TO_STACK registers

        mov %rsp, %r1           ; r1 = rsp
        add $-8, %r1            ; r1 = rsp - 8
        store_q $123, -8(%rsp)  ; *(u64*)r1 = 123  <- valid
        store_q $123, (%r1)     ; *(u64*)r1 = 123  <- previously invalid

mov $0, %r0
exit ; Always need to exit

And we'd get the following error:

	0: (bf) r1 = r10
	1: (07) r1 += -8
	2: (7a) *(u64 *)(r10 -8) = 999
	3: (7a) *(u64 *)(r1 +0) = 999
	R1 invalid mem access 'fp'

	Unable to load program

We already know that a register is a stack address and the appropriate offset, so we should be able to validate those references as well.

---
 kernel/bpf/verifier.c       |  6 ++++-
 samples/bpf/test_verifier.c | 59 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +5 -1
index 039d866..cd307df 100644
@@ -648,6 +648,9 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,
 	struct verifier_state *state = &env->cur_state;
 	int size, err = 0;
 
+	if (state->regs[regno].type == PTR_TO_STACK)
+		off += state->regs[regno].imm;
+
 	size = bpf_size_to_bytes(bpf_size);
 	if (size < 0)
 		return size;
@@ -667,7 +670,8 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,
 		if (!err && t == BPF_READ && value_regno >= 0)
 			mark_reg_unknown_value(state->regs, value_regno);
 
-	} else if (state->regs[regno].type == FRAME_PTR) {
+	} else if (state->regs[regno].type == FRAME_PTR ||
+		   state->regs[regno].type == PTR_TO_STACK) {
 		if (off >= 0 || off < -MAX_BPF_STACK) {
 			verbose("invalid stack off=%d size=%d\n", off, size);
 			return -EACCES;
samples/bpf/test_verifier.c +59 -0

65 lines not on this page · raw message

Signature · 1 line
Alex Gartrell <[email protected]>
Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Alex Gartrell
        mov %rsp, %r1           ; r1 = rsp
        add $-8, %r1            ; r1 = rsp - 8
        store_q $123, -8(%rsp)  ; *(u64*)r1 = 123  <- valid
        store_q $123, (%r1)     ; *(u64*)r1 = 123  <- previously invalid

mov $0, %r0
exit ; Always need to exit

And we'd get the following error:

	0: (bf) r1 = r10
	1: (07) r1 += -8
	2: (7a) *(u64 *)(r10 -8) = 999
	3: (7a) *(u64 *)(r1 +0) = 999
	R1 invalid mem access 'fp'

	Unable to load program
Quoted 8 lines

We already know that a register is a stack address and the appropriate offset, so we should be able to validate those references as well.

---
 kernel/bpf/verifier.c       |  6 ++++-
 samples/bpf/test_verifier.c | 59 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

Looks good.

Review

Quoted 1 line
(quoted diff) +1 -0
+			BPF_ST_MEM(BPF_DW, BPF_REG_1, 2, 0xfaceb00c),

nice constants :)

maintainer Applied
#2 lore raw
In reply to Alex Gartrell

From: Alex Gartrell <[email protected]>
Date: Thu, 23 Jul 2015 14:24:40 -0700

Quoted 17 lines
        mov %rsp, %r1           ; r1 = rsp
        add $-8, %r1            ; r1 = rsp - 8
        store_q $123, -8(%rsp)  ; *(u64*)r1 = 123  <- valid
        store_q $123, (%r1)     ; *(u64*)r1 = 123  <- previously invalid

mov $0, %r0
exit ; Always need to exit

And we'd get the following error:

	0: (bf) r1 = r10
	1: (07) r1 += -8
	2: (7a) *(u64 *)(r10 -8) = 999
	3: (7a) *(u64 *)(r1 +0) = 999
	R1 invalid mem access 'fp'

	Unable to load program
Quoted 4 lines

We already know that a register is a stack address and the appropriate offset, so we should be able to validate those references as well.

Applied, thanks.