bpf: Allow reinitializing unreferenced dynptr stack slots
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Sat, 21 Jan 2023 00:22:34 +0000 (05:52 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 21 Jan 2023 01:55:03 +0000 (17:55 -0800)
commit379d4ba831cfa895d0cc61d88cd0e1402f35818c
treec92abb771c014693238f181461d420eaf452484a
parentf8064ab90d6644bc8338d2d7ff6a0d6e7a1b2ef3
bpf: Allow reinitializing unreferenced dynptr stack slots

Consider a program like below:

void prog(void)
{
{
struct bpf_dynptr ptr;
bpf_dynptr_from_mem(...);
}
...
{
struct bpf_dynptr ptr;
bpf_dynptr_from_mem(...);
}
}

Here, the C compiler based on lifetime rules in the C standard would be
well within in its rights to share stack storage for dynptr 'ptr' as
their lifetimes do not overlap in the two distinct scopes. Currently,
such an example would be rejected by the verifier, but this is too
strict. Instead, we should allow reinitializing over dynptr stack slots
and forget information about the old dynptr object.

The destroy_if_dynptr_stack_slot function already makes necessary checks
to avoid overwriting referenced dynptr slots. This is done to present a
better error message instead of forgetting dynptr information on stack
and preserving reference state, leading to an inevitable but
undecipherable error at the end about an unreleased reference which has
to be associated back to its allocating call instruction to make any
sense to the user.

Acked-by: Joanne Koong <joannelkoong@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20230121002241.2113993-6-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c