You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Jeff Trawick <tr...@attglobal.net> on 2002/01/11 15:03:36 UTC

[PATCH] fix segfault in core input filter

With current CVS code, prefork, APR_POOL_DEBUG, Linux, and
ElectricFence I readily hit a segfault on the APR_BRIGADE_NORMALIZE()
call in core input filter.  It segfaulted because the brigade was
empty.  With the following patch I can't seem to hit a segfault
anymore.

Perhaps the brigade shouldn't be empty for some reason...

If this is really the right issue, APR_BRIGADE_NORMALIZE() should
probably have the logic to check for an empty brigade.

Index: server/core.c
===================================================================
RCS file: /home/cvs/httpd-2.0/server/core.c,v
retrieving revision 1.128
diff -u -r1.128 core.c
--- server/core.c	8 Jan 2002 17:07:19 -0000	1.128
+++ server/core.c	11 Jan 2002 13:57:30 -0000
@@ -2879,7 +2879,9 @@
     }
 
     /* ### This is bad. */
-    APR_BRIGADE_NORMALIZE(ctx->b);
+    if (!APR_BRIGADE_EMPTY(ctx->b)) {
+        APR_BRIGADE_NORMALIZE(ctx->b);
+    }
 
     /* ### AP_MODE_PEEK is a horrific name for this mode because we also
      * eat any CRLFs that we see.  That's not the obvious intention of

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
Jeff Trawick <tr...@attglobal.net> writes:

> The patch below gets rid of a segfault while processing every HTTP/1.1 
> request (w/ ElectricFence and APR_POOL_DEBUG) but there is a much less
> infrequent segfault remaining :(

The infrequent segfault still remaining is from a scenario where 
ap_rgetline() gets APR_SUCCESS from a call to ap_get_brigade() which
hit EOF on the socket.

1) pid 14795 status 3
2) 14795 getting brigade in ap_rgetline()
3) hit socket error 20514 in 14795, returning immortal
4) 14795 getting brigade returned 0
5) 14795 getting brigade in ap_rgetline()
6) [Fri Jan 11 14:41:47 2002] [notice] child pid 14795 exit signal Seg...

1: entering BUSY_READ state
2: ap_rgetline() about to call ap_get_brigade()
3: socket_read() hits EOF on socket, returns immortal "" bucket
4: ap_get_brigade() returns APR_SUCCESS to ap_rgetline()
   (it has a valid line)
5: ap_rgetline() about to call ap_get_brigade()
6: segfault in core input because ctx->b is empty

It would seem that the simplest patch to solve the last two problems
would be to check the brigade near the start of core-input and return
APR_EOF if it is empty.  (Justin suggested that some hours ago :) ).

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
"Bill Stoddard" <bi...@wstoddard.com> writes:

> > @@ -260,6 +261,12 @@
> >           */
> >          APR_BRIGADE_INSERT_HEAD(bb, e);
> >          ap_pass_brigade(r->connection->output_filters, bb);
> > +        if (rv != APR_SUCCESS) {
> > +            /* We got an error earlier reading from the socket.  Make sure
> > +             * keepalive is off so that we don't try to read another request.
> > +             */
> > +            r->connection->keepalive = 0;
> > +        }
> >      }
> >  }
> >  
> 
> Should we set r->connection->aborted here?

no, because that disables lingering close...  we just need to know not
to bother trying to read more requests from the socket, and clearing
keepalive does that

but I'm working on a simpler patch now anyway which handles both of
the paths I found where APR_EOF was getting lost

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Bill Stoddard <bi...@wstoddard.com>.
> @@ -260,6 +261,12 @@
>           */
>          APR_BRIGADE_INSERT_HEAD(bb, e);
>          ap_pass_brigade(r->connection->output_filters, bb);
> +        if (rv != APR_SUCCESS) {
> +            /* We got an error earlier reading from the socket.  Make sure
> +             * keepalive is off so that we don't try to read another request.
> +             */
> +            r->connection->keepalive = 0;
> +        }
>      }
>  }
>  

Should we set r->connection->aborted here?

Bill


Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
Jeff Trawick <tr...@attglobal.net> writes:

> I'm following what happens when we get EOF on socket...
> 
> socket_read() returns APR_SUCCESS and immortal bucket with ""
> 
> core-input doesn't error out since it got APR_SUCCESS
> 
> core-input walks over the "" and deletes the immortal bucket
> 
> core-input sees empty brigade and returns APR_EOF
> 
> it gets returned up through net_time_filter to check_pipeline_flush
> 
> check_pipeline_flush knows ap_get_brigade() failed but it lost the
> retcode and has no way to return the error anyway
> 
> return to caller (ap_process_request)
> 
> do ap_run_log_transaction
> 
> back to ap_process_http_connection
> 
> ap_process_http_connection calls ap_read_request again and we segfault
> since core-input's brigade was empty
> 
> --/--
> 
> if check_pipeline_flush cleared c->keepalive we wouldn't have tried to
> read another request

