Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck in infinite loop #205

Closed
drbugfinder-work opened this issue Nov 2, 2021 · 5 comments
Closed

Stuck in infinite loop #205

drbugfinder-work opened this issue Nov 2, 2021 · 5 comments

Comments

@drbugfinder-work
Copy link

drbugfinder-work commented Nov 2, 2021

In context of fluentd/td-agent, we ran into an issue, where yajl is running into an infinite loop.

In gdb we could find out, that buf->len is 0 at that time, so need is 0, as well.

need = buf->len;

The method is called with the variable want=0, so the shift loop will never exit.
while (want >= (need - buf->used)) need <<= 1;

After digging a little bit deeper, we could see, that the malloc functions are completely missing error handling

return malloc(sz);

In our case buf->len was 0, so we assume, that the malloc call failed.

@ppisar
Copy link

ppisar commented Apr 7, 2022

I worry the patch does not fix the integer overflow. The code still contains:

while (want >= (need - buf->used)) need <<= 1;

All the variables are unsigned int. If want and need are UINT_MAX and bug->used is 0, then it will loop indefinitely. Basically if want is UINT_MAX, then the while condition will be true all the time.

But checking want for UINT_MAX is not enough. You will get the same loop any time want >= need, provided need overflows at least once. At each overflow need becomes smaller and smaller until it becomes 0. Then the loop will never terminate because need will be costant 0 and not state changes at any subsequent iteration.

@ppisar
Copy link

ppisar commented Apr 7, 2022

A note regarding correctness of the patchset: It traded a memory corruption for a data corruption. Now if yajl_buf_ensure_available() cannot fulfill the allocation, it returns an error. yajl_buf_ensure_available() is called only from yajl_buf_append() whose job is to append a data into a buffer. Now on yajl_buf_ensure_available() yajl_buf_append() silently discards the request:

void yajl_buf_append(yajl_buf buf, const void * data, unsigned int len)
{
    yajl_buf_ensure_available(buf, len);
    if (yajl_buf_ensure_available(buf, len)) {
        return;
    }
    [...]
}

Hence an affected application won't crash and won't loop, but it will truncate data. A correct fix should keep propagating the error back to the application. If it is too difficult to fix it (and I guess it is because yajl cascades many calls without any mechanism for reporting errors), the bundled yajl should rather abort the process.

@jhawthorn
Copy link
Collaborator

jhawthorn commented Apr 7, 2022

@ppisar can you provide a failing test case, I believe the error cascading works correctly and results in an error being returned to the application. The error is recorded on the buffer and attempts to use the data in the buffer will return an error.

yajl_buf_state buf_err = yajl_buf_err((yajl_buf)g->ctx);
if (buf_err) {
return yajl_gen_alloc_error;
}

/home/jhawthorn/src/yajl-ruby/lib/yajl.rb:80:in `encode': YAJL internal error: failed to allocate memory (Yajl::EncodeError)
	from /home/jhawthorn/src/yajl-ruby/lib/yajl.rb:80:in `encode'
	from test.rb:6:in `block in <main>'
	from test.rb:5:in `loop'
	from test.rb:5:in `<main>'

@ppisar
Copy link

ppisar commented Apr 8, 2022 via email

@yvvdwf
Copy link

yvvdwf commented May 4, 2022

Hello,

I saw CVE-2022-24795 in release notes of GHE v3.4.1, then I'm here after searching a little bit about the CVE. I have had the same remark as ppisar about the infinite loop. Should I provide a test case for it here or send you via other private channel?

Best regards,

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

No branches or pull requests

4 participants