linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases
@ 2021-03-18 10:08 vjitta
  2021-03-18 16:59 ` Uladzislau Rezki
  0 siblings, 1 reply; 4+ messages in thread
From: vjitta @ 2021-03-18 10:08 UTC (permalink / raw)
  To: akpm, linux-mm, linux-kernel; +Cc: vjitta, vinmenon

From: Vijayanand Jitta <vjitta@codeaurora.org>

A potential use after free can occur in _vm_unmap_aliases
where an already freed vmap_area could be accessed, Consider
the following scenario:

Process 1						Process 2

__vm_unmap_aliases					__vm_unmap_aliases
	purge_fragmented_blocks_allcpus				rcu_read_lock()
		rcu_read_lock()
			list_del_rcu(&vb->free_list)
									list_for_each_entry_rcu(vb .. )
	__purge_vmap_area_lazy
		kmem_cache_free(va)
										va_start = vb->va->va_start

Here Process 1 is in purge path and it does list_del_rcu on vmap_block
and later frees the vmap_area, since Process 2 was holding the rcu lock
at this time vmap_block will still be present in and Process 2 accesse
it and thereby it tries to access vmap_area of that vmap_block which was
already freed by Process 1 and this results in use after free.

Fix this by adding a check for vb->dirty before accessing vmap_area
structure since vb->dirty will be set to VMAP_BBMAP_BITS in purge path
checking for this will prevent the use after free.

Signed-off-by: Vijayanand Jitta <vjitta@codeaurora.org>
---
 mm/vmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d5f2a84..ebb6f57 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1762,7 +1762,7 @@ static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
 		rcu_read_lock();
 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
 			spin_lock(&vb->lock);
-			if (vb->dirty) {
+			if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
 				unsigned long va_start = vb->va->va_start;
 				unsigned long s, e;
 
-- 
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
2.7.4


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

* Re: [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases
  2021-03-18 10:08 [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases vjitta
@ 2021-03-18 16:59 ` Uladzislau Rezki
  2021-03-24  3:29   ` Vijayanand Jitta
  0 siblings, 1 reply; 4+ messages in thread
From: Uladzislau Rezki @ 2021-03-18 16:59 UTC (permalink / raw)
  To: vjitta; +Cc: akpm, linux-mm, linux-kernel, vinmenon

On Thu, Mar 18, 2021 at 03:38:25PM +0530, vjitta@codeaurora.org wrote:
> From: Vijayanand Jitta <vjitta@codeaurora.org>
> 
> A potential use after free can occur in _vm_unmap_aliases
> where an already freed vmap_area could be accessed, Consider
> the following scenario:
> 
> Process 1						Process 2
> 
> __vm_unmap_aliases					__vm_unmap_aliases
> 	purge_fragmented_blocks_allcpus				rcu_read_lock()
> 		rcu_read_lock()
> 			list_del_rcu(&vb->free_list)
> 									list_for_each_entry_rcu(vb .. )
> 	__purge_vmap_area_lazy
> 		kmem_cache_free(va)
> 										va_start = vb->va->va_start
Or maybe we should switch to kfree_rcu() instead of kmem_cache_free()?

--
Vlad Rezki

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

* Re: [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases
  2021-03-18 16:59 ` Uladzislau Rezki
@ 2021-03-24  3:29   ` Vijayanand Jitta
  2021-03-24 13:32     ` Uladzislau Rezki
  0 siblings, 1 reply; 4+ messages in thread
From: Vijayanand Jitta @ 2021-03-24  3:29 UTC (permalink / raw)
  To: Uladzislau Rezki; +Cc: akpm, linux-mm, linux-kernel, vinmenon



On 3/18/2021 10:29 PM, Uladzislau Rezki wrote:
> On Thu, Mar 18, 2021 at 03:38:25PM +0530, vjitta@codeaurora.org wrote:
>> From: Vijayanand Jitta <vjitta@codeaurora.org>
>>
>> A potential use after free can occur in _vm_unmap_aliases
>> where an already freed vmap_area could be accessed, Consider
>> the following scenario:
>>
>> Process 1						Process 2
>>
>> __vm_unmap_aliases					__vm_unmap_aliases
>> 	purge_fragmented_blocks_allcpus				rcu_read_lock()
>> 		rcu_read_lock()
>> 			list_del_rcu(&vb->free_list)
>> 									list_for_each_entry_rcu(vb .. )
>> 	__purge_vmap_area_lazy
>> 		kmem_cache_free(va)
>> 										va_start = vb->va->va_start
> Or maybe we should switch to kfree_rcu() instead of kmem_cache_free()?
> 
> --
> Vlad Rezki
> 

Thanks for suggestion.

I see free_vmap_area_lock (spinlock) is taken in __purge_vmap_area_lazy
while it loops through list and calls kmem_cache_free on va's. So, looks
like we can't replace it with kfree_rcu as it might cause scheduling
within atomic context.

Thanks,
Vijay
-- 
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of Code Aurora Forum, hosted by The Linux Foundation

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

* Re: [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases
  2021-03-24  3:29   ` Vijayanand Jitta
@ 2021-03-24 13:32     ` Uladzislau Rezki
  0 siblings, 0 replies; 4+ messages in thread
From: Uladzislau Rezki @ 2021-03-24 13:32 UTC (permalink / raw)
  To: Vijayanand Jitta; +Cc: Uladzislau Rezki, akpm, linux-mm, linux-kernel, vinmenon

> 
> On 3/18/2021 10:29 PM, Uladzislau Rezki wrote:
> > On Thu, Mar 18, 2021 at 03:38:25PM +0530, vjitta@codeaurora.org wrote:
> >> From: Vijayanand Jitta <vjitta@codeaurora.org>
> >>
> >> A potential use after free can occur in _vm_unmap_aliases
> >> where an already freed vmap_area could be accessed, Consider
> >> the following scenario:
> >>
> >> Process 1						Process 2
> >>
> >> __vm_unmap_aliases					__vm_unmap_aliases
> >> 	purge_fragmented_blocks_allcpus				rcu_read_lock()
> >> 		rcu_read_lock()
> >> 			list_del_rcu(&vb->free_list)
> >> 									list_for_each_entry_rcu(vb .. )
> >> 	__purge_vmap_area_lazy
> >> 		kmem_cache_free(va)
> >> 										va_start = vb->va->va_start
> > Or maybe we should switch to kfree_rcu() instead of kmem_cache_free()?
> > 
> > --
> > Vlad Rezki
> > 
> 
> Thanks for suggestion.
> 
> I see free_vmap_area_lock (spinlock) is taken in __purge_vmap_area_lazy
> while it loops through list and calls kmem_cache_free on va's. So, looks
> like we can't replace it with kfree_rcu as it might cause scheduling
> within atomic context.
> 
A double argument of the kfree_rcu() is a safe way to be used from atomic
contexts, it does not use any sleeping primitives, so it can be replaced.

From the other hand i see that per-cpu KVA allocator is only one user of
the RCU and your change fixes it. Feel free to use:

Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>

Thanks.

--
Vlad Rezki

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

end of thread, other threads:[~2021-03-24 13:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-18 10:08 [PATCH] mm: vmalloc: Prevent use after free in _vm_unmap_aliases vjitta
2021-03-18 16:59 ` Uladzislau Rezki
2021-03-24  3:29   ` Vijayanand Jitta
2021-03-24 13:32     ` Uladzislau Rezki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).