Fixed out-of-bounds read in module protocol when a '%' line has no '='#6178
Fixed out-of-bounds read in module protocol when a '%' line has no '='#6178aizu-m wants to merge 2 commits into
Conversation
AddressSanitizer, parsing a "%" module-protocol line with no "=":
ERROR: AddressSanitizer: SEGV on a READ
#0 strlen
cfengine#1 BufferNewFrom buffer.c:68
cfengine#2 ModuleProtocol evalfunction.c:10252
Came across this whilst reading the module protocol parser. The "%" case
builds the JSON payload with
BufferNewFrom(line + strlen(name) + 1 + 1,
length - strlen(name) - 1 - 1);
which assumes a well-formed "%name=<json>" line. But sscanf("%256[^=]=",
name) also returns with name set when the line carries no "=": it leaves
the whole tail in name. So strlen(name) == length - 1, the length argument
underflows to SIZE_MAX, and the data pointer steps one past the terminating
NUL. BufferAppend() then scans from there and walks off the heap line
buffer.
read_module_protocol() hands arbitrary file lines straight to
ModuleProtocol(), so a single line "%x" is enough to reproduce it.
The other directives ("=", "@", ...) already guard their input; the "%"
case did not. Only do the arithmetic once the "=" delimiter is present.
Changelog: Title
Signed-off-by: aizu-m <aizumusheer2@gmail.com>
|
Thanks for submitting a PR! Maybe @larsewi can review this? |
| if (CheckID(name)) | ||
| /* A well-formed line is "%name=<json>". Without the '=' delimiter | ||
| * sscanf() leaves the whole tail in name, so strlen(name) == length-1 | ||
| * and the "length - strlen(name) - 1 - 1" below underflows. */ | ||
| if (CheckID(name) && line[strlen(name) + 1] == '=') |
There was a problem hiding this comment.
Your test passes without this change
There was a problem hiding this comment.
Good catch, you are right. The OOB read only trips under ASan. In a plain build the scan past the line just reads adjacent heap, JsonParse() rejects the garbage, and the variable ends up undefined either way, so the behavioural check could not tell the patched and unpatched paths apart.
Reworked the test to be deterministic instead of leaning on ASan. The "%bad" line now sits with its terminating NUL on the last readable byte before a PROT_NONE guard page, so the underflowed scan walks straight into it. Confirmed both ways locally:
without the fix: Bus error -> test_module_protocol_percent_no_delimiter: Test failed -> 1 out of 4 tests failed
with the fix: All 4 tests passed
Pushed in 5f94cc1.
The behavioural test only tripped under ASan: in a plain build the scan past the line buffer reads adjacent heap, JsonParse() rejects the garbage, and the variable is left undefined either way, so it passed without the fix. Place the malformed '%bad' line so its terminating NUL is the last readable byte before a PROT_NONE guard page. The unpatched arithmetic steps one past the NUL and BufferAppend() scans into the guard page, so the test now crashes (caught as a failure) without the fix and passes with it. Changelog: none Signed-off-by: aizu-m <aizumusheer2@gmail.com>
|
@cf-bottom Jenkins please :) |
|
Alright, I triggered a build: Jenkins: https://ci.cfengine.com/job/pr-pipeline/13979/ Packages: http://buildcache.cfengine.com/packages/testing-pr/jenkins-pr-pipeline-13979/ |
AddressSanitizer, parsing a "%" module-protocol line with no "=":
Came across this whilst reading the module protocol parser. The "%" case
builds the JSON payload with
which assumes a well-formed "%name=" line. But sscanf("%256[^=]=",
name) also returns with name set when the line carries no "=": it leaves
the whole tail in name. So strlen(name) == length - 1, the length argument
underflows to SIZE_MAX, and the data pointer steps one past the terminating
NUL. BufferAppend() then scans from there and walks off the heap line
buffer.
read_module_protocol() hands arbitrary file lines straight to
ModuleProtocol(), so a single line "%x" is enough to reproduce it.
The other directives ("=", "@", ...) already guard their input; the "%"
case did not. Only do the arithmetic once the "=" delimiter is present.