Skip to contentSkip to conversation

linux-user: Add syscall dispatch support

PATCH New Arusekk 1 participant 1 message Jul 11, 2025 RISC-V

lore
Share & tools
lore

Arusekk

author
#0 lore raw

PATCH v3 1/1 linux-user: Add syscall dispatch support

This commit adds support for the prctl(PR_SET_SYSCALL_USER_DISPATCH) function in the Linux userspace emulator.

It is implemented as a fully host-independent function, by forcing a SIGSYS early during syscall handling, if the PC is outside the allowed range.

Since disabled SUD is indistinguishable from enabled SUD with always-allowed region length == ~0, this encoding is used instead of introducing a new flag.

Tested on [uglendix][1], will probably also apply to software like tiny-wine, rpcsx, limbo, lazypoline, vicar, sysfail and endokernel, to name a few.

[1]: https://sr.ht/~arusekk/uglendix

---
Changes from v2: braces, vaddr TaskState->sys_dispatch[_selector], factor out do_prctl_syscall_user_dispatch,
access_ok instead of dummy get_u8, improve hot path, reword.

The files are not in the alphabetic order, architecture-specific changes are moved to top for easier review.

v2: https://lore.kernel.org/qemu-devel/[email protected]/

 linux-user/aarch64/signal.c     |  5 +++
 linux-user/alpha/signal.c       |  6 +++
 linux-user/arm/signal.c         |  5 +++
 linux-user/hexagon/signal.c     |  5 +++
 linux-user/hppa/signal.c        |  5 +++
 linux-user/i386/signal.c        | 10 +++++
 linux-user/loongarch64/signal.c |  5 +++
 linux-user/m68k/signal.c        |  5 +++
 linux-user/microblaze/signal.c  |  5 +++
 linux-user/mips/signal.c        | 10 +++++
 linux-user/openrisc/signal.c    |  5 +++
 linux-user/ppc/signal.c         | 10 +++++
 linux-user/riscv/signal.c       |  5 +++
 linux-user/s390x/signal.c       |  5 +++
 linux-user/sh4/signal.c         |  5 +++
 linux-user/sparc/signal.c       | 10 +++++
 linux-user/xtensa/signal.c      |  5 +++
 linux-user/main.c               |  2 +
 linux-user/qemu.h               |  5 +++
 linux-user/signal-common.h      |  1 +
 linux-user/syscall.c            | 74 ++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h       |  6 +++
 22 files changed, 193 insertions(+), 1 deletion(-)
linux-user/aarch64/signal.c +5 -0
index d50cab78d8..da2c039544 100644
@@ -746,3 +746,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/alpha/signal.c +6 -0
index 896c2c148a..48d6e4f790 100644
@@ -275,3 +275,9 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 6 * 4);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 3 * 4
+        || pc == default_rt_sigreturn + 3 * 4;
+}
linux-user/arm/signal.c +5 -0
index 8db1c4b233..53430908cd 100644
@@ -644,3 +644,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, total_size);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return (abi_ulong)(pc - default_sigreturn) <= 8 * RETCODE_BYTES;
+}
linux-user/hexagon/signal.c +5 -0
index 492b51f155..751155006e 100644
@@ -291,3 +291,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 4 * 2);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 4 * 2;
+}
linux-user/hppa/signal.c +5 -0
index f6f094c960..e17e3527b4 100644
@@ -216,3 +216,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = (sigtramp_page + 8) | 3;
     unlock_user(tramp, sigtramp_page, 6*4);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == (default_rt_sigreturn & ~3) + 4*4;
+}
linux-user/i386/signal.c +10 -0
index 0f11dba831..a8302c0b1e 100644
@@ -865,3 +865,13 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
 #endif
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifndef TARGET_X86_64
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 7) {
+        return true;
+    }
+#endif
+    return false;
+}
linux-user/loongarch64/signal.c +5 -0
index 1a322f9697..fedf297ca8 100644
@@ -452,3 +452,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/m68k/signal.c +5 -0
index 77555781aa..c6af2baee9 100644
@@ -408,3 +408,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 4 + 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 4 || pc == default_rt_sigreturn + 6;
+}
linux-user/microblaze/signal.c +5 -0
index f6d47d76ff..2027858025 100644
@@ -230,3 +230,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/mips/signal.c +10 -0
index d69a5d73dd..73914e001f 100644
@@ -393,3 +393,13 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8) {
+        return true;
+    }
+#endif
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/openrisc/signal.c +5 -0
index cb74a9fe5e..c368069980 100644
@@ -175,3 +175,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/ppc/signal.c +10 -0
index 24e5a02a78..7516fccfeb 100644
@@ -730,3 +730,13 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ARCH_HAS_SETUP_FRAME
+    if (pc == default_sigreturn + 8) {
+        return true;
+    }
+#endif
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/riscv/signal.c +5 -0
index 358fa1d82d..5ae889e8b9 100644
@@ -213,3 +213,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     default_rt_sigreturn = sigtramp_page;
     unlock_user(tramp, sigtramp_page, 8);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 8;
