From e77ed2807b0ab608b7927bae8b244e63582c2082 Mon Sep 17 00:00:00 2001 From: diviocity Date: Sun, 29 Nov 2026 23:17:40 +0100 Subject: [PATCH] Exercise signed feed installs and the packaged musl loader in Vinix --- tests/FEED.md | 35 +++++++++ tests/busybox/boot.v | 32 +++++--- tests/busybox/guest.v | 26 +++++++ tests/feed.v | 175 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 tests/FEED.md create mode 100644 tests/feed.v diff --git a/tests/FEED.md b/tests/FEED.md new file mode 100644 index 0000000..1800ec0 --- /dev/null +++ b/tests/FEED.md @@ -0,0 +1,35 @@ +# Signed package integration test + +Run the fixture on a Linux build machine with V, vpac, GnuPG, bsdtar, repo-add, sha256sum and unprivileged user/network namespaces. Set `archives` to the directory containing the source-built package archives. + +```sh +work=$(mktemp -d) +v -o "$work/feed" tests/feed.v +"$work/feed" keys "$work/keys" +"$work/feed" publish "$work/keys" "$work/repo" x86_64 \ + "$archives"/musl-*.pkg.tar.zst "$archives"/busybox-*.pkg.tar.zst +"$work/feed" install "$work/keys" "$work/repo" x86_64 "$work/sdk" musl busybox +``` + +Each destination must be new. The publisher verifies each package signature, embeds signatures in the signed repository database, verifies both index signatures and records archive SHA-256 values in `repo/packages.tsv`. The installer checks the database and package signatures through vpac while syncing and resolving package names. Its isolated client keeps the root, database, cache, configuration and log under `sdk/`. + +For a separate consumer, transfer the repository directory and obtain the expected fingerprint independently from the test owner. Import the public key into a client-only keyring. + +```sh +"$work/feed" trust "$work/client-keys" "$work/repo/test-signer.asc" "$fingerprint" +"$work/feed" install "$work/client-keys" "$work/repo" x86_64 "$work/client" musl busybox +``` + +For a later SDK, publish the cumulative archive set to a new repository directory and install the desired package names into a new SDK. A remote builder can unpack a relative snapshot and set `VPAC_MAKEPKG_ROOT` and `VPAC_MAKEPKG_DB` to its extracted `root` and `db` directories. + +```sh +tar --format=ustar --owner=0 --group=0 --zstd -cf "$work/sdk.tar.zst" -C "$work/sdk" root db +sha256sum "$work/sdk.tar.zst" +``` + +To boot the packages, pass an existing Vinix amd64 BIOS ISO to the diskless test. The runner uses the ISO's kernel and Limine boot files. Its `--musl` mode links a V probe with the installed musl SDK, checks its loader checksum in Vinix and exercises the Vinix `malloc_trim` implementation. BusyBox supplies PID 1, ash and the applets. The serial transcript is written to `guest/serial.log`. + +```sh +v -o "$work/boot" tests/busybox/boot.v +"$work/boot" "$work/sdk/root" "$vinix_iso" "$work/guest" --musl +``` diff --git a/tests/busybox/boot.v b/tests/busybox/boot.v index 7aba772..c5b32a4 100644 --- a/tests/busybox/boot.v +++ b/tests/busybox/boot.v @@ -1,4 +1,4 @@ -// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell] +// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell|--musl] // Run on x86_64 Linux with musl-gcc, bsdtar, GNU tar, xorriso and QEMU installed. module main @@ -12,14 +12,16 @@ fn run(argv []string) ! { } } -fn boot_image(root string, iso string, output string, shell bool) ! { +fn boot_image(root string, iso string, output string, mode string) ! { + shell := mode == '--shell' + musl := mode == '--musl' if root == '/' || output == root || output.starts_with(root + '/') { return error('place the output outside a separate BusyBox package root') } digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex() helper := if shell { 'serial-line' } else { 'busybox-probe' } if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) { - return error('use a clean root containing just the installed BusyBox package') + return error('use a clean package root without a test helper or inittab') } os.mkdir(output)! boot := os.join_path(output, 'image/boot') @@ -33,8 +35,17 @@ fn boot_image(root string, iso string, output string, shell bool) ! { } os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')! source := if shell { 'serial-line.v' } else { 'guest.v' } - run([@VEXE, '-prod', '-gc', 'none', '-cc', 'musl-gcc', '-cflags', '-static', '-o', - os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])! + mut compiler := [@VEXE, '-prod', '-gc', 'none'] + mut loader_digest := '' + if musl { + loader_digest = sha256.sum(os.read_bytes(os.join_path(root, 'lib/ld-musl-x86_64.so.1'))!).hex() + specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs') + compiler << ['-musl', '-d', 'packaged_musl', '-cc', 'gcc', '-cflags', + '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)] + } else { + compiler << ['-cc', 'musl-gcc', '-cflags', '-static'] + } + run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])! run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', 'boot/limine-bios.sys', 'boot/limine-bios-cd.bin'])! os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\n/BusyBox\n' + @@ -57,18 +68,21 @@ fn boot_image(root string, iso string, output string, shell bool) ! { if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines { return error('guest verification failed; see ${log}') } + if musl && '${loader_digest} /lib/ld-musl-x86_64.so.1' !in lines { + return error('guest musl differs from the packaged loader; see ${log}') + } println('Vinix booted the installed BusyBox ${digest}. See ${log}') } fn main() { - shell := os.args.len == 5 && os.args[4] == '--shell' - if os.args.len != 4 && !shell { - eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]') + mode := if os.args.len == 5 { os.args[4] } else { '' } + if os.args.len != 4 && mode !in ['--shell', '--musl'] { + eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell|--musl]') exit(2) } output := os.abs_path(os.args[3]) new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output)) - boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, shell) or { + boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode) or { eprintln(err) exit(1) } diff --git a/tests/busybox/guest.v b/tests/busybox/guest.v index 11bcd21..9101f22 100644 --- a/tests/busybox/guest.v +++ b/tests/busybox/guest.v @@ -6,11 +6,14 @@ import os #include #include #include +#include fn C.open(&char, int, ...) int fn C.dup2(int, int) int fn C.close(int) int fn C.reboot(int) int +fn C.malloc_trim(uint) int +fn C.malloc_usable_size(voidptr) uint fn run(argv []string) !string { result := os.exec(argv) @@ -39,6 +42,29 @@ fn verify() ! { check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')! check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')! println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!) + $if packaged_musl ? { + listing := run(['/lib/ld-musl-x86_64.so.1', '--list', '/busybox-probe'])! + if !listing.contains('libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1') { + return error('unexpected dynamic dependencies: ${listing}') + } + println(listing) + println(run(['/bin/busybox', 'sha256sum', '/lib/ld-musl-x86_64.so.1'])!) + // A freed 256 KiB mapping falls in a retained bucket in the Vinix allocator. + C.malloc_trim(0) + buffer := unsafe { C.malloc(256 * 1024) } + if buffer == unsafe { nil } { + return error('malloc failed to provide 256 KiB') + } + capacity := C.malloc_usable_size(buffer) + unsafe { C.free(buffer) } + if capacity < 256 * 1024 { + return error('malloc supplied a truncated allocation') + } + if C.malloc_trim(0) == 0 { + return error('malloc_trim did not release a mapping after free') + } + println('Packaged musl released a mapping with malloc_trim.') + } } fn main() { diff --git a/tests/feed.v b/tests/feed.v new file mode 100644 index 0000000..1e9e174 --- /dev/null +++ b/tests/feed.v @@ -0,0 +1,175 @@ +// Build an isolated file repository and install its signed packages in a new SDK. +module main + +import json2 +import net.urllib +import os + +fn output(argv []string) !string { + result := os.exec(argv) + if result.exit_code != 0 { + return error('${argv[0]} exited ${result.exit_code}: ${result.output}') + } + return result.output +} + +fn gpg(keyring string, args []string) !string { + return output(['gpg', '--homedir', keyring, '--batch', '--no-tty', ...args]) +} + +fn trust_key(state string, key string, fingerprint string) ! { + trust := os.join_path(state, 'verify') + os.mkdir(trust, mode: 0o700)! + gpg(trust, ['--import', key])! + records := gpg(trust, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines() + fingerprints := records.filter(it.starts_with('fpr:')) + if records.filter(it.starts_with('pub:')).len != 1 || fingerprints.len == 0 + || fingerprints[0].split(':')[9] != fingerprint { + return error('public key does not match the expected test signer fingerprint') + } + ownertrust := os.join_path(trust, 'fixture-ownertrust') + os.write_file(ownertrust, fingerprint + ':6:\n')! + gpg(trust, ['--import-ownertrust', ownertrust])! +} + +// Keep private signing material out of every repository and consumer root. +fn keys(state string) ! { + os.mkdir(state, mode: 0o700)! + signer := os.join_path(state, 'signing') + os.mkdir(signer, mode: 0o700)! + gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key', + 'Vinix ports test signer ', 'ed25519', 'sign', '0'])! + records := gpg(signer, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines() + fingerprints := records.filter(it.starts_with('fpr:')) + if fingerprints.len != 1 { + return error('expected one signing fingerprint in the new keyring') + } + fingerprint := fingerprints[0].split(':')[9] + os.write_file(os.join_path(state, 'signer.fingerprint'), fingerprint + '\n')! + public_key := os.join_path(state, 'signer.asc') + gpg(signer, ['--output', public_key, '--armor', '--export', fingerprint])! + trust_key(state, public_key, fingerprint)! +} + +fn metadata_value(raw string, key string) !string { + prefix := key + ' = ' + values := raw.split_into_lines().filter(it.starts_with(prefix)) + if values.len != 1 { + return error('expected one ${key} in .PKGINFO') + } + return values[0][prefix.len..] +} + +fn publish(state string, repo string, arch string, archives []string) ! { + fingerprint := os.read_file(os.join_path(state, 'signer.fingerprint'))!.trim_space() + signer := os.join_path(state, 'signing') + trust := os.join_path(state, 'verify') + os.mkdir(repo)! + stage := os.join_path(repo, '.staging') + os.mkdir(stage)! + mut names := map[string]bool{} + mut staged := []string{} + mut receipts := ['name\tversion\tarch\tarchive\tsha256'] + for archive in archives { + name := os.file_name(archive) + target := os.join_path(stage, name) + if !name.ends_with('.pkg.tar.zst') || os.exists(target) { + return error('expected a distinct .pkg.tar.zst archive, got ${archive}') + } + os.cp(archive, target)! + raw := output(['bsdtar', '-xOf', target, '.PKGINFO'])! + id := metadata_value(raw, 'pkgname')! + pkg_arch := metadata_value(raw, 'arch')! + if id in names || pkg_arch !in [arch, 'any'] { + return error('duplicate package name or incompatible architecture: ${name}') + } + names[id] = true + digest := output(['sha256sum', target])!.all_before(' ') + receipts << [id, metadata_value(raw, 'pkgver')!, pkg_arch, name, digest].join('\t') + gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--local-user', fingerprint, + '--output', target + '.sig', '--detach-sign', target])! + gpg(trust, ['--verify', target + '.sig', target])! + staged << target + } + print(output(['env', 'GNUPGHOME=' + signer, 'repo-add', '--nocolor', '--sign', '--include-sigs', + '--key', fingerprint, os.join_path(stage, 'vinix.db.tar.gz'), ...staged])!) + for index in ['vinix.db', 'vinix.files'] { + path := os.join_path(stage, index) + gpg(trust, ['--verify', path + '.sig', path])! + } + os.cp(os.join_path(state, 'signer.asc'), os.join_path(repo, 'test-signer.asc'))! + os.cp(os.join_path(state, 'signer.fingerprint'), os.join_path(repo, 'test-signer.fingerprint'))! + os.write_file(os.join_path(repo, 'packages.tsv'), receipts.join('\n') + '\n')! + os.rename(stage, os.join_path(repo, arch))! +} + +fn install(state string, repo string, arch string, client string, packages []string) ! { + if packages.any(it.len == 0 || it.starts_with('-') || it.contains('/')) { + return error('expected package names from the signed repository') + } + os.mkdir(client)! + for subdir in ['root', 'db/local', 'db/sync', 'cache', 'hooks'] { + os.mkdir_all(os.join_path(client, subdir))! + } + os.write_file(os.join_path(client, 'db/local/ALPM_DB_VERSION'), '9\n')! + config := os.join_path(client, 'config.vile') + url := 'file://' + os.join_path(repo, arch).split('/').map(urllib.path_escape(it)).join('/') + os.write_file(config, [ + 'path-root = ' + json2.encode(os.join_path(client, 'root') + '/'), + 'path-db = ' + json2.encode(os.join_path(client, 'db') + '/'), + 'path-cache = [' + json2.encode(os.join_path(client, 'cache') + '/') + ']', + 'path-gpg = ' + json2.encode(os.join_path(state, 'verify') + '/'), + 'path-log = ' + json2.encode(os.join_path(client, 'vpac.log')), + 'path-hook = [' + json2.encode(os.join_path(client, 'hooks') + '/') + ']', + 'pkg-arch = ' + json2.encode(arch), + 'network-siglevel = ["package", "database"]', + 'network-user = ""', + 'network-disable-sandbox = true', + 'repositories = list(sync-repo("vinix", [' + json2.encode(url) + ']))', + ].join('\n') + '\n')! + mut cmd := ['unshare', '-Urn', os.find_abs_path_of_executable('vpac')!, '--config', config, + 'i', '--noconfirm'] + cmd << packages + result := os.exec(cmd) + os.write_file(os.join_path(client, 'install.log'), result.output)! + print(result.output) + if result.exit_code != 0 { + return error('vpac install exited ${result.exit_code}') + } +} + +fn command(args []string) ! { + if args.len == 2 && args[0] == 'keys' { + keys(os.abs_path(args[1]))! + return + } + if args.len == 4 && args[0] == 'trust' { + state := os.abs_path(args[1]) + os.mkdir(state, mode: 0o700)! + trust_key(state, os.abs_path(args[2]), args[3])! + return + } + if args.len >= 5 && args[3] in ['x86_64', 'aarch64'] { + state, repo := os.abs_path(args[1]), os.abs_path(args[2]) + if args[0] == 'publish' { + publish(state, repo, args[3], args[4..])! + return + } + if args[0] == 'install' && args.len >= 6 { + install(state, repo, args[3], os.abs_path(args[4]), args[5..])! + return + } + } + eprintln('usage: feed keys NEW_KEY_STATE') + eprintln(' feed trust NEW_CLIENT_KEY_STATE PUBLIC_KEY EXPECTED_FINGERPRINT') + eprintln(' feed publish KEY_STATE NEW_REPO ARCH PACKAGE_ARCHIVE...') + eprintln(' feed install KEY_STATE REPO ARCH NEW_CLIENT PACKAGE_NAME...') + exit(2) +} + +fn main() { + command(os.args[1..]) or { + eprintln(err) + exit(1) + } +} -- 2.56.1