summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2013-07-03 09:56:53 +0200
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2013-07-03 10:00:06 +0200
commitcd252e356556e28cfc72d66f82fa87d12f3e5a2a (patch)
tree096f2103cef01dfaeebed3b3f9a6347c26d98791
parent476cf3337d79e5ed279aab33abb866ad48a39664 (diff)
gsm0408: Avoid unaligned memory access in gsm48_generate_mid_from_tmsi
The &buf[3] is unlikely to be aligned properly. Use memcpy instead of an assignment. Add a small testcase that verifies that I didn't mess up the conversion. Alignment trap: osmo-nitb (3293) PC=0x492b7094 Instr=0xe5803003 Address=0xbeb259db FSR 0x801
-rw-r--r--src/gsm/gsm48.c4
-rw-r--r--tests/gsm0408/gsm0408_test.c23
-rw-r--r--tests/gsm0408/gsm0408_test.ok1
3 files changed, 26 insertions, 2 deletions
diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index ea05d450..313d9a31 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -308,12 +308,12 @@ int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
{
- uint32_t *tptr = (uint32_t *) &buf[3];
+ uint32_t tmsi_be = htonl(tmsi);
buf[0] = GSM48_IE_MOBILE_ID;
buf[1] = GSM48_TMSI_LEN;
buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
- *tptr = htonl(tmsi);
+ memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
return 7;
}
diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c
index 077063be..b469b307 100644
--- a/tests/gsm0408/gsm0408_test.c
+++ b/tests/gsm0408/gsm0408_test.c
@@ -20,10 +20,13 @@
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
#include <osmocom/gsm/gsm48_ie.h>
+#include <osmocom/gsm/gsm48.h>
#include <osmocom/gsm/mncc.h>
+#include <osmocom/core/backtrace.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
@@ -127,7 +130,27 @@ static int test_bearer_cap()
return 0;
}
+static void test_mid_from_tmsi(void)
+{
+ static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd };
+
+
+ uint32_t tmsi = 0xAABBCCDD;
+ uint8_t buf[3 + sizeof(uint32_t)];
+
+ printf("Simple TMSI encoding test....");
+
+ memset(&buf, 0xFE, sizeof(buf));
+ gsm48_generate_mid_from_tmsi(buf, tmsi);
+
+ OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0);
+ printf("passed\n");
+}
+
int main(int argc, char **argv)
{
test_bearer_cap();
+ test_mid_from_tmsi();
+
+ return EXIT_SUCCESS;
}
diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok
index 5ce19e63..4a6d78b9 100644
--- a/tests/gsm0408/gsm0408_test.ok
+++ b/tests/gsm0408/gsm0408_test.ok
@@ -1,2 +1,3 @@
Test `CSD 9600/V.110/transparent' passed
Test `Speech, all codecs' passed
+Simple TMSI encoding test....passed