From e6008a2ae961851be803de80557d877db178248b Mon Sep 17 00:00:00 2001 From: Frajo Haider Date: Tue, 10 Jan 2023 00:31:30 +0000 Subject: [PATCH] [sb2] Allow redirecting syscalls (not all are supported yet). JB#59837 --- preload/Makefile | 1 + preload/interface.master | 2 ++ preload/syscalls.c | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 preload/syscalls.c diff --git a/preload/Makefile b/preload/Makefile index 149ec22a..fd891c5e 100644 --- a/preload/Makefile +++ b/preload/Makefile @@ -11,6 +11,7 @@ objs := wrappers.o privatewrappers.o \ fdpathdb.o procfs.o mempcpy.o \ union_dirs.o \ system.o \ + syscalls.o \ sb2context.o ifeq ($(shell uname -s),Linux) diff --git a/preload/interface.master b/preload/interface.master index 6ffd7d4e..d989dd2c 100644 --- a/preload/interface.master +++ b/preload/interface.master @@ -111,6 +111,8 @@ GATE: int execve (const char *filename, char *const argv [], char *const envp[]) GATE: int execvp (const char *file, char *const argv []) GATE: int execvpe(const char *file, char *const argv[], char *const envp[]) +GATE: long syscall(long number, ...) : pass_va_list + GATE: int posix_spawn (pid_t *pid, const char *path, \ const posix_spawn_file_actions_t *file_actions, \ const posix_spawnattr_t *attrp, \ diff --git a/preload/syscalls.c b/preload/syscalls.c new file mode 100644 index 00000000..86fe6016 --- /dev/null +++ b/preload/syscalls.c @@ -0,0 +1,60 @@ +/* + * + * SPDX-FileCopyrightText: 2023, Jolla Ltd. + * SPDX-License-Identifier: LGPL-2.1 + * + * syscallgate -- syscall() GATE for the scratchbox2 preload library + * + * Copyright (C) 2023 Jolla Ltd. + */ + +#include +#include +#include + +#include "libsb2.h" +#include "exported.h" + +#define FORWARD_TO_WRAPPER(sysc, ...) \ + case SYS_ ## sysc: { \ + ret = sysc(__VA_ARGS__); \ + } break + +extern long syscall_gate(int *result_errno_ptr, + long (*real_syscall_ptr)(long number, ...), + const char *realfnname, long number, va_list args) +{ + long arg0 = va_arg(args, long); + long arg1 = va_arg(args, long); + long arg2 = va_arg(args, long); + long arg3 = va_arg(args, long); + long arg4 = va_arg(args, long); + long arg5 = va_arg(args, long); + + long ret; + switch (number) { + FORWARD_TO_WRAPPER(execve, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(openat, arg0, arg1, arg2, arg3); + // FORWARD_TO_WRAPPER(open_by_handle_at, ...) + FORWARD_TO_WRAPPER(open, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(connect, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(sendto, arg0, arg1, arg2, arg3, arg4, arg5); + FORWARD_TO_WRAPPER(sendmsg, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(recvfrom, arg0, arg1, arg2, arg3, (struct sockaddr *)arg4, arg5); + FORWARD_TO_WRAPPER(recvmsg, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(accept, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(accept4, arg0, arg1, arg2, arg3); + FORWARD_TO_WRAPPER(utimensat, arg0, arg1, arg2, arg3); + FORWARD_TO_WRAPPER(utime, arg0, arg1); + FORWARD_TO_WRAPPER(utimes, arg0, arg1); + FORWARD_TO_WRAPPER(fcntl, arg0, arg1, arg2); + FORWARD_TO_WRAPPER(futimesat, arg0, arg1, arg2); + default: { + ret = real_syscall_ptr(number, arg0, arg1, arg2, arg3, arg4, arg5); + break; + } + }; + if (result_errno_ptr) *result_errno_ptr = errno; + return ret; +} +