me

/

vinix-ports Public
382 commits 0 issues 0 pull requests 1 contributor Discussions Projects Snippets LOC history Platform CI

Assemble a Vinix desktop initramfs from signed packages

diviocity committed 5 days ago · e827dd6f818964f440f1d24c8e33c30193ff416e View patch
Unverified signature 3 files changed +167 -4
tests/FEED.md +9 -0

@@ -151,6 +151,15 @@ python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" "

151151

152152The tool audits the package-owned root, makes a separate copy, and seeds the verification keyring using the distributor's fingerprint. It copies the local package records into `/var/lib/vpac/local`, checks their versions against the signed database, and installs the repository under `/var/lib/vinix/repository/$arch`. It verifies the database, file index and each included package signature, and compares the payload hashes with the signed database. The sync cache receives independent copies of the database and signature. Assembly exposes the final root only after these checks pass.

153153

154+Pass the native desktop programs and assets in a staging directory with `--native-layer`. Assembly rejects a native file at a package-owned path. `native-layer.tsv` records the installed file hashes and the symbolic link targets. `--initramfs` writes the root-owned USTAR archive for Vinix's ISO builder.

155+

156+```sh

157+python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" \

158+ "$trusted_fingerprint" --native-layer "$desktop_layer" --initramfs

159+```

160+

161+Use `$output/initramfs.tar` as `VINIX_AMD64_INITRAMFS` or `VINIX_AARCH64_INITRAMFS` in Vinix's architecture-specific ISO assembly command. The worker checked that adding a native command and its relative symlink kept the signed package database and executable modes. A second layer attempting to replace the signed `usr/bin/pkg` was rejected before exposing the output root or archive. See `foundation/native-layer-image-01`. Run `python3 tests/native-layer.py` for the five layer regression checks. The worker's receipt SHA-256 is `0d79749f6461ed629c39b929099d89d05d7c9037b7e0ccd4e3c792d6fae5968f`.

162+

154163To publish only the runtime closure selected by the release plan, use `tools/release-feed.py` with the matching `coverage.tsv`. It follows runtime dependency edges and rechecks every planned archive's version and SHA-256 before signing. The worker cold-installed `busybox-init` by name from this feed and audited six packages, 832 owned paths and 15 ELFs. The same test refused an incomplete X11 closure and a changed archive digest before creating either repository. See `foundation/exact-release-feed-proof-01`, receipt SHA-256 `e50d2c759f1e0045a9ef6e42522e02e6de4cfd03d9f51d6b5d6045d23d5cbdc3`.

155164

