Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp705120pxb; Tue, 5 Apr 2022 19:34:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhWDdfxu7su10d1flufV9W6wb0Q2W3TPJil/26BiwwdD1IWXtWqz0muly0K2dXLRLu0FSS X-Received: by 2002:a05:6a00:1312:b0:4e1:58c4:ddfd with SMTP id j18-20020a056a00131200b004e158c4ddfdmr6544423pfu.65.1649212373421; Tue, 05 Apr 2022 19:32:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649212373; cv=none; d=google.com; s=arc-20160816; b=0MaRRwsC9NeFzMkGrRU/zzQicuusG6r1PhFDT0/YnL2sgrWsYiZh6ZfR+ijwMMR5wa golz3Cs9pKImtwxgFsmzSZmltKw2+Eo+d3Mek8dWDwBgM2fCg0yZzl/NRBZWAvGiFNkJ ISzqa1Br7Z1v1PQD2+1tEklqOpZ4mIP/TLfyontv97EJ3cWigSP0mmEmHQkaUcfg12x3 m+Gj8KABwcQF4gIJxr+1FgROF8ivvyIBolcWIY7ANZMobP3lJWtGl+WiibOS16Yjpd4i 5xFITecL8+p6vzMuYrPbeJ8QgvuJrkKkzVY7O6UAi2OfIqfyXpamzhzmYZybshKtqv/J b7gg== 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=VuaA3FnqX3qh+D8+OdPOYd3QrrbLjUlplwDSVL0uu3NC5K5hS4YiZnRMbWPAr4HtTG XRmebDpINNTp36ocUsivFd5xaOLs1R0a2eYFOHoFiLyM/Tw/uqcnvc/ncX7wls0h4hOS u4ylka/51DkLYVRReTxv7aljlT/I8G/Vi3w45NwphubG2TsS1SkdT8DTcxfjcARIrWSq TNvDWoloeDoL7tE3GqWRsEzOA5QahQnaeY2cPtw7mRhXANncvCl6olQl4wTdKDpt+p03 sFQJ+V6KVRLcd0ZvWnfPdoYe6d8IeOcosnHQUhGlqnuv0cQPpTauE+aP6+Hq6YohZEu5 LSHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U7syJFVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id on14-20020a17090b1d0e00b001bd14e01f20si4077678pjb.14.2022.04.05.19.32.38; Tue, 05 Apr 2022 19:32:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U7syJFVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S235712AbiDEJC3 (ORCPT + 99 others); Tue, 5 Apr 2022 05:02:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237168AbiDEIRp (ORCPT ); Tue, 5 Apr 2022 04:17:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC25EB0D2B; Tue, 5 Apr 2022 01:05:31 -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 6EE5F6167A; Tue, 5 Apr 2022 08:05:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DDFDC385A0; Tue, 5 Apr 2022 08:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649145930; bh=VqGjegZfHHnvDHqgx50NeDfYYPQ+7pDQWp2m+VwfD28=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U7syJFVFa8QhK38cekiqYQJkEmCiRLf9SQIBNSGfVQ6I0gvWRxMALUGCqsOefDzKp 2QrH3/MOetgV+qDG1ub9qAwzFzuZjF5snJ/qSyhaghIerWvyRgcT9xlQ49R0u9Vbeu COLRr/GwfBmXqD0SH2zv4s4iXB7LuIBrwTuPbZGg= 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.17 0580/1126] vfio/pci: fix memory leak during D3hot to D0 transition Date: Tue, 5 Apr 2022 09:22:07 +0200 Message-Id: <20220405070424.656793434@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@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=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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