bucketking657 has uploaded this change for review.

View Change

gsm: fix division by zero in osmo_nri_v_limit_by_ranges()

If no NRI range fits within nri_bitlen (empty list, or all ranges above
2^nri_bitlen - 1), total_values is 0 and the modulo raises SIGFPE.
Return -1 instead.

Fixes: OS#7146
Change-Id: Ib4571d2a8e83ee94acb75a9d9d695048ee1bf370
---
M src/gsm/gsm23236.c
M tests/gsm23236/gsm23236_test.c
M tests/gsm23236/gsm23236_test.ok
3 files changed, 41 insertions(+), 0 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/87/44187/1
diff --git a/src/gsm/gsm23236.c b/src/gsm/gsm23236.c
index a80ecf4..f5c40bc 100644
--- a/src/gsm/gsm23236.c
+++ b/src/gsm/gsm23236.c
@@ -115,6 +115,10 @@
total_values += OSMO_MIN(range_max, range->last) - range->first + 1;
}

+ /* No range value fits within nri_bitlen, so there is nothing to pick from. */
+ if (!total_values)
+ return -1;
+
/* Modulo the given NRI value by that, and pick that nth value from the given ranges.
* (nri_ranges is pretty much guaranteed to be sorted and range_max checks thus would no longer be needed, but
* just check them anyway.) */
diff --git a/tests/gsm23236/gsm23236_test.c b/tests/gsm23236/gsm23236_test.c
index 6cbdbeb..a7c5936 100644
--- a/tests/gsm23236/gsm23236_test.c
+++ b/tests/gsm23236/gsm23236_test.c
@@ -594,6 +594,32 @@
}
}

+void test_nri_limit_by_ranges_no_fit(void)
+{
+ struct osmo_nri_ranges *nri_ranges = osmo_nri_ranges_alloc(ctx);
+ printf("\n%s()\n", __func__);
+
+#define LIMIT_NO_FIT(BITLEN) do { \
+ int16_t nri_v = 42; \
+ int rc = osmo_nri_v_limit_by_ranges(&nri_v, nri_ranges, BITLEN); \
+ printf("osmo_nri_v_limit_by_ranges(42, bitlen %u) -> nri_v=%d rc=%d", BITLEN, nri_v, rc); \
+ if (rc < 0 && nri_v == 42) { \
+ printf(" ok\n"); \
+ } else { \
+ printf(" ERROR: expected nri_v=42 rc<0\n"); \
+ ok = false; \
+ } \
+ } while (0)
+
+ printf("empty list:\n");
+ LIMIT_NO_FIT(8);
+
+ ADD(300, 400);
+ printf("all ranges above the bitlen:\n");
+ LIMIT_NO_FIT(8);
+ talloc_free(nri_ranges);
+}
+
int main(int argc, char **argv)
{
ctx = talloc_named_const(NULL, 0, "nri_test");
@@ -603,6 +629,7 @@
test_nri_range_validate();
test_nri_list();
test_nri_limit_by_ranges();
+ test_nri_limit_by_ranges_no_fit();

talloc_free(ctx);
if (!ok) {
diff --git a/tests/gsm23236/gsm23236_test.ok b/tests/gsm23236/gsm23236_test.ok
index 22630bf..d436e6d 100644
--- a/tests/gsm23236/gsm23236_test.ok
+++ b/tests/gsm23236/gsm23236_test.ok
@@ -511,4 +511,14 @@
osmo_tmsi_nri_v_limit_by_ranges(0x00110000, 8) -> tmsi=0x00200000 nri_v=32 rc=0 ok
osmo_tmsi_nri_v_limit_by_ranges(0x00120000, 8) -> tmsi=0x000a0000 nri_v=10 rc=0 ok

+test_nri_limit_by_ranges_no_fit()
+empty list:
+osmo_nri_v_limit_by_ranges(42, bitlen 8) -> nri_v=42 rc=-1 ok
+osmo_nri_ranges_add(300, 400) -> 0
+nri_ranges = {
+ { 300, 400 },
+};
+all ranges above the bitlen:
+osmo_nri_v_limit_by_ranges(42, bitlen 8) -> nri_v=42 rc=-1 ok
+
pass

To view, visit change 44187. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ib4571d2a8e83ee94acb75a9d9d695048ee1bf370
Gerrit-Change-Number: 44187
Gerrit-PatchSet: 1
Gerrit-Owner: bucketking657 <joshuashilts@gmail.com>