[M] Change in libosmocore[master]: gsm: fix one-byte overflow in BCD number encoders
pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/44142?usp=email ) Change subject: gsm: fix one-byte overflow in BCD number encoders ...................................................................... gsm: fix one-byte overflow in BCD number encoders gsm48_encode_bcd_number() checked only the L octet against max_len, the size of bcd_lv, but it also writes the L octet itself, so a number one or two digits over the spec maximum wrote one byte past the buffer. Count the L octet in the check, and compute the length in an int so that it cannot wrap. Add tests. osmo-sgsn passes max_len 8 for the 9-byte IMEISV, which needs Change-Id Ied06aa05ee6850ec0df34a51cdce6df12e0b594c first. Fixes: OS#7126 Change-Id: I99521d30896f3d58ecfb28de290bce7e31388fca --- M src/gsm/gsm48_ie.c M tests/gsm0408/gsm0408_test.c M tests/gsm0408/gsm0408_test.ok M tests/gsm0808/gsm0808_test.c M tests/gsm0808/gsm0808_test.ok 5 files changed, 140 insertions(+), 3 deletions(-) Approvals: pespin: Looks good to me, approved Jenkins Builder: Verified jolly: Looks good to me, but someone else must approve diff --git a/src/gsm/gsm48_ie.c b/src/gsm/gsm48_ie.c index e33e672..866e0da 100644 --- a/src/gsm/gsm48_ie.c +++ b/src/gsm/gsm48_ie.c @@ -151,16 +151,22 @@ int h_len, const char *input) { int in_len = strlen(input); + int len; int i; uint8_t *bcd_cur = bcd_lv + 1 + h_len; /* two digits per byte, plus type byte */ - bcd_lv[0] = in_len/2 + h_len; + len = in_len/2 + h_len; if (in_len % 2) - bcd_lv[0]++; + len++; - if (bcd_lv[0] > max_len) + /* the L octet plus len octets must fit into bcd_lv */ + if (1 + len > max_len) return -EIO; + /* Lengths spanning multiple bytes cannot be encoded */ + if (len > 0xff) + return -EIO; + bcd_lv[0] = len; for (i = 0; i < in_len; i++) { int rc = asc_to_bcd(input[i]); diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c index 5566b7c..401d8e3 100644 --- a/tests/gsm0408/gsm0408_test.c +++ b/tests/gsm0408/gsm0408_test.c @@ -1496,6 +1496,28 @@ .enc_buf_lim = 9, }, { + .test_name = "16-digit number, fills the buffer (OS#7126)", + + /* Encoding test (L octet plus 8 octets) */ + .enc_ascii = "1234567890123456", + .enc_hex = "082143658709214365", + .enc_rc = 9, + + /* Buffer length limitations */ + .enc_buf_lim = 9, + }, + { + .test_name = "17-digit number, one octet over the buffer (OS#7126)", + + /* Encoding test (not enough room in buffer) */ + .enc_ascii = "12345678901234567", + .enc_hex = "", /* nothing */ + .enc_rc = -EIO, + + /* Buffer length limitations */ + .enc_buf_lim = 9, + }, + { .test_name = "LV incorrect length", .dec_hex = "05214365", /* should be 0x03 */ .dec_ascii = "(none)", @@ -1587,6 +1609,64 @@ printf("\n"); } +static void test_bcd_number_ie(const char *name, + int (*enc)(struct msgb *msg, const struct gsm_mncc_number *num), + unsigned int num_digits, bool octet_3a) +{ + struct gsm_mncc_number num = { .screen = octet_3a ? 1 : 0 }; + struct msgb *msg = msgb_alloc(128, __func__); + int rc; + + OSMO_ASSERT(msg); + OSMO_ASSERT(num_digits < sizeof(num.number)); + memset(num.number, '1', num_digits); + + rc = enc(msg, &num); + printf("- %s, %u digits%s: rc=%d IE='%s'\n", name, num_digits, + octet_3a ? " (with octet 3a)" : "", rc, + osmo_hexdump_nospc(msgb_data(msg), msgb_length(msg))); + + msgb_free(msg); +} + +/* OS#7126: neither the L octet nor the IE buffers may overflow */ +static void test_bcd_number_encode_limits(void) +{ + /* 508 digits fill buf exactly (L = 254), 509 need one octet more + * (L = 255), and 511 give L = 256, which must not wrap to 0 */ + static const unsigned int num_digits[] = { 508, 509, 511 }; + char digits[512]; + uint8_t buf[255]; + unsigned int i; + int rc; + + printf("BCD number encoding limits test\n"); + + for (i = 0; i < ARRAY_SIZE(num_digits); i++) { + memset(digits, '1', num_digits[i]); + digits[num_digits[i]] = '\0'; + rc = gsm48_encode_bcd_number(buf, sizeof(buf), 0, digits); + printf("- %u digits, max_len=%zu: rc=%d\n", num_digits[i], sizeof(buf), rc); + } + + /* TS 24.008 10.5.4.9: Calling party BCD number, at most 14 octets */ + test_bcd_number_ie("Calling", gsm48_encode_calling, 22, false); + test_bcd_number_ie("Calling", gsm48_encode_calling, 23, false); + test_bcd_number_ie("Calling", gsm48_encode_calling, 20, true); + test_bcd_number_ie("Calling", gsm48_encode_calling, 21, true); + /* TS 24.008 10.5.4.13: Connected number, at most 14 octets */ + test_bcd_number_ie("Connected", gsm48_encode_connected, 22, false); + test_bcd_number_ie("Connected", gsm48_encode_connected, 23, false); + /* TS 24.008 10.5.4.21b: Redirecting party BCD number, at most 19 octets */ + test_bcd_number_ie("Redirecting", gsm48_encode_redirecting, 32, false); + test_bcd_number_ie("Redirecting", gsm48_encode_redirecting, 30, true); + test_bcd_number_ie("Redirecting", gsm48_encode_redirecting, 31, true); + /* TS 24.008 10.5.4.7: Called party BCD number, limited by lv[18] */ + test_bcd_number_ie("Called", gsm48_encode_called, 32, false); + + printf("\n"); +} + struct { int range; int arfcns_num; @@ -2026,6 +2106,7 @@ test_mid_decode_zero_length(); test_struct_mobile_identity(); test_bcd_number_encode_decode(); + test_bcd_number_encode_limits(); test_ra_cap(); test_lai_encode_decode(); test_osmo_routing_area_id(); diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok index 3580591..ae2b0a5 100644 --- a/tests/gsm0408/gsm0408_test.ok +++ b/tests/gsm0408/gsm0408_test.ok @@ -229,6 +229,14 @@ - Decoding HEX (buffer limit=16) '0a21436587092143658709'... - Expected: (rc=-28) '123456789012345' - Actual: (rc=-28) '123456789012345' +- Running test: 16-digit number, fills the buffer (OS#7126) + - Encoding ASCII (buffer limit=9) '1234567890123456'... + - Expected: (rc=9) '082143658709214365' + - Actual: (rc=9) '082143658709214365' +- Running test: 17-digit number, one octet over the buffer (OS#7126) + - Encoding ASCII (buffer limit=9) '12345678901234567'... + - Expected: (rc=-5) '' + - Actual: (rc=-5) '' - Running test: LV incorrect length - Decoding HEX (buffer limit=0) '05214365'... - Expected: (rc=-22) '(none)' @@ -245,6 +253,21 @@ - Expected: (rc=-28) '123' - Actual: (rc=-28) '123' +BCD number encoding limits test +- 508 digits, max_len=255: rc=255 +- 509 digits, max_len=255: rc=-5 +- 511 digits, max_len=255: rc=-5 +- Calling, 22 digits: rc=0 IE='5c0c801111111111111111111111' +- Calling, 23 digits: rc=-5 IE='' +- Calling, 20 digits (with octet 3a): rc=0 IE='5c0c008111111111111111111111' +- Calling, 21 digits (with octet 3a): rc=-5 IE='' +- Connected, 22 digits: rc=0 IE='4c0c801111111111111111111111' +- Connected, 23 digits: rc=-5 IE='' +- Redirecting, 32 digits: rc=0 IE='74118011111111111111111111111111111111' +- Redirecting, 30 digits (with octet 3a): rc=0 IE='74110081111111111111111111111111111111' +- Redirecting, 31 digits (with octet 3a): rc=-5 IE='' +- Called, 32 digits: rc=0 IE='5e118011111111111111111111111111111111' + Constructed RA: 077-121-666-5 MCC+MNC in BCD: 70 17 21 diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c index 8ebbd4b..cafea50 100644 --- a/tests/gsm0808/gsm0808_test.c +++ b/tests/gsm0808/gsm0808_test.c @@ -477,6 +477,29 @@ OSMO_ASSERT(rc == 5); } +/* OS#7126: TS 48.008 3.2.2.101 allows at most 10 octets of MSISDN digits */ +static void test_gsm0808_enc_msisdn_limits(void) +{ + static const char *msisdns[] = { + "12345678901234567890", /* 20 digits, the maximum */ + "123456789012345678901", /* 21 digits, too long */ + }; + unsigned int i; + + printf("Testing encoding MSISDN length limits\n"); + + for (i = 0; i < ARRAY_SIZE(msisdns); i++) { + struct msgb *msg = msgb_alloc(64, __func__); + uint8_t rc; + + OSMO_ASSERT(msg); + rc = gsm0808_enc_msisdn(msg, msisdns[i]); + printf("- %zu digits: rc=%u IE='%s'\n", strlen(msisdns[i]), rc, + osmo_hexdump_nospc(msgb_data(msg), msgb_length(msg))); + msgb_free(msg); + } +} + static void test_create_ass(void) { static const uint8_t res1[] = @@ -2822,6 +2845,7 @@ test_dec_confusion(); test_dec_perform_location_report_sys5891(); + test_gsm0808_enc_msisdn_limits(); printf("Done\n"); return EXIT_SUCCESS; diff --git a/tests/gsm0808/gsm0808_test.ok b/tests/gsm0808/gsm0808_test.ok index 2329fcd..b3d4cae 100644 --- a/tests/gsm0808/gsm0808_test.ok +++ b/tests/gsm0808/gsm0808_test.ok @@ -917,4 +917,7 @@ Diagnostics error bit location 15 (Reserved value) Diagnostics message that provoked the error: 00 03 25 03 25 Testing decoding Perform Location Report SYS#5891 +Testing encoding MSISDN length limits +- 20 digits: rc=12 IE='7b0a21436587092143658709' +- 21 digits: rc=0 IE='' Done -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/44142?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: I99521d30896f3d58ecfb28de290bce7e31388fca Gerrit-Change-Number: 44142 Gerrit-PatchSet: 4 Gerrit-Owner: bucketking657 <joshuashilts@gmail.com> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de> Gerrit-Reviewer: jolly <andreas@eversberg.eu> Gerrit-Reviewer: laforge <laforge@osmocom.org> Gerrit-Reviewer: pespin <pespin@sysmocom.de>
participants (1)
-
pespin