media: v4l2-ctrls: set handler error if array allocation fails
Subsystems (2)
PATCH 1/1 media: v4l2-ctrls: set handler error if array allocation fails
The control framework lets drivers create controls without checking every call: a function that fails stores the error in hdl->error and returns NULL, and the driver checks hdl->error once at the end.
v4l2_ctrl_new() does not follow this when the allocation of ctrl->p_array fails. It frees the control and returns NULL, but leaves hdl->error untouched, so the driver sees no error and carries on with a NULL control pointer.
vivid hits this with its pixel array control. vivid_create_controls() returns 0, and vivid_create_instance() goes on to call vivid_update_format_cap(), which passes dev->pixel_array to v4l2_ctrl_modify_dimensions() and dereferences it in v4l2_ctrl_lock().
Reproduced with fault injection (failslab and fail-nth) by failing that allocation while loading vivid:
KASAN: null-ptr-deref in range [0x0000000000000020-0x0000000000000027] RIP: 0010:vivid_update_format_cap+0xc83/0x1b30 [vivid] vivid_probe+0x37de/0x90b0 [vivid] platform_probe+0x9e/0x140 really_probe+0x1cb/0x6b0
The allocation was first added for dynamic arrays only. Commit 5f2c5c69a61d ("media: v4l2-ctrls: allocate space for arrays") then extended it to all array controls.
Set hdl->error to -ENOMEM in this error path, like the other allocation failures in v4l2_ctrl_new() do. The vivid probe then fails with -ENOMEM.
---
Found with a fault injection sweep over the vivid module load path (fail-nth, one failed allocation per load) on media-committers next. An LLM assistant helped with the sweep tooling, the analysis and the wording of this changelog.
Tested by re-running the sweep with the fix applied: failing the p_array allocation, including the one for the pixel array control, now makes the vivid probe fail with -ENOMEM and no longer oopses.
drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 + 1 file changed, 1 insertion(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +1 -0
@@ -2263,6 +2263,7 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, ctrl->p_array =kvzalloc (2 * elems * elem_size,GFP_KERNEL );if (!ctrl->p_array) {kvfree (ctrl); +handler_set_err (hdl, -ENOMEM );return NULL ; } data = ctrl->p_array;
Signature · 1 line
2.34.1