laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/44138?usp=email ) Change subject: gb: ns2: clamp embedded NS PDU to msgb tailroom ...................................................................... gb: ns2: clamp embedded NS PDU to msgb tailroom Per TS 48.016 9.2.7.2 an NS-STATUS PDU embeds the PDU that caused the error. reject_status_msg() copies it in without any length check, and ns2_tx_status() clamps it against nsvc->bind->mtu only, not against the space left in the reply msgb. 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, 32 insertions(+), 9 deletions(-) Approvals: Jenkins Builder: Verified laforge: Looks good to me, but someone else must approve pespin: Looks good to me, approved diff --git a/src/gb/gprs_ns2.c b/src/gb/gprs_ns2.c index 4e496c1..8d77e9e 100644 --- a/src/gb/gprs_ns2.c +++ b/src/gb/gprs_ns2.c @@ -751,10 +751,21 @@ 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. */ + const unsigned int avail = msgb_tailroom(msg); + unsigned int embed_len = msgb_l2len(orig_msg); + + if (avail < 3) + embed_len = 0; + else if (avail < 3 + embed_len) + embed_len = avail - 3; + + if (embed_len > 0) + 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..0fa2e26 100644 --- a/src/gb/gprs_ns2_message.c +++ b/src/gb/gprs_ns2_message.c @@ -426,7 +426,7 @@ struct msgb *msg = ns2_msgb_alloc(); struct gprs_ns_hdr *nsh; uint16_t encoded_nsvci; - unsigned int orig_len, max_orig_len; + unsigned int orig_len, avail; log_set_context(LOG_CTX_GB_NSE, nsvc->nse); log_set_context(LOG_CTX_GB_NSVC, nsvc); @@ -458,12 +458,24 @@ 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 */ + /* Trim available space to the MTU. bind->mtu can be far larger + * than NS_ALLOC_SIZE (65527 for NS-over-IP), so the msgb + * tailroom is the other bound we need. */ + avail = msgb_tailroom(msg); + if (msgb_length(msg) > nsvc->bind->mtu) + avail = 0; + else if (nsvc->bind->mtu - msgb_length(msg) < avail) + avail = nsvc->bind->mtu - msgb_length(msg); + + /* Figure out how many origin PDU bytes fit; TvLV header is 3 bytes: */ 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); + if (avail < 3) + orig_len = 0; + else if (avail < 3 + orig_len) + orig_len = avail - 3; + + if (orig_len > 0) + 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: merged Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: Iac1c5c15875f30f34d0f7e9fc74264530a02b6ae Gerrit-Change-Number: 44138 Gerrit-PatchSet: 3 Gerrit-Owner: ws1004 <ws1004@kakao.com> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: daniel <daniel@totalueberwachung.de> Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de> Gerrit-Reviewer: laforge <laforge@osmocom.org> Gerrit-Reviewer: lynxis <lynxis@fe80.eu> Gerrit-Reviewer: pespin <pespin@sysmocom.de>