[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 25/74] semihosting: Bound length for semihost_sys_{read, write
From: |
Richard Henderson |
Subject: |
[PATCH v2 25/74] semihosting: Bound length for semihost_sys_{read, write} |
Date: |
Tue, 3 May 2022 12:47:54 -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 v2 21/74] semihosting: Split out semihost_sys_open, (continued)
- [PATCH v2 21/74] semihosting: Split out semihost_sys_open, Richard Henderson, 2022/05/03
- [PATCH v2 20/74] semihosting: Move GET_ARG/SET_ARG earlier in the file, Richard Henderson, 2022/05/03
- [PATCH v2 23/74] semihosting: Split out semihost_sys_read, Richard Henderson, 2022/05/03
- [PATCH v2 27/74] semihosting: Split out semihost_sys_isatty, Richard Henderson, 2022/05/03
- [PATCH v2 18/74] semihosting: Split out common_semi_has_synccache, Richard Henderson, 2022/05/03
- [PATCH v2 11/74] semihosting: Move common-semi.h to include/semihosting/, Richard Henderson, 2022/05/03
- [PATCH v2 26/74] semihosting: Split out semihost_sys_lseek, Richard Henderson, 2022/05/03
- [PATCH v2 31/74] semihosting: Split out semihost_sys_system, Richard Henderson, 2022/05/03
- [PATCH v2 24/74] semihosting: Split out semihost_sys_write, Richard Henderson, 2022/05/03
- [PATCH v2 25/74] semihosting: Bound length for semihost_sys_{read, write},
Richard Henderson <=
- [PATCH v2 29/74] semihosting: Split out semihost_sys_remove, Richard Henderson, 2022/05/03
- [PATCH v2 30/74] semihosting: Split out semihost_sys_rename, Richard Henderson, 2022/05/03
- [PATCH v2 28/74] semihosting: Split out semihost_sys_flen, Richard Henderson, 2022/05/03
- [PATCH v2 34/74] gdbstub: Widen gdb_syscall_complete_cb return value, Richard Henderson, 2022/05/03
- [PATCH v2 32/74] semihosting: Create semihost_sys_{stat,fstat}, Richard Henderson, 2022/05/03
- [PATCH v2 33/74] semihosting: Create semihost_sys_gettimeofday, Richard Henderson, 2022/05/03
- [PATCH v2 35/74] semihosting: Fix docs comment for qemu_semihosting_console_inc, Richard Henderson, 2022/05/03
- [PATCH v2 38/74] semihosting: Cleanup chardev init, Richard Henderson, 2022/05/03
- [PATCH v2 37/74] semihosting: Expand qemu_semihosting_console_inc to read, Richard Henderson, 2022/05/03
- [PATCH v2 36/74] semihosting: Pass CPUState to qemu_semihosting_console_inc, Richard Henderson, 2022/05/03