Skip to content

Add -Zsanitize=kernel-hwaddress#153049

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

Add -Zsanitize=kernel-hwaddress#153049
Darksonn wants to merge 6 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
Tracking issue: #154171

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.

@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

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks! Two notes

View changes since this review

if sanitizer.contains(SanitizerSet::LEAK)
&& !sanitizer.contains(SanitizerSet::ADDRESS)
&& !sanitizer.contains(SanitizerSet::HWADDRESS)
&& !sanitizer.contains(SanitizerSet::KERNELHWADDRESS)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
&& !sanitizer.contains(SanitizerSet::KERNELHWADDRESS)

I don't think this is necessary since -Zsanitizer=leak,kernel-hwaddress gets rejected on grounds of being incompatible. That's likely also why KERNELADDRESS isn't listed here, -Zsanitizer=leak,kernel-address is an error, too.

See also https://github.com/llvm/llvm-project/blob/4fcc3d7f4ced4f911883f4427907d2a7d66d951f/clang/include/clang/Driver/SanitizerArgs.h#L105-L109.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Member

Choose a reason for hiding this comment

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

(commenting on an arbitrary file)

Could you add a section for KernelHwAddressSanitizer to the Unstable Book (in src/doc/unstable-book/, cc https://doc.rust-lang.org/unstable-book/compiler-flags/sanitizer.html). A stub would be sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@fmease
Copy link
Member

fmease commented Mar 24, 2026

@bors try jobs=test-various,aarch64-gnu

rust-bors bot pushed a commit that referenced this pull request Mar 24, 2026
Add `-Zsanitize=kernel-hwaddress`


try-job: test-various
try-job: aarch64-gnu
@rust-bors

This comment has been minimized.

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 24, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2026

Some changes occurred in src/doc/unstable-book/src/compiler-flags/sanitizer.md

cc @rust-lang/project-exploit-mitigations, @rcvalle

compiletest directives have been modified. Please add or update docs for the
new or modified directive in src/doc/rustc-dev-guide/.

These commits modify compiler targets.
(See the Target Tier Policy.)

@Darksonn
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 24, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 24, 2026

☀️ Try build successful (CI)
Build commit: 6ca6a86 (6ca6a8630bd864d73f6922a0a95b501387897d05, parent: 0312931d8c0ba1a28268a12c06202b68cbc65f76)

@fmease
Copy link
Member

fmease commented Mar 24, 2026

Thanks! @bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 24, 2026

📌 Commit 4d86840 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 24, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Mar 25, 2026
Add `-Zsanitize=kernel-hwaddress`

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
Tracking issue: rust-lang#154171

cc @rcvalle @maurer @ojeda
rust-bors bot pushed a commit that referenced this pull request Mar 25, 2026
Rollup of 6 pull requests

Successful merges:

 - #154004 (`Alignment`: move from `ptr` to `mem` and rename `as_nonzero` to `as_nonzero_usize`)
 - #153049 (Add `-Zsanitize=kernel-hwaddress`)
 - #154269 (miri recursive validation: only check one layer deep)
 - #154112 (some `tests/ui/macros` cleanup)
 - #154131 (begin `tests/ui/structs-enums` cleanup)
 - #154233 (Move ui/issues tests to relevant subdirectories)
jhpratt added a commit to jhpratt/rust that referenced this pull request Mar 25, 2026
Add `-Zsanitize=kernel-hwaddress`

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
Tracking issue: rust-lang#154171

cc @rcvalle @maurer @ojeda
rust-bors bot pushed a commit that referenced this pull request Mar 25, 2026
Rollup of 5 pull requests

Successful merges:

 - #153049 (Add `-Zsanitize=kernel-hwaddress`)
 - #154269 (miri recursive validation: only check one layer deep)
 - #154112 (some `tests/ui/macros` cleanup)
 - #154131 (begin `tests/ui/structs-enums` cleanup)
 - #154233 (Move ui/issues tests to relevant subdirectories)
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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