Skip to content

Add -Zsanitize=kernel-hwaddress#153049

Open
Darksonn wants to merge 4 commits intorust-lang:mainfrom
Darksonn:kasan-sw-tags
Open

Add -Zsanitize=kernel-hwaddress#153049
Darksonn wants to merge 4 commits intorust-lang:mainfrom
Darksonn:kasan-sw-tags

Conversation

@Darksonn
Copy link
Member

@Darksonn Darksonn commented Feb 24, 2026

View all comments

The Linux kernel has a config option called CONFIG_KASAN_SW_TAGS that enables -fsanitize=kernel-hwaddress. This is not supported by Rust.

One slightly awkward detail is that #[sanitize(address = "off")] applies to both -Zsanitize=address and -Zsanitize=kernel-address. Probably it was done this way because both are the same LLVM pass. I replicated this logic here for hwaddress, but it might be undesirable.

Note that #[sanitize(kernel_hwaddress = "off")] could be supported as an annotation on statics, but since it's also missing for #[sanitize(hwaddress = "off")], I did not add it.

MCP: rust-lang/compiler-team#975

cc @rcvalle @maurer @ojeda

@Darksonn Darksonn added the A-sanitizers Area: Sanitizers for correctness and code quality label Feb 24, 2026
@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-testsuite Area: The testsuite used to check the correctness of rustc PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment on lines +127 to +135
// KHWASAN: In LLVM versions prior to 21.1.0, the HWAddressSanitizer pass incorrectly
// ignores the pass-specific `CompileKernel` flag and only looks at the global command-line
// flag `-hwasan-kernel`. To work around this, pass `-hwasan-kernel` on the relevant LLVM
// versions.
//
// Fixed by: [HWASan][bugfix] Fix kernel check in ShadowMapping::init (#142226).
if sess.sanitizers().contains(SanitizerSet::KERNELHWADDRESS) && get_version() < (21, 1, 0) {
add("-hwasan-kernel", false);
}
Copy link
Member Author

@Darksonn Darksonn Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmayer Is this reasonable a reasonable fix for rustc to work around the lack of llvm/llvm-project#142226? Or do you think I should pass this on all versions? Thanks!

@rust-log-analyzer

This comment has been minimized.

@Darksonn Darksonn marked this pull request as ready for review March 7, 2026 16:19
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

Some changes occurred in cfg and check-cfg configuration

cc @Urgau

Some changes occurred in tests/ui/sanitizer

cc @rcvalle

Some changes occurred in src/tools/compiletest

cc @jieyouxu

Some changes occurred in tests/codegen-llvm/sanitizer

cc @rcvalle

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@rcvalle
Copy link
Member

rcvalle commented Mar 14, 2026

cc @1c3t3a @jakos-sec

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mati865
Copy link
Member

mati865 commented Mar 16, 2026

FYI LLVM prior to 21 is no longer supported: #153684

@rust-log-analyzer

This comment has been minimized.

@Darksonn
Copy link
Member Author

@mati865 Technically bug also occurs on 21.0, as it wasn't fixed until 21.1.0

@rust-log-analyzer

This comment has been minimized.

@mati865
Copy link
Member

mati865 commented Mar 16, 2026

@Darksonn sorry if it wasn't clear. LLVM uses X.1.0 for the first release of each major version and there is no such version as LLVM 21.0.0. So by saying LLVM 21 I meant LLVM >=21.1.0 since this is the first release of that major version.

Thus, LLVM 21.1.0 is the oldest supported version now.

@Darksonn
Copy link
Member Author

Darksonn commented Mar 16, 2026

@mati865 Oh I did not know that. In that case, I can take out the bugfix.

For posterity, it add this to compiler/rustc_codegen_llvm/src/llvm_util.rs

        // KHWASAN: In LLVM versions prior to 21.1.0, the HWAddressSanitizer pass incorrectly
        // ignores the pass-specific `CompileKernel` flag and only looks at the global command-line
        // flag `-hwasan-kernel`. To work around this, pass `-hwasan-kernel` on the relevant LLVM
        // versions.
        //
        // Fixed by: [HWASan][bugfix] Fix kernel check in ShadowMapping::init (#142226).
        if sess.sanitizers().contains(SanitizerSet::KERNELHWADDRESS) && get_version() < (21, 1, 0) {
            add("-hwasan-kernel", false);
        }

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 17, 2026
…hanBrouwer

Fix minor kasan bugs

Fixes a few minor bugs discovered during rust-lang#153049.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 17, 2026
…hanBrouwer

Fix minor kasan bugs

Fixes a few minor bugs discovered during rust-lang#153049.
@rust-bors

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Mar 18, 2026
Fix minor kasan bugs

Fixes a few minor bugs discovered during rust-lang/rust#153049.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Mar 18, 2026
Fix minor kasan bugs

Fixes a few minor bugs discovered during rust-lang/rust#153049.
@Darksonn
Copy link
Member Author

r? compiler

@rustbot rustbot assigned fmease and unassigned chenyukang Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-sanitizers Area: Sanitizers for correctness and code quality A-testsuite Area: The testsuite used to check the correctness of rustc PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants