Skip to contentSkip to conversation

linux-user/ppc: Fix sigmask endianness issue in sigreturn

PATCH Applied Ilya Leoshkevich 4 participants 10 messages Oct 17, 2024 – Oct 25, 2024

lore
Share & tools
lore
#0 lore raw

cover linux-user/ppc: Fix sigmask endianness issue in sigreturn

Hi,

This series fixes an issue where an emulated ppc64le process running on s390x attempting to wake up a sigwait()ing thread would instead terminate itself. Patch 1 is the fix, patch 2 is a testcase extracted from the real-world scenario.

Best regards,
Ilya

Ilya Leoshkevich (2):
linux-user/ppc: Fix sigmask endianness issue in sigreturn tests/tcg: Test that sigreturn() does not corrupt the signal mask

 linux-user/ppc/signal.c                 |  2 +-
 tests/tcg/multiarch/sigreturn-sigmask.c | 51 +++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/sigreturn-sigmask.c
Signature · 1 line
2.47.0
#6 lore raw

PATCH 2/2 tests/tcg: Test that sigreturn() does not corrupt the signal mask

Add a small test to prevent regressions.

---
 tests/tcg/multiarch/sigreturn-sigmask.c | 51 +++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 tests/tcg/multiarch/sigreturn-sigmask.c
tests/tcg/multiarch/sigreturn-sigmask.c new file +51 -0
new file mode 100644
index 00000000000..e6cc904898d
@@ -0,0 +1,51 @@
+/*
+ * Test that sigreturn() does not corrupt the signal mask.
+ * Block SIGUSR2 and handle SIGUSR1.
+ * Then sigwait() SIGUSR2, which relies on it remaining blocked.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int seen_sig = -1;
+
+static void signal_func(int sig)
+{
+    seen_sig = sig;
+}
+
+static void *thread_func(void *arg)
+{
+    kill(getpid(), SIGUSR2);
+    return NULL;
+}
+
+int main(void)
+{
+    struct sigaction act = {
+        .sa_handler = signal_func,
+    };
+    pthread_t thread;
+    sigset_t set;
+    int sig;
+
+    assert(sigaction(SIGUSR1, &act, NULL) == 0);
+
+    assert(sigemptyset(&set) == 0);
+    assert(sigaddset(&set, SIGUSR2) == 0);
+    assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
+
+    kill(getpid(), SIGUSR1);
+    assert(seen_sig == SIGUSR1);
+
+    assert(pthread_create(&thread, NULL, thread_func, NULL) == 0);
+    assert(sigwait(&set, &sig) == 0);
+    assert(sig == SIGUSR2);
+    assert(pthread_join(thread, NULL) == 0);
+
+    return EXIT_SUCCESS;
+}
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/2 linux-user/ppc: Fix sigmask endianness issue in sigreturn

In reply to Ilya Leoshkevich

do_setcontext() copies the target sigmask without endianness handling and then uses target_to_host_sigset_internal(), which expects a byte-swapped one. Use target_to_host_sigset() instead.

Fixes bcd4933a23f1 ("linux-user: ppc signal handling")
---
 linux-user/ppc/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/ppc/signal.c +1 -1
index a1d8c0bccc1..24e5a02a782 100644
@@ -628,7 +628,7 @@ static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
     if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
         return 1;
 
-    target_to_host_sigset_internal(&blocked, &set);
+    target_to_host_sigset(&blocked, &set);
     set_sigmask(&blocked);
     restore_user_regs(env, mcp, sig);
 
Signature · 1 line
2.47.0
Reviewed-by on v1 1/2
#2 lore raw
Quoted 9 lines · Ilya Leoshkevich

do_setcontext() copies the target sigmask without endianness handling and then uses target_to_host_sigset_internal(), which expects a byte-swapped one. Use target_to_host_sigset() instead.

Fixes bcd4933a23f1 ("linux-user: ppc signal handling")
---
  linux-user/ppc/signal.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

Reviewed-by on v1 1/2
#3 lore raw
In reply to Ilya Leoshkevich
Quoted 3 lines · Ilya Leoshkevich

do_setcontext() copies the target sigmask without endianness handling and then uses target_to_host_sigset_internal(), which expects a byte-swapped one. Use target_to_host_sigset() instead.

These function names are confusing.

Quoted 11 lines
Fixes bcd4933a23f1 ("linux-user: ppc signal handling")
---
  linux-user/ppc/signal.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/ppc/signal.c +0 -0
index a1d8c0bccc1..24e5a02a782 100644

linux-user/ppc/signal.c static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig) · line 634

Quoted 8 lines
linux-user/ppc/signal.c +1 -1
@@ -628,7 +628,7 @@ static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
      if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
          return 1;
 
-    target_to_host_sigset_internal(&blocked, &set);
+    target_to_host_sigset(&blocked, &set);
      set_sigmask(&blocked);
      restore_user_regs(env, mcp, sig);
 
Acked-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] tests/tcg: Test that sigreturn() does not corrupt the signal mask

In reply to Ilya Leoshkevich
Quoted 7 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
  tests/tcg/multiarch/sigreturn-sigmask.c | 51 +++++++++++++++++++++++++
  1 file changed, 51 insertions(+)

create mode 100644 tests/tcg/multiarch/sigreturn-sigmask.c

r~

Quoted 58 lines
tests/tcg/multiarch/sigreturn-sigmask.c new file +51 -0
new file mode 100644
index 00000000000..e6cc904898d
@@ -0,0 +1,51 @@
+/*
+ * Test that sigreturn() does not corrupt the signal mask.
+ * Block SIGUSR2 and handle SIGUSR1.
+ * Then sigwait() SIGUSR2, which relies on it remaining blocked.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int seen_sig = -1;
+
+static void signal_func(int sig)
+{
+    seen_sig = sig;
+}
+
+static void *thread_func(void *arg)
+{
+    kill(getpid(), SIGUSR2);
+    return NULL;
+}
+
+int main(void)
+{
+    struct sigaction act = {
+        .sa_handler = signal_func,
+    };
+    pthread_t thread;
+    sigset_t set;
+    int sig;
+
+    assert(sigaction(SIGUSR1, &act, NULL) == 0);
+
+    assert(sigemptyset(&set) == 0);
+    assert(sigaddset(&set, SIGUSR2) == 0);
+    assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
+
+    kill(getpid(), SIGUSR1);
+    assert(seen_sig == SIGUSR1);
+
+    assert(pthread_create(&thread, NULL, thread_func, NULL) == 0);
+    assert(sigwait(&set, &sig) == 0);
+    assert(sig == SIGUSR2);
+    assert(pthread_join(thread, NULL) == 0);
+
+    return EXIT_SUCCESS;
+}
#8 lore raw

Re: [PATCH 2/2] tests/tcg: Test that sigreturn() does not corrupt the signal mask

In reply to Ilya Leoshkevich
Quoted 7 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
  tests/tcg/multiarch/sigreturn-sigmask.c | 51 +++++++++++++++++++++++++
  1 file changed, 51 insertions(+)

create mode 100644 tests/tcg/multiarch/sigreturn-sigmask.c

This needs -lpthread. I'll fix while applying.

r~

#9 lore raw
In reply to Ilya Leoshkevich
Quoted 10 lines · Ilya Leoshkevich

Hi,

This series fixes an issue where an emulated ppc64le process running on s390x attempting to wake up a sigwait()ing thread would instead terminate itself. Patch 1 is the fix, patch 2 is a testcase extracted from the real-world scenario.

Best regards,
Ilya

Quoted 9 lines

Ilya Leoshkevich (2):
linux-user/ppc: Fix sigmask endianness issue in sigreturn tests/tcg: Test that sigreturn() does not corrupt the signal mask

  linux-user/ppc/signal.c                 |  2 +-
  tests/tcg/multiarch/sigreturn-sigmask.c | 51 +++++++++++++++++++++++++
  2 files changed, 52 insertions(+), 1 deletion(-)

create mode 100644 tests/tcg/multiarch/sigreturn-sigmask.c

Queued, thanks.

r~

#4 lore raw
In reply to Ilya Leoshkevich
Quoted 15 lines · Ilya Leoshkevich

do_setcontext() copies the target sigmask without endianness handling and then uses target_to_host_sigset_internal(), which expects a byte-swapped one. Use target_to_host_sigset() instead.

Fixes bcd4933a23f1 ("linux-user: ppc signal handling")
---
  linux-user/ppc/signal.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/ppc/signal.c +0 -0
index a1d8c0bccc1..24e5a02a782 100644

linux-user/ppc/signal.c static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig) · line 633

Quoted 7 lines
linux-user/ppc/signal.c +1 -1
@@ -628,7 +628,7 @@ static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
      if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
          return 1;
 
-    target_to_host_sigset_internal(&blocked, &set);
+    target_to_host_sigset(&blocked, &set);
      set_sigmask(&blocked);
      restore_user_regs(env, mcp, sig);

Shouldn't this change be picked for all stable releases too? Yes, the issue is very old so probably does not have big impact, but it looks like it should be okay to fix it finally?

I'm picking it up, please let me know if I shouldn't.

Thanks,

/mjt

#5 lore raw
Quoted 30 lines · Michael Tokarev
Quoted 19 lines · Ilya Leoshkevich

do_setcontext() copies the target sigmask without endianness handling
and then uses target_to_host_sigset_internal(), which expects a byte-swapped one. Use target_to_host_sigset() instead.

Fixes bcd4933a23f1 ("linux-user: ppc signal handling")

---
  linux-user/ppc/signal.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/ppc/signal.c +0 -0
index a1d8c0bccc1..24e5a02a782 100644
@@ -628,7 +628,7 @@ static int do_setcontext(struct target_ucontext
 *ucp, CPUPPCState *env, int sig)
       if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
           return 1;
Quoted 5 lines


-    target_to_host_sigset_internal(&blocked, &set); +    target_to_host_sigset(&blocked, &set); set_sigmask(&blocked); restore_user_regs(env, mcp, sig);

Shouldn't this change be picked for all stable releases too? Yes, the issue is very old so probably does not have big impact, but it looks like it should be okay to fix it finally?

Quoted 5 lines

I'm picking it up, please let me know if I shouldn't.

Thanks,

/mjt

I think it's worth taking it, since it leads to weird qemu-user crashes. I actually intended to Cc: stable here and forgot to do it, so thanks for bringing this up.