* [PATCH bpf-next v2 1/2] bpf: Reject offloaded maps as map-in-map inner maps
@ 2026-11-18 16:16 Shihuang Liu
2026-11-18 16:16 ` [PATCH bpf-next v2 2/2] bpf: Reject maps without iter_seq_info in bpf_iter_attach_map Shihuang Liu
0 siblings, 1 reply; 2+ messages in thread
From: Shihuang Liu @ 2026-11-18 16:16 UTC (permalink / raw)
To: bpf
Cc: netdev, linux-kernel, ast, daniel, andrii, eddyz87, memxor,
martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai,
kuba, qmo, Shihuang Liu
Offloaded ARRAY/HASH maps share map_type with the software maps but use
bpf_map_offload_ops and a different object layout. bpf_map_meta_equal()
compares map_type, key/value size, flags and the btf record only, so an
offloaded map can be inserted into an outer map created from a software
template. The verifier and JIT then use the software layout on the
offloaded object: an ARRAY lookup writes to bpf_offloaded_map.dev_priv
and a HASH lookup reads bpf_htab fields past the 320-byte allocation.
An offloaded map used as inner_map_fd is accepted too and leads to a
NULL map_lookup_elem call.
Drop map_meta_equal from bpf_map_offload_ops so the existing check
rejects offloaded templates, and compare ops in bpf_map_meta_equal() so
an offloaded candidate no longer matches a software template.
Fixes: a38845729ea398 ("bpf: offload: add map offload infrastructure")
Link: https://lore.kernel.org/all/20261112105620.57520-1-shlomojune6@gmail.com/
Signed-off-by: Shihuang Liu <shlomojune6@gmail.com>
---
kernel/bpf/map_in_map.c | 7 ++++---
kernel/bpf/syscall.c | 1 -
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/map_in_map.c b/kernel/bpf/map_in_map.c
index 0fa3f2ac0f992f..9ec3bbfb5d4e3c 100644
--- a/kernel/bpf/map_in_map.c
+++ b/kernel/bpf/map_in_map.c
@@ -38,6 +38,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
if (!inner_map_meta)
return ERR_PTR(-ENOMEM);
+ inner_map_meta->ops = inner_map->ops;
inner_map_meta->map_type = inner_map->map_type;
inner_map_meta->key_size = inner_map->key_size;
inner_map_meta->value_size = inner_map->value_size;
@@ -65,7 +66,6 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
}
/* Misc members not needed in bpf_map_meta_equal() check. */
- inner_map_meta->ops = inner_map->ops;
if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops) {
struct bpf_array *inner_array_meta =
container_of(inner_map_meta, struct bpf_array, map);
@@ -88,8 +88,9 @@ void bpf_map_meta_free(struct bpf_map *map_meta)
bool bpf_map_meta_equal(const struct bpf_map *meta0,
const struct bpf_map *meta1)
{
- /* No need to compare ops because it is covered by map_type */
- return meta0->map_type == meta1->map_type &&
+ /* An offloaded map has the map_type of a host map and its own ops */
+ return meta0->ops == meta1->ops &&
+ meta0->map_type == meta1->map_type &&
meta0->key_size == meta1->key_size &&
meta0->value_size == meta1->value_size &&
meta0->map_flags == meta1->map_flags &&
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 60c4d84b9aba6f..0cae12c3f70849 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -112,7 +112,6 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
}
const struct bpf_map_ops bpf_map_offload_ops = {
- .map_meta_equal = bpf_map_meta_equal,
.map_alloc = bpf_map_offload_map_alloc,
.map_free = bpf_map_offload_map_free,
.map_check_btf = map_check_no_btf,
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH bpf-next v2 2/2] bpf: Reject maps without iter_seq_info in bpf_iter_attach_map
2026-11-18 16:16 [PATCH bpf-next v2 1/2] bpf: Reject offloaded maps as map-in-map inner maps Shihuang Liu
@ 2026-11-18 16:16 ` Shihuang Liu
0 siblings, 0 replies; 2+ messages in thread
From: Shihuang Liu @ 2026-11-18 16:16 UTC (permalink / raw)
To: bpf
Cc: netdev, linux-kernel, ast, daniel, andrii, eddyz87, memxor,
martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai,
kuba, qmo, Shihuang Liu
bpf_iter_attach_map() checks map_type only, so an offloaded ARRAY/HASH
map can be attached to a bpf_map_elem iterator. Offloaded maps have no
iter_seq_info, so __get_seq_info() falls back to the target's seq_info,
which is NULL for bpf_map_elem, and prepare_seq_file() dereferences it
when the iterator is opened.
Reject maps without ops->iter_seq_info. All map types accepted here
provide it.
Fixes: d6c4503cc296 ("bpf: Implement bpf iterator for hash maps")
Link: https://lore.kernel.org/all/20261112105620.57520-1-shlomojune6@gmail.com/
Signed-off-by: Shihuang Liu <shlomojune6@gmail.com>
---
kernel/bpf/map_iter.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/kernel/bpf/map_iter.c b/kernel/bpf/map_iter.c
index a58c148692e6c3..9e459a2d34c3aa 100644
--- a/kernel/bpf/map_iter.c
+++ b/kernel/bpf/map_iter.c
@@ -176,6 +176,10 @@ static int bpf_iter_attach_map(struct bpf_prog *prog,
map->map_type != BPF_MAP_TYPE_RHASH)
goto put_map;
+ /* An offloaded map has the map_type of a host map and its own ops */
+ if (!map->ops->iter_seq_info)
+ goto put_map;
+
aux->map = map;
err = bpf_iter_map_check_prog(prog, aux);
if (err)
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-18 16:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 16:16 [PATCH bpf-next v2 1/2] bpf: Reject offloaded maps as map-in-map inner maps Shihuang Liu
2026-11-18 16:16 ` [PATCH bpf-next v2 2/2] bpf: Reject maps without iter_seq_info in bpf_iter_attach_map Shihuang Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®