From 1416730b40eb5fd627804928b1215c3ec74dd0c9 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Tue, 19 Feb 2019 18:14:13 +0000 Subject: [PATCH] mod_reqtimeout: Allow to configure (TLS-)handshake timeouts. The timeouts apply between the process_connection and pre_read_request hooks. They are disabled by default for compatibily reasons. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1853906 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 4 ++ docs/manual/mod/mod_reqtimeout.xml | 63 ++++++++++++++++-------------- modules/filters/mod_reqtimeout.c | 31 ++++++++++++--- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/CHANGES b/CHANGES index b686be6be9..0ebff11b39 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,9 @@ -*- coding: utf-8 -*- Changes with Apache 2.5.1 + + *) mod_reqtimeout: Allow to configure (TLS-)handshake timeouts. + PR 61310. [Yann Ylavic] + *) mod_auth_digest: Fix a race condition. Authentication with valid credentials could be refused in case of concurrent accesses from different users. PR 63124 [Simon Kappel ] diff --git a/docs/manual/mod/mod_reqtimeout.xml b/docs/manual/mod/mod_reqtimeout.xml index 1a230c8d21..5fd8253850 100644 --- a/docs/manual/mod/mod_reqtimeout.xml +++ b/docs/manual/mod/mod_reqtimeout.xml @@ -48,11 +48,12 @@
  1. - Allow 10 seconds to receive the request including the headers and - 30 seconds for receiving the request body: + Allow for 5 seconds to complete the TLS handshake, 10 seconds to + receive the request headers and 30 seconds for receiving the + request body: - RequestReadTimeout header=10 body=30 + RequestReadTimeout handshake=5 header=10 body=30
  2. @@ -69,10 +70,10 @@
  3. - Allow at least 10 seconds to receive the request including the headers. + Allow at least 10 seconds to receive the request headers. If the client sends data, increase the timeout by 1 second for every 500 bytes received. But do not allow more than 30 seconds for the - request including the headers: + request headers: RequestReadTimeout header=10-30,MinRate=500 @@ -94,65 +95,69 @@ RequestReadTimeout -Set timeout values for receiving request headers and body from client. +Set timeout values for completing the TLS handshake, receiving +the request headers and/or body from client. RequestReadTimeout +[handshake=timeout[-maxtimeout][,MinRate=rate] [header=timeout[-maxtimeout][,MinRate=rate] [body=timeout[-maxtimeout][,MinRate=rate] -RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500 +RequestReadTimeout handshake=0 header=20-40,MinRate=500 body=20,MinRate=500 server configvirtual host -Defaulted to disabled in -version 2.3.14 and earlier. +Defaulted to disabled in version 2.3.14 and earlier. The +handshake stage is available since version 2.5. + -

    This directive can set various timeouts for receiving the request headers - and the request body from the client. If the client fails to send headers or - body within the configured time, a 408 REQUEST TIME OUT error - is sent.

    +

    This directive can set various timeouts for completing the TLS handshake, + receiving the request headers and/or the request body from the client. + If the client fails to complete each of these stages within the configured + time, a 408 REQUEST TIME OUT error is sent.

    -

    For SSL virtual hosts, the header timeout values include the time needed - to do the initial SSL handshake. If the user's browser is configured to +

    For SSL virtual hosts, the handshake timeout values is the time + needed to do the initial SSL handshake. If the user's browser is configured to query certificate revocation lists and the CRL server is not reachable, the initial SSL handshake may take a significant time until the browser gives up - waiting for the CRL. Therefore the header timeout values should not be set - to very low values for SSL virtual hosts. + waiting for the CRL. Therefore the handshake timeout should take + this possible overhead into consideration for SSL virtual hosts (if necessary). The body timeout values include the time needed for SSL renegotiation (if necessary).

    When an AcceptFilter is in use (usually the case on Linux and FreeBSD), the socket is not sent to the server process before at least one byte (or the whole request for - httpready) is received. The header timeout configured with - RequestReadTimeout is only effective after the server process has - received the socket.

    + httpready) is received. The handshake and header timeouts + configured with RequestReadTimeout are only effective + after the server process has received the socket.

    -

    For each of the two timeout types (header or body), there are three ways - to specify the timeout: +

    For each of the three timeout stages (handshake, header or body), there are + three ways to specify the timeout:

    • Fixed timeout value:
      - type=timeout + stage=timeout -

      The time in seconds allowed for reading all of the request headers or - body, respectively. A value of 0 means no limit.

      +

      The time in seconds allowed for completing the whole stage (handshaking, + reading all of the request headers or body). A value of 0 means no limit.

    • Disable module for a vhost:
      - header=0 body=0 + handshake=0 header=0 body=0 -

      This disables mod_reqtimeout completely.

      +

      This disables mod_reqtimeout completely (note that + handshake=0 is the default already and could be omitted).

    • Timeout value that is increased when data is received:
      - type=timeout,MinRate=data_rate + stage=timeout,MinRate=data_rate

      Same as above, but whenever data is received, the timeout value is @@ -163,7 +168,7 @@ version 2.3.14 and earlier.

    • Timeout value that is increased when data is received, with an upper bound:
      - type=timeout-maxtimeout,MinRate=data_rate + stage=timeout-maxtimeout,MinRate=data_rate

      Same as above, but the timeout will not be increased above the second diff --git a/modules/filters/mod_reqtimeout.c b/modules/filters/mod_reqtimeout.c index daed0317b9..a29091c576 100644 --- a/modules/filters/mod_reqtimeout.c +++ b/modules/filters/mod_reqtimeout.c @@ -29,6 +29,9 @@ module AP_MODULE_DECLARE_DATA reqtimeout_module; #define UNSET -1 +#define MRT_DEFAULT_handshake_TIMEOUT 0 /* disabled */ +#define MRT_DEFAULT_handshake_MAX_TIMEOUT 0 +#define MRT_DEFAULT_handshake_MIN_RATE APR_INT32_MAX #define MRT_DEFAULT_header_TIMEOUT 20 #define MRT_DEFAULT_header_MAX_TIMEOUT 40 #define MRT_DEFAULT_header_MIN_RATE 500 @@ -46,6 +49,7 @@ typedef struct typedef struct { + reqtimeout_stage_t handshake; /* Handshaking (TLS) */ reqtimeout_stage_t header; /* Reading the HTTP header */ reqtimeout_stage_t body; /* Reading the HTTP body */ } reqtimeout_srv_cfg; @@ -63,6 +67,7 @@ typedef struct } reqtimeout_con_cfg; static const char *const reqtimeout_filter_name = "reqtimeout"; +static int default_handshake_rate_factor; static int default_header_rate_factor; static int default_body_rate_factor; @@ -372,7 +377,10 @@ static int reqtimeout_init(conn_rec *c) &reqtimeout_module); AP_DEBUG_ASSERT(cfg != NULL); - if (cfg->header.timeout == 0 && cfg->body.timeout == 0) { + /* For compatibility, handshake timeout is disabled when UNSET (< 0) */ + if (cfg->handshake.timeout <= 0 + && cfg->header.timeout == 0 + && cfg->body.timeout == 0) { /* disabled for this vhost */ return DECLINED; } @@ -383,6 +391,10 @@ static int reqtimeout_init(conn_rec *c) ap_set_module_config(c->conn_config, &reqtimeout_module, ccfg); ap_add_output_filter(reqtimeout_filter_name, ccfg, NULL, c); ap_add_input_filter(reqtimeout_filter_name, ccfg, NULL, c); + + if (cfg->handshake.timeout > 0) { + INIT_STAGE(cfg, ccfg, handshake); + } } /* we are not handling the connection, we just do initialization */ @@ -450,6 +462,7 @@ static void *reqtimeout_create_srv_config(apr_pool_t *p, server_rec *s) { reqtimeout_srv_cfg *cfg = apr_pcalloc(p, sizeof(reqtimeout_srv_cfg)); + UNSET_STAGE(cfg, handshake); UNSET_STAGE(cfg, header); UNSET_STAGE(cfg, body); @@ -473,6 +486,7 @@ static void *reqtimeout_merge_srv_config(apr_pool_t *p, void *base_, void *add_) reqtimeout_srv_cfg *add = add_; reqtimeout_srv_cfg *cfg = apr_pcalloc(p, sizeof(reqtimeout_srv_cfg)); + MERGE_STAGE(cfg, base, add, handshake); MERGE_STAGE(cfg, base, add, header); MERGE_STAGE(cfg, base, add, body); @@ -505,7 +519,10 @@ static const char *set_reqtimeout_param(reqtimeout_srv_cfg *conf, char *rate_str = NULL, *initial_str, *max_str = NULL; reqtimeout_stage_t *stage; - if (!strcasecmp(key, "header")) { + if (!strcasecmp(key, "handshake")) { + stage = &conf->handshake; + } + else if (!strcasecmp(key, "header")) { stage = &conf->header; } else if (!strcasecmp(key, "body")) { @@ -611,13 +628,17 @@ static void reqtimeout_hooks(apr_pool_t *pool) * e.g. mod_ftp. Also, if mod_reqtimeout used the pre_connection hook, it * would be inserted on mod_proxy's backend connections. */ - ap_hook_process_connection(reqtimeout_init, NULL, NULL, APR_HOOK_LAST); + ap_hook_process_connection(reqtimeout_init, NULL, NULL, APR_HOOK_FIRST); ap_hook_pre_read_request(reqtimeout_before_header, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_post_read_request(reqtimeout_before_body, NULL, NULL, APR_HOOK_MIDDLE); +#if MRT_DEFAULT_HANDSHAKE_MIN_RATE > 0 + default_handshake_rate_factor = apr_time_from_sec(1) / + MRT_DEFAULT_HANDSHAKE_MIN_RATE; +#endif #if MRT_DEFAULT_HEADER_MIN_RATE > 0 default_header_rate_factor = apr_time_from_sec(1) / MRT_DEFAULT_HEADER_MIN_RATE; @@ -630,8 +651,8 @@ static void reqtimeout_hooks(apr_pool_t *pool) static const command_rec reqtimeout_cmds[] = { AP_INIT_RAW_ARGS("RequestReadTimeout", set_reqtimeouts, NULL, RSRC_CONF, - "Set various timeout parameters for reading request " - "headers and body"), + "Set various timeout parameters for TLS handshake and/or " + "reading request headers and body"), {NULL} }; -- 2.40.0