Skip to contentSkip to conversation

mm/gup: fix null pointer dereference detected by coverity

PATCH In review Miles Chen 4 participants 5 messages Apr 7, 2020 – Apr 15, 2020

Subsystems (3)
lore
Share & tools
lore

Miles Chen

author
#0 lore raw

PATCH 1/1 mm/gup: fix null pointer dereference detected by coverity

In fixup_user_fault(), it is possible that unlocked is NULL, so we should test unlocked before using it.

For example, in arch/arc/kernel/process.c, NULL is passed to fixup_user_fault().

SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {
...

	ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
			       FAULT_FLAG_WRITE, NULL);

...
}

Fixes 4a9e1cda2748 ("mm: bring in additional flag for fixup_user_fault to signal unlock")
---
 mm/gup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/gup.c +2 -1
index da3e03185144..a68d11dc232d 100644
@@ -1230,7 +1230,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
 	if (ret & VM_FAULT_RETRY) {
 		down_read(&mm->mmap_sem);
 		if (!(fault_flags & FAULT_FLAG_TRIED)) {
-			*unlocked = true;
+			if (unlocked)
+				*unlocked = true;
 			fault_flags |= FAULT_FLAG_TRIED;
 			goto retry;
 		}
Signature · 1 line
2.18.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Miles Chen

In fixup_user_fault(), it is possible that unlocked is NULL, so we should test unlocked before using it.

For example, in arch/arc/kernel/process.c, NULL is passed to fixup_user_fault().

SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {
...

Quoted 7 lines
	ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
			       FAULT_FLAG_WRITE, NULL);

...
}

Fixes 4a9e1cda2748 ("mm: bring in additional flag for fixup_user_fault to signal unlock")
Quoted 20 lines
---
 mm/gup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/gup.c +2 -1
index da3e03185144..a68d11dc232d 100644
@@ -1230,7 +1230,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
 	if (ret & VM_FAULT_RETRY) {
 		down_read(&mm->mmap_sem);
 		if (!(fault_flags & FAULT_FLAG_TRIED)) {
-			*unlocked = true;
+			if (unlocked)
+				*unlocked = true;
 			fault_flags |= FAULT_FLAG_TRIED;
 			goto retry;
 		}
Signature · 1 line
2.18.0
#2 lore raw
Quoted 16 lines · Ira Weiny
Quoted 14 lines · Miles Chen

In fixup_user_fault(), it is possible that unlocked is NULL, so we should test unlocked before using it.

For example, in arch/arc/kernel/process.c, NULL is passed to fixup_user_fault().

SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {
...

      ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
                             FAULT_FLAG_WRITE, NULL);

...
}

Quoted 4 lines
Quoted 2 lines
Fixes 4a9e1cda2748 ("mm: bring in additional flag for fixup_user_fault to signal unlock")
Quoted 22 lines
Quoted 21 lines
---
 mm/gup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/gup.c +2 -2
index da3e03185144..a68d11dc232d 100644
@@ -1230,7 +1230,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
      if (ret & VM_FAULT_RETRY) {
              down_read(&mm->mmap_sem);
              if (!(fault_flags & FAULT_FLAG_TRIED)) {
-                     *unlocked = true;
+                     if (unlocked)
+                             *unlocked = true;
                      fault_flags |= FAULT_FLAG_TRIED;
                      goto retry;
              }
--

2.18.0

maintainer
#3 lore raw
In reply to Miles Chen
Quoted 6 lines · On Tue, 7 Apr 2020 17:51:07 +0800 Miles Chen <[email protected]> wrote:

In fixup_user_fault(), it is possible that unlocked is NULL, so we should test unlocked before using it.

For example, in arch/arc/kernel/process.c, NULL is passed to fixup_user_fault().

Quoted 7 lines

SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {
...

	ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
			       FAULT_FLAG_WRITE, NULL);

...
}

(cc Peter)

mm/gup.c int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, · line 1237

Quoted 12 lines
mm/gup.c +2 -1
@@ -1230,7 +1230,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
 	if (ret & VM_FAULT_RETRY) {
 		down_read(&mm->mmap_sem);
 		if (!(fault_flags & FAULT_FLAG_TRIED)) {
-			*unlocked = true;
+			if (unlocked)
+				*unlocked = true;
 			fault_flags |= FAULT_FLAG_TRIED;
 			goto retry;
 		}

Not sure. If the caller passes FAULT_FLAG_ALLOW_RETRY then they must also pass in a valid non-NULL `unlocked'. If the caller passed FAULT_FLAG_ALLOW_RETRY and unlocked==NULL then the resulting oops is an appropriate way of reporting this mistake. I think?

author
#4 lore raw
Quoted 27 lines · Andrew Morton
Quoted 6 lines · On Tue, 7 Apr 2020 17:51:07 +0800 Miles Chen <[email protected]> wrote:

In fixup_user_fault(), it is possible that unlocked is NULL, so we should test unlocked before using it.

For example, in arch/arc/kernel/process.c, NULL is passed to fixup_user_fault().

Quoted 7 lines

SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) {
...

	ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
			       FAULT_FLAG_WRITE, NULL);

...
}

(cc Peter)

Quoted 9 lines
mm/gup.c +2 -1
@@ -1230,7 +1230,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
 	if (ret & VM_FAULT_RETRY) {
 		down_read(&mm->mmap_sem);
 		if (!(fault_flags & FAULT_FLAG_TRIED)) {
-			*unlocked = true;
+			if (unlocked)
+				*unlocked = true;
Quoted 9 lines

mm/gup.c int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, · line 1237

Quoted 3 lines
mm/gup.c +0 -0
 			fault_flags |= FAULT_FLAG_TRIED;
 			goto retry;
 		}

Not sure. If the caller passes FAULT_FLAG_ALLOW_RETRY then they must also pass in a valid non-NULL `unlocked'. If the caller passed FAULT_FLAG_ALLOW_RETRY and unlocked==NULL then the resulting oops is an appropriate way of reporting this mistake. I think?

Agree. How about put "unlocked==NULL must not be used with FAULT_FLAG_ALLOW_RETRY." in the comment? Make it easier to understand the oops.

e.g.,

mm/gup.c +0 -0
@@ -1176,7 +1176,8 @@ static bool vma_permits_fault(struct

vm_area_struct *vma,
* @address: user address
* @fault_flags:flags to pass down to handle_mm_fault()
* @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if
caller

- *             does not allow retry
+ *             does not allow retry. If NULL, the caller must guarantee
+ *             the fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.