Skip to contentSkip to conversation

target/arm/arm-semi: Don't let the guest close stdin/stdout/stderr

PATCH In review Peter Maydell 3 participants 3 messages Jan 24, 2020 – Jan 27, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm/arm-semi: Don't let the guest close stdin/stdout/stderr

The guest can use the semihosting API to open a handle corresponding to QEMU's own stdin, stdout, or stderr. When the guest closes this handle, we should not close the underlying host stdin/stdout/stderr the way we would do if the handle corresponded to a host fd we'd opened on behalf of the guest in SYS_OPEN.

---
 target/arm/arm-semi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/arm/arm-semi.c +9 -0
index 788fe61b51a..8718fd01948 100644
@@ -403,6 +403,15 @@ static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
 {
     CPUARMState *env = &cpu->env;
 
+    /*
+     * Only close the underlying host fd if it's one we opened on behalf
+     * of the guest in SYS_OPEN.
+     */
+    if (gf->hostfd == STDIN_FILENO ||
+        gf->hostfd == STDOUT_FILENO ||
+        gf->hostfd == STDERR_FILENO) {
+        return 0;
+    }
     return set_swi_errno(env, close(gf->hostfd));
 }
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Peter Maydell

The guest can use the semihosting API to open a handle corresponding to QEMU's own stdin, stdout, or stderr. When the guest closes this handle, we should not close the underlying host stdin/stdout/stderr the way we would do if the handle corresponded to a host fd we'd opened on behalf of the guest in SYS_OPEN.

Quoted 23 lines
---
 target/arm/arm-semi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/arm/arm-semi.c +9 -0
index 788fe61b51a..8718fd01948 100644
@@ -403,6 +403,15 @@ static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
 {
     CPUARMState *env = &cpu->env;
 
+    /*
+     * Only close the underlying host fd if it's one we opened on behalf
+     * of the guest in SYS_OPEN.
+     */
+    if (gf->hostfd == STDIN_FILENO ||
+        gf->hostfd == STDOUT_FILENO ||
+        gf->hostfd == STDERR_FILENO) {
+        return 0;
+    }
     return set_swi_errno(env, close(gf->hostfd));
 }
Signature · 1 line
Alex Bennée
#2 lore raw
Quoted 9 lines · Alex Bennée
Quoted 7 lines · Peter Maydell

The guest can use the semihosting API to open a handle corresponding to QEMU's own stdin, stdout, or stderr. When the guest closes this handle, we should not close the underlying host stdin/stdout/stderr the way we would do if the handle corresponded to a host fd we'd opened on behalf of the guest in SYS_OPEN.

Quoted 3 lines
Quoted 1 line
Quoted 23 lines
Quoted 23 lines
---
  target/arm/arm-semi.c | 9 +++++++++
  1 file changed, 9 insertions(+)
target/arm/arm-semi.c +9 -0
index 788fe61b51a..8718fd01948 100644
@@ -403,6 +403,15 @@ static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
  {
      CPUARMState *env = &cpu->env;
 
+    /*
+     * Only close the underlying host fd if it's one we opened on behalf
+     * of the guest in SYS_OPEN.
+     */
+    if (gf->hostfd == STDIN_FILENO ||
+        gf->hostfd == STDOUT_FILENO ||
+        gf->hostfd == STDERR_FILENO) {
+        return 0;
+    }
      return set_swi_errno(env, close(gf->hostfd));
  }