Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 59 additions & 5 deletions crates/kiri-init/src/linux/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn untar(reader: impl Read, dest: &Path) -> io::Result<u64> {
if matches!(hidden, b"" | b"." | b"..") {
return Err(escapes());
}
let parent = whiteout_dir(dest, path.parent().unwrap_or(Path::new("")))?;
let parent = layer_dir(dest, path.parent().unwrap_or(Path::new("")))?;
if hidden == b".wh..opq" {
let p = sys::cstr(&parent)?;
// SAFETY: valid NUL-terminated path, name and a 1-byte value.
Expand All @@ -187,21 +187,44 @@ fn untar(reader: impl Read, dest: &Path) -> io::Result<u64> {
}
continue;
}
// A hard link's target is read from the layer's root, so `/usr/bin/perl`
// is the layer's `usr/bin/perl`, as Docker, containerd and Podman read
// it. tar-rs takes the leading `/` as the guest's root and refuses it.
if entry.header().entry_type().is_hard_link()
&& let Some(target) = entry.link_name()?.filter(|t| t.has_root())
{
hard_link_in(dest, &path, &target)?;
continue;
}
total += entry.size();
entry.unpack_in(dest)?;
}
Ok(total)
}

fn escapes() -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, "whiteout escapes the layer")
io::Error::new(io::ErrorKind::InvalidData, "path escapes the layer")
}

/// Makes `path` a hard link to `target`, both from the layer's root. Their
/// directories go through [`layer_dir`], so `..` and a symlink can't take
/// either outside the layer. `linkat` doesn't follow a symlink `target`: the
/// link is to the symlink itself, which is in the layer.
fn hard_link_in(dest: &Path, path: &Path, target: &Path) -> io::Result<()> {
let (path, target) = (path.strip_prefix("/").unwrap_or(path), target.strip_prefix("/").unwrap_or(target));
let (Some(name), Some(to)) = (path.file_name(), target.file_name()) else { return Err(escapes()) };
let src = layer_dir(dest, target.parent().unwrap_or(Path::new("")))?.join(to);
let dst = layer_dir(dest, path.parent().unwrap_or(Path::new("")))?.join(name);
// As tar-rs does with set_overwrite: an earlier entry of that name is replaced.
let _ = fs::remove_file(&dst);
fs::hard_link(&src, &dst).map_err(|e| io::Error::new(e.kind(), format!("hard link {} to /{}: {e}", path.display(), target.display())))
}

