diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/mvzr-bounds.md | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/docs/mvzr-bounds.md b/docs/mvzr-bounds.md new file mode 100644 index 00000000..290254b3 --- /dev/null +++ b/docs/mvzr-bounds.md @@ -0,0 +1,145 @@ +# mvzr: compile() reads past the end of a pattern cut short + +A report for upstream (https://github.com/mnemnion/mvzr), against trunk +3efcfe3 (0.3.9; still its trunk on 2026-11-19, and nothing like it in its +issues or pull requests). pardes patches its fetched copy at build time +(build/patches.zig) until it is fixed there. + +## What happens + +```zig +const mvzr = @import("mvzr"); +// each of these indexes past the pattern: a panic in Debug and +// ReleaseSafe, a read of whatever follows it in ReleaseFast +_ = mvzr.compile("a{"); +_ = mvzr.compile("a{,1"); +_ = mvzr.compile("a{1"); +_ = mvzr.compile("a{1,"); +_ = mvzr.compile("a{1,2"); +_ = mvzr.compile("\\x"); +_ = mvzr.compile("[\\"); +_ = mvzr.compile("[\\x41"); +``` + +`compileRegex` reads `in[i]` after a `{`, after each count and after the +`,` without checking `i < in.len`; `parseHex` slices `in[0..2]` of a +shorter tail (`\x`, `\x1`, and `{\\x`, where a `{` that opens no count +takes the next character with it); `valueFor` reads `in[0]` of an empty +slice (`[\`) and slices `in[1..4]` of a three-byte tail (`[\x41`); and +the end test of a character set is `i == in.len`, which a `\x` step that +moved `i` past the end misses (`[\x`). + +In a safe build each is an index-out-of-bounds panic. In a fast build +the bytes after the pattern in the caller's memory decide what the +expression means, or whether it compiles. A program that compiles what +a person types, at each key, reaches the first on the second key of +`a{2}`. + +Of every pattern up to five characters over `()[]{},*+?|\^$.-a1xnbf` +(5.4 million), 141,599 read past their end. + +## The fix + +Each read checks the pattern's end first, and a pattern cut short is a +bad string (`compile` returns null), as mvzr's other malformed patterns +are. With it the same 5.4 million patterns, and 20 million random ones +of 6 to 12 characters, make no out-of-bounds read. + +```diff +diff --git a/src/mvzr.zig b/src/mvzr.zig +index d4ea747..f8f6039 100644 +--- a/src/mvzr.zig ++++ b/src/mvzr.zig +@@ -1324,6 +1324,7 @@ fn parseByte(in: []const u8) !struct { usize, u8 } { + } + + fn parseHex(in: []const u8) !u8 { ++ if (in.len < 2) return error.BadString; + var out_buf: [1]u8 = undefined; + const b = try std.fmt.hexToBytes(&out_buf, in[0..2]); + return b[0]; +@@ -1446,6 +1447,10 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT { + }, + '{' => { // {M,N} etc, or just character literal is fine + i += 1; ++ if (i == in.len) { ++ bad_string = true; ++ break :dispatch; ++ } + if (in[i] == ',') { // {,? + i += 1; + const d, const c1 = parseByte(in[i..]) catch { +@@ -1453,7 +1458,7 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT { + break :dispatch; + }; + i += d; +- if (in[i] == '}') { // {,N} ++ if (i < in.len and in[i] == '}') { // {,N} + j += prefixModifier(patt, j, RegOp{ .up_to = c1 }) catch + { + bad_string = true; +@@ -1472,9 +1477,13 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT { + continue :dispatch; + }; + i += d1; ++ if (i == in.len) { ++ bad_string = true; ++ break :dispatch; ++ } + if (in[i] == ',') { // {M,? + i += 1; +- if (in[i] == '}') { // {M,} ++ if (i < in.len and in[i] == '}') { // {M,} + j += prefixModifier(patt, j, RegOp{ .star = {} }) catch { + bad_string = true; + break :dispatch; +@@ -1491,7 +1500,7 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT { + break :dispatch; + }; + i += d2; +- if (in[i] != '}') { ++ if (i == in.len or in[i] != '}') { + bad_string = true; + break :dispatch; + } +@@ -1647,6 +1656,7 @@ const ALL_MASK: u64 = ~@as(u64, 0); + /// Returns a value for an escaped character. Does not handle character + /// classes. + fn valueFor(in: []const u8) ?u8 { ++ if (in.len == 0) return null; + switch (in[0]) { + 't' => return '\t', + 'n' => return '\n', +@@ -1654,7 +1664,7 @@ fn valueFor(in: []const u8) ?u8 { + 'w', 'W', 's', 'S', 'd', 'D' => return null, + 'x' => { + if (in.len >= 3) { +- return parseHex(in[1..4]) catch return null; ++ return parseHex(in[1..3]) catch return null; + } else { + // ??? probably malformed but not... yet? + return 'x'; +@@ -1888,7 +1898,7 @@ fn parseCharSet(in: []const u8, patt: []RegOp, sets: []CharSet, j: usize, i_in: + } + } + } // end while +- if (i == in.len or in[i] != ']') { ++ if (i >= in.len or in[i] != ']') { + return BadString; + } + if (s > 255) { +@@ -2552,3 +2562,13 @@ test "iterator with empty alternation terminates" { + while (r_iter.next()) |_| {} + try expectEqual(null, r_iter.next()); + } ++ ++test "a pattern cut short is a bad string, not a read past its end" { ++ for ([_][]const u8{ ++ "a{", "a{,1", "a{1", "a{1,", "a{1,2", // in a repetition ++ "\\x", "\\x1", "{\\\\x", // in a byte escape ++ "[\\", "[\\x", "[\\x41", "[\\\\x]", // in a character set ++ }) |patt| { ++ try expect(compile(patt) == null); ++ } ++} +``` |