+}
linux-user/s390x/signal.c +5 -0
index df49c24708..17a194104f 100644
@@ -419,3 +419,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 + 2);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 2 || pc == default_rt_sigreturn + 2;
+}
linux-user/sh4/signal.c +5 -0
index 9ecc026fae..1c34dd27c1 100644
@@ -346,3 +346,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
 
     unlock_user(tramp, sigtramp_page, 2 * 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_sigreturn + 6 || pc == default_rt_sigreturn + 6;
+}
linux-user/sparc/signal.c +10 -0
index 8181b8b92c..d7121d1871 100644
@@ -789,3 +789,13 @@ do_sigsegv:
     force_sig(TARGET_SIGSEGV);
 }
 #endif /* TARGET_SPARC64 */
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+#ifdef TARGET_ABI32
+    if (pc == default_sigreturn + 8 || pc == default_rt_sigreturn + 8) {
+        return true;
+    }
+#endif
+    return false;
+}
linux-user/xtensa/signal.c +5 -0
index ef8b0c3a27..0ce3d826d7 100644
@@ -303,3 +303,8 @@ void setup_sigtramp(abi_ulong sigtramp_page)
     install_sigtramp(tramp);
     unlock_user(tramp, sigtramp_page, 6);
 }
+
+bool is_vdso_sigreturn(abi_ulong pc)
+{
+    return pc == default_rt_sigreturn + 6;
+}
linux-user/main.c +2 -0
index a9142ee726..420f91c023 100644
@@ -231,6 +231,8 @@ void init_task_state(TaskState *ts)
         ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
                               NANOSECONDS_PER_SECOND;
     }
+
+    ts->sys_dispatch_len = (abi_ulong)~0ULL;
 }
 
 CPUArchState *cpu_copy(CPUArchState *env)
linux-user/qemu.h +5 -0
index 0b19fa43e6..d5e28a0a4e 100644
@@ -161,6 +161,11 @@ struct TaskState {
     /* This thread's sigaltstack, if it has one */
     struct target_sigaltstack sigaltstack_used;
 
+    /* This thread's SYSCALL_USER_DISPATCH state, len=~0 means disabled */
+    vaddr sys_dispatch;
+    abi_ulong sys_dispatch_len;
+    vaddr sys_dispatch_selector;
+
     /* Start time of task after system boot in clock ticks */
     uint64_t start_boottime;
 };
linux-user/signal-common.h +1 -0
index 196d2406f8..7b17ac3221 100644
@@ -27,6 +27,7 @@ extern abi_ulong default_sigreturn;
 extern abi_ulong default_rt_sigreturn;
 
 void setup_sigtramp(abi_ulong tramp_page);
+bool is_vdso_sigreturn(abi_ulong pc);
 
 int on_sig_stack(unsigned long sp);
 int sas_ss_flags(unsigned long sp);
linux-user/syscall.c +73 -1

116 lines not on this page · raw message

linux-user/syscall_defs.h +6 -0
index 5d22759992..7812253455 100644
@@ -693,6 +693,12 @@ typedef struct target_siginfo {
 #define TARGET_TRAP_HWBKPT      (4)     /* hardware breakpoint/watchpoint */
 #define TARGET_TRAP_UNK         (5)     /* undiagnosed trap */
 
+/*
+ * SIGSYS si_codes
+ */
+#define TARGET_SYS_SECCOMP       (1)  /* seccomp triggered */
+#define TARGET_SYS_USER_DISPATCH (2)  /* syscall user dispatch triggered */
+
 /*
  * SIGEMT si_codes
  */
Signature · 1 line
2.50.0