From c2f316fead1da8728e8cd5028a49b88fc88fce67 Mon Sep 17 00:00:00 2001 From: richarddonkin Date: Wed, 22 Apr 2020 22:46:37 +0200 Subject: [PATCH] [cc1352,cc26xx] fix unaligned access of ext address in radio.c (#4617) This avoids misaligned memory access that was caused by the byte-aligned pointer being dereferenced as a uint64_t pointer. --- examples/platforms/cc1352/radio.c | 9 ++-- examples/platforms/cc2650/radio.c | 9 ++-- examples/platforms/cc2652/radio.c | 9 ++-- examples/platforms/utils/encoding.h | 68 +++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 examples/platforms/utils/encoding.h diff --git a/examples/platforms/cc1352/radio.c b/examples/platforms/cc1352/radio.c index b401fa8a6..a271f82d0 100644 --- a/examples/platforms/cc1352/radio.c +++ b/examples/platforms/cc1352/radio.c @@ -37,6 +37,7 @@ #include #include +#include #include /* to seed the CSMA-CA funciton */ #include #include @@ -1577,7 +1578,7 @@ otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx = rfCoreFindExtSrcMatchIdx(&extAddress); if (idx == CC1352_SRC_MATCH_NONE) @@ -1611,7 +1612,7 @@ otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddre OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx; otEXPECT_ACTION((idx = rfCoreFindExtSrcMatchIdx(&extAddress)) != CC1352_SRC_MATCH_NONE, @@ -1812,7 +1813,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA if (sState == cc1352_stateReceive) { otEXPECT(rfCoreExecuteAbortCmd() == CMDSTA_Done); - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); otEXPECT(rfCoreClearReceiveQueue(&sRxDataQueue) == CMDSTA_Done); otEXPECT(rfCoreSendReceiveCmd() == CMDSTA_Done); /* the interrupt from abort changed our state to sleep */ @@ -1820,7 +1821,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA } else if (sState != cc1352_stateTransmit) { - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); } exit: diff --git a/examples/platforms/cc2650/radio.c b/examples/platforms/cc2650/radio.c index 6d3628dc5..30124a51c 100644 --- a/examples/platforms/cc2650/radio.c +++ b/examples/platforms/cc2650/radio.c @@ -32,6 +32,7 @@ #include "cc2650_radio.h" #include #include +#include #include /* to seed the CSMA-CA funciton */ #include #include @@ -1528,7 +1529,7 @@ otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx = rfCoreFindExtSrcMatchIdx(&extAddress); if (idx == CC2650_SRC_MATCH_NONE) @@ -1562,7 +1563,7 @@ otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddre OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx; otEXPECT_ACTION((idx = rfCoreFindExtSrcMatchIdx(&extAddress)) != CC2650_SRC_MATCH_NONE, @@ -1761,7 +1762,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA if (sState == cc2650_stateReceive) { otEXPECT(rfCoreExecuteAbortCmd() == CMDSTA_Done); - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); otEXPECT(rfCoreClearReceiveQueue(&sRxDataQueue) == CMDSTA_Done); otEXPECT(rfCoreSendReceiveCmd() == CMDSTA_Done); /* the interrupt from abort changed our state to sleep */ @@ -1769,7 +1770,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA } else if (sState != cc2650_stateTransmit) { - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); } exit: diff --git a/examples/platforms/cc2652/radio.c b/examples/platforms/cc2652/radio.c index f6b471221..6dfba4286 100644 --- a/examples/platforms/cc2652/radio.c +++ b/examples/platforms/cc2652/radio.c @@ -37,6 +37,7 @@ #include #include +#include #include /* to seed the CSMA-CA funciton */ #include #include @@ -1558,7 +1559,7 @@ otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx = rfCoreFindExtSrcMatchIdx(&extAddress); if (idx == CC2652_SRC_MATCH_NONE) @@ -1592,7 +1593,7 @@ otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddre OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NONE; - uint64_t extAddress = *(uint64_t *)aExtAddress; + uint64_t extAddress = otEncodingReadUint64Le(aExtAddress->m8); uint8_t idx; otEXPECT_ACTION((idx = rfCoreFindExtSrcMatchIdx(&extAddress)) != CC2652_SRC_MATCH_NONE, @@ -1793,7 +1794,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA if (sState == cc2652_stateReceive) { otEXPECT(rfCoreExecuteAbortCmd() == CMDSTA_Done); - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); otEXPECT(rfCoreClearReceiveQueue(&sRxDataQueue) == CMDSTA_Done); otEXPECT(rfCoreSendReceiveCmd() == CMDSTA_Done); /* the interrupt from abort changed our state to sleep */ @@ -1801,7 +1802,7 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aA } else if (sState != cc2652_stateTransmit) { - sReceiveCmd.localExtAddr = *((uint64_t *)(aAddress)); + sReceiveCmd.localExtAddr = otEncodingReadUint64Le(aAddress->m8); } exit: diff --git a/examples/platforms/utils/encoding.h b/examples/platforms/utils/encoding.h new file mode 100644 index 000000000..55e3f4317 --- /dev/null +++ b/examples/platforms/utils/encoding.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2020, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes macros for validating runtime conditions. + */ + +#ifndef PLATFORM_UTILS_ENCODING_H +#define PLATFORM_UTILS_ENCODING_H + +#include + +/** + * Converts a 64-bit unsigned integer stored as a little-endian byte + * array to uint64_t. + * + * Allows for the array to be byte-aligned, so that loading the + * uint64_t value via this function avoids a processor exception due + * to unaligned access. + * + * @param[in] aSource The byte array. + * + * @returns The 64-bit value as a uint64_t. + * + */ +static inline uint64_t otEncodingReadUint64Le(const uint8_t *aSource) +{ + uint64_t value = 0; + + value |= (uint64_t)aSource[0]; + value |= ((uint64_t)aSource[1]) << 8; + value |= ((uint64_t)aSource[2]) << 16; + value |= ((uint64_t)aSource[3]) << 24; + value |= ((uint64_t)aSource[4]) << 32; + value |= ((uint64_t)aSource[5]) << 40; + value |= ((uint64_t)aSource[6]) << 48; + value |= ((uint64_t)aSource[7]) << 56; + + return value; +} + +#endif // PLATFORM_UTILS_ENCODING_H