Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1021471rdh; Fri, 27 Oct 2023 02:21:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHz5RtkoXTmH6j9ZuHqlTtPu3hY/Q8qqTIsoPIvlKtSbVTJgeof0mHwj0DBvXkJgpeljEi9 X-Received: by 2002:a81:cb0b:0:b0:5a5:575:e944 with SMTP id q11-20020a81cb0b000000b005a50575e944mr9045734ywi.4.1698398461596; Fri, 27 Oct 2023 02:21:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698398461; cv=none; d=google.com; s=arc-20160816; b=cAAboXtiYQHTqqvCYH4N5KSscIZ+W8awt8IxqApxG+3iFTViZtaVBhVsURFEb/EVDJ pFc9jyHS4TERBiW8kYY8MmhMhMpCt9E1nfhU41BjUpciHWCeO91F2COsNhkSxLdrT39z Z10oKXIYvXg0Xto3kFyu3m7WwC4bycuiNQ46eONTbQCO3cegVpEeZYx3HKZw3Nf0DGC/ gWOMptUuEOLGjmmo+LrXZynjgL9mxpGrn6ck2qEF1gN84gmcYTAJCT2VMN0/JZblihHG /11PV/ggnwcK1ZkLY8xf2D2YFNIA1sj6Jm45DTci/K+hA0MGo59yyIfCHWLnTDUJt37F KbMA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=PzNylZQPe72kv7lQwIcpgOIDAmXNbLyg1TXLLWgVRXs=; fh=jhBAW35p4b2oJLfuOUmbxBtG0fjoiGHBapgzYTQ+zbE=; b=DkUKOP56xhRspiVpSzhkkN/IdW4X97IrZ/0QkqI/ELjp56FxTK9hmZs8TOorAhC8O/ 6hHlp+IyQSLe9H6tQzMGJJGQfUiHIwK7pwXk2g6JvR0Z9PmDNDA5I9MatusSAS+ITMsi SOkoNQh7ICbFTrapLYdUxmdKOw8wExlBfKqI08p4frVJR/8KtMi+7FIg51cir3JXTs9i ZsOHMS/eFACAE08JuaaH0ZJzfh1lF8AkTNAZSC98cEfPj8SoMbZ4aXnqRs18qGoMsgJ7 or2J7866PxJ3tXR8OjF1Q602XlRN61+e0sHJxA027p5BwFC7sE+RWiwlOfTlVlJO6CBl i/qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=jZyS5Kyl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id w62-20020a25df41000000b00da061795261si2264207ybg.407.2023.10.27.02.21.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 02:21:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=jZyS5Kyl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id C139280CF513; Fri, 27 Oct 2023 02:20:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345554AbjJ0JT4 (ORCPT + 99 others); Fri, 27 Oct 2023 05:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231340AbjJ0JTw (ORCPT ); Fri, 27 Oct 2023 05:19:52 -0400 Received: from m15.mail.163.com (m15.mail.163.com [45.254.50.219]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7398EC0; Fri, 27 Oct 2023 02:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=PzNyl ZQPe72kv7lQwIcpgOIDAmXNbLyg1TXLLWgVRXs=; b=jZyS5KyldBlYbDPrhRgv7 EJlquVWUDw4MCzLxO1mg+veTPUIm9Aoh6cjTPBVbrBC40Ky8jlhSHs8IZgLxKKQC 6MeDSq8LnyUg+nNuvhqzSAL+klZS9Df5Djcgt3IrDIF75zFZLaGLFCLgfBb9quKp +0chaCw7dL02+ZoMwZSipU= Received: from leanderwang-LC4.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g2-2 (Coremail) with SMTP id _____wBHL5VbgDtlSc85BA--.14166S2; Fri, 27 Oct 2023 17:18:19 +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, 1395428693sheep@gmail.com, alex000young@gmail.com, amergnat@baylibre.com, wenst@chromium.org, Zheng Wang , stable@vger.kernel.org Subject: [PATCH 1/2] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Date: Fri, 27 Oct 2023 17:18:16 +0800 Message-Id: <20231027091816.39000-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wBHL5VbgDtlSc85BA--.14166S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1rGr1fJr1kJFW3KF43GFg_yoW8tw4fpr Zagw4DCFWUWrs0gr4UAa4UZF1rC398JF12gr1S9wn3Z343XFs7Jry0ya4IqFWIyr9rCa4r Xr10934xJrWDZFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRUKsUUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXRkWU1WBq4p1UQAAsQ 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Fri, 27 Oct 2023 02:20:22 -0700 (PDT) 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 --- 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 7194f88edc0f..252ba9b6857b 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