[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 18/31] linux-user: Fix types in uaccess.c
From: |
Richard Henderson |
Subject: |
[PATCH v6 18/31] linux-user: Fix types in uaccess.c |
Date: |
Tue, 9 Feb 2021 16:02:10 -0800 |
For copy_*_user, only 0 and -TARGET_EFAULT are returned; no need
to involve abi_long. Use size_t for lengths. Use bool for the
lock_user copy argument. Use ssize_t for target_strlen, because
we can't overflow the host memory space.
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
linux-user/qemu.h | 14 ++++++--------
linux-user/uaccess.c | 45 ++++++++++++++++++++++----------------------
2 files changed, 29 insertions(+), 30 deletions(-)
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 36b58bd840..d25a5dafc0 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -7,8 +7,6 @@
#include "exec/cpu_ldst.h"
#undef DEBUG_REMAP
-#ifdef DEBUG_REMAP
-#endif /* DEBUG_REMAP */
#include "exec/user/abitypes.h"
@@ -629,8 +627,8 @@ static inline bool access_ok(CPUState *cpu, int type,
* buffers between the target and host. These internally perform
* locking/unlocking of the memory.
*/
-abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
-abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
+int copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
+int copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
/* Functions for accessing guest memory. The tget and tput functions
read/write single values, byteswapping as necessary. The lock_user function
@@ -640,13 +638,13 @@ abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t
len);
/* Lock an area of guest memory into the host. If copy is true then the
host area will have the same contents as the guest. */
-void *lock_user(int type, abi_ulong guest_addr, long len, int copy);
+void *lock_user(int type, abi_ulong guest_addr, size_t len, bool copy);
/* Unlock an area of guest memory. The first LEN bytes must be
flushed back to guest memory. host_ptr = NULL is explicitly
allowed and does nothing. */
-#ifdef DEBUG_REMAP
-static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, long len)
+#ifndef DEBUG_REMAP
+static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, size_t
len)
{ }
#else
void unlock_user(void *host_ptr, abi_ulong guest_addr, long len);
@@ -654,7 +652,7 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long
len);
/* Return the length of a string in target memory or -TARGET_EFAULT if
access error. */
-abi_long target_strlen(abi_ulong gaddr);
+ssize_t target_strlen(abi_ulong gaddr);
/* Like lock_user but for null terminated strings. */
void *lock_user_string(abi_ulong guest_addr);
diff --git a/linux-user/uaccess.c b/linux-user/uaccess.c
index bba012ed15..76af6a92b1 100644
--- a/linux-user/uaccess.c
+++ b/linux-user/uaccess.c
@@ -4,7 +4,7 @@
#include "qemu.h"
-void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
+void *lock_user(int type, abi_ulong guest_addr, size_t len, bool copy)
{
if (!access_ok_untagged(type, guest_addr, len)) {
return NULL;
@@ -26,7 +26,7 @@ void *lock_user(int type, abi_ulong guest_addr, long len, int
copy)
}
#ifdef DEBUG_REMAP
-void unlock_user(void *host_ptr, abi_ulong guest_addr, long len);
+void unlock_user(void *host_ptr, abi_ulong guest_addr, size_t len);
{
if (!host_ptr) {
return;
@@ -34,7 +34,7 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long
len);
if (host_ptr == g2h_untagged(guest_addr)) {
return;
}
- if (len > 0) {
+ if (len != 0) {
memcpy(g2h_untagged(guest_addr), host_ptr, len);
}
g_free(host_ptr);
@@ -43,53 +43,53 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long
len);
void *lock_user_string(abi_ulong guest_addr)
{
- abi_long len = target_strlen(guest_addr);
+ ssize_t len = target_strlen(guest_addr);
if (len < 0) {
return NULL;
}
- return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
+ return lock_user(VERIFY_READ, guest_addr, (size_t)len + 1, 1);
}
/* copy_from_user() and copy_to_user() are usually used to copy data
* buffers between the target and host. These internally perform
* locking/unlocking of the memory.
*/
-abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len)
+int copy_from_user(void *hptr, abi_ulong gaddr, size_t len)
{
- abi_long ret = 0;
- void *ghptr;
+ int ret = 0;
+ void *ghptr = lock_user(VERIFY_READ, gaddr, len, 1);
- if ((ghptr = lock_user(VERIFY_READ, gaddr, len, 1))) {
+ if (ghptr) {
memcpy(hptr, ghptr, len);
unlock_user(ghptr, gaddr, 0);
- } else
+ } else {
ret = -TARGET_EFAULT;
-
+ }
return ret;
}
-
-abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len)
+int copy_to_user(abi_ulong gaddr, void *hptr, size_t len)
{
- abi_long ret = 0;
- void *ghptr;
+ int ret = 0;
+ void *ghptr = lock_user(VERIFY_WRITE, gaddr, len, 0);
- if ((ghptr = lock_user(VERIFY_WRITE, gaddr, len, 0))) {
+ if (ghptr) {
memcpy(ghptr, hptr, len);
unlock_user(ghptr, gaddr, len);
- } else
+ } else {
ret = -TARGET_EFAULT;
+ }
return ret;
}
/* Return the length of a string in target memory or -TARGET_EFAULT if
access error */
-abi_long target_strlen(abi_ulong guest_addr1)
+ssize_t target_strlen(abi_ulong guest_addr1)
{
uint8_t *ptr;
abi_ulong guest_addr;
- int max_len, len;
+ size_t max_len, len;
guest_addr = guest_addr1;
for(;;) {
@@ -101,11 +101,12 @@ abi_long target_strlen(abi_ulong guest_addr1)
unlock_user(ptr, guest_addr, 0);
guest_addr += len;
/* we don't allow wrapping or integer overflow */
- if (guest_addr == 0 ||
- (guest_addr - guest_addr1) > 0x7fffffff)
+ if (guest_addr == 0 || (guest_addr - guest_addr1) > 0x7fffffff) {
return -TARGET_EFAULT;
- if (len != max_len)
+ }
+ if (len != max_len) {
break;
+ }
}
return guest_addr - guest_addr1;
}
--
2.25.1
- [PATCH v6 07/31] linux-user: Tidy VERIFY_READ/VERIFY_WRITE, (continued)
- [PATCH v6 07/31] linux-user: Tidy VERIFY_READ/VERIFY_WRITE, Richard Henderson, 2021/02/09
- [PATCH v6 06/31] linux-user: Check for overflow in access_ok, Richard Henderson, 2021/02/09
- [PATCH v6 08/31] bsd-user: Tidy VERIFY_READ/VERIFY_WRITE, Richard Henderson, 2021/02/09
- [PATCH v6 13/31] linux-user: Explicitly untag memory management syscalls, Richard Henderson, 2021/02/09
- [PATCH v6 14/31] linux-user: Use guest_range_valid in access_ok, Richard Henderson, 2021/02/09
- [PATCH v6 09/31] linux-user: Do not use guest_addr_valid for h2g_valid, Richard Henderson, 2021/02/09
- [PATCH v6 12/31] exec: Use cpu_untagged_addr in g2h; split out g2h_untagged, Richard Henderson, 2021/02/09
- [PATCH v6 10/31] linux-user: Fix guest_addr_valid vs reserved_va, Richard Henderson, 2021/02/09
- [PATCH v6 11/31] exec: Introduce cpu_untagged_addr, Richard Henderson, 2021/02/09
- [PATCH v6 16/31] linux-user: Use cpu_untagged_addr in access_ok; split out *_untagged, Richard Henderson, 2021/02/09
- [PATCH v6 18/31] linux-user: Fix types in uaccess.c,
Richard Henderson <=
- [PATCH v6 15/31] exec: Rename guest_{addr,range}_valid to *_untagged, Richard Henderson, 2021/02/09
- [PATCH v6 17/31] linux-user: Move lock_user et al out of line, Richard Henderson, 2021/02/09
- [PATCH v6 20/31] linux-user/aarch64: Implement PR_TAGGED_ADDR_ENABLE, Richard Henderson, 2021/02/09
- [PATCH v6 30/31] target/arm: Enable MTE for user-only, Richard Henderson, 2021/02/09
- [PATCH v6 27/31] linux-user/aarch64: Signal SEGV_MTESERR for sync tag check fault, Richard Henderson, 2021/02/09
- [PATCH v6 29/31] target/arm: Add allocation tag storage for user mode, Richard Henderson, 2021/02/09
- [PATCH v6 31/31] tests/tcg/aarch64: Add mte smoke tests, Richard Henderson, 2021/02/09
- [PATCH v6 23/31] linux-user/aarch64: Implement PR_MTE_TCF and PR_MTE_TAG, Richard Henderson, 2021/02/09
- [PATCH v6 19/31] linux-user: Handle tags in lock_user/unlock_user, Richard Henderson, 2021/02/09