-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Tracking issue for stabilizing Error::type_id
#60784
Comments
This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting rust-lang#58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A security announcement will be shortly posted to the security mailing list as well as the Rust Blog, and when those links are available they'll be filled in for this PR as well. This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at rust-lang#60784.
This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting rust-lang#58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A security announcement will be shortly posted to the security mailing list as well as the Rust Blog, and when those links are available they'll be filled in for this PR as well. This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at rust-lang#60784.
This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting rust-lang#58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A security announcement will be shortly posted to the security mailing list as well as the Rust Blog, and when those links are available they'll be filled in for this PR as well. This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at rust-lang#60784.
Here's an unbaked thought: Can we make an extension |
I think my unbaked idea is not implementable in current Rust:
The only problem is we can't add a "where Self:Sized" bound to the "{ self as _ }" part. |
It’s tempting to make However |
@SimonSapin Wasn't relating Any's bound discussed at some point? |
On unstable we have |
…=pietroalbini [beta] Destabilize the `Error::type_id` function This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting #58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A formal announcement has been made to the [security mailing list](https://groups.google.com/forum/#!topic/rustlang-security-announcements/aZabeCMUv70) as well as [the blog](https://blog.rust-lang.org/2019/05/13/Security-advisory.html) This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at #60784.
I'm not sure why this functionality should be left to the library at all. Maybe we really need a |
@skade Yes, this was discussed to say that it would be unsound: #41875 (comment) @cuviper This sounds doable, but would require a If we add If rust-lang/rfcs#2580 is accepted and implemented we could have |
Yeah, I see -- I guess I'm basically proposing the equivalent of C++ RTTI and
In the context of replacing
|
Double I think this inconsistency in the proposed semantics make this not a good design. For an operation that only makes sense on trait objects, I’d prefer to have a type signature that requires a trait object. Which goes back to rust-lang/rfcs#2580 again. |
Other idea: can we replace the This might be a breaking change because generic code with a |
I believe that'd break code like this: fn box_me<T: Debug + Any>(t: T) -> Box<dyn Debug> {
Box::new(t)
} |
… r=pietroalbini [stable] Destabilize the `Error::type_id` function This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting #58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A formal announcement has been made to the [security mailing list](https://groups.google.com/forum/#!topic/rustlang-security-announcements/aZabeCMUv70) as well as [the blog](https://blog.rust-lang.org/2019/05/13/Security-advisory.html) This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at #60784.
Isn't that the exact same variability as use std::mem::size_of_val;
fn main() {
let mut range = 0..10;
println!("range: {}", size_of_val(&range));
let mut iter: &mut dyn Iterator<Item = i32> = &mut range;
println!("iter: {}", size_of_val(iter));
let iter2: &mut dyn Iterator<Item = i32> = &mut iter;
println!("iter2: {}", size_of_val(iter2));
}
|
Let’s take a
“Dynamically-sized” is a notion that exists in the language, so the definition of In fact this is the only possible answer for “the size of the value behind the |
Unfortunately, this is hardly practical, from a quick Rust source code search, there is a lot code using Would be cool if it could be done, but unfortunately it cannot be. I guess what can be done is deprecating |
… r=pietroalbini Destabilize the `Error::type_id` function This commit destabilizes the `Error::type_id` function in the standard library. This does so by effectively reverting #58048, restoring the `#[unstable]` attribute. The security mailing list has recently been notified of a vulnerability relating to the stabilization of this function. First stabilized in Rust 1.34.0, a stable function here allows users to implement a custom return value for this function: struct MyType; impl Error for MyType { fn type_id(&self) -> TypeId { // Enable safe casting to `String` by accident. TypeId::of::<String>() } } This, when combined with the `Error::downcast` family of functions, allows safely casting a type to any other type, clearly a memory safety issue! A formal announcement has been made to the [security mailing list](https://groups.google.com/forum/#!topic/rustlang-security-announcements/aZabeCMUv70) as well as [the blog](https://blog.rust-lang.org/2019/05/13/Security-advisory.html) This commit simply destabilizes the `Error::type_id` which, although breaking for users since Rust 1.34.0, is hoped to have little impact and has been deemed sufficient to mitigate this issue for the stable channel. The long-term fate of the `Error::type_id` API will be discussed at #60784.
Then the |
An alternative to making it use self::internal::Internal;
trait Error {
/// Since you can never import the `Internal` type, you can never override this method.
fn __type_id(&self, _: Internal) -> TypeId where Self: 'static {
TypeId::of::<Self>()
}
}
impl dyn Error + 'static {
fn type_id(&self) -> TypeId {
self.__type_id(Internal)
}
}
mod internal {
pub struct Internal;
} |
Making The private type approach should definitely work, though! |
type_id now takes an argument that can't be named outside of the std::error module, which prevents any implementations from overriding it. It's a pretty grody solution, and there's no way we can stabilize the method with this API, but it avoids the soudness issue! Closes rust-lang#60784
I opened up a PR using @seanmonstar's private type approach to patch the soundness hole for now: #60902 |
This one is partly on me. After we did FCP to stabilize in #27745, I grepped for unstable attributes that pointed there. I didn’t pay attention to the Perhaps this method was never intended to be user-visible, but it’s hard to say since this was four years ago (#24793). |
…crichton Prevent Error::type_id overrides type_id now takes an argument that can't be named outside of the std::error module, which prevents any implementations from overriding it. It's a pretty grody solution, and there's no way we can stabilize the method with this API, but it avoids the soudness issue! Closes rust-lang#60784 r? @alexcrichton
given that #60902 (comment) says
should this not be reopened? |
Yes I think it's fine to reopen this as a tracking issue for stability. |
Error::type_id
are memory unsafeError::type_id
I wouldn't want to stabilize this with the private type hack it currently uses, so I think a blocker on stabilizing this API is one of these two things:
|
what about something like: pub unsafe trait ErrorTypeId {
fn type_id(&self) -> TypeId where Self: 'static {
TypeId::of::<Self>()
}
}
unsafe impl<T: ?Sized> ErrorTypeId for T {}
pub trait Error: Debug + Display + ErrorTypeId {
// ...
} |
@programmerjake I've slightly extended this by making it not specific to Error and wrote up an RFC. rust-lang/rfcs#2738 |
@leo60228 Thanks! |
What's going on on this? |
trait method pub impl(self) fn type_id(&self) -> TypeId {
...
} |
Updated Issue
This is a tracking issue for stabilizing the functionality of
Error::type_id
somehow. The subject of a historical security advisory the API was recently changed to prevent memory unsafety issues on all channels including nightly. The functionality, however, is still unstable, so we should stabilize it at some point!Original issue.
Reported by @seanmonstar to the security mailing list recently, it was discovered that the recent stabilization of
Error::type_id
in Rust 1.34.0 is actually not memory safe. Described in a recent security announcement the stabilization ofError::type_id
has been reverted for stable, beta, and master.This leaves us, however, with the question of what to do about this API?
Error::type_id
has been present since the inception of theError
trait, all the way back to 1.0.0. It's unstable, however, and is pretty rare as well to have a manual implementation of thetype_id
function. Despite this we would ideally still like a path to stability which includes safety at some point.This tracking issue is intended to serve as a location to discuss this issue and determine the best way forward to fully removing
Error::type_id
(so even nightly users are not affected by this memory safety issue) and having a stable mechanism for the functionality.The text was updated successfully, but these errors were encountered: