On 11/16/26 16:51, Christian König wrote:
> 
> 
> On 11/12/26 23:09, David Hildenbrand (Arm) wrote:
>> GUP rejects VM_IO and VM_PFNMAP mappings early. __access_remote_vm() then
>> falls back to vm_ops->access without checking VMA permissions and without
>> limiting the request to the VMA size.
>>
>> Consequently, /proc/<pid>/mem can bypass protections even without
>> FOLL_FORCE, or cross into another mapping without checking its
>> permissions.
>>
>> Check VMA permissions before calling ->access and limit each request to
>> the current VMA. Implement similar access permission checks as GUP,
>> however:
>>  (1) Reject writes to CoW mappings because the callbacks access backing
>>      memory directly and cannot handle CoW.
>>  (2) Allow forced writes to non-CoW mappings with VM_MAYWRITE.
>>
>> This intentionally breaks forced writes through legacy amdgpu PROT_NONE,
>> MAP_PRIVATE debugger mappings. The workaround in commit fa5239f2af98
>> ("drm/amdgpu: workaround failed COW checks for Thunk VMAs") cleared
>> VM_MAYWRITE to avoid CoW, but debugger writes still succeeded by
>> exploiting the missing access-permission checks fixed here. Reads remain
>> supported. Userspace must use MAP_SHARED instead; ROCm 5.5.0, released
>> in May 2023, already does so.
>>
>> Link: 
>> https://github.com/ROCm/ROCT-Thunk-Interface/commit/fea44fab6e84eda52fcf6b419b35600c18f4c022
>> Fixes: 272ddc8b3735 ("proc: don't use FOLL_FORCE for reading cmdline and 
>> environment")
>> Cc: [email protected]
>> Cc: Felix Kuehling <[email protected]>
>> Cc: Christian König <[email protected]>
>> Cc: Simona Vetter <[email protected]>
>> Reported-by: Chris Mason <[email protected]>
>> Signed-off-by: David Hildenbrand (Arm) <[email protected]>
>> ---
>>  mm/memory.c | 25 +++++++++++++++++++++++--
>>  1 file changed, 23 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/memory.c b/mm/memory.c
>> index d4370c1b331c9..9872f289c1e49 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -7362,6 +7362,7 @@ EXPORT_SYMBOL_GPL(generic_access_phys);
>>  static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
>>                            void *buf, int len, unsigned int gup_flags)
>>  {
>> +    const bool force = gup_flags & FOLL_FORCE;
>>      void *old_buf = buf;
>>      int write = gup_flags & FOLL_WRITE;
>>  
>> @@ -7401,15 +7402,35 @@ static int __access_remote_vm(struct mm_struct *mm, 
>> unsigned long addr,
>>                      if (gup_flags & FOLL_ANON)
>>                              break;
>>  
>> +                    if (write) {
> 
>> +                            if (!vma_test(vma, VMA_MAYWRITE_BIT))
>> +                                    break;
>> +                            if (!vma_test(vma, VMA_WRITE_BIT) && !force)
>> +                                    break;
> 

Hi,

> I might be wrong but IIRC that was brought up before and was rejected.
> 
> We even had a bug report for AMDGPU from a security researcher claiming that 
> you could circumvent security checks using vma->vm_ops->access() which turned 
> out to be complete nonsense after spending tons of time investigating it.
> 

independent of all actual security issues, the current core-mm is completely
fragile as is.

This needs to be fixed independent of driver hacks that are in place.

> The technical background is that there are applications which runtime 
> generate shader code which is then protected by making the VMA read only. But 
> the debugger is still allowed to write to it using vma->vm_ops->access().
> 

Yes, that's precisely what FOLL_FORCE is for, which is what debuggers (ptrace
access) should be using.

So if there are debugger interfaces that are not using FOLL_FORCE, we would have
to see what we can do about that. (and not keep core-mm fragile)


> That behavior is ok because drivers specific and drivers have checks in place 
> if their backing store is read only or not.
> 

"trust me, drivers do the right thing"

Core-mm needs to to something reasonable.
> For an example see this code here: 
> https://elixir.bootlin.com/linux/v7.3.1/source/drivers/gpu/drm/i915/gem/i915_gem_ttm.c#L1163
> 
> So if you do this here you might break intentional uAPI.

Let's figure out what would actually be broken to then decide how we can keep it
working without keeping core-mm a swiss cheese.

Thanks for the review!

-- 
Cheers,

David

Reply via email to