Skip to content

Commit 4fbfbbc

Browse files
committed
compat: Improve LandlockStatus
Use named fields for the Available variant: effective_abi and kernel_abi, and improve documentation. Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent ec5e00b commit 4fbfbbc

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

examples/sandboxer.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,33 @@ fn main() -> anyhow::Result<()> {
185185
prepend \"landlock,\" to the content of CONFIG_LSM."
186186
);
187187
}
188-
LandlockStatus::Available(_, Some(raw_abi)) => {
188+
LandlockStatus::Available {
189+
kernel_abi: Some(raw_abi),
190+
..
191+
} => {
189192
eprintln!(
190193
"Hint: This sandboxer only supports Landlock ABI version up to {abi} \
191194
whereas the current kernel supports Landlock ABI version {raw_abi}. \
192195
To leverage all Landlock features, update this sandboxer."
193196
);
194197
}
195-
LandlockStatus::Available(current_abi, None) => {
196-
if current_abi < abi {
198+
LandlockStatus::Available {
199+
kernel_abi: None,
200+
effective_abi,
201+
} => {
202+
if effective_abi < abi {
197203
eprintln!(
198204
"Hint: This sandboxer supports Landlock ABI version up to {abi} \
199-
but the current kernel only supports Landlock ABI version {current_abi}. \
205+
but the current kernel only supports Landlock ABI version {effective_abi}. \
200206
To leverage all Landlock features, update the kernel."
201207
);
202-
} else if current_abi > abi {
208+
} else if effective_abi > abi {
203209
// This should not happen because the ABI used by the sandboxer
204210
// should be the latest supported by the Landlock crate, and
205211
// they should be updated at the same time.
206212
eprintln!(
207213
"Warning: This sandboxer only supports Landlock ABI version up to {abi} \
208-
but the current kernel supports Landlock ABI version {current_abi}. \
214+
but the current kernel supports Landlock ABI version {effective_abi}. \
209215
To leverage all Landlock features, update this sandboxer."
210216
);
211217
}

src/compat.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,24 @@ pub enum LandlockStatus {
158158
NotEnabled,
159159
/// Landlock is not implemented (i.e. not built into the running kernel: `ENOSYS`).
160160
NotImplemented,
161-
/// Landlock is available and supported up to the given ABI.
161+
/// Landlock is available and working on the running system.
162162
///
163-
/// `Option<i32>` contains the raw ABI value if it's greater than the greatest known ABI,
164-
/// which would mean that the running kernel is newer than the Landlock crate.
165-
Available(ABI, Option<i32>),
163+
/// This indicates that the kernel supports Landlock and it's properly enabled.
164+
/// The crate uses the `effective_abi` for all operations, which represents
165+
/// the highest ABI version that both the kernel and this crate understand.
166+
Available {
167+
/// The effective ABI version that this crate will use for Landlock operations.
168+
/// This is the intersection of what the kernel supports and what this crate knows about.
169+
effective_abi: ABI,
170+
/// The actual kernel ABI version when it's newer than any ABI supported by this crate.
171+
///
172+
/// If `Some(version)`, it means the running kernel supports Landlock ABI `version`
173+
/// which is higher than the latest ABI known by this crate.
174+
///
175+
/// This field is purely informational and is never used for Landlock operations.
176+
/// The crate always and only uses `effective_abi` for all functionality.
177+
kernel_abi: Option<i32>,
178+
},
166179
}
167180

168181
impl LandlockStatus {
@@ -188,7 +201,10 @@ impl LandlockStatus {
188201
}
189202
} else {
190203
let abi = ABI::from(v);
191-
Self::Available(abi, (v != abi as i32).then_some(v))
204+
Self::Available {
205+
effective_abi: abi,
206+
kernel_abi: (v != abi as i32).then_some(v),
207+
}
192208
}
193209
}
194210
}
@@ -200,10 +216,18 @@ fn test_current_landlock_status() {
200216
if *TEST_ABI == ABI::Unsupported {
201217
assert_eq!(status, LandlockStatus::NotImplemented);
202218
} else {
203-
assert!(matches!(status, LandlockStatus::Available(abi, _) if abi == *TEST_ABI));
219+
assert!(
220+
matches!(status, LandlockStatus::Available { effective_abi, .. } if effective_abi == *TEST_ABI)
221+
);
204222
if std::env::var(TEST_ABI_ENV_NAME).is_ok() {
205223
// We cannot reliably check for unknown kernel.
206-
assert!(matches!(status, LandlockStatus::Available(_, None)));
224+
assert!(matches!(
225+
status,
226+
LandlockStatus::Available {
227+
kernel_abi: None,
228+
..
229+
}
230+
));
207231
}
208232
}
209233
}
@@ -214,7 +238,7 @@ impl From<LandlockStatus> for ABI {
214238
// The only possible error values should be EOPNOTSUPP and ENOSYS,
215239
// but let's convert all kind of errors as unsupported.
216240
LandlockStatus::NotEnabled | LandlockStatus::NotImplemented => ABI::Unsupported,
217-
LandlockStatus::Available(abi, _) => abi,
241+
LandlockStatus::Available { effective_abi, .. } => effective_abi,
218242
}
219243
}
220244
}
@@ -227,7 +251,10 @@ impl From<ABI> for LandlockStatus {
227251
match abi {
228252
// Convert to ENOSYS because of check_ruleset_support() and ruleset_unsupported() tests.
229253
ABI::Unsupported => Self::NotImplemented,
230-
_ => Self::Available(abi, None),
254+
_ => Self::Available {
255+
effective_abi: abi,
256+
kernel_abi: None,
257+
},
231258
}
232259
}
233260
}
@@ -278,7 +305,7 @@ pub(crate) fn get_errno_from_landlock_status() -> Option<i32> {
278305
}
279306
}
280307
}
281-
LandlockStatus::Available(_, _) => None,
308+
LandlockStatus::Available { .. } => None,
282309
}
283310
}
284311

src/ruleset.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,10 @@ fn ruleset_unsupported() {
10621062
.unwrap(),
10631063
RestrictionStatus {
10641064
ruleset: RulesetStatus::NotEnforced,
1065-
landlock: LandlockStatus::Available(ABI::V1, None),
1065+
landlock: LandlockStatus::Available {
1066+
effective_abi: ABI::V1,
1067+
kernel_abi: None,
1068+
},
10661069
// With SoftRequirement, no_new_privs is still enabled, even if there is an error
10671070
// (e.g. unsupported access right).
10681071
no_new_privs: true,
@@ -1184,7 +1187,10 @@ fn ignore_abi_v2_with_abi_v1() {
11841187
.unwrap(),
11851188
RestrictionStatus {
11861189
ruleset: RulesetStatus::NotEnforced,
1187-
landlock: LandlockStatus::Available(ABI::V1, None),
1190+
landlock: LandlockStatus::Available {
1191+
effective_abi: ABI::V1,
1192+
kernel_abi: None,
1193+
},
11881194
no_new_privs: true,
11891195
}
11901196
);

0 commit comments

Comments
 (0)