Skip to contentSkip to conversation

usb: gadget: uvc: stability fixes on STREAMOFF.

PATCH Superseded Avichal Rakesh 6 participants 97 messages Sep 30, 2023 – Nov 14, 2023

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

cover usb: gadget: uvc: stability fixes on STREAMOFF.

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

 drivers/usb/gadget/function/f_uvc.c     |  11 +-
 drivers/usb/gadget/function/f_uvc.h     |   2 +-
 drivers/usb/gadget/function/uvc.h       |   6 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++-
 drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------
 5 files changed, 164 insertions(+), 65 deletions(-)

--
2.42.0.582.g8ccd20d70d-goog

#1 lore raw

PATCH 1/3 usb: gadget: uvc: prevent use of disabled endpoint

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 21 ++++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 27 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..75c9f9a3f884 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep) {
+		usb_ep_disable(uvc->video.ep);
+	}
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +18 -3
index 3f0a9795c0d4..3d3469883ed0 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,19 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0) {
+		return ret;
+	}
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +508,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	if (uvc->state == UVC_STATE_STREAMING) {
+		/*
+		 * Drop uvc->state to CONNECTED if it was streaming before.
+		 * This ensures that the usb_requests are no longer queued
+		 * to the controller.
+		 */
+		uvc->state = UVC_STATE_CONNECTED;
+	}
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +663,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..70ff88854539 100644
@@ -382,6 +382,7 @@ static void uvcg_video_pump(struct work_struct *work)
 {
 	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
+	struct uvc_device *uvc = video->uvc;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
@@ -390,7 +391,7 @@ static void uvcg_video_pump(struct work_struct *work)
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
#2 lore raw

PATCH v2 1/3 usb: gadget: uvc: prevent use of disabled endpoint

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message.

 drivers/usb/gadget/function/f_uvc.c    | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h    |  2 +-
 drivers/usb/gadget/function/uvc.h      |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c | 21 ++++++++++++++++++---
 4 files changed, 25 insertions(+), 11 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..75c9f9a3f884 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep) {
+		usb_ep_disable(uvc->video.ep);
+	}
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +18 -3
index 3f0a9795c0d4..3d3469883ed0 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,19 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0) {
+		return ret;
+	}
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +508,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	if (uvc->state == UVC_STATE_STREAMING) {
+		/*
+		 * Drop uvc->state to CONNECTED if it was streaming before.
+		 * This ensures that the usb_requests are no longer queued
+		 * to the controller.
+		 */
+		uvc->state = UVC_STATE_CONNECTED;
+	}
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +663,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-

--
2.42.0.582.g8ccd20d70d-goog

#3 lore raw

PATCH 2/3 usb: gadget: uvc: Allocate uvc_requests one at a time

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 90 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 42 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -41

140 lines not on this page · raw message

Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
#4 lore raw

PATCH v2 2/3 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 90 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 42 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -41

139 lines not on this page · raw message

--
2.42.0.582.g8ccd20d70d-goog

#5 lore raw

PATCH 3/3 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_request to mark it as 'abandoned'. When disabling the video stream, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by the gadget driver (as present in req_free). Other usb_requests have their corresponding 'is_abandoned' flag tripped, and the usb_requests complete handler takes care of freeing the usb_request and its corresponding uvc_request.

This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_video.c | 106 ++++++++++++++++++++----
 2 files changed, 91 insertions(+), 16 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..e69cfb7cced1 100644
