Exercise signed feed installs and the packaged musl loader in Vinix
@@ -0,0 +1,35 @@
1+# Signed package integration test
2+
3+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.
4+
5+```sh
6+work=$(mktemp -d)
7+v -o "$work/feed" tests/feed.v
8+"$work/feed" keys "$work/keys"
9+"$work/feed" publish "$work/keys" "$work/repo" x86_64 \
10+ "$archives"/musl-*.pkg.tar.zst "$archives"/busybox-*.pkg.tar.zst
11+"$work/feed" install "$work/keys" "$work/repo" x86_64 "$work/sdk" musl busybox
12+```
13+
14+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/`.
15+
16+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.
17+
18+```sh
19+"$work/feed" trust "$work/client-keys" "$work/repo/test-signer.asc" "$fingerprint"
20+"$work/feed" install "$work/client-keys" "$work/repo" x86_64 "$work/client" musl busybox
21+```
22+
23+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.
24+
25+```sh
26+tar --format=ustar --owner=0 --group=0 --zstd -cf "$work/sdk.tar.zst" -C "$work/sdk" root db
27+sha256sum "$work/sdk.tar.zst"
28+```
29+
30+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`.
31+
32+```sh
33+v -o "$work/boot" tests/busybox/boot.v
34+"$work/boot" "$work/sdk/root" "$vinix_iso" "$work/guest" --musl
35+```
@@ -1,4 +1,4 @@
1-// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]
1+// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell|--musl]
22// Run on x86_64 Linux with musl-gcc, bsdtar, GNU tar, xorriso and QEMU installed.
33module main
44
@@ -12,14 +12,16 @@ fn run(argv []string) ! {
1212 }
1313}
1414
15-fn boot_image(root string, iso string, output string, shell bool) ! {
15+fn boot_image(root string, iso string, output string, mode string) ! {
16+ shell := mode == '--shell'
17+ musl := mode == '--musl'
1618 if root == '/' || output == root || output.starts_with(root + '/') {
1719 return error('place the output outside a separate BusyBox package root')
1820 }
1921 digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex()
2022 helper := if shell { 'serial-line' } else { 'busybox-probe' }
2123 if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) {
22- return error('use a clean root containing just the installed BusyBox package')
24+ return error('use a clean package root without a test helper or inittab')
2325 }
2426 os.mkdir(output)!
2527 boot := os.join_path(output, 'image/boot')
@@ -33,8 +35,17 @@ fn boot_image(root string, iso string, output string, shell bool) ! {
3335 }
3436 os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')!
3537 source := if shell { 'serial-line.v' } else { 'guest.v' }
36- run([@VEXE, '-prod', '-gc', 'none', '-cc', 'musl-gcc', '-cflags', '-static', '-o',
37- os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])!
38+ mut compiler := [@VEXE, '-prod', '-gc', 'none']
39+ mut loader_digest := ''
40+ if musl {
41+ loader_digest = sha256.sum(os.read_bytes(os.join_path(root, 'lib/ld-musl-x86_64.so.1'))!).hex()
42+ specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs')
43+ compiler << ['-musl', '-d', 'packaged_musl', '-cc', 'gcc', '-cflags',
44+ '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)]
45+ } else {
46+ compiler << ['-cc', 'musl-gcc', '-cflags', '-static']
47+ }
48+ run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])!
3849 run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', 'boot/limine-bios.sys',
3950 'boot/limine-bios-cd.bin'])!
4051 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) ! {
5768 if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines {
5869 return error('guest verification failed; see ${log}')
5970 }
71+ if musl && '${loader_digest} /lib/ld-musl-x86_64.so.1' !in lines {
72+ return error('guest musl differs from the packaged loader; see ${log}')
73+ }
6074 println('Vinix booted the installed BusyBox ${digest}. See ${log}')
6175}
6276
6377fn main() {
64- shell := os.args.len == 5 && os.args[4] == '--shell'
65- if os.args.len != 4 && !shell {
66- eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]')
78+ mode := if os.args.len == 5 { os.args[4] } else { '' }
79+ if os.args.len != 4 && mode !in ['--shell', '--musl'] {
80+ eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell|--musl]')
6781 exit(2)
6882 }
6983 output := os.abs_path(os.args[3])
7084 new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output))
71- boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, shell) or {
85+ boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode) or {
7286 eprintln(err)
7387 exit(1)
7488 }
@@ -6,11 +6,14 @@ import os
66#include <fcntl.h>
77#include <unistd.h>
88#include <sys/reboot.h>
9+#include <malloc.h>
910
1011fn C.open(&char, int, ...) int
1112fn C.dup2(int, int) int
1213fn C.close(int) int
1314fn C.reboot(int) int
15+fn C.malloc_trim(uint) int
16+fn C.malloc_usable_size(voidptr) uint
1417
1518fn run(argv []string) !string {
1619 result := os.exec(argv)
@@ -39,6 +42,29 @@ fn verify() ! {
3942 check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')!
4043 check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')!
4144 println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!)
45+ $if packaged_musl ? {
46+ listing := run(['/lib/ld-musl-x86_64.so.1', '--list', '/busybox-probe'])!
47+ if !listing.contains('libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1') {
48+ return error('unexpected dynamic dependencies: ${listing}')
49+ }
50+ println(listing)
51+ println(run(['/bin/busybox', 'sha256sum', '/lib/ld-musl-x86_64.so.1'])!)
52+ // A freed 256 KiB mapping falls in a retained bucket in the Vinix allocator.
53+ C.malloc_trim(0)
54+ buffer := unsafe { C.malloc(256 * 1024) }
55+ if buffer == unsafe { nil } {
56+ return error('malloc failed to provide 256 KiB')
57+ }
58+ capacity := C.malloc_usable_size(buffer)
59+ unsafe { C.free(buffer) }
60+ if capacity < 256 * 1024 {
61+ return error('malloc supplied a truncated allocation')
62+ }
63+ if C.malloc_trim(0) == 0 {
64+ return error('malloc_trim did not release a mapping after free')
65+ }
66+ println('Packaged musl released a mapping with malloc_trim.')
67+ }
4268}
4369
4470fn main() {
@@ -0,0 +1,175 @@
1+// Build an isolated file repository and install its signed packages in a new SDK.
2+module main
3+
4+import json2
5+import net.urllib
6+import os
7+
8+fn output(argv []string) !string {
9+ result := os.exec(argv)
10+ if result.exit_code != 0 {
11+ return error('${argv[0]} exited ${result.exit_code}: ${result.output}')
12+ }
13+ return result.output
14+}
15+
16+fn gpg(keyring string, args []string) !string {
17+ return output(['gpg', '--homedir', keyring, '--batch', '--no-tty', ...args])
18+}
19+
20+fn trust_key(state string, key string, fingerprint string) ! {
21+ trust := os.join_path(state, 'verify')
22+ os.mkdir(trust, mode: 0o700)!
23+ gpg(trust, ['--import', key])!
24+ records := gpg(trust, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines()
25+ fingerprints := records.filter(it.starts_with('fpr:'))
26+ if records.filter(it.starts_with('pub:')).len != 1 || fingerprints.len == 0
27+ || fingerprints[0].split(':')[9] != fingerprint {
28+ return error('public key does not match the expected test signer fingerprint')
29+ }
30+ ownertrust := os.join_path(trust, 'fixture-ownertrust')
31+ os.write_file(ownertrust, fingerprint + ':6:\n')!
32+ gpg(trust, ['--import-ownertrust', ownertrust])!
33+}
34+
35+// Keep private signing material out of every repository and consumer root.
36+fn keys(state string) ! {
37+ os.mkdir(state, mode: 0o700)!
38+ signer := os.join_path(state, 'signing')
39+ os.mkdir(signer, mode: 0o700)!
40+ gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key',
41+ 'Vinix ports test signer <ports-test@example.invalid>', 'ed25519', 'sign', '0'])!
42+ records := gpg(signer, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines()
43+ fingerprints := records.filter(it.starts_with('fpr:'))
44+ if fingerprints.len != 1 {
45+ return error('expected one signing fingerprint in the new keyring')
46+ }
47+ fingerprint := fingerprints[0].split(':')[9]
48+ os.write_file(os.join_path(state, 'signer.fingerprint'), fingerprint + '\n')!
49+ public_key := os.join_path(state, 'signer.asc')
50+ gpg(signer, ['--output', public_key, '--armor', '--export', fingerprint])!
51+ trust_key(state, public_key, fingerprint)!
52+}
53+
54+fn metadata_value(raw string, key string) !string {
55+ prefix := key + ' = '
56+ values := raw.split_into_lines().filter(it.starts_with(prefix))
57+ if values.len != 1 {
58+ return error('expected one ${key} in .PKGINFO')
59+ }
60+ return values[0][prefix.len..]
61+}
62+
63+fn publish(state string, repo string, arch string, archives []string) ! {
64+ fingerprint := os.read_file(os.join_path(state, 'signer.fingerprint'))!.trim_space()
65+ signer := os.join_path(state, 'signing')
66+ trust := os.join_path(state, 'verify')
67+ os.mkdir(repo)!
68+ stage := os.join_path(repo, '.staging')
69+ os.mkdir(stage)!
70+ mut names := map[string]bool{}
71+ mut staged := []string{}
72+ mut receipts := ['name\tversion\tarch\tarchive\tsha256']
73+ for archive in archives {
74+ name := os.file_name(archive)
75+ target := os.join_path(stage, name)
76+ if !name.ends_with('.pkg.tar.zst') || os.exists(target) {
77+ return error('expected a distinct .pkg.tar.zst archive, got ${archive}')
78+ }
79+ os.cp(archive, target)!
80+ raw := output(['bsdtar', '-xOf', target, '.PKGINFO'])!
81+ id := metadata_value(raw, 'pkgname')!
82+ pkg_arch := metadata_value(raw, 'arch')!
83+ if id in names || pkg_arch !in [arch, 'any'] {
84+ return error('duplicate package name or incompatible architecture: ${name}')
85+ }
86+ names[id] = true
87+ digest := output(['sha256sum', target])!.all_before(' ')
88+ receipts << [id, metadata_value(raw, 'pkgver')!, pkg_arch, name, digest].join('\t')
89+ gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--local-user', fingerprint,
90+ '--output', target + '.sig', '--detach-sign', target])!
91+ gpg(trust, ['--verify', target + '.sig', target])!
92+ staged << target
93+ }
94+ print(output(['env', 'GNUPGHOME=' + signer, 'repo-add', '--nocolor', '--sign', '--include-sigs',
95+ '--key', fingerprint, os.join_path(stage, 'vinix.db.tar.gz'), ...staged])!)
96+ for index in ['vinix.db', 'vinix.files'] {
97+ path := os.join_path(stage, index)
98+ gpg(trust, ['--verify', path + '.sig', path])!
99+ }
100+ os.cp(os.join_path(state, 'signer.asc'), os.join_path(repo, 'test-signer.asc'))!
101+ os.cp(os.join_path(state, 'signer.fingerprint'), os.join_path(repo, 'test-signer.fingerprint'))!
102+ os.write_file(os.join_path(repo, 'packages.tsv'), receipts.join('\n') + '\n')!
103+ os.rename(stage, os.join_path(repo, arch))!
104+}
105+
106+fn install(state string, repo string, arch string, client string, packages []string) ! {
107+ if packages.any(it.len == 0 || it.starts_with('-') || it.contains('/')) {
108+ return error('expected package names from the signed repository')
109+ }
110+ os.mkdir(client)!
111+ for subdir in ['root', 'db/local', 'db/sync', 'cache', 'hooks'] {
112+ os.mkdir_all(os.join_path(client, subdir))!
113+ }
114+ os.write_file(os.join_path(client, 'db/local/ALPM_DB_VERSION'), '9\n')!
115+ config := os.join_path(client, 'config.vile')
116+ url := 'file://' + os.join_path(repo, arch).split('/').map(urllib.path_escape(it)).join('/')
117+ os.write_file(config, [
118+ 'path-root = ' + json2.encode(os.join_path(client, 'root') + '/'),
119+ 'path-db = ' + json2.encode(os.join_path(client, 'db') + '/'),
120+ 'path-cache = [' + json2.encode(os.join_path(client, 'cache') + '/') + ']',
121+ 'path-gpg = ' + json2.encode(os.join_path(state, 'verify') + '/'),
122+ 'path-log = ' + json2.encode(os.join_path(client, 'vpac.log')),
123+ 'path-hook = [' + json2.encode(os.join_path(client, 'hooks') + '/') + ']',
124+ 'pkg-arch = ' + json2.encode(arch),
125+ 'network-siglevel = ["package", "database"]',
126+ 'network-user = ""',
127+ 'network-disable-sandbox = true',
128+ 'repositories = list(sync-repo("vinix", [' + json2.encode(url) + ']))',
129+ ].join('\n') + '\n')!
130+ mut cmd := ['unshare', '-Urn', os.find_abs_path_of_executable('vpac')!, '--config', config,
131+ 'i', '--noconfirm']
132+ cmd << packages
133+ result := os.exec(cmd)
134+ os.write_file(os.join_path(client, 'install.log'), result.output)!
135+ print(result.output)
136+ if result.exit_code != 0 {
137+ return error('vpac install exited ${result.exit_code}')
138+ }
139+}
140+
141+fn command(args []string) ! {
142+ if args.len == 2 && args[0] == 'keys' {
143+ keys(os.abs_path(args[1]))!
144+ return
145+ }
146+ if args.len == 4 && args[0] == 'trust' {
147+ state := os.abs_path(args[1])
148+ os.mkdir(state, mode: 0o700)!
149+ trust_key(state, os.abs_path(args[2]), args[3])!
150+ return
151+ }
152+ if args.len >= 5 && args[3] in ['x86_64', 'aarch64'] {
153+ state, repo := os.abs_path(args[1]), os.abs_path(args[2])
154+ if args[0] == 'publish' {
155+ publish(state, repo, args[3], args[4..])!
156+ return
157+ }
158+ if args[0] == 'install' && args.len >= 6 {
159+ install(state, repo, args[3], os.abs_path(args[4]), args[5..])!
160+ return
161+ }
162+ }
163+ eprintln('usage: feed keys NEW_KEY_STATE')
164+ eprintln(' feed trust NEW_CLIENT_KEY_STATE PUBLIC_KEY EXPECTED_FINGERPRINT')
165+ eprintln(' feed publish KEY_STATE NEW_REPO ARCH PACKAGE_ARCHIVE...')
166+ eprintln(' feed install KEY_STATE REPO ARCH NEW_CLIENT PACKAGE_NAME...')
167+ exit(2)
168+}
169+
170+fn main() {
171+ command(os.args[1..]) or {
172+ eprintln(err)
173+ exit(1)
174+ }
175+}