Skip to content

Use ap_create_request() to build the daemon request object - #957

Open
notroj wants to merge 1 commit into
GrahamDumpleton:developfrom
notroj:feature/ap-create-request
Open

notroj wants to merge 1 commit into
GrahamDumpleton:developfrom
notroj:feature/ap-create-request

Conversation

@notroj

@notroj notroj commented Oct 16, 2026

Copy link
Copy Markdown

Where the daemon process synthesises a request_rec for an incoming request, delegate the allocation and field initialisation to ap_create_request() in httpd 2.4.49+, guarded by AP_MODULE_MAGIC_AT_LEAST(20120211, 106). This API was created specifically for the use-case here, to avoid future disasters like in CVE-2013-5704.

@GrahamDumpleton

Copy link
Copy Markdown
Owner

Thanks for that. Can't remember if this will solve another potential problem that would have arisen if Apache httpd 2.5.0 ever actually made it to 2.6.0. An issue in 2.5.0 was that something was made into a private API which meant mod_wsgi would no longer compile and I could not see a solution. I think it was related to this but I am not sure. Anyway, I will look it over when I get a chance and see if I can work out what it was that was made private in 2.5.0 that made it impossible to compile mod_wsgi against that version.

@GrahamDumpleton

Copy link
Copy Markdown
Owner

Totally unrelated to the Apache 2.5.x problems. #843

@GrahamDumpleton

Copy link
Copy Markdown
Owner

I am a little bit worried by the fact that ap_create_request() calls ap_run_create_request(). That could potentially call into handlers for modules besides mod_wsgi in the context of the mod_wsgi daemon processes, which because the daemon process isn't actually a genuine Apache child worker process and child process init hooks for other modules would not have been called, could cause unexpected behaviour, worst case a process crash if an attempt it made to access something that has not been initialised in context of mod_wsgi daemon process. So if going to use ap_create_request() I suspect it will be necessary to find a way to null out the list of hook handlers for ap_run_create_request() in just the mod_wsgi daemon process so nothing is called when it is run.

The ap_create_request() function also already does:

    r->proto_output_filters = conn->output_filters;
    r->output_filters  = r->proto_output_filters;
    r->proto_input_filters = conn->input_filters;
    r->input_filters   = r->proto_input_filters;

so not sure if they need to be done in conditional pre-processor branch where ap_create_request() is called. Unless this is done because not all versions of Apache supporting ap_create_request() include that setup and so it is being tolerant of older versions which didn't do that.

@GrahamDumpleton

Copy link
Copy Markdown
Owner

Seems like there are two options.

First is to see if the mod_wsgi daemon process can call apr_hook_deregister_all(), although this will clear hooks for all handler types and not sure whether mod_wsgi daemon process is dependent on them for anything such as logging.

Second is to embed magic knowledge of how hook registration macros store stuff.

#include "apr_hooks.h"

/* Re-declare the internal hook structure that Apache's core macro creates */
APR_DECLARE_EXTERNAL_HOOK(ap, AP, void, create_request, (request_rec *r))

void wsgi_clear_create_request_hook(void)
{
    /* Apache's hook macro generates a global variable named _hooks_<name> */
    if (_hooks_create_request.link_create_request) {
        /* Clear the array elements without wiping the memory pool allocation */
        _hooks_create_request.link_create_request->nelts = 0;
    }
}

So being naughty add access to _hooks_create_request directly even though it isn't notionally a public API access point you should be using and so if the implementation of hook macros ever changed, it would break.

@GrahamDumpleton

Copy link
Copy Markdown
Owner

The call ap_hook_get_create_request() may work to get the list of hooked handlers.

#include "apr_hooks.h"
#include "ap_hooks.h"

void wsgi_clear_create_request_hook(void)
{
    /* Use the API-provided getter function rather than internal struct naming */
    apr_array_header_t *hooks = ap_hook_get_create_request();
    
    if (hooks != NULL) {
        /* Truncate the elements array to 0 */
        hooks->nelts = 0;
    }
}

Will need to test this rather than trust the AI. :-)

@notroj

notroj commented Oct 19, 2026

Copy link
Copy Markdown
Author

This was definitely just about avoid future problems in 2.4.x if we extended request_rec (not that we plan to soon, but who knows), wasn't thinking about 2.5/2.6. It would think there's another solution to that referenced problem in #843 if the requirement there is just to override the filter chain.

And I think I needed to do the filter chain reset here exactly because ap_run_create_request was messing it up. Your trick to use ap_hook_get_create_request looks kind of unusual but I can't see a problem with it, that's a supported API and it seems very reasonable/good to clear the hooks in this situation. I will verify it.

Where the daemon process synthesises a request_rec for an incoming
connection, delegate the allocation and field initialisation to
ap_create_request() on the httpd versions that provide it, guarded by
AP_MODULE_MAGIC_AT_LEAST(20120211, 106). That API exists so that
modules building their own request objects pick up every field httpd
expects to be initialised, avoiding the class of problem seen in
CVE-2013-5704 when a new field is added. The hand-rolled apr_pcalloc()
path is kept for older httpd.

ap_create_request() runs the create_request hook, which is how the
HTTP protocol filters get inserted on a request. The daemon writes its
response in CGI script style for the Apache child process to parse,
so those filters must not be present. The daemon process serves only
daemon connections, so empty the create_request hook array through
ap_hook_get_create_request() before the call, leaving the request with
just the connection filters.
@notroj
notroj force-pushed the feature/ap-create-request branch from 301a0fb to 77cfdc1 Compare October 19, 2026 21:13
@notroj

notroj commented Oct 19, 2026

Copy link
Copy Markdown
Author

Passes just test locally for me with 2.4.69.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants