Skip to contentSkip to conversation

lack of permission checking with intel-iommu?

DISCUSSION Christoph Hellwig 4 participants 10 messages Jul 2, 2024 – Jul 3, 2024

lore
Share & tools
lore
#0 lore raw

Hi all,

we've recently had an interesting case where a device unexpectedly modified a dma buffer passed to it. On AMD systems this caused iommu faults because the buffer was mapped read only (DMA_FROM_DEVICE), but on an Intel system everything worked fine as the IOMMU did not trip up on the device writes to that buffer. Any idea why that could sneak through?

#1 lore raw
Quoted 8 lines · Christoph Hellwig

Hi all,

we've recently had an interesting case where a device unexpectedly modified a dma buffer passed to it. On AMD systems this caused iommu faults because the buffer was mapped read only (DMA_FROM_DEVICE), but on an Intel system everything worked fine as the IOMMU did not trip up on the device writes to that buffer. Any idea why that could sneak through?

Sorry if this was confusing, the permission checking is of course about the device reading from a buffer mapped read-only for the device (as that is what DMA_FROM_DEVICE is), which on intel still allows it to read.

#2 lore raw
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Christoph Hellwig

Hi all,

Hi Christoph,

Quoted 11 lines
Quoted 7 lines

we've recently had an interesting case where a device unexpectedly modified a dma buffer passed to it. On AMD systems this caused iommu faults because the buffer was mapped read only (DMA_FROM_DEVICE), but on an Intel system everything worked fine as the IOMMU did not trip up on the device writes to that buffer. Any idea why that could sneak through?

Sorry if this was confusing, the permission checking is of course about the device reading from a buffer mapped read-only for the device (as that is what DMA_FROM_DEVICE is), which on intel still allows it to read.

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

Best regards,
baolu

#3 lore raw
Quoted 6 lines · Baolu Lu

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

This is about device reading from buffer it is only supposed to write to (but doesn't).

#4 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Baolu Lu

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

This is about device reading from buffer it is only supposed to write to (but doesn't).

In this case, Intel IOMMU doesn't block device reading as the first- stage page table only grants two types of permissions: Read-only or Read-write. Write-only permission is not available.

Do you have a functional requirement for Write-only permission? If so, we can choose the second-stage page table.

Best regards,
baolu

#5 lore raw
Quoted 6 lines · Baolu Lu

In this case, Intel IOMMU doesn't block device reading as the first- stage page table only grants two types of permissions: Read-only or Read-write. Write-only permission is not available.

Do you have a functional requirement for Write-only permission? If so, we can choose the second-stage page table.

No functional need, but it would have found a device bug.

#6 lore raw
In reply to Baolu Lu
Quoted 14 lines

From: Baolu Lu <[email protected]>
Sent: Tuesday, July 2, 2024 5:13 PM

Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Baolu Lu

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

This is about device reading from buffer it is only supposed to write to (but doesn't).

Quoted 7 lines

In this case, Intel IOMMU doesn't block device reading as the first- stage page table only grants two types of permissions: Read-only or Read-write. Write-only permission is not available.

Do you have a functional requirement for Write-only permission? If so, we can choose the second-stage page table.

Note there could be configurations (e.g. vIOMMU) which only supports 1st-stage. So there is no guarantee that write-only permission can be always enforced on Intel.

btw according to current code looks write-only even doesn't work on 2nd-stage:

__domain_mapping()

{
	...
	attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP);
	attr |= DMA_FL_PTE_PRESENT;
	if (domain->use_first_level) {
                		...
	}
	...
}

DMA_FL_PTE_PRESENT and DMA_PTE_READ are both bit-0, so READ is always set in 2nd-stage. That line should be done only for domain->use_first_level==true

#7 lore raw
Quoted 40 lines · Kevin
Quoted 14 lines

From: Baolu Lu <[email protected]>
Sent: Tuesday, July 2, 2024 5:13 PM

Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Baolu Lu

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

This is about device reading from buffer it is only supposed to write to (but doesn't).

Quoted 7 lines

In this case, Intel IOMMU doesn't block device reading as the first- stage page table only grants two types of permissions: Read-only or Read-write. Write-only permission is not available.

Do you have a functional requirement for Write-only permission? If so, we can choose the second-stage page table.

Note there could be configurations (e.g. vIOMMU) which only supports 1st-stage. So there is no guarantee that write-only permission can be always enforced on Intel.

btw according to current code looks write-only even doesn't work on 2nd-stage:

__domain_mapping()

{
	...
	attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP);
	attr |= DMA_FL_PTE_PRESENT;
	if (domain->use_first_level) {
                 		...
	}
	...
}
Quoted 3 lines

DMA_FL_PTE_PRESENT and DMA_PTE_READ are both bit-0, so READ is always set in 2nd-stage. That line should be done only for domain->use_first_level==true

This is intended to make the permission consistent between first and second stage.

Thanks,
baolu

#8 lore raw
In reply to Baolu Lu

Hi Baolu,

Quoted 17 lines · Baolu Lu
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Christoph Hellwig

Hi all,

Hi Christoph,

Quoted 11 lines
Quoted 7 lines

we've recently had an interesting case where a device unexpectedly modified a dma buffer passed to it.  On AMD systems this caused iommu faults because the buffer was mapped read only (DMA_FROM_DEVICE), but on an Intel system everything worked fine as the IOMMU did not trip up on the device writes to that buffer.  Any idea why that could sneak through?

Sorry if this was confusing, the permission checking is of course about the device reading from a buffer mapped read-only for the device (as that is what DMA_FROM_DEVICE is), which on intel still allows it to read.

Quoted 6 lines

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

does the current kernel use the first-stage page table by default?

Signature · 2 lines
Regards,
Yi Liu
#9 lore raw
Quoted 12 lines · Yi Liu
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines

we've recently had an interesting case where a device unexpectedly modified a dma buffer passed to it.  On AMD systems this caused iommu faults because the buffer was mapped read only (DMA_FROM_DEVICE), but on an Intel system everything worked fine as the IOMMU did not trip up on the device writes to that buffer.  Any idea why that could sneak through?

Sorry if this was confusing, the permission checking is of course about the device reading from a buffer mapped read-only for the device (as that is what DMA_FROM_DEVICE is), which on intel still allows it to read.

Quoted 8 lines
Quoted 6 lines

Is it about IOMMU blocking "device reading from buffer" or "device writing to buffer"?

The intel iommu driver might use first-stage translation for kernel DMA. It seems not to block the "device reading from buffer" if I remember it correctly.

does the current kernel use the first-stage page table by default?

Use first-stage page table for kernel DMA by default.

Best regards,
baolu