Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unknown read in pcpp::NflogLayer::parseNextLayer #1178

Merged
merged 8 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ namespace pcpp
* @return A pointer to the beginning of the substring, or NULL if the substring is not found
*/
char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen);

/**
* Calculates alignment.
* @param[in] number Given number
* @return The aligned number
*/
template <int alignment>
static int align(int number)
{
// Only works for alignment with power of 2
constexpr bool isPowerOfTwo = alignment && ((alignment & (alignment - 1)) == 0);
static_assert(isPowerOfTwo, "Alignment must be a power of 2");
int mask = alignment - 1;
return (number + mask) & ~mask;
}
}

#endif // PCAPPP_GENERAL_UTILS
17 changes: 7 additions & 10 deletions Packet++/header/NflogLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Layer.h"
#include "TLVData.h"
#include "GeneralUtils.h"

/// @file

Expand Down Expand Up @@ -101,23 +102,19 @@ namespace pcpp
/**
* @return recordLen attribute in NflogTLVRawData
*/
size_t getTotalSize() const { return m_Data->recordLen; }
size_t getTotalSize() const
{
// as in https://github.com/the-tcpdump-group/libpcap/blob/766b607d60d8038087b49fc4cf433dac3dcdb49c/pcap-util.c#L371-L374
return align<4>(m_Data->recordLen);
}

/**
* Assign a pointer to the TLV record raw data (byte array)
* @param[in] recordRawData A pointer to the TLV record raw data
*/
void assign(uint8_t* recordRawData)
{
if(recordRawData == nullptr)
m_Data = nullptr;
else
{
// to pass from some unknown paddings after tlv with type NFULA_PREFIX
while (*recordRawData == 0)
recordRawData += 1;
m_Data = (NflogTLVRawData*)recordRawData;
}
m_Data = (NflogTLVRawData*)recordRawData;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Tests/Packet++Test/Tests/NflogTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ PTF_TEST_CASE(NflogPacketParsingTest)
pcpp::NflogTlvType::NFULA_PAYLOAD
};

int optSizes[6] = {8, 5, 8, 8, 8, 65};
int optSizes[6] = {8, 8, 8, 8, 8, 68};
std::string optDataAsHexString[6] = {
"0800010000000300",
"05000a0000",
"05000a0000000000",
"0800050000000002",
"08000b0000000000",
"08000e0000000000",
"410009004500003d021040004011208f0a00020f0a000203a542003500294156c04e0100000100000000000003777777076578616d706c65036e65740000010001"
"410009004500003d021040004011208f0a00020f0a000203a542003500294156c04e0100000100000000000003777777076578616d706c65036e657400000100012f0a31"
};

for (int i = 0; i < 6; i++) {
Expand All @@ -56,5 +56,5 @@ PTF_TEST_CASE(NflogPacketParsingTest)
}

/// sum of all TLVs before payload + size of nflog_header + size of (recordLength + recordType) variables of payload TLV
PTF_ASSERT_EQUAL(nflogLayer->getHeaderLen(), 45);
PTF_ASSERT_EQUAL(nflogLayer->getHeaderLen(), 48);
}