-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add allow-by-default lints for all attributes with unsafe behaviors #82499
Comments
Tangentially related: macros that "hide" the unsafe keyword behind a macro call: https://rustsec.org/advisories/RUSTSEC-2020-0011.html |
I think all of these should be part of |
Given that Can |
|
I don't see any obvious way that Likewise
I'd expect many uses of |
Linking libraries (even if not otherwise used) can cause implicit invocation of FFI calls through the constructors/destructors that library defines. Those can cause similar problems as calling any other FFI function, though there's nothing you can do about them either, other than not linking to the library. There was a recent issue in libloading related to this. |
use core::sync::atomic::{AtomicUsize, Ordering};
#[link_section = ".rodata"]
static BAD: AtomicUsize = AtomicUsize::new(0);
fn main() {
BAD.store(42, Ordering::SeqCst);
} |
That uses interior mutability - could we add a more targeted lint that warns against putting anything with interior mutability (or any |
#[link_section = ".init_array"]
static BAD: usize = 0;
fn main() {} |
yeah it's easier to just forbid link_section than try to carve out exactly how it might be misused. You're never required to forbid unsafe_code anyway, so we can be overly conservative about the lint. |
You can also use link_section to put functions into .rodata, or .data, or any section other than a .text section |
that's only usually UB |
I mean, I'd presume it must be at least conditionally-supported. Otherwise, it wouldn't be allowed to segfault on properly secured systems that don't allow executing .data. |
Right. Some embedded systems allow it, but you can generally assume that a "modern" system with an OS and all that won't let you play fast and loose with the program counter. |
It seems like we have rough consensus that I suppose the next step is to open an individual issue for each of them? (and at that point, perhaps this issue can be closed out unless anyone knows of any others) |
Given that all three of those are link-related, I think it'd be reasonable to use one issue for all three. The next step would likely be a PR adding them to |
|
Created #97086 for |
Complementing the efforts tracked here, I think we should move towards the more principled solution of having a concept of 'unsafe attributes'. |
With unsafe attributes landing in 1.82, perhaps this can be closed? |
Yeah true, this can be closed. Through unfortunately the unsafe attribute implementation built up parallel infrastructure to what was done as part of this issue, meaning there is some cleanup to be done: #131801. |
In #72188, an allow-by-default lint was added against
#[no_mangle]
, which previously permitted unsafety even in the event that#![forbid(unsafe_code)]
was used.I think it'd be a good idea to collect a list of all such "unsafe attributes" and try to add similar lints for them on a case-by-case basis. This shouldn't be terribly difficult to do by extracting them from the Built-in attributes index from the Rust Reference.
I've started a list below. Please leave comments and I'll update it (or anyone else who has the power to do so, feel free). Items in the list with
strikethroughare deemed out-of-scope.ABI, linking, symbols, and FFI
link
link_ordinal
(see Tracking issue forlink_args
link_args
stabilization #29596)(see this comment)link_name
(see this comment)repr
repr(packed)
export_name
(Add an allow-by-default lint against #[no_mangle] #72188, Add checking for no_mangle to unsafe_code lint #72209)link_section
(Report unsafe for overriding link sections #97086)no_mangle
(Add an allow-by-default lint against #[no_mangle] #72188, Add checking for no_mangle to unsafe_code lint #72209)(see this comment)used
Others?
Please leave comments with suggestions or corrections.
The text was updated successfully, but these errors were encountered: