Skip to contentSkip to conversation

hexagon semihosting: Address coverity finding

PATCH New Brian Cain 1 participant 5 messages Oct 20

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v5 cover hexagon semihosting: Address coverity finding

These fixes are to mitigate the issue(s) reported by Coverity and Peter Maydell.

v5 - Fully reviewed + queued for PR.

In v5: moved unlock_user() to "fix semihosting OPEN filename handling".

In v4:
- Dropped "add directory semihosting operations": needs a better design approach, perhaps w/some architecture-independent state. Not required to mitigate Coverity issues.
- Pulled the hexagon_read*()->get_user() out of "fix semihosting OPEN filename handling" into its own patch.

In v3:
- The first patch now uses get_user_*() for semihosting argument accesses, instead of hexagon_read_memory(), now removed.
- For HEX_SYS_OPEN, filename/len validation errors are handled together.

In v2:
- HEX_SYS_OPEN switched to lock_user() from lock_user_string()
- pulled in {open,read,close}dir from the a prev patch, changes since that v2:
* Replaces POSIX DIR, opendir(), readdir(), and closedir() with portable GLib GDir APIs. {un,}lock_user
* Replaces the fixed BUFSIZ path buffer and byte-by-byte guest reads with lock_user_string()/unlock_user().
* Adds SemihostingDir, which stores the GDir plus enumeration state
* Synthesizes . and .., omitted by glib
* Writes inode zero instead of host d_ino, which GLib does not expose portably.
* Limits returned names to 254 characters plus NUL, rather than depending on host struct dirent::d_name size. Prev opendir v2: https://lore.kernel.org/qemu-devel/[email protected]/

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v1: https://lore.kernel.org/qemu-devel/[email protected]/

Brian Cain (4):
target/hexagon: use get_user*() for semihosting arguments target/hexagon: fix semihosting OPEN filename handling target/hexagon: fix semihosting STAT filename handling target/hexagon: fix semihosting ACCESS filename handling

 target/hexagon/cpu_helper.h |   2 -
 target/hexagon/cpu_helper.c |  48 ++-----------
 target/hexagon/hexswi.c     | 134 +++++++++++++++++++++---------------
 3 files changed, 82 insertions(+), 102 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v5 1/4 target/hexagon: use get_user*() for semihosting arguments

---
 target/hexagon/cpu_helper.h |  2 -
 target/hexagon/cpu_helper.c | 48 ++------------------
 target/hexagon/hexswi.c     | 89 ++++++++++++++++++++++++-------------
 3 files changed, 63 insertions(+), 76 deletions(-)
target/hexagon/cpu_helper.h +0 -2
index 8b1fb98a51a..b261b9177bc 100644
@@ -7,8 +7,6 @@
 #ifndef HEXAGON_CPU_HELPER_H
 #define HEXAGON_CPU_HELPER_H
 
-void hexagon_read_memory(CPUHexagonState *env, target_ulong vaddr, int size,
-                         void *retptr, uintptr_t retaddr);
 void hexagon_write_memory(CPUHexagonState *env, target_ulong vaddr,
                           int size, uint64_t data, uintptr_t retaddr);
 void hexagon_peek_memory_range(CPUHexagonState *env, uint32_t start_addr,
target/hexagon/cpu_helper.c +4 -44

63 lines not on this page · raw message

target/hexagon/hexswi.c +59 -30

193 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v5 2/4 target/hexagon: fix semihosting OPEN filename handling

In reply to Brian Cain

HEX_SYS_OPEN copied guest bytes into a fixed-size buffer until it found a NUL. A missing terminator could overrun that buffer. Use lock_user() with the ABI filename length and validate its terminating NUL before opening it.

---
 target/hexagon/hexswi.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
target/hexagon/hexswi.c +13 -17
index a28f83515fb..cc48c9ba9f1 100644
@@ -469,35 +469,29 @@ static void sim_handle_trap0(CPUHexagonState *env)
 
     case HEX_SYS_OPEN:
     {
-        char filename[BUFSIZ];
+        char *filename;
         target_ulong physical_filename_addr;
         unsigned int filemode;
-        uint32_t length;
+        uint32_t filename_len;
+        size_t filename_size;
         int real_openmode;
         int ret, err = 0;
-        int i = 0;
 
         if (get_user_u32(physical_filename_addr, swi_info) ||
             get_user_u32(filemode, swi_info + 4) ||
-            get_user_u32(length, swi_info + 8)) {
+            get_user_u32(filename_len, swi_info + 8)) {
             goto do_fault;
         }
 
-        if (length >= BUFSIZ) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: filename too large (%" PRIu32 ")\n",
-                          __func__, length);
-            semi_cb(cs, -1, ENAMETOOLONG);
-            break;
+        /* The ABI length excludes the filename's terminating NUL. */
+        filename_size = (size_t)filename_len + 1;
+        filename = lock_user(VERIFY_READ, physical_filename_addr,
+                             filename_size, true);
+        if (!filename || filename[filename_len] != '\0') {
+            unlock_user(filename, physical_filename_addr, 0);
+            goto do_fault;
         }
 
-        do {
-            if (get_user_u8(filename[i], physical_filename_addr + i)) {
-                goto do_fault;
-            }
-            i++;
-        } while (filename[i - 1]);
-
         /* convert ARM ANGEL filemode into host filemode */
         if (filemode < ARRAY_SIZE(angel_to_host_filemode_table)) {
             real_openmode = angel_to_host_filemode_table[filemode];
@@ -505,6 +499,7 @@ static void sim_handle_trap0(CPUHexagonState *env)
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: invalid OPEN mode: %u\n",
                           __func__, filemode);
+            unlock_user(filename, physical_filename_addr, 0);
             semi_cb(cs, -1, EINVAL);
             break;
         }
