Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chengwen Feng" <fengchengwen@huawei.com>
Cc: kvm@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
Date: Mon, 23 Nov 2026 02:50:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-228565@kernel.org> (raw)
In-Reply-To: <20261123023628.7728-13-fengchengwen@huawei.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] vfio/pci: out-of-bounds read in cpu_online() via unchecked CPU
  ID
- [Medium] vfio/pci: vfio_tph_config_write() loses ST mode when TPH is
  disabled
--

commit d6106eda03cc2bd8d1bcbc6a878dc5163e37fa33
Author: Chengwen Feng <fengchengwen@huawei.com>

vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability

This commit adds the VFIO_PCI_TPH_POLICY_DS_ST policy level to allow
PCI_TPH_ST_DS_MODE selection and introduces the
VFIO_DEVICE_FEATURE_TPH_RESOLVE ioctl feature to resolve steering tags from
CPU sources.

> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index 2bae386a8469f..3b507a96edfc0 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -1228,7 +1228,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
>  	cap = vdev->tph_vcap;
>  	mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, new_ctrl);
>  	req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, new_ctrl);
> -	if (mode > PCI_TPH_ST_IV_MODE || !(cap & (1u << mode)) ||
> +	if (mode > PCI_TPH_ST_DS_MODE || !(cap & (1u << mode)) ||
>  		req == 0x2 /* reserved */ ||
>  		(req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
>  		goto out; /* Drop invalid or unsupported write value. */

[Severity: Medium]
Can vfio_tph_config_write() lose the ST Mode Select field when TPH
Requester Enable is PCI_TPH_REQ_DISABLE?

When req == PCI_TPH_REQ_DISABLE, vfio_tph_config_write() calls
pcie_disable_tph(), which either returns without writing to hardware when
!pdev->tph_enabled or writes 0 to the entire 32-bit hardware PCI_TPH_CTRL
register when pdev->tph_enabled is set.  Because vfio_tph_config_read()
reads PCI_TPH_CTRL directly from hardware and vfio_tph_config_write()
overwrites vdev->vconfig[start + PCI_TPH_CTRL] with the hardware register
on entry:

drivers/vfio/pci/vfio_pci_config.c:vfio_tph_config_write() {
	...
	vfio_user_config_read(pdev, start + PCI_TPH_CTRL, &ctrl_val, 4);
	vfio_default_config_write(vdev, start + PCI_TPH_CTRL, 4, perm,
				  PCI_TPH_CTRL, ctrl_val);
	...
	if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH) {
		pcie_disable_tph(pdev);
		pcie_enable_tph_ext(pdev, mode, req);
	} else if (req == PCI_TPH_REQ_DISABLE) {
		pcie_disable_tph(pdev);
	}
	...
}

any ST mode written while the requester is disabled (or preserved when
disabling the requester to update ST entries) is lost on the next config
read or write, reverting the device to PCI_TPH_ST_NS_MODE when the
requester is later enabled.

> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 83b81d1a61227..318b4f4332565 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -1915,6 +1921,50 @@ static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
>  	return ret;
>  }
>  
> +static int vfio_pci_core_feature_tph_resolve(struct vfio_pci_core_device *vdev,
> +			u32 flags,
> +			struct vfio_device_feature_tph_resolve __user *arg,
> +			size_t argsz)
> +{
> +	struct vfio_device_feature_tph_resolve resolve;
> +	u32 user_flags, src_mask;
> +	bool extended;
> +	u16 tag = 0;
> +	int ret;
> +
> +	if (!vdev->tph_opt_in || !(vdev->tph_vcap & PCI_TPH_CAP_ST_DS))
> +		return -EOPNOTSUPP;
> +
> +	ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
> +				 sizeof(resolve));
> +	if (ret != 1)
> +		return ret;
> +
> +	if (copy_from_user(&resolve, arg, sizeof(resolve)))
> +		return -EFAULT;
> +
> +	user_flags = resolve.flags;
> +	extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
> +	if (extended && !pcie_ext_tph_supported(vdev->pdev))
> +		return -EOPNOTSUPP;
> +	user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
> +
> +	src_mask = vfio_pci_tph_get_src_mask(vdev) & ~VFIO_DEVICE_TPH_SRC_NONE;
> +	if (user_flags & ~src_mask || !is_power_of_2(user_flags))
> +		return -EINVAL;
> +
> +	ret = vfio_pci_tph_resolve_st_tag(vdev->pdev, user_flags, resolve.src,
> +					  extended, &tag);

[Severity: High]
Can a user-supplied resolve.src >= nr_cpu_ids cause an out-of-bounds read
in cpu_online()?

vfio_pci_core_feature_tph_resolve() passes the unvalidated 32-bit
resolve.src value from userspace to vfio_pci_tph_resolve_st_tag(), which
calls cpu_online(src_handle) without first checking src_handle < nr_cpu_ids:

drivers/vfio/pci/vfio_pci_core.c:vfio_pci_tph_resolve_st_tag() {
	...
	if ((src_bits & cpu_srcs) && !cpu_online(src_handle)) {
		*st = 0;
		return -EINVAL;
	}
	...
}

Because cpumask_test_cpu() does not clamp out-of-range CPU numbers,
passing a large resolve.src indexes past __cpu_online_mask in test_bit()
and can trigger an out-of-bounds read or kernel page fault.  Checking
src_handle >= nr_cpu_ids before cpu_online(src_handle) in
vfio_pci_tph_resolve_st_tag() would also protect
vfio_pci_core_feature_tph_program().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=12

  reply	other threads:[~2026-11-23  2:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
2026-11-23  2:47   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-11-23  2:48   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
2026-11-23  2:58   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
2026-11-23  2:50   ` sashiko-bot [this message]
2026-11-23  2:36 ` [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-11-23  2:52 ` [PATCH v22 00/13] vfio/pci: Add PCIe TPH support fengchengwen
2026-11-23  2:58 ` sashiko-bot

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=sashiko-outbox-228565@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fengchengwen@huawei.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox