Skip to content

implied bound computation can use overly restrictive where-clauses to have stronger assumptions #162066

Description

@lcnr

original found by @carlini, it's bug 8 from zulip https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/Reporting.20a.20collection.20of.20soundness.20bugs.20in.20next-solver/near/617737400

trait Tr<'r> {
    type Out;
}
impl<'r, T> Tr<'r> for T {
    type Out = &'r ();
}

struct Foo<'b, X: Tr<'b> + 'b>(X, &'b ())
where
    X::Out: 'b;


fn bad<'a, 'b, X>(_wf: <Foo<'b, X> as Tr<'b>>::Out, s: &'a str) -> &'b str
where
    'b: 'a,
    X: Tr<'a, Out = &'a ()>,
{
    s
}

fn main() {
    let s = String::from("use after free?");
    let r: &'static str = bad::<()>(&(), &s);
    drop(s);
    println!("{r}");
}

The underlying issue is as follows:

  • we assume Foo<'b, X> is wf, this implies X::Out: 'b
  • proving X: Tr<'b> in the caller normalizes this to &'b (): 'b which trivially holds
  • when checking the function, we instead use the X: Tr<'a, Out = &'a ()> where bound, equating 'a and 'b
  • this should result in an error, however, the combination of the 'b: 'a bound and the implied &'a (): 'b bound mean that this is actually accepted.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-implied-boundsArea: Implied bounds / inferred outlives-boundsC-bugCategory: This is a bug.I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions