Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp733786pxb; Tue, 5 Apr 2022 20:55:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSvccNW7svkE/RvuXP2U7cYyaSet6F2cOuXm/7ehXiPJm/pql60sGMJPYRzriCbk1gLBQO X-Received: by 2002:a17:902:ecd1:b0:154:5393:aaa4 with SMTP id a17-20020a170902ecd100b001545393aaa4mr6466894plh.131.1649217306533; Tue, 05 Apr 2022 20:55:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649217306; cv=none; d=google.com; s=arc-20160816; b=ygFNIobfkquvFqv9TJDDmlDnNcVFFMm/vpskVV4HXNKTCyWK05ao95dBaQCabohdy5 iXfPTPHSlPWTmAcw4CTYDVIbT9SwpdU22EoUaiRbtLz6gxyVE5UZtnisrPfy9hB4bWmG KKht938skcXFjwnkpjHLkcUKmecPL5Kju3OWwATCC6DtLU6gsAdQ9y1aaPSJL1B1lEhv pCt0LkYgbnkAJtCBuXRMtxKPZHF3ylw4Awa/g2fQjU4h8a2oi4YEu6tqlgrVCRcQEsG0 +EMYlOetWgUyAeRqu46RHZ62VwpxAlD2KifQDe7ppg/EmNaIDHXnaGhhYso6ZmUPMaZu YQsA== 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=2kjgEbmQ64GNXBXN/C1lVKe6HkVvIGbXkujM8Zd2dSg=; b=HDalOGF/LKWzMVn0e00ffxLLOliG0sUX57rvmZxen0LfCJGYJtXznxr54p+pk0nSwE 68zdbNpveHxySblqNhIwNpoUgdg3IAye3C099ShlucjXNQrJ1XTFnzfYJ3lZkgf7kuXU lVDgIZeERW01QbSeMhZsU2skWmqYsMRuMZ+L7qsLXyDOeaJbAeFsGNLyK26yImB8nCsg tnCUg9o7KmQrzzWT2ir20nByO21H/qaTHciZ94Qq8DA3VVsLDRh7D9n3g3xGVTC6Adsl PKqmqgO8ss1LnH9UCtya6iS2uruBly0KHuJhe2w+8UajcpO1LJ7iQMe3Wjw9j1DvAV1Q OZxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1JaOi3JN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id b15-20020a63714f000000b003821c6b79c7si14667396pgn.666.2022.04.05.20.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 20:55:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1JaOi3JN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6595620DDFB; Tue, 5 Apr 2022 20:31:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385855AbiDEMjI (ORCPT + 99 others); Tue, 5 Apr 2022 08:39:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236740AbiDEJDe (ORCPT ); Tue, 5 Apr 2022 05:03:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1090103A; Tue, 5 Apr 2022 01:55:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7CD65614E4; Tue, 5 Apr 2022 08:55:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A025C385A0; Tue, 5 Apr 2022 08:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649148912; bh=VqGjegZfHHnvDHqgx50NeDfYYPQ+7pDQWp2m+VwfD28=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1JaOi3JNHeeiSKpq1LQQYU11Ty7aTHfm0vQqIlig3slvrWqN2VBT3JNZwBB4V2tkJ GQQYZVlKjlFizV7pnOZ9JRYoSJNTC0kcP5HVlHO0NL9beLturRjjqy7HfAXixag7wb pUzgljF2RqdMrx2RHyOOg7WEu6VzGHW2Dib1pkOg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Abhishek Sahu , Alex Williamson , Sasha Levin Subject: [PATCH 5.16 0527/1017] vfio/pci: fix memory leak during D3hot to D0 transition Date: Tue, 5 Apr 2022 09:24:00 +0200 Message-Id: <20220405070409.938685198@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Abhishek Sahu [ Upstream commit eadf88ecf6ac7d6a9f47a76c6055d9a1987a8991 ] If 'vfio_pci_core_device::needs_pm_restore' is set (PCI device does not have No_Soft_Reset bit set in its PMCSR config register), then the current PCI state will be saved locally in 'vfio_pci_core_device::pm_save' during D0->D3hot transition and same will be restored back during D3hot->D0 transition. For saving the PCI state locally, pci_store_saved_state() is being used and the pci_load_and_free_saved_state() will free the allocated memory. But for reset related IOCTLs, vfio driver calls PCI reset-related API's which will internally change the PCI power state back to D0. So, when the guest resumes, then it will get the current state as D0 and it will skip the call to vfio_pci_set_power_state() for changing the power state to D0 explicitly. In this case, the memory pointed by 'pm_save' will never be freed. In a malicious sequence, the state changing to D3hot followed by VFIO_DEVICE_RESET/VFIO_DEVICE_PCI_HOT_RESET can be run in a loop and it can cause an OOM situation. This patch frees the earlier allocated memory first before overwriting 'pm_save' to prevent the mentioned memory leak. Fixes: 51ef3a004b1e ("vfio/pci: Restore device state on PM transition") Signed-off-by: Abhishek Sahu Link: https://lore.kernel.org/r/20220217122107.22434-2-abhsahu@nvidia.com Signed-off-by: Alex Williamson Signed-off-by: Sasha Levin --- drivers/vfio/pci/vfio_pci_core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index f948e6cd2993..87b288affc13 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -228,6 +228,19 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat if (!ret) { /* D3 might be unsupported via quirk, skip unless in D3 */ if (needs_save && pdev->current_state >= PCI_D3hot) { + /* + * The current PCI state will be saved locally in + * 'pm_save' during the D3hot transition. When the + * device state is changed to D0 again with the current + * function, then pci_store_saved_state() will restore + * the state and will free the memory pointed by + * 'pm_save'. There are few cases where the PCI power + * state can be changed to D0 without the involvement + * of the driver. For these cases, free the earlier + * allocated memory first before overwriting 'pm_save' + * to prevent the memory leak. + */ + kfree(vdev->pm_save); vdev->pm_save = pci_store_saved_state(pdev); } else if (needs_restore) { pci_load_and_free_saved_state(pdev, &vdev->pm_save); -- 2.34.1