// staging follows pacman dload.c; fresh inodes keep worker descriptors out of the cache. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.mkdtemp(&char) &char fn C.fchown(i32, u32, u32) i32 fn C.fchmod(i32, u32) i32 fn C.umask(u32) u32 fn C.unlinkat(i32, &char, i32) i32 fn C.v_alpm_reflink(i32, i32) i32 fn C.v_alpm_copy_file_times(i32, i32) i32 struct SandboxStage { directory string parent string uid u32 gid u32 owner_uid u32 owner_gid u32 mode u32 mut: fd i32 = -1 sealed bool = true requests []DownloadRequest diagnostics []string } fn create_sandbox_stage(directory string, uid u32, gid u32) !SandboxStage { parent := os.real_path(directory) mut template := (os.join_path(parent, 'download-XXXXXX') + '\x00').bytes() if C.mkdtemp(unsafe { &char(template.data) }) == unsafe { nil } { return os.last_error() } path := unsafe { cstring_to_vstring(template.data) } fd := C.open(unsafe { &char(path.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_CLOEXEC | C.O_NOFOLLOW) if fd < 0 { failure := os.last_error() os.rmdir(path) or { return error('${failure.msg()}; cleanup: ${err.msg()}') } return failure } mask := C.umask(0) C.umask(mask) 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 } } fn sandbox_regular_size(fd i32, limit i64) !i64 { mut size := i64(0) mut mode := u32(0) mut links := u64(0) if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() } if mode & 0o170000 != 0o100000 || links != 1 || size < 0 { return error('sandbox output is not an unshared regular file') } if limit > 0 && size > limit { return error('sandbox output exceeds the download size limit') } return size } fn sandbox_copy_file(destination i32, source i32, limit i64) ! { size := sandbox_regular_size(source, limit)! if C.v_alpm_reflink(destination, source) == 0 { if sandbox_regular_size(destination, limit)! != size { return error('sandbox output changed size while copying') } } else { if C.ftruncate(destination, 0) != 0 || C.lseek(destination, 0, C.SEEK_SET) < 0 || C.lseek(source, 0, C.SEEK_SET) < 0 { return os.last_error() } mut remaining := size mut buffer := []u8{len: 32768} for remaining > 0 { count := C.read(source, buffer.data, uint(if remaining < buffer.len { remaining } else { buffer.len })) if count < 0 && C.errno == C.EINTR { continue } if count < 0 { return os.last_error() } if count == 0 { return error('sandbox output was truncated while copying') } mut offset := 0 for offset < count { written := C.write(destination, unsafe { &u8(buffer.data) + offset }, uint(count - offset)) if written < 0 && C.errno == C.EINTR { continue } if written <= 0 { return os.last_error() } offset += int(written) } remaining -= count } } if C.v_alpm_copy_file_times(destination, source) != 0 { return os.last_error() } } fn sandbox_open_output(path string) !i32 { fd := C.open(unsafe { &char(path.str) }, C.O_RDONLY | C.O_CLOEXEC | C.O_NOFOLLOW | C.O_NONBLOCK) if fd < 0 && C.errno != C.ENOENT { return os.last_error() } return fd } fn sandbox_copy_to(source i32, destination string, uid u32, gid u32, mode u32, limit i64, mut diagnostics []string) ! { mut template := (os.join_path(os.dir(destination), '.alpm-XXXXXX') + '\x00').bytes() fd := C.mkstemp(template.data) if fd < 0 { return os.last_error() } path := unsafe { cstring_to_vstring(template.data) } mut promoted := false defer { if C.close(fd) != 0 { diagnostics << 'could not close sandbox copy: ${os.last_error()}' } if !promoted && C.unlink(unsafe { &char(path.str) }) != 0 { diagnostics << 'could not remove sandbox copy: ${os.last_error()}' } } sandbox_copy_file(fd, source, limit)! if C.fchown(fd, uid, gid) != 0 || C.fchmod(fd, mode) != 0 { return os.last_error() } if C.rename(unsafe { &char(path.str) }, unsafe { &char(destination.str) }) != 0 { return os.last_error() } promoted = true } fn (mut stage SandboxStage) prepare(requests []DownloadRequest) ! { for request in requests { mut mtime := request.existing_mtime if info := os.stat(os.join_path(stage.parent, request.filename)) { if info.size > 0 { mtime = info.mtime } } stage.requests << DownloadRequest{ ...request, existing_mtime: mtime } stage.prepare_partial(request)! } if C.fchown(stage.fd, stage.uid, stage.gid) != 0 { return os.last_error() } stage.sealed = false } fn (mut stage SandboxStage) prepare_partial(request DownloadRequest) ! { name := request.filename + if request.temporary { '' } else { '.part' } path := os.join_path(stage.parent, name) source := sandbox_open_output(path)! if source < 0 { return } defer { if C.close(source) != 0 { stage.diagnostics << 'could not close staged partial: ${os.last_error()}' } } sandbox_copy_to(source, os.join_path(stage.directory, name), stage.uid, stage.gid, 0o600, request.max_size, mut stage.diagnostics)! if C.unlink(unsafe { &char(path.str) }) != 0 { return os.last_error() } } fn (mut stage SandboxStage) seal() ! { if stage.sealed { return } if C.fchown(stage.fd, stage.owner_uid, stage.owner_gid) != 0 || C.fchmod(stage.fd, 0o700) != 0 { return os.last_error() } stage.sealed = true } fn (mut stage SandboxStage) restore(name string, limit i64) !bool { path := os.join_path(stage.directory, name) source := sandbox_open_output(path)! if source < 0 { return false } defer { if C.close(source) != 0 { stage.diagnostics << 'could not close sandbox output: ${os.last_error()}' } } if sandbox_regular_size(source, limit)! == 0 { return false } sandbox_copy_to(source, os.join_path(stage.parent, name), stage.owner_uid, stage.owner_gid, stage.mode, limit, mut stage.diagnostics)! return true } fn (mut stage SandboxStage) finalize() ! { stage.seal()! mut failure := '' for request in stage.requests { mut names := [request.filename] if request.download_signature { names << request.filename + '.sig' } for name in names { limit := if name == request.filename { request.max_size } else { i64(16384) } complete := stage.restore(name, limit) or { failure = err.msg() continue } if !complete && !(request.temporary && name == request.filename) { stage.restore(name + '.part', limit) or { failure = err.msg() continue } } } } if failure != '' { return error(failure) } } fn (mut stage SandboxStage) cleanup() ! { defer { fd := stage.fd stage.fd = -1 if fd >= 0 && C.close(fd) != 0 { stage.diagnostics << 'could not close sandbox directory: ${os.last_error()}' } } stage.seal()! // transfers produce flat files. never traverse directories supplied by a worker. for name in os.ls(stage.directory)! { if C.unlinkat(stage.fd, unsafe { &char(name.str) }, 0) != 0 { return os.last_error() } } os.rmdir(stage.directory)! }