From 68b340f5f5d4f086a5ba25276312d13f2fb357b5 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:21:51 +0500 Subject: [PATCH 01/10] [core] Confine MSRP remote file-selector name to the receive directory Without an explicit path, `msrp_recv_file_function()` joined the `sip_msrp_file_name` channel variable, taken from the remote SDP `a=file-selector` name, onto the base directory unchanged, so a name with directory components opened a file outside it. Reduce the name to its last path component with `switch_cut_path()` and reject an empty name, `.` and `..`. `test_switch_cut_path_strips_directory_components` covers the path reduction the receive path relies on. --- src/switch_msrp.c | 17 ++++++++++++++--- tests/unit/switch_utils.c | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index f1a2d75cf2..8ea5ef80a8 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1701,14 +1701,25 @@ SWITCH_STANDARD_APP(msrp_recv_file_function) switch_channel_answer(channel); if (zstr(data)) { - filename = switch_channel_get_variable(channel, "sip_msrp_file_name"); + const char *sip_file_name = switch_channel_get_variable(channel, "sip_msrp_file_name"); + const char *safe_name; - if (zstr(filename)) { + if (zstr(sip_file_name)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No file specified.\n"); + return; } - filename = switch_core_session_sprintf(session, "%s%s%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR, filename); + /* The name is taken from the remote SDP file-selector. Reduce it to its final + path component so the opened file always stays inside the receive directory. */ + safe_name = switch_cut_path(sip_file_name); + if (zstr(safe_name) || !strcmp(safe_name, ".") || !strcmp(safe_name, "..")) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Rejecting remote file name '%s'\n", sip_file_name); + + return; + } + + filename = switch_core_session_sprintf(session, "%s%s%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR, safe_name); } if (!(msrp_session = switch_core_media_get_msrp_session(session))) { diff --git a/tests/unit/switch_utils.c b/tests/unit/switch_utils.c index 07657141d5..c4ff2e3818 100644 --- a/tests/unit/switch_utils.c +++ b/tests/unit/switch_utils.c @@ -488,6 +488,28 @@ FST_TEST_BEGIN(test_switch_http_parse_header) } FST_TEST_END() +FST_TEST_BEGIN(test_switch_cut_path_strips_directory_components) +{ + size_t i; + struct { + const char *in; + const char *want; + } cases[] = { + { "plain.txt", "plain.txt" }, /* no separator: returned unchanged */ + { "../escape/x.txt", "x.txt" }, /* relative components dropped */ + { "/etc/passwd", "passwd" }, /* leading absolute path dropped */ + { "a/../../x", "x" }, /* embedded .. dropped with the rest of the path */ + { "dir\\sub\\f.txt", "f.txt" }, /* backslash is also a separator */ + { "trailing/", "" }, /* nothing follows the final separator */ + { "..", ".." }, /* no separator: caller must reject this residue */ + }; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + fst_check_string_equals(switch_cut_path(cases[i].in), cases[i].want); + } +} +FST_TEST_END() + FST_SUITE_END() FST_MINCORE_END() From b3871f4066374e1df81b30a560c70cca01cb2e70 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:26:28 +0500 Subject: [PATCH 02/10] [core] Bound MSRP reply and report to their buffer `msrp_reply()` and `msrp_report()` formatted the To-Path, From-Path, Message-ID and delimiter of the received request into a 2048-byte stack buffer with an unbounded `sprintf()`. Use `snprintf()` and send nothing when the result does not fit, since a truncated reply would lack its end-line. --- src/switch_msrp.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 8ea5ef80a8..9149f39c7a 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1013,13 +1013,22 @@ switch_status_t msrp_reply(switch_msrp_client_socket_t *csock, switch_msrp_msg_t { char buf[2048]; switch_size_t len; - sprintf(buf, "MSRP %s 200 OK\r\nTo-Path: %s\r\nFrom-Path: %s\r\n" + int ret; + + ret = snprintf(buf, sizeof(buf), "MSRP %s 200 OK\r\nTo-Path: %s\r\nFrom-Path: %s\r\n" "%s$\r\n", msrp_msg->transaction_id, switch_str_nil(switch_msrp_msg_get_header(msrp_msg, MSRP_H_FROM_PATH)), switch_str_nil(switch_msrp_msg_get_header(msrp_msg, MSRP_H_TO_PATH)), msrp_msg->delimiter); - len = strlen(buf); + + if (ret < 0 || (switch_size_t)ret >= sizeof(buf)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "MSRP reply does not fit %" SWITCH_SIZE_T_FMT " bytes, not sent\n", sizeof(buf)); + + return SWITCH_STATUS_FALSE; + } + + len = ret; return msrp_socket_send(csock, buf, &len); } @@ -1028,7 +1037,9 @@ switch_status_t msrp_report(switch_msrp_client_socket_t *csock, switch_msrp_msg_ { char buf[2048]; switch_size_t len; - sprintf(buf, "MSRP %s REPORT\r\nTo-Path: %s\r\nFrom-Path: %s\r\nMessage-ID: %s\r\n" + int ret; + + ret = snprintf(buf, sizeof(buf), "MSRP %s REPORT\r\nTo-Path: %s\r\nFrom-Path: %s\r\nMessage-ID: %s\r\n" "Status: 000 %s\r\nByte-Range: 1-%" SWITCH_SIZE_T_FMT "/%" SWITCH_SIZE_T_FMT "\r\n%s$\r\n", msrp_msg->transaction_id, @@ -1039,7 +1050,14 @@ switch_status_t msrp_report(switch_msrp_client_socket_t *csock, switch_msrp_msg_ msrp_msg->accumulated_bytes ? msrp_msg->accumulated_bytes : msrp_msg->byte_end, msrp_msg->bytes, msrp_msg->delimiter); - len = strlen(buf); + + if (ret < 0 || (switch_size_t)ret >= sizeof(buf)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "MSRP report does not fit %" SWITCH_SIZE_T_FMT " bytes, not sent\n", sizeof(buf)); + + return SWITCH_STATUS_FALSE; + } + + len = ret; if (globals.debug) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "report: %" SWITCH_SIZE_T_FMT " bytes [\n%s]\n", len, buf); From 108e7f12661a4fe2342034139e27ba11d84f0a56 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:30:57 +0500 Subject: [PATCH 03/10] [core] Treat received MSRP header values as data, not a format `msrp_parse_header()` and `msrp_parse_headers()` passed the received header value as the format argument of `switch_msrp_msg_add_header()`, so `%` sequences in it were interpreted. Pass it through `"%s"`; header values containing `%` are now stored unchanged. --- src/switch_msrp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 9149f39c7a..9d64036f9e 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -697,7 +697,7 @@ static char *msrp_parse_header(char *start, int skip, const char *end, switch_ms if (q > p) { if (*(q-1) == '\r') *(q-1) = '\0'; *q = '\0'; - switch_msrp_msg_add_header(msrp_msg, htype, p); + switch_msrp_msg_add_header(msrp_msg, htype, "%s", p); return q + 1; } return start; @@ -718,7 +718,7 @@ static switch_msrp_msg_t *msrp_parse_headers(char *start, int len, switch_msrp_m if (q > p) { *q = '\0'; - switch_msrp_msg_add_header(msrp_msg, MSRP_H_TRASACTION_ID, p); + switch_msrp_msg_add_header(msrp_msg, MSRP_H_TRASACTION_ID, "%s", p); switch_msrp_msg_add_header(msrp_msg, MSRP_H_DELIMITER, "-------%s", p); msrp_msg->state = MSRP_ST_PARSE_HEADER; } @@ -748,7 +748,7 @@ static switch_msrp_msg_t *msrp_parse_headers(char *start, int len, switch_msrp_m if (q > p) { if (*(q-1) == '\r') *(q-1) = '\0'; *q = '\0'; - switch_msrp_msg_add_header(msrp_msg, MSRP_H_CODE_DESCRIPTION, p); + switch_msrp_msg_add_header(msrp_msg, MSRP_H_CODE_DESCRIPTION, "%s", p); p = ++q; } } From 93fe4cc929a23c55d783940be7245638d46aee72 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:39:46 +0500 Subject: [PATCH 04/10] [core] Treat the MSRP To-Path as data in msrp_find_uuid() `msrp_find_uuid()` passed the tail of the received To-Path as the format argument of `switch_snprintf()`. Pass it through `"%s"`. --- src/switch_msrp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 9d64036f9e..5c8578be50 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1077,7 +1077,7 @@ static switch_bool_t msrp_find_uuid(char *uuid, const char *to_path) } if (slash_count < 3) return SWITCH_FALSE; if (len - i++ < 36) return SWITCH_FALSE; - switch_snprintf(uuid, 37, to_path + i); + switch_snprintf(uuid, 37, "%s", to_path + i); return SWITCH_TRUE; } From eff3cc6f5e8035eb6a5c9af19a990e307bd207d9 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:42:01 +0500 Subject: [PATCH 05/10] [core] Check for a missing To-Path before msrp_find_uuid() reads it `msrp_find_uuid()` called `strlen()` on the To-Path before asserting it was set, and a first SEND without a To-Path header passes NULL. Return `SWITCH_FALSE` for it, so the worker handles it like any To-Path that names no session. --- src/switch_msrp.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 5c8578be50..4bac62e283 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1066,10 +1066,15 @@ switch_status_t msrp_report(switch_msrp_client_socket_t *csock, switch_msrp_msg_ static switch_bool_t msrp_find_uuid(char *uuid, const char *to_path) { - int len = strlen(to_path); + int len; int i; int slash_count = 0; - switch_assert(to_path); + + if (!to_path) { + return SWITCH_FALSE; + } + + len = strlen(to_path); for(i=0; i Date: Wed, 4 Nov 2026 19:43:13 +0500 Subject: [PATCH 06/10] [core] Remove the MSRP Byte-Range start assert `msrp_parse_headers()` asserted that the received Byte-Range start was above zero, so a range starting at 0 aborted the process. The payload length derived from it is already checked against the declared total and against the received data. --- src/switch_msrp.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 4bac62e283..68652fc6ad 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -793,7 +793,6 @@ static switch_msrp_msg_t *msrp_parse_headers(char *start, int len, switch_msrp_m if (q > p) { *q = '\0'; msrp_msg->byte_start = atoi(p); - switch_assert(msrp_msg->byte_start > 0); p = ++q; if (*p && *p == '*') { msrp_msg->range_star = 1; From 44b8010ba59831747be61e55973f9331ef3e3113 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:49:28 +0500 Subject: [PATCH 07/10] [core] Bound the MSRP SEND request built in switch_msrp_do_send() The request line and headers, the CRLF after the payload and the end-line were written into the fixed stack buffer with `sprintf()`; only the payload copy was checked, and the To-Path can be as long as the remote SDP `a=path`. Build each part with `snprintf()` or a checked copy, advance the length by what was written, and drop a message that does not fit with an error log. --- src/switch_msrp.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 68652fc6ad..6b1c5bc982 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1564,6 +1564,8 @@ static switch_status_t switch_msrp_do_send(switch_msrp_session_t *ms, switch_msr const char *to_path = msrp_h_to_path ? msrp_h_to_path : ms->remote_path; const char *from_path = msrp_h_from_path ? msrp_h_from_path: ms->local_path; const char *content_type = switch_msrp_msg_get_header(msrp_msg, MSRP_H_CONTENT_TYPE); + switch_size_t room; + int ret; if (msrp_msg->payload_bytes == 2 && msrp_msg->payload && !strncmp(msrp_msg->payload, "\r\n", 2)) { // discard \r\n appended in uuid_send_text @@ -1582,7 +1584,7 @@ static switch_status_t switch_msrp_do_send(switch_msrp_session_t *ms, switch_msr random_string(transaction_id, MSRP_TRANS_ID_LEN); switch_uuid_str(message_id, sizeof(message_id)); - sprintf(buf, "MSRP %s SEND\r\nTo-Path: %s\r\nFrom-Path: %s\r\n" + ret = snprintf(buf, sizeof(buf), "MSRP %s SEND\r\nTo-Path: %s\r\nFrom-Path: %s\r\n" "Message-ID: %s\r\n" "Byte-Range: 1-%" SWITCH_SIZE_T_FMT "/%" SWITCH_SIZE_T_FMT "\r\n" "%s%s%s", @@ -1596,25 +1598,43 @@ static switch_status_t switch_msrp_do_send(switch_msrp_session_t *ms, switch_msr msrp_msg->payload ? content_type : "", msrp_msg->payload ? "\r\n\r\n" : ""); - len = strlen(buf); + if (ret < 0 || (switch_size_t)ret >= sizeof(buf)) { + goto too_large; + } + + len = ret; if (msrp_msg->payload) { - if (len + msrp_msg->payload_bytes >= MSRP_BUFF_SIZE) { - switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, ms->call_id, SWITCH_LOG_ERROR, "payload too large! %" SWITCH_SIZE_T_FMT "\n", len + msrp_msg->payload_bytes); - return SWITCH_STATUS_FALSE; + /* at least one byte is left after the header */ + room = sizeof(buf) - len; + + /* the payload and its CRLF, leaving room for the end-line */ + if (msrp_msg->payload_bytes + 2 >= room) { + goto too_large; } + memcpy(buf + len, msrp_msg->payload, msrp_msg->payload_bytes); len += msrp_msg->payload_bytes; - sprintf(buf + len, "\r\n"); + memcpy(buf + len, "\r\n", 2); len += 2; } - sprintf(buf + len, "-------%s$\r\n", transaction_id); - len += (10 + strlen(transaction_id)); + ret = snprintf(buf + len, sizeof(buf) - len, "-------%s$\r\n", transaction_id); + + if (ret < 0 || (switch_size_t)ret >= sizeof(buf) - len) { + goto too_large; + } + + len += ret; if (globals.debug) dump_buffer(buf, len, __LINE__, 1); return ms->csock ? msrp_socket_send(ms->csock, buf, &len) : SWITCH_STATUS_FALSE; + +too_large: + switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, ms->call_id, SWITCH_LOG_ERROR, "MSRP message does not fit %" SWITCH_SIZE_T_FMT " bytes, not sent\n", sizeof(buf)); + + return SWITCH_STATUS_FALSE; } SWITCH_DECLARE (switch_status_t) switch_msrp_perform_send(switch_msrp_session_t *ms, switch_msrp_msg_t *msrp_msg, const char *file, const char *func, int line) From 1dc4fd7df0eebf394ff1bf9787f39d33a1f24a21 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 23:22:36 +0500 Subject: [PATCH 08/10] [core] Fix MSRP leaks and error handling in the worker - Free MSRP messages and their headers on every path that drops them, including `uuid_msrp_send` and `mod_sofia`'s filtered text frames. - Handle a failed `SSL_new()` and a call without an MSRP session by closing the connection instead of asserting. - Treat a zero-length TLS read as the peer closing the connection. --- src/mod/endpoints/mod_sofia/mod_sofia.c | 3 ++ src/switch_msrp.c | 37 +++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 209c7ca4bc..16d22fa14e 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -1084,6 +1084,9 @@ static switch_status_t sofia_read_text_frame(switch_core_session_t *session, swi switch_msrp_msg_destroy(&msrp_msg); status = SWITCH_STATUS_SUCCESS; } else { + if (msrp_msg) { + switch_msrp_msg_destroy(&msrp_msg); + } rframe->datalen = 2; rframe->flags = SFF_CNG; *frame = rframe; diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 6b1c5bc982..3dc72a9936 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -679,6 +679,8 @@ SWITCH_DECLARE(switch_status_t) switch_msrp_msg_add_header(switch_msrp_msg_t *ms default: break; } + switch_safe_free(data); + return status; } @@ -1183,7 +1185,11 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "MSRP setup TLS %s\n", msrp_session->call_id); ssl = SSL_new(globals.ssl_client_ctx); - assert(ssl); + if (!ssl) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MSRP %s cannot create a TLS client session\n", msrp_session->call_id); + goto end; + } + csock->ssl = ssl; SSL_set_fd(ssl, sockdes); @@ -1241,7 +1247,11 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) switch_assert(sockdes != SWITCH_SOCK_INVALID); ssl = SSL_new(globals.ssl_ctx); - assert(ssl); + if (!ssl) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MSRP cannot create a TLS server session\n"); + goto end; + } + csock->ssl = ssl; SSL_set_fd(ssl, sockdes); @@ -1322,7 +1332,12 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) } msrp_session = switch_core_media_get_msrp_session(session); - switch_assert(msrp_session); + if (!msrp_session) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Session %s has no MSRP session\n", uuid); + switch_core_session_rwunlock(session); + goto end; + } + msrp_session->csock = csock; msrp_session->running = 1; @@ -1339,8 +1354,9 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "read bytes: %" SWITCH_SIZE_T_FMT "\n", len); if (len == 0) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "read bytes: %" SWITCH_SIZE_T_FMT "\n", len); - continue; + /* only a TLS read reports success with no data, and only once the peer has closed */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MSRP connection closed by peer\n"); + break; } if (helper->debug) dump_buffer(buf, (p - buf) + len, __LINE__, 0); @@ -1454,6 +1470,10 @@ end: if (msrp_session) msrp_session->running = 0; + if (msrp_msg) { + switch_msrp_msg_destroy(&msrp_msg); + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "MSRP worker down %s\n", msrp_session ? msrp_session->call_id : "!"); return NULL; @@ -1725,6 +1745,7 @@ SWITCH_DECLARE(void) switch_msrp_msg_destroy(switch_msrp_msg_t **msg) } switch_safe_free(msrp_msg->payload); + switch_safe_free(*msg); *msg = NULL; } @@ -1794,7 +1815,7 @@ SWITCH_STANDARD_APP(msrp_recv_file_function) } } - switch_safe_free(msrp_msg); + switch_msrp_msg_destroy(&msrp_msg); } switch_file_close(fd); @@ -1890,12 +1911,14 @@ SWITCH_STANDARD_API(uuid_msrp_send_function) if (!(msession = switch_core_session_locate(argv[0]))) { stream->write_function(stream, "-ERR Usage: cannot locate session.\n"); + switch_safe_free(mycmd); return SWITCH_STATUS_SUCCESS; } if (!(msrp_session = switch_core_media_get_msrp_session(msession))) { stream->write_function(stream, "-ERR No msrp_session.\n"); switch_core_session_rwunlock(msession); + switch_safe_free(mycmd); return SWITCH_STATUS_SUCCESS; } @@ -1906,10 +1929,12 @@ SWITCH_STANDARD_API(uuid_msrp_send_function) switch_msrp_msg_destroy(&msrp_msg); stream->write_function(stream, "+OK message sent\n"); switch_core_session_rwunlock(msession); + switch_safe_free(mycmd); return SWITCH_STATUS_SUCCESS; error: stream->write_function(stream, "-ERR Usage: uuid_msrp_send msg\n"); + switch_safe_free(mycmd); return SWITCH_STATUS_SUCCESS; } From 8721a8f6547aefee6a29d805b7ad5b8babd69794 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 23:23:38 +0500 Subject: [PATCH 09/10] [core] Harden MSRP message parsing and sending - Reject a request with an empty transaction id, and a body that has no end-line delimiter to frame it. - Keep the receive buffer NUL-terminated after every read. - Free the setup message when an outbound connection fails before sending it. - Refuse to send a message without a To-Path. --- src/switch_msrp.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 3dc72a9936..17d8a25edf 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -718,6 +718,12 @@ static switch_msrp_msg_t *msrp_parse_headers(char *start, int len, switch_msrp_m while(*q && q < end && *q != ' ') q++; + if (q == p && q < end) { + /* empty transaction id: there is no end-line delimiter to frame the body */ + msrp_msg->state = MSRP_ST_ERROR; + goto done; + } + if (q > p) { *q = '\0'; switch_msrp_msg_add_header(msrp_msg, MSRP_H_TRASACTION_ID, "%s", p); @@ -910,13 +916,19 @@ static switch_msrp_msg_t *msrp_parse_buffer(char *buf, int len, switch_msrp_msg_ return msrp_msg; } + if (zstr(msrp_msg->delimiter)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MSRP body without a delimiter\n"); + msrp_msg->state = MSRP_ST_ERROR; + + return msrp_msg; + } + if (msrp_msg->range_star) { /* the * case */ /*hope we can find the delimiter at the end*/ int dlen; char *delim_pos = NULL; switch_size_t payload_bytes; - switch_assert(msrp_msg->delimiter); dlen = strlen(msrp_msg->delimiter); /* Need room for the trailing delimiter framing; a shorter @@ -1092,12 +1104,13 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) worker_helper_t *helper = (worker_helper_t *) obj; switch_msrp_client_socket_t *csock = &helper->csock; switch_memory_pool_t *pool = helper->pool; - char buf[MSRP_BUFF_SIZE]; + char buf[MSRP_BUFF_SIZE + 1]; /* received data is NUL-terminated in the extra byte */ char *p; char *last_p; switch_size_t len = MSRP_BUFF_SIZE; switch_status_t status; switch_msrp_msg_t *msrp_msg = NULL; + switch_msrp_msg_t *setup_msg = NULL; char uuid[128] = { 0 }; switch_msrp_session_t *msrp_session = NULL; int sanity = 10; @@ -1106,12 +1119,12 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) if (client_mode) { switch_sockaddr_t *sa = NULL; - switch_msrp_msg_t *setup_msg = switch_msrp_msg_create(); const char *remote_ip = NULL; switch_port_t remote_port = 0; char *dup = NULL; char *p = NULL; + setup_msg = switch_msrp_msg_create(); switch_assert(setup_msg); switch_assert(helper->msrp_session); msrp_session = helper->msrp_session; @@ -1287,6 +1300,7 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) } if (status == SWITCH_STATUS_SUCCESS) { + buf[len] = '\0'; msrp_msg = msrp_parse_buffer(buf, len, NULL, pool); switch_assert(msrp_msg); } else { @@ -1362,6 +1376,8 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) if (helper->debug) dump_buffer(buf, (p - buf) + len, __LINE__, 0); again: + /* p + len is the end of the received data on every path that reaches here */ + p[len] = '\0'; msrp_msg = msrp_parse_buffer(last_p, p - last_p + len, msrp_msg, pool); switch_assert(msrp_msg); @@ -1474,6 +1490,11 @@ end: switch_msrp_msg_destroy(&msrp_msg); } + /* set only when a client connection failed before its setup SEND */ + if (setup_msg) { + switch_msrp_msg_destroy(&setup_msg); + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "MSRP worker down %s\n", msrp_session ? msrp_session->call_id : "!"); return NULL; @@ -1597,6 +1618,12 @@ static switch_status_t switch_msrp_do_send(switch_msrp_session_t *ms, switch_msr return SWITCH_STATUS_SUCCESS; } + if (!to_path) { + switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, ms->call_id, SWITCH_LOG_WARNING, "NO TO PATH\n"); + + return SWITCH_STATUS_FALSE; + } + if (zstr(content_type)) { content_type = "text/plain"; } From 74f63975fdb94bde3a05ec75001ae9ae055b511c Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Tue, 17 Nov 2026 17:48:10 +0500 Subject: [PATCH 10/10] [core] Do not start an MSRP client without a remote path When the remote side offers `a=setup:passive`, negotiation started the MSRP client whether or not the offer carried an `a=path`, and the client then asserted on the missing `remote_path`. Start the client only when a path was negotiated. In `msrp_worker()`, also stop with an error instead of asserting when there is no path, and when the path gives no host or port. --- src/switch_core_media.c | 6 +++++- src/switch_msrp.c | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/switch_core_media.c b/src/switch_core_media.c index f8825aab74..760bb41c13 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -5101,7 +5101,11 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s smh->msrp_session->secure ? "s" : "", ip, smh->msrp_session->local_port, smh->msrp_session->call_id); - switch_msrp_start_client(smh->msrp_session); + if (zstr(smh->msrp_session->remote_path)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "MSRP remote is passive but sent no path, not connecting\n"); + } else { + switch_msrp_start_client(smh->msrp_session); + } } switch_core_session_start_text_thread(session); diff --git a/src/switch_msrp.c b/src/switch_msrp.c index 17d8a25edf..90028ae539 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1130,7 +1130,11 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) msrp_session = helper->msrp_session; msrp_session->running = 1; - switch_assert(msrp_session->remote_path); + if (zstr(msrp_session->remote_path)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MSRP %s has no remote path to connect to\n", msrp_session->call_id); + goto end; + } + dup = switch_core_strdup(pool, msrp_session->remote_path); switch_assert(dup); @@ -1155,8 +1159,9 @@ static void *SWITCH_THREAD_FUNC msrp_worker(switch_thread_t *thread, void *obj) } } - if (!remote_ip || remote_port <= 0) { + if (zstr(remote_ip) || remote_port <= 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error get remote MSRP ip:port from path: [%s]\n", msrp_session->remote_path); + goto end; } if (switch_sockaddr_info_get(&sa, remote_ip, SWITCH_UNSPEC, remote_port, 0, pool) != SWITCH_STATUS_SUCCESS) {