Skip to content

Introduce Landlock isolation support #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025

Conversation

n0toose
Copy link
Member

@n0toose n0toose commented Nov 30, 2024

Leaving this here for now, the most important design problem with this is how Landlock does not allow whitelisting files if they are not created. (So, we'd have to force the user to use a directory for that instead.)

We also need to avoid "parsing" the same --file-mapping inputs twice, as well as not use OnceLock for enforcing the whitelist when the kernel is actually being loaded. UhyveVm::new is called in a new thread because Landlock enforces the restrictions for the entire process and its children. We're not testing if the sandbox is applied correctly yet.

See: https://docs.kernel.org/userspace-api/landlock.html

Fixes #766

@n0toose n0toose changed the title introduce Landlock isolation support Introduce Landlock isolation support Nov 30, 2024
@n0toose n0toose marked this pull request as draft November 30, 2024 22:24
@n0toose
Copy link
Member Author

n0toose commented Dec 1, 2024

Depends on #814.

@n0toose

This comment was marked as outdated.

@n0toose

This comment was marked as outdated.

@n0toose

This comment was marked as outdated.

@n0toose
Copy link
Member Author

n0toose commented Dec 18, 2024

The change incorporates some changes from #844.

@n0toose

This comment was marked as outdated.

@n0toose

This comment was marked as outdated.

@n0toose n0toose marked this pull request as ready for review January 3, 2025 13:05
@n0toose

This comment was marked as outdated.

@mkroening mkroening requested a review from jounathaen January 3, 2025 13:11
n0toose added a commit to n0toose/uhyve that referenced this pull request Jan 5, 2025
Ported from hermit-os#816, fixes a regression introduced by
hermit-os/kernel#1529,
which modified the Hermit kernel so that it uses absolute paths instead
of relative ones.
@n0toose
Copy link
Member Author

n0toose commented Jan 5, 2025

This PR includes work that was split into separate PRs, #844 and #852, which should probably be merged first. This PR relies on hermit-os/kernel#1529. It includes some changes to our tests that reflect the changes made in hermit-os/kernel#1529.

github-merge-queue bot pushed a commit that referenced this pull request Jan 9, 2025
Ported from #816, fixes a regression introduced by
hermit-os/kernel#1529,
which modified the Hermit kernel so that it uses absolute paths instead
of relative ones.
@n0toose n0toose force-pushed the sandbox-landlock branch 3 times, most recently from 20f56b7 to c045ed7 Compare January 10, 2025 19:28
@n0toose

This comment was marked as outdated.

@jounathaen

This comment was marked as outdated.

@n0toose

This comment was marked as outdated.

@n0toose n0toose force-pushed the sandbox-landlock branch 3 times, most recently from cad6641 to 4c411d0 Compare April 24, 2025 22:53
@jounathaen jounathaen enabled auto-merge April 28, 2025 13:14
@jounathaen jounathaen added this pull request to the merge queue Apr 28, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 28, 2025
@jounathaen jounathaen added this pull request to the merge queue Apr 28, 2025
Landlock is a stackable Linux Security Module that lets Uhyve restrict
its own capabilities (specifically: access to the filesystem) before
loading untrusted code (here: machine images, which we consider to be
"black boxes").

The Landlock module is present in newer releases of modern Linux
distributions. Uhyve's interaction with Landlock is managed by the class
`UhyveLandlockWrapper`. After Uhyve "collects" all paths it requires to
function, it enforces Landlock _before_ the image is loaded in
`UhyveVm::load_kernel`.

We determine whether a file or a directory is present at a path to:
- whitelist a non-existent file's parent directory,
  see get_parent_directory. UhyveFileMap continues to protect
  the parent directory.
- set the right permissions for paths instead of letting the
  crate "dynamically" remove directory access rights for files,
  because that causes breakage when using strict file isolation
  mode or CompatLevel::HardRequirement (see determine_ruleset).

Additional integration tests were added to check for Landlock-specific
cases, as well as unit tests of the Landlock component (which also
increase coverage of UhyveFileMap).

Uhyve's Landlock functionality is disabled on non-Linux hosts, as Landlock
is never present on non-Linux hosts. Otherwise, it is enabled by default
("CompatLevel::BestEffort", plus the requirement that a version of Landlock
(all ABIs) has to be present on the system. This goes up against Landlock's
author's opinion of taking advantage of existing sandboxing features
"opportunistically", depending on what is actually available on the operating
system. What we chose instead is to prevent execution unless if the user
explicitly opts-out using `--file-isolation none`).

For now, we rely on the serial_test crate to not break filesystem tests.
Such tests should be run in separate, newly created processes, but that was
left for later. For now, `--file-isolation strict`'s functionality is ensured
by checking against `cargo run` for now in the CI.

We check whether Landlock was already enabled once before, which
should be useful when using Uhyve as a library. If that is the case,
Uhyve returns an error, as it (probably) cannot function in conjuction
with an existing set of enforced Landlock rulesets. As soon as a
Landlock ruleset is enforced, it cannot be "relaxed".
@jounathaen jounathaen removed this pull request from the merge queue due to a manual request Apr 28, 2025
@jounathaen jounathaen added this pull request to the merge queue Apr 28, 2025
Merged via the queue into hermit-os:main with commit e26a8b9 Apr 28, 2025
28 checks passed
@jounathaen jounathaen deleted the sandbox-landlock branch April 28, 2025 13:56
@n0toose n0toose added feature/security Concerns security-related behavior, soundness, isolation or reliability. feature/file system labels Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/file system feature/security Concerns security-related behavior, soundness, isolation or reliability.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File Isolation: Add support for Landlock
2 participants