linux-user: fix QEMU_STRACE=1 segfault
PATCH v2 1/1 linux-user: fix QEMU_STRACE=1 segfault
While debugging some issues with QEMU_STRACE I stumbled over segmentation faults that were pretty reproducible. Turns out we tried to treat a normal return value as errno, resulting in an access over array boundaries for the resolution.
Fix this by allowing failure to resolve invalid errnos into strings.
---
v1 -> v2:
- propagate fault further down, so we display the negative value
--- linux-user/strace.c | 9 +++++++-- linux-user/syscall.c | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-)
linux-user/strace.c +7 -2
@@ -1515,14 +1515,19 @@ void print_syscall_ret(int num, abi_long ret) { int i; +char *errstr =NULL ;for (i=0 ;i<nsyscalls;i++)if ( scnames[i].nr == num ) {if ( scnames[i].result !=NULL ) { scnames[i].result (&scnames[i],ret); }else { -if ( ret <0 ) { -gemu_log (" = -1 errno=" TARGET_ABI_FMT_ld" (%s)\n" , -ret,target_strerror (-ret)); +if (ret <0 ) { + errstr =target_strerror (-ret); + } +if (errstr) { +gemu_log (" = -1 errno=" TARGET_ABI_FMT_ld" (%s)\n" , + -ret, errstr); }else {gemu_log (" = " TARGET_ABI_FMT_ld"\n" , ret); }
linux-user/syscall.c +3 -0
@@ -731,6 +731,9 @@ static inline int is_error(abi_long ret)char *target_strerror (int err) { +if ((err >=ERRNO_TABLE_SIZE ) && (err >=0 )) { +return NULL ; + }return strerror (target_to_host_errno (err)); }
Signature · 1 line
1.6.0.2
Quoted 13 lines · Alexander Graf
linux-user/strace.c +1
-0@@ -1515,14 +1515,19 @@ void print_syscall_ret(int num, abi_long ret) { int i; + char *errstr = NULL; for(i=0;i<nsyscalls;i++) if( scnames[i].nr == num ) { if( scnames[i].result != NULL ) {
linux-user/strace.c void · line 1530
Quoted 10 lines
linux-user/strace.c +6
-2scnames[i].result(&scnames[i],ret); } else { - if( ret < 0 ) { - gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, target_strerror(-ret)); + if (ret < 0) { + errstr = target_strerror(-ret); + } + if (errstr) { + gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", + -ret, errstr);
Should this really be printing -1 all the time when ret isn't -1 ?
linux-user/strace.c void · line 1533
Quoted 3 lines
linux-user/strace.c +0
-0} else { gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret); }
print_syscall_ret_addr() also needs to handle NULL returns from target_strerror().
linux-user/syscall.c static inline int is_error(abi_long ret) · line 737
Quoted 12 lines
linux-user/syscall.c +3
-0@@ -731,6 +731,9 @@ static inline int is_error(abi_long ret) char *target_strerror(int err) { + if ((err >= ERRNO_TABLE_SIZE) && (err >= 0)) { + return NULL; + } return strerror(target_to_host_errno(err));
shouldn't that be ((err >= ERRNO_TABLE_SIZE) || (err < 0)) ?
-- PMM
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Alexander Graf
linux-user/strace.c +1
-0@@ -1515,14 +1515,19 @@ void print_syscall_ret(int num, abi_long ret) {int i; +char *errstr =NULL ;
Quoted 15 lines
linux-user/strace.c void · line 1530
Quoted 13 lines
linux-user/strace.c +6
-2for (i=0 ;i<nsyscalls;i++)if ( scnames[i].nr == num ) {if ( scnames[i].result !=NULL ) { scnames[i].result (&scnames[i],ret); }else { -if ( ret <0 ) { -gemu_log (" = -1 errno=" TARGET_ABI_FMT_ld" (%s)\n" , -ret,target_strerror (-ret)); +if (ret <0 ) { + errstr =target_strerror (-ret); + } +if (errstr) { +gemu_log (" = -1 errno=" TARGET_ABI_FMT_ld" (%s)\n" , + -ret, errstr);Should this really be printing -1 all the time when ret isn't -1 ?
In linux-user, the syscall emulation functions return -errno which later gets translated to -1 with errno=-errno. That's why the code looks so weird. So yes, it's correct.
Quoted 7 lines
linux-user/strace.c void · line 1533
Quoted 4 lines
linux-user/strace.c +0
-0}else {gemu_log (" = " TARGET_ABI_FMT_ld"\n" , ret); }
print_syscall_ret_addr()also needs to handle NULL returns fromtarget_strerror().
I don't think they can ever happen, since we're sure to use valid errnos there. But I can add a check.
Quoted 7 lines
Quoted 7 lines
linux-user/syscall.c +0
-0@@ -731,6 +731,9 @@ static inline int is_error(abi_long ret)
Quoted 8 lines
linux-user/syscall.c static inline int is_error(abi_long ret) · line 737
Quoted 6 lines
linux-user/syscall.c +3
-0char *target_strerror (int err) { +if ((err >=ERRNO_TABLE_SIZE ) && (err >=0 )) { +return NULL ; + }return strerror (target_to_host_errno (err));shouldn't that be ((err >=
ERRNO_TABLE_SIZE) || (err < 0)) ?
Yes.
Alex
Quoted 6 lines · Alexander Graf
Quoted 1 line · Peter Maydell
Should this really be printing -1 all the time when ret isn't -1 ?
In linux-user, the syscall emulation functions return -errno which later gets translated to -1 with errno=-errno. That's why the code looks so weird. So yes, it's correct.
Yes, but the syscall ABI (which is what we're implementing) returns -errno for errors; the errno variable is a purely userspace construct. I'd expected that the strace layer would be tracing exactly what the syscall ABI was.
However it looks like standard userspace strace(1) also does this translation to -1 errno=thing, so it makes sense for us to do that too.
-- PMM