Skip to content

Fix panic parsing a rockspec comment that ends at EOF#5053

Open
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:chore/lua-rockspec-eof-guard
Open

Fix panic parsing a rockspec comment that ends at EOF#5053
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:chore/lua-rockspec-eof-guard

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

While reading through the Lua cataloger I hit a couple of unchecked byte reads in the hand-written rockspec parser that can panic on malformed input. This tightens the bounds checks so a bad rockspec fails gracefully instead of taking the whole cataloger down.

There are two reads that go one byte past the end of the buffer when a comment runs right up to the end of the file:

  • parseRockspecBlock: when a block starts with a leading comment that consumes the rest of the file, the SkipWhitespace right after it leaves the index at len(data), and the following c = data[*i] reads out of range. Added an EOF check that returns the (empty) block cleanly.
  • parseComment: data[*i] is read after the index has been advanced, to detect a CR/LF pair. If a lone \r is the last byte, that read is out of bounds. Guarded it with *i < len(data).

You can reproduce both with a rockspec whose last line is a comment ending in a bare carriage return and no trailing newline, for example --\r on its own, or a trailing -- x\r. That's malformed but harmless input. The problem is that the panic aborts the entire Lua cataloger, so a single odd file drops every valid Lua package in that scan rather than just skipping the one file.

I added two table cases to rockspec_parser_test.go covering a comment-only file and a trailing comment, both ending in a bare CR. Before the change they panic with an index-out-of-range; after it they parse without error. go test ./syft/pkg/cataloger/lua/... passes.

This is a robustness fix for malformed lockfiles, not a security report.

The hand-written rockspec parser reads a byte past the end of the buffer
in two spots when a comment runs right up to the end of the file.

In parseRockspecBlock, when a block starts with a leading comment that
consumes the rest of the file, the SkipWhitespace afterward leaves the
index at len(data) and the following `c = data[*i]` reads out of range.
In parseComment, `data[*i]` is read after the index is advanced to check
for a CR/LF pair, so a bare carriage return as the last byte reads past
the end.

Both cases show up with a rockspec whose final line is a comment ending
in a lone \r with no trailing newline. That is malformed but harmless
input, and the panic aborts the whole Lua cataloger, so every valid Lua
package in the same scan gets dropped. Guard both reads with a length
check and return cleanly at EOF. Added table cases covering a
comment-only file and a trailing comment, both ending in a bare CR.

Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant