Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1005786ybi; Fri, 2 Aug 2019 07:51:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+c3Pop+Xvxv0Qb1ydIj3T+rgHc3WCgk82+XZprJXbQL87PJXAOoQl85Szgf4l+4eP6mwK X-Received: by 2002:a65:464d:: with SMTP id k13mr111980065pgr.99.1564757473127; Fri, 02 Aug 2019 07:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564757473; cv=none; d=google.com; s=arc-20160816; b=a+jtt/E5QtjXRRlDs6AlPYOLJRjv1UxFTJfX02En0LMwo9gOtOO/xVssFVMXwJY5JV 3acNYYCjPY4dkLyhtcxDCsH2qJh8ntKsdQZQihw9gtIKLVLg4oD+MEzrxYxiVHw4EzJo 8SIKFKwT+/VlEteYI03kZ4NIPzRwJEgC/5Iz06YK7rodeay/dpPIZqky4il9u2FpT6tu nuFUoyLWTfThGC1Y+eqNjOwRsJN2yE+C59YUArFkhZCI4UqUgnIVRb5O7PVvXI+S7kck cOUc/uf2eyf9wJI8/rEJj2D9dvmSvpdyXzaUjhUUcyPWicsAMW41BCc7L+oeYn2wB6JS w+mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9wLNqI4Hyg9ypGude6QgxnvGgQxUme+xBBWJbFmUVtI=; b=G/EsPy1xap7njzXFOMTNjjRwyoSktYk8vIdw0lrL7d5SoTj//VJdcFpzBX215apSaL GiBm7jR8rKdVb8CmzOzzuTiBs1DL+h3ohizqr6Ta2RvolskEFPOKzsE7B5yuL/NaY2V6 CcD4pDZUkBaywVjM5S6ushHvAYHsoI1Qp2U7neDaMSspq7NJ4oRPk4gvmZdeoX1enV/z Z4H5v7oK5ETaVMcN1nLwDH90eMDxWJoIRBbktG+QmCAE2KEQ+0mYbNqr3YmB/g6M4oYO rNL+pVnl09WprvBVdy0IU797qNk7GrKsJJ7sYJXs64VFyHc2WUHps3KOpXQjZDaofuYk acSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="KoXCmVu/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l6si40454473pgr.378.2019.08.02.07.50.57; Fri, 02 Aug 2019 07:51:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="KoXCmVu/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389113AbfHBJaG (ORCPT + 99 others); Fri, 2 Aug 2019 05:30:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:55430 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733176AbfHBJaE (ORCPT ); Fri, 2 Aug 2019 05:30:04 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 29C552183F; Fri, 2 Aug 2019 09:30:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564738203; bh=VgKfAdirD3uxO5u4ONPWgq0IJsZefFAqt1zZ6VWEGbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KoXCmVu/U0vS5Lm14TG2YJC7VPqGwZ7iNieIjiIKIJjbM9JjbWMb1U0/2UVszLr+W JMTbg/oYEdo6EVof0cViHG60zCDS4BCSf9yXl6PMY4g1WyHDo9+FJ6p+CYgp7PhmhG OkdAiysnQV6alYCi05R045/oCeg5p96tzIeVI8Hc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sven Van Asbroeck , Robin Gong , Vinod Koul , Sasha Levin Subject: [PATCH 4.4 003/158] dmaengine: imx-sdma: fix use-after-free on probe error path Date: Fri, 2 Aug 2019 11:27:04 +0200 Message-Id: <20190802092204.300487250@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190802092203.671944552@linuxfoundation.org> References: <20190802092203.671944552@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 2b8066c3deb9140fdf258417a51479b2aeaa7622 ] If probe() fails anywhere beyond the point where sdma_get_firmware() is called, then a kernel oops may occur. Problematic sequence of events: 1. probe() calls sdma_get_firmware(), which schedules the firmware callback to run when firmware becomes available, using the sdma instance structure as the context 2. probe() encounters an error, which deallocates the sdma instance structure 3. firmware becomes available, firmware callback is called with deallocated sdma instance structure 4. use after free - kernel oops ! Solution: only attempt to load firmware when we're certain that probe() will succeed. This guarantees that the firmware callback's context will remain valid. Note that the remove() path is unaffected by this issue: the firmware loader will increment the driver module's use count, ensuring that the module cannot be unloaded while the firmware callback is pending or running. Signed-off-by: Sven Van Asbroeck Reviewed-by: Robin Gong [vkoul: fixed braces for if condition] Signed-off-by: Vinod Koul Signed-off-by: Sasha Levin --- drivers/dma/imx-sdma.c | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c index 4054747af0cb..dd97dbf6618c 100644 --- a/drivers/dma/imx-sdma.c +++ b/drivers/dma/imx-sdma.c @@ -1786,27 +1786,6 @@ static int sdma_probe(struct platform_device *pdev) if (pdata && pdata->script_addrs) sdma_add_scripts(sdma, pdata->script_addrs); - if (pdata) { - ret = sdma_get_firmware(sdma, pdata->fw_name); - if (ret) - dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); - } else { - /* - * Because that device tree does not encode ROM script address, - * the RAM script in firmware is mandatory for device tree - * probe, otherwise it fails. - */ - ret = of_property_read_string(np, "fsl,sdma-ram-script-name", - &fw_name); - if (ret) - dev_warn(&pdev->dev, "failed to get firmware name\n"); - else { - ret = sdma_get_firmware(sdma, fw_name); - if (ret) - dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); - } - } - sdma->dma_device.dev = &pdev->dev; sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; @@ -1848,6 +1827,33 @@ static int sdma_probe(struct platform_device *pdev) of_node_put(spba_bus); } + /* + * Kick off firmware loading as the very last step: + * attempt to load firmware only if we're not on the error path, because + * the firmware callback requires a fully functional and allocated sdma + * instance. + */ + if (pdata) { + ret = sdma_get_firmware(sdma, pdata->fw_name); + if (ret) + dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); + } else { + /* + * Because that device tree does not encode ROM script address, + * the RAM script in firmware is mandatory for device tree + * probe, otherwise it fails. + */ + ret = of_property_read_string(np, "fsl,sdma-ram-script-name", + &fw_name); + if (ret) { + dev_warn(&pdev->dev, "failed to get firmware name\n"); + } else { + ret = sdma_get_firmware(sdma, fw_name); + if (ret) + dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); + } + } + return 0; err_register: -- 2.20.1