]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
AST-2020-002 - res_pjsip: Stop sending INVITEs after challenge limit.
authorBen Ford <bford@digium.com>
Mon, 2 Nov 2020 16:29:31 +0000 (10:29 -0600)
committerBen Ford <bford@digium.com>
Wed, 4 Nov 2020 19:27:42 +0000 (13:27 -0600)
If Asterisk sends out an INVITE and receives a challenge with a
different nonce value each time, it will continuously send out INVITEs,
even if the call is hung up. The endpoint must be configured for
outbound authentication for this to occur. A limit has been set on
outbound INVITEs so that, once reached, Asterisk will stop sending
INVITEs and the transaction will terminate.

ASTERISK-29013

Change-Id: I2d001ca745b00ca8aa12030f2240cd72363b46f7

include/asterisk/res_pjsip.h
include/asterisk/res_pjsip_session.h
res/res_pjsip.c
res/res_pjsip_session.c

index 3991ebdb979226b023c3a69d57e7ea9c261b73f4..f48ea3e3b27a0dd5a46bd92780ff610afa79b0ef 100644 (file)
@@ -75,6 +75,9 @@ struct pjsip_tpselector;
 /*! \brief Maximum number of ciphers supported for a TLS transport */
 #define SIP_TLS_MAX_CIPHERS 64
 
+/*! Maximum number of challenges before assuming that we are in a loop */
+#define MAX_RX_CHALLENGES      10
+
 /*!
  * \brief Structure for SIP transport information
  */
index 27d669fa159ca63b87148e24d8577bea4d6f78c0..efcdb7db0c969c0b21c3ef6a88db77bd9ee60ec1 100644 (file)
@@ -171,6 +171,8 @@ struct ast_sip_session {
        pjsip_uri *request_uri;
        /*! Joint capabilities */
        struct ast_format_cap *joint_caps;
+       /*! Number of challenges received during outgoing requests to determine if we are in a loop */
+       unsigned int authentication_challenge_count:4;
 };
 
 typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
index ac0d070f33da10eb48e7e26ec0e9b477f9e86380..93ac3ee62c053e3736eb2305c070b67a8e37cde4 100644 (file)
@@ -3833,8 +3833,6 @@ static pj_bool_t does_method_match(const pj_str_t *message_method, const char *s
        return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
 }
 
-/*! Maximum number of challenges before assuming that we are in a loop */
-#define MAX_RX_CHALLENGES      10
 #define TIMER_INACTIVE         0
 #define TIMEOUT_TIMER2         5
 
index c93be19ffd09a403e1510d5077e6a2e950a055c7..bb2fcea58f877982318665158d63c89e816ee6c4 100644 (file)
@@ -1210,7 +1210,6 @@ static pjsip_module session_reinvite_module = {
        .on_rx_request = session_reinvite_on_rx_request,
 };
 
-
 void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
                ast_sip_session_response_cb on_response)
 {
@@ -1508,12 +1507,17 @@ struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
                ao2_ref(session, -1);
                return NULL;
        }
+
+       /* Track the number of challenges received on outbound requests */
+       session->authentication_challenge_count = 0;
+
        AST_LIST_TRAVERSE(&session->supplements, iter, next) {
                if (iter->session_begin) {
                        iter->session_begin(session);
                }
        }
 
+
        /* Avoid unnecessary ref manipulation to return a session */
        ret_session = session;
        session = NULL;
@@ -1680,6 +1684,11 @@ static pj_bool_t outbound_invite_auth(pjsip_rx_data *rdata)
 
        session = inv->mod_data[session_module.id];
 
+       if (++session->authentication_challenge_count > MAX_RX_CHALLENGES) {
+               ast_debug(3, "Initial INVITE reached maximum number of auth attempts.\n");
+               return PJ_FALSE;
+       }
+
        if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata, tsx,
                &tdata)) {
                return PJ_FALSE;
@@ -2882,6 +2891,7 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
                                                ast_debug(1, "reINVITE received final response code %d\n",
                                                        tsx->status_code);
                                                if ((tsx->status_code == 401 || tsx->status_code == 407)
+                                                       && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
                                                        && !ast_sip_create_request_with_auth(
                                                                &session->endpoint->outbound_auths,
                                                                e->body.tsx_state.src.rdata, tsx, &tdata)) {
@@ -2976,6 +2986,7 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
                                                (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
                                                tsx->status_code);
                                        if ((tsx->status_code == 401 || tsx->status_code == 407)
+                                               && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
                                                && !ast_sip_create_request_with_auth(
                                                        &session->endpoint->outbound_auths,
                                                        e->body.tsx_state.src.rdata, tsx, &tdata)) {