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, Synthesis};
use crate::host::{HostError, io_at};
use crate::path::{canonical_parts, is_hostile_component};
use crate::source::Metadata;
use crate::time::Timestamp;
use crate::tree::{Attributes, FsTree, NodeKind, TreeEntry, TreeError};
use crate::xattr::Xattr;
const LOST_FOUND: &[u8] = b"/lost+found";
const BUILDING: Mode = Mode::from_bits_retain(0o700);
const WINDOW: usize = 1 << 20;
#[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 xattrs_dropped: bool,
pub fidelity: FidelityReport,
}
pub struct DirectorySink {
root: OwnedFd,
root_path: PathBuf,
skip_privileged: 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,
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
}
pub fn write_tree<T: FsTree>(self, tree: &mut T) -> Result<ExtractReport, HostError> {
let mut state = Extraction {
sink: &self,
report: ExtractReport::default(),
named: HashMap::new(),
open: Vec::new(),
held: Vec::new(),
};
let synthesis = self.synthesis;
tree.walk_tree(|tree, entry| {
if is_lost_found(&entry.path) {
return Ok(());
}
if entry.path.is_empty() {
let attrs = tree.stat(&entry.node, &synthesis)?;
state.note_synthesis(&attrs, 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;
}
struct Extraction<'a> {
sink: &'a DirectorySink,
report: ExtractReport,
named: HashMap<u64, Vec<u8>>,
open: Vec<OpenDir>,
held: Vec<OpenDir>,
}
impl Extraction<'_> {
fn write_entry<T: FsTree>(
&mut self,
tree: &mut T,
entry: TreeEntry<T::Node>,
synthesis: &Synthesis,
) -> Result<(), HostError> {
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);
let handle = match entry.kind {
NodeKind::Directory => {
self.make_directory(name, &entry.path, &attrs)?;
None
}
NodeKind::File { size } => Some(self.write_file(tree, name, &entry, size)?),
NodeKind::Symlink => {
let target = tree.link_target(&entry.node)?;
self.make_symlink(&target, name, &entry.path)?;
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)?,
(_, 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 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<(), HostError> {
rustix::fs::mkdirat(self.dir(), name, BUILDING).map_err(|e| self.io(path, e.into()))?;
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(())
}
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)
}
fn write_file<T: FsTree>(
&mut self,
tree: &mut T,
name: &[u8],
entry: &TreeEntry<T::Node>,
size: u64,
) -> Result<OwnedFd, HostError> {
tree.check_file_size(&entry.path, size)?;
let fd = rustix::fs::openat(
self.dir(),
name,
OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::from_bits_retain(0o600),
)
.map_err(|e| self.io(&entry.path, e.into()))?;
let mut out = std::fs::File::from(fd);
let window = usize::try_from(size).unwrap_or(usize::MAX).min(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(OwnedFd::from(out))
}
fn make_symlink(&mut self, target: &[u8], name: &[u8], path: &[u8]) -> Result<(), HostError> {
rustix::fs::symlinkat(target, self.dir(), name).map_err(|e| self.io(path, e.into()))
}
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(()) => 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) => Err(self.io(path, e.into())),
}
}
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) => Err(self.io(path, e.into())),
}
}
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],
) -> Result<(), HostError> {
self.chown_fd(fd, meta, path)?;
rustix::fs::fchmod(fd, mode_of(meta)).map_err(|e| self.io(path, e.into()))?;
self.set_xattrs_on_fd(fd, xattrs, path)?;
rustix::fs::futimens(fd, ×_of(meta)).map_err(|e| self.io(path, e.into()))
}
fn finish_name(
&mut self,
name: &[u8],
meta: &Metadata,
xattrs: &[Xattr],
path: &[u8],
symlink: bool,
) -> Result<(), HostError> {
let flags = if symlink {
AtFlags::SYMLINK_NOFOLLOW
} else {
AtFlags::empty()
};
self.chown_at(name, meta, path, flags)?;
if !symlink {
rustix::fs::chmodat(self.dir(), name, mode_of(meta), AtFlags::empty())
.map_err(|e| self.io(path, e.into()))?;
}
self.set_xattrs_by_name(name, xattrs, path)?;
rustix::fs::utimensat(self.dir(), name, ×_of(meta), flags)
.map_err(|e| self.io(path, e.into()))
}
fn chown_at(
&mut self,
name: &[u8],
meta: &Metadata,
path: &[u8],
flags: AtFlags,
) -> Result<(), 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<(), HostError> {
let (uid, gid) = owner_of(meta, path)?;
let result = rustix::fs::fchown(fd, Some(uid), Some(gid));
self.took_ownership(result, path)
}
fn took_ownership(
&mut self,
result: rustix::io::Result<()>,
path: &[u8],
) -> Result<(), HostError> {
match result {
Ok(()) => Ok(()),
Err(e) if forbidden(e) => {
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: path.to_vec(),
what: "an owner other than this process's needs CAP_CHOWN to set",
});
}
self.report.ownership_dropped = true;
Ok(())
}
Err(e) => Err(self.io(path, e.into())),
}
}
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> {
match result {
Ok(()) => Ok(()),
Err(e) if forbidden(e) => {
let by_kind = !opened
&& (name.starts_with(b"user.") || name == crate::acl::Acl::DEFAULT_NAME);
if !self.sink.skip_privileged {
return Err(if by_kind {
HostError::UnsupportedAttribute {
path: path.to_vec(),
name: name.to_vec(),
}
} else {
HostError::Unprivileged {
path: path.to_vec(),
what: "an extended attribute in the security or trusted namespace \
needs privilege to set",
}
});
}
self.report.xattrs_dropped = true;
Ok(())
}
Err(e) => Err(self.io(path, e.into())),
}
}
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 is_lost_found(path: &[u8]) -> bool {
path == LOST_FOUND || path.starts_with(b"/lost+found/")
}
fn fd_path(dir: &OwnedFd, name: &[u8]) -> PathBuf {
use std::os::fd::AsRawFd;
let mut path = PathBuf::from(format!("/proc/self/fd/{}", dir.as_raw_fd()));
path.push(display(name));
path
}
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 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 timespec(t: Timestamp) -> Timespec {
let carry = i64::from(t.nanos / Timestamp::NANOS_PER_SEC);
let nanos = t.nanos % Timestamp::NANOS_PER_SEC;
Timespec {
tv_sec: t.secs.saturating_add(carry),
tv_nsec: nanos as _,
}
}
impl From<TreeError> for HostError {
fn from(source: TreeError) -> Self {
Self::Read { source }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[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");
}
}
#[test]
fn lost_and_found_is_recognized_with_its_contents() {
assert!(is_lost_found(b"/lost+found"));
assert!(is_lost_found(b"/lost+found/17"));
assert!(!is_lost_found(b"/lost+found-old"));
assert!(!is_lost_found(b"/etc/lost+found"));
}
}