Skip to contentSkip to conversation

bpf: verifier: check for packet data access based on target prog

PATCH Superseded Udip Pant 2 participants 2 messages Aug 19, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Udip Pant

author
#0 lore raw

PATCH 1/1 bpf: verifier: check for packet data access based on target prog

While using dynamic program extension (of type BPF_PROG_TYPE_EXT), we need to check the program type of the target program to grant the read / write access to the packet data.

The BPF_PROG_TYPE_EXT type can be used to extend types such as XDP, SKB and others. Since the BPF_PROG_TYPE_EXT program type on itself is just a placeholder for those, we need this extended check for those target programs to actually work while using this option.

Tested this with a freplace xdp program. Without this patch, the verifier fails with error 'cannot write into packet'.

---
 kernel/bpf/verifier.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +5 -1
index ef938f17b944..4d7604430994 100644
@@ -2629,7 +2629,11 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
 				       const struct bpf_call_arg_meta *meta,
 				       enum bpf_access_type t)
 {
-	switch (env->prog->type) {
+	struct bpf_prog *prog = env->prog;
+	enum bpf_prog_type prog_type = prog->aux->linked_prog ?
+	      prog->aux->linked_prog->type : prog->type;
+
+	switch (prog_type) {
 	/* Program types only with direct read access go here! */
 	case BPF_PROG_TYPE_LWT_IN:
 	case BPF_PROG_TYPE_LWT_OUT:
Signature · 1 line
2.24.1
#1 lore raw
Quoted 11 lines · Udip Pant

While using dynamic program extension (of type BPF_PROG_TYPE_EXT), we need to check the program type of the target program to grant the read / write access to the packet data.

The BPF_PROG_TYPE_EXT type can be used to extend types such as XDP, SKB and others. Since the BPF_PROG_TYPE_EXT program type on itself is just a placeholder for those, we need this extended check for those target programs to actually work while using this option.

Tested this with a freplace xdp program. Without this patch, the verifier fails with error 'cannot write into packet'.

Could you add a selftest for this? FYI, current selftests/bpf have 4 freplace programs:
fexit_bpf2bpf.c:SEC("freplace/get_skb_len") fexit_bpf2bpf.c:SEC("freplace/get_skb_ifindex") fexit_bpf2bpf.c:SEC("freplace/get_constant") freplace_connect4.c:SEC("freplace/do_bind")

Quoted 24 lines
---
  kernel/bpf/verifier.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +5 -1
index ef938f17b944..4d7604430994 100644
@@ -2629,7 +2629,11 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
  				       const struct bpf_call_arg_meta *meta,
  				       enum bpf_access_type t)
  {
-	switch (env->prog->type) {
+	struct bpf_prog *prog = env->prog;
+	enum bpf_prog_type prog_type = prog->aux->linked_prog ?
+	      prog->aux->linked_prog->type : prog->type;
+
+	switch (prog_type) {
  	/* Program types only with direct read access go here! */
  	case BPF_PROG_TYPE_LWT_IN:
  	case BPF_PROG_TYPE_LWT_OUT: