From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3F833E2AA3 for ; Mon, 25 May 2026 09:52:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779702762; cv=none; b=YubaLA1YxdYK2Yosdn3n3mnq5/HbdPAJxCHWAPS/yz5Mkf3Zs9Wz8G51sI4ONGs3Q2XaMKXfoX4UBptZpvVRsV8CCqsVGY2Lj2tD9z0mUVyAa1AtEZanwOblExuedI9ElgvY1zzOmNbAhDMhpNHV8z9qaXDVsqEiYDufXvvfQio= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779702762; c=relaxed/simple; bh=WIS3BgyrEbuPFk0DAL3nE3V6IGRFLo7gF/BE5feLCxo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J5BbJ4nmysI0gL+cfBJ4EzsuSRUrwfvMnUxoXqBdR5ZcFlfvnFxc9K39mRAaDA4SMdfVzrytuM4XYAAL9sIU5NJeIV+UHK5fuNiGZZHs7v4sSgKxQpNpTBXR0Jh8TsWcuwM1+L5LHjAO5yvi5KTFUDEyXHdnDz43zjXPvjAQ4u8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=krDJvIw7; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="krDJvIw7" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2baef9f5ecdso77465865ad.1 for ; Mon, 25 May 2026 02:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779702760; x=1780307560; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5gZEIwW5TqIv7uy+nwwFRwQeWX+dkGvQ9eeETlxlcvg=; b=krDJvIw7DyTuW5gqNxDvwpPwCYjS9hn4JmztEKVe23sTtev/33wHFxgiKquNIpzzta Rc+bl+QuW8LOklanU5k6l4534mwhd98xUsBYokvImrPt+3E9DE6+fxJD2yj8PE3NjgHs 470bCEuQeTMdBwyhiVxunpw7wdRp06XNE6ltCVwE+Yu+XHORePjIzrdATRHw6+Rn3eIw A1Nz3YKs/PiA7+y5eRUvqFxPIhqxJUW103FOpH+xL08iTbqODxdHBOuVtxbG/UxaAJCR x7kF7BF86rIBrO7v4Xe2Zt9ZjjZ47qpfQ5KX6IZS5NgVL0oXbD8SN0RZvcWZ245H/qBP CTMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779702760; x=1780307560; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5gZEIwW5TqIv7uy+nwwFRwQeWX+dkGvQ9eeETlxlcvg=; b=GZo+Gz3HeTVHbhHPOJyurifueEBkr83tIWJwaII6BlTH7r9ku5Q0eguI443G2hEyD3 Oyo0E2IfpR6w7O9xLg45rcYlPn34xOPhldsAI01i/b6PvBprZe5UUe5SyPxnesJgT2vS ZU+LU+m0ab8rvSo0+wmNa6g+eoBjQP62PtYHvqr80VTeAHpTV3F5vpWIaxDM/DYMulSY d5bymcoBWljo2wQ+szoJZlA4JD4dtvsaD5jAuAZl0wdR5hsQK+rZ9nh6yestb8lHOYIq 8PF33p4hdwFmZZ2LOJAakFXCthh6pVoF5mNImNdqxyZFICpEOiwnViL0iEyGz3Ykb3Mw TNRg== X-Forwarded-Encrypted: i=1; AFNElJ/ElU3wUK1v7NSNPRVvlYF17CuyPrIu9DSvwv4NqbRljrAiL/Du0lARWE9R+mHpYLZfNYSEtYjIDfx2E0Q=@vger.kernel.org X-Gm-Message-State: AOJu0YzlmihU6iExokx5PsQnARM2FFb9qH506LCKi8zyN/37MX3tOzLW 5dY83apIFrp+IeMlTfFZR0lbXLyc2ojmA+z8DCsgRz0pWSN645vkw4Jf X-Gm-Gg: Acq92OFWpudqPnnB/uL9DYmBq2B4JT8INvQnK5nKqYjd5JQB5Jis/sXL5Dwxq0BmarN PaCm8WH2fffkdBF+BMPrv0Q9sSbljN4UNqEIoUuDU2XMeZja0qmSpwKQvZoDYMgRDVVp5zEpC02 qGNy9UO6kyNIkSCDxfmaDM0Jh1wiP2uxx7o6eIjSsEFT8rAMNQdHcXGAOmNoA6EYxwcKEE4mzNU Rt8t/eFxjAMXGy4nOjhOqH618jQV7yC4n9rwaHmbDuTwqprfNHsB7pThzP1ZQtd+ePtQRMlhtCR +JNdAOjh6E837lx9jJ/FJwjqIOtlfA4x4t1v2lKQiKocpaxpP+cvCl/9XYERTKk4Up7Y3eHW0Gn SAN1u8KhkJa2bdNxFzHUxkclEFLg5yX5jOOT4UTF3YmZlbFFXRpmIB62q1NgXhq4aD4FUvKr7dU QpG+jNcOEQaB2E24Lgjj/4GQ7C+EPsjMo= X-Received: by 2002:a17:902:f70e:b0:2bc:8ebd:af76 with SMTP id d9443c01a7336-2beb03466afmr148144335ad.0.1779702760099; Mon, 25 May 2026 02:52:40 -0700 (PDT) Received: from rockpi-5b ([45.112.0.230]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2beb58b2cd6sm92533615ad.52.2026.05.25.02.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2026 02:52:39 -0700 (PDT) From: Anand Moon To: Neil Armstrong , Mauro Carvalho Chehab , Greg Kroah-Hartman , Kevin Hilman , Jerome Brunet , Martin Blumenstingl , Hans Verkuil , Maxime Jourdan , linux-media@vger.kernel.org (open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS), linux-amlogic@lists.infradead.org (open list:MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS), linux-staging@lists.linux.dev (open list:STAGING SUBSYSTEM), linux-arm-kernel@lists.infradead.org (moderated list:ARM/Amlogic Meson SoC support), linux-kernel@vger.kernel.org (open list) Cc: Anand Moon , Nicolas Dufresne , Sashiko Subject: [PATCH v5 1/6] media: meson: vdec: Fix memory leak in error path of vdec_open Date: Mon, 25 May 2026 15:21:49 +0530 Message-ID: <20260525095216.12078-2-linux.amoon@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260525095216.12078-1-linux.amoon@gmail.com> References: <20260525095216.12078-1-linux.amoon@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The vdec_open() function previously jumped directly to err_m2m_release when vdec_init_ctrls() failed, skipping release of the m2m context. This caused a resource leak. Fix it by introducing a proper err_m2m_ctx_release label that calls v4l2_m2m_ctx_release(sess->m2m_ctx) before releasing the m2m device. Also free the v4l2 control handler memory allocated by vdec_init_ctrls() in vdec_close(). This was identified via kmemleak: unreferenced object 0xffff0000205d6878 (size 8): comm "v4l_id", pid 5289, jiffies 4294938580 hex dump (first 8 bytes): 40 d2 49 18 00 00 ff ff @.I..... backtrace (crc d3204599): kmemleak_alloc+0xc8/0xf0 __kvmalloc_node_noprof+0x60c/0x850 v4l2_ctrl_handler_init_class+0x1b4/0x2e8 [videodev] vdec_open+0x1f4/0x788 [meson_vdec] v4l2_open+0x144/0x460 [videodev] chrdev_open+0x1ac/0x500 do_dentry_open+0x3f0/0xfe8 vfs_open+0x68/0x320 do_open+0x2d8/0x9a8 path_openat+0x1d0/0x4f0 do_filp_open+0x190/0x380 do_sys_openat2+0xf8/0x1b0 __arm64_sys_openat+0x13c/0x1e8 invoke_syscall+0xdc/0x268 el0_svc_common.constprop.0+0x178/0x258 do_el0_svc+0x4c/0x70 Cc: Nicolas Dufresne Reported-by: Sashiko Closes: https://lore.kernel.org/all/20260520045905.6ACBA1F000E9@smtp.kernel.org/#t Fixes: 3e7f51bd9607 ("media: meson: add v4l2 m2m video decoder driver") Signed-off-by: Anand Moon --- v5: update the error path for v4l2_ctrl_handler_free() as per the review ccmment to fix the use after free bug. [4] https://patchwork.kernel.org/project/linux-amlogic/patch/20260521073449.10057-2-linux.amoon@gmail.com/ v4: update the commit message to add v4l2_ctrl_handler_free() in vdec_close() to adderss the issue: This isn't a bug introduced by this patch, but does vdec_close() properly free the v4l2 control handler memory allocated by vdec_init_ctrls() here? v3: https://lore.kernel.org/all/20260520044046.7553-1-linux.amoon@gmail.com/ update the commit messagee. v2: https://lore.kernel.org/all/20260321065408.209723-1-linux.amoon@gmail.com/ updated the commit message, applied the suggestion from sashiko below. [3] https://sashiko.dev/#/patchset/20260321065408.209723-1-linux.amoon%40gmail.com v1: https://lore.kernel.org/all/20260304100557.126488-1-linux.amoon@gmail.com/ tried to address the issue reported by Nicolas improve the commit message. --- drivers/staging/media/meson/vdec/vdec.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/staging/media/meson/vdec/vdec.c b/drivers/staging/media/meson/vdec/vdec.c index 4b77ec1af5a7..18a22b79e835 100644 --- a/drivers/staging/media/meson/vdec/vdec.c +++ b/drivers/staging/media/meson/vdec/vdec.c @@ -889,7 +889,7 @@ static int vdec_open(struct file *file) ret = vdec_init_ctrls(sess); if (ret) - goto err_m2m_release; + goto err_m2m_ctx_release; sess->pixfmt_cap = formats[0].pixfmts_cap[0]; sess->fmt_out = &formats[0]; @@ -913,6 +913,8 @@ static int vdec_open(struct file *file) return 0; +err_m2m_ctx_release: + v4l2_m2m_ctx_release(sess->m2m_ctx); err_m2m_release: v4l2_m2m_release(sess->m2m_dev); err_free_sess: @@ -928,6 +930,7 @@ static int vdec_close(struct file *file) v4l2_m2m_release(sess->m2m_dev); v4l2_fh_del(&sess->fh, file); v4l2_fh_exit(&sess->fh); + v4l2_ctrl_handler_free(&sess->ctrl_handler); mutex_destroy(&sess->lock); mutex_destroy(&sess->bufs_recycle_lock); -- 2.50.1