-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implementation is technically unsound #13
Comments
Not really necessary, since one can get the data pointer out of a pub unsafe fn downcast_ref_unchecked<T : $trait_>(&self) -> &T {
&*(self as *const _ as *const T)
} And this has already been done in this repo: it was done 5 years ago! 2bb823b But the crate seems to be unmaintained, and that commit never made it to crates.io |
GHSA-2gxj-qrp2-53jv Just linking the relevant CVE here. |
Any chance this can be fixed? |
@Cypher1, perhaps
|
Sadly a I've decided to go and publish https://crates.io/crates/mopa-maintained to palliate that, using my own otherwise-identical fork: master...danielhenrymantilla:mopa:v0.2.3 The fix is then to do: - mopa = "0.2.2"
+ mopa-maintained = "0.2.3" |
Thank you Daniel! I'll follow up with shred when I can to make sure all
shred users (including specs, amethyst etc.) get the fix.
…On Sat, 9 July 2022, 12:25 am Daniel Henry-Mantilla, < ***@***.***> wrote:
Sadly a patch section only works for the downstream user, so it would
require that specs use it when testing, and that further downstream users
do the same, etc.
I've decided to go and publish https://crates.io/crates/mopa-maintained
to palliate that.
The fix is then to do:
- mopa = "0.2.2"+ mopa-maintained = "0.2.3"
—
Reply to this email directly, view it on GitHub
<#13 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIRUHV5C37UUACAQS57O2TVTA25BANCNFSM45472VXQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
- The previous implementation uses `mopa`, which has a known safety issue: chris-morgan/mopa#13 - This commit essentially inlines the code generated by the `mopafy!` macro provided by `mopa`, thus removing the dependency - Since `mopa::Any` was part of the public interface of the `Resource` trait, this is technically a breaking change Fixes #217
@danielhenrymantilla sorry to ping this response after 2 years :) I just felt curious why casting can solve the problem here? As I know, the raw pointer to trait object is still fat pointer. Isn't it still unsound to cast a fat pointer? |
@shinmao if using fn as_<T : ?Sized>(p: *const T) -> *const () {
p as _
} which is guaranteed to extract the data pointer off the
|
@danielhenrymantilla thanks for the answer. So the difference with |
We could imagine having: //! Since the layout of wide pointers is unspecified, neither
//! `option_a` nor `option_b` are guaranteed to be sound!
fn option_a(p: *const dyn Trait) -> *const () {
let at_p = <*const _>::cast::<u8>(&p);
const OFFSET_TO_DATA_PTR: usize = 0; // ???
unsafe { at_p.add(OFFSET_TO_DATA_PTR).cast().read() }
}
fn option_b(p: *const dyn Trait) -> *const () {
let at_p = <*const _>::cast::<u8>(&p);
const OFFSET_TO_DATA_PTR: usize = mem::size_of::<&'static VTable>(); // ???
unsafe { at_p.add(OFFSET_TO_DATA_PTR).cast().read() }
} with the fn option_a(p: *const dyn Trait) -> *const () {
unsafe { transmute_copy(&p) }
} And since
And since this whole issue #13 is about the precise layout of wide pointers being unspecified, that would mean usage of And my point was basically that this first [META] If you want to keep talking about this, this Github issue may be causing too much "notification noise" to people already subscribed to it; so I'd advise to go elsewhere for this kind of discussions, such as the Rust Community Discord server (https://discord.gg/rust-lang-community), in the |
I'm talking about lines 167 to 173 of
lib.rs
:The macro expansion then goes on to use this declaration like this:
This code makes an assumption that the memory layout of
&dyn Trait
for any traitTrait
is the layout ofTraitObject
, which isn't a real guarantee that the Rust compiler is required to abide. Using the sameTraitObject
struct fromcore::raw
would require nightly, but break compilation when this assumption would be broken because of a change to how trait object references are stored, averting possible undefined behavior by stopping compilation of the code.The only sound alternative to this right now is to depend on a nightly compiler, make use of the unstable
Pointee
trait and await its stabilization, thus removing the nightly requirement.Keep in mind that this is a breaking change. Code which depends on the
0.2.x
line and is expected to be compiled with the stable compiler would break ifmopa
starts requiring nightly on that release line. This fix must therefore be deployed as0.3.0
or, preferably,1.0.0-rc1
(sincePointee
is not stable yet and its fate is not absolutely guaranteed). The previous0.2.x
and0.1.x
release lines may then be yanked.The text was updated successfully, but these errors were encountered: