Skip to contentSkip to conversation

mm/nommu: Fix NULL mm dereference in __vmalloc_user_flags

PATCH In review sooraj 2 participants 2 messages Jan 27, 2025

Subsystems (2)
lore
Share & tools
lore

sooraj

author
#0 lore raw

PATCH 1/1 mm/nommu: Fix NULL mm dereference in __vmalloc_user_flags

Problem:
The __vmalloc_user_flags() function attempts to acquire mmap_lock via current->mm without checking if the mm context exists. This causes a NULL pointer dereference when called from kernel threads (where current->mm == NULL), such as during filesystem operations.

Fix:
Add a NULL check for current->mm before attempting mmap_lock operations. Kernel threads don't have user memory mappings, so VM_USERMAP flag setting can be safely skipped in this context.

---
 mm/nommu.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/nommu.c +6 -0
index baa79abdaf03..df13c1c78d5b 100644
@@ -152,6 +152,11 @@ static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
 	ret = __vmalloc(size, flags);
 	if (ret) {
 		struct vm_area_struct *vma;
+		struct mm_struct *mm = current->mm;
+
+		if(!mm){
+			goto out;
+		}
 
 		mmap_write_lock(current->mm);
 		vma = find_vma(current->mm, (unsigned long)ret);
@@ -160,6 +165,7 @@ static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
 		mmap_write_unlock(current->mm);
 	}
 
+	out:
 	return ret;
 }
 
Signature · 1 line
2.45.2
#1 lore raw
Quoted 5 lines · sooraj

Problem:
The __vmalloc_user_flags() function attempts to acquire mmap_lock via current->mm without checking if the mm context exists. This causes a NULL pointer dereference when called from kernel threads (where current->mm == NULL), such as during filesystem operations.

I'm having a hard time thinking of a case where this could happen. Could you provide a backtrace showing an example?

Quoted 6 lines

Fix:
Add a NULL check for current->mm before attempting mmap_lock operations. Kernel threads don't have user memory mappings, so VM_USERMAP flag setting can be safely skipped in this context.

Do you sign cheques as 'sooraj'? You need to use your real, legal name.

mm/nommu.c static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) · line 159

Quoted 5 lines
mm/nommu.c +5 -0
+		struct mm_struct *mm = current->mm;
+
+		if(!mm){
+			goto out;
+		}

All kinds of whitespace problems here. It should be:

		if (!mm)
			goto out;

mm/nommu.c static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) · line 162

Quoted 2 lines
mm/nommu.c +0 -0
 		mmap_write_lock(current->mm);
 		vma = find_vma(current->mm, (unsigned long)ret);

And since you've gone to the trouble of loading current->mm into a local variable, you should use it throughout the function.

mm/nommu.c static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) · line 168

Quoted 5 lines
mm/nommu.c +1 -0
@@ -160,6 +165,7 @@ static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
 		mmap_write_unlock(current->mm);
 	}
 
+	out:

and this should not be indented.

Quoted 1 line
mm/nommu.c +0 -0
 	return ret;