Skip to content

Commit

Permalink
Fix a lingering panic by using a stricter type
Browse files Browse the repository at this point in the history
The functions `request_ephemeral_peer` and consecutively `new_client`
accepted an `IpAddr`, but due to only ever preparing a v4 socket this
lead to panic due to an `EAFNOSUPPORT` error if an IPv6 was provided.

It would also have made sense to change `new_client` to create either
an IPv4 or IPv6 socket depending on the type of the address, but the
tuncfg service is currently not accepting IPv6 connections, therefore
this was the cleaner change.
  • Loading branch information
maxz committed Aug 15, 2024
1 parent 532b8ad commit 4f69dbb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
3 changes: 1 addition & 2 deletions talpid-tunnel-config-client/examples/psk-exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Usage: ./psk-exchange <tuncfg_server_ip> <wireguard_public_key>
// e. g. ./psk-exchange 10.64.0.1 NkECLsf+VbZUjve7RVN6sE3NYUcYUmUn8qpFugqbXFk=

use std::net::IpAddr;
use talpid_types::net::wireguard::{PrivateKey, PublicKey};

#[tokio::main]
Expand All @@ -24,7 +23,7 @@ async fn main() {
let ephemeral_private_key = PrivateKey::new_from_random();

let ephemeral_peer = talpid_tunnel_config_client::request_ephemeral_peer(
IpAddr::V4(tuncfg_server_ip),
tuncfg_server_ip,
public_key, // Parent connection's public key.
ephemeral_private_key.public_key(),
true, // Whether to negotiate a "PQ-safe" PSK.
Expand Down
9 changes: 5 additions & 4 deletions talpid-tunnel-config-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proto::PostQuantumRequestV1;
use std::fmt;
#[cfg(not(target_os = "ios"))]
use std::net::IpAddr;
#[cfg(not(target_os = "ios"))]
use std::net::SocketAddr;
#[cfg(not(target_os = "ios"))]
use std::net::{IpAddr, Ipv4Addr};
use talpid_types::net::wireguard::{PresharedKey, PublicKey};
#[cfg(not(target_os = "ios"))]
use tokio::net::TcpSocket;
Expand Down Expand Up @@ -189,7 +189,7 @@ pub async fn request_ephemeral_peer_with(
/// Negotiate a short-lived peer with a PQ-safe PSK or with DAITA enabled.
#[cfg(not(target_os = "ios"))]
pub async fn request_ephemeral_peer(
service_address: IpAddr,
service_address: Ipv4Addr,
parent_pubkey: PublicKey,
ephemeral_pubkey: PublicKey,
enable_post_quantum: bool,
Expand Down Expand Up @@ -245,8 +245,9 @@ fn xor_assign(dst: &mut [u8; 32], src: &[u8; 32]) {
}

#[cfg(not(target_os = "ios"))]
async fn new_client(addr: IpAddr) -> Result<RelayConfigService, Error> {
async fn new_client(addr: Ipv4Addr) -> Result<RelayConfigService, Error> {
let endpoint = Endpoint::from_static("tcp://0.0.0.0:0");
let addr = IpAddr::V4(addr);

let conn = endpoint
.connect_with_connector(service_fn(move |_| async move {
Expand Down
2 changes: 1 addition & 1 deletion talpid-wireguard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ impl WireguardMonitor {
let ephemeral = tokio::time::timeout(
timeout,
talpid_tunnel_config_client::request_ephemeral_peer(
IpAddr::from(config.ipv4_gateway),
config.ipv4_gateway,
config.tunnel.private_key.public_key(),
wg_psk_pubkey,
enable_pq,
Expand Down

0 comments on commit 4f69dbb

Please sign in to comment.