Skip to contentSkip to conversation

mm/gup: fault in ranges that start at address 0

PATCH In review Hengyu Liang 2 participants 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup: fault in ranges that start at address 0

Commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions") made the loops of fault_in_readable(), fault_in_writeable() and fault_in_safe_writeable() stop when the current address is 0, to detect an overflow.

However, the loops then do not run at all for a range that starts at address 0, and the functions report that nothing was faulted in. As of now, a write() from a buffer at address 0 fails with EFAULT when the page is mapped but not present yet.

The issue can be reproduced with this program, run as root:

    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/mman.h>
    #include <unistd.h>

    int main(void)
    {
            int fd = open("file", O_WRONLY | O_CREAT, 0644);

            mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
            printf("%zd\n", write(fd, NULL, 7));
            return 0;
    }

Before commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions"), the result is:

    7

After that commit, the result is:

    -1

With this patch, the result is the same as before that commit.

This patch will stop the loops when the current address is below the start address instead, which is only the case after an overflow.

Fixes a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions")

---
I found this while comparing the behavior of different kernel versions. I used an LLM to analyze the problem and to draft the patch and this message, and I analyzed the result myself.

Tested on v7.4-rc3 with KASAN and lockdep: with the buffer at address 0 and at 0x10000, on ext4, tmpfs and xfs, write(), pwritev() and O_DIRECT reads and writes give the same results as before commit a7797e74bd39.

 mm/gup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/gup.c +3 -3
index 2b405cce2ca5..a174e93327e9 100644
@@ -2074,7 +2074,7 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
 		return size;
 
 	/* Stop once we overflow to 0. */
-	for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+	for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
 		unsafe_put_user(0, (char __user *)cur, out);
 out:
 	user_write_access_end();
@@ -2144,7 +2144,7 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
 
 	mmap_read_lock(mm);
 	/* Stop once we overflow to 0. */
-	for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+	for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
 		if (fixup_user_fault(mm, cur, FAULT_FLAG_WRITE, &unlocked))
 			break;
 	mmap_read_unlock(mm);
@@ -2176,7 +2176,7 @@ size_t fault_in_readable(const char __user *uaddr, size_t size)
 		return size;
 
 	/* Stop once we overflow to 0. */
-	for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+	for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
 		unsafe_get_user(c, (const char __user *)cur, out);
 out:
 	user_read_access_end();

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.53.0
#1 lore raw
Quoted 12 lines · Hengyu Liang

Commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions") made the loops of fault_in_readable(), fault_in_writeable() and fault_in_safe_writeable() stop when the current address is 0, to detect an overflow.

However, the loops then do not run at all for a range that starts at address 0, and the functions report that nothing was faulted in. As of now, a write() from a buffer at address 0 fails with EFAULT when the page is mapped but not present yet.

The issue can be reproduced with this program, run as root:

Quoted 14 lines
    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/mman.h>
    #include <unistd.h>

    int main(void)
    {
            int fd = open("file", O_WRONLY | O_CREAT, 0644);

            mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
            printf("%zd\n", write(fd, NULL, 7));
            return 0;
    }

Thanks for the report.

Ah, so we require root, because usually we have vm.mmap_min_addr limits in place.

Where we disallow mapping to address 0 for good reasons ;)

Quoted 15 lines

Before commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions"), the result is:

    7

After that commit, the result is:

    -1

With this patch, the result is the same as before that commit.

This patch will stop the loops when the current address is below the start address instead, which is only the case after an overflow.

Quoted 7 lines
Fixes a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions")

---
I found this while comparing the behavior of different kernel versions. I used an LLM to analyze the problem and to draft the patch and this message, and I analyzed the result myself.

Okay, but you did not actually run into this yourself, right?

Doing some research, mapping to address 0 should not really have any users.

There are some legacy dosem and wine emulators, but looks like that's only a problem for 32bit and all legacy.

Likely this is "nobody cares" and we don't need CC stable.

Quoted 13 lines

Tested on v7.4-rc3 with KASAN and lockdep: with the buffer at address 0 and at 0x10000, on ext4, tmpfs and xfs, write(), pwritev() and O_DIRECT reads and writes give the same results as before commit a7797e74bd39.

 mm/gup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/gup.c +0 -0
index 2b405cce2ca5..a174e93327e9 100644

mm/gup.c size_t fault_in_writeable(char __user *uaddr, size_t size) · line 2076

Quoted 3 lines
mm/gup.c +0 -0
@@ -2074,7 +2074,7 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
 		return size;
 
 	/* Stop once we overflow to 0. */

These comments are not really correct anymore. We don't stop once we overflow to 0 anymore.

Likely we want to be more careful when calcualting+handling end instead.

Signature · 3 lines
Cheers,

David