Skip to content

libbpf: Validate struct_ops member offsets

Mingpei CAO bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest3 patches 1 replyIn review

Changes in v2 from the cover letter

- name the local member and the matching kernel BTF type;
- remove JIT, compiler, and initialization details from the problem summary;
- check every offset used to create a data pointer;
- represent unsupported bitfield storage as padding while preserving the struct_ops map and supported field offsets;
- test adjacent bitfield members and an out-of-range non-bitfield member.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3libbpf: Validate struct_ops member offsets before data accesstools/lib/bpf/libbpf.c+68 -121 reply
2/3bpftool: Keep struct_ops field offsets when skipping bitfieldstools/bpf/bpftool/gen.c+97 -500 replies
3/3selftests/bpf: Cover struct_ops bitfield and offset validationtools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c tools/testing/selftests/bpf/progs/struct_ops_module.c+107 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH bpf v2 0/3] libbpf: Validate struct_ops member offsets

A local struct_ops type can contain a bitfield absent from the corresponding kernel BTF type. libbpf checks whether data for an absent local member is zero.

For a bitfield, member->offset contains the bit position and field width. bpf_map__init_kern_struct_ops() used the full value to create a data pointer before checking the member range. libbpf_is_mem_zeroed() could then read outside the local struct_ops data.
ASan reported a SEGV. bpf_map__init_kern_struct_ops() also failed to check offsets for non-bitfield members.

Patch 1 rejects local and kernel bitfields before reading member data. Patch 1 also checks every local and kernel member range before creating a data pointer.

Patch 2 represents unsupported bitfield storage as padding in generated skeletons. The padding preserves the struct_ops map and keeps supported fields at their declared BTF offsets.

Patch 3 checks the bitfield rejection, the generated C structure, and an out-of-range non-bitfield member.

AI assistance was used in preparing this series. I independently reviewed the changes and reproduced the issue and fix.

Changes since v1:
- name the local member and the matching kernel BTF type;
- remove JIT, compiler, and initialization details from the problem summary;
- check every offset used to create a data pointer;
- represent unsupported bitfield storage as padding while preserving the struct_ops map and supported field offsets;
- test adjacent bitfield members and an out-of-range non-bitfield member.

Mingpei CAO (3):
libbpf: Validate struct_ops member offsets before data access
bpftool: Keep struct_ops field offsets when skipping bitfields selftests/bpf: Cover struct_ops bitfield and offset validation

 tools/bpf/bpftool/gen.c                       | 147 ++++++++++++------
 tools/lib/bpf/libbpf.c                        |  80 ++++++++--
 .../bpf/prog_tests/test_struct_ops_module.c   |  91 ++++++++++-
 .../selftests/bpf/progs/struct_ops_module.c   |  17 ++
 4 files changed, 272 insertions(+), 63 deletions(-)

base-commit: 7d70a0b02d262971201fdd1e221586bdd95c2910

Signature · 1 line
2.43.0