fix(ext/http): avoid misaligned sockaddr_in access in SocketAddr constructor - #4629
Shubhammehta2008 wants to merge 21 commits into
Conversation
|
|
marcalff
left a comment
There was a problem hiding this comment.
Thanks for the patch.
Please see comments, the proper fix is to use sockaddr_storage to represent a socket in an arbitrary protocol.
Other places using m_data may need some cleanup.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4629 +/- ##
==========================================
+ Coverage 86.66% 86.67% +0.01%
==========================================
Files 525 525
Lines 20543 20542 -1
==========================================
+ Hits 17802 17803 +1
+ Misses 2741 2739 -2
🚀 New features to boost your workflow:
|
|
Updated the tests to use ss_family for sockaddr_storage to resolve the Windows build failure. Please approve the workflow runs and take another look when you get a chance. |
marcalff
left a comment
There was a problem hiding this comment.
Looks much better.
See comments.
Co-authored-by: Marc Alff <marc.alff@free.fr>
Co-authored-by: Marc Alff <marc.alff@free.fr>
Co-authored-by: Marc Alff <marc.alff@free.fr>
| // The parser memcpys a sockaddr_in into m_data, and the socket syscalls pass sizeof(SocketAddr) | ||
| // as the address length. This wrapper is IPv4-only, so require sockaddr and sockaddr_in to have | ||
| // the exact same size rather than trusting every ABI: passing an address length that is too large | ||
| // for the family is a documented EINVAL for connect()/bind(). Exact equality also keeps the memcpy | ||
| // safe. Together with the assertion below, sizeof(SocketAddr) == sizeof(sockaddr_in). |
There was a problem hiding this comment.
This is no longer true, now that m_data is aligned, but bigger than sockaddr_in.
I suspect this is what makes the test fail on MacOS, providing a length greater that sizeof(sockaddr_in).
Please implement:
- a method addr() to return &m_data
- a method addr_len() to return sizeof(sockaddr_in), since only AF_INET is supported anyway.
Use these methods when calling bind / connect, to pass the proper addr.addr() and addr.addr_len() to the networking code.
marcalff
left a comment
There was a problem hiding this comment.
See MacOS runtime failures, and possible fix.
Fixes #4307
Changes
Avoids undefined behavior caused by binding a
sockaddr_in &tom_datathroughreinterpret_castin theSocketAddr(u_long, uint16_t)constructor.The fix is to declare m_data as
sockaddr_storage.For significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changes