Skip to content

Commit

Permalink
[EFR32] Fix Storage audit - Allow storing null pointer of size 0 (#20290
Browse files Browse the repository at this point in the history
)

* Fix Storage audit - Allow storing null pointer of size 0

* Clarify comment
  • Loading branch information
jmartinez-silabs authored and pull[bot] committed Sep 30, 2023
1 parent 8ae6f38 commit acd35c6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
25 changes: 7 additions & 18 deletions src/platform/EFR32/EFR32Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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.

Expand All @@ -336,33 +335,23 @@ 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;
}

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:
Expand Down
3 changes: 0 additions & 3 deletions src/platform/EFR32/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit acd35c6

Please sign in to comment.