Skip to content

Commit e14adfd

Browse files
committed
Limit the maximum size of parsed RTLIL constants to 1 Gb.
Without this check it's trivially easy to crash Yosys with a tiny RTLIL input by specifying a constant with very large width. Fuzz testers love hitting this over and over again.
1 parent d867e3c commit e14adfd

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

frontends/rtlil/rtlil_frontend.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
YOSYS_NAMESPACE_BEGIN
3232

3333
struct RTLILFrontendWorker {
34+
// Forbid constants of more than 1 Gb.
35+
// This will help us not explode on malicious RTLIL.
36+
static constexpr int MAX_CONST_WIDTH = 1024 * 1024 * 1024;
37+
3438
std::istream *f = nullptr;
3539
RTLIL::Design *design;
3640
bool flag_nooverwrite = false;
@@ -267,7 +271,7 @@ struct RTLILFrontendWorker {
267271
// Can't test value<0 here because we need to stop parsing after '-0'
268272
if (negative_value || line[0] != '\'') {
269273
if (width < INT_MIN || width > INT_MAX)
270-
error("Integer %lld out of range in `%s'.", width, error_token());
274+
error("Integer %lld out of range before `%s'.", width, error_token());
271275
consume_whitespace_and_comments();
272276
return RTLIL::Const(width);
273277
}
@@ -278,6 +282,8 @@ struct RTLILFrontendWorker {
278282
++idx;
279283

280284
std::vector<RTLIL::State> bits;
285+
if (width > MAX_CONST_WIDTH)
286+
error("Constant width %lld out of range before `%s`.", width, error_token());
281287
bits.reserve(width);
282288
while (true) {
283289
RTLIL::State bit;

0 commit comments

Comments
 (0)