// Check a clean signed vpac client before booting its root on Vinix. module main import os import strconv struct VersionUse { consumer string provider string version string } struct Client { root string arch string mut: owners map[string]string exports map[string][]string imports []VersionUse 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) string { for dir in search { path := client.resolve(dir + '/' + name) or { continue } if path in client.owners && os.is_file(client.root + '/' + path) { return path } } return '' } fn (mut client Client) symbol_versions(path string, providers map[string]string) ! { // objdump -p prints the version tables without dumping a line per symbol. result := os.exec(['objdump', '-p', client.root + '/' + path]) if result.exit_code != 0 { return error('objdump ${path}: ${result.output}') } mut section := '' mut provider := '' for line in result.output.split_into_lines() { entry := line.trim_space() if entry in ['Version definitions:', 'Version References:'] { section = entry } else if entry == '' { section = '' } else if section == 'Version definitions:' { words := entry.fields() // A single indented name is an inherited version. if words.len == 4 { client.exports[path] << words[3] } } else if section == 'Version References:' { if entry.starts_with('required from ') { lib := entry.all_after('required from ').trim_right(':') provider = providers[lib] or { return error('${path} names unknown version provider ${lib}') } } else { words := entry.fields() if provider == '' || words.len != 4 { return error('unexpected version reference in ${path}: ${entry}') } // VER_FLG_WEAK lets the requested version be absent. flags := strconv.parse_uint(words[1], 0, 16)! if (flags & 2) == 0 { client.imports << VersionUse{consumer: path, provider: provider, version: words[3]} } } } } } 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{} page := if client.arch == 'aarch64' { u64(16384) } else { u64(4096) } mut loads := 0 for line in result.output.split_into_lines() { if line.trim_space().starts_with('LOAD ') { words := line.fields() if words.len < 8 { return error('malformed LOAD header in ${path}') } offset := strconv.parse_uint(words[1], 0, 64)! address := strconv.parse_uint(words[2], 0, 64)! memsz := strconv.parse_uint(words[5], 0, 64)! if memsz == 0 { continue } if address % page != offset % page { return error('${path}: PT_LOAD offset 0x${offset:x} and virtual address 0x${address:x} disagree on ${page}-byte Vinix pages') } loads++ } else 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 } } } if header[16] in [u8(2), 3] && loads == 0 { return error('no loadable segments in ${path}') } search << defaults mut providers := map[string]string{} for lib in needed { provider := client.library(lib, search) if lib.contains('/') || provider == '' { return error('${path} (${client.owners[path]}) needs missing or unowned library ${lib}') } providers[lib] = provider } if result.output.contains('(VERNEED)') || result.output.contains('(VERDEF)') { client.symbol_versions(path, providers)! } } 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 requirement in client.imports { if requirement.version !in client.exports[requirement.provider] { return error('${requirement.consumer} requires ABI version ${requirement.version} from ${requirement.provider}') } } 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) } }