From acd35c6ae72d628344ef70d696ec53be872c0f2d Mon Sep 17 00:00:00 2001 From: jmartinez-silabs <67972863+jmartinez-silabs@users.noreply.github.com> Date: Tue, 5 Jul 2022 08:47:49 -0400 Subject: [PATCH] [EFR32] Fix Storage audit - Allow storing null pointer of size 0 (#20290) * Fix Storage audit - Allow storing null pointer of size 0 * Clarify comment --- src/platform/EFR32/EFR32Config.cpp | 25 ++++++------------- .../EFR32/KeyValueStoreManagerImpl.cpp | 3 --- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/platform/EFR32/EFR32Config.cpp b/src/platform/EFR32/EFR32Config.cpp index 74308f1df2c684..9c156ac6aae8c3 100644 --- a/src/platform/EFR32/EFR32Config.cpp +++ b/src/platform/EFR32/EFR32Config.cpp @@ -236,7 +236,6 @@ CHIP_ERROR EFR32Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSiz // Get nvm3 object info. err = MapNvm3Error(nvm3_getObjectInfo(nvm3_defaultHandle, key, &objectType, &dataLen)); SuccessOrExit(err); - VerifyOrExit(dataLen > 0, err = CHIP_ERROR_INVALID_STRING_LENGTH); if (buf != NULL) { @@ -325,7 +324,7 @@ CHIP_ERROR EFR32Config::WriteConfigValueStr(Key key, const char * str) CHIP_ERROR EFR32Config::WriteConfigValueStr(Key key, const char * str, size_t strLen) { - CHIP_ERROR err; + CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT; VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id. @@ -336,10 +335,6 @@ CHIP_ERROR EFR32Config::WriteConfigValueStr(Key key, const char * str, size_t st err = MapNvm3Error(nvm3_writeData(nvm3_defaultHandle, key, str, (strLen > 0) ? strLen : 1)); SuccessOrExit(err); } - else - { - nvm3_deleteObject(nvm3_defaultHandle, key); // no error checking here. - } exit: return err; @@ -347,22 +342,16 @@ CHIP_ERROR EFR32Config::WriteConfigValueStr(Key key, const char * str, size_t st CHIP_ERROR EFR32Config::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) { - CHIP_ERROR err; + CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT; VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id. - if (data != NULL) + // Only write NULL pointer if the given size is 0, since in that case, nothing is read at the pointer + if ((data != NULL) || (dataLen == 0)) { - if (dataLen > 0) - { - // Write the binary data to nvm3. - err = MapNvm3Error(nvm3_writeData(nvm3_defaultHandle, key, data, dataLen)); - SuccessOrExit(err); - } - } - else - { - nvm3_deleteObject(nvm3_defaultHandle, key); // no error checking here. + // Write the binary data to nvm3. + err = MapNvm3Error(nvm3_writeData(nvm3_defaultHandle, key, data, dataLen)); + SuccessOrExit(err); } exit: diff --git a/src/platform/EFR32/KeyValueStoreManagerImpl.cpp b/src/platform/EFR32/KeyValueStoreManagerImpl.cpp index ec90a3f001a85b..3b577fce108eb4 100644 --- a/src/platform/EFR32/KeyValueStoreManagerImpl.cpp +++ b/src/platform/EFR32/KeyValueStoreManagerImpl.cpp @@ -126,8 +126,6 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t size_t offset_bytes) const { VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(value != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(value != 0, CHIP_ERROR_INVALID_ARGUMENT); uint32_t nvm3Key; CHIP_ERROR err = MapKvsKeyToNvm3(key, nvm3Key); @@ -146,7 +144,6 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size) { VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(value != nullptr, CHIP_ERROR_INVALID_ARGUMENT); uint32_t nvm3Key; CHIP_ERROR err = MapKvsKeyToNvm3(key, nvm3Key, /* isSlotNeeded */ true);