Re: [PATCH bpf] bpf: add check for invalid name in btf_name_valid_section()
From: Eduard Zingerman
Date: Fri Aug 30 2024 - 05:43:36 EST
On Fri, 2024-08-30 at 11:03 +0900, Jeongjun Park wrote:
[...]
> > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > index edad152cee8e..d583d76fcace 100644
> > --- a/kernel/bpf/btf.c
> > +++ b/kernel/bpf/btf.c
> > @@ -820,7 +820,6 @@ static bool btf_name_valid_section(const struct btf *btf, u32 offset)
> >
> > /* set a limit on identifier length */
> > src_limit = src + KSYM_NAME_LEN;
> > - src++;
> > while (*src && src < src_limit) {
> > if (!isprint(*src))
> > return false;
>
> However, this patch is logically flawed.
> It will return true for invalid names with
> length 1 and src[0] being NULL. So I think
> it's better to stick with the original patch.
Fair enough, however the isprint check should be done for the first character.
So the full fix is a combination :)
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -818,9 +818,11 @@ static bool btf_name_valid_section(const struct btf *btf, u32 offset)
const char *src = btf_str_by_offset(btf, offset);
const char *src_limit;
+ if (!*src)
+ return false;
+
/* set a limit on identifier length */
src_limit = src + KSYM_NAME_LEN;
- src++;
while (*src && src < src_limit) {
if (!isprint(*src))
return false;
And corresponding test cases (tools/testing/selftests/bpf/prog_tests/btf.c):
{
.descr = "datasec: name with non-printable first char not is ok",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
/* VAR x */ /* [2] */
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
BTF_VAR_STATIC,
/* DATASEC ?.data */ /* [3] */
BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
BTF_VAR_SECINFO_ENC(2, 0, 4),
BTF_END_RAW,
},
BTF_STR_SEC("\0x\0\7foo"),
.err_str = "Invalid name",
.btf_load_err = true,
},{
.descr = "datasec: name '\\0' is not ok",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
/* VAR x */ /* [2] */
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
BTF_VAR_STATIC,
/* DATASEC \0 */ /* [3] */
BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
BTF_VAR_SECINFO_ENC(2, 0, 4),
BTF_END_RAW,
},
BTF_STR_SEC("\0x\0"),
.err_str = "Invalid name",
.btf_load_err = true,
},
Could you please resend your patch as a patch-set fix + selftests update?