Skip to content

Commit 8512737

Browse files
committed
OrcLib: YaraScanner: fix Yara's callbacks file boundary
This could lead to loop execution.
1 parent d898ff0 commit 8512737

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/OrcLib/YaraScanner.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ HRESULT YaraScanner::ScanBlocks(const std::shared_ptr<ByteStream>& stream, Match
543543
}
544544

545545
// On first fetch only read 1MB as it will be often enough for header matching
546-
context->block.size = std::min(context->buffer.size(), static_cast<size_t>(1048576));
546+
context->block.size = std::min(
547+
static_cast<uint64_t>(context->buffer.size()),
548+
std::min(static_cast<uint64_t>(1048576), context->streamSize));
547549
context->block.base = 0;
548550
return &context->block;
549551
};
@@ -562,7 +564,14 @@ HRESULT YaraScanner::ScanBlocks(const std::shared_ptr<ByteStream>& stream, Match
562564
return nullptr;
563565
}
564566

565-
context->block.size = context->buffer.size(); // TODO: std::min with stream size ?
567+
if (context->block.base > context->streamSize)
568+
{
569+
Log::Warn(L"Unexpected file offset in Yara callback but rule evaluation should consistent");
570+
return nullptr;
571+
}
572+
573+
context->block.size =
574+
std::min(context->streamSize - context->block.base, static_cast<uint64_t>(context->buffer.size()));
566575
return &context->block;
567576
};
568577

0 commit comments

Comments
 (0)