Skip to contentSkip to conversation

Export mm_update_next_owner function for unuse_mm.

PATCH In review [email protected] 3 participants 5 messages Dec 18, 2018 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Export mm_update_next_owner function for unuse_mm.

From: guomin chen <[email protected]>

When mm->owner is modified by exit_mm, if the new owner directly calls unuse_mm to exit, it will cause Use-After-Free. Due to the unuse_mm() directly sets tsk->mm=NULL.

Under normal circumstances,When do_exit exits, mm->owner will be updated on exit_mm(). but when the kernel process calls unuse_mm() and then exits,mm->owner cannot be updated. And it will point to a task that has been released.

The current issue flow is as follows:
Process C Process A Process B
qemu-system-x86_64: kernel:vhost_net kernel: vhost_net
open /dev/vhost-net
VHOST_SET_OWNER create kthread vhost-%d create kthread vhost-%d
network init use_mm() use_mm()
... ...
Abnormal exited
...
do_exit
exit_mm()
update mm->owner to A
exit_files()
close_files()
kthread_should_stop() unuse_mm()
Stop Process A tsk->mm=NULL
do_exit()
can't update owner
A exit completed vhost-%d rcv first package
vhost-%d build rcv buffer for vq page fault
access mm & mm->owner NOW,mm->owner still pointer A kernel UAF
stop Process B

Although I am having this issue on vhost_net,But it affects all users of unuse_mm.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 kernel/exit.c    | 1 +
 mm/mmu_context.c | 1 +
 2 files changed, 2 insertions(+)
kernel/exit.c +1 -0
index 0e21e6d..9e046dd 100644
@@ -486,6 +486,7 @@ void mm_update_next_owner(struct mm_struct *mm)
 	task_unlock(c);
 	put_task_struct(c);
 }
+EXPORT_SYMBOL(mm_update_next_owner);
 #endif /* CONFIG_MEMCG */
 
 /*
mm/mmu_context.c +1 -0
index 3e612ae..9eb81aa 100644
@@ -60,5 +60,6 @@ void unuse_mm(struct mm_struct *mm)
 	/* active_mm is still 'mm' */
 	enter_lazy_tlb(mm, tsk);
 	task_unlock(tsk);
+	mm_update_next_owner(mm);
 }
 EXPORT_SYMBOL_GPL(unuse_mm);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 61 lines · On Tue, Dec 18, 2018 at 11:42:11AM +0800, [email protected] wrote:

From: guomin chen <[email protected]>

When mm->owner is modified by exit_mm, if the new owner directly calls unuse_mm to exit, it will cause Use-After-Free. Due to the unuse_mm() directly sets tsk->mm=NULL.

Under normal circumstances,When do_exit exits, mm->owner will be updated on exit_mm(). but when the kernel process calls unuse_mm() and then exits,mm->owner cannot be updated. And it will point to a task that has been released.

The current issue flow is as follows:
Process C Process A Process B
qemu-system-x86_64: kernel:vhost_net kernel: vhost_net
open /dev/vhost-net
VHOST_SET_OWNER create kthread vhost-%d create kthread vhost-%d
network init use_mm() use_mm()
... ...
Abnormal exited
...
do_exit
exit_mm()
update mm->owner to A
exit_files()
close_files()
kthread_should_stop() unuse_mm()
Stop Process A tsk->mm=NULL
do_exit()
can't update owner
A exit completed vhost-%d rcv first package
vhost-%d build rcv buffer for vq page fault
access mm & mm->owner NOW,mm->owner still pointer A kernel UAF
stop Process B

Although I am having this issue on vhost_net,But it affects all users of unuse_mm.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 kernel/exit.c    | 1 +
 mm/mmu_context.c | 1 +
 2 files changed, 2 insertions(+)
kernel/exit.c +0 -0
index 0e21e6d..9e046dd 100644

kernel/exit.c void mm_update_next_owner(struct mm_struct *mm) · line 492

Quoted 7 lines
kernel/exit.c +1 -0
@@ -486,6 +486,7 @@ void mm_update_next_owner(struct mm_struct *mm)
 	task_unlock(c);
 	put_task_struct(c);
 }
+EXPORT_SYMBOL(mm_update_next_owner);
 #endif /* CONFIG_MEMCG */
 
 /*

So why export it? Is that still needed?

Quoted 13 lines
mm/mmu_context.c +1 -0
index 3e612ae..9eb81aa 100644
@@ -60,5 +60,6 @@ void unuse_mm(struct mm_struct *mm)
 	/* active_mm is still 'mm' */
 	enter_lazy_tlb(mm, tsk);
 	task_unlock(tsk);
+	mm_update_next_owner(mm);
 }
 EXPORT_SYMBOL_GPL(unuse_mm);
