-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathlints.rs
More file actions
1380 lines (1257 loc) · 47.4 KB
/
lints.rs
File metadata and controls
1380 lines (1257 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! # Implementation of container build lints
//!
//! This module implements `bootc container lint`.
// Unfortunately needed here to work with linkme
#![allow(unsafe_code)]
use std::collections::{BTreeMap, BTreeSet};
use std::env::consts::ARCH;
use std::fmt::{Display, Write as WriteFmt};
use std::num::NonZeroUsize;
use std::ops::ControlFlow;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use anyhow::Result;
use bootc_utils::PathQuotedDisplay;
use camino::{Utf8Path, Utf8PathBuf};
use cap_std::fs::Dir;
use cap_std_ext::cap_std;
use cap_std_ext::cap_std::fs::MetadataExt;
use cap_std_ext::dirext::WalkConfiguration;
use cap_std_ext::dirext::{CapStdExtDirExt as _, WalkComponent};
use fn_error_context::context;
use indoc::indoc;
use linkme::distributed_slice;
use ostree_ext::ostree_prepareroot;
use serde::Serialize;
/// Reference to embedded default baseimage content that should exist.
const BASEIMAGE_REF: &str = "usr/share/doc/bootc/baseimage/base";
// https://systemd.io/API_FILE_SYSTEMS/ with /var added for us
const API_DIRS: &[&str] = &["dev", "proc", "sys", "run", "tmp", "var"];
/// Only output this many items by default
const DEFAULT_TRUNCATED_OUTPUT: NonZeroUsize = const { NonZeroUsize::new(5).unwrap() };
/// A lint check has failed.
#[derive(thiserror::Error, Debug)]
struct LintError(String);
/// The outer error is for unexpected fatal runtime problems; the
/// inner error is for the lint failing in an expected way.
type LintResult = Result<std::result::Result<(), LintError>>;
/// Everything is OK - we didn't encounter a runtime error, and
/// the targeted check passed.
fn lint_ok() -> LintResult {
Ok(Ok(()))
}
/// We successfully found a lint failure.
fn lint_err(msg: impl AsRef<str>) -> LintResult {
Ok(Err(LintError::new(msg)))
}
impl std::fmt::Display for LintError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl LintError {
fn new(msg: impl AsRef<str>) -> Self {
Self(msg.as_ref().to_owned())
}
}
#[derive(Debug, Default)]
struct LintExecutionConfig {
no_truncate: bool,
}
type LintFn = fn(&Dir, config: &LintExecutionConfig) -> LintResult;
type LintRecursiveResult = LintResult;
type LintRecursiveFn = fn(&WalkComponent, config: &LintExecutionConfig) -> LintRecursiveResult;
/// A lint can either operate as it pleases on a target root, or it
/// can be recursive.
#[derive(Debug)]
enum LintFnTy {
/// A lint that doesn't traverse the whole filesystem
Regular(LintFn),
/// A recursive lint
Recursive(LintRecursiveFn),
}
#[distributed_slice]
pub(crate) static LINTS: [Lint];
/// The classification of a lint type.
#[derive(Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
enum LintType {
/// If this fails, it is known to be fatal - the system will not install or
/// is effectively guaranteed to fail at runtime.
Fatal,
/// This is not a fatal problem, but something you likely want to fix.
Warning,
}
#[derive(Debug, Copy, Clone)]
pub(crate) enum WarningDisposition {
AllowWarnings,
FatalWarnings,
}
#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq)]
pub(crate) enum RootType {
Running,
Alternative,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
struct Lint {
name: &'static str,
#[serde(rename = "type")]
ty: LintType,
#[serde(skip)]
f: LintFnTy,
description: &'static str,
// Set if this only applies to a specific root type.
#[serde(skip_serializing_if = "Option::is_none")]
root_type: Option<RootType>,
}
// We require lint names to be unique, so we can just compare based on those.
impl PartialEq for Lint {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Lint {}
impl std::hash::Hash for Lint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
impl PartialOrd for Lint {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Lint {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name.cmp(other.name)
}
}
impl Lint {
pub(crate) const fn new_fatal(
name: &'static str,
description: &'static str,
f: LintFn,
) -> Self {
Lint {
name,
ty: LintType::Fatal,
f: LintFnTy::Regular(f),
description,
root_type: None,
}
}
pub(crate) const fn new_warning(
name: &'static str,
description: &'static str,
f: LintFn,
) -> Self {
Lint {
name,
ty: LintType::Warning,
f: LintFnTy::Regular(f),
description,
root_type: None,
}
}
const fn set_root_type(mut self, v: RootType) -> Self {
self.root_type = Some(v);
self
}
}
pub(crate) fn lint_list(output: impl std::io::Write) -> Result<()> {
// Dump in yaml format by default, it's readable enough
serde_yaml::to_writer(output, &*LINTS)?;
Ok(())
}
#[derive(Debug)]
struct LintExecutionResult {
warnings: usize,
passed: usize,
skipped: usize,
fatal: usize,
}
// Helper function to format items with optional truncation
fn format_items<T>(
config: &LintExecutionConfig,
header: &str,
items: impl Iterator<Item = T>,
o: &mut String,
) -> Result<()>
where
T: Display,
{
let mut items = items.into_iter();
if config.no_truncate {
let Some(first) = items.next() else {
return Ok(());
};
writeln!(o, "{header}:")?;
writeln!(o, " {first}")?;
for item in items {
writeln!(o, " {item}")?;
}
return Ok(());
} else {
let Some((samples, rest)) = bootc_utils::collect_until(items, DEFAULT_TRUNCATED_OUTPUT)
else {
return Ok(());
};
writeln!(o, "{header}:")?;
for item in samples {
writeln!(o, " {item}")?;
}
if rest > 0 {
writeln!(o, " ...and {rest} more")?;
}
}
Ok(())
}
// Helper to build a lint error message from multiple sections.
// The closure `build_message_fn` is responsible for calling `format_items`
// to populate the message buffer.
fn format_lint_err_from_items<T>(
config: &LintExecutionConfig,
header: &str,
items: impl Iterator<Item = T>,
) -> LintResult
where
T: Display,
{
let mut msg = String::new();
// SAFETY: Writing to a string can't fail
format_items(config, header, items, &mut msg).unwrap();
lint_err(msg)
}
fn lint_inner<'skip>(
root: &Dir,
root_type: RootType,
config: &LintExecutionConfig,
skip: impl IntoIterator<Item = &'skip str>,
mut output: impl std::io::Write,
) -> Result<LintExecutionResult> {
let mut fatal = 0usize;
let mut warnings = 0usize;
let mut passed = 0usize;
let skip: std::collections::HashSet<_> = skip.into_iter().collect();
let (mut applicable_lints, skipped_lints): (Vec<_>, Vec<_>) = LINTS.iter().partition(|lint| {
if skip.contains(lint.name) {
return false;
}
if let Some(lint_root_type) = lint.root_type {
if lint_root_type != root_type {
return false;
}
}
true
});
// SAFETY: Length must be smaller.
let skipped = skipped_lints.len();
// Default to predictablility here
applicable_lints.sort_by(|a, b| a.name.cmp(b.name));
// Split the lints by type
let (nonrec_lints, recursive_lints): (Vec<_>, Vec<_>) = applicable_lints
.into_iter()
.partition(|lint| matches!(lint.f, LintFnTy::Regular(_)));
let mut results = Vec::new();
for lint in nonrec_lints {
let f = match lint.f {
LintFnTy::Regular(f) => f,
LintFnTy::Recursive(_) => unreachable!(),
};
results.push((lint, f(&root, &config)));
}
let mut recursive_lints = BTreeSet::from_iter(recursive_lints);
let mut recursive_errors = BTreeMap::new();
root.walk(
&WalkConfiguration::default()
.noxdev()
.path_base(Path::new("/")),
|e| -> std::io::Result<_> {
// If there's no recursive lints, we're done!
if recursive_lints.is_empty() {
return Ok(ControlFlow::Break(()));
}
// Keep track of any errors we caught while iterating over
// the recursive lints.
let mut this_iteration_errors = Vec::new();
// Call each recursive lint on this directory entry.
for &lint in recursive_lints.iter() {
let f = match &lint.f {
// SAFETY: We know this set only holds recursive lints
LintFnTy::Regular(_) => unreachable!(),
LintFnTy::Recursive(f) => f,
};
// Keep track of the error if we found one
match f(e, &config) {
Ok(Ok(())) => {}
o => this_iteration_errors.push((lint, o)),
}
}
// For each recursive lint that errored, remove it from
// the set that we will continue running.
for (lint, err) in this_iteration_errors {
recursive_lints.remove(lint);
recursive_errors.insert(lint, err);
}
Ok(ControlFlow::Continue(()))
},
)?;
// Extend our overall result set with the recursive-lint errors.
results.extend(recursive_errors);
// Any recursive lint still in this list succeeded.
results.extend(recursive_lints.into_iter().map(|lint| (lint, lint_ok())));
for (lint, r) in results {
let name = lint.name;
let r = match r {
Ok(r) => r,
Err(e) => anyhow::bail!("Unexpected runtime error running lint {name}: {e}"),
};
if let Err(e) = r {
match lint.ty {
LintType::Fatal => {
writeln!(output, "Failed lint: {name}: {e}")?;
fatal += 1;
}
LintType::Warning => {
writeln!(output, "Lint warning: {name}: {e}")?;
warnings += 1;
}
}
} else {
// We'll be quiet for now
tracing::debug!("OK {name} (type={:?})", lint.ty);
passed += 1;
}
}
Ok(LintExecutionResult {
passed,
skipped,
warnings,
fatal,
})
}
#[context("Linting")]
pub(crate) fn lint<'skip>(
root: &Dir,
warning_disposition: WarningDisposition,
root_type: RootType,
skip: impl IntoIterator<Item = &'skip str>,
mut output: impl std::io::Write,
no_truncate: bool,
) -> Result<()> {
let config = LintExecutionConfig { no_truncate };
let r = lint_inner(root, root_type, &config, skip, &mut output)?;
writeln!(output, "Checks passed: {}", r.passed)?;
if r.skipped > 0 {
writeln!(output, "Checks skipped: {}", r.skipped)?;
}
let fatal = if matches!(warning_disposition, WarningDisposition::FatalWarnings) {
r.fatal + r.warnings
} else {
r.fatal
};
if r.warnings > 0 {
writeln!(output, "Warnings: {}", r.warnings)?;
}
if fatal > 0 {
anyhow::bail!("Checks failed: {}", fatal)
}
Ok(())
}
/// check for the existence of the /var/run directory
/// if it exists we need to check that it links to /run if not error
#[distributed_slice(LINTS)]
static LINT_VAR_RUN: Lint = Lint::new_fatal(
"var-run",
"Check for /var/run being a physical directory; this is always a bug.",
check_var_run,
);
fn check_var_run(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
if let Some(meta) = root.symlink_metadata_optional("var/run")? {
if !meta.is_symlink() {
return lint_err("Not a symlink: var/run");
}
}
lint_ok()
}
#[distributed_slice(LINTS)]
static LINT_BUILDAH_INJECTED: Lint = Lint::new_warning(
"buildah-injected",
indoc::indoc! { "
Check for an invalid /etc/hostname or /etc/resolv.conf that may have been injected by
a container build system." },
check_buildah_injected,
)
// This one doesn't make sense to run looking at the running root,
// because we do expect /etc/hostname to be injected as
.set_root_type(RootType::Alternative);
fn check_buildah_injected(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
const RUNTIME_INJECTED: &[&str] = &["etc/hostname", "etc/resolv.conf"];
for ent in RUNTIME_INJECTED {
if let Some(meta) = root.symlink_metadata_optional(ent)? {
if meta.is_file() && meta.size() == 0 {
return lint_err(format!("/{ent} is an empty file; this may have been synthesized by a container runtime."));
}
}
}
lint_ok()
}
#[distributed_slice(LINTS)]
static LINT_ETC_USRUSETC: Lint = Lint::new_fatal(
"etc-usretc",
indoc! { r#"
Verify that only one of /etc or /usr/etc exist. You should only have /etc
in a container image. It will cause undefined behavior to have both /etc
and /usr/etc.
"# },
check_usretc,
);
fn check_usretc(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
let etc_exists = root.symlink_metadata_optional("etc")?.is_some();
// For compatibility/conservatism don't bomb out if there's no /etc.
if !etc_exists {
return lint_ok();
}
// But having both /etc and /usr/etc is not something we want to support.
if root.symlink_metadata_optional("usr/etc")?.is_some() {
return lint_err(
"Found /usr/etc - this is a bootc implementation detail and not supported to use in containers"
);
}
lint_ok()
}
/// Validate that we can parse the /usr/lib/bootc/kargs.d files.
#[distributed_slice(LINTS)]
static LINT_KARGS: Lint = Lint::new_fatal(
"bootc-kargs",
"Verify syntax of /usr/lib/bootc/kargs.d.",
check_parse_kargs,
);
fn check_parse_kargs(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
let args = crate::bootc_kargs::get_kargs_in_root(root, ARCH)?;
tracing::debug!("found kargs: {args:?}");
lint_ok()
}
#[distributed_slice(LINTS)]
static LINT_KERNEL: Lint = Lint::new_fatal(
"kernel",
indoc! { r#"
Check for multiple kernels, i.e. multiple directories of the form /usr/lib/modules/$kver.
Only one kernel is supported in an image.
"# },
check_kernel,
);
fn check_kernel(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
let result = ostree_ext::bootabletree::find_kernel_dir_fs(&root)?;
tracing::debug!("Found kernel: {:?}", result);
lint_ok()
}
// This one can be lifted in the future, see https://github.com/bootc-dev/bootc/issues/975
#[distributed_slice(LINTS)]
static LINT_UTF8: Lint = Lint {
name: "utf8",
description: indoc! { r#"
Check for non-UTF8 filenames. Currently, the ostree backend of bootc only supports
UTF-8 filenames. Non-UTF8 filenames will cause a fatal error.
"#},
ty: LintType::Fatal,
root_type: None,
f: LintFnTy::Recursive(check_utf8),
};
fn check_utf8(e: &WalkComponent, _config: &LintExecutionConfig) -> LintRecursiveResult {
let path = e.path;
let filename = e.filename;
let dirname = path.parent().unwrap_or(Path::new("/"));
if filename.to_str().is_none() {
// This escapes like "abc\xFFdéf"
return lint_err(format!(
"{}: Found non-utf8 filename {filename:?}",
PathQuotedDisplay::new(&dirname)
));
};
if e.file_type.is_symlink() {
let target = e.dir.read_link_contents(filename)?;
if target.to_str().is_none() {
return lint_err(format!(
"{}: Found non-utf8 symlink target",
PathQuotedDisplay::new(&path)
));
}
}
lint_ok()
}
fn check_prepareroot_composefs_norecurse(dir: &Dir) -> LintResult {
let path = ostree_ext::ostree_prepareroot::CONF_PATH;
let Some(config) = ostree_prepareroot::load_config_from_root(dir)? else {
return lint_err(format!("{path} is not present to enable composefs"));
};
if !ostree_prepareroot::overlayfs_enabled_in_config(&config)? {
return lint_err(format!("{path} does not have composefs enabled"));
}
lint_ok()
}
#[distributed_slice(LINTS)]
static LINT_API_DIRS: Lint = Lint::new_fatal(
"api-base-directories",
indoc! { r#"
Verify that expected base API directories exist. For more information
on these, see <https://systemd.io/API_FILE_SYSTEMS/>.
Note that in addition, bootc requires that `/var` exist as a directory.
"#},
check_api_dirs,
);
fn check_api_dirs(root: &Dir, _config: &LintExecutionConfig) -> LintResult {
for d in API_DIRS {
let Some(meta) = root.symlink_metadata_optional(d)? else {
return lint_err(format!("Missing API filesystem base directory: /{d}"));
};
if !meta.is_dir() {
return lint_err(format!(
"Expected directory for API filesystem base directory: /{d}"
));
}
}
lint_ok()
}
#[distributed_slice(LINTS)]
static LINT_COMPOSEFS: Lint = Lint::new_warning(
"baseimage-composefs",
indoc! { r#"
Check that composefs is enabled for ostree. More in
<https://ostreedev.github.io/ostree/composefs/>.
"#},
check_composefs,
);
fn check_composefs(dir: &Dir, _config: &LintExecutionConfig) -> LintResult {
if let Err(e) = check_prepareroot_composefs_norecurse(dir)? {
return Ok(Err(e));
}
// If we have our own documentation with the expected root contents
// embedded, then check that too! Mostly just because recursion is fun.
if let Some(dir) = dir.open_dir_optional(BASEIMAGE_REF)? {
if let Err(e) = check_prepareroot_composefs_norecurse(&dir)? {
return Ok(Err(e));
}
}
lint_ok()
}
/// Check for a few files and directories we expect in the base image.
fn check_baseimage_root_norecurse(dir: &Dir, _config: &LintExecutionConfig) -> LintResult {
// Check /sysroot
let meta = dir.symlink_metadata_optional("sysroot")?;
match meta {
Some(meta) if !meta.is_dir() => return lint_err("Expected a directory for /sysroot"),
None => return lint_err("Missing /sysroot"),
_ => {}
}
// Check /ostree -> sysroot/ostree
let Some(meta) = dir.symlink_metadata_optional("ostree")? else {
return lint_err("Missing ostree -> sysroot/ostree link");
};
if !meta.is_symlink() {
return lint_err("/ostree should be a symlink");
}
let link = dir.read_link_contents("ostree")?;
let expected = "sysroot/ostree";
if link.as_os_str().as_bytes() != expected.as_bytes() {
return lint_err(format!("Expected /ostree -> {expected}, not {link:?}"));
}
lint_ok()
}
/// Check ostree-related base image content.
#[distributed_slice(LINTS)]
static LINT_BASEIMAGE_ROOT: Lint = Lint::new_fatal(
"baseimage-root",
indoc! { r#"
Check that expected files are present in the root of the filesystem; such
as /sysroot and a composefs configuration for ostree. More in
<https://bootc-dev.github.io/bootc/bootc-images.html#standard-image-content>.
"#},
check_baseimage_root,
);
fn check_baseimage_root(dir: &Dir, config: &LintExecutionConfig) -> LintResult {
if let Err(e) = check_baseimage_root_norecurse(dir, config)? {
return Ok(Err(e));
}
// If we have our own documentation with the expected root contents
// embedded, then check that too! Mostly just because recursion is fun.
if let Some(dir) = dir.open_dir_optional(BASEIMAGE_REF)? {
if let Err(e) = check_baseimage_root_norecurse(&dir, config)? {
return Ok(Err(e));
}
}
lint_ok()
}
fn collect_nonempty_regfiles(
root: &Dir,
path: &Utf8Path,
out: &mut BTreeSet<Utf8PathBuf>,
) -> Result<()> {
for entry in root.entries_utf8()? {
let entry = entry?;
let ty = entry.file_type()?;
let path = path.join(entry.file_name()?);
if ty.is_file() {
let meta = entry.metadata()?;
if meta.size() > 0 {
out.insert(path);
}
} else if ty.is_dir() {
let d = entry.open_dir()?;
collect_nonempty_regfiles(d.as_cap_std(), &path, out)?;
}
}
Ok(())
}
#[distributed_slice(LINTS)]
static LINT_VARLOG: Lint = Lint::new_warning(
"var-log",
indoc! { r#"
Check for non-empty regular files in `/var/log`. It is often undesired
to ship log files in container images. Log files in general are usually
per-machine state in `/var`. Additionally, log files often include
timestamps, causing unreproducible container images, and may contain
sensitive build system information.
"#},
check_varlog,
);
fn check_varlog(root: &Dir, config: &LintExecutionConfig) -> LintResult {
let Some(d) = root.open_dir_optional("var/log")? else {
return lint_ok();
};
let mut nonempty_regfiles = BTreeSet::new();
collect_nonempty_regfiles(&d, "/var/log".into(), &mut nonempty_regfiles)?;
if nonempty_regfiles.is_empty() {
return lint_ok();
}
let header = "Found non-empty logfiles";
let items = nonempty_regfiles.iter().map(PathQuotedDisplay::new);
format_lint_err_from_items(config, header, items)
}
#[distributed_slice(LINTS)]
static LINT_VAR_TMPFILES: Lint = Lint::new_warning(
"var-tmpfiles",
indoc! { r#"
Check for content in /var that does not have corresponding systemd tmpfiles.d entries.
This can cause a problem across upgrades because content in /var from the container
image will only be applied on the initial provisioning.
Instead, it's recommended to have /var effectively empty in the container image,
and use systemd tmpfiles.d to generate empty directories and compatibility symbolic links
as part of each boot.
"#},
check_var_tmpfiles,
)
.set_root_type(RootType::Running);
fn check_var_tmpfiles(_root: &Dir, config: &LintExecutionConfig) -> LintResult {
let r = bootc_tmpfiles::find_missing_tmpfiles_current_root()?;
if r.tmpfiles.is_empty() && r.unsupported.is_empty() {
return lint_ok();
}
let mut msg = String::new();
let header = "Found content in /var missing systemd tmpfiles.d entries";
format_items(config, header, r.tmpfiles.iter().map(|v| v as &_), &mut msg)?;
let header = "Found non-directory/non-symlink files in /var";
let items = r.unsupported.iter().map(PathQuotedDisplay::new);
format_items(config, header, items, &mut msg)?;
lint_err(msg)
}
#[distributed_slice(LINTS)]
static LINT_SYSUSERS: Lint = Lint::new_warning(
"sysusers",
indoc! { r#"
Check for users in /etc/passwd and groups in /etc/group that do not have corresponding
systemd sysusers.d entries in /usr/lib/sysusers.d.
This can cause a problem across upgrades because if /etc is not transient and is locally
modified (commonly due to local user additions), then the contents of /etc/passwd in the new container
image may not be visible.
Using systemd-sysusers to allocate users and groups will ensure that these are allocated
on system startup alongside other users.
More on this topic in <https://bootc-dev.github.io/bootc/building/users-and-groups.html>
"# },
check_sysusers,
);
fn check_sysusers(rootfs: &Dir, config: &LintExecutionConfig) -> LintResult {
let r = bootc_sysusers::analyze(rootfs)?;
if r.is_empty() {
return lint_ok();
}
let mut msg = String::new();
let header = "Found /etc/passwd entry without corresponding systemd sysusers.d";
let items = r.missing_users.iter().map(|v| v as &dyn std::fmt::Display);
format_items(config, header, items, &mut msg)?;
let header = "Found /etc/group entry without corresponding systemd sysusers.d";
format_items(config, header, r.missing_groups.into_iter(), &mut msg)?;
lint_err(msg)
}
#[distributed_slice(LINTS)]
static LINT_NONEMPTY_BOOT: Lint = Lint::new_warning(
"nonempty-boot",
indoc! { r#"
The `/boot` directory should be present, but empty. The kernel
content should be in /usr/lib/modules instead in the container image.
Any content here in the container image will be masked at runtime.
"#},
check_boot,
);
fn check_boot(root: &Dir, config: &LintExecutionConfig) -> LintResult {
let Some(d) = root.open_dir_optional("boot")? else {
return lint_err("Missing /boot directory");
};
// First collect all entries to determine if the directory is empty
let entries: Result<Vec<_>, _> = d.entries()?.collect();
let entries = entries?;
if entries.is_empty() {
return lint_ok();
}
// Gather sorted filenames
let mut entries = entries.iter().map(|v| v.file_name()).collect::<Vec<_>>();
entries.sort();
let header = "Found non-empty /boot";
let items = entries.iter().map(PathQuotedDisplay::new);
format_lint_err_from_items(config, header, items)
}
/// Lint for potential uid/gid drift for files under /etc.
/// Warn if files/dirs in /etc are owned by a non-root uid/gid and there is no
/// corresponding tmpfiles.d chown (z/Z) entry covering them.
#[distributed_slice(LINTS)]
static LINT_ETC_UID_DRIFT: Lint = Lint::new_warning(
"etc-uid-drift",
indoc! { r#"
Check for files in /etc owned by non-root users or groups which lack corresponding
systemd tmpfiles.d 'z' or 'Z' entries to chown them at boot. Ownership encoded
in the container may drift across upgrades if /etc is persistent.
This check ignores paths covered by tmpfiles.d chown entries.
"# },
check_etc_uid_drift,
);
fn check_etc_uid_drift(root: &Dir, config: &LintExecutionConfig) -> LintResult {
// Load chown-affecting tmpfiles entries
let ch = bootc_tmpfiles::read_tmpfiles_chowners(root)?;
// Build sets of fixed numeric uids/gids from sysusers
let mut fixed_uids = BTreeSet::new();
let mut fixed_gids = BTreeSet::new();
for ent in bootc_sysusers::read_sysusers(root)? {
match ent {
bootc_sysusers::SysusersEntry::User { uid, .. } => {
if let Some(bootc_sysusers::IdSource::Numeric(n)) = uid {
fixed_uids.insert(n);
}
}
bootc_sysusers::SysusersEntry::Group { id, .. } => {
if let Some(bootc_sysusers::IdSource::Numeric(n)) = id {
fixed_gids.insert(n);
}
}
bootc_sysusers::SysusersEntry::Range { .. } => {}
}
}
let Some(etcd) = root.open_dir_optional("etc")? else {
return lint_ok();
};
// We'll collect problematic items
let mut problems: BTreeSet<std::path::PathBuf> = BTreeSet::new();
// Depth-first walk under /etc
let mut stack: Vec<(Dir, std::path::PathBuf)> = vec![(etcd, std::path::PathBuf::from("/etc"))];
while let Some((dir, abspath)) = stack.pop() {
for ent in dir.entries()? {
let ent = ent?;
let name = ent.file_name();
let child_rel = abspath.join(&name);
// Convert absolute path to Path for chown coverage check
let child_abs_path = child_rel.as_path();
let fty = ent.file_type()?;
if fty.is_symlink() {
// Symlinks are not meaningful for ownership drift
continue;
}
let meta = ent.metadata()?;
// Recurse into subdirectories
if meta.is_dir() {
// Avoid traversing mount points
let rel = child_rel.strip_prefix("/").unwrap();
if let Some(subdir) = root.open_dir_noxdev(rel)? {
stack.push((subdir, child_rel));
}
}
let uid = meta.uid();
let gid = meta.gid();
// uid/gid of 0 (root) is always fine; others are fine if pinned numerically in sysusers
let is_potential_drift = (uid != 0 && !fixed_uids.contains(&uid))
|| (gid != 0 && !fixed_gids.contains(&gid));
if is_potential_drift {
// Ignore if covered by tmpfiles chown
if ch.covers(child_abs_path) {
continue;
}
problems.insert(child_rel);
}
}
}
if problems.is_empty() {
return lint_ok();
}
let header = "Potential uid/gid drift in /etc (non-root-owned without tmpfiles chown)";
let items = problems.iter().map(|p| PathQuotedDisplay::new(p.as_path()));
format_lint_err_from_items(config, header, items)
}
#[cfg(test)]
mod tests {
use std::sync::LazyLock;
use super::*;
static ALTROOT_LINTS: LazyLock<usize> = LazyLock::new(|| {
LINTS
.iter()
.filter(|lint| lint.root_type != Some(RootType::Running))
.count()
});
fn fixture() -> Result<cap_std_ext::cap_tempfile::TempDir> {
// Create a new temporary directory for test fixtures.
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
Ok(tempdir)
}
fn passing_fixture() -> Result<cap_std_ext::cap_tempfile::TempDir> {
// Create a temporary directory fixture that is expected to pass most lints.
let root = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
for d in API_DIRS {
root.create_dir(d)?;
}
root.create_dir_all("usr/lib/modules/5.7.2")?;
root.write("usr/lib/modules/5.7.2/vmlinuz", "vmlinuz")?;
root.create_dir("boot")?;
root.create_dir("sysroot")?;
root.symlink_contents("sysroot/ostree", "ostree")?;
const PREPAREROOT_PATH: &str = "usr/lib/ostree/prepare-root.conf";
const PREPAREROOT: &str =
include_str!("../../../baseimage/base/usr/lib/ostree/prepare-root.conf");
root.create_dir_all(Utf8Path::new(PREPAREROOT_PATH).parent().unwrap())?;
root.atomic_write(PREPAREROOT_PATH, PREPAREROOT)?;
Ok(root)
}
#[test]
fn test_var_run() -> Result<()> {
let root = &fixture()?;
let config = &LintExecutionConfig::default();
// This one should pass
check_var_run(root, config).unwrap().unwrap();
root.create_dir_all("var/run/foo")?;
assert!(check_var_run(root, config).unwrap().is_err());
root.remove_dir_all("var/run")?;
// Now we should pass again
check_var_run(root, config).unwrap().unwrap();
Ok(())
}
#[test]
fn test_api() -> Result<()> {
let root = &passing_fixture()?;
let config = &LintExecutionConfig::default();
// This one should pass
check_api_dirs(root, config).unwrap().unwrap();
root.remove_dir("var")?;
assert!(check_api_dirs(root, config).unwrap().is_err());
root.write("var", "a file for var")?;
assert!(check_api_dirs(root, config).unwrap().is_err());
Ok(())
}
#[test]
fn test_lint_main() -> Result<()> {
let root = &passing_fixture()?;
let config = &LintExecutionConfig::default();
let mut out = Vec::new();
let warnings = WarningDisposition::FatalWarnings;
let root_type = RootType::Alternative;
lint(root, warnings, root_type, [], &mut out, config.no_truncate).unwrap();
root.create_dir_all("var/run/foo")?;
let mut out = Vec::new();
assert!(lint(root, warnings, root_type, [], &mut out, config.no_truncate).is_err());
Ok(())
}
#[test]
fn test_lint_inner() -> Result<()> {
let root = &passing_fixture()?;
let config = &LintExecutionConfig::default();
// Verify that all lints run
let mut out = Vec::new();
let root_type = RootType::Alternative;
let r = lint_inner(root, root_type, config, [], &mut out).unwrap();
let running_only_lints = LINTS.len().checked_sub(*ALTROOT_LINTS).unwrap();
assert_eq!(r.warnings, 0);
assert_eq!(r.fatal, 0);
assert_eq!(r.skipped, running_only_lints);
assert_eq!(r.passed, *ALTROOT_LINTS);
let r = lint_inner(root, root_type, config, ["var-log"], &mut out).unwrap();
// Trigger a failure in var-log by creating a non-empty log file.
root.create_dir_all("var/log/dnf")?;
root.write("var/log/dnf/dnf.log", b"dummy dnf log")?;
assert_eq!(r.passed, ALTROOT_LINTS.checked_sub(1).unwrap());
assert_eq!(r.fatal, 0);
assert_eq!(r.skipped, running_only_lints + 1);
assert_eq!(r.warnings, 0);
// But verify that not skipping it results in a warning
let mut out = Vec::new();
let r = lint_inner(root, root_type, config, [], &mut out).unwrap();
assert_eq!(r.passed, ALTROOT_LINTS.checked_sub(1).unwrap());
assert_eq!(r.fatal, 0);
assert_eq!(r.skipped, running_only_lints);
assert_eq!(r.warnings, 1);
Ok(())
}
#[test]
fn test_kernel_lint() -> Result<()> {
let root = &fixture()?;
let config = &LintExecutionConfig::default();
// This one should pass
check_kernel(root, config).unwrap().unwrap();
root.create_dir_all("usr/lib/modules/5.7.2")?;
root.write("usr/lib/modules/5.7.2/vmlinuz", "old vmlinuz")?;
root.create_dir_all("usr/lib/modules/6.3.1")?;
root.write("usr/lib/modules/6.3.1/vmlinuz", "new vmlinuz")?;
assert!(check_kernel(root, config).is_err());
root.remove_dir_all("usr/lib/modules/5.7.2")?;
// Now we should pass again
check_kernel(root, config).unwrap().unwrap();
Ok(())
}
#[test]
fn test_kargs() -> Result<()> {
let root = &fixture()?;
let config = &LintExecutionConfig::default();
check_parse_kargs(root, config).unwrap().unwrap();
root.create_dir_all("usr/lib/bootc")?;