Skip to content

msg: fix entity_addr_t wire encoding on BSD and macOS - #71445

Merged
SrinivasaBharath merged 1 commit into
ceph:mainfrom
wjwithagen:wip-freebsd-entity-addr-encode
Oct 16, 2026
Merged

SrinivasaBharath merged 1 commit into
ceph:mainfrom
wjwithagen:wip-freebsd-entity-addr-encode

Conversation

@wjwithagen

@wjwithagen wjwithagen commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

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 x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@wjwithagen
wjwithagen requested a review from a team as a code owner August 31, 2026 16:49
@github-actions github-actions Bot added the core label Aug 31, 2026
@wjwithagen wjwithagen added the FreeBSD Modifications specific for (only) FreeBSD label Aug 31, 2026
Comment thread src/msg/msg_types.h Resolved
@dvanders

Copy link
Copy Markdown
Contributor

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: decode() has the matching off-by-one.

      u.sa.sa_family = ss_family;
      elen -= sizeof(ss_family);
      if (elen > get_sockaddr_len() - sizeof(u.sa.sa_family)) {

On the BSDs sizeof(u.sa.sa_family) is 1, so the bound allows one byte more than sa_data holds. With u being a union of sockaddr, sockaddr_in and sockaddr_in6 (28 bytes), an AF_INET6 address that arrives with elen = 29 becomes 27 after the family is removed. It passes 27 > 28 - 1, and bl.copy(27, u.sa.sa_data) then writes bytes 2..28, one past the end of the union, which is the last member of entity_addr_t. Linux is not affected, because its sa_family is 2 bytes there.

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.

@wjwithagen

wjwithagen commented Sep 25, 2026 •

Copy link
Copy Markdown
Member Author

@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
So fixed that as welll.

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]>
@wjwithagen
wjwithagen force-pushed the wip-freebsd-entity-addr-encode branch from e636f32 to a2c3329 Compare September 25, 2026 23:14

@tchaikov tchaikov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath14-testing-20260930.003013-main.

@dvanders dvanders left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@wjwithagen

Copy link
Copy Markdown
Member Author

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.

Feel free to enter this as a separate PR.... This one is already in QA, so I'd rather not disturbe this

@wjwithagen

Copy link
Copy Markdown
Member Author

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.

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.

@dvanders

dvanders commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Follow-up with the tests and the AF_INET6 fix: #72446.

@MattyWilliams22

Copy link
Copy Markdown
Contributor

@SrinivasaBharath
SrinivasaBharath merged commit 7e5362e into ceph:main Oct 16, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core FreeBSD Modifications specific for (only) FreeBSD needs-qa wip-bharath14-testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants