RDoc::MethodAttr is vulnerable to Regular Expression Denial of Service (ReDoS)
A regular expression in RDoc's method attribute parser contains overlapping repeating groups, so certain crafted input triggers catastrophic backtracking. An attacker who can supply such a string to the block_params parser could make it consume 100% CPU for an extremely long time, causing denial of service.
- Disclosed after 21 months
The method block_params in class RDoc::MethodAttr uses a regular expression that is vulnerable to Denial of Service due to catastrophic backtracking.
The regular expression is:
([A-Z:a-z0-9_]+)\.([a-z0-9_]+)(\s*\(\s*[a-z0-9_.,\s]*\s*\)\s*)?
Source: https://github.com/ruby/ruby/blob/master/lib/rdoc/method_attr.rb#L265
The ReDoS requence is: (\s*\(\s*[a-z0-9_.,\s]*\s*\)\s*). It contains three overlapping repeating groups (repeated characters are 0x20, 0xa0, [09-0d]), so the worst-case complexity is cubic as there are 3 infinitely repeating groups. Cubic complexity here means that if the vulnerable part of the string is doubled in length, the execution time should be about 8 times longer (2^3).
PoC
I have not found a way to exploit this vulnerability directly from the file documentation (by running rdoc), however directly it is very easy:
use 'rdoc'
RDoc::MethodAttr.new(nil, nil).block_params = '0.0(' + ' '*3456 + '0'
The client's code that relies on AnyMethod is also vulnerable since it inherits MethodAttr:
use 'rdoc'
RDoc::AnyMethod.new(nil, nil).block_params = '0.0(' + ' '*3456 + '0'
If an attacker provides a malicious string to AnyMethod|MethodAttr's block_params documentation parser, it will get stuck processing the input for an extremely long time, consuming 100% CPU.
Impact
An attacker could cause an effective denial of service, by crafting an input which exploits catastrophic backtracking for the regular expression.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The regular expression contains three overlapping repeating groups that can match the same whitespace characters, giving cubic worst-case backtracking.
Impact
An attacker could cause an effective denial of service by supplying a crafted string to the block_params parser, though the reporter did not find a way to trigger it directly through running rdoc on file documentation.
What the attacker needed
Attacker-controlled string reaches block_params; direct library use or a future rdoc path
How it was found
- Create an RDoc MethodAttr or AnyMethod object.
- Assign a crafted string to its block_params that places a long run of whitespace inside the bracketed section.
- Observe that processing the input takes an extremely long time and consumes 100% CPU.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A regular expression is a pattern used to match text. Some patterns can be written so that, on certain inputs, the matching engine tries a huge number of combinations and effectively hangs. Here a crafted string with many spaces inside brackets makes the parser burn CPU, so the program stops responding.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.