Skip to contentSkip to conversation

linux-user: Emulate /proc/self/maps under mmap_lock

PATCH In review Ilya Leoshkevich 3 participants 3 messages Oct 14, 2024 – Oct 22, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Emulate /proc/self/maps under mmap_lock

If one thread modifies the mappings and another thread prints them, a situation may occur that the printer thread sees a guest mapping without a corresponding host mapping, leading to a crash in open_self_maps_2().

Fixes 7b7a3366e142 ("linux-user: Use walk_memory_regions for open_self_maps")
---
 linux-user/syscall.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
linux-user/syscall.c +3 -1
index 1354e756941..dd2ec0712b8 100644
@@ -8151,17 +8151,19 @@ static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
 {
     struct open_self_maps_data d = {
         .ts = get_task_state(env_cpu(env)),
-        .host_maps = read_self_maps(),
         .fd = fd,
         .smaps = smaps
     };
 
+    mmap_lock();
+    d.host_maps = read_self_maps();
     if (d.host_maps) {
         walk_memory_regions(&d, open_self_maps_2);
         free_self_maps(d.host_maps);
     } else {
         walk_memory_regions(&d, open_self_maps_3);
     }
+    mmap_unlock();
     return 0;
 }
 
Signature · 1 line
2.47.0
Reviewed-by on v1 1/1
#1 lore raw

Le 14/10/2024 à 22:34, Ilya Leoshkevich a écrit :

Quoted 27 lines

If one thread modifies the mappings and another thread prints them, a situation may occur that the printer thread sees a guest mapping without a corresponding host mapping, leading to a crash in open_self_maps_2().

Fixes 7b7a3366e142 ("linux-user: Use walk_memory_regions for open_self_maps")
---
  linux-user/syscall.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
linux-user/syscall.c +2 -1
index 1354e756941..dd2ec0712b8 100644
@@ -8151,17 +8151,19 @@ static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
  {
      struct open_self_maps_data d = {
          .ts = get_task_state(env_cpu(env)),
-        .host_maps = read_self_maps(),
          .fd = fd,
          .smaps = smaps
      };
 
+    mmap_lock();
+    d.host_maps = read_self_maps();

linux-user/syscall.c static int open_self_maps_1(CPUArchState *env, int fd, bool smaps) · line 8169

Quoted 10 lines
linux-user/syscall.c +1 -0
      if (d.host_maps) {
          walk_memory_regions(&d, open_self_maps_2);
          free_self_maps(d.host_maps);
      } else {
          walk_memory_regions(&d, open_self_maps_3);
      }
+    mmap_unlock();
      return 0;
  }
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to Ilya Leoshkevich
Quoted 11 lines · Ilya Leoshkevich

If one thread modifies the mappings and another thread prints them, a situation may occur that the printer thread sees a guest mapping without a corresponding host mapping, leading to a crash in open_self_maps_2().

Fixes 7b7a3366e142 ("linux-user: Use walk_memory_regions for open_self_maps")
---
  linux-user/syscall.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Thanks, queued.

r~