From patchwork Tue Sep 28 20:39:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12523829 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 315B1C433F5 for ; Tue, 28 Sep 2021 20:40:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1280D6135F for ; Tue, 28 Sep 2021 20:40:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242780AbhI1Ulr (ORCPT ); Tue, 28 Sep 2021 16:41:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242768AbhI1Ulp (ORCPT ); Tue, 28 Sep 2021 16:41:45 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61A4EC06161C; Tue, 28 Sep 2021 13:40:05 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id e15so1233225lfr.10; Tue, 28 Sep 2021 13:40:05 -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=vBSw5+MqBmKzXLxWE7zwAHio1IPQp5qVLONa1LWZ798=; b=aTcMG8hb2vdBQq92D6DPQ3yxHAoBbhSMGNLmY7UhR+6s4wWEd0JV3uUL44nq8599fU niN3ZaUs59+HFd0n9VXnFa4Q/cOMviskMUI35UIle2U/Kb98dqnV6hSxSUT9zmocJSsY mHOOJfFhIY+5nAS1TnaWPMrShOEzUGYk/t+UAz0XcFijkEO1TJGP/OgMaCvAtAo98Ov8 xcX7bxFDwk77Ry5UJcLkbg0fhl7sqJkCpFtMgceEPHqlU617I2msgQrLbzubHuj95ikB 5It8I3GOAnwEEvXYg45gPMQDE/YLWO9c6YhfoJyWWwbJ911ZmRw79HNAK9O0nYdQpm6F Dkyg== 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=vBSw5+MqBmKzXLxWE7zwAHio1IPQp5qVLONa1LWZ798=; b=Se30bQaYGZCIIROc0/3AtBeh13QDChL6gHRv5YyBPuj5SZ1Xw8/VYQmUujeHgrTtQg ZSqnM4fG7PPwBEio3SqQs9FAbIui8g/lpF937obj1SZhcOOg+pnZd4OdlQzbIyEJ8D9I 9WzV9IZENHnd9jKzB7VLHfm0GTCK6mdZuGgbDijbt7xRHHmkwfwKCUrm563igaQSsMqp ervi7eN04pUcLclIYVfW6PY7f4R0SrlRiEd99zq5DUz9Ha+G4n5TAvWKirbiFHPJMR7R 6rKb7WMgS5w9e7v/Vhxe1g6GagAopKEFlpgoD6URkPoTUosxDoE2yBl8D4dT2yaGi1Q4 2ceQ== X-Gm-Message-State: AOAM531a4Z/SkQEgj2iciOyGL8bUOG8jFMKXInWWgN937WtqYO2s+wtQ Gf1VsknSHOyiTLrefwOYxmk= X-Google-Smtp-Source: ABdhPJwXpmy7rRsn+8NtEbNjk/pQkT0AVxg1q6xHz6+JXki4zWiRApkP4IBUHGZ6+3dnWxXTId5V0g== X-Received: by 2002:a2e:5450:: with SMTP id y16mr2133032ljd.21.1632861603652; Tue, 28 Sep 2021 13:40:03 -0700 (PDT) Received: from localhost.localdomain ([217.117.245.149]) by smtp.gmail.com with ESMTPSA id e17sm19531ljk.133.2021.09.28.13.40.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 13:40:03 -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, dan.carpenter@oracle.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin , Yanfei Xu Subject: [PATCH v2 1/2] Revert "net: mdiobus: Fix memory leak in __mdiobus_register" Date: Tue, 28 Sep 2021 23:39:15 +0300 Message-Id: <2324212c8d0a713eba0aae3c25635b3ca5c5243f.1632861239.git.paskripkin@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210927112017.19108-1-paskripkin@gmail.com> References: <20210927112017.19108-1-paskripkin@gmail.com> 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/ Cc: Yanfei Xu Signed-off-by: Pavel Skripkin Acked-by: Yanfei Xu --- 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 Tue Sep 28 20:40:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12523831 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 50463C433EF for ; Tue, 28 Sep 2021 20:40:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33DCD6135F for ; Tue, 28 Sep 2021 20:40:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242769AbhI1UmD (ORCPT ); Tue, 28 Sep 2021 16:42:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242774AbhI1UmC (ORCPT ); Tue, 28 Sep 2021 16:42:02 -0400 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE60BC06161C; Tue, 28 Sep 2021 13:40:22 -0700 (PDT) Received: by mail-lf1-x130.google.com with SMTP id z24so1173859lfu.13; Tue, 28 Sep 2021 13:40:22 -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=lgizGkLkXPS9gvBHE9FX2g3T10XoCNyE1O2S+oLIjrw=; b=ki1+waXFL/KbSQt1rI5nmEOaIBCufx2k8fcgszwBlA7KXVNVWsBft+wb08FGudDXbm rnPN5R6oMMpw97CERJOsSVKCGxEbZNtmCM1svcadg7vCA45qq2eRU9K7P6b/ZC82whpD 0opqaBkPRnqlIetNvlBjl8VhPXRBKSIf0Q63xQKEAqOwBCo5zoE+3NLOWY/Ss14QUKEU 2PJtK8AYSskS5OHQtIhdMDaBPixEGQVk4r83kYpzrsMxWVcNPWXWo7S3FdiPjtLHCZZ5 ygCtNSQ7pNIIClzZr7otzC6kaxIVzSSYrRZrgnPYYz2hPRMQAUYz3hrjmllwYbNlvv0j o21A== 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=lgizGkLkXPS9gvBHE9FX2g3T10XoCNyE1O2S+oLIjrw=; b=tdNBait6nn7OTG+RKlr7zM6PEjxQHEanIGM587hKNtBhQo7wIhJfbW98YND/GjHktV I6gTi53zNL6OWY1Et1cfLff1KA59Pbi0KMHLJNvGR/R56Zu863ldu/PeNLnvexNmASdS 3lEuec0NUq+EIP9I7HSSTKvhShxtdXUti3PMtEF61ttyi8bIJS4vMPjXP0nr/jqAZP6B CIWdm/EHqQg/KL/FwYtkAdErWZuUi9lMXjD2XkaDy7iM23upvxccCPY6aQ108LNFgoV2 kmF3wCwHHmT2ZxaiGZiTgh2UVHSaSJ2Jl00fp6FpIQQQJ4oImDUF8Cxww4+QrhwlMTQK Ycqw== X-Gm-Message-State: AOAM532x4VfyhdyRUKC6KPfxjAhYypZh9AXqfFGJDeeX98T02mjET1KG K+Lx93JLuQPOlbsCL4F+6Jg= X-Google-Smtp-Source: ABdhPJwU4MZdQsvvoKbHqbgp3s6cfLrkdjC1Layzkld6Oiwp4WwMSC1wn4vELaFHr1tUVPqI+0SrOw== X-Received: by 2002:a2e:22c3:: with SMTP id i186mr2003739lji.255.1632861621127; Tue, 28 Sep 2021 13:40:21 -0700 (PDT) Received: from localhost.localdomain ([217.117.245.149]) by smtp.gmail.com with ESMTPSA id n9sm7566lfu.88.2021.09.28.13.40.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 13:40:20 -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, dan.carpenter@oracle.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin , syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com Subject: [PATCH v2 2/2] phy: mdio: fix memory leak Date: Tue, 28 Sep 2021 23:40:15 +0300 Message-Id: <55e9785e2ae2eae03c4af850a07e3297c5a0b784.1632861239.git.paskripkin@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <2324212c8d0a713eba0aae3c25635b3ca5c5243f.1632861239.git.paskripkin@gmail.com> References: <2324212c8d0a713eba0aae3c25635b3ca5c5243f.1632861239.git.paskripkin@gmail.com> 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 Signed-off-by: Pavel Skripkin Reviewed-by: Dan Carpenter --- 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..2d16a93af1ae 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 realese + * 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);