hexagon semihosting: Address coverity finding
cover hexagon semihosting: Address coverity finding
These fixes are to mitigate the issue(s) reported by Coverity and Peter Maydell.
Brian Cain (3):
target/hexagon: Fix semihosting OPEN filename handling target/hexagon: Fix semihosting STAT filename handling target/hexagon: Fix semihosting ACCESS filename handling
target/hexagon/hexswi.c | 62 +++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 36 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 target/hexagon: Fix semihosting OPEN filename handling
HEX_SYS_OPEN copied guest bytes into a fixed-size buffer until it found a NUL. A missing terminator could overrun that buffer, so use lock_user_string() instead.
--- target/hexagon/hexswi.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
target/hexagon/hexswi.c +9 -18
@@ -28,6 +28,7 @@#include "semihosting/console.h" #include "semihosting/syscalls.h" #include "semihosting/guestfd.h" +#include "semihosting/uaccess.h" #include "system/runstate.h" /* non-arm-compatible semihosting calls */
@@ -461,31 +462,14 @@ static void sim_handle_trap0(CPUHexagonState *env)case HEX_SYS_OPEN : { -char filename[BUFSIZ ]; +char *filename;target_ulong physical_filename_addr;unsigned int filemode; -int length;int real_openmode;int ret, err =0 ; -int i =0 ;hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr);hexagon_read_memory (env, swi_info +4 ,4 , &filemode, retaddr); -hexagon_read_memory (env, swi_info +8 ,4 , &length, retaddr); - -if (length >=BUFSIZ ) { -qemu_log_mask (LOG_GUEST_ERROR , -"%s: filename too large (%d)\n" , - __func__, length); -semi_cb (cs,-1 ,ENAMETOOLONG ); -break ; - } - -do { -hexagon_read_memory (env, physical_filename_addr + i,1 , - &filename[i], retaddr); - i++; - }while (filename[i -1 ]);/* convert ARM ANGEL filemode into host filemode */ if (filemode <ARRAY_SIZE (angel_to_host_filemode_table)) {
@@ -498,6 +482,12 @@ static void sim_handle_trap0(CPUHexagonState *env)break ; } + filename =lock_user_string (physical_filename_addr); +if (!filename) { +semi_cb (cs,-1 ,EFAULT ); +break ; + } +if (strcmp (filename,":tt" ) ==0 &&qemu_semihosting_console_has_chardev ()) { ret =alloc_guestfd ();
@@ -513,6 +503,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
Re: [PATCH 1/3] target/hexagon: Fix semihosting OPEN filename handling
Quoted 3 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, so uselock_user_string()instead.
Do you have a link to the specification for these hexagon semihosting calls? Looking at this patch I'm wondering if maybe I steered you a bit wrong with suggesting lock_user_string():
Quoted 6 lines
target/hexagon/hexswi.c +1
-1@@ -461,31 +462,14 @@ static void sim_handle_trap0(CPUHexagonState *env)case HEX_SYS_OPEN : { -char filename[BUFSIZ ]; +char *filename;
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 474
Quoted 10 lines
target/hexagon/hexswi.c +0
-3target_ulong physical_filename_addr;unsigned int filemode; -int length;int real_openmode;int ret, err =0 ; -int i =0 ;hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr);hexagon_read_memory (env, swi_info +4 ,4 , &filemode, retaddr); -hexagon_read_memory (env, swi_info +8 ,4 , &length, retaddr);
We no longer do anything with the length argument, which seems like it might not be right.
If we know the (max) length of the string, then perhaps lock_user() (which takes a length argument) would fit better.
thanks
-- PMM
Re: [PATCH 1/3] target/hexagon: Fix semihosting OPEN filename handling
Quoted 6 lines · Peter Maydell
Quoted 2 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, so uselock_user_string()instead.Do you have a link to the specification for these hexagon semihosting calls? Looking at this patch I'm wondering if maybe I steered you a bit wrong with suggesting
lock_user_string():
Here's the link - https://docs.qualcomm.com/doc/80-N2040-101/topic/semihosting-specification.html
Quoted 13 lines
Quoted 13 lines
target/hexagon/hexswi.c +1
-3@@ -461,31 +462,14 @@ static void sim_handle_trap0(CPUHexagonState *env)case HEX_SYS_OPEN : { -char filename[BUFSIZ ]; +char *filename;target_ulong physical_filename_addr;unsigned int filemode; -int length;int real_openmode;int ret, err =0 ; -int i =0 ;
Quoted 8 lines
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 474
Quoted 3 lines
target/hexagon/hexswi.c +0
-1hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr);hexagon_read_memory (env, swi_info +4 ,4 , &filemode, retaddr); -hexagon_read_memory (env, swi_info +8 ,4 , &length, retaddr);We no longer do anything with the length argument, which seems like it might not be right.
If we know the (max) length of the string, then perhaps
lock_user()(which takes a length argument) would fit better.
Ok, I'll re-evaluate it with lock_user().
Quoted 2 lines
thanks
-- PMM
Re: [PATCH 1/3] target/hexagon: Fix semihosting OPEN filename handling
Quoted 6 lines · Brian Cain
Quoted 3 lines · Peter Maydell
Quoted 2 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, so uselock_user_string()instead.
Quoted 7 lines
Quoted 3 lines
Do you have a link to the specification for these hexagon semihosting calls? Looking at this patch I'm wondering if maybe I steered you a bit wrong with suggesting
lock_user_string():Here's the link -
https://docs.qualcomm.com/doc/80-N2040-101/topic/semihosting-specification.html
Thanks. It looks like it's rather inconsistent about how to pass filename strings, so for instance:
* SYS_OPEN requires that the string is NUL terminated and also that the caller passes its length
* SYS_REMOVE requires string + length but doesn't say it has to be NUL terminated
* SYS_ACCESS requires a NUL terminated string and the caller does not pass the length
Where the ABI gives us a length I guess we might as well use it plus lock_user() (that's more efficient as we don't need to do the "scan to find the NUL byte" part first).
Patches 2 and 3 in this series are probably OK as those calls don't give us a length. I'll review those later, hopefully.
-- PMM
PATCH 2/3 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 safely map the guest string instead.
--- target/hexagon/hexswi.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
target/hexagon/hexswi.c +7 -7
@@ -544,20 +544,20 @@ 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;hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr);if (what_swi ==HEX_SYS_STAT ) { -int i =0 ; -do { -hexagon_read_memory (env, physical_filename_addr + i,1 , - &filename[i], retaddr); - 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
Re: [PATCH 2/3] target/hexagon: Fix semihosting STAT filename handling
Quoted 4 lines · Brian Cain
HEX_SYS_STATcould pass an unterminated local filename buffer tostat()when the guest string filled it. Uselock_user_string()to safely map the guest string instead.
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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
thanks
-- PMM
PATCH 3/3 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 | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
target/hexagon/hexswi.c +10 -11
@@ -603,25 +603,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;hexagon_read_memory (env, swi_info,4 , &FileNameAddr, retaddr); -do { -hexagon_read_memory (env, FileNameAddr + i,1 , &filename[i], - retaddr); - i++; - }while ((i <BUFSIZ ) && (filename[i -1 ])); - filename[i] =0 ; + filename =lock_user_string (FileNameAddr); +if (!filename) { +semi_cb (cs,-1 ,EFAULT ); +break ; + }hexagon_read_memory (env, swi_info +4 ,4 , &BufferMode, retaddr); 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
Re: [PATCH 3/3] target/hexagon: Fix semihosting ACCESS filename handling
Quoted 4 lines · Brian Cain
HEX_SYS_ACCESSwrote a NUL byte past its fixed-size filename buffer when the guest string filled it. Uselock_user_string()to avoid the out-of-bounds write.
Quoted 8 lines
Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")Resolves: Coverity CID 1685942
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 | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
thanks
-- PMM