diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-05-16 20:15:20 +0200 |
---|---|---|
committer | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-05-17 12:13:04 +0200 |
commit | dab4db0217898007496ef9639a6d3f33d6c3873a (patch) | |
tree | b5e9c41c92b3f5ada0921f42eea172ffb6e0c000 /src/gsm | |
parent | 16e205bfb2e86685ba5abf77d61dc3e8c39ee568 (diff) |
gsm: kasumi: Fix dynamic-stack-buffer-overflow on out buffers not multiple of 64 bits
Fixes following AddressSanitizer report during gea_test run with gcc
8.1.0:
==8899==ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7ffc5f1719bb at pc 0x7fe574adc5fe bp 0x7ffc5f171460 sp 0x7ffc5f171450
WRITE of size 1 at 0x7ffc5f1719bb thread T0
#0 0x7fe574adc5fd in osmo_store64be_ext ../../include/osmocom/core/bit64gen.h:75
#1 0x7fe574adc649 in osmo_store64be ../../include/osmocom/core/bit64gen.h:104
#2 0x7fe574ade936 in _kasumi_kgcore libosmocore/src/gsm/kasumi.c:186
#3 0x7fe574ae2532 in gea4 libosmocore/src/gsm/gea.c:44
#4 0x7fe574ae266c in gea3 libosmocore/src/gsm/gea.c:60
#5 0x7fe574a9b616 in gprs_cipher_run libosmocore/src/gsm/gprs_cipher_core.c:95
#6 0x56422d3fb2ee in test_gea libosmocore/tests/gea/gea_test.c:29
#7 0x56422d3fb506 in main libosmocore/tests/gea/gea_test.c:49
#8 0x7fe5730f406a in __libc_start_main (/usr/lib/libc.so.6+0x2306a)
#9 0x56422d3fadf9 in _start (libosmocore/tests/gea/.libs/lt-gea_test+0x1df9)
The kasumi_test is updated to calculate the entire array of bits
according to expected result. Before this commit it worked by writing
the entire last 64bit block, and addressSanitizer cannot catch it
because the allocated buffer is 64bit aligned too.
Change-Id: I7b2a0224a3b5527d5a3ad7e17efc73081b63eac1
Diffstat (limited to 'src/gsm')
-rw-r--r-- | src/gsm/kasumi.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/gsm/kasumi.c b/src/gsm/kasumi.c index 7de5cd03..f93c002b 100644 --- a/src/gsm/kasumi.c +++ b/src/gsm/kasumi.c @@ -159,6 +159,7 @@ void _kasumi_key_expand(const uint8_t *key, uint16_t *KLi1, uint16_t *KLi2, uint } } +/* if cl is not multiple of 8 (a byte), co needs to be sized on the upper bound so the entire byte can be written. */ void _kasumi_kgcore(uint8_t CA, uint8_t cb, uint32_t cc, uint8_t cd, const uint8_t *ck, uint8_t *co, uint16_t cl) { uint16_t KLi1[8], KLi2[8], KOi1[8], KOi2[8], KOi3[8], KIi1[8], KIi2[8], KIi3[8], i; @@ -181,8 +182,16 @@ void _kasumi_kgcore(uint8_t CA, uint8_t cb, uint32_t cc, uint8_t cd, const uint8 _kasumi_key_expand(ck, KLi1, KLi2, KOi1, KOi2, KOi3, KIi1, KIi2, KIi3); /* i is a block counter */ - for (i = 0; i < cl / 64 + 1; i++) { + for (i = 0; i < cl / 64; i++) { BLK = _kasumi(A ^ i ^ BLK, KLi1, KLi2, KOi1, KOi2, KOi3, KIi1, KIi2, KIi3); osmo_store64be(BLK, co + (i * 8)); } + + /* Last 64-byte unaligned round. Take also into account last bits non-byte aligned. */ + uint8_t bytes_remain = cl/8%8 + (cl%8 ? 1 : 0); + if (bytes_remain) { + BLK = _kasumi(A ^ (cl / 64) ^ BLK, KLi1, KLi2, KOi1, KOi2, KOi3, KIi1, KIi2, KIi3); + BLK = BLK >> (8-bytes_remain)*8; + osmo_store64be_ext(BLK, co + (cl / 64 * 8), bytes_remain); + } } |