History

kthread: Warn if mm_struct lacks user_ns in kthread_use_mm()

Add a WARN_ON_ONCE() check to detect mm_struct instances that are missing user_ns initialization when passed to kthread_use_mm(). When a kthread adopts an mm via kthread_use_mm(), LSM hooks and capability checks may access current->mm->user_ns for credential validation. If user_ns is NULL, this leads to a NULL pointer dereference crash. This was observed with efi_mm on arm64, where commit a5baf582f4c0 ("arm64/efi: Call EFI runtime services without disabling preemption") introduced kthread_use_mm(&efi_mm), but efi_mm lacked user_ns initialization, causing crashes during /proc access. Adding this warning helps catch similar bugs early during development rather than waiting for hard-to-debug NULL pointer crashes in production.

Breno Leitao committed 10 months ago

Commit cfe54f41 parent 61ed08c1 file · +1 −0Browse files

Unified · Split

kernel/kthread.c+1−0
@@ -1599,6 +1599,7 @@ void kthread_use_mm(struct mm_struct *mm) · 1598 unmodified lines
1599 1599
1600 WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));1600 WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1601 WARN_ON_ONCE(tsk->mm);1601 WARN_ON_ONCE(tsk->mm);
1602 WARN_ON_ONCE(!mm->user_ns);
1602 1603
1603 /*1604 /*
1604 * It is possible for mm to be the same as tsk->active_mm, but1605 * It is possible for mm to be the same as tsk->active_mm, but