Skip to contentSkip to conversation

linux-user: Handle short reads in mmap_h_gt_g

PATCH Superseded Richard Henderson 3 participants 4 messages Aug 15, 2024 – Aug 16, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Handle short reads in mmap_h_gt_g

In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping.

Fixes eb5027ac618 ("linux-user: Split out mmap_h_gt_g")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504

---
 linux-user/mmap.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
linux-user/mmap.c +15 -4
index 6418e811f6..de9ab13754 100644
@@ -853,10 +853,21 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
     }
 
     if (misaligned_offset) {
-        /* TODO: The read could be short. */
-        if (pread(fd, p, host_len, offset + real_start - start) != host_len) {
-            do_munmap(p, host_len);
-            return -1;
+        size_t o = 0;
+        while (1) {
+            ssize_t r = pread(fd, p + o, host_len - o,
+                              o + offset + real_start - start);
+            if (likely(r == host_len - o) || r == 0) {
+                /* Complete or EOF */
+                break;
+            }
+            if (unlikely(r == -1)) {
+                /* Error */
+                do_munmap(p, host_len);
+                return -1;
+            }
+            /* Short read -- iterate */
+            o += r;
         }
         if (!(host_prot & PROT_WRITE)) {
             mprotect(p, host_len, host_prot);
Signature · 1 line
2.43.0
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · Richard Henderson

In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping.

Fixes eb5027ac618 ("linux-user: Split out mmap_h_gt_g")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504

---
  linux-user/mmap.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)

Thanks.
Song Gao

Quoted 30 lines
linux-user/mmap.c +15 -4
index 6418e811f6..de9ab13754 100644
@@ -853,10 +853,21 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
      }
 
      if (misaligned_offset) {
-        /* TODO: The read could be short. */
-        if (pread(fd, p, host_len, offset + real_start - start) != host_len) {
-            do_munmap(p, host_len);
-            return -1;
+        size_t o = 0;
+        while (1) {
+            ssize_t r = pread(fd, p + o, host_len - o,
+                              o + offset + real_start - start);
+            if (likely(r == host_len - o) || r == 0) {
+                /* Complete or EOF */
+                break;
+            }
+            if (unlikely(r == -1)) {
+                /* Error */
+                do_munmap(p, host_len);
+                return -1;
+            }
+            /* Short read -- iterate */
+            o += r;
          }
          if (!(host_prot & PROT_WRITE)) {
              mprotect(p, host_len, host_prot);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Richard Henderson
Quoted 16 lines · Richard Henderson

In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping.

Fixes eb5027ac618 ("linux-user: Split out mmap_h_gt_g")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504

---
  linux-user/mmap.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
linux-user/mmap.c +0 -0
index 6418e811f6..de9ab13754 100644

linux-user/mmap.c static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, · line 856

Quoted 4 lines
linux-user/mmap.c +0 -1
@@ -853,10 +853,21 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
      }
 
      if (misaligned_offset) {
-        /* TODO: The read could be short. */

I note there are other short reads in {linux,bsd}-user/mmap.c.

Quoted 21 lines
linux-user/mmap.c +15 -3
-        if (pread(fd, p, host_len, offset + real_start - start) != host_len) {
-            do_munmap(p, host_len);
-            return -1;
+        size_t o = 0;
+        while (1) {
+            ssize_t r = pread(fd, p + o, host_len - o,
+                              o + offset + real_start - start);
+            if (likely(r == host_len - o) || r == 0) {
+                /* Complete or EOF */
+                break;
+            }
+            if (unlikely(r == -1)) {
+                /* Error */
+                do_munmap(p, host_len);
+                return -1;
+            }
+            /* Short read -- iterate */
+            o += r;
          }
          if (!(host_prot & PROT_WRITE)) {
              mprotect(p, host_len, host_prot);
#3 lore raw
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Richard Henderson

In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping.

Fixes eb5027ac618 ("linux-user: Split out mmap_h_gt_g")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504

---
  linux-user/mmap.c | 19 +++++++++++++++----

  1 file changed, 15 insertions(+), 4 deletions(-)
Quoted 10 lines

linux-user/mmap.c static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, · line 855

Quoted 8 lines
linux-user/mmap.c +0 -1
index 6418e811f6..de9ab13754 100644
@@ -853,10 +853,21 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
       }
       if (misaligned_offset) {
-        /* TODO: The read could be short. */

I note there are other short reads in {linux,bsd}-user/mmap.c.

Ah, via mmap_frag. Hmm, yes. Worth fixing at the same time.

r~