156165```sh

tests/native-layer.py new file +78 -0

@@ -0,0 +1,78 @@

1+"""Check that a desktop layer adds native files without replacing the vpac root."""

2+import csv

3+import hashlib

4+import io

5+from pathlib import Path

6+import runpy

7+import tempfile

8+import unittest

9+

10+

11+install_native_layer = runpy.run_path(

12+ Path(__file__).resolve().parents[1] / "tools/stage-image.py"

13+)["install_native_layer"]

14+

15+

16+class NativeLayer(unittest.TestCase):

17+ def setUp(self):

18+ work = tempfile.TemporaryDirectory()

19+ self.addCleanup(work.cleanup)

20+ self.root = Path(work.name) / "root"

21+ self.source = Path(work.name) / "native"

22+ for tree in (self.root, self.source):

23+ (tree / "usr/bin").mkdir(parents=True)

24+ self.package_command = self.root / "usr/bin/pkg"

25+ self.package_command.write_bytes(b"package-owned command")

26+

27+ def install(self):

28+ receipt = io.StringIO()

29+ install_native_layer(self.source, self.root, csv.writer(receipt, delimiter="\t"))

30+ receipt.seek(0)

31+ return list(csv.reader(receipt, delimiter="\t"))

32+

33+ def test_preserves_package_files_and_records_added_native_files(self):

34+ program = self.source / "usr/bin/vinix-desktop"

35+ program.write_bytes(b"native desktop")

36+ program.chmod(0o755)

37+ (self.source / "usr/bin/vinix-system-information").symlink_to("vinix-desktop")

38+

39+ rows = self.install()

40+

41+ self.assertEqual(self.package_command.read_bytes(), b"package-owned command")

42+ self.assertEqual((self.root / "usr/bin/vinix-desktop").stat().st_mode & 0o777, 0o755)

43+ self.assertEqual(rows, [

44+ ["usr/bin/vinix-desktop", "file", "0755", hashlib.sha256(b"native desktop").hexdigest()],

45+ ["usr/bin/vinix-system-information", "link", "", "vinix-desktop"],

46+ ])

47+

48+ def test_refuses_to_replace_a_package_owner(self):

49+ (self.source / "usr/bin/pkg").write_bytes(b"shadow")

50+

51+ with self.assertRaisesRegex(ValueError, "shadows a staged path"):

52+ self.install()

53+ self.assertEqual(self.package_command.read_bytes(), b"package-owned command")

54+

55+ def test_refuses_to_traverse_a_destination_symlink(self):

56+ (self.root / "usr/share").symlink_to("bin")

57+ (self.source / "usr/share").mkdir()

58+ (self.source / "usr/share/new-file").touch()

59+

60+ with self.assertRaisesRegex(ValueError, "directory shadows a staged file"):

61+ self.install()

62+ self.assertFalse((self.root / "usr/bin/new-file").exists())

63+

64+ def test_refuses_paths_that_bypass_image_metadata(self):

65+ (self.source / "var/lib/vpac").mkdir(parents=True)

66+ with self.assertRaisesRegex(ValueError, "reserved path"):

67+ self.install()

68+ self.assertFalse((self.root / "var/lib/vpac").exists())

69+

70+ def test_refuses_links_that_leave_the_image(self):

71+ (self.source / "usr/bin/vinix-desktop").symlink_to("../../../outside")

72+ with self.assertRaisesRegex(ValueError, "leaves the guest root"):

73+ self.install()

74+ self.assertFalse((self.root / "usr/bin/vinix-desktop").is_symlink())

75+

76+

77+if __name__ == "__main__":

78+ unittest.main()

tools/stage-image.py +80 -4

@@ -1,12 +1,21 @@

11"""Assemble an offline image root from a signed vpac client and its package repository."""

22import argparse

3+import csv

34import hashlib

5+import os

46import shutil

7+import stat

58import subprocess

69import sys

710import tarfile

811import tempfile

9-from pathlib import Path

12+from pathlib import Path, PurePosixPath

13+

14+

15+NATIVE_RESERVED = tuple(PurePosixPath(path) for path in (

16+ ".vinix-image-id", "etc/alpine-release", "etc/apk", "lib/apk", "sbin/apk",

17+ "usr/bin/apk", "usr/sbin/apk", "etc/vpac.d", "var/lib/vpac", "var/lib/vinix/repository",

18+))

1019

1120

1221def package_fields(data):

@@ -31,6 +40,51 @@ def write_directory(root, relative):

3140 return directory

3241

3342

43+def native_link_target(relative, link):

44+ target = PurePosixPath(link)

45+ depth = 0 if target.is_absolute() else len(relative.parent.parts)

46+ for name in target.parts[1:] if target.is_absolute() else target.parts:

47+ depth += -1 if name == ".." else 1

48+ if depth < 0:

49+ raise ValueError(f"native link leaves the guest root: /{relative}")

50+

51+

52+def install_native_layer(source, root, table):

53+ def walk(directory, destination):

54+ for origin in sorted(directory.iterdir()):

55+ target = destination / origin.name

56+ relative = target.relative_to(root)

57+ if any(relative.is_relative_to(path) for path in NATIVE_RESERVED):

58+ raise ValueError(f"native layer touches a reserved path: /{relative}")

59+ mode = origin.lstat().st_mode

60+ exists = os.path.lexists(target)

61+ if stat.S_ISDIR(mode):

62+ if exists and (target.is_symlink() or not target.is_dir()):

63+ raise ValueError(f"native directory shadows a staged file: /{relative}")

64+ if not exists:

65+ target.mkdir()

66+ walk(origin, target)

67+ if not exists:

68+ shutil.copystat(origin, target)

69+ continue

70+ if exists:

71+ raise ValueError(f"native file shadows a staged path: /{relative}")

72+ if stat.S_ISLNK(mode):

73+ link = os.readlink(origin)

74+ native_link_target(relative, link)

75+ target.symlink_to(link)

76+ table.writerow((relative, "link", "", link))

77+ elif stat.S_ISREG(mode):

78+ shutil.copy2(origin, target)

79+ with target.open("rb") as stream:

80+ digest = hashlib.file_digest(stream, "sha256").hexdigest()

81+ table.writerow((relative, "file", f"{stat.S_IMODE(mode):04o}", digest))

82+ else:

83+ raise ValueError(f"unsupported native file type: /{relative}")

84+

85+ walk(source, root)

86+

87+

3488def snapshot(repo, dest, ring, arch, gpg_home):

3589 def verify(path):

3690 subprocess.run(["gpgv", "--homedir", gpg_home, "--keyring", ring, str(path) + ".sig", path],

@@ -78,13 +132,18 @@ def snapshot(repo, dest, ring, arch, gpg_home):

78132

79133def stage(args):

80134 client, repo, output = args.client.resolve(), args.repo.resolve(), args.output.resolve()

81- if any(output.is_relative_to(path) for path in (client, repo)):

82- raise ValueError("put image output outside the client and repository")

135+ native = args.native_layer.resolve(strict=True) if args.native_layer else None

136+ sources = (client, repo, native) if native else (client, repo)

137+ if any(output.is_relative_to(path) for path in sources):

138+ raise ValueError("put image output outside its input directories")

139+ if native and not native.is_dir():

140+ raise ValueError("expected a staging directory for the native layer")

83141 output.mkdir()

84142 audit = subprocess.check_output([args.auditor, args.arch, client], text=True)

85143 (output / "audited-root.log").write_text(audit)

86144 with tempfile.TemporaryDirectory(dir=output) as directory:

87- root = Path(directory) / "image"

145+ staging = Path(directory)

146+ root = staging / "image"

88147 subprocess.run(["cp", "-a", "--reflink=auto", client / "root", root], check=True)

89148 # This compares the installed certificate with the independently supplied pin.

90149 seed = Path(__file__).resolve().with_name("keyring.py")

@@ -109,6 +168,21 @@ def stage(args):

109168 sync = write_directory(root, "var/lib/vpac/sync")

110169 for name in ("vinix.db", "vinix.db.sig"):

111170 shutil.copy(package_dir / name, sync / name)

171+ if native:

172+ manifest = staging / "native-layer.tsv"

173+ with manifest.open("x", newline="") as records:

174+ table = csv.writer(records, delimiter="\t", lineterminator="\n")

175+ table.writerow(("path", "type", "mode", "sha256_or_link"))

176+ install_native_layer(native, root, table)

177+ if args.initramfs:

178+ if os.path.lexists(root / ".vinix-image-id"):

179+ raise ValueError("the ISO builder assigns the image identity")

180+ image = staging / "initramfs.tar"

181+ subprocess.run(["tar", "--format=ustar", "--sort=name", "--numeric-owner",

182+ "--owner=0", "--group=0", "-cf", image, "-C", root, "."], check=True)

183+ for artifact in ("native-layer.tsv", "initramfs.tar"):

184+ if (staging / artifact).exists():

185+ (staging / artifact).rename(output / artifact)

112186 root.rename(output / "root")

113187 print(audit, end="")

114188 print(f"staged {len(indexed)} signed seed packages and the installed database")

@@ -120,4 +194,6 @@ if __name__ == "__main__":

120194 for arg in ("client", "repo", "output", "auditor"):

121195 parser.add_argument(arg, type=Path)

122196 parser.add_argument("fingerprint", help="trusted distributor fingerprint from outside the feed")

197+ parser.add_argument("--native-layer", type=Path, help="Vinix-built programs and assets to add to the image")

198+ parser.add_argument("--initramfs", action="store_true", help="write a root-owned USTAR archive for the ISO builder")

123199 stage(parser.parse_args())