Skip to content

bpf: Set kfunc dynptr arg type flag based on prototype#11640

Closed
ameryhung wants to merge 1 commit intokernel-patches:bpf-next_basefrom
ameryhung:dynptr_proto_v1
Closed

bpf: Set kfunc dynptr arg type flag based on prototype#11640
ameryhung wants to merge 1 commit intokernel-patches:bpf-next_basefrom
ameryhung:dynptr_proto_v1

Conversation

@ameryhung
Copy link
Copy Markdown
Contributor

The verifer currently tracks the mutability of a kfunc's bpf_dynptr through its signature, where "const struct bpf_dynptr *" means the structure itself is immutable. However, many existing kfunc prototypes are inconsistent with the definition. For example, bpf_dynptr_adjust() mutates a dynptr's start and offset but marks the argument as a const pointer, while many other kfunc that does not mutate the dynptr but mark themselves as mutable. In addition, the verifier currently does not honor the const qualifier in the prototype as it currently determines whether tagging the arg_type with MEM_RDONLY based on the register type.

Since all the verifier care is to prevent CONST_PTR_TO_DYNPTR from being destroyed in callback and global subprogram, redefine the mutability at the bpf_dynptr level to just bpf_dynptr_kern->data. Then, explicitly prohibit passing CONST_PTR_TO_DYNPTR to an argument tagged with MEM_UNINIT or OBJ_RELEASE. The mutability of a dynptr's view is not really interesting so drop MEM_RDONLY annotation from the helpers and kfuncs.

In process_dynptr_func(), when passing CONST_PTR_TO_DYNPTR to dynptr argument that is not __uninit, check against OBJ_RELEASE instead of MEM_RDONLY and drop a now identical check in umark_stack_slots_dynptr().

Note that the prototype of various kfuncs are still fixed to maintain the semantic correctness in the C source code. Adding or removing the const qualifier does not break backward compatibility.

The verifer currently tracks the mutability of a kfunc's bpf_dynptr
through its signature, where "const struct bpf_dynptr *" means the
structure itself is immutable. However, many existing kfunc prototypes
are inconsistent with the definition. For example, bpf_dynptr_adjust()
mutates a dynptr's start and offset but marks the argument as a const
pointer, while many other kfunc that does not mutate the dynptr but
mark themselves as mutable. In addition, the verifier currently does not
honor the const qualifier in the prototype as it currently determines
whether tagging the arg_type with MEM_RDONLY based on the register
type.

Since all the verifier care is to prevent CONST_PTR_TO_DYNPTR from
being destroyed in callback and global subprogram, redefine the
mutability at the bpf_dynptr level to just bpf_dynptr_kern->data. Then,
explicitly prohibit passing CONST_PTR_TO_DYNPTR to an argument tagged
with MEM_UNINIT or OBJ_RELEASE. The mutability of a dynptr's view is not
really interesting so drop MEM_RDONLY annotation from the helpers and
kfuncs.

In process_dynptr_func(), when passing CONST_PTR_TO_DYNPTR to dynptr
argument that is not __uninit, check against OBJ_RELEASE instead of
MEM_RDONLY and drop a now identical check in umark_stack_slots_dynptr().

Note that the prototype of various kfuncs are still fixed to maintain
the semantic correctness in the C source code. Adding or removing the
const qualifier does not break backward compatibility.

In test_kfunc_dynptr_param.c, initialize dynptr to 0 to avoid
-Wuninitialized-const-pointer warning.

Signed-off-by: Amery Hung <ameryhung@gmail.com>
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the bpf-next_base branch 21 times, most recently from ef1cb9f to 3842f6d Compare April 9, 2026 01:48
@kernel-patches-daemon-bpf
Copy link
Copy Markdown

Automatically cleaning up stale PR; feel free to reopen if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant