Skip to contentSkip to conversation

linux-user: Fix mincore() with PROT_NONE

PATCH Applied Thomas Weißschuh 3 participants 8 messages Apr 22, 2023 – May 1, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user: Fix mincore() with PROT_NONE

The kernel does not require PROT_READ for addresses passed to mincore.

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2:
* Introduce symbolic flag VERIFY_NONE instead of hardcoding "0"

Thomas Weißschuh (2):
linux-user: Add new flag VERIFY_NONE
linux-user: Don't require PROT_READ for mincore

 linux-user/qemu.h    | 1 +
 linux-user/syscall.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

base-commit: 1cc6e1a20144c0ae360cbeb0e035fdee1bd80609

Signature · 1 line
2.40.0
#1 lore raw

PATCH v2 1/2 linux-user: Add new flag VERIFY_NONE

This can be used to validate that an address range is mapped but without being readable or writable.

It will be used by an updated implementation of mincore().

---
 linux-user/qemu.h | 1 +
 1 file changed, 1 insertion(+)
linux-user/qemu.h +1 -0
index e2e93fbd1d5d..92f9f5af41c7 100644
@@ -168,6 +168,7 @@ abi_long do_brk(abi_ulong new_brk);
 
 /* user access */
 
+#define VERIFY_NONE  0
 #define VERIFY_READ  PAGE_READ
 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
 
Signature · 1 line
2.40.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] linux-user: Add new flag VERIFY_NONE

Le 22/04/2023 à 12:03, Thomas Weißschuh a écrit :

Quoted 15 lines

This can be used to validate that an address range is mapped but without being readable or writable.

It will be used by an updated implementation of mincore().

---
  linux-user/qemu.h | 1 +
  1 file changed, 1 insertion(+)
linux-user/qemu.h +0 -0
index e2e93fbd1d5d..92f9f5af41c7 100644

linux-user/qemu.h abi_long do_brk(abi_ulong new_brk); · line 174

Quoted 7 lines
linux-user/qemu.h +1 -0
@@ -168,6 +168,7 @@ abi_long do_brk(abi_ulong new_brk);
 
  /* user access */
 
+#define VERIFY_NONE  0
  #define VERIFY_READ  PAGE_READ
  #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
 
#3 lore raw

Re: [PATCH v2 1/2] linux-user: Add new flag VERIFY_NONE

In reply to Thomas Weißschuh

Le 22/04/2023 à 12:03, Thomas Weißschuh a écrit :

Quoted 15 lines

This can be used to validate that an address range is mapped but without being readable or writable.

It will be used by an updated implementation of mincore().

---
  linux-user/qemu.h | 1 +
  1 file changed, 1 insertion(+)
linux-user/qemu.h +0 -0
index e2e93fbd1d5d..92f9f5af41c7 100644

linux-user/qemu.h abi_long do_brk(abi_ulong new_brk); · line 174

Quoted 7 lines
linux-user/qemu.h +1 -0
@@ -168,6 +168,7 @@ abi_long do_brk(abi_ulong new_brk);
 
  /* user access */
 
+#define VERIFY_NONE  0
  #define VERIFY_READ  PAGE_READ
  #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
 

Applied to my linux-user-for-8.1 branch.

Thanks,
Laurent

#4 lore raw

PATCH v2 2/2 linux-user: Don't require PROT_READ for mincore

In reply to Thomas Weißschuh

The kernel does not require PROT_READ for addresses passed to mincore. For example the fincore(1) tool from util-linux uses PROT_NONE and currently does not work under qemu-user.

Example (with fincore(1) from util-linux 2.38):

$ fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
$ qemu-x86_64 /usr/bin/fincore /proc/self/exe
fincore: failed to do mincore: /proc/self/exe: Cannot allocate memory

With this patch:

$ ./build/qemu-x86_64 /usr/bin/fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index 69f740ff98c8..5ec848b459f7 100644
@@ -11897,7 +11897,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
 #ifdef TARGET_NR_mincore
     case TARGET_NR_mincore:
         {
-            void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
+            void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
             if (!a) {
                 return -TARGET_ENOMEM;
             }
Signature · 1 line
2.40.0
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] linux-user: Don't require PROT_READ for mincore

Le 22/04/2023 à 12:03, Thomas Weißschuh a écrit :

Quoted 29 lines

The kernel does not require PROT_READ for addresses passed to mincore. For example the fincore(1) tool from util-linux uses PROT_NONE and currently does not work under qemu-user.

Example (with fincore(1) from util-linux 2.38):

$ fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
$ qemu-x86_64 /usr/bin/fincore /proc/self/exe
fincore: failed to do mincore: /proc/self/exe: Cannot allocate memory

With this patch:

$ ./build/qemu-x86_64 /usr/bin/fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
---
  linux-user/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 69f740ff98c8..5ec848b459f7 100644

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 11903

Quoted 8 lines
linux-user/syscall.c +1 -1
@@ -11897,7 +11897,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  #ifdef TARGET_NR_mincore
      case TARGET_NR_mincore:
          {
-            void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
+            void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
              if (!a) {
                  return -TARGET_ENOMEM;
              }
#6 lore raw

Re: [PATCH v2 2/2] linux-user: Don't require PROT_READ for mincore

In reply to Thomas Weißschuh

Le 22/04/2023 à 12:03, Thomas Weißschuh a écrit :

Quoted 29 lines

The kernel does not require PROT_READ for addresses passed to mincore. For example the fincore(1) tool from util-linux uses PROT_NONE and currently does not work under qemu-user.

Example (with fincore(1) from util-linux 2.38):

$ fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
$ qemu-x86_64 /usr/bin/fincore /proc/self/exe
fincore: failed to do mincore: /proc/self/exe: Cannot allocate memory

With this patch:

$ ./build/qemu-x86_64 /usr/bin/fincore /proc/self/exe
RES PAGES  SIZE FILE
24K     6 22.1K /proc/self/exe
---
  linux-user/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 69f740ff98c8..5ec848b459f7 100644

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 11903

Quoted 8 lines
linux-user/syscall.c +1 -1
@@ -11897,7 +11897,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  #ifdef TARGET_NR_mincore
      case TARGET_NR_mincore:
          {
-            void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
+            void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
              if (!a) {
                  return -TARGET_ENOMEM;
              }

Applied to my linux-user-for-8.1 branch.

Thanks,
Laurent

Reviewed-by on the v2 series
#7 lore raw
In reply to Thomas Weißschuh
Quoted 6 lines · Thomas Weißschuh

The kernel does not require PROT_READ for addresses passed to mincore.

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2:
* Introduce symbolic flag VERIFY_NONE instead of hardcoding "0"

Quoted 7 lines

Thomas Weißschuh (2):
linux-user: Add new flag VERIFY_NONE
linux-user: Don't require PROT_READ for mincore

  linux-user/qemu.h    | 1 +
  linux-user/syscall.c | 2 +-
  2 files changed, 2 insertions(+), 1 deletion(-)