From 8a6f3c50eee03e657d4faa0fc989424b237d18af Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:29:43 +0000 Subject: [PATCH] Net::Server::HTTP: bound exec_cgi body reads and stop at end of input exec_cgi forwarded the request body to the CGI script with read() calls sized by the client's Content-Length header. Perl grows the buffer to the requested length before reading, so a request declaring a huge length aborted the worker with an out-of-memory error that no eval can catch. The same loop subtracted the return value of read() from the remaining length without checking it. When the client closed its side before sending the declared body, read() returned 0, the remaining length never reached zero, the CGI's stdin stayed in the select set as writable, and the loop spun at full CPU until the request timed out. Read the body in chunks of at most 64 KiB, and stop forwarding when read() returns 0 or undef. When the body is complete or the client has gone, close the CGI's stdin so the script sees end of input instead of waiting for bytes that will never arrive. Notes for deciding whether to apply this patch: - The CGI script's stdin is now closed once the body has been forwarded or the client has gone. A script that reads stdin to end of file gets end of file at that point instead of waiting until it exits or the request times out. - A non-numeric or negative Content-Length is still passed to read() as before. A negative value dies inside the request eval and does not cause the out-of-memory abort or the busy loop. - Not tested on a running server. It was checked by review and by applying it to Net-Server 2.018. - Applies on its own to Net-Server 2.018, and together with the other CPANSec patches for Net-Server 2.018 when they are applied in CVE-ID order. --- lib/Net/Server/HTTP.pm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Net/Server/HTTP.pm b/lib/Net/Server/HTTP.pm index d186e06..1f00094 100644 --- a/lib/Net/Server/HTTP.pm +++ b/lib/Net/Server/HTTP.pm @@ -680,10 +680,13 @@ sub exec_cgi { } } if (@$i) { - my $bytes = read(STDIN, my $buf, $len); + my $bytes = read(STDIN, my $buf, $len > 65536 ? 65536 : $len); print $in $buf if $bytes; - $len -= $bytes; - $s_in = undef if $len <= 0; + $len -= $bytes if $bytes; + if (!$bytes || $len <= 0) { # body complete, or client closed or failed + $s_in = undef; + close $in; + } } } if (!$self->{'request_info'}->{'headers_sent'}) { -- 2.53.0