Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9102010pxu; Mon, 28 Dec 2020 06:41:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyKAcaRQQ8mO7QiItHzyr+znUFDgDlykG8VBQKGVYWVgRAeTbmhvMNY+GjQEf0BtjPiFpI X-Received: by 2002:aa7:d5d6:: with SMTP id d22mr42036152eds.160.1609166477392; Mon, 28 Dec 2020 06:41:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166477; cv=none; d=google.com; s=arc-20160816; b=ypz8d42GF8frIrDRhu4MyPR0qzAl7e0fK02V2JOt9gw2Jqnkg2yxEl5Exx8QlVN9jr a5DuHThRzAPPGcVHyJ8aje7aYM+BetF7ANhYs/iy948nnAxYpA2mBlv6WDwPEmbhORJQ /YTHtX2EbfmTbcPKTH3HsY6hRo+IJExw/DsY1ZGZM7NRn0WcKAf0kz6s5rXcEZYJh/73 VWdYMtFml/v/UgxnHoeuQxtrIbjA1azdT+BGv9bdE3IDFiCTECn/PEwzoF6bGGn747l9 O2rz11FPDKfNAivXbdh+ISh/SvHeF9t+2w5Dw404PW52nvFzyC8rM/keZctMJ7vXUko+ J1aA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pUhZtHm9nkwDpMIxi1KPDQnuuixybpiQXdfLjp/J0hA=; b=qjW9wZw6NjM6dJbwfkdJR1mbsMiTYySl9FLdUMeQYZcCcv43/nd/OD+tkjsHtQ3S8z rsFk7rHW/skoRlp9defSWeojTZ3sRHFi5mma08xJdm5evyInvUrOEK/i2YTCiRzPwfmk 2OBil6UpoCn2HBf0Ojf1CiRI/B0Z3kTz44JHVS7W2JmIFAEILyq8QZguWZj+jLjl/fO5 tb7LCnRGFdmoQvaJ6asMt8KYFCvjs85IJv29kNSNKKCHfVkA0sTcHhr6E17dJlELewEl yBLsQo8lN5TzMVphQw+qkhs3TePc6lne/xwaV2/oYHfsFhywQ91ZWGWQ7FUywkNNmXy3 Jc3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2KhGGxLI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o11si19041938ejg.118.2020.12.28.06.40.53; Mon, 28 Dec 2020 06:41:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2KhGGxLI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2504745AbgL1Och (ORCPT + 99 others); Mon, 28 Dec 2020 09:32:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:39888 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2504570AbgL1Obi (ORCPT ); Mon, 28 Dec 2020 09:31:38 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 62F1D221F0; Mon, 28 Dec 2020 14:30:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165857; bh=GRGnv1kv8/gEUPXoQjFlI3y8IkFzSXCvc5nt/zfAd+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2KhGGxLIuKK+Ac+pNH/SiowpnpEGSd0I5g4rXfvgTqYF8VRaVi0J2k4aqXCNAU/b+ Z2mY5w81HrYKG0zIcIDey3QJXqgTjtheXO2AtfUoe2BEhSu8F80Xlsa2FizVKtdHjM S7vtHAsJLGbqtAMi2nEKjk1kFPy/WoK+WU/hgw7k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lukas Wunner , Linus Walleij , Navid Emamdoost , Andrey Smirnov , Mark Brown Subject: [PATCH 5.10 648/717] spi: gpio: Dont leak SPI master in probe error path Date: Mon, 28 Dec 2020 13:50:46 +0100 Message-Id: <20201228125051.992756403@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lukas Wunner commit 7174dc655ef0578877b0b4598e69619d2be28b4d upstream. If the call to devm_spi_register_master() fails on probe of the GPIO SPI driver, the spi_master struct is erroneously not freed: After allocating the spi_master, its reference count is 1. The driver unconditionally decrements the reference count on unbind using a devm action. Before calling devm_spi_register_master(), the driver unconditionally increments the reference count because on success, that function will decrement the reference count on unbind. However on failure, devm_spi_register_master() does *not* decrement the reference count, so the spi_master is leaked. The issue was introduced by commits 8b797490b4db ("spi: gpio: Make sure spi_master_put() is called in every error path") and 79567c1a321e ("spi: gpio: Use devm_spi_register_master()"), which sought to plug leaks introduced by 9b00bc7b901f ("spi: spi-gpio: Rewrite to use GPIO descriptors") but missed this remaining leak. The situation was later aggravated by commit d3b0ffa1d75d ("spi: gpio: prevent memory leak in spi_gpio_probe"), which introduced a use-after-free because it releases a reference on the spi_master if devm_add_action_or_reset() fails even though the function already does that. Fix by switching over to the new devm_spi_alloc_master() helper. Fixes: 9b00bc7b901f ("spi: spi-gpio: Rewrite to use GPIO descriptors") Signed-off-by: Lukas Wunner Reviewed-by: Linus Walleij Cc: # v4.17+: 5e844cc37a5c: spi: Introduce device-managed SPI controller allocation Cc: # v5.1-: 8b797490b4db: spi: gpio: Make sure spi_master_put() is called in every error path Cc: # v5.1-: 45beec351998: spi: bitbang: Introduce spi_bitbang_init() Cc: # v5.1-: 79567c1a321e: spi: gpio: Use devm_spi_register_master() Cc: # v5.4-: d3b0ffa1d75d: spi: gpio: prevent memory leak in spi_gpio_probe Cc: # v4.17+ Cc: Navid Emamdoost Cc: Andrey Smirnov Link: https://lore.kernel.org/r/86eaed27431c3d709e3748eb76ceecbfc790dd37.1607286887.git.lukas@wunner.de Signed-off-by: Mark Brown Signed-off-by: Greg Kroah-Hartman --- drivers/spi/spi-gpio.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) --- a/drivers/spi/spi-gpio.c +++ b/drivers/spi/spi-gpio.c @@ -350,11 +350,6 @@ static int spi_gpio_probe_pdata(struct p return 0; } -static void spi_gpio_put(void *data) -{ - spi_master_put(data); -} - static int spi_gpio_probe(struct platform_device *pdev) { int status; @@ -363,16 +358,10 @@ static int spi_gpio_probe(struct platfor struct device *dev = &pdev->dev; struct spi_bitbang *bb; - master = spi_alloc_master(dev, sizeof(*spi_gpio)); + master = devm_spi_alloc_master(dev, sizeof(*spi_gpio)); if (!master) return -ENOMEM; - status = devm_add_action_or_reset(&pdev->dev, spi_gpio_put, master); - if (status) { - spi_master_put(master); - return status; - } - if (pdev->dev.of_node) status = spi_gpio_probe_dt(pdev, master); else @@ -432,7 +421,7 @@ static int spi_gpio_probe(struct platfor if (status) return status; - return devm_spi_register_master(&pdev->dev, spi_master_get(master)); + return devm_spi_register_master(&pdev->dev, master); } MODULE_ALIAS("platform:" DRIVER_NAME);