mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
magiskboot: handle decompression errors in unpack
Previously, decompress_bytes dropped the LoggedResult from decoding, returning void. In bootimg.cpp, unpack and split_image_dtb never checked whether decompression succeeded, leaving empty 0-byte output files and returning RETURN_OK (0). Return a boolean status from decompress_bytes and check it during unpack and split_image_dtb. On decompression failure, remove any incomplete output files and return RETURN_ERROR (1). Assisted-by: Gemini 3.8 Flash
This commit is contained in:
@@ -28,8 +28,8 @@ static int find_dtb_offset(const void *buf, size_t len) {
|
||||
return find_dtb_offset({static_cast<const uint8_t *>(buf), len});
|
||||
}
|
||||
|
||||
static void decompress(FileFormat type, int fd, const void *in, size_t size) {
|
||||
decompress_bytes(type, byte_view { in, size }, fd);
|
||||
static bool decompress(FileFormat type, int fd, const void *in, size_t size) {
|
||||
return decompress_bytes(type, byte_view { in, size }, fd);
|
||||
}
|
||||
|
||||
static off_t compress_len(FileFormat type, byte_view in, int fd) {
|
||||
@@ -472,7 +472,11 @@ int split_image_dtb(Utf8CStr filename, bool skip_decomp) {
|
||||
FileFormat fmt = check_fmt_lg(img.data(), img.size());
|
||||
if (!skip_decomp && fmt_compressed(fmt)) {
|
||||
int fd = creat(KERNEL_FILE, 0644);
|
||||
decompress(fmt, fd, img.data(), off);
|
||||
if (!decompress(fmt, fd, img.data(), off)) {
|
||||
close(fd);
|
||||
unlink(KERNEL_FILE);
|
||||
return 1;
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
dump(img.data(), off, KERNEL_FILE);
|
||||
@@ -495,7 +499,11 @@ int unpack(Utf8CStr image, bool skip_decomp, bool hdr) {
|
||||
if (!skip_decomp && fmt_compressed(boot.k_fmt)) {
|
||||
if (boot.hdr->kernel_size() != 0) {
|
||||
int fd = creat(KERNEL_FILE, 0644);
|
||||
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
|
||||
if (!decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size())) {
|
||||
close(fd);
|
||||
unlink(KERNEL_FILE);
|
||||
return RETURN_ERROR;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
} else {
|
||||
@@ -519,7 +527,10 @@ int unpack(Utf8CStr image, bool skip_decomp, bool hdr) {
|
||||
owned_fd fd = xopenat(dirfd, file_name, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
|
||||
FileFormat fmt = check_fmt_lg(boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
|
||||
if (!skip_decomp && fmt_compressed(fmt)) {
|
||||
decompress(fmt, fd, boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
|
||||
if (!decompress(fmt, fd, boot.ramdisk + it.ramdisk_offset, it.ramdisk_size)) {
|
||||
unlinkat(dirfd, file_name, 0);
|
||||
return RETURN_ERROR;
|
||||
}
|
||||
} else {
|
||||
xwrite(fd, boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
|
||||
}
|
||||
@@ -527,7 +538,11 @@ int unpack(Utf8CStr image, bool skip_decomp, bool hdr) {
|
||||
} else if (!skip_decomp && fmt_compressed(boot.r_fmt)) {
|
||||
if (boot.hdr->ramdisk_size() != 0) {
|
||||
int fd = creat(RAMDISK_FILE, 0644);
|
||||
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
|
||||
if (!decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size())) {
|
||||
close(fd);
|
||||
unlink(RAMDISK_FILE);
|
||||
return RETURN_ERROR;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
} else {
|
||||
@@ -541,7 +556,11 @@ int unpack(Utf8CStr image, bool skip_decomp, bool hdr) {
|
||||
if (!skip_decomp && fmt_compressed(boot.e_fmt)) {
|
||||
if (boot.hdr->extra_size() != 0) {
|
||||
int fd = creat(EXTRA_FILE, 0644);
|
||||
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
|
||||
if (!decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size())) {
|
||||
close(fd);
|
||||
unlink(EXTRA_FILE);
|
||||
return RETURN_ERROR;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -270,14 +270,15 @@ pub fn compress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) {
|
||||
}();
|
||||
}
|
||||
|
||||
pub fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) {
|
||||
pub fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) -> bool {
|
||||
let mut out_file = unsafe { ManuallyDrop::new(File::from_raw_fd(out_fd)) };
|
||||
|
||||
let _ = || -> LoggedResult<()> {
|
||||
(|| -> LoggedResult<()> {
|
||||
let mut decoder = get_decoder(format, in_bytes)?;
|
||||
std::io::copy(decoder.as_mut(), out_file.deref_mut())?;
|
||||
Ok(())
|
||||
}();
|
||||
})()
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
// Command-line entry points
|
||||
|
||||
@@ -80,7 +80,7 @@ pub mod ffi {
|
||||
|
||||
fn check_fmt(buf: &[u8]) -> FileFormat;
|
||||
fn compress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32);
|
||||
fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32);
|
||||
fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32) -> bool;
|
||||
fn fmt2name(fmt: FileFormat) -> *const c_char;
|
||||
fn fmt_compressed(fmt: FileFormat) -> bool;
|
||||
fn fmt_compressed_any(fmt: FileFormat) -> bool;
|
||||
|
||||
Reference in New Issue
Block a user