Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C5E68C4332F for ; Mon, 12 Dec 2022 15:30:21 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2A4588539F; Mon, 12 Dec 2022 16:30:19 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1670859019; bh=hheyLw+/Dur01EmHbw5vX5TrWa9waheqOH6ufCNPcV4=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=HBlV+krWlC3GGJqErr+lvynqy5QCfa0rHwleOxs0lp+6qs+9RF8UkPzySp7t6BLWr WUivHqJsC4fXYMtC5NasCwupQcAM02COQ0ANkN6kRg8HU4Y3GR7Rv2TlJdFLGphxfA P50T95Ym1rs01LX50ummKjn70OWuOkZIY0NQyjKVe9U791b9IpW3XlNWc6W2HiesPp pkwJBGl/OIInQVB3MoOiDQBpomLKzjbut65dXYdb6N2zhbUxL7CmqlARRgxDV5/d2z +FO4R8PDa86LwXxRRXlVukLTdFaoKpMabqb/b0aIx8Las/nLN9tK5Ecy4IATRwjPRT cjHsENBEZ4fKw== Received: from aldebaran.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lusus@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 90F5184913; Mon, 12 Dec 2022 16:30:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1670859016; bh=hheyLw+/Dur01EmHbw5vX5TrWa9waheqOH6ufCNPcV4=; h=From:To:Cc:Subject:Date:From; b=vkOIJKLJTOL/2LDhnkQH0wy5RDWlchGHt01uSsOiNixKMddi02GYJoGRljJo7Y76i +LlkKLoZLHz+s5zwAR4ebYRCFGNJ4xm8LkBNncSEyz+gbKt2oPlUFir0r9jTospStH kkdOzHWxEgxOCFUlNqz/H2d+UgDapLOAUBtdlbpcfRreLlh9S4fq7cdO1TfpGSinQF D9Gr9imUZGhjPVSNc+yxQ7mq0UeQMl35MJpLQAorg7PUA0fqe4djyZ8EGipUSjux5e /uRT4hstsXCJFvXlDjnVlOWpDNuCi4mxH109uOgojY0e31fFtxZqaVOMwcwMPKeead 0Lera0cCYqJCA== From: Niel Fourie To: u-boot@lists.denx.de Cc: Niel Fourie , Ramon Fried , Marek Vasut , Lukasz Majewski Subject: [PATCH v2] net: eth-uclass: revalidate priv after stop() in eth_halt() Date: Mon, 12 Dec 2022 16:29:44 +0100 Message-Id: <20221212152945.1870385-1-lusus@denx.de> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean In eth_halt(), reread and revalidate priv after calling stop(), as it may have been freed, leaving a dangling pointer. In the ethernet gadget implementation, the gadget device gets probed during start() and removed during stop(), which includes freeing `uclass_priv_` to which `priv` is pointing. Writing to `priv` after stop() may corrupt the `fd` member of `struct malloc_chunk`, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free(). Signed-off-by: Niel Fourie Cc: Ramon Fried Cc: Marek Vasut Cc: Lukasz Majewski --- Changes for v2: - Revalidate priv instead of changing state before stop() - Added explanational comment This patch my be dropped if the patch which addresses the root cause ("usb: gadget: ether: split start/stop from init/halt") is accepted. net/eth-uclass.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/eth-uclass.c b/net/eth-uclass.c index f41da4b37b3..7d5783b5cab 100644 --- a/net/eth-uclass.c +++ b/net/eth-uclass.c @@ -341,8 +341,11 @@ void eth_halt(void) priv = dev_get_uclass_priv(current); if (!priv || !priv->running) return; - eth_get_ops(current)->stop(current); + /* Ethernet gadget frees priv during stop, workaround until fixed... */ + priv = dev_get_uclass_priv(current); + if (!priv || !priv->running) + return; priv->state = ETH_STATE_PASSIVE; priv->running = false; } -- 2.38.1