Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp136534rdh; Wed, 25 Oct 2023 19:37:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFs6v6bJ7feTeDrJnzwY7Ub8A/aj+P2Hl1ydynCX3e+EmalvPX9hlzhTTq7FkfiibCkJuT1 X-Received: by 2002:a81:6f04:0:b0:594:e2e6:25e8 with SMTP id k4-20020a816f04000000b00594e2e625e8mr16334951ywc.48.1698287861623; Wed, 25 Oct 2023 19:37:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698287861; cv=none; d=google.com; s=arc-20160816; b=RjC+wMT4XWh7haJi3WyqFkj2xD0QOjIqWK6C3O3nyuu3UyH0nfxNW2gH+/D4hEpBhP AByh9im4Dss8IFCFHrjCyX9ez+hTVB3lGduwI9/Q5pnBq9z1UOQXwb4SU9wC4R+VKvoo BeKKD4V1+9LQrt8bbKVU63S8tpeTzoU4urJdysdOO6cC3mTxHMgR9rlZ1y76jlVpuASl esmmCuZYVu4NMWjADbB4e4zSxN4+GgZ0wv/wxNeACLDjTqAzoTT+sMQKgjLRqiaiszuX TV1i+/zKeBbA2Sm2E28nZ8/792e0q6KR/qaM1094+AolRnqESTg/sF1zi6eHgChrJnMx ST3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=O+/LYLyDdbkps/oxv4vLLfhE34izDmpgctZLGQjlrOU=; fh=S6uE2S2nFWFA86xRCB9SdRKVabwxlnlTf093FZwlnY8=; b=lIxlxUaEtDU9zLjUQKsLHwALB2MYPn42O4najqHf9V7Af6maVb4WOwoUhQQUvNutQW CoIgnP8NxvuJ40tROW4OIHNx8yvJbrStiapMBVqWlLBRMf4OK/Eura5MDsmMt1aVaxjF sfqkh6zOdWyWYDhOFWg0Q3+R79gr2TaBugRQAML3WYKoAZMBfoIm76E7C8IGzW+uYAds a+nWlRp02jAb/BbTBmhE3A+OYiTi6BTaZxOgs+Ekte6Sr9fV+CIAXMWktovSQMpBhOBB vBWM2xy3IaGDk0IeykiDiypP34ggOiq764krCmhqDmgR89fFJ+1I8YI5unWzGclnu2xQ x18w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=LnVHNher; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id v188-20020a0dd3c5000000b005a7be928f8bsi11925640ywd.272.2023.10.25.19.37.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 19:37:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=LnVHNher; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 6B9D881904FC; Wed, 25 Oct 2023 19:37:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232626AbjJZChZ (ORCPT + 99 others); Wed, 25 Oct 2023 22:37:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229596AbjJZChY (ORCPT ); Wed, 25 Oct 2023 22:37:24 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9613910A; Wed, 25 Oct 2023 19:37:20 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-5b8f68ba4e5so376451a12.1; Wed, 25 Oct 2023 19:37:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698287840; x=1698892640; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=O+/LYLyDdbkps/oxv4vLLfhE34izDmpgctZLGQjlrOU=; b=LnVHNherEob6mbFgxWHG35CRUalku9oJrHnFwANijraFbynH9RP9hMyFoHx4HQfPfd 5FxJqa9EpINl1QIc1rjlkmvhD/yjsAyKtDUeDJ3HVu663POU4MhESOqdH0TC5v4ysPQu FYFQqcmSsRTIytnb7iRufo1cjLEMmnVwO4pv8re+MhC8om909AeaZj714aVh1vFb8HiY suktaswu3wpHJjTqJcOwGq+789NhP6GtaIxhq5GCzuQY85iykfU7iTDzprsovQmsUixQ tNS8hwSTJtjJtEdRD1wBnNMe+BluqcxumV4TLUV9RUcNKPICUiZ0SeNozH30lHb1VVBL SUKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698287840; x=1698892640; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O+/LYLyDdbkps/oxv4vLLfhE34izDmpgctZLGQjlrOU=; b=DmUv9C+YLCblKlC1vaszGWK/w+bfew4JJreToSTOEQiwhZQp/8tnztqvJdbWL521wa EE7N/ciZ3zZhQ3JK6jeBlUQ8EyeoPwMierS1mYr6WUvucqxTCKMnotSGvgfeTc/GB/CH 1UwmEro34Tz0xlSRzxS/ryRFWdwDZicTzYY5DH/BFc2kGSm86iu/376VRUbut81kjn38 tQU9kac8O8l9FIit3PGOX+jeAGk1cxFqhHeLHyM5MVTU6jwA2UY+BtelaV8fvRQgX7Zc yjm9R5fSTfPLqA49mCu2kkzN2WCwzt85LavCQMwF2eWcMPq8ZmLQL3ZNaoNCu3kP5Z/d eQeQ== X-Gm-Message-State: AOJu0YzZhJQmmGiOOlaoq2F9CRT1TNlq6fQdBvmNmehS00J3bjy89R/H 9t4Ux7eCzVf2vnrA2HseiVUs/4bpn6zc4XZm62g= X-Received: by 2002:a17:90a:4f45:b0:27e:3880:d03d with SMTP id w5-20020a17090a4f4500b0027e3880d03dmr10065765pjl.7.1698287839741; Wed, 25 Oct 2023 19:37:19 -0700 (PDT) MIME-Version: 1.0 References: <20231020040732.2499269-1-zyytlz.wz@163.com> <46f704c2-ba42-4e16-d798-ea2171e5ba16@collabora.com> In-Reply-To: <46f704c2-ba42-4e16-d798-ea2171e5ba16@collabora.com> From: Zheng Hacker Date: Thu, 26 Oct 2023 10:37:06 +0800 Message-ID: Subject: Re: [PATCH] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run To: Dmitry Osipenko Cc: Zheng Wang , 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, 1395428693sheep@gmail.com, alex000young@gmail.com, amergnat@baylibre.com, wenst@chromium.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 25 Oct 2023 19:37:38 -0700 (PDT) Get it. I'll figure it out how to split up. Thanks, Zheng Dmitry Osipenko =E4=BA=8E2023=E5=B9=B410=E6= =9C=8824=E6=97=A5=E5=91=A8=E4=BA=8C 21:18=E5=86=99=E9=81=93=EF=BC=9A > > On 10/20/23 07:07, Zheng Wang wrote: > > 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 start timeout worker only if started jpegdec worker > > successfully so the v4l2_m2m_job_finish will only be called on > > either mtk_jpeg_job_timeout_work or mtk_jpeg_dec_device_run. > > > > This patch also reverts commit c677d7ae8314 > > ("media: mtk-jpeg: Fix use after free bug due to uncanceled work") > > for this patch also fixed the use-after-free bug mentioned before. > > Before mtk_jpeg_remove is invoked, mtk_jpeg_release must be invoked > > to close opened files. And it will call v4l2_m2m_cancel_job to wait > > for the timeout worker finished so the canceling in mtk_jpeg_remove > > is unnecessary. > > > > Fixes: b2f0d2724ba4 ("[media] vcodec: mediatek: Add Mediatek JPEG Decod= er Driver") > > Signed-off-by: Zheng Wang > > Signed-off-by: Dmitry Osipenko > > Cc: stable@vger.kernel.org > > --- > > .../media/platform/mediatek/jpeg/mtk_jpeg_core.c | 13 ++++++------- > > 1 file changed, 6 insertions(+), 7 deletions(-) > > > > diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/dri= vers/media/platform/mediatek/jpeg/mtk_jpeg_core.c > > index 7194f88edc0f..c3456c700c07 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, > > @@ -1403,7 +1403,6 @@ static void mtk_jpeg_remove(struct platform_devic= e *pdev) > > { > > struct mtk_jpeg_dev *jpeg =3D platform_get_drvdata(pdev); > > > > - cancel_delayed_work_sync(&jpeg->job_timeout_work); > > pm_runtime_disable(&pdev->dev); > > video_unregister_device(jpeg->vdev); > > v4l2_m2m_release(jpeg->m2m_dev); > > @@ -1750,9 +1749,6 @@ static void mtk_jpegdec_worker(struct work_struct= *work) > > v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); > > v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); > > > > - schedule_delayed_work(&comp_jpeg[hw_id]->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, > > @@ -1762,6 +1758,9 @@ static void mtk_jpegdec_worker(struct work_struct= *work) > > goto setdst_end; > > } > > > > + schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work, > > + msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC))= ; > > + > > spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags); > > ctx->total_frame_num++; > > mtk_jpeg_dec_reset(comp_jpeg[hw_id]->reg_base); > > What about to split this patch into 3 patches: > > 1. will remove cancel_delayed_work_sync() > 2. will update mtk_jpeg_dec_device_run() > 3. will update mtk_jpegdec_worker() > > The reason for splitting is because the multi-core mtk_jpegdec_worker() > doesn't present in older stable kernels, and thus, the patch isn't > backportable as-is. > > -- > Best regards, > Dmitry >