Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On Mon, Sep 14, 2026 at 03:29:21PM +0100, Alan Maguire wrote:
> On 11/09/2026 20:26, Jiri Olsa wrote:
> > On Tue, Sep 01, 2026 at 05: 57: 40PM +0100, Alan Maguire wrote: SNIP > + >
> > +/* > + * BTF_KIND_LOC_PROTO specifies location prototypes; i. e. how
> > locations relate > + * to parameters; a struct btf_type of
> > BTF_KIND_LOC_PROTO is followed
> >
> >
> > On Tue, Sep 01, 2026 at 05:57:40PM +0100, Alan Maguire wrote:
> >
> > SNIP
> >
> >> +
> >> +/*
> >> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
> >> relate
> >> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
> >> + * a vlen-specified number of __u32 BTF type ids which specify the
> >> associated
> >> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
> >> + * location. The type should either be 0 (no location info) or point at
> >> + * a BTF_KIND_LOC_PARAM.
> >> + */
> >> +
> >> +/*
> >> + * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
> >> + * containing location site-specific information;
> >> + *
> >> + * - function (func)
> >> + * - location prototype type id (loc_proto)
> >> + * - address offset (offset) relative to kernel base address
> >> + */
> >> +
> >> +struct btf_loc {
> >> + __u32 func;
> >> + __u32 loc_proto;
> >> + __u32 offset;
> >> +};
> >
> > would it be possible to include btf id of the immediate parent function?
> > we have use cases for tracing just inlined function called from particular
> > parent
> >
> > I thought we could use the location offset for that, but because inline
> > functions could be nested, this seems like hard search.. also we do not
> > know the size of the inlined code, so not sure how reliable it'd be
> >
> > I checked the dwarves code and I wonder we could instead store parent
> > function (struct inline_expansion) during dwarf decoding and use that
> > later to get parent function FUNC_ID and have:
> >
> > struct btf_loc {
> > __u32 func;
> > __u32 parent;
> > __u32 loc_proto;
> > __u32 offset;
> > };
> >
> > wdyt? thanks,
>
> hi Jiri,
>
> So in the case of encoding the immediate parent it is quite expensive -
> my concern is that adding it as an extra field is another 4 bytes per BTF
> location, and because there are a lot of inline sites that would mean
> somewhere in the region of 4 * 50 = 2Mb extra. This is the tricky part
> with this stuff; even small per-site tweaks really add up.
ugh right..
>
> Is the immediacy of the parent-child relationship a dealbreaker for your
> use-case? As you say the offset can tell us the function B was inlined
it's one of the use cases, but it's still useful without this
> within the bounds of uninlined function A, but it won't tell us if it was
> via function C. The other thing that complicates this is that an inlining
> doesn't always have a size; it can be inlined in a non-contiguous manner.
> So in some ways maybe the best we can do anyway is to be able to say a
> function was inlined in a particular uninlined function?
I guess we could get that info from dwarf, it'd just be simpler
having that in BTF
thanks,
jirka
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On 01/09/2026 18:17, [email protected] wrote: > Thank you for your contribution! Sashiko AI review found 2 potential issue(s) > to consider: > - [High] btf_loc_proto_resolve fails to validate the return type location > parameter (t->type). > - [Medium] Missing strict validation of unused fields and flags in check_meta > callbacks for new BTF kinds. > -- > > commit 32008454060b92e68c858ee6b661e8885cb28034 > Author: Alan Maguire > > btf: Extend UAPI to support BTF location (inline site) info > > This commit adds BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and > BTF_KIND_LOCSEC to help represent location information for functions. > It enables representing fully or partially inlined functions at > potentially multiple inline sites. > >> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c >> index 9c2cab08bb794..d74c8668aa3f6 100644 >> --- a/kernel/bpf/btf.c >> +++ b/kernel/bpf/btf.c > [ ... ] >> +static s32 btf_loc_param_check_meta(struct btf_verifier_env *env, >> +const struct btf_type *t, >> +u32 meta_left) >> +{ >> +const struct btf_loc_param *p = btf_loc_param(t); >> +u32 meta_needed; >> +u32 size; >> + >> +meta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t); > > [Severity: Medium] > Should this function strictly validate that p->flags only contains valid > enum bits, and check that btf_type_kflag(t) is 0? > > The BTF verifier typically rejects non-zero unused fields and unknown flags > to ensure forward compatibility. If malformed BTF objects with arbitrary > garbage in these fields are accepted, they might silently alter behavior > or break if these bits are repurposed in the future. > kflag test makes sense, restricting the semantics of the loc param flags less so given that we don't use them in the kernel. >> +if (meta_left < meta_needed) { >> +btf_verifier_log_basic(env, t, >> + "meta_left:%u meta_needed:%u", >> + meta_left, meta_needed); >> +return -EINVAL; >> +} > [ ... ] >> +static int btf_loc_proto_resolve(struct btf_verifier_env *env, >> + const struct resolve_vertex *v) >> +{ >> +const __u32 *params = btf_loc_params(v->t); >> +u32 i, nr_params = btf_type_vlen(v->t); >> +struct btf *btf = env->btf; >> + >> +for (i = 0; i < nr_params; i++) { > > [Severity: High] > Does this function need to validate the return type location parameter > encoded in v->t->type? > > It appears to validate the vlen parameters (params[i]) but entirely skips > verifying v->t->type. If a malformed BTF object specifies an out-of-bounds > type ID or an incorrect BTF kind in the t->type field, could it result in > type confusion or out-of-bounds memory accesses when subsequent code trusts > this field? > loc params don't have a return type since we can only get at inbound parameters at the location. We should check for 0 I suppose. >> +const struct btf_type *param_type; >> +u32 param_type_id = params[i]; >> + >> +if (!param_type_id) >> +continue; > [ ... ] >> +static s32 btf_locsec_check_meta(struct btf_verifier_env *env, >> + const struct btf_type *t, >> + u32 meta_left) >> +{ >> +u32 meta_needed; >> + >> +meta_needed = sizeof(struct btf_loc) * btf_type_vlen(t); > > [Severity: Medium] > Similar to the location parameter check, should this callback enforce that > the unused t->size/t->type union and the kflag are 0? Without these > checks, the verifier might accept garbage data in these unused fields which > violates the strict validation design pattern. > Sure. >> + >> +if (meta_left < meta_needed) { >> +btf_verifier_log_basic(env, t, >> + "meta_left:%u meta_needed:%u", >> + meta_left, meta_needed); >> +return -EINVAL; >> +} >> +return meta_needed; >> +} >
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On 11/09/2026 20:26, Jiri Olsa wrote:
> On Tue, Sep 01, 2026 at 05: 57: 40PM +0100, Alan Maguire wrote: SNIP > + >
> +/* > + * BTF_KIND_LOC_PROTO specifies location prototypes; i. e. how
> locations relate > + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO
> is followed
>
>
> On Tue, Sep 01, 2026 at 05:57:40PM +0100, Alan Maguire wrote:
>
> SNIP
>
>> +
>> +/*
>> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
>> relate
>> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
>> + * a vlen-specified number of __u32 BTF type ids which specify the
>> associated
>> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
>> + * location. The type should either be 0 (no location info) or point at
>> + * a BTF_KIND_LOC_PARAM.
>> + */
>> +
>> +/*
>> + * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
>> + * containing location site-specific information;
>> + *
>> + * - function (func)
>> + * - location prototype type id (loc_proto)
>> + * - address offset (offset) relative to kernel base address
>> + */
>> +
>> +struct btf_loc {
>> +__u32 func;
>> +__u32 loc_proto;
>> +__u32 offset;
>> +};
>
> would it be possible to include btf id of the immediate parent function?
> we have use cases for tracing just inlined function called from particular
> parent
>
> I thought we could use the location offset for that, but because inline
> functions could be nested, this seems like hard search.. also we do not
> know the size of the inlined code, so not sure how reliable it'd be
>
> I checked the dwarves code and I wonder we could instead store parent
> function (struct inline_expansion) during dwarf decoding and use that
> later to get parent function FUNC_ID and have:
>
> struct btf_loc {
>__u32 func;
>__u32 parent;
>__u32 loc_proto;
>__u32 offset;
> };
>
> wdyt? thanks,
hi Jiri,
So in the case of encoding the immediate parent it is quite expensive -
my concern is that adding it as an extra field is another 4 bytes per BTF
location, and because there are a lot of inline sites that would mean
somewhere in the region of 4 * 50 = 2Mb extra. This is the tricky part
with this stuff; even small per-site tweaks really add up.
Is the immediacy of the parent-child relationship a dealbreaker for your
use-case? As you say the offset can tell us the function B was inlined
within the bounds of uninlined function A, but it won't tell us if it was
via function C. The other thing that complicates this is that an inlining
doesn't always have a size; it can be inlined in a non-contiguous manner.
So in some ways maybe the best we can do anyway is to be able to say a
function was inlined in a particular uninlined function?
Alan
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On 09/09/2026 23:26, Eduard Zingerman wrote:
> On Tue, 2026-09-01 at 17:57 +0100, Alan Maguire wrote:
>> Add BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO and BTF_KIND_LOCSEC
>> to help represent location information for functions.
>>
>> BTF_KIND_LOC_PARAM is used to represent how we retrieve data at a
>> location; either via register(s), or register+offset, a dereference
>> of a register+offset or a constant value.
>>
>> BTF_KIND_LOC_PROTO represents location information about a location
>> with multiple BTF_KIND_LOC_PARAMs.
>>
>> And finally BTF_KIND_LOCSEC is a set of location sites, each
>> of which has
>>
>> - a BTF_KIND_FUNC function associated with the inline site
>> - a location prototype specifying where to find the function
>> parameters
>> - an address offset relative to the kernel base address
>>
>> This can be used to support representing
>>
>> - a fully-inlined function at potentially multiple inline sites
>> with potentially different parameter availability
>> - a partially-inlined function where some _LOC_PROTOs represent
>> inlined sites as above and others have normal _FUNC representations
>>
>> Also BTF_KIND_LOCSEC struct btf_loc will have two type id
>> references; one for the associated func, the other for the loc_proto.
>> Accordingly increase the number of m_offs references in btf_field_desc
>> to 2.
>>
>> Signed-off-by: Alan Maguire
>> ---
>
> Acked-by: Eduard Zingerman
>
Thanks for reviewing all of this, much appreciated! Replies below..
> ...
>
>> diff --git a/include/linux/btf.h b/include/linux/btf.h
>> index ddd0f4f32d24..a4412bc16688 100644
>> --- a/include/linux/btf.h
>> +++ b/include/linux/btf.h
>
> ...
>
>> +static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
>> +static inline __u32 *btf_loc_params(const struct btf_type *t)
>
> Maybe rename the latter to btf_loc_proto_params?
>
much clearer; will do.
> ...
>
>> diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
>> index 618167cab4e6..6062c9958034 100644
>> --- a/include/uapi/linux/btf.h
>> +++ b/include/uapi/linux/btf.h
>
> ...
>
>> @@ -212,4 +214,65 @@ struct btf_enum64 {
>> __u32 val_hi32;
>> };
>>
>> +/*
>> + * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
>> + * that contains flags specifying the contents of the vlen-specified
>> + * number of 4-byte values that follow.
>> + */
>> +struct btf_loc_param {
>> +__u32 flags;
>
> Wdyt about adding a flexible array member here?
>
> __u32 params[];
>
> Would make btf_loc_param_log() a little bit easier to follow.
>
>> +};
>> +
>> +/*
>> + * The combination of size, vlen and flags gives us the means to interpret
>> + * the following vlen-specified set of 4-byte values:
>> + *
>> + * - a BTF_LOC_PARAM_CONST is a constant value; combination
>> + * of size, vlen and _SIGNED flag determines it. If the value requires
>> + * 64 bits it is stored in {lo,hi} order.
>> + * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
>> + * respect to kernel base address.
>
> Nit: pahole generates ADDR | CONST.
>
thanks, will fix.
>> + * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
>> + * with vlen 2 it is a multi-register parameter.
>
> Nit: REG | OFFSET is not discussed.
>
good catch.
>> + * - a _REG | DEREF with vlen 1 dereferences the value in the register
>> + * number specified.
>> + * - a REG | DEREF | OFFSET with vlen specifies the register value in
>> + * the first 4-byte value and the offset in the remainder.
>
> --- >8 ---
>
>> + * - binary logical operators operate on a combination of register
>> + * number and constant value, aside from _NOT which operates on
>> + * a register
>
> --- 8< ---
>
> This is probably a leftover.
>
yep, thanks for catching; I was experimenting to see if adding logical operators
bought us anything in terms of being able to encode more locations. Didn't
really
help so I left it out.
>> + */
>> +enum btf_loc_param_flags {
>> +BTF_LOC_PARAM_SIGNED= 0x1,
>> +BTF_LOC_PARAM_CONST = 0x2,
>> +BTF_LOC_PARAM_ADDR = 0x4,
>> +BTF_LOC_PARAM_REG = 0x8,
>> +BTF_LOC_PARAM_DEREF = 0x10,
>> +BTF_LOC_PARAM_OFFSET= 0x20,
>> +};
>> +
>> +/*
>> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
>> relate
>> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
>> + * a vlen-specified number of __u32 BTF type ids which specify the
>> associated
>> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
>> + * location. The type should either be 0 (no location info) or point at
>> + * a BTF_KIND_LOC_PARAM.
>> + */
>> +
>> +/*
>> + * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
>> + * containing location site-specific information;
>> + *
>> + * - function (func)
>> + * - location prototype type id
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On Tue, Sep 01, 2026 at 05:57:40PM +0100, Alan Maguire wrote:
SNIP
> +
> +/*
> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
> relate
> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
> + * a vlen-specified number of __u32 BTF type ids which specify the associated
> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
> + * location. The type should either be 0 (no location info) or point at
> + * a BTF_KIND_LOC_PARAM.
> + */
> +
> +/*
> + * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
> + * containing location site-specific information;
> + *
> + * - function (func)
> + * - location prototype type id (loc_proto)
> + * - address offset (offset) relative to kernel base address
> + */
> +
> +struct btf_loc {
> + __u32 func;
> + __u32 loc_proto;
> + __u32 offset;
> +};
would it be possible to include btf id of the immediate parent function?
we have use cases for tracing just inlined function called from particular
parent
I thought we could use the location offset for that, but because inline
functions could be nested, this seems like hard search.. also we do not
know the size of the inlined code, so not sure how reliable it'd be
I checked the dwarves code and I wonder we could instead store parent
function (struct inline_expansion) during dwarf decoding and use that
later to get parent function FUNC_ID and have:
struct btf_loc {
__u32 func;
__u32 parent;
__u32 loc_proto;
__u32 offset;
};
wdyt? thanks,
jirka
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
On Tue, 2026-09-01 at 17:57 +0100, Alan Maguire wrote:
> Add BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO and BTF_KIND_LOCSEC
> to help represent location information for functions.
>
> BTF_KIND_LOC_PARAM is used to represent how we retrieve data at a
> location; either via register(s), or register+offset, a dereference
> of a register+offset or a constant value.
>
> BTF_KIND_LOC_PROTO represents location information about a location
> with multiple BTF_KIND_LOC_PARAMs.
>
> And finally BTF_KIND_LOCSEC is a set of location sites, each
> of which has
>
> - a BTF_KIND_FUNC function associated with the inline site
> - a location prototype specifying where to find the function
> parameters
> - an address offset relative to the kernel base address
>
> This can be used to support representing
>
> - a fully-inlined function at potentially multiple inline sites
> with potentially different parameter availability
> - a partially-inlined function where some _LOC_PROTOs represent
> inlined sites as above and others have normal _FUNC representations
>
> Also BTF_KIND_LOCSEC struct btf_loc will have two type id
> references; one for the associated func, the other for the loc_proto.
> Accordingly increase the number of m_offs references in btf_field_desc
> to 2.
>
> Signed-off-by: Alan Maguire
> ---
Acked-by: Eduard Zingerman
...
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index ddd0f4f32d24..a4412bc16688 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
...
> +static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
> +static inline __u32 *btf_loc_params(const struct btf_type *t)
Maybe rename the latter to btf_loc_proto_params?
...
> diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
> index 618167cab4e6..6062c9958034 100644
> --- a/include/uapi/linux/btf.h
> +++ b/include/uapi/linux/btf.h
...
> @@ -212,4 +214,65 @@ struct btf_enum64 {
> __u32 val_hi32;
> };
>
> +/*
> + * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
> + * that contains flags specifying the contents of the vlen-specified
> + * number of 4-byte values that follow.
> + */
> +struct btf_loc_param {
> + __u32 flags;
Wdyt about adding a flexible array member here?
__u32 params[];
Would make btf_loc_param_log() a little bit easier to follow.
> +};
> +
> +/*
> + * The combination of size, vlen and flags gives us the means to interpret
> + * the following vlen-specified set of 4-byte values:
> + *
> + * - a BTF_LOC_PARAM_CONST is a constant value; combination
> + * of size, vlen and _SIGNED flag determines it. If the value requires
> + * 64 bits it is stored in {lo,hi} order.
> + * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
> + * respect to kernel base address.
Nit: pahole generates ADDR | CONST.
> + * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
> + * with vlen 2 it is a multi-register parameter.
Nit: REG | OFFSET is not discussed.
> + * - a _REG | DEREF with vlen 1 dereferences the value in the register
> + * number specified.
> + * - a REG | DEREF | OFFSET with vlen specifies the register value in
> + * the first 4-byte value and the offset in the remainder.
--- >8 ---
> + * - binary logical operators operate on a combination of register
> + * number and constant value, aside from _NOT which operates on
> + * a register
--- 8< ---
This is probably a leftover.
> + */
> +enum btf_loc_param_flags {
> + BTF_LOC_PARAM_SIGNED= 0x1,
> + BTF_LOC_PARAM_CONST = 0x2,
> + BTF_LOC_PARAM_ADDR = 0x4,
> + BTF_LOC_PARAM_REG = 0x8,
> + BTF_LOC_PARAM_DEREF = 0x10,
> + BTF_LOC_PARAM_OFFSET= 0x20,
> +};
> +
> +/*
> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
> relate
> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
> + * a vlen-specified number of __u32 BTF type ids which specify the associated
> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
> + * location. The type should either be 0 (no location info) or point at
> + * a BTF_KIND_LOC_PARAM.
> + */
> +
> +/*
> + * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
> + * containing location site-specific information;
> + *
> + * - function (func)
> + * - location prototype type id (loc_proto)
> + * - address offset (offset) relative to kernel base address
pahole uses loc->section_offset to create LOCSEC entries,
which corresponds to an offset within an ELF containing
the function section. Would it make sense to rephrase the
above comment a bit?
> + */
> +
> +struct btf_loc {
> + __u32 func;
> + __u32 loc_proto;
> + __u32 offset;
> +};
> +
> #endif /* _UAPI__LINUX_BTF_H__ */
...
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index ddd0f4f32d24..a4412bc16688 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -559,7 +574,7 @@ struct btf_field_desc {
> /* member struct size, or zero, if no members */
> int m_sz;
> /* repeated per-member offsets */
> - int m_off_cnt, m_offs[1];
> + int m_off_cnt, m_offs[2];
> };
Eduard Zingerman noted in the RFC v1 review that this change to m_offs[2]
should be moved to patch #2, since that patch's commit message explains
why it is needed. The change was acknowledged but not addressed in v2.
See:
https://lore.kernel.org/bpf/[email protected]/
> diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
> index 618167cab4e6..6062c9958034 100644
> --- a/include/uapi/linux/btf.h
> +++ b/include/uapi/linux/btf.h
[ ... ]
> +/*
> + * The combination of size, vlen and flags gives us the means to interpret
> + * the following vlen-specified set of 4-byte values:
> + *
> + * - a BTF_LOC_PARAM_CONST is a constant value; combination
> + * of size, vlen and _SIGNED flag determines it. If the value requires
> + * 64 bits it is stored in {lo,hi} order.
> + * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
> + * respect to kernel base address.
> + * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
> + * with vlen 2 it is a multi-register parameter.
> + * - a _REG | DEREF with vlen 1 dereferences the value in the register
> + * number specified.
> + * - a REG | DEREF | OFFSET with vlen specifies the register value in
> + * the first 4-byte value and the offset in the remainder.
> + * - binary logical operators operate on a combination of register
> + * number and constant value, aside from _NOT which operates on
> + * a register
> + */
> +enum btf_loc_param_flags {
> + BTF_LOC_PARAM_SIGNED= 0x1,
> + BTF_LOC_PARAM_CONST = 0x2,
> + BTF_LOC_PARAM_ADDR = 0x4,
> + BTF_LOC_PARAM_REG = 0x8,
> + BTF_LOC_PARAM_DEREF = 0x10,
> + BTF_LOC_PARAM_OFFSET= 0x20,
> +};
Does the comment above the enum match the actual flags? The text mentions
"binary logical operators" and "_NOT which operates on a register," but
the enum defines only SIGNED, CONST, ADDR, REG, DEREF and OFFSET. Where
is BTF_LOC_PARAM_NOT and where are the logical operator flags?
This same mismatch appears in tools/include/uapi/linux/btf.h as well.
> +/*
> + * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations
> relate
> + * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
> + * a vlen-specified number of __u32 BTF type ids which specify the associated
> + * BTF_KIND_LOC_PARAM for each function parameter associated with the
> + * location. The type should either be 0 (no location info) or point at
> + * a BTF_KIND_LOC_PARAM.
> + */
This isn't a bug, but there's a duplicated word: "followed by a / a
vlen-specified number". The same duplication appears in the tools copy at
tools/include/uapi/linux/btf.h.
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 9c2cab08bb79..d74c8668aa3f 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
[ ... ]
> +static s32 btf_loc_param_check_meta(struct btf_verifier_env *env,
> + const struct btf_type *t,
> + u32 meta_left)
> +{
> + const struct btf_loc_param *p = btf_loc_param(t);
> + u32 meta_needed;
> + u32 size;
> +
> + meta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t);
> + if (meta_left < meta_needed) {
> + btf_verifier_log_basic(env, t,
> +"meta_left:%u meta_needed:%u",
> + meta_left, meta_needed);
> + return -EINVAL;
> + }
> +
> + if (t->name_off) {
> + btf_verifier_log_type(env, t, "Invalid name");
> + return -EINVAL;
> + }
> + size = t->size;
> + if (size > 16 || !is_power_of_2(size)) {
> + btf_verifier_log_type(env, t, "Unexpected size");
> + return -EINVAL;
> + }
> +
> + return meta_needed;
> +}
Does this handler miss the trailing btf_verifier_log_type(env, t, NULL)
call before returning successfully? Looking at __btf_verifier_log_type()
in this file, the comment states: "btf verifier prints all types it is
processing via btf_verifier_log_type(..., fmt = NULL)". Every other
check_meta handler in kernel/bpf/btf.c ends with that call on the
success path (int, ptr, array, struct, enum, enum64, fwd, typedef,
func_proto, func, var, datasec, decl_tag, float), but
btf_loc_param_check_meta, btf_loc_proto_check_meta, and
btf_locsec_check_meta all return successfully without it.
When userspace passes a log buffer to BPF_BTF_LOAD, wouldn't the per-type
"[id] KIND name ..." trace skip
Re: [PATCH v2 bpf-next 01/18] btf: Extend UAPI to support BTF location (inline site) info
Thank you for your contribution! Sashiko AI review found 2 potential issue(s)
to consider:
- [High] btf_loc_proto_resolve fails to validate the return type location
parameter (t->type).
- [Medium] Missing strict validation of unused fields and flags in check_meta
callbacks for new BTF kinds.
--
commit 32008454060b92e68c858ee6b661e8885cb28034
Author: Alan Maguire
btf: Extend UAPI to support BTF location (inline site) info
This commit adds BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and
BTF_KIND_LOCSEC to help represent location information for functions.
It enables representing fully or partially inlined functions at
potentially multiple inline sites.
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 9c2cab08bb794..d74c8668aa3f6 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
[ ... ]
> +static s32 btf_loc_param_check_meta(struct btf_verifier_env *env,
> + const struct btf_type *t,
> + u32 meta_left)
> +{
> + const struct btf_loc_param *p = btf_loc_param(t);
> + u32 meta_needed;
> + u32 size;
> +
> + meta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t);
[Severity: Medium]
Should this function strictly validate that p->flags only contains valid
enum bits, and check that btf_type_kflag(t) is 0?
The BTF verifier typically rejects non-zero unused fields and unknown flags
to ensure forward compatibility. If malformed BTF objects with arbitrary
garbage in these fields are accepted, they might silently alter behavior
or break if these bits are repurposed in the future.
> + if (meta_left < meta_needed) {
> + btf_verifier_log_basic(env, t,
> +"meta_left:%u meta_needed:%u",
> + meta_left, meta_needed);
> + return -EINVAL;
> + }
[ ... ]
> +static int btf_loc_proto_resolve(struct btf_verifier_env *env,
> + const struct resolve_vertex *v)
> +{
> + const __u32 *params = btf_loc_params(v->t);
> + u32 i, nr_params = btf_type_vlen(v->t);
> + struct btf *btf = env->btf;
> +
> + for (i = 0; i < nr_params; i++) {
[Severity: High]
Does this function need to validate the return type location parameter
encoded in v->t->type?
It appears to validate the vlen parameters (params[i]) but entirely skips
verifying v->t->type. If a malformed BTF object specifies an out-of-bounds
type ID or an incorrect BTF kind in the t->type field, could it result in
type confusion or out-of-bounds memory accesses when subsequent code trusts
this field?
> + const struct btf_type *param_type;
> + u32 param_type_id = params[i];
> +
> + if (!param_type_id)
> + continue;
[ ... ]
> +static s32 btf_locsec_check_meta(struct btf_verifier_env *env,
> + const struct btf_type *t,
> + u32 meta_left)
> +{
> + u32 meta_needed;
> +
> + meta_needed = sizeof(struct btf_loc) * btf_type_vlen(t);
[Severity: Medium]
Similar to the location parameter check, should this callback enforce that
the unused t->size/t->type union and the kflag are 0? Without these
checks, the verifier might accept garbage data in these unused fields which
violates the strict validation design pattern.
> +
> + if (meta_left < meta_needed) {
> + btf_verifier_log_basic(env, t,
> +"meta_left:%u meta_needed:%u",
> +meta_left, meta_needed);
> + return -EINVAL;
> + }
> + return meta_needed;
> +}
--
Sashiko AI review ·
https://sashiko.dev/#/patchset/[email protected]?part=1
