The fix is in main through #398, which makes every hard link through the confined inroot lookup. These tests keep issue 302's case working: - unit: a link to `/bin/tool` is the layer's `bin/tool` (same inode); `/../../etc/hostname` is refused; `/root/etc/hostname`, with `root` a symlink to `/`, follows `root` inside the layer and finds no file; a missing target fails; nothing is linked in any refused case. - e2e: `load-hardlink-absolute` loads the issue's one-layer image and reads `/bin/tool-1.0` with `docker cp`; `load-hardlink-escape` checks that `/../../etc/hostname` is refused. `tests/hardlink-image.py` writes the test image, as in the issue.
23 lines
1.4 KiB
Python
23 lines
1.4 KiB
Python
"""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))
|