Skip to content

Commit 1f7df3a

Browse files
committed
genirq/msi: Store the IOMMU IOVA directly in msi_desc instead of iommu_cookie
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 <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent dc10ba2 commit 1f7df3a

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

drivers/iommu/dma-iommu.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
18151815
static DEFINE_MUTEX(msi_prepare_lock); /* see below */
18161816

18171817
if (!domain || !domain->iova_cookie) {
1818-
desc->iommu_cookie = NULL;
1818+
msi_desc_set_iommu_msi_iova(desc, 0, 0);
18191819
return 0;
18201820
}
18211821

@@ -1827,11 +1827,12 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
18271827
mutex_lock(&msi_prepare_lock);
18281828
msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
18291829
mutex_unlock(&msi_prepare_lock);
1830-
1831-
msi_desc_set_iommu_cookie(desc, msi_page);
1832-
18331830
if (!msi_page)
18341831
return -ENOMEM;
1832+
1833+
msi_desc_set_iommu_msi_iova(
1834+
desc, msi_page->iova,
1835+
ilog2(cookie_msi_granule(domain->iova_cookie)));
18351836
return 0;
18361837
}
18371838

@@ -1842,18 +1843,15 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
18421843
*/
18431844
void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
18441845
{
1845-
struct device *dev = msi_desc_to_dev(desc);
1846-
const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1847-
const struct iommu_dma_msi_page *msi_page;
1846+
#ifdef CONFIG_IRQ_MSI_IOMMU
1847+
if (desc->iommu_msi_shift) {
1848+
u64 msi_iova = desc->iommu_msi_iova << desc->iommu_msi_shift;
18481849

1849-
msi_page = msi_desc_get_iommu_cookie(desc);
1850-
1851-
if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1852-
return;
1853-
1854-
msg->address_hi = upper_32_bits(msi_page->iova);
1855-
msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1856-
msg->address_lo += lower_32_bits(msi_page->iova);
1850+
msg->address_hi = upper_32_bits(msi_iova);
1851+
msg->address_lo = lower_32_bits(msi_iova) |
1852+
(msg->address_lo & ((1 << desc->iommu_msi_shift) - 1));
1853+
}
1854+
#endif
18571855
}
18581856

18591857
static int iommu_dma_init(void)

include/linux/msi.h

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ struct msi_desc_data {
166166
* @dev: Pointer to the device which uses this descriptor
167167
* @msg: The last set MSI message cached for reuse
168168
* @affinity: Optional pointer to a cpu affinity mask for this descriptor
169+
* @iommu_msi_iova: Optional shifted IOVA from the IOMMU to override the msi_addr.
170+
* Only used if iommu_msi_shift != 0
171+
* @iommu_msi_shift: Indicates how many bits of the original address should be
172+
* preserved when using iommu_msi_iova.
169173
* @sysfs_attr: Pointer to sysfs device attribute
170174
*
171175
* @write_msi_msg: Callback that may be called when the MSI message
@@ -184,7 +188,8 @@ struct msi_desc {
184188
struct msi_msg msg;
185189
struct irq_affinity_desc *affinity;
186190
#ifdef CONFIG_IRQ_MSI_IOMMU
187-
const void *iommu_cookie;
191+
u64 iommu_msi_iova : 58;
192+
u64 iommu_msi_shift : 6;
188193
#endif
189194
#ifdef CONFIG_SYSFS
190195
struct device_attribute *sysfs_attrs;
@@ -285,28 +290,14 @@ struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
285290

286291
#define msi_desc_to_dev(desc) ((desc)->dev)
287292

288-
#ifdef CONFIG_IRQ_MSI_IOMMU
289-
static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
290-
{
291-
return desc->iommu_cookie;
292-
}
293-
294-
static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
295-
const void *iommu_cookie)
296-
{
297-
desc->iommu_cookie = iommu_cookie;
298-
}
299-
#else
300-
static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
293+
static inline void msi_desc_set_iommu_msi_iova(struct msi_desc *desc, u64 msi_iova,
294+
unsigned int msi_shift)
301295
{
302-
return NULL;
303-
}
304-
305-
static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
306-
const void *iommu_cookie)
307-
{
308-
}
296+
#ifdef CONFIG_IRQ_MSI_IOMMU
297+
desc->iommu_msi_iova = msi_iova >> msi_shift;
298+
desc->iommu_msi_shift = msi_shift;
309299
#endif
300+
}
310301

311302
int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
312303
struct msi_desc *init_desc);

0 commit comments

Comments
 (0)