From nobody Sun Feb 8 09:29:02 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C67BE1E1C09 for ; Wed, 30 Apr 2025 04:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745987272; cv=none; b=hckqUub8AYFF8PbikSCXjLz0cOXldTh81yn2tURNfodAJ5SMslr3zFcH6rftQrGb4EDVJz++YGrLiXUuW1N8rlhNA3YtWDG8YTr+b9GFl985NoYZxPAjw8istalE6PcMXy3KmVsHmBrfHJDHIYzwZA4mjO7IqxZC49isRJ0KU5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745987272; c=relaxed/simple; bh=9QLQzR2LZqRiGtzgc/CnL0hE02EicK9KVjH9YleUWoo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ppd/M7ULnIGNW+dUORNmFFPnp7CJ9W9T+w/C+Sk2HNa9GmJdqZefNTdcrBWQyhEh3Epv1XbaJlSOqOYd5WA67EOLSfmV/EgnwzrP5pY0YLVQoml/vR9+R20c4eLMxw6z5EqaZz77wMMzlwlPRKtOCDmdCwPk2qbgRGl278DyjK8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PguT0xP/; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PguT0xP/" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2241053582dso102570085ad.1 for ; Tue, 29 Apr 2025 21:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745987270; x=1746592070; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+Rda5w8ftnYQQ6W3q5FgOQJiq+Hj9EhDJE6pWtJC4Ow=; b=PguT0xP/YdQPkyo8MNDll/uHYZWKEDWopJS016hvHmIFcg46/rwaw8ar9ZrLeUP1mN YccaPwas3xlRG9Bbi0VKTx9HXkzelNHkmMmS4hNaIXaeSAwNd8dqJYJZZy0ZzWrcpqav z/trWt2qZwiYFSAgGz6vUJLYiyiqIUR+8MkDDDd0VKf9myD7iniUV9N2GafLh/xveWdP vnC6/rPnrXfHNsMemeDDke0hNOCHS/ncmib+lVb216AbXMHGNDPpoXJmcfb7BqQ1/ncF NRAJtesaFEXcT2Y44bW9boMu3DYg2hqPvy3ogCoQLkgkGuk+GyekFc/pVsWR4cUFFM6d yZWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745987270; x=1746592070; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+Rda5w8ftnYQQ6W3q5FgOQJiq+Hj9EhDJE6pWtJC4Ow=; b=qbckiz3jduD5twjE8uEvwAXmT5dh9akIz04RDZ6cPZ0bY27mKbzeULlH7kHvdxCxgu YPzRHW39BRsW2El9Me2fV2PlcZx2kG534W/25DBf08JPUSAhQvwU2ykzalYlLz5k/fmd OOuP8UONLdm9b1XwgdO9B4F2RkHV4jf6OORrR/GkieKDczmvwmn4VUFOGnRWk6j3RHnb hHQFTuh0c+dIDKPumZNXV0Gmhta08aBQ6jAgq/fBynGcMyP/ATMYdltbx0qURA4C7ktk 65zTlmN0em99X5Mx90TyN8M4RXlZQjPUbTtYQekk2DL9PTR7Si6kd3DqOXx05tMifpbL bzBg== X-Forwarded-Encrypted: i=1; AJvYcCXuqKVenEN2pPCrpMuf1oz7JK6MK8rveRO36jpRoMuZNotQXuR2uO6lWwXrQHJ+hjuzdPAfTRO9icmm7wg=@vger.kernel.org X-Gm-Message-State: AOJu0YxGwafPfSPVWvzwuCJrR3SE001wBONd6gabhPFYISoZp5+Yx+bz bLUNw0+eSem+o2nn5eAnH9NbWYfYtqXTgpo0wVYVTm3u3AtczsJ2 X-Gm-Gg: ASbGncsm0vJLU1Q6jrxYDjLu5PICXiiKjazEoSGo2VPc4bYnk6veFufemsgAzTKWxty m4ZWSUnCIwrYL8wJ4CfOAVFUap4Rn4eE/iIgiYJNe8+rS3xxctz9E5X3y/W8m3tnw/rwwiAWcgW 6V3SsbY+pnbXAtP4sRB2xMjaoGNRYcfzYo4Xww7mgQCjSgsGiq2leEkkz7IouZXkI0+7xxBO0MP 8SMOHD05s1Il8bDYII1ph6vLZ0GJNq+ZThj1+y3NlbLPVYsGrT7vs3TlZiuSXxjB2OvpYodlK3j Le+eQelV3CvUPQXhs0jmgNu1svTxRK1ejwlOvZP4TGnffnr5Yb6qNsNQ/nUqUEbW6LJtuuR266z mm222by27uqY= X-Google-Smtp-Source: AGHT+IFM9+qSbHZo6PrAllTRnzE6ZKUFkumQZ/B0AYyUyUFgvdPZvBGr40PMIlqIRsils5Ezf29wWA== X-Received: by 2002:a17:902:e84b:b0:223:5c33:56a8 with SMTP id d9443c01a7336-22df581e52cmr18188695ad.35.1745987269925; Tue, 29 Apr 2025 21:27:49 -0700 (PDT) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db50e7c1bsm112367585ad.107.2025.04.29.21.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 21:27:49 -0700 (PDT) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 6F158E9481C; Wed, 30 Apr 2025 13:27:47 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, linux-kernel@vger.kernel.org, Kenichi Yasukata Subject: [PATCH v8 05/12] um: nommu: seccomp syscalls hook Date: Wed, 30 Apr 2025 13:27:11 +0900 Message-ID: <1a00eb90cbfa24befe9bdd5416a8fb837881deae.1745980082.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This commit adds syscall hook with seccomp. Using seccomp raises SIGSYS to UML process, which is captured in the (UML) kernel, then jumps to the syscall entry point, __kernel_vsyscall, to hook the original syscall instructions. The SIGSYS signal is raised upon the execution from uml_reserved and high_physmem, which locates userspace memory. Signed-off-by: Hajime Tazaki Signed-off-by: Kenichi Yasukata --- arch/um/include/shared/kern_util.h | 2 + arch/um/include/shared/os.h | 10 +++ arch/um/kernel/um_arch.c | 3 + arch/um/nommu/Makefile | 3 + arch/um/nommu/os-Linux/Makefile | 7 +++ arch/um/nommu/os-Linux/signal.c | 16 +++++ arch/um/os-Linux/Makefile | 5 ++ arch/um/os-Linux/seccomp.c | 87 +++++++++++++++++++++++++++ arch/um/os-Linux/signal.c | 8 +++ arch/x86/um/nommu/Makefile | 2 +- arch/x86/um/nommu/os-Linux/Makefile | 6 ++ arch/x86/um/nommu/os-Linux/mcontext.c | 13 ++++ arch/x86/um/shared/sysdep/mcontext.h | 3 + 13 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 arch/um/nommu/Makefile create mode 100644 arch/um/nommu/os-Linux/Makefile create mode 100644 arch/um/nommu/os-Linux/signal.c create mode 100644 arch/um/os-Linux/seccomp.c create mode 100644 arch/x86/um/nommu/os-Linux/Makefile create mode 100644 arch/x86/um/nommu/os-Linux/mcontext.c diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/ke= rn_util.h index 00ca3e12fd9a..2c8c18bd5860 100644 --- a/arch/um/include/shared/kern_util.h +++ b/arch/um/include/shared/kern_util.h @@ -66,6 +66,8 @@ extern void segv_handler(int sig, struct siginfo *unused_= si, struct uml_pt_regs extern void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *= regs, void *mc); extern void fatal_sigsegv(void) __attribute__ ((noreturn)); +extern void sigsys_handler(int sig, struct siginfo *si, struct uml_pt_regs= *, + void *mc); =20 void um_idle_sleep(void); =20 diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 152a60080d5b..7f6703869dde 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -338,4 +338,14 @@ extern void um_trace_signals_off(void); /* time-travel */ extern void deliver_time_travel_irqs(void); =20 +/* seccomp.c */ +#ifdef CONFIG_MMU +static inline int os_setup_seccomp(void) +{ + return 0; +} +#else +extern int os_setup_seccomp(void); +#endif + #endif diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c index 7f050783885a..2b591e929eca 100644 --- a/arch/um/kernel/um_arch.c +++ b/arch/um/kernel/um_arch.c @@ -433,6 +433,9 @@ void __init setup_arch(char **cmdline_p) add_bootloader_randomness(rng_seed, sizeof(rng_seed)); memzero_explicit(rng_seed, sizeof(rng_seed)); } + + /* install seccomp filter */ + os_setup_seccomp(); } =20 void __init arch_cpu_finalize_init(void) diff --git a/arch/um/nommu/Makefile b/arch/um/nommu/Makefile new file mode 100644 index 000000000000..baab7c2f57c2 --- /dev/null +++ b/arch/um/nommu/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y :=3D os-Linux/ diff --git a/arch/um/nommu/os-Linux/Makefile b/arch/um/nommu/os-Linux/Makef= ile new file mode 100644 index 000000000000..68833c576437 --- /dev/null +++ b/arch/um/nommu/os-Linux/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y :=3D signal.o +USER_OBJS :=3D $(obj-y) + +include $(srctree)/arch/um/scripts/Makefile.rules +USER_CFLAGS+=3D-I$(srctree)/arch/um/os-Linux diff --git a/arch/um/nommu/os-Linux/signal.c b/arch/um/nommu/os-Linux/signa= l.c new file mode 100644 index 000000000000..19043b9652e2 --- /dev/null +++ b/arch/um/nommu/os-Linux/signal.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include + +void sigsys_handler(int sig, struct siginfo *si, + struct uml_pt_regs *regs, void *ptr) +{ + mcontext_t *mc =3D (mcontext_t *) ptr; + + /* hook syscall via SIGSYS */ + set_mc_sigsys_hook(mc); +} diff --git a/arch/um/os-Linux/Makefile b/arch/um/os-Linux/Makefile index 331564888400..6bfdfec8cbcc 100644 --- a/arch/um/os-Linux/Makefile +++ b/arch/um/os-Linux/Makefile @@ -21,4 +21,9 @@ USER_OBJS :=3D $(user-objs-y) elf_aux.o execvp.o file.o h= elper.o irq.o \ main.o mem.o process.o registers.o sigio.o signal.o start_up.o time.o \ tty.o umid.o util.o =20 +ifneq ($(CONFIG_MMU),y) +obj-y +=3D seccomp.o +USER_OBJS +=3D seccomp.o +endif + include $(srctree)/arch/um/scripts/Makefile.rules diff --git a/arch/um/os-Linux/seccomp.c b/arch/um/os-Linux/seccomp.c new file mode 100644 index 000000000000..d1cfa6e3d632 --- /dev/null +++ b/arch/um/os-Linux/seccomp.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include /* For SYS_xxx definitions */ +#include +#include +#include +#include +#include + +int __init os_setup_seccomp(void) +{ + int err; + unsigned long __userspace_start =3D uml_reserved, + __userspace_end =3D high_physmem; + + struct sock_filter filter[] =3D { + /* if (IP_high > __userspace_end) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JGT + BPF_K, __userspace_end >> 32, + /*true-skip=3D*/0, /*false-skip=3D*/1), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high =3D=3D __userspace_end && IP_low >=3D __userspace_end) al= low; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __userspace_end >> 32, + /*true-skip=3D*/0, /*false-skip=3D*/3), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer)), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_end, + /*true-skip=3D*/0, /*false-skip=3D*/1), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high < __userspace_start) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_start >> 32, + /*true-skip=3D*/1, /*false-skip=3D*/0), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high =3D=3D __userspace_start && IP_low < __userspace_start) a= llow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __userspace_start >> 32, + /*true-skip=3D*/0, /*false-skip=3D*/3), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer)), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_start, + /*true-skip=3D*/1, /*false-skip=3D*/0), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* other address; trap */ + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_TRAP), + }; + struct sock_fprog prog =3D { + .len =3D ARRAY_SIZE(filter), + .filter =3D filter, + }; + + err =3D prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + if (err) + os_warn("PR_SET_NO_NEW_PRIVS (err=3D%d, ernro=3D%d)\n", + err, errno); + + err =3D syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, + SECCOMP_FILTER_FLAG_TSYNC, &prog); + if (err) { + os_warn("SECCOMP_SET_MODE_FILTER (err=3D%d, ernro=3D%d)\n", + err, errno); + exit(1); + } + + set_handler(SIGSYS); + + os_info("seccomp: setup filter syscalls in the range: 0x%lx-0x%lx\n", + __userspace_start, __userspace_end); + + return 0; +} + diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c index e71e5b4878d1..9adc729c20bd 100644 --- a/arch/um/os-Linux/signal.c +++ b/arch/um/os-Linux/signal.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *, void *= mc) =3D { [SIGTRAP] =3D relay_signal, @@ -29,6 +30,7 @@ void (*sig_info[NSIG])(int, struct siginfo *, struct uml_= pt_regs *, void *mc) =3D [SIGBUS] =3D relay_signal, [SIGSEGV] =3D segv_handler, [SIGIO] =3D sigio_handler, + [SIGSYS] =3D sigsys_handler, }; =20 static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) @@ -167,6 +169,11 @@ static void sigusr1_handler(int sig, struct siginfo *u= nused_si, mcontext_t *mc) uml_pm_wake(); } =20 +__weak void sigsys_handler(int sig, struct siginfo *unused_si, + struct uml_pt_regs *regs, void *mc) +{ +} + void register_pm_wake_signal(void) { set_handler(SIGUSR1); @@ -178,6 +185,7 @@ static void (*handlers[_NSIG])(int sig, struct siginfo = *si, mcontext_t *mc) =3D { [SIGILL] =3D sig_handler, [SIGFPE] =3D sig_handler, [SIGTRAP] =3D sig_handler, + [SIGSYS] =3D sig_handler, =20 [SIGIO] =3D sig_handler, [SIGWINCH] =3D sig_handler, diff --git a/arch/x86/um/nommu/Makefile b/arch/x86/um/nommu/Makefile index d72c63afffa5..ebe47d4836f4 100644 --- a/arch/x86/um/nommu/Makefile +++ b/arch/x86/um/nommu/Makefile @@ -5,4 +5,4 @@ else BITS :=3D 64 endif =20 -obj-y =3D do_syscall_$(BITS).o entry_$(BITS).o +obj-y =3D do_syscall_$(BITS).o entry_$(BITS).o os-Linux/ diff --git a/arch/x86/um/nommu/os-Linux/Makefile b/arch/x86/um/nommu/os-Lin= ux/Makefile new file mode 100644 index 000000000000..4571e403a6ff --- /dev/null +++ b/arch/x86/um/nommu/os-Linux/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y =3D mcontext.o +USER_OBJS :=3D mcontext.o + +include $(srctree)/arch/um/scripts/Makefile.rules diff --git a/arch/x86/um/nommu/os-Linux/mcontext.c b/arch/x86/um/nommu/os-L= inux/mcontext.c new file mode 100644 index 000000000000..c4ef877d5ea0 --- /dev/null +++ b/arch/x86/um/nommu/os-Linux/mcontext.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#define __FRAME_OFFSETS +#include +#include +#include +#include + +void set_mc_sigsys_hook(mcontext_t *mc) +{ + mc->gregs[REG_RCX] =3D mc->gregs[REG_RIP]; + mc->gregs[REG_RIP] =3D (unsigned long) __kernel_vsyscall; +} diff --git a/arch/x86/um/shared/sysdep/mcontext.h b/arch/x86/um/shared/sysd= ep/mcontext.h index b724c54da316..15e9ff287021 100644 --- a/arch/x86/um/shared/sysdep/mcontext.h +++ b/arch/x86/um/shared/sysdep/mcontext.h @@ -7,6 +7,9 @@ #define __SYS_SIGCONTEXT_X86_H =20 extern void get_regs_from_mc(struct uml_pt_regs *, mcontext_t *); +#ifndef CONFIG_MMU +extern void set_mc_sigsys_hook(mcontext_t *mc); +#endif =20 #ifdef __i386__ =20 --=20 2.43.0