diff --git a/src/cli/cli_dataset.cpp b/src/cli/cli_dataset.cpp index 18a4ff1a5..d8331d2a2 100644 --- a/src/cli/cli_dataset.cpp +++ b/src/cli/cli_dataset.cpp @@ -40,6 +40,7 @@ #include #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(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; } diff --git a/src/core/common/string.cpp b/src/core/common/string.cpp index f10e017a5..3ec72d411 100644 --- a/src/core/common/string.cpp +++ b/src/core/common/string.cpp @@ -33,6 +33,8 @@ #include "string.hpp" +#include + 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(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(aString + position); + ++position; + + // Verify the continuation byte pattern 10xx-xxxx + VerifyOrExit((byte & 0xc0) == 0x80, ret = false); + } + } + +exit: + return ret; +} + } // namespace ot diff --git a/src/core/common/string.hpp b/src/core/common/string.hpp index 6cb7edce6..79acbd47c 100644 --- a/src/core/common/string.hpp +++ b/src/core/common/string.hpp @@ -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_ diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index bf397c6bd..8a285d38a 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -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(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) diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index de01625aa..b9537c014 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -590,6 +590,8 @@ otError Commissioner::SetProvisioningUrl(const char *aProvisioningUrl) ExitNow(); } + VerifyOrExit(IsValidUtf8String(aProvisioningUrl), error = OT_ERROR_INVALID_ARGS); + len = static_cast(StringLength(aProvisioningUrl, sizeof(mProvisioningUrl))); VerifyOrExit(len < sizeof(mProvisioningUrl), error = OT_ERROR_INVALID_ARGS); diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index e1f310b41..349a4c68d 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -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().IsUp() && Get().GetRole() == Mle::kRoleDisabled, error = OT_ERROR_INVALID_STATE); diff --git a/src/core/meshcop/meshcop.cpp b/src/core/meshcop/meshcop.cpp index 6a0d52268..1a5e3cd70 100644 --- a/src/core/meshcop/meshcop.cpp +++ b/src/core/meshcop/meshcop.cpp @@ -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(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1)); networkNameLen = static_cast(StringLength(aNetworkName.GetAsCString(), OT_NETWORK_NAME_MAX_SIZE + 1)); diff --git a/src/core/meshcop/meshcop_tlvs.cpp b/src/core/meshcop/meshcop_tlvs.cpp index 6a71844c9..390ca2aa5 100644 --- a/src/core/meshcop/meshcop_tlvs.cpp +++ b/src/core/meshcop/meshcop_tlvs.cpp @@ -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()); diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index aa77ddbec..b040a1620 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -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. diff --git a/src/core/thread/discover_scanner.cpp b/src/core/thread/discover_scanner.cpp index 66b1f0fa1..f4947f802 100644 --- a/src/core/thread/discover_scanner.cpp +++ b/src/core/thread/discover_scanner.cpp @@ -336,7 +336,10 @@ void DiscoverScanner::HandleDiscoveryResponse(const Message &aMessage, const Ip6 case MeshCoP::Tlv::kNetworkName: IgnoreError(aMessage.Read(offset, networkName)); - IgnoreError(static_cast(result.mNetworkName).Set(networkName.GetNetworkName())); + if (networkName.IsValid()) + { + IgnoreError(static_cast(result.mNetworkName).Set(networkName.GetNetworkName())); + } break; case MeshCoP::Tlv::kSteeringData: diff --git a/tests/scripts/expect/cli-dataset.exp b/tests/scripts/expect/cli-dataset.exp index e5c5dde1b..259f9cfd2 100755 --- a/tests/scripts/expect/cli-dataset.exp +++ b/tests/scripts/expect/cli-dataset.exp @@ -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" diff --git a/tests/scripts/expect/cli-networkname.exp b/tests/scripts/expect/cli-networkname.exp new file mode 100755 index 000000000..2b0ce3e70 --- /dev/null +++ b/tests/scripts/expect/cli-networkname.exp @@ -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 diff --git a/tests/unit/test_string.cpp b/tests/unit/test_string.cpp index d7dd50531..e16801d1e 100644 --- a/tests/unit/test_string.cpp +++ b/tests/unit/test_string.cpp @@ -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; }