[posix] avoid CLI buffer overflow (#6578)

This commit is contained in:
Yakun Xu
2021-05-10 21:19:12 -07:00
committed by GitHub
parent 9432e9a659
commit 7721a5119f
4 changed files with 32 additions and 10 deletions
+14
View File
@@ -134,6 +134,20 @@ do_check()
sudo "${OT_CTL}" -I "${NETIF_NAME}" factoryreset
# sleep a while for daemon ready
sleep 2
readonly OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH=640
local -r kMaxStringLength="$((OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH - 1))"
# verify success if command length doesn't exceed the limit
for len in $(seq 1 ${kMaxStringLength}); do
sudo "${OT_CTL}" -I "${NETIF_NAME}" "$(printf '1%.0s' $(seq 1 "${len}"))"
done
# verify failure if command length exceeds the limit
len=${OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH}
if sudo "${OT_CTL}" -I "${NETIF_NAME}" "$(printf '1%.0s' $(seq 1 "${len}"))"; then
die
fi
OT_CLI_CMD="${OT_CTL} -I ${NETIF_NAME}"
else
OT_CLI="$PWD/build/posix/src/posix/ot-cli"
+1 -1
View File
@@ -49,7 +49,7 @@
/**
* @def OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH
*
* The maximum size of the CLI line in bytes.
* The maximum size of the CLI line in bytes including the null terminator.
*
*/
#ifndef OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH
+15 -8
View File
@@ -197,9 +197,9 @@ void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode)
exit(aExitCode);
}
bool IsSeparator(char aChar)
static bool ShouldEscape(char aChar)
{
return (aChar == ' ') || (aChar == '\t') || (aChar == '\r') || (aChar == '\n');
return (aChar == ' ') || (aChar == '\t') || (aChar == '\r') || (aChar == '\n') || (aChar == '\\');
}
Config ParseArg(int &aArgCount, char **&aArgVector)
@@ -253,20 +253,27 @@ int main(int argc, char *argv[])
for (int i = 0; i < argc; i++)
{
for (const char *c = argv[i]; *c; ++c)
for (const char *c = argv[i]; *c && count < sizeof(buffer);)
{
if (IsSeparator(*c))
if (ShouldEscape(*c))
{
buffer[count++] = '\\';
VerifyOrExit(count < sizeof(buffer), ret = OT_EXIT_INVALID_ARGUMENTS);
}
buffer[count++] = *c;
buffer[count++] = *c++;
}
VerifyOrExit(count < sizeof(buffer), ret = OT_EXIT_INVALID_ARGUMENTS);
buffer[count++] = ' ';
}
// replace the trailing space with newline
buffer[count - 1] = '\n';
VerifyOrExit(DoWrite(sSessionFd, buffer, count), ret = OT_EXIT_FAILURE);
// ignore the trailing space
if (--count)
{
VerifyOrExit(DoWrite(sSessionFd, buffer, count), ret = OT_EXIT_FAILURE);
}
isInteractive = false;
}
+2 -1
View File
@@ -284,7 +284,8 @@ void platformDaemonProcess(const otSysMainloopContext *aContext)
{
uint8_t buffer[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
rval = read(sSessionSocket, buffer, sizeof(buffer));
// leave 1 byte for the null terminator
rval = read(sSessionSocket, buffer, sizeof(buffer) - 1);
if (rval > 0)
{