Skip to content

Commit ad19b26

Browse files
committed
Fix heap-buffer-overflow in TelnetLayer::getOption(TelnetCommand)
In getOption(TelnetCommand) and getOptionData(TelnetCommand, size_t&), the 'offset' variable was calculated before getNextCommandField() advanced 'pos', causing a stale value to be used as maxLength in subsequent getFieldLen() calls. This allowed distanceToNextIAC() to set an endIt pointer past the end of the allocated buffer, triggering an out-of-bounds read in std::find() inside findNextIAC(). Fix: recalculate offset from the updated pos after getNextCommandField(). Also fix getOptionData(TelnetCommand, size_t&) which incorrectly used m_Data instead of pos when extracting field data for the matched command. Fixes: #2144
1 parent 6b84492 commit ad19b26

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

Packet++/src/TelnetLayer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ namespace pcpp
370370
pos = getNextCommandField(pos, m_DataLen - offset);
371371

372372
if (pos && pos[1] == static_cast<int>(command))
373-
return static_cast<TelnetOption>(getSubCommand(pos, getFieldLen(pos, m_DataLen - offset)));
373+
{
374+
size_t newOffset = pos - m_Data;
375+
return static_cast<TelnetOption>(getSubCommand(pos, getFieldLen(pos, m_DataLen - newOffset)));
376+
}
374377
}
375378

376379
PCPP_LOG_DEBUG("Can't find requested command");
@@ -417,8 +420,9 @@ namespace pcpp
417420

418421
if (pos && pos[1] == static_cast<int>(command))
419422
{
420-
size_t lenBuffer = getFieldLen(m_Data, m_DataLen);
421-
uint8_t* posBuffer = getCommandData(m_Data, lenBuffer);
423+
size_t newOffset = pos - m_Data;
424+
size_t lenBuffer = getFieldLen(pos, m_DataLen - newOffset);
425+
uint8_t* posBuffer = getCommandData(pos, lenBuffer);
422426

423427
length = lenBuffer;
424428
return posBuffer;

0 commit comments

Comments
 (0)