[libcamera-devel] [PATCH v3 17/22] v4l2: v4l2_camera: Clear pending requests on freeBuffers and streamOff

Paul Elder paul.elder at ideasonboard.com
Tue Jun 23 21:08:31 CEST 2020


V4L2 allows buffer queueing before streamon while libcamera does not.
The compatibility layer thus saves these buffers in a pending queue
until streamon, and then automatically queues them. However, this
pending queue is not cleared when the buffers are freed, so the
following sequence of actions will cause a use-after-free:

1. queue buffers
2. free buffers
   - buffers from 1. stay in pending queue but have been freed
3. queue buffers
4. streamon
   - buffers from 1. are enqueued, then the buffers from 3. are
     enqueued. Use-after-free segfault when libcamera tries to handle
     the enqueued buffers from 1.

Fix this by clearing the pending request queue upon buffers being freed.
Also clear the pending request queue on streamOff, for correctness.

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

---
Changes in v3:
- reorder clearing the pending request queue, to before freeing
  buffers, and to after checking isRunning

Changes in v2:
- also clear pending request queue on streamOff
- clarify the issue in changelog
---
 src/v4l2/v4l2_camera.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp
index bdf0036..ee6f907 100644
--- a/src/v4l2/v4l2_camera.cpp
+++ b/src/v4l2/v4l2_camera.cpp
@@ -149,6 +149,7 @@ void V4L2Camera::freeBuffers()
 {
 	Stream *stream = *camera_->streams().begin();
 
+	pendingRequests_.clear();
 	bufferAllocator_->free(stream);
 }
 
@@ -189,10 +190,11 @@ int V4L2Camera::streamOn()
 
 int V4L2Camera::streamOff()
 {
-	/* \todo Restore buffers to reqbufs state? */
 	if (!isRunning_)
 		return 0;
 
+	pendingRequests_.clear();
+
 	int ret = camera_->stop();
 	if (ret < 0)
 		return ret == -EACCES ? -EBUSY : ret;
-- 
2.27.0



More information about the libcamera-devel mailing list