Commit 512443322a for freeswitch.com

commit 512443322af0405e41bd9e2aa9df27855aca3995
Author: Dmitry Verenitsin <morbit85@gmail.com>
Date:   Wed Nov 18 22:23:56 2026 +0500

    Merge commit from fork

    `switch_rtp_add_crypto_key()` allocated each MKI id buffer with
    `sizeof(key_material->mki_size)`, a constant 4 bytes, then copied
    `mki_size` bytes into it. That length comes from the remote `a=crypto`
    attribute and was only checked against a lower bound. The same copy
    read `mki_size` bytes starting at the 4-byte `mki_id` field.

    Reject an MKI length outside 1..`SRTP_MAX_MKI_LEN`, allocate the id
    buffer at that length, and copy no more than the width of `mki_id`,
    leaving the remaining bytes zero. MKI lengths of 1 to 4 keep the bytes
    they carry today.

diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 34567e4620..24fcc7db13 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -4398,8 +4398,10 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess

 		while (key_material && (mki_idx < *key_material_n)) {

-			if (key_material->mki_size < 1) {
-				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size at MKI %d\n", mki_idx);
+			if (key_material->mki_size < 1 || key_material->mki_size > SRTP_MAX_MKI_LEN) {
+				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size %u at MKI %d (allowed 1..%d)\n",
+								  key_material->mki_size, mki_idx, SRTP_MAX_MKI_LEN);
+
 				return SWITCH_STATUS_FALSE;
 			}

@@ -4409,7 +4411,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess
 			}

 			/* Setup MKI. */
-			mki->mki_id = switch_core_alloc(rtp_session->pool, sizeof(key_material->mki_size));
+			mki->mki_id = switch_core_alloc(rtp_session->pool, key_material->mki_size);
 			if (!mki->mki_id) {
 				return SWITCH_STATUS_FALSE;
 			}
@@ -4419,7 +4421,9 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess
 				return SWITCH_STATUS_FALSE;
 			}

-			memcpy(mki->mki_id, &key_material->mki_id, key_material->mki_size);
+			/* The id lives in an unsigned int: copy no more than its width, bytes past it stay zero. */
+			memcpy(mki->mki_id, &key_material->mki_id,
+				   key_material->mki_size < sizeof(key_material->mki_id) ? key_material->mki_size : sizeof(key_material->mki_id));
 			mki->mki_size = key_material->mki_size;
 			memcpy(mki->key, key_material->raw_key, keysalt_len);