Skip to contentSkip to conversation

Fix writev syscall emulation

Qemu-devel Richard Purdie 2 participants 2 messages Feb 4, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

OpenEmbedded/Poky use qemu for locale generation when cross compiling. When we upgraded to qemu 0.9.1 it started giving locale generation errors on all 64 bit machines and some 32 bit ones.

I've traced it to the writev syscall failing. localedef passes several { NULL, 0 } iovec entries which trip up lock_iovec(). That function is returning an error for these but the return value isn't checked. The syscall is therefore always made but sometimes with a iovec that has only been half copied. If the total writes exceed size_t, EINVAL is returned by the kernel and glibc code emulates the call with a write which is most likely to happen on 32 bit so it sometimes works. size_t is unlikely to be exceeded on 64 bit so that returns an EFAULT and always corrupts/fails.

Anyhow, it seems 0 length iovec entries are allowed and we shouldn't care about the addresses in those cases. The patch below is one way to fix this. Ideally the return value of lock_iovec() needs be be checked too.

Regards,

Richard

---
 linux-user/syscall.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
Index: qemu-0.9.1/linux-user/syscall.c
===================================================================
@@ -1048,7 +1048,7 @@ static abi_long lock_iovec(int type, str
         base = tswapl(target_vec[i].iov_base);
         vec[i].iov_len = tswapl(target_vec[i].iov_len);
         vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
-	if (!vec[i].iov_base) 
+	if (!vec[i].iov_base && vec[i].iov_len) 
             goto fail;
     }
     unlock_user (target_vec, target_addr, 0);
#1 lore raw
Quoted 34 lines · Richard Purdie

Hi,

OpenEmbedded/Poky use qemu for locale generation when cross compiling. When we upgraded to qemu 0.9.1 it started giving locale generation errors on all 64 bit machines and some 32 bit ones.

I've traced it to the writev syscall failing. localedef passes several { NULL, 0 } iovec entries which trip up lock_iovec(). That function is returning an error for these but the return value isn't checked. The syscall is therefore always made but sometimes with a iovec that has only been half copied. If the total writes exceed size_t, EINVAL is returned by the kernel and glibc code emulates the call with a write which is most likely to happen on 32 bit so it sometimes works. size_t is unlikely to be exceeded on 64 bit so that returns an EFAULT and always corrupts/fails.

Anyhow, it seems 0 length iovec entries are allowed and we shouldn't care about the addresses in those cases. The patch below is one way to fix this. Ideally the return value of lock_iovec() needs be be checked too.

Regards,

Richard

---
 linux-user/syscall.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: qemu-0.9.1/linux-user/syscall.c
===================================================================

linux-user/syscall.c static abi_long lock_iovec(int type, str · line 1054

Quoted 8 lines
linux-user/syscall.c +1 -1
@@ -1048,7 +1048,7 @@ static abi_long lock_iovec(int type, str
         base = tswapl(target_vec[i].iov_base);
         vec[i].iov_len = tswapl(target_vec[i].iov_len);
         vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
-	if (!vec[i].iov_base) 
+	if (!vec[i].iov_base && vec[i].iov_len) 
             goto fail;
     }
     unlock_user (target_vec, target_addr, 0);

I have post similar patch some days ago.

Signature · 4 lines
Regards,  Kirill A. Shutemov
 + Belarus, Minsk
 + Velesys Ltd, http://www.velesys.com/
 + ALT Linux Team, http://www.altlinux.com/