From 4cc4c14ba02b15b417509a5527d705f72a033eb2 Mon Sep 17 00:00:00 2001 From: diviocity Date: Sun, 29 Nov 2026 23:46:12 +0100 Subject: [PATCH] Audit file ownership and target ELF dependencies in signed installed roots --- README.md | 18 ++-- tests/installed-root.v | 187 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 tests/installed-root.v diff --git a/README.md b/README.md index 2af204c..f2f007c 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Each port contains a native vpac recipe and its local build inputs. The bootstrap packages build Vinix's patched musl and a static BusyBox. BusyBox owns the shell, init and applet symlinks. See [the release profiles](PROFILES.md) for the Alpine components that need Vinix equivalents on each architecture. +The [bootstrap order](BOOTSTRAP.md) lists the toolchain, networking and desktop prerequisites. To reproduce the v3.21 input inventory for either release, compile the inventory tool against the same `alpinecore` module used by the image builder. @@ -22,26 +23,27 @@ the profiles that require it. The pinned older graphics stack is listed in Use an ordinary user on x86-64 Linux with vpac's native Vile builder. The tested vpac revision is `e7147c7fac0dc130fe03c7accf07c75a88188d3d`. -The first stages need GNU Make, GCC, binutils, patch, coreutils, sed, curl and +The first stages need GNU Make, GCC, binutils, patch, coreutils, sed, rsync, curl and bsdtar with bzip2 and zstd support. BusyBox's upstream Makefiles use the host shell and its bootstrap configuration uses Linux userspace headers. The test feed utility uses V, GnuPG, repo-add and unprivileged Linux user namespaces. -From the root of this checkout, build musl, install it into an empty SDK -through vpac and compile BusyBox against that SDK: +From the root of this checkout, build musl and the Linux UAPI headers, +install both into an empty SDK through vpac and compile BusyBox against it. ```sh work=$(mktemp -d) -cp -R musl busybox "$work/" -export MAKEFLAGS=-j1 +cp -R musl linux-headers busybox "$work/" +export MAKEFLAGS=-j2 CMAKE_BUILD_PARALLEL_LEVEL=2 v -o "$work/feed" tests/feed.v v -o "$work/vinix-cc" tools/vinix-cc.v ln -s vinix-cc "$work/vinix-c++" export PATH="$work:$PATH" vpac --native-build "$work/musl/PKGBUILD.vile" "$work/musl.txt" +vpac --native-build "$work/linux-headers/PKGBUILD.vile" "$work/headers.txt" "$work/feed" keys "$work/test-keys" -"$work/feed" publish "$work/test-keys" "$work/first-repo" x86_64 "$(cat "$work/musl.txt")" -"$work/feed" install "$work/test-keys" "$work/first-repo" x86_64 "$work/sdk" musl +"$work/feed" publish "$work/test-keys" "$work/first-repo" x86_64 "$(cat "$work/musl.txt")" "$(cat "$work/headers.txt")" +"$work/feed" install "$work/test-keys" "$work/first-repo" x86_64 "$work/sdk" musl linux-headers export VPAC_MAKEPKG_ROOT="$work/sdk/root" export VPAC_MAKEPKG_DB="$work/sdk/db" export VINIX_SYSROOT="$VPAC_MAKEPKG_ROOT" @@ -57,6 +59,8 @@ To install both packages by name into a new root, publish a new test feed: "$work/feed" publish "$work/test-keys" "$work/repo" x86_64 "$(cat "$work/musl.txt")" "$(cat "$work/busybox.txt")" "$work/feed" install "$work/test-keys" "$work/repo" x86_64 "$work/client" musl busybox installed_root="$work/client/root" +v -o "$work/installed-root" tests/installed-root.v +"$work/installed-root" x86_64 "$work/client" ``` ## Verify on Vinix diff --git a/tests/installed-root.v b/tests/installed-root.v new file mode 100644 index 0000000..3c112d3 --- /dev/null +++ b/tests/installed-root.v @@ -0,0 +1,187 @@ +// Check a clean signed vpac client before booting its root on Vinix. +module main + +import os + +struct Client { + root string + arch string +mut: + owners map[string]string + files int + elfs int +} + +fn field(body string, key string) string { + return ('\n' + body).all_after('\n%' + key + '%\n').all_before('\n') +} + +// Resolve every component inside the guest. A host realpath would follow +// guest-absolute links such as /lib/libc.so into the build machine. +fn (client &Client) resolve(path string) !string { + mut rest := path.split('/') + mut parts := []string{} + mut links := 0 + for rest.len != 0 { + name := rest[0] + rest = rest[1..].clone() + if name in ['', '.'] { + continue + } + if name == '..' { + if parts.len != 0 { parts.delete_last() } + continue + } + parts << name + current := client.root + '/' + parts.join('/') + if os.is_link(current) { + links++ + if links > 40 { return error('symlink loop at ${path}') } + target := os.readlink(current)! + parts.delete_last() + if target.starts_with('/') { parts.clear() } + rest.prepend(target.split('/')) + } + } + return parts.join('/') +} + +fn (mut client Client) load_owners(database string) !int { + mut packages := 0 + for entry in os.ls(database)! { + if entry == 'ALPM_DB_VERSION' { + continue + } + dir := os.join_path(database, entry) + desc := os.read_file(os.join_path(dir, 'desc'))! + pkg := field(desc, 'NAME') + if pkg == '' || field(desc, 'ARCH') !in ['any', client.arch] + || field(desc, 'VALIDATION') != 'pgp' { + return error('missing name, wrong architecture or missing PGP validation: ${entry}') + } + packages++ + list := os.read_file(os.join_path(dir, 'files'))! + if !list.starts_with('%FILES%\n') { return error('missing file list for ${entry}') } + for file in list.all_after('%FILES%\n').all_before('\n\n').split_into_lines() { + if file == '' || file.ends_with('/') { + continue + } + if file.starts_with('/') || file.split('/').any(it in ['', '.', '..']) { + return error('invalid package path in ${entry}: ${file}') + } + if other := client.owners[file] { + return error('${file} claimed by both ${pkg} and ${other}') + } + client.owners[file] = pkg + } + } + if packages == 0 { return error('empty package database') } + return packages +} + +fn (client &Client) library(name string, search []string) bool { + for dir in search { + path := client.resolve(dir + '/' + name) or { continue } + if path in client.owners && os.is_file(client.root + '/' + path) { return true } + } + return false +} + +fn (mut client Client) inspect(path string, defaults []string) ! { + full := client.root + '/' + path + client.files++ + if os.is_link(full) { return } + if !os.is_file(full) { return error('unexpected file type: ${path}') } + mut fd := os.open(full)! + header := fd.read_bytes(20) + fd.close() + if header.len < 4 || header[..4] != [u8(127), 69, 76, 70] { return } + machine := if client.arch == 'aarch64' { u8(183) } else { u8(62) } + if header.len < 20 || header[4] != 2 || header[5] != 1 || header[18] != machine + || header[19] != 0 { + return error('wrong target ELF machine, class or byte order: ${path}') + } + client.elfs++ + if header[16] == 1 && header[17] == 0 { return } // Relocatable SDK object. + result := os.exec(['readelf', '--wide', '-l', '-d', full]) + if result.exit_code != 0 { return error('readelf ${path}: ${result.output}') } + mut needed := []string{} + mut search := []string{} + for line in result.output.split_into_lines() { + if line.contains('Requesting program interpreter: ') { + interp := line.all_after('Requesting program interpreter: ').all_before(']') + if interp != '/lib/ld-musl-' + client.arch + '.so.1' + || !client.library(os.base(interp), ['/lib']) { + return error('wrong or unowned interpreter ${interp} in ${path}') + } + } else if line.contains('(NEEDED)') { + needed << line.all_after('[').all_before(']') + } else if line.contains('(RUNPATH)') || line.contains('(RPATH)') { + for item in line.all_after('[').all_before(']').split(':') { + dir := item.replace(r'${ORIGIN}', '/' + os.dir(path)).replace(r'$ORIGIN', '/' + + os.dir(path)) + if !dir.starts_with('/') || dir.contains('$') { + return error('unsupported library search path ${item} in ${path}') + } + search << dir + } + } + } + search << defaults + for lib in needed { + if lib.contains('/') || !client.library(lib, search) { + return error('${path} (${client.owners[path]}) needs missing or unowned library ${lib}') + } + } +} + +fn (mut client Client) walk(dir string, defaults []string, mut seen map[string]bool) ! { + for entry in os.ls(os.join_path(client.root, dir))! { + path := if dir == '' { entry } else { dir + '/' + entry } + full := client.root + '/' + path + if !os.is_link(full) && os.is_dir(full) { + client.walk(path, defaults, mut seen)! + continue + } + if path !in client.owners { return error('unowned file: ${path}') } + seen[path] = true + client.inspect(path, defaults)! + } +} + +fn audit(arch string, sdk string) ! { + if arch !in ['x86_64', 'aarch64'] { return error('unknown architecture: ${arch}') } + os.setenv('LC_ALL', 'C', true) + mut client := Client{ + root: os.real_path(os.join_path(sdk, 'root')) + arch: arch + } + packages := client.load_owners(os.join_path(sdk, 'db/local'))! + conf := client.resolve('/etc/ld-musl-' + arch + '.path')! + mut defaults := ['/lib', '/usr/local/lib', '/usr/lib'] + if os.is_file(client.root + '/' + conf) { + defaults = + os.read_file(client.root + '/' + conf)!.replace('\n', ':').split(':').filter(it != '') + if defaults.any(!it.starts_with('/')) { + return error('relative directory in musl library path') + } + } + mut seen := map[string]bool{} + client.walk('', defaults, mut seen)! + for path, pkg in client.owners { + if path !in seen { return error('absent package file: ${pkg} ${path}') } + } + if client.elfs == 0 { return error('root has no target ELF') } + println('${arch}: ${packages} PGP-validated package records, ${client.files} owned files and links, ${client.elfs} target ELFs') +} + +fn main() { + if os.args.len != 3 { + eprintln('usage: installed-root ARCH SDK_DIRECTORY') + exit(2) + } + audit(os.args[1], os.args[2]) or { + eprintln(err) + exit(1) + } +} -- 2.56.1