-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[msan] Re-exec with no ASLR if memory layout is incompatible on Linux #85142
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0c878b6
[msan] Re-exec with no ASLR if memory layout is incompatible on Linux
thurstond b714a62
Skip CheckMemoryRangeAvailability if !dry_run, as suggested by Vitaly
thurstond 9bda315
Bug fix: if the memory layout is incompatible and we did not re-exec,…
thurstond 63d529f
Simplify code per Vitaly's comment
thurstond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |
| # include <signal.h> | ||
| # include <stdio.h> | ||
| # include <stdlib.h> | ||
| # if SANITIZER_LINUX | ||
| # include <sys/personality.h> | ||
| # endif | ||
| # include <sys/resource.h> | ||
| # include <sys/time.h> | ||
| # include <unistd.h> | ||
|
|
@@ -43,11 +46,13 @@ void ReportMapRange(const char *descr, uptr beg, uptr size) { | |
| } | ||
| } | ||
|
|
||
| static bool CheckMemoryRangeAvailability(uptr beg, uptr size) { | ||
| static bool CheckMemoryRangeAvailability(uptr beg, uptr size, bool verbose) { | ||
| if (size > 0) { | ||
| uptr end = beg + size - 1; | ||
| if (!MemoryRangeIsAvailable(beg, end)) { | ||
| Printf("FATAL: Memory range 0x%zx - 0x%zx is not available.\n", beg, end); | ||
| if (verbose) | ||
| Printf("FATAL: Memory range 0x%zx - 0x%zx is not available.\n", beg, | ||
| end); | ||
| return false; | ||
| } | ||
| } | ||
|
|
@@ -106,7 +111,7 @@ static void CheckMemoryLayoutSanity() { | |
| } | ||
| } | ||
|
|
||
| bool InitShadow(bool init_origins) { | ||
| static bool InitShadow(bool init_origins, bool dry_run) { | ||
| // Let user know mapping parameters first. | ||
| VPrintf(1, "__msan_init %p\n", reinterpret_cast<void *>(&__msan_init)); | ||
| for (unsigned i = 0; i < kMemoryLayoutSize; ++i) | ||
|
|
@@ -116,8 +121,9 @@ bool InitShadow(bool init_origins) { | |
| CheckMemoryLayoutSanity(); | ||
|
|
||
| if (!MEM_IS_APP(&__msan_init)) { | ||
| Printf("FATAL: Code %p is out of application range. Non-PIE build?\n", | ||
| reinterpret_cast<void *>(&__msan_init)); | ||
| if (!dry_run) | ||
| Printf("FATAL: Code %p is out of application range. Non-PIE build?\n", | ||
| reinterpret_cast<void *>(&__msan_init)); | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -141,29 +147,61 @@ bool InitShadow(bool init_origins) { | |
| if (!map && !protect) { | ||
| CHECK(type == MappingDesc::APP || type == MappingDesc::ALLOCATOR); | ||
|
|
||
| if (type == MappingDesc::ALLOCATOR && | ||
| !CheckMemoryRangeAvailability(start, size)) | ||
| if (dry_run && type == MappingDesc::ALLOCATOR && | ||
| !CheckMemoryRangeAvailability(start, size, !dry_run)) | ||
| return false; | ||
| } | ||
| if (map) { | ||
| if (!CheckMemoryRangeAvailability(start, size)) | ||
| if (dry_run && !CheckMemoryRangeAvailability(start, size, !dry_run)) | ||
| return false; | ||
| if (!MmapFixedSuperNoReserve(start, size, kMemoryLayout[i].name)) | ||
| if (!dry_run && | ||
| !MmapFixedSuperNoReserve(start, size, kMemoryLayout[i].name)) | ||
| return false; | ||
| if (common_flags()->use_madv_dontdump) | ||
| if (!dry_run && common_flags()->use_madv_dontdump) | ||
| DontDumpShadowMemory(start, size); | ||
| } | ||
| if (protect) { | ||
| if (!CheckMemoryRangeAvailability(start, size)) | ||
| if (dry_run && !CheckMemoryRangeAvailability(start, size, !dry_run)) | ||
| return false; | ||
| if (!ProtectMemoryRange(start, size, kMemoryLayout[i].name)) | ||
| if (!dry_run && !ProtectMemoryRange(start, size, kMemoryLayout[i].name)) | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool InitShadowWithReExec(bool init_origins) { | ||
| // Start with dry run: check layout is ok, but don't print warnings because | ||
| // warning messages will cause tests to fail (even if we successfully re-exec | ||
| // after the warning). | ||
| bool success = InitShadow(__msan_get_track_origins(), true); | ||
| if (!success) { | ||
| # if SANITIZER_LINUX | ||
| // Perhaps ASLR entropy is too high. If ASLR is enabled, re-exec without it. | ||
| int old_personality = personality(0xffffffff); | ||
| bool aslr_on = | ||
| (old_personality != -1) && ((old_personality & ADDR_NO_RANDOMIZE) == 0); | ||
|
|
||
| if (aslr_on) { | ||
| VReport(1, | ||
| "WARNING: MemorySanitizer: memory layout is incompatible, " | ||
| "possibly due to high-entropy ASLR.\n" | ||
| "Re-execing with fixed virtual address space.\n" | ||
| "N.B. reducing ASLR entropy is preferable.\n"); | ||
| CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1); | ||
| ReExec(); | ||
| } | ||
| # endif | ||
| } | ||
|
|
||
| // The earlier dry run didn't actually map or protect anything. Run again in | ||
| // non-dry run mode. | ||
| success = success && InitShadow(__msan_get_track_origins(), false); | ||
|
||
|
|
||
| return success; | ||
| } | ||
|
|
||
| static void MsanAtExit(void) { | ||
| if (flags()->print_stats && (flags()->atexit || msan_report_count > 0)) | ||
| ReportStats(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.