Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2622242pxj; Mon, 31 May 2021 06:47:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySneBSIjLd7w8qJDYY4FC7nRKBsGHPnunsI86v4ahbX5yWCTovNZaG6mH724kKvL6YmPno X-Received: by 2002:a05:6602:158d:: with SMTP id e13mr346980iow.147.1622468831506; Mon, 31 May 2021 06:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622468831; cv=none; d=google.com; s=arc-20160816; b=urjhOjuuuGXWvWUoKdPvHNvDDVJvyu/Y3/aaFkfxz7ijpTs2x//IJojwHV4tQ6BUWS fixtCjYaTRbiqm6XnhEe35LPBc+yagwN/as+C80lt3U7Omr9MiwFyexn/FqXeJdc4NRL GxezpnomVeGsqxBc8Snr30gG9XhUA+RnFZF8+vRq/HT8+51K/wL9goNatGODmJLkXqXk TIw+W57baSVpOS/pwAhhE5kQ2dL7jOqX77yH4NsKp0+hMZV+juM2Vn8MqRpX67K4RVim 64bLNvN1Z2j8dOxik4vq6ixE8zylSEhHIlAJeQRS39Tk3nRNgcTyZzI5SKs831q8gyLo J6xA== 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=PKScnacgIVpr8lO+oln4AKGRSoQwxQAhf20SM9vFK1s=; b=fDsu6evzELEBGhDcaZJlWM/FToT/nOlyHnKfx2yS7Ox5PTF0YtLIc6wKcQDUCw16D7 U2p6eKJFrucaqMNNKlXrl7fy8pbeCAYQY6eF7tzBM5u2oAex6bb7bcA7h/2aYFBHTU7a ejaFkhG+uI/t1ps/AqulWI/gOZvJ+yqijUOpISOI49DzjC3Za6d0Zb1Z0dag27CZz0V5 r3yxkD5SK5rBV0f2wD3pdlsMRZzWUEB+M9gWp8lKGhsm42oFBRI5j0CSfkR+0BRnA4LK PfHVRMl2HzWasJS9FvBFXYvH6jBBGWkl3rrO8YPfQ57kdALbjyj1sh+Prj1gMaqJTtEm EDBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rTlE0oo3; 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 c15si4085718jam.64.2021.05.31.06.46.57; Mon, 31 May 2021 06:47:11 -0700 (PDT) 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=rTlE0oo3; 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 S231663AbhEaNqb (ORCPT + 99 others); Mon, 31 May 2021 09:46:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:39364 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232378AbhEaNbb (ORCPT ); Mon, 31 May 2021 09:31:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 104F3613C8; Mon, 31 May 2021 13:23:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622467424; bh=+w22d5Lh5qfl2TsfF9L41caHmM9xpcYax38cT+6efQY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rTlE0oo3D6C5t/ykWNWrAJ7XH+bppPjU2WsUBZh8AilwnkNnNeXu6L0hWbiLsFOZS YqGhQ+kGa8PDNQh3qOMvki1LhIl7OuXyQ16UsVw8am1QwqF4EYJOGmIo3o5EJ9upsd Ol0OL0iES+k5EQPFKDIHyoajaoGkKja2herfqcw4= 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 4.19 059/116] spi: gpio: Dont leak SPI master in probe error path Date: Mon, 31 May 2021 15:13:55 +0200 Message-Id: <20210531130642.176191748@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130640.131924542@linuxfoundation.org> References: <20210531130640.131924542@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 [lukas: backport to v4.19.192] Signed-off-by: Lukas Wunner Signed-off-by: Greg Kroah-Hartman --- drivers/spi/spi-gpio.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) --- a/drivers/spi/spi-gpio.c +++ b/drivers/spi/spi-gpio.c @@ -382,7 +382,7 @@ static int spi_gpio_probe(struct platfor return -ENODEV; #endif - master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio)); + master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi_gpio)); if (!master) return -ENOMEM; @@ -438,11 +438,7 @@ static int spi_gpio_probe(struct platfor } spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; - status = spi_bitbang_start(&spi_gpio->bitbang); - if (status) - spi_master_put(master); - - return status; + return spi_bitbang_start(&spi_gpio->bitbang); } static int spi_gpio_remove(struct platform_device *pdev)