| 1 | // Check a clean signed vpac client before booting its root on Vinix. |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | import strconv |
| 6 | |
| 7 | struct VersionUse { |
| 8 | consumer string |
| 9 | provider string |
| 10 | version string |
| 11 | } |
| 12 | |
| 13 | struct Client { |
| 14 | root string |
| 15 | arch string |
| 16 | mut: |
| 17 | owners map[string]string |
| 18 | exports map[string][]string |
| 19 | imports []VersionUse |
| 20 | files int |
| 21 | elfs int |
| 22 | } |
| 23 | |
| 24 | fn field(body string, key string) string { |
| 25 | return ('\n' + body).all_after('\n%' + key + '%\n').all_before('\n') |
| 26 | } |
| 27 | |
| 28 | // Resolve every component inside the guest. A host realpath would follow |
| 29 | // guest-absolute links such as /lib/libc.so into the build machine. |
| 30 | fn (client &Client) resolve(path string) !string { |
| 31 | mut rest := path.split('/') |
| 32 | mut parts := []string{} |
| 33 | mut links := 0 |
| 34 | for rest.len != 0 { |
| 35 | name := rest[0] |
| 36 | rest = rest[1..].clone() |
| 37 | if name in ['', '.'] { |
| 38 | continue |
| 39 | } |
| 40 | if name == '..' { |
| 41 | if parts.len != 0 { parts.delete_last() } |
| 42 | continue |
| 43 | } |
| 44 | parts << name |
| 45 | current := client.root + '/' + parts.join('/') |
| 46 | if os.is_link(current) { |
| 47 | links++ |
| 48 | if links > 40 { return error('symlink loop at ${path}') } |
| 49 | target := os.readlink(current)! |
| 50 | parts.delete_last() |
| 51 | if target.starts_with('/') { parts.clear() } |
| 52 | rest.prepend(target.split('/')) |
| 53 | } |
| 54 | } |
| 55 | return parts.join('/') |
| 56 | } |
| 57 | |
| 58 | fn check_empty_file_list(dir string) ! { |
| 59 | // libalpm writes a zero-byte files record for metadata-only packages. |
| 60 | mtree := os.exec(['gzip', '-dc', '--', os.join_path(dir, 'mtree')]) |
| 61 | if mtree.exit_code != 0 || !mtree.output.starts_with('#mtree\n') { |
| 62 | return error('missing or invalid mtree for ${os.base(dir)}') |
| 63 | } |
| 64 | mut has_pkginfo := false |
| 65 | for line in mtree.output.split_into_lines() { |
| 66 | if line == '' || line.starts_with('#') || line.starts_with('/set ') |
| 67 | || line.starts_with('/unset ') { |
| 68 | continue |
| 69 | } |
| 70 | path := line.all_before(' ') |
| 71 | if path !in ['./.PKGINFO', './.BUILDINFO', './.INSTALL', './.CHANGELOG'] { |
| 72 | return error('empty file list for ${os.base(dir)} with payload path ${path}') |
| 73 | } |
| 74 | if path == './.PKGINFO' { |
| 75 | has_pkginfo = true |
| 76 | } |
| 77 | } |
| 78 | if !has_pkginfo { |
| 79 | return error('missing package metadata in mtree for ${os.base(dir)}') |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn (mut client Client) load_owners(database string) !int { |
| 84 | mut packages := 0 |
| 85 | for entry in os.ls(database)! { |
| 86 | if entry == 'ALPM_DB_VERSION' { |
| 87 | continue |
| 88 | } |
| 89 | dir := os.join_path(database, entry) |
| 90 | desc := os.read_file(os.join_path(dir, 'desc'))! |
| 91 | pkg := field(desc, 'NAME') |
| 92 | if pkg == '' || field(desc, 'ARCH') !in ['any', client.arch] |
| 93 | || field(desc, 'VALIDATION') != 'pgp' { |
| 94 | return error('missing name, wrong architecture or missing PGP validation: ${entry}') |
| 95 | } |
| 96 | packages++ |
| 97 | list := os.read_file(os.join_path(dir, 'files'))! |
| 98 | if list == '' { |
| 99 | check_empty_file_list(dir)! |
| 100 | continue |
| 101 | } |
| 102 | if !list.starts_with('%FILES%\n') { return error('missing file list for ${entry}') } |
| 103 | for file in list.all_after('%FILES%\n').all_before('\n\n').split_into_lines() { |
| 104 | if file == '' || file.ends_with('/') { |
| 105 | continue |
| 106 | } |
| 107 | if file.starts_with('/') || file.split('/').any(it in ['', '.', '..']) { |
| 108 | return error('invalid package path in ${entry}: ${file}') |
| 109 | } |
| 110 | if other := client.owners[file] { |
| 111 | return error('${file} claimed by both ${pkg} and ${other}') |
| 112 | } |
| 113 | client.owners[file] = pkg |
| 114 | } |
| 115 | } |
| 116 | if packages == 0 { return error('empty package database') } |
| 117 | return packages |
| 118 | } |
| 119 | |
| 120 | fn (client &Client) library(name string, search []string) string { |
| 121 | for dir in search { |
| 122 | path := client.resolve(dir + '/' + name) or { continue } |
| 123 | if path in client.owners && os.is_file(client.root + '/' + path) { return path } |
| 124 | } |
| 125 | return '' |
| 126 | } |
| 127 | |
| 128 | fn (mut client Client) symbol_versions(path string, providers map[string]string) ! { |
| 129 | // objdump -p prints the version tables without dumping a line per symbol. |
| 130 | result := os.exec(['objdump', '-p', client.root + '/' + path]) |
| 131 | if result.exit_code != 0 { return error('objdump ${path}: ${result.output}') } |
| 132 | mut section := '' |
| 133 | mut provider := '' |
| 134 | for line in result.output.split_into_lines() { |
| 135 | entry := line.trim_space() |
| 136 | if entry in ['Version definitions:', 'Version References:'] { |
| 137 | section = entry |
| 138 | } else if entry == '' { |
| 139 | section = '' |
| 140 | } else if section == 'Version definitions:' { |
| 141 | words := entry.fields() |
| 142 | // A single indented name is an inherited version. |
| 143 | if words.len == 4 { client.exports[path] << words[3] } |
| 144 | } else if section == 'Version References:' { |
| 145 | if entry.starts_with('required from ') { |
| 146 | lib := entry.all_after('required from ').trim_right(':') |
| 147 | provider = providers[lib] or { return error('${path} names unknown version provider ${lib}') } |
| 148 | } else { |
| 149 | words := entry.fields() |
| 150 | if provider == '' || words.len != 4 { return error('unexpected version reference in ${path}: ${entry}') } |
| 151 | // VER_FLG_WEAK lets the requested version be absent. |
| 152 | flags := strconv.parse_uint(words[1], 0, 16)! |
| 153 | if (flags & 2) == 0 { |
| 154 | client.imports << VersionUse{consumer: path, provider: provider, version: words[3]} |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn (mut client Client) inspect(path string, defaults []string) ! { |
| 162 | full := client.root + '/' + path |
| 163 | client.files++ |
| 164 | if os.is_link(full) { return } |
| 165 | if !os.is_file(full) { return error('unexpected file type: ${path}') } |
| 166 | mut fd := os.open(full)! |
| 167 | header := fd.read_bytes(20) |
| 168 | fd.close() |
| 169 | if header.len < 4 || header[..4] != [u8(127), 69, 76, 70] { return } |
| 170 | machine := if client.arch == 'aarch64' { u8(183) } else { u8(62) } |
| 171 | if header.len < 20 || header[4] != 2 || header[5] != 1 || header[18] != machine |
| 172 | || header[19] != 0 { |
| 173 | return error('wrong target ELF machine, class or byte order: ${path}') |
| 174 | } |
| 175 | client.elfs++ |
| 176 | if header[16] == 1 && header[17] == 0 { return } // Relocatable SDK object. |
| 177 | result := os.exec(['readelf', '--wide', '-l', '-d', full]) |
| 178 | if result.exit_code != 0 { return error('readelf ${path}: ${result.output}') } |
| 179 | mut needed := []string{} |
| 180 | mut search := []string{} |
| 181 | page := if client.arch == 'aarch64' { u64(16384) } else { u64(4096) } |
| 182 | mut loads := 0 |
| 183 | for line in result.output.split_into_lines() { |
| 184 | if line.trim_space().starts_with('LOAD ') { |
| 185 | words := line.fields() |
| 186 | if words.len < 8 { return error('malformed LOAD header in ${path}') } |
| 187 | offset := strconv.parse_uint(words[1], 0, 64)! |
| 188 | address := strconv.parse_uint(words[2], 0, 64)! |
| 189 | memsz := strconv.parse_uint(words[5], 0, 64)! |
| 190 | if memsz == 0 { continue } |
| 191 | if address % page != offset % page { |
| 192 | return error('${path}: PT_LOAD offset 0x${offset:x} and virtual address 0x${address:x} disagree on ${page}-byte Vinix pages') |
| 193 | } |
| 194 | loads++ |
| 195 | } else if line.contains('Requesting program interpreter: ') { |
| 196 | interp := line.all_after('Requesting program interpreter: ').all_before(']') |
| 197 | if interp != '/lib/ld-musl-' + client.arch + '.so.1' |
| 198 | || client.library(os.base(interp), ['/lib']) == '' { |
| 199 | return error('wrong or unowned interpreter ${interp} in ${path}') |
| 200 | } |
| 201 | } else if line.contains('(NEEDED)') { |
| 202 | needed << line.all_after('[').all_before(']') |
| 203 | } else if line.contains('(RUNPATH)') || line.contains('(RPATH)') { |
| 204 | for item in line.all_after('[').all_before(']').split(':') { |
| 205 | dir := item.replace(r'${ORIGIN}', '/' + os.dir(path)).replace(r'$ORIGIN', '/' + |
| 206 | os.dir(path)) |
| 207 | if !dir.starts_with('/') || dir.contains('$') { |
| 208 | return error('unsupported library search path ${item} in ${path}') |
| 209 | } |
| 210 | search << dir |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | if header[16] in [u8(2), 3] && loads == 0 { return error('no loadable segments in ${path}') } |
| 215 | search << defaults |
| 216 | mut providers := map[string]string{} |
| 217 | for lib in needed { |
| 218 | provider := client.library(lib, search) |
| 219 | if lib.contains('/') || provider == '' { |
| 220 | return error('${path} (${client.owners[path]}) needs missing or unowned library ${lib}') |
| 221 | } |
| 222 | providers[lib] = provider |
| 223 | } |
| 224 | if result.output.contains('(VERNEED)') || result.output.contains('(VERDEF)') { |
| 225 | client.symbol_versions(path, providers)! |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | fn (mut client Client) walk(dir string, defaults []string, mut seen map[string]bool) ! { |
| 230 | for entry in os.ls(os.join_path(client.root, dir))! { |
| 231 | path := if dir == '' { entry } else { dir + '/' + entry } |
| 232 | full := client.root + '/' + path |
| 233 | if !os.is_link(full) && os.is_dir(full) { |
| 234 | client.walk(path, defaults, mut seen)! |
| 235 | continue |
| 236 | } |
| 237 | if path !in client.owners { return error('unowned file: ${path}') } |
| 238 | seen[path] = true |
| 239 | client.inspect(path, defaults)! |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fn audit(arch string, sdk string) ! { |
| 244 | if arch !in ['x86_64', 'aarch64'] { return error('unknown architecture: ${arch}') } |
| 245 | os.setenv('LC_ALL', 'C', true) |
| 246 | mut client := Client{ |
| 247 | root: os.real_path(os.join_path(sdk, 'root')) |
| 248 | arch: arch |
| 249 | } |
| 250 | packages := client.load_owners(os.join_path(sdk, 'db/local'))! |
| 251 | conf := client.resolve('/etc/ld-musl-' + arch + '.path')! |
| 252 | mut defaults := ['/lib', '/usr/local/lib', '/usr/lib'] |
| 253 | if os.is_file(client.root + '/' + conf) { |
| 254 | defaults = |
| 255 | os.read_file(client.root + '/' + conf)!.replace('\n', ':').split(':').filter(it != '') |
| 256 | if defaults.any(!it.starts_with('/')) { |
| 257 | return error('relative directory in musl library path') |
| 258 | } |
| 259 | } |
| 260 | mut seen := map[string]bool{} |
| 261 | client.walk('', defaults, mut seen)! |
| 262 | for requirement in client.imports { |
| 263 | if requirement.version !in client.exports[requirement.provider] { |
| 264 | return error('${requirement.consumer} requires ABI version ${requirement.version} from ${requirement.provider}') |
| 265 | } |
| 266 | } |
| 267 | for path, pkg in client.owners { |
| 268 | if path !in seen { return error('absent package file: ${pkg} ${path}') } |
| 269 | } |
| 270 | if client.elfs == 0 { return error('root has no target ELF') } |
| 271 | println('${arch}: ${packages} PGP-validated package records, ${client.files} owned files and links, ${client.elfs} target ELFs') |
| 272 | } |
| 273 | |
| 274 | fn main() { |
| 275 | if os.args.len != 3 { |
| 276 | eprintln('usage: installed-root ARCH SDK_DIRECTORY') |
| 277 | exit(2) |
| 278 | } |
| 279 | audit(os.args[1], os.args[2]) or { |
| 280 | eprintln(err) |
| 281 | exit(1) |
| 282 | } |
| 283 | } |
| 284 | |