Skip to content

Fixed out-of-bounds read in module protocol when a '%' line has no '='#6178

Open
aizu-m wants to merge 2 commits into
cfengine:masterfrom
aizu-m:module-protocol-percent-no-equals
Open

Fixed out-of-bounds read in module protocol when a '%' line has no '='#6178
aizu-m wants to merge 2 commits into
cfengine:masterfrom
aizu-m:module-protocol-percent-no-equals

Conversation

@aizu-m

@aizu-m aizu-m commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

AddressSanitizer, parsing a "%" module-protocol line with no "=":

ERROR: AddressSanitizer: SEGV on a READ
    #0 strlen
    #1 BufferNewFrom buffer.c:68
    #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=" 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.

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>
@cf-bottom

Copy link
Copy Markdown

Thanks for submitting a PR! Maybe @larsewi can review this?

Comment on lines -10249 to +10252
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] == '=')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your test passes without this change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants