Skip to content

feat(libcgroups): add systemd_base, the systemd manager without the eBPF filter - #3939

Open
nappa85 wants to merge 2 commits into
youki-dev:mainfrom
nappa85:libcgroups-systemd-base
Open

nappa85 wants to merge 2 commits into
youki-dev:mainfrom
nappa85:libcgroups-systemd-base

Conversation

@nappa85

@nappa85 nappa85 commented Nov 24, 2026

Copy link
Copy Markdown

Description

Since #3721 the systemd feature implies cgroupsv2_devices, so every build with the systemd manager compiles libbpf from C source through libbpf-sys. That brings in a C toolchain, make, and the libelf and zlib headers on every build host, plus libelf for every cross target.

That is a problem for pure-Rust users of libcgroups. hakoniwa is a Rust sandbox whose cgroups feature has so far built as pure Rust through libcgroups. Updating it to 0.8.0 (souk4711/hakoniwa#209) means taking on the C build, a new licence (BSD-2-Clause) and libelf in its aarch64 and musl release builds. Yet hakoniwa usually runs rootless, where libcgroups 0.8 skips the eBPF filter anyway.

This PR adds systemd_base, the systemd manager without the eBPF device filter:

systemd = ["systemd_base", "cgroupsv2_devices"]
systemd_base = ["v2", "nix/socket", "nix/uio"]

systemd and the default features keep exactly their current meaning, so nothing changes for existing users. Crates that can't take the C dependency opt into systemd_base.

  • The cfg(feature = "systemd") gates in libcgroups now use systemd_base, which systemd implies.
  • The device rule Emulator and default_devices()/default_allow_devices() are pure Rust and are now available to systemd_base. Only bpf, program and the eBPF controller still require cgroupsv2_devices.
  • Without the filter, device rules stay safe. Allow-only rule sets (the OCI default) are fully expressed through DevicePolicy/DeviceAllow. A rule set with a deny rule, which DeviceAllow cannot express, is refused with the new SystemdDevicesError::DenyRuleWithoutFilter. Without that, the deny-all fallback would silently leave the container with no devices; with the filter, behaviour is unchanged.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test updates
  • CI/CD related changes
  • Other (please describe):

Testing

  • Added new unit tests

  • Added new integration tests

  • Ran existing test suite

  • Tested manually (please provide steps)

  • A new test checks that a deny rule is refused without the filter. The two deny-all fallback tests now run only with cgroupsv2_devices.

  • scripts/features_test.sh builds libcgroups with systemd_base and with v1 systemd_base.

  • Builds: libcgroups builds with v1, v2, systemd, systemd_base, v1 systemd_base, v2 cgroupsv2_devices and systemd_base cgroupsv2_devices. With v1 v2 systemd_base, cargo tree shows no libbpf-sys or rbpf.

  • Lint: cargo +nightly fmt --all -- --check and cargo clippy --all --all-targets --all-features -- -D warnings pass. Clippy also passes on libcgroups with systemd_base and with v1,systemd_base.

  • Tests: cargo test -p libcgroups --lib passes 260 tests with the default features and 194 with systemd_base only. In both, the same 9 systemd and D-Bus tests fail on my machine, which has no systemd; they fail the same way on unmodified 0.8.0.

Related Issues

Follows up #3721 and #3904. Related: souk4711/hakoniwa#209.

Additional Context

libcontainer still enables libcgroups/systemd, so youki itself is unchanged. If useful, libcontainer could expose a matching feature later; I kept this PR to libcgroups.

🤖 Generated with Claude Code

…BPF filter

The systemd feature implies cgroupsv2_devices since youki-dev#3721, so every
systemd build compiles libbpf from C source (libbpf-sys, needing libelf
and zlib on every build host and target). systemd_base is the systemd
manager without it; systemd is now systemd_base plus cgroupsv2_devices,
so its meaning and the default features are unchanged.

The device rule emulator and the default device lists are pure Rust and
are now available to systemd_base; only bpf, program and the eBPF
controller still require cgroupsv2_devices. Without the filter, device
rules are expressed only through DevicePolicy/DeviceAllow, and a rule
set with a deny rule, which DeviceAllow cannot express, is refused with
DenyRuleWithoutFilter instead of leaving the container without devices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marco Napetti <marco.napetti@proton.me>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

With systemd_base alone, unsupported allow rules can be silently dropped, so apply() reports success while requested device access remains denied.

1 open issue
What changed in this PR

Adds systemd_base so libcgroups users can use the systemd manager without compiling libbpf or its native dependencies. Existing systemd and default features retain their behavior.

Changes:

  • Splits systemd support from the optional eBPF device filter.
  • Makes pure-Rust device-rule helpers available to systemd_base and rejects deny rules when the filter is disabled.
  • Adds filter-free feature builds and adjusts device-rule tests.
File Description
scripts/​features_test.sh Adds systemd_base feature combinations.
crates/​libcgroups/​src/​v2/​mod.rs Exposes device helpers to systemd_base.
crates/​libcgroups/​src/​v2/​devices/​mod.rs Gates eBPF components separately from the emulator.
crates/​libcgroups/​src/​systemd/​devices.rs Adds filter-free deny-rule rejection and tests.
crates/​libcgroups/​src/​systemd/​dbus_native/​dbus.rs Enables D-Bus tests under systemd_base.
crates/​libcgroups/​src/​lib.rs Selects the systemd implementation through systemd_base.
crates/​libcgroups/​src/​common.rs Updates manager and device-helper feature gates.
crates/​libcgroups/​Cargo.toml Defines systemd_base and preserves existing defaults.

Review effort: Balanced

Comment on lines +123 to +126
#[cfg(not(feature = "cgroupsv2_devices"))]
if has_deny_rule {
return Err(SystemdDevicesError::DenyRuleWithoutFilter);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0b19399. Without cgroupsv2_devices, allow rules that DeviceAllow cannot express are now refused with UnrepresentableRuleWithoutFilter: *:minor, and on systemd < 240 a major with no /proc/devices group or a node that does not exist. With the filter nothing changes (warn and skip). A rule granting no access is left out in both builds, since it grants nothing. Filter-free regression tests are added, and the three tests that expect skipping now run only with cgroupsv2_devices.

With systemd_base alone nothing carries a device rule that DeviceAllow
cannot express, so apply() used to succeed while the requested access
stayed denied. Refuse '*:minor' rules and the systemd-version-dependent
cases (unknown device group, missing node before systemd 240) with
UnrepresentableRuleWithoutFilter. Behaviour with cgroupsv2_devices is
unchanged (warn and skip). Rules granting no access are left out
silently in both builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marco Napetti <marco.napetti@proton.me>
@nayuta723

nayuta723 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

@nappa85
Thank you for the PR!

We'd like to avoid relying on systemd's DevicePolicy/DeviceAllow alone, without youki's own eBPF filter. DeviceAllow can only express allow rules, so deny rules can't be enforced, and which rules are representable depends on the systemd version (e.g. *:minor rules, or majors without a /proc/devices entry on systemd < 240). So we don't think it's a reliable way to enforce device rules.

That said, we agree that systemd shouldn't always force the eBPF filter. How about the following approach instead of adding systemd_base?

  • Remove cgroupsv2_devices from the systemd feature, and add cgroupsv2_devices to the default features so the default behavior stays the same.
  • If cgroupsv2_devices is not enabled and device rules are specified, return an error.

This is similar to runc, where device support is opt-in and ErrDevicesUnsupported is returned when device rules are given without it.

What do you think?

@nappa85

nappa85 commented Nov 29, 2026 •

Copy link
Copy Markdown
Author

@nappa85 Thank you for the PR!

We'd like to avoid relying on systemd's DevicePolicy/DeviceAllow alone, without youki's own eBPF filter. DeviceAllow can only express allow rules, so deny rules can't be enforced, and which rules are representable depends on the systemd version (e.g. *:minor rules, or majors without a /proc/devices entry on systemd < 240). So we don't think it's a reliable way to enforce device rules.

That said, we agree that systemd shouldn't always force the eBPF filter. How about the following approach instead of adding systemd_base?

* Remove cgroupsv2_devices from the systemd feature, and add cgroupsv2_devices to the default features so the default behavior stays the same.

* If cgroupsv2_devices is not enabled and device rules are specified, return an error.

This is similar to runc, where device support is opt-in and ErrDevicesUnsupported is returned when device rules are given without it.

What do you think?

My use case, hakoniwa, won't rely on eBPF anyway, because rootless, so #3940 is probably enough to not break musl builds, you can probably close this PR if it's not the desired approach

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants