Skip to contentSkip to conversation

linux-user: fix wait* syscall status returns

PATCH Superseded Alexander Graf 2 participants 8 messages Nov 23, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: fix wait* syscall status returns

When calling wait4 or waitpid with a status pointer and WNOHANG, the syscall can potentially not modify the status pointer input. Now if we have guest code like:

  int status = 0;
  waitpid(pid, &status, WNOHANG);
  if (status)
     <breakage>

then we have to make sure that in case status did not change we actually return the guest's initialized status variable instead of our own uninitialized. We fail to do so today, as we proxy everything through an uninitialized status variable which for me ended up always containing the last error code.

This patch fixes some test cases when building yast2-core in OBS for ARM.

---
 linux-user/syscall.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
linux-user/syscall.c +7 -1
index 3e6f3bd..f86fe4a 100644
@@ -4833,7 +4833,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef TARGET_NR_waitpid
     case TARGET_NR_waitpid:
         {
-            int status;
+            int status = 0;
+            if (arg2) {
+                get_user_s32(status, arg2);
+            }
             ret = get_errno(waitpid(arg1, &status, arg3));
             if (!is_error(ret) && arg2
                 && put_user_s32(host_to_target_waitstatus(status), arg2))
@@ -6389,6 +6392,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 rusage_ptr = &rusage;
             else
                 rusage_ptr = NULL;
+            if (status_ptr) {
+                get_user_s32(status, status_ptr);
+            }
             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
             if (!is_error(ret)) {
                 if (status_ptr) {
Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 29 lines · Alexander Graf

When calling wait4 or waitpid with a status pointer and WNOHANG, the syscall can potentially not modify the status pointer input. Now if we have guest code like:

 int status = 0;
 waitpid(pid, &status, WNOHANG);
 if (status)
    <breakage>

then we have to make sure that in case status did not change we actually return the guest's initialized status variable instead of our own uninitialized. We fail to do so today, as we proxy everything through an uninitialized status variable which for me ended up always containing the last error code.

This patch fixes some test cases when building yast2-core in OBS for ARM.

---
 linux-user/syscall.c |    8 +++++++-

 1 files changed, 7 insertions(+), 1 deletions(-)
linux-user/syscall.c +0 -0
index 3e6f3bd..f86fe4a 100644
@@ -4833,7 +4833,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  #ifdef TARGET_NR_waitpid

    case TARGET_NR_waitpid:
        {

Quoted 8 lines

-            int status;
+            int status = 0;
+            if (arg2) {
+                get_user_s32(status, arg2);
+            }
            ret = get_errno(waitpid(arg1, &status, arg3));
            if (!is_error(ret) && arg2
                && put_user_s32(host_to_target_waitstatus(status), arg2))

If the problem is that waitpid() can return success without writing to status, then this code is still not right because we will get the initial target waitstatus into status, and then pass it through host_to_target_waitstatus(), possibly modifying it, before writing it back to guest memory.

I think waitpid() will always and only write to status if the return value is > 0 (ie it's a PID, not 0 or -1). So I think the right fix for this problem is to have the if() protecting the put_user_s32() read "if (ret && !is_error(ret) && ...".

(ret == 0 is of course the WNOHANG-and-no-child-yet case you are hitting.)

Quoted 10 lines
linux-user/syscall.c +0 -0
@@ -6389,6 +6392,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 rusage_ptr = &rusage;

            else
                rusage_ptr = NULL;
+            if (status_ptr) {
+                get_user_s32(status, status_ptr);
+            }
            ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr)); if (!is_error(ret)) {
                if (status_ptr) {

...and similarly here.

-- PMM

#2 lore raw
Quoted 36 lines · Peter Maydell
Quoted 35 lines · Alexander Graf

When calling wait4 or waitpid with a status pointer and WNOHANG, the syscall can potentially not modify the status pointer input. Now if we have guest code like:

int status = 0;
waitpid(pid, &status, WNOHANG);
if (status)
<breakage>

then we have to make sure that in case status did not change we actually return the guest's initialized status variable instead of our own uninitialized. We fail to do so today, as we proxy everything through an uninitialized status variable which for me ended up always containing the last error code.

This patch fixes some test cases when building yast2-core in OBS for ARM.

---
 linux-user/syscall.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
linux-user/syscall.c +4 -1
index 3e6f3bd..f86fe4a 100644
@@ -4833,7 +4833,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef TARGET_NR_waitpid
    case TARGET_NR_waitpid:
        {
-            int status;
+            int status = 0;
+            if (arg2) {
+                get_user_s32(status, arg2);
+            }
            ret = get_errno(waitpid(arg1, &status, arg3));
Quoted 8 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 4842

Quoted 2 lines
linux-user/syscall.c +0 -0
            if (!is_error(ret) && arg2
                && put_user_s32(host_to_target_waitstatus(status), arg2))

If the problem is that waitpid() can return success without writing to status, then this code is still not right because we will get the initial target waitstatus into status, and then pass it through host_to_target_waitstatus(), possibly modifying it, before writing it back to guest memory.

Yes. Maybe we should add a check if input_state != output_state and only then do the conversion?

Quoted 7 lines

I think waitpid() will always and only write to status if the return value is > 0 (ie it's a PID, not 0 or -1). So I think the right fix for this problem is to have the if() protecting the put_user_s32() read "if (ret && !is_error(ret) && ...".

(ret == 0 is of course the WNOHANG-and-no-child-yet case you are hitting.)

The man page wasn't really clear here. It sounded as if you can also get 0 as return value and still have status change. That's why I jumped through this hoop in the first place :)

Alex

Quoted 16 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 6400

Quoted 11 lines
linux-user/syscall.c +3 -0
@@ -6389,6 +6392,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                rusage_ptr = &rusage;
            else
                rusage_ptr = NULL;
+            if (status_ptr) {
+                get_user_s32(status, status_ptr);
+            }
            ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
            if (!is_error(ret)) {
                if (status_ptr) {

...and similarly here.

-- PMM

#3 lore raw
Quoted 9 lines · Alexander Graf
Quoted 5 lines · Peter Maydell

If the problem is that waitpid() can return success without writing to status, then this code is still not right because we will get the initial target waitstatus into status, and then pass it through host_to_target_waitstatus(), possibly modifying it, before writing it back to guest memory.

Yes. Maybe we should add a check if input_state != output_state and only then do the conversion?

I'm not sure this works (unless you go to the effort of implementing target_to_host_waitstatus() which seems overkill to me) but I'm not entirely sure what you're proposing to compare to what.

Quoted 10 lines
Quoted 6 lines

I think waitpid() will always and only write to status if the return value is > 0 (ie it's a PID, not 0 or -1). So I think the right fix for this problem is to have the if() protecting the put_user_s32() read "if (ret && !is_error(ret) && ...".

(ret == 0 is of course the WNOHANG-and-no-child-yet case you are hitting.)

The man page wasn't really clear here. It sounded as if you can also get 0 as return value and still have status change. That's why I jumped through this hoop in the first place :)

I think POSIX is clear enough. Either
* a child status is available, return pid and write to status
* WNOHANG & not ready, return 0 (don't write to status)
* -1 and set errno (don't write to status)

(strictly, whether status is written for EINTR is unpredictable, but I think we can assume nobody relies on this and I bet the kernel folks wouldn't worry too much about changing that between versions...)

-- PMM

#4 lore raw
Quoted 8 lines · Peter Maydell
Quoted 7 lines · Alexander Graf
Quoted 5 lines · Peter Maydell

If the problem is that waitpid() can return success without writing to status, then this code is still not right because we will get the initial target waitstatus into status, and then pass it through host_to_target_waitstatus(), possibly modifying it, before writing it back to guest memory.

Quoted 6 lines
Quoted 2 lines

Yes. Maybe we should add a check if input_state != output_state and only then do the conversion?

I'm not sure this works (unless you go to the effort of implementing target_to_host_waitstatus() which seems overkill to me) but I'm not entirely sure what you're proposing to compare to what.

It's an integer. If the number is the same before and after the wait syscall, we can safely assume that it's the same thing, so we don't have to convert it, no?

Quoted 12 lines
Quoted 11 lines
Quoted 7 lines

I think waitpid() will always and only write to status if the return value is > 0 (ie it's a PID, not 0 or -1). So I think the right fix for this problem is to have the if() protecting the put_user_s32() read "if (ret && !is_error(ret) && ...".

(ret == 0 is of course the WNOHANG-and-no-child-yet case you are hitting.)

The man page wasn't really clear here. It sounded as if you can also get 0 as return value and still have status change. That's why I jumped through this hoop in the first place :)

Quoted 8 lines

I think POSIX is clear enough. Either
* a child status is available, return pid and write to status
* WNOHANG & not ready, return 0 (don't write to status)
* -1 and set errno (don't write to status)

(strictly, whether status is written for EINTR is unpredictable, but I think we can assume nobody relies on this and I bet the kernel folks wouldn't worry too much about changing that between versions...)

Hrm, ok. I'll change it to your version then.

Alex

#5 lore raw
Quoted 7 lines · Alexander Graf
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Alexander Graf

Yes. Maybe we should add a check if input_state != output_state and only then do the conversion?

Quoted 7 lines
Quoted 3 lines

I'm not sure this works (unless you go to the effort of implementing target_to_host_waitstatus() which seems overkill to me) but I'm not entirely sure what you're proposing to compare to what.

It's an integer. If the number is the same before and after the wait syscall, we can safely assume that it's the same thing, so we don't have to convert it, no?

But you don't know whether it's the same before and after because wait() didn't write to it [=> don't write to guest memory] or if wait() did write to it but happened to write it as the same value it had before [=> do write to guest memory].

-- PMM

#6 lore raw
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Alexander Graf
Quoted 7 lines · Peter Maydell
Quoted 2 lines · Alexander Graf

Yes. Maybe we should add a check if input_state != output_state and only then do the conversion?

I'm not sure this works (unless you go to the effort of implementing target_to_host_waitstatus() which seems overkill to me) but I'm not entirely sure what you're proposing to compare to what.

Quoted 8 lines
Quoted 3 lines

It's an integer. If the number is the same before and after the wait syscall, we can safely assume that it's the same thing, so we don't have to convert it, no?

But you don't know whether it's the same before and after because wait() didn't write to it [=> don't write to guest memory] or if wait() did write to it but happened to write it as the same value it had before [=> do write to guest memory].

If it was the same value before, it will still be the same value in guest memory.

  get_guest_s32(status, status_ptr);
  old_status = status;
  wait(...)
  if (old_status != status) {
    status = convert_status(status);
    put_guest_s32(status, status_ptr);
  }

If the values are identical, it's safe to assume that we don't have to convert. And the value will already be in guest memory :)

Alex

#7 lore raw
Quoted 9 lines · Alexander Graf

If it was the same value before, it will still be the same value in guest memory.

 get_guest_s32(status, status_ptr);
 old_status = status;
 wait(...)
 if (old_status != status) {
   status = convert_status(status);
   put_guest_s32(status, status_ptr);
 }

Picking some concrete numbers as an illustration; obviously they're not really sensible status values:

Suppose guest memory contains the value 1, and that convert_status(1) == 2. Now if you come out of wait() and status == 1 (ie old_status == status), then either:
(a) wait() didn't write to status => do nothing
(b) wait() did write to status => since convert_status(1) == 2 we need to write 2 to guest memory

For this approach to work you have to have a conversion function from guest to host status, I think.

-- PMM