@@ -82,6 +82,7 @@ struct uvc_request {
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
 	struct list_head list;
+	bool is_abandoned;
 };
 
 struct uvc_video {
drivers/usb/gadget/function/uvc_video.c +90 -16

148 lines not on this page · raw message

Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
#6 lore raw

PATCH v2 3/3 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_request to mark it as 'abandoned'. When disabling the video stream, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by the gadget driver (as present in req_free). Other usb_requests have their corresponding 'is_abandoned' flag tripped, and the usb_requests complete handler takes care of freeing the usb_request and its corresponding uvc_request.

This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_video.c | 118 ++++++++++++++++++++----
 2 files changed, 102 insertions(+), 17 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..e69cfb7cced1 100644
@@ -82,6 +82,7 @@ struct uvc_request {
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
 	struct list_head list;
+	bool is_abandoned;
 };
 
 struct uvc_video {
drivers/usb/gadget/function/uvc_video.c +101 -17

160 lines not on this page · raw message

--
2.42.0.582.g8ccd20d70d-goog

#7 lore raw
In reply to Avichal Rakesh

Hi

Quoted 13 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 6 lines
drivers/usb/gadget/function/f_uvc.c     |  11 +-
drivers/usb/gadget/function/f_uvc.h     |   2 +-
drivers/usb/gadget/function/uvc.h       |   6 +-
drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++-
drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------
5 files changed, 164 insertions(+), 65 deletions(-)

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

In the updated version I the stack runs into the following error, when enabling lockdep. Could you try your version with lockdep enabled?

[   41.278520] configfs-gadget.vz gadget.0: uvc: reset UVC
[   47.156261] configfs-gadget.vz gadget.0: uvc: uvc_function_set_alt(2, 0)
[   47.169177]
[   47.170903] ============================================
[   47.176857] WARNING: possible recursive locking detected
[   47.182798] 6.5.0-20230919-1+ #19 Tainted: G         C
[   47.189323] --------------------------------------------
[   47.195256] vzuvcd/412 is trying to acquire lock:
[   47.200511] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvc_video_complete+0x44/0x2e0
[   47.210172]
[   47.210172] but task is already holding lock:
[   47.216687] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvcg_video_enable+0x2d0/0x5c0
[   47.226333]
[   47.226333] other info that might help us debug this:
[   47.233625]  Possible unsafe locking scenario:
[   47.233625]
[   47.240242]        CPU0
[   47.242974]        ----
[   47.245709]   lock(&video->req_lock);
[   47.249802]   lock(&video->req_lock);
[   47.253897]
[   47.253897]  *** DEADLOCK ***
[   47.253897]
[   47.260511]  May be due to missing lock nesting notation
[   47.260511]

Regards,
Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw

Thank you for testing the patch, Michael!

Quoted 23 lines · Michael Grzeschik

Hi

Quoted 13 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
 usb: gadget: uvc: prevent use of disabled endpoint
 usb: gadget: uvc: Allocate uvc_requests one at a time
 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 6 lines

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

Ah, I didn't realize Greg had picked up your changes in his tree. Rebased the patches in V2.

Also want to point out that
https://lore.kernel.org/[email protected]/
may have introduced an issue when setting the uvc->state to UVC_STATE_CONNECTED in uvcg_video_enable.

Effectively, uvc_video_enable can be called
1. because the host asks to halt the stream, or
2. if the gadget was disabled.

Setting uvc->state to CONNECTED is fine for (1). In (2), uvc_v4l2 sets uvc->state to DISCONNECTED before calling uvcg_video_enable. In this case, your change would overwrite the value to CONNECTED without any further checks.

Quoted 22 lines

In the updated version I the stack runs into the following error, when enabling lockdep. Could you try your version with lockdep enabled?

[   41.278520] configfs-gadget.vz gadget.0: uvc: reset UVC
[   47.156261] configfs-gadget.vz gadget.0: uvc: uvc_function_set_alt(2, 0)
[   47.169177]
[   47.170903] ============================================
[   47.176857] WARNING: possible recursive locking detected
[   47.182798] 6.5.0-20230919-1+ #19 Tainted: G         C
[   47.189323] --------------------------------------------
[   47.195256] vzuvcd/412 is trying to acquire lock:
[   47.200511] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvc_video_complete+0x44/0x2e0 [   47.210172]
[   47.210172] but task is already holding lock:
[   47.216687] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvcg_video_enable+0x2d0/0x5c0 [   47.226333]
[   47.226333] other info that might help us debug this:
[   47.233625]  Possible unsafe locking scenario:
[   47.233625]
[   47.240242]        CPU0

Quoted 9 lines

[   47.242974]        ----
[   47.245709]   lock(&video->req_lock);
[   47.249802]   lock(&video->req_lock);
[   47.253897]
[   47.253897]  *** DEADLOCK ***
[   47.253897]
[   47.260511]  May be due to missing lock nesting notation [   47.260511]

Thank you for catching this. I couldn't repro the deadlock, but it looks like DWC3 controller can call the complete callback directly from usb_ep_dequeue for requests in its pending list. I am not sure if that is up to spec, considering calling usb_ep_queue explicitly forbids calling the complete callback from within. Regardless, it is easy enough to fix by moving the dequeue calls to before we acquire req_lock. The semantics of the rest of the patch is unchanged.

Uploading the fixed patches shortly.

Regards,
Avi.

#9 lore raw
Quoted 26 lines · Avichal Rakesh

Thank you for testing the patch, Michael!

Quoted 23 lines · Michael Grzeschik

Hi

Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
 usb: gadget: uvc: prevent use of disabled endpoint
 usb: gadget: uvc: Allocate uvc_requests one at a time
 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

Ah, I didn't realize Greg had picked up your changes in his tree. Rebased the patches in V2.

The "v2" series here is almost impossible to follow, sorry.

Please send it as a new thread, not as responses to the individual commits, how am I supposed to pick them up that way?

And make it v3 please.

thanks,

greg k-h

#10 lore raw
Quoted 33 lines · Greg Kroah-Hartman
Quoted 26 lines · Avichal Rakesh

Thank you for testing the patch, Michael!

Quoted 23 lines · Michael Grzeschik

Hi

Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
 usb: gadget: uvc: prevent use of disabled endpoint
 usb: gadget: uvc: Allocate uvc_requests one at a time
 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

Ah, I didn't realize Greg had picked up your changes in his tree. Rebased the patches in V2.

Quoted 6 lines

The "v2" series here is almost impossible to follow, sorry.

Please send it as a new thread, not as responses to the individual commits, how am I supposed to pick them up that way?

And make it v3 please.

Sent out v3 as a new thread. Sorry about that!

- Avi.

#11 lore raw
In reply to Michael Grzeschik
Quoted 21 lines · Michael Grzeschik
Quoted 13 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 6 lines
drivers/usb/gadget/function/f_uvc.c     |  11 +-
drivers/usb/gadget/function/f_uvc.h     |   2 +-
drivers/usb/gadget/function/uvc.h       |   6 +-
drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++-
drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------
5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Quoted 29 lines

In the updated version I the stack runs into the following error, when enabling lockdep. Could you try your version with lockdep enabled?

[   41.278520] configfs-gadget.vz gadget.0: uvc: reset UVC
[   47.156261] configfs-gadget.vz gadget.0: uvc: uvc_function_set_alt(2, 0)
[   47.169177]
[   47.170903] ============================================
[   47.176857] WARNING: possible recursive locking detected
[   47.182798] 6.5.0-20230919-1+ #19 Tainted: G         C
[   47.189323] --------------------------------------------
[   47.195256] vzuvcd/412 is trying to acquire lock:
[   47.200511] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvc_video_complete+0x44/0x2e0
[   47.210172]
[   47.210172] but task is already holding lock:
[   47.216687] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvcg_video_enable+0x2d0/0x5c0
[   47.226333]
[   47.226333] other info that might help us debug this:
[   47.233625]  Possible unsafe locking scenario:
[   47.233625]
[   47.240242]        CPU0
[   47.242974]        ----
[   47.245709]   lock(&video->req_lock);
[   47.249802]   lock(&video->req_lock);
[   47.253897]
[   47.253897]  *** DEADLOCK ***
[   47.253897]
[   47.260511]  May be due to missing lock nesting notation
[   47.260511]
Signature · 3 lines
Regards,

Laurent Pinchart
#12 lore raw

Hi Laurent

Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +-
drivers/usb/gadget/function/f_uvc.h     |   2 +-
drivers/usb/gadget/function/uvc.h       |   6 +-
drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++-
drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------
5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

What do you think?

Regards,
Michael

Quoted 35 lines
Quoted 29 lines

In the updated version I the stack runs into the following error, when enabling lockdep. Could you try your version with lockdep enabled?

[   41.278520] configfs-gadget.vz gadget.0: uvc: reset UVC
[   47.156261] configfs-gadget.vz gadget.0: uvc: uvc_function_set_alt(2, 0)
[   47.169177]
[   47.170903] ============================================
[   47.176857] WARNING: possible recursive locking detected
[   47.182798] 6.5.0-20230919-1+ #19 Tainted: G         C
[   47.189323] --------------------------------------------
[   47.195256] vzuvcd/412 is trying to acquire lock:
[   47.200511] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvc_video_complete+0x44/0x2e0
[   47.210172]
[   47.210172] but task is already holding lock:
[   47.216687] ffffff8009560928 (&video->req_lock){....}-{3:3}, at: uvcg_video_enable+0x2d0/0x5c0
[   47.226333]
[   47.226333] other info that might help us debug this:
[   47.233625]  Possible unsafe locking scenario:
[   47.233625]
[   47.240242]        CPU0
[   47.242974]        ----
[   47.245709]   lock(&video->req_lock);
[   47.249802]   lock(&video->req_lock);
[   47.253897]
[   47.253897]  *** DEADLOCK ***
[   47.253897]
[   47.260511]  May be due to missing lock nesting notation
[   47.260511]
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#13 lore raw
Quoted 48 lines · Michael Grzeschik

Hi Laurent

Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

Quoted 5 lines

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here. There are two cleanup paths:
1. uvcg_video_disable cleans up only the requests in req_free, and
2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

The other "complications" are around making sure we can trust the values in an inherently racey situation. The reasoning can admittedly be difficult to follow at a glance, which incidentally is why I went with a simple to prove timed wait in the past
(https://lore.kernel.org/[email protected]).

I am not suggesting we go back to a timed wait, but please do look at the patch and let me know which parts don't make sense, or are difficult to understand. We can add more documentation about our assumptions there, or if you have a way to do this that you think is simpler to reason about, then please let me know and I'll be more than happy to use that!

Regards,
Avi.

#14 lore raw

Hi Avichal,

Quoted 47 lines · Avichal Rakesh
Quoted 46 lines · Michael Grzeschik
Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

Quoted 7 lines
Quoted 5 lines

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here.

Yes, it was just to summ up what the latest state of the idea was, so Laurent does not read the whole thread in detail. Sorry for not being clear enough about that.

Quoted 7 lines

There are two cleanup paths:
1. uvcg_video_disable cleans up only the requests in req_free, and
2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

What I don't get is, why in the case of shutdown there needs to be something re-queued back to the gadget driver. There should not need to be any sort of barrier flag for the requests. Just the complete handler running past a barrier where it knows that the whole device is stopped. So every call on complete should then clean that exact request it is touching currently.

I don't know where the extra complexity comes from.

Quoted 6 lines

The other "complications" are around making sure we can trust the values in an inherently racey situation. The reasoning can admittedly be difficult to follow at a glance, which incidentally is why I went with a simple to prove timed wait in the past
(https://lore.kernel.org/[email protected]).

Quoted 6 lines

I am not suggesting we go back to a timed wait, but please do look at the patch and let me know which parts don't make sense, or are difficult to understand. We can add more documentation about our assumptions there, or if you have a way to do this that you think is simpler to reason about, then please let me know and I'll be more than happy to use that!

I really try to spin my head around the idea of the is_abondoned flag you are using. Unfortunatly for now I am out to debug the issues I see with your series.

So I did try these patches you send. Yes the deadlock error is gone with v3. But the linked list is still running into cases where dwc3_gadget_giveback(complete) is touching requests that are already freed.

[   61.408715] ------------[ cut here ]------------
[   61.413897] kernel BUG at lib/list_debug.c:56!
...
[   61.590762] Call trace:
[   61.596890]  __list_del_entry_valid+0xb8/0xe8
[   61.603408]  dwc3_gadget_giveback+0x3c/0x1b0
[   61.607594]  dwc3_remove_requests.part.0+0xcc/0x100
[   61.612948]  __dwc3_gadget_ep_disable+0xbc/0x1b8
[   61.621019]  dwc3_gadget_ep_disable+0x48/0x100
[   61.627925]  usb_ep_disable+0x3c/0x138
[   61.638230]  uvc_function_setup_continue+0x3c/0x60
[   61.645040]  uvc_v4l2_streamoff+0x5c/0x80
[   61.659812]  v4l_streamoff+0x40/0x60
[   61.668950]  __video_do_ioctl+0x344/0x420
[   61.679548]  video_usercopy+0x1d0/0x788
[   61.685677]  video_ioctl2+0x40/0x70
[   61.697439]  v4l2_ioctl+0x68/0xa0
[   61.709200]  __arm64_sys_ioctl+0x304/0xda0
[   61.720768]  invoke_syscall.constprop.0+0x70/0x130

Regards,
Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#15 lore raw
Quoted 54 lines · Michael Grzeschik

Hi Avichal,

Quoted 51 lines · Avichal Rakesh
Quoted 50 lines · Michael Grzeschik
Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here.

Yes, it was just to summ up what the latest state of the idea was, so Laurent does not read the whole thread in detail. Sorry for not being clear enough about that.

Whoops! Sorry about the misunderstanding!

Quoted 9 lines
Quoted 8 lines

There are two cleanup paths:
 1. uvcg_video_disable cleans up only the requests in req_free, and
 2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

Quoted 8 lines

What I don't get is, why in the case of shutdown there needs to be something re-queued back to the gadget driver. There should not need to be any sort of barrier flag for the requests. Just the complete handler running past a barrier where it knows that the whole device is stopped. So every call on complete should then clean that exact request it is touching currently.

I don't know where the extra complexity comes from.

A lot of this complexity comes from assuming a back to back STREAMOFF -> STREAMON sequence is possible where the gadget driver doesn't have the time to clean up all in-flight usb_requests. However, looking through the usb gadget APIs again, and it looks like usb_ep_disable enforces that all requests will be sent back to the gadget driver before it returns.

So you're right:
With Patch 1/3 in place, I think we can just guard on uvc->state alone, because control requests are blocked until usb_ep_disable is finished anyway. I'll upload v4 with the "is_abandoned" flag removed and the checks simplified once I've verified the fix locally.

That should also remove any bookkeeping issues that may have triggered the stack below.

Regards,
Avi.

Quoted 43 lines
Quoted 7 lines

The other "complications" are around making sure we can trust the values in an inherently racey situation. The reasoning can admittedly be difficult to follow at a glance, which incidentally is why I went with a simple to prove timed wait in the past
(https://lore.kernel.org/[email protected]).

Quoted 6 lines

I am not suggesting we go back to a timed wait, but please do look at the patch and let me know which parts don't make sense, or are difficult to understand. We can add more documentation about our assumptions there, or if you have a way to do this that you think is simpler to reason about, then please let me know and I'll be more than happy to use that!

I really try to spin my head around the idea of the is_abondoned flag you are using. Unfortunatly for now I am out to debug the issues I see with your series.

So I did try these patches you send. Yes the deadlock error is gone with v3. But the linked list is still running into cases where dwc3_gadget_giveback(complete) is touching requests that are already freed.

[   61.408715] ------------[ cut here ]------------ [   61.413897] kernel BUG at lib/list_debug.c:56!
...
[   61.590762] Call trace:
[   61.596890]  __list_del_entry_valid+0xb8/0xe8 [   61.603408]  dwc3_gadget_giveback+0x3c/0x1b0 [   61.607594]  dwc3_remove_requests.part.0+0xcc/0x100 [   61.612948]  __dwc3_gadget_ep_disable+0xbc/0x1b8 [   61.621019]  dwc3_gadget_ep_disable+0x48/0x100 [   61.627925]  usb_ep_disable+0x3c/0x138
[   61.638230]  uvc_function_setup_continue+0x3c/0x60 [   61.645040]  uvc_v4l2_streamoff+0x5c/0x80 [   61.659812]  v4l_streamoff+0x40/0x60
[   61.668950]  __video_do_ioctl+0x344/0x420 [   61.679548]  video_usercopy+0x1d0/0x788
[   61.685677]  video_ioctl2+0x40/0x70
[   61.697439]  v4l2_ioctl+0x68/0xa0
[   61.709200]  __arm64_sys_ioctl+0x304/0xda0 [   61.720768]  invoke_syscall.constprop.0+0x70/0x130

#16 lore raw
Quoted 83 lines · Avichal Rakesh
Quoted 54 lines · Michael Grzeschik

Hi Avichal,

Quoted 51 lines · Avichal Rakesh
Quoted 50 lines · Michael Grzeschik
Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here.

Yes, it was just to summ up what the latest state of the idea was, so Laurent does not read the whole thread in detail. Sorry for not being clear enough about that.

Whoops! Sorry about the misunderstanding!

Quoted 16 lines
Quoted 8 lines

There are two cleanup paths:
 1. uvcg_video_disable cleans up only the requests in req_free, and
 2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

What I don't get is, why in the case of shutdown there needs to be something re-queued back to the gadget driver. There should not need to be any sort of barrier flag for the requests. Just the complete handler running past a barrier where it knows that the whole device is stopped. So every call on complete should then clean that exact request it is touching currently.

Quoted 8 lines
Quoted 1 line

I don't know where the extra complexity comes from.

A lot of this complexity comes from assuming a back to back STREAMOFF -> STREAMON sequence is possible where the gadget driver doesn't have the time to clean up all in-flight usb_requests. However, looking through the usb gadget APIs again, and it looks like usb_ep_disable enforces that all requests will be sent back to the gadget driver before it returns.

Great!

Quoted 9 lines

So you're right:
With Patch 1/3 in place, I think we can just guard on uvc->state alone, because control requests are blocked until usb_ep_disable is finished anyway. I'll upload v4 with the "is_abandoned" flag removed and the checks simplified once I've verified the fix locally.

That should also remove any bookkeeping issues that may have triggered the stack below.

I am currious if we should handle -ECONNRESET and -ESHUTDOWN in more detail in the completion handler and make sure that the request will not be added into the req_free list from there.

Will review your v4 then.

Quoted 44 lines
Quoted 42 lines
Quoted 6 lines

The other "complications" are around making sure we can trust the values in an inherently racey situation. The reasoning can admittedly be difficult to follow at a glance, which incidentally is why I went with a simple to prove timed wait in the past
(https://lore.kernel.org/[email protected]).

Quoted 6 lines

I am not suggesting we go back to a timed wait, but please do look at the patch and let me know which parts don't make sense, or are difficult to understand. We can add more documentation about our assumptions there, or if you have a way to do this that you think is simpler to reason about, then please let me know and I'll be more than happy to use that!

I really try to spin my head around the idea of the is_abondoned flag you are using. Unfortunatly for now I am out to debug the issues I see with your series.

So I did try these patches you send. Yes the deadlock error is gone with v3. But the linked list is still running into cases where dwc3_gadget_giveback(complete) is touching requests that are already freed.

[   61.408715] ------------[ cut here ]------------ [   61.413897] kernel BUG at lib/list_debug.c:56!
...
[   61.590762] Call trace:
[   61.596890]  __list_del_entry_valid+0xb8/0xe8 [   61.603408]  dwc3_gadget_giveback+0x3c/0x1b0 [   61.607594]  dwc3_remove_requests.part.0+0xcc/0x100 [   61.612948]  __dwc3_gadget_ep_disable+0xbc/0x1b8 [   61.621019]  dwc3_gadget_ep_disable+0x48/0x100 [   61.627925]  usb_ep_disable+0x3c/0x138
[   61.638230]  uvc_function_setup_continue+0x3c/0x60 [   61.645040]  uvc_v4l2_streamoff+0x5c/0x80 [   61.659812]  v4l_streamoff+0x40/0x60
[   61.668950]  __video_do_ioctl+0x344/0x420 [   61.679548]  video_usercopy+0x1d0/0x788
[   61.685677]  video_ioctl2+0x40/0x70
[   61.697439]  v4l2_ioctl+0x68/0xa0
[   61.709200]  __arm64_sys_ioctl+0x304/0xda0 [   61.720768]  invoke_syscall.constprop.0+0x70/0x130

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#17 lore raw
Quoted 86 lines · Michael Grzeschik
Quoted 85 lines · Avichal Rakesh
Quoted 54 lines · Michael Grzeschik

Hi Avichal,

Quoted 51 lines · Avichal Rakesh
Quoted 50 lines · Michael Grzeschik
Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here.

Yes, it was just to summ up what the latest state of the idea was, so Laurent does not read the whole thread in detail. Sorry for not being clear enough about that.

Whoops! Sorry about the misunderstanding!

Quoted 17 lines
Quoted 8 lines

There are two cleanup paths:
 1. uvcg_video_disable cleans up only the requests in req_free, and
 2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

What I don't get is, why in the case of shutdown there needs to be something re-queued back to the gadget driver. There should not need to be any sort of barrier flag for the requests. Just the complete handler running past a barrier where it knows that the whole device is stopped. So every call on complete should then clean that exact request it is touching currently.

I don't know where the extra complexity comes from.

Quoted 8 lines
Quoted 6 lines

A lot of this complexity comes from assuming a back to back STREAMOFF -> STREAMON sequence is possible where the gadget driver doesn't have the time to clean up all in-flight usb_requests. However, looking through the usb gadget APIs again, and it looks like  usb_ep_disable enforces that all requests will be sent back to the gadget driver before it returns.

Great!

Uhh...apologies, I will have to take this back. I've been trying to use uvc->state as the condition for when completion handler should clean up usb_requests, and I cannot figure out a way to do so cleanly.

The fundamental problem with using uvc->state is that it is not protected by any locks. So there is no real way to assert that its value has not changed between reading uvc->state and acting on it.

Naively we can write something like the following in the completion handler:

void uvc_video_complete(...) {
    if (uvc->state != UVC_EVENT_STREAMING) {
        usb_ep_free_request(....);
    } else { 
        // handle usb_request normally
    }
}

But without any locks, there are no guarantees that uvc->state didn't mutate immediately after the if condition was checked, and the complete handler is handling a request that it should've freed instead or vice-versa. This argument would hold for any logic we guard with uvc->state, making uvc->state effectively useless as a check for freeing memory.

We can work around it by either
1. Locking uvc->state with some driver level lock to ensure that we can trust the value of uvc->state at least for a little while, or
2. Using some other barrier condition that is protected by another lock

If we go with (1), we'd have to add a lock around every and every write to uvc->state, which isn't terrible, but would require more testing to ensure that it doesn't create any new deadlocks.

For (2), with the realization that usb_ep_disable flushes all requests, we can add a barrier in uvc_video, protected by req_lock. That should simplify the logic a little bit and will hopefully be easier to reason about.

I could of course be missing a simpler solution here, and am happy to be wrong. So please let me know if you have any other ideas on how to guarantee such a check.

Quoted 8 lines
Quoted 8 lines

So you're right:
With Patch 1/3 in place, I think we can just guard on uvc->state alone, because control requests are blocked until usb_ep_disable is finished anyway. I'll upload v4 with the "is_abandoned" flag removed and the checks simplified once I've verified the fix locally.

Quoted 8 lines
Quoted 2 lines

That should also remove any bookkeeping issues that may have triggered the stack below.

I am currious if we should handle -ECONNRESET and -ESHUTDOWN in more detail in the completion handler and make sure that the request will not be added into the req_free list from there.

Will review your v4 then.

Appreciate the reviews, thank you!

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 7 lines

The other "complications" are around making sure we can trust the values in an inherently racey situation. The reasoning can admittedly be difficult to follow at a glance, which incidentally is why I went with a simple to prove timed wait in the past
(https://lore.kernel.org/[email protected]).

Quoted 6 lines

I am not suggesting we go back to a timed wait, but please do look at the patch and let me know which parts don't make sense, or are difficult to understand. We can add more documentation about our assumptions there, or if you have a way to do this that you think is simpler to reason about, then please let me know and I'll be more than happy to use that!

I really try to spin my head around the idea of the is_abondoned flag you are using. Unfortunatly for now I am out to debug the issues I see with your series.

So I did try these patches you send. Yes the deadlock error is gone with v3. But the linked list is still running into cases where dwc3_gadget_giveback(complete) is touching requests that are already freed.

[   61.408715] ------------[ cut here ]------------ [   61.413897] kernel BUG at lib/list_debug.c:56!
...
[   61.590762] Call trace:
[   61.596890]  __list_del_entry_valid+0xb8/0xe8 [   61.603408]  dwc3_gadget_giveback+0x3c/0x1b0 [   61.607594]  dwc3_remove_requests.part.0+0xcc/0x100 [   61.612948]  __dwc3_gadget_ep_disable+0xbc/0x1b8 [   61.621019]  dwc3_gadget_ep_disable+0x48/0x100 [   61.627925]  usb_ep_disable+0x3c/0x138
[   61.638230]  uvc_function_setup_continue+0x3c/0x60

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

[   61.645040]  uvc_v4l2_streamoff+0x5c/0x80 [   61.659812]  v4l_streamoff+0x40/0x60
[   61.668950]  __video_do_ioctl+0x344/0x420 [   61.679548]  video_usercopy+0x1d0/0x788
[   61.685677]  video_ioctl2+0x40/0x70
[   61.697439]  v4l2_ioctl+0x68/0xa0
[   61.709200]  __arm64_sys_ioctl+0x304/0xda0 [   61.720768]  invoke_syscall.constprop.0+0x70/0x130

Just to confirm: this stack was with all 3 patches applied? Want to make sure this won't happen with v4.

Regards,
Avi.

#18 lore raw
Quoted 117 lines · Avichal Rakesh
Quoted 86 lines · Michael Grzeschik
Quoted 85 lines · Avichal Rakesh
Quoted 54 lines · Michael Grzeschik

Hi Avichal,

Quoted 51 lines · Avichal Rakesh
Quoted 50 lines · Michael Grzeschik
Quoted 22 lines · Laurent Pinchart
Quoted 21 lines · Michael Grzeschik
Quoted 19 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
  1. Attempting to queue usb_requests to a disabled usb_ep
  2. use-after-free issue for inflight usb_requests

The three patches below fix the two issues above. Patch 1/3 fixes the first issue, and Patch 2/3 and 3/3 fix the second issue.

Avichal Rakesh (3):
   usb: gadget: uvc: prevent use of disabled endpoint
   usb: gadget: uvc: Allocate uvc_requests one at a time
   usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/f_uvc.c     |  11 +- drivers/usb/gadget/function/f_uvc.h     |   2 +- drivers/usb/gadget/function/uvc.h       |   6 +- drivers/usb/gadget/function/uvc_v4l2.c  |  21 ++- drivers/usb/gadget/function/uvc_video.c | 189 +++++++++++++++++-------

5 files changed, 164 insertions(+), 65 deletions(-)
Quoted 5 lines
Quoted 2 lines

These patches are not applying on gregkh/usb-testing since Greg did take my patches first. I have already rebased them.

I think they got merged too soon :-( We could fix things on top, but there's very little time to do so for v6.7.

Agreed. I was jumping from one workaround to another one, since this is not easy to fix in a proper way. And still after this long discussion with Avichal I don't think we are there yet.

So far the first two patches from Avichal look legit. But the overall Use-After-Free fix is yet to be done properly.

The "abondoned" method he suggested is really bad to follow and will add too much complexity and will be hard to debug.

IMHO it should be possible to introduce two cleanup pathes.

One path would be in the uvc_cleanup_requests that will cleanup the requests that are actually not used in the controller and are registered in the req_free list.

The second path would be the complete functions that are being run from the controller and will ensure that the cleanup will really free the requests from the controller after they were consumed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What do you think?

I am not sure I follow. Patch 3/3 does exactly what you say here.

Yes, it was just to summ up what the latest state of the idea was, so Laurent does not read the whole thread in detail. Sorry for not being clear enough about that.

Whoops! Sorry about the misunderstanding!

Quoted 17 lines
Quoted 8 lines

There are two cleanup paths:
 1. uvcg_video_disable cleans up only the requests in req_free, and
 2. complete handler cleans up the in-flight requests.

The "abandoned" flag is simply to let the completion handler know which requests to clean up and which ones to re-queue back to the gadget driver.

What I don't get is, why in the case of shutdown there needs to be something re-queued back to the gadget driver. There should not need to be any sort of barrier flag for the requests. Just the complete handler running past a barrier where it knows that the whole device is stopped. So every call on complete should then clean that exact request it is touching currently.

I don't know where the extra complexity comes from.

Quoted 8 lines
Quoted 6 lines

A lot of this complexity comes from assuming a back to back STREAMOFF -> STREAMON sequence is possible where the gadget driver doesn't have the time to clean up all in-flight usb_requests. However, looking through the usb gadget APIs again, and it looks like  usb_ep_disable enforces that all requests will be sent back to the gadget driver before it returns.

Great!

Uhh...apologies, I will have to take this back. I've been trying to use uvc->state as the condition for when completion handler should clean up usb_requests, and I cannot figure out a way to do so cleanly.

The fundamental problem with using uvc->state is that it is not protected by any locks. So there is no real way to assert that its value has not changed between reading uvc->state and acting on it.

Naively we can write something like the following in the completion handler:

void uvc_video_complete(...) {
   if (uvc->state != UVC_EVENT_STREAMING) {
       usb_ep_free_request(....);
   } else {
       // handle usb_request normally
   }
}
Quoted 7 lines

But without any locks, there are no guarantees that uvc->state didn't mutate immediately after the if condition was checked, and the complete handler is handling a request that it should've freed instead or vice-versa. This argument would hold for any logic we guard with uvc->state, making uvc->state effectively useless as a check for freeing memory.

Yes, this makes total sense. Since the above condition was also part of the wait_event patch you created in the first place, I bet this issue was there aswell and was probably causing the issues I saw while testing it.

Quoted 12 lines

We can work around it by either
1. Locking uvc->state with some driver level lock to ensure that we can trust the value of uvc->state at least for a little while, or
2. Using some other barrier condition that is protected by another lock

If we go with (1), we'd have to add a lock around every and every write to uvc->state, which isn't terrible, but would require more testing to ensure that it doesn't create any new deadlocks.

Quoted 8 lines

For (2), with the realization that usb_ep_disable flushes all requests, we can add a barrier in uvc_video, protected by req_lock. That should simplify the logic a little bit and will hopefully be easier to reason about.

I could of course be missing a simpler solution here, and am happy to be wrong. So please let me know if you have any other ideas on how to guarantee such a check.

For now, I have no better Idea. Idea (2) sounds like a good compromise. But I will have to review that code to really judge.

Thanks for the work!

Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#19 lore raw
Quoted 73 lines · Michael Grzeschik
Quoted 42 lines · Avichal Rakesh
Quoted 11 lines · Michael Grzeschik
Quoted 10 lines · Avichal Rakesh
Quoted 6 lines · Michael Grzeschik

Hi Avichal,

Quoted 1 line · Avichal Rakesh

On 10/5/23 03:14, Michael Grzeschik wrote:

<snip>
I don't know where the extra complexity comes from.

Quoted 8 lines
Quoted 6 lines

A lot of this complexity comes from assuming a back to back STREAMOFF -> STREAMON sequence is possible where the gadget driver doesn't have the time to clean up all in-flight usb_requests. However, looking through the usb gadget APIs again, and it looks like  usb_ep_disable enforces that all requests will be sent back to the gadget driver before it returns.

Great!

Uhh...apologies, I will have to take this back. I've been trying to use uvc->state as the condition for when completion handler should clean up usb_requests, and I cannot figure out a way to do so cleanly.

The fundamental problem with using uvc->state is that it is not protected by any locks. So there is no real way to assert that its value has not changed between reading uvc->state and acting on it.

Naively we can write something like the following in the completion handler:

void uvc_video_complete(...) {
   if (uvc->state != UVC_EVENT_STREAMING) {
       usb_ep_free_request(....);
   } else {
       // handle usb_request normally
   }
}

Quoted 7 lines

But without any locks, there are no guarantees that uvc->state didn't mutate immediately after the if condition was checked, and the complete handler is handling a request that it should've freed instead or vice-versa. This argument would hold for any logic we guard with uvc->state, making uvc->state effectively useless as a check for freeing memory.

Yes, this makes total sense. Since the above condition was also part of the wait_event patch you created in the first place, I bet this issue was there aswell and was probably causing the issues I saw while testing it>

Quoted 17 lines

We can work around it by either
1. Locking uvc->state with some driver level lock to ensure that we can trust the value of uvc->state at least for a little while, or
2. Using some other barrier condition that is protected by another lock

If we go with (1), we'd have to add a lock around every and every write to uvc->state, which isn't terrible, but would require more testing to ensure that it doesn't create any new deadlocks.

For (2), with the realization that usb_ep_disable flushes all requests, we can add a barrier in uvc_video, protected by req_lock. That should simplify the logic a little bit and will hopefully be easier to reason about.

Quoted 8 lines
Quoted 3 lines

I could of course be missing a simpler solution here, and am happy to be wrong. So please let me know if you have any other ideas on how to guarantee such a check.

For now, I have no better Idea. Idea (2) sounds like a good compromise. But I will have to review that code to really judge.

Sent out v4 patches with option (2). It simplifies the logic decently because we no longer have to reason about per-request consistency. uvc_video now tracks its own state of whether requests should be flowing or not based on calls to uvcg_video_enable. This state is protected, and is the source of truth for queueing usb_requests.

The last bit of complexity left is around returning in-flight video buffers. AFAICT it should now be protected, and in my local testing I didn't notice any un-returned buffers, but please to take a look and let me know if your testing uncovers anything.

Thanks,
Avi.

#20 lore raw

PATCH v3 1/3 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up

 drivers/usb/gadget/function/f_uvc.c    | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h    |  2 +-
 drivers/usb/gadget/function/uvc.h      |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c | 21 ++++++++++++++++++---
 4 files changed, 25 insertions(+), 11 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..75c9f9a3f884 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep) {
+		usb_ep_disable(uvc->video.ep);
+	}
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +18 -3
index 3f0a9795c0d4..3d3469883ed0 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,19 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0) {
+		return ret;
+	}
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +508,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	if (uvc->state == UVC_STATE_STREAMING) {
+		/*
+		 * Drop uvc->state to CONNECTED if it was streaming before.
+		 * This ensures that the usb_requests are no longer queued
+		 * to the controller.
+		 */
+		uvc->state = UVC_STATE_CONNECTED;
+	}
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +663,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-

--
2.42.0.609.gbb76f46606-goog

#21 lore raw

PATCH v3 2/3 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
V2 -> V3: Fix email threading goof-up

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 90 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 42 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -41

139 lines not on this page · raw message

--
2.42.0.609.gbb76f46606-goog

#22 lore raw

Re: [PATCH v3 2/3] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 13 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 2 lines

Please use my full address here. [email protected]

Quoted 84 lines

---
v1 -> v2: Rebased to ToT
V2 -> V3: Fix email threading goof-up

drivers/usb/gadget/function/uvc.h | 3 +-
drivers/usb/gadget/function/uvc_video.c | 90 ++++++++++++++-----------

2 files changed, 51 insertions(+), 42 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +17 -0
index 97d875c27dcf..3c4d286d81c0 100644
@@ -227,6 +227,23 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
 * Request handling
 */
 
+static void uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+	sg_free_table(&ureq->sgt);
+	if (ureq->req && ep) {
+		usb_ep_free_request(ep, ureq->req);
+		ureq->req = NULL;
+	}
+
+	kfree(ureq->req_buffer);
+	ureq->req_buffer = NULL;
+
+	if (!list_empty(&ureq->list))
+		list_del_init(&ureq->list);
+
+	kfree(ureq);
+}
+
 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
 {
 	int ret;
@@ -299,27 +316,13 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)

static int
uvc_video_free_requests(struct uvc_video *video)
{

drivers/usb/gadget/function/uvc_video.c +3 -18
-	unsigned int i;
-
-	if (video->ureq) {
-		for (i = 0; i < video->uvc_num_requests; ++i) {
-			sg_free_table(&video->ureq[i].sgt);
-
-			if (video->ureq[i].req) {
-				usb_ep_free_request(video->ep, video->ureq[i].req);
-				video->ureq[i].req = NULL;
-			}
+	struct uvc_request *ureq, *temp;
 
-			if (video->ureq[i].req_buffer) {
-				kfree(video->ureq[i].req_buffer);
-				video->ureq[i].req_buffer = NULL;
-			}
-		}
-
-		kfree(video->ureq);
-		video->ureq = NULL;
+	list_for_each_entry_safe(ureq, temp, &video->ureqs, list) {
+		uvc_video_free_request(ureq, video->ep);
Quoted 1 line
	}

Please drop the extra braces.

Quoted 30 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+	INIT_LIST_HEAD(&video->ureqs);
	INIT_LIST_HEAD(&video->req_free);
	video->req_size = 0;
	return 0;
drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -328,6 +331,7 @@ uvc_video_free_requests(struct uvc_video *video)

static int
uvc_video_alloc_requests(struct uvc_video *video)
{

drivers/usb/gadget/function/uvc_video.c +1 -0
+	struct uvc_request *ureq;
	unsigned int req_size;
	unsigned int i;
	int ret = -ENOMEM;
drivers/usb/gadget/function/uvc_video.c +5 -7
@@ -338,29 +342,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
 		 * max_t(unsigned int, video->ep->maxburst, 1)
 		 * (video->ep->mult);
 
-	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-	if (video->ureq == NULL)
-		return -ENOMEM;
-
-	for (i = 0; i < video->uvc_num_requests; ++i) {
-		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-		if (video->ureq[i].req_buffer == NULL)
+	INIT_LIST_HEAD(&video->ureqs);
+	for (i = 0; i < video->uvc_num_requests; i++) {
+		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+		if (ureq == NULL)
 			goto error;
+		INIT_LIST_HEAD(&ureq->list);

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 353

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +5 -3
+		list_add_tail(&ureq->list, &video->ureqs);
+	}
 
-		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
-		if (video->ureq[i].req == NULL)
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+		if (ureq->req_buffer == NULL)
 			goto error;
-

Instead of looping twice. Wouldn't it be possible to fill the just created ureq from the for loop here directly.

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +0 -7
-		video->ureq[i].req->buf = video->ureq[i].req_buffer;
-		video->ureq[i].req->length = 0;
-		video->ureq[i].req->complete = uvc_video_complete;
-		video->ureq[i].req->context = &video->ureq[i];
-		video->ureq[i].video = video;
-		video->ureq[i].last_buf = NULL;
-

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 366

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +9 -1
-		list_add_tail(&video->ureq[i].req->list, &video->req_free);
+		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
+		if (ureq->req == NULL)
+			goto error;
+		ureq->req->buf = ureq->req_buffer;
+		ureq->req->length = 0;
+		ureq->req->complete = uvc_video_complete;
+		ureq->req->context = ureq;
+		ureq->video = video;
+		ureq->last_buf = NULL;

Add an extra spare line just like in the code before.

Quoted 41 lines
drivers/usb/gadget/function/uvc_video.c +8 -6
+		list_add_tail(&ureq->req->list, &video->req_free);
 		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
-		sg_alloc_table(&video->ureq[i].sgt,
+		sg_alloc_table(&ureq->sgt,
 			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
 					    PAGE_SIZE) + 2, GFP_KERNEL);
 	}
@@ -504,7 +510,7 @@ static void uvcg_video_pump(struct work_struct *work)
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
 	struct uvc_device *uvc = video->uvc;
-	unsigned int i;
+	struct uvc_request *ureq;
 	int ret;
 
 	if (video->ep == NULL) {
@@ -519,9 +525,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 		cancel_work_sync(&video->pump);
 		uvcg_queue_cancel(&video->queue, 0);
 
-		for (i = 0; i < video->uvc_num_requests; ++i)
-			if (video->ureq && video->ureq[i].req)
-				usb_ep_dequeue(video->ep, video->ureq[i].req);
+		list_for_each_entry(ureq, &video->ureqs, list) {
+			if (ureq->req)
+				usb_ep_dequeue(video->ep, ureq->req);
+		}
 
 		uvc_video_free_requests(video);
 		uvcg_queue_enable(&video->queue, 0);
@@ -555,6 +562,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 */
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 {
+	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
 	INIT_WORK(&video->pump, uvcg_video_pump);
--

2.42.0.609.gbb76f46606-goog

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#23 lore raw

PATCH v3 3/3 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_request to mark it as 'abandoned'. When disabling the video stream, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by the gadget driver (as present in req_free). Other usb_requests have their corresponding 'is_abandoned' flag tripped, and the usb_requests complete handler takes care of freeing the usb_request and its corresponding uvc_request.

This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_video.c | 118 ++++++++++++++++++++----
 2 files changed, 102 insertions(+), 17 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..e69cfb7cced1 100644
@@ -82,6 +82,7 @@ struct uvc_request {
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
 	struct list_head list;
+	bool is_abandoned;
 };
 
 struct uvc_video {
drivers/usb/gadget/function/uvc_video.c +101 -17

160 lines not on this page · raw message

--
2.42.0.609.gbb76f46606-goog

#24 lore raw

Re: [PATCH v3 1/3] usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh
Quoted 29 lines · Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up

drivers/usb/gadget/function/f_uvc.c    | 11 +++++------
drivers/usb/gadget/function/f_uvc.h    |  2 +-
drivers/usb/gadget/function/uvc.h      |  2 +-
drivers/usb/gadget/function/uvc_v4l2.c | 21 ++++++++++++++++++---
4 files changed, 25 insertions(+), 11 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -0
index faa398109431..75c9f9a3f884 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;

drivers/usb/gadget/function/f_uvc.c uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) · line 272

Quoted 10 lines
drivers/usb/gadget/function/f_uvc.c +4 -1
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep) {
+		usb_ep_disable(uvc->video.ep);
+	}

Could you drop the extra braces and add one spare line here.

Quoted 64 lines
drivers/usb/gadget/function/f_uvc.c +1 -5
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
 * Functions
 */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +2 -1
index 3f0a9795c0d4..3d3469883ed0 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,19 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 475

Quoted 10 lines
drivers/usb/gadget/function/uvc_v4l2.c +5 -1
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0) {
+		return ret;
+	}

Please drop those extra braces.

Quoted 8 lines
drivers/usb/gadget/function/uvc_v4l2.c +3 -0
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 

static int

drivers/usb/gadget/function/uvc_v4l2.c +0 -0
@@ -500,6 +508,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_subscribe_event(struct v4l2_fh *fh, · line 518

Quoted 10 lines
drivers/usb/gadget/function/uvc_v4l2.c +8 -0
 {
 	uvc_function_disconnect(uvc);
+	if (uvc->state == UVC_STATE_STREAMING) {
+		/*
+		 * Drop uvc->state to CONNECTED if it was streaming before.
+		 * This ensures that the usb_requests are no longer queued
+		 * to the controller.
+		 */
+		uvc->state = UVC_STATE_CONNECTED;
+	}

Could you write the comment above the check
and also remove the extra braces.

Quoted 10 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -2
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +663,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
--

2.42.0.609.gbb76f46606-goog

With this you can add my:

Thanks

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#25 lore raw

PATCH v4 1/3 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 21 ++++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 27 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +18 -3
index 3f0a9795c0d4..c0d77564a204 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,19 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0) {
+		return ret;
+	}
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +508,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +663,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.609.gbb76f46606-goog

#26 lore raw

PATCH v4 2/3 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 87 ++++++++++++++-----------
 2 files changed, 50 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +48 -39

139 lines not on this page · raw message

--
2.42.0.609.gbb76f46606-goog

#27 lore raw

Re: [PATCH v4 2/3] usb: gadget: uvc: Allocate uvc_requests one at a time

A short second review.

Quoted 118 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT

drivers/usb/gadget/function/uvc.h | 3 +-
drivers/usb/gadget/function/uvc_video.c | 87 ++++++++++++++-----------

2 files changed, 50 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +18 -0
index c334802ac0a4..b62b3de79153 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
 * Request handling
 */
 
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+	sg_free_table(&ureq->sgt);
+	if (ureq->req && ep) {
+		usb_ep_free_request(ep, ureq->req);
+		ureq->req = NULL;
+	}
+
+	kfree(ureq->req_buffer);
+	ureq->req_buffer = NULL;
+
+	if (!list_empty(&ureq->list))
+		list_del_init(&ureq->list);
+
+	kfree(ureq);
+}
+
 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
 {
 	int ret;
@@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)

static int
uvc_video_free_requests(struct uvc_video *video)
{

drivers/usb/gadget/function/uvc_video.c +4 -19
-	unsigned int i;
+	struct uvc_request *ureq, *temp;
 
-	if (video->ureq) {
-		for (i = 0; i < video->uvc_num_requests; ++i) {
-			sg_free_table(&video->ureq[i].sgt);
-
-			if (video->ureq[i].req) {
-				usb_ep_free_request(video->ep, video->ureq[i].req);
-				video->ureq[i].req = NULL;
-			}
-
-			if (video->ureq[i].req_buffer) {
-				kfree(video->ureq[i].req_buffer);
-				video->ureq[i].req_buffer = NULL;
-			}
-		}
-
-		kfree(video->ureq);
-		video->ureq = NULL;
-	}
+	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
+		uvc_video_free_request(ureq, video->ep);
 
+	INIT_LIST_HEAD(&video->ureqs);
	INIT_LIST_HEAD(&video->req_free);
	video->req_size = 0;
	return 0;
drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video)

static int
uvc_video_alloc_requests(struct uvc_video *video)
{

drivers/usb/gadget/function/uvc_video.c +1 -0
+	struct uvc_request *ureq;
	unsigned int req_size;
	unsigned int i;
	int ret = -ENOMEM;
drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -332,29 +336,32 @@ uvc_video_alloc_requests(struct uvc_video *video)
 		 * max_t(unsigned int, video->ep->maxburst, 1)

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 343

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +5 -3
 		 * (video->ep->mult);
 
-	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-	if (video->ureq == NULL)
-		return -ENOMEM;
+	INIT_LIST_HEAD(&video->ureqs);
+	for (i = 0; i < video->uvc_num_requests; i++) {
+		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+		if (ureq == NULL)
+			goto error;

Please add an empty line here.

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 344

Quoted 1 line
drivers/usb/gadget/function/uvc_video.c +1 -0
+		INIT_LIST_HEAD(&ureq->list);

Please add an empty line here.

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 348

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +3 -3
+		list_add_tail(&ureq->list, &video->ureqs);
 
-	for (i = 0; i < video->uvc_num_requests; ++i) {
-		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-		if (video->ureq[i].req_buffer == NULL)
+		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+		if (ureq->req_buffer == NULL)

You could also use if (!ureq->req_buffer)

Quoted 65 lines
drivers/usb/gadget/function/uvc_video.c +16 -15
 			goto error;
 
-		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
-		if (video->ureq[i].req == NULL)
+		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
+		if (ureq->req == NULL)
 			goto error;
 
-		video->ureq[i].req->buf = video->ureq[i].req_buffer;
-		video->ureq[i].req->length = 0;
-		video->ureq[i].req->complete = uvc_video_complete;
-		video->ureq[i].req->context = &video->ureq[i];
-		video->ureq[i].video = video;
-		video->ureq[i].last_buf = NULL;
+		ureq->req->buf = ureq->req_buffer;
+		ureq->req->length = 0;
+		ureq->req->complete = uvc_video_complete;
+		ureq->req->context = ureq;
+		ureq->video = video;
+		ureq->last_buf = NULL;
 
-		list_add_tail(&video->ureq[i].req->list, &video->req_free);
+		list_add_tail(&ureq->req->list, &video->req_free);
 		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
-		sg_alloc_table(&video->ureq[i].sgt,
+		sg_alloc_table(&ureq->sgt,
 			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
 					    PAGE_SIZE) + 2, GFP_KERNEL);
 	}
@@ -489,8 +496,8 @@ static void uvcg_video_pump(struct work_struct *work)
 */
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
-	unsigned int i;
 	int ret;
+	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
@@ -502,9 +509,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 		cancel_work_sync(&video->pump);
 		uvcg_queue_cancel(&video->queue, 0);
 
-		for (i = 0; i < video->uvc_num_requests; ++i)
-			if (video->ureq && video->ureq[i].req)
-				usb_ep_dequeue(video->ep, video->ureq[i].req);
+		list_for_each_entry(ureq, &video->ureqs, list) {
+			if (ureq->req)
+				usb_ep_dequeue(video->ep, ureq->req);
+		}
 
 		uvc_video_free_requests(video);
 		uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +544,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 */
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 {
+	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
 	INIT_WORK(&video->pump, uvcg_video_pump);
--

2.42.0.609.gbb76f46606-goog

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#28 lore raw

Re: [PATCH v4 2/3] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 1 line · Michael Grzeschik

A short second review.

Thank you for the review. Sent out a v5 with the comments addressed!

Quoted 24 lines · Avichal Rakesh
Quoted 22 lines

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT

Quoted 7 lines
Quoted 6 lines

drivers/usb/gadget/function/uvc.h       |  3 +- drivers/usb/gadget/function/uvc_video.c | 87 ++++++++++++++-----------

2 files changed, 50 insertions(+), 40 deletions(-)

-        if (video->ureq[i].req_buffer == NULL)
+        ureq->req_buffer = kmalloc(req_size, GFP_KERNEL); +        if (ureq->req_buffer == NULL)

You could also use if (!ureq->req_buffer)

Keeping this as is because I prefer == NULL check for readability. Didn't find any specific rules in the kernel codestyle, so sticking with the more readable option (in my opinion).

Quoted 67 lines
Quoted 66 lines

            goto error;

-        video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL); -        if (video->ureq[i].req == NULL)
+        ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL); +        if (ureq->req == NULL)
            goto error;

-        video->ureq[i].req->buf = video->ureq[i].req_buffer; -        video->ureq[i].req->length = 0;
-        video->ureq[i].req->complete = uvc_video_complete; -        video->ureq[i].req->context = &video->ureq[i]; -        video->ureq[i].video = video;
-        video->ureq[i].last_buf = NULL;
+        ureq->req->buf = ureq->req_buffer;
+        ureq->req->length = 0;
+        ureq->req->complete = uvc_video_complete; +        ureq->req->context = ureq;
+        ureq->video = video;
+        ureq->last_buf = NULL;

-        list_add_tail(&video->ureq[i].req->list, &video->req_free); +        list_add_tail(&ureq->req->list, &video->req_free); /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */ -        sg_alloc_table(&video->ureq[i].sgt,
+        sg_alloc_table(&ureq->sgt,
                   DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN, PAGE_SIZE) + 2, GFP_KERNEL);
    }

drivers/usb/gadget/function/uvc_video.c +6 -4
@@ -489,8 +496,8 @@ static void uvcg_video_pump(struct work_struct *work)
  */
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
-    unsigned int i;
     int ret;
+    struct uvc_request *ureq;
 
     if (video->ep == NULL) {
         uvcg_info(&video->uvc->func,
@@ -502,9 +509,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
         cancel_work_sync(&video->pump);
         uvcg_queue_cancel(&video->queue, 0);
 
-        for (i = 0; i < video->uvc_num_requests; ++i)
-            if (video->ureq && video->ureq[i].req)
-                usb_ep_dequeue(video->ep, video->ureq[i].req);
+        list_for_each_entry(ureq, &video->ureqs, list) {
+            if (ureq->req)
+                usb_ep_dequeue(video->ep, ureq->req);
+        }
 
         uvc_video_free_requests(video);
         uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +544,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  */
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 {
+    INIT_LIST_HEAD(&video->ureqs);
     INIT_LIST_HEAD(&video->req_free);
     spin_lock_init(&video->req_lock);
     INIT_WORK(&video->pump, uvcg_video_pump);
Signature · 1 line
2.42.0.609.gbb76f46606-goog
#29 lore raw

PATCH v4 3/3 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
 drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----
 3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index c0d77564a204..ded7d33c2a52 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,12 +468,12 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0) {
 		return ret;
 	}
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -508,14 +508,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +125 -31

220 lines not on this page · raw message

--
2.42.0.609.gbb76f46606-goog

#30 lore raw

Re: [PATCH v4 3/3] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

drivers/usb/gadget/function/uvc.h

Quoted 37 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
 drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----
 3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc.h +0 -0

Apologies, I realized I forgot to run checkpatch on this patch. Will fix the lint issues in the next version. This patch is functionally okay, but has 2 minor formatting issues. Feel free to review the patch, and I will fix the formatting as I am addressing the comments.

- Avi.

Reviewed-by Tested-by on v4 3/3
#31 lore raw

Re: [PATCH v4 3/3] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh
Quoted 23 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 7 lines

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/

I tested this and I no longer saw any use after free errors anymore! :)

Here comes some more review:

Quoted 138 lines
drivers/usb/gadget/function/uvc.h       |   1 +
drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----
3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index c0d77564a204..ded7d33c2a52 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,12 +468,12 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0) {
 		return ret;
 	}
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -508,14 +508,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +3 -0
index b62b3de79153..05b89b5b6c48 100644
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
 * Request handling
 */
 
+/**
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

static void
uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep) {

drivers/usb/gadget/function/uvc_video.c +33 -8
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	struct uvc_request *ureq = req->context;
 	struct uvc_video *video = ureq->video;
 	struct uvc_video_queue *queue = &video->queue;
-	struct uvc_device *uvc = video->uvc;
+	struct uvc_buffer *last_buf = NULL;
 	unsigned long flags;
 
+	spin_lock_irqsave(&video->req_lock, flags);
+	if (!video->is_enabled) {
+		/*
+		 * When is_enabled is false, uvc_video_disable ensures that
+		 * in-flight uvc_buffers are returned, so we can safely
+		 * call free_request without worrying about last_buf.
+		 */
+		uvc_video_free_request(ureq, ep);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		return;
+	}
+
+	last_buf = ureq->last_buf;
+	ureq->last_buf = NULL;
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
 	switch (req->status) {
 	case 0:
 		break;
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 		uvcg_queue_cancel(queue, 0);
 	}
 
-	if (ureq->last_buf) {
-		uvcg_complete_buffer(&video->queue, ureq->last_buf);
-		ureq->last_buf = NULL;
+	if (last_buf) {
+		spin_lock_irqsave(&queue->irqlock, flags);
+		uvcg_complete_buffer(&video->queue, last_buf);
+		spin_unlock_irqrestore(&queue->irqlock, flags);
 	}
 
 	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
-	spin_unlock_irqrestore(&video->req_lock, flags);
-
-	if (uvc->state == UVC_STATE_STREAMING)
+	/*
+	 * Video stream might have been disabled while we were
+	 * processing the current usb_request. So make sure
+	 * we're still streaming before queueing the usb_request
+	 * back to req_free
+	 */
+	if (video->is_enabled) {
+		list_add_tail(&req->list, &video->req_free);
 		queue_work(video->async_wq, &video->pump);
+	} else {
+		uvc_video_free_request(ureq, ep);
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
 }
 

static int

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -391,20 +419,22 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 428

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +1 -2
 	bool is_bulk = video->max_payload_size;
-	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+	while(true) {

Missing space after "while".

Quoted 114 lines
drivers/usb/gadget/function/uvc_video.c +77 -13
+		if (!video->ep->enabled)
+			return;
+
 		/*
-		 * Retrieve the first available USB request, protected by the
-		 * request lock.
+		 * Check is_enabled and retrieve the first available USB
+		 * request, protected by the request lock.
 		 */
 		spin_lock_irqsave(&video->req_lock, flags);
-		if (list_empty(&video->req_free)) {
+		if (!video->is_enabled || list_empty(&video->req_free)) {
 			spin_unlock_irqrestore(&video->req_lock, flags);
 			return;
 		}
@@ -486,9 +516,78 @@ static void uvcg_video_pump(struct work_struct *work)
 		return;
 
 	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
+	if (video->is_enabled)
+		list_add_tail(&req->list, &video->req_free);
+	else
+		uvc_video_free_request(req->context, video->ep);
+	spin_unlock_irqrestore(&video->req_lock, flags);
+}
+
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video) {
+	unsigned long flags;
+	struct list_head inflight_bufs;
+	struct usb_request *req, *temp;
+	struct uvc_buffer *buf, *btemp;
+	struct uvc_request *ureq, *utemp;
+
+	INIT_LIST_HEAD(&inflight_bufs);
+	spin_lock_irqsave(&video->req_lock, flags);
+	video->is_enabled = false;
+
+	/*
+	 * Remove any in-flight buffers from the uvc_requests
+	 * because we want to return them before cancelling the
+	 * queue. This ensures that we aren't stuck waiting for
+	 * all complete callbacks to come through before disabling
+	 * vb2 queue.
+	 */
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->last_buf) {
+			list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+			ureq->last_buf = NULL;
+		}
+	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
-	return;
+
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
+
+	spin_lock_irqsave(&video->req_lock, flags);
+	/*
+	 * Remove all uvc_reqeusts from from ureqs with list_del_init
+	 * This lets uvc_video_free_request correctly identify
+	 * if the uvc_request is attached to a list or not when freeing
+	 * memory.
+	 */
+	list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+		list_del_init(&ureq->list);
+
+	list_for_each_entry_safe(req, temp, &video->req_free, list) {
+		list_del(&req->list);
+		uvc_video_free_request(req->context, video->ep);
+	}
+
+	INIT_LIST_HEAD(&video->ureqs);
+	INIT_LIST_HEAD(&video->req_free);
+	video->req_size = 0;
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
+	/*
+	 * Return all the video buffers before disabling the queue.
+	 */
+	spin_lock_irqsave(&video->queue.irqlock, flags);
+	list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+		list_del(&buf->queue);
+		uvcg_complete_buffer(&video->queue, buf);
+	}
+	spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
 }
 
 /*
@@ -497,28 +596,22 @@ static void uvcg_video_pump(struct work_struct *work)
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
 	int ret;
-	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
 			  "Video enable failed, device is uninitialized.\n");
 		return -ENODEV;
 	}
-
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
-
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 606

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +2 -8
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
-
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
-	}
-
+	if (!enable)
+		return uvcg_video_disable(video);

Could you refactor this code as it is to an separate function and prepand this change as an extra patch to this one? It would make the changes in the functions more obvious and better to review.

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 614

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +8 -0
+
+	/*
+	 * Safe to access request related fields without req_lock because
+	 * this is the only thread currently active, and no other
+	 * request handling thread will become active until this function
+	 * returns.
+	 */
+	video->is_enabled = true;

Add an extra empty line.

Quoted 12 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
 		return ret;
 
@@ -544,6 +637,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 */
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 {
+	video->is_enabled = false;
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
--
Quoted 3 lines

2.42.0.609.gbb76f46606-goog

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#32 lore raw

Re: [PATCH v4 3/3] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 32 lines · Michael Grzeschik
Quoted 23 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 7 lines

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
         https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
         https://lore.kernel.org/[email protected]/

Quoted 2 lines

I tested this and I no longer saw any use after free errors anymore! :)

Yay! Glad to hear!

Quoted 104 lines

Here comes some more review:

Quoted 101 lines

drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +- drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----

3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc_video.c +29 -0
+
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video) {
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;
+
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }

    spin_unlock_irqrestore(&video->req_lock, flags); -    return;

drivers/usb/gadget/function/uvc_video.c +36 -0
+
+    cancel_work_sync(&video->pump);
+    uvcg_queue_cancel(&video->queue, 0);
+
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from from ureqs with list_del_init
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }
+
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
+    uvcg_queue_enable(&video->queue, 0);
+    return 0;

}

/*

drivers/usb/gadget/function/uvc_video.c +0 -15
@@ -497,28 +596,22 @@ static void uvcg_video_pump(struct work_struct *work)
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
     int ret;
-    struct uvc_request *ureq;
 
     if (video->ep == NULL) {
         uvcg_info(&video->uvc->func,
               "Video enable failed, device is uninitialized.\n");
         return -ENODEV;
     }
-
-    if (!enable) {
-        cancel_work_sync(&video->pump);
-        uvcg_queue_cancel(&video->queue, 0);
-
-        list_for_each_entry(ureq, &video->ureqs, list) {
-            if (ureq->req)
-                usb_ep_dequeue(video->ep, ureq->req);
-        }
-
-        uvc_video_free_requests(video);
-        uvcg_queue_enable(&video->queue, 0);
-        return 0;
-    }
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 606

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +2 -1
-
+    if (!enable)
+        return uvcg_video_disable(video);

Could you refactor this code as it is to an separate function and prepand this change as an extra patch to this one? It would make the changes in the functions more obvious and better to review.

Sure I can send a follow up patch, but I am curious why you think this needs to be a separate function? Refactoring into a function would have the functions structured something like:

uvcg_video_disable(video) {
    // ...
    // disable impl
    // ...
}
uvcg_video_enable(video) {
    // ...
    // enable impl
    // ...
}
uvcg_video_enable(video, enable) {
    // ep test
   
    if (!enable)
        return uvcg_video_disable(video);

    return uvc_video_enable(video);
}

instead of the current structure:

uvcg_video_disable(video) {
    // ...
    // disable impl
    // ...
}
uvcg_video_enable(video, enable) {
    // ep test
   
    if (!enable)
        return uvcg_video_disable(video);

    // ...
    // enable impl
    // ...
}

I am not sure if one is more readable than the other.

Quoted 32 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 614

Quoted 9 lines
drivers/usb/gadget/function/uvc_video.c +8 -0
+
+    /*
+     * Safe to access request related fields without req_lock because
+     * this is the only thread currently active, and no other
+     * request handling thread will become active until this function
+     * returns.
+     */
+    video->is_enabled = true;

Add an extra empty line.

Quoted 15 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
     if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
         return ret;
 
@@ -544,6 +637,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  */
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 {
+    video->is_enabled = false;
     INIT_LIST_HEAD(&video->ureqs);
     INIT_LIST_HEAD(&video->req_free);
     spin_lock_init(&video->req_lock);
Signature · 1 line
2.42.0.609.gbb76f46606-goog
#33 lore raw

Re: [PATCH v4 3/3] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 197 lines · Avichal Rakesh
Quoted 32 lines · Michael Grzeschik
Quoted 23 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 7 lines

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
         https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
         https://lore.kernel.org/[email protected]/

Quoted 2 lines

I tested this and I no longer saw any use after free errors anymore! :)

Yay! Glad to hear!

Quoted 104 lines

Here comes some more review:

Quoted 101 lines

drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +- drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----

3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc_video.c +29 -0
+
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video) {
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;
+
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }

    spin_unlock_irqrestore(&video->req_lock, flags); -    return;

drivers/usb/gadget/function/uvc_video.c +36 -0
+
+    cancel_work_sync(&video->pump);
+    uvcg_queue_cancel(&video->queue, 0);
+
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from from ureqs with list_del_init
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }
+
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
+    uvcg_queue_enable(&video->queue, 0);
+    return 0;

}

