Skip to contentSkip to conversation

bpf: fix to allow ptr stack program type

PATCH In review [email protected] 2 participants 6 messages Aug 9, 2019 – Aug 13, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix to allow ptr stack program type

From: Jerin Jacob <[email protected]>

bpf_validate does not allow to execute
RTE_BPF_ARG_PTR_STACK for no reason.
Fix it by enhancing the prog_arg.type check.

Fixes 6e12ec4c4d6d ("bpf: add more checks")
---
 lib/librte_bpf/bpf_validate.c | 1 +
 1 file changed, 1 insertion(+)
lib/librte_bpf/bpf_validate.c +1 -0
index 0cf41fa27..c75777b6d 100644
@@ -2216,6 +2216,7 @@ bpf_validate(struct rte_bpf *bpf)
 
 	/* check input argument type, don't allow mbuf ptr on 32-bit */
 	if (bpf->prm.prog_arg.type != RTE_BPF_ARG_RAW &&
+			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR_STACK &&
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR &&
 			(sizeof(uint64_t) != sizeof(uintptr_t) ||
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR_MBUF)) {
Signature · 1 line
2.22.0
#1 lore raw

Hi Jerin,

Quoted 3 lines

bpf_validate does not allow to execute
RTE_BPF_ARG_PTR_STACK for no reason.

I believe there is a reason,
ARG_PTR_STACK is reserved for memory within BPF program internal stack only. User that calls BPF program should never pass parameter with that type. If the user allocates parameter for bpf function on the stack, he can still use RTE_BPF_ARG_PTR for it.
Konstantin

Quoted 24 lines

Fix it by enhancing the prog_arg.type check.

Fixes 6e12ec4c4d6d ("bpf: add more checks")
---
 lib/librte_bpf/bpf_validate.c | 1 +
 1 file changed, 1 insertion(+)
lib/librte_bpf/bpf_validate.c +1 -1
index 0cf41fa27..c75777b6d 100644
@@ -2216,6 +2216,7 @@ bpf_validate(struct rte_bpf *bpf)
 
 	/* check input argument type, don't allow mbuf ptr on 32-bit */
 	if (bpf->prm.prog_arg.type != RTE_BPF_ARG_RAW &&
+			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR_STACK &&
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR &&
 			(sizeof(uint64_t) != sizeof(uintptr_t) ||
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR_MBUF)) {
--

2.22.0

#2 lore raw
Quoted 10 lines

-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Monday, August 12, 2019 2:20 PM
To: Jerin Jacob Kollanukkaran <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: [EXT] RE: [dpdk-dev] [PATCH] bpf: fix to allow ptr stack program type

----------------------------------------------------------------------
Hi Jerin,

Hi Konstantin,

Quoted 9 lines
Quoted 4 lines

bpf_validate does not allow to execute RTE_BPF_ARG_PTR_STACK for no reason.

I believe there is a reason,
ARG_PTR_STACK is reserved for memory within BPF program internal stack only.
User that calls BPF program should never pass parameter with that type.

OK.
Shouldn't we remove that from public header file
(lib/librte_bpf/rte_bpf.h) then ?

Quoted 2 lines

If the user allocates parameter for bpf function on the stack, he can still use RTE_BPF_ARG_PTR for it.

I see the _stack_ is only allocated under RTE_BPF_ARG_PTR_STACK checks in bpf_validate.c. Can you check? I agree that stack should be allocated for RTE_BPF_ARG_PTR as well.

I am writing the arm64 JIT support now, I see always stack of size of 0. I did not spend much Time on the generic piece of ebpf code(Focusing only on JIT side now).

Can you share more detail the stack allocation scheme, Is validate code parse the eBPF opcode and Figure out the stack depth it by its own and pass to JIT function where Arch code can allocate enough stack.

Quoted 31 lines

Konstantin

Quoted 15 lines

Fix it by enhancing the prog_arg.type check.

Fixes 6e12ec4c4d6d ("bpf: add more checks")
---
 lib/librte_bpf/bpf_validate.c | 1 +
 1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/lib/librte_bpf/bpf_validate.c index 0cf41fa27..c75777b6d 100644

lib/librte_bpf/bpf_validate.c bpf_validate(struct rte_bpf *bpf) · line 2219

Quoted 4 lines
lib/librte_bpf/bpf_validate.c +1 -0
@@ -2216,6 +2216,7 @@ bpf_validate(struct rte_bpf *bpf)
 
 	/* check input argument type, don't allow mbuf ptr on 32-bit */
 	if (bpf->prm.prog_arg.type != RTE_BPF_ARG_RAW &&
+			bpf->prm.prog_arg.type !=

RTE_BPF_ARG_PTR_STACK &&

lib/librte_bpf/bpf_validate.c bpf_validate(struct rte_bpf *bpf)

Quoted 3 lines
lib/librte_bpf/bpf_validate.c +0 -0
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR &&
 			(sizeof(uint64_t) != sizeof(uintptr_t) ||
 			bpf->prm.prog_arg.type !=

RTE_BPF_ARG_PTR_MBUF)) {

Quoted 2 lines

--
2.22.0

#3 lore raw
Quoted 10 lines
Quoted 2 lines

Hi Jerin,

Hi Konstantin,

Quoted 5 lines
Quoted 4 lines

bpf_validate does not allow to execute RTE_BPF_ARG_PTR_STACK for no reason.

Quoted 8 lines
Quoted 4 lines

I believe there is a reason,
ARG_PTR_STACK is reserved for memory within BPF program internal stack only.
User that calls BPF program should never pass parameter with that type.

OK.
Shouldn't we remove that from public header file
(lib/librte_bpf/rte_bpf.h) then ?

Probably... or might be just put extra comments to mark it as internal? The reason I left it here, so we can add new public values for enum here, before RTE_BPF_ARG_PTR_STACK.
Of course in theory we can use for RTE_BPF_ARG_PTR_STACK some reserved value instead.

Quoted 6 lines
Quoted 3 lines

If the user allocates parameter for bpf function on the stack, he can still use RTE_BPF_ARG_PTR for it.

I see the _stack_ is only allocated under RTE_BPF_ARG_PTR_STACK checks in bpf_validate.c. Can you check? I agree that stack should be allocated for RTE_BPF_ARG_PTR as well.

Not sure I understand your query here...
Each BPF program can use up to MAX_BPF_STACK_SIZE bytes for stack. Though to avoid JIT to allocate unused space for the stack, in bpf_validate.c we figure out does given BPF program really allocate things on the stack and if yes, how many bytes is needed.
This info is stored in rte_bpf.stack_sz and can be used later by the JIT. Let say for x86 jit is used in emit_prolog().

Quoted 7 lines

I am writing the arm64 JIT support now, I see always stack of size of 0. I did not spend much Time on the generic piece of ebpf code(Focusing only on JIT side now).

Can you share more detail the stack allocation scheme, Is validate code parse the eBPF opcode and Figure out the stack depth it by its own and pass to JIT function where Arch code can allocate enough stack.

Yep, see above.
Konstantin

Quoted 29 lines
Quoted 29 lines
Quoted 18 lines

Fix it by enhancing the prog_arg.type check.

Fixes 6e12ec4c4d6d ("bpf: add more checks")
---
 lib/librte_bpf/bpf_validate.c | 1 +
 1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/lib/librte_bpf/bpf_validate.c index 0cf41fa27..c75777b6d 100644

lib/librte_bpf/bpf_validate.c bpf_validate(struct rte_bpf *bpf) · line 2219

Quoted 4 lines
lib/librte_bpf/bpf_validate.c +1 -0
@@ -2216,6 +2216,7 @@ bpf_validate(struct rte_bpf *bpf)
 
 	/* check input argument type, don't allow mbuf ptr on 32-bit */
 	if (bpf->prm.prog_arg.type != RTE_BPF_ARG_RAW &&
+			bpf->prm.prog_arg.type !=

RTE_BPF_ARG_PTR_STACK &&

lib/librte_bpf/bpf_validate.c bpf_validate(struct rte_bpf *bpf)

Quoted 3 lines
lib/librte_bpf/bpf_validate.c +0 -0
 			bpf->prm.prog_arg.type != RTE_BPF_ARG_PTR &&
 			(sizeof(uint64_t) != sizeof(uintptr_t) ||
 			bpf->prm.prog_arg.type !=

RTE_BPF_ARG_PTR_MBUF)) {

Quoted 2 lines

--
2.22.0

#4 lore raw
Quoted 45 lines

-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Monday, August 12, 2019 5:08 PM
To: Jerin Jacob Kollanukkaran <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: [EXT] RE: [dpdk-dev] [PATCH] bpf: fix to allow ptr stack program type

External Email

----------------------------------------------------------------------

Quoted 9 lines
Quoted 1 line

Hi Jerin,

Hi Konstantin,

Quoted 5 lines
Quoted 4 lines

bpf_validate does not allow to execute RTE_BPF_ARG_PTR_STACK for no reason.

Quoted 8 lines
Quoted 4 lines

I believe there is a reason,
ARG_PTR_STACK is reserved for memory within BPF program internal stack only.
User that calls BPF program should never pass parameter with that type.

OK.
Shouldn't we remove that from public header file
(lib/librte_bpf/rte_bpf.h) then ?

Probably... or might be just put extra comments to mark it as internal? The reason I left it here, so we can add new public values for enum here, before RTE_BPF_ARG_PTR_STACK.
Of course in theory we can use for RTE_BPF_ARG_PTR_STACK some reserved value instead.

Quoted 5 lines
Quoted 3 lines

If the user allocates parameter for bpf function on the stack, he can still use RTE_BPF_ARG_PTR for it.

I see the _stack_ is only allocated under RTE_BPF_ARG_PTR_STACK checks

in bpf_validate.c.

Quoted 1 line

Can you check? I agree that stack should be allocated for

RTE_BPF_ARG_PTR as well.

Quoted 7 lines

Not sure I understand your query here...
Each BPF program can use up to MAX_BPF_STACK_SIZE bytes for stack. Though to avoid JIT to allocate unused space for the stack, in bpf_validate.c we figure out does given BPF program really allocate things on the stack and if yes, how many bytes is needed.
This info is stored in rte_bpf.stack_sz and can be used later by the JIT. Let say for x86 jit is used in emit_prolog().

I thought, stack will be allocated only when user gives RTE_BPF_ARG_PTR_STACK.
I tested following program with RTE_BPF_ARG_PTR. It allocates stacks Properly. So everything is good.

stdw [r10-64], 0xab
mov r0, 0
exit

I will modify this patch to following to avoid any confusion to user:
1) Change RTE_BPF_ARG_PTR_STACK to RTE_BPF_ARG_RESERVED in public header file
2) In the implementation #define RTE_BPF_ARG_RESERVED BPF_ARG_PTR_STACK

Is it OK?

#5 lore raw

Hi Jerin,

Quoted 45 lines
Quoted 28 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

bpf_validate does not allow to execute RTE_BPF_ARG_PTR_STACK for no reason.

Quoted 8 lines
Quoted 4 lines

I believe there is a reason,
ARG_PTR_STACK is reserved for memory within BPF program internal stack only.
User that calls BPF program should never pass parameter with that type.

OK.
Shouldn't we remove that from public header file
(lib/librte_bpf/rte_bpf.h) then ?

Probably... or might be just put extra comments to mark it as internal? The reason I left it here, so we can add new public values for enum here, before RTE_BPF_ARG_PTR_STACK.
Of course in theory we can use for RTE_BPF_ARG_PTR_STACK some reserved value instead.

Quoted 5 lines
Quoted 3 lines

If the user allocates parameter for bpf function on the stack, he can still use RTE_BPF_ARG_PTR for it.

I see the _stack_ is only allocated under RTE_BPF_ARG_PTR_STACK checks

in bpf_validate.c.

Quoted 1 line

Can you check? I agree that stack should be allocated for

RTE_BPF_ARG_PTR as well.

Quoted 7 lines

Not sure I understand your query here...
Each BPF program can use up to MAX_BPF_STACK_SIZE bytes for stack. Though to avoid JIT to allocate unused space for the stack, in bpf_validate.c we figure out does given BPF program really allocate things on the stack and if yes, how many bytes is needed.
This info is stored in rte_bpf.stack_sz and can be used later by the JIT. Let say for x86 jit is used in emit_prolog().

I thought, stack will be allocated only when user gives RTE_BPF_ARG_PTR_STACK.
I tested following program with RTE_BPF_ARG_PTR. It allocates stacks Properly. So everything is good.

stdw [r10-64], 0xab
mov r0, 0
exit

Quoted 5 lines

I will modify this patch to following to avoid any confusion to user:
1) Change RTE_BPF_ARG_PTR_STACK to RTE_BPF_ARG_RESERVED in public header file
2) In the implementation #define RTE_BPF_ARG_RESERVED BPF_ARG_PTR_STACK

Is it OK?

Yes, sounds like a good approach to me.
Thanks
Konstantin