Skip to content

Commit 92d681b

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
bpf: Remove redundant dynptr arg check for helper
unmark_stack_slots_dynptr() already makes sure that CONST_PTR_TO_DYNPTR cannot be released. process_dynptr_func() also prevents passing uninitialized dynptr to helpers expecting initialized dynptr. Now that unmark_stack_slots_dynptr() also reports error returned from release_reference(), there should be no reason to keep these redundant checks. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260529014936.2811085-7-ameryhung@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 308c7a0 commit 92d681b

3 files changed

Lines changed: 6 additions & 25 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8220,26 +8220,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
82208220

82218221
skip_type_check:
82228222
if (arg_type_is_release(arg_type)) {
8223-
if (arg_type_is_dynptr(arg_type)) {
8224-
struct bpf_func_state *state = bpf_func(env, reg);
8225-
int spi;
8226-
8227-
/* Only dynptr created on stack can be released, thus
8228-
* the get_spi and stack state checks for spilled_ptr
8229-
* should only be done before process_dynptr_func for
8230-
* PTR_TO_STACK.
8231-
*/
8232-
if (reg->type == PTR_TO_STACK) {
8233-
spi = dynptr_get_spi(env, reg);
8234-
if (spi < 0 || !state->stack[spi].spilled_ptr.id) {
8235-
verbose(env, "arg %d is an unacquired reference\n", regno);
8236-
return -EINVAL;
8237-
}
8238-
} else {
8239-
verbose(env, "cannot release unowned const bpf_dynptr\n");
8240-
return -EINVAL;
8241-
}
8242-
} else if (!reg_is_referenced(env, reg) && !bpf_register_is_null(reg)) {
8223+
if (!arg_type_is_dynptr(arg_type) && !reg_is_referenced(env, reg) && !bpf_register_is_null(reg)) {
82438224
verbose(env, "R%d must be referenced when passed to release function\n",
82448225
regno);
82458226
return -EINVAL;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int ringbuf_missing_release_callback(void *ctx)
136136

137137
/* Can't call bpf_ringbuf_submit/discard_dynptr on a non-initialized dynptr */
138138
SEC("?raw_tp")
139-
__failure __msg("arg 1 is an unacquired reference")
139+
__failure __msg("Expected an initialized dynptr as R1")
140140
int ringbuf_release_uninit_dynptr(void *ctx)
141141
{
142142
struct bpf_dynptr ptr;
@@ -650,7 +650,7 @@ int invalid_offset(void *ctx)
650650

651651
/* Can't release a dynptr twice */
652652
SEC("?raw_tp")
653-
__failure __msg("arg 1 is an unacquired reference")
653+
__failure __msg("Expected an initialized dynptr as R1")
654654
int release_twice(void *ctx)
655655
{
656656
struct bpf_dynptr ptr;
@@ -677,7 +677,7 @@ static int release_twice_callback_fn(__u32 index, void *data)
677677
* within a callback function, fails
678678
*/
679679
SEC("?raw_tp")
680-
__failure __msg("arg 1 is an unacquired reference")
680+
__failure __msg("Expected an initialized dynptr as R1")
681681
int release_twice_callback(void *ctx)
682682
{
683683
struct bpf_dynptr ptr;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ try_discard_dynptr(struct bpf_dynptr *dynptr, void *context)
146146
* not be able to read past the end of the pointer.
147147
*/
148148
SEC("?raw_tp")
149-
__failure __msg("cannot release unowned const bpf_dynptr")
149+
__failure __msg("CONST_PTR_TO_DYNPTR cannot be released")
150150
int user_ringbuf_callback_discard_dynptr(void *ctx)
151151
{
152152
bpf_user_ringbuf_drain(&user_ringbuf, try_discard_dynptr, NULL, 0);
@@ -166,7 +166,7 @@ try_submit_dynptr(struct bpf_dynptr *dynptr, void *context)
166166
* not be able to read past the end of the pointer.
167167
*/
168168
SEC("?raw_tp")
169-
__failure __msg("cannot release unowned const bpf_dynptr")
169+
__failure __msg("CONST_PTR_TO_DYNPTR cannot be released")
170170
int user_ringbuf_callback_submit_dynptr(void *ctx)
171171
{
172172
bpf_user_ringbuf_drain(&user_ringbuf, try_submit_dynptr, NULL, 0);

0 commit comments

Comments
 (0)