Skip to content

Fix read overflow #1812

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open

Fix read overflow #1812

wants to merge 4 commits into from

Conversation

Marti2203
Copy link

Fixes the following issue detected by OSS-Fuzz:

This fix was generated by CodeRover-S, an LLM agent for fixing security vulnerabilities. More details can be found at:

@Marti2203 Marti2203 requested a review from seladb as a code owner May 11, 2025 07:58
Copy link

codecov bot commented May 11, 2025

Codecov Report

Attention: Patch coverage is 46.66667% with 8 lines in your changes missing coverage. Please review.

Project coverage is 82.97%. Comparing base (8bcb200) to head (b342d18).

Files with missing lines Patch % Lines
Packet++/src/BgpLayer.cpp 50.00% 4 Missing and 2 partials ⚠️
Packet++/src/RawPacket.cpp 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1812      +/-   ##
==========================================
+ Coverage   80.40%   82.97%   +2.57%     
==========================================
  Files         278      284       +6     
  Lines       44998    48868    +3870     
  Branches    10245    10375     +130     
==========================================
+ Hits        36180    40550    +4370     
- Misses       7033     7213     +180     
+ Partials     1785     1105     -680     
Flag Coverage Δ
alpine320 75.02% <12.50%> (-0.03%) ⬇️
fedora42 75.09% <12.50%> (-0.05%) ⬇️
macos-13 80.51% <27.27%> (-0.01%) ⬇️
macos-14 80.51% <27.27%> (-0.01%) ⬇️
macos-15 80.48% <27.27%> (-0.01%) ⬇️
mingw32 71.36% <12.50%> (?)
mingw64 71.33% <12.50%> (?)
npcap 84.98% <33.33%> (?)
rhel94 74.89% <11.11%> (-0.02%) ⬇️
ubuntu2004 58.53% <22.22%> (+0.01%) ⬆️
ubuntu2004-zstd 58.63% <22.22%> (-0.02%) ⬇️
ubuntu2204 74.84% <11.11%> (-0.02%) ⬇️
ubuntu2204-icpx 61.47% <18.18%> (-0.02%) ⬇️
ubuntu2404 75.08% <12.50%> (+0.01%) ⬆️
ubuntu2404-arm64 75.06% <12.50%> (-0.02%) ⬇️
unittest 82.97% <46.66%> (+2.57%) ⬆️
windows-2019 85.00% <45.45%> (?)
windows-2022 85.01% <33.33%> (?)
winpcap 85.16% <45.45%> (?)
xdp 50.50% <11.11%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 140 to 144
if ((size_t)m_RawDataLen + dataToInsertLen < (size_t)m_RawDataLen)
{
PCPP_LOG_ERROR("RawPacket::insertData: dataToInsertLen causes overflow");
return;
}
Copy link
Collaborator

@Dimi1010 Dimi1010 May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we throw a std::length_error instead of just returning with a error message? The error message will not indicate the error condition to the caller.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will throwing an exception prevent the application from crashing? 🤔

This method was called in BgpLayer so we should probably think how to prevent it from being called with malformed data

Copy link
Collaborator

@Dimi1010 Dimi1010 May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want a crash if overflow happens. Or at least you want an exception thrown to indicate the failure of the operation, and if the caller doesn't handle it up the call stack, then to crash. It isn't really possible to recover from the situation as the system has reached the limit of how much data can be stored inside a packet.

Silently continuing with just an error message in the log is worse as the program will assume the operation went fine and produce corrupted data down the line. This is exactly the type of improbable situation the exception mechanism was made to handle.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree, however I don't think throwing an exception will resolve the OSS-Fuzz issue. We should probably throw an exception and fix the issue that caused it in BgpLayer

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just to confirm - I will update the patch to throw an std::length_error . Would that be accepted by both of you? @Dimi1010 @seladb

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marti2203 thanks for working on it!
Catching exceptions is pretty expensive in C++. I'd avoid using try..catch and instead check the sizes and prevent calling extendLayer is we know it'd fail

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Will rework it then, I know exceptions are expensive but did not understand initially how you wanted it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure no worries 👍
We try to minimize the use of exceptions in critical paths like packet parsing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seladb @Marti2203

Generally, I find exceptions are fine as long as they aren't used to dictate common control flow. The current mainstream compilers use an architecture that has a "zero cost" try-catch blocks if the exception isn't thrown. The tradeoff is a significant overhead if an exception is thrown, so you only want them thrown for rare events.

The current usage seems ok to me? How often are overflows expected to happen?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, I wouldn't expect an exception to be thrown, however:

  • In applications that use PcapPlusPlus with an arbitrary payload (either to test malformed packet handling or because they generate a random payload), an exception can be thrown and we would like to avoid the cost of it
  • Most (or all?) of our critical path code doesn't catch exceptions and try to handle edge cases so they don't end up with exceptions. I think we should do the same here for consistency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants