Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp71226rdb; Tue, 31 Oct 2023 00:26:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXraOIOrVXYmMfenHQiHo4r7th6RhKJLaBwsdju9P+BuabBScCByN1MneVyQTwJe/ouawN X-Received: by 2002:a05:6808:f09:b0:3a1:e7fb:76fc with SMTP id m9-20020a0568080f0900b003a1e7fb76fcmr15557754oiw.17.1698737198415; Tue, 31 Oct 2023 00:26:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698737198; cv=none; d=google.com; s=arc-20160816; b=JexTf4wueZS2f6N5kbJJfPHmrH+tITdGW3hQIH9UP9t6sRBsPtPYJ970E3VXvnTTTm 8qbOC+5lYk9f7japM25tPsNo8WtFGtkr3UUBLno/6B2mpDuBkWHx+4ElrrMuA8vSpgZj sex8QtOjxIuXG/uVynYQxotpQvzQgXOWED11MF3dozhV/NfC8/23h/D0mEYC87W42F9G vS2WOcrn2zBPfvVauXSUoqXwIqGK/35GEMSf4Q/PKXFfWrWNBUudzIo7c7SXev8GpJSd 7s8t5n3kNOgwSlqj7h0AGsYQeI5euegZ0UezL9idB+g9LDa6pG0uQaZSYk5oTaetQFzL s8Yw== 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=m5rDevM6mq47exVmNmcdoqBiutr2WJH740i+crgJziw4qITiLknSG74fzCuWIO54ab +JzdYpnKU1NAD9ChGe+oDpItxXnwR0Fv3oh3LepYtdEzBqV4xPbsLXTaT51U2jJ7ugZ/ z4+Bwiwn4LExH8a5LIse191jC7XrnrXBVQB1c0GzzQDFWb7JXnCiUIpaSkkVaWU1KV+t arz/STozuMeN0Vksu2a08rtvR1g/rn0jfe2Rt9s0LDezeD22TrzmsmabJ+nxyp4/GLKL FAPeCIxcp1+OakjWwwlvbgJPxyK/C27bIMthmDruROqh6zfWL/lFYAq1jMunP9W005Fz GTPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=Mes0fKhH; 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 i130-20020a639d88000000b005774cf04028si626151pgd.764.2023.10.31.00.26.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 00:26:38 -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=Mes0fKhH; 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 6BF5980A1653; Tue, 31 Oct 2023 00:26:32 -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 S1343606AbjJaH0S (ORCPT + 99 others); Tue, 31 Oct 2023 03:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343733AbjJaH0B (ORCPT ); Tue, 31 Oct 2023 03:26:01 -0400 X-Greylist: delayed 480 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 31 Oct 2023 00:25:43 PDT Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.214]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1EF54119; Tue, 31 Oct 2023 00:25:42 -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=Mes0fKhH6e6cs5tzJFYB0 32joL+cSIJU3YdEePznWPbUFvzOMLE8iBOXw+mjhqJAg1RW07I5e1ycGmr3rLIIa tPnVhec9C8RY7QQpdJCHqs3k1gh9e8l6Md2DAkl/zmUjyqfedPazaHLetzdRvttJ uVs0alVwXTpi3eTDcKzlTg= Received: from leanderwang-LC4.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g5-0 (Coremail) with SMTP id _____wDXP+6wq0Blv5KVAQ--.8863S4; Tue, 31 Oct 2023 15:24:32 +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:24:28 +0800 Message-Id: <20231031072429.21448-3-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231031072429.21448-1-zyytlz.wz@163.com> References: <20231031072429.21448-1-zyytlz.wz@163.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wDXP+6wq0Blv5KVAQ--.8863S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1rGr1fJr1kJFW3KF43GFg_yoW8KF48pr Zagw4DCFWUGrs0gr48Aa4UZF1rC398tF12gr4S9wn3Z343XFs7Jry0ya4IqFWIyr9rCa4r Zr1F9a4xJr4DZFJanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UouWLUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXAMaU1Xl78ZMQwAAsA 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]); Tue, 31 Oct 2023 00:26:33 -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