Signature · 1 line
1.8.3.1
author
#2 lore raw

Sorry,It does not need to be exported.
I have modified this patch and re-commit it.
Thank you very much for reminding.

thanks

Michael S. Tsirkin <[email protected]> 于2018年12月18日周二 下午12:38写道:

Quoted 23 lines · On Tue, Dec 18, 2018 at 11:42:11AM +0800, [email protected] wrote:

From: guomin chen <[email protected]>

When mm->owner is modified by exit_mm, if the new owner directly calls unuse_mm to exit, it will cause Use-After-Free. Due to the unuse_mm() directly sets tsk->mm=NULL.

Under normal circumstances,When do_exit exits, mm->owner will be updated on exit_mm(). but when the kernel process calls unuse_mm() and then exits,mm->owner cannot be updated. And it will point to a task that has been released.

The current issue flow is as follows:
Process C Process A Process B
qemu-system-x86_64: kernel:vhost_net kernel: vhost_net
open /dev/vhost-net
VHOST_SET_OWNER create kthread vhost-%d create kthread vhost-%d
network init use_mm() use_mm()
... ...
Abnormal exited
...
do_exit
exit_mm()
update mm->owner to A

Quoted 8 lines

exit_files()
close_files()
kthread_should_stop() unuse_mm()
Stop Process A tsk->mm=NULL
do_exit()
can't update owner
A exit completed vhost-%d rcv first package
vhost-%d build rcv buffer for

vq

Quoted 30 lines
                                           page fault
                                           access mm & mm->owner
                                           NOW,mm->owner still pointer A
                                           kernel UAF
    stop Process B

Although I am having this issue on vhost_net,But it affects all users of unuse_mm.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 kernel/exit.c    | 1 +
 mm/mmu_context.c | 1 +
 2 files changed, 2 insertions(+)
kernel/exit.c +0 -0
index 0e21e6d..9e046dd 100644

kernel/exit.c void mm_update_next_owner(struct mm_struct *mm) · line 492

Quoted 7 lines
kernel/exit.c +1 -0
@@ -486,6 +486,7 @@ void mm_update_next_owner(struct mm_struct *mm)
      task_unlock(c);
      put_task_struct(c);
 }
+EXPORT_SYMBOL(mm_update_next_owner);
 #endif /* CONFIG_MEMCG */
 
 /*

So why export it? Is that still needed?

Quoted 13 lines
mm/mmu_context.c +1 -1
index 3e612ae..9eb81aa 100644
@@ -60,5 +60,6 @@ void unuse_mm(struct mm_struct *mm)
      /* active_mm is still 'mm' */
      enter_lazy_tlb(mm, tsk);
      task_unlock(tsk);
+     mm_update_next_owner(mm);
 }
 EXPORT_SYMBOL_GPL(unuse_mm);
--

1.8.3.1

#3 lore raw
In reply to [email protected]
Quoted 1 line · On Tue, Dec 18, 2018 at 11:42:11AM +0800, [email protected] wrote:

+EXPORT_SYMBOL(mm_update_next_owner);

Unless you've figured out how to build mmu_context.c as a module, you don't need to EXPORT the symbol. Just the below hunk is enough.

Quoted 14 lines
mm/mmu_context.c +1 -0
index 3e612ae..9eb81aa 100644
@@ -60,5 +60,6 @@ void unuse_mm(struct mm_struct *mm)
 	/* active_mm is still 'mm' */
 	enter_lazy_tlb(mm, tsk);
 	task_unlock(tsk);
+	mm_update_next_owner(mm);
 }
 EXPORT_SYMBOL_GPL(unuse_mm);
Signature · 1 line
1.8.3.1
author
#4 lore raw

Yes, I don't need to EXPORT this symbol again.
i have modified this patch and re-committed it. The email link is :
https://lkml.org/lkml/2018/12/18/8
this patch you see is old.

thanks and regards

Quoted 21 lines · Matthew Wilcox <[email protected]> 于2018年12月18日周二 下午8:47写道:
Quoted 1 line · On Tue, Dec 18, 2018 at 11:42:11AM +0800, [email protected] wrote:

+EXPORT_SYMBOL(mm_update_next_owner);

Unless you've figured out how to build mmu_context.c as a module, you don't need to EXPORT the symbol. Just the below hunk is enough.

Quoted 14 lines
mm/mmu_context.c +1 -1
index 3e612ae..9eb81aa 100644
@@ -60,5 +60,6 @@ void unuse_mm(struct mm_struct *mm)
      /* active_mm is still 'mm' */
      enter_lazy_tlb(mm, tsk);
      task_unlock(tsk);
+     mm_update_next_owner(mm);
 }
 EXPORT_SYMBOL_GPL(unuse_mm);
--

1.8.3.1