Skip to contentSkip to conversation

linux-user: Check for EFAULT failure in nanosleep

PATCH Applied Peter Maydell 2 participants 3 messages Jul 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Check for EFAULT failure in nanosleep

target_to_host_timespec() returns an error if the memory the guest passed us isn't actually readable. We check for this everywhere except the callsite in the TARGET_NR_nanosleep case, so this mistake was caught by a Coverity heuristic.

Add the missing error checks to the calls that convert between the host and target timespec structs.

Coverity: CID 1507104

---
Tested via the LTP nanosleep tests, but they don't actually exercise the EFAULT codepaths...

 linux-user/syscall.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
linux-user/syscall.c +6 -2
index fc37028597c..c600d5ccc0e 100644
@@ -11630,10 +11630,14 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
     case TARGET_NR_nanosleep:
         {
             struct timespec req, rem;
-            target_to_host_timespec(&req, arg1);
+            if (target_to_host_timespec(&req, arg1)) {
+                return -TARGET_EFAULT;
+            }
             ret = get_errno(safe_nanosleep(&req, &rem));
             if (is_error(ret) && arg2) {
-                host_to_target_timespec(arg2, &rem);
+                if (host_to_target_timespec(arg2, &rem)) {
+                    return -TARGET_EFAULT;
+                }
             }
         }
         return ret;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · Peter Maydell

target_to_host_timespec() returns an error if the memory the guest passed us isn't actually readable. We check for this everywhere except the callsite in the TARGET_NR_nanosleep case, so this mistake was caught by a Coverity heuristic.

Add the missing error checks to the calls that convert between the host and target timespec structs.

Quoted 8 lines

Coverity: CID 1507104

---
Tested via the LTP nanosleep tests, but they don't actually exercise the EFAULT codepaths...

  linux-user/syscall.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)

Queued, thanks.

r~

Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

target_to_host_timespec() returns an error if the memory the guest passed us isn't actually readable. We check for this everywhere except the callsite in the TARGET_NR_nanosleep case, so this mistake was caught by a Coverity heuristic.

Add the missing error checks to the calls that convert between the host and target timespec structs.

Quoted 8 lines

Coverity: CID 1507104

---
Tested via the LTP nanosleep tests, but they don't actually exercise the EFAULT codepaths...

  linux-user/syscall.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)

r~

Quoted 22 lines
linux-user/syscall.c +6 -2
index fc37028597c..c600d5ccc0e 100644
@@ -11630,10 +11630,14 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
      case TARGET_NR_nanosleep:
          {
              struct timespec req, rem;
-            target_to_host_timespec(&req, arg1);
+            if (target_to_host_timespec(&req, arg1)) {
+                return -TARGET_EFAULT;
+            }
              ret = get_errno(safe_nanosleep(&req, &rem));
              if (is_error(ret) && arg2) {
-                host_to_target_timespec(arg2, &rem);
+                if (host_to_target_timespec(arg2, &rem)) {
+                    return -TARGET_EFAULT;
+                }
              }
          }
          return ret;