From: Bjorn Helgaas <helgaas@kernel.org>
To: Ding Hui <dinghui@sangfor.com.cn>
Cc: bhelgaas@google.com, sathyanarayanan.kuppuswamy@linux.intel.com,
vidyas@nvidia.com, david.e.box@linux.intel.com,
kai.heng.feng@canonical.com, michael.a.bottini@linux.intel.com,
rajatja@google.com, refactormyself@gmail.com,
qinzongquan@sangfor.com.cn, linux-pci@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] PCI/ASPM: Fix UAF by disabling ASPM for link when child function is removed
Date: Thu, 18 May 2023 16:54:46 -0500 [thread overview]
Message-ID: <ZGaepuV6VNpbbDXb@bhelgaas> (raw)
In-Reply-To: <20230507034057.20970-1-dinghui@sangfor.com.cn>
On Sun, May 07, 2023 at 11:40:57AM +0800, Ding Hui wrote:
> If the Function 0 of a Multi-Function device is software removed,
> a freed downstream pointer will be left in struct pcie_link_state,
> and then when pcie_config_aspm_link() be invoked from any path,
> we will trigger use-after-free, e.g.:
>
> Reproducer:
>
> [root@host ~]# cat repro.sh
> #!/bin/bash
> DEV_F0="0000:03:00.0"
> echo 1 > /sys/bus/pci/devices/$DEV_F0/remove
> echo powersave > /sys/module/pcie_aspm/parameters/policy
>
> Result:
>
> ==================================================================
> BUG: KASAN: slab-use-after-free in pcie_config_aspm_link+0x42d/0x500
> Read of size 4 at addr ffff8881070c80a0 by task repro.sh/2056
> CPU: 3 PID: 2056 Comm: repro.sh Not tainted 6.3.0+ #15
> Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 11/12/2020
> Call Trace:
> <TASK>
> dump_stack_lvl+0x33/0x50
> print_address_description.constprop.0+0x27/0x310
> print_report+0x3e/0x70
> kasan_report+0xae/0xe0
> pcie_config_aspm_link+0x42d/0x500
> pcie_aspm_set_policy+0x8e/0x1a0
> param_attr_store+0x162/0x2c0
> module_attr_store+0x3e/0x80
> kernfs_fop_write_iter+0x2d5/0x460
> vfs_write+0x72e/0xae0
> ksys_write+0xed/0x1c0
> do_syscall_64+0x38/0x90
> entry_SYSCALL_64_after_hwframe+0x72/0xdc
>
> As per PCIe spec r6.0, sec 7.5.3.7, it is recommended that software
> program the same value in all Functions for Multi-Function Devices
> (including ARI Devices). For ARI Devices, ASPM Control is determined
> solely by the setting in Function 0.
>
> So we can just disable ASPM of the whole component if any child
> function is removed, the downstream pointer will be avoided from
> use-after-free, that will also avoid other potential corner cases.
>
> Fixes: b5a0a9b59c81 ("PCI/ASPM: Read and set up L1 substate capabilities")
> Debugged-by: Zongquan Qin <qinzongquan@sangfor.com.cn>
> Suggested-by: Bjorn Helgaas <bhelgaas@google.com>
> Signed-off-by: Ding Hui <dinghui@sangfor.com.cn>
Applied to pci/aspm for v6.5, thank you!
> ---
> v2:
> - better commit title and message
> - update comment
> - add reproduction steps
>
> v1: https://lore.kernel.org/lkml/20230504123418.4438-1-dinghui@sangfor.com.cn/
>
> Link: https://lore.kernel.org/lkml/20230429132604.31853-1-dinghui@sangfor.com.cn/
>
> ---
> drivers/pci/pcie/aspm.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
> index 66d7514ca111..06152cc39fea 100644
> --- a/drivers/pci/pcie/aspm.c
> +++ b/drivers/pci/pcie/aspm.c
> @@ -1010,18 +1010,15 @@ void pcie_aspm_exit_link_state(struct pci_dev *pdev)
>
> down_read(&pci_bus_sem);
> mutex_lock(&aspm_lock);
> - /*
> - * All PCIe functions are in one slot, remove one function will remove
> - * the whole slot, so just wait until we are the last function left.
> - */
> - if (!list_empty(&parent->subordinate->devices))
> - goto out;
>
> link = parent->link_state;
> root = link->root;
> parent_link = link->parent;
>
> - /* All functions are removed, so just disable ASPM for the link */
> + /*
> + * For any function removed, disable ASPM for the link. See PCIe r6.0,
> + * sec 7.7.3.7 for details.
> + */
> pcie_config_aspm_link(link, 0);
> list_del(&link->sibling);
> /* Clock PM is for endpoint device */
> @@ -1032,7 +1029,7 @@ void pcie_aspm_exit_link_state(struct pci_dev *pdev)
> pcie_update_aspm_capable(root);
> pcie_config_aspm_path(parent_link);
> }
> -out:
> +
> mutex_unlock(&aspm_lock);
> up_read(&pci_bus_sem);
> }
> --
> 2.17.1
>
next prev parent reply other threads:[~2023-05-18 21:54 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-07 3:40 [PATCH v2] PCI/ASPM: Fix UAF by disabling ASPM for link when child function is removed Ding Hui
2023-05-18 21:54 ` Bjorn Helgaas [this message]
2024-12-23 3:39 ` [PATCH 0/1] " Daniel Stodden
2024-12-23 3:39 ` [PATCH 1/1] PCI/ASPM: fix link state exit during switch upstream function removal Daniel Stodden
2025-01-03 1:53 ` Daniel Stodden
2025-01-29 21:29 ` Bjorn Helgaas
2025-01-30 6:54 ` Sathyanarayanan Kuppuswamy
2024-12-28 5:00 ` [PATCH 0/1] Re: PCI/ASPM: Fix UAF by disabling ASPM for link when child function is removed Ding Hui
2024-12-30 15:17 ` [PATCH 0/1] " Daniel Stodden
2025-01-01 12:25 ` Ding Hui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZGaepuV6VNpbbDXb@bhelgaas \
--to=helgaas@kernel.org \
--cc=bhelgaas@google.com \
--cc=david.e.box@linux.intel.com \
--cc=dinghui@sangfor.com.cn \
--cc=kai.heng.feng@canonical.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=michael.a.bottini@linux.intel.com \
--cc=qinzongquan@sangfor.com.cn \
--cc=rajatja@google.com \
--cc=refactormyself@gmail.com \
--cc=sathyanarayanan.kuppuswamy@linux.intel.com \
--cc=vidyas@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.