Skip to contentSkip to conversation

media: v4l2-core: v4l2-ctrls: check for handler_new_ref misuse

PATCH In review Hans Verkuil 2 participants 4 messages Nov 12, 2024 – Nov 26, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover media: v4l2-core: v4l2-ctrls: check for handler_new_ref misuse

This supersedes (1).

The first patch fixes the tegra-video driver: it now creates the new controls for the control handler first, before calling v4l2_ctrl_add_handler().

This prevents a warning, which is introduced in the next patch.

The second patch checks for adding a duplicate control to the same handler, and returns an error, but it also adds a warning if new controls are added to a handler after v4l2_ctrl_add_handler() is called.

Eventually this sequence should result in an error instead of a warning, but I feel it is better to keep the warning for a few kernel cycles to make sure any broken driver is fixed.

From my analysis it is only the tegra-video driver that has this issue, but I may have missed a corner case somewhere.

Regards,

Hans

1: https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

Hans Verkuil (2):
media: staging: tegra-video: postpone v4l2_ctrl_add_handler
media: v4l2-core: v4l2-ctrls: check for handler_new_ref misuse

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 46 ++++++++++++++++++++---
 drivers/staging/media/tegra-video/vi.c    | 11 +++---
 include/media/v4l2-ctrls.h                |  5 +++
 3 files changed, 50 insertions(+), 12 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 1/2 media: staging: tegra-video: postpone v4l2_ctrl_add_handler

The v4l2_ctrl_add_handler call should be done after all local controls are added. This ensures that local controls override any controls inherited from another handler.

This will be checked and a WARN will be issued if this happens, so fix this behavior in this driver.

---
 drivers/staging/media/tegra-video/vi.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/staging/media/tegra-video/vi.c +5 -6
index ad481b35e618..32db6a36d058 100644
@@ -980,6 +980,11 @@ static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
 	if (!subdev)
 		return -ENODEV;
 
+	if (chan->vi->soc->has_h_v_flip) {
+		v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
+		v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
+	}
+
 	ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
 				    NULL, true);
 	if (ret < 0) {
@@ -989,12 +994,6 @@ static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
 		v4l2_ctrl_handler_free(&chan->ctrl_handler);
 		return ret;
 	}
-
-	if (chan->vi->soc->has_h_v_flip) {
-		v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
-		v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
-	}
-
 #endif
 
 	/* setup the controls */
Signature · 1 line
2.45.2
#2 lore raw

PATCH v3 2/2 media: v4l2-core: v4l2-ctrls: check for handler_new_ref misuse

In reply to Hans Verkuil

An out-of-tree driver created a control handler, added controls, then called v4l2_ctrl_add_handler to add references to controls from another handler, and finally added another control that happened to have the same control ID as one of the controls from that other handler.

This caused a NULL pointer crash when an attempt was made to use that last control.

Besides the fact that two out-of-tree drivers used the same control ID for different (private) controls, which is obviously a bug, this specific scenario should have been caught. The root cause is the 'duplicate ID' check in handler_new_ref(): it expects that drivers will first add all controls to a control handler before calling v4l2_ctrl_add_handler. That way the local controls will always override references to controls from another handler.

It never considered the case where new local controls were added after calling v4l2_ctrl_add_handler. Add a check to handler_new_ref() to return an error in the case that a new local control is added with the same ID as an existing control reference. Also use WARN_ON since this is a driver bug.

This situation can only happen when out-of-tree drivers are used or during driver development, since mainlined drivers all have their own control ranges reserved in v4l2-controls.h, thus preventing duplicate control IDs.

In addition to this check, also add a WARN_ON if a new control is added to a local control handler, after v4l2_ctrl_add_handler() was called. For now just WARN, but this will return an error in the future.

---
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 46 ++++++++++++++++++++---
 include/media/v4l2-ctrls.h                |  5 +++
 2 files changed, 45 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +40 -6
index eeab6a5eb7ba..9ecf2e491b61 100644
@@ -1546,6 +1546,7 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
 	hdl->buckets = kvcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
 				GFP_KERNEL);
 	hdl->error = hdl->buckets ? 0 : -ENOMEM;
+	hdl->add_handler_called = false;
 	v4l2_ctrl_handler_init_request(hdl);
 	return hdl->error;
 }
