Skip to content

Bound reference lifetimes sourced from pointer parameters - #697

Merged
ctz merged 7 commits into
mainfrom
jbp-lifetimes
Oct 26, 2026
Merged

ctz merged 7 commits into
mainfrom
jbp-lifetimes

Conversation

@ctz

@ctz ctz commented Oct 22, 2026

Copy link
Copy Markdown
Member

The goal of this PR is to arrange that the compiler complains about GHSA-rc3f-4j82-cmrg in a useful way, and then spread that pattern as far as possible.

With reordering the commits in this PR, this is achieved:

error[E0597]: `server_name` does not live long enough
   --> librustls/src/ffi.rs:470:46
    |
470 |               unsafe { $crate::ffi::bound_cstr(&$var) }
    |                                                ^^^^^ borrowed value does not live long enough
    |
   ::: librustls/src/client.rs:886:9
    |
886 |           server_name: *const c_char,
    |           ----------- binding `server_name` declared here
...
890 |           let server_name = try_cstr!(server_name);
    |                             ---------------------- in this macro invocation
...
901 | /                 ClientConnection::new_with_alpn(
902 | |                     try_clone_arc!(config),
903 | |                     server_name,
904 | |                     alpn_protocols,
905 | |                 )
    | |_________________- argument requires that `server_name` is borrowed for `'static`
...
910 |       }
    |        - `server_name` dropped here while still borrowed
    |
    = note: this error originates in the macro `try_cstr` (in Nightly builds, run with -Z macro-backtrace for more info)

@ctz
ctz requested a review from cpu October 22, 2026 12:12
Comment thread librustls/src/ffi.rs Fixed
Comment thread librustls/src/ffi.rs Fixed

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! This looks good to me.

I tacked on two commits at the end to try and get some targetted regression coverage. Those two commits applied to main without the rest of the rework fail in CI as expected.

Feel free to drop/rework them if you have opinions.

Comment thread librustls/src/acceptor.rs
Comment thread librustls/src/rslice.rs Outdated
Comment thread librustls/src/client.rs
Comment thread librustls/src/ffi.rs Fixed
@cpu

cpu commented Oct 25, 2026

Copy link
Copy Markdown
Member

#699 should fix the macOS CI failure.

@ctz
ctz force-pushed the jbp-lifetimes branch 4 times, most recently from f4556ca to eaba94b Compare October 26, 2026 16:18

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

documentation / Generate API documentation (pull_request) - Failing after 1m

I think you need to run the docgen too (sorry, lots of manual steps in this repo).

ctz and others added 7 commits October 26, 2026 16:26
This created a `&'static CStr` from a function parameter, retaining the caller's
memory.  Copy it instead.
Functions like `slice::from_raw_parts` and `CStr::from_ptr` infer the
lifetime of the resulting reference.  This makes it easy to take a pointer
argument and end up with a `'static` reference, which is not a common
interpretation of a C API.

Instead, take the pointer itself by reference to its local, and then
transfer that lifetime to the result.  This means the lifetimes created by
`try_ref_from_ptr!`, `try_mut_from_ptr!`, `try_slice!`, `try_slice_mut!`
and the new `try_cstr!` are bound to the local.

This exposes two places that relied on an inferred `'static`:
`Hpke::grease_public_key`, whose suites really are `'static`; and
`rustls_accepted_alpn`, which has preexisting documentation explaining
this and cannot express a better lifetime.
Help verify our claim that the server_name is copied by the rustls-ffi
internals by freeing it explicitly after
`rustls_client_connection_new()`.
@ctz
ctz merged commit 89ba39f into main Oct 26, 2026
64 checks passed
@ctz
ctz deleted the jbp-lifetimes branch October 26, 2026 16:51
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.

3 participants