The V4L2 control framework employs a deferred error handling mechanism for
control creation. Drivers can invoke multiple control creation helpers
(such as v4l2_ctrl_new_std()) without checking the return value of each
call. If an error occurs, such as a memory allocation failure during
v4l2_ctrl_handler_init() or v4l2_ctrl_new_*(), the handler records the
error in hdl->error, and subsequent control creation calls immediately
return NULL. Drivers check hdl->error once at the end of setup.
Drivers are documented and encouraged to cluster controls right after
creating them using helpers like v4l2_ctrl_cluster() or
v4l2_ctrl_auto_cluster(). However, v4l2_ctrl_cluster() checked
`WARN_ON(ncontrols == 0 || controls[0] == NULL)`. If an allocation failure
occurs prior to clustering, controls[0] is NULL. Because this condition can
legitimately happen when memory allocation fails under deferred error
handling, WARN_ON must not be used for conditions that can legitimately
happen, and pr_err should be used instead if necessary.
Additionally, on systems where panic_on_warn is not set and execution
continues after the warning, v4l2_ctrl_cluster() returns early, but
v4l2_ctrl_auto_cluster() proceeds to dereference master (controls[0]),
causing a NULL pointer dereference crash when evaluating master->minimum
and master->maximum. v4l2_ctrl_auto_cluster() also assigned `master =
controls[0]` unconditionally prior to checking ncontrols.
For instance, during hackrf_probe(), if handler initialization fails to
allocate memory, dev->rx_bandwidth_auto remains NULL. Calling
v4l2_ctrl_auto_cluster() with dev->rx_bandwidth_auto triggers the warning:
WARNING: drivers/media/v4l2-core/v4l2-ctrls-core.c:2525 at
v4l2_ctrl_auto_cluster+0x1c1/0x610
drivers/media/v4l2-core/v4l2-ctrls-core.c:2547
Call Trace:
hackrf_probe+0x80a/0x1360 drivers/media/usb/hackrf/hackrf.c:1430
usb_probe_interface+0x788/0xe50 drivers/usb/core/driver.c:399
really_probe+0x254/0xae0 drivers/base/dd.c:706
__driver_probe_device+0x1e8/0x360 drivers/base/dd.c:868
driver_probe_device+0x4f/0x240 drivers/base/dd.c:898
__device_attach_driver+0x270/0x410 drivers/base/dd.c:1026
bus_for_each_drv+0x258/0x2f0 drivers/base/bus.c:500
__device_attach+0x2c4/0x450 drivers/base/dd.c:1098
Fix this by separating the checks in v4l2_ctrl_cluster(): retain
WARN_ON(ncontrols == 0) as passing zero controls indicates a driver bug,
but silently return when controls[0] is NULL, consistent with other control
helpers like v4l2_ctrl_activate() and __v4l2_ctrl_grab(). In
v4l2_ctrl_auto_cluster(), safely initialize master with `ncontrols ?
controls[0] : NULL` and return immediately if master is NULL before
dereferencing any of its fields.
Fixes: c817d927b0f7 ("[media] v4l2-ctrls: replace BUG_ON by WARN_ON")
Assisted-by: Gemini:gemini-3.8-flash Gemini:gemini-3.1-pro-preview syzbot
Reported-by: syzbot+cf896de36144391bcde1@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=cf896de36144391bcde1
Link: https://syzkaller.appspot.com/ai_job?id=b5f6d956-eb5a-4244-a774-4b9f4cf9a078
To:
To: "Mauro Carvalho Chehab"
To: "Hans Verkuil"
Cc: "Hans Verkuil"
Cc:
Cc: "Ming Qian"
Cc: "Nicolas Dufresne"
Cc: "Pavan Bobba"
---
diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 5b8a594fb..e77ce7b56 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -2521,8 +2521,14 @@ void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
bool has_volatiles = false;
int i;
- /* The first control is the master control and it must not be NULL */
- if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
+ if (WARN_ON(ncontrols == 0))
+ return;
+
+ /*
+ * The first control is the master control. If this is NULL then
+ * there is nothing to do.
+ */
+ if (controls[0] == NULL)
return;
for (i = 0; i < ncontrols; i++) {
@@ -2540,12 +2546,14 @@ EXPORT_SYMBOL(v4l2_ctrl_cluster);
void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
u8 manual_val, bool set_volatile)
{
- struct v4l2_ctrl *master = controls[0];
+ struct v4l2_ctrl *master = ncontrols ? controls[0] : NULL;
u32 flag = 0;
int i;
v4l2_ctrl_cluster(ncontrols, controls);
WARN_ON(ncontrols <= 1);
+ if (!master)
+ return;
WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
master->is_auto = true;
base-commit: df2908090cda368b01ff43709f51890076c56157
--
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at syzkaller@googlegroups.com.