bpf: Fix bpf_strtol and bpf_strtoul helpers for 32bit
Subsystems (9)
Daniel Borkmann
maintainerPATCH v4 1/8 bpf: Fix bpf_strtol and bpf_strtoul helpers for 32bit
The bpf_strtol() and bpf_strtoul() helpers are currently broken on 32bit:
The argument type ARG_PTR_TO_LONG is BPF-side "long", not kernel-side "long" and therefore always considered fixed 64bit no matter if 64 or 32bit underlying architecture.
This contract breaks in case of the two mentioned helpers since their BPF_CALL definition for the helpers was added with {unsigned,}long *res. Meaning, the transition from BPF-side "long" (BPF program) to kernel-side "long" (BPF helper) breaks here.
Both helpers call __bpf_strtoll() with "long long" correctly, but later assigning the result into 32-bit "*(long *)" on 32bit architectures. From a BPF program point of view, this means upper bits will be seen as uninitialised.
Therefore, fix both BPF_CALL signatures to {s,u}64 types to fix this situation.
Now, changing also uapi/bpf.h helper documentation which generates bpf_helper_defs.h for BPF programs is tricky: Changing signatures there to __{s,u}64 would trigger compiler warnings (incompatible pointer types passing 'long *' to parameter of type '__s64 *' (aka 'long long *')) for existing BPF programs.
Leaving the signatures as-is would be fine as from BPF program point of view it is still BPF-side "long" and thus equivalent to __{s,u}64 on 64 or 32bit underlying architectures.
Note that bpf_strtol() and bpf_strtoul() are the only helpers with this issue.
---
v3 -> v4:
- added patch
kernel/bpf/helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +2 -2
@@ -518,7 +518,7 @@ static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags, } BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags, - long *, res) + s64 *, res) {long long _res;int err;
@@ -543,7 +543,7 @@ const struct bpf_func_proto bpf_strtol_proto = { }; BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags, - unsigned long *, res) + u64 *, res) {unsigned long long _res;bool is_negative;
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v4 2/8 bpf: Remove truncation test in bpf_strtol and bpf_strtoul helpers
Both bpf_strtol() and bpf_strtoul() helpers passed a temporary "long long" respectively "unsigned long long" to __bpf_strtoll() / __bpf_strtoull().
Later, the result was checked for truncation via _res != ({unsigned,} long)_res as the destination buffer for the BPF helpers was of type {unsigned,} long which is 32bit on 32bit architectures.
Given the latter was a bug in the helper signatures where the destination buffer got adjusted to {s,u}64, the truncation check can now be removed.
---
v3 -> v4:
- added patch
kernel/bpf/helpers.c | 4 ---- 1 file changed, 4 deletions(-)
kernel/bpf/helpers.c +0 -4
@@ -526,8 +526,6 @@ BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags, err =__bpf_strtoll (buf, buf_len, flags, &_res);if (err <0 )return err; -if (_res != (long )_res) -return -ERANGE ; *res = _res;return err; }
@@ -554,8 +552,6 @@ BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,return err;if (is_negative)return -EINVAL ; -if (_res != (unsigned long )_res) -return -ERANGE ; *res = _res;return err; }
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 2/8] bpf: Remove truncation test in bpf_strtol and bpf_strtoul helpers
Quoted 8 lines · Daniel Borkmann
Both
bpf_strtol()andbpf_strtoul()helpers passed a temporary "long long" respectively "unsigned long long" to__bpf_strtoll()/__bpf_strtoull().Later, the result was checked for truncation via
_res!= ({unsigned,} long)_res as the destination buffer for the BPF helpers was of type {unsigned,} long which is 32bit on 32bit architectures.
Quoted 8 lines
Given the latter was a bug in the helper signatures where the destination buffer got adjusted to {s,u}64, the truncation check can now be removed.
Signed-off-by Daniel Borkmann <[email protected]>---
v3 -> v4:
- added patch
Quoted 28 lines
kernel/bpf/helpers.c | 4 ---- 1 file changed, 4 deletions(-)kernel/bpf/helpers.c +0
-5@@ -526,8 +526,6 @@ BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags, err =__bpf_strtoll (buf, buf_len, flags, &_res);if (err <0 )return err; -if (_res != (long )_res) -return -ERANGE ; *res = _res;return err; }@@ -554,8 +552,6 @@ BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,return err;if (is_negative)return -EINVAL ; -if (_res != (unsigned long )_res) -return -ERANGE ; *res = _res;return err; } --2.43.0
Daniel Borkmann
maintainerPATCH v4 3/8 bpf: Fix helper writes to read-only maps
In reply to Daniel BorkmannLonial found an issue that despite user- and BPF-side frozen BPF map (like in case of .rodata), it was still possible to write into it from a BPF program side through specific helpers having ARG_PTR_TO_{LONG,INT} as arguments.
In check_func_arg() when the argument is as mentioned, the meta->raw_mode is never set. Later, check_helper_mem_access(), under the case of PTR_TO_MAP_VALUE as register base type, it assumes BPF_READ for the subsequent call to check_map_access_type() and given the BPF map is read-only it succeeds.
The helpers really need to be annotated as ARG_PTR_TO_{LONG,INT} | MEM_UNINIT when results are written into them as opposed to read out of them. The latter indicates that it's okay to pass a pointer to uninitialized memory as the memory is written to anyway.
However, ARG_PTR_TO_{LONG,INT} is a special case of ARG_PTR_TO_FIXED_SIZE_MEM just with additional alignment requirement. So it is better to just get rid of the ARG_PTR_TO_{LONG,INT} special cases altogether and reuse the fixed size memory types. For this, add MEM_ALIGNED to additionally ensure alignment given these helpers write directly into the args via *<ptr> = val. The .arg*_size has been initialized reflecting the actual sizeof(*<ptr>).
MEM_ALIGNED can only be used in combination with MEM_FIXED_SIZE annotated argument types, since in !MEM_FIXED_SIZE cases the verifier does not know the buffer size a priori and therefore cannot blindly write *<ptr> = val.
---
v1 -> v2:
- switch to MEM_FIXED_SIZE
v3 -> v4:
- fixed 64bit in strto{u,}l (Alexei)
include/linux/bpf.h | 7 +++++-- kernel/bpf/helpers.c | 8 ++++++-- kernel/bpf/syscall.c | 4 +++- kernel/bpf/verifier.c | 38 +++++--------------------------------- kernel/trace/bpf_trace.c | 8 ++++++-- net/core/filter.c | 8 ++++++-- 6 files changed, 31 insertions(+), 42 deletions(-)
include/linux/bpf.h +5 -2
@@ -695,6 +695,11 @@ enum bpf_type_flag {/* DYNPTR points to xdp_buff */ DYNPTR_TYPE_XDP =BIT (16 +BPF_BASE_TYPE_BITS ), +/* Memory must be aligned on some architectures, used in combination with +* MEM_FIXED_SIZE. +*/ +MEM_ALIGNED =BIT (17 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, };
@@ -732,8 +737,6 @@ enum bpf_arg_type {ARG_ANYTHING ,/* any (initialized) argument is ok */ ARG_PTR_TO_SPIN_LOCK ,/* pointer to bpf_spin_lock */ ARG_PTR_TO_SOCK_COMMON ,/* pointer to sock_common */ -ARG_PTR_TO_INT ,/* pointer to int */ -ARG_PTR_TO_LONG ,/* pointer to long */ ARG_PTR_TO_SOCKET ,/* pointer to bpf_sock (fullsock) */ ARG_PTR_TO_BTF_ID ,/* pointer to in-kernel struct */ ARG_PTR_TO_RINGBUF_MEM, /* pointer to dynamically reserved ringbuf memory */
kernel/bpf/helpers.c +6 -2
@@ -537,7 +537,9 @@ const struct bpf_func_proto bpf_strtol_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg4_size = sizeof(s64), }; BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
@@ -563,7 +565,9 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg4_size = sizeof(u64), }; BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
kernel/bpf/syscall.c +3 -1
@@ -5954,7 +5954,9 @@ static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { .arg1_type = ARG_PTR_TO_MEM, .arg2_type = ARG_CONST_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg4_size = sizeof(u64), }; static const struct bpf_func_proto *
kernel/bpf/verifier.c +5 -33
@@ -8301,16 +8301,6 @@ static bool arg_type_is_dynptr(enum bpf_arg_type type)return base_type (type) ==ARG_PTR_TO_DYNPTR ; } -static int int_ptr_type_to_size (enum bpf_arg_type type) -{ -if (type ==ARG_PTR_TO_INT ) -return sizeof (u32); -else if (type ==ARG_PTR_TO_LONG ) -return sizeof (u64); - -return -EINVAL ; -} -static int resolve_map_arg_type (struct bpf_verifier_env *env,const struct bpf_call_arg_meta *meta,enum bpf_arg_type *arg_type)
@@ -8383,16 +8373,6 @@ static const struct bpf_reg_types mem_types = { }, }; -static const struct bpf_reg_types int_ptr_types = { - .types = { -PTR_TO_STACK , -PTR_TO_PACKET , -PTR_TO_PACKET_META , -PTR_TO_MAP_KEY , -PTR_TO_MAP_VALUE , - }, -}; -static const struct bpf_reg_types spin_lock_types = { .types = {PTR_TO_MAP_VALUE ,
@@ -8453,8 +8433,6 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, [ARG_PTR_TO_MEM] = &mem_types, [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, - [ARG_PTR_TO_INT] = &int_ptr_types, - [ARG_PTR_TO_LONG] = &int_ptr_types, [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, [ARG_PTR_TO_FUNC] = &func_ptr_types, [ARG_PTR_TO_STACK] = &stack_ptr_types,
@@ -9020,6 +8998,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, fn->arg_size[arg], false, meta); +if (err) +return err; +if (arg_type &MEM_ALIGNED ) + err =check_ptr_alignment (env, reg,0 , + fn->arg_size[arg], true); }break ;case ARG_CONST_SIZE :
@@ -9044,17 +9027,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,if (err)return err;break ; -case ARG_PTR_TO_INT : -case ARG_PTR_TO_LONG : - { -int size =int_ptr_type_to_size (arg_type); - - err =check_helper_mem_access (env, regno, size, false, meta); -if (err) -return err; - err =check_ptr_alignment (env, reg,0 , size, true); -break ; - }case ARG_PTR_TO_CONST_STR : { err =check_reg_const_str (env, reg, regno);
kernel/trace/bpf_trace.c +6 -2
@@ -1202,7 +1202,9 @@ static const struct bpf_func_proto bpf_get_func_arg_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_LONG, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u64), }; BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
@@ -1218,7 +1220,9 @@ static const struct bpf_func_proto bpf_get_func_ret_proto = { .func = get_func_ret, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_LONG, + .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg2_size = sizeof(u64), }; BPF_CALL_1(get_func_arg_cnt, void *, ctx)
net/core/filter.c +6 -2
@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32), .arg4_type = ARG_ANYTHING, .arg5_type = ARG_ANYTHING, };
@@ -6357,7 +6359,9 @@ static const struct bpf_func_proto bpf_xdp_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32), .arg4_type = ARG_ANYTHING, .arg5_type = ARG_ANYTHING, };
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 3/8] bpf: Fix helper writes to read-only maps
Quoted 37 lines · Daniel Borkmann
Lonial found an issue that despite user- and BPF-side frozen BPF map (like in case of .rodata), it was still possible to write into it from a BPF program side through specific helpers having ARG_PTR_TO_{LONG,INT} as arguments.
In
check_func_arg()when the argument is as mentioned, themeta->raw_modeis never set. Later,check_helper_mem_access(), under the case ofPTR_TO_MAP_VALUEas register base type, it assumesBPF_READfor the subsequent call tocheck_map_access_type()and given the BPF map is read-only it succeeds.The helpers really need to be annotated as ARG_PTR_TO_{LONG,INT} |
MEM_UNINITwhen results are written into them as opposed to read out of them. The latter indicates that it's okay to pass a pointer to uninitialized memory as the memory is written to anyway.However, ARG_PTR_TO_{LONG,INT} is a special case of
ARG_PTR_TO_FIXED_SIZE_MEMjust with additional alignment requirement. So it is better to just get rid of the ARG_PTR_TO_{LONG,INT} special cases altogether and reuse the fixed size memory types. For this, addMEM_ALIGNEDto additionally ensure alignment given these helpers write directly into the args via *<ptr> = val. The .arg*_size has been initialized reflecting the actualsizeof(*<ptr>).
MEM_ALIGNEDcan only be used in combination withMEM_FIXED_SIZEannotated argument types, since in !MEM_FIXED_SIZE cases the verifier does not know the buffer size a priori and therefore cannot blindly write *<ptr> = val.Fixes 57c3bb725a3d ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")Reported-by Lonial Con <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>---
v1 -> v2:
- switch toMEM_FIXED_SIZE
v3 -> v4:
- fixed 64bit in strto{u,}l (Alexei)
Quoted 8 lines
include/linux/bpf.h | 7 +++++-- kernel/bpf/helpers.c | 8 ++++++-- kernel/bpf/syscall.c | 4 +++- kernel/bpf/verifier.c | 38 +++++--------------------------------- kernel/trace/bpf_trace.c | 8 ++++++-- net/core/filter.c | 8 ++++++-- 6 files changed, 31 insertions(+), 42 deletions(-)
Very neat. I only have stylistic nits, but LGTM anyways
[...]
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtol_proto = { · line 541
Quoted 11 lines
kernel/bpf/helpers.c +2
-1@@ -537,7 +537,9 @@ const struct bpf_func_proto bpf_strtol_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type =ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED,
nit: I wouldn't wrap the line here and everywhere else
Quoted 14 lines
kernel/bpf/helpers.c +4
-1+ .arg4_size = sizeof(s64), }; BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,@@ -563,7 +565,9 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg4_size = sizeof(u64), };
[...]
Quoted 13 lines
static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE,kernel/bpf/helpers.c +0
-2@@ -8453,8 +8433,6 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, [ARG_PTR_TO_MEM] = &mem_types, [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, - [ARG_PTR_TO_INT] = &int_ptr_types, - [ARG_PTR_TO_LONG] = &int_ptr_types, [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, [ARG_PTR_TO_FUNC] = &func_ptr_types, [ARG_PTR_TO_STACK] = &stack_ptr_types,
kernel/bpf/helpers.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 9005
Quoted 8 lines
kernel/bpf/helpers.c +5
-0@@ -9020,6 +8998,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, fn->arg_size[arg], false, meta); +if (err) +return err; +if (arg_type &MEM_ALIGNED ) + err =check_ptr_alignment (env, reg,0 , + fn->arg_size[arg], true);
nit: we should take advantage of 100 character lines and make this streamlined:
@@ -9016,11 +9016,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
* 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], false,
- meta);
- }
+ if (arg_type & MEM_FIXED_SIZE)
+ err = check_helper_mem_access(env, regno,
fn->arg_size[arg], false, meta);
+ if (arg_type & MEM_ALIGNED)
+ err = err ?: check_ptr_alignment(env, reg, 0,
fn->arg_size[arg], true);
break;Quoted 7 lines
kernel/bpf/helpers.c +0
-0}break ;case ARG_CONST_SIZE :@@ -9044,17 +9027,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,if (err)return err;break ;
[...]
Re: [PATCH bpf-next v4 3/8] bpf: Fix helper writes to read-only maps
Quoted 125 lines · Andrii Nakryiko
Quoted 37 lines · Daniel Borkmann
Lonial found an issue that despite user- and BPF-side frozen BPF map (like in case of .rodata), it was still possible to write into it from a BPF program side through specific helpers having ARG_PTR_TO_{LONG,INT} as arguments.
In
check_func_arg()when the argument is as mentioned, themeta->raw_modeis never set. Later,check_helper_mem_access(), under the case ofPTR_TO_MAP_VALUEas register base type, it assumesBPF_READfor the subsequent call tocheck_map_access_type()and given the BPF map is read-only it succeeds.The helpers really need to be annotated as ARG_PTR_TO_{LONG,INT} |
MEM_UNINITwhen results are written into them as opposed to read out of them. The latter indicates that it's okay to pass a pointer to uninitialized memory as the memory is written to anyway.However, ARG_PTR_TO_{LONG,INT} is a special case of
ARG_PTR_TO_FIXED_SIZE_MEMjust with additional alignment requirement. So it is better to just get rid of the ARG_PTR_TO_{LONG,INT} special cases altogether and reuse the fixed size memory types. For this, addMEM_ALIGNEDto additionally ensure alignment given these helpers write directly into the args via *<ptr> = val. The .arg*_size has been initialized reflecting the actualsizeof(*<ptr>).
MEM_ALIGNEDcan only be used in combination withMEM_FIXED_SIZEannotated argument types, since in !MEM_FIXED_SIZE cases the verifier does not know the buffer size a priori and therefore cannot blindly write *<ptr> = val.Fixes 57c3bb725a3d ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")Reported-by Lonial Con <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>---
v1 -> v2:
- switch toMEM_FIXED_SIZE
v3 -> v4:
- fixed 64bit in strto{u,}l (Alexei)Quoted 8 lines
include/linux/bpf.h | 7 +++++-- kernel/bpf/helpers.c | 8 ++++++-- kernel/bpf/syscall.c | 4 +++- kernel/bpf/verifier.c | 38 +++++--------------------------------- kernel/trace/bpf_trace.c | 8 ++++++-- net/core/filter.c | 8 ++++++-- 6 files changed, 31 insertions(+), 42 deletions(-)Very neat. I only have stylistic nits, but LGTM anyways
Acked-by Andrii Nakryiko <[email protected]>[...]
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtol_proto = { · line 541
Quoted 11 lines
kernel/bpf/helpers.c +2
-1@@ -537,7 +537,9 @@ const struct bpf_func_proto bpf_strtol_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type =ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED,nit: I wouldn't wrap the line here and everywhere else
Quoted 14 lines
kernel/bpf/helpers.c +4
-1+ .arg4_size = sizeof(s64), }; BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,@@ -563,7 +565,9 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_LONG, + .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg4_size = sizeof(u64), };[...]
Quoted 13 lines
static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE,kernel/bpf/helpers.c +0
-2@@ -8453,8 +8433,6 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, [ARG_PTR_TO_MEM] = &mem_types, [ARG_PTR_TO_RINGBUF_MEM] = &ringbuf_mem_types, - [ARG_PTR_TO_INT] = &int_ptr_types, - [ARG_PTR_TO_LONG] = &int_ptr_types, [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, [ARG_PTR_TO_FUNC] = &func_ptr_types, [ARG_PTR_TO_STACK] = &stack_ptr_types,kernel/bpf/helpers.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 9005
Quoted 8 lines
kernel/bpf/helpers.c +5
-0@@ -9020,6 +8998,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, fn->arg_size[arg], false, meta); +if (err) +return err; +if (arg_type &MEM_ALIGNED ) + err =check_ptr_alignment (env, reg,0 , + fn->arg_size[arg], true);nit: we should take advantage of 100 character lines and make this streamlined:
(quoted diff) +4
-5@@ -9016,11 +9016,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, * 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], false, - meta); - } + if (arg_type & MEM_FIXED_SIZE) + err = check_helper_mem_access(env, regno, fn->arg_size[arg], false, meta); + if (arg_type & MEM_ALIGNED) + err = err ?: check_ptr_alignment(env, reg, 0, fn->arg_size[arg], true);
Quoted 1 line
break;
Agree that it's a bit cleaner this way.
Re: [PATCH bpf-next v4 3/8] bpf: Fix helper writes to read-only maps
In reply to Daniel BorkmannQuoted 23 lines · Daniel Borkmann
Lonial found an issue that despite user- and BPF-side frozen BPF map (like in case of .rodata), it was still possible to write into it from a BPF program side through specific helpers having ARG_PTR_TO_{LONG,INT} as arguments.
In
check_func_arg()when the argument is as mentioned, themeta->raw_modeis never set. Later,check_helper_mem_access(), under the case ofPTR_TO_MAP_VALUEas register base type, it assumesBPF_READfor the subsequent call tocheck_map_access_type()and given the BPF map is read-only it succeeds.The helpers really need to be annotated as ARG_PTR_TO_{LONG,INT} |
MEM_UNINITwhen results are written into them as opposed to read out of them. The latter indicates that it's okay to pass a pointer to uninitialized memory as the memory is written to anyway.However, ARG_PTR_TO_{LONG,INT} is a special case of
ARG_PTR_TO_FIXED_SIZE_MEMjust with additional alignment requirement. So it is better to just get rid of the ARG_PTR_TO_{LONG,INT} special cases altogether and reuse the fixed size memory types. For this, addMEM_ALIGNEDto additionally ensure alignment given these helpers write directly into the args via *<ptr> = val. The .arg*_size has been initialized reflecting the actualsizeof(*<ptr>).
Quoted 7 lines
MEM_ALIGNEDcan only be used in combination withMEM_FIXED_SIZEannotated argument types, since in !MEM_FIXED_SIZE cases the verifier does not know the buffer size a priori and therefore cannot blindly write *<ptr> = val.Fixes 57c3bb725a3d ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")Reported-by Lonial Con <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Agree with Andrii's suggestion in silbing thread. That said, logic-wise LGTM
Daniel Borkmann
maintainerPATCH v4 4/8 bpf: Improve check_raw_mode_ok test for MEM_UNINIT-tagged types
In reply to Daniel BorkmannWhen checking malformed helper function signatures, also take other argument types into account aside from just ARG_PTR_TO_UNINIT_MEM.
This concerns (formerly) ARG_PTR_TO_{INT,LONG} given uninitialized memory can be passed there, too.
The func proto sanity check goes back to commit 435faee1aae9 ("bpf, verifier:
add ARG_PTR_TO_RAW_STACK type"), and its purpose was to detect wrong func protos which had more than just one MEM_UNINIT-tagged type as arguments.
The reason more than one is currently not supported is as we mark stack slots with STACK_MISC in check_helper_call() in case of raw mode based on meta.access_size to allow uninitialized stack memory to be passed to helpers when they just write into the buffer.
Probing for base type as well as MEM_UNINIT tagging ensures that other types do not get missed (as it used to be the case for ARG_PTR_TO_{INT,LONG}).
---
v1 -> v2:
- new patch (Shung-Hsi)
v2 -> v3:
- base_type(type) was needed also
kernel/bpf/verifier.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
kernel/bpf/verifier.c +11 -5
@@ -8291,6 +8291,12 @@ static bool arg_type_is_mem_size(enum bpf_arg_type type) type ==ARG_CONST_SIZE_OR_ZERO ; } +static bool arg_type_is_raw_mem (enum bpf_arg_type type) +{ +return base_type (type) ==ARG_PTR_TO_MEM && + type &MEM_UNINIT ; +} +static bool arg_type_is_release (enum bpf_arg_type type) {return type &OBJ_RELEASE ;
@@ -9343,15 +9349,15 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn) {int count =0 ; -if (fn->arg1_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg1_type)) count++; -if (fn->arg2_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg2_type)) count++; -if (fn->arg3_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg3_type)) count++; -if (fn->arg4_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg4_type)) count++; -if (fn->arg5_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg5_type)) count++;/* We only support one arg being in raw mode at the moment,
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 4/8] bpf: Improve check_raw_mode_ok test for MEM_UNINIT-tagged types
Quoted 22 lines · Daniel Borkmann
When checking malformed helper function signatures, also take other argument types into account aside from just
ARG_PTR_TO_UNINIT_MEM.This concerns (formerly) ARG_PTR_TO_{INT,LONG} given uninitialized memory can be passed there, too.
The func proto sanity check goes back to commit
435faee1aae9("bpf, verifier:
addARG_PTR_TO_RAW_STACKtype"), and its purpose was to detect wrong func protos which had more than just one MEM_UNINIT-tagged type as arguments.The reason more than one is currently not supported is as we mark stack slots with
STACK_MISCincheck_helper_call()in case of raw mode based on meta.access_size to allow uninitialized stack memory to be passed to helpers when they just write into the buffer.Probing for base type as well as
MEM_UNINITtagging ensures that other types do not get missed (as it used to be the case for ARG_PTR_TO_{INT,LONG}).Fixes 57c3bb725a3d ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")Reported-by Shung-Hsi Yu <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Quoted 9 lines
---
v1 -> v2:
- new patch (Shung-Hsi)
v2 -> v3:
-base_type(type)was needed alsokernel/bpf/verifier.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
LGTM (though I like straight unwrapped lines ;))
Quoted 41 lines
kernel/bpf/verifier.c +11
-6@@ -8291,6 +8291,12 @@ static bool arg_type_is_mem_size(enum bpf_arg_type type) type ==ARG_CONST_SIZE_OR_ZERO ; } +static bool arg_type_is_raw_mem (enum bpf_arg_type type) +{ +return base_type (type) ==ARG_PTR_TO_MEM && + type &MEM_UNINIT ; +} +static bool arg_type_is_release (enum bpf_arg_type type) {return type &OBJ_RELEASE ;@@ -9343,15 +9349,15 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn) {int count =0 ; -if (fn->arg1_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg1_type)) count++; -if (fn->arg2_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg2_type)) count++; -if (fn->arg3_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg3_type)) count++; -if (fn->arg4_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg4_type)) count++; -if (fn->arg5_type ==ARG_PTR_TO_UNINIT_MEM ) +if (arg_type_is_raw_mem (fn->arg5_type)) count++;/* We only support one arg being in raw mode at the moment, -- 2.43.0
Re: [PATCH bpf-next v4 4/8] bpf: Improve check_raw_mode_ok test for MEM_UNINIT-tagged types
In reply to Daniel BorkmannQuoted 15 lines · Daniel Borkmann
When checking malformed helper function signatures, also take other argument types into account aside from just
ARG_PTR_TO_UNINIT_MEM.This concerns (formerly) ARG_PTR_TO_{INT,LONG} given uninitialized memory can be passed there, too.
The func proto sanity check goes back to commit
435faee1aae9("bpf, verifier:
addARG_PTR_TO_RAW_STACKtype"), and its purpose was to detect wrong func protos which had more than just one MEM_UNINIT-tagged type as arguments.The reason more than one is currently not supported is as we mark stack slots with
STACK_MISCincheck_helper_call()in case of raw mode based on meta.access_size to allow uninitialized stack memory to be passed to helpers when they just write into the buffer.
Quoted 6 lines
Probing for base type as well as
MEM_UNINITtagging ensures that other types do not get missed (as it used to be the case for ARG_PTR_TO_{INT,LONG}).Fixes 57c3bb725a3d ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")Reported-by Shung-Hsi Yu <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Daniel Borkmann
maintainerPATCH v4 5/8 bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
In reply to Daniel BorkmannFor all non-tracing helpers which formerly had ARG_PTR_TO_{LONG,INT} as input arguments, zero the value for the case of an error as otherwise it could leak memory. For tracing, it is not needed given CAP_PERFMON can already read all kernel memory anyway hence bpf_get_func_arg() and bpf_get_func_ret() is skipped in here.
Also, rearrange the MTU checker helpers a bit to among other nit fixes consolidate flag checks such that we only need to zero in one location with regards to malformed flag inputs.
---
v1 -> v2:
- only set *mtu_len in error path (Alexei)
kernel/bpf/helpers.c | 2 ++ kernel/bpf/syscall.c | 1 + net/core/filter.c | 35 +++++++++++++++++------------------ 3 files changed, 20 insertions(+), 18 deletions(-)
kernel/bpf/helpers.c +2 -0
@@ -523,6 +523,7 @@ BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,long long _res;int err; + *res =0 ; err =__bpf_strtoll (buf, buf_len, flags, &_res);if (err <0 )return err;
@@ -549,6 +550,7 @@ BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,bool is_negative;int err; + *res =0 ; err =__bpf_strtoull (buf, buf_len, flags, &_res, &is_negative);if (err <0 )return err;
kernel/bpf/syscall.c +1 -0
@@ -5934,6 +5934,7 @@ static const struct bpf_func_proto bpf_sys_close_proto = { BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) { + *res =0 ;if (flags)return -EINVAL ;
net/core/filter.c +17 -18
@@ -6264,18 +6264,19 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,int skb_len, dev_len;int mtu; -if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) { + *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + } mtu =READ_ONCE (dev->mtu); - dev_len = mtu + dev->hard_header_len;/* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
@@ -6286,10 +6287,10 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, ret =BPF_MTU_CHK_RET_SUCCESS ; goto out; } -/* At this point, skb->len exceed MTU, but as it include length of all -* segments, it can still be below MTU. The SKB can possibly get -* re-segmented in transmit path (see validate_xmit_skb). Thus, user -* must choose if segs are to be MTU checked. +/* At this point, skb->len exceeds MTU, but as it includes the length +* of all segments, it can still be below MTU. The skb can possibly +* get re-segmented in transmit path (see validate_xmit_skb). Thus, +* the user must choose if segments are to be MTU checked. */ if (skb_is_gso (skb)) { ret =BPF_MTU_CHK_RET_SUCCESS ;
@@ -6299,9 +6300,7 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, ret =BPF_MTU_CHK_RET_SEGS_TOOBIG ; } out: -/* BPF verifier guarantees valid pointer */ *mtu_len = mtu; -return ret; }
@@ -6314,16 +6313,18 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,int mtu, dev_len;/* XDP variant doesn't support multi-buffer segment check (yet) */ -if (unlikely (flags)) +if (unlikely (flags)) { + *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + } mtu =READ_ONCE (dev->mtu); - -/* Add L2-header as dev MTU is L3 size */ dev_len = mtu + dev->hard_header_len;/* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
@@ -6334,9 +6335,7 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,if (xdp_len > dev_len) ret =BPF_MTU_CHK_RET_FRAG_NEEDED ; -/* BPF verifier guarantees valid pointer */ *mtu_len = mtu; -return ret; }
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Quoted 62 lines · Daniel Borkmann
For all non-tracing helpers which formerly had ARG_PTR_TO_{LONG,INT} as input arguments, zero the value for the case of an error as otherwise it could leak memory. For tracing, it is not needed given
CAP_PERFMONcan already read all kernel memory anyway hencebpf_get_func_arg()andbpf_get_func_ret()is skipped in here.Also, rearrange the MTU checker helpers a bit to among other nit fixes consolidate flag checks such that we only need to zero in one location with regards to malformed flag inputs.
Fixes 8a67f2de9b1d ("bpf: expose bpf_strtol and bpf_strtoul to all program types")Fixes d7a4cb9b6705 ("bpf: Introduce bpf_strtol and bpf_strtoul helpers")Signed-off-by Daniel Borkmann <[email protected]>---
v1 -> v2:
- only set*mtu_lenin error path (Alexei)kernel/bpf/helpers.c | 2 ++ kernel/bpf/syscall.c | 1 + net/core/filter.c | 35 +++++++++++++++++------------------ 3 files changed, 20 insertions(+), 18 deletions(-)kernel/bpf/helpers.c +2
-0@@ -523,6 +523,7 @@ BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,long long _res;int err; + *res =0 ; err =__bpf_strtoll (buf, buf_len, flags, &_res);if (err <0 )return err;@@ -549,6 +550,7 @@ BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,bool is_negative;int err; + *res =0 ; err =__bpf_strtoull (buf, buf_len, flags, &_res, &is_negative);if (err <0 )return err;kernel/bpf/syscall.c +1
-0@@ -5934,6 +5934,7 @@ static const struct bpf_func_proto bpf_sys_close_proto = { BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) { + *res =0 ;if (flags)return -EINVAL ;net/core/filter.c +0
-0@@ -6264,18 +6264,19 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,int skb_len, dev_len;int mtu;
net/core/filter.c BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, · line 6271
Quoted 10 lines
net/core/filter.c +4
-4-if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) { + *mtu_len =0 ;return -EINVAL ; + }
meh, why? you have *mtu_len = 0 below anyways, so there is already duplication. I'd rather have extra *mtu_len than much more convoluted condition
Quoted 13 lines
net/core/filter.c +3
-2dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + } mtu =READ_ONCE (dev->mtu); - dev_len = mtu + dev->hard_header_len;/* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
[...]
Re: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
In reply to Daniel BorkmannQuoted 7 lines · Daniel Borkmann
net/core/filter.c +2
-4-if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) {
net/core/filter.c BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, · line 6277
Quoted 10 lines
net/core/filter.c +5
-1+ *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + }
I don't understand this mtu_len clearing.
My earlier comment was that mtu is in&out argument. The program has to set it to something. It cannot be uninit. So zeroing it on error looks very odd.
In that sense the patch 3 looks wrong. Instead of:
@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_ANYTHING,
- .arg3_type = ARG_PTR_TO_INT,
+ .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM |+ MEM_UNINIT | MEM_ALIGNED,
+ .arg3_size = sizeof(u32),
MEM_UNINIT should be removed, because bpf_xdp_check_mtu, bpf_skb_check_mtu will read it.
If there is a program out there that calls this helper without initializing mtu_len it will be rejected after we fix it, but I think that's a good thing.
Passing random mtu_len and let helper do:
skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
is just bad.
pw-bot: cr
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Quoted 40 lines · Alexei Starovoitov
Quoted 7 lines · Daniel Borkmann
net/core/filter.c +2
-4-if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) {net/core/filter.c BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, · line 6277
Quoted 10 lines
net/core/filter.c +5
-1+ *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + }I don't understand this
mtu_lenclearing.My earlier comment was that mtu is in&out argument. The program has to set it to something. It cannot be uninit. So zeroing it on error looks very odd.
In that sense the patch 3 looks wrong. Instead of:
(quoted diff) +3
-1@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32),
MEM_UNINITshould be removed, becausebpf_xdp_check_mtu,bpf_skb_check_mtuwill read it.
Quoted 8 lines
If there is a program out there that calls this helper without initializing
mtu_lenit will be rejected after we fix it, but I think that's a good thing.
Passing randommtu_lenand let helper do:
skb_len=*mtu_len?*mtu_len+dev->hard_header_len:skb->len;is just bad.
Ok, fair. Removing MEM_UNINIT sounds reasonable, was mostly thinking that even if its garbage MTU being passed in, mtu_len gets filled in either case (BPF_MTU_CHK_RET_{SUCCESS,FRAG_NEEDED}) assuming no invalid ifindex e.g.:
__u32 mtu_len; bpf_skb_check_mtu(skb, skb->ifindex, &mtu_len, 0, 0);
Will spin a v5 then.
Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Quoted 55 lines · Daniel Borkmann
Quoted 40 lines · Alexei Starovoitov
Quoted 11 lines · Daniel Borkmann
- if (
unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
- return-EINVAL;
-
- if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) + if (unlikely((flags & ~(BPF_MTU_CHK_SEGS)) ||
+ (flags &BPF_MTU_CHK_SEGS&& (len_diff||*mtu_len)))) { +*mtu_len= 0;
return-EINVAL;
+ }Quoted 6 lines
dev =
__dev_via_ifindex(dev, ifindex);
- if (unlikely(!dev))
+ if (unlikely(!dev)) {
+*mtu_len= 0;
return-ENODEV;
+ }I don't understand this
mtu_lenclearing.My earlier comment was that mtu is in&out argument. The program has to set it to something. It cannot be uninit. So zeroing it on error looks very odd.
In that sense the patch 3 looks wrong. Instead of:
(quoted diff) +3
-1@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32),
MEM_UNINITshould be removed, becausebpf_xdp_check_mtu,bpf_skb_check_mtuwill read it.Quoted 8 lines
If there is a program out there that calls this helper without initializing
mtu_lenit will be rejected after we fix it, but I think that's a good thing.
Passing randommtu_lenand let helper do:
skb_len=*mtu_len?*mtu_len+dev->hard_header_len:skb->len;is just bad.
Ok, fair. Removing
MEM_UNINITsounds reasonable, was mostly thinking that even if its garbage MTU being passed in,mtu_lengets filled in either case (BPF_MTU_CHK_RET_{SUCCESS,FRAG_NEEDED}) assuming no invalid ifindex e.g.:
Quoted 2 lines
__u32mtu_len;
bpf_skb_check_mtu(skb, skb->ifindex, &mtu_len, 0, 0);
Getting back at this, removing MEM_UNINIT needs more verifier rework:MEM_UNINIT right now implies two things actually: i) write into memory, ii) memory does not have to be initialized. If we lift MEM_UNINIT, it then becomes: i) read into memory, ii) memory must be initialized.
This means that for bpf_*_check_mtu() we're readding the issue we're trying to fix, that is, it would then be able to write back into things like .rodata maps.
My suggestion is for this series is to go with MEM_UNINIT tag and clearing on error case to avoid leaking, and then in a subsequent series to break up MEM_UNINIT in the verifier into two properties:MEM_WRITE and MEM_UNINIT to better declare intent of the helpers. Then the bpf_*_check_mtu() will have MEM_WRITE but not MEM_UNINIT.
Thoughts? (If preference is to further extend this series, I can also look into that ofc.)
Thanks,
Daniel
Re: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Quoted 69 lines · Daniel Borkmann
Quoted 55 lines · Daniel Borkmann
Quoted 40 lines · Alexei Starovoitov
Quoted 7 lines · Daniel Borkmann
net/core/filter.c +2
-4-if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) {net/core/filter.c BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, · line 6277
Quoted 10 lines
net/core/filter.c +5
-1+ *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + }I don't understand this
mtu_lenclearing.My earlier comment was that mtu is in&out argument. The program has to set it to something. It cannot be uninit. So zeroing it on error looks very odd.
In that sense the patch 3 looks wrong. Instead of:
(quoted diff) +3
-1@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32),
MEM_UNINITshould be removed, becausebpf_xdp_check_mtu,bpf_skb_check_mtuwill read it.Quoted 8 lines
If there is a program out there that calls this helper without initializing
mtu_lenit will be rejected after we fix it, but I think that's a good thing.
Passing randommtu_lenand let helper do:
skb_len=*mtu_len?*mtu_len+dev->hard_header_len:skb->len;is just bad.
Ok, fair. Removing
MEM_UNINITsounds reasonable, was mostly thinking that even if its garbage MTU being passed in,mtu_lengets filled in either case (BPF_MTU_CHK_RET_{SUCCESS,FRAG_NEEDED}) assuming no invalid ifindex e.g.:Quoted 2 lines
__u32 mtu_len; bpf_skb_check_mtu(skb, skb->ifindex, &mtu_len, 0, 0);Getting back at this, removing
MEM_UNINITneeds more verifier rework:MEM_UNINITright now implies two things actually: i) write into memory, ii) memory does not have to be initialized. If we liftMEM_UNINIT, it then becomes: i) read into memory, ii) memory must be initialized.This means that for bpf_*_check_mtu() we're readding the issue we're trying to fix, that is, it would then be able to write back into things like .rodata maps.
Quoted 8 lines
My suggestion is for this series is to go with
MEM_UNINITtag and clearing on error case to avoid leaking, and then in a subsequent series to break upMEM_UNINITin the verifier into two properties:MEM_WRITEandMEM_UNINITto better declare intent of the helpers. Then the bpf_*_check_mtu() will haveMEM_WRITEbut notMEM_UNINIT.Thoughts? (If preference is to further extend this series, I can also look into that ofc.)
We have MEM_RDONLY which literally means that memory is meant to be only consumed for reading. Will this solve these issues?
Quoted 3 lines
Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Quoted 75 lines · Andrii Nakryiko
Quoted 74 lines · Daniel Borkmann
Quoted 55 lines · Daniel Borkmann
Quoted 40 lines · Alexei Starovoitov
Quoted 7 lines · Daniel Borkmann
net/core/filter.c +2
-4-if (unlikely (flags & ~(BPF_MTU_CHK_SEGS ))) -return -EINVAL ; - -if (unlikely (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) +if (unlikely ((flags & ~(BPF_MTU_CHK_SEGS )) || + (flags &BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) {net/core/filter.c BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, · line 6277
Quoted 10 lines
net/core/filter.c +5
-1+ *mtu_len =0 ;return -EINVAL ; + } dev =__dev_via_ifindex (dev, ifindex); -if (unlikely (!dev)) +if (unlikely (!dev)) { + *mtu_len =0 ;return -ENODEV ; + }I don't understand this
mtu_lenclearing.My earlier comment was that mtu is in&out argument. The program has to set it to something. It cannot be uninit. So zeroing it on error looks very odd.
In that sense the patch 3 looks wrong. Instead of:
(quoted diff) +3
-1@@ -6346,7 +6346,9 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, - .arg3_type = ARG_PTR_TO_INT, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | + MEM_UNINIT | MEM_ALIGNED, + .arg3_size = sizeof(u32),
MEM_UNINITshould be removed, becausebpf_xdp_check_mtu,bpf_skb_check_mtuwill read it.Quoted 8 lines
If there is a program out there that calls this helper without initializing
mtu_lenit will be rejected after we fix it, but I think that's a good thing.
Passing randommtu_lenand let helper do:
skb_len=*mtu_len?*mtu_len+dev->hard_header_len:skb->len;is just bad.
Ok, fair. Removing
MEM_UNINITsounds reasonable, was mostly thinking that even if its garbage MTU being passed in,mtu_lengets filled in either case (BPF_MTU_CHK_RET_{SUCCESS,FRAG_NEEDED}) assuming no invalid ifindex e.g.:Quoted 2 lines
__u32 mtu_len; bpf_skb_check_mtu(skb, skb->ifindex, &mtu_len, 0, 0);Getting back at this, removing
MEM_UNINITneeds more verifier rework:MEM_UNINITright now implies two things actually: i) write into memory, ii) memory does not have to be initialized. If we liftMEM_UNINIT, it then becomes: i) read into memory, ii) memory must be initialized.This means that for bpf_*_check_mtu() we're readding the issue we're trying to fix, that is, it would then be able to write back into things like .rodata maps.
My suggestion is for this series is to go with
MEM_UNINITtag and clearing on error case to avoid leaking, and then in a subsequent series to break upMEM_UNINITin the verifier into two properties:MEM_WRITEandMEM_UNINITto better declare intent of the helpers. Then the bpf_*_check_mtu() will haveMEM_WRITEbut notMEM_UNINIT.
Quoted 5 lines
Quoted 2 lines
Thoughts? (If preference is to further extend this series, I can also look into that ofc.)
We have
MEM_RDONLYwhich literally means that memory is meant to be only consumed for reading. Will this solve these issues?
It doesn't help, in check_helper_mem_access() we check for writes e.g. for map values only via BPF_WRITE when meta->raw_mode is set which is the case for MEM_UNINIT-tagged ARG_PTR_TO_MEM, otherwise BPF_READ is implied hence my suggestion to break this up into two properties as a next step.
Thanks,
Daniel
Daniel Borkmann
maintainerPATCH v4 6/8 selftests/bpf: Fix ARG_PTR_TO_LONG {half-,}uninitialized test
In reply to Daniel BorkmannThe assumption of 'in privileged mode reads from uninitialized stack locations are permitted' is not quite correct since the verifier was probing for read access rather than write access. Both tests need to be annotated as __success for privileged and unprivileged.
--- tools/testing/selftests/bpf/progs/verifier_int_ptr.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tools/testing/selftests/bpf/progs/verifier_int_ptr.c +1 -4
@@ -8,7 +8,6 @@ SEC("socket") __description("ARG_PTR_TO_LONG uninitialized") __success -__failure_unpriv __msg_unpriv("invalid indirect read from stack R4 off -16+0 size 8")__naked void arg_ptr_to_long_uninitialized (void ) { asm volatile (" \
@@ -36,9 +35,7 @@ __naked void arg_ptr_to_long_uninitialized(void) SEC("socket") __description("ARG_PTR_TO_LONG half-uninitialized") -/* in privileged mode reads from uninitialized stack locations are permitted */ -__success __failure_unpriv -__msg_unpriv("invalid indirect read from stack R4 off -16+4 size 8") +__success __retval(0) __naked void ptr_to_long_half_uninitialized(void) {
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 6/8] selftests/bpf: Fix ARG_PTR_TO_LONG {half-,}uninitialized test
Quoted 11 lines · Daniel Borkmann
The assumption of 'in privileged mode reads from uninitialized stack locations are permitted' is not quite correct since the verifier was probing for read access rather than write access. Both tests need to be annotated as
__successfor privileged and unprivileged.Signed-off-by Daniel Borkmann <[email protected]>--- tools/testing/selftests/bpf/progs/verifier_int_ptr.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
Quoted 26 lines
tools/testing/selftests/bpf/progs/verifier_int_ptr.c +1
-5@@ -8,7 +8,6 @@ SEC("socket") __description("ARG_PTR_TO_LONG uninitialized") __success -__failure_unpriv __msg_unpriv("invalid indirect read from stack R4 off -16+0 size 8")__naked void arg_ptr_to_long_uninitialized (void ) { asm volatile (" \@@ -36,9 +35,7 @@ __naked void arg_ptr_to_long_uninitialized(void) SEC("socket") __description("ARG_PTR_TO_LONG half-uninitialized") -/* in privileged mode reads from uninitialized stack locations are permitted */ -__success __failure_unpriv -__msg_unpriv("invalid indirect read from stack R4 off -16+4 size 8") +__success __retval(0) __naked void ptr_to_long_half_uninitialized(void) { --2.43.0
Daniel Borkmann
maintainerPATCH v4 7/8 selftests/bpf: Rename ARG_PTR_TO_LONG test description
In reply to Daniel BorkmannGiven we got rid of ARG_PTR_TO_LONG, change the test case description to avoid potential confusion:
# ./vmtest.sh -- ./test_progs -t verifier_int_ptr [...] ./test_progs -t verifier_int_ptr [ 1.610563] bpf_testmod: loading out-of-tree module taints kernel. [ 1.611049] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel #489/1 verifier_int_ptr/arg pointer to long uninitialized:OK #489/2 verifier_int_ptr/arg pointer to long half-uninitialized:OK #489/3 verifier_int_ptr/arg pointer to long misaligned:OK #489/4 verifier_int_ptr/arg pointer to long size < sizeof(long):OK #489/5 verifier_int_ptr/arg pointer to long initialized:OK #489 verifier_int_ptr:OK Summary: 1/5 PASSED, 0 SKIPPED, 0 FAILED
---
v1 -> v2:
- new patch
tools/testing/selftests/bpf/progs/verifier_int_ptr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/progs/verifier_int_ptr.c +5 -5
@@ -6,7 +6,7 @@#include "bpf_misc.h" SEC("socket") -__description("ARG_PTR_TO_LONG uninitialized") +__description("arg pointer to long uninitialized") __success__naked void arg_ptr_to_long_uninitialized (void ) {
@@ -34,7 +34,7 @@ __naked void arg_ptr_to_long_uninitialized(void) } SEC("socket") -__description("ARG_PTR_TO_LONG half-uninitialized") +__description("arg pointer to long half-uninitialized") __success __retval(0) __naked void ptr_to_long_half_uninitialized(void)
@@ -64,7 +64,7 @@ __naked void ptr_to_long_half_uninitialized(void) } SEC("cgroup/sysctl") -__description("ARG_PTR_TO_LONG misaligned") +__description("arg pointer to long misaligned") __failure __msg("misaligned stack access off 0+-20+0 size 8") __naked void arg_ptr_to_long_misaligned(void) {
@@ -95,7 +95,7 @@ __naked void arg_ptr_to_long_misaligned(void) } SEC("cgroup/sysctl") -__description("ARG_PTR_TO_LONG size < sizeof(long)") +__description("arg pointer to long size < sizeof(long)") __failure __msg("invalid indirect access to stack R4 off=-4 size=8") __naked void to_long_size_sizeof_long(void) {
@@ -124,7 +124,7 @@ __naked void to_long_size_sizeof_long(void) } SEC("cgroup/sysctl") -__description("ARG_PTR_TO_LONG initialized") +__description("arg pointer to long initialized") __success__naked void arg_ptr_to_long_initialized (void ) {
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v4 8/8 selftests/bpf: Add a test case to write into .rodata
In reply to Daniel BorkmannAdd a test case which attempts to write into .rodata section of the BPF program, and for comparison this adds test cases also for .bss and .data section.
Before fix:
# ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const tester_init:PASS:tester_log_buf 0 nsec process_subtest:PASS:obj_open_mem 0 nsec process_subtest:PASS:specs_alloc 0 nsec run_subtest:PASS:obj_open_mem 0 nsec run_subtest:FAIL:unexpected_load_success unexpected success: 0 #465/1 verifier_const/rodata: write rejected:FAIL #465/2 verifier_const/bss: write accepted:OK #465/3 verifier_const/data: write accepted:OK #465 verifier_const:FAIL [...]
After fix:
# ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const #465/1 verifier_const/rodata: write rejected:OK #465/2 verifier_const/bss: write accepted:OK #465/3 verifier_const/data: write accepted:OK #465 verifier_const:OK [...]
---
v1 -> v2:
- const volatile long (Andrii)
.../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/verifier_const.c | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_const.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -21,6 +21,7 @@#include "verifier_cgroup_inv_retcode.skel.h" #include "verifier_cgroup_skb.skel.h" #include "verifier_cgroup_storage.skel.h" +#include "verifier_const.skel.h" #include "verifier_const_or.skel.h" #include "verifier_ctx.skel.h" #include "verifier_ctx_sk_msg.skel.h"
@@ -146,6 +147,7 @@ void test_verifier_cfg(void) { RUN(verifier_cfg); }void test_verifier_cgroup_inv_retcode (void ) {RUN (verifier_cgroup_inv_retcode); }void test_verifier_cgroup_skb (void ) {RUN (verifier_cgroup_skb); }void test_verifier_cgroup_storage (void ) {RUN (verifier_cgroup_storage); } +void test_verifier_const (void ) {RUN (verifier_const); }void test_verifier_const_or (void ) {RUN (verifier_const_or); }void test_verifier_ctx (void ) {RUN (verifier_ctx); }void test_verifier_ctx_sk_msg (void ) {RUN (verifier_ctx_sk_msg); }
tools/testing/selftests/bpf/progs/verifier_const.c new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v4 8/8] selftests/bpf: Add a test case to write into .rodata
Quoted 32 lines · Daniel Borkmann
Add a test case which attempts to write into .rodata section of the BPF program, and for comparison this adds test cases also for .bss and .data section.
Before fix:
# ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const tester_init:PASS:tester_log_buf 0 nsec process_subtest:PASS:obj_open_mem 0 nsec process_subtest:PASS:specs_alloc 0 nsec run_subtest:PASS:obj_open_mem 0 nsec run_subtest:FAIL:unexpected_load_success unexpected success: 0 #465/1 verifier_const/rodata: write rejected:FAIL #465/2 verifier_const/bss: write accepted:OK #465/3 verifier_const/data: write accepted:OK #465 verifier_const:FAIL [...]After fix:
# ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const #465/1 verifier_const/rodata: write rejected:OK #465/2 verifier_const/bss: write accepted:OK #465/3 verifier_const/data: write accepted:OK #465 verifier_const:OK [...]
Quoted 6 lines
Signed-off-by Daniel Borkmann <[email protected]>Acked-by Shung-Hsi Yu <[email protected]>---
v1 -> v2:
- const volatile long (Andrii)
LGTM
Quoted 76 lines
.../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/verifier_const.c | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_const.ctools/testing/selftests/bpf/prog_tests/verifier.c +2
-0@@ -21,6 +21,7 @@#include "verifier_cgroup_inv_retcode.skel.h" #include "verifier_cgroup_skb.skel.h" #include "verifier_cgroup_storage.skel.h" +#include "verifier_const.skel.h" #include "verifier_const_or.skel.h" #include "verifier_ctx.skel.h" #include "verifier_ctx_sk_msg.skel.h" @@ -146,6 +147,7 @@ void test_verifier_cfg(void) { RUN(verifier_cfg); }void test_verifier_cgroup_inv_retcode (void ) {RUN (verifier_cgroup_inv_retcode); }void test_verifier_cgroup_skb (void ) {RUN (verifier_cgroup_skb); }void test_verifier_cgroup_storage (void ) {RUN (verifier_cgroup_storage); } +void test_verifier_const (void ) {RUN (verifier_const); }void test_verifier_const_or (void ) {RUN (verifier_const_or); }void test_verifier_ctx (void ) {RUN (verifier_ctx); }void test_verifier_ctx_sk_msg (void ) {RUN (verifier_ctx_sk_msg); }tools/testing/selftests/bpf/progs/verifier_const.c new file +42
-1@@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Isovalent */ + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" + +const volatile long foo =42 ; +long bar; +long bart =96 ; + +SEC("tc/ingress") +__description("rodata: write rejected") +__failure __msg("write into map forbidden") +int tcx1(struct __sk_buff *skb) +{ +char buff[] = {'8' ,'4' ,'\0' }; +bpf_strtol (buff,sizeof (buff),0 , (long *)&foo); +return TCX_PASS ; +} + +SEC("tc/ingress") +__description("bss: write accepted") +__success +int tcx2 (struct __sk_buff *skb) +{ +char buff[] = {'8' ,'4' ,'\0' }; +bpf_strtol (buff,sizeof (buff),0 , &bar); +return TCX_PASS ; +} + +SEC("tc/ingress") +__description("data: write accepted") +__success +int tcx3 (struct __sk_buff *skb) +{ +char buff[] = {'8' ,'4' ,'\0' }; +bpf_strtol (buff,sizeof (buff),0 , &bart); +return TCX_PASS ; +} + +char LICENSE[] SEC("license") = "GPL"; --2.43.0
Quoted 23 lines · Daniel Borkmann
The
bpf_strtol()andbpf_strtoul()helpers are currently broken on 32bit:The argument type
ARG_PTR_TO_LONGis BPF-side "long", not kernel-side "long" and therefore always considered fixed 64bit no matter if 64 or 32bit underlying architecture.This contract breaks in case of the two mentioned helpers since their
BPF_CALLdefinition for the helpers was added with {unsigned,}long *res. Meaning, the transition from BPF-side "long" (BPF program) to kernel-side "long" (BPF helper) breaks here.Both helpers call
__bpf_strtoll()with "long long" correctly, but later assigning the result into 32-bit "*(long *)" on 32bit architectures. From a BPF program point of view, this means upper bits will be seen as uninitialised.Therefore, fix both
BPF_CALLsignatures to {s,u}64 types to fix this situation.Now, changing also
uapi/bpf.hhelper documentation which generatesbpf_helper_defs.hfor BPF programs is tricky: Changing signatures there to __{s,u}64 would trigger compiler warnings (incompatible pointer types passing 'long *' to parameter of type '__s64*' (aka 'long long *')) for existing BPF programs.
Quoted 6 lines
Leaving the signatures as-is would be fine as from BPF program point of view it is still BPF-side "long" and thus equivalent to __{s,u}64 on 64 or 32bit underlying architectures.
Note that
bpf_strtol()andbpf_strtoul()are the only helpers with this issue.
I think the bpf_helper_defs.h (and UAPI comment) side is completely correct and valid. That "long" in BPF helpers is always s64 and we rely on that on the BPF side. So I don't think we even need to change anything there.
Perhaps the original intent was to do arch-native long, not sure... But in any case, it's easy to check this in user code given we always have full 64-bit result.
LGTM:
Quoted 36 lines
Fixes d7a4cb9b6705 ("bpf: Introduce bpf_strtol and bpf_strtoul helpers")Reported-by Alexei Starovoitov <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>---
v3 -> v4:
- added patchkernel/bpf/helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/bpf/helpers.c +2
-3@@ -518,7 +518,7 @@ static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags, } BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags, - long *, res) + s64 *, res) {long long _res;int err;@@ -543,7 +543,7 @@ const struct bpf_func_proto bpf_strtol_proto = { }; BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags, - unsigned long *, res) + u64 *, res) {unsigned long long _res;bool is_negative; --2.43.0