Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2172788rdf; Mon, 6 Nov 2023 06:49:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdoXU9ztxPfirnexVA401/imK8MYarUBSvpF+u9ErUsGJtar824M3GvcZ6bVARzDl+54Fn X-Received: by 2002:a05:6a00:1911:b0:6bd:7cbd:15a2 with SMTP id y17-20020a056a00191100b006bd7cbd15a2mr24854567pfi.26.1699282180114; Mon, 06 Nov 2023 06:49:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699282180; cv=none; d=google.com; s=arc-20160816; b=f2JHmXcA8j0MlY9wgXxSE2HT7nB1DTLOcjtuQTJQP6lFfQeO7tpfZDFsdsXioglkPr rPnJ5j6QR0bxJKOmyFtmapj21GSdymM9ZhQvZYEO8LSAXEtTRXZ3UjsRhjPqS1y9EIBd Mx08+D9RtAi/9b9rRH32p62gZv3YaB27O9+vY/qXWt9flJbhkrmjgbj4IHknwRdgyCJm Ydbsf7ThHmZ68bdPqtKvMyEhudFnyUW1dzrdnvGvvPSwc7Rg2As/rmBlL6Xtl9cd4JWz 3LjuOB4XQ2zTw1pqqVm1z55negactFrtWiiXhLjRwamjdf57/QUNvMlmywq6QTox46ih c8sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Mn+LMvMphVLhG3rmeD4sGc/rl3mIEcUGZbCfQih6dB0=; fh=J+2+1vbe2QKJ61F+pm+qTQ42g1lLVufrDL4LIIdBYUE=; b=s/Oy5UDO6gZwAYzXhzgORSSZV2C5dyzpE1AKYluszMkuGGUnlBVbkGtuaCavpxaB2w 24i2SEv4R1RttFiBFd50H3xA37JRpH7w8DJUH+clIakmTGeikabPPdB0TNW4f9HyP8Nw y7iQY7YteRBuRnz/IBEnxBHg6izzfEvObMiJIYk20S34sFJzn5q8z3N5GO6IIzqZHnXh Fmj0tZJKDbYfKIP+wWyp3Mijf+JVU95MmhDbgNfk9PUuey4um59fyTdp+3W7ufEnVFb7 SLDLHQ8yFc6qTmmUvIqJe/n2izWGO/PTJqF/DJqYb47V8dPB+CnPWA3IinOB1vyWcRx4 +Jcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=QbRgIzfn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id m9-20020a056a00080900b00690ffbb46e6si8664555pfk.261.2023.11.06.06.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 06:49:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=QbRgIzfn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 708AB8039A2E; Mon, 6 Nov 2023 06:49:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231825AbjKFOtG (ORCPT + 99 others); Mon, 6 Nov 2023 09:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229705AbjKFOtF (ORCPT ); Mon, 6 Nov 2023 09:49:05 -0500 Received: from m15.mail.163.com (m15.mail.163.com [45.254.50.220]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8BA37B6; Mon, 6 Nov 2023 06:49:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=Mn+LM vMphVLhG3rmeD4sGc/rl3mIEcUGZbCfQih6dB0=; b=QbRgIzfncyvm29xEQRCsf s9y9BGRM9GSawz/gOF6YOeat//QqI6nuffkL22AtZO7U2xXL6Eih9rkATqjR1PWl pEAitLtyJ3GYDBHnDBZC2RzODczZUUsL8iCEUsTcTetxOm5IeaTR/DvYMI3cjEgL WFlXqpEzLvFsPX6gVhWijg= Received: from leanderwang-LC4.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g2-2 (Coremail) with SMTP id _____wAXH5Gu_Ehl9FmbBw--.1952S4; Mon, 06 Nov 2023 22:48:21 +0800 (CST) From: Zheng Wang To: dmitry.osipenko@collabora.com Cc: Kyrie.Wu@mediatek.com, bin.liu@mediatek.com, mchehab@kernel.org, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, Irui.Wang@mediatek.com, security@kernel.org, hackerzheng666@gmail.com, amergnat@baylibre.com, wenst@chromium.org, stable@vger.kernel.org, Zheng Wang Subject: [RESEND PATCH v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Date: Mon, 6 Nov 2023 22:48:10 +0800 Message-Id: <20231106144811.868127-3-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231106144811.868127-1-zyytlz.wz@163.com> References: <20231106144811.868127-1-zyytlz.wz@163.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wAXH5Gu_Ehl9FmbBw--.1952S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1rGr1fJr1kJFW3KF43GFg_yoW8KF48pr Zagw4DCFWUGrs0gr48Aa4UZF1rC398tF12gr4S9wn3Z343XFs7Jry0ya4IqFWIyr9rCa4r Zr1F9a4xJr4DZFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zR7DGOUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/xtbBzhggU2I0bOKhvAAAsg X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 06:49:26 -0800 (PST) In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with mtk_jpeg_job_timeout_work. In mtk_jpeg_dec_device_run, if error happens in mtk_jpeg_set_dec_dst, it will finally start the worker while mark the job as finished by invoking v4l2_m2m_job_finish. There are two methods to trigger the bug. If we remove the module, it which will call mtk_jpeg_remove to make cleanup. The possible sequence is as follows, which will cause a use-after-free bug. CPU0 CPU1 mtk_jpeg_dec_... | start worker | |mtk_jpeg_job_timeout_work mtk_jpeg_remove | v4l2_m2m_release | kfree(m2m_dev); | | | v4l2_m2m_get_curr_priv | m2m_dev->curr_ctx //use If we close the file descriptor, which will call mtk_jpeg_release, it will have a similar sequence. Fix this bug by starting timeout worker only if started jpegdec worker successfully. Then v4l2_m2m_job_finish will only be called in either mtk_jpeg_job_timeout_work or mtk_jpeg_dec_device_run. Fixes: b2f0d2724ba4 ("[media] vcodec: mediatek: Add Mediatek JPEG Decoder Driver") Signed-off-by: Zheng Wang Signed-off-by: Dmitry Osipenko Cc: stable@vger.kernel.org --- v2: - put the patches into a single series suggested by Dmitry --- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c index 60425c99a2b8..a39acde2724a 100644 --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c @@ -1021,13 +1021,13 @@ static void mtk_jpeg_dec_device_run(void *priv) if (ret < 0) goto dec_end; - schedule_delayed_work(&jpeg->job_timeout_work, - msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); - mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs); if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb)) goto dec_end; + schedule_delayed_work(&jpeg->job_timeout_work, + msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); + spin_lock_irqsave(&jpeg->hw_lock, flags); mtk_jpeg_dec_reset(jpeg->reg_base); mtk_jpeg_dec_set_config(jpeg->reg_base, -- 2.25.1