From patchwork Thu Sep 30 17:49:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12528937 X-Patchwork-Delegate: kuba@kernel.org 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35C7BC433F5 for ; Thu, 30 Sep 2021 17:50:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1AAEE61A07 for ; Thu, 30 Sep 2021 17:50:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352849AbhI3RwL (ORCPT ); Thu, 30 Sep 2021 13:52:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352839AbhI3RwK (ORCPT ); Thu, 30 Sep 2021 13:52:10 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8F9DC06176A; Thu, 30 Sep 2021 10:50:27 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id x27so28236759lfa.9; Thu, 30 Sep 2021 10:50:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WbMGoSeIG8Nre7YI94HB+1s+5OyVO37vI/FQasjDNY0=; b=qKckho3veE14jzoQZa3FHX5ehyczkirMrSzEPpck72OtAr3vB9PyWSrebSRG9I/sHP 6IsrpzAOdLNiEudtcudInoxpEGgzbYB33JMUki6jD9wXa/qa689vM8U8HfVP69AJGFzd eFswiR4DEymO4lgUpvyk9smlkFfWLENrJvza3tWKrfpWI8hTcNmyO0oXJAuNqMfIj5FJ YPvJRxm2kHaW0kZezFrmKGfUtWSG5CzIwo701Pe4Y3LOV3EEZEvTTlInFIpA7mZxQEZA RNGwB0Ey6aa4NH+bBMZs+KVFO2xgrgx4yj7s31jeiPVutjV6P/j4VyPhqiNWy5hVCPRa F4IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WbMGoSeIG8Nre7YI94HB+1s+5OyVO37vI/FQasjDNY0=; b=5UE9e/4Qa57fHv4xKEFv2juW4QGHb18JxdKtihZgOgLwrHwPEy90WLdamsPpeEqcCP g9doasuvfFJek0G55MAZGcNRn0ITbd9ZThCzUPyTzzxwnQ38KZWM1be+XIZQP2ufaCRv oUPkNyzaFnUT7GZ9SH7WT4cE9/ksM5/7Cuno8idPvj+O5BCSnQDSk5mrPCO2WFJwpYph kLci+OAF2maV5sWNcDlScjWmgtSamDFvylf9EGl3rQiMrt8ctjvXL7qSWwJOAaeuC99K MNeNk3WEeO+VfCKhi1vE7Lp0rYV8JMPJVOIKtLN4rRnIbmXG4BnsAMcWktR3g8Vypa1x QyQQ== X-Gm-Message-State: AOAM5305tBPDRCNof88UMxdOmNIBNygnQormNWhcs9UfvF7ciXm7CkOS 997nFqMwbIyCOq5ReAKSdb4= X-Google-Smtp-Source: ABdhPJzld8V0K1gmh+IgO8cfbtKL/xWokjolndUwXEnW/wfwaR9oljJXtjxvR61kmj0h8nuNQxqJoA== X-Received: by 2002:a05:6512:b0c:: with SMTP id w12mr568372lfu.240.1633024225985; Thu, 30 Sep 2021 10:50:25 -0700 (PDT) Received: from localhost.localdomain ([217.117.245.149]) by smtp.gmail.com with ESMTPSA id q6sm485712lfn.170.2021.09.30.10.50.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 10:50:25 -0700 (PDT) From: Pavel Skripkin To: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net, kuba@kernel.org, buytenh@marvell.com, afleming@freescale.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin , Yanfei Xu Subject: [PATCH v4 1/2] Revert "net: mdiobus: Fix memory leak in __mdiobus_register" Date: Thu, 30 Sep 2021 20:49:42 +0300 Message-Id: X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org This reverts commit ab609f25d19858513919369ff3d9a63c02cd9e2e. This patch is correct in the sense that we _should_ call device_put() in case of device_register() failure, but the problem in this code is more vast. We need to set bus->state to UNMDIOBUS_REGISTERED before calling device_register() to correctly release the device in mdiobus_free(). This patch prevents us from doing it, since in case of device_register() failure put_device() will be called 2 times and it will cause UAF or something else. Also, Reported-by: tag in revered commit was wrong, since syzbot reported different leak in same function. Link: https://lore.kernel.org/netdev/20210928092657.GI2048@kadam/ Acked-by: Yanfei Xu Signed-off-by: Pavel Skripkin --- Chnages in v4: No changes Changes in v3: CC Yanfei -> Acked-by Yanfei Changes in v2: Added this revert --- drivers/net/phy/mdio_bus.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c index 6f4b4e5df639..53f034fc2ef7 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -537,7 +537,6 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner) err = device_register(&bus->dev); if (err) { pr_err("mii_bus %s failed to register\n", bus->id); - put_device(&bus->dev); return -EINVAL; } From patchwork Thu Sep 30 17:50:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12528939 X-Patchwork-Delegate: kuba@kernel.org 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F284EC433F5 for ; Thu, 30 Sep 2021 17:50:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C78AB61A02 for ; Thu, 30 Sep 2021 17:50:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352858AbhI3RwU (ORCPT ); Thu, 30 Sep 2021 13:52:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352854AbhI3RwR (ORCPT ); Thu, 30 Sep 2021 13:52:17 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A8BBC061770; Thu, 30 Sep 2021 10:50:33 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id b15so28500814lfe.7; Thu, 30 Sep 2021 10:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+JDjAgU8bFcCk8yPUu11mn0T3Xk2GAlmfZNXipC8Lmo=; b=BcLNUXFG9WHALB535wMqdB0A1rvUc6Ym3l6cHuLOP9qxUsmK/YExZ1OUhfNkc4Vcd6 ftCMTd3RTke73TDteUclXgPPwjsxXbG5N3YR56FGv9xGoccT3Qsd/cldozoXsJ962BJp ep5FV+JwAh/vql+orWrR4XRjCc/UNaPZm5qsgXCvXnPETPIFSonsI4dwwX4mBpj3mzkn JTFAja4r/VBwW+2gSPtTvxCpoOiKCDKMDFJv28gI28FUDVt9upO4/iANDOvTlY2v8Pol /4GJRhrOgldEp3KiHIdZWcODUATh7kq70xiGXuTi0S6QO0B9GIh5k/9RZMO5P++rfjj6 3lbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+JDjAgU8bFcCk8yPUu11mn0T3Xk2GAlmfZNXipC8Lmo=; b=u4/ZoPAd5Qb+eOhVmTPtYZawrnYejy18X+dGTM6mTvdxAiP94AMov9gr7oimG0KLPj yCvs9M7Q27PbvJd373zws0RwlOWm5GafuB89d/n42faIkmWTJc1YOuz2vZMcoLRLKCjk ONfIyADAtLCtgB4AtbnUYOnLIi5TQZuLCTxOTIU3TmL7uzd8aZ73Od3yntbCOx2cFPM4 yZFLkSKy0iW1ZdTl2LkcMyvldQnAWHrJj4dGurD67nCVt73eir8IJXQ4Wp25dqr0UBsL Rl/k00N85DyYe4TxERCHpSeaimwy6GL5dkr/sFz2BxJFoSFDX6TmYzObYCT7mDqCjgfM /SYw== X-Gm-Message-State: AOAM533Jo9QlYjgVhdOlGb1UUy4U1L4pxNVpPsuKBBe1McCi2nt7Dcn5 7WDydxJ1aiKGq6X+rsVwJWs= X-Google-Smtp-Source: ABdhPJz4Ga693I91NXRp0LZZrwh/IFFbkUIE4pp0WVDA7T9qC6OrD08aq9cS+nj4qzZkK0BeCjUBzA== X-Received: by 2002:ac2:46d8:: with SMTP id p24mr489777lfo.227.1633024231719; Thu, 30 Sep 2021 10:50:31 -0700 (PDT) Received: from localhost.localdomain ([217.117.245.149]) by smtp.gmail.com with ESMTPSA id m5sm443567lfg.121.2021.09.30.10.50.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 10:50:31 -0700 (PDT) From: Pavel Skripkin To: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net, kuba@kernel.org, buytenh@marvell.com, afleming@freescale.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin , syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com, Dan Carpenter Subject: [PATCH v4 2/2] phy: mdio: fix memory leak Date: Thu, 30 Sep 2021 20:50:28 +0300 Message-Id: X-Mailer: git-send-email 2.33.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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 --- Changes in v4: Fixed typo in comment, thanks to Wong Vee Khee Changes in v3: s/==/=/ Added Dan's Reviewed-by tag Changes in v2: Removed new state, since MDIOBUS_UNREGISTERED can be used. Also, moved bus->state initialization _before_ device_register() call, because Yanfei's patch is reverted in [v2 1/2] --- 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 53f034fc2ef7..fca8e335d750 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -534,6 +534,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);