The patch below gets rid of a segfault while processing every HTTP/1.1 
request (w/ ElectricFence and APR_POOL_DEBUG) but there is a much less
infrequent segfault remaining :(

Index: modules/http/http_request.c
===================================================================
RCS file: /home/cvs/httpd-2.0/modules/http/http_request.c,v
retrieving revision 1.122
diff -u -r1.122 http_request.c
--- modules/http/http_request.c	14 Dec 2001 03:29:13 -0000	1.122
+++ modules/http/http_request.c	11 Jan 2002 18:26:04 -0000
@@ -240,6 +240,7 @@
        ### need to send a FLUSH. */
     apr_bucket_brigade *bb = apr_brigade_create(r->pool);
     apr_off_t zero = 0;
+    apr_status_t rv = APR_SUCCESS;
 
     /* Flush the filter contents if:
      *
@@ -249,7 +250,7 @@
     /* ### shouldn't this read from the connection input filters? */
     /* ### is zero correct? that means "read one line" */
     if (!r->connection->keepalive || 
-        ap_get_brigade(r->input_filters, bb, AP_MODE_PEEK, &zero) != APR_SUCCESS) {
+        (rv = ap_get_brigade(r->input_filters, bb, AP_MODE_PEEK, &zero)) != APR_SUCCESS) {
         apr_bucket *e = apr_bucket_flush_create();
 
         /* We just send directly to the connection based filters.  At
@@ -260,6 +261,12 @@
          */
         APR_BRIGADE_INSERT_HEAD(bb, e);
         ap_pass_brigade(r->connection->output_filters, bb);
+        if (rv != APR_SUCCESS) {
+            /* We got an error earlier reading from the socket.  Make sure
+             * keepalive is off so that we don't try to read another request.
+             */
+            r->connection->keepalive = 0;
+        }
     }
 }
 


-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
Justin Erenkrantz <je...@ebuilt.com> writes:

> On Fri, Jan 11, 2002 at 09:03:36AM -0500, Jeff Trawick wrote:
> > With current CVS code, prefork, APR_POOL_DEBUG, Linux, and
> > ElectricFence I readily hit a segfault on the APR_BRIGADE_NORMALIZE()
> > call in core input filter.  It segfaulted because the brigade was
> > empty.  With the following patch I can't seem to hit a segfault
> > anymore.
> > 
> > Perhaps the brigade shouldn't be empty for some reason...
> 
> If the brigade in core_input_filter is empty, it means that the
> socket (and its bucket) has disappeared on us.  Most likely,
> someone isn't paying attention to APR_EOF return values.  
> 
> We should return an error or at the very least APR_EOF if we
> see an empty brigade there.  But, it would be more useful to
> find out which input filter isn't paying attention.  -- justin

I'm following what happens when we get EOF on socket...

socket_read() returns APR_SUCCESS and immortal bucket with ""

core-input doesn't error out since it got APR_SUCCESS

core-input walks over the "" and deletes the immortal bucket

core-input sees empty brigade and returns APR_EOF

it gets returned up through net_time_filter to check_pipeline_flush

check_pipeline_flush knows ap_get_brigade() failed but it lost the
retcode and has no way to return the error anyway

return to caller (ap_process_request)

do ap_run_log_transaction

back to ap_process_http_connection

ap_process_http_connection calls ap_read_request again and we segfault
since core-input's brigade was empty

--/--

if check_pipeline_flush cleared c->keepalive we wouldn't have tried to
read another request

any other ideas?

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
Ian Holsman <ia...@apache.org> writes:

> Justin Erenkrantz wrote:
> > On Fri, Jan 11, 2002 at 03:14:58PM -0500, Jeff Trawick wrote:
> >
> >>>If the brigade in core_input_filter is empty, it means that the
> >>>socket (and its bucket) has disappeared on us.  Most likely,
> >>> someone isn't paying attention to APR_EOF return values.  We
> >>> should return an error or at the very least APR_EOF if we
> >>>see an empty brigade there.  But, it would be more useful to
> >>>find out which input filter isn't paying attention.  -- justin
> >>>
> >>Does anybody have any concerns before I commit?
> >>
> > Looks good to me.  +1.  -- justin
> >
> 
> does the proxy break?
> it usually does whenever the input filter changes ;(

I have a very simple regression test for proxy that still runs :)

