mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
feat(native): run native sessions under a device's own app seccomp filter for testing
This commit is contained in:
1 parent
049da87a67
commit
0e56b54a69
5 files changed
+105
-7
No files matched your search
@@ -0,0 +1,50 @@
|
||||
// Writes the seccomp filter Android's zygote gives an app (from the device's libandroid_runtime.so)
|
||||
// to a file, as the raw sock_filter array, for seccomp-sim -f on devices whose zygote skips it
|
||||
// (a permissive SELinux). A bionic program, built with the NDK and run from adb shell:
|
||||
// aarch64-linux-android33-clang -O2 -rdynamic -o app-seccomp-dump app-seccomp-dump.c -ldl
|
||||
// app-seccomp-dump OUT
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <linux/filter.h>
|
||||
#include <linux/seccomp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char *g_out;
|
||||
static int g_dumped;
|
||||
|
||||
// the runtime's own prctl() call lands here first
|
||||
__attribute__((visibility("default"))) int prctl(int option, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, option);
|
||||
unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long);
|
||||
unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long);
|
||||
va_end(ap);
|
||||
if (option == PR_SET_SECCOMP && a2 == SECCOMP_MODE_FILTER && g_out) {
|
||||
const struct sock_fprog *prog = (const struct sock_fprog *)a3;
|
||||
FILE *f = fopen(g_out, "wb");
|
||||
if (f) {
|
||||
fwrite(prog->filter, sizeof(prog->filter[0]), prog->len, f);
|
||||
fclose(f);
|
||||
fprintf(stderr, "app-seccomp-dump: %u instructions to %s\n", prog->len, g_out);
|
||||
g_dumped = 1;
|
||||
}
|
||||
return 0; // not installed in this process
|
||||
}
|
||||
return (int)syscall(SYS_prctl, option, a2, a3, a4, a5);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) { fprintf(stderr, "usage: %s OUT\n", argv[0]); return 2; }
|
||||
g_out = argv[1];
|
||||
void *rt = dlopen("libandroid_runtime.so", RTLD_NOW);
|
||||
if (!rt) { fprintf(stderr, "%s\n", dlerror()); return 2; }
|
||||
int (*set_app_filter)(void) = (int (*)(void))dlsym(rt, "_Z22set_app_seccomp_filterv");
|
||||
if (!set_app_filter) { fprintf(stderr, "no set_app_seccomp_filter\n"); return 2; }
|
||||
set_app_filter();
|
||||
return g_dumped ? 0 : 1;
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
// Runs a program under a seccomp filter that answers the system calls Android's app policy blocks
|
||||
// with SIGSYS, the way a stock app process does, for testing on builds that have no filter:
|
||||
// seccomp-sim [-a NR,NR...] PROGRAM [ARGS...]
|
||||
// -a leaves the listed system calls allowed (to find which one kills a program).
|
||||
// seccomp-sim -f FILTER PROGRAM [ARGS...]
|
||||
// -a leaves the listed system calls allowed (to find which one kills a program). -f installs a
|
||||
// filter as it is (raw struct sock_filter entries), such as the app filter a device's zygote makes.
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <linux/audit.h>
|
||||
@@ -19,7 +21,32 @@ static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 14
|
||||
#define MAX_ALLOWED 64
|
||||
#define NBLOCKED (sizeof(blocked) / sizeof(blocked[0]))
|
||||
|
||||
static int run_filter_file(const char *path, char **argv) {
|
||||
static struct sock_filter filter[BPF_MAXINSNS];
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
perror("seccomp-sim: -f");
|
||||
return 2;
|
||||
}
|
||||
size_t n = fread(filter, sizeof(filter[0]), BPF_MAXINSNS, f);
|
||||
int extra = fgetc(f) != EOF;
|
||||
fclose(f);
|
||||
if (n == 0 || extra) {
|
||||
fprintf(stderr, "seccomp-sim: %s holds no filter of at most %d instructions\n", path, BPF_MAXINSNS);
|
||||
return 2;
|
||||
}
|
||||
struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter };
|
||||
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
|
||||
perror("seccomp-sim");
|
||||
return 1;
|
||||
}
|
||||
execv(argv[0], argv);
|
||||
perror("seccomp-sim: exec");
|
||||
return 127;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc > 3 && strcmp(argv[1], "-f") == 0) return run_filter_file(argv[2], argv + 3);
|
||||
int skip[MAX_ALLOWED], nskip = 0, arg = 1;
|
||||
if (argc > 3 && strcmp(argv[1], "-a") == 0) {
|
||||
for (char *p = argv[2];; p++) {
|
||||
|
||||
Reference in new issue
Block a user