Skip to content

Commit fbcc68a

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
selftests/bpf: Test creating dynptr from dynptr data and slice
The verifier currently does not allow creating dynptr from dynptr data or slice. Add a selftest to test this explicitly. Signed-off-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260529014936.2811085-11-ameryhung@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 2eee6fe commit fbcc68a

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tools/testing/selftests/bpf/progs/dynptr_fail.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,48 @@ int dynptr_from_mem_invalid_api(void *ctx)
705705
return 0;
706706
}
707707

708+
/* Cannot create dynptr from dynptr data */
709+
SEC("?raw_tp")
710+
__failure __msg("Unsupported reg type mem for bpf_dynptr_from_mem data")
711+
int dynptr_from_dynptr_data(void *ctx)
712+
{
713+
struct bpf_dynptr ptr, ptr2;
714+
__u8 *data;
715+
716+
if (get_map_val_dynptr(&ptr))
717+
return 0;
718+
719+
data = bpf_dynptr_data(&ptr, 0, sizeof(__u32));
720+
if (!data)
721+
return 0;
722+
723+
/* this should fail */
724+
bpf_dynptr_from_mem(data, sizeof(__u32), 0, &ptr2);
725+
726+
return 0;
727+
}
728+
729+
/* Cannot create dynptr from dynptr slice */
730+
SEC("?tc")
731+
__failure __msg("Unsupported reg type mem for bpf_dynptr_from_mem data")
732+
int dynptr_from_dynptr_slice(struct __sk_buff *skb)
733+
{
734+
struct bpf_dynptr ptr, ptr2;
735+
struct ethhdr *hdr;
736+
char buffer[sizeof(*hdr)] = {};
737+
738+
bpf_dynptr_from_skb(skb, 0, &ptr);
739+
740+
hdr = bpf_dynptr_slice_rdwr(&ptr, 0, buffer, sizeof(buffer));
741+
if (!hdr)
742+
return SK_DROP;
743+
744+
/* this should fail */
745+
bpf_dynptr_from_mem(hdr, sizeof(*hdr), 0, &ptr2);
746+
747+
return SK_PASS;
748+
}
749+
708750
SEC("?tc")
709751
__failure __msg("cannot overwrite referenced dynptr") __log_level(2)
710752
int dynptr_pruning_overwrite(struct __sk_buff *ctx)

0 commit comments

Comments
 (0)