Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796076308; b=vlFplnOB/zwcyclf7ScJjHhWPBWCcGhDN6jtJz9OdFkm/9VCz4ZzluD1zwIwS3qLS6vh JDMrqLN70WkjcYYdAneAigQR5LknksdKQckeE032uU99+BbS/QRgY9s/tLkhibSE5D7a1 qBRmlpX2ZWFIMXoHyHRcXFXotW1GzfvMTvAU4mTnItfFfLwuiWuQPiqvhtHnyoI+S581j pab4WokFYjBOm3tOtILORayCgwUyvesGAGCTmG4rDuPNt66/hVhYppkjcvMLO5jeUpDNn B+gxwZNrubL4puAGjX7osCVrrZIRmQYY/zhYFcBMhvRWo4Ihusy1eFRKVJsA6tTKrwg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796076308; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=nBV5+Qu8dH8wtLN7zW8rHT8V80RNqVIyTT/v3eobk2s=; b=0LXW1n1H4K0MT6Iy/f1ho3f/MXXKBFuCNeGH7KKJObSwyvlwuEC2Ep94TMnVB3NRE2Ic U0Oj4KvWTcSib3yGqUSVVuy+lLSsvhp3FQcgZLURDiiisH17Xs2Zekf+ICeHQRu79eZBq rIs6VmrgCT1OPohNMiXU0rG8cSbxJBZEENZMZIM+RFMxaVyQQCBlkKDoMeQHl/j9WA5Mb 0L60CqE1f6X/WJ0H9Qnh06xJ8eFn0e23tyxgdE1Loqy/Zrc+z/u25Uc73YEYjbHb7096V X5tyl5FX8kKylPrH/v8Dq+AKAfWipPy4CCtHwOZ1BgXhsU/ksWHKzc4cKvkeOiP/X+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Rdqp1D7f; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59A54606A325 for ; Mon, 30 Nov 2026 22:05:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Rdqp1D7f Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE43C1F000C1; Mon, 30 Nov 2026 22:05:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796076307; bh=nBV5+Qu8dH8wtLN7zW8rHT8V80RNqVIyTT/v3eobk2s=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Rdqp1D7f4UCEZwhmQmJXo2+UCcFI7EktQDqHGqyOQc4ZvfZQ6TpxmsDU9I10yFoI/ rJGBFdYO+hTcrJIeHVzbc9gQBE3kpAvOiUDMDuPEQENAuNWLvlKIBmBGhD9sU/a53G sQcHx07MKh9lZez1wU3j9IFX+ochm2QVVvOcXggla5q8ypda8nzZn28oM2xzGsJteC iGPMX8JaDopm9/1aknEmHLPZWbyvDUCWJJt7mvPHbydngGzs5dt4YPlQp0R4ia55eI g3/Vx/xYCG9Lp4v1jRhYbtZaU3gm7oirbEKfWvnobukGGrBHCymATieII1DMmqSLgr wAecaypwH7vWw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Reply-To: sashiko-reviews@lists.linux.dev To: "Siddharth Nayyar" Cc: bpf@vger.kernel.org In-Reply-To: <20261130-lskel-struct-ops-v2-1-8b76e65073ab@google.com> References: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> <20261130-lskel-struct-ops-v2-1-8b76e65073ab@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 22:05:07 +0000 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] bpf: out-of-bounds heap read in local_to_kern_value() on crafted local BTF - [Medium] bpf: unlocked struct_ops_tab access in find_desc_by_local_type() -- commit 358556cdd1778df7a46e8f700b565dd624b3828f Author: Siddharth Nayyar bpf: Support struct_ops with object BTF This commit allows BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS programs to specify their target struct and members using the BPF object's own BTF, resolving the corresponding kernel struct_ops type and members by name. > diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c > index 3d334cd526c03..96eca8493892a 100644 > --- a/kernel/bpf/bpf_struct_ops.c > +++ b/kernel/bpf/bpf_struct_ops.c [ ... ] > @@ -729,8 +736,152 @@ static void bpf_struct_ops_map_free_ksyms(struct bp= f_struct_ops_map *st_map) [ ... ] > +/* > + * 'id' is a local struct, e.g. tcp_congestion_ops or tcp_congestion_ops= ___v2. > + * Find bpf_struct_ops_tcp_congestion_ops in the BTF of vmlinux or of a = module. > + * A module can have it for a struct of vmlinux. The caller does btf_put= (*kern_btf). > + */ > +static const struct bpf_struct_ops_desc * > +find_desc_by_local_type(const struct btf *btf, u32 id, struct btf **kern= _btf) > +{ > + const struct btf_type *t =3D btf_type_by_id(btf, id); > + const struct bpf_struct_ops_desc *st_ops_desc; > + char name[KSYM_NAME_LEN]; > + const char *tname; > + s32 value_id; > + > + if (!t || !__btf_type_is_struct(t)) > + return ERR_PTR(-EINVAL); > + tname =3D btf_name_by_offset(btf, t->name_off); > + if (snprintf(name, sizeof(name), VALUE_PREFIX "%.*s", > + (int)bpf_core_essential_name_len(tname), tname) >=3D sizeof(name)) > + return ERR_PTR(-EINVAL); > + value_id =3D bpf_find_btf_id(name, BTF_KIND_STRUCT, kern_btf); > + if (value_id < 0) > + return ERR_PTR(value_id); > + st_ops_desc =3D bpf_struct_ops_find_value(*kern_btf, value_id); > + if (!st_ops_desc) { > + btf_put(*kern_btf); > + return ERR_PTR(-EOPNOTSUPP); > + } > + return st_ops_desc; > +} > + > +/* > + * attach_btf_id is a local struct and expected_attach_type is the index= of its > + * member. Replace them, and the BTF, with what the kernel has under the= same names. > + */ > +int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr) > +{ > + const struct bpf_struct_ops_desc *st_ops_desc; > + const struct btf_member *member; > + const struct btf_type *t; > + struct btf *kern_btf; > + > + st_ops_desc =3D find_desc_by_local_type(*btf, attr->attach_btf_id, &ker= n_btf); > + if (IS_ERR(st_ops_desc)) > + return PTR_ERR(st_ops_desc); > + t =3D btf_type_by_id(*btf, attr->attach_btf_id); > + if (attr->expected_attach_type >=3D btf_type_vlen(t)) { > + btf_put(kern_btf); > + return -EINVAL; > + } > + member =3D btf_type_member(t) + attr->expected_attach_type; > + member =3D find_member(kern_btf, st_ops_desc->type, > + btf_name_by_offset(*btf, member->name_off)); [Severity: Medium] Can find_desc_by_local_type() race with btf_add_struct_ops() and trigger a use-after-free on (*kern_btf)->struct_ops_tab? When a module loads, btf_module_notify() publishes the module BTF in btf_idr on MODULE_STATE_COMING before the module's init function runs and before BTF_MODULE_F_LIVE is set on MODULE_STATE_LIVE. While init runs, register_bpf_struct_ops() calls btf_add_struct_ops(), which reallocates btf->struct_ops_tab via krealloc() once tab->cnt reaches tab->capacity: CPU0 CPU1 module_init() register_bpf_struct_ops() btf_add_struct_ops() bpf_struct_ops_resolve_attach() find_desc_by_local_type() bpf_find_btf_id() bpf_struct_ops_find_value() krealloc(btf->struct_ops_tab) find_member(..., st_ops_desc->type) <-- use-after-free Existing struct_ops paths (such as check_struct_ops_btf_id() and the BPF_F_VTYPE_BTF_OBJ_FD branch in bpf_struct_ops_map_alloc()) guard against this by calling btf_try_get_module() to verify BTF_MODULE_F_LIVE before calling bpf_struct_ops_find() or bpf_struct_ops_find_value(). Here, bpf_struct_ops_resolve_attach() never calls btf_try_get_module(), and local_to_kern_attr() calls it only after find_desc_by_local_type() has already searched struct_ops_tab and cached st_ops_desc. > + if (!member) { > + btf_put(kern_btf); > + return -ENOENT; > + } > + attr->attach_btf_id =3D st_ops_desc->type_id; > + attr->expected_attach_type =3D member - btf_type_member(st_ops_desc->ty= pe); > + btf_put(*btf); > + *btf =3D kern_btf; > + return 0; > +} > + > +u32 bpf_struct_ops_map_update_size(const struct bpf_map *map) > +{ > + const struct bpf_struct_ops_map *st_map =3D (const struct bpf_struct_op= s_map *)map; > + > + return st_map->local_type ? st_map->local_type->size : map->value_size; > +} > + > +/* > + * 'data' has the layout of the local struct. Copy every member to the p= lace > + * of the member with the same name in the struct of the kernel. > + */ > +static void *local_to_kern_value(const struct bpf_struct_ops_map *st_map= , const void *data) > +{ > + const struct btf_type *t =3D st_map->local_type, *kern_t =3D st_map->st= _ops_desc->type; > + const struct btf *btf =3D st_map->local_btf, *kern_btf =3D st_map->btf; > + const struct btf_member *member, *kern_member; > + const struct btf_type *mtype, *kern_mtype; > + struct bpf_struct_ops_value *value; > + u32 i, size, kern_size; > + int err =3D -EINVAL; > + > + value =3D kvzalloc(st_map->map.value_size, GFP_USER); > + if (!value) > + return ERR_PTR(-ENOMEM); > + > + for_each_member(i, t, member) { > + const void *src =3D data + __btf_member_bit_offset(t, member) / 8; > + > + if (__btf_member_bitfield_size(t, member) || > + IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, member->type), &s= ize))) > + goto err; > + kern_member =3D find_member(kern_btf, kern_t, > + btf_name_by_offset(btf, member->name_off)); > + if (!kern_member) { > + /* This kernel doesn't have the member. That's fine when it's not in = use. */ > + if (!memchr_inv(src, 0, size)) > + continue; > + err =3D -ENOENT; > + goto err; > + } > + if (__btf_member_bitfield_size(kern_t, kern_member) || > + IS_ERR(btf_resolve_size(kern_btf, > + btf_type_by_id(kern_btf, kern_member->type), > + &kern_size)) || > + size !=3D kern_size) > + goto err; > + mtype =3D btf_type_skip_modifiers(btf, member->type, NULL); > + kern_mtype =3D btf_type_skip_modifiers(kern_btf, kern_member->type, NU= LL); > + if (BTF_INFO_KIND(mtype->info) !=3D BTF_INFO_KIND(kern_mtype->info)) > + goto err; > + if (btf_type_is_ptr(mtype) && > + !!btf_type_resolve_func_ptr(btf, member->type, NULL) !=3D > + !!btf_type_resolve_func_ptr(kern_btf, kern_member->type, NULL)) > + goto err; > + memcpy(value->data + __btf_member_bit_offset(kern_t, kern_member) / 8,= src, size); > + } > + err =3D check_zero_holes(btf, t, data); [Severity: High] Can a crafted local BTF cause local_to_kern_value() to read past the end of data? map_update_elem() allocates data with size st_map->local_type->size (t->size). Unlike kernel BTF, a user-loaded BTF validated by BPF_BTF_LOAD allows a BTF_KIND_INT member where member_type->size exceeds the remaining bytes in t->size: btf_int_check_meta() only requires BITS_ROUNDUP_BYTES(nr_bits) <=3D t->size, and btf_int_check_member() checks struct_size - bytes_offset >=3D BITS_ROUNDUP_BYTES(nr_copy_bits) rather than member_type->size, while btf_resolve_size() returns size =3D member_type->size. Because neither local_to_kern_value() nor check_zero_holes() verifies that moff + size <=3D t->size, memchr_inv(src, = 0, size) on an absent member (or memcpy() on a matched member) can read out of bounds on the kernel heap. Also, __btf_member_bitfield_size(t, member) returns 0 whenever !btf_type_kflag(t), so non-kflag bitfields and non-byte-aligned bit offsets (__btf_member_bit_offset(t, member) % 8 !=3D 0) are not rejected. And because btf_struct_check_meta() only checks that member offsets are non-decreasing rather than non-overlapping (moff >=3D prev_mend), overlappi= ng members in t can move prev_mend backwards in check_zero_holes() and bypass padding hole checks. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130-lskel-stru= ct-ops-v2-0-8b76e65073ab@google.com?part=3D1