Skip to content

HID: bpf: Fix hid_bpf_get_data() range check

Yiyang Chen bpf linux-input linux-kselftest lkml 4 versions – Applied

Status from this message: Applied to https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git (for-7.2/upstream-fixes), thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 1 replyApplied
  2. v33 patches 1 replySuperseded
  3. v22 patches 1 replySuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v12 patches2 repliesSuperseded

Changes in v4 from the cover letter

- Use check_add_overflow() in hid_bpf_get_data() before comparing the

Patches in v4 Applied

#Subject and filesSizeRepliesApplied
1/3HID: bpf: Fix hid_bpf_get_data() range checkdrivers/hid/bpf/hid_bpf_dispatch.c+4 -10 replies
2/3selftests/hid: Load only requested struct_ops mapstools/testing/selftests/hid/hid_bpf.c+18 -70 replies
3/3selftests/hid: Cover hid_bpf_get_data() size overflowtools/testing/selftests/hid/Makefile tools/testing/selftests/hid/hid_bpf.c tools/testing/selftests/hid/progs/hid.c+27 -10 replies

Reviews trailers per patch

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

Read the v4 thread

Cover letter [PATCH bpf-next v4 0/3] HID: bpf: Fix hid_bpf_get_data() range check

hid_bpf_get_data() exposes a pointer into the HID-BPF context data when the caller-provided offset and size fit inside ctx->allocated_size. The helper currently checks that range with:

rdwr_buf_size + offset > ctx->allocated_size

Since both operands are unsigned, a very large size can wrap the sum and make an out-of-range request look valid.

Patch 1 changes the helper to use check_add_overflow() for the range end and then compare the computed end against ctx->allocated_size.

Patch 2 updates the HID selftest loader to create only the struct_ops maps requested by the current test, so unrelated programs from the shared HID-BPF skeleton are not autoloaded.

Patch 3 adds a HID-BPF regression check that asks hid_bpf_get_data() for offset 2 and size ~0ULL from an rdesc_fixup callback and expects NULL. It also adds KHDR_INCLUDES to the HID selftest build so the userspace test sees current kernel UAPI HID definitions.

Changes in v4:
- Use check_add_overflow() in hid_bpf_get_data() before comparing the

    computed range end against ctx->allocated_size.

- Update the fix commit message to describe the overflow-helper check.

Changes in v3:
- Split out a HID selftest loader fix that disables autocreate for unrelated struct_ops maps.
- Add a Fixes tag to the selftest patch.
- Keep the BSS result flag in the rdesc fixup callback and explain why the callback must still return 0.

Changes in v2:
- Drop the temporary data variable around the overflow hid_bpf_get_data() call in the selftest callback.
- Correct the Fixes tag to commit 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs").

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

Yiyang Chen (3):
HID: bpf: Fix hid_bpf_get_data() range check selftests/hid: Load only requested struct_ops maps selftests/hid: Cover hid_bpf_get_data() size overflow

 drivers/hid/bpf/hid_bpf_dispatch.c      |  5 +++-
 tools/testing/selftests/hid/Makefile    |  2 +-
 tools/testing/selftests/hid/hid_bpf.c   | 36 ++++++++++++++++++++-----
 tools/testing/selftests/hid/progs/hid.c | 15 +++++++++++
 4 files changed, 49 insertions(+), 9 deletions(-)

base-commit: a975094bf98ca97be9146f9d3b5681a6f9cf5ce3

Signature · 1 line
2.34.1