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

Add timeout to the tcp connection for the pq key negotiation ios 701 #6343

Conversation

buggmagnet
Copy link
Contributor

@buggmagnet buggmagnet commented Jun 12, 2024

This PR adds an exponential backoff timeout when negotiating post quantum keys.
The timeout plays 2 important part :

  • Trigger a reset of the key exchange process if the in-tunnel TCP connection does not become ready in time
  • Trigger a reset of the key exchange process if it takes too long to exchange keys.

This PR was co-authored by @pinkisemils and @buggmagnet
@MarkusPettersson98's approval is required for the rust side changes
@acb-mv's approval is required for the swift side changes.


This change is Reviewable

@buggmagnet buggmagnet added the iOS Issues related to iOS label Jun 12, 2024
@buggmagnet buggmagnet self-assigned this Jun 12, 2024
Copy link

linear bot commented Jun 12, 2024

Copy link
Contributor

@acb-mv acb-mv left a comment

Choose a reason for hiding this comment

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

Reviewed 16 of 16 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)


ios/MullvadPostQuantum/PostQuantumKeyNegotiator.swift line 17 at r1 (raw file):

// swiftlint:disable function_parameter_count
public protocol PostQuantumKeyNegotiation {

Are you sure about the name? This doesn't look like a negotiation but rather as something that negotiates. Perhaps PostQuantumKeyNegotiating would be a better name.

Copy link
Contributor Author

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewable status: 13 of 16 files reviewed, 1 unresolved discussion (waiting on @acb-mv)


ios/MullvadPostQuantum/PostQuantumKeyNegotiator.swift line 17 at r1 (raw file):

Previously, acb-mv wrote…

Are you sure about the name? This doesn't look like a negotiation but rather as something that negotiates. Perhaps PostQuantumKeyNegotiating would be a better name.

Done

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 16 files at r1, all commit messages.
Reviewable status: 13 of 16 files reviewed, 8 unresolved discussions (waiting on @acb-mv and @buggmagnet)


talpid-tunnel-config-client/src/ios_ffi/ios_runtime.rs line 100 at r1 (raw file):

    }
    /// Creates a `RelayConfigService` using the in-tunnel TCP Connection provided by the Packet
    /// Tunnel Provider # Safety

Nit A bit of a weird line formatting here 🤓

Code quote:

   /// Tunnel Provider # Safety

talpid-tunnel-config-client/src/ios_ffi/ios_runtime.rs line 159 at r1 (raw file):

                                    log::error!("No suitable peer was found");
                                    swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null());
                                }

Nit The log-statement shouldn't be in the unsafe block. Consider moving the unsafe token to the line where it is actually needed

None => {
    log::error!("No suitable peer was found");
    unsafe { swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null()) };
}

Code quote:

                                None => unsafe {
                                    log::error!("No suitable peer was found");
                                    swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null());
                                }

talpid-tunnel-config-client/src/ios_ffi/ios_runtime.rs line 166 at r1 (raw file):

                            log::error!("Key exchange failed {}", error);
                            swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null());
                        }

Dito

Code quote:

                        Err(error) => unsafe {
                            log::error!("Key exchange failed {}", error);
                            swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null());
                        }

talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 69 at r1 (raw file):

    /// `tcp_connection` must be pointing to a valid instance of a `NWTCPConnection`, created by the
    /// `PacketTunnelProvider`
    pub unsafe fn new(connection: Arc<Mutex<ConnectionContext>>) -> (Self, IosTcpShutdownHandle) {

Nit The comment says tcp_connection, but the function parameter is named connection 😊

Code quote:

    /// # Safety
    /// `tcp_connection` must be pointing to a valid instance of a `NWTCPConnection`, created by the
    /// `PacketTunnelProvider`
    pub unsafe fn new(connection: Arc<Mutex<ConnectionContext>>) -> (Self, IosTcpShutdownHandle) {

talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 107 at r1 (raw file):

            std::mem::drop(context);
        }
    }

Nit Seems like there is an unncecessary level of indentation here?

pub fn shutdown(self) {
    let Ok(mut context) = self.context.lock() else {
        return;
    };

    context.tcp_connection = None;
    if let Some(waker) = context.waker.take() {
        waker.wake();
    }
    std::mem::drop(context);
}

Code quote:

    pub fn shutdown(self) {
        {
            let Ok(mut context) = self.context.lock() else {
                return;
            };

            context.tcp_connection = None;
            if let Some(waker) = context.waker.take() {
                waker.wake();
            }
            std::mem::drop(context);
        }
    }

talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 139 at r1 (raw file):

                    unsafe {
                        swift_nw_tcp_connection_send(
                            // self.tcp_connection,

Comment

Code quote:

                            // self.tcp_connection,

talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 197 at r1 (raw file):

                    unsafe {
                        // TODO
                        swift_nw_tcp_connection_read(tcp_ptr, raw_sender as _);

TODO

Code quote:

                        // TODO
                        swift_nw_tcp_connection_read(tcp_ptr, raw_sender as _);

Copy link
Contributor Author

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewable status: 13 of 16 files reviewed, 6 unresolved discussions (waiting on @acb-mv, @MarkusPettersson98, and @pinkisemils)


talpid-tunnel-config-client/src/ios_ffi/ios_runtime.rs line 159 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Nit The log-statement shouldn't be in the unsafe block. Consider moving the unsafe token to the line where it is actually needed

None => {
    log::error!("No suitable peer was found");
    unsafe { swift_post_quantum_key_ready(packet_tunnel_ptr, ptr::null(), ptr::null()) };
}

Done


talpid-tunnel-config-client/src/ios_ffi/ios_runtime.rs line 166 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Dito

Done.


talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 69 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Nit The comment says tcp_connection, but the function parameter is named connection 😊

Done


talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 107 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Nit Seems like there is an unncecessary level of indentation here?

pub fn shutdown(self) {
    let Ok(mut context) = self.context.lock() else {
        return;
    };

    context.tcp_connection = None;
    if let Some(waker) = context.waker.take() {
        waker.wake();
    }
    std::mem::drop(context);
}

Indeed ! Fixed.


talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 139 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Comment

Done.


talpid-tunnel-config-client/src/ios_ffi/ios_tcp_connection.rs line 197 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

TODO

I think it's just a leftover of a different idea @pinkisemils was thinking about.
I've removed it.

@buggmagnet buggmagnet force-pushed the add-timeout-to-the-tcp-connection-for-the-pq-key-negotiation-ios-701 branch from 1484f8b to 76d6ac5 Compare June 14, 2024 08:33
Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 4 files at r3, all commit messages.
Reviewable status: 11 of 16 files reviewed, 2 unresolved discussions (waiting on @acb-mv and @buggmagnet)

Copy link
Contributor

@acb-mv acb-mv left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r2, 2 of 4 files at r3, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)

@buggmagnet buggmagnet force-pushed the add-timeout-to-the-tcp-connection-for-the-pq-key-negotiation-ios-701 branch from 76d6ac5 to 0becdb7 Compare June 17, 2024 11:02
@buggmagnet buggmagnet force-pushed the add-timeout-to-the-tcp-connection-for-the-pq-key-negotiation-ios-701 branch from 0becdb7 to 0af08f5 Compare June 17, 2024 11:22
@buggmagnet buggmagnet merged commit 4204b0f into main Jun 17, 2024
25 of 26 checks passed
@buggmagnet buggmagnet deleted the add-timeout-to-the-tcp-connection-for-the-pq-key-negotiation-ios-701 branch June 17, 2024 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants