A hands-on walkthrough builds a working C program that uses Linux's Landlock LSM to sandbox a process's filesystem access without root, containers, or daemons. It covers the three Landlock syscalls, explains why PR_SET_NO_NEW_PRIVS is mandatory, demonstrates that rulesets intersect rather than accumulate, shows how restrictions are inherited across fork and exec, and builds a small wrapper (llrun) that confines an arbitrary unmodified program to a chosen directory. It also covers using ldd and strace to discover the full set of paths a program needs, and checking the Landlock ABI version to know which features (network rules, ioctl control, reparenting) are available on a given kernel.
Table of contents
Table of ContentsWhat You NeedWhere Landlock SitsThree System Calls and No LibraryA Program That Restricts ItselfWhy no_new_privs is MandatoryRulesets Intersect, They Never WidenWhat Your Children InheritThe exec TrapWrapping a Program You Didn't WriteFinding the Paths a Program NeedsWhich ABI Version You HaveConclusionEpilogueQuestions this post answers
Why do I need to call prctl with PR_SET_NO_NEW_PRIVS before landlock_restrict_self?
Without setting PR_SET_NO_NEW_PRIVS first, landlock_restrict_self fails with EPERM. This prctl call tells the kernel that the process and its descendants can never gain privileges through execve, which prevents a sandboxed process from escaping by running a setuid binary like sudo; Landlock refuses to apply itself at all without that guarantee. Anyone sandboxing processes with Landlock can review setup patterns like this one on daily.dev before shipping.
Do multiple Landlock rulesets combine to grant more filesystem access, or do they restrict further?
Multiple Landlock rulesets intersect rather than accumulate. Applying a ruleset allowing /etc, then a second ruleset allowing /tmp, leaves the process with access to neither, because each restrict_self call intersects with everything already applied, and what survives is the overlap between the two allow lists, which is empty. All needed paths must go into one ruleset before applying it. Developers building layered sandboxes with Landlock can check gotchas like ruleset intersection on daily.dev.
Why does exec fail with permission denied inside a Landlock sandbox even though the target binary path itself wasn't restricted?
Executing a binary requires reading it, so if a ruleset handles LANDLOCK_ACCESS_FS_READ_FILE but has no rule covering the binary's directory (like /usr/bin/cat under /usr), the kernel refuses the read before the program can start, producing a permission denied error. The fix is adding the binary's directory, its shared libraries, and its interpreter to the same ruleset before restricting. Debugging exec failures under Landlock sandboxes is easier with practical breakdowns like this on daily.dev.
1 Comment
Share this post