/// The directory under `dest` a whiteout lives in, made if missing. Every
/// The directory under `dest` that `parent` names, made if missing. Every
/// step must be a real directory of this layer: an earlier entry could have
/// made one a symlink, which a check of the path's text cannot see and which
/// would carry the whiteout outside the layer.
fn whiteout_dir(dest: &Path, parent: &Path) -> io::Result<PathBuf> {
/// would carry a whiteout or a hard link outside the layer.
fn layer_dir(dest: &Path, parent: &Path) -> io::Result<PathBuf> {
let mut dir = dest.to_path_buf();
for part in parent.components() {
match part {
Expand Down Expand Up @@ -333,6 +356,37 @@ mod tests {
let _ = fs::remove_dir_all(&base);
}

/// A hard link to `/bin/tool` links the layer's `bin/tool`, as with Docker
/// (issue 302). `..`, a symlink in the layer and a missing target are
/// still refused, and nothing outside the layer is linked.
#[test]
fn absolute_hard_link_is_read_from_the_layer_root() {
let layer = |to: &str| {
let mut b = tar::Builder::new(Vec::new());
b.append_data(&mut header(3, 0o755), "bin/tool", &b"hi\n"[..]).unwrap();
let mut h = header(0, 0o777);
h.set_entry_type(tar::EntryType::Symlink);
b.append_link(&mut h, "root", "/").unwrap();
let mut h = header(0, 0o755);
h.set_entry_type(tar::EntryType::Link);
b.append_link(&mut h, "bin/tool-1.0", to).unwrap();
b.into_inner().unwrap()
};
let dest = scratch("abs-link");
untar(Capped { inner: &layer("/bin/tool")[..], left: u64::MAX }, &dest).unwrap();
let (tool, link) = (fs::metadata(dest.join("bin/tool")).unwrap(), fs::metadata(dest.join("bin/tool-1.0")).unwrap());
assert_eq!(std::os::unix::fs::MetadataExt::ino(&link), std::os::unix::fs::MetadataExt::ino(&tool), "not a hard link");
let _ = fs::remove_dir_all(&dest);

for (to, why) in [("/../../etc/hostname", "escapes"), ("/root/etc/hostname", "escapes"), ("/bin/nope", "No such file")] {
let dest = scratch("abs-link-bad");
let err = untar(Capped { inner: &layer(to)[..], left: u64::MAX }, &dest).expect_err(to);
assert!(err.to_string().contains(why), "{to}: unexpected error: {err}");
assert!(!dest.join("bin/tool-1.0").exists(), "{to}: linked");
let _ = fs::remove_dir_all(&dest);
}
}

/// An ordinary whiteout becomes overlayfs's 0:0 character device and
/// leaves no `.wh.` file behind. mknod needs root, as in the guest.
#[test]
Expand Down
8 changes: 7 additions & 1 deletion tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,13 @@ t load "Loaded image: kiri-test/alp:v1" $D load -i "$IMD/a.tar"
t load-runs "3." $D run --rm kiri-test/alp:v1 cat /etc/alpine-release
t load-stdin "Loaded image" bash -c "$D rmi kiri-test/alp:v1 >/dev/null; $D load < $IMD/a.tar"
code load-garbage 1 bash -c "echo nope | $D load"
$D rmi kiri-test/alp:v1 >/dev/null 2>&1; rm -rf "$IMD"
# A hard link's target is read from the layer's root, so a leading / is the
# layer's, as with Docker (issue 302); `..` still can't leave the layer.
python3 "$(dirname "$0")/hardlink-image.py" "$IMD/hl.tar" kiri-test/hl:abs /bin/tool
python3 "$(dirname "$0")/hardlink-image.py" "$IMD/hl-esc.tar" kiri-test/hl:esc /../../etc/hostname
t load-hardlink-absolute "hello from tool" bash -c "$D load -q -i $IMD/hl.tar >/dev/null && id=\$($D create kiri-test/hl:abs) && $D cp \$id:/bin/tool-1.0 - | tar -xO; $D rm \$id >/dev/null"
t load-hardlink-escape "escapes the layer" bash -c "$D load -q -i $IMD/hl-esc.tar >/dev/null && $D create kiri-test/hl:esc"
$D rmi kiri-test/alp:v1 kiri-test/hl:abs kiri-test/hl:esc >/dev/null 2>&1; rm -rf "$IMD"

section "docker build (BuildKit in the VM)"
BD=$(mktemp -d)
Expand Down
22 changes: 22 additions & 0 deletions tests/hardlink-image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Writes a one-layer `docker save` archive whose layer has `bin/tool` and a
hard link `bin/tool-1.0` to LINK, for the e2e checks of issue 302.
Usage: hardlink-image.py OUT.tar TAG LINK"""
import hashlib, io, json, platform, sys, tarfile

out, tag, link = sys.argv[1:4]
layer = io.BytesIO()
with tarfile.open(fileobj=layer, mode="w", format=tarfile.GNU_FORMAT) as t:
d = tarfile.TarInfo("bin"); d.type = tarfile.DIRTYPE; d.mode = 0o755; t.addfile(d)
data = b"#!/bin/sh\necho hello from tool\n"
f = tarfile.TarInfo("bin/tool"); f.size = len(data); f.mode = 0o755; t.addfile(f, io.BytesIO(data))
h = tarfile.TarInfo("bin/tool-1.0"); h.type = tarfile.LNKTYPE; h.linkname = link; h.mode = 0o755; t.addfile(h)
lb = layer.getvalue()
ld = hashlib.sha256(lb).hexdigest()
arch = "arm64" if platform.machine().lower() in ("arm64", "aarch64") else "amd64"
cfg = json.dumps({"architecture": arch, "os": "linux", "config": {"Cmd": ["/bin/tool-1.0"]},
"rootfs": {"type": "layers", "diff_ids": ["sha256:" + ld]}}).encode()
cd = hashlib.sha256(cfg).hexdigest()
manifest = json.dumps([{"Config": f"{cd}.json", "RepoTags": [tag], "Layers": [f"{ld}/layer.tar"]}]).encode()
with tarfile.open(out, mode="w") as t:
for name, b in [(f"{ld}/layer.tar", lb), (f"{cd}.json", cfg), ("manifest.json", manifest)]:
i = tarfile.TarInfo(name); i.size = len(b); t.addfile(i, io.BytesIO(b))