BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yassine Ouamer" <yassine.ouamer@proton.me>
Cc: linux-input@vger.kernel.org, bpf@vger.kernel.org,
	dmitry.torokhov@gmail.com
Subject: Re: [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer
Date: Wed, 02 Dec 2026 21:40:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-246651@kernel.org> (raw)
In-Reply-To: <20261202-hid-bpf-event-buffer-v1-3-53e3b6ce8336@proton.me>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] HID: bpf: unclamped *size in dispatch_hid_bpf_device_event()
  when size is 0
--

commit 427f1ddc5c111a0ad5c8eadb8072a9ea963b193e
Author: Yassine Ouamer <yassine.ouamer@proton.me>

HID: bpf: don't copy a report longer than the device event buffer

This commit clamps the input report size copied into the HID-BPF device
event buffer to hdev->bpf.allocated_data to prevent an out-of-bounds write
when a device sends a report larger than declared in its descriptor.

> diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
> index 714014560fb8d..df4c30dd6c05d 100644
> --- a/drivers/hid/bpf/hid_bpf_dispatch.c
> +++ b/drivers/hid/bpf/hid_bpf_dispatch.c
> @@ -50,8 +50,15 @@ dispatch_hid_bpf_device_event(struct hid_device *hdev, enum hid_report_type type
>  	if (!hdev->bpf.device_data)
>  		return data;
>  
> +	/*
> +	 * A device may send a report longer than its report descriptor
> +	 * declares, and so longer than the buffer: the programs get what
> +	 * fits, which holds the longest report the descriptor declares.
> +	 */
> +	ctx_kern.ctx.size = min(*size, hdev->bpf.allocated_data);
> +
>  	memset(ctx_kern.data, 0, hdev->bpf.allocated_data);
> -	memcpy(ctx_kern.data, data, *size);
> +	memcpy(ctx_kern.data, data, ctx_kern.ctx.size);
>  
>  	rcu_read_lock();
>  	list_for_each_entry_rcu(e, &hdev->bpf.prog_list, list) {

[Severity: Medium]
Can *size remain larger than hdev->bpf.allocated_data on return if a BPF
program sets ctx->size (or ctx->retval) to 0 and returns 0?

hid_bpf_ops_btf_struct_access() allows hid_device_event programs to write
to ctx->retval / ctx->size, and the exit path only writes ctx_kern.ctx.size
back to *size when it is non-zero:

drivers/hid/bpf/hid_bpf_dispatch.c:dispatch_hid_bpf_device_event() {
	...
	ret = ctx_kern.ctx.size;
	if (ret) {
		if (ret > ctx_kern.ctx.allocated_size)
			return ERR_PTR(-EINVAL);

		*size = ret;
	}

	*buf_size = ctx_kern.ctx.allocated_size;
	return ctx_kern.data;
}

When ctx_kern.ctx.size is 0, dispatch_hid_bpf_device_event() returns
hdev->bpf.device_data while leaving *size at the original oversized report
length, so __hid_input_report() can read past the end of
hdev->bpf.device_data in hid_dump_report() and hdrv->raw_event(). Clamping
*size directly before initializing ctx_kern.ctx.size would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-hid-bpf-event-buffer-v1-0-53e3b6ce8336@proton.me?part=3

  reply	other threads:[~2026-12-02 21:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:40   ` sashiko-bot [this message]
2026-12-02 21:22 ` [PATCH 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-246651@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yassine.ouamer@proton.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox