EFAULT implmentation in linux-user
PATCH 1/1 EFAULT implmentation in linux-user
The attached patch implements EFAULT detection in linux-user. It is suprising how much software "relies" on the kernel returning EFAULT instead of just crashing.
This patch can be broken down into 3 parts:
exec.c:
Add page_check_range(), which uses the page map of the target to determine wether an address and range are valid.EFAULT is returned when an invalid address is detected.
linux-user/qemu.h:
Add lock_and_check_user_struct(), wich is similar to lock_user_struct(), but adds a call to page_check_range() to detect bad addresses. Once all occurrances of lock_user_struct() have been converted over to this new function, the new function could be renamed back to lock_user_struct() to shorten it slightly.
linux-user/syscall.s:
This is where lock_and_check_user_struct() is used. It is used to perform the mapping from target to host addresses. Generally, calls to lock_user_struct() have been replaced with calls to lock_and_check_user_struct(). For example
- lock_user_struct(target_tv, target_addr, 0);
+ if( (ret=lock_and_check_user_struct(&target_tv,target_addr,
sizeof(*target_tv),0,PAGE_WRITE)) != 0 ) return -ret;
These changes permit many of the tests in LTP for error conditions to now pass.
Stuart
Stuart R. Anderson [email protected] Network & Software Engineering http://www.netsweng.com/ 1024D/37A79149: 0791 D3B8 9A4C 2CDC A31F BD03 0A62 E534 37A7 9149
exec.c +23 -0
@@ -1846,6 +1846,29 @@spin_unlock (&tb_lock); } +int page_check_range (target_ulong start,target_ulong len,int flags) +{ +PageDesc *p; +target_ulong end; +target_ulong addr; + + end =TARGET_PAGE_ALIGN (start+len);/* must do before we loose bits in the next step */ + start = start &TARGET_PAGE_MASK ; + +if ( end < start )return EFAULT ;/* we've wrapped around */ +for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { + p =page_find (addr >>TARGET_PAGE_BITS ); +if ( !p )return EFAULT ; +if ( !(p->flags &PAGE_VALID ) )return EFAULT ; + +if (!(p->flags &PAGE_READ ) && + (flags &PAGE_READ ) )return EFAULT ; +if (!(p->flags &PAGE_WRITE ) && + (flags &PAGE_WRITE ) )return EFAULT ; + } +return 0 ; +} +/* called from signal handler: invalidate the code and unprotect the page. Return TRUE if the fault was succesfully handled. */ int page_unprotect (target_ulong address,unsigned long pc,void *puc)
cpu-all.h +1 -0
@@ -691,6 +691,7 @@int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);void page_unprotect_range (target_ulong data,target_ulong data_size); +int page_check_range (target_ulong start,target_ulong len,int flags);#define SINGLE_CPU_DEFINES #ifdef SINGLE_CPU_DEFINES
linux-user/syscall.c +216 -139
926 lines not on this page · raw message
linux-user/qemu.h +9 -0
@@ -295,6 +295,15 @@#define unlock_user_struct (host_ptr, guest_addr, copy) \ unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) +static inline long lock_and_check_user_struct (void *host_addr,target_ulong guest_addr,long len,int copy,int pg_access) +{ +long *haddr, ret =0 ; + haddr = (long *)host_addr; +if ( (ret=page_check_range (guest_addr,len,pg_access)) !=0 )return ret; + *haddr = (long )lock_user (guest_addr, len, copy); +return 0 ; +} +#define tget8 (addr) ldub(addr)#define tput8 (addr, val) stb(addr, val)#define tget16 (addr) lduw(addr)