Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp921349rdb; Fri, 20 Oct 2023 03:32:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHBU3NXuXzIBpgqSubY03U7oxaQvixozBcy3yrETH4YKaSOvboQiODb/an2myte193vQElH X-Received: by 2002:a17:90b:3e8c:b0:27d:433e:e69c with SMTP id rj12-20020a17090b3e8c00b0027d433ee69cmr2232519pjb.18.1697797955538; Fri, 20 Oct 2023 03:32:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697797955; cv=none; d=google.com; s=arc-20160816; b=V8rkB7qoFg7wy1iiYgvqJo81/aajagtbTUJFl/cQhprwVpcetH3AFO42PmSTRYdJAV Q3O+c2ga8zBgE94zQOlWF495WSio68SN7E/56bgRLiZplnPPW2yaTI+89ZXA9mYDYcj9 7woFf2V7Sq/Ttnu1ePJiVdJ22z1GpHzD//rlSB07ZLY7uVaMzskEKmQ/Sd0fccv4UGXa JFf0BWM8yAlD2LdA8X6KFwbZCW3yB/NLwrlLtq0xNjhqrlaRjRGqjqD44zVnZvi6lLWh qBCwd4vali7B8CKXXpJExuM7SgNd5BCuu2Uv5Ek4O9juj8RmmAdw9vtAqdyiFHjyTNjd ImYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=igrslGOc8lOfnbcsO3yOOnOBbB2z/+ZB5557ersesus=; fh=8exgOxvjFlXS6YcQh3Al/BSG5vg3IK6zZ+RPpMev7vY=; b=CU4tOsrY8oiyxMqZ4Qei/nlXsSZ3yAVe7wzpcQzHTut8IT92nisUFP/wuT92ulTLzQ xAAWUvTtwZkOpozgJER1WGQLwlgkO52/7R3Q6THb5W49yUmqMz0LzrmIXCbaSvh5dikS 9VmJ9K2FA7aC31YC/J5qkU49yndKrH/37sK8R5iAej8Re+rOA1AC7tceqDPptg6Cv6oM 7VRhN4u7OYHxq0hrf7DLJnPcT7gcfXn1ueOWfhZ76hQO6pftr7WawoF3tnphiWmVgosS D9hiPEjBqgl+2+9G9KgvKgziiWMsU7o+eLYhAjJtr04cFf/1En+7lMfZie0A33784Dki Gp2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=COix8rpR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b11-20020a170902d50b00b001c58401354dsi1518247plg.565.2023.10.20.03.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 03:32:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=COix8rpR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 20D7180ADF01; Fri, 20 Oct 2023 03:32:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376913AbjJTKc3 (ORCPT + 99 others); Fri, 20 Oct 2023 06:32:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376907AbjJTKc2 (ORCPT ); Fri, 20 Oct 2023 06:32:28 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 821FED52 for ; Fri, 20 Oct 2023 03:32:25 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-32d8c2c6dfdso456417f8f.1 for ; Fri, 20 Oct 2023 03:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1697797944; x=1698402744; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=igrslGOc8lOfnbcsO3yOOnOBbB2z/+ZB5557ersesus=; b=COix8rpRK0gECbprls+QXHnDCg1Z9FbNGRogAEbOrFiXgZ061joGgNvVjBHYDrdSmD N33CJr9SIH/7MlnE0UrxW0xdXKWu0Ufhwge91bnE53zKIT1VCOeLSz1xdT2n0+rlauXx vvDpmDmiGIRE3SpcVb4KKHx2lNPuQ7MIs530PSXKSH4lJLeb85Ac2ZlUM/tbxeODaIgL Sbpn2L2lt3uuXhDuMksx74NLrJR8u4mWlILQ/sz5AnZ533k5/nTRZ3r7ssWE+1JcSi0/ iFt4H5xv6ZSoRny/Nn1F0xUs0cyfkifHbEtOBshUFj/7xOlnkN+2zD14pbLnpDaMopqn ZYXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697797944; x=1698402744; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=igrslGOc8lOfnbcsO3yOOnOBbB2z/+ZB5557ersesus=; b=AhiGGwfy/OpCKKuWey3y5erb8JZKSzorwMQZKjakYU6WG55CWxFBWZVrTGtYCFFHtn wbOraKXSiOXwQBlQsy3AVLk6yCJj3ifvs8FGt/KplAV/3+wDcc+gklilrO/Wn6tSA1ET lBSswCscU0Xl+0j0r6+Y3DBhVb6Z1g6r+uMG65NFyRjHrIjKN7c1IpSVrp6Cmd3sR0ow MxJB/iC6Q62eCS7QiLbb/Kiv+GDZrG0gPqqmODesaneuuvDUre4ajdplWqW9IbVQ4x/s Jgg+698kflavH5i7VaUjvqQ0cDyIFPBCL3llYnPVtXspA6Nix0UC9eB6rIwXYseLDIiZ Pocw== X-Gm-Message-State: AOJu0YygNMmh/ikU+ofR0ePFAf7KxuP1bI5+ItSxP2X+Thq5cN9JowZh PDYuyJ16/fmNPMfLlX6lJJ5d/Q== X-Received: by 2002:adf:ec03:0:b0:32d:d879:1c3 with SMTP id x3-20020adfec03000000b0032dd87901c3mr1041129wrn.31.1697797943877; Fri, 20 Oct 2023 03:32:23 -0700 (PDT) Received: from [192.168.1.172] ([93.5.22.158]) by smtp.gmail.com with ESMTPSA id y10-20020a5d470a000000b0032d9efeccd8sm1396888wrq.51.2023.10.20.03.32.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 20 Oct 2023 03:32:23 -0700 (PDT) Message-ID: Date: Fri, 20 Oct 2023 12:32:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Content-Language: en-US To: Zheng Wang , 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, wenst@chromium.org, stable@vger.kernel.org References: <20231020040732.2499269-1-zyytlz.wz@163.com> From: Alexandre Mergnat In-Reply-To: <20231020040732.2499269-1-zyytlz.wz@163.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,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 lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Fri, 20 Oct 2023 03:32:34 -0700 (PDT) Reviewed-by: Alexandre Mergnat On 20/10/2023 06: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. -- Regards, Alexandre