Skip to content

bpf: Fix NULL-ptr-derefs when showing a void BTF type

Jiayuan Chen bpf linux-kselftest lkml 3 versions – Applied

Status from this message: This series was applied to bpf/bpf.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest5 patches 0 repliesApplied 5/5
  2. v35 patches 8 repliesSuperseded
  3. v24 patches 0 repliesSuperseded

Patches in v4 All 5 applied

#Subject and filesSizeRepliesApplied
1/5bpf: Reject key-less BTF for hash mapskernel/bpf/hashtab.c+6 -00 repliesApplied
2/5bpf: Fix NULL-ptr-deref when showing a void BTF typekernel/bpf/btf.c+8 -10 repliesApplied
3/5bpf: Fix NULL-ptr-deref in btf_var_show()kernel/bpf/btf.c+9 -10 repliesApplied
4/5selftests/bpf: Add test for key-less BTF hash maptools/testing/selftests/bpf/prog_tests/btf_map_keyless.c+59 -00 repliesApplied
5/5selftests/bpf: Add test for showing a void BTF typetools/testing/selftests/bpf/prog_tests/snprintf_btf.c tools/testing/selftests/bpf/progs/snprintf_btf_void.c+103 -00 repliesApplied

Reviews trailers per patch

3 of 5 patches have a Reviewed-by or Acked-by.

PatchIhor Solodrai
1/5A-b
2/5A-b
3/5A-b
4/5
5/5

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH bpf v4 0/5] bpf: Fix NULL-ptr-derefs when showing a void BTF type

This series fixes three NULL-ptr-derefs in BTF handling.

Patch 1 handles the syzbot report. A key-less BTF (btf_key_type_id == 0) used to be rejected for hash maps, until htab and rhtab gained a ->map_check_btf (to register a dtor) that does not look at the key, so a key-less hash map is now accepted. Dumping it through bpffs feeds the key type_id 0 into btf_type_seq_show() and NULL-derefs in btf_type_show(). Reject it again.

Patches 2 and 3 fix two related, pre-existing crashes reachable via bpf_snprintf_btf(), which renders a type_id taken straight from the BPF program against the vmlinux BTF. A "const void" (a modifier resolving to void) NULL-derefs in btf_modifier_show() - void has no ->show op; a BTF_KIND_VAR NULL-derefs in btf_var_show() - the vmlinux base BTF has no resolved_ids. Patch 2 falls back to btf_df_show(), the "<unsupported kind:N>" placeholder already used for FWD/FUNC/FLOAT/DECL_TAG; patch 3 resolves the var's type directly, mirroring the existing guard in btf_modifier_show().

Patches 4 and 5 add selftests. Patch 4 checks a key-less hash and rhash map is rejected at creation. Patch 5 extends the snprintf_btf test to render a "const void" and a BTF_KIND_VAR from the vmlinux BTF and checks they resolve without crashing.

v3 -> v4: Drop the pin-and-read reproducer from the key-less map test, fold the void/VAR test into the existing snprintf_btf test, and simplify patch 2's wording (review comments).

v2 -> v3: Fold in a third fix for the same class of bug, btf_var_show(), reported while reviewing v2. Address review comments (Fixes attribution, verbatim syzbot trace, skip instead of fail).

v1 -> v2: AI reported a pre-exist issue. Let's fold it in this series.

v3: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/

Jiayuan Chen (5):
bpf: Reject key-less BTF for hash maps
bpf: Fix NULL-ptr-deref when showing a void BTF type
bpf: Fix NULL-ptr-deref in btf_var_show() selftests/bpf: Add test for key-less BTF hash map selftests/bpf: Add test for showing a void BTF type

 kernel/bpf/btf.c                              | 19 ++++-
 kernel/bpf/hashtab.c                          |  6 ++
 .../bpf/prog_tests/btf_map_keyless.c          | 59 ++++++++++++++
 .../selftests/bpf/prog_tests/snprintf_btf.c   | 79 +++++++++++++++++++
 .../selftests/bpf/progs/snprintf_btf_void.c   | 24 ++++++
 5 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_map_keyless.c
 create mode 100644 tools/testing/selftests/bpf/progs/snprintf_btf_void.c
Signature · 1 line
2.43.0