From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 822D93B19D1; Mon, 4 May 2026 14:11:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777903871; cv=none; b=eVm67mZ86MkpYUHpbFDtLQb1clPaznKo8tpj//3aQP83qdxC1t6FOOXCc0UQptBhMOnjPq4WVSjhEiiYQcCp6d+kUpBMY++uD76qUFdIyPlqp38RIKQ314n3xIXOxFze9ch8tJvt7+nP3qm5ppOvHft2dZHRJJtifqNNXo4q2f0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777903871; c=relaxed/simple; bh=gENbwNyFLzvlAwdmSo1+TQ8Zqbd26x8vfQLvNtGiOaY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pb/uxQFDsFa0ePmAA/ZLq6usYpZtvoM2dAKu9RYmF8Hsv71Sd9Cw0nm3AzcIDwJK55IRiLVAVAGiTSlmFhTN+6iTLyfzqxU5lfT3oRzRSSsNkIQ0SBkkdcJ7lkw1wHgtm7um2K2Ct48fqPxD0dz4yWqM1Md+P8QU9Xp+Kg5BrLw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=kZP/iikn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="kZP/iikn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1880EC2BCB8; Mon, 4 May 2026 14:11:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1777903871; bh=gENbwNyFLzvlAwdmSo1+TQ8Zqbd26x8vfQLvNtGiOaY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kZP/iikn8UoJ8ZhlBC2SswZy1ppcEQbBQ+kTOrwHXrv3pD3Fn6+Xu9OIdFG5XksEJ t3+gZlZx2ExdA1J3CHZPb1Og+EJojlzopPXqGUXyDjfODuSjn5h9vjLF9Wu+d05qgz px0d+3ztXZQ6RvtteUZVAndVnBNeeAaBbDmODnBM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Fan Wu , Nicolas Dufresne , Hans Verkuil Subject: [PATCH 6.18 051/275] media: mtk-jpeg: fix use-after-free in release path due to uncancelled work Date: Mon, 4 May 2026 15:49:51 +0200 Message-ID: <20260504135144.831724790@linuxfoundation.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260504135142.929052779@linuxfoundation.org> References: <20260504135142.929052779@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Fan Wu commit 34c519feef3e4fcff1078dc8bdb25fbbbd10303f upstream. The mtk_jpeg_release() function frees the context structure (ctx) without first cancelling any pending or running work in ctx->jpeg_work. This creates a race window where the workqueue callback may still be accessing the context memory after it has been freed. Race condition: CPU 0 (release) CPU 1 (workqueue) ---------------- ------------------ close() mtk_jpeg_release() mtk_jpegenc_worker() ctx = work->data // accessing ctx kfree(ctx) // freed! access ctx // UAF! The work is queued via queue_work() during JPEG encode/decode operations (via mtk_jpeg_device_run). If the device is closed while work is pending or running, the work handler will access freed memory. Fix this by calling cancel_work_sync() BEFORE acquiring the mutex. This ordering is critical: if cancel_work_sync() is called after mutex_lock(), and the work handler also tries to acquire the same mutex, it would cause a deadlock. Note: The open error path does NOT need cancel_work_sync() because INIT_WORK() only initializes the work structure - it does not schedule it. Work is only scheduled later during ioctl operations. Fixes: 5fb1c2361e56 ("mtk-jpegenc: add jpeg encode worker interface") Cc: stable@vger.kernel.org Signed-off-by: Fan Wu Reviewed-by: Nicolas Dufresne Signed-off-by: Nicolas Dufresne Signed-off-by: Hans Verkuil Signed-off-by: Greg Kroah-Hartman --- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 1 + 1 file changed, 1 insertion(+) --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c @@ -1209,6 +1209,7 @@ static int mtk_jpeg_release(struct file struct mtk_jpeg_dev *jpeg = video_drvdata(file); struct mtk_jpeg_ctx *ctx = mtk_jpeg_file_to_ctx(file); + cancel_work_sync(&ctx->jpeg_work); mutex_lock(&jpeg->lock); v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); v4l2_ctrl_handler_free(&ctx->ctrl_hdl);