Linux IOMMU Development
 help / color / mirror / Atom feed
* lack of permission checking with intel-iommu?
@ 2024-07-02  7:46 Christoph Hellwig
  2024-07-02  8:02 ` Christoph Hellwig
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2024-07-02  7:46 UTC (permalink / raw)
  To: David Woodhouse, Lu Baolu; +Cc: iommu

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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  7:46 lack of permission checking with intel-iommu? Christoph Hellwig
@ 2024-07-02  8:02 ` Christoph Hellwig
  2024-07-02  8:22   ` Baolu Lu
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2024-07-02  8:02 UTC (permalink / raw)
  To: David Woodhouse, Lu Baolu; +Cc: iommu

On Tue, Jul 02, 2024 at 12:46:59AM -0700, Christoph Hellwig wrote:
> 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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  8:02 ` Christoph Hellwig
@ 2024-07-02  8:22   ` Baolu Lu
  2024-07-02  8:25     ` Christoph Hellwig
  2024-07-02  8:47     ` Yi Liu
  0 siblings, 2 replies; 10+ messages in thread
From: Baolu Lu @ 2024-07-02  8:22 UTC (permalink / raw)
  To: Christoph Hellwig, David Woodhouse; +Cc: baolu.lu, iommu

On 2024/7/2 16:02, Christoph Hellwig wrote:
> On Tue, Jul 02, 2024 at 12:46:59AM -0700, Christoph Hellwig wrote:
>> Hi all,
Hi Christoph,

>>
>> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  8:22   ` Baolu Lu
@ 2024-07-02  8:25     ` Christoph Hellwig
  2024-07-02  9:12       ` Baolu Lu
  2024-07-02  8:47     ` Yi Liu
  1 sibling, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2024-07-02  8:25 UTC (permalink / raw)
  To: Baolu Lu; +Cc: Christoph Hellwig, David Woodhouse, iommu

On Tue, Jul 02, 2024 at 04:22:25PM +0800, Baolu Lu wrote:
> 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).


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  8:22   ` Baolu Lu
  2024-07-02  8:25     ` Christoph Hellwig
@ 2024-07-02  8:47     ` Yi Liu
  2024-07-02  9:07       ` Baolu Lu
  1 sibling, 1 reply; 10+ messages in thread
From: Yi Liu @ 2024-07-02  8:47 UTC (permalink / raw)
  To: Baolu Lu, Christoph Hellwig, David Woodhouse; +Cc: iommu

Hi Baolu,

On 2024/7/2 16:22, Baolu Lu wrote:
> On 2024/7/2 16:02, Christoph Hellwig wrote:
>> On Tue, Jul 02, 2024 at 12:46:59AM -0700, Christoph Hellwig wrote:
>>> Hi all,
> Hi Christoph,
> 
>>>
>>> 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.

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

-- 
Regards,
Yi Liu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  8:47     ` Yi Liu
@ 2024-07-02  9:07       ` Baolu Lu
  0 siblings, 0 replies; 10+ messages in thread
From: Baolu Lu @ 2024-07-02  9:07 UTC (permalink / raw)
  To: Yi Liu, Christoph Hellwig, David Woodhouse; +Cc: baolu.lu, iommu

On 2024/7/2 16:47, Yi Liu wrote:
>>>>
>>>> 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.
> 
> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  8:25     ` Christoph Hellwig
@ 2024-07-02  9:12       ` Baolu Lu
  2024-07-02 11:42         ` Christoph Hellwig
  2024-07-03  0:56         ` Tian, Kevin
  0 siblings, 2 replies; 10+ messages in thread
From: Baolu Lu @ 2024-07-02  9:12 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: baolu.lu, David Woodhouse, iommu

On 2024/7/2 16:25, Christoph Hellwig wrote:
> On Tue, Jul 02, 2024 at 04:22:25PM +0800, Baolu Lu wrote:
>> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-02  9:12       ` Baolu Lu
@ 2024-07-02 11:42         ` Christoph Hellwig
  2024-07-03  0:56         ` Tian, Kevin
  1 sibling, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2024-07-02 11:42 UTC (permalink / raw)
  To: Baolu Lu; +Cc: Christoph Hellwig, David Woodhouse, iommu

On Tue, Jul 02, 2024 at 05:12:32PM +0800, Baolu Lu wrote:
> 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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: lack of permission checking with intel-iommu?
  2024-07-02  9:12       ` Baolu Lu
  2024-07-02 11:42         ` Christoph Hellwig
@ 2024-07-03  0:56         ` Tian, Kevin
  2024-07-03  2:28           ` Baolu Lu
  1 sibling, 1 reply; 10+ messages in thread
From: Tian, Kevin @ 2024-07-03  0:56 UTC (permalink / raw)
  To: Baolu Lu, Christoph Hellwig; +Cc: David Woodhouse, iommu@lists.linux.dev

> From: Baolu Lu <baolu.lu@linux.intel.com>
> Sent: Tuesday, July 2, 2024 5:13 PM
> 
> On 2024/7/2 16:25, Christoph Hellwig wrote:
> > On Tue, Jul 02, 2024 at 04:22:25PM +0800, Baolu Lu wrote:
> >> 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.
> 

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: lack of permission checking with intel-iommu?
  2024-07-03  0:56         ` Tian, Kevin
@ 2024-07-03  2:28           ` Baolu Lu
  0 siblings, 0 replies; 10+ messages in thread
From: Baolu Lu @ 2024-07-03  2:28 UTC (permalink / raw)
  To: Tian, Kevin, Christoph Hellwig
  Cc: baolu.lu, David Woodhouse, iommu@lists.linux.dev

On 7/3/24 8:56 AM, Tian, Kevin wrote:
>> From: Baolu Lu <baolu.lu@linux.intel.com>
>> Sent: Tuesday, July 2, 2024 5:13 PM
>>
>> On 2024/7/2 16:25, Christoph Hellwig wrote:
>>> On Tue, Jul 02, 2024 at 04:22:25PM +0800, Baolu Lu wrote:
>>>> 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.
>>
> 
> 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

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

Thanks,
baolu

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-07-03  2:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-02  7:46 lack of permission checking with intel-iommu? Christoph Hellwig
2024-07-02  8:02 ` Christoph Hellwig
2024-07-02  8:22   ` Baolu Lu
2024-07-02  8:25     ` Christoph Hellwig
2024-07-02  9:12       ` Baolu Lu
2024-07-02 11:42         ` Christoph Hellwig
2024-07-03  0:56         ` Tian, Kevin
2024-07-03  2:28           ` Baolu Lu
2024-07-02  8:47     ` Yi Liu
2024-07-02  9:07       ` Baolu Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox