From patchwork Thu May 14 08:40:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 3794 Return-Path: Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AEB4860DE6 for ; Thu, 14 May 2020 10:40:10 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="NLsex3Q3"; dkim-atps=neutral Received: by mail-wr1-x444.google.com with SMTP id l11so2800541wru.0 for ; Thu, 14 May 2020 01:40:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id; bh=9T45KgV/DtI1j/9bQO+bF54N9jAoocbfPKibMpLyd68=; b=NLsex3Q3sQNEKk76ZEkWGLQyzfWO0nqyPxR4ydMqyA586sJzNNFW03rEs7n1GGJwDB 405xOId0ZwIH/c0bCvkS29HXko42nankW8RUG1zaHSNLfYgUycdwLcBUqu/FiB8xaaJ3 yh4gqyXy7e3IrYXS6REARf4heCqk8+KINM3mahb/qnACA7VavuotyrTB193I7fJtk4aK 2H8NVttB7dHOjAzgw5bGYoYgTCq7KjfzWK1ulG2wOMXIGJbsw8uAs2d6JcKIwP0Jr1S1 VqHsmp4OqapKfoiLg4b9B3FJOErXLYQ2gBirj/aM7K776hzUgseIuZHhr8YxEF+upvyD zGUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=9T45KgV/DtI1j/9bQO+bF54N9jAoocbfPKibMpLyd68=; b=knd4Drs24UYqY1hVieG/HwNJTnolIk+/1AcHztw721+4+r8bv+TANyahaRMWwf0Swv B6dVun6QiiRA+Xey7gOdonExtmtz7Qi84h2F8ZHTdNuIG2xsr5+T6Hierml2IY1NZ+mb ASs4v3f9p1ClJ8rBbGVR9W9vKbE1C8Da8r3j5oHtzNkeIR8FNODd0WUvBy27GtpaLuQ/ G4k2pG5RXx3P5ihoXMNuygl37DNo0t9s0QzS/FE26j+wIHNi4FOL18mCZ3EMaSHlasCh OrTzuqenGdOPpAPdnrwprcCIyJNMo/NB7sOa4NEwUiR9tPwICZMl/iDp/eRAKERthu0o Fp+A== X-Gm-Message-State: AOAM531umIq3crdRZcGfU2b3DNYvqCA7hJzxFwLomh1h1B/+BCLFqkf4 xLZ/jsavJAgOuTZEpWq86m0ntF45JItVhQ== X-Google-Smtp-Source: ABdhPJzb2ttwD4JZ15Rz6zF8vLtDMelzRtYG6x0Zt8xXogQutYzY0azrIS1aMxWRv3zCZwNXNt9ydw== X-Received: by 2002:adf:a51a:: with SMTP id i26mr3956114wrb.332.1589445610282; Thu, 14 May 2020 01:40:10 -0700 (PDT) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id o203sm23337886wme.12.2020.05.14.01.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2020 01:40:09 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Thu, 14 May 2020 09:40:03 +0100 Message-Id: <20200514084003.16948-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.17.1 Subject: [libcamera-devel] [PATCH v2] libcamera: v4l2_videodevice: Fix dangling file descriptor X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 May 2020 08:40:12 -0000 The FileDescriptor constructor used in V4L2VideoDevice::exportDmabufFd() creates a duplicate of the fd to store in the object. The original fd returned by the VIDIOC_EXPBUF ioctl was never closed, and left dangling. This would cause out of memory conditions if the camera stream was repeatedly started and stopped. This change closes the original fd explicitly, fixing the leak. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/libcamera/v4l2_videodevice.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 4b9f8b5c..2f9c1333 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1276,7 +1276,14 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, return FileDescriptor(); } - return FileDescriptor(expbuf.fd); + FileDescriptor fd(expbuf.fd); + /* + * FileDescriptor takes a duplicate of fd, so we must close the + * original here, otherwise it will be left dangling. + */ + ::close(expbuf.fd); + + return fd; } /**