From 8931f1763af3dbd25ab8fe22f4fa277375bd657d Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Mon, 18 Mar 2019 18:12:10 +0100 Subject: a_iface_bssap: check bssmap length field At the moment the length field of the bssmap header is not parsed. Instead the length is computed out of the known header length and the number of bytes received. This is prone to error, lets make sure that extranous data at the end of a message is ignored by parsing the bssmap length correctly. Change-Id: I3b89dd5a66ec83b03860b58b6b8eb58007f433a4 Related: OS#3806 --- src/libmsc/a_iface_bssap.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libmsc/a_iface_bssap.c b/src/libmsc/a_iface_bssap.c index cb245b805..3e33c46c1 100644 --- a/src/libmsc/a_iface_bssap.c +++ b/src/libmsc/a_iface_bssap.c @@ -703,6 +703,33 @@ static int rx_dtap(const struct osmo_sccp_user *scu, const struct a_conn_info *a return 0; } +/* Extract and verify the length information from the BSSMAP header. */ +void bssmap_msg_verify_len(struct msgb *msg) +{ + unsigned int expected_len; + unsigned int calculated_len; + struct bssmap_header *bssmap_header; + + bssmap_header = (struct bssmap_header *)msg->l2h; + + calculated_len = msgb_l3len(msg); + expected_len = bssmap_header->length; + + /* In case of contradictory length information, decide for the + * shorter length */ + if (calculated_len > expected_len) { + LOGP(DBSSAP, LOGL_NOTICE, + "BSSMAP message contains extra data, expected %u bytes, got %u bytes, truncated\n", + expected_len, calculated_len); + msgb_l3trim(msg, expected_len); + } else if (calculated_len < expected_len) { + LOGP(DMSC, LOGL_NOTICE, + "Short BSSMAP message, expected %u bytes, got %u bytes\n", + expected_len, calculated_len); + msgb_l3trim(msg, calculated_len); + } +} + /* Handle incoming connection oriented messages. No ownership of 'msg' is passed on! */ int a_sccp_rx_dt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) { @@ -718,6 +745,7 @@ int a_sccp_rx_dt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_in switch (msg->l2h[0]) { case BSSAP_MSG_BSS_MANAGEMENT: msg->l3h = &msg->l2h[sizeof(struct bssmap_header)]; + bssmap_msg_verify_len(msg); return rx_bssmap(scu, a_conn_info, msg); case BSSAP_MSG_DTAP: return rx_dtap(scu, a_conn_info, msg); -- cgit v1.2.3