genirq/msi: Store the IOMMU IOVA directly in msi_desc instead of iommu_cookie

[ Upstream commit 1f7df3a691740a7736bbc99dc4ed536120eb4746 ]

The IOMMU translation for MSI message addresses has been a 2-step process,
separated in time:

 1) iommu_dma_prepare_msi(): A cookie pointer containing the IOVA address
    is stored in the MSI descriptor when an MSI interrupt is allocated.

 2) iommu_dma_compose_msi_msg(): this cookie pointer is used to compute a
    translated message address.

This has an inherent lifetime problem for the pointer stored in the cookie
that must remain valid between the two steps. However, there is no locking
at the irq layer that helps protect the lifetime. Today, this works under
the assumption that the iommu domain is not changed while MSI interrupts
being programmed. This is true for normal DMA API users within the kernel,
as the iommu domain is attached before the driver is probed and cannot be
changed while a driver is attached.

Classic VFIO type1 also prevented changing the iommu domain while VFIO was
running as it does not support changing the "container" after starting up.

However, iommufd has improved this so that the iommu domain can be changed
during VFIO operation. This potentially allows userspace to directly race
VFIO_DEVICE_ATTACH_IOMMUFD_PT (which calls iommu_attach_group()) and
VFIO_DEVICE_SET_IRQS (which calls into iommu_dma_compose_msi_msg()).

This potentially causes both the cookie pointer and the unlocked call to
iommu_get_domain_for_dev() on the MSI translation path to become UAFs.

Fix the MSI cookie UAF by removing the cookie pointer. The translated IOVA
address is already known during iommu_dma_prepare_msi() and cannot change.
Thus, it can simply be stored as an integer in the MSI descriptor.

The other UAF related to iommu_get_domain_for_dev() will be addressed in
patch "iommu: Make iommu_dma_prepare_msi() into a generic operation" by
using the IOMMU group mutex.

Link: https://patch.msgid.link/r/a4f2cd76b9dc1833ee6c1cf325cba57def22231c.1740014950.git.nicolinc@nvidia.com
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Jason Gunthorpe
2025-02-19 17:31:36 -08:00
committed by Greg Kroah-Hartman
parent 2ed497f369
commit ba41e4e627
2 changed files with 25 additions and 36 deletions

View File

@@ -1716,7 +1716,7 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
static DEFINE_MUTEX(msi_prepare_lock); /* see below */ static DEFINE_MUTEX(msi_prepare_lock); /* see below */
if (!domain || !domain->iova_cookie) { if (!domain || !domain->iova_cookie) {
desc->iommu_cookie = NULL; msi_desc_set_iommu_msi_iova(desc, 0, 0);
return 0; return 0;
} }
@@ -1728,11 +1728,12 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
mutex_lock(&msi_prepare_lock); mutex_lock(&msi_prepare_lock);
msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain); msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
mutex_unlock(&msi_prepare_lock); mutex_unlock(&msi_prepare_lock);
msi_desc_set_iommu_cookie(desc, msi_page);
if (!msi_page) if (!msi_page)
return -ENOMEM; return -ENOMEM;
msi_desc_set_iommu_msi_iova(
desc, msi_page->iova,
ilog2(cookie_msi_granule(domain->iova_cookie)));
return 0; return 0;
} }
@@ -1743,18 +1744,15 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
*/ */
void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg) void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
{ {
struct device *dev = msi_desc_to_dev(desc); #ifdef CONFIG_IRQ_MSI_IOMMU
const struct iommu_domain *domain = iommu_get_domain_for_dev(dev); if (desc->iommu_msi_shift) {
const struct iommu_dma_msi_page *msi_page; u64 msi_iova = desc->iommu_msi_iova << desc->iommu_msi_shift;
msi_page = msi_desc_get_iommu_cookie(desc); msg->address_hi = upper_32_bits(msi_iova);
msg->address_lo = lower_32_bits(msi_iova) |
if (!domain || !domain->iova_cookie || WARN_ON(!msi_page)) (msg->address_lo & ((1 << desc->iommu_msi_shift) - 1));
return; }
#endif
msg->address_hi = upper_32_bits(msi_page->iova);
msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
msg->address_lo += lower_32_bits(msi_page->iova);
} }
static int iommu_dma_init(void) static int iommu_dma_init(void)

View File

@@ -171,6 +171,10 @@ struct msi_desc_data {
* @dev: Pointer to the device which uses this descriptor * @dev: Pointer to the device which uses this descriptor
* @msg: The last set MSI message cached for reuse * @msg: The last set MSI message cached for reuse
* @affinity: Optional pointer to a cpu affinity mask for this descriptor * @affinity: Optional pointer to a cpu affinity mask for this descriptor
* @iommu_msi_iova: Optional shifted IOVA from the IOMMU to override the msi_addr.
* Only used if iommu_msi_shift != 0
* @iommu_msi_shift: Indicates how many bits of the original address should be
* preserved when using iommu_msi_iova.
* @sysfs_attr: Pointer to sysfs device attribute * @sysfs_attr: Pointer to sysfs device attribute
* *
* @write_msi_msg: Callback that may be called when the MSI message * @write_msi_msg: Callback that may be called when the MSI message
@@ -189,7 +193,8 @@ struct msi_desc {
struct msi_msg msg; struct msi_msg msg;
struct irq_affinity_desc *affinity; struct irq_affinity_desc *affinity;
#ifdef CONFIG_IRQ_MSI_IOMMU #ifdef CONFIG_IRQ_MSI_IOMMU
const void *iommu_cookie; u64 iommu_msi_iova : 58;
u64 iommu_msi_shift : 6;
#endif #endif
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
struct device_attribute *sysfs_attrs; struct device_attribute *sysfs_attrs;
@@ -306,28 +311,14 @@ struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
#define msi_desc_to_dev(desc) ((desc)->dev) #define msi_desc_to_dev(desc) ((desc)->dev)
static inline void msi_desc_set_iommu_msi_iova(struct msi_desc *desc, u64 msi_iova,
unsigned int msi_shift)
{
#ifdef CONFIG_IRQ_MSI_IOMMU #ifdef CONFIG_IRQ_MSI_IOMMU
static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc) desc->iommu_msi_iova = msi_iova >> msi_shift;
{ desc->iommu_msi_shift = msi_shift;
return desc->iommu_cookie;
}
static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
const void *iommu_cookie)
{
desc->iommu_cookie = iommu_cookie;
}
#else
static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
{
return NULL;
}
static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
const void *iommu_cookie)
{
}
#endif #endif
}
int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid, int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
struct msi_desc *init_desc); struct msi_desc *init_desc);