@@ -1676,6 +1677,7 @@ int handler_new_ref(struct v4l2_ctrl_handler *hdl,
 	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
 	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
 	unsigned int size_extra_req = 0;
+	int ret = 0;
 
 	if (ctrl_ref)
 		*ctrl_ref = NULL;
@@ -1719,13 +1721,32 @@ int handler_new_ref(struct v4l2_ctrl_handler *hdl,
 	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
 		if (ref->ctrl->id < id)
 			continue;
-		/* Don't add duplicates */
-		if (ref->ctrl->id == id) {
-			kfree(new_ref);
-			goto unlock;
+		/* Check we're not adding a duplicate */
+		if (ref->ctrl->id != id) {
+			list_add(&new_ref->node, ref->node.prev);
+			break;
 		}
-		list_add(&new_ref->node, ref->node.prev);
-		break;
+
+		/*
+		 * If we add a new control to this control handler, and we find
+		 * that it is a duplicate, then that is a driver bug. Warn and
+		 * return an error.
+		 *
+		 * It can be caused by either adding the same control twice, or
+		 * by first calling v4l2_ctrl_add_handler, and then adding a new
+		 * control to this control handler.
+		 *
+		 * Either sequence is incorrect.
+		 *
+		 * However, if the control is owned by another handler, and
+		 * a control with that ID already exists in the list, then we
+		 * can safely skip it: in that case it the control is overridden
+		 * by the existing control.
+		 */
+		if (WARN_ON(hdl == ctrl->handler))
+			ret = -EEXIST;
+		kfree(new_ref);
+		goto unlock;
 	}
 
 insert_in_hash:
@@ -1742,10 +1763,22 @@ int handler_new_ref(struct v4l2_ctrl_handler *hdl,
 		 */
 		ctrl->cluster = &new_ref->ctrl;
 		ctrl->ncontrols = 1;
+		/*
+		 * It is a bad idea to add a new control to a handler once
+		 * v4l2_ctrl_add_handler() was called. For now just WARN
+		 * if this happens, but in the future this will be marked
+		 * as an error.
+		 *
+		 * A special exception is made for the control class type.
+		 */
+		if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS)
+			WARN_ON(hdl->add_handler_called);
 	}
 
 unlock:
 	mutex_unlock(hdl->lock);
+	if (ret)
+		return handler_set_err(hdl, ret);
 	return 0;
 }
 
@@ -2197,6 +2230,7 @@ int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
 	if (hdl->error)
 		return hdl->error;
 	mutex_lock(add->lock);
+	hdl->add_handler_called = true;
 	list_for_each_entry(ref, &add->ctrl_refs, node) {
 		struct v4l2_ctrl *ctrl = ref->ctrl;
 
include/media/v4l2-ctrls.h +5 -0
index 59679a42b3e7..e54d4a958cc5 100644
@@ -381,6 +381,10 @@ struct v4l2_ctrl_ref {
  * @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
  * @nr_of_buckets: Total number of buckets in the array.
  * @error:	The error code of the first failed control addition.
+ * @add_handler_called: v4l2_ctrl_add_handler() was called for this control
+ *		handler. If this is true, then WARN if someone attempts to
+ *		add new controls to this handler. Eventually the WARN will
+ *		be replaced by an error.
  * @request_is_queued: True if the request was queued.
  * @requests:	List to keep track of open control handler request objects.
  *		For the parent control handler (@req_obj.ops == NULL) this
@@ -404,6 +408,7 @@ struct v4l2_ctrl_handler {
 	void *notify_priv;
 	u16 nr_of_buckets;
 	int error;
+	bool add_handler_called;
 	bool request_is_queued;
 	struct list_head requests;
 	struct list_head requests_queued;
Signature · 1 line
2.45.2
#3 lore raw
In reply to Hans Verkuil

Hi Hans

Quoted 11 lines · Hans Verkuil

This supersedes (1).

The first patch fixes the tegra-video driver: it now creates the new controls for the control handler first, before calling v4l2_ctrl_add_handler().

This prevents a warning, which is introduced in the next patch.

The second patch checks for adding a duplicate control to the same handler, and returns an error, but it also adds a warning if new controls are added to a handler after v4l2_ctrl_add_handler() is called.

In the uvc driver, users can create controls with the ioct UVCIOC_CTRL_MAP
https://docs.kernel.org/userspace-api/media/drivers/uvcvideo.html

I guess that would be the equivalent of adding controls after v4l2_ctrl_add_handler()

Right now, uvc does not use the control framework, but I would like to dream about a world where this is possible :)

Is there a chance that you consider that usecase?

Regards!

Quoted 27 lines

Eventually this sequence should result in an error instead of a warning, but I feel it is better to keep the warning for a few kernel cycles to make sure any broken driver is fixed.

From my analysis it is only the tegra-video driver that has this issue, but I may have missed a corner case somewhere.

Regards,

Hans

1: https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

Hans Verkuil (2):
media: staging: tegra-video: postpone v4l2_ctrl_add_handler
media: v4l2-core: v4l2-ctrls: check for handler_new_ref misuse

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 46 ++++++++++++++++++++---
 drivers/staging/media/tegra-video/vi.c    | 11 +++---
 include/media/v4l2-ctrls.h                |  5 +++
 3 files changed, 50 insertions(+), 12 deletions(-)

--
2.45.2

Signature · 1 line
Ricardo Ribalda