Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1615734pxb; Mon, 11 Oct 2021 09:32:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRqiOUiq7InIOEjDS24dTi8lnhUTUgyup+qxGKMYSDSAlC0f8fMHEeQODWjKLqc5s3uub/ X-Received: by 2002:a05:6402:2684:: with SMTP id w4mr42505319edd.108.1633969956656; Mon, 11 Oct 2021 09:32:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633969956; cv=none; d=google.com; s=arc-20160816; b=osGRiYEWzxMaLbd7ajwGXMf4hDG8K3mHfiWhRiBZCQW9WOXpA78rhEQ9su/EmT1sz/ rBjAmiGIb9FF0GuvbdH+ykWkG2rWntCW7bc5UnZtGXqUSYdDbsDcLxSPztfHDDF2WD13 R/W1gLxhWPBNL1f8H3DJvKhpYHnNBz6MWm2D1nGCDEsgFVp/V3Iw2Ac7gWxFuoZUZsac LXQh7KPjWnKondn1rw/b/o0Y7Cp3Ha5vDlgU3YEt9yltnXIAA7B6xz1mtM00eh4cO5EN MlRvtnQeBZ5bPPO8ZF4+LIE2OgONM+bEf6iMWRP3KLDSsWYl0qC3vGo+iZK0Z1PiR1dt hF+g== 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=u5efhBJqSGyk4tqzgkivCI2kugQyUjPa0gEbL6YDdQU=; b=q4Ui7r4yqauoUungRUDZyIVD/leNzvm+ZHgML7z4yRtPeoU+qeBZUmcREvikH60Hgh TNf6bjhHh6UNEnE/EXKkY2QooFt1+uvaZ6y2/vnlnJDifDOPsFXtCA8i8htk/xqgIRvk uz/voqgRtZ+5RiM0XxdYe+C1D0Mcm//9lPbwMmZ+VSLgiJqf7mjQak35XArwF2OwXO0x PC5RN6CbOSstPkhFStXCH9EsMjZ1eoHBW2cB2i5rSpwG67Czdpjl2offBaQ1ueGRFko7 2dp7ihBgsY3n4T0qWbNVsiJOsWOCc/pPrGU3oSceVApkhnfiXXNXqK63mYGdPxTIlY36 h8Zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Im+J0U5e; 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 di18si10931602edb.289.2021.10.11.09.32.12; Mon, 11 Oct 2021 09:32:36 -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=Im+J0U5e; 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 S237420AbhJKN75 (ORCPT + 99 others); Mon, 11 Oct 2021 09:59:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:50128 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238082AbhJKN6S (ORCPT ); Mon, 11 Oct 2021 09:58:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3029E610A4; Mon, 11 Oct 2021 13:54:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633960485; bh=jQdWEU3oinQkFh+ee4lCjDlbx/41xS4Q0nR3hEriURo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Im+J0U5e3WRssfpux0h+Ou2nh7W81FLQ4MP3Qb7cGVaZAM1sJaUFdVqjrWC/KvU5H 7lf6FtwnFxkh0rESAWekHdEoo1UyTuByrMaaZLbou8eTumcMLFFwrG1HiDPew3XN7o 6VKRqbbxFiRhDwjP2NU9MhxV6RfhJHVbd96Ztku8= 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 5.10 40/83] phy: mdio: fix memory leak Date: Mon, 11 Oct 2021 15:46:00 +0200 Message-Id: <20211011134509.779135625@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211011134508.362906295@linuxfoundation.org> References: <20211011134508.362906295@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 b848439fa837..2645ca35103c 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); -- 2.33.0