(yeah, I was worried)

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Ian Holsman <ia...@apache.org>.
Justin Erenkrantz wrote:
> On Fri, Jan 11, 2002 at 03:14:58PM -0500, Jeff Trawick wrote:
> 
>>>If the brigade in core_input_filter is empty, it means that the
>>>socket (and its bucket) has disappeared on us.  Most likely,
>>>someone isn't paying attention to APR_EOF return values.  
>>>
>>>We should return an error or at the very least APR_EOF if we
>>>see an empty brigade there.  But, it would be more useful to
>>>find out which input filter isn't paying attention.  -- justin
>>>
>>Does anybody have any concerns before I commit?
>>
> 
> Looks good to me.  +1.  -- justin
> 
> 

does the proxy break?
it usually does whenever the input filter changes ;(


Re: [PATCH] fix segfault in core input filter

Posted by Justin Erenkrantz <je...@ebuilt.com>.
On Fri, Jan 11, 2002 at 03:14:58PM -0500, Jeff Trawick wrote:
> > If the brigade in core_input_filter is empty, it means that the
> > socket (and its bucket) has disappeared on us.  Most likely,
> > someone isn't paying attention to APR_EOF return values.  
> > 
> > We should return an error or at the very least APR_EOF if we
> > see an empty brigade there.  But, it would be more useful to
> > find out which input filter isn't paying attention.  -- justin
> 
> Does anybody have any concerns before I commit?

Looks good to me.  +1.  -- justin


Re: [PATCH] fix segfault in core input filter

Posted by Jeff Trawick <tr...@attglobal.net>.
Justin Erenkrantz <je...@ebuilt.com> writes:

> On Fri, Jan 11, 2002 at 09:03:36AM -0500, Jeff Trawick wrote:
> > With current CVS code, prefork, APR_POOL_DEBUG, Linux, and
> > ElectricFence I readily hit a segfault on the APR_BRIGADE_NORMALIZE()
> > call in core input filter.  It segfaulted because the brigade was
> > empty.  With the following patch I can't seem to hit a segfault
> > anymore.
> > 
> > Perhaps the brigade shouldn't be empty for some reason...
> 
> If the brigade in core_input_filter is empty, it means that the
> socket (and its bucket) has disappeared on us.  Most likely,
> someone isn't paying attention to APR_EOF return values.  
> 
> We should return an error or at the very least APR_EOF if we
> see an empty brigade there.  But, it would be more useful to
> find out which input filter isn't paying attention.  -- justin

Does anybody have any concerns before I commit?

(I should run with ElectricFence more often...)

Index: server/core.c
===================================================================
RCS file: /home/cvs/httpd-2.0/server/core.c,v
retrieving revision 1.129
diff -u -r1.129 core.c
--- server/core.c	11 Jan 2002 18:55:27 -0000	1.129
+++ server/core.c	11 Jan 2002 20:13:48 -0000
@@ -3035,6 +3035,10 @@
         APR_BRIGADE_INSERT_TAIL(ctx->b, e);
         net->in_ctx = ctx;
     }
+    else if (APR_BRIGADE_EMPTY(ctx->b)) {
+        /* hit EOF on socket already */
+        return APR_EOF;
+    }
 
     /* ### This is bad. */
     APR_BRIGADE_NORMALIZE(ctx->b);
@@ -3114,9 +3118,6 @@
 
         AP_DEBUG_ASSERT(*readbytes > 0);
         
-        if (APR_BRIGADE_EMPTY(ctx->b))
-            return APR_EOF;
-
         e = APR_BRIGADE_FIRST(ctx->b);
         rv = apr_bucket_read(e, &str, &len, mode);
 


-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: [PATCH] fix segfault in core input filter

Posted by Justin Erenkrantz <je...@ebuilt.com>.
On Fri, Jan 11, 2002 at 09:03:36AM -0500, Jeff Trawick wrote:
> With current CVS code, prefork, APR_POOL_DEBUG, Linux, and
> ElectricFence I readily hit a segfault on the APR_BRIGADE_NORMALIZE()
> call in core input filter.  It segfaulted because the brigade was
> empty.  With the following patch I can't seem to hit a segfault
> anymore.
> 
> Perhaps the brigade shouldn't be empty for some reason...

If the brigade in core_input_filter is empty, it means that the
socket (and its bucket) has disappeared on us.  Most likely,
someone isn't paying attention to APR_EOF return values.  

We should return an error or at the very least APR_EOF if we
see an empty brigade there.  But, it would be more useful to
find out which input filter isn't paying attention.  -- justin