From 37d40e087f73ff4f7ffe31c0d9693eb1ecb65c01 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 3 Nov 2022 09:32:12 -0700 Subject: [PATCH] Ignore SIGSYS on BSD by default --- libc/stdio/getrandom.c | 17 +++-------------- libc/sysv/calls/sys_bogus.s | 2 +- libc/sysv/syscalls.sh | 2 +- libc/sysv/systemfive.S | 28 ++++++++++++++++++++++++++++ test/libc/calls/sigaction_test.c | 7 +++++++ tool/emacs/cosmo-cpp-constants.el | 5 +++++ tool/net/redbean.c | 3 +++ 7 files changed, 48 insertions(+), 16 deletions(-) diff --git a/libc/stdio/getrandom.c b/libc/stdio/getrandom.c index 9df21814afa..14d766a5d84 100644 --- a/libc/stdio/getrandom.c +++ b/libc/stdio/getrandom.c @@ -127,25 +127,14 @@ ssize_t getrandom(void *p, size_t n, unsigned f) { return rc; } -static textstartup void getrandom_init(void) { +__attribute__((__constructor__)) static textstartup void getrandom_init(void) { int e, rc; - struct sigaction sa, oldsa; if (IsWindows()) return; e = errno; - if (IsBsd()) { - sa.sa_flags = 0; - sa.sa_handler = SIG_IGN; - sigemptyset(&sa.sa_mask); - sigaction(SIGSYS, &sa, &oldsa); - } if (!(rc = sys_getrandom(0, 0, 0))) { have_getrandom = true; + } else { + errno = e; } STRACE("sys_getrandom(0,0,0) → %d% m", rc); - if (IsBsd()) { - sigaction(SIGSYS, &oldsa, 0); - } - errno = e; } - -const void *const g_getrandom_init[] initarray = {getrandom_init}; diff --git a/libc/sysv/calls/sys_bogus.s b/libc/sysv/calls/sys_bogus.s index ee6bfcd5766..efed30bda53 100644 --- a/libc/sysv/calls/sys_bogus.s +++ b/libc/sysv/calls/sys_bogus.s @@ -1,2 +1,2 @@ .include "o/libc/sysv/macros.internal.inc" -.scall sys_bogus,0x5005005002500500,globl +.scall sys_bogus,0x00b5005002500500,globl diff --git a/libc/sysv/syscalls.sh b/libc/sysv/syscalls.sh index f592cc4fa11..bcba2eea775 100755 --- a/libc/sysv/syscalls.sh +++ b/libc/sysv/syscalls.sh @@ -372,7 +372,7 @@ scall sys_io_uring_register 0xfffffffffffff1ab globl #────────────────────────RHEL CLOUD────────────────────────── # ←──────┬─ red hat terminates community release of enterprise linux circa 2020 scall sys_pledge 0xfff06cffffffffff globl hidden # └─ online linux services ban the president of united states of america scall sys_msyscall 0xfff025ffffffffff globl # no wrapper -scall sys_bogus 0x5005005002500500 globl +scall sys_bogus 0x00b5005002500500 globl scall sys_open_tree 0xfffffffffffff1ac globl # no wrapper scall sys_move_mount 0xfffffffffffff1ad globl # no wrapper scall sys_fsopen 0xfffffffffffff1ae globl # no wrapper diff --git a/libc/sysv/systemfive.S b/libc/sysv/systemfive.S index ba906ed1374..a16b00d1b80 100644 --- a/libc/sysv/systemfive.S +++ b/libc/sysv/systemfive.S @@ -20,8 +20,11 @@ #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/macros.h" +#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/nr.h" +#define SIG_IGN 1 + /* ▄▄▄ ▄▄▄ ▀▓▓▒▄ ▄▓▒▒░ ▀▓▒▒▒▄ @@ -363,6 +366,31 @@ _init_systemfive_pid: 1: stosq .endfn _init_systemfive_pid #endif +#if SupportsBsd() && !defined(TINY) +_init_systemfive_sigsys: + testb IsBsd() # BSDs will trap SIGSYS! + jz 1f # We want ENOSYS instead + push %rdi # XNU removed some calls + push %rsi # in past, so this makes + xor %eax,%eax # troubleshooting easier + push %rax # but it's non-essential + push %rax + push %rax + push %rax + push %rax + push $SIG_IGN # sigaction_meta size 48 + mov __NR_sigaction,%eax # mag + mov SIGSYS,%edi # sig + mov %rsp,%rsi # new + xor %edx,%edx # old + mov $8,%r10d # for linux + xor %r8d,%r8d # for netbsd + syscall + add $6*8,%rsp + pop %rsi + pop %rdi +1: .endfn _init_systemfive_sigsys +#endif #if SupportsSystemv() && !defined(TINY) _init_systemfive_syscall: mov __NR_msyscall,%eax # syscall origin protect diff --git a/test/libc/calls/sigaction_test.c b/test/libc/calls/sigaction_test.c index 26ff2bd07db..6157d0cbb35 100644 --- a/test/libc/calls/sigaction_test.c +++ b/test/libc/calls/sigaction_test.c @@ -21,6 +21,7 @@ #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/siginfo.h" #include "libc/calls/struct/sigset.h" +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/ucontext.h" #include "libc/dce.h" #include "libc/errno.h" @@ -204,3 +205,9 @@ TEST(sigaction, autoZombieSlayer) { // clean up ASSERT_SYS(0, 0, sigaction(SIGCHLD, &sa, 0)); } + +TEST(sigaction, enosys_returnsErrnoRatherThanSigsysByDefault) { + if (IsTiny()) return; // systemfive.S disables the fix w/ tiny + if (IsOpenbsd()) return; // TODO: Why does OpenBSD raise SIGABRT? + ASSERT_SYS(ENOSYS, -1, sys_bogus()); +} diff --git a/tool/emacs/cosmo-cpp-constants.el b/tool/emacs/cosmo-cpp-constants.el index 61dad14d7d6..73abc1bd640 100644 --- a/tool/emacs/cosmo-cpp-constants.el +++ b/tool/emacs/cosmo-cpp-constants.el @@ -16,6 +16,11 @@ "__GNUC_PATCHLEVEL__" "__GNUC__" "__APPLE__" + "__FreeBSD__" + "__NetBSD__" + "__NetBSD_Version__" + "__OpenBSD__" + "__linux" "__MACH__" "__GNUG__" "__INCLUDE_LEVEL__" diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 4ca66dc97b9..20bdc089d22 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -54,6 +54,7 @@ #include "libc/nexgen32e/crc32.h" #include "libc/nexgen32e/nt2sysv.h" #include "libc/nexgen32e/rdtsc.h" +#include "libc/nexgen32e/vendor.internal.h" #include "libc/nexgen32e/x86feature.h" #include "libc/nt/enum/fileflagandattributes.h" #include "libc/nt/thread.h" @@ -7259,6 +7260,8 @@ static void TlsDestroy(void) { static void GetOpts(int argc, char *argv[]) { int opt; bool storeasset = false; + // only generate ecp cert under blinkenlights (rsa is slow) + norsagen = IsGenuineCosmo(); while ((opt = getopt(argc, argv, GETOPTS)) != -1) { switch (opt) { CASE('S', ++sandboxed);