-
Notifications
You must be signed in to change notification settings - Fork 948
fix: TOCTOU issue in network config #8551
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
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,21 +26,6 @@ pub fn unused_tcp4_port() -> Result<u16, String> { | |
| zero_port(Transport::Tcp, IpVersion::Ipv4) | ||
| } | ||
|
|
||
| /// A convenience wrapper over [`zero_port`]. | ||
| pub fn unused_udp4_port() -> Result<u16, String> { | ||
| zero_port(Transport::Udp, IpVersion::Ipv4) | ||
| } | ||
|
|
||
| /// A convenience wrapper over [`zero_port`]. | ||
| pub fn unused_tcp6_port() -> Result<u16, String> { | ||
| zero_port(Transport::Tcp, IpVersion::Ipv6) | ||
| } | ||
|
|
||
| /// A convenience wrapper over [`zero_port`]. | ||
| pub fn unused_udp6_port() -> Result<u16, String> { | ||
| zero_port(Transport::Udp, IpVersion::Ipv6) | ||
| } | ||
|
|
||
| /// A bit of hack to find an unused port. | ||
| /// | ||
| /// Does not guarantee that the given port is unused after the function exits, just that it was | ||
|
|
@@ -97,3 +82,31 @@ fn find_unused_port(transport: Transport, socket_addr: SocketAddr) -> Result<u16 | |
|
|
||
| Ok(local_addr.port()) | ||
| } | ||
|
|
||
| /// Bind a TCPv4 listener on localhost with an ephemeral port (port 0) and return it. | ||
| /// Safe against TOCTOU: the socket remains open and reserved by the OS. | ||
| pub fn bind_tcp4_any() -> Result<TcpListener, String> { | ||
| let addr = std::net::SocketAddr::new(std::net::Ipv4Addr::LOCALHOST.into(), 0); | ||
| TcpListener::bind(addr).map_err(|e| format!("Failed to bind TCPv4 listener: {:?}", e)) | ||
| } | ||
|
|
||
| /// Bind a TCPv6 listener on localhost with an ephemeral port (port 0) and return it. | ||
| /// Safe against TOCTOU: the socket remains open and reserved by the OS. | ||
| pub fn bind_tcp6_any() -> Result<TcpListener, String> { | ||
| let addr = std::net::SocketAddr::new(std::net::Ipv6Addr::LOCALHOST.into(), 0); | ||
| TcpListener::bind(addr).map_err(|e| format!("Failed to bind TCPv6 listener: {:?}", e)) | ||
| } | ||
|
|
||
| /// Bind a UDPv4 socket on localhost with an ephemeral port (port 0) and return it. | ||
| /// Safe against TOCTOU: the socket remains open and reserved by the OS. | ||
| pub fn bind_udp4_any() -> Result<UdpSocket, String> { | ||
| let addr = std::net::SocketAddr::new(std::net::Ipv4Addr::LOCALHOST.into(), 0); | ||
| UdpSocket::bind(addr).map_err(|e| format!("Failed to bind UDPv4 socket: {:?}", e)) | ||
| } | ||
|
|
||
| /// Bind a UDPv6 socket on localhost with an ephemeral port (port 0) and return it. | ||
| /// Safe against TOCTOU: the socket remains open and reserved by the OS. | ||
| pub fn bind_udp6_any() -> Result<UdpSocket, String> { | ||
| let addr = std::net::SocketAddr::new(std::net::Ipv6Addr::LOCALHOST.into(), 0); | ||
| UdpSocket::bind(addr).map_err(|e| format!("Failed to bind UDPv6 socket: {:?}", e)) | ||
| } | ||
|
Comment on lines
+88
to
+112
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these functions used anywhere?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those methods are not used, there there as part of requirements from issue.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC the issue mentions
not that they are required right?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so to get rid of theoretical toctou when actually running the node, you just need to set port to zero when your setting up config (you dont need those methods). as for the integ tests that is a separate issue, please read next step section because that will influence if these methods stay or not. They were not used in original CR from @sashaodessa either |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need all these tests?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can lighten them up a bit