Skip to contentSkip to conversation

linux-user: Add missing check for return value of lock_user

PATCH In review Stefan Weil 3 participants 3 messages Mar 14, 2015 – Oct 19, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Add missing check for return value of lock_user

This fixes a warning from Coverity:
"Dereference null return value (NULL_RETURNS)"

---
 linux-user/flatload.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
linux-user/flatload.c +5 -3
index 566a7a8..56ac790 100644
@@ -97,11 +97,13 @@ static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
                         abi_ulong offset)
 {
     void *buf;
-    int ret;
+    int ret = -TARGET_EFAULT;
 
     buf = lock_user(VERIFY_WRITE, ptr, len, 0);
-    ret = pread(fd, buf, len, offset);
-    unlock_user(buf, ptr, len);
+    if (buf) {
+        ret = pread(fd, buf, len, offset);
+        unlock_user(buf, ptr, len);
+    }
     return ret;
 }
 /****************************************************************************/
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 13 lines · Stefan Weil

This fixes a warning from Coverity:
"Dereference null return value (NULL_RETURNS)"

---
 linux-user/flatload.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
linux-user/flatload.c +0 -0
index 566a7a8..56ac790 100644

linux-user/flatload.c static int target_pread(int fd, abi_ulong ptr, abi_ulong len, · line 100

Quoted 5 lines
linux-user/flatload.c +1 -1
@@ -97,11 +97,13 @@ static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
                         abi_ulong offset)
 {
     void *buf;
-    int ret;
+    int ret = -TARGET_EFAULT;

The return value for this function should be a host errno, not a target errno, I think. (If you track back upwards then eventually main.c calls loader_exec() and treats the return value as a host errno.) This seems like the right thing given that these loader functions are all pre-run setup, and are not involved in emulation of guest syscalls.

linux-user/flatload.c static int target_pread(int fd, abi_ulong ptr, abi_ulong len, · line 104

Quoted 6 lines
linux-user/flatload.c +2 -2
     buf = lock_user(VERIFY_WRITE, ptr, len, 0);
-    ret = pread(fd, buf, len, offset);
-    unlock_user(buf, ptr, len);
+    if (buf) {
+        ret = pread(fd, buf, len, offset);

A different bug, but if ret here indicates that pread() failed we should be returning -errno.

linux-user/flatload.c static int target_pread(int fd, abi_ulong ptr, abi_ulong len, · line 108

Quoted 4 lines
linux-user/flatload.c +2 -0
+        unlock_user(buf, ptr, len);
+    }
     return ret;
 }

-- PMM

#2 lore raw
In reply to Stefan Weil
Quoted 20 lines · Stefan Weil

This fixes a warning from Coverity:
"Dereference null return value (NULL_RETURNS)"

---
 linux-user/flatload.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
linux-user/flatload.c +1 -1
index 566a7a8..56ac790 100644
@@ -97,11 +97,13 @@ static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
                         abi_ulong offset)
 {
     void *buf;
-    int ret;
+    int ret = -TARGET_EFAULT;
 

linux-user/flatload.c static int target_pread(int fd, abi_ulong ptr, abi_ulong len, · line 109

Quoted 10 lines
linux-user/flatload.c +4 -2
     buf = lock_user(VERIFY_WRITE, ptr, len, 0);
-    ret = pread(fd, buf, len, offset);
-    unlock_user(buf, ptr, len);
+    if (buf) {
+        ret = pread(fd, buf, len, offset);
+        unlock_user(buf, ptr, len);
+    }
     return ret;
 }
 /****************************************************************************/

Hi Stefan,

are you going to update and resend this patch?

Thanks,

Paolo