vinix-ports / tests / installed-root.v
187 lines · 177 sloc · 5.98 KB · 4cc4c14ba02b15b417509a5527d705f72a033eb2
Raw
1// Check a clean signed vpac client before booting its root on Vinix.
2module main
3
4import os
5
6struct Client {
7 root string
8 arch string
9mut:
10 owners map[string]string
11 files int
12 elfs int
13}
14
15fn 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.
21fn (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
49fn (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
82fn (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
90fn (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
138fn (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
152fn 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
178fn 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}
188