Skip to content

Commit 6362289

Browse files
committed
userfaultfd: fix a race between writeprotect and exit_mmap()
jira VULN-4370 cve CVE-2021-47461 commit-author Nadav Amit <[email protected]> commit cb185d5 A race is possible when a process exits, its VMAs are removed by exit_mmap() and at the same time userfaultfd_writeprotect() is called. The race was detected by KASAN on a development kernel, but it appears to be possible on vanilla kernels as well. Use mmget_not_zero() to prevent the race as done in other userfaultfd operations. Link: https://lkml.kernel.org/r/[email protected] Fixes: 63b2d41 ("userfaultfd: wp: add the writeprotect API to userfaultfd ioctl") Signed-off-by: Nadav Amit <[email protected]> Tested-by: Li Wang <[email protected]> Reviewed-by: Peter Xu <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> (cherry picked from commit cb185d5) Signed-off-by: Greg Rose <[email protected]>
1 parent 0c3116e commit 6362289

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

fs/userfaultfd.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1801,9 +1801,15 @@ static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
18011801
if (mode_wp && mode_dontwake)
18021802
return -EINVAL;
18031803

1804-
ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
1805-
uffdio_wp.range.len, mode_wp,
1806-
&ctx->mmap_changing);
1804+
if (mmget_not_zero(ctx->mm)) {
1805+
ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
1806+
uffdio_wp.range.len, mode_wp,
1807+
&ctx->mmap_changing);
1808+
mmput(ctx->mm);
1809+
} else {
1810+
return -ESRCH;
1811+
}
1812+
18071813
if (ret)
18081814
return ret;
18091815

0 commit comments

Comments
 (0)