1use crate::Result;
11use crate::generic_decompress::Decompressor;
12use anyhow::{Context, anyhow};
13use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
14
15use cap_std::io_lifetimes;
16use cap_std_ext::cap_std::fs::Dir;
17use cap_std_ext::cmdext::CapStdExtCommandExt;
18use cap_std_ext::{cap_std, cap_tempfile};
19use containers_image_proxy::oci_spec::image as oci_image;
20use fn_error_context::context;
21use ostree::gio;
22use ostree::prelude::FileExt;
23use std::borrow::Cow;
24use std::collections::{BTreeMap, HashMap};
25use std::io::{BufWriter, Seek, Write};
26use std::path::Path;
27use std::process::Stdio;
28use std::sync::Arc;
29use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};
30use tracing::instrument;
31
32const EXCLUDED_TOPLEVEL_PATHS: &[&str] = &["run", "tmp", "proc", "sys", "dev"];
35
36#[context("Copying entry")]
38pub(crate) fn copy_entry(
39 mut entry: tar::Entry<impl std::io::Read>,
40 dest: &mut tar::Builder<impl std::io::Write>,
41 path: Option<&Path>,
42) -> Result<()> {
43 let path = if let Some(path) = path {
45 path.to_owned()
46 } else {
47 (*entry.path()?).to_owned()
48 };
49 let mut header = entry.header().clone();
50 if let Some(headers) = entry.pax_extensions()? {
51 let mut extensions_to_keep = Vec::new();
57 for ext_res in headers {
58 let ext = ext_res?;
59 let key = ext.key()?;
60 if key != "path" && key != "linkpath" {
61 extensions_to_keep.push((key, ext.value_bytes()));
62 }
63 }
64 if !extensions_to_keep.is_empty() {
65 dest.append_pax_extensions(extensions_to_keep)?;
66 }
67 }
68
69 match entry.header().entry_type() {
73 tar::EntryType::Symlink => {
74 let target = entry.link_name()?.ok_or_else(|| anyhow!("Invalid link"))?;
75 let target: &Utf8Path = (&*target).try_into()?;
77 dest.append_link(&mut header, path, target)
78 }
79 tar::EntryType::Link => {
80 let target = entry.link_name()?.ok_or_else(|| anyhow!("Invalid link"))?;
81 let target: &Utf8Path = (&*target).try_into()?;
82 let target = remap_etc_path(target);
85 dest.append_link(&mut header, path, &*target)
86 }
87 _ => dest.append_data(&mut header, path, entry),
88 }
89 .map_err(Into::into)
90}
91
92#[derive(Debug, Default)]
94#[non_exhaustive]
95pub struct WriteTarOptions {
96 pub base: Option<String>,
98 pub selinux: bool,
101 pub allow_nonusr: bool,
103 pub retain_var: bool,
106}
107
108#[derive(Debug, Default)]
113pub struct WriteTarResult {
114 pub commit: String,
116 pub filtered: BTreeMap<String, u32>,
118}
119
120fn sepolicy_from_base(repo: &ostree::Repo, base: &str) -> Result<tempfile::TempDir> {
123 let cancellable = gio::Cancellable::NONE;
124 let policypath = "usr/etc/selinux";
125 let tempdir = tempfile::tempdir()?;
126 let (root, _) = repo.read_commit(base, cancellable)?;
127 let policyroot = root.resolve_relative_path(policypath);
128 if policyroot.query_exists(cancellable) {
129 let policydest = tempdir.path().join(policypath);
130 std::fs::create_dir_all(policydest.parent().unwrap())?;
131 let opts = ostree::RepoCheckoutAtOptions {
132 mode: ostree::RepoCheckoutMode::User,
133 subpath: Some(Path::new(policypath).to_owned()),
134 ..Default::default()
135 };
136 repo.checkout_at(Some(&opts), ostree::AT_FDCWD, policydest, base, cancellable)?;
137 }
138 Ok(tempdir)
139}
140
141#[derive(Debug, PartialEq, Eq)]
142enum NormalizedPathResult<'a> {
143 Filtered(&'a str),
144 Normal(Utf8PathBuf),
145}
146
147#[derive(Debug, Clone, PartialEq, Eq, Default)]
148pub(crate) struct TarImportConfig {
149 allow_nonusr: bool,
150 remap_factory_var: bool,
151}
152
153fn remap_etc_path(path: &Utf8Path) -> Cow<'_, Utf8Path> {
155 let mut components = path.components();
156 let Some(prefix) = components.next() else {
157 return Cow::Borrowed(path);
158 };
159 let (prefix, first) = if matches!(prefix, Utf8Component::CurDir | Utf8Component::RootDir) {
160 let Some(next) = components.next() else {
161 return Cow::Borrowed(path);
162 };
163 (Some(prefix), next)
164 } else {
165 (None, prefix)
166 };
167 if first.as_str() == "etc" {
168 let usr = Utf8Component::Normal("usr");
169 Cow::Owned(
170 prefix
171 .into_iter()
172 .chain([usr, first])
173 .chain(components)
174 .collect(),
175 )
176 } else {
177 Cow::Borrowed(path)
178 }
179}
180
181fn normalize_validate_path<'a>(
182 path: &'a Utf8Path,
183 config: &'_ TarImportConfig,
184) -> Result<NormalizedPathResult<'a>> {
185 let mut components = path
187 .components()
188 .map(|part| {
189 match part {
190 camino::Utf8Component::RootDir => Ok(camino::Utf8Component::CurDir),
192 camino::Utf8Component::Normal(_) | camino::Utf8Component::CurDir => Ok(part),
194 _ => Err(anyhow!("Invalid path: {}", path)),
196 }
197 })
198 .peekable();
199 let mut ret = Utf8PathBuf::new();
200 if let Some(Ok(camino::Utf8Component::Normal(_))) = components.peek() {
202 ret.push(camino::Utf8Component::CurDir);
203 }
204 let mut found_first = false;
205 let mut excluded = false;
206 for part in components {
207 let part = part?;
208 if excluded {
209 return Ok(NormalizedPathResult::Filtered(part.as_str()));
210 }
211 if !found_first {
212 if let Utf8Component::Normal(part) = part {
213 found_first = true;
214 match part {
215 "usr" => ret.push(part),
217 "etc" => {
219 ret.push("usr/etc");
220 }
221 "var" => {
222 if config.remap_factory_var {
224 ret.push("usr/share/factory/var");
225 } else {
226 ret.push(part)
227 }
228 }
229 o if EXCLUDED_TOPLEVEL_PATHS.contains(&o) => {
230 excluded = true;
233 ret.push(part)
234 }
235 _ if config.allow_nonusr => ret.push(part),
236 _ => {
237 return Ok(NormalizedPathResult::Filtered(part));
238 }
239 }
240 } else {
241 ret.push(part);
242 }
243 } else {
244 ret.push(part);
245 }
246 }
247
248 Ok(NormalizedPathResult::Normal(ret))
249}
250
251pub(crate) fn filter_tar(
261 src: impl std::io::Read,
262 dest: impl std::io::Write,
263 config: &TarImportConfig,
264 tmpdir: &Dir,
265) -> Result<BTreeMap<String, u32>> {
266 let src = std::io::BufReader::new(src);
267 let mut src = tar::Archive::new(src);
268 let dest = BufWriter::new(dest);
269 let mut dest = tar::Builder::new(dest);
270 let mut filtered = BTreeMap::new();
271
272 let ents = src.entries()?;
273
274 tracing::debug!("Filtering tar; config={config:?}");
275
276 let mut changed_sysroot_objects = HashMap::new();
278 let mut new_sysroot_link_targets = HashMap::<Utf8PathBuf, Utf8PathBuf>::new();
279
280 for entry in ents {
281 let mut entry = entry?;
282 let header = entry.header();
283 let path = entry.path()?;
284 let path: &Utf8Path = (&*path).try_into()?;
285 let path = path.strip_prefix("/").unwrap_or(path);
287
288 let is_modified = header.mtime().unwrap_or_default() > 0;
289 let is_regular = header.entry_type() == tar::EntryType::Regular;
290 if path.strip_prefix(crate::tar::REPO_PREFIX).is_ok() {
291 if is_modified && is_regular {
296 tracing::debug!("Processing modified sysroot file {path}");
297 let mut tmpf = cap_tempfile::TempFile::new_anonymous(tmpdir)
299 .map(BufWriter::new)
300 .context("Creating tmpfile")?;
301 let path = path.to_owned();
302 let header = header.clone();
303 std::io::copy(&mut entry, &mut tmpf)
304 .map_err(anyhow::Error::msg)
305 .context("Copying")?;
306 let mut tmpf = tmpf.into_inner()?;
307 tmpf.seek(std::io::SeekFrom::Start(0))?;
308 changed_sysroot_objects.insert(path, (header, tmpf));
310 continue;
311 }
312 } else if header.entry_type() == tar::EntryType::Link && is_modified {
313 let target = header
314 .link_name()?
315 .ok_or_else(|| anyhow!("Invalid empty hardlink"))?;
316 let target: &Utf8Path = (&*target).try_into()?;
317 let target = path.strip_prefix("/").unwrap_or(target);
319 if target.strip_prefix(crate::tar::REPO_PREFIX).is_ok() {
321 if let Some((mut header, data)) = changed_sysroot_objects.remove(target) {
323 tracing::debug!("Making {path} canonical for sysroot link {target}");
324 dest.append_data(&mut header, path, data)?;
326 new_sysroot_link_targets.insert(target.to_owned(), path.to_owned());
328 } else if let Some(real_target) = new_sysroot_link_targets.get(target) {
329 tracing::debug!("Relinking {path} to {real_target}");
330 let mut header = header.clone();
333 dest.append_link(&mut header, path, real_target)?;
334 } else {
335 tracing::debug!("Found unhandled modified link from {path} to {target}");
336 }
337 continue;
338 }
339 }
340
341 let normalized = match normalize_validate_path(path, config)? {
342 NormalizedPathResult::Filtered(path) => {
343 tracing::trace!("Filtered: {path}");
344 if let Some(v) = filtered.get_mut(path) {
345 *v += 1;
346 } else {
347 filtered.insert(path.to_string(), 1);
348 }
349 continue;
350 }
351 NormalizedPathResult::Normal(path) => path,
352 };
353
354 copy_entry(entry, &mut dest, Some(normalized.as_std_path()))?;
355 }
356 dest.into_inner()?.flush()?;
357 Ok(filtered)
358}
359
360#[context("Filtering tar stream")]
362async fn filter_tar_async(
363 src: impl AsyncRead + Send + 'static,
364 media_type: oci_image::MediaType,
365 mut dest: impl AsyncWrite + Send + Unpin,
366 config: &TarImportConfig,
367 repo_tmpdir: Dir,
368) -> Result<BTreeMap<String, u32>> {
369 let (tx_buf, mut rx_buf) = tokio::io::duplex(8192);
370 let src = Box::pin(src);
372 let config = config.clone();
373 let tar_transformer = crate::tokio_util::spawn_blocking_flatten(move || {
374 let src = tokio_util::io::SyncIoBridge::new(src);
375 let mut src = Decompressor::new(&media_type, src)?;
376 let dest = tokio_util::io::SyncIoBridge::new(tx_buf);
377
378 let r = filter_tar(&mut src, dest, &config, &repo_tmpdir);
379
380 src.finish()?;
381
382 Ok(r)
383 });
384 let copier = tokio::io::copy(&mut rx_buf, &mut dest);
385 let (r, v) = tokio::join!(tar_transformer, copier);
386 let _v: u64 = v?;
387 r?
388}
389
390#[allow(unsafe_code)] #[instrument(level = "debug", skip_all)]
393pub async fn write_tar(
394 repo: &ostree::Repo,
395 src: impl tokio::io::AsyncRead + Send + Unpin + 'static,
396 media_type: oci_image::MediaType,
397 refname: &str,
398 options: Option<WriteTarOptions>,
399) -> Result<WriteTarResult> {
400 let repo = repo.clone();
401 let options = options.unwrap_or_default();
402 let sepolicy = if options.selinux {
403 if let Some(base) = options.base {
404 Some(sepolicy_from_base(&repo, &base).context("tar: Preparing sepolicy")?)
405 } else {
406 None
407 }
408 } else {
409 None
410 };
411 let mut c = std::process::Command::new("ostree");
412 c.env_remove("G_MESSAGES_DEBUG");
418 let repofd = repo.dfd_as_file()?;
419 let repofd: Arc<io_lifetimes::OwnedFd> = Arc::new(repofd.into());
420 {
421 let c = c
422 .stdin(Stdio::piped())
423 .stdout(Stdio::piped())
424 .stderr(Stdio::piped())
425 .args(["commit"]);
426 c.take_fd_n(repofd.clone(), 3);
427 c.arg("--repo=/proc/self/fd/3");
428 if let Some(sepolicy) = sepolicy.as_ref() {
429 c.arg("--selinux-policy");
430 c.arg(sepolicy.path());
431 }
432 c.arg(format!(
433 "--add-metadata-string=ostree.importer.version={}",
434 env!("CARGO_PKG_VERSION")
435 ));
436 c.args([
437 "--no-bindings",
438 "--tar-autocreate-parents",
439 "--tree=tar=/proc/self/fd/0",
440 "--branch",
441 refname,
442 ]);
443 }
444 let mut c = tokio::process::Command::from(c);
445 c.kill_on_drop(true);
446 let mut r = c.spawn()?;
447 tracing::trace!("Spawned ostree child process");
448 let child_stdin = r.stdin.take().unwrap();
450 let mut child_stdout = r.stdout.take().unwrap();
451 let mut child_stderr = r.stderr.take().unwrap();
452 let import_config = TarImportConfig {
454 allow_nonusr: options.allow_nonusr,
455 remap_factory_var: !options.retain_var,
456 };
457 let repo_tmpdir = Dir::reopen_dir(&repo.dfd_borrow())?
458 .open_dir("tmp")
459 .context("Getting repo tmpdir")?;
460 let filtered_result =
461 filter_tar_async(src, media_type, child_stdin, &import_config, repo_tmpdir);
462 let output_copier = async move {
463 let mut child_stdout_buf = String::new();
465 let mut child_stderr_buf = String::new();
466 let (_a, _b) = tokio::try_join!(
467 child_stdout.read_to_string(&mut child_stdout_buf),
468 child_stderr.read_to_string(&mut child_stderr_buf)
469 )?;
470 Ok::<_, anyhow::Error>((child_stdout_buf, child_stderr_buf))
471 };
472
473 let status = async move {
476 let status = r.wait().await?;
477 if !status.success() {
478 return Err(anyhow!("Failed to commit tar: {:?}", status));
479 }
480 anyhow::Ok(())
481 };
482 tracing::debug!("Waiting on child process");
483 let (filtered_result, child_stdout) =
484 match tokio::try_join!(status, filtered_result).context("Processing tar") {
485 Ok(((), filtered_result)) => {
486 let (child_stdout, _) = output_copier.await.context("Copying child output")?;
487 (filtered_result, child_stdout)
488 }
489 Err(e) => {
490 if let Ok((_, child_stderr)) = output_copier.await {
491 let child_stderr = child_stderr.trim();
493 Err(e.context(child_stderr.to_string()))?
494 } else {
495 Err(e)?
496 }
497 }
498 };
499 drop(sepolicy);
500
501 tracing::trace!("tar written successfully");
502 let s = child_stdout.trim();
504 Ok(WriteTarResult {
505 commit: s.to_string(),
506 filtered: filtered_result,
507 })
508}
509
510#[cfg(test)]
511mod tests {
512 use super::*;
513 use std::io::Cursor;
514
515 #[test]
516 fn test_remap_etc() {
517 let unchanged = ["", "foo", "/etcc/foo", "../etc/baz"];
519 for x in unchanged {
520 similar_asserts::assert_eq!(x, remap_etc_path(x.into()).as_str());
521 }
522 for (p, expected) in [
525 ("/etc/foo/../bar/baz", "/usr/etc/foo/../bar/baz"),
526 ("etc/foo//bar", "usr/etc/foo/bar"),
527 ("./etc/foo", "./usr/etc/foo"),
528 ("etc", "usr/etc"),
529 ] {
530 similar_asserts::assert_eq!(remap_etc_path(p.into()).as_str(), expected);
531 }
532 }
533
534 #[test]
535 fn test_normalize_path() {
536 let imp_default = &TarImportConfig {
537 allow_nonusr: false,
538 remap_factory_var: true,
539 };
540 let allow_nonusr = &TarImportConfig {
541 allow_nonusr: true,
542 remap_factory_var: true,
543 };
544 let composefs_and_new_ostree = &TarImportConfig {
545 allow_nonusr: true,
546 remap_factory_var: false,
547 };
548 let valid_all = &[
549 ("/usr/bin/blah", "./usr/bin/blah"),
550 ("usr/bin/blah", "./usr/bin/blah"),
551 ("usr///share/.//blah", "./usr/share/blah"),
552 ("var/lib/blah", "./usr/share/factory/var/lib/blah"),
553 ("./var/lib/blah", "./usr/share/factory/var/lib/blah"),
554 ("dev", "./dev"),
555 ("/proc", "./proc"),
556 ("./", "."),
557 ];
558 let valid_nonusr = &[("boot", "./boot"), ("opt/puppet/blah", "./opt/puppet/blah")];
559 for &(k, v) in valid_all {
560 let r = normalize_validate_path(k.into(), imp_default).unwrap();
561 let r2 = normalize_validate_path(k.into(), allow_nonusr).unwrap();
562 assert_eq!(r, r2);
563 match r {
564 NormalizedPathResult::Normal(r) => assert_eq!(r, v),
565 NormalizedPathResult::Filtered(o) => panic!("Should not have filtered {o}"),
566 }
567 }
568 for &(k, v) in valid_nonusr {
569 let strict = normalize_validate_path(k.into(), imp_default).unwrap();
570 assert!(
571 matches!(strict, NormalizedPathResult::Filtered(_)),
572 "Incorrect filter for {k}"
573 );
574 let nonusr = normalize_validate_path(k.into(), allow_nonusr).unwrap();
575 match nonusr {
576 NormalizedPathResult::Normal(r) => assert_eq!(r, v),
577 NormalizedPathResult::Filtered(o) => panic!("Should not have filtered {o}"),
578 }
579 }
580 let filtered = &["/run/blah", "/sys/foo", "/dev/somedev"];
581 for &k in filtered {
582 match normalize_validate_path(k.into(), imp_default).unwrap() {
583 NormalizedPathResult::Filtered(_) => {}
584 NormalizedPathResult::Normal(_) => {
585 panic!("{k} should be filtered")
586 }
587 }
588 }
589 let errs = &["usr/foo/../../bar"];
590 for &k in errs {
591 assert!(normalize_validate_path(k.into(), allow_nonusr).is_err());
592 assert!(normalize_validate_path(k.into(), imp_default).is_err());
593 }
594 assert!(matches!(
595 normalize_validate_path("var/lib/foo".into(), composefs_and_new_ostree).unwrap(),
596 NormalizedPathResult::Normal(_)
597 ));
598 }
599
600 #[tokio::test]
601 async fn tar_filter() -> Result<()> {
602 let tempd = tempfile::tempdir()?;
603 let rootfs = &tempd.path().join("rootfs");
604
605 std::fs::create_dir_all(rootfs.join("etc/systemd/system"))?;
606 std::fs::write(rootfs.join("etc/systemd/system/foo.service"), "fooservice")?;
607 std::fs::write(rootfs.join("blah"), "blah")?;
608 let rootfs_tar_path = &tempd.path().join("rootfs.tar");
609 let rootfs_tar = std::fs::File::create(rootfs_tar_path)?;
610 let mut rootfs_tar = tar::Builder::new(rootfs_tar);
611 rootfs_tar.append_dir_all(".", rootfs)?;
612 let _ = rootfs_tar.into_inner()?;
613 let mut dest = Vec::new();
614 let src = tokio::io::BufReader::new(tokio::fs::File::open(rootfs_tar_path).await?);
615 let cap_tmpdir = Dir::open_ambient_dir(&tempd, cap_std::ambient_authority())?;
616 filter_tar_async(
617 src,
618 oci_image::MediaType::ImageLayer,
619 &mut dest,
620 &Default::default(),
621 cap_tmpdir,
622 )
623 .await?;
624 let dest = dest.as_slice();
625 let mut final_tar = tar::Archive::new(Cursor::new(dest));
626 let destdir = &tempd.path().join("destdir");
627 final_tar.unpack(destdir)?;
628 assert!(destdir.join("usr/etc/systemd/system/foo.service").exists());
629 assert!(!destdir.join("blah").exists());
630 Ok(())
631 }
632
633 #[tokio::test]
637 async fn tar_filter_pax_etc_remap() -> Result<()> {
638 let tempd = tempfile::tempdir()?;
639 let src_tar_path = tempd.path().join("src.tar");
640 let pax_path = "etc/ssl/certs/Főtanúsítvány.pem";
641
642 {
645 let mut builder = tar::Builder::new(std::fs::File::create(&src_tar_path)?);
646 let data = b"cert";
647 let mut header = tar::Header::new_gnu();
648 header.set_size(data.len() as u64);
649 header.set_mode(0o644);
650 header.set_entry_type(tar::EntryType::Regular);
651 header.set_cksum();
652 builder.append_pax_extensions([("path", pax_path.as_bytes())].into_iter())?;
653 builder.append_data(&mut header, pax_path, &data[..])?;
654 builder.into_inner()?;
655 }
656
657 let mut dest = Vec::new();
658 let src = tokio::io::BufReader::new(tokio::fs::File::open(&src_tar_path).await?);
659 let cap_tmpdir = Dir::open_ambient_dir(&tempd, cap_std::ambient_authority())?;
660 filter_tar_async(
661 src,
662 oci_image::MediaType::ImageLayer,
663 &mut dest,
664 &Default::default(),
665 cap_tmpdir,
666 )
667 .await?;
668
669 let mut found_remapped = false;
673 let mut archive = tar::Archive::new(Cursor::new(dest.as_slice()));
674 for entry in archive.entries()? {
675 let mut entry = entry?;
676 let entry_path = entry.path()?;
677 let entry_path = entry_path.to_string_lossy();
678 let entry_path = entry_path.trim_start_matches("./");
679 if entry_path == format!("usr/{pax_path}") {
680 found_remapped = true;
681 }
682 if let Some(pax) = entry.pax_extensions()? {
683 for ext_res in pax {
684 let ext = ext_res?;
685 if let Ok("path" | "linkpath") = ext.key() {
686 let value = String::from_utf8_lossy(ext.value_bytes());
687 let clean = value.trim_start_matches("./").trim_end_matches('\0');
688 assert!(
689 !clean.starts_with("etc/") && clean != "etc",
690 "PAX header still contains unremapped /etc path: {value}"
691 );
692 }
693 }
694 }
695 }
696 assert!(
697 found_remapped,
698 "Expected remapped file at usr/{pax_path} not found in output"
699 );
700 Ok(())
701 }
702}