* [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle
@ 2026-07-22 4:23 Cen Zhang (Microsoft)
2026-07-27 14:58 ` Hans Verkuil
0 siblings, 1 reply; 3+ messages in thread
From: Cen Zhang (Microsoft) @ 2026-07-22 4:23 UTC (permalink / raw)
To: mchehab, linux-media
Cc: kees, linux-kernel, AutonomousCodeSecurity, tgopinath, kys,
blbllhy, stable
v4l2_event_subscribe() allows an unbounded number of event subscriptions
per file handle. Since the subscription id field is fully user-controlled
(32-bit), an unprivileged user with access to a V4L2 device node can
create up to 2^32 distinct subscriptions, each pinning a kernel
allocation (~200 bytes). This can exhaust kernel memory, causing an OOM
condition and kernel panic.
An unprivileged local user can trigger this by issuing repeated
VIDIOC_SUBSCRIBE_EVENT ioctls with incrementing id values. The allocated
objects reside in kernel slab (not accounted to the process cgroup), so
existing memory limits (ulimit, memcg) do not prevent this. Most V4L2
drivers are affected because the framework function v4l2_event_subscribe()
enforces no limit, such as uvcvideo (USB webcams) and the vicodec test
driver used to reproduce this issue. This leads to:
Kernel panic - not syncing: Out of memory: compulsory panic_on_oom
is enabled
Fix by adding a per-filehandle subscription counter and capping it at 256.
Fixes: 6e239399e580 ("[media] v4l2-ctrls: add control events")
Cc: stable@vger.kernel.org
Reported-by: Autonomous Code Security <AutonomousCodeSecurity@microsoft.com>
Link: https://lore.kernel.org/all/20260722004818.72310-1-blbllhy@gmail.com
Signed-off-by: Cen Zhang (Microsoft) <blbllhy@gmail.com>
---
v2:
- Add Cc: stable@vger.kernel.org
- Add Link: to v1
- Wrap long line in commit message
drivers/media/v4l2-core/v4l2-event.c | 14 +++++++++++++-
include/media/v4l2-fh.h | 2 ++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index 9dd2aaa95a67..fcd8ce4addc1 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -18,6 +18,9 @@
#include <linux/slab.h>
#include <linux/export.h>
+/* Per-filehandle limit on the number of event subscriptions. */
+#define V4L2_MAX_EVENT_SUBSCRIPTIONS 256
+
static unsigned int sev_pos(const struct v4l2_subscribed_event *sev, unsigned int idx)
{
idx += sev->first;
@@ -218,6 +221,7 @@ static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
fh->navailable--;
}
list_del(&sev->list);
+ fh->nsubscribed--;
}
int v4l2_event_subscribe(struct v4l2_fh *fh,
@@ -251,8 +255,16 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
- if (!found_ev)
+ if (!found_ev) {
+ if (fh->nsubscribed >= V4L2_MAX_EVENT_SUBSCRIPTIONS) {
+ spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ kvfree(sev);
+ mutex_unlock(&fh->subscribe_lock);
+ return -ENOSPC;
+ }
list_add(&sev->list, &fh->subscribed);
+ fh->nsubscribed++;
+ }
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
if (found_ev) {
diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
index aad4b3689d7e..65a7f31af889 100644
--- a/include/media/v4l2-fh.h
+++ b/include/media/v4l2-fh.h
@@ -33,6 +33,7 @@ struct v4l2_ctrl_handler;
* @subscribe_lock: serialise changes to the subscribed list; guarantee that
* the add and del event callbacks are orderly called
* @subscribed: list of subscribed events
+ * @nsubscribed: number of subscribed events at @subscribed list
* @available: list of events waiting to be dequeued
* @navailable: number of available events at @available list
* @sequence: event sequence number
@@ -49,6 +50,7 @@ struct v4l2_fh {
wait_queue_head_t wait;
struct mutex subscribe_lock;
struct list_head subscribed;
+ unsigned int nsubscribed;
struct list_head available;
unsigned int navailable;
u32 sequence;
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle
2026-07-22 4:23 [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle Cen Zhang (Microsoft)
@ 2026-07-27 14:58 ` Hans Verkuil
2026-07-30 22:27 ` Cen Zhang (Microsoft)
0 siblings, 1 reply; 3+ messages in thread
From: Hans Verkuil @ 2026-07-27 14:58 UTC (permalink / raw)
To: Cen Zhang (Microsoft), mchehab, linux-media
Cc: kees, linux-kernel, AutonomousCodeSecurity, tgopinath, kys,
stable
On 22/07/2026 06:23, Cen Zhang (Microsoft) wrote:
> v4l2_event_subscribe() allows an unbounded number of event subscriptions
> per file handle. Since the subscription id field is fully user-controlled
> (32-bit), an unprivileged user with access to a V4L2 device node can
> create up to 2^32 distinct subscriptions, each pinning a kernel
> allocation (~200 bytes). This can exhaust kernel memory, causing an OOM
> condition and kernel panic.
>
> An unprivileged local user can trigger this by issuing repeated
> VIDIOC_SUBSCRIBE_EVENT ioctls with incrementing id values. The allocated
> objects reside in kernel slab (not accounted to the process cgroup), so
> existing memory limits (ulimit, memcg) do not prevent this. Most V4L2
> drivers are affected because the framework function v4l2_event_subscribe()
> enforces no limit, such as uvcvideo (USB webcams) and the vicodec test
> driver used to reproduce this issue. This leads to:
>
> Kernel panic - not syncing: Out of memory: compulsory panic_on_oom
> is enabled
>
> Fix by adding a per-filehandle subscription counter and capping it at 256.
Hmm, nice catch.
But the problem is really at a higher uAPI level: the VIDIOC_SUBSCRIBE_EVENT and
VIDIOC_SUBDEV_SUBSCRIBE_EVENT ioctls should check the id field before passing
it to drivers.
Only two event types use the id field: V4L2_EVENT_CTRL and V4L2_EVENT_SOURCE_CHANGE.
The first event already checks the id field through helpers, but the second doesn't
check. And for other event types than these two the id field should just be set to 0.
Also, v4l2-compliance must check against this.
I'll post new patches for this tomorrow.
Regards,
Hans
>
> Fixes: 6e239399e580 ("[media] v4l2-ctrls: add control events")
> Cc: stable@vger.kernel.org
> Reported-by: Autonomous Code Security <AutonomousCodeSecurity@microsoft.com>
> Link: https://lore.kernel.org/all/20260722004818.72310-1-blbllhy@gmail.com
> Signed-off-by: Cen Zhang (Microsoft) <blbllhy@gmail.com>
> ---
> v2:
> - Add Cc: stable@vger.kernel.org
> - Add Link: to v1
> - Wrap long line in commit message
>
> drivers/media/v4l2-core/v4l2-event.c | 14 +++++++++++++-
> include/media/v4l2-fh.h | 2 ++
> 2 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
> index 9dd2aaa95a67..fcd8ce4addc1 100644
> --- a/drivers/media/v4l2-core/v4l2-event.c
> +++ b/drivers/media/v4l2-core/v4l2-event.c
> @@ -18,6 +18,9 @@
> #include <linux/slab.h>
> #include <linux/export.h>
>
> +/* Per-filehandle limit on the number of event subscriptions. */
> +#define V4L2_MAX_EVENT_SUBSCRIPTIONS 256
> +
> static unsigned int sev_pos(const struct v4l2_subscribed_event *sev, unsigned int idx)
> {
> idx += sev->first;
> @@ -218,6 +221,7 @@ static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
> fh->navailable--;
> }
> list_del(&sev->list);
> + fh->nsubscribed--;
> }
>
> int v4l2_event_subscribe(struct v4l2_fh *fh,
> @@ -251,8 +255,16 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
>
> spin_lock_irqsave(&fh->vdev->fh_lock, flags);
> found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
> - if (!found_ev)
> + if (!found_ev) {
> + if (fh->nsubscribed >= V4L2_MAX_EVENT_SUBSCRIPTIONS) {
> + spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
> + kvfree(sev);
> + mutex_unlock(&fh->subscribe_lock);
> + return -ENOSPC;
> + }
> list_add(&sev->list, &fh->subscribed);
> + fh->nsubscribed++;
> + }
> spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
>
> if (found_ev) {
> diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
> index aad4b3689d7e..65a7f31af889 100644
> --- a/include/media/v4l2-fh.h
> +++ b/include/media/v4l2-fh.h
> @@ -33,6 +33,7 @@ struct v4l2_ctrl_handler;
> * @subscribe_lock: serialise changes to the subscribed list; guarantee that
> * the add and del event callbacks are orderly called
> * @subscribed: list of subscribed events
> + * @nsubscribed: number of subscribed events at @subscribed list
> * @available: list of events waiting to be dequeued
> * @navailable: number of available events at @available list
> * @sequence: event sequence number
> @@ -49,6 +50,7 @@ struct v4l2_fh {
> wait_queue_head_t wait;
> struct mutex subscribe_lock;
> struct list_head subscribed;
> + unsigned int nsubscribed;
> struct list_head available;
> unsigned int navailable;
> u32 sequence;
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle
2026-07-27 14:58 ` Hans Verkuil
@ 2026-07-30 22:27 ` Cen Zhang (Microsoft)
0 siblings, 0 replies; 3+ messages in thread
From: Cen Zhang (Microsoft) @ 2026-07-30 22:27 UTC (permalink / raw)
To: hverkuil+cisco
Cc: AutonomousCodeSecurity, blbllhy, kees, kys, linux-kernel,
linux-media, mchehab, stable, tgopinath
Sure, thanks for the review and proposing the cleaner patch!
Regards,
Cen
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-30 22:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-22 4:23 [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle Cen Zhang (Microsoft)
2026-07-27 14:58 ` Hans Verkuil
2026-07-30 22:27 ` Cen Zhang (Microsoft)
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.