Skip to content

common/wireaddr: validate port range in separate_address_and_port - #9637

Open
bhuvan-somisetty wants to merge 1 commit into
ElementsProject:masterfrom
bhuvan-somisetty:fix-wireaddr-port-truncation
Open

bhuvan-somisetty wants to merge 1 commit into
ElementsProject:masterfrom
bhuvan-somisetty:fix-wireaddr-port-truncation

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown

Description

In common/wireaddr.c, separate_address_and_port() parsed the port suffix with strtol() and assigned the result directly to *port (type u16), causing integer truncation modulo 65536. Consequently:

  • Out-of-range ports > 65535 (such as 127.0.0.1:65537 or 127.0.0.1:75271) were accepted and silently converted to lower ports (1 and 9735).
  • Negative ports such as 127.0.0.1:-1 were converted to 65535 and accepted.
  • Signed (+9735) and space-padded ports were accepted by strtol.

This PR fixes separate_address_and_port() by parsing port strings with str_to_u64() to enforce strict unsigned decimal digits and rejecting ports that are 0 or greater than 65535. Unit tests covering valid boundaries and invalid ports have been added to common/test/run-ip_port_parsing.c.

Fixes #9636.

Checklist

Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:

  • The changelog has been updated in the relevant commit(s) according to the guidelines.
  • Tests have been added or modified to reflect the changes.
  • Documentation has been reviewed and updated as needed.
  • Related issues have been listed and linked, including any that this PR closes.
  • Important All PRs must consider how to reverse any persistent changes for tools/lightning-downgrade

Signed-off-by: bhuvan-somisetty somisettybhuvan5@gmail.com

`separate_address_and_port()` used `strtol()` and assigned the result
directly to `*port` (type `u16`), which truncated numbers modulo 65536.
As a result, out-of-range ports like `127.0.0.1:65537` and `127.0.0.1:75271`
were accepted and truncated to ports 1 and 9735, negative ports like `-1`
were accepted as 65535, and signed or space-padded ports were accepted.

Use `str_to_u64()` to parse strict unsigned decimal digits and reject
ports equal to 0 or greater than 65535.

Changelog-Fixed: common/wireaddr: reject out-of-range (>65535) and negative ports in separate_address_and_port
Fixes: ElementsProject#9636
Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>

This branch has not been deployed

No deployments
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.

common/wireaddr: separate_address_and_port truncates out-of-range port numbers and accepts negative/signed ports

1 participant