mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hyeonjun Hong <hyeonjunhong330@gmail.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Hans Verkuil <hverkuil+cisco@kernel.org>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	Nicolas Dufresne <nicolas.dufresne@collabora.com>,
	Benjamin Gaignard <benjamin.gaignard@collabora.com>,
	Hyeonjun Hong <hyeonjunhong330@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] media: v4l2-ctrls: set handler error if array allocation fails
Date: Tue, 24 Nov 2026 21:17:30 +0900	[thread overview]
Message-ID: <20261124121730.1598114-1-hyeonjunhong330@gmail.com> (raw)

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.

Fixes: fb582cba4492 ("media: v4l2-ctrls: add support for dynamically allocated arrays.")
Cc: stable@vger.kernel.org
Signed-off-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
---
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(+)

diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 6e751a043a21..12a799824c44 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -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;
-- 
2.34.1


                 reply	other threads:[~2026-11-24 12:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261124121730.1598114-1-hyeonjunhong330@gmail.com \
    --to=hyeonjunhong330@gmail.com \
    --cc=benjamin.gaignard@collabora.com \
    --cc=hverkuil+cisco@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=nicolas.dufresne@collabora.com \
    --cc=stable@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®