linux-user EFAULT implementation
PATCH 1/1 linux-user EFAULT implementation
Here is a refresh of the linux-user EFAULT patch vs todays cvs. This patch has now received a lot more runtime, and a couple of bug have been fixed that affected some of the socket APIs.
The purpose of this patch is to detect bad pointers and return EFAULT instead of just crashing when the bad pointers are used by qemu.
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
@@ -1862,6 +1862,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);CPUState *cpu_copy (CPUState *env);
linux-user/syscall.c +224 -141
943 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)
Please update page_check_range() (and other related functions) to return -EFAULT instead of EFAULT in case of error.
Moreover, I believe using similar functions as Linux for memory access (copyfromuser, copytouser, get_user, put_user) would be cleaner.
Regards,
Fabrice.
Quoted 10 lines · Stuart Anderson
Here is a refresh of the linux-user EFAULT patch vs todays cvs. This patch has now received a lot more runtime, and a couple of bug have been fixed that affected some of the socket APIs.
The purpose of this patch is to detect bad pointers and return EFAULT instead of just crashing when the bad pointers are used by qemu.
Stuart
Quoted 2 lines · Fabrice Bellard
Please update
page_check_range()(and other related functions) to return-EFAULTinstead of EFAULT in case of error.
Will do.
Quoted 1 line
Moreover, I believe using similar functions as Linux for memory access (copyfromuser, copytouser,
get_user,put_user) would be cleaner.
This makes sense. That code that I sent was based on the idea that I was just extending the model that was already there, instead of giving it a complete overhaul. I'll look into how much work the overhaul would be.
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
Quoted 5 lines · Stuart Anderson
Quoted 1 line
Moreover, I believe using similar functions as Linux for memory access (copyfromuser, copytouser,
get_user,put_user) would be cleaner.This makes sense. That code that I sent was based on the idea that I was just extending the model that was already there, instead of giving it a complete overhaul. I'll look into how much work the overhaul would be.
After some digging, the one "design-level" issue I have found is that the current lock_user_struct() and the new lock_and_check_user_struct(), interfaces are based on the assumption that we need to map addresses between guest and host, so they provide a place to call g2h() and return the new address. This is different from copy_{to|from}_user(), which assumes all addresses are valid as is, but that the system just needs to ensure they are correctly mapped in for the data copy.
Contrary to this, cpu-all.h has the following comment & code:
#define GUEST_BASE 0
/* All direct uses of g2h and h2g need to go away for usermode softmmu. */ #define g2h(x) ((void *)((unsigned long)(x) + GUEST_BASE)) #define h2g(x) ((target_ulong)(x - GUEST_BASE))
It appears that g2h() and h2g() are mostly no-ops, with the typecasting being the only possible useful part remaining.
So, the question is:
Can I simplify this code to assume that guest and host addresses coexist and use the copy_*_user() or just the access_ok() interfaces?
This would be one step closer to eliminating the use of g2h() and h2g() in the usermode code as is suggested by the comment in cpu-all.h.
One other comment to make, is that in most cases, a simple copy is not sufficient. Unlike the real Linux kernel, in many places, we are doing structure mapping instead of a simple buffer copy.
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
Quoted 5 lines · Stuart Anderson
So, the question is:
Can I simplify this code to assume that guest and host addresses coexist and use the copy_*_user() or just the
access_ok()interfaces?
Attached is a diff that shows what this will look like for the struct sockaddr handling case. In short, the two functions for mapping this structure get renamed
target_to_host_sockaddr -> copy_from_user_sockaddr host_to_target_sockaddr -> copy_to_user_sockaddr
and lose the pg_access parameter as read/write access is determined by wether we are copying to or from "user space". The macro access_ok() is used to check the validity of the memory being accessed.
This change also completely drops the notion of locking and unlocking memory, which was the previous model being used.
Fabrice,
Is this change what you had in mind? Does it cause a problem for anyone else?
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
Quoted 1 line · Stuart Anderson
Attached is a diff ...
Here's the diff.
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
*** linux-user/syscall.c 2007-07-09 07:33:46.000000000 -0400
--- linux-user/syscall.c.stu 2007-07-09 07:01:12.000000000 -0400
***************
*** 522,550 ****
return ret;
}
! static inline long copy_from_user_sockaddr(struct sockaddr *addr,
target_ulong target_addr,
! socklen_t len)
{
long ret = 0;! struct target_sockaddr *target_saddr = (struct target_sockaddr *)target_addr;
! if( (ret=access_ok(VERIFY_READ,target_saddr,len)) != 0 ) return ret;
memcpy(addr, target_saddr, len);
addr->sa_family = tswap16(target_saddr->sa_family);
return ret;}
! static inline long copy_to_user_sockaddr(target_ulong target_addr,
struct sockaddr *addr,
! socklen_t len)
{
long ret = 0;! struct target_sockaddr *target_saddr = (struct target_sockaddr *)target_addr;
! if( (ret=access_ok(VERIFY_WRITE,target_saddr,len)) != 0 ) return ret;
memcpy(target_saddr, addr, len);
target_saddr->sa_family = tswap16(addr->sa_family);
return ret;}
--- 522,554 ----
return ret;
}
! static inline long target_to_host_sockaddr(struct sockaddr *addr,
target_ulong target_addr,
! socklen_t len,
! int pg_access)
{
long ret = 0;! struct target_sockaddr *target_saddr;
! if( (ret=lock_and_check_user_struct(&target_saddr,target_addr,len,1,pg_access)) != 0 ) return ret;
memcpy(addr, target_saddr, len);
addr->sa_family = tswap16(target_saddr->sa_family);
+ unlock_user(target_saddr, target_addr, 0);
return ret;}
! static inline long host_to_target_sockaddr(target_ulong target_addr,
struct sockaddr *addr,
! socklen_t len,
! int pg_access)
{
long ret = 0;! struct target_sockaddr *target_saddr;
! if( (ret=lock_and_check_user_struct(&target_saddr,target_addr,len,1,pg_access)) != 0 ) return ret;
memcpy(target_saddr, addr, len);
target_saddr->sa_family = tswap16(addr->sa_family);
+ unlock_user(target_saddr, target_addr, len);
return ret;}
***************
*** 909,915 ****
long ret = 0;
void *addr = alloca(addrlen);! if( (ret=copy_from_user_sockaddr(addr, target_addr, addrlen)) != 0 ) return ret;
return get_errno(bind(sockfd, addr, addrlen));}
--- 913,919 ----
long ret = 0;
void *addr = alloca(addrlen);! if( (ret=target_to_host_sockaddr(addr, target_addr, addrlen,PAGE_READ)) != 0 ) return ret;
return get_errno(bind(sockfd, addr, addrlen));}
***************
*** 919,925 ****
long ret = 0;
void *addr = alloca(addrlen);! if( (ret=copy_from_user_sockaddr(addr, target_addr, addrlen)) != 0 ) return ret;
return get_errno(connect(sockfd, addr, addrlen));}
--- 923,929 ----
long ret = 0;
void *addr = alloca(addrlen);! if( (ret=target_to_host_sockaddr(addr, target_addr, addrlen,PAGE_READ)) != 0 ) return ret;
return get_errno(connect(sockfd, addr, addrlen));}
***************
*** 944,956 ****
msg.msg_namelen = tswap32(msgp->msg_namelen);
msg.msg_name = alloca(msg.msg_namelen);
if( send ) {
! if( (ret=copy_from_user_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
! msg.msg_namelen)) != 0 ) return ret;
} else {
! /* FIXME
! if( (ret=copy_from_user_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
msg.msg_namelen,PAGE_WRITE)) != 0 ) return ret;
- */
}
} else {
msg.msg_name = NULL;
--- 948,958 ----
msg.msg_namelen = tswap32(msgp->msg_namelen);
msg.msg_name = alloca(msg.msg_namelen);
if( send ) {
! if( (ret=target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
! msg.msg_namelen,PAGE_READ)) != 0 ) return ret;
} else {
! if( (ret=target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
msg.msg_namelen,PAGE_WRITE)) != 0 ) return ret;
}
} else {
msg.msg_name = NULL;***************
*** 991,997 ****
ret = get_errno(accept(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=copy_to_user_sockaddr(target_addr, addr, addrlen)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}
return ret;--- 993,999 ----
ret = get_errno(accept(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=host_to_target_sockaddr(target_addr, addr, addrlen, PAGE_WRITE)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}
return ret;***************
*** 1010,1016 ****
ret = get_errno(getpeername(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=copy_to_user_sockaddr(target_addr, addr, addrlen)) != 0 ) return ret2;
if( (ret2=page_check_range(target_addrlen,sizeof(socklen_t),PAGE_WRITE)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}--- 1012,1018 ----
ret = get_errno(getpeername(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=host_to_target_sockaddr(target_addr, addr, addrlen, PAGE_WRITE)) != 0 ) return ret2;
if( (ret2=page_check_range(target_addrlen,sizeof(socklen_t),PAGE_WRITE)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}***************
*** 1030,1036 ****
ret = get_errno(getsockname(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=copy_to_user_sockaddr(target_addr, addr, addrlen)) ) return ret2;
tput32(target_addrlen, addrlen);
}
return ret;--- 1032,1038 ----
ret = get_errno(getsockname(fd, addr, &addrlen));
if (!is_error(ret)) {
! if( (ret2=host_to_target_sockaddr(target_addr, addr, addrlen, PAGE_WRITE)) ) return ret2;
tput32(target_addrlen, addrlen);
}
return ret;
***************
*** 1060,1066 ****
if( (ret=lock_and_check_user_struct(&host_msg,msg,len,1,PAGE_READ)) != 0 ) return ret;
if (target_addr) {
addr = alloca(addrlen);
! if( (ret=copy_from_user_sockaddr(addr, target_addr, addrlen)) != 0 ) return ret;
ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
} else {
ret = get_errno(send(fd, host_msg, len, flags));
--- 1062,1068 ----
if( (ret=lock_and_check_user_struct(&host_msg,msg,len,1,PAGE_READ)) != 0 ) return ret;
if (target_addr) {
addr = alloca(addrlen);
! if( (ret=target_to_host_sockaddr(addr, target_addr, addrlen, PAGE_READ)) != 0 ) return ret;
ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
} else {
ret = get_errno(send(fd, host_msg, len, flags));
***************
*** 1088,1094 ****
}
if (!is_error(ret)) {
if (target_addr) {
! if( (ret2=copy_to_user_sockaddr(target_addr, addr, addrlen)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}
unlock_user(host_msg, msg, len);
--- 1090,1096 ----
}
if (!is_error(ret)) {
if (target_addr) {
! if( (ret2=host_to_target_sockaddr(target_addr, addr, addrlen, PAGE_WRITE)) != 0 ) return ret2;
tput32(target_addrlen, addrlen);
}
unlock_user(host_msg, msg, len);Quoted 7 lines · Stuart Anderson
Quoted 5 lines · Stuart Anderson
So, the question is:
Can I simplify this code to assume that guest and host addresses coexist and use the copy_*_user() or just the
access_ok()interfaces?
No. Ideally you should use the same conventions as the Linux kernel and assume that you cannot access the user data directly. To summarise:
- access_ok() is needed before __copy_*_user and __[get|put]_user.
- access_ok() is not needed before copy_[to|from]_user and [get|put]_user.
- [get|put]_user do automatic endianness change.
- Use tswapx and the other related functions to change the endianness before or after copy_*_user.
- for specific cases requiring speed (e.g. buffers for read/write syscalls), specific tricks to avoid memory copies can be used with a lock/unlock style API.
Quoted 1 line
Attached is a diff [...]
Please send a unified diff.
For the time being, I would suggest to minimize the number of changes and just extend lock_user()/unlock_user() as you began to do to handle -EFAULT. The rest is mostly a question of cosmetics.
Regards,
Fabrice.
Quoted 1 line · Fabrice Bellard
No. Ideally you should use the same conventions as the Linux kernel and assume that you cannot access the user data directly.
That's what I had already started doing today.
Quoted 1 line
For the time being, I would suggest to minimize the number of changes and just extend lock_user()/unlock_user() as you began to do to handle
-EFAULT. The rest is mostly a question of cosmetics.
The attached patch is my in-progress work of the complete overhaul to use the kernel conventions. It needs some more work to finish the conversion, but enough should be done to see how it is going to turn out. Overall, I think the converted code is easier to read, especially if you are familiar with reading kernel code. I also think it will end up being more correct both becasue of the additional time now spent on reviewing each syscall, as well as the kernel conventions tend to make you be more thorough and explicite.
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
@@ -1862,6 +1862,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);CPUState *cpu_copy (CPUState *env);
linux-user/syscall.c +258 -217
1,067 lines not on this page · raw message
linux-user/qemu.h +20 -3
@@ -171,7 +171,8 @@#define VERIFY_READ 0#define VERIFY_WRITE 1 -#define access_ok (type,addr,size) (1) +#define access_ok (type,addr,size) \ + (page_check_range((target_ulong)addr,size,(type==VERIFY_READ)?PAGE_READ:PAGE_WRITE)==0)/* NOTE get_user and put_user use host addresses. */ #define __put_user (x,ptr)\
@@ -218,13 +219,14 @@0 ;\ }) +/* The abort() is just for debugging. Take it out after a while */ #define put_user (x,ptr)\ ({\ int __ret;\ if (access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))\ __ret = __put_user(x, ptr);\ else\ - __ret = -EFAULT;\ + {__ret = -EFAULT;abort();}\ __ret;\ })
@@ -234,10 +236,16 @@if (access_ok (VERIFY_READ , ptr,sizeof (*ptr)))\ __ret =__get_user (x, ptr);\else \ - __ret = -EFAULT ;\ + {__ret = -EFAULT ;abort ();}\ __ret;\ }) +#define copy_from_user (hpptr, gptr, len) \ +({ \ + *(hpptr) = g2h(gptr); \ + access_ok(VERIFY_READ, gptr, len);\ +}) +/* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as neccessary. The lock_user gets a pointer to a contiguous area of guest memory, but does not perform
@@ -295,6 +303,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)
Quoted 24 lines · Stuart Anderson
Quoted 2 lines · Fabrice Bellard
No. Ideally you should use the same conventions as the Linux kernel and assume that you cannot access the user data directly.
That's what I had already started doing today.
Quoted 3 lines
For the time being, I would suggest to minimize the number of changes and just extend lock_user()/unlock_user() as you began to do to handle
-EFAULT. The rest is mostly a question of cosmetics.The attached patch is my in-progress work of the complete overhaul to use the kernel conventions. It needs some more work to finish the conversion, but enough should be done to see how it is going to turn out. Overall, I think the converted code is easier to read, especially if you are familiar with reading kernel code. I also think it will end up being more correct both becasue of the additional time now spent on reviewing each syscall, as well as the kernel conventions tend to make you be more thorough and explicite.
{ struct target_rusage *target_rusage;(quoted diff) +1
-1- lock_user_struct(target_rusage, target_addr, 0); + if( !access_ok(VERIFY_READ,target_addr,sizeof(*target_rusage)) )
Quoted 2 lines
return -1;
+target_rusage= (structtarget_rusage*)g2h(target_addr);
Using g2h directly is bad. g2h is an implementation detail of one particular memory model.
The whole point of the lock_user abstraction (or a similar copy_from_user abstraction) is that almost none of the code cares how "user" memory is accessed. One of the long-term goals of this abstraction is to allow the softmmu code to be used with userspace emulation. In this case a region may be split across multiple discontiguous host pages.
The reason I used a locking paradigm rather than a copying one is that it allows a zero-copy implementation in the common case. I've no strong objections to a copying interface, however it must be implementation agnostic.
Paul
Paul Brook wrote:
(...]
Quoted 3 lines
Using g2h directly is bad. g2h is an implementation detail of one particular memory model.
The whole point of the
lock_userabstraction (or a similarcopy_from_userabstraction) is that almost none of the code cares how "user" memory is accessed. One of the long-term goals of this abstraction is to allow the softmmu code to be used with userspace emulation. In this case a region may be split across multiple discontiguous host pages.
I confirm that I expected g2h() to be completely removed in case the Linux user access API is used.
Quoted 1 line
The reason I used a locking paradigm rather than a copying one is that it allows a zero-copy implementation in the common case. I've no strong objections to a copying interface, however it must be implementation agnostic.
Since you agree for the copying interface, I suggest to convert all the code to it. The exact implementation of access_ok(), copy_to_user(), copy_from_user()... will be easily modifiable if one day softmmu user mode is needed.
In the only specific case of potentially big unbounded memory areas (such as read/write buffers), a different API must be used where it is possible to get a pointer to user pages (e.g. ptr = lock_user_page(addr, rw_mode); if (!ptr) return -EFAULT; unlock_user_page(ptr)), so that no copy is performed while being able to handle any user page remapping.
Regards,
Fabrice.
Quoted 1 line · Fabrice Bellard
I confirm that I expected
g2h()to be completely removed in case the Linux user access API is used.
I agree too that it should not be used. I'm testing ARM on x86_64, and used it in a few places to clean up some warnings. The real fix is to change what I was doing to not need this asignment anyway. I will take another look at how to bury this part in the copy* interfaces.
Would it be OK to use g2h() inside the implementation of copy* ?
Quoted 1 line
Since you agree for the copying interface, I suggest to convert all the code to it.
OK, I'll continue in this direction, after taking a fresh look at what I did yesterday.
Thanks for the feedback.
Stuart
Stuart R. Anderson [email protected] Solutions Architect http://www.c2.net/ 1024D/37A79149: 0791 D3B8 9A4C 2CDC A31F BD03 0A62 E534 37A7 9149
Quoted 2 lines · Stuart Anderson
OK, I'll continue in this direction, after taking a fresh look at what I did yesterday.
Attached is a snapshot of the work after a couple more evenings on it. I'm still doing a lot of testing, and I haven't finished converting everything, but there should be enough in this patch to be able to tell if this is heading in a good direction or not. There are even a couple of systems calls actually yield better test suite results as a result of the additional scrutiny of this process.
Further comments would be appreciated.
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
@@ -1862,6 +1862,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 -1 ;/* we've wrapped around */ +for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { + p =page_find (addr >>TARGET_PAGE_BITS ); +if ( !p )return -1 ; +if ( !(p->flags &PAGE_VALID ) )return -1 ; + +if (!(p->flags &PAGE_READ ) && + (flags &PAGE_READ ) )return -1 ; +if (!(p->flags &PAGE_WRITE ) && + (flags &PAGE_WRITE ) )return -1 ; + } +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);CPUState *cpu_copy (CPUState *env);
linux-user/syscall.c +312 -285
1,252 lines not on this page · raw message
linux-user/qemu.h +32 -1
@@ -171,7 +171,8 @@#define VERIFY_READ 0#define VERIFY_WRITE 1 -#define access_ok (type,addr,size) (1) +#define access_ok (type,addr,size) \ + (page_check_range((target_ulong)addr,size,(type==VERIFY_READ)?PAGE_READ:PAGE_WRITE)==0)/* NOTE get_user and put_user use host addresses. */ #define __put_user (x,ptr)\
@@ -238,6 +239,26 @@ __ret;\ }) +#define copy_from_user (hptr, gptr, len) \ +({ \ + int ret=0;\ + if( access_ok(VERIFY_READ, gptr, (len) ) )\ + memcpy(hptr,gptr,(len)); \ + else \ + ret=1;\ + ret; \ +}) + +#define copy_to_user (gptr, hptr, len) \ +({ \ + int ret=0;\ + if( access_ok(VERIFY_WRITE, gptr, (len)) )\ + memcpy(gptr,hptr,(len)); \ + else \ + ret=1;\ + ret; \ +}) +/* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as neccessary. The lock_user gets a pointer to a contiguous area of guest memory, but does not perform
@@ -295,6 +316,16 @@#define unlock_user_struct (host_ptr, guest_addr, copy) \ unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) +/* This is being phased ot in favor of the copy*user() interfaces */ +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)
PATCH 1/1 linux-user EFAULT implementation
Quoted 4 lines · Stuart Anderson
Further comments would be appreciated.
Having heard no further comments, I have continued on in the process of coverting things to use the copy_{to|from}_user() interfaces. The attached patch does well with the LTP test suite, and is currently happily building an armel Debian glibc package on a x86_64 host.
There are still a few uses of the lock*() interfaces remaining, but considering the size of this patch already, I'd like to suggest that if this patch is acceptable, to commit this patch, and the rest will be sent in as smaller, easier to digest patches, probably one or two syscalls at a time.
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
@@ -1862,6 +1862,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 -1 ;/* we've wrapped around */ +for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { + p =page_find (addr >>TARGET_PAGE_BITS ); +if ( !p )return -1 ; +if ( !(p->flags &PAGE_VALID ) )return -1 ; + +if (!(p->flags &PAGE_READ ) && + (flags &PAGE_READ ) )return -1 ; +if (!(p->flags &PAGE_WRITE ) && + (flags &PAGE_WRITE ) )return -1 ; + } +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);CPUState *cpu_copy (CPUState *env);
linux-user/syscall.c +327 -321
1,360 lines not on this page · raw message
linux-user/qemu.h +32 -1
@@ -171,7 +171,8 @@#define VERIFY_READ 0#define VERIFY_WRITE 1 -#define access_ok (type,addr,size) (1) +#define access_ok (type,addr,size) \ + (page_check_range((target_ulong)addr,size,(type==VERIFY_READ)?PAGE_READ:PAGE_WRITE)==0)/* NOTE get_user and put_user use host addresses. */ #define __put_user (x,ptr)\
@@ -238,6 +239,26 @@ __ret;\ }) +#define copy_from_user (hptr, gptr, len) \ +({ \ + int __cfu_ret=0;\ + if( access_ok(VERIFY_READ, gptr, (len) ) )\ + memcpy(hptr,gptr,(len)); \ + else \ + __cfu_ret=1;\ + __cfu_ret; \ +}) + +#define copy_to_user (gptr, hptr, len) \ +({ \ + int __ctu_ret=0;\ + if( access_ok(VERIFY_WRITE, gptr, (len)) )\ + memcpy(gptr,hptr,(len)); \ + else \ + __ctu_ret=1;\ + __ctu_ret; \ +}) +/* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as neccessary. The lock_user gets a pointer to a contiguous area of guest memory, but does not perform
@@ -295,6 +316,16 @@#define unlock_user_struct (host_ptr, guest_addr, copy) \ unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) +/* This is being phased out in favor of the copy*user() interfaces */ +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)