Bug #60629 [Asn->Fbk]: memory corruption when web server closed the fcgi fd(?)

From: Date: Tue, 03 Jan 2012 18:03:28 +0000
Subject: Bug #60629 [Asn->Fbk]: memory corruption when web server closed the fcgi fd(?)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-166187@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60629&edit=1

 ID:                 60629
 Updated by:         fat@php.net
 Reported by:        phpbugs at oops dot mooo dot com
 Summary:            memory corruption when web server closed the fcgi
                     fd(?)
-Status:             Assigned
+Status:             Feedback
 Type:               Bug
 Package:            FPM related
 Operating System:   Debian Squeeze
 PHP Version:        5.3.9RC4
 Assigned To:        fat
 Block user comment: N
 Private report:     N

 New Comment:

Can you please test and validate the attached patch please ?

thx
++ jerome


Previous Comments:
------------------------------------------------------------------------
[2012-01-03 18:02:55] fat@php.net

The following patch has been added/updated:

Patch Name: fpm-bugs-60629.patch
Revision:   1325613774
URL:        https://bugs.php.net/patch-display.php?bug=60629&patch=fpm-bugs-60629.patch&revision=1325613774

------------------------------------------------------------------------
[2012-01-03 12:13:34] fat@php.net

it's on my todo list. I'll try to take time to look at this bugs this week.

++ jerome

------------------------------------------------------------------------
[2012-01-03 12:12:09] laruence@php.net

fat, could you plz look at this? thanks

------------------------------------------------------------------------
[2011-12-30 23:40:43] phpbugs at oops dot mooo dot com

I think it might've been introduced in this commit (~line 270).

http://svn.php.net/viewvc/php/php-src/tags/php_5_3_9RC4/sapi/fpm/fpm/fpm_main.c?r1=317894&r2=317897

It looks like write() might have the same problem, since it returns a ssize_t that's casted to
size_t.

Fix might be to use ssize_t instead?

------------------------------------------------------------------------
[2011-12-30 23:12:00] phpbugs at oops dot mooo dot com

Description:
------------
I tried php5.3.9RC4 today and got a few core dumps.

I think b)fcgi_flush() returns false, making fcgi_write return -1. Then sapi_cgibin_single_write
will make it positive because ret is unsigned in c). As a result, the comparison in d) will fail.

In debugger it looks like PHP is looping over open_packet() in an infinite loop. Each time out_pos
gets increased a little. Finally, after overwriting the whole stack, it will SIGSEGV.

=== https://svn.php.net/repository/php/php-src/tags/php_5_3_9RC4/sapi/fpm/fpm/fastcgi.c
===
int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len)
{
	int limit, rest;

	if (len <= 0) {
		return 0;
	}

	if (req->out_hdr && req->out_hdr->type != type) {
		close_packet(req);
	}

	/* Optimized version */
	limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
	if (!req->out_hdr) {
		limit -= sizeof(fcgi_header);
		if (limit < 0) limit = 0;
	}

	if (len < limit) {
		if (!req->out_hdr) {
			open_packet(req, type);
		}
		memcpy(req->out_pos, str, len);
		req->out_pos += len;
	} else if (len - limit < sizeof(req->out_buf) - sizeof(fcgi_header)) {
		if (!req->out_hdr) {
a)			open_packet(req, type);
		}
		if (limit > 0) {
			memcpy(req->out_pos, str, limit);
			req->out_pos += limit;
		}
		if (!fcgi_flush(req, 0)) {
b)			return -1;
		}

=== https://svn.php.net/repository/php/php-src/tags/php_5_3_9RC4/sapi/fpm/fpm/fpm_main.c
===
static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC)
{
c)	size_t ret;

	/* sapi has started which means everyhting must be send through fcgi */
	if (fpm_is_running) {
		fcgi_request *request = (fcgi_request*) SG(server_context);
		ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
d)		if (ret <= 0) {
			return 0;
		}
		return ret;
	}



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=60629&edit=1


Thread (8 messages)

« previous php.bugs (#166187) next »