* [PATCH v2 0/2] media: uvcvideo: Do not read beyond the uvc_status urb
@ 2026-10-02 11:21 Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 1/2] media: uvcvideo: Introduce scattered field for uvc_control_mapping Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 2/2] media: uvcvideo: Do not read beyond the uvc_status_control memory Ricardo Ribalda
0 siblings, 2 replies; 3+ messages in thread
From: Ricardo Ribalda @ 2026-10-02 11:21 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: Mauro Carvalho Chehab, linux-media, linux-kernel,
Ricardo Ribalda, stable
When we receive an interrupt status with a control change, verify that
the urb has enough data before we read it.
While fixing it we figured out that one of the mappings was using an
invalid size. It was working fine because it used a custom map function,
but now we rely on that value.
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
Changes in v2:
- Use wMaxPacketSize for the urb size (Thanks Laurent).
- New patch to set the proper size of V4L2_CID_ZOOM_CONTINUOUS.
- Link to v1: https://lore.kernel.org/r/20260813-uvc-status-11-v1-1-2cf43e9590b0@chromium.org
---
Ricardo Ribalda (2):
media: uvcvideo: Introduce scattered field for uvc_control_mapping
media: uvcvideo: Do not read beyond the uvc_status_control memory
drivers/media/usb/uvc/uvc_ctrl.c | 18 +++++++++++++-----
drivers/media/usb/uvc/uvc_driver.c | 2 +-
drivers/media/usb/uvc/uvc_status.c | 18 +++++++++++++-----
drivers/media/usb/uvc/uvcvideo.h | 15 ++++++++++++---
4 files changed, 39 insertions(+), 14 deletions(-)
---
base-commit: 7fa44c2c123c17c4a3856ffac5e384409267cced
change-id: 20260813-uvc-status-11-99b9e27a8ea9
Best regards,
--
Ricardo Ribalda <ribalda@chromium.org>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] media: uvcvideo: Introduce scattered field for uvc_control_mapping
2026-10-02 11:21 [PATCH v2 0/2] media: uvcvideo: Do not read beyond the uvc_status urb Ricardo Ribalda
@ 2026-10-02 11:21 ` Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 2/2] media: uvcvideo: Do not read beyond the uvc_status_control memory Ricardo Ribalda
1 sibling, 0 replies; 3+ messages in thread
From: Ricardo Ribalda @ 2026-10-02 11:21 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: Mauro Carvalho Chehab, linux-media, linux-kernel,
Ricardo Ribalda, stable
Most of the mappings are contiguous, but the one for
V4L2_CID_ZOOM_CONTINUOUS is not. Instead it uses bytes 0 and 2 but
not 1.
To force reading the byte 1 every time we set the control we used a
fake size of 0, but that was far from nice.
Instead make a new field for this kind of controls.
Cc: stable@vger.kernel.org
Fixes: e5225c820c05 ("media: uvcvideo: Send a control event when a Control Change interrupt arrives")
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_ctrl.c | 6 ++++--
drivers/media/usb/uvc/uvcvideo.h | 6 ++++++
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index d2f722bc3459..6224ecf28ec6 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -998,7 +998,9 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
.id = V4L2_CID_ZOOM_CONTINUOUS,
.entity = UVC_GUID_UVC_CAMERA,
.selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
- .size = 0,
+ .size = 24,
+ /* Byte 1 (bDigitalZoom) is not part of the mapping. */
+ .scattered = true,
.offset = 0,
.v4l2_type = V4L2_CTRL_TYPE_INTEGER,
.data_type = UVC_CTRL_DATA_TYPE_SIGNED,
@@ -2815,7 +2817,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
* needs to be loaded from the device to perform the read-modify-write
* operation.
*/
- if ((ctrl->info.size * 8) != mapping->size) {
+ if ((ctrl->info.size * 8) != mapping->size || mapping->scattered) {
ret = __uvc_ctrl_load_cur(chain, ctrl);
if (ret < 0)
return ret;
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af8cef94ffff..69ee3d0c971d 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -126,6 +126,12 @@ struct uvc_control_mapping {
*/
u8 size;
+ /*
+ * True if the mapping does not cover all bits between offset and
+ * offset + size.
+ */
+ bool scattered;
+
u8 offset;
enum v4l2_ctrl_type v4l2_type;
u32 data_type;
--
2.56.0.rc1.315.gc6ed9934b7-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] media: uvcvideo: Do not read beyond the uvc_status_control memory
2026-10-02 11:21 [PATCH v2 0/2] media: uvcvideo: Do not read beyond the uvc_status urb Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 1/2] media: uvcvideo: Introduce scattered field for uvc_control_mapping Ricardo Ribalda
@ 2026-10-02 11:21 ` Ricardo Ribalda
1 sibling, 0 replies; 3+ messages in thread
From: Ricardo Ribalda @ 2026-10-02 11:21 UTC (permalink / raw)
To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
Guennadi Liakhovetski
Cc: Mauro Carvalho Chehab, linux-media, linux-kernel,
Ricardo Ribalda, stable
When we receive an event from the camera we only receive up to
wMaxPacketSize bytes. If a v4l2 control is mapped into a UVC control
beyond those bytes, right now the code is blindly reading them.
Add a check in the event handler to ignore controls that are outside the
data provided by the camera.
Note that this patch now dynamically allocates the size for the status
based on wMaxPacketSize instead of blindly use 16 bytes.
Cc: stable@vger.kernel.org
Closes: https://sashiko.dev/#/patchset/F0F008459FFA835D%2B20260813074632.2021311-1-raoxu%40uniontech.com
Fixes: e5225c820c05 ("media: uvcvideo: Send a control event when a Control Change interrupt arrives")
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
drivers/media/usb/uvc/uvc_ctrl.c | 12 +++++++++---
drivers/media/usb/uvc/uvc_driver.c | 2 +-
drivers/media/usb/uvc/uvc_status.c | 18 +++++++++++++-----
drivers/media/usb/uvc/uvcvideo.h | 9 ++++++---
4 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index 6224ecf28ec6..e61235e83e80 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -2142,7 +2142,8 @@ static int uvc_ctrl_clear_handle(struct uvc_control *ctrl)
}
void uvc_ctrl_status_event(struct uvc_video_chain *chain,
- struct uvc_control *ctrl, const u8 *data)
+ struct uvc_control *ctrl, const u8 *data,
+ size_t size)
{
struct uvc_control_mapping *mapping;
struct uvc_fh *handle;
@@ -2162,6 +2163,9 @@ void uvc_ctrl_status_event(struct uvc_video_chain *chain,
if (uvc_ctrl_mapping_is_compound(mapping))
value = 0;
+ else if (DIV_ROUND_UP(mapping->offset + mapping->size, 8) >
+ size)
+ continue;
else
value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data);
@@ -2191,7 +2195,7 @@ static void uvc_ctrl_status_event_work(struct work_struct *work)
struct uvc_ctrl_work *w = &dev->async_ctrl;
int ret;
- uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
+ uvc_ctrl_status_event(w->chain, w->ctrl, w->data, w->size);
/* The barrier is needed to synchronize with uvc_status_stop(). */
if (smp_load_acquire(&dev->flush_status))
@@ -2206,7 +2210,8 @@ static void uvc_ctrl_status_event_work(struct work_struct *work)
}
bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
- struct uvc_control *ctrl, const u8 *data)
+ struct uvc_control *ctrl, const u8 *data,
+ size_t size)
{
struct uvc_device *dev = chain->dev;
struct uvc_ctrl_work *w = &dev->async_ctrl;
@@ -2218,6 +2223,7 @@ bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
w->urb = urb;
w->chain = chain;
w->ctrl = ctrl;
+ w->size = size;
schedule_work(&w->work);
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index 468d46dfb9e7..b57825fadea1 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -1305,7 +1305,7 @@ static void uvc_gpio_event(struct uvc_device *dev)
/* GPIO entities are always on the first chain. */
chain = list_first_entry(&dev->chains, struct uvc_video_chain, list);
- uvc_ctrl_status_event(chain, unit->controls, &new_val);
+ uvc_ctrl_status_event(chain, unit->controls, &new_val, sizeof(new_val));
}
static int uvc_gpio_get_cur(struct uvc_device *dev, struct uvc_entity *entity,
diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c
index 97c939206302..a88fa57513fb 100644
--- a/drivers/media/usb/uvc/uvc_status.c
+++ b/drivers/media/usb/uvc/uvc_status.c
@@ -169,6 +169,7 @@ static bool uvc_event_control(struct urb *urb,
struct uvc_device *dev = urb->context;
struct uvc_video_chain *chain;
struct uvc_control *ctrl;
+ size_t ctrl_len;
if (len < 6 || status->bEvent != 0 ||
status->control.bAttribute >= ARRAY_SIZE(attrs)) {
@@ -187,8 +188,10 @@ static bool uvc_event_control(struct urb *urb,
switch (status->control.bAttribute) {
case UVC_CTRL_VALUE_CHANGE:
+ ctrl_len = len - offsetof(struct uvc_status, control.bValue);
return uvc_ctrl_status_event_async(urb, chain, ctrl,
- status->control.bValue);
+ status->control.bValue,
+ ctrl_len);
case UVC_CTRL_INFO_CHANGE:
case UVC_CTRL_FAILURE_CHANGE:
@@ -257,13 +260,18 @@ int uvc_status_init(struct uvc_device *dev)
struct usb_host_endpoint *ep = dev->int_ep;
unsigned int pipe;
int interval;
+ size_t size;
mutex_init(&dev->status_lock);
if (ep == NULL)
return 0;
- dev->status = kzalloc_obj(*dev->status);
+ size = usb_endpoint_maxp(&ep->desc);
+ if (!size)
+ return -EINVAL;
+
+ dev->status = kzalloc(size, GFP_KERNEL);
if (!dev->status)
return -ENOMEM;
@@ -286,7 +294,7 @@ int uvc_status_init(struct uvc_device *dev)
interval = fls(interval) - 1;
usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
- dev->status, sizeof(*dev->status), uvc_status_complete,
+ dev->status, size, uvc_status_complete,
dev, interval);
uvc_input_init(dev);
@@ -360,7 +368,7 @@ static void uvc_status_stop(struct uvc_device *dev)
* process it synchronously.
*/
if (cancel_work_sync(&w->work))
- uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
+ uvc_ctrl_status_event(w->chain, w->ctrl, w->data, w->size);
/* Kill the urb. */
usb_kill_urb(dev->int_urb);
@@ -372,7 +380,7 @@ static void uvc_status_stop(struct uvc_device *dev)
* uvc_status_start() call.
*/
if (cancel_work_sync(&w->work))
- uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
+ uvc_ctrl_status_event(w->chain, w->ctrl, w->data, w->size);
}
int uvc_status_resume(struct uvc_device *dev)
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index 69ee3d0c971d..788cee0c409d 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -567,7 +567,7 @@ struct uvc_status_streaming {
struct uvc_status_control {
u8 bSelector;
u8 bAttribute;
- u8 bValue[11];
+ u8 bValue[];
} __packed;
struct uvc_status {
@@ -629,6 +629,7 @@ struct uvc_device {
struct uvc_video_chain *chain;
struct uvc_control *ctrl;
const void *data;
+ size_t size;
} async_ctrl;
struct uvc_entity *gpio_unit;
@@ -771,9 +772,11 @@ int uvc_ctrl_init_device(struct uvc_device *dev);
void uvc_ctrl_cleanup_device(struct uvc_device *dev);
int uvc_ctrl_restore_values(struct uvc_device *dev);
bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
- struct uvc_control *ctrl, const u8 *data);
+ struct uvc_control *ctrl, const u8 *data,
+ size_t size);
void uvc_ctrl_status_event(struct uvc_video_chain *chain,
- struct uvc_control *ctrl, const u8 *data);
+ struct uvc_control *ctrl, const u8 *data,
+ size_t size);
int uvc_ctrl_begin(struct uvc_video_chain *chain);
int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
--
2.56.0.rc1.315.gc6ed9934b7-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-02 11:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-02 11:21 [PATCH v2 0/2] media: uvcvideo: Do not read beyond the uvc_status urb Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 1/2] media: uvcvideo: Introduce scattered field for uvc_control_mapping Ricardo Ribalda
2026-10-02 11:21 ` [PATCH v2 2/2] media: uvcvideo: Do not read beyond the uvc_status_control memory Ricardo Ribalda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®