use std::collections::HashMap;
use std::io::Write;
use std::os::fd::OwnedFd;
use std::path::{Path, PathBuf};
use rustix::fs::{AtFlags, FileType, Gid, Mode, OFlags, Timespec, Timestamps, Uid};
use crate::fidelity::{Direction, FidelityReport, Property, Synthesis};
use crate::host::{HostError, fd_path, io_at};
use crate::path::{canonical_parts, is_hostile_component};
use crate::source::Metadata;
use crate::time::Timestamp;
use crate::tree::{
Attributes, BODY_WINDOW, FsTree, NodeKind, TreeEntry, TreeError, flags_not_carried,
made_by_the_format,
};
use crate::xattr::Xattr;
const NAME: &str = "a name this long";
const NAME_OR_TARGET: &str = "a name or a link target this long";
const BUILDING: Mode = Mode::from_bits_retain(0o700);
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct ExtractReport {
pub written: u64,
pub skipped: Vec<Vec<u8>>,
pub more_skipped: bool,
pub ownership_dropped: bool,
pub dropped_xattrs: Vec<Vec<u8>>,
pub more_dropped_xattrs: bool,
pub times_not_held: Vec<Vec<u8>>,
pub more_times_not_held: bool,
pub fidelity: FidelityReport,
}
pub struct DirectorySink {
root: OwnedFd,
root_path: PathBuf,
skip_privileged: bool,
skip_unsupported: bool,
synthesis: Synthesis,
}
impl DirectorySink {
pub fn new(root: impl AsRef<Path>) -> Result<Self, HostError> {
let root_path = root.as_ref().to_path_buf();
let fd = rustix::fs::open(
&root_path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| match e {
rustix::io::Errno::NOTDIR => HostError::NotADirectory {
path: root_path.clone(),
},
other => io_at(&root_path)(other.into()),
})?;
if !is_empty_dir(&fd).map_err(|e| io_at(&root_path)(e.into()))? {
return Err(HostError::NotEmpty { path: root_path });
}
Ok(Self {
root: fd,
root_path,
skip_privileged: false,
skip_unsupported: false,
synthesis: Synthesis::new(),
})
}
#[must_use]
pub fn synthesis(mut self, synthesis: Synthesis) -> Self {
self.synthesis = synthesis;
self
}
#[must_use]
pub fn skip_privileged(mut self) -> Self {
self.skip_privileged = true;
self
}
#[must_use]
pub fn skip_unsupported(mut self) -> Self {
self.skip_unsupported = true;
self
}
pub fn write_tree<T: FsTree>(self, tree: &mut T) -> Result<ExtractReport, HostError> {
let inherits_acls =
rustix::fs::fgetxattr(&self.root, crate::acl::Acl::DEFAULT_NAME, &mut [0u8; 0][..])
.is_ok();
let mut state = Extraction {
sink: &self,
report: ExtractReport::default(),
named: HashMap::new(),
open: Vec::new(),
held: Vec::new(),
left_out: None,
inherits_acls,
};
let synthesis = self.synthesis;
tree.walk_tree(|tree, entry| {
if made_by_the_format(&entry.path, tree.family()) {
return Ok(());
}
if entry.path.is_empty() {
let attrs = tree.stat(&entry.node, &synthesis)?;
state.note_synthesis(&attrs, b"/");
state.note_flags(tree, &entry.node, b"/")?;
state.open.push(OpenDir {
path: Vec::new(),
fd: rustix::fs::openat(
&self.root,
c".",
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| state.io(b"/", e.into()))?,
meta: attrs.meta,
xattrs: attrs.xattrs,
});
return Ok(());
}
state.write_entry(tree, entry, &synthesis)
})?;
while let Some(dir) = state.open.pop() {
state.finish_directory(dir)?;
}
for dir in std::mem::take(&mut state.held) {
state.apply_directory(&dir)?;
}
Ok(state.report)
}
}
const MAX_DEFERRED_DIRECTORIES: usize = 256;
struct OpenDir {
path: Vec<u8>,
fd: OwnedFd,
meta: Metadata,
xattrs: Vec<Xattr>,
}
impl ExtractReport {
pub const MAX_SKIPPED: usize = 1024;
pub const MAX_DROPPED_XATTRS: usize = 256;
}
struct Extraction<'a> {
sink: &'a DirectorySink,
report: ExtractReport,
named: HashMap<u64, Vec<u8>>,
open: Vec<OpenDir>,
held: Vec<OpenDir>,
left_out: Option<Vec<u8>>,
inherits_acls: bool,
}
impl Extraction<'_> {
fn write_entry<T: FsTree>(
&mut self,
tree: &mut T,
entry: TreeEntry<T::Node>,
synthesis: &Synthesis,
) -> Result<(), HostError> {
if let Some(dir) = &self.left_out {
if entry.path.starts_with(dir) && entry.path.get(dir.len()) == Some(&b'/') {
return Ok(());
}
self.left_out = None;
}
let (parent, name) = split(&entry.path)?;
while self.open.len() > 1 && self.open[self.open.len() - 1].path != parent {
let dir = self.open.pop().expect("the loop checked the length");
self.finish_directory(dir)?;
}
if self.open.last().map(|d| d.path.as_slice()) != Some(parent) {
return Err(HostError::OutOfOrder {
path: entry.path.clone(),
});
}
if let Some(id) = entry.shared
&& let Some(first) = self.named.get(&id)
{
let first = first.clone();
if self.link(&first, name, &entry.path)? {
self.report.written += 1;
}
return Ok(());
}
let attrs = tree.stat(&entry.node, synthesis)?;
self.note_synthesis(&attrs, &entry.path);
self.note_flags(tree, &entry.node, &entry.path)?;
let handle = match entry.kind {
NodeKind::Directory => {
if !self.make_directory(name, &entry.path, &attrs)? {
self.left_out = Some(entry.path.clone());
return Ok(());
}
None
}
NodeKind::File { size } => match self.write_file(tree, name, &entry, size)? {
Some(fd) => Some(fd),
None => return Ok(()),
},
NodeKind::Symlink => {
let target = tree.link_target(&entry.node)?;
if !self.make_symlink(&target, name, &entry.path)? {
return Ok(());
}
None
}
NodeKind::CharDevice { major, minor } | NodeKind::BlockDevice { major, minor } => {
let file_type = if matches!(entry.kind, NodeKind::CharDevice { .. }) {
FileType::CharacterDevice
} else {
FileType::BlockDevice
};
if !self.make_node(
file_type,
rustix::fs::makedev(major, minor),
name,
&entry.path,
&attrs,
)? {
return Ok(());
}
None
}
NodeKind::Fifo => {
if !self.make_node(FileType::Fifo, 0, name, &entry.path, &attrs)? {
return Ok(());
}
None
}
NodeKind::Socket => {
if !self.make_node(FileType::Socket, 0, name, &entry.path, &attrs)? {
return Ok(());
}
None
}
};
if let Some(id) = entry.shared {
self.named.insert(id, entry.path.clone());
}
match (entry.kind, &handle) {
(NodeKind::Directory, _) => {}
(_, Some(fd)) => {
self.finish_fd(fd, &attrs.meta, &attrs.xattrs, &entry.path, false)?;
}
(_, None) => self.finish_name(
name,
&attrs.meta,
&attrs.xattrs,
&entry.path,
matches!(entry.kind, NodeKind::Symlink),
)?,
}
self.report.written += 1;
Ok(())
}
fn note_synthesis(&mut self, attrs: &Attributes, path: &[u8]) {
for property in &attrs.synthesized {
self.report
.fidelity
.record(Direction::Synthesized, path, *property);
}
}
fn note_flags<T: FsTree>(
&mut self,
tree: &mut T,
node: &T::Node,
path: &[u8],
) -> Result<(), HostError> {
let flags = flags_not_carried(tree, node)?;
if flags == 0 {
return Ok(());
}
if !self.sink.skip_unsupported {
return Err(HostError::FlagsNotHeld {
path: path.to_vec(),
flags,
});
}
self.report
.fidelity
.record(Direction::Dropped, path, Property::BsdFlags);
Ok(())
}
fn dir(&self) -> &OwnedFd {
&self
.open
.last()
.expect("the destination itself is never popped")
.fd
}
fn make_directory(
&mut self,
name: &[u8],
path: &[u8],
attrs: &Attributes,
) -> Result<bool, HostError> {
if let Err(e) = rustix::fs::mkdirat(self.dir(), name, BUILDING) {
self.refused_name(e, path, NAME)?;
return Ok(false);
}
let fd = rustix::fs::openat(
self.dir(),
name,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| self.io(path, e.into()))?;
self.open.push(OpenDir {
path: path.to_vec(),
fd,
meta: attrs.meta,
xattrs: attrs.xattrs.clone(),
});
Ok(true)
}
fn finish_directory(&mut self, dir: OpenDir) -> Result<(), HostError> {
if mode_of(&dir.meta).contains(Mode::XUSR) {
return self.apply_directory(&dir);
}
if self.held.len() >= MAX_DEFERRED_DIRECTORIES {
return Err(HostError::TooManyDeferredDirectories {
limit: MAX_DEFERRED_DIRECTORIES,
});
}
self.held.push(dir);
Ok(())
}
fn apply_directory(&mut self, dir: &OpenDir) -> Result<(), HostError> {
let path: &[u8] = if dir.path.is_empty() { b"/" } else { &dir.path };
self.finish_fd(&dir.fd, &dir.meta, &dir.xattrs, path, true)
}
fn write_file<T: FsTree>(
&mut self,
tree: &mut T,
name: &[u8],
entry: &TreeEntry<T::Node>,
size: u64,
) -> Result<Option<OwnedFd>, HostError> {
tree.check_file_size(&entry.path, size)?;
let fd = match rustix::fs::openat(
self.dir(),
name,
OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::from_bits_retain(0o600),
) {
Ok(fd) => fd,
Err(e) => {
self.refused_name(e, &entry.path, NAME)?;
return Ok(None);
}
};
let mut out = std::fs::File::from(fd);
let window = usize::try_from(size).unwrap_or(usize::MAX).min(BODY_WINDOW);
let mut buf = vec![0u8; window];
let mut offset = 0u64;
while offset < size {
let want = usize::try_from(size - offset)
.unwrap_or(usize::MAX)
.min(window);
let filled = tree.read_bytes(&entry.node, offset, &mut buf[..want])?;
if filled == 0 {
return Err(HostError::ShortRead {
path: entry.path.clone(),
size,
got: offset,
});
}
out.write_all(&buf[..filled])
.map_err(|e| self.io(&entry.path, e))?;
offset += filled as u64;
}
Ok(Some(OwnedFd::from(out)))
}
fn make_symlink(&mut self, target: &[u8], name: &[u8], path: &[u8]) -> Result<bool, HostError> {
match rustix::fs::symlinkat(target, self.dir(), name) {
Ok(()) => Ok(true),
Err(e) => {
self.refused_name(e, path, NAME_OR_TARGET)?;
Ok(false)
}
}
}
fn make_node(
&mut self,
file_type: FileType,
dev: rustix::fs::Dev,
name: &[u8],
path: &[u8],
attrs: &Attributes,
) -> Result<bool, HostError> {
match rustix::fs::mknodat(self.dir(), name, file_type, mode_of(&attrs.meta), dev) {
Ok(()) if matches!(file_type, FileType::CharacterDevice | FileType::BlockDevice) => {
self.held_device(name, path, dev)
}
Ok(()) => Ok(true),
Err(e) if forbidden(e) => {
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: path.to_vec(),
what: match file_type {
FileType::CharacterDevice | FileType::BlockDevice => {
"a device node needs CAP_MKNOD to create"
}
_ => "this host refuses to create a node of this kind",
},
});
}
self.note_skipped(path);
Ok(false)
}
Err(e) => {
self.refused_name(e, path, NAME)?;
Ok(false)
}
}
}
fn held_device(
&mut self,
name: &[u8],
path: &[u8],
dev: rustix::fs::Dev,
) -> Result<bool, HostError> {
let made = rustix::fs::statat(self.dir(), name, AtFlags::SYMLINK_NOFOLLOW)
.map_err(|e| self.io(path, e.into()))?
.st_rdev;
let wanted = (rustix::fs::major(dev), rustix::fs::minor(dev));
if (rustix::fs::major(made), rustix::fs::minor(made)) == wanted {
return Ok(true);
}
rustix::fs::unlinkat(self.dir(), name, AtFlags::empty())
.map_err(|e| self.io(path, e.into()))?;
if !self.sink.skip_unsupported {
return Err(HostError::UnrepresentableDevice {
path: path.to_vec(),
major: wanted.0,
minor: wanted.1,
});
}
self.note_skipped(path);
Ok(false)
}
fn link(&mut self, first: &[u8], name: &[u8], path: &[u8]) -> Result<bool, HostError> {
let (first_parent, first_name) = split(first)?;
let parent = self.open_dir(first_parent, path)?;
match rustix::fs::linkat(&parent, first_name, self.dir(), name, AtFlags::empty()) {
Ok(()) => Ok(true),
Err(e) if forbidden(e) => {
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: path.to_vec(),
what: "a second name for a node needs a host that permits a hard link \
to it",
});
}
self.note_skipped(path);
Ok(false)
}
Err(e) => {
self.refused_name(e, path, NAME)?;
Ok(false)
}
}
}
fn open_dir(&self, path: &[u8], blame: &[u8]) -> Result<OwnedFd, HostError> {
let flags = OFlags::PATH | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC;
let mut fd = rustix::fs::openat(&self.sink.root, c".", flags, Mode::empty())
.map_err(|e| self.io(blame, e.into()))?;
for component in path.split(|&b| b == b'/') {
if component.is_empty() {
continue;
}
check_name(component, blame)?;
fd = rustix::fs::openat(&fd, component, flags, Mode::empty())
.map_err(|e| self.io(blame, e.into()))?;
}
Ok(fd)
}
fn finish_fd(
&mut self,
fd: &OwnedFd,
meta: &Metadata,
xattrs: &[Xattr],
path: &[u8],
directory: bool,
) -> Result<(), HostError> {
let owned = self.chown_fd(fd, meta, path)?;
let (acls, others) = split_acls(xattrs);
self.drop_inherited_acls_on_fd(fd, &acls, directory, path)?;
self.set_xattrs_on_fd(fd, &others, path)?;
let mode = self.applied_mode(meta, owned, directory, path);
rustix::fs::fchmod(fd, mode).map_err(|e| self.io(path, e.into()))?;
self.set_xattrs_on_fd(fd, &acls, path)?;
rustix::fs::futimens(fd, ×_of(meta)).map_err(|e| self.io(path, e.into()))?;
let held = rustix::fs::fstat(fd).map_err(|e| self.io(path, e.into()))?;
self.took_times(meta, held_times(&held), path)
}
fn finish_name(
&mut self,
name: &[u8],
meta: &Metadata,
xattrs: &[Xattr],
path: &[u8],
symlink: bool,
) -> Result<(), HostError> {
let flags = AtFlags::SYMLINK_NOFOLLOW;
let owned = self.chown_at(name, meta, path, flags)?;
let (acls, others) = split_acls(xattrs);
if !symlink {
self.drop_inherited_acls_by_name(name, &acls, path)?;
}
self.set_xattrs_by_name(name, &others, path)?;
if !symlink {
let mode = self.applied_mode(meta, owned, false, path);
rustix::fs::chmodat(self.dir(), name, mode, AtFlags::empty())
.map_err(|e| self.io(path, e.into()))?;
}
self.set_xattrs_by_name(name, &acls, path)?;
rustix::fs::utimensat(self.dir(), name, ×_of(meta), flags)
.map_err(|e| self.io(path, e.into()))?;
let held =
rustix::fs::statat(self.dir(), name, flags).map_err(|e| self.io(path, e.into()))?;
self.took_times(meta, held_times(&held), path)
}
fn took_times(
&mut self,
meta: &Metadata,
held: [(i64, u32); 2],
path: &[u8],
) -> Result<(), HostError> {
let recorded = [("access", meta.atime), ("modification", meta.mtime)];
for ((which, recorded), held) in recorded.into_iter().zip(held) {
if instant(recorded) == held {
continue;
}
if !self.sink.skip_unsupported {
return Err(HostError::TimeNotHeld {
path: path.to_vec(),
which,
recorded,
held: Timestamp {
secs: held.0,
nanos: held.1,
},
});
}
self.note_time_not_held(path);
return Ok(());
}
Ok(())
}
fn note_time_not_held(&mut self, path: &[u8]) {
if self.report.times_not_held.len() < ExtractReport::MAX_SKIPPED {
self.report.times_not_held.push(path.to_vec());
} else {
self.report.more_times_not_held = true;
}
}
fn chown_at(
&mut self,
name: &[u8],
meta: &Metadata,
path: &[u8],
flags: AtFlags,
) -> Result<bool, HostError> {
let (uid, gid) = owner_of(meta, path)?;
let result = rustix::fs::chownat(self.dir(), name, Some(uid), Some(gid), flags);
self.took_ownership(result, path)
}
fn chown_fd(&mut self, fd: &OwnedFd, meta: &Metadata, path: &[u8]) -> Result<bool, HostError> {
let (uid, gid) = owner_of(meta, path)?;
let result = rustix::fs::fchown(fd, Some(uid), Some(gid));
self.took_ownership(result, path)
}
fn applied_mode(&mut self, meta: &Metadata, owned: bool, directory: bool, path: &[u8]) -> Mode {
let mode = mode_of(meta);
let set_id = Mode::SUID | Mode::SGID;
if owned || directory || !mode.intersects(set_id) {
return mode;
}
self.report
.fidelity
.record(Direction::Dropped, path, Property::SpecialBits);
mode.difference(set_id)
}
fn drop_inherited_acls_on_fd(
&mut self,
fd: &OwnedFd,
acls: &[&Xattr],
directory: bool,
path: &[u8],
) -> Result<(), HostError> {
if !self.inherits_acls {
return Ok(());
}
for name in inherited(acls, directory) {
let result = rustix::fs::fremovexattr(fd, name);
self.took_removal(result, path)?;
}
Ok(())
}
fn drop_inherited_acls_by_name(
&mut self,
name: &[u8],
acls: &[&Xattr],
path: &[u8],
) -> Result<(), HostError> {
if !self.inherits_acls {
return Ok(());
}
let at_dir = fd_path(self.dir(), name);
for acl in inherited(acls, false) {
let result = rustix::fs::lremovexattr(&at_dir, acl);
self.took_removal(result, path)?;
}
Ok(())
}
fn took_removal(
&mut self,
result: rustix::io::Result<()>,
path: &[u8],
) -> Result<(), HostError> {
match result {
Ok(()) | Err(rustix::io::Errno::NODATA | rustix::io::Errno::OPNOTSUPP) => Ok(()),
Err(e) => Err(self.io(path, e.into())),
}
}
fn took_ownership(
&mut self,
result: rustix::io::Result<()>,
path: &[u8],
) -> Result<bool, HostError> {
let what = match result {
Ok(()) => return Ok(true),
Err(e) if forbidden(e) => "an owner other than this process's needs CAP_CHOWN to set",
Err(rustix::io::Errno::INVAL) => {
"an owner this user namespace maps to no id cannot be set"
}
Err(e) => return Err(self.io(path, e.into())),
};
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: path.to_vec(),
what,
});
}
self.report.ownership_dropped = true;
Ok(false)
}
fn set_xattrs_by_name(
&mut self,
name: &[u8],
xattrs: &[&Xattr],
path: &[u8],
) -> Result<(), HostError> {
if xattrs.is_empty() {
return Ok(());
}
let at_dir = fd_path(self.dir(), name);
for xattr in xattrs {
let result = rustix::fs::lsetxattr(
&at_dir,
&xattr.name[..],
&xattr.value,
rustix::fs::XattrFlags::empty(),
);
self.took_xattr(result, path, &xattr.name, false)?;
}
Ok(())
}
fn set_xattrs_on_fd(
&mut self,
fd: &OwnedFd,
xattrs: &[&Xattr],
path: &[u8],
) -> Result<(), HostError> {
for xattr in xattrs {
let result = rustix::fs::fsetxattr(
fd,
&xattr.name[..],
&xattr.value,
rustix::fs::XattrFlags::empty(),
);
self.took_xattr(result, path, &xattr.name, true)?;
}
Ok(())
}
fn took_xattr(
&mut self,
result: rustix::io::Result<()>,
path: &[u8],
name: &[u8],
opened: bool,
) -> Result<(), HostError> {
let why = match result {
Ok(()) => return Ok(()),
Err(e) if forbidden(e) => {
let by_kind = !opened
&& (name.starts_with(b"user.") || name == crate::acl::Acl::DEFAULT_NAME);
if !by_kind {
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: path.to_vec(),
what: "an extended attribute in the security or trusted namespace \
needs privilege to set",
});
}
self.note_dropped_xattr(name);
return Ok(());
}
"it holds this attribute only on other kinds of node"
}
Err(rustix::io::Errno::OPNOTSUPP) => {
"the destination holds no attribute in this namespace"
}
Err(
rustix::io::Errno::TOOBIG | rustix::io::Errno::NOSPC | rustix::io::Errno::RANGE,
) => "the destination holds no attribute this large",
Err(e) => return Err(self.io(path, e.into())),
};
if !self.sink.skip_unsupported {
return Err(HostError::UnsupportedAttribute {
path: path.to_vec(),
name: name.to_vec(),
why,
});
}
self.note_dropped_xattr(name);
Ok(())
}
fn note_dropped_xattr(&mut self, name: &[u8]) {
let dropped = &mut self.report.dropped_xattrs;
if dropped.iter().any(|seen| seen == name) {
return;
}
if dropped.len() < ExtractReport::MAX_DROPPED_XATTRS {
dropped.push(name.to_vec());
} else {
self.report.more_dropped_xattrs = true;
}
}
fn refused_name(
&mut self,
e: rustix::io::Errno,
path: &[u8],
what: &'static str,
) -> Result<(), HostError> {
if e != rustix::io::Errno::NAMETOOLONG {
return Err(self.io(path, e.into()));
}
if !self.sink.skip_unsupported {
return Err(HostError::NameTooLong {
path: path.to_vec(),
what,
});
}
self.note_skipped(path);
Ok(())
}
fn note_skipped(&mut self, path: &[u8]) {
if self.report.skipped.len() < ExtractReport::MAX_SKIPPED {
self.report.skipped.push(path.to_vec());
} else {
self.report.more_skipped = true;
}
}
fn io(&self, path: &[u8], source: std::io::Error) -> HostError {
HostError::Io {
path: under_root(&self.sink.root_path, path),
source,
}
}
}
fn forbidden(e: rustix::io::Errno) -> bool {
e == rustix::io::Errno::PERM || e == rustix::io::Errno::ACCESS
}
fn is_empty_dir(fd: &OwnedFd) -> rustix::io::Result<bool> {
for entry in rustix::fs::Dir::read_from(fd)? {
let entry = entry?;
let name = entry.file_name().to_bytes();
if name != b"." && name != b".." {
return Ok(false);
}
}
Ok(true)
}
fn split(path: &[u8]) -> Result<(&[u8], &[u8]), HostError> {
let cut = path
.iter()
.rposition(|&b| b == b'/')
.ok_or_else(|| HostError::HostileName {
path: path.to_vec(),
})?;
let (parent, name) = (&path[..cut], &path[cut + 1..]);
check_name(name, path)?;
Ok((parent, name))
}
fn check_name(name: &[u8], path: &[u8]) -> Result<(), HostError> {
if !is_hostile_component(name) {
return Ok(());
}
Err(HostError::HostileName {
path: path.to_vec(),
})
}
fn display(bytes: &[u8]) -> &Path {
use std::os::unix::ffi::OsStrExt;
Path::new(std::ffi::OsStr::from_bytes(bytes))
}
fn under_root(root: &Path, path: &[u8]) -> PathBuf {
let mut out = root.to_path_buf();
for component in canonical_parts(path) {
out.push(display(component));
}
out
}
fn split_acls(xattrs: &[Xattr]) -> (Vec<&Xattr>, Vec<&Xattr>) {
xattrs.iter().partition(|x| {
x.name == crate::acl::Acl::ACCESS_NAME || x.name == crate::acl::Acl::DEFAULT_NAME
})
}
fn inherited<'a>(recorded: &[&Xattr], directory: bool) -> Vec<&'a [u8]> {
let mut names: Vec<&'a [u8]> = vec![crate::acl::Acl::ACCESS_NAME];
if directory {
names.push(crate::acl::Acl::DEFAULT_NAME);
}
names.retain(|name| !recorded.iter().any(|x| x.name == *name));
names
}
fn mode_of(meta: &Metadata) -> Mode {
Mode::from_bits_retain(u32::from(meta.mode & 0o7777))
}
fn owner_of(meta: &Metadata, path: &[u8]) -> Result<(Uid, Gid), HostError> {
if meta.uid == u32::MAX || meta.gid == u32::MAX {
return Err(HostError::UnrepresentableOwner {
path: path.to_vec(),
uid: meta.uid,
gid: meta.gid,
});
}
Ok((Uid::from_raw(meta.uid), Gid::from_raw(meta.gid)))
}
fn times_of(meta: &Metadata) -> Timestamps {
Timestamps {
last_access: timespec(meta.atime),
last_modification: timespec(meta.mtime),
}
}
fn instant(t: Timestamp) -> (i64, u32) {
let carry = i64::from(t.nanos / Timestamp::NANOS_PER_SEC);
(
t.secs.saturating_add(carry),
t.nanos % Timestamp::NANOS_PER_SEC,
)
}
fn timespec(t: Timestamp) -> Timespec {
let (tv_sec, nanos) = instant(t);
Timespec {
tv_sec,
tv_nsec: nanos as _,
}
}
fn held_times(stat: &rustix::fs::Stat) -> [(i64, u32); 2] {
[
(stat.st_atime as _, stat.st_atime_nsec as _),
(stat.st_mtime as _, stat.st_mtime_nsec as _),
]
}
impl From<TreeError> for HostError {
fn from(source: TreeError) -> Self {
Self::Read { source }
}
}
#[cfg(test)]
mod tests {
use super::*;
fn extraction(sink: &DirectorySink) -> Extraction<'_> {
Extraction {
sink,
report: ExtractReport::default(),
named: HashMap::new(),
open: Vec::new(),
held: Vec::new(),
left_out: None,
inherits_acls: false,
}
}
#[test]
fn an_owner_the_user_namespace_does_not_map_is_a_privilege_the_run_lacks() {
let host = tempfile::tempdir().expect("temp dir");
for refusal in [rustix::io::Errno::PERM, rustix::io::Errno::INVAL] {
let strict = DirectorySink::new(host.path()).expect("an empty destination");
let mut run = extraction(&strict);
assert!(
matches!(
run.took_ownership(Err(refusal), b"/file"),
Err(HostError::Unprivileged { .. })
),
"{refusal:?}"
);
let lenient = DirectorySink::new(host.path())
.expect("an empty destination")
.skip_privileged();
let mut run = extraction(&lenient);
run.took_ownership(Err(refusal), b"/file")
.expect("left to the process that wrote it");
assert!(run.report.ownership_dropped, "{refusal:?}");
}
}
#[test]
fn a_node_the_host_made_as_another_device_is_taken_out_again() {
let host = tempfile::tempdir().expect("temp dir");
let wanted = rustix::fs::makedev(255, 16_777_215);
for skip in [false, true] {
let sink = DirectorySink::new(host.path()).expect("an empty destination");
let sink = if skip { sink.skip_unsupported() } else { sink };
let mut run = extraction(&sink);
run.open.push(OpenDir {
path: Vec::new(),
fd: rustix::fs::openat(
&sink.root,
c".",
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
.expect("the destination"),
meta: Metadata::new(0o755, Timestamp::from_secs(0)),
xattrs: Vec::new(),
});
rustix::fs::mknodat(
run.dir(),
"node",
FileType::Fifo,
Mode::from_bits_retain(0o600),
0,
)
.expect("a FIFO");
let held = run.held_device(b"node", b"/node", wanted);
if skip {
assert!(!held.expect("left out on request"));
assert_eq!(run.report.skipped, vec![b"/node".to_vec()]);
} else {
assert!(matches!(
held,
Err(HostError::UnrepresentableDevice {
major: 255,
minor: 16_777_215,
..
})
));
}
assert!(
!host.path().join("node").exists(),
"the node made as another device is not left behind"
);
}
}
#[test]
fn a_time_the_destination_holds_as_another_is_refused_or_named() {
let host = tempfile::tempdir().expect("temp dir");
let recorded = Metadata::new(0o644, Timestamp::from_secs(-2_208_988_805));
let held = [(-2_147_483_648, 0), (-2_208_988_805, 0)];
let sink = DirectorySink::new(host.path()).expect("an empty destination");
let mut run = extraction(&sink);
assert!(matches!(
run.took_times(&recorded, held, b"/old"),
Err(HostError::TimeNotHeld {
which: "access",
..
})
));
let sink = DirectorySink::new(host.path())
.expect("an empty destination")
.skip_unsupported();
let mut run = extraction(&sink);
run.took_times(&recorded, held, b"/old")
.expect("kept as held on request");
assert_eq!(run.report.times_not_held, vec![b"/old".to_vec()]);
let fine = Metadata::new(
0o644,
Timestamp {
secs: 10,
nanos: 123_456_789,
},
);
assert!(
run.took_times(&fine, [(10, 123_456_000); 2], b"/fine")
.is_ok()
);
assert_eq!(run.report.times_not_held.len(), 2);
let carried = Metadata::new(
0o644,
Timestamp {
secs: 10,
nanos: 1_500_000_000,
},
);
run.took_times(&carried, [(11, 500_000_000); 2], b"/carried")
.expect("the same instant");
assert_eq!(run.report.times_not_held.len(), 2);
}
#[test]
fn an_attribute_too_large_for_the_destination_is_the_destinations_refusal() {
use rustix::io::Errno;
let host = tempfile::tempdir().expect("temp dir");
for errno in [Errno::TOOBIG, Errno::NOSPC, Errno::RANGE] {
let sink = DirectorySink::new(host.path()).expect("an empty destination");
let mut run = extraction(&sink);
assert!(
matches!(
run.took_xattr(Err(errno), b"/f", b"user.big", true),
Err(HostError::UnsupportedAttribute { .. })
),
"{errno:?} is refused by name"
);
let sink = DirectorySink::new(host.path())
.expect("an empty destination")
.skip_unsupported();
let mut run = extraction(&sink);
run.took_xattr(Err(errno), b"/f", b"user.big", true)
.expect("left out on request");
assert_eq!(run.report.dropped_xattrs, vec![b"user.big".to_vec()]);
}
let sink = DirectorySink::new(host.path())
.expect("an empty destination")
.skip_unsupported();
let mut run = extraction(&sink);
assert!(matches!(
run.took_xattr(Err(Errno::IO), b"/f", b"user.big", true),
Err(HostError::Io { .. })
));
}
#[test]
fn an_image_path_lands_under_the_destination_rather_than_replacing_it() {
let root = Path::new("/tmp/unpacked");
assert_eq!(
under_root(root, b"/etc/hostname"),
root.join("etc/hostname")
);
assert_eq!(under_root(root, b"/"), root);
assert_eq!(under_root(root, b""), root);
assert_eq!(under_root(root, b"//var//log"), root.join("var/log"));
}
#[test]
fn a_path_splits_into_the_directory_it_is_in_and_the_name_it_holds() {
assert_eq!(
split(b"/etc/hostname").unwrap(),
(&b"/etc"[..], &b"hostname"[..])
);
assert_eq!(split(b"/etc").unwrap(), (&b""[..], &b"etc"[..]));
}
#[test]
fn a_name_that_could_leave_the_destination_is_refused() {
for name in [&b".."[..], b".", b"", b"a/b", b"a\0b"] {
assert!(
matches!(check_name(name, b"/x"), Err(HostError::HostileName { .. })),
"{name:?} must be refused"
);
}
for name in [&b"etc"[..], b"..a", b"a..", b"...", b"\xff\xfe"] {
assert!(check_name(name, b"/x").is_ok(), "{name:?} is a name");
}
}
}