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

Zip may call __iterator_get_unchecked twice with the same index #82291

Closed
SkiFire13 opened this issue Feb 19, 2021 · 6 comments · Fixed by #82292
Closed

Zip may call __iterator_get_unchecked twice with the same index #82291

SkiFire13 opened this issue Feb 19, 2021 · 6 comments · Fixed by #82292
Labels
C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@SkiFire13
Copy link
Contributor

Here __iterator_get_unchecked is called for potential side effects until self.index == self.a.size(), ignoring however that it could have already been called in next_back with those indexes.

} else if A::may_have_side_effect() && self.index < self.a.size() {
let i = self.index;
self.index += 1;
// match the base implementation's potential side effects
// SAFETY: we just checked that `i` < `self.a.len()`
unsafe {
self.a.__iterator_get_unchecked(i);
}
None

Playground link that demonstrates how this can be exploited to get two mutable references to the same data and cause an use-after-free bug.

@SkiFire13 SkiFire13 added the C-bug Category: This is a bug. label Feb 19, 2021
@jackh726 jackh726 added I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 19, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 19, 2021
@hameerabbasi
Copy link
Contributor

Assigning P-critical as part of the WG-prioritization discussion on Zulip.

@hameerabbasi hameerabbasi added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Feb 19, 2021
@the8472
Copy link
Member

the8472 commented Feb 19, 2021

The gift that keeps on giving 🤣

Maybe it is time to get rid of TrustedRandomAccess as it exists today and replace it with a slightly safer (but still unsafe) variant? One that imposes the additional requirement on the caller that it must bring the iterator back into a safe state after it is done iterating.

The upside would be eliminating a lot of code in ZipImpl and enabling the same optimization for vec::IntoIter<T> where T: Drop and similar sources.
While the downside would be that external iteration (i.e. for loops and manual calls to next()) would cease to benefit from TrustedRandomAccess optimizations and only internal iteration methods (including for_each) would continue to do so.

@SkiFire13
Copy link
Contributor Author

I don't know, I feel like that would just make more complex to upheld the invariants. IMO a good improvement over the current __iterator_get_unchecked would be separating forward and backward iteration, this way it becomes easier to keep track of the state and I think would also make it possible to implement them for vec::IntoIter<T> where T: Drop

@the8472
Copy link
Member

the8472 commented Feb 19, 2021

I don't see how separating the forward and backward state would help with a drop implementation. The issue is that the source (IntoIter) currently does not have access to that state, instead the consumer drives the iteration by direct access through __iterator_get_unchecked and never informs the source about it.

@SkiFire13
Copy link
Contributor Author

SkiFire13 commented Feb 19, 2021

It would help because it allows the source iterator to keep track of its state by updating it when the method is called. Anyway, looks like someone already tried this approach back in 2016 and it resulted in worse optimizations, but maybe LLVM got better in the meantime, or maybe that implementation could have been better.

Anyway I don't think this is the right place to discuss this, a topic on zulip would probably be better.

@the8472
Copy link
Member

the8472 commented Feb 20, 2021

JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 6, 2021
Prevent specialized ZipImpl from calling `__iterator_get_unchecked` twice with the same index

Fixes rust-lang#82291

It's open for review, but conflicts with rust-lang#82289, wait before merging. The conflict involves only the new test, so it should be rather trivial to fix.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 6, 2021
Prevent specialized ZipImpl from calling `__iterator_get_unchecked` twice with the same index

Fixes rust-lang#82291

It's open for review, but conflicts with rust-lang#82289, wait before merging. The conflict involves only the new test, so it should be rather trivial to fix.
m-ou-se added a commit to m-ou-se/rust that referenced this issue Mar 6, 2021
Prevent specialized ZipImpl from calling `__iterator_get_unchecked` twice with the same index

Fixes rust-lang#82291

It's open for review, but conflicts with rust-lang#82289, wait before merging. The conflict involves only the new test, so it should be rather trivial to fix.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 6, 2021
Prevent specialized ZipImpl from calling `__iterator_get_unchecked` twice with the same index

Fixes rust-lang#82291

It's open for review, but conflicts with rust-lang#82289, wait before merging. The conflict involves only the new test, so it should be rather trivial to fix.
@bors bors closed this as completed in 1d5b2dc Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
5 participants