-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
336: Prevent abuse of private module r=taiki-e a=taiki-e *This was originally reported by `@danielhenrymantilla` in https://discord.com/channels/273534239310479360/512792629516173323/870075511009857617*. Currently, you can break the soundness of the pin-project by overriding the private module as follows. Considering that it uses a hidden private API that is not guaranteed to be stable, I don't think this will actually happen except in cases where users intentionally abuse it or use a malicious crate. Also, in such a case, fixing this would not help much because the attacker can do anything. That said, it seems relatively easy to fix this, so I'm going to fix this. ```rust extern crate pin_project as pin_project_orig; extern crate self as pin_project; pub use ::pin_project_orig::*; mod __private { pub use ::pin_project_orig::__private::*; pub trait Drop {} } use std::{marker::PhantomPinned, mem}; #[pin_project] struct S { #[pin] f: (u8, PhantomPinned), } impl Drop for S { fn drop(&mut self) { let prev = &self.f.0 as *const _ as usize; let moved = mem::take(&mut self.f); // move pinned field let moved = &moved.0 as *const _ as usize; assert_eq!(prev, moved); // panic } } fn main() { let mut x = Box::pin(S { f: (1, PhantomPinned) }); let _f = x.as_mut().project().f; // first mutable access } ``` [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=faf882d7f20845d992817a21ce444463) --- This patch forces the macro to refer to the `pin-project` crate by referring to the `_pin_project` imported into the `const _` scope instead of `::pin_project`. https://github.com/taiki-e/pin-project/blob/74444360566ccef04da2bde8f0273469df6ad67b/tests/expand/pub/struct.expanded.rs#L21-L23 https://github.com/taiki-e/pin-project/blob/74444360566ccef04da2bde8f0273469df6ad67b/tests/expand/pub/struct.expanded.rs#L49 In the above example, a user-implemented Drop is detected, resulting in an error. https://github.com/taiki-e/pin-project/blob/74444360566ccef04da2bde8f0273469df6ad67b/tests/ui/pin_project/override-priv-mod.stderr#L1-L10 If other items (e.g., `PinnedDrop`) are replaced in the same way, either nothing happens (nothing is affected), or `#[pin_project]` and `#[pinned_drop]` refer to different items, resulting in an error. Co-authored-by: Taiki Endo <[email protected]>
- Loading branch information
Showing
43 changed files
with
780 additions
and
627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.