hexagon semihosting: Address coverity finding
v4 cover hexagon semihosting: Address coverity finding
These fixes are to mitigate the issue(s) reported by Coverity and Peter Maydell.
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
PATCH v4 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 | 90 ++++++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 76 deletions(-)
target/hexagon/cpu_helper.h +0 -2
@@ -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 +60 -30
200 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v4 1/4] target/hexagon: use get_user*() for semihosting arguments
Quoted 13 lines · Brian Cain
Link https://lore.kernel.org/qemu-devel/CAFEAcA9EL10xpPx_0YD-q6YcWWP9-iwcyG8x0s1yQhKB-XQGXQ@mail.gmail.com/ open hereSuggested-by Peter Maydell <[email protected]>Signed-off-by Brian Cain <[email protected]>--- target/hexagon/cpu_helper.h | 2 - target/hexagon/cpu_helper.c | 48 ++------------------ target/hexagon/hexswi.c | 90 ++++++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 76 deletions(-)
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 530
Quoted 7 lines
target/hexagon/hexswi.c +1
-0@@ -513,6 +524,7 @@ static void sim_handle_trap0(CPUHexagonState *env) ret = guestfd; } } +unlock_user (filename, physical_filename_addr,0 );semi_cb (cs, ret, err); }break ;
This piece should be in patch 2, I think?
Otherwise
thanks
-- PMM
PATCH v4 2/4 target/hexagon: fix semihosting OPEN filename handling
In reply to Brian CainHEX_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 | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-)
target/hexagon/hexswi.c +12 -17
@@ -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 ; }
Signature · 1 line
2.34.1
Re: [PATCH v4 2/4] target/hexagon: fix semihosting OPEN filename handling
Quoted 5 lines · Brian Cain
HEX_SYS_OPENcopied guest bytes into a fixed-size buffer until it found a NUL. A missing terminator could overrun that buffer. Uselock_user()with the ABI filename length and validate its terminating NUL before opening it.
Quoted 8 lines
Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")Link https://lore.kernel.org/all/CAFEAcA9MOs6VfHf2UHZ8z4cdvaB+7DY6hJb2emYZ2aLSe-t6iw@mail.gmail.com/ open hereSuggested-by Peter Maydell <[email protected]>Signed-off-by Brian Cain <[email protected]>--- target/hexagon/hexswi.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-)
With the unlock call from patch 1 moved into here
thanks
-- PMM
PATCH v4 3/4 target/hexagon: fix semihosting STAT filename handling
In reply to Brian CainHEX_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
@@ -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
PATCH v4 4/4 target/hexagon: fix semihosting ACCESS filename handling
In reply to Brian CainHEX_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.
Resolves: Coverity CID 1685942
--- target/hexagon/hexswi.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
target/hexagon/hexswi.c +10 -12
@@ -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