@@ -135,6 +135,7 @@ $(LIBPSEUDO): $(WRAPOBJS) pseudo_client.o pseudo_client_scanf.o pseudo_ipc.o $(S
$(OBJS): pseudo_tables.h
pseudo_client.o pseudo_server.o pseudo_ipc.o: pseudo_ipc.h
+pseudo_wrappers.o: ports/linux/pseudo_io_uring_seccomp.c ports/linux/io_uring_syscalls.h
pseudo_client.o: pseudo_client.h
new file mode 100644
@@ -0,0 +1,72 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * io_uring syscall numbers. Prefer the host/SDK sysroot definitions: pseudo
+ * native and nativesdk builds may execute on different architectures.
+ */
+#ifndef PSEUDO_IO_URING_SYSCALLS_H
+#define PSEUDO_IO_URING_SYSCALLS_H
+
+#include <sys/syscall.h>
+
+#ifndef SYS_io_uring_setup
+# ifdef __NR_io_uring_setup
+# define SYS_io_uring_setup __NR_io_uring_setup
+# endif
+#endif
+#ifndef SYS_io_uring_enter
+# ifdef __NR_io_uring_enter
+# define SYS_io_uring_enter __NR_io_uring_enter
+# endif
+#endif
+#ifndef SYS_io_uring_register
+# ifdef __NR_io_uring_register
+# define SYS_io_uring_register __NR_io_uring_register
+# endif
+#endif
+
+/* Linux headers older than 5.1 do not define the io_uring calls. Supply
+ * fallbacks only for ABIs whose assignments are known. In particular, Alpha
+ * has its own numbers, MIPS adds its ABI base, and x32 adds its syscall bit.
+ */
+#if !defined(SYS_io_uring_setup) || !defined(SYS_io_uring_enter) || !defined(SYS_io_uring_register)
+# if defined(__alpha__)
+# define PSEUDO_NR_io_uring_setup 535
+# define PSEUDO_NR_io_uring_enter 536
+# define PSEUDO_NR_io_uring_register 537
+# elif defined(__mips__) && defined(__NR_Linux)
+# define PSEUDO_NR_io_uring_setup (__NR_Linux + 425)
+# define PSEUDO_NR_io_uring_enter (__NR_Linux + 426)
+# define PSEUDO_NR_io_uring_register (__NR_Linux + 427)
+# elif defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
+# define PSEUDO_NR_io_uring_setup (__X32_SYSCALL_BIT + 425)
+# define PSEUDO_NR_io_uring_enter (__X32_SYSCALL_BIT + 426)
+# define PSEUDO_NR_io_uring_register (__X32_SYSCALL_BIT + 427)
+# elif defined(__i386__) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(__arm__) || defined(__aarch64__) || defined(__riscv) || defined(__powerpc__) || defined(__s390__) || defined(__sparc__) || defined(__m68k__) || defined(__hppa__) || defined(__sh__) || defined(__loongarch__) || defined(__arc__) || defined(__csky__) || defined(__hexagon__) || defined(__microblaze__) || defined(__nios2__) || defined(__or1k__) || defined(__xtensa__)
+# define PSEUDO_NR_io_uring_setup 425
+# define PSEUDO_NR_io_uring_enter 426
+# define PSEUDO_NR_io_uring_register 427
+# endif
+#endif
+
+#ifndef SYS_io_uring_setup
+# ifdef PSEUDO_NR_io_uring_setup
+# define SYS_io_uring_setup PSEUDO_NR_io_uring_setup
+# endif
+#endif
+#ifndef SYS_io_uring_enter
+# ifdef PSEUDO_NR_io_uring_enter
+# define SYS_io_uring_enter PSEUDO_NR_io_uring_enter
+# endif
+#endif
+#ifndef SYS_io_uring_register
+# ifdef PSEUDO_NR_io_uring_register
+# define SYS_io_uring_register PSEUDO_NR_io_uring_register
+# endif
+#endif
+
+#if defined(SYS_io_uring_setup) && defined(SYS_io_uring_enter) && defined(SYS_io_uring_register)
+#define PSEUDO_HAVE_IO_URING_SYSCALLS 1
+#endif
+
+#endif
@@ -110,3 +110,5 @@ struct mount_attr;
#define SYS_open_tree_attr __NR_open_tree_attr
#endif
+
+#include "io_uring_syscalls.h"
new file mode 100644
@@ -0,0 +1,363 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Kernel-enforced io_uring refusal for pseudo.
+ *
+ * An LD_PRELOAD wrapper can only see callers which go through libc. The two
+ * most common io_uring users do not: liburing is normally built with
+ * CONFIG_NOLIBC and emits syscall instructions of its own, and fio inlines a
+ * syscall instruction on its submission path. Those callers reach the kernel
+ * directly, which means they hold a real ring and can create, rename or
+ * unlink files without pseudo recording any of it - and wrong ownership baked
+ * into an image is far harder to notice than a failure would have been.
+ *
+ * There is nothing to wrap, either: glibc exports no io_uring entry point at
+ * all, so the kernel is the only place the question can be answered. A
+ * seccomp filter marks io_uring_setup(), io_uring_enter() and
+ * io_uring_register() and answers them with ENOSYS, which is what a kernel
+ * built without io_uring says and what every caller worth worrying about
+ * already has a fallback for. The filter is inherited across fork() and
+ * execve(), so one installation covers an entire build, including the
+ * processes libpseudo cannot be loaded into.
+ *
+ * Two modes, chosen by environment variable: deny (the default) and
+ * passthrough. See the mode enum below. The mode only
+ * decides whether the filter is installed; pseudo's syscall() wrapper refuses
+ * io_uring for callers which go through libc either way.
+ *
+ * no_new_privs is a precondition for installing a filter without privilege,
+ * and this never sets it: doing so would suppress setuid and file
+ * capabilities for the process and everything it execs, which in a build
+ * stops a sysroot's own setuid helpers working, and that is not a decision
+ * pseudo should make on an application's behalf. Where it is unset and
+ * pseudo is unprivileged, no filter can be installed, and callers which make
+ * the system call themselves are not refused.
+ *
+ * The mode in force, and anything which went wrong installing the filter,
+ * are reported only in the debug output (PSEUDO_DEBUG=c). pseudo is preloaded
+ * into every process a build runs, and none of this stops pseudo working, so
+ * it must not end up in the output of programs whose output is captured.
+ */
+
+#include <stddef.h>
+#include <sys/prctl.h>
+#include <linux/filter.h>
+#include <linux/seccomp.h>
+
+#ifndef SECCOMP_MODE_FILTER
+#define SECCOMP_MODE_FILTER 2
+#endif
+#ifndef SECCOMP_SET_MODE_FILTER
+#define SECCOMP_SET_MODE_FILTER 1
+#endif
+#ifndef SECCOMP_FILTER_FLAG_TSYNC
+#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
+#endif
+#ifndef SECCOMP_FILTER_FLAG_LOG
+#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
+#endif
+#ifndef SECCOMP_RET_ALLOW
+#define SECCOMP_RET_ALLOW 0x7fff0000U
+#endif
+#ifndef SECCOMP_RET_ERRNO
+#define SECCOMP_RET_ERRNO 0x00050000U
+#endif
+#ifndef SECCOMP_RET_DATA
+#define SECCOMP_RET_DATA 0x0000ffffU
+#endif
+#ifndef PR_GET_NO_NEW_PRIVS
+#define PR_GET_NO_NEW_PRIVS 39
+#endif
+
+/* AUDIT_ARCH_* for the ABI this copy of libpseudo was built for. There is no
+ * value meaning "whatever this is", so the ABI still has to be named, but the
+ * number behind the name comes from the sysroot: one table fewer to keep
+ * right, and a new architecture is two lines rather than a constant to look
+ * up and get wrong.
+ *
+ * A sysroot without <linux/audit.h>, or with one too old to name this ABI,
+ * leaves PSEUDO_SECCOMP_AUDIT_ARCH undefined - as does an architecture missing
+ * below - and then no filter is installed and io_uring reaches the kernel on
+ * it unexamined.
+ *
+ * Endianness does not come into it, which is worth saying because the
+ * constants suggest it should. <linux/audit.h> defines AUDIT_ARCH_ARMEB
+ * beside AUDIT_ARCH_ARM, but nothing in the kernel ever answers with it:
+ * arch/arm's syscall_get_arch() returns AUDIT_ARCH_ARM for every arm task -
+ * "ARM tasks don't change audit architectures on the fly" - and its
+ * SECCOMP_ARCH_NATIVE is that same value, while an arm task under an aarch64
+ * kernel is reported as AUDIT_ARCH_ARM as well. aarch64 has the one value
+ * for both endiannesses too. So a big-endian build matches on the same
+ * constant as a little-endian one, and there is nothing to exclude.
+ */
+#if !defined(__has_include) || __has_include(<linux/audit.h>)
+#include <linux/audit.h>
+#endif
+
+#if defined(__x86_64__) && !defined(__ILP32__) && defined(AUDIT_ARCH_X86_64)
+#define PSEUDO_SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
+#elif defined(__i386__) && defined(AUDIT_ARCH_I386)
+#define PSEUDO_SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
+#elif defined(__aarch64__) && defined(AUDIT_ARCH_AARCH64)
+#define PSEUDO_SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
+#elif defined(__arm__) && defined(AUDIT_ARCH_ARM)
+#define PSEUDO_SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
+#elif defined(__riscv) && __riscv_xlen == 64 && defined(AUDIT_ARCH_RISCV64)
+#define PSEUDO_SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64
+#endif
+
+/* The variable which picks the mode, and the marker pseudo carries in its
+ * own environment. The marker stops the thousands of processes in
+ * a build each stacking another copy, since every filter in the chain is
+ * evaluated on every syscall the process makes. pseudo re-exports its own
+ * variables across exec, including for callers that build their own
+ * environment, so these survive where a plain setenv() would not.
+ */
+#define PSEUDO_IO_URING_MODE_VAR "PSEUDO_IO_URING_SECCOMP"
+#define PSEUDO_IO_URING_FILTER_MARKER "PSEUDO_IO_URING_FILTER"
+
+/* How this process treats io_uring.
+ *
+ * Deny is the default. A build does not need io_uring; it needs pseudo to see
+ * the file operations. Refusing the syscalls outright costs a caller the
+ * fallback path it already has, and costs pseudo nothing.
+ *
+ * PSEUDO_IO_URING_SECCOMP=0 passthrough: no filter is installed, so
+ * callers which make the system call
+ * themselves get a working io_uring, and
+ * pseudo does not see what they do with it.
+ * PSEUDO_IO_URING_SECCOMP=1 deny, and the default: io_uring_setup()
+ * answers ENOSYS and callers take the fallback
+ * they already have.
+ *
+ * The values are the numbers the variable is set to, so that a mode can be
+ * added without renumbering the ones already documented.
+ */
+enum pseudo_io_uring_mode {
+ PSEUDO_IO_URING_MODE_PASSTHROUGH = 0,
+ PSEUDO_IO_URING_MODE_DENY = 1,
+ PSEUDO_IO_URING_MODE_LAST = PSEUDO_IO_URING_MODE_DENY
+};
+
+#ifdef PSEUDO_SECCOMP_AUDIT_ARCH
+static const char *
+pseudo_io_uring_mode_name(enum pseudo_io_uring_mode mode)
+{
+ switch (mode) {
+ case PSEUDO_IO_URING_MODE_PASSTHROUGH:
+ return "passthrough";
+ default:
+ return "deny";
+ }
+}
+
+/* The mode asked for by the environment, or by the value pseudo carried
+ * across an exec for a caller which built its own environment.
+ */
+static enum pseudo_io_uring_mode
+pseudo_io_uring_wanted_mode(void)
+{
+ enum pseudo_io_uring_mode mode = PSEUDO_IO_URING_MODE_DENY;
+ char *env = getenv(PSEUDO_IO_URING_MODE_VAR);
+ char *owned = NULL;
+ char *end;
+ long value;
+
+ if (env)
+ pseudo_set_value(PSEUDO_IO_URING_MODE_VAR, env);
+ else
+ env = owned = pseudo_get_value(PSEUDO_IO_URING_MODE_VAR);
+ if (!env)
+ return mode;
+
+ value = strtol(env, &end, 10);
+ if (end != env && !*end && value >= 0 &&
+ value <= PSEUDO_IO_URING_MODE_LAST) {
+ mode = (enum pseudo_io_uring_mode) value;
+ } else {
+ /* Worth saying, because the mode which was asked for is not
+ * the mode which is in force. */
+ pseudo_debug(PDBGF_CLIENT, "io_uring: %s=%s is not a mode; using %s\n",
+ PSEUDO_IO_URING_MODE_VAR, env,
+ pseudo_io_uring_mode_name(mode));
+ }
+ free(owned);
+ return mode;
+}
+
+/* Has some ancestor already installed the filter? Asking the kernel would
+ * mean issuing an io_uring syscall, which is fatal under an unrelated filter
+ * whose action is SECCOMP_RET_KILL, so the answer is carried in pseudo's own
+ * environment instead.
+ */
+static int
+pseudo_io_uring_filter_present(void)
+{
+ char *env = getenv(PSEUDO_IO_URING_FILTER_MARKER);
+ int present;
+
+ if (env) {
+ pseudo_set_value(PSEUDO_IO_URING_FILTER_MARKER, env);
+ return 1;
+ }
+ env = pseudo_get_value(PSEUDO_IO_URING_FILTER_MARKER);
+ present = env != NULL;
+ free(env);
+ return present;
+}
+
+/* Why a filter could not be installed, almost always.
+ *
+ * Installing one requires either privilege or no_new_privs, and this never
+ * sets the latter: it suppresses setuid and file capabilities for the process
+ * and everything it execs, which in a build stops a sysroot's own setuid
+ * helpers working, and that is not a decision pseudo should make on an
+ * application's behalf. So where it is unset and pseudo is unprivileged,
+ * deny cannot be enforced and says so. */
+static int
+pseudo_io_uring_no_new_privs(void)
+{
+ return real_prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) == 1;
+}
+
+/* The three syscalls, answered with ENOSYS in the kernel, so nothing about
+ * io_uring reaches this process at all: io_uring_setup() fails the way it does
+ * on a kernel built without it.
+ */
+static int
+pseudo_io_uring_install_filter(void)
+{
+ struct sock_filter filter[] = {
+ /* Only the native ABI is decoded below, so anything else is
+ * left to the kernel. */
+ BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
+ offsetof(struct seccomp_data, arch)),
+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, PSEUDO_SECCOMP_AUDIT_ARCH, 0, 4),
+ BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
+ offsetof(struct seccomp_data, nr)),
+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_io_uring_setup, 3, 0),
+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_io_uring_enter, 2, 0),
+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_io_uring_register, 1, 0),
+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
+ BPF_STMT(BPF_RET | BPF_K,
+ SECCOMP_RET_ERRNO | (ENOSYS & SECCOMP_RET_DATA)),
+ };
+ struct sock_fprog prog = {
+ .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
+ .filter = filter,
+ };
+
+ /* real_syscall, because pseudo's own wrappers answer seccomp requests
+ * with a polite lie so that applications cannot sandbox away the
+ * wrappers they are supposed to be using. */
+#ifdef SYS_seccomp
+ /* What to ask for, in descending order of preference.
+ *
+ * TSYNC applies the filter to every thread in the process, which is
+ * what the marker recorded on success is a claim about. LOG has the
+ * kernel record each refusal where an operator can find it - the audit
+ * log, or the kernel ring buffer where nothing is listening to audit -
+ * and changes nothing about what the filter does.
+ *
+ * Neither is insisted on, because a sandbox can implement seccomp
+ * without implementing every flag of it: gVisor answers EINVAL to LOG,
+ * with or without TSYNC beside it, as does a kernel older than 4.14.
+ * Refusing to install over that would trade the thing which denies
+ * io_uring for a flag which only describes it, so a rejected flag is
+ * dropped and the filter goes in anyway. TSYNC is given up last of
+ * the two, being a property of the filter rather than of the record of
+ * it.
+ *
+ * Where TSYNC is given up, the call can also fail by answering with a
+ * sibling thread's id rather than -1, which is why only zero is taken
+ * for success. What is installed then covers this thread and whatever
+ * it goes on to create, and not that sibling or the children the
+ * sibling execs - which will find the marker and decide they need no
+ * filter of their own. Partial coverage recorded as whole-process
+ * coverage, in other words, and it is left that way because the path
+ * is not reachable the way pseudo is loaded: the constructor runs
+ * before main() in the single thread a process starts with, so there
+ * is no sibling to disagree. Getting there takes libpseudo dlopen'd
+ * into a running process whose threads have sandboxed themselves
+ * apart, and the thread which asked is still better off filtered than
+ * not.
+ */
+ static const unsigned long flag_sets[] = {
+ SECCOMP_FILTER_FLAG_TSYNC | SECCOMP_FILTER_FLAG_LOG,
+ SECCOMP_FILTER_FLAG_TSYNC,
+ SECCOMP_FILTER_FLAG_LOG,
+ 0
+ };
+ unsigned int i;
+
+ for (i = 0; i < sizeof(flag_sets) / sizeof(flag_sets[0]); ++i) {
+ if (real_syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER,
+ flag_sets[i], &prog) == 0)
+ return 0;
+ }
+#endif
+ /* Kernels without seccomp(2) have no flags to ask for at all, so this
+ * one is per-thread and unlogged whatever the circumstances. */
+ return real_prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
+}
+#endif /* PSEUDO_SECCOMP_AUDIT_ARCH */
+
+/* Called from _libpseudo_init(), so: once per process, after the real_*
+ * pointers and the client are set up, and again in every process that execs.
+ */
+static void
+pseudo_io_uring_init(void)
+{
+#ifdef PSEUDO_SECCOMP_AUDIT_ARCH
+ static int done = 0;
+ enum pseudo_io_uring_mode mode;
+
+ if (done)
+ return;
+ done = 1;
+
+ /* PSEUDO_DISABLED means "stay out of the way", which is the same
+ * answer as asking for passthrough. */
+ if (pseudo_disabled)
+ return;
+
+ mode = pseudo_io_uring_wanted_mode();
+
+ if (mode == PSEUDO_IO_URING_MODE_PASSTHROUGH) {
+ /* Nothing installed and nothing asked of the kernel, so there
+ * is nothing to report but the choice itself. */
+ if (pseudo_io_uring_filter_present())
+ /* An ancestor's filter cannot be removed, so the
+ * request cannot be honoured: io_uring stays refused.
+ * Worth saying, because the caller asked for one thing
+ * and is getting another. */
+ pseudo_debug(PDBGF_CLIENT, "io_uring: passthrough requested but a filter is inherited; io_uring calls will fail with ENOSYS\n");
+ else
+ pseudo_debug(PDBGF_CLIENT, "io_uring: passthrough (io_uring calls made without syscall() are not refused)\n");
+ return;
+ }
+
+ /* Already in force for this whole process tree, and a second copy
+ * would only cost every syscall the process makes. */
+ if (pseudo_io_uring_filter_present()) {
+ pseudo_debug(PDBGF_CLIENT, "io_uring: deny (filter inherited)\n");
+ return;
+ }
+
+ if (pseudo_io_uring_install_filter() < 0) {
+ int save_errno = errno;
+
+ /* Nearly always the precondition rather than anything
+ * exotic, so it is named: a filter can be installed either
+ * with privilege or with no_new_privs, and pseudo has neither
+ * to offer. */
+ pseudo_debug(PDBGF_CLIENT, "io_uring: deny requested but no filter could be installed (%s%s); io_uring calls made without syscall() will go unrecorded\n",
+ strerror(save_errno),
+ pseudo_io_uring_no_new_privs() ? "" :
+ "; no_new_privs is unset");
+ return;
+ }
+ pseudo_set_value(PSEUDO_IO_URING_FILTER_MARKER, "1");
+ pseudo_debug(PDBGF_CLIENT, "io_uring: deny\n");
+#endif
+}
@@ -169,6 +169,10 @@ out:
return fd;
}
+#ifdef PSEUDO_HAVE_IO_URING_SYSCALLS
+#include "pseudo_io_uring_seccomp.c"
+#endif
+
long
syscall(long number, ...) {
long rc = -1;
@@ -624,6 +624,85 @@ discarded silently, even when
.B PSEUDO_DISABLED
is set. The value specified doesn't matter.
.TP 8
+.B PSEUDO_IO_URING_SECCOMP
+Selects whether
+.I pseudo
+installs a seccomp filter refusing
+.IR io_uring (7).
+.I pseudo
+cannot intercept the file operations done through io_uring, so it always
+answers
+.IR io_uring_setup (2),
+.IR io_uring_enter (2)
+and
+.IR io_uring_register (2)
+made through
+.IR syscall (3)
+with
+.BR ENOSYS ,
+whatever this variable is set to. But it is an
+.I LD_PRELOAD
+library, and the common io_uring users do not go through libc - liburing is
+normally built with
+.I CONFIG_NOLIBC
+and issues the system calls itself, and
+.IR fio (1)
+inlines one on its submission path. Such a caller reaches the kernel directly
+and can create, rename or unlink files without
+.I pseudo
+recording any of it. There is also nothing to wrap, since libc exports no
+io_uring entry point, so a seccomp filter is used instead. The filter is
+inherited across
+.IR fork (2)
+and
+.IR execve (2),
+so one process tree needs it installed only once, including for the processes
+.I pseudo
+cannot be loaded into at all.
+.IP
+.I 0
+means passthrough: no filter is installed, so callers which make the system
+calls themselves get a working io_uring, and
+.I pseudo
+does not see the file operations they perform through it.
+.IP
+.I 1
+means deny, and is the default. The filter answers
+.IR io_uring_setup (2),
+.IR io_uring_enter (2)
+and
+.IR io_uring_register (2)
+with
+.BR ENOSYS ,
+which is what a kernel built without io_uring support answers, so callers take
+the fallback they already have for that case. Where the kernel supports
+.BR SECCOMP_FILTER_FLAG_LOG ,
+each refused call is also recorded by the kernel, in the audit log or in the
+kernel ring buffer where nothing is listening to audit. A kernel or sandbox
+which rejects that flag still gets the filter, without the records.
+.IP
+Installing a filter requires either privilege or
+.BR PR_SET_NO_NEW_PRIVS ,
+which suppresses setuid binaries and file capabilities for the process and
+everything it executes.
+.I pseudo
+never sets it, since that is not its decision to make on an application's
+behalf: where it is unset and
+.I pseudo
+is unprivileged, no filter can be installed, and callers which make the
+system call directly, rather than through
+.IR syscall (3),
+are not refused.
+.IP
+The mode, and any problem installing the filter, are reported only in the
+debug output selected by the
+.B c
+flag of
+.BR PSEUDO_DEBUG ,
+since none of them stops
+.I pseudo
+from working.
+.TP 8
.B PSEUDO_ENOSYS_ABORT
If this variable is set, the
.I pseudo
@@ -58,6 +58,8 @@ static struct pseudo_variables pseudo_env[] = {
{ "PSEUDO_DISABLED", 15, NULL },
{ "PSEUDO_UNLOAD", 13, NULL },
{ "PSEUDO_ALLOW_FSYNC", 18, NULL },
+ { "PSEUDO_IO_URING_SECCOMP", 23, NULL },
+ { "PSEUDO_IO_URING_FILTER", 22, NULL },
#ifdef PSEUDO_PROFILING
{ "PSEUDO_PROFILE_PATH", 19, NULL },
#endif
@@ -65,6 +65,12 @@ static void pseudo_sigblock(sigset_t *);
extern char *program_invocation_short_name;
static sigset_t pseudo_saved_sigmask;
+#if PSEUDO_PORT_LINUX && defined(PSEUDO_HAVE_IO_URING_SYSCALLS)
+/* Defined in ports/linux/pseudo_io_uring_seccomp.c, which is pulled in with
+ * the rest of the port's wrappers at the bottom of this file. */
+static void pseudo_io_uring_init(void);
+#endif
+
/* Constructor only exists in libpseudo */
static void _libpseudo_init(void) __attribute__ ((constructor));
@@ -115,6 +121,11 @@ _libpseudo_init(void) {
pseudo_init_wrappers();
pseudo_init_client();
+#if PSEUDO_PORT_LINUX && defined(PSEUDO_HAVE_IO_URING_SYSCALLS)
+ /* Needs the real_* pointers and pseudo_disabled, so it goes last. */
+ pseudo_io_uring_init();
+#endif
+
pseudo_magic();
pseudo_droplock();
}
new file mode 100644
@@ -0,0 +1,267 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Does deny deny, and does passthrough stay out of the way?
+ *
+ * Deny needs a filter, a filter needs no_new_privs, and pseudo will not set
+ * it on an application's behalf - so the test sets it and re-executes itself.
+ * The copy which comes back has libpseudo loaded afresh with the precondition
+ * met, and it is that copy which asks the kernel for a ring. Without that the
+ * test would only ever see the fallback on a machine where no_new_privs is
+ * unset, which is most of them.
+ *
+ * Everything which is not pseudo's doing is a skip rather than a failure: a
+ * kernel with no seccomp to install a filter into, an architecture pseudo has
+ * no audit value for, an io_uring which has been switched off. The one thing
+ * reported as a failure is a filter being in force and io_uring working
+ * anyway, because that is pseudo not keeping its word.
+ *
+ * pseudo's syscall() wrapper refuses io_uring in both modes, so both are
+ * checked with the system call made directly, as liburing does, which only
+ * the filter can see.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "../ports/linux/io_uring_syscalls.h"
+
+#define SKIP 77
+
+#ifndef PSEUDO_HAVE_IO_URING_SYSCALLS
+int
+main(void)
+{
+ return SKIP;
+}
+#else
+
+/* Old sysroots may not have them, and they are part of the kernel ABI. */
+#ifndef PR_SET_NO_NEW_PRIVS
+#define PR_SET_NO_NEW_PRIVS 38
+#endif
+#ifndef PR_GET_SECCOMP
+#define PR_GET_SECCOMP 21
+#endif
+
+/* io_uring_setup() needs somewhere to put what it negotiated, and the
+ * argument is rejected before it is written when the call is refused, so the
+ * size is all that has to be right. */
+struct params {
+ unsigned char opaque[120];
+};
+
+/* Make the system call without libc, so pseudo's syscall() wrapper can't
+ * answer it. Returns -1 and sets errno on failure, as syscall() does.
+ */
+#if defined(__x86_64__) && !defined(__ILP32__)
+#define HAVE_RAW_SYSCALL 1
+static long
+raw_syscall(long n, long a, long b, long c, long d, long e, long f)
+{
+ register long r10 __asm__("r10") = d;
+ register long r8 __asm__("r8") = e;
+ register long r9 __asm__("r9") = f;
+ long ret;
+
+ __asm__ volatile ("syscall"
+ : "=a"(ret)
+ : "a"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9)
+ : "rcx", "r11", "memory");
+ if (ret < 0 && ret > -4096) {
+ errno = -ret;
+ return -1;
+ }
+ return ret;
+}
+#elif defined(__aarch64__)
+#define HAVE_RAW_SYSCALL 1
+static long
+raw_syscall(long n, long a, long b, long c, long d, long e, long f)
+{
+ register long x8 __asm__("x8") = n;
+ register long x0 __asm__("x0") = a;
+ register long x1 __asm__("x1") = b;
+ register long x2 __asm__("x2") = c;
+ register long x3 __asm__("x3") = d;
+ register long x4 __asm__("x4") = e;
+ register long x5 __asm__("x5") = f;
+
+ __asm__ volatile ("svc 0"
+ : "+r"(x0)
+ : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
+ : "memory");
+ if (x0 < 0 && x0 > -4096) {
+ errno = -x0;
+ return -1;
+ }
+ return x0;
+}
+#elif defined(__riscv) && __riscv_xlen == 64
+#define HAVE_RAW_SYSCALL 1
+static long
+raw_syscall(long n, long a, long b, long c, long d, long e, long f)
+{
+ register long a7 __asm__("a7") = n;
+ register long a0 __asm__("a0") = a;
+ register long a1 __asm__("a1") = b;
+ register long a2 __asm__("a2") = c;
+ register long a3 __asm__("a3") = d;
+ register long a4 __asm__("a4") = e;
+ register long a5 __asm__("a5") = f;
+
+ __asm__ volatile ("ecall"
+ : "+r"(a0)
+ : "r"(a7), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5)
+ : "memory");
+ if (a0 < 0 && a0 > -4096) {
+ errno = -a0;
+ return -1;
+ }
+ return a0;
+}
+#endif
+
+#ifdef HAVE_RAW_SYSCALL
+/* The two calls which need no ring, asked for with a descriptor which is not
+ * one. A refusal is decided from the syscall number alone, so the arguments
+ * only have to be ones the kernel would not fault on. */
+static long
+io_uring_other(int which)
+{
+ if (which == 0)
+ return raw_syscall(SYS_io_uring_enter, -1, 0, 0, 0, 0, 0);
+ return raw_syscall(SYS_io_uring_register, -1, 0, 0, 0, 0, 0);
+}
+
+static const char *const other_name[] = {
+ "io_uring_enter", "io_uring_register"
+};
+
+static int
+check_deny(void)
+{
+ struct params p;
+ long rc;
+ int which;
+
+ /* io_uring_setup() first, because its answer is what says whether the
+ * filter is in force at all. */
+ memset(&p, 0, sizeof(p));
+ errno = 0;
+ rc = raw_syscall(SYS_io_uring_setup, 8, (long) &p, 0, 0, 0, 0);
+ if (rc >= 0) {
+ close((int) rc);
+ if (prctl(PR_GET_SECCOMP, 0, 0, 0, 0) != 2) {
+ /* No filter is in force, so this kernel or this
+ * architecture could not have one installed and there
+ * is nothing here to test. */
+ fprintf(stderr,
+ "test-io-uring-deny: no seccomp filter in force, skipping\n");
+ return SKIP;
+ }
+ fprintf(stderr,
+ "test-io-uring-deny: io_uring_setup was not denied\n");
+ return 1;
+ }
+ if (errno != ENOSYS) {
+ /* Refused, but not by pseudo: a kernel which never had
+ * io_uring, or one with it switched off. Deny's promise is
+ * kept either way and there is nothing to tell apart. */
+ fprintf(stderr,
+ "test-io-uring-deny: io_uring unavailable (errno %d), skipping\n",
+ errno);
+ return SKIP;
+ }
+
+ /* The filter is in force, so the other two have to answer the same
+ * way; anything else is pseudo covering only part of what it said. */
+ for (which = 0; which < 2; ++which) {
+ errno = 0;
+ rc = io_uring_other(which);
+ if (rc < 0 && errno == ENOSYS)
+ continue;
+ fprintf(stderr,
+ "test-io-uring-deny: %s was not denied (rc %ld, errno %d)\n",
+ other_name[which], rc, rc < 0 ? errno : 0);
+ return 1;
+ }
+ return 0;
+}
+
+static int
+check_passthrough(void)
+{
+ struct params p;
+ long rc;
+
+ memset(&p, 0, sizeof(p));
+ errno = 0;
+ rc = raw_syscall(SYS_io_uring_setup, 8, (long) &p, 0, 0, 0, 0);
+ if (rc >= 0) {
+ close((int) rc);
+ return 0;
+ }
+ /* ENOSYS from a filter an ancestor installed, which cannot be removed,
+ * or from a kernel which never had io_uring; EPERM or EACCES where it
+ * has been switched off. None of them is pseudo letting io_uring
+ * through, and none can be told from the others here. */
+ fprintf(stderr,
+ "test-io-uring-deny: io_uring unavailable (errno %d), skipping\n",
+ errno);
+ return SKIP;
+}
+#endif /* HAVE_RAW_SYSCALL */
+
+int
+main(int argc, char **argv)
+{
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s deny|passthrough\n", argv[0]);
+ return 1;
+ }
+ if (strcmp(argv[1], "passthrough") && strcmp(argv[1], "deny")) {
+ fprintf(stderr, "usage: %s deny|passthrough\n", argv[0]);
+ return 1;
+ }
+
+#ifndef HAVE_RAW_SYSCALL
+ fprintf(stderr,
+ "test-io-uring-deny: no direct system call for this architecture, skipping\n");
+ return SKIP;
+#else
+ if (!strcmp(argv[1], "passthrough"))
+ return check_passthrough();
+
+ /* Deny, and this is the first of the two passes: arrange the
+ * precondition and come back through libpseudo's constructor with it
+ * in place. */
+ if (argc < 3) {
+ char *args[4];
+
+ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
+ fprintf(stderr,
+ "test-io-uring-deny: no_new_privs unavailable (errno %d), skipping\n",
+ errno);
+ return SKIP;
+ }
+ args[0] = argv[0];
+ args[1] = argv[1];
+ args[2] = (char *) "installed";
+ args[3] = NULL;
+ execv(argv[0], args);
+ fprintf(stderr,
+ "test-io-uring-deny: cannot re-execute %s (errno %d), skipping\n",
+ argv[0], errno);
+ return SKIP;
+ }
+
+ return check_deny();
+#endif
+}
+#endif /* PSEUDO_HAVE_IO_URING_SYSCALLS */
new file mode 100755
@@ -0,0 +1,16 @@
+#!/bin/bash
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Deny is the default, so the first run says nothing about the mode. It sets
+# no_new_privs itself, so it is checked whatever the machine does about that,
+# and it decides the result: a skip here means this kernel or architecture has
+# no filter to put in force.
+./test/test-io-uring-deny deny
+case $? in 0);; 77) exit 255;; *) exit 1;; esac
+# Passthrough can only fail the test, not skip it. A filter installed by an
+# ancestor cannot be removed, so where something upstream of the test has
+# already denied io_uring - including pseudo itself, when it was started
+# somewhere no_new_privs was already set - passthrough is not reachable, and
+# the run above is what there was to check.
+PSEUDO_IO_URING_SECCOMP=0 ./test/test-io-uring-deny passthrough
+case $? in 0 | 77);; *) exit 1;; esac