@@ -524,6 +519,7 @@ static void sim_handle_trap0(CPUHexagonState *env)
                 ret = guestfd;
             }
         }
+        unlock_user(filename, physical_filename_addr, 0);
         semi_cb(cs, ret, err);
     }
     break;
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v5 3/4 target/hexagon: fix semihosting STAT filename handling

In reply to Brian Cain

HEX_SYS_STAT could pass an unterminated local filename buffer to stat() when the guest string filled it. Use lock_user_string() to map the guest string safely.

---
 target/hexagon/hexswi.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
target/hexagon/hexswi.c +7 -8
index cc48c9ba9f1..a5c8ee27c7e 100644
@@ -564,7 +564,7 @@ static void sim_handle_trap0(CPUHexagonState *env)
         struct stat st_buf;
         uint8_t *st_bufptr = (uint8_t *)&sys_stat;
         int rc, err = 0;
-        char filename[BUFSIZ];
+        char *filename;
         target_ulong physical_filename_addr;
         target_ulong statBufferAddr;
         if (get_user_u32(physical_filename_addr, swi_info) ||
@@ -573,15 +573,14 @@ static void sim_handle_trap0(CPUHexagonState *env)
         }
 
         if (what_swi == HEX_SYS_STAT) {
-            int i = 0;
-            do {
-                if (get_user_u8(filename[i], physical_filename_addr + i)) {
-                    goto do_fault;
-                }
-                i++;
-            } while ((i < BUFSIZ) && filename[i - 1]);
+            filename = lock_user_string(physical_filename_addr);
+            if (!filename) {
+                semi_cb(cs, -1, EFAULT);
+                break;
+            }
             rc = stat(filename, &st_buf);
             err = errno;
+            unlock_user(filename, physical_filename_addr, 0);
         } else {
             int fd = physical_filename_addr;
             GuestFD *gf = get_guestfd(fd);
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v5 4/4 target/hexagon: fix semihosting ACCESS filename handling

In reply to Brian Cain

HEX_SYS_ACCESS wrote a NUL byte past its fixed-size filename buffer when the guest string filled it. Use lock_user_string() to avoid the out-of-bounds write.

Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")

Resolves: Coverity CID 1685942

---
 target/hexagon/hexswi.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
target/hexagon/hexswi.c +10 -12
index a5c8ee27c7e..9ff72253385 100644
@@ -626,26 +626,24 @@ static void sim_handle_trap0(CPUHexagonState *env)
 
     case HEX_SYS_ACCESS:
     {
-        char filename[BUFSIZ];
+        char *filename;
         uint32_t FileNameAddr;
         uint32_t BufferMode;
-        int rc;
-
-        int i = 0;
+        int rc, err;
 
         if (get_user_u32(FileNameAddr, swi_info) ||
             get_user_u32(BufferMode, swi_info + 4)) {
             goto do_fault;
         }
-        do {
-            if (get_user_u8(filename[i], FileNameAddr + i)) {
-                goto do_fault;
-            }
-            i++;
-        } while ((i < BUFSIZ) && (filename[i - 1]));
-        filename[i] = 0;
+        filename = lock_user_string(FileNameAddr);
+        if (!filename) {
+            semi_cb(cs, -1, EFAULT);
+            break;
+        }
         rc = access(filename, BufferMode);
-        semi_cb(cs, rc,  rc == 0 ? 0 : errno);
+        err = errno;
+        unlock_user(filename, FileNameAddr, 0);
+        semi_cb(cs, rc,  rc == 0 ? 0 : err);
     }
     break;
 
Signature · 1 line
2.34.1