landlock/errata.rs
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3use crate::compat::ABI;
4use crate::{uapi, BitFlags};
5use enumflags2::bitflags;
6
7/// Fixed kernel issues for the running Landlock implementation.
8///
9/// Each variant represents a specific bug fix that may have been
10/// backported to the running kernel. Use [`Erratum::current()`]
11/// before building a [`Ruleset`](crate::Ruleset) to decide which
12/// features are safe to use.
13///
14/// An [`ABI`] version can be converted into the set of applicable errata
15/// with `BitFlags::<Erratum>::from(abi)`.
16///
17/// # Warning
18///
19/// Most applications should **not** check errata. Disabling a sandboxing
20/// feature because an erratum is not fixed could leave the system **less**
21/// secure than using Landlock's best-effort protection with the buggy
22/// feature enabled. Errata should only be used to **add** features
23/// (e.g., enabling a restriction only when its bug is confirmed fixed),
24/// never to remove them.
25#[bitflags]
26#[repr(u32)]
27#[derive(Copy, Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum Erratum {
30 /// Erratum 1 (ABI 4): non-TCP stream sockets (SMC, MPTCP, SCTP)
31 /// were incorrectly restricted by TCP access rights during
32 /// `bind(2)` and `connect(2)`.
33 ///
34 /// Affects [`crate::AccessNet::BindTcp`] and [`crate::AccessNet::ConnectTcp`].
35 ///
36 /// See [erratum 1](https://docs.kernel.org/userspace-api/landlock.html#erratum-1-tcp-socket-identification).
37 TcpSocketIdentification = 1 << 0,
38 /// Erratum 2 (ABI 6): signal scoping was overly restrictive,
39 /// preventing sandboxed threads from signaling other threads
40 /// within the same process in different domains.
41 ///
42 /// Affects [`crate::Scope::Signal`].
43 ///
44 /// See [erratum 2](https://docs.kernel.org/userspace-api/landlock.html#erratum-2-scoped-signal-handling).
45 ScopedSignalHandling = 1 << 1,
46 /// Erratum 3 (ABI 1): access rights could be widened through
47 /// rename or link actions on disconnected directories under
48 /// bind mounts, potentially bypassing `LANDLOCK_ACCESS_FS_REFER`
49 /// restrictions.
50 ///
51 /// See [erratum 3](https://docs.kernel.org/userspace-api/landlock.html#erratum-3-disconnected-directory-handling).
52 DisconnectedDirectoryHandling = 1 << 2,
53}
54
55impl Erratum {
56 /// Queries the running kernel for fixed errata.
57 ///
58 /// Returns a bitmask of errata that have been fixed in the running
59 /// kernel. Unknown errata bits from newer kernels are preserved.
60 /// Returns empty if the kernel doesn't support the errata interface.
61 pub fn current() -> BitFlags<Self> {
62 let ret = unsafe {
63 uapi::landlock_create_ruleset(std::ptr::null(), 0, uapi::LANDLOCK_CREATE_RULESET_ERRATA)
64 };
65 if ret >= 0 {
66 // SAFETY: The kernel may return bits unknown to this crate version.
67 // Using from_bits_unchecked to preserve them.
68 unsafe { BitFlags::from_bits_unchecked(ret as u32) }
69 } else {
70 BitFlags::empty()
71 }
72 }
73}
74
75/// Converts an [`ABI`] version into the set of errata applicable to that ABI.
76///
77/// An erratum is applicable if the ABI includes the feature affected by the bug.
78/// For example, [`Erratum::TcpSocketIdentification`] is only applicable to
79/// [`ABI::V4`] and later, since TCP access rights were introduced in that version.
80///
81/// Uses the same incremental accumulation pattern as
82/// [`AccessFs::from_write()`](crate::AccessFs::from_write).
83///
84/// # Stability
85///
86/// The set of errata returned for a given ABI may grow in future versions
87/// of this crate as new kernel bug fixes are identified and backported.
88/// Do not rely on the exact set being stable across crate versions.
89impl From<ABI> for BitFlags<Erratum> {
90 fn from(abi: ABI) -> Self {
91 match abi {
92 ABI::Unsupported => BitFlags::empty(),
93 // Erratum 3: disconnected directory handling (FS, ABI 1+).
94 ABI::V1 | ABI::V2 | ABI::V3 => Erratum::DisconnectedDirectoryHandling.into(),
95 // Erratum 1: TCP socket identification (net, ABI 4+).
96 ABI::V4 | ABI::V5 => Self::from(ABI::V3) | Erratum::TcpSocketIdentification,
97 // Erratum 2: scoped signal handling (scopes, ABI 6+).
98 // When adding a new ABI version without new errata, append it here.
99 ABI::V6 | ABI::V7 | ABI::V8 | ABI::V9 => {
100 Self::from(ABI::V5) | Erratum::ScopedSignalHandling
101 }
102 }
103 }
104}
105
106/// Extracts the (major, minor, patch, suffix) from /proc/version's
107/// "Linux version X.Y.Z-suffix..." line.
108///
109/// Returns `None` if the input does not start with "Linux version " or if
110/// the major/minor numbers cannot be parsed. The patch number defaults to
111/// 0 when absent (e.g., for RC kernels).
112#[cfg(test)]
113fn parse_kernel_version(proc_version: &str) -> Option<(u32, u32, u32, &str)> {
114 let after_prefix = proc_version.strip_prefix("Linux version ")?;
115 let token = after_prefix.split_whitespace().next()?;
116 let first_dot = token.find('.')?;
117 let major: u32 = token[..first_dot].parse().ok()?;
118 let rest = &token[first_dot + 1..];
119 let minor_end = rest
120 .find(|c: char| !c.is_ascii_digit())
121 .unwrap_or(rest.len());
122 let minor: u32 = rest[..minor_end].parse().ok()?;
123 let after_minor = &rest[minor_end..];
124 // If a second dot follows, parse the patch number; otherwise default to 0.
125 let (patch, suffix) = match after_minor.strip_prefix('.') {
126 Some(after_dot) => {
127 let patch_end = after_dot
128 .find(|c: char| !c.is_ascii_digit())
129 .unwrap_or(after_dot.len());
130 let patch: u32 = after_dot[..patch_end].parse().unwrap_or(0);
131 (patch, &after_dot[patch_end..])
132 }
133 None => (0, after_minor),
134 };
135 Some((major, minor, patch, suffix))
136}
137
138#[test]
139fn parse_kernel_version_cases() {
140 // Distro-suffixed stable release.
141 assert_eq!(
142 parse_kernel_version("Linux version 6.15.0-29-generic (build@host) ..."),
143 Some((6, 15, 0, "-29-generic")),
144 );
145 // Distro-suffixed older stable.
146 assert_eq!(
147 parse_kernel_version("Linux version 5.10.234-1-amd64 (debian) ..."),
148 Some((5, 10, 234, "-1-amd64")),
149 );
150 // Release candidate without patch number.
151 assert_eq!(
152 parse_kernel_version("Linux version 6.15-rc1 (...)"),
153 Some((6, 15, 0, "-rc1")),
154 );
155 // Bare version with no suffix.
156 assert_eq!(
157 parse_kernel_version("Linux version 6.15"),
158 Some((6, 15, 0, "")),
159 );
160 // Stable patch level with no distro suffix.
161 assert_eq!(
162 parse_kernel_version("Linux version 6.12.5"),
163 Some((6, 12, 5, "")),
164 );
165 // Missing prefix.
166 assert_eq!(parse_kernel_version(""), None);
167 assert_eq!(parse_kernel_version("Some other text"), None);
168 // Unparseable version after prefix.
169 assert_eq!(parse_kernel_version("Linux version garbage"), None);
170}
171
172/// Returns the set of errata that have not been backported yet for a given
173/// kernel version.
174///
175/// This is the single source of truth for known backport gaps. The version
176/// is the (major, minor, patch, suffix) parsed from /proc/version.
177/// Backports are made per kernel version (not per Landlock ABI), so this
178/// lookup is keyed by kernel version. The patch number and distro suffix
179/// allow narrowing to specific kernel builds when a fix arrives in a stable
180/// patch level or distro-specific backport. When an erratum is backported
181/// to a kernel version, remove it from the corresponding match arm. The
182/// CI will catch mismatches.
183#[cfg(test)]
184fn not_backported_yet(version: (u32, u32, u32, &str)) -> BitFlags<Erratum> {
185 match version {
186 // TODO: erratum 3 (DisconnectedDirectoryHandling) should be backported.
187 (5, 15, _, _) | (6, 1, _, _) => Erratum::DisconnectedDirectoryHandling.into(),
188
189 // 6.15: errata 1 and 2 backported.
190 // TODO: erratum 3 (DisconnectedDirectoryHandling) should be backported.
191 (6, 15, _, _) => Erratum::DisconnectedDirectoryHandling.into(),
192
193 // 6.4, 6.7, 6.10: EOL, no errata interface on stable.kernel.
194 // 6.12: all errata backported.
195 // 7.0: all errata backported (erratum 3 fix landed via errata/abi-1.h).
196 // 7.1: all errata backported (no new errata in ABI v9).
197 // Future or unknown kernel: assume all errata backported.
198 _ => BitFlags::empty(),
199 }
200}
201
202#[test]
203fn errata_query() {
204 // Verifies the syscall wrapper works on any kernel.
205 let _errata = Erratum::current();
206}
207
208#[test]
209fn errata_up_to_date() {
210 use crate::compat::{ABI, TEST_ABI, TEST_ABI_ENV_NAME};
211
212 // Print /proc/version for diagnostic info when this test runs in CI.
213 let proc_version = std::fs::read_to_string("/proc/version").unwrap_or_default();
214 eprintln!("/proc/version: {}", proc_version.trim());
215
216 // This test requires LANDLOCK_CRATE_TEST_ABI to be explicitly set because
217 // the errata assertions are tied to specific CI kernel versions. Without
218 // it, TEST_ABI is auto-detected from the running kernel, but From<i32>
219 // maps unknown ABI versions to the highest known one, making the
220 // ABI-to-kernel mapping ambiguous (e.g., a 6.15 kernel maps to V6 before
221 // ABI::V7 exists). Since Erratum::current() queries the real kernel, the
222 // expected errata for the declared ABI may not match.
223 if std::env::var(TEST_ABI_ENV_NAME).is_err() {
224 eprintln!("Skipping errata_up_to_date: {} not set", TEST_ABI_ENV_NAME,);
225 return;
226 }
227
228 let kernel_version =
229 parse_kernel_version(&proc_version).expect("Failed to parse /proc/version");
230 eprintln!("Parsed kernel version: {:?}", kernel_version);
231
232 let current = Erratum::current();
233 let applicable: BitFlags<Erratum> = (*TEST_ABI).into();
234 let expected = applicable & !not_backported_yet(kernel_version);
235
236 // Kernel must never report errata for features absent from this ABI.
237 assert!(
238 current & !applicable == BitFlags::empty(),
239 "kernel reported errata not applicable to ABI {:?}: {:?}",
240 *TEST_ABI,
241 current & !applicable,
242 );
243
244 match *TEST_ABI {
245 ABI::Unsupported => assert!(current.is_empty()),
246 ABI::V1 | ABI::V2 => assert_eq!(current, expected),
247 // 6.4, 6.7, 6.10: EOL, no errata interface on stable.kernel.
248 ABI::V3 | ABI::V4 | ABI::V5 => {}
249 ABI::V6 | ABI::V7 | ABI::V8 | ABI::V9 => assert_eq!(current, expected),
250 }
251}