Skip to content

Commit

Permalink
[sb2] Allow redirecting syscalls (not all are supported yet). JB#59837
Browse files Browse the repository at this point in the history
  • Loading branch information
krnlyng committed Jan 12, 2023
1 parent 5373beb commit e6008a2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions preload/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions preload/interface.master
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
60 changes: 60 additions & 0 deletions preload/syscalls.c
Original file line number Diff line number Diff line change
@@ -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 <asm-generic/unistd.h>
#include <sys/syscall.h>
#include <unistd.h>

#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;
}

0 comments on commit e6008a2

Please sign in to comment.