/*

drivers/usb/gadget/function/uvc_video.c +0 -15
@@ -497,28 +596,22 @@ static void uvcg_video_pump(struct work_struct *work)
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
     int ret;
-    struct uvc_request *ureq;
 
     if (video->ep == NULL) {
         uvcg_info(&video->uvc->func,
               "Video enable failed, device is uninitialized.\n");
         return -ENODEV;
     }
-
-    if (!enable) {
-        cancel_work_sync(&video->pump);
-        uvcg_queue_cancel(&video->queue, 0);
-
-        list_for_each_entry(ureq, &video->ureqs, list) {
-            if (ureq->req)
-                usb_ep_dequeue(video->ep, ureq->req);
-        }
-
-        uvc_video_free_requests(video);
-        uvcg_queue_enable(&video->queue, 0);
-        return 0;
-    }
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 606

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +2 -1
-
+    if (!enable)
+        return uvcg_video_disable(video);

Could you refactor this code as it is to an separate function and prepand this change as an extra patch to this one? It would make the changes in the functions more obvious and better to review.

Sure I can send a follow up patch, but I am curious why you think this needs to be a separate function? Refactoring into a function would have the functions structured something like:

uvcg_video_disable(video) {
   // ...
   // disable impl
   // ...
}
uvcg_video_enable(video) {
   // ...
   // enable impl
   // ...
}
uvcg_video_enable(video, enable) {
   // ep test

   if (!enable)
       return uvcg_video_disable(video);

   return uvc_video_enable(video);
}

instead of the current structure:

uvcg_video_disable(video) {
   // ...
   // disable impl
   // ...
}
uvcg_video_enable(video, enable) {
   // ep test

   if (!enable)
       return uvcg_video_disable(video);

   // ...
   // enable impl
   // ...
}
Quoted 1 line

I am not sure if one is more readable than the other.

I think you misunderstood. The second structure is all right.

What I did want you to do is as follows.

Lets look at your series:

patch 0/3
patch 1/3
patch 2/3

<--- add a patch here that does the refactoring of the separate
function uvcg_video_disable without changing the functional content of it:

uvcg_video_disable(video) {
    // ...
    // disable impl
    // ...
}
uvcg_video_enable(video, enable) {
    // ep test

    if (!enable)
        return uvcg_video_disable(video);

    // ...
    // enable impl
    // ...
}

patch 3/3

This way in the patch 3/3 the functional changes you introduce to the uvcg_video_diable will get better to review.

Regards,
Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#34 lore raw

Re: [PATCH v4 3/3] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 231 lines · Michael Grzeschik
Quoted 192 lines · Avichal Rakesh
Quoted 32 lines · Michael Grzeschik
Quoted 23 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accomodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 7 lines

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
         https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
         https://lore.kernel.org/[email protected]/

Quoted 2 lines

I tested this and I no longer saw any use after free errors anymore! :)

Yay! Glad to hear!

Quoted 104 lines

Here comes some more review:

Quoted 101 lines

drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +- drivers/usb/gadget/function/uvc_video.c | 156 +++++++++++++++++++-----

3 files changed, 128 insertions(+), 41 deletions(-)
drivers/usb/gadget/function/uvc_video.c +29 -0
+
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video) {
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;
+
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }

    spin_unlock_irqrestore(&video->req_lock, flags); -    return;

drivers/usb/gadget/function/uvc_video.c +36 -0
+
+    cancel_work_sync(&video->pump);
+    uvcg_queue_cancel(&video->queue, 0);
+
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from from ureqs with list_del_init
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }
+
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
+    uvcg_queue_enable(&video->queue, 0);
+    return 0;

}

/*

drivers/usb/gadget/function/uvc_video.c +0 -15
@@ -497,28 +596,22 @@ static void uvcg_video_pump(struct work_struct *work)
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
     int ret;
-    struct uvc_request *ureq;
 
     if (video->ep == NULL) {
         uvcg_info(&video->uvc->func,
               "Video enable failed, device is uninitialized.\n");
         return -ENODEV;
     }
-
-    if (!enable) {
-        cancel_work_sync(&video->pump);
-        uvcg_queue_cancel(&video->queue, 0);
-
-        list_for_each_entry(ureq, &video->ureqs, list) {
-            if (ureq->req)
-                usb_ep_dequeue(video->ep, ureq->req);
-        }
-
-        uvc_video_free_requests(video);
-        uvcg_queue_enable(&video->queue, 0);
-        return 0;
-    }
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 606

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +2 -1
-
+    if (!enable)
+        return uvcg_video_disable(video);

Could you refactor this code as it is to an separate function and prepand this change as an extra patch to this one? It would make the changes in the functions more obvious and better to review.

Sure I can send a follow up patch, but I am curious why you think this needs to be a separate function? Refactoring into a function would have the functions structured something like:

uvcg_video_disable(video) {
   // ...
   // disable impl
   // ...
}

uvcg_video_enable(video) {
   // ...
   // enable impl
   // ...
}

uvcg_video_enable(video, enable) {
   // ep test

   if (!enable)
       return uvcg_video_disable(video);

   return uvc_video_enable(video);
}

instead of the current structure:

uvcg_video_disable(video) {
   // ...
   // disable impl
   // ...
}

uvcg_video_enable(video, enable) {
   // ep test

   if (!enable)
       return uvcg_video_disable(video);

Quoted 6 lines

   // ...
   // enable impl
   // ...
}

I am not sure if one is more readable than the other.

I think you misunderstood. The second structure is all right.

What I did want you to do is as follows.

Lets look at your series:

patch 0/3
patch 1/3
patch 2/3

<--- add a patch here that does the refactoring of the separate function uvcg_video_disable without changing the functional content of it:

uvcg_video_disable(video) {
    // ...
    // disable impl
    // ...
}

uvcg_video_enable(video, enable) {
    // ep test

    if (!enable)
        return uvcg_video_disable(video);

    // ...
    // enable impl
    // ...
}

Quoted 4 lines

patch 3/3

This way in the patch 3/3 the functional changes you introduce to the uvcg_video_diable will get better to review.

I see! I did indeed misunderstand. Sent out v6 with 4 patches!

Thank you!
- Avi.

#35 lore raw

PATCH v5 1/3 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.758.gaed0368e0e-goog

#36 lore raw

PATCH v5 2/3 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by.

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -39

141 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#37 lore raw

PATCH v5 3/3 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by.

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
 drivers/usb/gadget/function/uvc_video.c | 154 +++++++++++++++++++-----
 3 files changed, 128 insertions(+), 39 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index 7cb8d027ff0c..f4d2e24835d4 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +125 -29

219 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#38 lore raw

v6 cover usb: gadget: uvc: stability fixes on STREAMOFF.

In reply to Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The four patches below fix the two issues above. Patch 1/4 fixes the first issue, and Patch 2/4 and 4/4 fix the second issue. Patch 3/4 is only there to make the diff in 4/4 cleaner.

Avichal Rakesh (4):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: move video disable logic to its own function
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

 drivers/usb/gadget/function/f_uvc.c     |  11 +-
 drivers/usb/gadget/function/f_uvc.h     |   2 +-
 drivers/usb/gadget/function/uvc.h       |   6 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
 drivers/usb/gadget/function/uvc_video.c | 231 +++++++++++++++++-------
 5 files changed, 189 insertions(+), 73 deletions(-)

--
2.42.0.758.gaed0368e0e-goog

#39 lore raw

PATCH v6 1/4 usb: gadget: uvc: prevent use of disabled endpoint

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.758.gaed0368e0e-goog

#40 lore raw

PATCH v6 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -39

141 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#41 lore raw

PATCH v6 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'.

---
v6: Introduced this patch to make the next one easier to review

 drivers/usb/gadget/function/uvc_video.c | 37 +++++++++++++++----------
 1 file changed, 23 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/uvc_video.c +23 -14
index c180866c8e34..80b8eaea2d39 100644
@@ -493,13 +493,33 @@ static void uvcg_video_pump(struct work_struct *work)
 	return;
 }
 
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video)
+{
+	struct uvc_request *ureq;
+
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
+
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
+
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
 /*
  * Enable or disable the video stream.
  */
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
 	int ret;
-	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
@@ -507,19 +527,8 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
-
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
-
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
-	}
+	if (!enable)
+		return uvcg_video_disable(video);
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
 		return ret;

--
2.42.0.758.gaed0368e0e-goog

#42 lore raw

PATCH v6 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
easier to review.

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
 drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
 3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index 7cb8d027ff0c..f4d2e24835d4 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +108 -20

198 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#43 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

kernel test robot noticed the following build warnings:

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

vim +231 drivers/usb/gadget/function/uvc_video.c

225
226 /* --------------------------------------------------------------------------
227 * Request handling
228 */
229
230 /**

 > 231	 * Must be called with req_lock held as it modifies the list ureq is held in

232 */
233 static void
234 uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
235 {
236 sg_free_table(&ureq->sgt);
237 if (ureq->req && ep) {
238 usb_ep_free_request(ep, ureq->req);
239 ureq->req = NULL;
240 }
241
242 kfree(ureq->req_buffer);
243 ureq->req_buffer = NULL;
244
245 if (!list_empty(&ureq->list))
246 list_del_init(&ureq->list);
247
248 kfree(ureq);
249 }
250

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#44 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 23 lines · kernel test robot

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 6 lines

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

Greg, apologies for the newb question: do you want me to upload the fix for this as a reply to [PATCH v6 4/4], or upload a new chain of v7s with this patch fixed?

I am not familiar with the kernel merging process, so not sure which one would work better for you to pick up.

Regards,
Avi

maintainer
#45 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 28 lines · Avichal Rakesh
Quoted 25 lines · kernel test robot

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

Greg, apologies for the newb question: do you want me to upload the fix for this as a reply to [PATCH v6 4/4], or upload a new chain of v7s with this patch fixed?

A whole new v7 series please.

thanks,

greg k-h

#46 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 35 lines · Greg KH
Quoted 33 lines · Avichal Rakesh
Quoted 29 lines · kernel test robot

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

Quoted 6 lines
Quoted 3 lines

Greg, apologies for the newb question: do you want me to upload the fix for this as a reply to [PATCH v6 4/4], or upload a new chain of v7s with this patch fixed?

A whole new v7 series please.

Had a feeling, so sent out v7 series preemptively. Let me know if that doesn't work.

v7: https://lore.kernel.org/[email protected]/

Thank you!
- Avi

maintainer
#47 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 40 lines · Avichal Rakesh
Quoted 39 lines · Greg KH
Quoted 36 lines · Avichal Rakesh
Quoted 23 lines · kernel test robot

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 6 lines

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

Greg, apologies for the newb question: do you want me to upload the fix for this as a reply to [PATCH v6 4/4], or upload a new chain of v7s with this patch fixed?

Quoted 7 lines
Quoted 2 lines

A whole new v7 series please.

Had a feeling, so sent out v7 series preemptively. Let me know if that doesn't work.

v7: https://lore.kernel.org/[email protected]/

I have already dropped that from my review queue as your emails crossed with that, so I thought it was obsolete by now, sorry.

Can you send a v8 please?

thanks,

greg k-h

#48 lore raw

Re: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 48 lines · Greg KH
Quoted 46 lines · Avichal Rakesh
Quoted 35 lines · Greg KH
Quoted 33 lines · Avichal Rakesh
Quoted 29 lines · kernel test robot

