Skip to contentSkip to conversation

linux-user: Check array bounds in errno conversion

PATCH Superseded Peter Maydell 2 participants 2 messages Mar 3, 2016 – Mar 6, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Check array bounds in errno conversion

From: Timothy E Baldwin <[email protected]>

Check array bounds in host_to_target_errno() and target_to_host_errno().

Message-id: 1441497448-32489-2-git-send-email-T.E.Baldwin99@members.leeds.ac.uk

[PMM: Add a lower-bound check, use braces on if(), tweak commit message]

---
This is a bugfix patch fished out of Timothy's signal-race-fixes patch series. We had a previous go-around doing this with unsigned integers, but that doesn't work.

 linux-user/syscall.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
linux-user/syscall.c +6 -2
index 9517531..f9dcdd4 100644
@@ -617,15 +617,19 @@ static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
 
 static inline int host_to_target_errno(int err)
 {
-    if(host_to_target_errno_table[err])
+    if (err >= 0 && err < ERRNO_TABLE_SIZE &&
+        host_to_target_errno_table[err]) {
         return host_to_target_errno_table[err];
+    }
     return err;
 }
 
 static inline int target_to_host_errno(int err)
 {
-    if (target_to_host_errno_table[err])
+    if (err >= 0 && err < ERRNO_TABLE_SIZE &&
+        target_to_host_errno_table[err]) {
         return target_to_host_errno_table[err];
+    }
     return err;
 }
 
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Peter Maydell

From: Timothy E Baldwin <[email protected]>

Check array bounds in host_to_target_errno() and target_to_host_errno().

Message-id: 1441497448-32489-2-git-send-email-T.E.Baldwin99@members.leeds.ac.uk

[PMM: Add a lower-bound check, use braces on if(), tweak commit message]
Quoted 35 lines

---
This is a bugfix patch fished out of Timothy's signal-race-fixes patch series. We had a previous go-around doing this with unsigned integers, but that doesn't work.

 linux-user/syscall.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
linux-user/syscall.c +6 -2
index 9517531..f9dcdd4 100644
@@ -617,15 +617,19 @@ static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
 
 static inline int host_to_target_errno(int err)
 {
-    if(host_to_target_errno_table[err])
+    if (err >= 0 && err < ERRNO_TABLE_SIZE &&
+        host_to_target_errno_table[err]) {
         return host_to_target_errno_table[err];
+    }
     return err;
 }
 
 static inline int target_to_host_errno(int err)
 {
-    if (target_to_host_errno_table[err])
+    if (err >= 0 && err < ERRNO_TABLE_SIZE &&
+        target_to_host_errno_table[err]) {
         return target_to_host_errno_table[err];
+    }
     return err;
 }