Skip to content

BTree's insert_fit breaks pointer provenance rules #78477

Closed

Description

@RalfJung

This code violates pointer provenance rules:

    fn insert_fit(&mut self, key: K, val: V) -> *mut V {
        debug_assert!(self.node.len() < CAPACITY);

        unsafe {
            slice_insert(self.node.keys_mut(), self.idx, key);
            slice_insert(self.node.vals_mut(), self.idx, val);
            self.node.as_leaf_mut().len += 1;

            self.node.val_mut_at(self.idx)
        }
    }

Specifically, self.node.keys_mut() returns a slice covering the previously existing elements of this node, but it is used to also access the new element one-past-the-end of the previous slice.

Either slice_insert needs to be passed a slice covering all the memory it needs to access (of type &mut [MaybeUninit<_>]), or else it needs to be passed a raw pointer (that may access the entire buffer) and a length. But keys_mut/vals_mut can only be used to access elements that already exist, not to initialize new elements.

Cc @ssomers

Activity

  1. ssomers commented on Oct 28, 2020

    @ssomers
    Contributor

    I was surprised when I realized that, but I think it has been like that for a long time. So I wonder if you're tightening up Miri or I should check out where we went wrong recently?

  2. RalfJung commented on Oct 28, 2020

    @RalfJung
    MemberAuthor

    I only found this when tightening Miri while debugging why #77187 broke BtreeMap (that PR should only break code that casts pointers to integers and back, with this code does not do AFAIK).

  3. ssomers commented on Oct 28, 2020

    @ssomers
    Contributor

    Ok. The fix conflicts with #78476, so maybe it's better to add it onto that PR?

  4. RalfJung commented on Oct 28, 2020

    @RalfJung
    MemberAuthor

    Probably, yes. I spent enough time on this for now that I should have used for the work I am paid to do. ;) So I decided to just open the issue instead of trying to fix this problem myself.

  5. added
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    C-bugCategory: This is a bug.
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Oct 28, 2020
  6. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 28, 2020
  7. RalfJung commented on Oct 28, 2020

    @RalfJung
    MemberAuthor

    rust-lang/miri#1603 adds a flag to Miri that lets you opt-in to that more strict tracking yourself. Note that this might reject some valid code when integer-pointer casts are involved.

  8. spastorino commented on Oct 28, 2020

    @spastorino
    Member

    Assigning P-high as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

  9. added
    P-highHigh priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 28, 2020
  10. RalfJung commented on Oct 28, 2020

    @RalfJung
    MemberAuthor

    FWIW, these are violations with Stacked Borrows -- but the actual impact is likely low as we barely exploit aliasing assumptions currently. So IMO there is no reason to treat this as high priority. It barely qualifies as a soundness issue, given that Stacked Borrows is purely experimental (and not RFC'd or anything).

  11. added and removed
    P-highHigh priority
    on Oct 29, 2020
  12. added a commit that references this issue on Nov 9, 2020
    2187f3c
  13. added
    A-btreeArea: `std::collections::{BTreeMap, BTreeSet}`
    and removed
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    on Nov 2, 2026
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-btreeArea: `std::collections::{BTreeMap, BTreeSet}`C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-libsRelevant to the library 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