Hi Avichal,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.6-rc6 next-20231019] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Avichal-Rakesh/usb-gadget-uvc-prevent-use-of-disabled-endpoint/20231020-025512
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231019185319.2714000-5-arakesh%40google.com
patch subject: [PATCH v6 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231020/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/usb/gadget/function/uvc_video.c:231: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst

* Must be called with req_lock held as it modifies the list ureq is held in

Quoted 6 lines
Quoted 3 lines

Greg, apologies for the newb question: do you want me to upload the fix for this as a reply to [PATCH v6 4/4], or upload a new chain of v7s with this patch fixed?

A whole new v7 series please.

Had a feeling, so sent out v7 series preemptively. Let me know if that doesn't work.

Quoted 7 lines
Quoted 1 line

v7: https://lore.kernel.org/[email protected]/

I have already dropped that from my review queue as your emails crossed with that, so I thought it was obsolete by now, sorry.

Can you send a v8 please?

Sent out v8! PTAL when you get the chance.

https://lore.kernel.org/[email protected]/

Thank you!
- Avi.

#49 lore raw
In reply to Avichal Rakesh
Quoted 22 lines · Avichal Rakesh

We have been seeing two main stability issues that uvc gadget driver runs into when stopping streams:
1. Attempting to queue usb_requests to a disabled usb_ep
2. use-after-free issue for inflight usb_requests

The four patches below fix the two issues above. Patch 1/4 fixes the first issue, and Patch 2/4 and 4/4 fix the second issue. Patch 3/4 is only there to make the diff in 4/4 cleaner.

Avichal Rakesh (4):
usb: gadget: uvc: prevent use of disabled endpoint
usb: gadget: uvc: Allocate uvc_requests one at a time
usb: gadget: uvc: move video disable logic to its own function
usb: gadget: uvc: Fix use-after-free for inflight usb_requests

 drivers/usb/gadget/function/f_uvc.c     |  11 +-
 drivers/usb/gadget/function/f_uvc.h     |   2 +-
 drivers/usb/gadget/function/uvc.h       |   6 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-
 drivers/usb/gadget/function/uvc_video.c | 231 +++++++++++++++++-------
 5 files changed, 189 insertions(+), 73 deletions(-)
Quoted 2 lines

--
2.42.0.758.gaed0368e0e-goog

Dan and Laurent, please go over the patches whenever you get a chance. I think they're ready to submit as neither Michael nor I have seen any use-after-free issues after the patches.

Thank you!
- Avi.

#50 lore raw

PATCH v9 1/4 usb: gadget: uvc: prevent use of disabled endpoint

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue v8 -> v9: Fix typo. No functional change.

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..083aef0c65c6 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.820.g83a721a137-goog

#51 lore raw

PATCH v9 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue
v8 -> v9: Address review comments.

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -39

145 lines not on this page · raw message

--
2.42.0.820.g83a721a137-goog

maintainer
#52 lore raw

Re: [PATCH v9 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

drivers/usb/gadget/function/uvc_video.c uvc_video_free_requests(struct uvc_video *video) · line 334

Quoted 2 lines · Avichal Rakesh
drivers/usb/gadget/function/uvc_video.c +1 -0
 	BUG_ON(video->req_size);
+	BUG_ON(!list_empty(&video->ureqs));

Again, please do not add new BUG_ON() lines, the existing ones need to be removed as well, but you can do that in later changes. I can't take changes that add new ones, sorry.

thanks,

greg k-h

#53 lore raw

Re: [PATCH v9 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

In reply to Avichal Rakesh

Hi Avichal

Quoted 13 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 3 lines

Sorry - I was unclear in my response to the first patch on v8. I meant my R-b to apply to the first patch only rather than to all of them. For this one I understand now the use of the conditional in uvc_video_free_request(), so that point is fine. I agree with Greg that the BUG_ON() shouldn't stand though.

Quoted 195 lines

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue
v8 -> v9: Address review comments.

  drivers/usb/gadget/function/uvc.h       |  3 +-
  drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
  2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
  	struct sg_table sgt;
  	u8 header[UVCG_REQUEST_HEADER_LEN];
  	struct uvc_buffer *last_buf;
+	struct list_head list;
  };
 
  struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
  	/* Requests */
  	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
  	struct list_head req_free;
  	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -40
index c334802ac0a4..f8f9209fee50 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
   * Request handling
   */
 
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+	sg_free_table(&ureq->sgt);
+	if (ureq->req && ep) {
+		usb_ep_free_request(ep, ureq->req);
+		ureq->req = NULL;
+	}
+
+	kfree(ureq->req_buffer);
+	ureq->req_buffer = NULL;
+
+	if (!list_empty(&ureq->list))
+		list_del_init(&ureq->list);
+
+	kfree(ureq);
+}
+
  static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  {
  	int ret;
@@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  static int
  uvc_video_free_requests(struct uvc_video *video)
  {
-	unsigned int i;
-
-	if (video->ureq) {
-		for (i = 0; i < video->uvc_num_requests; ++i) {
-			sg_free_table(&video->ureq[i].sgt);
+	struct uvc_request *ureq, *temp;
 
-			if (video->ureq[i].req) {
-				usb_ep_free_request(video->ep, video->ureq[i].req);
-				video->ureq[i].req = NULL;
-			}
-
-			if (video->ureq[i].req_buffer) {
-				kfree(video->ureq[i].req_buffer);
-				video->ureq[i].req_buffer = NULL;
-			}
-		}
-
-		kfree(video->ureq);
-		video->ureq = NULL;
-	}
+	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
+		uvc_video_free_request(ureq, video->ep);
 
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	video->req_size = 0;
  	return 0;
@@ -322,39 +325,45 @@ uvc_video_free_requests(struct uvc_video *video)
  static int
  uvc_video_alloc_requests(struct uvc_video *video)
  {
+	struct uvc_request *ureq;
  	unsigned int req_size;
  	unsigned int i;
  	int ret = -ENOMEM;
 
  	BUG_ON(video->req_size);
+	BUG_ON(!list_empty(&video->ureqs));
 
  	req_size = video->ep->maxpacket
  		 * max_t(unsigned int, video->ep->maxburst, 1)
  		 * (video->ep->mult);
 
-	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-	if (video->ureq == NULL)
-		return -ENOMEM;
+	for (i = 0; i < video->uvc_num_requests; i++) {
+		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+		if (ureq == NULL)
+			goto error;
+
+		INIT_LIST_HEAD(&ureq->list);
+
+		list_add_tail(&ureq->list, &video->ureqs);
 
-	for (i = 0; i < video->uvc_num_requests; ++i) {
-		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-		if (video->ureq[i].req_buffer == NULL)
+		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+		if (ureq->req_buffer == NULL)
  			goto error;
 
-		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
-		if (video->ureq[i].req == NULL)
+		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
+		if (ureq->req == NULL)
  			goto error;
 
-		video->ureq[i].req->buf = video->ureq[i].req_buffer;
-		video->ureq[i].req->length = 0;
-		video->ureq[i].req->complete = uvc_video_complete;
-		video->ureq[i].req->context = &video->ureq[i];
-		video->ureq[i].video = video;
-		video->ureq[i].last_buf = NULL;
+		ureq->req->buf = ureq->req_buffer;
+		ureq->req->length = 0;
+		ureq->req->complete = uvc_video_complete;
+		ureq->req->context = ureq;
+		ureq->video = video;
+		ureq->last_buf = NULL;
 
-		list_add_tail(&video->ureq[i].req->list, &video->req_free);
+		list_add_tail(&ureq->req->list, &video->req_free);
  		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
-		sg_alloc_table(&video->ureq[i].sgt,
+		sg_alloc_table(&ureq->sgt,
  			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
  					    PAGE_SIZE) + 2, GFP_KERNEL);
  	}
@@ -489,8 +498,8 @@ static void uvcg_video_pump(struct work_struct *work)
   */
  int uvcg_video_enable(struct uvc_video *video, int enable)
  {
-	unsigned int i;
  	int ret;
+	struct uvc_request *ureq;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -502,9 +511,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  		cancel_work_sync(&video->pump);
  		uvcg_queue_cancel(&video->queue, 0);
 
-		for (i = 0; i < video->uvc_num_requests; ++i)
-			if (video->ureq && video->ureq[i].req)
-				usb_ep_dequeue(video->ep, video->ureq[i].req);
+		list_for_each_entry(ureq, &video->ureqs, list) {
+			if (ureq->req)
+				usb_ep_dequeue(video->ep, ureq->req);
+		}
 
  		uvc_video_free_requests(video);
  		uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +546,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
   */
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  {
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	spin_lock_init(&video->req_lock);
  	INIT_WORK(&video->pump, uvcg_video_pump);
--

2.42.0.820.g83a721a137-goog

#54 lore raw

Re: [PATCH v9 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 16 lines · Dan Scally

Hi Avichal

Quoted 13 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 6 lines
Quoted 3 lines

Sorry - I was unclear in my response to the first patch on v8. I meant my R-b to apply to the first patch only rather than to all of them. For this one I understand now the use of the conditional in uvc_video_free_request(), so that point is fine. I agree with Greg that the BUG_ON() shouldn't stand though.

Ah, didn't realize BUG_ON is discouraged. Removed BUG_ON. It was supposed to be a defensive bit of code anyway, so removing the check entirely. If the state is inconsistent, we'd see other errors, so the BUG_ON wasn't providing value anyway.

Also removed your Reviewed-by, my apologies.

Quoted 196 lines
Quoted 196 lines

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue
v8 -> v9: Address review comments.

  drivers/usb/gadget/function/uvc.h       |  3 +- drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------

  2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
       struct sg_table sgt;
       u8 header[UVCG_REQUEST_HEADER_LEN];
       struct uvc_buffer *last_buf;
+    struct list_head list;
   };
 
   struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
       /* Requests */
       unsigned int req_size;
-    struct uvc_request *ureq;
+    struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
       struct list_head req_free;
       spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +18 -0
index c334802ac0a4..f8f9209fee50 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
    * Request handling
    */
 
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+    sg_free_table(&ureq->sgt);
+    if (ureq->req && ep) {
+        usb_ep_free_request(ep, ureq->req);
+        ureq->req = NULL;
+    }
+
+    kfree(ureq->req_buffer);
+    ureq->req_buffer = NULL;
+
+    if (!list_empty(&ureq->list))
+        list_del_init(&ureq->list);
+
+    kfree(ureq);
+}
+
   static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
   {
       int ret;
@@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)

  static int uvc_video_free_requests(struct uvc_video *video) {
-    unsigned int i;

drivers/usb/gadget/function/uvc_video.c +4 -18
-
-    if (video->ureq) {
-        for (i = 0; i < video->uvc_num_requests; ++i) {
-            sg_free_table(&video->ureq[i].sgt);
+    struct uvc_request *ureq, *temp;
 
-            if (video->ureq[i].req) {
-                usb_ep_free_request(video->ep, video->ureq[i].req);
-                video->ureq[i].req = NULL;
-            }
-
-            if (video->ureq[i].req_buffer) {
-                kfree(video->ureq[i].req_buffer);
-                video->ureq[i].req_buffer = NULL;
-            }
-        }
-
-        kfree(video->ureq);
-        video->ureq = NULL;
-    }
+    list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
+        uvc_video_free_request(ureq, video->ep);
 
+    INIT_LIST_HEAD(&video->ureqs);

      INIT_LIST_HEAD(&video->req_free);
      video->req_size = 0;
      return 0;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -322,39 +325,45 @@ uvc_video_free_requests(struct uvc_video *video)

  static int uvc_video_alloc_requests(struct uvc_video *video) {
+    struct uvc_request *ureq;
      unsigned int req_size;
      unsigned int i;
      int ret = -ENOMEM;

      BUG_ON(video->req_size);
+    BUG_ON(!list_empty(&video->ureqs));

      req_size = video->ep->maxpacket
           * max_t(unsigned int, video->ep->maxburst, 1)
           * (video->ep->mult);

-    video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL); -    if (video->ureq == NULL)
-        return -ENOMEM;
+    for (i = 0; i < video->uvc_num_requests; i++) {
+        ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+        if (ureq == NULL)
+            goto error;

drivers/usb/gadget/function/uvc_video.c +6 -3
+
+        INIT_LIST_HEAD(&ureq->list);
+
+        list_add_tail(&ureq->list, &video->ureqs);
 
-    for (i = 0; i < video->uvc_num_requests; ++i) {
-        video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-        if (video->ureq[i].req_buffer == NULL)
+        ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+        if (ureq->req_buffer == NULL)

              goto error;

-        video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL); -        if (video->ureq[i].req == NULL)
+        ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL); +        if (ureq->req == NULL)
              goto error;

-        video->ureq[i].req->buf = video->ureq[i].req_buffer; -        video->ureq[i].req->length = 0;
-        video->ureq[i].req->complete = uvc_video_complete; -        video->ureq[i].req->context = &video->ureq[i]; -        video->ureq[i].video = video;
-        video->ureq[i].last_buf = NULL;
+        ureq->req->buf = ureq->req_buffer;
+        ureq->req->length = 0;
+        ureq->req->complete = uvc_video_complete; +        ureq->req->context = ureq;
+        ureq->video = video;
+        ureq->last_buf = NULL;

-        list_add_tail(&video->ureq[i].req->list, &video->req_free); +        list_add_tail(&ureq->req->list, &video->req_free); /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */ -        sg_alloc_table(&video->ureq[i].sgt,
+        sg_alloc_table(&ureq->sgt,
                     DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN, PAGE_SIZE) + 2, GFP_KERNEL);
      }

drivers/usb/gadget/function/uvc_video.c +6 -4
@@ -489,8 +498,8 @@ static void uvcg_video_pump(struct work_struct *work)
    */
   int uvcg_video_enable(struct uvc_video *video, int enable)
   {
-    unsigned int i;
       int ret;
+    struct uvc_request *ureq;
 
       if (video->ep == NULL) {
           uvcg_info(&video->uvc->func,
@@ -502,9 +511,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
           cancel_work_sync(&video->pump);
           uvcg_queue_cancel(&video->queue, 0);
 
-        for (i = 0; i < video->uvc_num_requests; ++i)
-            if (video->ureq && video->ureq[i].req)
-                usb_ep_dequeue(video->ep, video->ureq[i].req);
+        list_for_each_entry(ureq, &video->ureqs, list) {
+            if (ureq->req)
+                usb_ep_dequeue(video->ep, ureq->req);
+        }
 
           uvc_video_free_requests(video);
           uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +546,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
    */
   int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
   {
+    INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
       spin_lock_init(&video->req_lock);
       INIT_WORK(&video->pump, uvcg_video_pump);
Signature · 1 line
2.42.0.820.g83a721a137-goog
#55 lore raw

PATCH v9 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'. This function is now used anywhere uvcg_video_enable(video, 0) was used.

---
v6: Introduced this patch to make the next one easier to review
v6 -> v7: Add Suggested-by
v7 -> v8: No change. Getting back in review queue
v8 -> v9: Call uvcg_video_disable directly instead of uvcg_video_enable(video, 0)

 drivers/usb/gadget/function/uvc_v4l2.c  |  6 ++--
 drivers/usb/gadget/function/uvc_video.c | 40 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  3 +-
 3 files changed, 31 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/uvc_v4l2.c +3 -3
index 7cb8d027ff0c..904dd283cbf7 100644
@@ -443,7 +443,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	/* Enable UVC video. */
-	ret = uvcg_video_enable(video, 1);
+	ret = uvcg_video_enable(video);
 	if (ret < 0)
 		return ret;
 
@@ -469,7 +469,7 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	uvc->state = UVC_STATE_CONNECTED;
-	ret = uvcg_video_enable(video, 0);
+	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
@@ -515,7 +515,7 @@ static void uvc_v4l2_disable(struct uvc_device *uvc)
 	if (uvc->state == UVC_STATE_STREAMING)
 		uvc->state = UVC_STATE_CONNECTED;
 
-	uvcg_video_enable(&uvc->video, 0);
+	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
 	wake_up_interruptible(&uvc->func_connected_queue);
drivers/usb/gadget/function/uvc_video.c +26 -14
index f8f9209fee50..1081dd790fd6 100644
@@ -494,31 +494,43 @@ static void uvcg_video_pump(struct work_struct *work)
 }
 
 /*
- * Enable or disable the video stream.
+ * Disable the video stream
  */
-int uvcg_video_enable(struct uvc_video *video, int enable)
+int
+uvcg_video_disable(struct uvc_video *video)
 {
-	int ret;
 	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
-			  "Video enable failed, device is uninitialized.\n");
+			  "Video disable failed, device is uninitialized.\n");
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
 
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
 
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
+/*
+ * Enable the video stream.
+ */
+int uvcg_video_enable(struct uvc_video *video)
+{
+	int ret;
+
+	if (video->ep == NULL) {
+		uvcg_info(&video->uvc->func,
+			  "Video enable failed, device is uninitialized.\n");
+		return -ENODEV;
 	}
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
drivers/usb/gadget/function/uvc_video.h +2 -1
index 03adeefa343b..8ef6259741f1 100644
@@ -14,7 +14,8 @@
 
 struct uvc_video;
 
-int uvcg_video_enable(struct uvc_video *video, int enable);
+int uvcg_video_enable(struct uvc_video *video);
+int uvcg_video_disable(struct uvc_video *video);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 

--
2.42.0.820.g83a721a137-goog

#56 lore raw

Re: [PATCH v9 3/4] usb: gadget: uvc: move video disable logic to its own function

Hi Avichal

Quoted 5 lines · Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'. This function is now used anywhere uvcg_video_enable(video, 0) was used.

For this patch you can keep the R-b - it's fine by me now :)

Quoted 122 lines

---
v6: Introduced this patch to make the next one easier to review
v6 -> v7: Add Suggested-by
v7 -> v8: No change. Getting back in review queue
v8 -> v9: Call uvcg_video_disable directly instead of uvcg_video_enable(video, 0)

  drivers/usb/gadget/function/uvc_v4l2.c  |  6 ++--
  drivers/usb/gadget/function/uvc_video.c | 40 ++++++++++++++++---------
  drivers/usb/gadget/function/uvc_video.h |  3 +-
  3 files changed, 31 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/uvc_v4l2.c +3 -3
index 7cb8d027ff0c..904dd283cbf7 100644
@@ -443,7 +443,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  		return -EINVAL;
 
  	/* Enable UVC video. */
-	ret = uvcg_video_enable(video, 1);
+	ret = uvcg_video_enable(video);
  	if (ret < 0)
  		return ret;
 
@@ -469,7 +469,7 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  		return -EINVAL;
 
  	uvc->state = UVC_STATE_CONNECTED;
-	ret = uvcg_video_enable(video, 0);
+	ret = uvcg_video_disable(video);
  	if (ret < 0)
  		return ret;
 
@@ -515,7 +515,7 @@ static void uvc_v4l2_disable(struct uvc_device *uvc)
  	if (uvc->state == UVC_STATE_STREAMING)
  		uvc->state = UVC_STATE_CONNECTED;
 
-	uvcg_video_enable(&uvc->video, 0);
+	uvcg_video_disable(&uvc->video);
  	uvcg_free_buffers(&uvc->video.queue);
  	uvc->func_connected = false;
  	wake_up_interruptible(&uvc->func_connected_queue);
drivers/usb/gadget/function/uvc_video.c +26 -14
index f8f9209fee50..1081dd790fd6 100644
@@ -494,31 +494,43 @@ static void uvcg_video_pump(struct work_struct *work)
  }
 
  /*
- * Enable or disable the video stream.
+ * Disable the video stream
   */
-int uvcg_video_enable(struct uvc_video *video, int enable)
+int
+uvcg_video_disable(struct uvc_video *video)
  {
-	int ret;
  	struct uvc_request *ureq;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
-			  "Video enable failed, device is uninitialized.\n");
+			  "Video disable failed, device is uninitialized.\n");
  		return -ENODEV;
  	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
 
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
 
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
+/*
+ * Enable the video stream.
+ */
+int uvcg_video_enable(struct uvc_video *video)
+{
+	int ret;
+
+	if (video->ep == NULL) {
+		uvcg_info(&video->uvc->func,
+			  "Video enable failed, device is uninitialized.\n");
+		return -ENODEV;
  	}
 
  	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
drivers/usb/gadget/function/uvc_video.h +2 -2
index 03adeefa343b..8ef6259741f1 100644
@@ -14,7 +14,8 @@
 
  struct uvc_video;
 
-int uvcg_video_enable(struct uvc_video *video, int enable);
+int uvcg_video_enable(struct uvc_video *video);
+int uvcg_video_disable(struct uvc_video *video);
 
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
--

2.42.0.820.g83a721a137-goog

#57 lore raw

PATCH v9 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
easier to review.
v6 -> v7: Fix warning reported in

          https://lore.kernel.org/[email protected]/

v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
 drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
 3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +108 -20

203 lines not on this page · raw message

--
2.42.0.820.g83a721a137-goog

#58 lore raw

Re: [PATCH v9 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Hi Avichal

Quoted 74 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
easier to review.
v6 -> v7: Fix warning reported in

           https://lore.kernel.org/[email protected]/

v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

  drivers/usb/gadget/function/uvc.h       |   1 +
  drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
  drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
  3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
  	unsigned int uvc_num_requests;
 
  	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
  	unsigned int req_size;
  	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
  	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -1
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  	 * Complete the alternate setting selection setup phase now that
  	 * userspace is ready to provide video frames.
  	 */
-	uvc_function_setup_continue(uvc, 0);
  	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
  	return 0;
  }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  	if (type != video->queue.queue.type)
  		return -EINVAL;

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 478

Quoted 10 lines
drivers/usb/gadget/function/uvc_v4l2.c +1 -1
 
-	uvc->state = UVC_STATE_CONNECTED;
  	ret = uvcg_video_disable(video);
  	if (ret < 0)
  		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
  	uvc_function_setup_continue(uvc, 1);
  	return 0;
  }

I'm not sure I understand what these re-orderings are for...can you explain please?

Quoted 20 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -8
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
  static void uvc_v4l2_disable(struct uvc_device *uvc)
  {
  	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
  	uvcg_video_disable(&uvc->video);
  	uvcg_free_buffers(&uvc->video.queue);
  	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 1081dd790fd6..8f330ce696ec 100644

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 232

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
   * Request handling
   */
 
+/*
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

This comment probably belongs in patch #2. And in that case, shouldn't uvc_video_free_requests() hold the lock in that patch?

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +0 -0
  static void
  uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
  {
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	struct uvc_request *ureq = req->context;

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 283

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +5 -1
  	struct uvc_video *video = ureq->video;
  	struct uvc_video_queue *queue = &video->queue;
-	struct uvc_device *uvc = video->uvc;
+	struct uvc_buffer *last_buf = NULL;
  	unsigned long flags;
 
+	spin_lock_irqsave(&video->req_lock, flags);
+	if (!video->is_enabled) {
+		/*
+		 * When is_enabled is false, uvc_video_disable ensures that

s/uvc_video_disable/uvc_video_disable()

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 287

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+		 * in-flight uvc_buffers are returned, so we can safely
+		 * call free_request without worrying about last_buf.
+		 */
+		uvc_video_free_request(ureq, ep);

Now I understand the conditional in this function in patch 2 :)

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 294

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		return;
+	}
+
+	last_buf = ureq->last_buf;
+	ureq->last_buf = NULL;
+	spin_unlock_irqrestore(&video->req_lock, flags);

I'm not a huge fan of this locking, unlocking and relocking the same spinlock within the same function. Can we just hold the lock for the duration? if not, can there be an explanatory comment as to why?

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+
  	switch (req->status) {
  	case 0:
  		break;

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 320

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +4 -3
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  		uvcg_queue_cancel(queue, 0);
  	}
 
-	if (ureq->last_buf) {
-		uvcg_complete_buffer(&video->queue, ureq->last_buf);
-		ureq->last_buf = NULL;
+	if (last_buf) {
+		spin_lock_irqsave(&queue->irqlock, flags);
+		uvcg_complete_buffer(&video->queue, last_buf);
+		spin_unlock_irqrestore(&queue->irqlock, flags);

I think it's right to take the irqlock here but it probably should have always been held, so this probably ought to go in its own commit with a Fixes:

Quoted 166 lines
drivers/usb/gadget/function/uvc_video.c +84 -17
  	}
 
  	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
-	spin_unlock_irqrestore(&video->req_lock, flags);
-
-	if (uvc->state == UVC_STATE_STREAMING)
+	/*
+	 * Video stream might have been disabled while we were
+	 * processing the current usb_request. So make sure
+	 * we're still streaming before queueing the usb_request
+	 * back to req_free
+	 */
+	if (video->is_enabled) {
+		list_add_tail(&req->list, &video->req_free);
  		queue_work(video->async_wq, &video->pump);
+	} else {
+		uvc_video_free_request(ureq, ep);
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
  }
 
  static int
@@ -393,20 +421,22 @@ static void uvcg_video_pump(struct work_struct *work)
  	struct uvc_video_queue *queue = &video->queue;
  	/* video->max_payload_size is only set when using bulk transfer */
  	bool is_bulk = video->max_payload_size;
-	struct uvc_device *uvc = video->uvc;
  	struct usb_request *req = NULL;
  	struct uvc_buffer *buf;
  	unsigned long flags;
  	bool buf_done;
  	int ret;
 
-	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+	while (true) {
+		if (!video->ep->enabled)
+			return;
+
  		/*
-		 * Retrieve the first available USB request, protected by the
-		 * request lock.
+		 * Check is_enabled and retrieve the first available USB
+		 * request, protected by the request lock.
  		 */
  		spin_lock_irqsave(&video->req_lock, flags);
-		if (list_empty(&video->req_free)) {
+		if (!video->is_enabled || list_empty(&video->req_free)) {
  			spin_unlock_irqrestore(&video->req_lock, flags);
  			return;
  		}
@@ -488,9 +518,11 @@ static void uvcg_video_pump(struct work_struct *work)
  		return;
 
  	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
+	if (video->is_enabled)
+		list_add_tail(&req->list, &video->req_free);
+	else
+		uvc_video_free_request(req->context, video->ep);
  	spin_unlock_irqrestore(&video->req_lock, flags);
-	return;
  }
 
  /*
@@ -499,7 +531,11 @@ static void uvcg_video_pump(struct work_struct *work)
  int
  uvcg_video_disable(struct uvc_video *video)
  {
-	struct uvc_request *ureq;
+	unsigned long flags;
+	struct list_head inflight_bufs;
+	struct usb_request *req, *temp;
+	struct uvc_buffer *buf, *btemp;
+	struct uvc_request *ureq, *utemp;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -507,15 +543,58 @@ uvcg_video_disable(struct uvc_video *video)
  		return -ENODEV;
  	}
 
+	INIT_LIST_HEAD(&inflight_bufs);
+	spin_lock_irqsave(&video->req_lock, flags);
+	video->is_enabled = false;
+
+	/*
+	 * Remove any in-flight buffers from the uvc_requests
+	 * because we want to return them before cancelling the
+	 * queue. This ensures that we aren't stuck waiting for
+	 * all complete callbacks to come through before disabling
+	 * vb2 queue.
+	 */
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->last_buf) {
+			list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+			ureq->last_buf = NULL;
+		}
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
  	cancel_work_sync(&video->pump);
  	uvcg_queue_cancel(&video->queue, 0);
 
-	list_for_each_entry(ureq, &video->ureqs, list) {
-		if (ureq->req)
-			usb_ep_dequeue(video->ep, ureq->req);
+	spin_lock_irqsave(&video->req_lock, flags);
+	/*
+	 * Remove all uvc_reqeusts from ureqs with list_del_init
+	 * This lets uvc_video_free_request correctly identify
+	 * if the uvc_request is attached to a list or not when freeing
+	 * memory.
+	 */
+	list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+		list_del_init(&ureq->list);
+
+	list_for_each_entry_safe(req, temp, &video->req_free, list) {
+		list_del(&req->list);
+		uvc_video_free_request(req->context, video->ep);
  	}
 
-	uvc_video_free_requests(video);
+	INIT_LIST_HEAD(&video->ureqs);
+	INIT_LIST_HEAD(&video->req_free);
+	video->req_size = 0;
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
+	/*
+	 * Return all the video buffers before disabling the queue.
+	 */
+	spin_lock_irqsave(&video->queue.irqlock, flags);
+	list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+		list_del(&buf->queue);
+		uvcg_complete_buffer(&video->queue, buf);
+	}
+	spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
  	uvcg_queue_enable(&video->queue, 0);
  	return 0;
  }
@@ -533,6 +612,14 @@ int uvcg_video_enable(struct uvc_video *video)
  		return -ENODEV;
  	}
 
+	/*
+	 * Safe to access request related fields without req_lock because
+	 * this is the only thread currently active, and no other
+	 * request handling thread will become active until this function
+	 * returns.
+	 */
+	video->is_enabled = true;
+
  	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  		return ret;
 
@@ -558,6 +645,7 @@ int uvcg_video_enable(struct uvc_video *video)
   */
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  {
+	video->is_enabled = false;
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	spin_lock_init(&video->req_lock);
--

2.42.0.820.g83a721a137-goog

#59 lore raw

Re: [PATCH v9 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Thank you for taking a look Dan!

Quoted 83 lines · Dan Scally

Hi Avichal

Quoted 80 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
           https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
           https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable easier to review.
v6 -> v7: Fix warning reported in
           https://lore.kernel.org/[email protected]/
v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

  drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-- drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----

  3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
       unsigned int uvc_num_requests;
 
       /* Requests */
+    bool is_enabled; /* tracks whether video stream is enabled */
       unsigned int req_size;

      struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ struct list_head req_free;

drivers/usb/gadget/function/uvc_v4l2.c +1 -2
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
        * Complete the alternate setting selection setup phase now that
        * userspace is ready to provide video frames.
        */
-    uvc_function_setup_continue(uvc, 0);
       uvc->state = UVC_STATE_STREAMING;
+    uvc_function_setup_continue(uvc, 0);
 
       return 0;
   }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
       if (type != video->queue.queue.type)
           return -EINVAL;
 
-    uvc->state = UVC_STATE_CONNECTED;
       ret = uvcg_video_disable(video);
       if (ret < 0)
           return ret;
 
Quoted 7 lines

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 478

Quoted 4 lines
drivers/usb/gadget/function/uvc_v4l2.c +1 -0
+    uvc->state = UVC_STATE_CONNECTED;
       uvc_function_setup_continue(uvc, 1);
       return 0;
   }

I'm not sure I understand what these re-orderings are for...can you explain please?

This specific one was a leftover from testing, removed this hunk. But the ones below are undoing the change in patch 1, which is flawed in its use of uvc->state without any memory guarantees.

So from patch 1 to patch 4, we shuffle the code around a bit, but this makes patch 1 somewhat complete and functional even if patch 4 were to be reverted.

Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -8
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
   static void uvc_v4l2_disable(struct uvc_device *uvc)
   {
       uvc_function_disconnect(uvc);
-    /*
-     * Drop uvc->state to CONNECTED if it was streaming before.
-     * This ensures that the usb_requests are no longer queued
-     * to the controller.
-     */
-    if (uvc->state == UVC_STATE_STREAMING)
-        uvc->state = UVC_STATE_CONNECTED;
-
       uvcg_video_disable(&uvc->video);
       uvcg_free_buffers(&uvc->video.queue);
       uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 1081dd790fd6..8f330ce696ec 100644
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
    * Request handling
    */
 
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 232

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
+/*
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

This comment probably belongs in patch #2. And in that case, shouldn't uvc_video_free_requests() hold the lock in that patch?

Patch 2 doesn't change any existing locking semantics. The current code does not enforce any locking on freeing the requests, and neither does patch 2.

Patch 4 introduces another call site for uvc_video_free_request, so some synchronization guarantees are needed (and hence the addition of this comment).

As for uvc_video_free_requests not holding the lock, it is safe because uvc_video_free_requests is only called if request initialization fails. So uvc_video_free_requests should be the thread safe, as no other thread is processing requests when it is called.

I did add a comment in uvcg_video_enable mentioning why it is safe to not hold req_free even though it accesses request related fields.

Happy to add another comment to uvc_video_free_requests if that makes it clearer!

Quoted 12 lines
Quoted 12 lines

  static void
  uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep) {

drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_request *ureq = req->context;
       struct uvc_video *video = ureq->video;
       struct uvc_video_queue *queue = &video->queue;
-    struct uvc_device *uvc = video->uvc;
+    struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
 
Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 283

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    spin_lock_irqsave(&video->req_lock, flags);
+    if (!video->is_enabled) {
+        /*
+         * When is_enabled is false, uvc_video_disable ensures that

s/uvc_video_disable/uvc_video_disable()

Done!

Quoted 14 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 287

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+         * in-flight uvc_buffers are returned, so we can safely
+         * call free_request without worrying about last_buf.
+         */
+        uvc_video_free_request(ureq, ep);

Now I understand the conditional in this function in patch 2 :)

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 294

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        spin_unlock_irqrestore(&video->req_lock, flags);
+        return;
+    }
+
+    last_buf = ureq->last_buf;
+    ureq->last_buf = NULL;
+    spin_unlock_irqrestore(&video->req_lock, flags);
Quoted 1 line

I'm not a huge fan of this locking, unlocking and relocking the same spinlock within the same function. Can we just hold the lock for the duration? if not, can there be an explanatory comment as to why?

I agree that this is a little unfortunate, and it'd be nice if we only had a single driver level lock. However, as it stands, if we hold req_lock for the entirety of completion handler, we risk two things:

1. Adding dependencies between queue->irqlock and video->reqlock
2. Starving the video_pump thread.

As of this patch, uvc_video_complete follows the same pattern as video_pump function:
1. Acquire req_lock
2. Fetch/Query usb_request
3. Drop req_lock

4. Acquire queue->irqlock
5. Buffer ops (encode/free/stop)
6. Drop queue->irqlock

7. Acquire req_lock
8. usb_request cleanup/handling
9. Drop req_lock

(7), (8), and (9) are optional for video_pump, while (4), (5), and (6) are optional for uvc_video_complete.

We can short-circuit uvc_video_complete with only one lock on the happy path, but this would have to be the flow for non-happy paths unless we want to hold the two locks at the same time (which isn't the worst idea, but comes with its own set of concerns).

Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+
       switch (req->status) {

      case 0:
          break;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
           uvcg_queue_cancel(queue, 0);
       }
 
Quoted 11 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 320

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +4 -3
-    if (ureq->last_buf) {
-        uvcg_complete_buffer(&video->queue, ureq->last_buf);
-        ureq->last_buf = NULL;
+    if (last_buf) {
+        spin_lock_irqsave(&queue->irqlock, flags);
+        uvcg_complete_buffer(&video->queue, last_buf);
+        spin_unlock_irqrestore(&queue->irqlock, flags);

I think it's right to take the irqlock here but it probably should have always been held, so this probably ought to go in its own commit with a Fixes:

The lock here wasn't required before, because uvcg_complete_buffer was only ever called by the completion handler, which is synchronized by the usb controller. This is the reason we never saw an issue despite not holding the lock.

This patch introduces another call site in uvcg_video_disable, so to protect memory consistency, we need to make sure calls to uvcg_complete_buffer are synchronized on something other than the usb controller.

Quoted 167 lines
Quoted 167 lines
drivers/usb/gadget/function/uvc_video.c +12 -4
       }
 
       spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
-    spin_unlock_irqrestore(&video->req_lock, flags);
-
-    if (uvc->state == UVC_STATE_STREAMING)
+    /*
+     * Video stream might have been disabled while we were
+     * processing the current usb_request. So make sure
+     * we're still streaming before queueing the usb_request
+     * back to req_free
+     */
+    if (video->is_enabled) {
+        list_add_tail(&req->list, &video->req_free);
           queue_work(video->async_wq, &video->pump);
+    } else {
+        uvc_video_free_request(ureq, ep);
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
   }
 

  static int

drivers/usb/gadget/function/uvc_video.c +11 -7
@@ -393,20 +421,22 @@ static void uvcg_video_pump(struct work_struct *work)
       struct uvc_video_queue *queue = &video->queue;
       /* video->max_payload_size is only set when using bulk transfer */
       bool is_bulk = video->max_payload_size;
-    struct uvc_device *uvc = video->uvc;
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
       bool buf_done;
       int ret;
 
-    while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+    while (true) {
+        if (!video->ep->enabled)
+            return;
+
           /*
-         * Retrieve the first available USB request, protected by the
-         * request lock.
+         * Check is_enabled and retrieve the first available USB
+         * request, protected by the request lock.
            */
           spin_lock_irqsave(&video->req_lock, flags);
-        if (list_empty(&video->req_free)) {
+        if (!video->is_enabled || list_empty(&video->req_free)) {
               spin_unlock_irqrestore(&video->req_lock, flags);
               return;
           }
@@ -488,9 +518,11 @@ static void uvcg_video_pump(struct work_struct *work)
           return;
 
       spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
+    if (video->is_enabled)
+        list_add_tail(&req->list, &video->req_free);
+    else
+        uvc_video_free_request(req->context, video->ep);
       spin_unlock_irqrestore(&video->req_lock, flags);
-    return;
   }
 
   /*
@@ -499,7 +531,11 @@ static void uvcg_video_pump(struct work_struct *work)

  int
  uvcg_video_disable(struct uvc_video *video)
  {
-    struct uvc_request *ureq;
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;

      if (video->ep == NULL) {
          uvcg_info(&video->uvc->func,

drivers/usb/gadget/function/uvc_video.c +56 -4
@@ -507,15 +543,58 @@ uvcg_video_disable(struct uvc_video *video)
           return -ENODEV;
       }
 
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
       cancel_work_sync(&video->pump);
       uvcg_queue_cancel(&video->queue, 0);
 
-    list_for_each_entry(ureq, &video->ureqs, list) {
-        if (ureq->req)
-            usb_ep_dequeue(video->ep, ureq->req);
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
       }
 
-    uvc_video_free_requests(video);
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
       uvcg_queue_enable(&video->queue, 0);
       return 0;
   }
@@ -533,6 +612,14 @@ int uvcg_video_enable(struct uvc_video *video)
           return -ENODEV;
       }
 
+    /*
+     * Safe to access request related fields without req_lock because
+     * this is the only thread currently active, and no other
+     * request handling thread will become active until this function
+     * returns.
+     */
+    video->is_enabled = true;
+
       if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
           return ret;
 
@@ -558,6 +645,7 @@ int uvcg_video_enable(struct uvc_video *video)
    */
   int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
   {
+    video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
       spin_lock_init(&video->req_lock);
Signature · 1 line
2.42.0.820.g83a721a137-goog
#60 lore raw

Re: [PATCH v9 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Hi Avichal

Quoted 92 lines · Avichal Rakesh

Thank you for taking a look Dan!

Quoted 83 lines · Dan Scally

Hi Avichal

Quoted 80 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
           https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
           as discussed in
           https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
           easier to review.
v6 -> v7: Fix warning reported in
           https://lore.kernel.org/[email protected]/
v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

   drivers/usb/gadget/function/uvc.h       |   1 +
   drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
   drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
   3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
       unsigned int uvc_num_requests;
 
       /* Requests */
+    bool is_enabled; /* tracks whether video stream is enabled */
       unsigned int req_size;
       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
       struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -2
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
        * Complete the alternate setting selection setup phase now that
        * userspace is ready to provide video frames.
        */
-    uvc_function_setup_continue(uvc, 0);
       uvc->state = UVC_STATE_STREAMING;
+    uvc_function_setup_continue(uvc, 0);
 
       return 0;
   }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
       if (type != video->queue.queue.type)
           return -EINVAL;
 
-    uvc->state = UVC_STATE_CONNECTED;
       ret = uvcg_video_disable(video);
       if (ret < 0)
           return ret;
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 478

Quoted 4 lines
drivers/usb/gadget/function/uvc_v4l2.c +1 -0
+    uvc->state = UVC_STATE_CONNECTED;
       uvc_function_setup_continue(uvc, 1);
       return 0;
   }

I'm not sure I understand what these re-orderings are for...can you explain please?

Quoted 7 lines

This specific one was a leftover from testing, removed this hunk. But the ones below are undoing the change in patch 1, which is flawed in its use of uvc->state without any memory guarantees.

So from patch 1 to patch 4, we shuffle the code around a bit, but this makes patch 1 somewhat complete and functional even if patch 4 were to be reverted.

Okedokey - that's fine (and good)

Quoted 38 lines
Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -8
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
   static void uvc_v4l2_disable(struct uvc_device *uvc)
   {
       uvc_function_disconnect(uvc);
-    /*
-     * Drop uvc->state to CONNECTED if it was streaming before.
-     * This ensures that the usb_requests are no longer queued
-     * to the controller.
-     */
-    if (uvc->state == UVC_STATE_STREAMING)
-        uvc->state = UVC_STATE_CONNECTED;
-
       uvcg_video_disable(&uvc->video);
       uvcg_free_buffers(&uvc->video.queue);
       uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 1081dd790fd6..8f330ce696ec 100644
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
    * Request handling
    */
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 232

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
+/*
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

This comment probably belongs in patch #2. And in that case, shouldn't uvc_video_free_requests() hold the lock in that patch?

Patch 2 doesn't change any existing locking semantics. The current code does not enforce any locking on freeing the requests, and neither does patch 2.

Patch 4 introduces another call site for uvc_video_free_request, so some synchronization guarantees are needed (and hence the addition of this comment).

Quoted 7 lines

As for uvc_video_free_requests not holding the lock, it is safe because uvc_video_free_requests is only called if request initialization fails. So uvc_video_free_requests should be the thread safe, as no other thread is processing requests when it is called.

I did add a comment in uvcg_video_enable mentioning why it is safe to not hold req_free even though it accesses request related fields.

I understand, but in that case I think the comment is a little confusing - it's not the fact that the function modifies the list ureq is held in that requires it to be locked but that there're potentially multiple threads doing so. Can we go for something like "Callers must take care to hold req_lock when using this function outside of a single thread"? Feel free to re-word that however you like, as long as it's clear that it's only necessary when multiple threads could be active.

Quoted 4 lines

Happy to add another comment to uvc_video_free_requests if that makes it clearer!

Quoted 9 lines
Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 277

Quoted 9 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
   static void
   uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
   {
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_request *ureq = req->context;
       struct uvc_video *video = ureq->video;
       struct uvc_video_queue *queue = &video->queue;
-    struct uvc_device *uvc = video->uvc;
+    struct uvc_buffer *last_buf = NULL;

This initialisation's unnecessary since it's unconditionally set below.

Quoted 45 lines
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 283

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
       unsigned long flags;
 
+    spin_lock_irqsave(&video->req_lock, flags);
+    if (!video->is_enabled) {
+        /*
+         * When is_enabled is false, uvc_video_disable ensures that

s/uvc_video_disable/uvc_video_disable()

Done!

Quoted 13 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 287

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+         * in-flight uvc_buffers are returned, so we can safely
+         * call free_request without worrying about last_buf.
+         */
+        uvc_video_free_request(ureq, ep);

Now I understand the conditional in this function in patch 2 :)

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 294

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        spin_unlock_irqrestore(&video->req_lock, flags);
+        return;
+    }
+
+    last_buf = ureq->last_buf;
+    ureq->last_buf = NULL;
+    spin_unlock_irqrestore(&video->req_lock, flags);
Quoted 1 line

I'm not a huge fan of this locking, unlocking and relocking the same spinlock within the same function. Can we just hold the lock for the duration? if not, can there be an explanatory comment as to why?

I agree that this is a little unfortunate, and it'd be nice if we only had a single driver level lock. However, as it stands, if we hold req_lock for the entirety of completion handler, we risk two things:

1. Adding dependencies between queue->irqlock and video->reqlock
2. Starving the video_pump thread.

As of this patch, uvc_video_complete follows the same pattern as video_pump function:
1. Acquire req_lock
2. Fetch/Query usb_request
3. Drop req_lock

4. Acquire queue->irqlock
5. Buffer ops (encode/free/stop)
6. Drop queue->irqlock

7. Acquire req_lock
8. usb_request cleanup/handling
9. Drop req_lock

Quoted 9 lines

(7), (8), and (9) are optional for video_pump, while (4), (5), and (6) are optional for uvc_video_complete.

We can short-circuit uvc_video_complete with only one lock on the happy path, but this would have to be the flow for non-happy paths unless we want to hold the two locks at the same time (which isn't the worst idea, but comes with its own set of concerns).

Yeah. Sorry - I had missed that uvcg_video_pump() follows the same pattern too. Alright, I think leave this as-is.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+
       switch (req->status) {
       case 0:
           break;
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
           uvcg_queue_cancel(queue, 0);
       }
 
Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 320

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +4 -3
-    if (ureq->last_buf) {
-        uvcg_complete_buffer(&video->queue, ureq->last_buf);
-        ureq->last_buf = NULL;
+    if (last_buf) {
+        spin_lock_irqsave(&queue->irqlock, flags);
+        uvcg_complete_buffer(&video->queue, last_buf);
+        spin_unlock_irqrestore(&queue->irqlock, flags);

I think it's right to take the irqlock here but it probably should have always been held, so this probably ought to go in its own commit with a Fixes:

Quoted 4 lines

The lock here wasn't required before, because uvcg_complete_buffer was only ever called by the completion handler, which is synchronized by the usb controller. This is the reason we never saw an issue despite not holding the lock.

Ah - I was misled by the "called with &queue_irqlock held..." comment on uvc_complete_buffer()...I assumed that meant "you must call this function with &queue->irqlock held", but that turns out to be something of a hangover - originally it was placed there by 95faf82bd3ea6 because the callers of (at the time uvc_queue_next_buffer(), later renamed to) uvcg_complete_buffer() already held the irqlock. Clearly it needed locking then, but the function at the time manipulated queue->irqqueue and now it no longer does - that part has been stripped out to encode_bulk/encode_isoc/encode_isoc_sg(). So, probably that comment ought to have been removed at some point.

Quoted 4 lines

This patch introduces another call site in uvcg_video_disable, so to protect memory consistency, we need to make sure calls to uvcg_complete_buffer are synchronized on something other than the usb controller.

If it's simply to prevent double calling uvcg_complete_buffer() for a buffer, is holding the irqlock necessary? Both uvc_video_complete() and uvc_video_disable() conditionally call uvcg_complete_buffer() based on whether ureq->last_buf is set or not, and both functions assess that whilst holding req_lock, so unless I'm missing something that situation is already guarded against.

Quoted 105 lines
Quoted 105 lines
Quoted 105 lines
drivers/usb/gadget/function/uvc_video.c +47 -12
       }
 
       spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
-    spin_unlock_irqrestore(&video->req_lock, flags);
-
-    if (uvc->state == UVC_STATE_STREAMING)
+    /*
+     * Video stream might have been disabled while we were
+     * processing the current usb_request. So make sure
+     * we're still streaming before queueing the usb_request
+     * back to req_free
+     */
+    if (video->is_enabled) {
+        list_add_tail(&req->list, &video->req_free);
           queue_work(video->async_wq, &video->pump);
+    } else {
+        uvc_video_free_request(ureq, ep);
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
   }
 
   static int
@@ -393,20 +421,22 @@ static void uvcg_video_pump(struct work_struct *work)
       struct uvc_video_queue *queue = &video->queue;
       /* video->max_payload_size is only set when using bulk transfer */
       bool is_bulk = video->max_payload_size;
-    struct uvc_device *uvc = video->uvc;
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
       bool buf_done;
       int ret;
 
-    while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+    while (true) {
+        if (!video->ep->enabled)
+            return;
+
           /*
-         * Retrieve the first available USB request, protected by the
-         * request lock.
+         * Check is_enabled and retrieve the first available USB
+         * request, protected by the request lock.
            */
           spin_lock_irqsave(&video->req_lock, flags);
-        if (list_empty(&video->req_free)) {
+        if (!video->is_enabled || list_empty(&video->req_free)) {
               spin_unlock_irqrestore(&video->req_lock, flags);
               return;
           }
@@ -488,9 +518,11 @@ static void uvcg_video_pump(struct work_struct *work)
           return;
 
       spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
+    if (video->is_enabled)
+        list_add_tail(&req->list, &video->req_free);
+    else
+        uvc_video_free_request(req->context, video->ep);
       spin_unlock_irqrestore(&video->req_lock, flags);
-    return;
   }
 
   /*
@@ -499,7 +531,11 @@ static void uvcg_video_pump(struct work_struct *work)
   int
   uvcg_video_disable(struct uvc_video *video)
   {
-    struct uvc_request *ureq;
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;
 
       if (video->ep == NULL) {
           uvcg_info(&video->uvc->func,
@@ -507,15 +543,58 @@ uvcg_video_disable(struct uvc_video *video)
           return -ENODEV;
       }
 
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
       cancel_work_sync(&video->pump);
       uvcg_queue_cancel(&video->queue, 0);
 
Quoted 6 lines
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 570

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +3 -3
-    list_for_each_entry(ureq, &video->ureqs, list) {
-        if (ureq->req)
-            usb_ep_dequeue(video->ep, ureq->req);
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from ureqs with list_del_init

s/uvc_reqeusts/uvc_requests

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
drivers/usb/gadget/function/uvc_video.c +34 -1
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
       }
 
-    uvc_video_free_requests(video);
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
       uvcg_queue_enable(&video->queue, 0);
       return 0;
   }
@@ -533,6 +612,14 @@ int uvcg_video_enable(struct uvc_video *video)
           return -ENODEV;
       }
 
+    /*
+     * Safe to access request related fields without req_lock because
+     * this is the only thread currently active, and no other
+     * request handling thread will become active until this function
+     * returns.
+     */
+    video->is_enabled = true;
+
       if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
           return ret;
 
@@ -558,6 +645,7 @@ int uvcg_video_enable(struct uvc_video *video)
    */
   int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
   {
+    video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
       spin_lock_init(&video->req_lock);
Signature · 1 line
2.42.0.820.g83a721a137-goog
#61 lore raw

Re: [PATCH v9 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Hey Dan, thank you for taking the time to review the patch!

Sent out v11 with some comments addressed:
https://lore.kernel.org/all/[email protected]/

Quoted 149 lines · Dan Scally

Hi Avichal

Quoted 92 lines · Avichal Rakesh

Thank you for taking a look Dan!

Quoted 83 lines · Dan Scally

Hi Avichal

Quoted 80 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
            https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
            https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable easier to review.
v6 -> v7: Fix warning reported in
            https://lore.kernel.org/[email protected]/
v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

   drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-- drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----

   3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
        unsigned int uvc_num_requests;
 
        /* Requests */
+    bool is_enabled; /* tracks whether video stream is enabled */
        unsigned int req_size;

       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ struct list_head req_free;

drivers/usb/gadget/function/uvc_v4l2.c +1 -2
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
         * Complete the alternate setting selection setup phase now that
         * userspace is ready to provide video frames.
         */
-    uvc_function_setup_continue(uvc, 0);
        uvc->state = UVC_STATE_STREAMING;
+    uvc_function_setup_continue(uvc, 0);
 
        return 0;
    }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
        if (type != video->queue.queue.type)
            return -EINVAL;
 
-    uvc->state = UVC_STATE_CONNECTED;
        ret = uvcg_video_disable(video);
        if (ret < 0)
            return ret;
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 478

Quoted 4 lines
drivers/usb/gadget/function/uvc_v4l2.c +1 -0
+    uvc->state = UVC_STATE_CONNECTED;
        uvc_function_setup_continue(uvc, 1);
        return 0;
    }

I'm not sure I understand what these re-orderings are for...can you explain please?

Quoted 7 lines

This specific one was a leftover from testing, removed this hunk. But the ones below are undoing the change in patch 1, which is flawed in its use of uvc->state without any memory guarantees.

So from patch 1 to patch 4, we shuffle the code around a bit, but this makes patch 1 somewhat complete and functional even if patch 4 were to be reverted.

Okedokey - that's fine (and good)

Quoted 43 lines
Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -8
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
    static void uvc_v4l2_disable(struct uvc_device *uvc)
    {
        uvc_function_disconnect(uvc);
-    /*
-     * Drop uvc->state to CONNECTED if it was streaming before.
-     * This ensures that the usb_requests are no longer queued
-     * to the controller.
-     */
-    if (uvc->state == UVC_STATE_STREAMING)
-        uvc->state = UVC_STATE_CONNECTED;
-
        uvcg_video_disable(&uvc->video);
        uvcg_free_buffers(&uvc->video.queue);
        uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 1081dd790fd6..8f330ce696ec 100644
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
     * Request handling
     */
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 232

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
+/*
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

This comment probably belongs in patch #2. And in that case, shouldn't uvc_video_free_requests() hold the lock in that patch?

Patch 2 doesn't change any existing locking semantics. The current code does not enforce any locking on freeing the requests, and neither does patch 2.

Patch 4 introduces another call site for uvc_video_free_request, so some synchronization guarantees are needed (and hence the addition of this comment).

As for uvc_video_free_requests not holding the lock, it is safe because uvc_video_free_requests is only called if request initialization fails. So uvc_video_free_requests should be the thread safe, as no other thread is processing requests when it is called.

Quoted 5 lines
Quoted 2 lines

I did add a comment in uvcg_video_enable mentioning why it is safe to not hold req_free even though it accesses request related fields.

I understand, but in that case I think the comment is a little confusing - it's not the fact that the function modifies the list ureq is held in that requires it to be locked but that there're potentially multiple threads doing so. Can we go for something like "Callers must take care to hold req_lock when using this function outside of a single thread"? Feel free to re-word that however you like, as long as it's clear that it's only necessary when multiple threads could be active.

I see. I think having all the context made it obvious in my mind. Updated the comment to be more explicit!

Quoted 99 lines
Quoted 5 lines

Happy to add another comment to uvc_video_free_requests if that makes it clearer!

Quoted 9 lines
Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 277

Quoted 9 lines

   static void
   uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep) {

drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        struct uvc_request *ureq = req->context;
        struct uvc_video *video = ureq->video;
        struct uvc_video_queue *queue = &video->queue;
-    struct uvc_device *uvc = video->uvc;
+    struct uvc_buffer *last_buf = NULL;

This initialisation's unnecessary since it's unconditionally set below.

Quoted 45 lines
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 283

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
        unsigned long flags;
 
+    spin_lock_irqsave(&video->req_lock, flags);
+    if (!video->is_enabled) {
+        /*
+         * When is_enabled is false, uvc_video_disable ensures that

s/uvc_video_disable/uvc_video_disable()

Done!

Quoted 13 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 287

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+         * in-flight uvc_buffers are returned, so we can safely
+         * call free_request without worrying about last_buf.
+         */
+        uvc_video_free_request(ureq, ep);

Now I understand the conditional in this function in patch 2 :)

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 294

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        spin_unlock_irqrestore(&video->req_lock, flags);
+        return;
+    }
+
+    last_buf = ureq->last_buf;
+    ureq->last_buf = NULL;
+    spin_unlock_irqrestore(&video->req_lock, flags);
Quoted 1 line

I'm not a huge fan of this locking, unlocking and relocking the same spinlock within the same function. Can we just hold the lock for the duration? if not, can there be an explanatory comment as to why?

I agree that this is a little unfortunate, and it'd be nice if we only had a single driver level lock. However, as it stands, if we hold req_lock for the entirety of completion handler, we risk two things:

1. Adding dependencies between queue->irqlock and video->reqlock
2. Starving the video_pump thread.

As of this patch, uvc_video_complete follows the same pattern as video_pump function:
1. Acquire req_lock
2. Fetch/Query usb_request
3. Drop req_lock

4. Acquire queue->irqlock
5. Buffer ops (encode/free/stop)
6. Drop queue->irqlock

7. Acquire req_lock
8. usb_request cleanup/handling
9. Drop req_lock

Quoted 9 lines

(7), (8), and (9) are optional for video_pump, while (4), (5), and (6) are optional for uvc_video_complete.

We can short-circuit uvc_video_complete with only one lock on the happy path, but this would have to be the flow for non-happy paths unless we want to hold the two locks at the same time (which isn't the worst idea, but comes with its own set of concerns).

Yeah. Sorry - I had missed that uvcg_video_pump() follows the same pattern too. Alright, I think leave this as-is.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+
        switch (req->status) {

       case 0:
           break;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
            uvcg_queue_cancel(queue, 0);
        }
 
Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 320

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +4 -3
-    if (ureq->last_buf) {
-        uvcg_complete_buffer(&video->queue, ureq->last_buf);
-        ureq->last_buf = NULL;
+    if (last_buf) {
+        spin_lock_irqsave(&queue->irqlock, flags);
+        uvcg_complete_buffer(&video->queue, last_buf);
+        spin_unlock_irqrestore(&queue->irqlock, flags);

I think it's right to take the irqlock here but it probably should have always been held, so this probably ought to go in its own commit with a Fixes:

Quoted 4 lines

The lock here wasn't required before, because uvcg_complete_buffer was only ever called by the completion handler, which is synchronized by the usb controller. This is the reason we never saw an issue despite not holding the lock.

Quoted 8 lines

Ah - I was misled by the "called with &queue_irqlock held..." comment on uvc_complete_buffer()...I assumed that meant "you must call this function with &queue->irqlock held", but that turns out to be something of a hangover - originally it was placed there by 95faf82bd3ea6 because the callers of (at the time uvc_queue_next_buffer(), later renamed to) uvcg_complete_buffer() already held the irqlock. Clearly it needed locking then, but the function at the time manipulated queue->irqqueue and now it no longer does - that part has been stripped out to encode_bulk/encode_isoc/encode_isoc_sg(). So, probably that comment ought to have been removed at some point.

Quoted 4 lines

This patch introduces another call site in uvcg_video_disable, so to protect memory consistency, we need to make sure calls to uvcg_complete_buffer are synchronized on something other than the usb controller.

If it's simply to prevent double calling uvcg_complete_buffer() for a buffer, is holding the irqlock necessary? Both uvc_video_complete() and uvc_video_disable() conditionally call uvcg_complete_buffer() based on whether ureq->last_buf is set or not, and both functions assess that whilst holding req_lock, so unless I'm missing something that situation is already guarded against.

Even without holding queue->irqlock, we guaratee that uvcg_complete_buffer() is only called once per buffer (by querying last_buf with req_lock held). However, uvcg_complete_buffer modifies queue->flags which in honesty will not cause any problems, but is promlematic from theoretical correctness standpoint.

Either way, this shouldn't be too much overhead, as this is only called once per frame, and most requests don't have last_buf set anyway. Let me know if you feel strongly about not having the locks there, and I can drop it.

Quoted 170 lines
Quoted 107 lines
Quoted 107 lines
Quoted 107 lines
drivers/usb/gadget/function/uvc_video.c +12 -4
        }
 
        spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
-    spin_unlock_irqrestore(&video->req_lock, flags);
-
-    if (uvc->state == UVC_STATE_STREAMING)
+    /*
+     * Video stream might have been disabled while we were
+     * processing the current usb_request. So make sure
+     * we're still streaming before queueing the usb_request
+     * back to req_free
+     */
+    if (video->is_enabled) {
+        list_add_tail(&req->list, &video->req_free);
            queue_work(video->async_wq, &video->pump);
+    } else {
+        uvc_video_free_request(ureq, ep);
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
    }
 

   static int

drivers/usb/gadget/function/uvc_video.c +11 -7
@@ -393,20 +421,22 @@ static void uvcg_video_pump(struct work_struct *work)
        struct uvc_video_queue *queue = &video->queue;
        /* video->max_payload_size is only set when using bulk transfer */
        bool is_bulk = video->max_payload_size;
-    struct uvc_device *uvc = video->uvc;
        struct usb_request *req = NULL;
        struct uvc_buffer *buf;
        unsigned long flags;
        bool buf_done;
        int ret;
 
-    while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+    while (true) {
+        if (!video->ep->enabled)
+            return;
+
            /*
-         * Retrieve the first available USB request, protected by the
-         * request lock.
+         * Check is_enabled and retrieve the first available USB
+         * request, protected by the request lock.
             */
            spin_lock_irqsave(&video->req_lock, flags);
-        if (list_empty(&video->req_free)) {
+        if (!video->is_enabled || list_empty(&video->req_free)) {
                spin_unlock_irqrestore(&video->req_lock, flags);
                return;
            }
@@ -488,9 +518,11 @@ static void uvcg_video_pump(struct work_struct *work)
            return;
 
        spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
+    if (video->is_enabled)
+        list_add_tail(&req->list, &video->req_free);
+    else
+        uvc_video_free_request(req->context, video->ep);
        spin_unlock_irqrestore(&video->req_lock, flags);
-    return;
    }
 
    /*
@@ -499,7 +531,11 @@ static void uvcg_video_pump(struct work_struct *work)

   int
   uvcg_video_disable(struct uvc_video *video)
   {
-    struct uvc_request *ureq;
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;

       if (video->ep == NULL) {
           uvcg_info(&video->uvc->func,

drivers/usb/gadget/function/uvc_video.c +19 -0
@@ -507,15 +543,58 @@ uvcg_video_disable(struct uvc_video *video)
            return -ENODEV;
        }
 
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
        cancel_work_sync(&video->pump);
        uvcg_queue_cancel(&video->queue, 0);
 
Quoted 6 lines
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 570

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +3 -3
-    list_for_each_entry(ureq, &video->ureqs, list) {
-        if (ureq->req)
-            usb_ep_dequeue(video->ep, ureq->req);
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from ureqs with list_del_init

s/uvc_reqeusts/uvc_requests

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
drivers/usb/gadget/function/uvc_video.c +34 -1
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
        }
 
-    uvc_video_free_requests(video);
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
        uvcg_queue_enable(&video->queue, 0);
        return 0;
    }
@@ -533,6 +612,14 @@ int uvcg_video_enable(struct uvc_video *video)
            return -ENODEV;
        }
 
+    /*
+     * Safe to access request related fields without req_lock because
+     * this is the only thread currently active, and no other
+     * request handling thread will become active until this function
+     * returns.
+     */
+    video->is_enabled = true;
+
        if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
            return ret;
 
@@ -558,6 +645,7 @@ int uvcg_video_enable(struct uvc_video *video)
     */
    int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
    {
+    video->is_enabled = false;
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
        spin_lock_init(&video->req_lock);

--
2.42.0.820.g83a721a137-goog

#62 lore raw

Re: [PATCH v9 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 443 lines · Avichal Rakesh

Hey Dan, thank you for taking the time to review the patch!

Sent out v11 with some comments addressed:
https://lore.kernel.org/all/[email protected]/

Quoted 149 lines · Dan Scally

Hi Avichal

Quoted 92 lines · Avichal Rakesh

Thank you for taking a look Dan!

Quoted 83 lines · Dan Scally

Hi Avichal

Quoted 80 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
            https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock as discussed in
            https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable easier to review.
v6 -> v7: Fix warning reported in
            https://lore.kernel.org/[email protected]/
v7 -> v8: No change. Getting back in review queue
v8 -> v9: No change.

   drivers/usb/gadget/function/uvc.h       |   1 + drivers/usb/gadget/function/uvc_v4l2.c  |  12 +-- drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----

   3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
        unsigned int uvc_num_requests;
 
        /* Requests */
+    bool is_enabled; /* tracks whether video stream is enabled */
        unsigned int req_size;

       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ struct list_head req_free;

drivers/usb/gadget/function/uvc_v4l2.c +1 -2
index 904dd283cbf7..2f8634e05612 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
         * Complete the alternate setting selection setup phase now that
         * userspace is ready to provide video frames.
         */
-    uvc_function_setup_continue(uvc, 0);
        uvc->state = UVC_STATE_STREAMING;
+    uvc_function_setup_continue(uvc, 0);
 
        return 0;
    }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
        if (type != video->queue.queue.type)
            return -EINVAL;
 
-    uvc->state = UVC_STATE_CONNECTED;
        ret = uvcg_video_disable(video);
        if (ret < 0)
            return ret;
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) · line 478

Quoted 4 lines
drivers/usb/gadget/function/uvc_v4l2.c +1 -0
+    uvc->state = UVC_STATE_CONNECTED;
        uvc_function_setup_continue(uvc, 1);
        return 0;
    }

I'm not sure I understand what these re-orderings are for...can you explain please?

Quoted 7 lines

This specific one was a leftover from testing, removed this hunk. But the ones below are undoing the change in patch 1, which is flawed in its use of uvc->state without any memory guarantees.

So from patch 1 to patch 4, we shuffle the code around a bit, but this makes patch 1 somewhat complete and functional even if patch 4 were to be reverted.

Okedokey - that's fine (and good)

Quoted 43 lines
Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/function/uvc_v4l2.c +0 -8
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
    static void uvc_v4l2_disable(struct uvc_device *uvc)
    {
        uvc_function_disconnect(uvc);
-    /*
-     * Drop uvc->state to CONNECTED if it was streaming before.
-     * This ensures that the usb_requests are no longer queued
-     * to the controller.
-     */
-    if (uvc->state == UVC_STATE_STREAMING)
-        uvc->state = UVC_STATE_CONNECTED;
-
        uvcg_video_disable(&uvc->video);
        uvcg_free_buffers(&uvc->video.queue);
        uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 1081dd790fd6..8f330ce696ec 100644
@@ -227,6 +227,9 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
     * Request handling
     */
 
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 232

Quoted 3 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
+/*
+ * Must be called with req_lock held as it modifies the list ureq is held in
+ */

This comment probably belongs in patch #2. And in that case, shouldn't uvc_video_free_requests() hold the lock in that patch?

Patch 2 doesn't change any existing locking semantics. The current code does not enforce any locking on freeing the requests, and neither does patch 2.

Patch 4 introduces another call site for uvc_video_free_request, so some synchronization guarantees are needed (and hence the addition of this comment).

As for uvc_video_free_requests not holding the lock, it is safe because uvc_video_free_requests is only called if request initialization fails. So uvc_video_free_requests should be the thread safe, as no other thread is processing requests when it is called.

Quoted 5 lines
Quoted 2 lines

I did add a comment in uvcg_video_enable mentioning why it is safe to not hold req_free even though it accesses request related fields.

I understand, but in that case I think the comment is a little confusing - it's not the fact that the function modifies the list ureq is held in that requires it to be locked but that there're potentially multiple threads doing so. Can we go for something like "Callers must take care to hold req_lock when using this function outside of a single thread"? Feel free to re-word that however you like, as long as it's clear that it's only necessary when multiple threads could be active.

I see. I think having all the context made it obvious in my mind. Updated the comment to be more explicit!

Quoted 99 lines
Quoted 5 lines

Happy to add another comment to uvc_video_free_requests if that makes it clearer!

Quoted 9 lines
Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 277

Quoted 9 lines

   static void
   uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep) {

drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -271,9 +274,25 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        struct uvc_request *ureq = req->context;
        struct uvc_video *video = ureq->video;
        struct uvc_video_queue *queue = &video->queue;
-    struct uvc_device *uvc = video->uvc;
+    struct uvc_buffer *last_buf = NULL;

This initialisation's unnecessary since it's unconditionally set below.

Quoted 45 lines
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 283

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
        unsigned long flags;
 
+    spin_lock_irqsave(&video->req_lock, flags);
+    if (!video->is_enabled) {
+        /*
+         * When is_enabled is false, uvc_video_disable ensures that

s/uvc_video_disable/uvc_video_disable()

Done!

Quoted 13 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 287

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+         * in-flight uvc_buffers are returned, so we can safely
+         * call free_request without worrying about last_buf.
+         */
+        uvc_video_free_request(ureq, ep);

Now I understand the conditional in this function in patch 2 :)

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 294

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        spin_unlock_irqrestore(&video->req_lock, flags);
+        return;
+    }
+
+    last_buf = ureq->last_buf;
+    ureq->last_buf = NULL;
+    spin_unlock_irqrestore(&video->req_lock, flags);
Quoted 1 line

I'm not a huge fan of this locking, unlocking and relocking the same spinlock within the same function. Can we just hold the lock for the duration? if not, can there be an explanatory comment as to why?

I agree that this is a little unfortunate, and it'd be nice if we only had a single driver level lock. However, as it stands, if we hold req_lock for the entirety of completion handler, we risk two things:

1. Adding dependencies between queue->irqlock and video->reqlock
2. Starving the video_pump thread.

As of this patch, uvc_video_complete follows the same pattern as video_pump function:
1. Acquire req_lock
2. Fetch/Query usb_request
3. Drop req_lock

4. Acquire queue->irqlock
5. Buffer ops (encode/free/stop)
6. Drop queue->irqlock

7. Acquire req_lock
8. usb_request cleanup/handling
9. Drop req_lock

Quoted 9 lines

(7), (8), and (9) are optional for video_pump, while (4), (5), and (6) are optional for uvc_video_complete.

We can short-circuit uvc_video_complete with only one lock on the happy path, but this would have to be the flow for non-happy paths unless we want to hold the two locks at the same time (which isn't the worst idea, but comes with its own set of concerns).

Yeah. Sorry - I had missed that uvcg_video_pump() follows the same pattern too. Alright, I think leave this as-is.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
+
        switch (req->status) {

       case 0:
           break;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -295,17 +314,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
            uvcg_queue_cancel(queue, 0);
        }
 
Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 320

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +4 -3
-    if (ureq->last_buf) {
-        uvcg_complete_buffer(&video->queue, ureq->last_buf);
-        ureq->last_buf = NULL;
+    if (last_buf) {
+        spin_lock_irqsave(&queue->irqlock, flags);
+        uvcg_complete_buffer(&video->queue, last_buf);
+        spin_unlock_irqrestore(&queue->irqlock, flags);

I think it's right to take the irqlock here but it probably should have always been held, so this probably ought to go in its own commit with a Fixes:

Quoted 4 lines

The lock here wasn't required before, because uvcg_complete_buffer was only ever called by the completion handler, which is synchronized by the usb controller. This is the reason we never saw an issue despite not holding the lock.

Quoted 8 lines

Ah - I was misled by the "called with &queue_irqlock held..." comment on uvc_complete_buffer()...I assumed that meant "you must call this function with &queue->irqlock held", but that turns out to be something of a hangover - originally it was placed there by 95faf82bd3ea6 because the callers of (at the time uvc_queue_next_buffer(), later renamed to) uvcg_complete_buffer() already held the irqlock. Clearly it needed locking then, but the function at the time manipulated queue->irqqueue and now it no longer does - that part has been stripped out to encode_bulk/encode_isoc/encode_isoc_sg(). So, probably that comment ought to have been removed at some point.

Quoted 4 lines

This patch introduces another call site in uvcg_video_disable, so to protect memory consistency, we need to make sure calls to uvcg_complete_buffer are synchronized on something other than the usb controller.

If it's simply to prevent double calling uvcg_complete_buffer() for a buffer, is holding the irqlock necessary? Both uvc_video_complete() and uvc_video_disable() conditionally call uvcg_complete_buffer() based on whether ureq->last_buf is set or not, and both functions assess that whilst holding req_lock, so unless I'm missing something that situation is already guarded against.

Even without holding queue->irqlock, we guaratee that uvcg_complete_buffer() is only called once per buffer (by querying last_buf with req_lock held). However, uvcg_complete_buffer modifies queue->flags which in honesty will not cause any problems, but is promlematic from theoretical correctness standpoint.

Either way, this shouldn't be too much overhead, as this is only called once per frame, and most requests don't have last_buf set anyway. Let me know if you feel strongly about not having the locks there, and I can drop it.

Quoted 160 lines
Quoted 107 lines
Quoted 107 lines
Quoted 107 lines
drivers/usb/gadget/function/uvc_video.c +12 -4
        }
 
        spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
-    spin_unlock_irqrestore(&video->req_lock, flags);
-
-    if (uvc->state == UVC_STATE_STREAMING)
+    /*
+     * Video stream might have been disabled while we were
+     * processing the current usb_request. So make sure
+     * we're still streaming before queueing the usb_request
+     * back to req_free
+     */
+    if (video->is_enabled) {
+        list_add_tail(&req->list, &video->req_free);
            queue_work(video->async_wq, &video->pump);
+    } else {
+        uvc_video_free_request(ureq, ep);
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
    }
 

   static int

drivers/usb/gadget/function/uvc_video.c +11 -7
@@ -393,20 +421,22 @@ static void uvcg_video_pump(struct work_struct *work)
        struct uvc_video_queue *queue = &video->queue;
        /* video->max_payload_size is only set when using bulk transfer */
        bool is_bulk = video->max_payload_size;
-    struct uvc_device *uvc = video->uvc;
        struct usb_request *req = NULL;
        struct uvc_buffer *buf;
        unsigned long flags;
        bool buf_done;
        int ret;
 
-    while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+    while (true) {
+        if (!video->ep->enabled)
+            return;
+
            /*
-         * Retrieve the first available USB request, protected by the
-         * request lock.
+         * Check is_enabled and retrieve the first available USB
+         * request, protected by the request lock.
             */
            spin_lock_irqsave(&video->req_lock, flags);
-        if (list_empty(&video->req_free)) {
+        if (!video->is_enabled || list_empty(&video->req_free)) {
                spin_unlock_irqrestore(&video->req_lock, flags);
                return;
            }
@@ -488,9 +518,11 @@ static void uvcg_video_pump(struct work_struct *work)
            return;
 
        spin_lock_irqsave(&video->req_lock, flags);
-    list_add_tail(&req->list, &video->req_free);
+    if (video->is_enabled)
+        list_add_tail(&req->list, &video->req_free);
+    else
+        uvc_video_free_request(req->context, video->ep);
        spin_unlock_irqrestore(&video->req_lock, flags);
-    return;
    }
 
    /*
@@ -499,7 +531,11 @@ static void uvcg_video_pump(struct work_struct *work)

   int
   uvcg_video_disable(struct uvc_video *video)
   {
-    struct uvc_request *ureq;
+    unsigned long flags;
+    struct list_head inflight_bufs;
+    struct usb_request *req, *temp;
+    struct uvc_buffer *buf, *btemp;
+    struct uvc_request *ureq, *utemp;

       if (video->ep == NULL) {
           uvcg_info(&video->uvc->func,

drivers/usb/gadget/function/uvc_video.c +19 -0
@@ -507,15 +543,58 @@ uvcg_video_disable(struct uvc_video *video)
            return -ENODEV;
        }
 
+    INIT_LIST_HEAD(&inflight_bufs);
+    spin_lock_irqsave(&video->req_lock, flags);
+    video->is_enabled = false;
+
+    /*
+     * Remove any in-flight buffers from the uvc_requests
+     * because we want to return them before cancelling the
+     * queue. This ensures that we aren't stuck waiting for
+     * all complete callbacks to come through before disabling
+     * vb2 queue.
+     */
+    list_for_each_entry(ureq, &video->ureqs, list) {
+        if (ureq->last_buf) {
+            list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+            ureq->last_buf = NULL;
+        }
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
        cancel_work_sync(&video->pump);
        uvcg_queue_cancel(&video->queue, 0);
 
Quoted 6 lines
Quoted 6 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 570

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +3 -3
-    list_for_each_entry(ureq, &video->ureqs, list) {
-        if (ureq->req)
-            usb_ep_dequeue(video->ep, ureq->req);
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Remove all uvc_reqeusts from ureqs with list_del_init

s/uvc_reqeusts/uvc_requests

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
drivers/usb/gadget/function/uvc_video.c +33 -1
+     * This lets uvc_video_free_request correctly identify
+     * if the uvc_request is attached to a list or not when freeing
+     * memory.
+     */
+    list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+        list_del_init(&ureq->list);
+
+    list_for_each_entry_safe(req, temp, &video->req_free, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
        }
 
-    uvc_video_free_requests(video);
+    INIT_LIST_HEAD(&video->ureqs);
+    INIT_LIST_HEAD(&video->req_free);
+    video->req_size = 0;
+    spin_unlock_irqrestore(&video->req_lock, flags);
+
+    /*
+     * Return all the video buffers before disabling the queue.
+     */
+    spin_lock_irqsave(&video->queue.irqlock, flags);
+    list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+        list_del(&buf->queue);
+        uvcg_complete_buffer(&video->queue, buf);
+    }
+    spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
        uvcg_queue_enable(&video->queue, 0);
        return 0;
    }
@@ -533,6 +612,14 @@ int uvcg_video_enable(struct uvc_video *video)
            return -ENODEV;
        }
 
+    /*
+     * Safe to access request related fields without req_lock because
+     * this is the only thread currently active, and no other
+     * request handling thread will become active until this function
+     * returns.
+     */
+    video->is_enabled = true;
+
        if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
            return ret;
 
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -558,6 +645,7 @@ int uvcg_video_enable(struct uvc_video *video)
     */
    int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
    {
+    video->is_enabled = false;
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
        spin_lock_init(&video->req_lock);

--
2.42.0.820.g83a721a137-goog

Hey Dan, let me know if Patch v11 looks good.

Thank you!
- Avi.

#63 lore raw

PATCH v10 1/4 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2 : Rebased to ToT and reworded commit message. v2 -> v3 : Fix email threading goof-up
v3 -> v4 : Address review comments & re-rebase to ToT v4 -> v5 : Add Reviewed-by & Tested-by
v5 -> v6 : No change
v6 -> v7 : No change
v7 -> v8 : No change. Getting back in review queue v8 -> v9 : Fix typo. No functional change.
v9 -> v10: Rebase to ToT (usb-next)

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index 786379f1b7b7..77999ed53d33 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..083aef0c65c6 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.820.g83a721a137-goog

#64 lore raw

PATCH v10 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2 : Rebased to ToT
v2 -> v3 : Fix email threading goof-up
v3 -> v4 : Address review comments & re-rebase to ToT
v4 -> v5 : Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6 : No change
v6 -> v7 : No change
v7 -> v8 : No change. Getting back in review queue
v8 -> v9 : Address review comments.
v9 -> v10: Address review comments; remove BUG_ON(&video->reqs);
Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 88 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -39

140 lines not on this page · raw message

--
2.42.0.820.g83a721a137-goog

Reviewed-by on v10 2/4
#65 lore raw

Re: [PATCH v10 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Morning Avichal

Quoted 13 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 5 lines

Thanks for the update; this seems ok now:

Quoted 190 lines

---
v1 -> v2 : Rebased to ToT
v2 -> v3 : Fix email threading goof-up
v3 -> v4 : Address review comments & re-rebase to ToT
v4 -> v5 : Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6 : No change
v6 -> v7 : No change
v7 -> v8 : No change. Getting back in review queue
v8 -> v9 : Address review comments.
v9 -> v10: Address review comments; remove BUG_ON(&video->reqs);
Rebase to ToT (usb-next)

  drivers/usb/gadget/function/uvc.h       |  3 +-
  drivers/usb/gadget/function/uvc_video.c | 88 ++++++++++++++-----------
  2 files changed, 51 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
  	struct sg_table sgt;
  	u8 header[UVCG_REQUEST_HEADER_LEN];
  	struct uvc_buffer *last_buf;
+	struct list_head list;
  };
 
  struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
  	/* Requests */
  	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
  	struct list_head req_free;
  	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -40
index c334802ac0a4..1619f9664748 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
   * Request handling
   */
 
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+	sg_free_table(&ureq->sgt);
+	if (ureq->req && ep) {
+		usb_ep_free_request(ep, ureq->req);
+		ureq->req = NULL;
+	}
+
+	kfree(ureq->req_buffer);
+	ureq->req_buffer = NULL;
+
+	if (!list_empty(&ureq->list))
+		list_del_init(&ureq->list);
+
+	kfree(ureq);
+}
+
  static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  {
  	int ret;
@@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  static int
  uvc_video_free_requests(struct uvc_video *video)
  {
-	unsigned int i;
-
-	if (video->ureq) {
-		for (i = 0; i < video->uvc_num_requests; ++i) {
-			sg_free_table(&video->ureq[i].sgt);
+	struct uvc_request *ureq, *temp;
 
-			if (video->ureq[i].req) {
-				usb_ep_free_request(video->ep, video->ureq[i].req);
-				video->ureq[i].req = NULL;
-			}
-
-			if (video->ureq[i].req_buffer) {
-				kfree(video->ureq[i].req_buffer);
-				video->ureq[i].req_buffer = NULL;
-			}
-		}
-
-		kfree(video->ureq);
-		video->ureq = NULL;
-	}
+	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
+		uvc_video_free_request(ureq, video->ep);
 
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	video->req_size = 0;
  	return 0;
@@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video)
  static int
  uvc_video_alloc_requests(struct uvc_video *video)
  {
+	struct uvc_request *ureq;
  	unsigned int req_size;
  	unsigned int i;
  	int ret = -ENOMEM;
@@ -332,29 +336,33 @@ uvc_video_alloc_requests(struct uvc_video *video)
  		 * max_t(unsigned int, video->ep->maxburst, 1)
  		 * (video->ep->mult);
 
-	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-	if (video->ureq == NULL)
-		return -ENOMEM;
+	for (i = 0; i < video->uvc_num_requests; i++) {
+		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+		if (ureq == NULL)
+			goto error;
+
+		INIT_LIST_HEAD(&ureq->list);
+
+		list_add_tail(&ureq->list, &video->ureqs);
 
-	for (i = 0; i < video->uvc_num_requests; ++i) {
-		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-		if (video->ureq[i].req_buffer == NULL)
+		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+		if (ureq->req_buffer == NULL)
  			goto error;
 
-		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
-		if (video->ureq[i].req == NULL)
+		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
+		if (ureq->req == NULL)
  			goto error;
 
-		video->ureq[i].req->buf = video->ureq[i].req_buffer;
-		video->ureq[i].req->length = 0;
-		video->ureq[i].req->complete = uvc_video_complete;
-		video->ureq[i].req->context = &video->ureq[i];
-		video->ureq[i].video = video;
-		video->ureq[i].last_buf = NULL;
+		ureq->req->buf = ureq->req_buffer;
+		ureq->req->length = 0;
+		ureq->req->complete = uvc_video_complete;
+		ureq->req->context = ureq;
+		ureq->video = video;
+		ureq->last_buf = NULL;
 
-		list_add_tail(&video->ureq[i].req->list, &video->req_free);
+		list_add_tail(&ureq->req->list, &video->req_free);
  		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
-		sg_alloc_table(&video->ureq[i].sgt,
+		sg_alloc_table(&ureq->sgt,
  			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
  					    PAGE_SIZE) + 2, GFP_KERNEL);
  	}
@@ -489,8 +497,8 @@ static void uvcg_video_pump(struct work_struct *work)
   */
  int uvcg_video_enable(struct uvc_video *video, int enable)
  {
-	unsigned int i;
  	int ret;
+	struct uvc_request *ureq;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -502,9 +510,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  		cancel_work_sync(&video->pump);
  		uvcg_queue_cancel(&video->queue, 0);
 
-		for (i = 0; i < video->uvc_num_requests; ++i)
-			if (video->ureq && video->ureq[i].req)
-				usb_ep_dequeue(video->ep, video->ureq[i].req);
+		list_for_each_entry(ureq, &video->ureqs, list) {
+			if (ureq->req)
+				usb_ep_dequeue(video->ep, ureq->req);
+		}
 
  		uvc_video_free_requests(video);
  		uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +545,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
   */
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  {
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	spin_lock_init(&video->req_lock);
  	INIT_WORK(&video->pump, uvcg_video_pump);
--

2.42.0.820.g83a721a137-goog

#66 lore raw

Re: [PATCH v10 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 23 lines · Dan Scally

Morning Avichal

Quoted 13 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 5 lines
Quoted 4 lines

Thanks for the update; this seems ok now:

Awesome, thank you! I'll add the Reviewed-by in the next patchset (assuming you have more review comments on patch 4/4 v10).

Regards,
Avi.

Quoted 14 lines
Quoted 14 lines

---
v1 -> v2 : Rebased to ToT
v2 -> v3 : Fix email threading goof-up
v3 -> v4 : Address review comments & re-rebase to ToT
v4 -> v5 : Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6 : No change
v6 -> v7 : No change
v7 -> v8 : No change. Getting back in review queue
v8 -> v9 : Address review comments.
v9 -> v10: Address review comments; remove BUG_ON(&video->reqs); Rebase to ToT (usb-next)

  <snip>

#67 lore raw

Re: [PATCH v10 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 24 lines · Avichal Rakesh
Quoted 22 lines · Dan Scally

Morning Avichal

Quoted 18 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

Quoted 6 lines
Quoted 4 lines

Thanks for the update; this seems ok now:

Awesome, thank you! I'll add the Reviewed-by in the next patchset (assuming you have more review comments on patch 4/4 v10).

Sorry yes - taking me a while to wrap my head around everything but I hope to be done shortly!

Quoted 17 lines

Regards,
Avi.

Quoted 13 lines
Quoted 13 lines

---
v1 -> v2 : Rebased to ToT
v2 -> v3 : Fix email threading goof-up
v3 -> v4 : Address review comments & re-rebase to ToT
v4 -> v5 : Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6 : No change
v6 -> v7 : No change
v7 -> v8 : No change. Getting back in review queue
v8 -> v9 : Address review comments.
v9 -> v10: Address review comments; remove BUG_ON(&video->reqs);
            Rebase to ToT (usb-next)

  <snip>

#68 lore raw

PATCH v10 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'. This function is now used anywhere uvcg_video_enable(video, 0) was used.

---
v6 : Introduced this patch to make the next one easier to review
v6 -> v7 : Add Suggested-by
v7 -> v8 : No change. Getting back in review queue
v8 -> v9 : Call uvcg_video_disable directly instead of uvcg_video_enable(video, 0) v9 -> v10: Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc_v4l2.c  |  6 ++--
 drivers/usb/gadget/function/uvc_video.c | 40 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  3 +-
 3 files changed, 31 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/uvc_v4l2.c +3 -3
index 7cb8d027ff0c..904dd283cbf7 100644
@@ -443,7 +443,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	/* Enable UVC video. */
-	ret = uvcg_video_enable(video, 1);
+	ret = uvcg_video_enable(video);
 	if (ret < 0)
 		return ret;
 
@@ -469,7 +469,7 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	uvc->state = UVC_STATE_CONNECTED;
-	ret = uvcg_video_enable(video, 0);
+	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
@@ -515,7 +515,7 @@ static void uvc_v4l2_disable(struct uvc_device *uvc)
 	if (uvc->state == UVC_STATE_STREAMING)
 		uvc->state = UVC_STATE_CONNECTED;
 
-	uvcg_video_enable(&uvc->video, 0);
+	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
 	wake_up_interruptible(&uvc->func_connected_queue);
drivers/usb/gadget/function/uvc_video.c +26 -14
index 1619f9664748..c3e8c48f46a9 100644
@@ -493,31 +493,43 @@ static void uvcg_video_pump(struct work_struct *work)
 }
 
 /*
- * Enable or disable the video stream.
+ * Disable the video stream
  */
-int uvcg_video_enable(struct uvc_video *video, int enable)
+int
+uvcg_video_disable(struct uvc_video *video)
 {
-	int ret;
 	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
-			  "Video enable failed, device is uninitialized.\n");
+			  "Video disable failed, device is uninitialized.\n");
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
 
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
 
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
+/*
+ * Enable the video stream.
+ */
+int uvcg_video_enable(struct uvc_video *video)
+{
+	int ret;
+
+	if (video->ep == NULL) {
+		uvcg_info(&video->uvc->func,
+			  "Video enable failed, device is uninitialized.\n");
+		return -ENODEV;
 	}
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
drivers/usb/gadget/function/uvc_video.h +2 -1
index 03adeefa343b..8ef6259741f1 100644
@@ -14,7 +14,8 @@
 
 struct uvc_video;
 
-int uvcg_video_enable(struct uvc_video *video, int enable);
+int uvcg_video_enable(struct uvc_video *video);
+int uvcg_video_disable(struct uvc_video *video);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 

--
2.42.0.820.g83a721a137-goog

#69 lore raw

PATCH v10 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2 : Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3 : Fix email threading goof-up
v3 -> v4 : re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5 : Address review comments. Add Reviewed-by & Tested-by. v5 -> v6 : Added another patch before this one to make uvcg_video_disable
easier to review.
v6 -> v7 : Fix warning reported in

           https://lore.kernel.org/[email protected]/

v7 -> v8 : No change. Getting back in review queue v8 -> v9 : No change.
v9 -> v10: Address review comments. Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  10 +-
 drivers/usb/gadget/function/uvc_video.c | 129 ++++++++++++++++++++----
 3 files changed, 111 insertions(+), 29 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -9
index 904dd283cbf7..c7e5fa4f29e0 100644
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +109 -20

204 lines not on this page · raw message

--
2.42.0.820.g83a721a137-goog

#70 lore raw

PATCH v11 1/4 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---

v1  -> v2  : Rebased to ToT and reworded commit message.
v2  -> v3  : Fix email threading goof-up
v3  -> v4  : Address review comments & re-rebase to ToT
v4  -> v5  : Add Reviewed-by & Tested-by
v5  -> v6  : No change
v6  -> v7  : No change
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Fix typo. No functional change.
v9  -> v10 : Rebase to ToT (usb-next)

v10 -> v11 : No change

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index 786379f1b7b7..77999ed53d33 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..083aef0c65c6 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.869.gea05f2083d-goog

#71 lore raw

PATCH v11 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---

v1  -> v2  : Rebased to ToT
v2  -> v3  : Fix email threading goof-up
v3  -> v4  : Address review comments & re-rebase to ToT
v4  -> v5  : Address more review comments. Add Reviewed-by & Tested-by.
v5  -> v6  : No change
v6  -> v7  : No change
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Address review comments.
v9  -> v10 : Address review comments; remove BUG_ON(&video->reqs);

Rebase to ToT (usb-next)
v10 -> v11 : Add Reviewed-by

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 88 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -39

140 lines not on this page · raw message

--
2.42.0.869.gea05f2083d-goog

#72 lore raw

PATCH v11 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'. This function is now used anywhere uvcg_video_enable(video, 0) was used.

---

XX  -> v6  : Introduced this patch to make the next one easier to review
v6  -> v7  : Add Suggested-by
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Call uvcg_video_disable directly instead of uvcg_video_enable(video, 0)
v9  -> v10 : Rebase to ToT (usb-next)

v10 -> v11 : No change

 drivers/usb/gadget/function/uvc_v4l2.c  |  6 ++--
 drivers/usb/gadget/function/uvc_video.c | 40 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  3 +-
 3 files changed, 31 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/uvc_v4l2.c +3 -3
index 7cb8d027ff0c..904dd283cbf7 100644
@@ -443,7 +443,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	/* Enable UVC video. */
-	ret = uvcg_video_enable(video, 1);
+	ret = uvcg_video_enable(video);
 	if (ret < 0)
 		return ret;
 
@@ -469,7 +469,7 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	uvc->state = UVC_STATE_CONNECTED;
-	ret = uvcg_video_enable(video, 0);
+	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
@@ -515,7 +515,7 @@ static void uvc_v4l2_disable(struct uvc_device *uvc)
 	if (uvc->state == UVC_STATE_STREAMING)
 		uvc->state = UVC_STATE_CONNECTED;
 
-	uvcg_video_enable(&uvc->video, 0);
+	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
 	wake_up_interruptible(&uvc->func_connected_queue);
drivers/usb/gadget/function/uvc_video.c +26 -14
index 1619f9664748..c3e8c48f46a9 100644
@@ -493,31 +493,43 @@ static void uvcg_video_pump(struct work_struct *work)
 }
 
 /*
- * Enable or disable the video stream.
+ * Disable the video stream
  */
-int uvcg_video_enable(struct uvc_video *video, int enable)
+int
+uvcg_video_disable(struct uvc_video *video)
 {
-	int ret;
 	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
-			  "Video enable failed, device is uninitialized.\n");
+			  "Video disable failed, device is uninitialized.\n");
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
 
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
 
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
+/*
+ * Enable the video stream.
+ */
+int uvcg_video_enable(struct uvc_video *video)
+{
+	int ret;
+
+	if (video->ep == NULL) {
+		uvcg_info(&video->uvc->func,
+			  "Video enable failed, device is uninitialized.\n");
+		return -ENODEV;
 	}
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
drivers/usb/gadget/function/uvc_video.h +2 -1
index 03adeefa343b..8ef6259741f1 100644
@@ -14,7 +14,8 @@
 
 struct uvc_video;
 
-int uvcg_video_enable(struct uvc_video *video, int enable);
+int uvcg_video_enable(struct uvc_video *video);
+int uvcg_video_disable(struct uvc_video *video);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 

--
2.42.0.869.gea05f2083d-goog

#73 lore raw

PATCH v11 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2 : Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/

v2  -> v3  : Fix email threading goof-up
v3  -> v4  : re-rebase to ToT & moved to a uvc_video level lock
             as discussed in
             https://lore.kernel.org/[email protected]/
v4  -> v5  : Address review comments. Add Reviewed-by & Tested-by.
v5  -> v6  : Added another patch before this one to make uvcg_video_disable
             easier to review.
v6  -> v7  : Fix warning reported in
             https://lore.kernel.org/[email protected]/
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : No change.
v9  -> v10 : Address review comments. Rebase to ToT (usb-next)

v10 -> v11 : Address review comments

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  10 +-
 drivers/usb/gadget/function/uvc_video.c | 130 ++++++++++++++++++++----
 3 files changed, 112 insertions(+), 29 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -9
index 904dd283cbf7..c7e5fa4f29e0 100644
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +110 -20

205 lines not on this page · raw message

--
2.42.0.869.gea05f2083d-goog

Reviewed-by on v11 4/4
#74 lore raw

Re: [PATCH v11 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Hi Avichal

Quoted 20 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 6 lines

---

Thanks for the update. Let's leave the locking as it is; I think albeit not strictly necessary on that occasion it certainly is necessary to take the lock to protect the flags elsewhere, and probably better to be consistent with it.

Quoted 285 lines

v1 -> v2 : Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/

v2  -> v3  : Fix email threading goof-up
v3  -> v4  : re-rebase to ToT & moved to a uvc_video level lock
              as discussed in
              https://lore.kernel.org/[email protected]/
v4  -> v5  : Address review comments. Add Reviewed-by & Tested-by.
v5  -> v6  : Added another patch before this one to make uvcg_video_disable
              easier to review.
v6  -> v7  : Fix warning reported in
              https://lore.kernel.org/[email protected]/
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : No change.
v9  -> v10 : Address review comments. Rebase to ToT (usb-next)

v10 -> v11 : Address review comments

  drivers/usb/gadget/function/uvc.h       |   1 +
  drivers/usb/gadget/function/uvc_v4l2.c  |  10 +-
  drivers/usb/gadget/function/uvc_video.c | 130 ++++++++++++++++++++----
  3 files changed, 112 insertions(+), 29 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
  	unsigned int uvc_num_requests;
 
  	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
  	unsigned int req_size;
  	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
  	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -9
index 904dd283cbf7..c7e5fa4f29e0 100644
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  	if (type != video->queue.queue.type)
  		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
  	ret = uvcg_video_disable(video);
  	if (ret < 0)
  		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
  	uvc_function_setup_continue(uvc, 1);
  	return 0;
  }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
  static void uvc_v4l2_disable(struct uvc_device *uvc)
  {
  	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
  	uvcg_video_disable(&uvc->video);
  	uvcg_free_buffers(&uvc->video.queue);
  	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +110 -21
index c3e8c48f46a9..164bdeb7f2a9 100644
@@ -227,6 +227,10 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
   * Request handling
   */
 
+/*
+ * Callers must take care to hold req_lock when this function may be called
+ * from multiple threads. For example, when frames are streaming to the host.
+ */
  static void
  uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
  {
@@ -271,9 +275,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	struct uvc_request *ureq = req->context;
  	struct uvc_video *video = ureq->video;
  	struct uvc_video_queue *queue = &video->queue;
-	struct uvc_device *uvc = video->uvc;
+	struct uvc_buffer *last_buf;
  	unsigned long flags;
 
+	spin_lock_irqsave(&video->req_lock, flags);
+	if (!video->is_enabled) {
+		/*
+		 * When is_enabled is false, uvcg_video_disable() ensures
+		 * that in-flight uvc_buffers are returned, so we can
+		 * safely call free_request without worrying about
+		 * last_buf.
+		 */
+		uvc_video_free_request(ureq, ep);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		return;
+	}
+
+	last_buf = ureq->last_buf;
+	ureq->last_buf = NULL;
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
  	switch (req->status) {
  	case 0:
  		break;
@@ -295,17 +316,26 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  		uvcg_queue_cancel(queue, 0);
  	}
 
-	if (ureq->last_buf) {
-		uvcg_complete_buffer(&video->queue, ureq->last_buf);
-		ureq->last_buf = NULL;
+	if (last_buf) {
+		spin_lock_irqsave(&queue->irqlock, flags);
+		uvcg_complete_buffer(queue, last_buf);
+		spin_unlock_irqrestore(&queue->irqlock, flags);
  	}
 
  	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
-	spin_unlock_irqrestore(&video->req_lock, flags);
-
-	if (uvc->state == UVC_STATE_STREAMING)
+	/*
+	 * Video stream might have been disabled while we were
+	 * processing the current usb_request. So make sure
+	 * we're still streaming before queueing the usb_request
+	 * back to req_free
+	 */
+	if (video->is_enabled) {
+		list_add_tail(&req->list, &video->req_free);
  		queue_work(video->async_wq, &video->pump);
+	} else {
+		uvc_video_free_request(ureq, ep);
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
  }
 
  static int
@@ -392,20 +422,22 @@ static void uvcg_video_pump(struct work_struct *work)
  	struct uvc_video_queue *queue = &video->queue;
  	/* video->max_payload_size is only set when using bulk transfer */
  	bool is_bulk = video->max_payload_size;
-	struct uvc_device *uvc = video->uvc;
  	struct usb_request *req = NULL;
  	struct uvc_buffer *buf;
  	unsigned long flags;
  	bool buf_done;
  	int ret;
 
-	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
+	while (true) {
+		if (!video->ep->enabled)
+			return;
+
  		/*
-		 * Retrieve the first available USB request, protected by the
-		 * request lock.
+		 * Check is_enabled and retrieve the first available USB
+		 * request, protected by the request lock.
  		 */
  		spin_lock_irqsave(&video->req_lock, flags);
-		if (list_empty(&video->req_free)) {
+		if (!video->is_enabled || list_empty(&video->req_free)) {
  			spin_unlock_irqrestore(&video->req_lock, flags);
  			return;
  		}
@@ -487,9 +519,11 @@ static void uvcg_video_pump(struct work_struct *work)
  		return;
 
  	spin_lock_irqsave(&video->req_lock, flags);
-	list_add_tail(&req->list, &video->req_free);
+	if (video->is_enabled)
+		list_add_tail(&req->list, &video->req_free);
+	else
+		uvc_video_free_request(req->context, video->ep);
  	spin_unlock_irqrestore(&video->req_lock, flags);
-	return;
  }
 
  /*
@@ -498,7 +532,11 @@ static void uvcg_video_pump(struct work_struct *work)
  int
  uvcg_video_disable(struct uvc_video *video)
  {
-	struct uvc_request *ureq;
+	unsigned long flags;
+	struct list_head inflight_bufs;
+	struct usb_request *req, *temp;
+	struct uvc_buffer *buf, *btemp;
+	struct uvc_request *ureq, *utemp;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -506,15 +544,58 @@ uvcg_video_disable(struct uvc_video *video)
  		return -ENODEV;
  	}
 
+	INIT_LIST_HEAD(&inflight_bufs);
+	spin_lock_irqsave(&video->req_lock, flags);
+	video->is_enabled = false;
+
+	/*
+	 * Remove any in-flight buffers from the uvc_requests
+	 * because we want to return them before cancelling the
+	 * queue. This ensures that we aren't stuck waiting for
+	 * all complete callbacks to come through before disabling
+	 * vb2 queue.
+	 */
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->last_buf) {
+			list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
+			ureq->last_buf = NULL;
+		}
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
  	cancel_work_sync(&video->pump);
  	uvcg_queue_cancel(&video->queue, 0);
 
-	list_for_each_entry(ureq, &video->ureqs, list) {
-		if (ureq->req)
-			usb_ep_dequeue(video->ep, ureq->req);
+	spin_lock_irqsave(&video->req_lock, flags);
+	/*
+	 * Remove all uvc_requests from ureqs with list_del_init
+	 * This lets uvc_video_free_request correctly identify
+	 * if the uvc_request is attached to a list or not when freeing
+	 * memory.
+	 */
+	list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
+		list_del_init(&ureq->list);
+
+	list_for_each_entry_safe(req, temp, &video->req_free, list) {
+		list_del(&req->list);
+		uvc_video_free_request(req->context, video->ep);
  	}
 
-	uvc_video_free_requests(video);
+	INIT_LIST_HEAD(&video->ureqs);
+	INIT_LIST_HEAD(&video->req_free);
+	video->req_size = 0;
+	spin_unlock_irqrestore(&video->req_lock, flags);
+
+	/*
+	 * Return all the video buffers before disabling the queue.
+	 */
+	spin_lock_irqsave(&video->queue.irqlock, flags);
+	list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
+		list_del(&buf->queue);
+		uvcg_complete_buffer(&video->queue, buf);
+	}
+	spin_unlock_irqrestore(&video->queue.irqlock, flags);
+
  	uvcg_queue_enable(&video->queue, 0);
  	return 0;
  }
@@ -532,6 +613,14 @@ int uvcg_video_enable(struct uvc_video *video)
  		return -ENODEV;
  	}
 
+	/*
+	 * Safe to access request related fields without req_lock because
+	 * this is the only thread currently active, and no other
+	 * request handling thread will become active until this function
+	 * returns.
+	 */
+	video->is_enabled = true;
+
  	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  		return ret;
 
@@ -557,6 +646,7 @@ int uvcg_video_enable(struct uvc_video *video)
   */
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  {
+	video->is_enabled = false;
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	spin_lock_init(&video->req_lock);
--

2.42.0.869.gea05f2083d-goog

#75 lore raw

Re: [PATCH v11 4/4] usb: gadget: uvc: Fix use-after-free for inflight usb_requests

Quoted 31 lines · Dan Scally

Hi Avichal

Quoted 20 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

Quoted 6 lines

---

Quoted 4 lines

Thanks for the update. Let's leave the locking as it is; I think albeit not strictly necessary on that occasion it certainly is necessary to take the lock to protect the flags elsewhere, and probably better to be consistent with it.

Thank you for reviewing, Dan!

Greg, I just sent out v12 with the Reviewed-by tag:
https://lore.kernel.org/all/[email protected]/
They should be ready to submit now. Thank you!

Regards,
Avi.

Quoted 2 lines
Quoted 2 lines

<snip>

#76 lore raw

PATCH v12 1/4 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---

v1  -> v2  : Rebased to ToT and reworded commit message.
v2  -> v3  : Fix email threading goof-up
v3  -> v4  : Address review comments & re-rebase to ToT
v4  -> v5  : Add Reviewed-by & Tested-by
v5  -> v6  : No change
v6  -> v7  : No change
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Fix typo. No functional change.
v9  -> v10 : Rebase to ToT (usb-next)

v10 -> v11 : No change
v11 -> v12 : Rebase to ToT (usb-next)

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index 786379f1b7b7..77999ed53d33 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..083aef0c65c6 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.869.gea05f2083d-goog

#77 lore raw

PATCH v12 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---

v1  -> v2  : Rebased to ToT
v2  -> v3  : Fix email threading goof-up
v3  -> v4  : Address review comments & re-rebase to ToT
v4  -> v5  : Address more review comments. Add Reviewed-by & Tested-by.
v5  -> v6  : No change
v6  -> v7  : No change
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Address review comments.
v9  -> v10 : Address review comments; remove BUG_ON(&video->reqs);

Rebase to ToT (usb-next)
v10 -> v11 : Add Reviewed-by
v11 -> v12 : Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 88 ++++++++++++++-----------
 2 files changed, 51 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +49 -39

140 lines not on this page · raw message

--
2.42.0.869.gea05f2083d-goog

#78 lore raw

PATCH v12 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'. This function is now used anywhere uvcg_video_enable(video, 0) was used.

---

XX  -> v6  : Introduced this patch to make the next one easier to review
v6  -> v7  : Add Suggested-by
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Call uvcg_video_disable directly instead of uvcg_video_enable(video, 0)
v9  -> v10 : Rebase to ToT (usb-next)

v10 -> v11 : No change
v11 -> v12 : Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc_v4l2.c  |  6 ++--
 drivers/usb/gadget/function/uvc_video.c | 40 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  3 +-
 3 files changed, 31 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/uvc_v4l2.c +3 -3
index 7cb8d027ff0c..904dd283cbf7 100644
@@ -443,7 +443,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	/* Enable UVC video. */
-	ret = uvcg_video_enable(video, 1);
+	ret = uvcg_video_enable(video);
 	if (ret < 0)
 		return ret;
 
@@ -469,7 +469,7 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 		return -EINVAL;
 
 	uvc->state = UVC_STATE_CONNECTED;
-	ret = uvcg_video_enable(video, 0);
+	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
@@ -515,7 +515,7 @@ static void uvc_v4l2_disable(struct uvc_device *uvc)
 	if (uvc->state == UVC_STATE_STREAMING)
 		uvc->state = UVC_STATE_CONNECTED;
 
-	uvcg_video_enable(&uvc->video, 0);
+	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
 	wake_up_interruptible(&uvc->func_connected_queue);
drivers/usb/gadget/function/uvc_video.c +26 -14
index 1619f9664748..c3e8c48f46a9 100644
@@ -493,31 +493,43 @@ static void uvcg_video_pump(struct work_struct *work)
 }
 
 /*
- * Enable or disable the video stream.
+ * Disable the video stream
  */
-int uvcg_video_enable(struct uvc_video *video, int enable)
+int
+uvcg_video_disable(struct uvc_video *video)
 {
-	int ret;
 	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
-			  "Video enable failed, device is uninitialized.\n");
+			  "Video disable failed, device is uninitialized.\n");
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
 
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
 
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
+/*
+ * Enable the video stream.
+ */
+int uvcg_video_enable(struct uvc_video *video)
+{
+	int ret;
+
+	if (video->ep == NULL) {
+		uvcg_info(&video->uvc->func,
+			  "Video enable failed, device is uninitialized.\n");
+		return -ENODEV;
 	}
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
drivers/usb/gadget/function/uvc_video.h +2 -1
index 03adeefa343b..8ef6259741f1 100644
@@ -14,7 +14,8 @@
 
 struct uvc_video;
 
-int uvcg_video_enable(struct uvc_video *video, int enable);
+int uvcg_video_enable(struct uvc_video *video);
+int uvcg_video_disable(struct uvc_video *video);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 

--
2.42.0.869.gea05f2083d-goog

#79 lore raw

PATCH v12 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2 : Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/

v2  -> v3  : Fix email threading goof-up
v3  -> v4  : re-rebase to ToT & moved to a uvc_video level lock
             as discussed in
             https://lore.kernel.org/[email protected]/
v4  -> v5  : Address review comments. Add Reviewed-by & Tested-by.
v5  -> v6  : Added another patch before this one to make uvcg_video_disable
             easier to review.
v6  -> v7  : Fix warning reported in
             https://lore.kernel.org/[email protected]/
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : No change.
v9  -> v10 : Address review comments. Rebase to ToT (usb-next)

v10 -> v11 : Address review comments
v11 -> v12 : Add Reviewed-by; Rebase to ToT (usb-next)

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  10 +-
 drivers/usb/gadget/function/uvc_video.c | 130 ++++++++++++++++++++----
 3 files changed, 112 insertions(+), 29 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +1 -9
index 904dd283cbf7..c7e5fa4f29e0 100644
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_disable(video);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_disable(&uvc->video);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +110 -20

205 lines not on this page · raw message

--
2.42.0.869.gea05f2083d-goog

#80 lore raw

Re: [PATCH v12 1/4] usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Hey Greg, I think this patchset is ready for submission. Let me know if I am missing something and something else needs to be done.

Thank you!
- Avi

Quoted 174 lines · Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---

v1  -> v2  : Rebased to ToT and reworded commit message.
v2  -> v3  : Fix email threading goof-up
v3  -> v4  : Address review comments & re-rebase to ToT
v4  -> v5  : Add Reviewed-by & Tested-by
v5  -> v6  : No change
v6  -> v7  : No change
v7  -> v8  : No change. Getting back in review queue
v8  -> v9  : Fix typo. No functional change.
v9  -> v10 : Rebase to ToT (usb-next)

v10 -> v11 : No change
v11 -> v12 : Rebase to ToT (usb-next)

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index 786379f1b7b7..77999ed53d33 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..083aef0c65c6 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -2
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.
--

2.42.0.869.gea05f2083d-goog

#81 lore raw

PATCH v7 1/4 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change
v6 -> v7: No change

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.758.gaed0368e0e-goog

#82 lore raw

PATCH v7 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -39

141 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#83 lore raw

PATCH v7 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'.

---
v6: Introduced this patch to make the next one easier to review v6 -> v7: Add Suggested-by

 drivers/usb/gadget/function/uvc_video.c | 37 +++++++++++++++----------
 1 file changed, 23 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/uvc_video.c +23 -14
index c180866c8e34..80b8eaea2d39 100644
@@ -493,13 +493,33 @@ static void uvcg_video_pump(struct work_struct *work)
 	return;
 }
 
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video)
+{
+	struct uvc_request *ureq;
+
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
+
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
+
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
 /*
  * Enable or disable the video stream.
  */
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
 	int ret;
-	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
@@ -507,19 +527,8 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
-
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
-
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
-	}
+	if (!enable)
+		return uvcg_video_disable(video);
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
 		return ret;

--
2.42.0.758.gaed0368e0e-goog

#84 lore raw

PATCH v7 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
easier to review.
v6 -> v7: Fix warning reported in

          https://lore.kernel.org/[email protected]/
 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
 drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
 3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index 7cb8d027ff0c..f4d2e24835d4 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +108 -20

198 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#85 lore raw

PATCH v8 1/4 usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -1
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.

--
2.42.0.758.gaed0368e0e-goog

#86 lore raw

PATCH v8 2/4 usb: gadget: uvc: Allocate uvc_requests one at a time

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue

 drivers/usb/gadget/function/uvc.h       |  3 +-
 drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
 	struct sg_table sgt;
 	u8 header[UVCG_REQUEST_HEADER_LEN];
 	struct uvc_buffer *last_buf;
+	struct list_head list;
 };
 
 struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
 	/* Requests */
 	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
 	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +50 -39

141 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#87 lore raw

Re: [PATCH v8 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Hi Avichal - thanks for the patch

Quoted 63 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately.

---
v1 -> v2: Rebased to ToT
v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT
v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue

  drivers/usb/gadget/function/uvc.h       |  3 +-
  drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++-----------
  2 files changed, 52 insertions(+), 40 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -1
index 989bc6b4e93d..993694da0bbc 100644
@@ -81,6 +81,7 @@ struct uvc_request {
  	struct sg_table sgt;
  	u8 header[UVCG_REQUEST_HEADER_LEN];
  	struct uvc_buffer *last_buf;
+	struct list_head list;
  };
 
  struct uvc_video {
@@ -102,7 +103,7 @@ struct uvc_video {
 
  	/* Requests */
  	unsigned int req_size;
-	struct uvc_request *ureq;
+	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
  	struct list_head req_free;
  	spinlock_t req_lock;
 
drivers/usb/gadget/function/uvc_video.c +3 -0
index c334802ac0a4..c180866c8e34 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
   * Request handling
   */
 
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 242

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+	sg_free_table(&ureq->sgt);
+	if (ureq->req && ep) {
+		usb_ep_free_request(ep, ureq->req);
+		ureq->req = NULL;
+	}
+
+	kfree(ureq->req_buffer);
+	ureq->req_buffer = NULL;
+
+	if (!list_empty(&ureq->list))

Is this conditional needed? You can only get here through the list_for_each_entry_safe()

Quoted 49 lines
drivers/usb/gadget/function/uvc_video.c +10 -19
+		list_del_init(&ureq->list);
+
+	kfree(ureq);
+}
+
  static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  {
  	int ret;
@@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  static int
  uvc_video_free_requests(struct uvc_video *video)
  {
-	unsigned int i;
-
-	if (video->ureq) {
-		for (i = 0; i < video->uvc_num_requests; ++i) {
-			sg_free_table(&video->ureq[i].sgt);
+	struct uvc_request *ureq, *temp;
 
-			if (video->ureq[i].req) {
-				usb_ep_free_request(video->ep, video->ureq[i].req);
-				video->ureq[i].req = NULL;
-			}
-
-			if (video->ureq[i].req_buffer) {
-				kfree(video->ureq[i].req_buffer);
-				video->ureq[i].req_buffer = NULL;
-			}
-		}
-
-		kfree(video->ureq);
-		video->ureq = NULL;
-	}
+	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
+		uvc_video_free_request(ureq, video->ep);
 
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	video->req_size = 0;
  	return 0;
@@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video)
  static int
  uvc_video_alloc_requests(struct uvc_video *video)
  {
+	struct uvc_request *ureq;
  	unsigned int req_size;
  	unsigned int i;
  	int ret = -ENOMEM;

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 339

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +1 -3
@@ -332,29 +336,34 @@ uvc_video_alloc_requests(struct uvc_video *video)
  		 * max_t(unsigned int, video->ep->maxburst, 1)
  		 * (video->ep->mult);
 
-	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-	if (video->ureq == NULL)
-		return -ENOMEM;
+	INIT_LIST_HEAD(&video->ureqs);

Probably unecessary here; it's done in uvc_video_free_requests() and uvcg_video_init() already

Quoted 77 lines
drivers/usb/gadget/function/uvc_video.c +26 -18
+	for (i = 0; i < video->uvc_num_requests; i++) {
+		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+		if (ureq == NULL)
+			goto error;
+
+		INIT_LIST_HEAD(&ureq->list);
+
+		list_add_tail(&ureq->list, &video->ureqs);
 
-	for (i = 0; i < video->uvc_num_requests; ++i) {
-		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
-		if (video->ureq[i].req_buffer == NULL)
+		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
+		if (ureq->req_buffer == NULL)
  			goto error;
 
-		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
-		if (video->ureq[i].req == NULL)
+		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
+		if (ureq->req == NULL)
  			goto error;
 
-		video->ureq[i].req->buf = video->ureq[i].req_buffer;
-		video->ureq[i].req->length = 0;
-		video->ureq[i].req->complete = uvc_video_complete;
-		video->ureq[i].req->context = &video->ureq[i];
-		video->ureq[i].video = video;
-		video->ureq[i].last_buf = NULL;
+		ureq->req->buf = ureq->req_buffer;
+		ureq->req->length = 0;
+		ureq->req->complete = uvc_video_complete;
+		ureq->req->context = ureq;
+		ureq->video = video;
+		ureq->last_buf = NULL;
 
-		list_add_tail(&video->ureq[i].req->list, &video->req_free);
+		list_add_tail(&ureq->req->list, &video->req_free);
  		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
-		sg_alloc_table(&video->ureq[i].sgt,
+		sg_alloc_table(&ureq->sgt,
  			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
  					    PAGE_SIZE) + 2, GFP_KERNEL);
  	}
@@ -489,8 +498,8 @@ static void uvcg_video_pump(struct work_struct *work)
   */
  int uvcg_video_enable(struct uvc_video *video, int enable)
  {
-	unsigned int i;
  	int ret;
+	struct uvc_request *ureq;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -502,9 +511,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  		cancel_work_sync(&video->pump);
  		uvcg_queue_cancel(&video->queue, 0);
 
-		for (i = 0; i < video->uvc_num_requests; ++i)
-			if (video->ureq && video->ureq[i].req)
-				usb_ep_dequeue(video->ep, video->ureq[i].req);
+		list_for_each_entry(ureq, &video->ureqs, list) {
+			if (ureq->req)
+				usb_ep_dequeue(video->ep, ureq->req);
+		}
 
  		uvc_video_free_requests(video);
  		uvcg_queue_enable(&video->queue, 0);
@@ -536,6 +546,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
   */
  int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  {
+	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
  	spin_lock_init(&video->req_lock);
  	INIT_WORK(&video->pump, uvcg_video_pump);
--

2.42.0.758.gaed0368e0e-goog

#88 lore raw

Re: [PATCH v8 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Thank you for the reviews, Dan!

Uploaded v9 with the comments addressed.

Quoted 19 lines · Dan Scally

Hi Avichal - thanks for the patch

Quoted 16 lines · Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

<snip>

drivers/usb/gadget/function/uvc_video.c +0 -0
index c334802ac0a4..c180866c8e34 100644
@@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
    * Request handling
    */
 
Quoted 16 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, · line 242

Quoted 13 lines
drivers/usb/gadget/function/uvc_video.c +13 -0
+static void
+uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
+{
+    sg_free_table(&ureq->sgt);
+    if (ureq->req && ep) {
+        usb_ep_free_request(ep, ureq->req);
+        ureq->req = NULL;
+    }
+
+    kfree(ureq->req_buffer);
+    ureq->req_buffer = NULL;
+
+    if (!list_empty(&ureq->list))

Is this conditional needed? You can only get here through the list_for_each_entry_safe()

Strictly speaking, we don't need this check right now. As you said, we currently only get to this from within a list_for_each_entry_safe block. However, we end up needing the check in the very next patch. Considering this is a function with no real control over who might call it, it seemed reasonable to write this a little defensively in case of a partial revert of the patchset.

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +5 -0
+        list_del_init(&ureq->list);
+
+    kfree(ureq);
+}
+
<snip>
@@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video)

  static int uvc_video_alloc_requests(struct uvc_video *video) {
+    struct uvc_request *ureq;
      unsigned int req_size;
      unsigned int i;
      int ret = -ENOMEM;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -332,29 +336,34 @@ uvc_video_alloc_requests(struct uvc_video *video)
            * max_t(unsigned int, video->ep->maxburst, 1)
            * (video->ep->mult);
 
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 339

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -3
-    video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-    if (video->ureq == NULL)
-        return -ENOMEM;
+    INIT_LIST_HEAD(&video->ureqs);

Probably unecessary here; it's done in uvc_video_free_requests() and uvcg_video_init() already

Ah, that is fair. Added a BUG_ON instead, like we do for video->req_size so we still catch cases where the state might be inconsistent.

Quoted 11 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 348

Quoted 11 lines
drivers/usb/gadget/function/uvc_video.c +8 -0
+    for (i = 0; i < video->uvc_num_requests; i++) {
+        ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
+        if (ureq == NULL)
+            goto error;
+
+        INIT_LIST_HEAD(&ureq->list);
+
+        list_add_tail(&ureq->list, &video->ureqs);
 
<snip>

Regards,
Avi.

maintainer
#89 lore raw

Re: [PATCH v8 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time

Quoted 20 lines · Avichal Rakesh
Quoted 12 lines
Quoted 12 lines
(quoted diff) +0 -0
@@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video)

  static int uvc_video_alloc_requests(struct uvc_video *video) {
+    struct uvc_request *ureq;
      unsigned int req_size;
      unsigned int i;
      int ret = -ENOMEM;

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -332,29 +336,34 @@ uvc_video_alloc_requests(struct uvc_video *video)
            * max_t(unsigned int, video->ep->maxburst, 1)
            * (video->ep->mult);
 
Quoted 7 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 339

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -3
-    video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
-    if (video->ureq == NULL)
-        return -ENOMEM;
+    INIT_LIST_HEAD(&video->ureqs);

Probably unecessary here; it's done in uvc_video_free_requests() and uvcg_video_init() already

Quoted 2 lines

Ah, that is fair. Added a BUG_ON instead, like we do for video->req_size so we still catch cases where the state might be inconsistent.

Please no, that means you just crashed a machine and all data is lost and the user will get very mad.

Either handle the error properly or it's something that can never happen and so you don't need to handle it.

thanks,

greg k-h

#90 lore raw

PATCH v8 3/4 usb: gadget: uvc: move video disable logic to its own function

In reply to Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'.

---
v6: Introduced this patch to make the next one easier to review v6 -> v7: Add Suggested-by
v7 -> v8: No change. Getting back in review queue

 drivers/usb/gadget/function/uvc_video.c | 37 +++++++++++++++----------
 1 file changed, 23 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/uvc_video.c +23 -14
index c180866c8e34..80b8eaea2d39 100644
@@ -493,13 +493,33 @@ static void uvcg_video_pump(struct work_struct *work)
 	return;
 }
 
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video)
+{
+	struct uvc_request *ureq;
+
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
+
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
+
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
 /*
  * Enable or disable the video stream.
  */
 int uvcg_video_enable(struct uvc_video *video, int enable)
 {
 	int ret;
-	struct uvc_request *ureq;
 
 	if (video->ep == NULL) {
 		uvcg_info(&video->uvc->func,
@@ -507,19 +527,8 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 		return -ENODEV;
 	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
-
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
-
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
-	}
+	if (!enable)
+		return uvcg_video_disable(video);
 
 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
 		return ret;

--
2.42.0.758.gaed0368e0e-goog

#91 lore raw

Re: [PATCH v8 3/4] usb: gadget: uvc: move video disable logic to its own function

Hi Avichal

Quoted 2 lines · Avichal Rakesh

This patch refactors the video disable logic in uvcg_video_enable into its own separate function 'uvcg_video_disable'.

In that case, can we just replace any calls to uvcg_video_enable(video, 0) with calls to uvcg_video_disable() directly and drop the enable argument from uvcg_video_enable()?

Quoted 73 lines

---
v6: Introduced this patch to make the next one easier to review v6 -> v7: Add Suggested-by
v7 -> v8: No change. Getting back in review queue

  drivers/usb/gadget/function/uvc_video.c | 37 +++++++++++++++----------
  1 file changed, 23 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/uvc_video.c +23 -15
index c180866c8e34..80b8eaea2d39 100644
@@ -493,13 +493,33 @@ static void uvcg_video_pump(struct work_struct *work)
  	return;
  }
 
+/*
+ * Disable video stream
+ */
+static int
+uvcg_video_disable(struct uvc_video *video)
+{
+	struct uvc_request *ureq;
+
+	cancel_work_sync(&video->pump);
+	uvcg_queue_cancel(&video->queue, 0);
+
+	list_for_each_entry(ureq, &video->ureqs, list) {
+		if (ureq->req)
+			usb_ep_dequeue(video->ep, ureq->req);
+	}
+
+	uvc_video_free_requests(video);
+	uvcg_queue_enable(&video->queue, 0);
+	return 0;
+}
+
  /*
   * Enable or disable the video stream.
   */
  int uvcg_video_enable(struct uvc_video *video, int enable)
  {
  	int ret;
-	struct uvc_request *ureq;
 
  	if (video->ep == NULL) {
  		uvcg_info(&video->uvc->func,
@@ -507,19 +527,8 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
  		return -ENODEV;
  	}
 
-	if (!enable) {
-		cancel_work_sync(&video->pump);
-		uvcg_queue_cancel(&video->queue, 0);
-
-		list_for_each_entry(ureq, &video->ureqs, list) {
-			if (ureq->req)
-				usb_ep_dequeue(video->ep, ureq->req);
-		}
-
-		uvc_video_free_requests(video);
-		uvcg_queue_enable(&video->queue, 0);
-		return 0;
-	}
+	if (!enable)
+		return uvcg_video_disable(video);
 
  	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  		return ret;
--

2.42.0.758.gaed0368e0e-goog

#92 lore raw

PATCH v8 4/4 usb: gadget: uvc: Fix use-after-free for inflight usb_requests

In reply to Avichal Rakesh

Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller.

This is patch 2 of 2 in fixing the use-after-free issue. It adds a new flag to uvc_video to track when frames and requests should be flowing. When disabling the video stream, the flag is tripped and, instead of de-allocating all uvc_requests and usb_requests, the gadget driver only de-allocates those usb_requests that are currently owned by it (as present in req_free). Other usb_requests are left untouched until their completion handler is called which takes care of freeing the usb_request and its corresponding uvc_request.

Now that uvc_video does not depends on uvc->state, this patch removes unnecessary upates to uvc->state that were made to accommodate uvc_video logic. This should ensure that uvc gadget driver never accidentally de-allocates a usb_request that it doesn't own.

---
v1 -> v2: Rebased to ToT, and fixed deadlock reported in
https://lore.kernel.org/all/[email protected]/
v2 -> v3: Fix email threading goof-up
v3 -> v4: re-rebase to ToT & moved to a uvc_video level lock
as discussed in
https://lore.kernel.org/[email protected]/
v4 -> v5: Address review comments. Add Reviewed-by & Tested-by. v5 -> v6: Added another patch before this one to make uvcg_video_disable
easier to review.
v6 -> v7: Fix warning reported in

          https://lore.kernel.org/[email protected]/

v7 -> v8: No change. Getting back in review queue

 drivers/usb/gadget/function/uvc.h       |   1 +
 drivers/usb/gadget/function/uvc_v4l2.c  |  12 +--
 drivers/usb/gadget/function/uvc_video.c | 128 ++++++++++++++++++++----
 3 files changed, 111 insertions(+), 30 deletions(-)
drivers/usb/gadget/function/uvc.h +1 -0
index 993694da0bbc..be0d012aa244 100644
@@ -102,6 +102,7 @@ struct uvc_video {
 	unsigned int uvc_num_requests;
 
 	/* Requests */
+	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
 	struct list_head req_free;
drivers/usb/gadget/function/uvc_v4l2.c +2 -10
index 7cb8d027ff0c..f4d2e24835d4 100644
@@ -451,8 +451,8 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
+	uvc_function_setup_continue(uvc, 0);
 
 	return 0;
 }
@@ -468,11 +468,11 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	uvc->state = UVC_STATE_CONNECTED;
 	ret = uvcg_video_enable(video, 0);
 	if (ret < 0)
 		return ret;
 
+	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
 }
@@ -507,14 +507,6 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
-	/*
-	 * Drop uvc->state to CONNECTED if it was streaming before.
-	 * This ensures that the usb_requests are no longer queued
-	 * to the controller.
-	 */
-	if (uvc->state == UVC_STATE_STREAMING)
-		uvc->state = UVC_STATE_CONNECTED;
-
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
drivers/usb/gadget/function/uvc_video.c +108 -20

198 lines not on this page · raw message

--
2.42.0.758.gaed0368e0e-goog

#93 lore raw

Re: [PATCH v8 1/4] usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh
Quoted 168 lines · Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue

 drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
 drivers/usb/gadget/function/f_uvc.h     |  2 +-
 drivers/usb/gadget/function/uvc.h       |  2 +-
 drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
 drivers/usb/gadget/function/uvc_video.c |  3 ++-
 5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 	return 0;
 }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
 {
 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
 	usb_composite_setup_continue(cdev);
 }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
 		if (uvc->state != UVC_STATE_STREAMING)
 			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
 		memset(&v4l2_event, 0, sizeof(v4l2_event));
 		v4l2_event.type = UVC_EVENT_STREAMOFF;
 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
 	case 1:
 		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +1 -1
index 1db972d4beeb..e7f9f13f14dc 100644
@@ -11,7 +11,7 @@
 
 struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);
 
 void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
  * Functions
  */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
 extern void uvc_function_connect(struct uvc_device *uvc);
 extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
 	 * Complete the alternate setting selection setup phase now that
 	 * userspace is ready to provide video frames.
 	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
 	uvc->state = UVC_STATE_STREAMING;
 
 	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	struct video_device *vdev = video_devdata(file);
 	struct uvc_device *uvc = video_get_drvdata(vdev);
 	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
 	if (type != video->queue.queue.type)
 		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
 }
 
 static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
 static void uvc_v4l2_disable(struct uvc_device *uvc)
 {
 	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
 	uvcg_video_enable(&uvc->video, 0);
 	uvcg_free_buffers(&uvc->video.queue);
 	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
 #endif
 };
-
drivers/usb/gadget/function/uvc_video.c +2 -2
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
 	struct uvc_video_queue *queue = &video->queue;
 	/* video->max_payload_size is only set when using bulk transfer */
 	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
 	struct usb_request *req = NULL;
 	struct uvc_buffer *buf;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
 		/*
 		 * Retrieve the first available USB request, protected by the
 		 * request lock.
--
Quoted 1 line

2.42.0.758.gaed0368e0e-goog

Hey Greg,

Considering Laurent and Dan haven't responded, and Michael and I have tested this change, would it be possible to merge this patch set if the changes look OK to you? I don't think there are any outstanding items to be done around these fixes.

Thank you!
- Avi.

maintainer
#94 lore raw

Re: [PATCH v8 1/4] usb: gadget: uvc: prevent use of disabled endpoint

Quoted 4 lines · Avichal Rakesh

Considering Laurent and Dan haven't responded, and Michael and I have tested this change, would it be possible to merge this patch set if the changes look OK to you? I don't think there are any outstanding items to be done around these fixes.

I would like their review first please.

thanks,

greg k-h

#95 lore raw

Re: [PATCH v8 1/4] usb: gadget: uvc: prevent use of disabled endpoint

Good morning

Quoted 6 lines · Greg KH
Quoted 4 lines · Avichal Rakesh

Considering Laurent and Dan haven't responded, and Michael and I have tested this change, would it be possible to merge this patch set if the changes look OK to you? I don't think there are any outstanding items to be done around these fixes.

I would like their review first please.

Apologies for the delay - I am reviewing it now.

Quoted 4 lines

thanks,

greg k-h

Reviewed-by on v8 1/4
#96 lore raw

Re: [PATCH v8 1/4] usb: gadget: uvc: prevent use of disabled endpoint

In reply to Avichal Rakesh

Hi Avichal

Quoted 5 lines · Avichal Rakesh

Currently the set_alt callback immediately disables the endpoint and queues the v4l2 streamoff event. However, as the streamoff event is processed asynchronously, it is possible that the video_pump thread attempts to queue requests to an already disabled endpoint.

Quoted 8 lines

This change moves disabling usb endpoint to the end of streamoff event callback. As the endpoint's state can no longer be used, video_pump is now guarded by uvc->state as well. To be consistent with the actual streaming state, uvc->state is now toggled between CONNECTED and STREAMING from the v4l2 event callback only.

Thank you for picking this up! And I'm sorry to have been so remiss looking at your patches - I will try to be more responsive to them. I spotted a typo below, but otherwise:

Quoted 60 lines

---
v1 -> v2: Rebased to ToT and reworded commit message. v2 -> v3: Fix email threading goof-up
v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Add Reviewed-by & Tested-by
v5 -> v6: No change
v6 -> v7: No change
v7 -> v8: No change. Getting back in review queue

  drivers/usb/gadget/function/f_uvc.c     | 11 +++++------
  drivers/usb/gadget/function/f_uvc.h     |  2 +-
  drivers/usb/gadget/function/uvc.h       |  2 +-
  drivers/usb/gadget/function/uvc_v4l2.c  | 20 +++++++++++++++++---
  drivers/usb/gadget/function/uvc_video.c |  3 ++-
  5 files changed, 26 insertions(+), 12 deletions(-)
drivers/usb/gadget/function/f_uvc.c +5 -6
index faa398109431..ae08341961eb 100644
@@ -263,10 +263,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  	return 0;
  }
 
-void uvc_function_setup_continue(struct uvc_device *uvc)
+void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
  {
  	struct usb_composite_dev *cdev = uvc->func.config->cdev;
 
+	if (disable_ep && uvc->video.ep)
+		usb_ep_disable(uvc->video.ep);
+
  	usb_composite_setup_continue(cdev);
  }
 
@@ -337,15 +340,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  		if (uvc->state != UVC_STATE_STREAMING)
  			return 0;
 
-		if (uvc->video.ep)
-			usb_ep_disable(uvc->video.ep);
-
  		memset(&v4l2_event, 0, sizeof(v4l2_event));
  		v4l2_event.type = UVC_EVENT_STREAMOFF;
  		v4l2_event_queue(&uvc->vdev, &v4l2_event);
 
-		uvc->state = UVC_STATE_CONNECTED;
-		return 0;
+		return USB_GADGET_DELAYED_STATUS;
 
  	case 1:
  		if (uvc->state != UVC_STATE_CONNECTED)
drivers/usb/gadget/function/f_uvc.h +0 -0
index 1db972d4beeb..e7f9f13f14dc 100644

drivers/usb/gadget/function/f_uvc.h line 14

Quoted 5 lines
drivers/usb/gadget/function/f_uvc.h +1 -1
@@ -11,7 +11,7 @@
 
  struct uvc_device;
 
-void uvc_function_setup_continue(struct uvc_device *uvc);
+void uvc_function_setup_continue(struct uvc_device *uvc, int disale_ep);

s/disale_ep/disable_ep

Quoted 91 lines
drivers/usb/gadget/function/f_uvc.h +0 -0
  void uvc_function_connect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc.h +1 -1
index 6751de8b63ad..989bc6b4e93d 100644
@@ -177,7 +177,7 @@ struct uvc_file_handle {
   * Functions
   */
 
-extern void uvc_function_setup_continue(struct uvc_device *uvc);
+extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
  extern void uvc_function_connect(struct uvc_device *uvc);
  extern void uvc_function_disconnect(struct uvc_device *uvc);
 
drivers/usb/gadget/function/uvc_v4l2.c +17 -3
index 3f0a9795c0d4..7cb8d027ff0c 100644
@@ -451,7 +451,7 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  	 * Complete the alternate setting selection setup phase now that
  	 * userspace is ready to provide video frames.
  	 */
-	uvc_function_setup_continue(uvc);
+	uvc_function_setup_continue(uvc, 0);
  	uvc->state = UVC_STATE_STREAMING;
 
  	return 0;
@@ -463,11 +463,18 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  	struct video_device *vdev = video_devdata(file);
  	struct uvc_device *uvc = video_get_drvdata(vdev);
  	struct uvc_video *video = &uvc->video;
+	int ret = 0;
 
  	if (type != video->queue.queue.type)
  		return -EINVAL;
 
-	return uvcg_video_enable(video, 0);
+	uvc->state = UVC_STATE_CONNECTED;
+	ret = uvcg_video_enable(video, 0);
+	if (ret < 0)
+		return ret;
+
+	uvc_function_setup_continue(uvc, 1);
+	return 0;
  }
 
  static int
@@ -500,6 +507,14 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
  static void uvc_v4l2_disable(struct uvc_device *uvc)
  {
  	uvc_function_disconnect(uvc);
+	/*
+	 * Drop uvc->state to CONNECTED if it was streaming before.
+	 * This ensures that the usb_requests are no longer queued
+	 * to the controller.
+	 */
+	if (uvc->state == UVC_STATE_STREAMING)
+		uvc->state = UVC_STATE_CONNECTED;
+
  	uvcg_video_enable(&uvc->video, 0);
  	uvcg_free_buffers(&uvc->video.queue);
  	uvc->func_connected = false;
@@ -647,4 +662,3 @@ const struct v4l2_file_operations uvc_v4l2_fops = {
  	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
  #endif
  };
-
drivers/usb/gadget/function/uvc_video.c +2 -2
index 91af3b1ef0d4..c334802ac0a4 100644
@@ -384,13 +384,14 @@ static void uvcg_video_pump(struct work_struct *work)
  	struct uvc_video_queue *queue = &video->queue;
  	/* video->max_payload_size is only set when using bulk transfer */
  	bool is_bulk = video->max_payload_size;
+	struct uvc_device *uvc = video->uvc;
  	struct usb_request *req = NULL;
  	struct uvc_buffer *buf;
  	unsigned long flags;
  	bool buf_done;
  	int ret;
 
-	while (video->ep->enabled) {
+	while (uvc->state == UVC_STATE_STREAMING && video->ep->enabled) {
  		/*
  		 * Retrieve the first available USB request, protected by the
  		 * request lock.
--

2.42.0.758.gaed0368e0e-goog