[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 27/49] semihosting: Bound length for semihost_sys_{read, write
From: |
Richard Henderson |
Subject: |
[PATCH v3 27/49] semihosting: Bound length for semihost_sys_{read, write} |
Date: |
Fri, 20 May 2022 17:03:38 -0700 |
Fixes a minor bug in which a 64-bit guest on a 32-bit host could
truncate the length. This would only ever cause a problem if
there were no bits set in the low 32, so that it truncates to 0.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
semihosting/syscalls.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c
index db4561b798..a5623ebf0f 100644
--- a/semihosting/syscalls.c
+++ b/semihosting/syscalls.c
@@ -313,6 +313,14 @@ void semihost_sys_close(CPUState *cs,
gdb_syscall_complete_cb complete, int fd)
void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
+ /*
+ * Bound length for 64-bit guests on 32-bit hosts, not overlowing ssize_t.
+ * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
+ * idea to do this unconditionally.
+ */
+ if (len > INT32_MAX) {
+ len = INT32_MAX;
+ }
switch (gf->type) {
case GuestFDGDB:
gdb_read(cs, complete, gf, buf, len);
@@ -343,6 +351,14 @@ void semihost_sys_read(CPUState *cs,
gdb_syscall_complete_cb complete,
void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
+ /*
+ * Bound length for 64-bit guests on 32-bit hosts, not overlowing ssize_t.
+ * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
+ * idea to do this unconditionally.
+ */
+ if (len > INT32_MAX) {
+ len = INT32_MAX;
+ }
switch (gf->type) {
case GuestFDGDB:
gdb_write(cs, complete, gf, buf, len);
--
2.34.1
- [PATCH v3 28/49] semihosting: Split out semihost_sys_lseek, (continued)
- [PATCH v3 28/49] semihosting: Split out semihost_sys_lseek, Richard Henderson, 2022/05/20
- [PATCH v3 34/49] semihosting: Create semihost_sys_{stat,fstat}, Richard Henderson, 2022/05/20
- [PATCH v3 35/49] semihosting: Create semihost_sys_gettimeofday, Richard Henderson, 2022/05/20
- [PATCH v3 14/49] include/exec: Move gdb open flags to gdbstub.h, Richard Henderson, 2022/05/20
- [PATCH v3 20/49] semihosting: Split out common_semi_has_synccache, Richard Henderson, 2022/05/20
- [PATCH v3 19/49] semihosting: Split common_semi_flen_buf per target, Richard Henderson, 2022/05/20
- [PATCH v3 27/49] semihosting: Bound length for semihost_sys_{read, write},
Richard Henderson <=
- [PATCH v3 33/49] semihosting: Split out semihost_sys_system, Richard Henderson, 2022/05/20
- [PATCH v3 36/49] gdbstub: Widen gdb_syscall_complete_cb return value, Richard Henderson, 2022/05/20
- [PATCH v3 03/49] semihosting: Improve condition for config.c and console.c, Richard Henderson, 2022/05/20
- [PATCH v3 23/49] semihosting: Split out semihost_sys_open, Richard Henderson, 2022/05/20
[PATCH v3 40/49] semihosting: Cleanup chardev init, Richard Henderson, 2022/05/20