Received: by 2002:a05:6520:1682:b0:147:d1a0:b502 with SMTP id ck2csp5596808lkb; Mon, 11 Oct 2021 09:39:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyNnIwtSZmPHpvAtL//LCSPL4r5wVHddMFc8bncixeE12QrSgmsojSWKXDdNFjbPTr480x X-Received: by 2002:a17:906:48c8:: with SMTP id d8mr27458826ejt.420.1633970361001; Mon, 11 Oct 2021 09:39:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633970360; cv=none; d=google.com; s=arc-20160816; b=rajdmaJK18bQJ1oToVHdLRaHKL2RuyiIp2puQERM3i9zr+jk2N8EihT7QX4fYBLMlq vEtd6YGaS5ePNAMqw3eH5Z9gSz3J7OuxDkyLDTALd2c9X9VfaIUJH0T5pLzgVYxo6x1e wPYanIP9y5m6OnnaBJUDpAK7ZYxP6UkGfQ1u0RZej7s93MBYwdr+IX4peZi1uYgb/of1 RJ+SE2NuVGs7wZR06c9TI9YDpHSqty8kDxdmi3w4+ZshzCIx5sDuA42ChgTq2rpCqBW8 DO/fGKo6wlrzjyBROnGotoRrDpKNi/OhPaXIFmulP5G9KARy6xma+prbQuAC9mWo+eoc 9Zxw== 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=Y6M0iwqQgOPyDC8HB/eBnTRp3IDjhs50N5GkrygrLEM=; b=uHbmUVY0BPfbX05Zyq2YGPEMp4GIdFXHTUdDc6LDItsMFqpx8KdiCvR7o3RfweBFR9 fAGAAw5vHM0xOLNLYZCxNWl78qJlc6vlFLJE0bLQ14LH2Rdw+TE5oXCs4eC63bwC0Voy 0JqkUNMubweqQH/3uZo7ckiPPCwKJo4y9ix5lSnweZ3y73QTVL18Tz90cVU4P2xdlZJR z7hbA5Qi1CdD8Bit1bcQquzGIDxDE1Kvt2C+CiQHokaTwFk+srAsrfJPtV09dYXyCO99 fasn+QO/qI0Jhz7aCVWyhU9JeeXHbALy/Ikc9SlCQIIGAuUrIKgWmrPepKOJ3HMzpb8K V6lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eYYUNnwF; 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 d5si12888356edn.209.2021.10.11.09.38.57; Mon, 11 Oct 2021 09:39:20 -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=eYYUNnwF; 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 S244125AbhJKOTK (ORCPT + 99 others); Mon, 11 Oct 2021 10:19:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:39266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241233AbhJKOQF (ORCPT ); Mon, 11 Oct 2021 10:16:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C4B8761076; Mon, 11 Oct 2021 14:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633961152; bh=PgHwjF3wtrbnQbGkUF54xuFzIv4x4wSDnP6YuNA+2gU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eYYUNnwFBe1HHUlczi7W3Cf7dVnm9v3zrTMAoixFJCahYjSY2BpG495Jgh/QIZgRO 6bbcvCkcv5OQrpLFTyu2HZgU06ll708Zwo4Rnmi1t5bUOV8XvnHOvUvRKglcq20wJz 1YerMkYH/QIhfB+Mr5sRYIyFrGR76R+Gac2kXswI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Pavel Skripkin , Jakub Kicinski , Sasha Levin , syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com Subject: [PATCH 4.19 14/28] phy: mdio: fix memory leak Date: Mon, 11 Oct 2021 15:47:04 +0200 Message-Id: <20211011134641.172882204@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211011134640.711218469@linuxfoundation.org> References: <20211011134640.711218469@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: Pavel Skripkin [ Upstream commit ca6e11c337daf7925ff8a2aac8e84490a8691905 ] Syzbot reported memory leak in MDIO bus interface, the problem was in wrong state logic. MDIOBUS_ALLOCATED indicates 2 states: 1. Bus is only allocated 2. Bus allocated and __mdiobus_register() fails, but device_register() was called In case of device_register() has been called we should call put_device() to correctly free the memory allocated for this device, but mdiobus_free() calls just kfree(dev) in case of MDIOBUS_ALLOCATED state To avoid this behaviour we need to set bus->state to MDIOBUS_UNREGISTERED _before_ calling device_register(), because put_device() should be called even in case of device_register() failure. Link: https://lore.kernel.org/netdev/YVMRWNDZDUOvQjHL@shell.armlinux.org.uk/ Fixes: 46abc02175b3 ("phylib: give mdio buses a device tree presence") Reported-and-tested-by: syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com Reviewed-by: Dan Carpenter Signed-off-by: Pavel Skripkin Link: https://lore.kernel.org/r/eceae1429fbf8fa5c73dd2a0d39d525aa905074d.1633024062.git.paskripkin@gmail.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- drivers/net/phy/mdio_bus.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c index 08c81d4cfca8..3207da2224f6 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -378,6 +378,13 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner) bus->dev.groups = NULL; dev_set_name(&bus->dev, "%s", bus->id); + /* We need to set state to MDIOBUS_UNREGISTERED to correctly release + * the device in mdiobus_free() + * + * State will be updated later in this function in case of success + */ + bus->state = MDIOBUS_UNREGISTERED; + err = device_register(&bus->dev); if (err) { pr_err("mii_bus %s failed to register\n", bus->id); -- 2.33.0