bpf: Fix memory access flags in helper prototypes
Subsystems (8)
v2 cover bpf: Fix memory access flags in helper prototypes
Hi,
This series adds missing memory access flags (MEM_RDONLY or MEM_WRITE) to several bpf helper function prototypes that use ARG_PTR_TO_MEM but lack the correct flag. It also adds a new check in verifier to ensure the flag is specified.
Missing memory access flags in helper prototypes can lead to critical correctness issues when the verifier tries to perform code optimization. After commit 37cce22dbd51 ("bpf: verifier: Refactor helper access type tracking"), the verifier relies on the memory access flags, rather than treating all arguments in helper functions as potentially modifying the pointed-to memory.
Using ARG_PTR_TO_MEM alone without flags does not make sense because:
- If the helper does not change the argument, missing MEM_RDONLY causes the verifier to incorrectly reject a read-only buffer.
- If the helper does change the argument, missing MEM_WRITE causes the verifier to incorrectly assume the memory is unchanged, leading to errors in code optimization.
We have already seen several reports regarding this:
- commit ac44dcc788b9 ("bpf: Fix verifier assumptions of bpf_d_path's output buffer") adds MEM_WRITE to bpf_d_path;
- commit 2eb7648558a7 ("bpf: Specify access type of bpf_sysctl_get_name args") adds MEM_WRITE to bpf_sysctl_get_name.
This series looks through all prototypes in the kernel and completes the flags. It also adds a new check (check_func_proto) in verifier.c to statically restrict ARG_PTR_TO_MEM from appearing without memory access flags.
Changelog
=========
v2:
- Add missing MEM_RDONLY flags to protos with ARG_PTR_TO_FIXED_SIZE_MEM.
Thanks,
Zesen Liu
---
Zesen Liu (2):
bpf: Fix memory access flags in helper prototypes
bpf: Require ARG_PTR_TO_MEM with memory flag
kernel/bpf/helpers.c | 2 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 17 +++++++++++++++++ kernel/trace/bpf_trace.c | 6 +++--- net/core/filter.c | 20 ++++++++++---------- 5 files changed, 32 insertions(+), 15 deletions(-)
---
base-commit: ab86d0bf01f6d0e37fd67761bb62918321b64efc
change-id: 20251220-helper_proto-fb6e64182467
Best regards,
Signature · 1 line
Zesen Liu <[email protected]>
PATCH RESEND v2 1/2 bpf: Fix memory access flags in helper prototypes
After commit 37cce22dbd51 ("bpf: verifier: Refactor helper access type tracking"), the verifier started relying on the access type flags in helper function prototypes to perform memory access optimizations.
Currently, several helper functions utilizing ARG_PTR_TO_MEM lack the corresponding MEM_RDONLY or MEM_WRITE flags. This omission causes the verifier to incorrectly assume that the buffer contents are unchanged across the helper call. Consequently, the verifier may optimize away subsequent reads based on this wrong assumption, leading to correctness issues.
For bpf_get_stack_proto_raw_tp, the original MEM_RDONLY was incorrect since the helper writes to the buffer. Change it to ARG_PTR_TO_UNINIT_MEM which correctly indicates write access to potentially uninitialized memory.
Similar issues were recently addressed for specific helpers in commit ac44dcc788b9 ("bpf: Fix verifier assumptions of bpf_d_path's output buffer") and commit 2eb7648558a7 ("bpf: Specify access type of bpf_sysctl_get_name args").
Fix these prototypes by adding the correct memory access flags.
--- kernel/bpf/helpers.c | 2 +- kernel/bpf/syscall.c | 2 +- kernel/trace/bpf_trace.c | 6 +++--- net/core/filter.c | 20 ++++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-)
kernel/bpf/helpers.c +1 -1
@@ -1077,7 +1077,7 @@ const struct bpf_func_proto bpf_snprintf_proto = { .func = bpf_snprintf, .gpl_only = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM_OR_NULL, + .arg1_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, .arg2_type = ARG_CONST_SIZE_OR_ZERO, .arg3_type = ARG_PTR_TO_CONST_STR, .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
kernel/bpf/syscall.c +1 -1
@@ -6407,7 +6407,7 @@ static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { .func = bpf_kallsyms_lookup_name, .gpl_only = false, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
kernel/trace/bpf_trace.c +3 -3
@@ -1022,7 +1022,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .func = bpf_snprintf_btf, .gpl_only = false, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_WRITE, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg4_type = ARG_CONST_SIZE,
@@ -1526,7 +1526,7 @@ static const struct bpf_func_proto bpf_read_branch_records_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM_OR_NULL, + .arg2_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -1661,7 +1661,7 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, + .arg2_type = ARG_PTR_TO_UNINIT_MEM, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
net/core/filter.c +10 -10
@@ -6399,7 +6399,7 @@ static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE, .arg3_type = ARG_CONST_SIZE, .arg4_type = ARG_ANYTHING, };
@@ -6454,7 +6454,7 @@ static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE, .arg3_type = ARG_CONST_SIZE, .arg4_type = ARG_ANYTHING, };
@@ -8008,9 +8008,9 @@ static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = { .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */ .pkt_access = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg1_size = sizeof(struct iphdr), - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -8040,9 +8040,9 @@ static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = { .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */ .pkt_access = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg1_size = sizeof(struct ipv6hdr), - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -8060,9 +8060,9 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = { .gpl_only = true, /* __cookie_v4_check is GPL */ .pkt_access = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg1_size = sizeof(struct iphdr), - .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg2_size = sizeof(struct tcphdr), };
@@ -8084,9 +8084,9 @@ static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = { .gpl_only = true, /* __cookie_v6_check is GPL */ .pkt_access = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg1_size = sizeof(struct ipv6hdr), - .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM, + .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY, .arg2_size = sizeof(struct tcphdr), };#endif /* CONFIG_SYN_COOKIES */
Signature · 1 line
2.43.0
Eduard Zingerman
maintainerQuoted 29 lines · Zesen Liu
After commit
37cce22dbd51("bpf: verifier: Refactor helper access type tracking"), the verifier started relying on the access type flags in helper function prototypes to perform memory access optimizations.Currently, several helper functions utilizing
ARG_PTR_TO_MEMlack the correspondingMEM_RDONLYorMEM_WRITEflags. This omission causes the verifier to incorrectly assume that the buffer contents are unchanged across the helper call. Consequently, the verifier may optimize away subsequent reads based on this wrong assumption, leading to correctness issues.For
bpf_get_stack_proto_raw_tp, the originalMEM_RDONLYwas incorrect since the helper writes to the buffer. Change it toARG_PTR_TO_UNINIT_MEMwhich correctly indicates write access to potentially uninitialized memory.Similar issues were recently addressed for specific helpers in commit
ac44dcc788b9("bpf: Fix verifier assumptions of bpf_d_path's output buffer") and commit2eb7648558a7("bpf: Specify access type ofbpf_sysctl_get_nameargs").Fix these prototypes by adding the correct memory access flags.
Fixes 37cce22dbd51 ("bpf: verifier: Refactor helper access type tracking")Co-developed-by Shuran Liu <[email protected]>Signed-off-by Shuran Liu <[email protected]>Co-developed-by Peili Gao <[email protected]>Signed-off-by Peili Gao <[email protected]>Co-developed-by Haoran Ni <[email protected]>Signed-off-by Haoran Ni <[email protected]>Signed-off-by Zesen Liu <[email protected]>
Quoted 1 line
---
I looked trough the helpers annotated with MEM_WRITE in this patch, indeed the write annotation is missing from these helpers.
In conjunction with the following logic in verifier.c:check_func_arg:
case ARG_PTR_TO_MEM:
/* The access to this pointer is only checked when we hit the
* next is_mem_size argument below.
*/
meta->raw_mode = arg_type & MEM_UNINIT;
if (arg_type & MEM_FIXED_SIZE) {
err = check_helper_mem_access(env, regno, fn->arg_size[arg],
arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// arguments considered read-only by default
false, meta);
if (err)
return err;
if (arg_type & MEM_ALIGNED)
err = check_ptr_alignment(env, reg, 0, fn->arg_size[arg], true);
}
break;This patch fixes a real problem.
[...]
Quoted 3 lines
index
fe28d86f7c35..59c2394981c7100644kernel/trace/bpf_trace.c +0
-0
[...]
Quoted 10 lines
kernel/trace/bpf_trace.c +1
-1@@ -1526,7 +1526,7 @@ static const struct bpf_func_proto bpf_read_branch_records_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM_OR_NULL, + .arg2_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { · line 1664
Quoted 5 lines
kernel/trace/bpf_trace.c +1
-1@@ -1661,7 +1661,7 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, + .arg2_type = ARG_PTR_TO_UNINIT_MEM,
Q: why ARG_PTR_TO_UNINIT_MEM here, but not for a previous function and
not for snprintf variants?
Quoted 3 lines
kernel/trace/bpf_trace.c +0
-0.arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
[...]
[...]
Quoted 4 lines · Eduard Zingerman
Q: why
ARG_PTR_TO_UNINIT_MEMhere, but not for a previous function and
not for snprintf variants?
For bpf_get_stack_proto_raw_tp, I chose ARG_PTR_TO_UNINIT_MEM to be consistent with its siblings:
• bpf_get_stack_proto_tp (bpf_trace.c:1425)
• bpf_get_stack_proto (stackmap.c:525)
• bpf_get_stack_sleepable_proto (stackmap.c:541)
All of these are wrappers around the same core function bpf_get_stack() / __bpf_get_stack(), passing the buffer with identical semantics, and all use ARG_PTR_TO_UNINIT_MEM.
Eduard Zingerman
maintainerQuoted 7 lines · Zesen Liu
[...]
Quoted 4 lines · Eduard Zingerman
Q: why
ARG_PTR_TO_UNINIT_MEMhere, but not for a previous function and
not for snprintf variants?
Quoted 7 lines
For
bpf_get_stack_proto_raw_tp, I choseARG_PTR_TO_UNINIT_MEMto be consistent with its siblings:•
bpf_get_stack_proto_tp(bpf_trace.c:1425)
•bpf_get_stack_proto(stackmap.c:525)
•bpf_get_stack_sleepable_proto(stackmap.c:541)All of these are wrappers around the same core function
bpf_get_stack()/__bpf_get_stack(), passing the buffer with identical semantics, and all useARG_PTR_TO_UNINIT_MEM.
Ack, makes sense, thank you for explaining.
PATCH RESEND v2 2/2 bpf: Require ARG_PTR_TO_MEM with memory flag
In reply to Zesen LiuAdd check to ensure that ARG_PTR_TO_MEM is used with either MEM_WRITE or MEM_RDONLY.
Using ARG_PTR_TO_MEM alone without tags does not make sense because:
- If the helper does not change the argument, missing MEM_RDONLY causes the verifier to incorrectly reject a read-only buffer.
- If the helper does change the argument, missing MEM_WRITE causes the verifier to incorrectly assume the memory is unchanged, leading to errors in code optimization.
--- kernel/bpf/verifier.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
kernel/bpf/verifier.c +17 -0
@@ -10349,10 +10349,27 @@ static bool check_btf_id_ok(const struct bpf_func_proto *fn)return true; } +static bool check_mem_arg_rw_flag_ok (const struct bpf_func_proto *fn) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (fn->arg_type); i++) { +enum bpf_arg_type arg_type = fn->arg_type[i]; + +if (base_type (arg_type) !=ARG_PTR_TO_MEM ) +continue ; +if (!(arg_type & (MEM_WRITE |MEM_RDONLY ))) +return false; + } + +return true; +} +static int check_func_proto (const struct bpf_func_proto *fn,int func_id) {return check_raw_mode_ok (fn) &&check_arg_pair_ok (fn) && +check_mem_arg_rw_flag_ok (fn) &&check_btf_id_ok (fn) ?0 : -EINVAL ; }
Signature · 1 line
2.43.0
Eduard Zingerman
maintainerRe: [PATCH bpf RESEND v2 2/2] bpf: Require ARG_PTR_TO_MEM with memory flag
Quoted 11 lines · Zesen Liu
Add check to ensure that
ARG_PTR_TO_MEMis used with eitherMEM_WRITEorMEM_RDONLY.Using
ARG_PTR_TO_MEMalone without tags does not make sense because:- If the helper does not change the argument, missing
MEM_RDONLYcauses the verifier to incorrectly reject a read-only buffer.
- If the helper does change the argument, missingMEM_WRITEcauses the verifier to incorrectly assume the memory is unchanged, leading to errors in code optimization.
Quoted 8 lines
Co-developed-by Shuran Liu <[email protected]>Signed-off-by Shuran Liu <[email protected]>Co-developed-by Peili Gao <[email protected]>Signed-off-by Peili Gao <[email protected]>Co-developed-by Haoran Ni <[email protected]>Signed-off-by Haoran Ni <[email protected]>Signed-off-by Zesen Liu <[email protected]>---
Note: this patch no longer applies properly because of the change in
the check_func_proto() signature.
[...]
Re: [PATCH bpf RESEND v2 2/2] bpf: Require ARG_PTR_TO_MEM with memory flag
[...]
Quoted 4 lines · Eduard Zingerman
Note: this patch no longer applies properly because of the change in
thecheck_func_proto()signature.
Thanks for pointing this out. I will rebase to bpf-next in v3 to address the check_func_proto() signature changes.