Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp73403rdb; Tue, 31 Oct 2023 00:33:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG8jph3sKNKYK2DSu7pQBREvYdnPQNh4Mr8EDX9frkGJ5mcnHxWil9BR81krC9CQesayeAh X-Received: by 2002:a17:902:e848:b0:1c2:193e:1126 with SMTP id t8-20020a170902e84800b001c2193e1126mr2385638plg.28.1698737587157; Tue, 31 Oct 2023 00:33:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698737587; cv=none; d=google.com; s=arc-20160816; b=PGd4Yie7jawvNS5JqKt6I9FWrXmfURzOThnlfI+Aq1Ty5SS9FJqaT01abPKg+x3lgC HD0CRRLjZVd5qMYMdIB0lK4TitfpasfS1g59T1dlRlxsq1FodiwsFrMzXPI48elRGJ3u zIwn8J8v1hD2sb960To8zW02sYHUhM4GLGLPre/1tfNV7dPrKxDIazIo6VrOQSf5PDa6 uAt4c4OVUt1vPLaas9DBgWHUFi9DYheeNhleiDHoHO+H8zbnVQcxnNh47F9XQK8aHZzx 3n6w1/oLYf7MoPvFLSoF5y0xgoZd4xWoTkIIT5o7k4j2LHGATUDoFaMEoxdfspPV1GhI dpVQ== 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=jhBAW35p4b2oJLfuOUmbxBtG0fjoiGHBapgzYTQ+zbE=; b=kDMqSMX1TzEVfDKAEra5lP8sC+byKh+8z0coqcDktQyvGhmQxhfJhDb8sz38knjmpb OD3rhC2z4mWtKfBIYkhN36PWHde1VgDql7wQrwf7wL8qY64xb5WtTeyz3ouxIk53NTd7 9JbQwAdB8Lf0AKbxPEzwGM/0tTBpeCNk+w/b6bzf+6tUCNZr1pkzvyZkQnglbOHCKBJ4 StMIYds3M4LbJpbl0Wem8+2B3OQlCLV6v5Lr1cjsJ1rijFCKyS+loeqCZ+zyv9v3giRK P50Kly6QyA073LHdg220VufxD8Gh7t4EceNNg6dgwsPqTuNUDnTnx5mW5Tt7gi6jfptT Aiog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=inoHGQEW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id jy15-20020a17090342cf00b001cc38a6cb77si604291plb.204.2023.10.31.00.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 00:33:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=inoHGQEW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 7FAF080324E3; Tue, 31 Oct 2023 00:33:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343707AbjJaHcz (ORCPT + 99 others); Tue, 31 Oct 2023 03:32:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343679AbjJaHcy (ORCPT ); Tue, 31 Oct 2023 03:32:54 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.196]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8BF16DB; Tue, 31 Oct 2023 00:32:50 -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=Mn+LM vMphVLhG3rmeD4sGc/rl3mIEcUGZbCfQih6dB0=; b=inoHGQEWq45H7OWt9vxdL gcEu5WHlChE2gTYy/LCfyYSTURKT8BhJqux+5873ahRiKlH+ka0xkMxVQM8meOM6 Viizx/mgLGPzBXgT4l2vkBRbBx6JTOPxMTNa6hYgqFO7Qtfq9CDL7SCnmCF1Rw1F zfOZ6cIjYF4IQ8ZBNA6S1o= Received: from leanderwang-LC4.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g3-3 (Coremail) with SMTP id _____wBn3ojfqUBlwMj4AQ--.51864S4; Tue, 31 Oct 2023 15:16:50 +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 v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Date: Tue, 31 Oct 2023 15:16:43 +0800 Message-Id: <20231031071644.20086-3-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231031071644.20086-1-zyytlz.wz@163.com> References: <20231031071644.20086-1-zyytlz.wz@163.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wBn3ojfqUBlwMj4AQ--.51864S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1rGr1fJr1kJFW3KF43GFg_yoW8KF48pr Zagw4DCFWUGrs0gr48Aa4UZF1rC398tF12gr4S9wn3Z343XFs7Jry0ya4IqFWIyr9rCa4r Zr1F9a4xJr4DZFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRLYFZUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/xtbBdgQaU2Dkptft2gAAs5 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 fry.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 (fry.vger.email [0.0.0.0]); Tue, 31 Oct 2023 00:33:04 -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 --- 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