[string] add UTF-8 validation (#5810)

This commit is contained in:
Moandor
2020-11-30 08:33:05 -08:00
committed by GitHub
parent 18b82fdef5
commit c7192ba798
13 changed files with 277 additions and 15 deletions
+27 -9
View File
@@ -40,6 +40,7 @@
#include <openthread/dataset_ftd.h>
#include "cli/cli.hpp"
#include "common/string.hpp"
#include "utils/parse_cmdline.hpp"
using ot::Utils::CmdLineParser::ParseAsHexString;
@@ -485,6 +486,7 @@ otError Dataset::ProcessNetworkName(uint8_t aArgsLength, char *aArgs[])
size_t length;
VerifyOrExit((length = strlen(aArgs[0])) <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(IsValidUtf8String(aArgs[0]), error = OT_ERROR_INVALID_ARGS);
memset(&sDataset.mNetworkName, 0, sizeof(sDataset.mNetworkName));
memcpy(sDataset.mNetworkName.m8, aArgs[0], length);
@@ -865,29 +867,45 @@ exit:
otError Dataset::ProcessSet(uint8_t aArgsLength, char *aArgs[])
{
otError error = OT_ERROR_NONE;
otOperationalDatasetTlvs dataset;
uint16_t tlvsLength;
otError error = OT_ERROR_NONE;
MeshCoP::Dataset::Type datasetType;
VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS);
tlvsLength = sizeof(dataset.mTlvs);
SuccessOrExit(error = ParseAsHexString(aArgs[1], tlvsLength, dataset.mTlvs));
dataset.mLength = static_cast<uint8_t>(tlvsLength);
if (strcmp(aArgs[0], "active") == 0)
{
SuccessOrExit(error = otDatasetSetActiveTlvs(mInterpreter.mInstance, &dataset));
datasetType = MeshCoP::Dataset::Type::kActive;
}
else if (strcmp(aArgs[0], "pending") == 0)
{
SuccessOrExit(error = otDatasetSetPendingTlvs(mInterpreter.mInstance, &dataset));
datasetType = MeshCoP::Dataset::Type::kPending;
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
{
MeshCoP::Dataset dataset(datasetType);
MeshCoP::Dataset::Info datasetInfo;
uint16_t tlvsLength = MeshCoP::Dataset::kMaxSize;
SuccessOrExit(error = ParseAsHexString(aArgs[1], tlvsLength, dataset.GetBytes()));
dataset.SetSize(tlvsLength);
VerifyOrExit(dataset.IsValid(), error = OT_ERROR_INVALID_ARGS);
dataset.ConvertTo(datasetInfo);
switch (datasetType)
{
case MeshCoP::Dataset::Type::kActive:
SuccessOrExit(error = otDatasetSetActive(mInterpreter.mInstance, &datasetInfo));
break;
case MeshCoP::Dataset::Type::kPending:
SuccessOrExit(error = otDatasetSetPending(mInterpreter.mInstance, &datasetInfo));
break;
}
}
exit:
return error;
}
+64
View File
@@ -33,6 +33,8 @@
#include "string.hpp"
#include <string.h>
namespace ot {
uint16_t StringLength(const char *aString, uint16_t aMaxLength)
@@ -73,4 +75,66 @@ otError StringBase::Write(char *aBuffer, uint16_t aSize, uint16_t &aLength, cons
return error;
}
bool IsValidUtf8String(const char *aString)
{
return IsValidUtf8String(aString, strlen(aString));
}
bool IsValidUtf8String(const char *aString, size_t aLength)
{
bool ret = true;
uint8_t byte;
uint8_t continuationBytes = 0;
size_t position = 0;
while (position < aLength)
{
byte = *reinterpret_cast<const uint8_t *>(aString + position);
++position;
if ((byte & 0x80) == 0)
{
continue;
}
// This is a leading byte 1xxx-xxxx.
if ((byte & 0x40) == 0) // 10xx-xxxx
{
// We got a continuation byte pattern without seeing a leading byte earlier.
ExitNow(ret = false);
}
else if ((byte & 0x20) == 0) // 110x-xxxx
{
continuationBytes = 1;
}
else if ((byte & 0x10) == 0) // 1110-xxxx
{
continuationBytes = 2;
}
else if ((byte & 0x08) == 0) // 1111-0xxx
{
continuationBytes = 3;
}
else // 1111-1xxx (invalid pattern).
{
ExitNow(ret = false);
}
while (continuationBytes-- != 0)
{
VerifyOrExit(position < aLength, ret = false);
byte = *reinterpret_cast<const uint8_t *>(aString + position);
++position;
// Verify the continuation byte pattern 10xx-xxxx
VerifyOrExit((byte & 0xc0) == 0x80, ret = false);
}
}
exit:
return ret;
}
} // namespace ot
+23
View File
@@ -258,6 +258,29 @@ private:
*
*/
/**
* This function validates whether a given byte sequence (string) follows UTF-8 encoding.
*
* @param[in] aString A null-terminated byte sequence.
*
* @retval TRUE The sequence is a valid UTF-8 string.
* @retval FALSE The sequence is not a valid UTF-8 string.
*
*/
bool IsValidUtf8String(const char *aString);
/**
* This function validates whether a given byte sequence (string) follows UTF-8 encoding.
*
* @param[in] aString A byte sequence.
* @param[in] aLength Length of the sequence.
*
* @retval TRUE The sequence is a valid UTF-8 string.
* @retval FALSE The sequence is not a valid UTF-8 string.
*
*/
bool IsValidUtf8String(const char *aString, size_t aLength);
} // namespace ot
#endif // STRING_HPP_
+16 -2
View File
@@ -42,6 +42,7 @@
#include "common/locator-getters.hpp"
#include "common/logging.hpp"
#include "common/random.hpp"
#include "common/string.hpp"
#include "crypto/aes_ccm.hpp"
#include "crypto/sha256.hpp"
#include "mac/mac_frame.hpp"
@@ -261,6 +262,7 @@ otError Mac::ConvertBeaconToActiveScanResult(const RxFrame *aBeaconFrame, Active
aResult.mIsJoinable = beaconPayload->IsJoiningPermitted();
aResult.mIsNative = beaconPayload->IsNative();
IgnoreError(static_cast<NetworkName &>(aResult.mNetworkName).Set(beaconPayload->GetNetworkName()));
VerifyOrExit(IsValidUtf8String(aResult.mNetworkName.m8), error = OT_ERROR_PARSE);
aResult.mExtendedPanId = beaconPayload->GetExtendedPanId();
}
@@ -475,9 +477,15 @@ otError Mac::SetNetworkName(const char *aNameString)
// longer than `kMaxSize` is correctly rejected (returning error
// `OT_ERROR_INVALID_ARGS`).
otError error;
NameData data(aNameString, NetworkName::kMaxSize + 1);
return SetNetworkName(data);
VerifyOrExit(IsValidUtf8String(aNameString), error = OT_ERROR_INVALID_ARGS);
error = SetNetworkName(data);
exit:
return error;
}
otError Mac::SetNetworkName(const NameData &aNameData)
@@ -509,9 +517,15 @@ otError Mac::SetDomainName(const char *aNameString)
// longer than `kMaxSize` is correctly rejected (returning error
// `OT_ERROR_INVALID_ARGS`).
otError error;
NameData data(aNameString, DomainName::kMaxSize + 1);
return SetDomainName(data);
VerifyOrExit(IsValidUtf8String(aNameString), error = OT_ERROR_INVALID_ARGS);
error = SetDomainName(data);
exit:
return error;
}
otError Mac::SetDomainName(const NameData &aNameData)
+2
View File
@@ -590,6 +590,8 @@ otError Commissioner::SetProvisioningUrl(const char *aProvisioningUrl)
ExitNow();
}
VerifyOrExit(IsValidUtf8String(aProvisioningUrl), error = OT_ERROR_INVALID_ARGS);
len = static_cast<uint8_t>(StringLength(aProvisioningUrl, sizeof(mProvisioningUrl)));
VerifyOrExit(len < sizeof(mProvisioningUrl), error = OT_ERROR_INVALID_ARGS);
+4
View File
@@ -140,6 +140,10 @@ otError Joiner::Start(const char * aPskd,
otLogInfoMeshCoP("Joiner starting");
VerifyOrExit(aProvisioningUrl == nullptr || IsValidUtf8String(aProvisioningUrl), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aVendorName == nullptr || IsValidUtf8String(aVendorName), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aVendorSwVersion == nullptr || IsValidUtf8String(aVendorSwVersion), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(mState == kStateIdle, error = OT_ERROR_BUSY);
VerifyOrExit(Get<ThreadNetif>().IsUp() && Get<Mle::Mle>().GetRole() == Mle::kRoleDisabled,
error = OT_ERROR_INVALID_STATE);
+3
View File
@@ -37,6 +37,7 @@
#include "common/debug.hpp"
#include "common/locator-getters.hpp"
#include "common/logging.hpp"
#include "common/string.hpp"
#include "crypto/pbkdf2_cmac.h"
#include "crypto/sha256.hpp"
#include "mac/mac_types.hpp"
@@ -325,6 +326,8 @@ otError GeneratePskc(const char * aPassPhrase,
uint16_t passphraseLen;
uint8_t networkNameLen;
VerifyOrExit(IsValidUtf8String(aPassPhrase), error = OT_ERROR_INVALID_ARGS);
passphraseLen = static_cast<uint16_t>(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
networkNameLen = static_cast<uint8_t>(StringLength(aNetworkName.GetAsCString(), OT_NETWORK_NAME_MAX_SIZE + 1));
+6
View File
@@ -34,6 +34,7 @@
#include "meshcop_tlvs.hpp"
#include "common/debug.hpp"
#include "common/string.hpp"
#include "meshcop/meshcop.hpp"
namespace ot {
@@ -129,6 +130,11 @@ void NetworkNameTlv::SetNetworkName(const Mac::NameData &aNameData)
SetLength(len);
}
bool NetworkNameTlv::IsValid(void) const
{
return IsValidUtf8String(mNetworkName, GetLength());
}
void SteeringDataTlv::CopyTo(SteeringData &aSteeringData) const
{
aSteeringData.Init(GetSteeringDataLength());
+1 -1
View File
@@ -511,7 +511,7 @@ public:
* @retval FALSE If the TLV does not appear to be well-formed.
*
*/
bool IsValid(void) const { return true; }
bool IsValid(void) const;
/**
* This method gets the Network Name value.
+4 -1
View File
@@ -336,7 +336,10 @@ void DiscoverScanner::HandleDiscoveryResponse(const Message &aMessage, const Ip6
case MeshCoP::Tlv::kNetworkName:
IgnoreError(aMessage.Read(offset, networkName));
IgnoreError(static_cast<Mac::NetworkName &>(result.mNetworkName).Set(networkName.GetNetworkName()));
if (networkName.IsValid())
{
IgnoreError(static_cast<Mac::NetworkName &>(result.mNetworkName).Set(networkName.GetNetworkName()));
}
break;
case MeshCoP::Tlv::kSteeringData:
+31 -2
View File
@@ -204,18 +204,47 @@ address $addr\n"
expect "Done"
switch_node 1
send "dataset init active\n"
expect "Done"
send "dataset networkname Thread\\ 网络\n"
expect "Done"
send "dataset commit active\n"
expect "Done"
send "dataset active -x\n"
expect "54687265616420e7bd91e7bb9c" ;# UTF-8 of "Thread 网络"
expect "Done"
send "dataset active -x\n"
expect "dataset active -x"
expect -re {([0-9a-f]+)}
expect -re {([0-9a-f]+)[\r\n]+Done}
set binary $expect_out(1,string)
expect "Done"
send "dataset set pending $binary\n"
expect "Done"
send "dataset pending -x\n"
expect $binary
expect "Done"
send "dataset pending\n"
expect "Network Name: Thread 网络"
expect "Done"
send "dataset set active $binary\n"
expect "Done"
send "dataset mgmtsetcommand active activetimestamp 200 -x 030d54687265616420e7bd91e7bb9c\n"
expect "Done"
send "dataset active\n"
expect "Network Name: Thread 网络"
expect "Done"
send "dataset mgmtsetcommand active activetimestamp 210 -x 0301ff\n"
expect "Done"
send "dataset active\n"
expect "Active Timestamp: 200"
expect "Network Name: Thread 网络"
expect "Done"
send "dataset set active 03023432\n"
expect "Done"
send "dataset active\n"
expect "Network Name: 42"
expect "Done"
send "dataset set active 0301bf\n"
expect "Error 7: InvalidArgs"
send "dataset help\n"
expect "Done"
send "dataset\n"
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/expect -f
#
# 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.
#
source "tests/scripts/expect/_common.exp"
spawn_node 1
spawn_node 2
switch_node 1
send "networkname Thread\\ Network\n"
expect "Done"
send "ifconfig up\n"
expect "Done"
send "thread start\n"
expect "Done"
wait_for "state" "leader"
expect "Done"
switch_node 2
send "scan\n"
expect "Thread Network"
expect "Done"
switch_node 1
send "thread stop\n"
expect "Done"
send "networkname Thread\\ 网络\n"
expect "Done"
send "thread start\n"
expect "Done"
wait_for "state" "leader"
expect "Done"
switch_node 2
send "scan\n"
expect "Thread 网络"
expect "Done"
switch_node 1
send "thread stop\n"
expect "Done"
send "networkname スレッド\n"
expect "Done"
send "thread start\n"
expect "Done"
wait_for "state" "leader"
expect "Done"
switch_node 2
send "scan\n"
expect "スレッド"
expect "Done"
dispose_all
+16
View File
@@ -139,12 +139,28 @@ void TestStringLength(void)
printf(" -- PASS\n");
}
void TestUtf8(void)
{
printf("\nTest 5: IsValidUtf8String() function\n");
VerifyOrQuit(IsValidUtf8String("An ASCII string"), "IsValidUtf8String() ASCII string fails");
VerifyOrQuit(IsValidUtf8String(u8"Строка UTF-8"), "IsValidUtf8String() UTF-8 string fails");
VerifyOrQuit(!IsValidUtf8String("\xbf"), "IsValidUtf8String() illegal string fails");
VerifyOrQuit(!IsValidUtf8String("\xdf"), "IsValidUtf8String() illegal string fails");
VerifyOrQuit(!IsValidUtf8String("\xef\x80"), "IsValidUtf8String() illegal string fails");
VerifyOrQuit(!IsValidUtf8String("\xf7\x80\x80"), "IsValidUtf8String() illegal string fails");
VerifyOrQuit(!IsValidUtf8String("\xff"), "IsValidUtf8String() illegal string fails");
printf(" -- PASS\n");
}
} // namespace ot
int main(void)
{
ot::TestString();
ot::TestStringLength();
ot::TestUtf8();
printf("\nAll tests passed.\n");
return 0;
}