[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20220329050637.15187-1-xiam0nd.tong@gmail.com>
Date: Tue, 29 Mar 2022 13:06:37 +0800
From: Xiaomeng Tong <xiam0nd.tong@...il.com>
To: laurent.pinchart@...asonboard.com, chehab@...nel.org
Cc: linux-media@...r.kernel.org, linux-kernel@...r.kernel.org,
Xiaomeng Tong <xiam0nd.tong@...il.com>
Subject: [PATCH v2] uvc: fix incorrect uses of list iterator
The bug is here:
pin = iterm->id;
The list iterator 'iterm' will point to a bogus position containing
HEAD if the list is empty or the element is not found after running
previous list_for_each_entry. As a result, iterm->id will lead to a
invalid memory access, and the check iterm->id != pin lately will
always be bypassed.
In addition, the list iterator 'iterm' will *always* be set and non-NULL
by list_for_each_entry(), so it is incorrect to assume that the iterator
value will be NULL if the element is not found in list, considering
the (mis)use here: "if (iterm == NULL".
Use a new value 'it' as the list iterator, while use the old value
'iterm' as a dedicated pointer to point to the found element, which
1. can fix this bug, due to 'iterm' is NULL only if it's not found.
2. do not need to change all the uses of 'iterm' after the loop.
3. can also limit the scope of the list iterator 'it' *only inside*
the traversal loop by simply declaring 'it' inside the loop in the
future, as usage of the iterator outside of the list_for_each_entry
is considered harmful. https://lkml.org/lkml/2022/2/17/1032
And remove the unneeded 'pin'.
Fixes: d5e90b7a6cd1c ("[media] uvcvideo: Move to video_ioctl2")
Reviewed-by: Laurent Pinchart <laurent.pinchart@...asonboard.com>
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@...il.com>
---
changes since v1:
- remove the unneeded 'pin'. (Laurent Pinchart)
v1:https://lore.kernel.org/all/20220319102222.3079-1-xiam0nd.tong@gmail.com/
---
drivers/media/usb/uvc/uvc_v4l2.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index 711556d13d03..2ca43ce814c9 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -871,29 +871,31 @@ static int uvc_ioctl_enum_input(struct file *file, void *fh,
struct uvc_video_chain *chain = handle->chain;
const struct uvc_entity *selector = chain->selector;
struct uvc_entity *iterm = NULL;
+ struct uvc_entity *it;
u32 index = input->index;
- int pin = 0;
if (selector == NULL ||
(chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
if (index != 0)
return -EINVAL;
- list_for_each_entry(iterm, &chain->entities, chain) {
- if (UVC_ENTITY_IS_ITERM(iterm))
+ list_for_each_entry(it, &chain->entities, chain) {
+ if (UVC_ENTITY_IS_ITERM(it)) {
+ iterm = it;
break;
+ }
}
- pin = iterm->id;
} else if (index < selector->bNrInPins) {
- pin = selector->baSourceID[index];
- list_for_each_entry(iterm, &chain->entities, chain) {
- if (!UVC_ENTITY_IS_ITERM(iterm))
+ list_for_each_entry(it, &chain->entities, chain) {
+ if (!UVC_ENTITY_IS_ITERM(it))
continue;
- if (iterm->id == pin)
+ if (it->id == selector->baSourceID[index];) {
+ iterm = it;
break;
+ }
}
}
- if (iterm == NULL || iterm->id != pin)
+ if (iterm == NULL)
return -EINVAL;
memset(input, 0, sizeof(*input));
--
2.17.1
Powered by blists - more mailing lists