Skip to content

Commit

Permalink
LHA/LZH: fix issue for archives with directories
Browse files Browse the repository at this point in the history
  • Loading branch information
micahsnyder committed Mar 2, 2024
1 parent 72d963d commit 408716e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion libclamav/filetypes_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,6 @@ static const char *ftypes_int[] = {
"0:0:00010d0a:PyPy 3.8 byte-compiled (.pyc):CL_TYPE_ANY:CL_TYPE_PYTHON_COMPILED:200",
"0:0:50010d0a:PyPy 3.9 byte-compiled (.pyc):CL_TYPE_ANY:CL_TYPE_PYTHON_COMPILED:200",
"1:0:??0d0d0a:Python 3.7 or newer byte-compiled (.pyc):CL_TYPE_ANY:CL_TYPE_PYTHON_COMPILED:200",
"1:0:????2d6c68(30|31|32|33|34|35|36|37)2d:LHA or LZH archive:CL_TYPE_ANY:CL_TYPE_LHA_LZH:210",
"1:0:????2d6c68(30|31|32|33|34|35|36|37|64)2d:LHA or LZH archive:CL_TYPE_ANY:CL_TYPE_LHA_LZH:210",
NULL};
#endif
99 changes: 49 additions & 50 deletions libclamav_rust/src/scanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

use std::{
ffi::{c_char, CString},
io::{Read},
io::Read,
panic,
path::Path,
ptr::null_mut,
};

use delharc::{LhaDecodeReader};
use delharc::LhaDecodeReader;
use libc::c_void;
use log::{debug, error, warn};

Expand Down Expand Up @@ -222,61 +222,60 @@ pub unsafe extern "C" fn scan_lha_lzh(ctx: *mut cli_ctx) -> cl_error_t {
let filename = filepath.to_string_lossy();
if header.is_directory() {
debug!("Skipping directory {filename}");
continue;
}

debug!("Found file in LHA archive: {filename}");

// Scan the archive metadata first.
if scan_archive_metadata(
ctx,
&filename,
header.compressed_size as usize,
header.original_size as usize,
false,
index,
header.file_crc as i32,
) != cl_error_t_CL_SUCCESS
{
debug!("Extracted file '{filename}' would exceed size limits. Skipping.");
index += 1;
continue;
}
} else {
debug!("Found file in LHA archive: {filename}");

// Scan the archive metadata first.
if scan_archive_metadata(
ctx,
&filename,
header.compressed_size as usize,
header.original_size as usize,
false,
index,
header.file_crc as i32,
) != cl_error_t_CL_SUCCESS
{
debug!("Extracted file '{filename}' would exceed size limits. Skipping.");
index += 1;
continue;
}

// Check if scanning the next file would exceed the limits and should be skipped.
if check_scan_limits("LHA", ctx, header.original_size, 0, 0) != cl_error_t_CL_SUCCESS {
debug!("Extracted file '{filename}' would exceed size limits. Skipping.");
index += 1;
continue;
}
// Check if scanning the next file would exceed the limits and should be skipped.
if check_scan_limits("LHA", ctx, header.original_size, 0, 0) != cl_error_t_CL_SUCCESS {
debug!("Extracted file '{filename}' would exceed size limits. Skipping.");
index += 1;
continue;
}

if !decoder.is_decoder_supported() {
debug!("err: unsupported compression method");
index += 1;
continue;
}
if !decoder.is_decoder_supported() {
debug!("err: unsupported compression method");
index += 1;
continue;
}

// Read the file into a buffer.
let mut file_data = Vec::<u8>::new();

match decoder.read_to_end(&mut file_data) {
Ok(bytes_read) => {
if bytes_read > 0 {
let ret = magic_scan(ctx, &file_data, Some(filename.to_string()));
if ret != cl_error_t_CL_SUCCESS {
debug!("cl_scandesc_magic returned error: {}", ret);
return ret;
// Read the file into a buffer.
let mut file_data = Vec::<u8>::new();

match decoder.read_to_end(&mut file_data) {
Ok(bytes_read) => {
if bytes_read > 0 {
let ret = magic_scan(ctx, &file_data, Some(filename.to_string()));
if ret != cl_error_t_CL_SUCCESS {
debug!("cl_scandesc_magic returned error: {}", ret);
return ret;
}
} else {
debug!("Read zero-byte file.");
}
} else {
debug!("Read zero-byte file.");
}
err => {
debug!("Error reading file {err:?}");
}
}
err => {
debug!("Error reading file {err:?}");
}
}

index += 1;
index += 1;
}

// Get the next file.
match decoder.next_file() {
Expand Down

0 comments on commit 408716e

Please sign in to comment.