use std::collections::HashMap;
use std::io::{Read, Seek, Write};
use std::os::fd::OwnedFd;
use std::path::{Path, PathBuf};
use rustix::fs::{AtFlags, FileType, Gid, Mode, OFlags, Timespec, Timestamps, Uid};
use crate::acl::Acl;
use crate::host::HostError;
use crate::ondisk::{Inode, Timestamp, Xattr};
use crate::read::{ReadError, Reader};
const LOST_FOUND: &[u8] = b"/lost+found";
const IFMT: u16 = 0o170000;
const IFDIR: u16 = 0o040000;
const IFREG: u16 = 0o100000;
const IFLNK: u16 = 0o120000;
const IFCHR: u16 = 0o020000;
const IFBLK: u16 = 0o060000;
const IFIFO: u16 = 0o010000;
const IFSOCK: u16 = 0o140000;
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 ownership_dropped: bool,
pub xattrs_dropped: bool,
}
pub struct DirectorySink {
root: OwnedFd,
root_path: PathBuf,
skip_privileged: bool,
}
impl DirectorySink {
pub fn new(root: impl AsRef<Path>) -> Result<Self, HostError> {
let root_path = root.as_ref().to_path_buf();
let meta = std::fs::metadata(&root_path).map_err(io_at(&root_path))?;
if !meta.is_dir() {
return Err(HostError::NotADirectory { path: root_path });
}
let mut listing = std::fs::read_dir(&root_path).map_err(io_at(&root_path))?;
if listing.next().is_some() {
return Err(HostError::NotEmpty { path: root_path });
}
let fd = rustix::fs::open(
&root_path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| io_at(&root_path)(e.into()))?;
Ok(Self {
root: fd,
root_path,
skip_privileged: false,
})
}
#[must_use]
pub fn skip_privileged(mut self) -> Self {
self.skip_privileged = true;
self
}
pub fn write_tree<R: Read + Seek>(
self,
reader: &mut Reader<R>,
) -> Result<ExtractReport, HostError> {
let mut state = Extraction {
sink: &self,
report: ExtractReport::default(),
named: HashMap::new(),
open: Vec::new(),
};
let root = reader.inode(ROOT_INO)?;
let xattrs = reader.xattrs(&root)?;
state.set_xattrs_on_fd(&self.root, &xattrs, 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()))?,
inode: root,
});
reader.walk_with(|reader, entry| {
if is_lost_found(&entry.path) {
return Ok(());
}
state.write_entry(reader, entry)
})?;
while let Some(dir) = state.open.pop() {
state.finish_directory(&dir)?;
}
Ok(state.report)
}
}
const ROOT_INO: u32 = 2;
struct OpenDir {
path: Vec<u8>,
fd: OwnedFd,
inode: Inode,
}
struct Extraction<'a> {
sink: &'a DirectorySink,
report: ExtractReport,
named: HashMap<u32, Vec<u8>>,
open: Vec<OpenDir>,
}
impl Extraction<'_> {
fn write_entry<R: Read + Seek>(
&mut self,
reader: &mut Reader<R>,
entry: crate::read::WalkEntry,
) -> 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::HostileName {
path: entry.path.clone(),
});
}
let kind = entry.inode.mode & IFMT;
if kind != IFDIR
&& let Some(first) = self.named.get(&entry.number)
{
let first = first.clone();
self.link(&first, name, &entry.path)?;
self.report.written += 1;
return Ok(());
}
let handle = match kind {
IFDIR => {
self.make_directory(name, &entry)?;
None
}
IFREG => Some(self.write_file(reader, name, &entry)?),
IFLNK => {
let target = reader.read_symlink(&entry.inode)?;
self.make_symlink(&target, name, &entry)?;
None
}
IFCHR | IFBLK => {
let (major, minor) = reader.device(&entry.inode);
let file_type = if kind == IFCHR {
FileType::CharacterDevice
} else {
FileType::BlockDevice
};
if !self.make_node(file_type, rustix::fs::makedev(major, minor), name, &entry)? {
return Ok(());
}
None
}
IFIFO => {
if !self.make_node(FileType::Fifo, 0, name, &entry)? {
return Ok(());
}
None
}
IFSOCK => {
if !self.make_node(FileType::Socket, 0, name, &entry)? {
return Ok(());
}
None
}
_ => {
return Err(HostError::Unsupported {
path: under_root(&self.sink.root_path, &entry.path),
});
}
};
if kind != IFDIR && entry.inode.links_count > 1 {
self.named.insert(entry.number, entry.path.clone());
}
match (kind, &handle) {
(IFDIR, _) => {}
(_, Some(fd)) => {
let xattrs = reader.xattrs(&entry.inode)?;
self.set_xattrs_on_fd(fd, &xattrs, &entry.path)?;
self.finish_fd(fd, &entry.inode, &entry.path)?;
}
(_, None) => {
let xattrs = reader.xattrs(&entry.inode)?;
self.set_xattrs_by_name(name, &xattrs, &entry.path)?;
self.finish_name(name, &entry.inode, &entry.path, kind == IFLNK)?;
}
}
self.report.written += 1;
Ok(())
}
fn dir(&self) -> &OwnedFd {
&self
.open
.last()
.expect("the destination itself is never popped")
.fd
}
fn make_directory(
&mut self,
name: &[u8],
entry: &crate::read::WalkEntry,
) -> Result<(), HostError> {
rustix::fs::mkdirat(self.dir(), name, BUILDING)
.map_err(|e| self.io(&entry.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(&entry.path, e.into()))?;
self.open.push(OpenDir {
path: entry.path.clone(),
fd,
inode: entry.inode.clone(),
});
Ok(())
}
fn finish_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.inode, path)
}
fn write_file<R: Read + Seek>(
&mut self,
reader: &mut Reader<R>,
name: &[u8],
entry: &crate::read::WalkEntry,
) -> Result<OwnedFd, HostError> {
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 mut buf = vec![0u8; WINDOW];
let mut offset = 0u64;
loop {
let filled = reader.read_into(&entry.inode, offset, &mut buf)?;
if filled == 0 {
break;
}
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],
entry: &crate::read::WalkEntry,
) -> Result<(), HostError> {
rustix::fs::symlinkat(target, self.dir(), name).map_err(|e| self.io(&entry.path, e.into()))
}
fn make_node(
&mut self,
file_type: FileType,
dev: rustix::fs::Dev,
name: &[u8],
entry: &crate::read::WalkEntry,
) -> Result<bool, HostError> {
match rustix::fs::mknodat(self.dir(), name, file_type, mode_of(&entry.inode), dev) {
Ok(()) => Ok(true),
Err(e) if forbidden(e) => {
if !self.sink.skip_privileged {
return Err(HostError::Unprivileged {
path: entry.path.clone(),
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.report.skipped.push(entry.path.clone());
Ok(false)
}
Err(e) => Err(self.io(&entry.path, e.into())),
}
}
fn link(&mut self, first: &[u8], name: &[u8], path: &[u8]) -> Result<(), HostError> {
let (first_parent, first_name) = split(first)?;
let parent = self.open_dir(first_parent, path)?;
rustix::fs::linkat(&parent, first_name, self.dir(), name, AtFlags::empty())
.map_err(|e| 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, inode: &Inode, path: &[u8]) -> Result<(), HostError> {
self.chown_fd(fd, inode, path)?;
rustix::fs::fchmod(fd, mode_of(inode)).map_err(|e| self.io(path, e.into()))?;
rustix::fs::futimens(fd, ×_of(inode)).map_err(|e| self.io(path, e.into()))
}
fn finish_name(
&mut self,
name: &[u8],
inode: &Inode,
path: &[u8],
symlink: bool,
) -> Result<(), HostError> {
let flags = if symlink {
AtFlags::SYMLINK_NOFOLLOW
} else {
AtFlags::empty()
};
self.chown_at(name, inode, path, flags)?;
if !symlink {
rustix::fs::chmodat(self.dir(), name, mode_of(inode), AtFlags::empty())
.map_err(|e| self.io(path, e.into()))?;
}
rustix::fs::utimensat(self.dir(), name, ×_of(inode), flags)
.map_err(|e| self.io(path, e.into()))
}
fn chown_at(
&mut self,
name: &[u8],
inode: &Inode,
path: &[u8],
flags: AtFlags,
) -> Result<(), HostError> {
let (uid, gid) = owner_of(inode, 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, inode: &Inode, path: &[u8]) -> Result<(), HostError> {
let (uid, gid) = owner_of(inode, 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 value = xattr_value(xattr, path)?;
let result = rustix::fs::lsetxattr(
&at_dir,
&xattr.name[..],
&value,
rustix::fs::XattrFlags::empty(),
);
self.took_xattr(result, path)?;
}
Ok(())
}
fn set_xattrs_on_fd(
&mut self,
fd: &OwnedFd,
xattrs: &[Xattr],
path: &[u8],
) -> Result<(), HostError> {
for xattr in xattrs {
let value = xattr_value(xattr, path)?;
let result =
rustix::fs::fsetxattr(fd, &xattr.name[..], &value, rustix::fs::XattrFlags::empty());
self.took_xattr(result, path)?;
}
Ok(())
}
fn took_xattr(&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 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 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 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> {
let ok = !name.is_empty()
&& name != b"."
&& name != b".."
&& !name.contains(&b'/')
&& !name.contains(&0);
if ok {
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 path.split(|&b| b == b'/') {
if !component.is_empty() {
out.push(display(component));
}
}
out
}
fn mode_of(inode: &Inode) -> Mode {
Mode::from_bits_retain(u32::from(inode.mode & 0o7777))
}
fn owner_of(inode: &Inode, path: &[u8]) -> Result<(Uid, Gid), HostError> {
if inode.uid == u32::MAX || inode.gid == u32::MAX {
return Err(HostError::UnrepresentableOwner {
path: path.to_vec(),
uid: inode.uid,
gid: inode.gid,
});
}
Ok((Uid::from_raw(inode.uid), Gid::from_raw(inode.gid)))
}
fn times_of(inode: &Inode) -> Timestamps {
Timestamps {
last_access: timespec(inode.atime),
last_modification: timespec(inode.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 _,
}
}
fn xattr_value(xattr: &Xattr, path: &[u8]) -> Result<Vec<u8>, HostError> {
if xattr.name == Acl::ACCESS_NAME || xattr.name == Acl::DEFAULT_NAME {
let acl = Acl::decode(&xattr.value).map_err(|source| HostError::Acl {
path: display(path).to_path_buf(),
source,
})?;
return Ok(acl.encode_xattr_v2());
}
Ok(xattr.value.clone())
}
impl From<ReadError> for HostError {
fn from(source: ReadError) -> Self {
Self::Read { source }
}
}
fn io_at(path: &Path) -> impl Fn(std::io::Error) -> HostError + '_ {
move |source| HostError::Io {
path: path.to_path_buf(),
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"));
}
}