nspkg / engine / sandbox_stage.v
200 lines · 186 sloc · 6.79 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// staging follows pacman dload.c; fresh inodes keep worker descriptors out of the cache.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7fn C.mkdtemp(&char) &char
8fn C.fchown(i32, u32, u32) i32
9fn C.fchmod(i32, u32) i32
10fn C.umask(u32) u32
11fn C.unlinkat(i32, &char, i32) i32
12fn C.v_alpm_reflink(i32, i32) i32
13fn C.v_alpm_copy_file_times(i32, i32) i32
14
15struct SandboxStage {
16 directory string
17 parent string
18 uid u32
19 gid u32
20 owner_uid u32
21 owner_gid u32
22 mode u32
23mut:
24 fd i32 = -1
25 sealed bool = true
26 requests []DownloadRequest
27 diagnostics []string
28}
29
30fn create_sandbox_stage(directory string, uid u32, gid u32) !SandboxStage {
31 parent := os.real_path(directory)
32 mut template := (os.join_path(parent, 'download-XXXXXX') + '\x00').bytes()
33 if C.mkdtemp(unsafe { &char(template.data) }) == unsafe { nil } { return os.last_error() }
34 path := unsafe { cstring_to_vstring(template.data) }
35 fd := C.open(unsafe { &char(path.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_CLOEXEC | C.O_NOFOLLOW)
36 if fd < 0 {
37 failure := os.last_error()
38 os.rmdir(path) or { return error('${failure.msg()}; cleanup: ${err.msg()}') }
39 return failure
40 }
41 mask := C.umask(0)
42 C.umask(mask)
43 return SandboxStage{ directory: path, parent: parent, uid: uid, gid: gid, owner_uid: u32(C.getuid()), owner_gid: u32(C.getgid()), mode: 0o666 & ~mask, fd: fd }
44}
45
46fn sandbox_regular_size(fd i32, limit i64) !i64 {
47 mut size := i64(0)
48 mut mode := u32(0)
49 mut links := u64(0)
50 if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() }
51 if mode & 0o170000 != 0o100000 || links != 1 || size < 0 {
52 return error('sandbox output is not an unshared regular file')
53 }
54 if limit > 0 && size > limit { return error('sandbox output exceeds the download size limit') }
55 return size
56}
57
58fn sandbox_copy_file(destination i32, source i32, limit i64) ! {
59 size := sandbox_regular_size(source, limit)!
60 if C.v_alpm_reflink(destination, source) == 0 {
61 if sandbox_regular_size(destination, limit)! != size {
62 return error('sandbox output changed size while copying')
63 }
64 } else {
65 if C.ftruncate(destination, 0) != 0 || C.lseek(destination, 0, C.SEEK_SET) < 0 || C.lseek(source, 0, C.SEEK_SET) < 0 {
66 return os.last_error()
67 }
68 mut remaining := size
69 mut buffer := []u8{len: 32768}
70 for remaining > 0 {
71 count := C.read(source, buffer.data, uint(if remaining < buffer.len {
72 remaining
73 } else {
74 buffer.len
75 }))
76 if count < 0 && C.errno == C.EINTR { continue }
77 if count < 0 { return os.last_error() }
78 if count == 0 { return error('sandbox output was truncated while copying') }
79 mut offset := 0
80 for offset < count {
81 written := C.write(destination, unsafe { &u8(buffer.data) + offset }, uint(count - offset))
82 if written < 0 && C.errno == C.EINTR { continue }
83 if written <= 0 { return os.last_error() }
84 offset += int(written)
85 }
86 remaining -= count
87 }
88 }
89 if C.v_alpm_copy_file_times(destination, source) != 0 { return os.last_error() }
90}
91
92fn sandbox_open_output(path string) !i32 {
93 fd := C.open(unsafe { &char(path.str) }, C.O_RDONLY | C.O_CLOEXEC | C.O_NOFOLLOW | C.O_NONBLOCK)
94 if fd < 0 && C.errno != C.ENOENT { return os.last_error() }
95 return fd
96}
97
98fn sandbox_copy_to(source i32, destination string, uid u32, gid u32, mode u32, limit i64, mut diagnostics []string) ! {
99 mut template := (os.join_path(os.dir(destination), '.alpm-XXXXXX') + '\x00').bytes()
100 fd := C.mkstemp(template.data)
101 if fd < 0 { return os.last_error() }
102 path := unsafe { cstring_to_vstring(template.data) }
103 mut promoted := false
104 defer {
105 if C.close(fd) != 0 { diagnostics << 'could not close sandbox copy: ${os.last_error()}' }
106 if !promoted && C.unlink(unsafe { &char(path.str) }) != 0 {
107 diagnostics << 'could not remove sandbox copy: ${os.last_error()}'
108 }
109 }
110 sandbox_copy_file(fd, source, limit)!
111 if C.fchown(fd, uid, gid) != 0 || C.fchmod(fd, mode) != 0 { return os.last_error() }
112 if C.rename(unsafe { &char(path.str) }, unsafe { &char(destination.str) }) != 0 {
113 return os.last_error()
114 }
115 promoted = true
116}
117
118fn (mut stage SandboxStage) prepare(requests []DownloadRequest) ! {
119 for request in requests {
120 mut mtime := request.existing_mtime
121 if info := os.stat(os.join_path(stage.parent, request.filename)) {
122 if info.size > 0 { mtime = info.mtime }
123 }
124 stage.requests << DownloadRequest{ ...request, existing_mtime: mtime }
125 stage.prepare_partial(request)!
126 }
127 if C.fchown(stage.fd, stage.uid, stage.gid) != 0 { return os.last_error() }
128 stage.sealed = false
129}
130
131fn (mut stage SandboxStage) prepare_partial(request DownloadRequest) ! {
132 name := request.filename + if request.temporary { '' } else { '.part' }
133 path := os.join_path(stage.parent, name)
134 source := sandbox_open_output(path)!
135 if source < 0 { return }
136 defer { if C.close(source) != 0 {
137 stage.diagnostics << 'could not close staged partial: ${os.last_error()}'
138 } }
139 sandbox_copy_to(source, os.join_path(stage.directory, name), stage.uid, stage.gid, 0o600, request.max_size, mut stage.diagnostics)!
140 if C.unlink(unsafe { &char(path.str) }) != 0 { return os.last_error() }
141}
142
143fn (mut stage SandboxStage) seal() ! {
144 if stage.sealed { return }
145 if C.fchown(stage.fd, stage.owner_uid, stage.owner_gid) != 0 || C.fchmod(stage.fd, 0o700) != 0 {
146 return os.last_error()
147 }
148 stage.sealed = true
149}
150
151fn (mut stage SandboxStage) restore(name string, limit i64) !bool {
152 path := os.join_path(stage.directory, name)
153 source := sandbox_open_output(path)!
154 if source < 0 { return false }
155 defer { if C.close(source) != 0 {
156 stage.diagnostics << 'could not close sandbox output: ${os.last_error()}'
157 } }
158 if sandbox_regular_size(source, limit)! == 0 { return false }
159 sandbox_copy_to(source, os.join_path(stage.parent, name), stage.owner_uid, stage.owner_gid, stage.mode, limit, mut stage.diagnostics)!
160 return true
161}
162
163fn (mut stage SandboxStage) finalize() ! {
164 stage.seal()!
165 mut failure := ''
166 for request in stage.requests {
167 mut names := [request.filename]
168 if request.download_signature { names << request.filename + '.sig' }
169 for name in names {
170 limit := if name == request.filename { request.max_size } else { i64(16384) }
171 complete := stage.restore(name, limit) or {
172 failure = err.msg()
173 continue
174 }
175 if !complete && !(request.temporary && name == request.filename) {
176 stage.restore(name + '.part', limit) or {
177 failure = err.msg()
178 continue
179 }
180 }
181 }
182 }
183 if failure != '' { return error(failure) }
184}
185
186fn (mut stage SandboxStage) cleanup() ! {
187 defer {
188 fd := stage.fd
189 stage.fd = -1
190 if fd >= 0 && C.close(fd) != 0 {
191 stage.diagnostics << 'could not close sandbox directory: ${os.last_error()}'
192 }
193 }
194 stage.seal()!
195 // transfers produce flat files. never traverse directories supplied by a worker.
196 for name in os.ls(stage.directory)! {
197 if C.unlinkat(stage.fd, unsafe { &char(name.str) }, 0) != 0 { return os.last_error() }
198 }
199 os.rmdir(stage.directory)!
200}
201