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 gethostbyaddr for IPv6 arguments and make it domain-safe #11461

Merged
merged 2 commits into from Aug 1, 2022

Conversation

OlivierNicole
Copy link
Contributor

caml_gethostbyaddr always calls gethostbyaddr with address type AF_INET, i.e., IPv4, even when its argument is an IPv6 address. This commit also fixes a domain safety bug by splitting a function in two rather than using a file-local mutable variable.

@OlivierNicole OlivierNicole changed the title Fix gethostbyaddr for IPv6 arguments, remove mutable state Fix gethostbyaddr for IPv6 arguments and make it domain-safe Jul 28, 2022
@OlivierNicole
Copy link
Contributor Author

Unfortunately I am not able to test these changes properly, as IPv6 does not work on my setup… it is also unclear to me whether a test can be added to the test suite, given the uncertain nature of IPv6 support.

Copy link
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

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

I believe the patch is correct (modulo a small bug), and am tempted to merge it witout a test.

otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
@nojb
Copy link
Contributor

nojb commented Jul 28, 2022

Also a Changes entry is in order I think.

@OlivierNicole
Copy link
Contributor Author

Changes entry added.

Copy link
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

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

LGTM

@nojb
Copy link
Contributor

nojb commented Jul 29, 2022

@dra27 do you want to take a second look?

@dra27 dra27 self-requested a review July 29, 2022 07:21
Copy link
Member

@dra27 dra27 left a comment

Choose a reason for hiding this comment

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

Are you sure that IPv6 isn't working on your system - the code has definitely become domain-safe, but the IPv6 portion isn't yet correct. You should be able to do Unix.(gethostbyaddr (inet_addr_of_string "::1")). The correct flags (apart from an argument typo in the gethostbyaddr case!) were being passed, but the struct itself was wrong - it needs to be struct in6_addr for an IPv6 address, converted using GET_INET6_ADDR. I've put the version I just tested on my system in as review comments - this resolves both ::1 (localhost-ip6) and 2001:4860:4860::8888.

As far as I can tell, this has been wrong since the dawn of time?! 🙂 The IPv6 part of the fix should definitely go in the 4.14 branch as well.

Assuming all of the Jenkins hosts are IPv6-configured, checking that Unix.(gethostbyaddr (inet_addr_of_string "::1")) doesn't raise would be a good enough test, I think.

otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
otherlibs/unix/gethost.c Outdated Show resolved Hide resolved
@OlivierNicole OlivierNicole force-pushed the fixgethost branch 2 times, most recently from 5843d6f to bffb305 Compare August 1, 2022 09:43
@OlivierNicole
Copy link
Contributor Author

Thank you for the careful review! I confirm that your fixes allows me to resolve ::1 and 2001:4860:4860::8888. I added a test for ::1.

Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks. A couple of suggestions below concerning the test file.

Comment on lines 9 to 13
let () =
let addr = Unix.inet_addr_of_string "127.0.0.1" in
ignore @@ Sys.opaque_identity Unix.gethostbyaddr addr;
let addr = Unix.inet_addr_of_string "::1" in
ignore @@ Sys.opaque_identity Unix.gethostbyaddr addr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why opaque_identity? Also, you could check parts of the result, e.g. the protocol family:

Suggested change
let () =
let addr = Unix.inet_addr_of_string "127.0.0.1" in
ignore @@ Sys.opaque_identity Unix.gethostbyaddr addr;
let addr = Unix.inet_addr_of_string "::1" in
ignore @@ Sys.opaque_identity Unix.gethostbyaddr addr;
let check a ty =
let addr = Unix.inet_addr_of_string a in
let host = Unix.gethostbyaddr addr in
assert (host.Unix.h_addrtype = ty)
let () =
check "127.0.0.1" Unix.PF_INET;
check "::1" Unix.PF_INET6;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I integrated this suggestion.

Why opaque_identity?

I'm always a bit unsure about what might be optimized away.

testsuite/tests/lib-unix/common/gethostbyname.ml Outdated Show resolved Hide resolved
caml_gethostbyaddr would always call gethostbyaddr with an IPv4 address
type and structure, even when its argument is an IPv6 address. This
commit also removes a file-local mutable variable for the sake of domain
safety.
Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@xavierleroy
Copy link
Contributor

xavierleroy commented Aug 1, 2022

I'll merge and cherry-pick to 5.0 when CI is green.

@xavierleroy xavierleroy merged commit 4ec8c91 into ocaml:trunk Aug 1, 2022
xavierleroy pushed a commit that referenced this pull request Aug 1, 2022
caml_gethostbyaddr would always call gethostbyaddr with an IPv4 address
type and structure, even when its argument is an IPv6 address. This
commit also removes a file-local mutable variable for the sake of domain
safety.

Added a test.

(cherry picked from commit 4ec8c91)
@xavierleroy xavierleroy added the bug label Aug 1, 2022
@xavierleroy xavierleroy added this to the 5.0 milestone Aug 1, 2022
@OlivierNicole OlivierNicole deleted the fixgethost branch August 1, 2022 13:37
xavierleroy added a commit to xavierleroy/ocaml that referenced this pull request Aug 1, 2022
The fix from ocaml#11461 did not handle the case where HAS_IPV6 is not defined.
xavierleroy added a commit that referenced this pull request Aug 1, 2022
The fix from #11461 did not handle the case where HAS_IPV6 is not defined.
xavierleroy added a commit that referenced this pull request Aug 1, 2022
The fix from #11461 did not handle the case where HAS_IPV6 is not defined.

(cherry picked from commit c48641a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants