Audit file ownership and target ELF dependencies in signed installed roots
@@ -4,6 +4,7 @@ Each port contains a native vpac recipe and its local build inputs. The
44bootstrap packages build Vinix's patched musl and a static BusyBox. BusyBox
55owns the shell, init and applet symlinks. See [the release profiles](PROFILES.md)
66for the Alpine components that need Vinix equivalents on each architecture.
7+The [bootstrap order](BOOTSTRAP.md) lists the toolchain, networking and desktop prerequisites.
78
89To reproduce the v3.21 input inventory for either release, compile the
910inventory 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
2223
2324Use an ordinary user on x86-64 Linux with vpac's native Vile builder. The
2425tested vpac revision is `e7147c7fac0dc130fe03c7accf07c75a88188d3d`.
25-The first stages need GNU Make, GCC, binutils, patch, coreutils, sed, curl and
26+The first stages need GNU Make, GCC, binutils, patch, coreutils, sed, rsync, curl and
2627bsdtar with bzip2 and zstd support. BusyBox's upstream Makefiles use the host
2728shell and its bootstrap configuration uses Linux userspace headers. The test
2829feed utility uses V, GnuPG, repo-add and unprivileged Linux user namespaces.
2930
30-From the root of this checkout, build musl, install it into an empty SDK
31-through vpac and compile BusyBox against that SDK:
31+From the root of this checkout, build musl and the Linux UAPI headers,
32+install both into an empty SDK through vpac and compile BusyBox against it.
3233
3334```sh
3435work=$(mktemp -d)
35-cp -R musl busybox "$work/"
36-export MAKEFLAGS=-j1
36+cp -R musl linux-headers busybox "$work/"
37+export MAKEFLAGS=-j2 CMAKE_BUILD_PARALLEL_LEVEL=2
3738v -o "$work/feed" tests/feed.v
3839v -o "$work/vinix-cc" tools/vinix-cc.v
3940ln -s vinix-cc "$work/vinix-c++"
4041export PATH="$work:$PATH"
4142vpac --native-build "$work/musl/PKGBUILD.vile" "$work/musl.txt"
43+vpac --native-build "$work/linux-headers/PKGBUILD.vile" "$work/headers.txt"
4244"$work/feed" keys "$work/test-keys"
43-"$work/feed" publish "$work/test-keys" "$work/first-repo" x86_64 "$(cat "$work/musl.txt")"
44-"$work/feed" install "$work/test-keys" "$work/first-repo" x86_64 "$work/sdk" musl
45+"$work/feed" publish "$work/test-keys" "$work/first-repo" x86_64 "$(cat "$work/musl.txt")" "$(cat "$work/headers.txt")"
46+"$work/feed" install "$work/test-keys" "$work/first-repo" x86_64 "$work/sdk" musl linux-headers
4547export VPAC_MAKEPKG_ROOT="$work/sdk/root"
4648export VPAC_MAKEPKG_DB="$work/sdk/db"
4749export VINIX_SYSROOT="$VPAC_MAKEPKG_ROOT"
@@ -57,6 +59,8 @@ To install both packages by name into a new root, publish a new test feed:
5759"$work/feed" publish "$work/test-keys" "$work/repo" x86_64 "$(cat "$work/musl.txt")" "$(cat "$work/busybox.txt")"
5860"$work/feed" install "$work/test-keys" "$work/repo" x86_64 "$work/client" musl busybox
5961installed_root="$work/client/root"
62+v -o "$work/installed-root" tests/installed-root.v
63+"$work/installed-root" x86_64 "$work/client"
6064```
6165
6266## Verify on Vinix
@@ -0,0 +1,187 @@
1+// Check a clean signed vpac client before booting its root on Vinix.
2+module main
3+
4+import os
5+
6+struct Client {
7+ root string
8+ arch string
9+mut:
10+ owners map[string]string
11+ files int
12+ elfs int
13+}
14+
15+fn field(body string, key string) string {
16+ return ('\n' + body).all_after('\n%' + key + '%\n').all_before('\n')
17+}
18+
19+// Resolve every component inside the guest. A host realpath would follow
20+// guest-absolute links such as /lib/libc.so into the build machine.
21+fn (client &Client) resolve(path string) !string {
22+ mut rest := path.split('/')
23+ mut parts := []string{}
24+ mut links := 0
25+ for rest.len != 0 {
26+ name := rest[0]
27+ rest = rest[1..].clone()
28+ if name in ['', '.'] {
29+ continue
30+ }
31+ if name == '..' {
32+ if parts.len != 0 { parts.delete_last() }
33+ continue
34+ }
35+ parts << name
36+ current := client.root + '/' + parts.join('/')
37+ if os.is_link(current) {
38+ links++
39+ if links > 40 { return error('symlink loop at ${path}') }
40+ target := os.readlink(current)!
41+ parts.delete_last()
42+ if target.starts_with('/') { parts.clear() }
43+ rest.prepend(target.split('/'))
44+ }
45+ }
46+ return parts.join('/')
47+}
48+
49+fn (mut client Client) load_owners(database string) !int {
50+ mut packages := 0
51+ for entry in os.ls(database)! {
52+ if entry == 'ALPM_DB_VERSION' {
53+ continue
54+ }
55+ dir := os.join_path(database, entry)
56+ desc := os.read_file(os.join_path(dir, 'desc'))!
57+ pkg := field(desc, 'NAME')
58+ if pkg == '' || field(desc, 'ARCH') !in ['any', client.arch]
59+ || field(desc, 'VALIDATION') != 'pgp' {
60+ return error('missing name, wrong architecture or missing PGP validation: ${entry}')
61+ }
62+ packages++
63+ list := os.read_file(os.join_path(dir, 'files'))!
64+ if !list.starts_with('%FILES%\n') { return error('missing file list for ${entry}') }
65+ for file in list.all_after('%FILES%\n').all_before('\n\n').split_into_lines() {
66+ if file == '' || file.ends_with('/') {
67+ continue
68+ }
69+ if file.starts_with('/') || file.split('/').any(it in ['', '.', '..']) {
70+ return error('invalid package path in ${entry}: ${file}')
71+ }
72+ if other := client.owners[file] {
73+ return error('${file} claimed by both ${pkg} and ${other}')
74+ }
75+ client.owners[file] = pkg
76+ }
77+ }
78+ if packages == 0 { return error('empty package database') }
79+ return packages
80+}
81+
82+fn (client &Client) library(name string, search []string) bool {
83+ for dir in search {
84+ path := client.resolve(dir + '/' + name) or { continue }
85+ if path in client.owners && os.is_file(client.root + '/' + path) { return true }
86+ }
87+ return false
88+}
89+
90+fn (mut client Client) inspect(path string, defaults []string) ! {
91+ full := client.root + '/' + path
92+ client.files++
93+ if os.is_link(full) { return }
94+ if !os.is_file(full) { return error('unexpected file type: ${path}') }
95+ mut fd := os.open(full)!
96+ header := fd.read_bytes(20)
97+ fd.close()
98+ if header.len < 4 || header[..4] != [u8(127), 69, 76, 70] { return }
99+ machine := if client.arch == 'aarch64' { u8(183) } else { u8(62) }
100+ if header.len < 20 || header[4] != 2 || header[5] != 1 || header[18] != machine
101+ || header[19] != 0 {
102+ return error('wrong target ELF machine, class or byte order: ${path}')
103+ }
104+ client.elfs++
105+ if header[16] == 1 && header[17] == 0 { return } // Relocatable SDK object.
106+ result := os.exec(['readelf', '--wide', '-l', '-d', full])
107+ if result.exit_code != 0 { return error('readelf ${path}: ${result.output}') }
108+ mut needed := []string{}
109+ mut search := []string{}
110+ for line in result.output.split_into_lines() {
111+ if line.contains('Requesting program interpreter: ') {
112+ interp := line.all_after('Requesting program interpreter: ').all_before(']')
113+ if interp != '/lib/ld-musl-' + client.arch + '.so.1'
114+ || !client.library(os.base(interp), ['/lib']) {
115+ return error('wrong or unowned interpreter ${interp} in ${path}')
116+ }
117+ } else if line.contains('(NEEDED)') {
118+ needed << line.all_after('[').all_before(']')
119+ } else if line.contains('(RUNPATH)') || line.contains('(RPATH)') {
120+ for item in line.all_after('[').all_before(']').split(':') {
121+ dir := item.replace(r'${ORIGIN}', '/' + os.dir(path)).replace(r'$ORIGIN', '/' +
122+ os.dir(path))
123+ if !dir.starts_with('/') || dir.contains('$') {
124+ return error('unsupported library search path ${item} in ${path}')
125+ }
126+ search << dir
127+ }
128+ }
129+ }
130+ search << defaults
131+ for lib in needed {
132+ if lib.contains('/') || !client.library(lib, search) {
133+ return error('${path} (${client.owners[path]}) needs missing or unowned library ${lib}')
134+ }
135+ }
136+}
137+
138+fn (mut client Client) walk(dir string, defaults []string, mut seen map[string]bool) ! {
139+ for entry in os.ls(os.join_path(client.root, dir))! {
140+ path := if dir == '' { entry } else { dir + '/' + entry }
141+ full := client.root + '/' + path
142+ if !os.is_link(full) && os.is_dir(full) {
143+ client.walk(path, defaults, mut seen)!
144+ continue
145+ }
146+ if path !in client.owners { return error('unowned file: ${path}') }
147+ seen[path] = true
148+ client.inspect(path, defaults)!
149+ }
150+}
151+
152+fn audit(arch string, sdk string) ! {
153+ if arch !in ['x86_64', 'aarch64'] { return error('unknown architecture: ${arch}') }
154+ os.setenv('LC_ALL', 'C', true)
155+ mut client := Client{
156+ root: os.real_path(os.join_path(sdk, 'root'))
157+ arch: arch
158+ }
159+ packages := client.load_owners(os.join_path(sdk, 'db/local'))!
160+ conf := client.resolve('/etc/ld-musl-' + arch + '.path')!
161+ mut defaults := ['/lib', '/usr/local/lib', '/usr/lib']
162+ if os.is_file(client.root + '/' + conf) {
163+ defaults =
164+ os.read_file(client.root + '/' + conf)!.replace('\n', ':').split(':').filter(it != '')
165+ if defaults.any(!it.starts_with('/')) {
166+ return error('relative directory in musl library path')
167+ }
168+ }
169+ mut seen := map[string]bool{}
170+ client.walk('', defaults, mut seen)!
171+ for path, pkg in client.owners {
172+ if path !in seen { return error('absent package file: ${pkg} ${path}') }
173+ }
174+ if client.elfs == 0 { return error('root has no target ELF') }
175+ println('${arch}: ${packages} PGP-validated package records, ${client.files} owned files and links, ${client.elfs} target ELFs')
176+}
177+
178+fn main() {
179+ if os.args.len != 3 {
180+ eprintln('usage: installed-root ARCH SDK_DIRECTORY')
181+ exit(2)
182+ }
183+ audit(os.args[1], os.args[2]) or {
184+ eprintln(err)
185+ exit(1)
186+ }
187+}