Initial commit

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@iki.fi>
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..bb5e776
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,19 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright (c) 2026 Jarkko Sakkinen
+---
+BasedOnStyle: LLVM
+AlignAfterOpenBracket: Align
+AllowShortBlocksOnASingleLine: Never
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+BreakBeforeBraces: Linux
+ColumnLimit: 80
+ContinuationIndentWidth: 8
+DerivePointerAlignment: false
+IndentCaseLabels: false
+IndentWidth: 8
+PointerAlignment: Right
+SortIncludes: Never
+TabWidth: 8
+UseTab: Always
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..b9ba99d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,19 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[{Makefile,*.mk,*.c,*.h,*.S}]
+indent_style = tab
+indent_size = 8
+tab_width = 8
+
+[*.{sh,yml,yaml,md}]
+indent_style = space
+indent_size = 2
+
+[*.md]
+max_line_length = 80
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..fc90579
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,3 @@
+* text=auto eol=lf
+*.sh text eol=lf
+Makefile text eol=lf
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1bdb298
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+/build*/
+/target/
+*.o
+*.so
+*.a
+todo.txt
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..9efa6fb
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,338 @@
+                    GNU GENERAL PUBLIC LICENSE
+                       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+                            NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Moe Ghoul>, 1 April 1989
+  Moe Ghoul, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ae15461
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright (c) 2026 Jarkko Sakkinen
+
+VERSION := 0.1.0
+
+BPF_CLANG ?= clang
+BPFTOOL ?= bpftool
+PKG_CONFIG ?= pkg-config
+CLANG_FORMAT ?= clang-format
+SHELLCHECK ?= shellcheck
+INSTALL ?= install
+BUILD_DIR ?= build
+UAPI_DIR ?= $(CURDIR)/build/uapi/include
+PREFIX ?= /usr/local
+BINDIR ?= $(PREFIX)/bin
+
+CFLAGS ?= -O2 -g
+BPF_CFLAGS ?= -O2 -g
+
+TARGET := $(shell $(CC) -dumpmachine)
+ifeq ($(and $(filter x86_64-% aarch64-%,$(TARGET)),$(findstring -linux,$(TARGET))),)
+$(error ucells requires x86-64 or AArch64 Linux (got $(TARGET)))
+endif
+
+DEPS := libbpf >= 1.0
+ifeq ($(filter clean uninstall format ci check,$(MAKECMDGOALS)),)
+ifneq ($(shell $(PKG_CONFIG) --exists '$(DEPS)' && echo ok),ok)
+$(error missing dependencies: $(DEPS))
+endif
+ifneq ($(words $(wildcard $(addprefix $(UAPI_DIR)/,asm/unistd.h linux/runfd.h))),2)
+$(error runfd UAPI headers not found in $(UAPI_DIR); run make headers_install)
+endif
+endif
+DEP_CFLAGS := $(shell $(PKG_CONFIG) --cflags libbpf 2>/dev/null)
+DEP_LIBS := $(shell $(PKG_CONFIG) --libs libbpf 2>/dev/null)
+
+SRCS := $(wildcard src/*.c)
+OBJS := $(SRCS:src/%.c=$(BUILD_DIR)/%.o)
+BIN := $(BUILD_DIR)/ucells
+BPF_OBJ := $(BUILD_DIR)/ucells_lsm.bpf.o
+BPF_SKEL := $(BUILD_DIR)/ucells_lsm.skel.h
+
+UCELLS_CPPFLAGS := -D_GNU_SOURCE -DUCELLS_VERSION='"$(VERSION)"' \
+	-Ibpf -I$(BUILD_DIR) -isystem $(UAPI_DIR) $(DEP_CFLAGS) $(CPPFLAGS)
+UCELLS_CFLAGS := -std=c23 -Wall -Wextra -Wpedantic -Werror $(CFLAGS)
+
+.PHONY: all check format ci install uninstall clean
+
+all: $(BIN)
+
+$(BIN): $(OBJS)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(DEP_LIBS) $(LDLIBS)
+
+$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR)
+	$(CC) $(UCELLS_CPPFLAGS) $(UCELLS_CFLAGS) -MD -MP -c -o $@ $<
+
+# The skeleton embeds the BPF object as a single string literal.
+$(BUILD_DIR)/bpf_lsm.o: UCELLS_CFLAGS += -Wno-overlength-strings
+$(BUILD_DIR)/bpf_lsm.o: $(BPF_SKEL)
+
+$(BPF_SKEL): $(BPF_OBJ)
+	$(BPFTOOL) gen skeleton $< name ucells_lsm > $@.tmp
+	mv $@.tmp $@
+
+$(BPF_OBJ): bpf/ucells_lsm.bpf.c | $(BUILD_DIR)
+	$(BPF_CLANG) $(BPF_CFLAGS) -Wall -Wextra -Werror -target bpf \
+		-MMD -MP -c -o $@ $<
+
+$(BUILD_DIR):
+	mkdir -p $@
+
+check: ci
+
+format:
+	$(CLANG_FORMAT) --dry-run --Werror src/*.c src/*.h
+
+ci:
+	MAKE="$(MAKE)" BPF_CLANG="$(BPF_CLANG)" BPFTOOL="$(BPFTOOL)" \
+		CLANG_FORMAT="$(CLANG_FORMAT)" SHELLCHECK="$(SHELLCHECK)" \
+		./scripts/ci.sh
+
+install: $(BIN)
+	$(INSTALL) -D -m 0755 $(BIN) "$(DESTDIR)$(BINDIR)/ucells"
+
+uninstall:
+	rm -f "$(DESTDIR)$(BINDIR)/ucells"
+
+clean:
+	rm -rf "$(BUILD_DIR)"
+
+-include $(OBJS:.o=.d) $(BPF_OBJ:.o=.d)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..cc5c2f7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,30 @@
+# ucells
+
+Run Cells v3 programs on Linux.
+
+Requires a kernel with `CONFIG_RUNFD=y`, `NS_SET_IDMAP`, and BPF LSM
+enabled at boot.
+
+The Cells v3 syscall numbers are intercepted in the supervised program.
+In particular, slot 473 is `cell_create` there and `runfd` in the kernel.
+
+## Build and install
+
+The build requires a C23 compiler, Clang with BPF support, `bpftool`,
+`pkg-config`, and libbpf 1.0 or later. Build on x86-64 or AArch64 with
+exported UAPI headers from the runfd kernel, not the SDK syscall tables:
+
+    make -C /path/to/linux headers_install INSTALL_HDR_PATH="$PWD/build/uapi"
+    make
+    sudo make install
+
+If the headers are already exported elsewhere, set `UAPI_DIR` to their
+`include` directory when building or installing.
+
+## Usage
+
+    sudo ucells -- cat /proc/sys/user/max_cell_id_blocks
+
+## License
+
+Licensed under [GPL-2.0-only](LICENSE).
diff --git a/bpf/ucells_lsm.bpf.c b/bpf/ucells_lsm.bpf.c
new file mode 100644
index 0000000..2d0ec52
--- /dev/null
+++ b/bpf/ucells_lsm.bpf.c
@@ -0,0 +1,341 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 Jarkko Sakkinen
+ *
+ * BPF LSM boundary enforcement for ucells over Linux runfd(2) hooks.
+ */
+
+#include <linux/bpf.h>
+#include <linux/errno.h>
+#include <bpf/bpf_helpers.h>
+
+#include "ucells_lsm.h"
+
+#define AF_UNIX		1
+#define AF_PACKET	17
+#define SOCK_RAW	3
+#define SOCK_TYPE_MASK	0x0f
+
+#define ATTR_MODE	(1U << 0)
+#define S_IFMT		00170000
+#define S_IFDIR		0040000
+#define S_ISUID		0004000
+#define S_ISGID		0002000
+
+#define CELL_NET_RAW	(1ULL << 9)
+
+struct ns_common {
+	unsigned int inum;
+} __attribute__((preserve_access_index));
+
+struct user_namespace {
+	struct ns_common ns;
+} __attribute__((preserve_access_index));
+
+struct run {
+	struct user_namespace *user_ns;
+} __attribute__((preserve_access_index));
+
+struct kuid_t {
+	unsigned int val;
+} __attribute__((preserve_access_index));
+
+struct cred {
+	struct kuid_t euid;
+	struct user_namespace *user_ns;
+} __attribute__((preserve_access_index));
+
+struct task_struct {
+	const struct cred *real_cred;
+} __attribute__((preserve_access_index));
+
+struct inode {
+	unsigned short i_mode;
+} __attribute__((preserve_access_index));
+
+struct dentry {
+	struct inode *d_inode;
+} __attribute__((preserve_access_index));
+
+struct iattr {
+	unsigned int ia_valid;
+	unsigned short ia_mode;
+} __attribute__((preserve_access_index));
+
+struct sock_common {
+	unsigned short skc_family;
+} __attribute__((preserve_access_index));
+
+struct sock {
+	struct sock_common __sk_common;
+	const struct cred *sk_peer_cred;
+} __attribute__((preserve_access_index));
+
+struct socket {
+	short type;
+	struct sock *sk;
+} __attribute__((preserve_access_index));
+
+struct msghdr {
+	__u64 msg_controllen;
+} __attribute__((preserve_access_index));
+
+/* libbpf sets this before load; volatile prevents constant folding. */
+const volatile __u32 supervisor_tgid;
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 4096);
+	__type(key, __u32);
+	__type(value, struct cell_policy);
+} CELL_BY_USERNS SEC(".maps");
+
+static __always_inline __u32 current_tgid(void)
+{
+	return (__u32)(bpf_get_current_pid_tgid() >> 32);
+}
+
+static __always_inline int is_supervisor_task(void)
+{
+	return supervisor_tgid && supervisor_tgid == current_tgid();
+}
+
+static __always_inline struct cell_policy *
+task_cell_policy(const struct task_struct *task)
+{
+	const struct cred *cred;
+	struct user_namespace *uns;
+	__u32 inum;
+
+	if (!task)
+		return 0;
+	cred = task->real_cred;
+	if (!cred)
+		return 0;
+	uns = cred->user_ns;
+	if (!uns)
+		return 0;
+	inum = uns->ns.inum;
+	return bpf_map_lookup_elem(&CELL_BY_USERNS, &inum);
+}
+
+static __always_inline struct cell_policy *current_cell_policy(void)
+{
+	return task_cell_policy(bpf_get_current_task_btf());
+}
+
+/*
+ * lsm/runfd_alloc: tasks inside a cell cannot allocate runs.
+ */
+SEC("lsm/runfd_alloc")
+int ucells_runfd_alloc(__u64 *ctx)
+{
+	if (ctx[2])
+		return (int)ctx[2];
+	if (current_cell_policy())
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/runfd_free: drop BPF policy entry when the kernel frees struct run.
+ */
+SEC("lsm/runfd_free")
+int ucells_runfd_free(__u64 *ctx)
+{
+	struct run *run = (struct run *)ctx[0];
+	struct user_namespace *uns;
+	__u32 inum;
+
+	if (!run)
+		return 0;
+	uns = run->user_ns;
+	if (!uns)
+		return 0;
+	inum = uns->ns.inum;
+	bpf_map_delete_elem(&CELL_BY_USERNS, &inum);
+	return 0;
+}
+
+/*
+ * lsm/runfd_clone: only the supervisor may spawn tasks into a managed run,
+ * and tasks inside a cell may not spawn into any run.
+ */
+SEC("lsm/runfd_clone")
+int ucells_runfd_clone(__u64 *ctx)
+{
+	struct run *run = (struct run *)ctx[0];
+	struct user_namespace *uns;
+	__u32 inum;
+
+	if (ctx[1])
+		return (int)ctx[1];
+	if (current_cell_policy())
+		return -EPERM;
+	if (!run)
+		return 0;
+	uns = run->user_ns;
+	if (!uns)
+		return 0;
+	inum = uns->ns.inum;
+	if (bpf_map_lookup_elem(&CELL_BY_USERNS, &inum) && !is_supervisor_task())
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/kernel_module_request: no module is loaded because a cell asked.
+ */
+SEC("lsm/kernel_module_request")
+int ucells_kmod_request(__u64 *ctx)
+{
+	(void)ctx;
+	if (current_cell_policy())
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/ptrace_traceme: refuse PTRACE_TRACEME across cells.
+ */
+SEC("lsm/ptrace_traceme")
+int ucells_ptrace_traceme(__u64 *ctx)
+{
+	struct task_struct *parent = (struct task_struct *)ctx[0];
+	struct cell_policy *cur = current_cell_policy();
+	struct cell_policy *par = task_cell_policy(parent);
+	__u64 cur_id = cur ? cur->cell_id : 0;
+	__u64 par_id = par ? par->cell_id : 0;
+
+	if (cur_id != par_id)
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/ptrace_access_check: refuse ptrace across cells, and block unprivileged
+ * host tasks from ptracing a same-UID one-ID cell.
+ */
+SEC("lsm/ptrace_access_check")
+int ucells_ptrace_access(__u64 *ctx)
+{
+	struct task_struct *child = (struct task_struct *)ctx[0];
+	struct cell_policy *cur = current_cell_policy();
+	struct cell_policy *target = task_cell_policy(child);
+	__u64 cur_id = cur ? cur->cell_id : 0;
+	__u64 dst_id = target ? target->cell_id : 0;
+
+	if (cur_id != 0 && cur_id != dst_id)
+		return -EPERM;
+	if (cur_id == 0 && dst_id != 0 && (__u32)bpf_get_current_uid_gid() != 0)
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/task_kill: a UID means nothing across cells.
+ */
+SEC("lsm/task_kill")
+int ucells_task_kill(__u64 *ctx)
+{
+	struct task_struct *target_task = (struct task_struct *)ctx[0];
+	struct cell_policy *cur = current_cell_policy();
+	struct cell_policy *dst = task_cell_policy(target_task);
+	__u64 cur_id = cur ? cur->cell_id : 0;
+	__u64 dst_id = dst ? dst->cell_id : 0;
+
+	if (cur_id != 0 && cur_id != dst_id)
+		return -EPERM;
+	if (cur_id == 0 && dst_id != 0 && !is_supervisor_task() &&
+	    (__u32)bpf_get_current_uid_gid() != 0)
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/inode_setattr: block S_ISUID and non-directory S_ISGID inside cells.
+ */
+SEC("lsm/inode_setattr")
+int ucells_inode_setattr(__u64 *ctx)
+{
+	struct dentry *dentry = (struct dentry *)ctx[1];
+	struct iattr *attr = (struct iattr *)ctx[2];
+	struct inode *inode;
+	unsigned short mode;
+
+	if (!current_cell_policy() || !attr)
+		return 0;
+	if (!(attr->ia_valid & ATTR_MODE))
+		return 0;
+
+	mode = attr->ia_mode;
+	if (mode & S_ISUID)
+		return -EPERM;
+	if (mode & S_ISGID) {
+		inode = dentry ? dentry->d_inode : 0;
+		if (!inode || (inode->i_mode & S_IFMT) != S_IFDIR)
+			return -EPERM;
+	}
+	return 0;
+}
+
+/*
+ * lsm/socket_create: enforce empty "net" slot (AF_UNIX only) and CELL_NET_RAW.
+ */
+SEC("lsm/socket_create")
+int ucells_socket_create(__u64 *ctx)
+{
+	int family = (int)ctx[0];
+	int type = (int)ctx[1];
+	int kern = (int)ctx[3];
+	struct cell_policy *pol;
+
+	if (kern)
+		return 0;
+	pol = current_cell_policy();
+	if (!pol)
+		return 0;
+	if (pol->no_net && family != AF_UNIX)
+		return -EAFNOSUPPORT;
+	if (((type & SOCK_TYPE_MASK) == SOCK_RAW || family == AF_PACKET) &&
+	    !(pol->net_rights & CELL_NET_RAW))
+		return -EPERM;
+	return 0;
+}
+
+/*
+ * lsm/socket_sendmsg: forbid AF_UNIX ancillary control messages across cells.
+ */
+SEC("lsm/socket_sendmsg")
+int ucells_socket_sendmsg(__u64 *ctx)
+{
+	struct socket *sock = (struct socket *)ctx[0];
+	struct msghdr *msg = (struct msghdr *)ctx[1];
+	struct cell_policy *sender;
+	struct cell_policy *peer_pol;
+	struct sock *sk;
+	const struct cred *peer_cred;
+	struct user_namespace *peer_uns;
+	__u32 peer_inum;
+
+	if (!sock || !msg || msg->msg_controllen == 0)
+		return 0;
+	sender = current_cell_policy();
+	sk = sock->sk;
+	if (!sk || sk->__sk_common.skc_family != AF_UNIX)
+		return 0;
+	peer_cred = sk->sk_peer_cred;
+	if (!peer_cred)
+		return 0;
+	peer_uns = peer_cred->user_ns;
+	if (!peer_uns)
+		return 0;
+	peer_inum = peer_uns->ns.inum;
+	peer_pol = bpf_map_lookup_elem(&CELL_BY_USERNS, &peer_inum);
+
+	if ((sender ? sender->cell_id : 0) != (peer_pol ? peer_pol->cell_id : 0))
+		return -EPERM;
+	return 0;
+}
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/bpf/ucells_lsm.h b/bpf/ucells_lsm.h
new file mode 100644
index 0000000..5d95098
--- /dev/null
+++ b/bpf/ucells_lsm.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Jarkko Sakkinen */
+
+#ifndef UCELLS_LSM_H
+#define UCELLS_LSM_H
+
+#include <linux/types.h>
+
+struct cell_policy {
+	__u64 cell_id;
+	__u64 net_rights;
+	__u32 no_net;
+	__u32 single_id;
+};
+
+#endif
diff --git a/scripts/ci.sh b/scripts/ci.sh
new file mode 100755
index 0000000..14ae0ef
--- /dev/null
+++ b/scripts/ci.sh
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright (c) 2026 Jarkko Sakkinen
+
+set -euo pipefail
+
+MAKE="${MAKE:-make}"
+BPF_CLANG="${BPF_CLANG:-clang}"
+BPFTOOL="${BPFTOOL:-bpftool}"
+CLANG_FORMAT="${CLANG_FORMAT:-clang-format}"
+SHELLCHECK="${SHELLCHECK:-shellcheck}"
+
+for tool in "$MAKE" "$BPF_CLANG" "$BPFTOOL" "$CLANG_FORMAT" "$SHELLCHECK" awk; do
+  if ! command -v "$tool" >/dev/null 2>&1; then
+    printf 'error: missing required tool: %s\n' "$tool" >&2
+    exit 1
+  fi
+done
+
+cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
+"$SHELLCHECK" scripts/ci.sh
+
+awk 'length($0) > 80 {
+  printf "error: %s:%d exceeds 80 columns (%d)\n", FILENAME, FNR, length($0) > "/dev/stderr"
+  bad = 1
+}
+END { exit bad }' README.md
+
+"$CLANG_FORMAT" --dry-run --Werror src/*.c src/*.h
+
+build_dir=$(mktemp -d)
+trap 'rm -rf -- "$build_dir"' EXIT
+"$MAKE" -j"${JOBS:-4}" BUILD_DIR="$build_dir" BPF_CLANG="$BPF_CLANG" \
+  BPFTOOL="$BPFTOOL"
diff --git a/src/base.h b/src/base.h
new file mode 100644
index 0000000..593fe60
--- /dev/null
+++ b/src/base.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_BASE_H
+#define UCELLS_BASE_H
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
+
+#define container_of(ptr, type, member)                                        \
+	__extension__({                                                        \
+		static_assert(__builtin_types_compatible_p(                    \
+			typeof(ptr), typeof(&((type *)0)->member)));           \
+		(type *)((char *)(ptr) - offsetof(type, member));              \
+	})
+
+extern thread_local const char *ucells_error;
+
+static inline int err_ctx(int err, const char *context)
+{
+	ucells_error = context;
+	return err;
+}
+
+static inline void close_fd(int fd)
+{
+	int saved_errno = errno;
+
+	if (fd >= 0)
+		close(fd);
+	errno = saved_errno;
+}
+
+static inline int take_fd(int *fd)
+{
+	int old = *fd;
+
+	*fd = -1;
+	return old;
+}
+
+#endif
diff --git a/src/bpf_lsm.c b/src/bpf_lsm.c
new file mode 100644
index 0000000..811a19d
--- /dev/null
+++ b/src/bpf_lsm.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "bpf_lsm.h"
+#include "ucells_lsm.h"
+#include "ucells_lsm.skel.h"
+
+#define LSM_PATH "/sys/kernel/security/lsm"
+
+static int require_bpf_lsm(void)
+{
+	char *line = NULL, *names, *name;
+	size_t cap = 0;
+	ssize_t len;
+	FILE *file;
+	int ret;
+
+	file = fopen(LSM_PATH, "re");
+	if (!file)
+		return err_ctx(-errno, "read " LSM_PATH);
+
+	errno = 0;
+	len = getline(&line, &cap, file);
+	if (len < 0) {
+		ret = err_ctx(errno ? -errno : -EIO, "read " LSM_PATH);
+		goto out;
+	}
+	if (len && line[len - 1] == '\n')
+		line[len - 1] = '\0';
+	names = line;
+	while ((name = strsep(&names, ","))) {
+		if (!strcmp(name, "bpf")) {
+			ret = 0;
+			goto out;
+		}
+	}
+	ret = err_ctx(-EOPNOTSUPP, "BPF LSM is not enabled in " LSM_PATH);
+out:
+	free(line);
+	fclose(file);
+	return ret;
+}
+
+int bpf_lsm_load(struct bpf_lsm *out)
+{
+	struct ucells_lsm *skel;
+	int ret;
+
+	ret = require_bpf_lsm();
+	if (ret)
+		return ret;
+
+	skel = ucells_lsm__open();
+	if (!skel)
+		return err_ctx(errno ? -errno : -EIO,
+			       "open embedded BPF LSM object");
+
+	skel->rodata->supervisor_tgid = getpid();
+	ret = ucells_lsm__load(skel);
+	if (ret < 0) {
+		err_ctx(ret, "load embedded BPF LSM object");
+		goto destroy;
+	}
+	ret = ucells_lsm__attach(skel);
+	if (ret < 0) {
+		err_ctx(ret, "attach BPF LSM programs");
+		goto destroy;
+	}
+	out->skel = skel;
+	return 0;
+
+destroy:
+	ucells_lsm__destroy(skel);
+	return ret;
+}
+
+void bpf_lsm_destroy(struct bpf_lsm *lsm)
+{
+	ucells_lsm__destroy(lsm->skel);
+	lsm->skel = NULL;
+}
+
+int bpf_lsm_upsert(struct bpf_lsm *lsm, uint32_t userns_inum, uint64_t cell_id,
+		   uint64_t net_rights, bool no_net, bool single_id)
+{
+	const struct cell_policy policy = {cell_id, net_rights, no_net,
+					   single_id};
+	int ret;
+
+	ret = bpf_map__update_elem(lsm->skel->maps.CELL_BY_USERNS, &userns_inum,
+				   sizeof(userns_inum), &policy, sizeof(policy),
+				   BPF_ANY);
+	if (ret < 0)
+		return err_ctx(ret,
+			       "install BPF LSM policy for user namespace");
+	return 0;
+}
+
+void bpf_lsm_remove(struct bpf_lsm *lsm, uint32_t userns_inum)
+{
+	int ret;
+
+	ret = bpf_map__delete_elem(lsm->skel->maps.CELL_BY_USERNS, &userns_inum,
+				   sizeof(userns_inum), 0);
+	if (ret < 0 && ret != -ENOENT)
+		fprintf(stderr, "ucells: remove BPF policy for userns %u: %s\n",
+			userns_inum, strerror(-ret));
+}
diff --git a/src/bpf_lsm.h b/src/bpf_lsm.h
new file mode 100644
index 0000000..518dcf1
--- /dev/null
+++ b/src/bpf_lsm.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_BPF_LSM_H
+#define UCELLS_BPF_LSM_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+struct ucells_lsm;
+
+struct bpf_lsm {
+	struct ucells_lsm *skel;
+};
+
+int bpf_lsm_load(struct bpf_lsm *out);
+void bpf_lsm_destroy(struct bpf_lsm *lsm);
+int bpf_lsm_upsert(struct bpf_lsm *lsm, uint32_t userns_inum, uint64_t cell_id,
+		   uint64_t net_rights, bool no_net, bool single_id);
+void bpf_lsm_remove(struct bpf_lsm *lsm, uint32_t userns_inum);
+
+#endif
diff --git a/src/budget.c b/src/budget.c
new file mode 100644
index 0000000..479ba6a
--- /dev/null
+++ b/src/budget.c
@@ -0,0 +1,368 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/magic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+
+#include "base.h"
+#include "budget.h"
+#include "uapi.h"
+
+struct budget_path {
+	struct budget_path *next;
+	char name[];
+};
+
+static const struct {
+	const char *name;
+	const char *error;
+} delegated_files[] = {
+	{"cgroup.procs", "delegate budget cgroup.procs"},
+	{"cgroup.threads", "delegate budget cgroup.threads"},
+	{"cgroup.subtree_control", "delegate budget cgroup.subtree_control"},
+};
+
+static bool path_starts_with(const char *path, const char *base)
+{
+	size_t len = strlen(base);
+
+	if (len == 1 && base[0] == '/')
+		return path[0] == '/';
+	return !strncmp(path, base, len) && (path[len] == '/' || !path[len]);
+}
+
+static int fd_canonical_path(int fd, char path[PATH_MAX])
+{
+	static const char deleted[] = " (deleted)";
+	char link[sizeof("/proc/self/fd/") + 12];
+	ssize_t len;
+
+	snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
+	len = readlink(link, path, PATH_MAX);
+	if (len < 0)
+		return err_ctx(-errno, "read budget fd link");
+	if (len == PATH_MAX)
+		return err_ctx(-ENAMETOOLONG, "read budget fd link");
+	path[len] = '\0';
+	if ((size_t)len >= sizeof(deleted) - 1 &&
+	    !strcmp(path + len - (sizeof(deleted) - 1), deleted))
+		return err_ctx(-ENODEV, "cgroup directory deleted");
+	return 0;
+}
+
+static DIR *open_dir_at(int parent_fd, const char *name)
+{
+	int fd;
+	DIR *dir;
+
+	fd = openat(parent_fd, name,
+		    O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW);
+	if (fd < 0)
+		return NULL;
+	dir = fdopendir(fd);
+	if (!dir)
+		close_fd(fd);
+	return dir;
+}
+
+static int is_subdir(DIR *dir, const struct dirent *entry)
+{
+	struct stat st;
+
+	if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+		return 0;
+	if (entry->d_type != DT_UNKNOWN)
+		return entry->d_type == DT_DIR;
+	if (fstatat(dirfd(dir), entry->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
+		return -errno;
+	return S_ISDIR(st.st_mode);
+}
+
+static int ensure_empty_cgroup(int fd, const struct caller_cred *caller)
+{
+	struct dirent *entry;
+	bool blank = true;
+	int procs_fd, ret;
+	struct stat st;
+	char buf[4096];
+	ssize_t n;
+	DIR *dir;
+
+	procs_fd = openat(fd, "cgroup.procs", O_RDONLY | O_CLOEXEC);
+	if (procs_fd < 0)
+		return err_ctx(-errno, "open budget cgroup.procs");
+	for (;;) {
+		n = read(procs_fd, buf, sizeof(buf));
+		if (n < 0) {
+			if (errno == EINTR)
+				continue;
+			ret = err_ctx(-errno, "read budget cgroup.procs");
+			goto close_procs;
+		}
+		if (!n)
+			break;
+		for (ssize_t i = 0; blank && i < n; i++)
+			blank = buf[i] && strchr(" \t\n\r\v\f", buf[i]);
+	}
+	if (!blank) {
+		ret = err_ctx(-EBUSY, "budget cgroup already contains tasks");
+		goto close_procs;
+	}
+	if (caller->uid) {
+		if (fstat(procs_fd, &st) < 0) {
+			ret = err_ctx(-errno, "stat budget cgroup.procs");
+			goto close_procs;
+		}
+		if (st.st_uid != caller->uid) {
+			ret = err_ctx(-EPERM,
+				      "cannot write budget cgroup.procs");
+			goto close_procs;
+		}
+	}
+	close_fd(procs_fd);
+
+	dir = open_dir_at(fd, ".");
+	if (!dir)
+		return err_ctx(-errno, "read budget cgroup directory");
+	for (;;) {
+		errno = 0;
+		entry = readdir(dir);
+		if (!entry) {
+			ret = -errno;
+			break;
+		}
+		ret = is_subdir(dir, entry);
+		if (ret)
+			break;
+	}
+	closedir(dir);
+	if (ret > 0)
+		return err_ctx(-EBUSY,
+			       "budget cgroup already has child cgroups");
+	if (ret < 0)
+		return err_ctx(ret, "read budget cgroup directory");
+	return 0;
+
+close_procs:
+	close_fd(procs_fd);
+	return ret;
+}
+
+int budget_claim(int fd, const struct caller_cred *caller,
+		 struct budget_registry *registry, struct budget *out)
+{
+	struct budget_path *entry;
+	char path[PATH_MAX];
+	struct statfs stfs;
+	struct stat st;
+	size_t len;
+	int ret;
+
+	if (fstatfs(fd, &stfs) < 0)
+		return err_ctx(-errno, "fstatfs budget fd");
+	if (stfs.f_type != CGROUP2_SUPER_MAGIC)
+		return err_ctx(-EINVAL, "budget fd is not on cgroup2fs");
+	if (fstat(fd, &st) < 0)
+		return err_ctx(-errno, "fstat budget fd");
+	if (!S_ISDIR(st.st_mode))
+		return err_ctx(-ENOTDIR, "budget fd is not a directory");
+	if (caller->uid && st.st_uid != caller->uid)
+		return err_ctx(-EPERM,
+			       "caller does not own budget cgroup directory");
+	ret = fd_canonical_path(fd, path);
+	if (ret)
+		return ret;
+	ret = ensure_empty_cgroup(fd, caller);
+	if (ret)
+		return ret;
+	for (entry = registry->paths; entry; entry = entry->next) {
+		if (path_starts_with(path, entry->name) ||
+		    path_starts_with(entry->name, path))
+			return err_ctx(-EBUSY, "overlapping cgroup budgets");
+	}
+	len = strlen(path) + 1;
+	entry = malloc(sizeof(*entry) + len);
+	if (!entry)
+		return err_ctx(-ENOMEM, "claim budget cgroup path");
+	memcpy(entry->name, path, len);
+	entry->next = registry->paths;
+	registry->paths = entry;
+	*out = (struct budget){
+		.fd = fd,
+		.cgroup_id = st.st_ino,
+		.path = entry,
+		.orig_uid = st.st_uid,
+		.orig_gid = st.st_gid,
+	};
+	return 0;
+}
+
+static bool restore_owner(const struct budget *budget)
+{
+	uid_t uid = budget->orig_uid;
+	gid_t gid = budget->orig_gid;
+	int fd = budget->fd;
+	bool clean;
+	size_t i;
+
+	clean = fchownat(fd, "", uid, gid, AT_EMPTY_PATH) == 0;
+	for (i = 0; i < ARRAY_SIZE(delegated_files); i++) {
+		if (fchownat(fd, delegated_files[i].name, uid, gid, 0) < 0)
+			clean = false;
+	}
+	return clean;
+}
+
+int budget_delegate(struct budget *budget, uint32_t uid, uint32_t gid)
+{
+	int fd = budget->fd;
+	size_t i;
+	int ret;
+
+	if (uid == budget->orig_uid && gid == budget->orig_gid)
+		goto delegated;
+	if (fchownat(fd, "", uid, gid, AT_EMPTY_PATH) < 0)
+		return err_ctx(-errno, "delegate budget directory owner");
+	for (i = 0; i < ARRAY_SIZE(delegated_files); i++) {
+		if (fchownat(fd, delegated_files[i].name, uid, gid, 0) < 0) {
+			ret = err_ctx(-errno, delegated_files[i].error);
+			restore_owner(budget);
+			return ret;
+		}
+	}
+delegated:
+	budget->delegated = true;
+	return 0;
+}
+
+int budget_validate_spawn_cgroup(const struct budget *budget, int fd)
+{
+	char path[PATH_MAX];
+	struct statfs stfs;
+	struct stat st;
+	int ret;
+
+	if (fstatfs(fd, &stfs) < 0)
+		return err_ctx(-errno, "fstatfs spawn cgroup");
+	if (stfs.f_type != CGROUP2_SUPER_MAGIC)
+		return err_ctx(-EINVAL, "spawn cgroup fd is not on cgroup2fs");
+	if (fstat(fd, &st) < 0)
+		return err_ctx(-errno, "fstat spawn cgroup");
+	if (!st.st_nlink)
+		return err_ctx(-ENODEV, "spawn cgroup was removed");
+	ret = fd_canonical_path(fd, path);
+	if (ret)
+		return ret;
+	if (!path_starts_with(path, budget->path->name))
+		return err_ctx(-EPERM, "spawn cgroup lies outside cell budget");
+	return 0;
+}
+
+static void kill_cgroup(int fd)
+{
+	int kill_fd = openat(fd, "cgroup.kill", O_WRONLY | O_CLOEXEC);
+
+	if (kill_fd >= 0) {
+		(void)write(kill_fd, "1", 1);
+		close_fd(kill_fd);
+	}
+}
+
+void budget_kill_all(const struct budget *budget)
+{
+	kill_cgroup(budget->fd);
+}
+
+struct cgroup_walk {
+	DIR *dir;
+	struct cgroup_walk *parent;
+	char name[];
+};
+
+static bool remove_subcgroups_bottom_up(int fd)
+{
+	struct cgroup_walk *frame, *child, root = {0};
+	struct dirent *entry;
+	bool clean = true;
+	size_t len;
+	int ret;
+
+	root.dir = open_dir_at(fd, ".");
+	if (!root.dir)
+		return false;
+	frame = &root;
+	for (;;) {
+		errno = 0;
+		entry = readdir(frame->dir);
+		if (!entry) {
+			if (errno)
+				clean = false;
+			closedir(frame->dir);
+			if (frame == &root)
+				return clean;
+			child = frame;
+			frame = frame->parent;
+			if (unlinkat(dirfd(frame->dir), child->name,
+				     AT_REMOVEDIR) < 0)
+				clean = false;
+			free(child);
+			continue;
+		}
+		ret = is_subdir(frame->dir, entry);
+		if (ret < 0)
+			clean = false;
+		if (ret <= 0)
+			continue;
+		len = strlen(entry->d_name) + 1;
+		child = malloc(sizeof(*child) + len);
+		if (!child) {
+			clean = false;
+			continue;
+		}
+		child->dir = open_dir_at(dirfd(frame->dir), entry->d_name);
+		if (!child->dir) {
+			clean = false;
+			unlinkat(dirfd(frame->dir), entry->d_name,
+				 AT_REMOVEDIR);
+			free(child);
+			continue;
+		}
+		kill_cgroup(dirfd(child->dir));
+		memcpy(child->name, entry->d_name, len);
+		child->parent = frame;
+		frame = child;
+	}
+}
+
+bool budget_teardown(struct budget *budget, struct budget_registry *registry)
+{
+	struct budget_path **entry;
+	bool clean;
+
+	if (!budget->path)
+		return true;
+	budget_kill_all(budget);
+	clean = remove_subcgroups_bottom_up(budget->fd);
+	if (budget->delegated &&
+	    (budget->orig_uid || budget->orig_gid || clean)) {
+		if (!restore_owner(budget))
+			clean = false;
+	}
+	for (entry = &registry->paths; *entry; entry = &(*entry)->next) {
+		if (*entry == budget->path) {
+			*entry = budget->path->next;
+			break;
+		}
+	}
+	free(budget->path);
+	close_fd(budget->fd);
+	*budget = (struct budget){.fd = -1};
+	return clean;
+}
diff --git a/src/budget.h b/src/budget.h
new file mode 100644
index 0000000..7fbb558
--- /dev/null
+++ b/src/budget.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_BUDGET_H
+#define UCELLS_BUDGET_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+struct budget_path;
+struct caller_cred;
+
+struct budget_registry {
+	struct budget_path *paths;
+};
+
+struct budget {
+	int fd;
+	uint64_t cgroup_id;
+	struct budget_path *path;
+	uid_t orig_uid;
+	gid_t orig_gid;
+	bool delegated;
+};
+
+/* Takes ownership of fd on success only. */
+int budget_claim(int fd, const struct caller_cred *caller,
+		 struct budget_registry *registry, struct budget *out);
+int budget_delegate(struct budget *budget, uint32_t uid, uint32_t gid);
+int budget_validate_spawn_cgroup(const struct budget *budget, int fd);
+void budget_kill_all(const struct budget *budget);
+/* Consumes the budget; false means lent IDs cannot be reclaimed safely. */
+bool budget_teardown(struct budget *budget, struct budget_registry *registry);
+
+#endif
diff --git a/src/cell.c b/src/cell.c
new file mode 100644
index 0000000..d595dc9
--- /dev/null
+++ b/src/cell.c
@@ -0,0 +1,566 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <linux/magic.h>
+#include <linux/nsfs.h>
+#include <linux/sched.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/statfs.h>
+#include <sys/wait.h>
+
+#include "base.h"
+#include "cell.h"
+#include "fdpass.h"
+#include "runfd.h"
+
+static int ensure_netns(int fd)
+{
+	struct statfs st;
+	int type;
+
+	if (fstatfs(fd, &st) < 0)
+		return -errno;
+	if (st.f_type != NSFS_MAGIC)
+		return -EINVAL;
+	type = ioctl(fd, NS_GET_NSTYPE);
+	if (type < 0)
+		return -errno;
+	return type == CLONE_NEWNET ? 0 : -EINVAL;
+}
+
+/* The helper creates the netns in the builder's user namespace. */
+static int allocate_builder_netns(void)
+{
+	int fds[2], ret;
+	pid_t pid;
+
+	ret = fd_pair(fds);
+	if (ret < 0)
+		return ret;
+	pid = fork();
+	if (pid < 0) {
+		ret = -errno;
+		close_fd(fds[1]);
+		goto close_parent;
+	}
+	if (!pid) {
+		close_fd(fds[0]);
+		if (unshare(CLONE_NEWNET) == 0) {
+			int fd =
+				open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
+
+			if (fd >= 0)
+				send_fd(fds[1], fd);
+		}
+		_exit(0);
+	}
+
+	close_fd(fds[1]);
+	ret = recv_fd(fds[0]);
+	while (waitpid(pid, NULL, 0) < 0 && errno == EINTR)
+		;
+close_parent:
+	close_fd(fds[0]);
+	return ret;
+}
+
+static void configure_new_net(struct cell *cell)
+{
+	int fd = allocate_builder_netns();
+
+	if (fd >= 0) {
+		runfd_share_ns_fd(cell->runfd, fd);
+		close_fd(fd);
+	}
+	cell->net_mode = NET_SLOT_NEW;
+}
+
+static int apply_ids_shape(struct cell *cell, enum id_shape shape,
+			   const struct caller_cred *caller,
+			   struct id_block_pool *pool)
+{
+	struct userns_idmap_extent uid, gid;
+	int ret;
+
+	ret = id_shape_validate_caller(shape, caller->uid, caller->gid);
+	if (ret < 0)
+		return ret;
+	if (cell->runfd < 0)
+		return -EBUSY;
+
+	switch (shape) {
+	case ID_SINGLE:
+		uid = (struct userns_idmap_extent){caller->uid, caller->uid, 1,
+						   0};
+		gid = (struct userns_idmap_extent){caller->gid, caller->gid, 1,
+						   0};
+		ret = runfd_set_idmap(cell->runfd, &uid, &gid, 0);
+		break;
+	case ID_BLOCK:
+		ret = id_block_allocate(pool, caller->uid, &cell->id_lease);
+		if (ret < 0)
+			return ret;
+		uid = (struct userns_idmap_extent){
+			.lower_first = cell->id_lease.first_id,
+			.count = ID_BLOCK_SIZE,
+		};
+		ret = runfd_set_idmap(cell->runfd, &uid, &uid,
+				      USERNS_SETGROUPS_ALLOWED);
+		if (ret < 0)
+			id_block_release(pool, &cell->id_lease, false);
+		break;
+	default:
+		return -EINVAL;
+	}
+	if (ret < 0)
+		return ret;
+	cell->id_shape = shape;
+	return 0;
+}
+
+int cell_init(struct cell *cell, uint64_t id, const struct caller_cred *creator)
+{
+	uint32_t userns_inum;
+	int fd;
+
+	fd = runfd_create(&userns_inum);
+	if (fd < 0)
+		return fd;
+	*cell = (struct cell){
+		.id = id,
+		.creator = *creator,
+		.runfd = fd,
+		.userns_inum = userns_inum,
+		.state = CELL_BUILDING,
+		.root.fd = -1,
+		.budget.fd = -1,
+	};
+	return 0;
+}
+
+int cell_configure(struct cell *cell, const struct slot_action *action,
+		   int *aux_fd, const struct caller_cred *caller,
+		   struct cell_resources *resources)
+{
+	int ret;
+
+	if (cell->state != CELL_BUILDING)
+		return -EBUSY;
+
+	switch (action->type) {
+	case SLOT_HOSTNAME:
+		if (cell->has_hostname)
+			return -EBUSY;
+		memcpy(cell->hostname, action->hostname,
+		       sizeof(cell->hostname));
+		cell->has_hostname = true;
+		return 0;
+	case SLOT_IDS:
+		if (cell->id_shape != ID_UNSET)
+			return -EBUSY;
+		return apply_ids_shape(cell, action->shape, caller,
+				       &resources->id_pool);
+	case SLOT_CAPS:
+		if (cell->has_caps)
+			return -EBUSY;
+		cell->caps = action->caps;
+		cell->has_caps = true;
+		return 0;
+	case SLOT_NET_NEW:
+		if (cell->net_mode != NET_SLOT_NONE || cell->runfd < 0)
+			return -EBUSY;
+		configure_new_net(cell);
+		return 0;
+	case SLOT_NET_FD:
+		if (cell->net_mode != NET_SLOT_NONE)
+			return -EBUSY;
+		if (*aux_fd < 0)
+			return -EBADF;
+		ret = ensure_netns(*aux_fd);
+		if (ret < 0)
+			return ret;
+		if (cell->runfd < 0)
+			return -EBUSY;
+		ret = runfd_share_ns_fd(cell->runfd, *aux_fd);
+		if (ret < 0)
+			return ret;
+		cell->net_mode = NET_SLOT_PROVIDED_FD;
+		return 0;
+	case SLOT_NET_RIGHTS:
+		if (cell->has_net_rights)
+			return -EBUSY;
+		if (cell->net_mode == NET_SLOT_NONE)
+			return -EINVAL;
+		cell->net_rights = action->rights;
+		cell->has_net_rights = true;
+		return 0;
+	case SLOT_ROOT_FD:
+		if (cell->root.fd >= 0 || cell->root_installed)
+			return -EBUSY;
+		if (*aux_fd < 0)
+			return -EBADF;
+		ret = mount_tree_from_slot_fd(*aux_fd, &cell->root);
+		if (!ret)
+			take_fd(aux_fd);
+		return ret;
+	case SLOT_BUDGET_FD:
+		if (cell->budget.fd >= 0)
+			return -EBUSY;
+		if (*aux_fd < 0)
+			return -EBADF;
+		ret = budget_claim(*aux_fd, caller, &resources->budgets,
+				   &cell->budget);
+		if (ret < 0)
+			return ret;
+		take_fd(aux_fd);
+		cell->budget_cgroup_id = cell->budget.cgroup_id;
+		return 0;
+	}
+	return -EINVAL;
+}
+
+int cell_open_net(struct cell *cell)
+{
+	if (cell->net_mode == NET_SLOT_NONE)
+		configure_new_net(cell);
+	return runfd_get_ns_fd(cell->runfd, CLONE_NEWNET);
+}
+
+int cell_idmap_fd(const struct cell *cell)
+{
+	if (cell->id_shape == ID_UNSET)
+		return -EINVAL;
+	if (cell->runfd < 0)
+		return -ESRCH;
+	return runfd_get_ns_fd(cell->runfd, CLONE_NEWUSER);
+}
+
+int cell_seal(struct cell *cell, const struct caller_cred *caller,
+	      struct cell_resources *resources)
+{
+	uint32_t uid = cell->creator.uid, gid = cell->creator.gid;
+	bool no_net = cell->net_mode == NET_SLOT_NONE;
+	bool single_id;
+	int ret;
+
+	if (cell->state != CELL_BUILDING)
+		return -EBUSY;
+	if ((cell->root.fd < 0 && !cell->root_installed) || cell->budget.fd < 0)
+		return -EINVAL;
+	if (cell->id_shape == ID_UNSET) {
+		ret = apply_ids_shape(cell, ID_SINGLE, caller,
+				      &resources->id_pool);
+		if (ret < 0)
+			return ret;
+	}
+
+	single_id = cell->id_shape == ID_SINGLE;
+	if (single_id && cell->caps)
+		return -EINVAL;
+
+	ret = cell_sandbox_compile(cell->caps, cell->net_rights, no_net,
+				   &cell->sandbox);
+	if (ret < 0)
+		return ret;
+
+	if (!single_id) {
+		uid = cell->id_lease.first_id;
+		gid = cell->id_lease.first_id;
+	}
+	if (cell->root.fd >= 0) {
+		ret = -EBUSY;
+		if (cell->runfd < 0)
+			goto destroy_sandbox;
+		ret = runfd_seal_root(cell->runfd, &cell->root);
+		if (ret < 0)
+			goto destroy_sandbox;
+		mount_tree_destroy(&cell->root);
+		cell->root_installed = true;
+	}
+	ret = budget_delegate(&cell->budget, uid, gid);
+	if (ret < 0)
+		goto destroy_sandbox;
+	ret = bpf_lsm_upsert(&resources->bpf_lsm, cell->userns_inum, cell->id,
+			     cell->net_rights, no_net, single_id);
+	if (ret < 0)
+		goto destroy_sandbox;
+	cell->has_sandbox = true;
+	cell->state = CELL_SEALED;
+	return 0;
+
+destroy_sandbox:
+	cell_sandbox_destroy(&cell->sandbox);
+	return ret;
+}
+
+void cell_snapshot(const struct cell *cell, uint64_t mask,
+		   struct cell_info *out)
+{
+	*out = (struct cell_info){.supported_mask = CELL_INFO_ALL};
+
+	if (mask & CELL_INFO_ID) {
+		out->id = cell->id;
+		out->mask |= CELL_INFO_ID;
+	}
+	if (mask & CELL_INFO_STATE) {
+		out->state = cell->state;
+		out->mask |= CELL_INFO_STATE;
+	}
+	if ((mask & CELL_INFO_EXIT) && cell->state == CELL_DEAD) {
+		out->exit_status = cell->exit_status;
+		out->mask |= CELL_INFO_EXIT;
+	}
+	if ((mask & CELL_INFO_BUDGET) && cell->budget_cgroup_id) {
+		out->budget_cgroupid = cell->budget_cgroup_id;
+		out->mask |= CELL_INFO_BUDGET;
+	}
+	if ((mask & CELL_INFO_IDS) && cell->id_shape != ID_UNSET) {
+		out->ids_count = cell->id_shape == ID_BLOCK ? ID_BLOCK_SIZE : 1;
+		out->mask |= CELL_INFO_IDS;
+	}
+	if (mask & CELL_INFO_CAPS) {
+		out->caps = cell->caps;
+		out->mask |= CELL_INFO_CAPS;
+	}
+	if (mask & CELL_INFO_NET_RIGHTS) {
+		out->net_rights = cell->net_rights;
+		out->mask |= CELL_INFO_NET_RIGHTS;
+	}
+	if ((mask & CELL_INFO_IDS_FIRST) && cell->id_shape == ID_BLOCK &&
+	    cell->state != CELL_DEAD) {
+		out->ids_first = cell->id_lease.first_id;
+		out->mask |= CELL_INFO_IDS_FIRST;
+	}
+}
+
+int cell_check_spawnable(const struct cell *cell)
+{
+	switch (cell->state) {
+	case CELL_SEALED:
+	case CELL_RUNNING:
+		return 0;
+	case CELL_DYING:
+	case CELL_DEAD:
+		return -ESRCH;
+	case CELL_BUILDING:
+		return -EINVAL;
+	}
+	return -EINVAL;
+}
+
+static int validate_cred(const struct cell *cell, const struct cred_spec *cred)
+{
+	if (cell->id_shape != ID_BLOCK) {
+		if (cred->uid != cell->creator.uid ||
+		    cred->gid != cell->creator.gid)
+			return -EINVAL;
+		return cred->nr_groups ? -EPERM : 0;
+	}
+
+	if (cred->uid >= ID_BLOCK_SIZE || cred->gid >= ID_BLOCK_SIZE)
+		return -EINVAL;
+	for (size_t i = 0; i < cred->nr_groups; i++) {
+		if (cred->groups[i] >= ID_BLOCK_SIZE)
+			return -EINVAL;
+	}
+	return 0;
+}
+
+static void finish_teardown(struct cell *cell, int status,
+			    struct cell_resources *resources)
+{
+	bool clean_budget = true;
+
+	if (cell->root.fd >= 0)
+		mount_tree_destroy(&cell->root);
+	if (cell->has_sandbox)
+		cell_sandbox_destroy(&cell->sandbox);
+	if (cell->budget.fd >= 0)
+		clean_budget =
+			budget_teardown(&cell->budget, &resources->budgets);
+
+	/* Close the runfd before releasing the ID block. */
+	close_fd(take_fd(&cell->runfd));
+	bpf_lsm_remove(&resources->bpf_lsm, cell->userns_inum);
+	if (cell->id_shape == ID_BLOCK)
+		id_block_release(&resources->id_pool, &cell->id_lease,
+				 !clean_budget);
+
+	cell->exit_status = status;
+	cell->state = CELL_DEAD;
+}
+
+int cell_start_embryo(struct cell *cell, const struct embryo *embryo,
+		      struct cell_resources *resources)
+{
+	char *const default_argv[] = {embryo->exec, NULL};
+	struct spawn_plan plan;
+	int cgroup_fd, status, ret;
+
+	ret = cell_check_spawnable(cell);
+	if (ret < 0)
+		return ret;
+	if (!embryo->exec)
+		return -EINVAL;
+	if (embryo->has_cred) {
+		ret = validate_cred(cell, &embryo->cred);
+		if (ret < 0)
+			return ret;
+	}
+	if (cell->budget.fd < 0)
+		return -ESRCH;
+	cgroup_fd = cell->budget.fd;
+	if (embryo->cgroup_fd >= 0) {
+		ret = budget_validate_spawn_cgroup(&cell->budget,
+						   embryo->cgroup_fd);
+		if (ret < 0)
+			return ret;
+		cgroup_fd = embryo->cgroup_fd;
+	}
+	if (!cell->has_sandbox)
+		return -EINVAL;
+	if (cell->runfd < 0)
+		return -ESRCH;
+
+	plan = (struct spawn_plan){
+		.exec_path = embryo->exec,
+		.argv = embryo->argv.items ? embryo->argv.items : default_argv,
+		.envp = embryo->envp.items,
+		.cred = embryo->has_cred ? &embryo->cred : NULL,
+		.allow_setgroups = cell->id_shape == ID_BLOCK,
+		.is_init = !cell->has_init,
+		.hostname = cell->has_hostname ? cell->hostname : "(none)",
+		.staged_fds = embryo->staged_fds,
+		.nr_staged_fds = embryo->nr_staged_fds,
+		.cgroup_fd = cgroup_fd,
+		.sandbox = &cell->sandbox,
+	};
+	ret = runfd_spawn(cell->runfd, &plan);
+	if (ret >= 0) {
+		cell->has_init = true;
+		cell->state = CELL_RUNNING;
+	} else if (plan.is_init && runfd_poll_exit(cell->runfd, &status) > 0) {
+		finish_teardown(cell, status, resources);
+	}
+	return ret;
+}
+
+void cell_kill(struct cell *cell, struct cell_resources *resources)
+{
+	int status;
+
+	switch (cell->state) {
+	case CELL_BUILDING:
+	case CELL_SEALED:
+		finish_teardown(cell, SIGKILL, resources);
+		break;
+	case CELL_RUNNING:
+	case CELL_DYING:
+		cell->state = CELL_DYING;
+		if (cell->budget.fd >= 0)
+			budget_kill_all(&cell->budget);
+		if (cell->runfd >= 0) {
+			runfd_send_signal(cell->runfd, SIGKILL);
+			if (runfd_poll_exit(cell->runfd, &status) > 0)
+				finish_teardown(cell, status, resources);
+		}
+		break;
+	case CELL_DEAD:
+		break;
+	}
+}
+
+void cell_on_run_exit(struct cell *cell, struct cell_resources *resources)
+{
+	int status;
+
+	if (cell->state == CELL_DEAD)
+		return;
+	if (cell->runfd < 0 || runfd_poll_exit(cell->runfd, &status) <= 0)
+		status = SIGKILL;
+	finish_teardown(cell, status, resources);
+}
+
+static int embryo_stage_fd(struct embryo *embryo, int target, int *aux_fd)
+{
+	struct staged_fd *fds = embryo->staged_fds;
+	size_t count = embryo->nr_staged_fds, pos = 0;
+
+	while (pos < count && fds[pos].target < target)
+		pos++;
+	if (pos < count && fds[pos].target == target) {
+		close_fd(fds[pos].source_fd);
+		fds[pos].source_fd = take_fd(aux_fd);
+		return 0;
+	}
+	if (count == EMBRYO_MAX_FDS)
+		return -EMFILE;
+	if (count == embryo->staged_capacity) {
+		size_t cap = count ? count * 2 : 8;
+
+		if (cap > EMBRYO_MAX_FDS)
+			cap = EMBRYO_MAX_FDS;
+		fds = realloc(fds, cap * sizeof(*fds));
+		if (!fds)
+			return -ENOMEM;
+		embryo->staged_fds = fds;
+		embryo->staged_capacity = cap;
+	}
+	memmove(fds + pos + 1, fds + pos, (count - pos) * sizeof(*fds));
+	fds[pos] = (struct staged_fd){target, take_fd(aux_fd)};
+	embryo->nr_staged_fds++;
+	return 0;
+}
+
+int embryo_configure(struct embryo *embryo, struct embryo_action *action,
+		     int *aux_fd)
+{
+	struct string_block *block;
+
+	switch (action->type) {
+	case EMBRYO_EXEC:
+		free(embryo->exec);
+		embryo->exec = action->exec;
+		action->exec = NULL;
+		return 0;
+	case EMBRYO_ARGV:
+	case EMBRYO_ENVP:
+		block = action->type == EMBRYO_ARGV ? &embryo->argv
+						    : &embryo->envp;
+		string_block_destroy(block);
+		*block = action->strings;
+		action->strings = (struct string_block){};
+		return 0;
+	case EMBRYO_CRED:
+		free(embryo->cred.groups);
+		embryo->cred = action->cred;
+		embryo->has_cred = true;
+		action->cred = (struct cred_spec){};
+		return 0;
+	case EMBRYO_PLACE_FD:
+		return embryo_stage_fd(embryo, action->target_fd, aux_fd);
+	case EMBRYO_CGROUP_FD:
+		if (*aux_fd < 0)
+			return -EBADF;
+		close_fd(embryo->cgroup_fd);
+		embryo->cgroup_fd = take_fd(aux_fd);
+		return 0;
+	}
+	return -EINVAL;
+}
+
+void embryo_destroy(struct embryo *embryo)
+{
+	for (size_t i = 0; i < embryo->nr_staged_fds; i++)
+		close_fd(embryo->staged_fds[i].source_fd);
+	free(embryo->staged_fds);
+	close_fd(embryo->cgroup_fd);
+	free(embryo->cred.groups);
+	string_block_destroy(&embryo->envp);
+	string_block_destroy(&embryo->argv);
+	free(embryo->exec);
+}
diff --git a/src/cell.h b/src/cell.h
new file mode 100644
index 0000000..5ebe330
--- /dev/null
+++ b/src/cell.h
@@ -0,0 +1,80 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_CELL_H
+#define UCELLS_CELL_H
+
+#include "bpf_lsm.h"
+#include "budget.h"
+#include "ids.h"
+#include "sandbox.h"
+#include "spawn.h"
+#include "tree.h"
+#include "uapi.h"
+
+struct cell_resources {
+	struct id_block_pool id_pool;
+	struct budget_registry budgets;
+	struct bpf_lsm bpf_lsm;
+};
+
+struct cell {
+	uint64_t id;
+	struct caller_cred creator;
+	int runfd;
+	uint32_t userns_inum;
+	enum cell_state state;
+	int32_t exit_status;
+	bool has_init;
+
+	enum id_shape id_shape;
+	struct id_block_lease id_lease;
+	uint64_t caps;
+	bool has_caps;
+	char hostname[UTS_HOSTNAME_MAX + 1];
+	bool has_hostname;
+	enum net_slot_mode net_mode;
+	uint64_t net_rights;
+	bool has_net_rights;
+	struct mount_tree root;
+	bool root_installed;
+	struct budget budget;
+	uint64_t budget_cgroup_id;
+	struct cell_sandbox sandbox;
+	bool has_sandbox;
+};
+
+struct embryo {
+	char *exec;
+	struct string_block argv;
+	struct string_block envp;
+	struct cred_spec cred;
+	bool has_cred;
+	struct staged_fd *staged_fds;
+	size_t nr_staged_fds;
+	size_t staged_capacity;
+	int cgroup_fd;
+};
+
+int cell_init(struct cell *cell, uint64_t id,
+	      const struct caller_cred *creator);
+int cell_configure(struct cell *cell, const struct slot_action *action,
+		   int *aux_fd, const struct caller_cred *caller,
+		   struct cell_resources *resources);
+int cell_open_net(struct cell *cell);
+int cell_idmap_fd(const struct cell *cell);
+int cell_seal(struct cell *cell, const struct caller_cred *caller,
+	      struct cell_resources *resources);
+void cell_snapshot(const struct cell *cell, uint64_t mask,
+		   struct cell_info *out);
+int cell_check_spawnable(const struct cell *cell);
+int cell_start_embryo(struct cell *cell, const struct embryo *embryo,
+		      struct cell_resources *resources);
+void cell_kill(struct cell *cell, struct cell_resources *resources);
+void cell_on_run_exit(struct cell *cell, struct cell_resources *resources);
+
+int embryo_configure(struct embryo *embryo, struct embryo_action *action,
+		     int *aux_fd);
+void embryo_destroy(struct embryo *embryo);
+
+#endif
diff --git a/src/fdpass.c b/src/fdpass.c
new file mode 100644
index 0000000..88525ee
--- /dev/null
+++ b/src/fdpass.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <string.h>
+#include <sys/socket.h>
+
+#include "base.h"
+#include "fdpass.h"
+
+int fd_pair(int fds[2])
+{
+	if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, fds) < 0)
+		return err_ctx(-errno, "socketpair");
+	return 0;
+}
+
+int send_fd(int socket, int fd)
+{
+	alignas(struct cmsghdr) char control[CMSG_SPACE(sizeof(int))] = {};
+	char payload = 0;
+	struct iovec iov = {.iov_base = &payload, .iov_len = 1};
+	struct msghdr msg = {
+		.msg_iov = &iov,
+		.msg_iovlen = 1,
+		.msg_control = control,
+		.msg_controllen = sizeof(control),
+	};
+	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+	ssize_t ret;
+
+	cmsg->cmsg_level = SOL_SOCKET;
+	cmsg->cmsg_type = SCM_RIGHTS;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+	memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+	do {
+		ret = sendmsg(socket, &msg, MSG_NOSIGNAL);
+	} while (ret < 0 && errno == EINTR);
+
+	if (ret < 0)
+		return err_ctx(-errno, "sendmsg SCM_RIGHTS");
+	return 0;
+}
+
+int recv_fd(int socket)
+{
+	alignas(struct cmsghdr) char control[CMSG_SPACE(sizeof(int))] = {};
+	char payload = 0;
+	struct iovec iov = {.iov_base = &payload, .iov_len = 1};
+	struct msghdr msg = {
+		.msg_iov = &iov,
+		.msg_iovlen = 1,
+		.msg_control = control,
+		.msg_controllen = sizeof(control),
+	};
+	struct cmsghdr *cmsg;
+	int received = -1;
+	size_t nr_fds = 0;
+	ssize_t bytes;
+	int ret;
+
+	do {
+		bytes = recvmsg(socket, &msg, MSG_CMSG_CLOEXEC);
+	} while (bytes < 0 && errno == EINTR);
+	if (bytes < 0)
+		return err_ctx(-errno, "recvmsg SCM_RIGHTS");
+
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		size_t count;
+
+		if (cmsg->cmsg_level != SOL_SOCKET ||
+		    cmsg->cmsg_type != SCM_RIGHTS ||
+		    cmsg->cmsg_len < CMSG_LEN(0))
+			continue;
+
+		count = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+		for (size_t i = 0; i < count; i++) {
+			int fd;
+
+			memcpy(&fd, CMSG_DATA(cmsg) + i * sizeof(fd),
+			       sizeof(fd));
+			if (nr_fds++ == 0)
+				received = fd;
+			else
+				close_fd(fd);
+		}
+	}
+
+	if (!bytes)
+		ret = err_ctx(-EPIPE, "recvmsg SCM_RIGHTS: peer closed");
+	else if (msg.msg_flags & MSG_CTRUNC)
+		ret = err_ctx(-ENOBUFS,
+			      "recvmsg SCM_RIGHTS: control data truncated");
+	else if (msg.msg_flags & MSG_TRUNC)
+		ret = err_ctx(-EMSGSIZE,
+			      "recvmsg SCM_RIGHTS: payload truncated");
+	else if (nr_fds != 1)
+		ret = err_ctx(-EBADMSG,
+			      "recvmsg SCM_RIGHTS: expected one descriptor");
+	else
+		return received;
+
+	close_fd(received);
+	return ret;
+}
diff --git a/src/fdpass.h b/src/fdpass.h
new file mode 100644
index 0000000..03d47bd
--- /dev/null
+++ b/src/fdpass.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_FDPASS_H
+#define UCELLS_FDPASS_H
+
+int fd_pair(int fds[2]);
+int send_fd(int socket, int fd);
+int recv_fd(int socket);
+
+#endif
diff --git a/src/handle.c b/src/handle.c
new file mode 100644
index 0000000..38b6e11
--- /dev/null
+++ b/src/handle.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <sys/socket.h>
+
+#include "base.h"
+#include "handle.h"
+
+#define STATUS_SOCK_SNDBUF 4096
+#define MAX_STATUS_BATCH 256
+
+int handle_pair_create(bool nonblocking_client, struct handle_pair *pair)
+{
+	const int sndbuf = STATUS_SOCK_SNDBUF;
+	int fds[2], ret;
+
+	if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK,
+		       0, fds) < 0)
+		return -errno;
+
+	if (!nonblocking_client) {
+		int flags = fcntl(fds[1], F_GETFL);
+
+		if (flags >= 0)
+			fcntl(fds[1], F_SETFL, flags & ~O_NONBLOCK);
+	}
+
+	setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
+	ret = file_identity_of_fd(fds[1], &pair->client_id);
+	if (ret < 0) {
+		close_fd(fds[1]);
+		close_fd(fds[0]);
+		return ret;
+	}
+	pair->supervisor_fd = fds[0];
+	return fds[1];
+}
+
+bool handle_push_exit_status(const struct handle_pair *pair, int32_t status)
+{
+	for (int i = 0; i < MAX_STATUS_BATCH; i++) {
+		if (send(pair->supervisor_fd, &status, sizeof(status),
+			 MSG_DONTWAIT | MSG_NOSIGNAL) < 0)
+			return errno == EAGAIN;
+	}
+	return false;
+}
diff --git a/src/handle.h b/src/handle.h
new file mode 100644
index 0000000..e097cdb
--- /dev/null
+++ b/src/handle.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_HANDLE_H
+#define UCELLS_HANDLE_H
+
+#include "uapi.h"
+
+struct handle_pair {
+	int supervisor_fd;
+	struct file_identity client_id;
+};
+
+/* Returns the client fd; pair owns the supervisor end on success. */
+int handle_pair_create(bool nonblocking_client, struct handle_pair *pair);
+bool handle_push_exit_status(const struct handle_pair *pair, int32_t status);
+
+#endif
diff --git a/src/handler.c b/src/handler.c
new file mode 100644
index 0000000..e2cd5c7
--- /dev/null
+++ b/src/handler.c
@@ -0,0 +1,612 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <linux/mount.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "handler.h"
+#include "memory.h"
+#include "notify.h"
+#include "supervisor.h"
+#include "uapi.h"
+
+#define CELL_STRING_READ_MAX 256
+#define TRACEE_WRITE_CHUNK 4096
+
+static int send_cloexec(int fd, struct dispatch_outcome *out)
+{
+	if (fd < 0)
+		return fd;
+	*out = (struct dispatch_outcome){
+		.type = DISPATCH_SEND_FD,
+		.fd = fd,
+		.cloexec = true,
+	};
+	return 0;
+}
+
+static int cell_configure(int listener, struct supervisor *supervisor,
+			  const struct seccomp_notif *notif)
+{
+	const __u64 *args = notif->data.args;
+	uint32_t cmd = (uint32_t)args[1];
+	uint64_t key_ptr = args[2], val_ptr = args[3];
+	int32_t aux = (int32_t)args[4];
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	char key[CELL_KEY_MAX];
+	struct caller_cred caller;
+	struct slot_action action;
+	int cfd, slot_fd = -1;
+	int ret;
+
+	if (cmd > CELL_CMD_SEAL)
+		return -EOPNOTSUPP;
+	cfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
+	if (cfd < 0)
+		return cfd;
+	ret = caller_cred_from_pid(pid, &caller);
+	if (ret < 0)
+		goto close_cell;
+	if (!notif_is_id_valid(listener, id)) {
+		ret = -ENOENT;
+		goto close_cell;
+	}
+
+	if (cmd == CELL_CMD_SEAL) {
+		ret = key_ptr || val_ptr || aux
+			      ? -EINVAL
+			      : supervisor_cell_seal(supervisor, cfd, &caller);
+		goto close_cell;
+	}
+	if (!key_ptr || (cmd == CELL_SET_FLAG && (val_ptr || aux)) ||
+	    (cmd == CELL_SET_STRING && (!val_ptr || aux)) ||
+	    (cmd == CELL_SET_BINARY && (!val_ptr || aux <= 0)) ||
+	    (cmd == CELL_SET_FD && (val_ptr || aux < 0))) {
+		ret = -EINVAL;
+		goto close_cell;
+	}
+
+	ret = read_tracee_cstring(listener, id, pid, key_ptr, key, sizeof(key));
+	if (ret < 0)
+		goto close_cell;
+
+	switch (cmd) {
+	case CELL_SET_FLAG:
+		ret = slot_action_flag(key);
+		goto close_cell;
+	case CELL_SET_STRING: {
+		char value[CELL_STRING_READ_MAX];
+
+		ret = read_tracee_cstring(listener, id, pid, val_ptr, value,
+					  sizeof(value));
+		if (!ret)
+			ret = slot_action_string(key, value, &action);
+		break;
+	}
+	case CELL_SET_BINARY: {
+		void *bytes;
+
+		ret = read_tracee_alloc(listener, id, pid, val_ptr, (size_t)aux,
+					&bytes);
+		if (!ret) {
+			ret = slot_action_binary(key, bytes, (size_t)aux,
+						 &action);
+			free(bytes);
+		}
+		break;
+	}
+	case CELL_SET_FD:
+		slot_fd = dup_tracee_fd(listener, id, pid, aux);
+		if (slot_fd < 0) {
+			ret = slot_fd;
+			goto close_cell;
+		}
+		ret = slot_action_fd(key, &action);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+		goto close_cell;
+	}
+	if (!ret)
+		ret = supervisor_cell_configure(supervisor, cfd, &action,
+						&slot_fd, &caller);
+	close_fd(slot_fd);
+close_cell:
+	close_fd(cfd);
+	return ret;
+}
+
+static int write_cell_info(int listener, uint64_t id, uint32_t pid,
+			   uint64_t addr, size_t size,
+			   const struct cell_info *info)
+{
+	unsigned char chunk[TRACEE_WRITE_CHUNK] = {};
+	size_t len = size < sizeof(chunk) ? size : sizeof(chunk);
+	size_t offset = len;
+	int ret;
+
+	if (!addr || size > UINT64_MAX - addr)
+		return -EFAULT;
+
+	memcpy(chunk, info, sizeof(*info));
+	ret = write_tracee_bytes(listener, id, pid, addr, chunk, len);
+	if (ret < 0)
+		return ret;
+
+	memset(chunk, 0, sizeof(*info));
+	while (offset < size) {
+		len = size - offset;
+		if (len > sizeof(chunk))
+			len = sizeof(chunk);
+		ret = write_tracee_bytes(listener, id, pid, addr + offset,
+					 chunk, len);
+		if (ret < 0)
+			return ret;
+		offset += len;
+	}
+	return 0;
+}
+
+static int cell_info(int listener, const struct supervisor *supervisor,
+		     const struct seccomp_notif *notif)
+{
+	const __u64 *args = notif->data.args;
+	int cfd_num = (int32_t)args[0];
+	uint64_t addr = args[1];
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	struct cell_info info = {};
+	uint64_t mask;
+	int cfd, ret;
+
+	if (args[2] > SIZE_MAX || args[2] < CELL_INFO_SIZE_VER0 ||
+	    (uint32_t)args[3])
+		return -EINVAL;
+
+	ret = read_tracee_bytes(listener, id, pid, addr, &mask, sizeof(mask));
+	if (ret < 0)
+		return ret;
+	if (cfd_num == CELL_SELF) {
+		supervisor_cell_info_self(supervisor, mask, pid, &info);
+	} else {
+		cfd = dup_tracee_fd(listener, id, pid, cfd_num);
+		if (cfd < 0)
+			return cfd;
+		ret = supervisor_cell_info(supervisor, cfd, mask, &info);
+		close_fd(cfd);
+		if (ret < 0)
+			return ret;
+	}
+
+	return write_cell_info(listener, id, pid, addr, (size_t)args[2], &info);
+}
+
+static int cell_open(int listener, struct supervisor *supervisor,
+		     const struct seccomp_notif *notif,
+		     struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	char key[CELL_KEY_MAX];
+	enum open_slot slot;
+	int cfd, ret;
+
+	if ((uint32_t)args[1] != CELL_OPEN_SLOT)
+		return -EOPNOTSUPP;
+	if (!args[2] || args[3] || (uint32_t)args[4])
+		return -EINVAL;
+
+	cfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
+	if (cfd < 0)
+		return cfd;
+	ret = read_tracee_cstring(listener, id, pid, args[2], key, sizeof(key));
+	if (ret < 0)
+		goto close_cell;
+	ret = parse_open_slot(key, &slot);
+	if (ret < 0)
+		goto close_cell;
+
+	ret = send_cloexec(supervisor_cell_open(supervisor, cfd, slot), out);
+close_cell:
+	close_fd(cfd);
+	return ret;
+}
+
+static int decode_pidfd_binary(int listener, const struct seccomp_notif *notif,
+			       const char *key, uint64_t addr, size_t len,
+			       struct embryo_action *action)
+{
+	struct pidfd_cred raw;
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	void *bytes = NULL;
+	int ret;
+
+	if (!strcmp(key, "argv") || !strcmp(key, "envp")) {
+		ret = read_tracee_alloc(listener, id, pid, addr, len, &bytes);
+		if (ret < 0)
+			return ret;
+
+		action->type = !strcmp(key, "argv") ? EMBRYO_ARGV : EMBRYO_ENVP;
+		ret = string_block_parse(bytes, len, &action->strings);
+		if (ret < 0)
+			free(bytes);
+		return ret;
+	}
+	if (strcmp(key, "cred") || len != sizeof(raw))
+		return -EINVAL;
+	ret = read_tracee_bytes(listener, id, pid, addr, &raw, sizeof(raw));
+	if (ret < 0)
+		return ret;
+	if (raw.spare || raw.ngroups > CELL_NGROUPS_MAX)
+		return -EINVAL;
+	if (raw.ngroups) {
+		ret = read_tracee_alloc(listener, id, pid, raw.groups,
+					raw.ngroups * sizeof(uint32_t), &bytes);
+		if (ret < 0)
+			return ret;
+	}
+
+	action->type = EMBRYO_CRED;
+	action->cred = (struct cred_spec){
+		.uid = raw.uid,
+		.gid = raw.gid,
+		.groups = bytes,
+		.nr_groups = raw.ngroups,
+	};
+	cred_spec_sort_groups(&action->cred);
+	return 0;
+}
+
+static int pidfd_configure(int listener, struct supervisor *supervisor,
+			   const struct seccomp_notif *notif,
+			   struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	int pfd_num = (int32_t)args[0];
+	uint32_t cmd = (uint32_t)args[1];
+	uint64_t key_ptr = args[2], val_ptr = args[3];
+	int32_t aux = (int32_t)args[4];
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	char key[EMBRYO_KEY_MAX];
+	struct embryo_action action = {};
+	int efd, src_fd = -1;
+	int ret;
+
+	if (cmd > PIDFD_CMD_START)
+		return -EOPNOTSUPP;
+	efd = dup_tracee_fd(listener, id, pid, pfd_num);
+	if (efd < 0)
+		return efd;
+
+	if (cmd == PIDFD_CMD_START) {
+		if (key_ptr || val_ptr || aux) {
+			ret = -EINVAL;
+			goto close_embryo;
+		}
+		ret = supervisor_pidfd_start(supervisor, efd);
+		if (ret >= 0) {
+			*out = (struct dispatch_outcome){
+				.type = DISPATCH_REPLACE_FD,
+				.src = ret,
+				.target_num = (uint32_t)pfd_num,
+			};
+			ret = 0;
+		}
+		goto close_embryo;
+	}
+
+	if (!key_ptr || (cmd == PIDFD_SET_STRING && (!val_ptr || aux)) ||
+	    (cmd == PIDFD_SET_BINARY && (!val_ptr || aux <= 0)) ||
+	    (cmd == PIDFD_SET_FD && (val_ptr || aux < 0))) {
+		ret = -EINVAL;
+		goto close_embryo;
+	}
+
+	ret = read_tracee_cstring(listener, id, pid, key_ptr, key, sizeof(key));
+	if (ret < 0)
+		goto close_embryo;
+
+	switch (cmd) {
+	case PIDFD_SET_STRING: {
+		char value[PATH_MAX];
+		char *owned;
+
+		ret = read_tracee_cstring(listener, id, pid, val_ptr, value,
+					  sizeof(value));
+		if (ret < 0)
+			goto close_embryo;
+		owned = strdup(value);
+		if (!owned) {
+			ret = -ENOMEM;
+			goto close_embryo;
+		}
+		ret = embryo_action_string(key, owned, &action);
+		if (ret < 0)
+			free(owned);
+		break;
+	}
+	case PIDFD_SET_BINARY:
+		ret = decode_pidfd_binary(listener, notif, key, val_ptr,
+					  (size_t)aux, &action);
+		break;
+	case PIDFD_SET_FD:
+		src_fd = dup_tracee_fd(listener, id, pid, aux);
+		if (src_fd < 0) {
+			ret = src_fd;
+			goto close_embryo;
+		}
+		ret = embryo_action_fd(key, &action);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+		goto close_embryo;
+	}
+
+	if (!ret) {
+		ret = supervisor_pidfd_configure(supervisor, efd, &action,
+						 &src_fd);
+		embryo_action_destroy(&action);
+	}
+	close_fd(src_fd);
+close_embryo:
+	close_fd(efd);
+	return ret;
+}
+
+static int handle_fsconfig(int listener, const struct supervisor *supervisor,
+			   const struct seccomp_notif *notif,
+			   struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	int32_t aux = (int32_t)args[4];
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	char key[CELL_KEY_MAX];
+	int fsfd, slot_fd, ret;
+
+	if (!args[2] || args[3] || aux < 0)
+		goto continue_syscall;
+	ret = read_tracee_cstring(listener, id, pid, args[2], key, sizeof(key));
+	if (ret < 0 || strcmp(key, "pidns"))
+		goto continue_syscall;
+
+	fsfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
+	if (fsfd < 0)
+		return fsfd;
+	slot_fd = dup_tracee_fd(listener, id, pid, aux);
+	if (slot_fd < 0) {
+		close_fd(fsfd);
+		return slot_fd;
+	}
+
+	ret = supervisor_fsconfig_pidns(supervisor, fsfd, slot_fd);
+	close_fd(slot_fd);
+	close_fd(fsfd);
+	if (ret != -EINVAL)
+		return ret;
+
+continue_syscall:
+	out->type = DISPATCH_CONTINUE;
+	return 0;
+}
+
+static int resolve_mount_target(int listener, const struct seccomp_notif *notif,
+				char path[PATH_MAX])
+{
+	const __u64 *args = notif->data.args;
+	char proc_path[sizeof("/proc//root") + 10];
+	int dfd_num = (int32_t)args[0];
+	const char *entry;
+	int ret;
+
+	path[0] = '\0';
+	if (args[1]) {
+		ret = read_tracee_cstring(listener, notif->id, notif->pid,
+					  args[1], path, PATH_MAX);
+		if (ret < 0)
+			return ret;
+	}
+	if (path[0] == '/')
+		entry = "root";
+	else if (dfd_num == AT_FDCWD)
+		entry = "cwd";
+	else
+		return dup_tracee_fd(listener, notif->id, notif->pid, dfd_num);
+
+	snprintf(proc_path, sizeof(proc_path), "/proc/%" PRIu32 "/%s",
+		 notif->pid, entry);
+	ret = open(proc_path, O_RDONLY | O_PATH | O_CLOEXEC);
+	return ret < 0 ? -errno : ret;
+}
+
+static int handle_idmap_mount(int listener, const struct supervisor *supervisor,
+			      const struct seccomp_notif *notif,
+			      struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	bool open_tree = notif->data.nr == __NR_open_tree_attr;
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	struct mount_attr attr;
+	char path[PATH_MAX];
+	const char *rel_path;
+	void *bytes;
+	int tracee_cfd, userns_fd, dfd, ret;
+	long page_size, rc;
+
+	static_assert(sizeof(attr) == MOUNT_ATTR_SIZE_VER0);
+	if (args[4] > SIZE_MAX)
+		return -EINVAL;
+	if (!args[3] || args[4] < MOUNT_ATTR_SIZE_VER0)
+		goto continue_syscall;
+
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size <= 0)
+		return -EIO;
+	if (args[4] > (uint64_t)page_size)
+		goto continue_syscall;
+
+	ret = read_tracee_alloc(listener, id, pid, args[3], (size_t)args[4],
+				&bytes);
+	if (ret < 0)
+		return ret;
+	memcpy(&attr, bytes, sizeof(attr));
+	if (!(attr.attr_set & MOUNT_ATTR_IDMAP)) {
+		out->type = DISPATCH_CONTINUE;
+		goto free_bytes;
+	}
+	if (attr.userns_fd > INT32_MAX) {
+		ret = -EBADF;
+		goto free_bytes;
+	}
+
+	tracee_cfd = dup_tracee_fd(listener, id, pid, (int32_t)attr.userns_fd);
+	if (tracee_cfd < 0) {
+		ret = tracee_cfd;
+		goto free_bytes;
+	}
+	userns_fd = supervisor_cell_idmap_fd(supervisor, tracee_cfd);
+	if (userns_fd < 0) {
+		struct cell_info info;
+
+		ret = userns_fd;
+		if (ret == -EINVAL &&
+		    supervisor_cell_info(supervisor, tracee_cfd, 0, &info) <
+			    0) {
+			out->type = DISPATCH_CONTINUE;
+			ret = 0;
+		}
+		goto close_cell;
+	}
+
+	attr.userns_fd = (uint64_t)userns_fd;
+	memcpy(bytes, &attr, sizeof(attr));
+	dfd = resolve_mount_target(listener, notif, path);
+	if (dfd < 0) {
+		ret = dfd;
+		goto close_userns;
+	}
+	rel_path = path[0] == '/' ? path + 1 : path;
+	rc = syscall(open_tree ? __NR_open_tree_attr : __NR_mount_setattr, dfd,
+		     rel_path, (uint32_t)args[2], bytes, (size_t)args[4]);
+	if (rc < 0)
+		ret = -errno;
+	else if (open_tree)
+		ret = rc > INT_MAX ? -EINVAL : send_cloexec((int)rc, out);
+	else
+		ret = 0;
+	close_fd(dfd);
+close_userns:
+	close_fd(userns_fd);
+close_cell:
+	close_fd(tracee_cfd);
+free_bytes:
+	free(bytes);
+	return ret;
+
+continue_syscall:
+	out->type = DISPATCH_CONTINUE;
+	return 0;
+}
+
+static int dispatch(int listener, struct supervisor *supervisor,
+		    const struct seccomp_notif *notif,
+		    struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+	int cfd_num = (int32_t)args[0];
+	uint32_t flags = (uint32_t)args[1];
+	int cfd, ret;
+
+	switch (notif->data.nr) {
+	case SYS_CELL_CREATE:
+		ret = supervisor_cell_create(supervisor, cfd_num, flags, pid);
+		return send_cloexec(ret, out);
+	case SYS_CELL_CONFIGURE:
+		return cell_configure(listener, supervisor, notif);
+	case SYS_CELL_INFO:
+		return cell_info(listener, supervisor, notif);
+	case SYS_CELL_OPEN:
+		return cell_open(listener, supervisor, notif, out);
+	case SYS_CELL_KILL:
+		if (flags)
+			return -EINVAL;
+		cfd = dup_tracee_fd(listener, id, pid, cfd_num);
+		if (cfd < 0)
+			return cfd;
+		ret = supervisor_cell_kill(supervisor, cfd);
+		close_fd(cfd);
+		return ret;
+	case __NR_pidfd_open:
+		if (!(flags & PIDFD_EMPTY)) {
+			out->type = DISPATCH_CONTINUE;
+			return 0;
+		}
+		if (cfd_num == -EBADF)
+			return -EOPNOTSUPP;
+		cfd = dup_tracee_fd(listener, id, pid, cfd_num);
+		if (cfd < 0)
+			return cfd;
+		ret = supervisor_pidfd_open_empty(supervisor, cfd, flags, pid);
+		close_fd(cfd);
+		return send_cloexec(ret, out);
+	case SYS_PIDFD_CONFIGURE:
+		return pidfd_configure(listener, supervisor, notif, out);
+	case __NR_fsconfig:
+		return handle_fsconfig(listener, supervisor, notif, out);
+	case __NR_mount_setattr:
+	case __NR_open_tree_attr:
+		return handle_idmap_mount(listener, supervisor, notif, out);
+	default:
+		return supervisor_sysctl_notification(supervisor, listener,
+						      notif, out);
+	}
+}
+
+int handle_notification(int listener, struct supervisor *supervisor,
+			const struct seccomp_notif *notif)
+{
+	struct dispatch_outcome out = {.type = DISPATCH_VALUE, .value = 0};
+	uint64_t id = notif->id;
+	int ret;
+
+	if (!notif_is_id_valid(listener, id))
+		return 0;
+	ret = dispatch(listener, supervisor, notif, &out);
+	if (ret < 0)
+		return notif_send_errno(listener, id, ret);
+
+	switch (out.type) {
+	case DISPATCH_CONTINUE:
+		return notif_send_continue(listener, id);
+	case DISPATCH_VALUE:
+		return notif_send_value(listener, id, out.value);
+	case DISPATCH_SEND_FD:
+		ret = notif_add_fd(listener, id, out.fd, 0,
+				   SECCOMP_ADDFD_FLAG_SEND,
+				   out.cloexec ? O_CLOEXEC : 0);
+		close_fd(out.fd);
+		return ret < 0 ? notif_send_errno(listener, id, ret) : 0;
+	case DISPATCH_REPLACE_FD:
+		ret = notif_add_fd(listener, id, out.src, out.target_num,
+				   SECCOMP_ADDFD_FLAG_SETFD, O_CLOEXEC);
+		close_fd(out.src);
+		if (ret < 0)
+			return notif_send_errno(listener, id, ret);
+		return notif_send_value(listener, id, 0);
+	}
+
+	return notif_send_errno(listener, id, -EINVAL);
+}
diff --git a/src/handler.h b/src/handler.h
new file mode 100644
index 0000000..5dd67d9
--- /dev/null
+++ b/src/handler.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_HANDLER_H
+#define UCELLS_HANDLER_H
+
+#include <linux/seccomp.h>
+
+struct supervisor;
+
+int handle_notification(int listener, struct supervisor *supervisor,
+			const struct seccomp_notif *notif);
+
+#endif
diff --git a/src/ids.c b/src/ids.c
new file mode 100644
index 0000000..ad17e3b
--- /dev/null
+++ b/src/ids.c
@@ -0,0 +1,182 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "ids.h"
+#include "uapi.h"
+
+#define DEFAULT_MAX_BLOCKS_PER_USER 64
+#define KEY_USER_BYTES ((ID_TOTAL_BLOCKS + CHAR_BIT - 1) / CHAR_BIT)
+
+static const char whitespace[] = " \t\n\v\f\r";
+
+enum id_block_state {
+	BLOCK_FREE,
+	BLOCK_ACTIVE,
+	BLOCK_QUARANTINED,
+	BLOCK_RETIRED,
+};
+
+struct id_block_slot {
+	enum id_block_state state;
+	uint32_t owner_uid;
+};
+
+static void record_key_user(char *line, unsigned char *blocks)
+{
+	char *colon, *start, *end;
+	unsigned long uid;
+	size_t idx;
+
+	colon = strchr(line, ':');
+	if (!colon)
+		return;
+	*colon = '\0';
+	start = line + strspn(line, whitespace);
+	if (*start < '0' || *start > '9')
+		return;
+	errno = 0;
+	uid = strtoul(start, &end, 10);
+	if (errno || end + strspn(end, whitespace) != colon ||
+	    uid < ID_WINDOW_FIRST || uid > ID_WINDOW_LAST)
+		return;
+	idx = (uid - ID_WINDOW_FIRST) / ID_BLOCK_SIZE;
+	blocks[idx / CHAR_BIT] |= 1U << (idx % CHAR_BIT);
+}
+
+static int read_key_users(unsigned char *blocks)
+{
+	char *line = NULL;
+	size_t cap = 0;
+	FILE *file;
+	int ret = 0;
+
+	file = fopen("/proc/key-users", "re");
+	if (!file) {
+		/* Kernels without key support do not expose this file. */
+		if (errno == ENOENT)
+			return 0;
+		return err_ctx(-errno, "open /proc/key-users");
+	}
+	for (;;) {
+		errno = 0;
+		if (getline(&line, &cap, file) < 0) {
+			if (!feof(file))
+				ret = err_ctx(errno ? -errno : -EIO,
+					      "read /proc/key-users");
+			break;
+		}
+		record_key_user(line, blocks);
+	}
+	free(line);
+	fclose(file);
+	return ret;
+}
+
+static bool block_has_key_user(const unsigned char *blocks, size_t idx)
+{
+	return blocks[idx / CHAR_BIT] & (1U << (idx % CHAR_BIT));
+}
+
+static void reclaim_quarantined(struct id_block_pool *pool,
+				const unsigned char *blocks)
+{
+	struct id_block_slot *slot;
+	size_t idx;
+
+	for (idx = 0; idx < ID_TOTAL_BLOCKS; idx++) {
+		slot = &pool->slots[idx];
+		if (slot->state == BLOCK_QUARANTINED &&
+		    !block_has_key_user(blocks, idx))
+			*slot = (struct id_block_slot){0};
+	}
+}
+
+int id_block_pool_init(struct id_block_pool *pool)
+{
+	struct id_block_slot *slots;
+
+	slots = calloc(ID_TOTAL_BLOCKS, sizeof(*slots));
+	if (!slots)
+		return err_ctx(-ENOMEM, "allocate cell ID block pool");
+	*pool = (struct id_block_pool){
+		.max_per_user = DEFAULT_MAX_BLOCKS_PER_USER,
+		.slots = slots,
+	};
+	return 0;
+}
+
+void id_block_pool_destroy(struct id_block_pool *pool)
+{
+	free(pool->slots);
+	*pool = (struct id_block_pool){0};
+}
+
+int id_block_allocate(struct id_block_pool *pool, uint32_t owner_uid,
+		      struct id_block_lease *out)
+{
+	unsigned char blocks[KEY_USER_BYTES] = {0};
+	struct id_block_slot *slot;
+	size_t idx, offset, used = 0;
+	int ret;
+
+	ret = read_key_users(blocks);
+	if (ret)
+		return ret;
+	reclaim_quarantined(pool, blocks);
+
+	/* Retired blocks continue to count against their original owner. */
+	for (idx = 0; idx < ID_TOTAL_BLOCKS; idx++) {
+		slot = &pool->slots[idx];
+		if (slot->state != BLOCK_FREE && slot->owner_uid == owner_uid)
+			used++;
+	}
+	if (used >= pool->max_per_user)
+		return err_ctx(-ENOSPC, "per-user cell ID block limit reached");
+
+	for (offset = 0; offset < ID_TOTAL_BLOCKS; offset++) {
+		idx = (pool->next_cursor + offset) % ID_TOTAL_BLOCKS;
+		slot = &pool->slots[idx];
+		if (slot->state != BLOCK_FREE ||
+		    block_has_key_user(blocks, idx))
+			continue;
+		*slot = (struct id_block_slot){BLOCK_ACTIVE, owner_uid};
+		pool->next_cursor = (idx + 1) % ID_TOTAL_BLOCKS;
+		*out = (struct id_block_lease){
+			.first_id = ID_WINDOW_FIRST + idx * ID_BLOCK_SIZE,
+			.owner_uid = owner_uid,
+		};
+		return 0;
+	}
+	return err_ctx(-ENOSPC, "no free 65536-ID cell blocks available");
+}
+
+void id_block_release(struct id_block_pool *pool,
+		      const struct id_block_lease *lease, bool retire)
+{
+	unsigned char blocks[KEY_USER_BYTES] = {0};
+	struct id_block_slot *slot;
+	uint32_t offset;
+
+	if (lease->first_id < ID_WINDOW_FIRST ||
+	    lease->first_id > ID_WINDOW_LAST)
+		return;
+	offset = lease->first_id - ID_WINDOW_FIRST;
+	if (offset % ID_BLOCK_SIZE)
+		return;
+	slot = &pool->slots[offset / ID_BLOCK_SIZE];
+	if (slot->state != BLOCK_ACTIVE || slot->owner_uid != lease->owner_uid)
+		return;
+	if (retire) {
+		slot->state = BLOCK_RETIRED;
+		return;
+	}
+	slot->state = BLOCK_QUARANTINED;
+	if (!read_key_users(blocks))
+		reclaim_quarantined(pool, blocks);
+}
diff --git a/src/ids.h b/src/ids.h
new file mode 100644
index 0000000..3254d6d
--- /dev/null
+++ b/src/ids.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_IDS_H
+#define UCELLS_IDS_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+struct id_block_slot;
+
+struct id_block_lease {
+	uint32_t first_id;
+	uint32_t owner_uid;
+};
+
+struct id_block_pool {
+	uint16_t max_per_user;
+	struct id_block_slot *slots;
+	size_t next_cursor;
+};
+
+int id_block_pool_init(struct id_block_pool *pool);
+void id_block_pool_destroy(struct id_block_pool *pool);
+int id_block_allocate(struct id_block_pool *pool, uint32_t owner_uid,
+		      struct id_block_lease *out);
+void id_block_release(struct id_block_pool *pool,
+		      const struct id_block_lease *lease, bool retire);
+
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..24fb48c
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/signalfd.h>
+
+#include "base.h"
+#include "fdpass.h"
+#include "notify.h"
+#include "supervisor.h"
+#include "tracee.h"
+
+static void print_error(int err)
+{
+	if (ucells_error)
+		fprintf(stderr, "ucells: %s: %s\n", ucells_error,
+			strerror(-err));
+	else
+		fprintf(stderr, "ucells: %s\n", strerror(-err));
+}
+
+[[noreturn]] static void exec_tracee(int socket, const sigset_t *original_mask,
+				     char *const command[])
+{
+	int listener, ret;
+
+	if (sigprocmask(SIG_SETMASK, original_mask, NULL) < 0) {
+		ret = err_ctx(-errno, "restore tracee signal mask");
+		goto fail;
+	}
+
+	listener = install_cells_notif_filter();
+	if (listener < 0) {
+		ret = listener;
+		goto fail;
+	}
+
+	ret = send_fd(socket, listener);
+	if (ret < 0)
+		goto fail;
+	close_fd(listener);
+	close_fd(socket);
+
+	execvp(command[0], command);
+	fprintf(stderr, "ucells: exec %s: %s\n", command[0], strerror(errno));
+	_exit(127);
+
+fail:
+	print_error(ret);
+	_exit(126);
+}
+
+static int run(char *const command[])
+{
+	struct supervisor *supervisor;
+	struct tracee tracee;
+	sigset_t mask, original_mask;
+	int listener, signals, sockets[2];
+	pid_t pid;
+	int ret;
+
+	ret = supervisor_create(&supervisor);
+	if (ret < 0)
+		return ret;
+
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+	sigaddset(&mask, SIGTERM);
+	sigaddset(&mask, SIGCHLD);
+	if (sigprocmask(SIG_BLOCK, &mask, &original_mask) < 0) {
+		ret = err_ctx(-errno, "block supervisor signals");
+		goto destroy_supervisor;
+	}
+
+	signals = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK);
+	if (signals < 0) {
+		ret = err_ctx(-errno, "create signalfd");
+		goto destroy_supervisor;
+	}
+
+	ret = fd_pair(sockets);
+	if (ret < 0)
+		goto close_signals;
+
+	pid = fork();
+	if (pid < 0) {
+		ret = err_ctx(-errno, "fork tracee");
+		close_fd(sockets[1]);
+		goto close_parent;
+	}
+	if (!pid) {
+		close_fd(sockets[0]);
+		close_fd(signals);
+		/* Only the parent tears down the supervisor's BPF links. */
+		exec_tracee(sockets[1], &original_mask, command);
+	}
+
+	tracee.pid = pid;
+	close_fd(sockets[1]);
+	listener = recv_fd(sockets[0]);
+	if (listener < 0) {
+		ret = listener;
+		goto destroy_tracee;
+	}
+	close_fd(take_fd(&sockets[0]));
+
+	ret = supervisor_run(supervisor, &tracee, listener, signals);
+	close_fd(listener);
+destroy_tracee:
+	tracee_destroy(&tracee);
+close_parent:
+	close_fd(sockets[0]);
+close_signals:
+	close_fd(signals);
+destroy_supervisor:
+	supervisor_destroy(supervisor);
+	return ret;
+}
+
+static const char usage[] = "Usage: ucells [OPTIONS] [--] <COMMAND>...\n";
+
+int main(int argc, char *argv[])
+{
+	int command = 1;
+	int ret;
+
+	if (argc > 1) {
+		const char *option = argv[1];
+
+		if (!strcmp(option, "-h") || !strcmp(option, "--help")) {
+			printf("Run Cells v3 programs on Linux\n\n%s\n"
+			       "Options:\n  -h, --help     Print help\n"
+			       "  -V, --version  Print version\n",
+			       usage);
+			return 0;
+		}
+		if (!strcmp(option, "-V") || !strcmp(option, "--version")) {
+			puts("ucells " UCELLS_VERSION);
+			return 0;
+		}
+		if (!strcmp(option, "--")) {
+			command++;
+		} else if (option[0] == '-' && option[1]) {
+			fprintf(stderr, "ucells: unexpected option '%s'\n%s",
+				option, usage);
+			return 2;
+		}
+	}
+	if (command >= argc) {
+		fprintf(stderr, "ucells: missing command\n%s", usage);
+		return 2;
+	}
+
+	ret = run(argv + command);
+	if (ret < 0) {
+		print_error(ret);
+		return 1;
+	}
+	return ret;
+}
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 0000000..9bebe65
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/uio.h>
+
+#include "base.h"
+#include "memory.h"
+#include "notify.h"
+#include "uapi.h"
+
+#define TRACEE_READ_PAGE_SIZE 4096
+
+static int check_range(uint32_t pid, uint64_t addr)
+{
+	if (!addr || addr > UINTPTR_MAX)
+		return -EFAULT;
+	if (pid > INT_MAX)
+		return -EINVAL;
+	return 0;
+}
+
+static int read_bytes(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		      void *out, size_t len)
+{
+	struct iovec local = {.iov_base = out, .iov_len = len};
+	struct iovec remote = {
+		.iov_base = (void *)(uintptr_t)addr,
+		.iov_len = len,
+	};
+	ssize_t n;
+
+	n = process_vm_readv((pid_t)pid, &local, 1, &remote, 1, 0);
+	if (n < 0)
+		return -errno;
+	if ((size_t)n != len)
+		return -EFAULT;
+	return notif_is_id_valid(listener, id) ? 0 : -EINTR;
+}
+
+int read_tracee_bytes(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		      void *out, size_t len)
+{
+	int ret;
+
+	if (!len)
+		return 0;
+	ret = check_range(pid, addr);
+	if (ret < 0)
+		return ret;
+	return read_bytes(listener, id, pid, addr, out, len);
+}
+
+int read_tracee_alloc(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		      size_t len, void **out)
+{
+	void *data;
+	int ret;
+
+	if (!len) {
+		*out = NULL;
+		return 0;
+	}
+	ret = check_range(pid, addr);
+	if (ret < 0)
+		return ret;
+	data = malloc(len);
+	if (!data)
+		return -ENOMEM;
+	ret = read_bytes(listener, id, pid, addr, data, len);
+	if (ret < 0) {
+		free(data);
+		return ret;
+	}
+
+	*out = data;
+	return 0;
+}
+
+int read_tracee_cstring(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+			char *out, size_t max_len)
+{
+	size_t offset = 0;
+	uintptr_t ptr;
+	int ret;
+
+	ret = check_range(pid, addr);
+	if (ret < 0)
+		return ret;
+	ptr = (uintptr_t)addr;
+	while (offset < max_len) {
+		size_t page_rem = TRACEE_READ_PAGE_SIZE -
+				  (ptr & (TRACEE_READ_PAGE_SIZE - 1));
+		size_t len = max_len - offset;
+		struct iovec local, remote;
+		const char *nul;
+		ssize_t n;
+
+		if (len > page_rem)
+			len = page_rem;
+		local = (struct iovec){.iov_base = out + offset,
+				       .iov_len = len};
+		remote =
+			(struct iovec){.iov_base = (void *)ptr, .iov_len = len};
+		n = process_vm_readv((pid_t)pid, &local, 1, &remote, 1, 0);
+		if (n < 0)
+			return -errno;
+		if (!n)
+			return -EFAULT;
+		nul = memchr(out + offset, 0, (size_t)n);
+		if (nul) {
+			if (!notif_is_id_valid(listener, id))
+				return -EINTR;
+			return valid_utf8(out, (size_t)(nul - out)) ? 0
+								    : -EINVAL;
+		}
+		offset += (size_t)n;
+		if (offset < max_len && (uintptr_t)n > UINTPTR_MAX - ptr)
+			return -EFAULT;
+		ptr += (uintptr_t)n;
+	}
+
+	return -ENAMETOOLONG;
+}
+
+int write_tracee_bytes(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		       const void *bytes, size_t len)
+{
+	struct iovec local = {.iov_base = (void *)bytes, .iov_len = len};
+	struct iovec remote = {
+		.iov_base = (void *)(uintptr_t)addr,
+		.iov_len = len,
+	};
+	ssize_t n;
+	int ret;
+
+	if (!addr)
+		return -EFAULT;
+	if (!notif_is_id_valid(listener, id))
+		return -EINTR;
+	ret = check_range(pid, addr);
+	if (ret < 0)
+		return ret;
+	n = process_vm_writev((pid_t)pid, &local, 1, &remote, 1, 0);
+	if (n < 0)
+		return -errno;
+	return (size_t)n == len ? 0 : -EFAULT;
+}
+
+int dup_tracee_fd(int listener, uint64_t id, uint32_t pid, int remote_fd)
+{
+	int pidfd, fd;
+
+	if (remote_fd < 0)
+		return -EBADF;
+	if (pid > INT_MAX)
+		return -EINVAL;
+
+	/* A notification identifies a thread; PIDFD_THREAD is O_EXCL. */
+	pidfd = syscall(__NR_pidfd_open, (pid_t)pid, O_EXCL);
+	if (pidfd < 0)
+		return -errno;
+	fd = syscall(__NR_pidfd_getfd, pidfd, remote_fd, 0U);
+	close_fd(pidfd);
+	if (fd < 0)
+		return -errno;
+	if (!notif_is_id_valid(listener, id)) {
+		close_fd(fd);
+		return -EINTR;
+	}
+
+	return fd;
+}
diff --git a/src/memory.h b/src/memory.h
new file mode 100644
index 0000000..d3e604f
--- /dev/null
+++ b/src/memory.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_MEMORY_H
+#define UCELLS_MEMORY_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+int read_tracee_bytes(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		      void *out, size_t len);
+int read_tracee_alloc(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		      size_t len, void **out);
+int read_tracee_cstring(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+			char *out, size_t max_len);
+int write_tracee_bytes(int listener, uint64_t id, uint32_t pid, uint64_t addr,
+		       const void *bytes, size_t len);
+int dup_tracee_fd(int listener, uint64_t id, uint32_t pid, int remote_fd);
+
+#endif
diff --git a/src/notify.c b/src/notify.c
new file mode 100644
index 0000000..c758125
--- /dev/null
+++ b/src/notify.c
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <linux/mount.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/prctl.h>
+
+#include "base.h"
+#include "notify.h"
+#include "seccomp_filter.h"
+#include "uapi.h"
+
+int install_cells_notif_filter(void)
+{
+	static struct sock_filter filter[] = {
+		SC_LOAD(arch),
+		SC_EQ(SC_AUDIT_ARCH, 1, 0),
+		SC_RET(SECCOMP_RET_KILL_THREAD),
+		SC_LOAD(nr),
+#ifdef __x86_64__
+		/* Reject x32, except for the skipped-syscall marker (-1). */
+		BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, __X32_SYSCALL_BIT, 0, 2),
+		SC_EQ(-1U, 1, 0),
+		SC_RET(SECCOMP_RET_KILL_THREAD),
+#endif
+		SC_EQ(SYS_CELL_CREATE, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(SYS_CELL_CONFIGURE, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(SYS_CELL_INFO, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(SYS_CELL_OPEN, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(SYS_CELL_KILL, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(SYS_PIDFD_CONFIGURE, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_mount_setattr, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_open_tree_attr, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_openat, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_newfstatat, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_readlinkat, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_write, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+#ifdef __x86_64__
+		SC_EQ(__NR_open, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_stat, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_lstat, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_EQ(__NR_readlink, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+#endif
+		/* pidfd_open() with PIDFD_EMPTY. */
+		SC_EQ(__NR_pidfd_open, 0, 4),
+		SC_ARG_LO(1),
+		BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, PIDFD_EMPTY, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_RET(SECCOMP_RET_ALLOW),
+
+		/* fsconfig() with FSCONFIG_SET_FD. */
+		SC_EQ(__NR_fsconfig, 0, 3),
+		SC_ARG_LO(1),
+		SC_EQ(FSCONFIG_SET_FD, 0, 1),
+		SC_RET(SECCOMP_RET_USER_NOTIF),
+		SC_RET(SECCOMP_RET_ALLOW),
+	};
+	const struct sock_fprog prog = {
+		.len = ARRAY_SIZE(filter),
+		.filter = filter,
+	};
+	uint32_t action = SECCOMP_RET_USER_NOTIF;
+	long fd;
+
+	if (syscall(__NR_seccomp, SECCOMP_GET_ACTION_AVAIL, 0U, &action) < 0)
+		return err_ctx(-errno,
+			       "kernel lacks SECCOMP_RET_USER_NOTIF support");
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1L, 0L, 0L, 0L) < 0)
+		return err_ctx(-errno, "PR_SET_NO_NEW_PRIVS");
+	fd = syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER,
+		     SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog);
+	if (fd < 0)
+		return err_ctx(-errno, "install seccomp user-notif filter");
+	return fd;
+}
+
+static int notif_ioctl(int listener, unsigned long request, void *arg)
+{
+	int ret;
+
+	do {
+		ret = ioctl(listener, request, arg);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret < 0 ? -errno : ret;
+}
+
+int notif_recv(int listener, struct seccomp_notif *out)
+{
+	int ret;
+
+	memset(out, 0, sizeof(*out));
+	ret = notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, out);
+	if (ret == -ENOENT)
+		return 0;
+	return ret < 0 ? ret : 1;
+}
+
+bool notif_is_id_valid(int listener, uint64_t id)
+{
+	return notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &id) == 0;
+}
+
+int notif_add_fd(int listener, uint64_t id, int src_fd, uint32_t newfd,
+		 uint32_t flags, uint32_t newfd_flags)
+{
+	struct seccomp_notif_addfd req = {
+		.id = id,
+		.flags = flags,
+		.srcfd = (uint32_t)src_fd,
+		.newfd = newfd,
+		.newfd_flags = newfd_flags,
+	};
+
+	if (src_fd < 0)
+		return -EBADF;
+
+	return notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &req);
+}
+
+static int notif_send(int listener, struct seccomp_notif_resp *resp)
+{
+	int ret = notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp);
+
+	return ret == -ENOENT ? 0 : ret;
+}
+
+int notif_send_value(int listener, uint64_t id, int64_t val)
+{
+	struct seccomp_notif_resp resp = {.id = id, .val = val};
+
+	return notif_send(listener, &resp);
+}
+
+int notif_send_errno(int listener, uint64_t id, int err)
+{
+	struct seccomp_notif_resp resp = {.id = id, .error = err};
+
+	return notif_send(listener, &resp);
+}
+
+int notif_send_continue(int listener, uint64_t id)
+{
+	struct seccomp_notif_resp resp = {
+		.id = id,
+		.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE,
+	};
+
+	return notif_send(listener, &resp);
+}
diff --git a/src/notify.h b/src/notify.h
new file mode 100644
index 0000000..e8cf764
--- /dev/null
+++ b/src/notify.h
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_NOTIFY_H
+#define UCELLS_NOTIFY_H
+
+#include <linux/seccomp.h>
+#include <stdint.h>
+
+struct dispatch_outcome {
+	enum {
+		DISPATCH_CONTINUE,
+		DISPATCH_VALUE,
+		DISPATCH_SEND_FD,
+		DISPATCH_REPLACE_FD,
+	} type;
+	union {
+		int64_t value;
+		struct {
+			int fd;
+			bool cloexec;
+		};
+		struct {
+			int src;
+			uint32_t target_num;
+		};
+	};
+};
+
+int install_cells_notif_filter(void);
+int notif_recv(int listener, struct seccomp_notif *out);
+bool notif_is_id_valid(int listener, uint64_t id);
+int notif_add_fd(int listener, uint64_t id, int src_fd, uint32_t newfd,
+		 uint32_t flags, uint32_t newfd_flags);
+int notif_send_value(int listener, uint64_t id, int64_t val);
+int notif_send_errno(int listener, uint64_t id, int err);
+int notif_send_continue(int listener, uint64_t id);
+
+#endif
diff --git a/src/runfd.c b/src/runfd.c
new file mode 100644
index 0000000..52ad518
--- /dev/null
+++ b/src/runfd.c
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/runfd.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <linux/wait.h>
+
+#include "base.h"
+#include "runfd.h"
+#include "spawn.h"
+#include "tree.h"
+#include "uapi.h"
+
+int runfd_get_ns_fd(int runfd, uint64_t type)
+{
+	int fd = ioctl(runfd, RUNFD_GET_NSFD, (unsigned long)type);
+
+	return fd < 0 ? err_ctx(-errno, "ioctl RUNFD_GET_NSFD") : fd;
+}
+
+int runfd_share_ns_fd(int runfd, int nsfd)
+{
+	if (nsfd < 0)
+		return err_ctx(-EBADF, "negative namespace fd");
+	if (ioctl(runfd, RUNFD_SHARE_NSFD, (unsigned long)nsfd) < 0)
+		return err_ctx(-errno, "ioctl RUNFD_SHARE_NSFD");
+	return 0;
+}
+
+int runfd_set_idmap(int runfd, const struct userns_idmap_extent *uid,
+		    const struct userns_idmap_extent *gid, uint32_t flags)
+{
+	struct userns_idmap_extent uid_ext = *uid, gid_ext = *gid;
+	struct userns_idmap map = {
+		.nr_uid_extents = 1,
+		.nr_gid_extents = 1,
+		.flags = flags,
+		.uid_extents = (uintptr_t)&uid_ext,
+		.gid_extents = (uintptr_t)&gid_ext,
+	};
+	int fd, ret = 0;
+
+	fd = runfd_get_ns_fd(runfd, CLONE_NEWUSER);
+	if (fd < 0)
+		return fd;
+	uid_ext.reserved = 0;
+	gid_ext.reserved = 0;
+	if (ioctl(fd, NS_SET_IDMAP, &map) < 0)
+		ret = err_ctx(-errno, "ioctl NS_SET_IDMAP");
+	close_fd(fd);
+	return ret;
+}
+
+int runfd_bind_fsconfig(int runfd, int fsfd)
+{
+	if (syscall(__NR_fsconfig, fsfd, FSCONFIG_SET_RUN, NULL, NULL, runfd) <
+	    0)
+		return err_ctx(-errno, "fsconfig FSCONFIG_SET_RUN");
+	return 0;
+}
+
+int runfd_seal_root(int runfd, const struct mount_tree *tree)
+{
+	int fd, ret;
+
+	fd = runfd_get_ns_fd(runfd, CLONE_NEWNS);
+	if (fd < 0)
+		return fd;
+	ret = mount_tree_seal_into(tree, fd);
+	close_fd(fd);
+	return ret;
+}
+
+int runfd_send_signal(int runfd, int sig)
+{
+	if (syscall(__NR_pidfd_send_signal, runfd, sig, NULL, 0U) < 0 &&
+	    errno != ESRCH)
+		return err_ctx(-errno, "pidfd_send_signal runfd");
+	return 0;
+}
+
+int runfd_poll_exit(int runfd, int *status)
+{
+	siginfo_t info = {};
+
+	if (syscall(__NR_waitid, P_RUNFD, runfd, &info,
+		    WEXITED | WNOHANG | WNOWAIT, NULL) < 0) {
+		if (errno == ECHILD || errno == EAGAIN)
+			return 0;
+		return err_ctx(-errno, "waitid P_RUNFD");
+	}
+	if (info.si_pid <= 0)
+		return 0;
+	*status = spawn_wait_status(&info);
+	return 1;
+}
+
+int runfd_spawn(int runfd, const struct spawn_plan *plan)
+{
+	int pidfd = -1;
+	struct clone_args args = {
+		.flags = CLONE_INTO_RUN | CLONE_PIDFD | CLONE_INTO_CGROUP |
+			 CLONE_AUTOREAP | CLONE_NNP | CLONE_CLEAR_SIGHAND,
+		.pidfd = (uintptr_t)&pidfd,
+		.cgroup = plan->cgroup_fd,
+		.runfd = runfd,
+	};
+	struct child_bootstrap boot;
+	int err_pipe[2], ret;
+	long child;
+
+	if (!plan->sandbox || !plan->argv || !plan->argv[0])
+		return err_ctx(-EINVAL, "incomplete spawn plan");
+	if (plan->cgroup_fd < 0)
+		return err_ctx(-EBADF, "negative cgroup fd");
+	ret = child_bootstrap_init(&boot, plan);
+	if (ret)
+		return ret;
+	if (pipe2(err_pipe, O_CLOEXEC) < 0) {
+		ret = err_ctx(-errno, "pipe2 spawn sync");
+		goto destroy_boot;
+	}
+
+	child = syscall(__NR_clone3, &args, sizeof(args));
+	if (child < 0) {
+		ret = err_ctx(-errno, "clone3 CLONE_INTO_RUN");
+		close_fd(err_pipe[1]);
+		close_fd(err_pipe[0]);
+		goto destroy_boot;
+	}
+	if (!child) {
+		close_fd(err_pipe[0]);
+		child_bootstrap_run(&boot, err_pipe[1]);
+	}
+
+	close_fd(err_pipe[1]);
+	child_bootstrap_destroy(&boot);
+	ret = await_child_exec(err_pipe[0], pidfd);
+	close_fd(err_pipe[0]);
+	if (ret < 0) {
+		close_fd(pidfd);
+		return ret;
+	}
+	return pidfd;
+
+destroy_boot:
+	child_bootstrap_destroy(&boot);
+	return ret;
+}
+
+static int open_runfd(void)
+{
+	long fd = syscall(__NR_runfd, RUNFD_AUTOKILL);
+
+	if (fd < 0)
+		return err_ctx(-errno, "runfd");
+	if (fd > INT_MAX)
+		return err_ctx(-EINVAL, "runfd fd overflow");
+	return fd;
+}
+
+int runfd_create(uint32_t *userns_inum)
+{
+	int fd, userns, type, ret;
+	struct stat st;
+
+	fd = open_runfd();
+	if (fd < 0)
+		return fd;
+	userns = runfd_get_ns_fd(fd, CLONE_NEWUSER);
+	if (userns < 0) {
+		ret = userns;
+		goto close_runfd;
+	}
+	type = ioctl(userns, NS_GET_NSTYPE);
+	if (type < 0) {
+		ret = err_ctx(-errno, "ioctl NS_GET_NSTYPE");
+		goto close_userns;
+	}
+	if (type != CLONE_NEWUSER) {
+		ret = err_ctx(-EINVAL,
+			      "runfd returned the wrong namespace type");
+		goto close_userns;
+	}
+	if (fstat(userns, &st) < 0) {
+		ret = err_ctx(-errno, "fstat userns");
+		goto close_userns;
+	}
+	if (st.st_ino > UINT32_MAX) {
+		ret = err_ctx(-EINVAL, "userns inode overflow");
+		goto close_userns;
+	}
+	*userns_inum = st.st_ino;
+	close_fd(userns);
+	return fd;
+
+close_userns:
+	close_fd(userns);
+close_runfd:
+	close_fd(fd);
+	return ret;
+}
+
+int runfd_probe(void)
+{
+	int fd, userns, ret = 0;
+
+	fd = open_runfd();
+	if (fd < 0)
+		return fd;
+	userns = runfd_get_ns_fd(fd, CLONE_NEWUSER);
+	if (userns < 0) {
+		ret = userns;
+		goto close_runfd;
+	}
+	if (ioctl(userns, NS_SET_IDMAP, NULL) >= 0)
+		ret = err_ctx(-EINVAL, "NS_SET_IDMAP accepted a null map");
+	else if (errno != EFAULT)
+		ret = err_ctx(-errno, "probe NS_SET_IDMAP");
+	close_fd(userns);
+close_runfd:
+	close_fd(fd);
+	return ret;
+}
diff --git a/src/runfd.h b/src/runfd.h
new file mode 100644
index 0000000..5efa278
--- /dev/null
+++ b/src/runfd.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_RUNFD_H
+#define UCELLS_RUNFD_H
+
+#include <stdint.h>
+
+struct mount_tree;
+struct spawn_plan;
+struct userns_idmap_extent;
+
+int runfd_probe(void);
+int runfd_create(uint32_t *userns_inum);
+int runfd_get_ns_fd(int runfd, uint64_t type);
+int runfd_share_ns_fd(int runfd, int nsfd);
+int runfd_set_idmap(int runfd, const struct userns_idmap_extent *uid,
+		    const struct userns_idmap_extent *gid, uint32_t flags);
+int runfd_bind_fsconfig(int runfd, int fsfd);
+int runfd_seal_root(int runfd, const struct mount_tree *tree);
+int runfd_send_signal(int runfd, int sig);
+int runfd_poll_exit(int runfd, int *status);
+int runfd_spawn(int runfd, const struct spawn_plan *plan);
+
+#endif
diff --git a/src/sandbox.c b/src/sandbox.c
new file mode 100644
index 0000000..304255c
--- /dev/null
+++ b/src/sandbox.c
@@ -0,0 +1,245 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <linux/landlock.h>
+#include <linux/sched.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/prctl.h>
+#include <sys/socket.h>
+
+#include "base.h"
+#include "sandbox.h"
+#include "seccomp_filter.h"
+#include "uapi.h"
+
+#define SOCK_TYPE_MASK 0x0f
+#define CLONE_NS_MASK                                                          \
+	(CLONE_NEWTIME | CLONE_NEWNS | CLONE_NEWCGROUP | CLONE_NEWUTS |        \
+	 CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWNET)
+#define LANDLOCK_SCOPE_ABI 6
+#define SC_ERRNO(err) (SECCOMP_RET_ERRNO | (err))
+
+static int build_filter(uint64_t net_rights, bool no_net,
+			struct sock_fprog *out)
+{
+	static const struct sock_filter common[] = {
+		SC_LOAD(arch),
+		SC_EQ(SC_AUDIT_ARCH, 1, 0),
+		SC_RET(SECCOMP_RET_KILL_PROCESS),
+		SC_LOAD(nr),
+#ifdef __x86_64__
+		/* Reject x32, except for the skipped-syscall marker (-1). */
+		BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, __X32_SYSCALL_BIT, 0, 2),
+		SC_EQ(-1U, 1, 0),
+		SC_RET(SECCOMP_RET_KILL_PROCESS),
+#endif
+		/* Force libc to fall back to clone(), whose flags we inspect.
+		 */
+		SC_EQ(__NR_clone3, 0, 1),
+		SC_RET(SC_ERRNO(ENOSYS)),
+		SC_EQ(__NR_unshare, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_setns, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_mount, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_umount2, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_pivot_root, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_open_tree, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_move_mount, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_fsopen, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_fsconfig, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_fsmount, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_mount_setattr, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_open_tree_attr, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_fchroot, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(SYS_CELL_CREATE, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(SYS_CELL_CONFIGURE, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(SYS_CELL_OPEN, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(SYS_CELL_KILL, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(SYS_PIDFD_CONFIGURE, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_init_module, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_finit_module, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_EQ(__NR_delete_module, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+#if __NR_runfd != SYS_CELL_CREATE
+		SC_EQ(__NR_runfd, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+#endif
+		/* Ignore the direction and size of the NS_SET_IDMAP request. */
+		SC_EQ(__NR_ioctl, 0, 4),
+		SC_ARG_LO(1),
+		BPF_STMT(BPF_ALU | BPF_AND | BPF_K,
+			 _IO(_IOC_TYPEMASK, _IOC_NRMASK)),
+		SC_EQ(_IO(NSIO, _IOC_NR(NS_SET_IDMAP)), 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_LOAD(nr),
+
+		SC_EQ(__NR_clone, 0, 3),
+		SC_ARG_LO(0),
+		BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, CLONE_NS_MASK, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+	};
+	static const struct sock_filter unix_only[] = {
+		SC_LOAD(nr),
+		SC_EQ(__NR_socket, 1, 0),
+		SC_EQ(__NR_socketpair, 0, 5),
+		SC_ARG_HI(0),
+		SC_EQ(0, 0, 2),
+		SC_ARG_LO(0),
+		SC_EQ(AF_UNIX, 1, 0),
+		SC_RET(SC_ERRNO(EAFNOSUPPORT)),
+	};
+	static const struct sock_filter no_raw[] = {
+		SC_LOAD(nr),
+		SC_EQ(__NR_socket, 0, 7),
+		SC_ARG_LO(1),
+		BPF_STMT(BPF_ALU | BPF_AND | BPF_K, SOCK_TYPE_MASK),
+		SC_EQ(SOCK_RAW, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+		SC_ARG_LO(0),
+		SC_EQ(AF_PACKET, 0, 1),
+		SC_RET(SC_ERRNO(EPERM)),
+	};
+	static const struct sock_filter allow = SC_RET(SECCOMP_RET_ALLOW);
+	const struct sock_filter *network = NULL;
+	struct sock_filter *filter;
+	size_t net_len = 0;
+
+	if (no_net) {
+		network = unix_only;
+		net_len = ARRAY_SIZE(unix_only);
+	} else if (!(net_rights & CELL_NET_RAW)) {
+		network = no_raw;
+		net_len = ARRAY_SIZE(no_raw);
+	}
+
+	out->len = ARRAY_SIZE(common) + net_len + 1;
+	filter = malloc(out->len * sizeof(*filter));
+	if (!filter)
+		return err_ctx(-ENOMEM, "allocate cell seccomp filter");
+	memcpy(filter, common, sizeof(common));
+	if (net_len)
+		memcpy(filter + ARRAY_SIZE(common), network,
+		       net_len * sizeof(*filter));
+	filter[out->len - 1] = allow;
+	out->filter = filter;
+	return 0;
+}
+
+int cell_sandbox_compile(uint64_t caps, uint64_t net_rights, bool no_net,
+			 struct cell_sandbox *out)
+{
+	int ret;
+
+	ret = build_filter(net_rights, no_net, &out->prog);
+	if (ret)
+		return ret;
+	out->caps = caps;
+	return 0;
+}
+
+void cell_sandbox_destroy(struct cell_sandbox *sandbox)
+{
+	free(sandbox->prog.filter);
+	*sandbox = (struct cell_sandbox){};
+}
+
+static int last_error(void)
+{
+	return errno > 0 ? -errno : -EIO;
+}
+
+static int apply_capabilities(uint64_t caps)
+{
+	struct __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
+	uint32_t low = caps, high = caps >> 32;
+	struct __user_cap_data_struct data[2] = {{low, low, 0},
+						 {high, high, 0}};
+	unsigned long cap;
+	int present;
+
+	prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
+	for (cap = 0; cap <= CAP_LAST_CAP; cap++) {
+		if (caps & (1ULL << cap))
+			continue;
+		present = prctl(PR_CAPBSET_READ, cap, 0, 0, 0);
+		if (present < 0) {
+			if (errno == EINVAL)
+				break;
+			return last_error();
+		}
+		if (present && prctl(PR_CAPBSET_DROP, cap, 0, 0, 0) < 0 &&
+		    errno != EPERM)
+			return last_error();
+	}
+	if (syscall(__NR_capset, &hdr, data) < 0)
+		return last_error();
+	return 0;
+}
+
+int cell_sandbox_apply(const struct cell_sandbox *sandbox,
+		       uint32_t effective_uid)
+{
+	int ret;
+
+	ret = apply_capabilities(effective_uid ? 0 : sandbox->caps);
+	if (ret)
+		return ret;
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
+		return last_error();
+	if (syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0U, &sandbox->prog) <
+	    0)
+		return last_error();
+	return 0;
+}
+
+int landlock_prepare(void)
+{
+	struct landlock_ruleset_attr attr = {
+		.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
+			  LANDLOCK_SCOPE_SIGNAL,
+	};
+	long abi, fd;
+
+	abi = syscall(__NR_landlock_create_ruleset, NULL, 0,
+		      LANDLOCK_CREATE_RULESET_VERSION);
+	if (abi < LANDLOCK_SCOPE_ABI)
+		return -1;
+	fd = syscall(__NR_landlock_create_ruleset, &attr,
+		     offsetof(struct landlock_ruleset_attr, scoped) +
+			     sizeof(attr.scoped),
+		     0U);
+	return fd < 0 ? -1 : (int)fd;
+}
+
+void landlock_restrict(int fd)
+{
+	int saved_errno = errno;
+
+	if (fd < 0)
+		return;
+	prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+	syscall(__NR_landlock_restrict_self, fd, 0U);
+	close_fd(fd);
+	errno = saved_errno;
+}
diff --git a/src/sandbox.h b/src/sandbox.h
new file mode 100644
index 0000000..cb993cd
--- /dev/null
+++ b/src/sandbox.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_SANDBOX_H
+#define UCELLS_SANDBOX_H
+
+#include <linux/filter.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+struct cell_sandbox {
+	struct sock_fprog prog;
+	uint64_t caps;
+};
+
+int cell_sandbox_compile(uint64_t caps, uint64_t net_rights, bool no_net,
+			 struct cell_sandbox *out);
+void cell_sandbox_destroy(struct cell_sandbox *sandbox);
+int cell_sandbox_apply(const struct cell_sandbox *sandbox,
+		       uint32_t effective_uid);
+int landlock_prepare(void);
+void landlock_restrict(int fd);
+
+#endif
diff --git a/src/seccomp_filter.h b/src/seccomp_filter.h
new file mode 100644
index 0000000..1b49885
--- /dev/null
+++ b/src/seccomp_filter.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Opinsys Oy
+
+#ifndef UCELLS_SECCOMP_FILTER_H
+#define UCELLS_SECCOMP_FILTER_H
+
+#include <asm/unistd.h>
+#include <linux/audit.h>
+#include <linux/filter.h>
+#include <linux/seccomp.h>
+#include <stddef.h>
+
+#define SC_WORD(offset) BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offset))
+#define SC_OFFSET(field) offsetof(struct seccomp_data, field)
+#define SC_LOAD(field) SC_WORD(SC_OFFSET(field))
+#define SC_RET(action) BPF_STMT(BPF_RET | BPF_K, (action))
+#define SC_EQ(value, yes, no)                                                  \
+	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (value), (yes), (no))
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define SC_ARG_LO(n) SC_LOAD(args[n])
+#define SC_ARG_HI(n) SC_WORD(SC_OFFSET(args[n]) + 4)
+#else
+#define SC_ARG_LO(n) SC_WORD(SC_OFFSET(args[n]) + 4)
+#define SC_ARG_HI(n) SC_LOAD(args[n])
+#endif
+
+#if defined(__x86_64__) && !defined(__ILP32__)
+#define SC_AUDIT_ARCH AUDIT_ARCH_X86_64
+#elif defined(__aarch64__) && !defined(__ILP32__)
+#define SC_AUDIT_ARCH AUDIT_ARCH_AARCH64
+#else
+#error "ucells seccomp requires x86-64 or AArch64 Linux"
+#endif
+
+#endif
diff --git a/src/spawn.c b/src/spawn.c
new file mode 100644
index 0000000..d51a824
--- /dev/null
+++ b/src/spawn.c
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <fcntl.h>
+#include <grp.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "sandbox.h"
+#include "spawn.h"
+#include "uapi.h"
+
+static void report_child_error(int fd, int err)
+{
+	int code = -err;
+
+	while (write(fd, &code, sizeof(code)) < 0 && errno == EINTR)
+		;
+}
+
+static int reset_signals_and_session(void)
+{
+	struct sigaction sa = {.sa_handler = SIG_DFL};
+	sigset_t empty;
+	int sig;
+
+	for (sig = 1; sig < 64; sig++) {
+		if (sig != SIGKILL && sig != SIGSTOP)
+			sigaction(sig, &sa, NULL);
+	}
+	sigemptyset(&empty);
+	sigprocmask(SIG_SETMASK, &empty, NULL);
+	if (setsid() < 0 && errno != EPERM)
+		return -errno;
+	return 0;
+}
+
+static int apply_credentials(const struct cred_spec *cred, bool allow_groups)
+{
+	if (cred->nr_groups) {
+		if (!allow_groups)
+			return -EPERM;
+		static_assert(sizeof(gid_t) == sizeof(uint32_t));
+		if (setgroups(cred->nr_groups, (const gid_t *)cred->groups) < 0)
+			return -errno;
+	} else if (allow_groups) {
+		setgroups(0, NULL);
+	}
+	if (setresgid(cred->gid, cred->gid, cred->gid) < 0)
+		return -errno;
+	if (setresuid(cred->uid, cred->uid, cred->uid) < 0)
+		return -errno;
+	return 0;
+}
+
+static int close_all_except(const int *kept, size_t count, int err_fd)
+{
+	unsigned int cursor = 0;
+	size_t i;
+
+	/* The error pipe is above every staged target. */
+	for (i = 0; i < count; i++) {
+		unsigned int fd = kept[i];
+
+		if (fd < cursor)
+			continue;
+		if (fd > cursor &&
+		    syscall(__NR_close_range, cursor, fd - 1, 0U) < 0)
+			return -errno;
+		cursor = fd + 1;
+	}
+	if ((unsigned int)err_fd > cursor &&
+	    syscall(__NR_close_range, cursor, (unsigned int)err_fd - 1, 0U) < 0)
+		return -errno;
+	if (syscall(__NR_close_range, (unsigned int)err_fd + 1, UINT_MAX, 0U) <
+	    0)
+		return -errno;
+	return 0;
+}
+
+static int child_steps(const struct child_bootstrap *boot, int err_fd)
+{
+	const struct spawn_plan *plan = boot->plan;
+	const char *hostname = plan->hostname ? plan->hostname : "";
+	char *const empty_env[] = {NULL};
+	uint32_t target_uid;
+	size_t i;
+	int ret;
+
+	ret = reset_signals_and_session();
+	if (ret)
+		return ret;
+	if (plan->is_init && sethostname(hostname, strlen(hostname)) < 0 &&
+	    errno != EPERM)
+		return -errno;
+
+	landlock_restrict(boot->landlock_fd);
+
+	for (i = 0; i < plan->nr_staged_fds; i++) {
+		ret = fcntl(plan->staged_fds[i].source_fd, F_DUPFD_CLOEXEC,
+			    err_fd + 1);
+		if (ret < 0)
+			return -errno;
+		boot->temp_slots[i] = ret;
+	}
+	for (i = 0; i < plan->nr_staged_fds; i++) {
+		int dst = plan->staged_fds[i].target;
+
+		if (dup2(boot->temp_slots[i], dst) < 0)
+			return -errno;
+	}
+
+	ret = close_all_except(boot->sorted_kept, plan->nr_staged_fds, err_fd);
+	if (ret)
+		return ret;
+	if (chdir("/") < 0)
+		return -errno;
+
+	if (plan->cred) {
+		ret = apply_credentials(plan->cred, plan->allow_setgroups);
+		if (ret)
+			return ret;
+		target_uid = plan->cred->uid;
+	} else {
+		target_uid = geteuid();
+	}
+	ret = cell_sandbox_apply(plan->sandbox, target_uid);
+	if (ret)
+		return ret;
+
+	execve(plan->exec_path, plan->argv,
+	       plan->envp ? plan->envp : empty_env);
+	return -errno;
+}
+
+static int compare_fds(const void *a, const void *b)
+{
+	int left = *(const int *)a;
+	int right = *(const int *)b;
+
+	return (left > right) - (left < right);
+}
+
+int child_bootstrap_init(struct child_bootstrap *boot,
+			 const struct spawn_plan *plan)
+{
+	size_t count = plan->nr_staged_fds;
+	int max_target = 3;
+	int *slots = NULL;
+	size_t i;
+
+	if (count) {
+		slots = calloc(count, sizeof(int[2]));
+		if (!slots)
+			return err_ctx(-ENOMEM, "allocate bootstrap fd slots");
+		for (i = 0; i < count; i++) {
+			slots[i] = plan->staged_fds[i].target;
+			if (slots[i] > max_target)
+				max_target = slots[i];
+		}
+		qsort(slots, count, sizeof(*slots), compare_fds);
+	}
+
+	*boot = (struct child_bootstrap){
+		.plan = plan,
+		.sorted_kept = slots,
+		.temp_slots = slots ? slots + count : NULL,
+		.safe_fd_base =
+			max_target > INT_MAX - 16 ? INT_MAX : max_target + 16,
+		.landlock_fd = landlock_prepare(),
+	};
+	return 0;
+}
+
+void child_bootstrap_destroy(struct child_bootstrap *boot)
+{
+	close_fd(take_fd(&boot->landlock_fd));
+	free(boot->sorted_kept);
+}
+
+/* The child must stay allocation-free and never return to its caller. */
+[[noreturn]] void child_bootstrap_run(const struct child_bootstrap *boot,
+				      int initial_err_fd)
+{
+	int err_fd, ret;
+
+	err_fd = fcntl(initial_err_fd, F_DUPFD_CLOEXEC, boot->safe_fd_base);
+	if (err_fd < 0) {
+		report_child_error(initial_err_fd, -errno);
+		_exit(126);
+	}
+	close_fd(initial_err_fd);
+
+	ret = child_steps(boot, err_fd);
+	if (ret)
+		report_child_error(err_fd, ret);
+	close_fd(err_fd);
+	_exit(127);
+}
+
+int await_child_exec(int err_read, int pidfd)
+{
+	int child_errno;
+	ssize_t n;
+
+	do {
+		n = read(err_read, &child_errno, sizeof(child_errno));
+	} while (n < 0 && errno == EINTR);
+
+	if (!n)
+		return 0;
+	if (n == sizeof(child_errno)) {
+		syscall(__NR_pidfd_send_signal, pidfd, SIGKILL, NULL, 0U);
+		return err_ctx(-child_errno, "cell task bootstrap failed");
+	}
+	if (n > 0)
+		return err_ctx(-EIO, "partial bootstrap status from child");
+	return err_ctx(-errno, "read bootstrap pipe");
+}
+
+int spawn_wait_status(const siginfo_t *info)
+{
+	int status = info->si_status;
+
+	switch (info->si_code) {
+	case CLD_EXITED:
+		return (status & 0xff) << 8;
+	case CLD_DUMPED:
+		return (status & 0x7f) | 0x80;
+	default:
+		return status & 0x7f;
+	}
+}
diff --git a/src/spawn.h b/src/spawn.h
new file mode 100644
index 0000000..fa635ae
--- /dev/null
+++ b/src/spawn.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_SPAWN_H
+#define UCELLS_SPAWN_H
+
+#include <signal.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base.h"
+
+struct cell_sandbox;
+struct cred_spec;
+
+struct staged_fd {
+	int target;
+	int source_fd;
+};
+
+struct spawn_plan {
+	const char *exec_path;
+	char *const *argv;
+	char *const *envp;
+	const struct cred_spec *cred;
+	bool allow_setgroups;
+	bool is_init;
+	const char *hostname;
+	const struct staged_fd *staged_fds;
+	size_t nr_staged_fds;
+	int cgroup_fd;
+	const struct cell_sandbox *sandbox;
+};
+
+struct child_bootstrap {
+	const struct spawn_plan *plan;
+	int *sorted_kept;
+	int *temp_slots;
+	int safe_fd_base;
+	int landlock_fd;
+};
+
+int child_bootstrap_init(struct child_bootstrap *boot,
+			 const struct spawn_plan *plan);
+void child_bootstrap_destroy(struct child_bootstrap *boot);
+[[noreturn]] void child_bootstrap_run(const struct child_bootstrap *boot,
+				      int initial_err_fd);
+int await_child_exec(int err_read, int pidfd);
+int spawn_wait_status(const siginfo_t *info);
+
+#endif
diff --git a/src/supervisor.c b/src/supervisor.c
new file mode 100644
index 0000000..47b519b
--- /dev/null
+++ b/src/supervisor.c
@@ -0,0 +1,845 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <ctype.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <linux/magic.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/epoll.h>
+#include <sys/queue.h>
+#include <sys/signalfd.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+
+#include "base.h"
+#include "cell.h"
+#include "handle.h"
+#include "handler.h"
+#include "runfd.h"
+#include "supervisor.h"
+#include "sysctl.h"
+#include "tracee.h"
+
+#define MAX_EPOLL_EVENTS 64
+#define LISTENER_TOKEN UINT64_C(1)
+#define SIGNAL_TOKEN UINT64_C(2)
+
+enum watch_type {
+	WATCH_CELL,
+	WATCH_RUNFD,
+	WATCH_EMBRYO,
+	WATCH_PIDNS,
+};
+
+struct watch {
+	TAILQ_ENTRY(watch) link;
+	uint64_t token;
+	enum watch_type type;
+};
+
+struct tracked_handle {
+	TAILQ_ENTRY(tracked_handle) link;
+	struct handle_pair pair;
+	struct watch watch;
+	uint64_t cell_id;
+};
+
+struct cell_entry {
+	TAILQ_ENTRY(cell_entry) link;
+	struct cell cell;
+	struct tracked_handle handle;
+	struct watch runfd_watch;
+	bool exit_synced;
+	bool refill_armed;
+};
+
+struct embryo_entry {
+	struct embryo embryo;
+	struct tracked_handle handle;
+};
+
+struct supervisor {
+	int epoll;
+	struct cell_resources resources;
+	struct sysctl sysctl;
+	uint64_t next_cell_id;
+	uint64_t next_token;
+
+	TAILQ_HEAD(, cell_entry) cells;
+	TAILQ_HEAD(, tracked_handle) handles;
+	TAILQ_HEAD(, watch) watches;
+};
+
+static bool status_id(const char *ids, int field, uint32_t *id)
+{
+	unsigned long val;
+	char *end;
+
+	for (int i = 0;; i++) {
+		ids += strspn(ids, " \t");
+		if (!isdigit((unsigned char)*ids))
+			return false;
+		errno = 0;
+		val = strtoul(ids, &end, 10);
+		if (errno || val > UINT32_MAX ||
+		    (*end && !isspace((unsigned char)*end)))
+			return false;
+		if (i == field) {
+			*id = val;
+			return true;
+		}
+		ids = end;
+	}
+}
+
+int caller_cred_from_pid(uint32_t pid, struct caller_cred *out)
+{
+	char path[sizeof("/proc/4294967295/status")];
+	bool seen_uid = false, seen_gid = false, valid = true;
+	struct caller_cred cred;
+	char *line = NULL;
+	size_t cap = 0;
+	int ret = 0;
+	FILE *file;
+
+	snprintf(path, sizeof(path), "/proc/%" PRIu32 "/status", pid);
+	file = fopen(path, "re");
+	if (!file)
+		return -errno;
+	while (!seen_uid || !seen_gid) {
+		errno = 0;
+		if (getline(&line, &cap, file) < 0) {
+			if (!feof(file))
+				ret = errno ? -errno : -EIO;
+			break;
+		}
+		if (!seen_uid && !strncmp(line, "Uid:", 4)) {
+			seen_uid = true;
+			valid = valid && status_id(line + 4, 1, &cred.uid) &&
+				status_id(line + 4, 3, &cred.fsuid);
+		} else if (!seen_gid && !strncmp(line, "Gid:", 4)) {
+			seen_gid = true;
+			valid = valid && status_id(line + 4, 1, &cred.gid);
+		}
+	}
+	free(line);
+	fclose(file);
+	if (ret < 0)
+		return ret;
+	if (!seen_uid || !seen_gid || !valid)
+		return -EINVAL;
+	*out = cred;
+	return 0;
+}
+
+static int epoll_control(int epoll, int op, int fd, uint32_t flags,
+			 uint64_t token)
+{
+	struct epoll_event event = {.events = flags, .data.u64 = token};
+
+	return epoll_ctl(epoll, op, fd, &event) < 0 ? -errno : 0;
+}
+
+static int register_watch(struct supervisor *sup, struct watch *watch,
+			  enum watch_type type, int fd, uint32_t flags)
+{
+	uint64_t token = sup->next_token;
+	int ret;
+
+	/* Never recycle tokens: a queued event may outlive its registration. */
+	if (token == UINT64_MAX)
+		return -EOVERFLOW;
+	sup->next_token++;
+	ret = epoll_control(sup->epoll, EPOLL_CTL_ADD, fd, flags, token);
+	if (ret < 0)
+		return ret;
+	watch->token = token;
+	watch->type = type;
+	TAILQ_INSERT_TAIL(&sup->watches, watch, link);
+	return 0;
+}
+
+static void unregister_watch(struct supervisor *sup, struct watch *watch,
+			     int fd)
+{
+	if (!watch->token)
+		return;
+	TAILQ_REMOVE(&sup->watches, watch, link);
+	watch->token = 0;
+	if (fd >= 0)
+		epoll_ctl(sup->epoll, EPOLL_CTL_DEL, fd, NULL);
+}
+
+static int register_handle(struct supervisor *sup,
+			   struct tracked_handle *handle, enum watch_type type)
+{
+	int ret;
+
+	ret = register_watch(sup, &handle->watch, type,
+			     handle->pair.supervisor_fd, 0);
+	if (ret < 0)
+		return ret;
+	TAILQ_INSERT_TAIL(&sup->handles, handle, link);
+	return 0;
+}
+
+static void untrack_handle(struct supervisor *sup,
+			   struct tracked_handle *handle)
+{
+	unregister_watch(sup, &handle->watch, handle->pair.supervisor_fd);
+	TAILQ_REMOVE(&sup->handles, handle, link);
+}
+
+static struct cell_entry *cell_by_id(const struct supervisor *sup, uint64_t id)
+{
+	struct cell_entry *entry;
+
+	for (entry = TAILQ_FIRST(&sup->cells); entry;
+	     entry = TAILQ_NEXT(entry, link)) {
+		if (entry->cell.id == id)
+			return entry;
+	}
+	return NULL;
+}
+
+static uint64_t cell_id_of_pid(const struct supervisor *sup, uint32_t pid)
+{
+	char path[sizeof("/proc/4294967295/ns/user")];
+	struct cell_entry *entry;
+	struct stat st;
+
+	snprintf(path, sizeof(path), "/proc/%" PRIu32 "/ns/user", pid);
+	if (stat(path, &st) < 0 || st.st_ino > UINT32_MAX)
+		return 0;
+	for (entry = TAILQ_FIRST(&sup->cells); entry;
+	     entry = TAILQ_NEXT(entry, link)) {
+		if (entry->cell.state != CELL_DEAD &&
+		    entry->cell.userns_inum == st.st_ino)
+			return entry->cell.id;
+	}
+	return 0;
+}
+
+static struct tracked_handle *handle_by_identity(const struct supervisor *sup,
+						 const struct file_identity *id)
+{
+	struct tracked_handle *handle;
+
+	for (handle = TAILQ_FIRST(&sup->handles); handle;
+	     handle = TAILQ_NEXT(handle, link)) {
+		if (handle->pair.client_id.dev == id->dev &&
+		    handle->pair.client_id.ino == id->ino)
+			return handle;
+	}
+	return NULL;
+}
+
+static int lookup_handle(const struct supervisor *sup, int fd,
+			 enum watch_type type, struct tracked_handle **out)
+{
+	struct tracked_handle *handle;
+	struct file_identity id;
+	int ret;
+
+	ret = file_identity_of_fd(fd, &id);
+	if (ret < 0)
+		return ret;
+	handle = handle_by_identity(sup, &id);
+	if (!handle || handle->watch.type != type)
+		return type == WATCH_EMBRYO ? -EPERM : -EINVAL;
+	*out = handle;
+	return 0;
+}
+
+static int cell_by_fd(const struct supervisor *sup, int fd,
+		      struct cell_entry **out)
+{
+	struct tracked_handle *handle;
+	int ret;
+
+	ret = lookup_handle(sup, fd, WATCH_CELL, &handle);
+	if (ret < 0)
+		return ret;
+	*out = container_of(handle, struct cell_entry, handle);
+	return 0;
+}
+
+static void detach_runfd(struct supervisor *sup, struct cell_entry *entry)
+{
+	unregister_watch(sup, &entry->runfd_watch, entry->cell.runfd);
+}
+
+static void sync_post_death(struct supervisor *sup, struct cell_entry *entry)
+{
+	struct tracked_handle *handle = &entry->handle;
+	int status = entry->cell.exit_status;
+
+	if (entry->cell.state != CELL_DEAD)
+		return;
+	detach_runfd(sup, entry);
+	if (!entry->exit_synced) {
+		entry->exit_synced = true;
+		entry->refill_armed =
+			handle_push_exit_status(&handle->pair, status);
+	}
+	if (entry->refill_armed && handle->watch.token)
+		epoll_control(sup->epoll, EPOLL_CTL_MOD,
+			      handle->pair.supervisor_fd, EPOLLOUT,
+			      handle->watch.token);
+}
+
+static void kill_cell(struct supervisor *sup, struct cell_entry *entry)
+{
+	if (entry->cell.state == CELL_BUILDING ||
+	    entry->cell.state == CELL_SEALED)
+		detach_runfd(sup, entry);
+	cell_kill(&entry->cell, &sup->resources);
+	sync_post_death(sup, entry);
+}
+
+static void drop_cell(struct supervisor *sup, struct cell_entry *entry)
+{
+	detach_runfd(sup, entry);
+	untrack_handle(sup, &entry->handle);
+	TAILQ_REMOVE(&sup->cells, entry, link);
+	close_fd(entry->handle.pair.supervisor_fd);
+	free(entry);
+}
+
+static void handle_cell_event(struct supervisor *sup, struct cell_entry *entry,
+			      uint32_t events)
+{
+	struct tracked_handle *handle = &entry->handle;
+
+	if (events & (EPOLLHUP | EPOLLERR)) {
+		kill_cell(sup, entry);
+		if (entry->cell.state == CELL_DEAD)
+			drop_cell(sup, entry);
+		else
+			unregister_watch(sup, &handle->watch,
+					 handle->pair.supervisor_fd);
+		return;
+	}
+	if (!(events & EPOLLOUT) || entry->cell.state != CELL_DEAD)
+		return;
+	if (handle_push_exit_status(&handle->pair, entry->cell.exit_status))
+		return;
+	epoll_control(sup->epoll, EPOLL_CTL_MOD, handle->pair.supervisor_fd, 0,
+		      handle->watch.token);
+	entry->refill_armed = false;
+}
+
+static void handle_run_exit(struct supervisor *sup, struct cell_entry *entry)
+{
+	detach_runfd(sup, entry);
+	cell_on_run_exit(&entry->cell, &sup->resources);
+	sync_post_death(sup, entry);
+	if (!entry->handle.watch.token)
+		drop_cell(sup, entry);
+}
+
+static void destroy_embryo(struct embryo_entry *entry)
+{
+	embryo_destroy(&entry->embryo);
+	close_fd(entry->handle.pair.supervisor_fd);
+	free(entry);
+}
+
+static void drop_handle(struct supervisor *sup, struct tracked_handle *handle)
+{
+	untrack_handle(sup, handle);
+	if (handle->watch.type == WATCH_EMBRYO) {
+		struct embryo_entry *entry;
+
+		entry = container_of(handle, struct embryo_entry, handle);
+		destroy_embryo(entry);
+	} else {
+		close_fd(handle->pair.supervisor_fd);
+		free(handle);
+	}
+}
+
+int supervisor_create(struct supervisor **out)
+{
+	struct supervisor *sup;
+	int ret;
+
+	ret = runfd_probe();
+	if (ret < 0)
+		return ret;
+
+	sup = calloc(1, sizeof(*sup));
+	if (!sup)
+		return err_ctx(-ENOMEM, "allocate supervisor");
+	sup->next_cell_id = 1;
+	sup->next_token = SIGNAL_TOKEN + 1;
+	TAILQ_INIT(&sup->cells);
+	TAILQ_INIT(&sup->handles);
+	TAILQ_INIT(&sup->watches);
+	ret = bpf_lsm_load(&sup->resources.bpf_lsm);
+	if (ret < 0)
+		goto free_sup;
+	sup->epoll = epoll_create1(EPOLL_CLOEXEC);
+	if (sup->epoll < 0) {
+		ret = err_ctx(-errno, "epoll_create1");
+		goto destroy_bpf;
+	}
+	ret = sysctl_create(&sup->sysctl);
+	if (ret < 0) {
+		err_ctx(ret, "create synthetic quota sysctl");
+		goto close_epoll;
+	}
+	ret = id_block_pool_init(&sup->resources.id_pool);
+	if (ret < 0)
+		goto destroy_sysctl;
+	*out = sup;
+	return 0;
+
+destroy_sysctl:
+	sysctl_destroy(&sup->sysctl);
+close_epoll:
+	close_fd(sup->epoll);
+destroy_bpf:
+	bpf_lsm_destroy(&sup->resources.bpf_lsm);
+free_sup:
+	free(sup);
+	return ret;
+}
+
+void supervisor_destroy(struct supervisor *sup)
+{
+	const char *saved_error = ucells_error;
+
+	if (!sup)
+		return;
+	while (!TAILQ_EMPTY(&sup->cells)) {
+		struct cell_entry *entry = TAILQ_FIRST(&sup->cells);
+
+		kill_cell(sup, entry);
+		if (entry->cell.state != CELL_DEAD) {
+			detach_runfd(sup, entry);
+			cell_on_run_exit(&entry->cell, &sup->resources);
+		}
+		drop_cell(sup, entry);
+	}
+	while (!TAILQ_EMPTY(&sup->handles))
+		drop_handle(sup, TAILQ_FIRST(&sup->handles));
+	id_block_pool_destroy(&sup->resources.id_pool);
+	sysctl_destroy(&sup->sysctl);
+	close_fd(sup->epoll);
+	bpf_lsm_destroy(&sup->resources.bpf_lsm);
+	free(sup);
+	ucells_error = saved_error;
+}
+
+int supervisor_sysctl_notification(struct supervisor *sup, int listener,
+				   const struct seccomp_notif *notif,
+				   struct dispatch_outcome *out)
+{
+	uint16_t quota = sup->resources.id_pool.max_per_user;
+	int ret;
+
+	ret = sysctl_dispatch(&sup->sysctl, listener, notif, &quota, out);
+	if (ret >= 0)
+		sup->resources.id_pool.max_per_user = quota;
+	return ret;
+}
+
+static void dispatch_watch(struct supervisor *sup, uint64_t token,
+			   uint32_t events)
+{
+	struct cell_entry *entry;
+	struct tracked_handle *handle;
+	struct watch *watch;
+
+	for (watch = TAILQ_FIRST(&sup->watches); watch;
+	     watch = TAILQ_NEXT(watch, link)) {
+		if (watch->token == token)
+			break;
+	}
+	if (!watch)
+		return;
+	switch (watch->type) {
+	case WATCH_CELL:
+		handle = container_of(watch, struct tracked_handle, watch);
+		entry = container_of(handle, struct cell_entry, handle);
+		handle_cell_event(sup, entry, events);
+		break;
+	case WATCH_RUNFD:
+		if (events & EPOLLHUP)
+			handle_run_exit(sup,
+					container_of(watch, struct cell_entry,
+						     runfd_watch));
+		break;
+	case WATCH_EMBRYO:
+	case WATCH_PIDNS:
+		if (events & (EPOLLHUP | EPOLLERR))
+			drop_handle(sup,
+				    container_of(watch, struct tracked_handle,
+						 watch));
+		break;
+	}
+}
+
+int supervisor_run(struct supervisor *sup, struct tracee *tracee, int listener,
+		   int sigfd)
+{
+	struct epoll_event events[MAX_EPOLL_EVENTS];
+	int ret, count, exit_code;
+
+	ret = epoll_control(sup->epoll, EPOLL_CTL_ADD, listener, EPOLLIN,
+			    LISTENER_TOKEN);
+	if (ret < 0)
+		return err_ctx(ret, "register seccomp listener in epoll");
+	ret = epoll_control(sup->epoll, EPOLL_CTL_ADD, sigfd, EPOLLIN,
+			    SIGNAL_TOKEN);
+	if (ret < 0)
+		return err_ctx(ret, "register signalfd in epoll");
+
+	for (;;) {
+		ret = tracee_poll_exit(tracee, &exit_code);
+		if (ret)
+			return ret < 0 ? ret : exit_code;
+
+		count = epoll_wait(sup->epoll, events, MAX_EPOLL_EVENTS, -1);
+		if (count < 0) {
+			if (errno == EINTR)
+				continue;
+			return err_ctx(-errno, "epoll_wait");
+		}
+		for (int i = 0; i < count; i++) {
+			uint64_t token = events[i].data.u64;
+			uint32_t flags = events[i].events;
+
+			if (token == LISTENER_TOKEN) {
+				struct seccomp_notif notif;
+
+				if (!(flags & EPOLLIN))
+					continue;
+				ret = notif_recv(listener, &notif);
+				if (ret > 0)
+					ret = handle_notification(listener, sup,
+								  &notif);
+				if (ret < 0)
+					return ret;
+			} else if (token == SIGNAL_TOKEN) {
+				struct signalfd_siginfo info;
+				ssize_t n = read(sigfd, &info, sizeof(info));
+
+				if (n < 0) {
+					if (errno == EAGAIN || errno == EINTR)
+						continue;
+					return err_ctx(-errno, "read signalfd");
+				}
+				if ((size_t)n != sizeof(info))
+					return err_ctx(-EIO,
+						       "short signalfd read");
+				if (info.ssi_signo != SIGCHLD)
+					return 128 + (int)info.ssi_signo;
+			} else {
+				dispatch_watch(sup, token, flags);
+			}
+		}
+	}
+}
+
+int supervisor_cell_configure(struct supervisor *sup, int cfd,
+			      const struct slot_action *action, int *aux_fd,
+			      const struct caller_cred *caller)
+{
+	struct cell_entry *entry;
+	int ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	return cell_configure(&entry->cell, action, aux_fd, caller,
+			      &sup->resources);
+}
+
+int supervisor_cell_seal(struct supervisor *sup, int cfd,
+			 const struct caller_cred *caller)
+{
+	struct cell_entry *entry;
+	int ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	return cell_seal(&entry->cell, caller, &sup->resources);
+}
+
+int supervisor_cell_kill(struct supervisor *sup, int cfd)
+{
+	struct cell_entry *entry;
+	int ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	kill_cell(sup, entry);
+	return 0;
+}
+
+int supervisor_cell_idmap_fd(const struct supervisor *sup, int cfd)
+{
+	struct cell_entry *entry;
+	int ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	return cell_idmap_fd(&entry->cell);
+}
+
+int supervisor_cell_create(struct supervisor *sup, int32_t parent,
+			   uint32_t flags, uint32_t pid)
+{
+	struct caller_cred caller;
+	struct cell_entry *entry;
+	uint64_t id;
+	int client, ret;
+
+	if (flags || parent != CELL_SELF)
+		return -EINVAL;
+	if (cell_id_of_pid(sup, pid))
+		return -EPERM;
+	id = sup->next_cell_id;
+	if (id == UINT64_MAX)
+		return -EOVERFLOW;
+	sup->next_cell_id++;
+
+	ret = caller_cred_from_pid(pid, &caller);
+	if (ret < 0)
+		return ret;
+	entry = calloc(1, sizeof(*entry));
+	if (!entry)
+		return -ENOMEM;
+	ret = cell_init(&entry->cell, id, &caller);
+	if (ret < 0)
+		goto free_entry;
+	client = handle_pair_create(false, &entry->handle.pair);
+	if (client < 0) {
+		ret = client;
+		goto close_runfd;
+	}
+	ret = register_handle(sup, &entry->handle, WATCH_CELL);
+	if (ret < 0)
+		goto close_pair;
+	ret = register_watch(sup, &entry->runfd_watch, WATCH_RUNFD,
+			     entry->cell.runfd, EPOLLHUP);
+	if (ret < 0)
+		goto untrack;
+	TAILQ_INSERT_TAIL(&sup->cells, entry, link);
+	return client;
+
+untrack:
+	untrack_handle(sup, &entry->handle);
+close_pair:
+	close_fd(client);
+	close_fd(entry->handle.pair.supervisor_fd);
+close_runfd:
+	close_fd(entry->cell.runfd);
+free_entry:
+	free(entry);
+	return ret;
+}
+
+void supervisor_cell_info_self(const struct supervisor *sup, uint64_t mask,
+			       uint32_t pid, struct cell_info *out)
+{
+	*out = (struct cell_info){
+		.mask = mask & CELL_INFO_ID,
+		.supported_mask = CELL_INFO_ALL,
+		.id = cell_id_of_pid(sup, pid),
+	};
+}
+
+int supervisor_cell_info(const struct supervisor *sup, int cfd, uint64_t mask,
+			 struct cell_info *out)
+{
+	struct cell_entry *entry;
+	int ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	cell_snapshot(&entry->cell, mask, out);
+	return 0;
+}
+
+int supervisor_cell_open(struct supervisor *sup, int cfd, enum open_slot slot)
+{
+	struct tracked_handle *handle;
+	struct cell_entry *entry;
+	int client, ret;
+
+	ret = cell_by_fd(sup, cfd, &entry);
+	if (ret < 0)
+		return ret;
+	if (entry->cell.state != CELL_BUILDING || entry->cell.runfd < 0)
+		return -EBUSY;
+	if (slot == OPEN_SLOT_NET)
+		return cell_open_net(&entry->cell);
+	if (slot != OPEN_SLOT_PIDS)
+		return -EINVAL;
+
+	handle = calloc(1, sizeof(*handle));
+	if (!handle)
+		return -ENOMEM;
+	client = handle_pair_create(false, &handle->pair);
+	if (client < 0) {
+		ret = client;
+		goto free_handle;
+	}
+	handle->cell_id = entry->cell.id;
+	ret = register_handle(sup, handle, WATCH_PIDNS);
+	if (ret < 0)
+		goto close_pair;
+	return client;
+
+close_pair:
+	close_fd(client);
+	close_fd(handle->pair.supervisor_fd);
+free_handle:
+	free(handle);
+	return ret;
+}
+
+int supervisor_fsconfig_pidns(const struct supervisor *sup, int fsfd,
+			      int pidns_fd)
+{
+	struct tracked_handle *handle;
+	struct cell_entry *entry;
+	int ret;
+
+	ret = lookup_handle(sup, pidns_fd, WATCH_PIDNS, &handle);
+	if (ret < 0)
+		return ret;
+	entry = cell_by_id(sup, handle->cell_id);
+	if (!entry || entry->cell.runfd < 0)
+		return -ESRCH;
+	return runfd_bind_fsconfig(entry->cell.runfd, fsfd);
+}
+
+int supervisor_pidfd_open_empty(struct supervisor *sup, int cfd, uint32_t flags,
+				uint32_t pid)
+{
+	const uint32_t allowed = PIDFD_EMPTY | O_NONBLOCK;
+	struct embryo_entry *entry;
+	struct cell_entry *owner;
+	int client, ret;
+
+	if (cell_id_of_pid(sup, pid))
+		return -EPERM;
+	if (!(flags & PIDFD_EMPTY) || (flags & ~allowed))
+		return -EINVAL;
+	ret = cell_by_fd(sup, cfd, &owner);
+	if (ret < 0)
+		return ret;
+	ret = cell_check_spawnable(&owner->cell);
+	if (ret < 0)
+		return ret;
+
+	entry = calloc(1, sizeof(*entry));
+	if (!entry)
+		return -ENOMEM;
+	client = handle_pair_create(flags & O_NONBLOCK, &entry->handle.pair);
+	if (client < 0) {
+		ret = client;
+		goto free_entry;
+	}
+	entry->embryo.cgroup_fd = -1;
+	entry->handle.cell_id = owner->cell.id;
+	ret = register_handle(sup, &entry->handle, WATCH_EMBRYO);
+	if (ret < 0)
+		goto close_pair;
+	return client;
+
+close_pair:
+	close_fd(client);
+	close_fd(entry->handle.pair.supervisor_fd);
+free_entry:
+	free(entry);
+	return ret;
+}
+
+static int validate_passable_fd(const struct supervisor *sup, int fd)
+{
+	char path[sizeof("/proc/self/fd/") + sizeof(int) * CHAR_BIT];
+	char target[PATH_MAX + 1];
+	struct file_identity id;
+	struct statfs st;
+	ssize_t len;
+	int ret;
+
+	if (fd < 0)
+		return -EBADF;
+	ret = file_identity_of_fd(fd, &id);
+	if (ret < 0)
+		return ret;
+	if (handle_by_identity(sup, &id))
+		return -EINVAL;
+	if (fstatfs(fd, &st) < 0)
+		return -errno;
+	if (st.f_type == NSFS_MAGIC)
+		return -EINVAL;
+
+	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+	len = readlink(path, target, sizeof(target) - 1);
+	if (len >= 0) {
+		target[len] = '\0';
+		if (strstr(target, "[io_uring]"))
+			return -EINVAL;
+	}
+	return 0;
+}
+
+int supervisor_pidfd_configure(struct supervisor *sup, int efd,
+			       struct embryo_action *action, int *aux_fd)
+{
+	struct tracked_handle *handle;
+	struct embryo_entry *entry;
+	int ret;
+
+	ret = lookup_handle(sup, efd, WATCH_EMBRYO, &handle);
+	if (ret < 0)
+		return ret;
+	if (action->type == EMBRYO_PLACE_FD) {
+		ret = validate_passable_fd(sup, *aux_fd);
+		if (ret < 0)
+			return ret;
+	}
+	entry = container_of(handle, struct embryo_entry, handle);
+	return embryo_configure(&entry->embryo, action, aux_fd);
+}
+
+int supervisor_pidfd_start(struct supervisor *sup, int efd)
+{
+	struct tracked_handle *handle;
+	struct embryo_entry *entry;
+	struct cell_entry *owner;
+	int ret;
+
+	ret = lookup_handle(sup, efd, WATCH_EMBRYO, &handle);
+	if (ret < 0)
+		return ret;
+	entry = container_of(handle, struct embryo_entry, handle);
+	untrack_handle(sup, handle);
+
+	owner = cell_by_id(sup, handle->cell_id);
+	if (owner) {
+		ret = cell_start_embryo(&owner->cell, &entry->embryo,
+					&sup->resources);
+		sync_post_death(sup, owner);
+	} else {
+		ret = -ESRCH;
+	}
+	destroy_embryo(entry);
+	return ret;
+}
diff --git a/src/supervisor.h b/src/supervisor.h
new file mode 100644
index 0000000..29b488b
--- /dev/null
+++ b/src/supervisor.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_SUPERVISOR_H
+#define UCELLS_SUPERVISOR_H
+
+#include "uapi.h"
+
+struct dispatch_outcome;
+struct seccomp_notif;
+struct supervisor;
+struct tracee;
+
+int supervisor_create(struct supervisor **out);
+void supervisor_destroy(struct supervisor *sup);
+int supervisor_run(struct supervisor *sup, struct tracee *tracee, int listener,
+		   int sigfd);
+
+int caller_cred_from_pid(uint32_t pid, struct caller_cred *out);
+int supervisor_cell_configure(struct supervisor *sup, int cfd,
+			      const struct slot_action *action, int *aux_fd,
+			      const struct caller_cred *caller);
+int supervisor_cell_seal(struct supervisor *sup, int cfd,
+			 const struct caller_cred *caller);
+int supervisor_cell_kill(struct supervisor *sup, int cfd);
+int supervisor_cell_idmap_fd(const struct supervisor *sup, int cfd);
+int supervisor_pidfd_configure(struct supervisor *sup, int efd,
+			       struct embryo_action *action, int *aux_fd);
+int supervisor_cell_create(struct supervisor *sup, int32_t parent,
+			   uint32_t flags, uint32_t pid);
+void supervisor_cell_info_self(const struct supervisor *sup, uint64_t mask,
+			       uint32_t pid, struct cell_info *out);
+int supervisor_cell_info(const struct supervisor *sup, int cfd, uint64_t mask,
+			 struct cell_info *out);
+int supervisor_cell_open(struct supervisor *sup, int cfd, enum open_slot slot);
+int supervisor_fsconfig_pidns(const struct supervisor *sup, int fsfd,
+			      int pidns_fd);
+int supervisor_pidfd_open_empty(struct supervisor *sup, int cfd, uint32_t flags,
+				uint32_t pid);
+int supervisor_pidfd_start(struct supervisor *sup, int efd);
+int supervisor_sysctl_notification(struct supervisor *sup, int listener,
+				   const struct seccomp_notif *notif,
+				   struct dispatch_outcome *out);
+
+#endif
diff --git a/src/sysctl.c b/src/sysctl.c
new file mode 100644
index 0000000..f4ee253
--- /dev/null
+++ b/src/sysctl.c
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include "base.h"
+#include "memory.h"
+#include "supervisor.h"
+#include "sysctl.h"
+
+#define SYSCTL_NAME "max_cell_id_blocks"
+#define SYSCTL_PATH "/proc/sys/user/" SYSCTL_NAME
+#define SYSCTL_MAX_WRITE 32
+
+static int create_memfd(void)
+{
+	int fd = memfd_create(SYSCTL_NAME, MFD_CLOEXEC | MFD_ALLOW_SEALING);
+
+	return fd < 0 ? -errno : fd;
+}
+
+static int seal_memfd(int fd)
+{
+	if (fcntl(fd, F_ADD_SEALS,
+		  F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL) < 0)
+		return -errno;
+	return 0;
+}
+
+static int reopen_fd(int fd, int flags)
+{
+	char path[sizeof("/proc/self/fd/") + 10];
+
+	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+	fd = open(path, flags | O_CLOEXEC);
+	return fd < 0 ? -errno : fd;
+}
+
+static int write_all(int fd, const char *data, size_t len)
+{
+	while (len) {
+		ssize_t n = write(fd, data, len);
+
+		if (n < 0) {
+			if (errno == EINTR)
+				continue;
+			return -errno;
+		}
+		if (!n)
+			return -EIO;
+		data += n;
+		len -= (size_t)n;
+	}
+	return 0;
+}
+
+static int require_root(uint32_t pid)
+{
+	struct caller_cred cred;
+	int ret;
+
+	ret = caller_cred_from_pid(pid, &cred);
+	if (ret < 0)
+		return ret;
+	return cred.fsuid ? -EACCES : 0;
+}
+
+static bool is_sysctl_path(int listener, uint64_t id, uint32_t pid,
+			   uint64_t addr)
+{
+	char path[sizeof(SYSCTL_PATH)];
+
+	return !read_tracee_cstring(listener, id, pid, addr, path,
+				    sizeof(path)) &&
+	       !strcmp(path, SYSCTL_PATH);
+}
+
+int sysctl_create(struct sysctl *sysctl)
+{
+	int fd, ret;
+
+	sysctl->writer_fd = -1;
+	fd = create_memfd();
+	if (fd < 0)
+		return err_ctx(fd, "memfd_create");
+
+	ret = file_identity_of_fd(fd, &sysctl->writer_id);
+	if (ret < 0) {
+		err_ctx(ret, "fstat handle");
+		goto close_writer;
+	}
+	if (fchmod(fd, 0644) < 0) {
+		ret = err_ctx(-errno, "fchmod");
+		goto close_writer;
+	}
+	ret = seal_memfd(fd);
+	if (ret < 0) {
+		err_ctx(ret, "F_ADD_SEALS");
+		goto close_writer;
+	}
+	sysctl->writer_fd = fd;
+	return 0;
+
+close_writer:
+	close_fd(fd);
+	return ret;
+}
+
+void sysctl_destroy(struct sysctl *sysctl)
+{
+	close_fd(take_fd(&sysctl->writer_fd));
+}
+
+static int open_snapshot(uint16_t quota)
+{
+	char data[sizeof("65535\n")];
+	int snapshot, ret;
+
+	snapshot = create_memfd();
+	if (snapshot < 0)
+		return snapshot;
+
+	snprintf(data, sizeof(data), "%u\n", (unsigned int)quota);
+	ret = write_all(snapshot, data, strlen(data));
+	if (ret < 0)
+		goto close_snapshot;
+	ret = seal_memfd(snapshot);
+	if (ret < 0)
+		goto close_snapshot;
+	ret = reopen_fd(snapshot, O_RDONLY);
+close_snapshot:
+	close_fd(snapshot);
+	return ret;
+}
+
+static int sysctl_open(const struct sysctl *sysctl, uint32_t pid, int flags,
+		       uint16_t quota, struct dispatch_outcome *out)
+{
+	int fd, ret;
+
+	if (flags & O_DIRECTORY)
+		return -ENOTDIR;
+	if ((flags & O_PATH) == O_PATH)
+		return -EOPNOTSUPP;
+	if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+		return -EEXIST;
+
+	switch (flags & O_ACCMODE) {
+	case O_RDONLY:
+		fd = open_snapshot(quota);
+		break;
+	case O_WRONLY:
+		ret = require_root(pid);
+		if (ret < 0)
+			return ret;
+		fd = reopen_fd(sysctl->writer_fd, O_WRONLY);
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+	if (fd < 0)
+		return fd;
+
+	*out = (struct dispatch_outcome){
+		.type = DISPATCH_SEND_FD,
+		.fd = fd,
+		.cloexec = !!(flags & O_CLOEXEC),
+	};
+	return 0;
+}
+
+static int sysctl_stat(const struct sysctl *sysctl, int listener, uint64_t id,
+		       uint32_t pid, uint64_t addr,
+		       struct dispatch_outcome *out)
+{
+	struct stat st;
+	int ret;
+
+	memset(&st, 0, sizeof(st));
+	if (fstat(sysctl->writer_fd, &st) < 0)
+		return -errno;
+	ret = write_tracee_bytes(listener, id, pid, addr, &st, sizeof(st));
+	if (ret < 0)
+		return ret;
+	*out = (struct dispatch_outcome){.type = DISPATCH_VALUE, .value = 0};
+	return 0;
+}
+
+static bool quota_whitespace(char c)
+{
+	return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
+}
+
+static int parse_quota(const char *start, size_t len, uint16_t *quota)
+{
+	const char *end = start + len;
+	unsigned int value = 0;
+
+	while (start < end && quota_whitespace(*start))
+		start++;
+	while (start < end && quota_whitespace(end[-1]))
+		end--;
+	if (start < end && *start == '+')
+		start++;
+	if (start == end)
+		return -EINVAL;
+
+	for (; start < end; start++) {
+		if (*start < '0' || *start > '9')
+			return -EINVAL;
+		value = value * 10 + (*start - '0');
+		if (value > ID_TOTAL_BLOCKS)
+			return -EINVAL;
+	}
+
+	*quota = (uint16_t)value;
+	return 0;
+}
+
+static int sysctl_write(const struct sysctl *sysctl, int listener,
+			const struct seccomp_notif *notif, uint16_t *quota,
+			struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	uint32_t raw_fd = (uint32_t)args[0];
+	struct file_identity file_id;
+	char bytes[SYSCTL_MAX_WRITE];
+	uint64_t count = args[2];
+	int fd, flags, ret;
+
+	if (raw_fd > INT32_MAX)
+		return 0;
+	fd = dup_tracee_fd(listener, notif->id, notif->pid, (int)raw_fd);
+	if (fd < 0)
+		return 0;
+
+	ret = file_identity_of_fd(fd, &file_id);
+	if (ret < 0 || !file_identity_equal(&file_id, &sysctl->writer_id))
+		goto close_file;
+
+	flags = fcntl(fd, F_GETFL);
+	if (flags < 0) {
+		ret = -errno;
+		goto close_file;
+	}
+	if ((flags & O_ACCMODE) != O_WRONLY) {
+		ret = -EBADF;
+		goto close_file;
+	}
+
+	ret = require_root(notif->pid);
+	if (ret < 0)
+		goto close_file;
+	if (count > sizeof(bytes)) {
+		ret = -EINVAL;
+		goto close_file;
+	}
+	if (!count)
+		goto close_file;
+
+	ret = read_tracee_bytes(listener, notif->id, notif->pid, args[1], bytes,
+				(size_t)count);
+	if (ret < 0)
+		goto close_file;
+
+	ret = parse_quota(bytes, (size_t)count, quota);
+	if (!ret)
+		*out = (struct dispatch_outcome){
+			.type = DISPATCH_VALUE,
+			.value = (int64_t)count,
+		};
+close_file:
+	close_fd(fd);
+	return ret;
+}
+
+int sysctl_dispatch(const struct sysctl *sysctl, int listener,
+		    const struct seccomp_notif *notif, uint16_t *quota,
+		    struct dispatch_outcome *out)
+{
+	const __u64 *args = notif->data.args;
+	uint64_t id = notif->id;
+	uint32_t pid = notif->pid;
+
+	*out = (struct dispatch_outcome){.type = DISPATCH_CONTINUE};
+	switch (notif->data.nr) {
+	case __NR_openat:
+		if (is_sysctl_path(listener, id, pid, args[1]))
+			return sysctl_open(sysctl, pid, (int)args[2], *quota,
+					   out);
+		break;
+#if defined(__x86_64__)
+	case __NR_open:
+		if (is_sysctl_path(listener, id, pid, args[0]))
+			return sysctl_open(sysctl, pid, (int)args[1], *quota,
+					   out);
+		break;
+#endif
+	case __NR_newfstatat:
+		if (is_sysctl_path(listener, id, pid, args[1]))
+			return sysctl_stat(sysctl, listener, id, pid, args[2],
+					   out);
+		break;
+#if defined(__x86_64__)
+	case __NR_stat:
+	case __NR_lstat:
+		if (is_sysctl_path(listener, id, pid, args[0]))
+			return sysctl_stat(sysctl, listener, id, pid, args[1],
+					   out);
+		break;
+#endif
+	/* procps expects EINVAL for readlink on a regular file. */
+	case __NR_readlinkat:
+		if (is_sysctl_path(listener, id, pid, args[1]))
+			return -EINVAL;
+		break;
+#if defined(__x86_64__)
+	case __NR_readlink:
+		if (is_sysctl_path(listener, id, pid, args[0]))
+			return -EINVAL;
+		break;
+#endif
+	case __NR_write:
+		return sysctl_write(sysctl, listener, notif, quota, out);
+	}
+	return 0;
+}
diff --git a/src/sysctl.h b/src/sysctl.h
new file mode 100644
index 0000000..37cff98
--- /dev/null
+++ b/src/sysctl.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_SYSCTL_H
+#define UCELLS_SYSCTL_H
+
+#include "notify.h"
+#include "uapi.h"
+
+struct sysctl {
+	int writer_fd;
+	struct file_identity writer_id;
+};
+
+int sysctl_create(struct sysctl *sysctl);
+void sysctl_destroy(struct sysctl *sysctl);
+int sysctl_dispatch(const struct sysctl *sysctl, int listener,
+		    const struct seccomp_notif *notif, uint16_t *quota,
+		    struct dispatch_outcome *out);
+
+#endif
diff --git a/src/tracee.c b/src/tracee.c
new file mode 100644
index 0000000..e51bde7
--- /dev/null
+++ b/src/tracee.c
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <signal.h>
+#include <sys/wait.h>
+
+#include "base.h"
+#include "tracee.h"
+
+void tracee_kill(struct tracee *tracee)
+{
+	if (tracee->pid > 0)
+		kill(tracee->pid, SIGKILL);
+}
+
+void tracee_destroy(struct tracee *tracee)
+{
+	if (tracee->pid <= 0)
+		return;
+
+	tracee_kill(tracee);
+	while (waitpid(tracee->pid, NULL, 0) < 0 && errno == EINTR)
+		;
+	tracee->pid = 0;
+}
+
+int tracee_poll_exit(struct tracee *tracee, int *exit_code)
+{
+	int status;
+	pid_t waited;
+
+	if (tracee->pid <= 0)
+		return 0;
+
+	do {
+		waited = waitpid(tracee->pid, &status, WNOHANG);
+	} while (waited < 0 && errno == EINTR);
+
+	if (waited < 0) {
+		int err = errno;
+
+		if (err == ECHILD)
+			tracee->pid = 0;
+		return err_ctx(-err, "waitpid tracee");
+	}
+	if (!waited)
+		return 0;
+	if (WIFEXITED(status))
+		*exit_code = WEXITSTATUS(status);
+	else if (WIFSIGNALED(status))
+		*exit_code = 128 + WTERMSIG(status);
+	else
+		return 0;
+
+	tracee->pid = 0;
+	return 1;
+}
diff --git a/src/tracee.h b/src/tracee.h
new file mode 100644
index 0000000..893e301
--- /dev/null
+++ b/src/tracee.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_TRACEE_H
+#define UCELLS_TRACEE_H
+
+#include <sys/types.h>
+
+struct tracee {
+	pid_t pid;
+};
+
+void tracee_destroy(struct tracee *tracee);
+int tracee_poll_exit(struct tracee *tracee, int *exit_code);
+void tracee_kill(struct tracee *tracee);
+
+#endif
diff --git a/src/tree.c b/src/tree.c
new file mode 100644
index 0000000..bf59cf4
--- /dev/null
+++ b/src/tree.c
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <fcntl.h>
+#include <linux/magic.h>
+#include <linux/mount.h>
+#include <linux/nsfs.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/statfs.h>
+#include <sys/wait.h>
+
+#include "base.h"
+#include "tree.h"
+#include "uapi.h"
+
+static bool is_locked_submount(char *line)
+{
+	char *point, *opts, *save;
+	const char *sep = " \t\n";
+	int i;
+
+	if (!strtok_r(line, sep, &save))
+		return false;
+	for (i = 0; i < 3; i++) {
+		if (!strtok_r(NULL, sep, &save))
+			return false;
+	}
+	point = strtok_r(NULL, sep, &save);
+	if (!point || !strcmp(point, "/"))
+		return false;
+	opts = strtok_r(NULL, sep, &save);
+	if (!opts)
+		return false;
+	for (opts = strtok_r(opts, ",", &save); opts;
+	     opts = strtok_r(NULL, ",", &save)) {
+		if (!strcmp(opts, "locked"))
+			return true;
+	}
+	return false;
+}
+
+static int validate_submounts(int proc_dir)
+{
+	char path[32], *line = NULL;
+	size_t cap = 0;
+	int fd, ret = 0;
+	FILE *file;
+
+	snprintf(path, sizeof(path), "%d/mountinfo", gettid());
+	fd = openat(proc_dir, path, O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return 0;
+	file = fdopen(fd, "r");
+	if (!file) {
+		ret = -errno;
+		close_fd(fd);
+		return ret;
+	}
+	for (;;) {
+		errno = 0;
+		if (getline(&line, &cap, file) < 0) {
+			if (!feof(file))
+				ret = errno ? -errno : -EIO;
+			break;
+		}
+		if (is_locked_submount(line)) {
+			ret = -EPERM;
+			break;
+		}
+	}
+	free(line);
+	fclose(file);
+	return ret;
+}
+
+static int enter_source_mntns(int fd)
+{
+	int proc_dir, ret;
+
+	/* The source namespace may not mount procfs. */
+	proc_dir = open("/proc", O_RDONLY | O_CLOEXEC);
+	if (proc_dir < 0)
+		return -errno;
+	if (setns(fd, CLONE_NEWNS) < 0)
+		ret = -errno;
+	else
+		ret = validate_submounts(proc_dir);
+	close_fd(proc_dir);
+	if (ret)
+		return ret;
+	mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
+	return 0;
+}
+
+static int child_seal(const struct mount_tree *tree, int mntns_fd)
+{
+	int ret;
+
+	ret = enter_source_mntns(tree->fd);
+	if (ret)
+		return ret;
+	if (syscall(__NR_move_mount, AT_FDCWD, "/", mntns_fd, "/",
+		    MOVE_MOUNT_T_MNTNS | MOVE_MOUNT_CLONE) < 0)
+		return -errno;
+	return 0;
+}
+
+static int seal_from_mntns(const struct mount_tree *tree, int mntns_fd)
+{
+	int pipe_fds[2], ret;
+	int32_t status;
+	pid_t child;
+	ssize_t n;
+
+	if (pipe2(pipe_fds, O_CLOEXEC) < 0)
+		return err_ctx(-errno, "pipe2 tree helper");
+	child = fork();
+	if (child < 0) {
+		ret = err_ctx(-errno, "fork tree helper");
+		close_fd(pipe_fds[0]);
+		close_fd(pipe_fds[1]);
+		return ret;
+	}
+	if (!child) {
+		close_fd(pipe_fds[0]);
+		status = child_seal(tree, mntns_fd);
+		while (write(pipe_fds[1], &status, sizeof(status)) < 0 &&
+		       errno == EINTR)
+			;
+		_exit(0);
+	}
+
+	close_fd(pipe_fds[1]);
+	do {
+		n = read(pipe_fds[0], &status, sizeof(status));
+	} while (n < 0 && errno == EINTR);
+	ret = n < 0 ? -errno : n == sizeof(status) ? status : -EIO;
+	close_fd(pipe_fds[0]);
+	while (waitpid(child, NULL, 0) < 0 && errno == EINTR)
+		;
+	return ret;
+}
+
+int mount_tree_from_slot_fd(int fd, struct mount_tree *out)
+{
+	struct statfs st;
+	bool is_mntns;
+	int type;
+
+	if (fstatfs(fd, &st) < 0)
+		return err_ctx(-errno, "fstatfs root slot fd");
+	is_mntns = st.f_type == NSFS_MAGIC;
+	if (is_mntns) {
+		type = ioctl(fd, NS_GET_NSTYPE);
+		if (type < 0)
+			return err_ctx(-errno, "NS_GET_NSTYPE root slot fd");
+		if (type != CLONE_NEWNS)
+			return err_ctx(-EINVAL,
+				       "root slot fd is not a mount namespace");
+	}
+	*out = (struct mount_tree){.fd = fd, .is_mntns = is_mntns};
+	return 0;
+}
+
+void mount_tree_destroy(struct mount_tree *tree)
+{
+	close_fd(take_fd(&tree->fd));
+}
+
+int mount_tree_seal_into(const struct mount_tree *tree, int mntns_fd)
+{
+	if (tree->is_mntns) {
+		int ret = seal_from_mntns(tree, mntns_fd);
+
+		return ret ? err_ctx(ret, "seal mount namespace into runfd")
+			   : 0;
+	}
+	if (syscall(__NR_move_mount, tree->fd, "", mntns_fd, "/",
+		    MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_MNTNS) < 0)
+		return err_ctx(-errno, "move_mount root tree");
+	return 0;
+}
diff --git a/src/tree.h b/src/tree.h
new file mode 100644
index 0000000..55b49dd
--- /dev/null
+++ b/src/tree.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_TREE_H
+#define UCELLS_TREE_H
+
+struct mount_tree {
+	int fd;
+	bool is_mntns;
+};
+
+/* Takes ownership of fd on success. */
+int mount_tree_from_slot_fd(int fd, struct mount_tree *out);
+void mount_tree_destroy(struct mount_tree *tree);
+int mount_tree_seal_into(const struct mount_tree *tree, int mntns_fd);
+
+#endif
diff --git a/src/uapi.c b/src/uapi.c
new file mode 100644
index 0000000..1724397
--- /dev/null
+++ b/src/uapi.c
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include "base.h"
+#include "uapi.h"
+
+thread_local const char *ucells_error;
+
+static_assert(__NR_pidfd_send_signal == 424);
+static_assert(__NR_open_tree == 428);
+static_assert(__NR_move_mount == 429);
+static_assert(__NR_fsopen == 430);
+static_assert(__NR_fsconfig == 431);
+static_assert(__NR_fsmount == 432);
+static_assert(__NR_pidfd_open == 434);
+static_assert(__NR_clone3 == 435);
+static_assert(__NR_pidfd_getfd == 438);
+static_assert(__NR_mount_setattr == 442);
+
+int file_identity_of_fd(int fd, struct file_identity *id)
+{
+	struct stat st;
+
+	if (fstat(fd, &st) < 0)
+		return -errno;
+	*id = (struct file_identity){.dev = st.st_dev, .ino = st.st_ino};
+	return 0;
+}
+
+bool file_identity_equal(const struct file_identity *a,
+			 const struct file_identity *b)
+{
+	return a->dev == b->dev && a->ino == b->ino;
+}
+
+static bool id_in_window(uint32_t id)
+{
+	return id >= ID_WINDOW_FIRST && id <= ID_WINDOW_LAST;
+}
+
+int id_shape_validate_caller(enum id_shape shape, uint32_t uid, uint32_t gid)
+{
+	if (shape != ID_SINGLE)
+		return 0;
+	if (!uid || !gid)
+		return -EPERM;
+	if (id_in_window(uid) || id_in_window(gid))
+		return -EINVAL;
+	return 0;
+}
+
+bool valid_utf8(const char *data, size_t len)
+{
+	const unsigned char *bytes = (const unsigned char *)data;
+	size_t i = 0;
+
+	while (i < len) {
+		unsigned char b = bytes[i];
+		unsigned char lo = 0x80;
+		unsigned char hi = 0xbf;
+		size_t width;
+
+		if (b < 0x80) {
+			i++;
+			continue;
+		}
+		if (b >= 0xc2 && b <= 0xdf) {
+			width = 2;
+		} else if (b >= 0xe0 && b <= 0xef) {
+			width = 3;
+			if (b == 0xe0)
+				lo = 0xa0;
+			else if (b == 0xed)
+				hi = 0x9f;
+		} else if (b >= 0xf0 && b <= 0xf4) {
+			width = 4;
+			if (b == 0xf0)
+				lo = 0x90;
+			else if (b == 0xf4)
+				hi = 0x8f;
+		} else {
+			return false;
+		}
+		if (len - i < width || bytes[i + 1] < lo || bytes[i + 1] > hi)
+			return false;
+		for (size_t j = 2; j < width; j++) {
+			if ((bytes[i + j] & 0xc0) != 0x80)
+				return false;
+		}
+		i += width;
+	}
+	return true;
+}
+
+int string_block_parse(char *data, size_t len, struct string_block *out)
+{
+	char *end, *p, **items;
+	size_t count = 0;
+
+	if (!len || len > EMBRYO_MAX_BLOCK)
+		return -E2BIG;
+	if (data[len - 1])
+		return -EINVAL;
+
+	end = data + len;
+	for (p = data; p < end;) {
+		size_t part = strlen(p);
+
+		if (++count > EMBRYO_MAX_STRINGS)
+			return -E2BIG;
+		if (!valid_utf8(p, part))
+			return -EINVAL;
+		p += part + 1;
+	}
+
+	items = malloc((count + 1) * sizeof(*items));
+	if (!items)
+		return -ENOMEM;
+	p = data;
+	for (size_t i = 0; i < count; i++) {
+		items[i] = p;
+		p += strlen(p) + 1;
+	}
+	items[count] = NULL;
+	*out = (struct string_block){.data = data, .items = items};
+	return 0;
+}
+
+void string_block_destroy(struct string_block *block)
+{
+	free(block->items);
+	free(block->data);
+	*block = (struct string_block){};
+}
+
+static int compare_groups(const void *p, const void *q)
+{
+	uint32_t a = *(const uint32_t *)p;
+	uint32_t b = *(const uint32_t *)q;
+
+	return (a > b) - (a < b);
+}
+
+void cred_spec_sort_groups(struct cred_spec *cred)
+{
+	size_t kept = 1;
+
+	if (cred->nr_groups < 2)
+		return;
+	qsort(cred->groups, cred->nr_groups, sizeof(*cred->groups),
+	      compare_groups);
+	for (size_t i = 1; i < cred->nr_groups; i++) {
+		if (cred->groups[i] != cred->groups[kept - 1])
+			cred->groups[kept++] = cred->groups[i];
+	}
+	cred->nr_groups = kept;
+}
+
+static bool valid_key(const char *key, size_t max)
+{
+	return *key && strnlen(key, max) < max;
+}
+
+int embryo_action_string(const char *key, char *value,
+			 struct embryo_action *out)
+{
+	if (strcmp(key, "exec") != 0 || !*value)
+		return -EINVAL;
+	if (strnlen(value, PATH_MAX) == PATH_MAX)
+		return -ENAMETOOLONG;
+	*out = (struct embryo_action){.type = EMBRYO_EXEC, .exec = value};
+	return 0;
+}
+
+static int parse_target_fd(const char *key)
+{
+	int fd = 0;
+
+	if (!*key || (*key == '0' && key[1]))
+		return -EINVAL;
+	for (; *key; key++) {
+		if (*key < '0' || *key > '9')
+			return -EINVAL;
+		fd = fd * 10 + (*key - '0');
+		if (fd > EMBRYO_MAX_FD_NUM)
+			return -EINVAL;
+	}
+	return fd;
+}
+
+int embryo_action_fd(const char *key, struct embryo_action *out)
+{
+	int fd;
+
+	if (!valid_key(key, EMBRYO_KEY_MAX))
+		return -EINVAL;
+	if (!strcmp(key, "cgroup")) {
+		*out = (struct embryo_action){.type = EMBRYO_CGROUP_FD};
+		return 0;
+	}
+	fd = parse_target_fd(key);
+	if (fd < 0)
+		return fd;
+	*out = (struct embryo_action){.type = EMBRYO_PLACE_FD, .target_fd = fd};
+	return 0;
+}
+
+void embryo_action_destroy(struct embryo_action *action)
+{
+	switch (action->type) {
+	case EMBRYO_EXEC:
+		free(action->exec);
+		break;
+	case EMBRYO_ARGV:
+	case EMBRYO_ENVP:
+		string_block_destroy(&action->strings);
+		break;
+	case EMBRYO_CRED:
+		free(action->cred.groups);
+		break;
+	case EMBRYO_PLACE_FD:
+	case EMBRYO_CGROUP_FD:
+		break;
+	}
+	*action = (struct embryo_action){};
+}
+
+static int validate_slot_key(const char *key)
+{
+	if (!valid_key(key, CELL_KEY_MAX))
+		return -EINVAL;
+	if (!strcmp(key, "spawn"))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
+int slot_action_string(const char *key, const char *val,
+		       struct slot_action *out)
+{
+	int ret = validate_slot_key(key);
+
+	if (ret)
+		return ret;
+	if (!strcmp(key, "hostname")) {
+		size_t len = strnlen(val, sizeof(out->hostname));
+
+		if (len > UTS_HOSTNAME_MAX)
+			return -ENAMETOOLONG;
+		out->type = SLOT_HOSTNAME;
+		memcpy(out->hostname, val, len + 1);
+		return 0;
+	}
+	if (!strcmp(key, "net") && !strcmp(val, "new")) {
+		*out = (struct slot_action){.type = SLOT_NET_NEW};
+		return 0;
+	}
+	return -EINVAL;
+}
+
+static int parse_id_slot(const void *value, size_t len, struct slot_action *out)
+{
+	uint32_t count;
+
+	if (len != sizeof(count))
+		return -EINVAL;
+	memcpy(&count, value, sizeof(count));
+	if (count != 1 && count != ID_BLOCK_SIZE)
+		return -EINVAL;
+	*out = (struct slot_action){
+		.type = SLOT_IDS,
+		.shape = count == 1 ? ID_SINGLE : ID_BLOCK,
+	};
+	return 0;
+}
+
+int slot_action_binary(const char *key, const void *value, size_t len,
+		       struct slot_action *out)
+{
+	uint64_t bits;
+	int ret = validate_slot_key(key);
+
+	if (ret)
+		return ret;
+	if (!strcmp(key, "ids"))
+		return parse_id_slot(value, len, out);
+	if (len != sizeof(bits))
+		return -EINVAL;
+	memcpy(&bits, value, sizeof(bits));
+	if (!strcmp(key, "caps")) {
+		if (bits >> (CELL_CAP_LAST + 1) || bits & (1ULL << CAP_SETFCAP))
+			return -EINVAL;
+		*out = (struct slot_action){.type = SLOT_CAPS, .caps = bits};
+	} else if (!strcmp(key, "net-rights")) {
+		if (bits & ~CELL_NET_ALL)
+			return -EINVAL;
+		*out = (struct slot_action){
+			.type = SLOT_NET_RIGHTS,
+			.rights = bits,
+		};
+	} else {
+		return -EINVAL;
+	}
+	return 0;
+}
+
+int slot_action_fd(const char *key, struct slot_action *out)
+{
+	int ret = validate_slot_key(key);
+
+	if (ret)
+		return ret;
+	if (!strcmp(key, "root"))
+		out->type = SLOT_ROOT_FD;
+	else if (!strcmp(key, "budget"))
+		out->type = SLOT_BUDGET_FD;
+	else if (!strcmp(key, "net"))
+		out->type = SLOT_NET_FD;
+	else
+		return -EINVAL;
+	return 0;
+}
+
+int slot_action_flag(const char *key)
+{
+	int ret = validate_slot_key(key);
+
+	return ret ? ret : -EINVAL;
+}
+
+int parse_open_slot(const char *key, enum open_slot *out)
+{
+	if (!strcmp(key, "net"))
+		*out = OPEN_SLOT_NET;
+	else if (!strcmp(key, "pids"))
+		*out = OPEN_SLOT_PIDS;
+	else
+		return -EINVAL;
+	return 0;
+}
diff --git a/src/uapi.h b/src/uapi.h
new file mode 100644
index 0000000..7a5fcbf
--- /dev/null
+++ b/src/uapi.h
@@ -0,0 +1,223 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+// Copyright (c) 2026 Jarkko Sakkinen
+
+#ifndef UCELLS_UAPI_H
+#define UCELLS_UAPI_H
+
+#include <asm/unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/capability.h>
+#include <linux/mount.h>
+#include <linux/nsfs.h>
+#include <linux/sched.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#define SYS_CELL_CREATE 473
+#define SYS_CELL_CONFIGURE 474
+#define SYS_CELL_INFO 475
+#define SYS_CELL_OPEN 476
+#define SYS_CELL_KILL 477
+#define SYS_PIDFD_CONFIGURE 478
+
+#define CELL_SELF (-10005)
+#define CELL_INFO_SIZE_VER0 80
+#define PIDFD_EMPTY O_NOCTTY
+
+#define ID_WINDOW_FIRST (1U << 31)
+#define ID_BLOCK_SIZE 65536U
+#define ID_TOTAL_BLOCKS 32767U
+#define ID_WINDOW_LAST (ID_WINDOW_FIRST + ID_TOTAL_BLOCKS * ID_BLOCK_SIZE - 1)
+
+#define EMBRYO_KEY_MAX 32
+#define EMBRYO_MAX_STRINGS 4096
+#define EMBRYO_MAX_BLOCK (256UL * 1024)
+#define EMBRYO_MAX_FDS 256
+#define EMBRYO_MAX_FD_NUM 4095
+#define CELL_NGROUPS_MAX 65536
+#define CELL_KEY_MAX 64
+#define UTS_HOSTNAME_MAX 64
+#define CELL_CAP_LAST 40
+
+#define CELL_INFO_ID (1ULL << 0)
+#define CELL_INFO_STATE (1ULL << 1)
+#define CELL_INFO_EXIT (1ULL << 2)
+#define CELL_INFO_BUDGET (1ULL << 3)
+#define CELL_INFO_IDS (1ULL << 4)
+#define CELL_INFO_CAPS (1ULL << 5)
+#define CELL_INFO_IDS_FIRST (1ULL << 6)
+#define CELL_INFO_NET_RIGHTS (1ULL << 7)
+#define CELL_INFO_ALL 0xffULL
+
+#define CELL_NET_LINK (1ULL << 0)
+#define CELL_NET_LINK_NEW (1ULL << 1)
+#define CELL_NET_ADDR (1ULL << 2)
+#define CELL_NET_ROUTE (1ULL << 3)
+#define CELL_NET_FILTER (1ULL << 4)
+#define CELL_NET_TC (1ULL << 5)
+#define CELL_NET_TUN (1ULL << 6)
+#define CELL_NET_SYSCTL (1ULL << 7)
+#define CELL_NET_SOCKOPT (1ULL << 8)
+#define CELL_NET_RAW (1ULL << 9)
+#define CELL_NET_ALL 0x3ffULL
+
+struct cell_info {
+	uint64_t mask;
+	uint64_t supported_mask;
+	uint64_t id;
+	uint64_t parent_id;
+	uint32_t state;
+	uint32_t flags;
+	int32_t exit_status;
+	uint32_t ids_count;
+	uint64_t budget_cgroupid;
+	uint64_t caps;
+	uint32_t ids_first;
+	uint32_t spare;
+	uint64_t net_rights;
+};
+
+struct pidfd_cred {
+	uint32_t uid;
+	uint32_t gid;
+	uint32_t ngroups;
+	uint32_t spare;
+	uint64_t groups;
+};
+
+static_assert(sizeof(struct cell_info) == CELL_INFO_SIZE_VER0);
+static_assert(offsetof(struct cell_info, state) == 32);
+static_assert(offsetof(struct cell_info, budget_cgroupid) == 48);
+static_assert(offsetof(struct cell_info, ids_first) == 64);
+static_assert(offsetof(struct cell_info, net_rights) == 72);
+static_assert(sizeof(struct pidfd_cred) == 24);
+static_assert(offsetof(struct pidfd_cred, groups) == 16);
+static_assert(ID_WINDOW_LAST == 4294901759U);
+
+enum cell_state {
+	CELL_BUILDING = 0,
+	CELL_SEALED = 1,
+	CELL_RUNNING = 2,
+	CELL_DYING = 3,
+	CELL_DEAD = 4,
+};
+
+enum cell_configure_cmd {
+	CELL_SET_FLAG = 0,
+	CELL_SET_STRING = 1,
+	CELL_SET_BINARY = 2,
+	CELL_SET_FD = 3,
+	CELL_CMD_SEAL = 4,
+};
+
+enum pidfd_configure_cmd {
+	PIDFD_SET_STRING = 0,
+	PIDFD_SET_BINARY = 1,
+	PIDFD_SET_FD = 2,
+	PIDFD_CMD_START = 3,
+};
+
+#define CELL_OPEN_SLOT 0
+
+enum id_shape {
+	ID_UNSET,
+	ID_SINGLE,
+	ID_BLOCK,
+};
+
+enum open_slot {
+	OPEN_SLOT_NET,
+	OPEN_SLOT_PIDS,
+};
+
+enum net_slot_mode {
+	NET_SLOT_NONE,
+	NET_SLOT_NEW,
+	NET_SLOT_PROVIDED_FD,
+};
+
+struct file_identity {
+	uint64_t dev;
+	uint64_t ino;
+};
+
+struct caller_cred {
+	uint32_t uid;
+	uint32_t gid;
+	uint32_t fsuid;
+};
+
+struct string_block {
+	char *data;
+	char **items;
+};
+
+struct cred_spec {
+	uint32_t uid;
+	uint32_t gid;
+	uint32_t *groups;
+	size_t nr_groups;
+};
+
+enum embryo_action_type {
+	EMBRYO_EXEC,
+	EMBRYO_ARGV,
+	EMBRYO_ENVP,
+	EMBRYO_CRED,
+	EMBRYO_PLACE_FD,
+	EMBRYO_CGROUP_FD,
+};
+
+struct embryo_action {
+	enum embryo_action_type type;
+	union {
+		char *exec;
+		struct string_block strings;
+		struct cred_spec cred;
+		int target_fd;
+	};
+};
+
+enum slot_action_type {
+	SLOT_HOSTNAME,
+	SLOT_NET_NEW,
+	SLOT_IDS,
+	SLOT_CAPS,
+	SLOT_NET_RIGHTS,
+	SLOT_ROOT_FD,
+	SLOT_BUDGET_FD,
+	SLOT_NET_FD,
+};
+
+struct slot_action {
+	enum slot_action_type type;
+	union {
+		char hostname[UTS_HOSTNAME_MAX + 1];
+		enum id_shape shape;
+		uint64_t caps;
+		uint64_t rights;
+	};
+};
+
+int file_identity_of_fd(int fd, struct file_identity *id);
+bool file_identity_equal(const struct file_identity *a,
+			 const struct file_identity *b);
+int id_shape_validate_caller(enum id_shape shape, uint32_t uid, uint32_t gid);
+bool valid_utf8(const char *bytes, size_t len);
+int string_block_parse(char *data, size_t len, struct string_block *out);
+void string_block_destroy(struct string_block *block);
+void cred_spec_sort_groups(struct cred_spec *cred);
+int embryo_action_string(const char *key, char *value,
+			 struct embryo_action *out);
+int embryo_action_fd(const char *key, struct embryo_action *out);
+void embryo_action_destroy(struct embryo_action *action);
+int slot_action_string(const char *key, const char *val,
+		       struct slot_action *out);
+int slot_action_binary(const char *key, const void *value, size_t len,
+		       struct slot_action *out);
+int slot_action_fd(const char *key, struct slot_action *out);
+int slot_action_flag(const char *key);
+int parse_open_slot(const char *key, enum open_slot *out);
+
+#endif