Skip to contentSkip to conversation

fix for bug 1783362

PATCH Superseded Alex Bennée 4 participants 10 messages Jul 26, 2018 – Jul 28, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fix for bug 1783362

Hi,

This fixes a bug where a zero len is passed to mmap. It comes with an enhancement to the mmap test case.

Alex Bennée (2):
linux-user/mmap.c: handle len = 0 maps correctly
tests: add check_invalid_maps to test-mmap

 linux-user/mmap.c               | 14 +++++++++++---
 tests/tcg/multiarch/test-mmap.c | 16 +++++++++++++++-
 2 files changed, 26 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 linux-user/mmap.c: handle len = 0 maps correctly

I've slightly re-organised the check to more closely match the sequence that the kernel uses in do_mmap().

---
 linux-user/mmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
linux-user/mmap.c +11 -3
index d0c50e4888..3ef69fa2d0 100644
@@ -391,14 +391,22 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK) {
+    if (!len) {
         errno = EINVAL;
         goto fail;
     }
 
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
-        goto the_end;
+    if (!len) {
+        errno = EINVAL;
+        goto fail;
+    }
+
+    if (offset & ~TARGET_PAGE_MASK) {
+        errno = EINVAL;
+        goto fail;
+    }
+
     real_start = start & qemu_host_page_mask;
     host_offset = offset & qemu_host_page_mask;
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [Qemu-devel] [PATCH v1 for 3.0 1/2] linux-user/mmap.c: handle len = 0 maps correctly

Le 26/07/2018 à 15:29, Alex Bennée a écrit :

Quoted 20 lines

I've slightly re-organised the check to more closely match the sequence that the kernel uses in do_mmap().

---
 linux-user/mmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
linux-user/mmap.c +1 -1
index d0c50e4888..3ef69fa2d0 100644
@@ -391,14 +391,22 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK) {
+    if (!len) {
         errno = EINVAL;

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 403

Quoted 10 lines
linux-user/mmap.c +4 -2
         goto fail;
     }
 
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
-        goto the_end;
+    if (!len) {
+        errno = EINVAL;
+        goto fail;
+    }

Why do you check twice len?
TARGET_PAGE_ALIGN() rounds up the value, so if it was not 0, it cannot be now.

Thanks,
Laurent

#3 lore raw

Re: [Qemu-devel] [PATCH v1 for 3.0 1/2] linux-user/mmap.c: handle len = 0 maps correctly

Quoted 24 lines · Laurent Vivier
Quoted 23 lines · Alex Bennée

I've slightly re-organised the check to more closely match the sequence that the kernel uses in do_mmap().

---
 linux-user/mmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
linux-user/mmap.c +1 -1
index d0c50e4888..3ef69fa2d0 100644
@@ -391,14 +391,22 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK) {
+    if (!len) {
         errno = EINVAL;
         goto fail;
     }
 
Quoted 11 lines

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 403

Quoted 7 lines
linux-user/mmap.c +4 -2
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
-        goto the_end;
+    if (!len) {
+        errno = EINVAL;
+        goto fail;
+    }

Why do you check twice len?
TARGET_PAGE_ALIGN() rounds up the value, so if it was not 0, it cannot be now.

I was trying to follow the kernel style but I realise TARGET_PAGE_ALIGN might be a different test compared to the kernel's PAGE_ALIGN(len) for overflows:

	if (!len)
		return -EINVAL;

	/*
	 * Does the application expect PROT_READ to imply PROT_EXEC?
	 *
	 * (the exception is when the underlying filesystem is noexec
	 *  mounted, in which case we dont add PROT_EXEC.)
	 */
	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
		if (!(file && path_noexec(&file->f_path)))
			prot |= PROT_EXEC;

	/* force arch specific MAP_FIXED handling in get_unmapped_area */
	if (flags & MAP_FIXED_NOREPLACE)
		flags |= MAP_FIXED;

	if (!(flags & MAP_FIXED))
		addr = round_hint_to_min(addr);

	/* Careful about overflows.. */
	len = PAGE_ALIGN(len);
	if (!len)
		return -ENOMEM;
Quoted 3 lines

Thanks,
Laurent

--
Alex Bennée

#4 lore raw

Re: [Qemu-devel] [PATCH v1 for 3.0 1/2] linux-user/mmap.c: handle len = 0 maps correctly

Le 26/07/2018 à 19:58, Alex Bennée a écrit :

Quoted 42 lines
Quoted 24 lines · Laurent Vivier
Quoted 23 lines · Alex Bennée

I've slightly re-organised the check to more closely match the sequence that the kernel uses in do_mmap().

---
 linux-user/mmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
linux-user/mmap.c +1 -1
index d0c50e4888..3ef69fa2d0 100644
@@ -391,14 +391,22 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK) {
+    if (!len) {
         errno = EINVAL;
         goto fail;
     }
 
Quoted 11 lines

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 403

Quoted 7 lines
linux-user/mmap.c +4 -2
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
-        goto the_end;
+    if (!len) {
+        errno = EINVAL;
+        goto fail;
+    }

Why do you check twice len?
TARGET_PAGE_ALIGN() rounds up the value, so if it was not 0, it cannot be now.

I was trying to follow the kernel style but I realise TARGET_PAGE_ALIGN might be a different test compared to the kernel's PAGE_ALIGN(len) for overflows:

...

Quoted 5 lines
	/* Careful about overflows.. */
	len = PAGE_ALIGN(len);
	if (!len)
		return -ENOMEM;

OK, so keep it but you should use ENOMEM, not EINVAL (and add a comment :) )

