Repository navigation
msg: fix entity_addr_t wire encoding on BSD and macOS - #71445
Conversation
8de3295 to
e636f32
Compare
|
This is the same encode() fix as the one I had in #71888, and yours is further along, so I've dropped mine. One thing mine also covered, which I think belongs here: u.sa.sa_family = ss_family;
elen -= sizeof(ss_family);
if (elen > get_sockaddr_len() - sizeof(u.sa.sa_family)) {On the BSDs Using the same expression as in your encode() keeps the two sides symmetric: - if (elen > get_sockaddr_len() - sizeof(u.sa.sa_family)) {
+ if (elen > get_sockaddr_len() - offsetof(struct sockaddr, sa_data)) {Would you fold that in? With it, this looks good to me. |
|
@dvanders Yes, good find.... I only looked for the problem that the tests show, but it also holds on the decode side. I'll your side into the code. But I found a similar construction also in src/msg/msg_types.cc |
get_sockaddr_len() includes the sa_len member that BSD-derived sockaddrs carry and Linux does not. encode() subtracted sizeof(sa_len) from elen before writing elen to the wire, but then appended sa_data using the full remaining length. The advertised length was therefore one byte short of the data that followed, corrupting every subsequent field in the frame for any peer. decode() does not compensate: it zeroes sa_len, reads the family and consumes the remaining elen bytes as given. Its bounds check uses the local get_sockaddr_len(), which includes sa_len, so a BSD host reading a Linux-encoded address had a byte of slack and the mismatch only showed on the encode side. Move the subtraction to after elen is encoded, so the advertised length and the appended data agree and the encoding matches what a Linux peer produces. Found on FreeBSD via unittest_compression's round_trip_osdmap case: the OSDMap contains entity_addr_t addresses, and comparing the re-encoded map against the reference showed the length field as 15 where 16 was expected. The compressors were not at fault. bin/unittest_compression --plugin_dir=<build>/lib \ --gtest_filter='*round_trip_osdmap*' This can very well impact interoperability between Linux and BSD type components. While here, use defined(__FreeBSD__) rather than testing the macro's value, matching the __APPLE__ test beside it and the other guards in this file. msg/msg_types: fix decode() off-by-one on BSD sa_family size Add same offset fix to src/msg/msg_types.cc On BSDs sizeof(sa_family) is 1 byte vs 2 on Linux, causing the bound check to allow one byte more than sa_data holds. Use offsetof(struct sockaddr, sa_data) to keep encode() and decode() symmetric. For the decode() change to be tested it requires a faulty packet to be entered into the decode(). Otherwise this if with error will not be reported. Suggested-by: Dan van der Ster <[email protected]> Signed-off-by: Willem Jan Withagen <[email protected]>
e636f32 to
a2c3329
Compare
|
This PR has been added to wip-bharath14-testing-20260930.003013-main. |
There was a problem hiding this comment.
Thanks @wjwithagen, the code looks right to me. Encode now matches the Linux layout byte for byte, both decoders bound sa_data by offsetof(sockaddr, sa_data), nothing changes on Linux or Windows (offsetof is 2 there, the same as sizeof(sa_family)), and old and new BSD peers still decode each other, because DECODE_FINISH skips to the end of the struct.
Tests: I wrote two in dvanders@fd6865c, on top of your branch, so git cherry-pick fd6865c should apply cleanly.
- TestAddrEncodingIsPortable pins the msgr2 AF_INET encoding byte for byte.
- TestAddrDecodeRejectsOversizedAddr checks that both entity_addr_t and entity_addrvec_t reject an elen one byte too long.
On macOS, each half of the decode test fails without the matching change in msg_types.h and msg_types.cc, and everything passes with your PR. On Linux they pass either way. Feel free to fold them in, or I can send them as a follow-up once this merges.
A few nits on the commit message, which has drifted from the code:
- "Move the subtraction to after elen is encoded": the subtraction is gone now, replaced by offsetof().
- "use defined(FreeBSD) rather than testing the macro's value": that #if is deleted rather than changed.
- The second commit's subject line ended up in the body when they were squashed.
- "corrupting every subsequent field in the frame" is stronger than what happens, since DECODE_FINISH resynchronises after the struct. The real effects are that the last sa_data byte is dropped (padding for AF_INET, part of sin6_scope_id for AF_INET6), and the encoding no longer matches what Linux writes. In a mixed cluster that means OSDMap CRC mismatches, which I think is the strongest argument for the fix.
A separate issue for later, not this PR: encode() only maps AF_INET6 to the Linux value under _WIN32. AF_INET6 is 30 on macOS and 28 on FreeBSD, against 10 on Linux, so IPv6 addresses from a BSD or macOS daemon probably reach a Linux peer with a family it doesn't recognise. The legacy sockaddr_storage path has the same Windows-only mapping. Reusing AF_INET6_LINUX for __FreeBSD__ and __APPLE__ would likely fix both.
Feel free to enter this as a separate PR.... This one is already in QA, so I'd rather not disturbe this |
One of the peculiarities is that al lot of the implementation(-test) only run IPv4. And to be honest, I did not even consider that this has would even be different. But giving it some thought, there is no reason why the should be equal. |
|
Follow-up with the tests and the AF_INET6 fix: #72446. |
get_sockaddr_len() includes the sa_len member that BSD-derived sockaddrs carry and Linux does not. encode() subtracted sizeof(sa_len) from elen before writing elen to the wire, but then appended sa_data using the full remaining length. The advertised length was therefore one byte short of the data that followed, corrupting every subsequent field in the frame for any peer.
decode() does not compensate: it zeroes sa_len, reads the family and consumes the remaining elen bytes as given. Its bounds check uses the local get_sockaddr_len(), which includes sa_len, so a BSD host reading a Linux-encoded address had a byte of slack and the mismatch only showed on the encode side.
Move the subtraction to after elen is encoded, so the advertised length and the appended data agree and the encoding matches what a Linux peer produces.
Found on FreeBSD via unittest_compression's round_trip_osdmap case: the OSDMap contains entity_addr_t addresses, and comparing the re-encoded map against the reference showed the length field as 15 where 16 was expected. The compressors were not at fault.
bin/unittest_compression --plugin_dir=/lib --gtest_filter='round_trip_osdmap'
This can very well impact interoperability between Linux and BSD type components.
While here, use defined(FreeBSD) rather than testing the macro's value, matching the APPLE test beside it and the other guards in this file.
msg/msg_types: fix decode() off-by-one on BSD sa_family size
Add same offset fix to src/msg/msg_types.cc
On BSDs sizeof(sa_family) is 1 byte vs 2 on Linux, causing the bound
check to allow one byte more than sa_data holds. Use offsetof(struct
sockaddr, sa_data) to keep encode() and decode() symmetric.
For the decode() change to be tested it requires a faulty packet
to be entered into the decode(). Otherwise this if with error
will not be reported.
Suggested-by: Dan van der Ster [email protected]
Contribution Guidelines
To sign and title your commits, please refer to Submitting Patches to Ceph.
If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.
When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an
xbetween the brackets:[x]. Spaces and capitalization matter when checking off items this way.Checklist
Show available Jenkins commands
jenkins test classic perfJenkins Job | Jenkins Job Definitionjenkins test crimson perfJenkins Job | Jenkins Job Definitionjenkins test signedJenkins Job | Jenkins Job Definitionjenkins test make checkJenkins Job | Jenkins Job Definitionjenkins test make check arm64Jenkins Job | Jenkins Job Definitionjenkins test submodulesJenkins Job | Jenkins Job Definitionjenkins test dashboardJenkins Job | Jenkins Job Definitionjenkins test dashboard cephadmJenkins Job | Jenkins Job Definitionjenkins test apiJenkins Job | Jenkins Job Definitionjenkins test docsReadTheDocs | Github Workflow Definitionjenkins test ceph-volume allJenkins Jobs | Jenkins Jobs Definitionjenkins test windowsJenkins Job | Jenkins Job Definitionjenkins test rook e2eJenkins Job | Jenkins Job DefinitionYou must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.