ws1004 has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/44138?usp=email ) Change subject: gb: ns2: clamp the embedded NS PDU to the msgb, not just to the MTU ...................................................................... gb: ns2: clamp the embedded NS PDU to the msgb, not just to the MTU reject_status_msg() embeds the offending PDU into the NS-STATUS reply without any length check, and ns2_tx_status() clamps it against nsvc->bind->mtu only. For an NS-over-IP bind that MTU is 65527 (gprs_ns2_udp.c:344), while the STATUS msgb from ns2_msgb_alloc() offers 3052 usable bytes (NS_ALLOC_SIZE 3072 minus NS_ALLOC_HEADROOM 20), of which 3048 remain after the NS header and the Cause IE. A received PDU of 3046 bytes or more therefore makes msgb_tvlv_put() -> msgb_put() hit MSGB_ABORT, which aborts the process. No NS-VC, handshake or credentials are needed to reach either site. Clamp against the msgb tailroom in both places, and use a saturating clamp instead of a subtraction on an unsigned (the old form could underflow if the MTU were smaller than the message built so far). bssgp_tx_status() in the same tree already does this correctly via OSMO_MIN(..., msgb_tailroom(msg) - 3). Fixes: 6a161497cfd8cacfef10c2e81cff85a45447e107 Change-Id: Iac1c5c15875f30f34d0f7e9fc74264530a02b6ae --- M src/gb/gprs_ns2.c M src/gb/gprs_ns2_message.c 2 files changed, 28 insertions(+), 8 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/38/44138/1 diff --git a/src/gb/gprs_ns2.c b/src/gb/gprs_ns2.c index 4e496c1..773498a 100644 --- a/src/gb/gprs_ns2.c +++ b/src/gb/gprs_ns2.c @@ -751,10 +751,20 @@ case NS_CAUSE_PDU_INCOMP_PSTATE: case NS_CAUSE_PROTO_ERR_UNSPEC: case NS_CAUSE_INVAL_ESSENT_IE: - case NS_CAUSE_MISSING_ESSENT_IE: - msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), - orig_msg->l2h); + case NS_CAUSE_MISSING_ESSENT_IE: { + /* The original PDU can be larger than what is left in our own + * msgb (NS_ALLOC_SIZE), so clamp it. TvLV header is 3 bytes. */ + int avail = (int)msgb_tailroom(msg) - 3; + unsigned int embed_len = msgb_l2len(orig_msg); + + if (avail < 0) + avail = 0; + if (embed_len > (unsigned int)avail) + embed_len = (unsigned int)avail; + if (embed_len) + msgb_tvlv_put(msg, NS_IE_PDU, embed_len, orig_msg->l2h); break; + } default: break; } diff --git a/src/gb/gprs_ns2_message.c b/src/gb/gprs_ns2_message.c index de63b7a..3431c0c 100644 --- a/src/gb/gprs_ns2_message.c +++ b/src/gb/gprs_ns2_message.c @@ -458,12 +458,22 @@ case NS_CAUSE_INVAL_ESSENT_IE: case NS_CAUSE_MISSING_ESSENT_IE: /* Section 9.2.7.2: Static conditions for NS PDU */ - /* ensure the PDU doesn't exceed the MTU */ + /* Ensure the embedded PDU fits both into the link MTU and into + * our own message buffer. bind->mtu can be far larger than + * NS_ALLOC_SIZE (65527 for NS-over-IP), so clamping against the + * MTU alone lets msgb_put() hit MSGB_ABORT. */ orig_len = msgb_l2len(orig_msg); - max_orig_len = msgb_length(msg) + TVLV_GROSS_LEN(orig_len); - if (max_orig_len > nsvc->bind->mtu) - orig_len -= max_orig_len - nsvc->bind->mtu; - msgb_tvlv_put(msg, NS_IE_PDU, orig_len, orig_msg->l2h); + max_orig_len = msgb_tailroom(msg); + if (nsvc->bind->mtu <= msgb_length(msg)) + max_orig_len = 0; + else if (nsvc->bind->mtu - msgb_length(msg) < max_orig_len) + max_orig_len = nsvc->bind->mtu - msgb_length(msg); + /* TvLV header: 1 byte tag + 2 bytes length */ + max_orig_len = (max_orig_len > 3) ? max_orig_len - 3 : 0; + if (orig_len > max_orig_len) + orig_len = max_orig_len; + if (orig_len) + msgb_tvlv_put(msg, NS_IE_PDU, orig_len, orig_msg->l2h); break; case NS_CAUSE_BVCI_UNKNOWN: /* Section 9.2.7.3: Static conditions for BVCI */ -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/44138?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newchange Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: Iac1c5c15875f30f34d0f7e9fc74264530a02b6ae Gerrit-Change-Number: 44138 Gerrit-PatchSet: 1 Gerrit-Owner: ws1004 <ws1004@kakao.com>