Thanks,
Laurent

#5 lore raw

Re: [Qemu-devel] [PATCH v1 for 3.0 1/2] linux-user/mmap.c: handle len = 0 maps correctly

Will do, thanks!

Quoted 58 lines · Laurent Vivier
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Laurent Vivier
Quoted 9 lines · Alex Bennée

I've slightly re-organised the check to more closely match the sequence that the kernel uses in do_mmap().

---
 linux-user/mmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
Quoted 5 lines
Quoted 5 lines

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong

Quoted 5 lines
linux-user/mmap.c +0 -0
index d0c50e4888..3ef69fa2d0 100644
@@ -391,14 +391,22 @@ abi_long target_mmap(abi_ulong start, abi_ulong

len, int prot,

Quoted 24 lines
Quoted 17 lines
Quoted 6 lines
linux-user/mmap.c +1 -1
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK) {
+    if (!len) {
         errno = EINVAL;

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 403

Quoted 10 lines
linux-user/mmap.c +4 -2
         goto fail;
     }
 
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
-        goto the_end;
+    if (!len) {
+        errno = EINVAL;
+        goto fail;
+    }
Quoted 3 lines

Why do you check twice len?
TARGET_PAGE_ALIGN() rounds up the value, so if it was not 0, it cannot be now.

I was trying to follow the kernel style but I realise TARGET_PAGE_ALIGN might be a different test compared to the kernel's PAGE_ALIGN(len) for overflows:

...

Quoted 5 lines
      /* Careful about overflows.. */
      len = PAGE_ALIGN(len);
      if (!len)
              return -ENOMEM;

OK, so keep it but you should use ENOMEM, not EINVAL (and add a comment :) )

Thanks,
Laurent

Signature · 2 lines
Alex Bennée
KVM/QEMU Hacker for Linaro
#6 lore raw

PATCH 2/2 tests: add check_invalid_maps to test-mmap

In reply to Alex Bennée

This adds a test to make sure we fail properly for a 0 length mmap. There are most likely other failure conditions we should also check.

---
 tests/tcg/multiarch/test-mmap.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tests/tcg/multiarch/test-mmap.c +15 -1
index 5c0afe6e49..7f62eba4e9 100644
@@ -27,7 +27,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
-
+#include <errno.h>
 #include <sys/mman.h>
 
 #define D(x)
@@ -435,6 +435,19 @@ void checked_write(int fd, const void *buf, size_t count)
     fail_unless(rc == count);
 }
 
+void check_invalid_mmaps(void)
+{
+    unsigned char *addr;
+
+    /* Attempt to map a zero length page.  */
+    addr = mmap(NULL, 0, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    fprintf(stdout, "%s addr=%p", __func__, (void *)addr);
+    fail_unless(addr == MAP_FAILED);
+    fail_unless(errno == EINVAL);
+
+    fprintf(stdout, " passed\n");
+}
+
 int main(int argc, char **argv)
 {
 	char tempname[] = "/tmp/.cmmapXXXXXX";
@@ -476,6 +489,7 @@ int main(int argc, char **argv)
 	check_file_fixed_mmaps();
 	check_file_fixed_eof_mmaps();
 	check_file_unfixed_eof_mmaps();
+	check_invalid_mmaps();
 
 	/* Fails at the moment.  */
 	/* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#7 lore raw

Re: [Qemu-devel] [PATCH v1 for 3.0 2/2] tests: add check_invalid_maps to test-mmap

Quoted 8 lines · Alex Bennée

This adds a test to make sure we fail properly for a 0 length mmap. There are most likely other failure conditions we should also check.

---
 tests/tcg/multiarch/test-mmap.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Might want to test length of e.g. -4 as well, which should get ENOMEM.

r~

#8 lore raw
In reply to Alex Bennée
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v1 for 3.0 0/2] fix for bug 1783362

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
e00d491aae tests: add check_invalid_maps to test-mmap 08f3c02268 linux-user/mmap.c: handle len = 0 maps correctly

=== OUTPUT BEGIN ===
Checking PATCH 1/2: linux-user/mmap.c: handle len = 0 maps correctly... Checking PATCH 2/2: tests: add check_invalid_maps to test-mmap...
ERROR: code indent should never use tabs
#53: FILE: tests/tcg/multiarch/test-mmap.c:492:
+^Icheck_invalid_mmaps();$

total: 1 errors, 0 warnings, 34 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#9 lore raw
In reply to Alex Bennée
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v1 for 3.0 0/2] fix for bug 1783362

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
024d2b3da3 tests: add check_invalid_maps to test-mmap a133b4a8b5 linux-user/mmap.c: handle len = 0 maps correctly

=== OUTPUT BEGIN ===
Checking PATCH 1/2: linux-user/mmap.c: handle len = 0 maps correctly... Checking PATCH 2/2: tests: add check_invalid_maps to test-mmap...
ERROR: code indent should never use tabs
#54: FILE: tests/tcg/multiarch/test-mmap.c:492:
+^Icheck_invalid_mmaps();$

total: 1 errors, 0 warnings, 34 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]