Skip to content

[tsan] Lazily call 'personality' to minimize sandbox violations #79334

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 3 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ static void ReExecIfNeeded() {
}

# if SANITIZER_LINUX
# if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__))
// ASLR personality check.
int old_personality = personality(0xffffffff);
bool aslr_on =
(old_personality != -1) && ((old_personality & ADDR_NO_RANDOMIZE) == 0);

# if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__))
// After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in
// linux kernel, the random gap between stack and mapped area is increased
// from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover
Expand All @@ -267,6 +267,14 @@ static void ReExecIfNeeded() {
if (reexec) {
// Don't check the address space since we're going to re-exec anyway.
} else if (!CheckAndProtect(false, false, false)) {
// ASLR personality check.
// N.B. 'personality' is sometimes forbidden by sandboxes, so we only call
// this as a last resort (when the memory mapping is incompatible and TSan
// would fail anyway).
int old_personality = personality(0xffffffff);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably could test this by defining own personality function in a test that will simply abort. This way we can have a list of functions that must not be called by the runtime (prohibited by sandboxes).
Though my bet is that it will fail on some platforms for some reasons. So probably the test should be limited to Linux/x86 only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure how I could add this to the TSan tests, since the sandbox behavior (e.g., allowing/disallowing 'personality') is dependent on the vendor's configuration.

Taken to an extreme, there's probably some sandbox out there that disallows 'printf', but it would be unreasonable to avoid printf entirely in TSan.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant adding this to a test:

extern "C" int personality(unsigned long) { abort(); }

Such test should abort w/o this fix.
Probably better to add to sanitizer_common tests so that all sanitizers are tested.

Of course, there can be sandboxes that prohibit too much and sanitizers won't work under these sandboxes. We can't fix that. But we do want to avoid personality and we want it to not regress tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see! I've added compiler-rt/test/sanitizer_common/TestCases/Linux/sandbox_forbidden_functions.cpp. I tested that the test failed with TSan in the absence of this fix, and passes with this fix.

Note that it does have a false positive when TSan is run with high-entropy ASLR: in that environment, calling 'personality' (to check/disable ASLR) is the intended behavior.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that it does have a false positive when TSan is run with high-entropy ASLR

Will it be flaky on bots then? That's not good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be flaky on bots that have very high-entropy ASLR. AFAIK there are currently no such bots (because without the re-exec patch, all TSan tests would be failing consistently on those bots).

With that caveat in mind, please let me know if you would prefer to not test 'personality'.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it would be only about bots, then I would say let's try to add it (perhaps with an additional comment).
But it will also fail on local developer ninja check-sanitizer runs, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will fail on local developer ninja check-sanitizer if they are running with very high entropy ASLR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then I afraid it's probably better to disable the test.
But also work under that sandbox will probably break soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the test in the latest commit (0b4c846).

With the re-exec patch + this patch:

  • low entropy ASLR, no sandbox: TSan works
  • low entropy ASLR, with sandbox: TSan works
  • high entropy ASLR, no sandbox: TSan works
  • high entropy ASLR, with sandbox: TSan will trigger a sandbox violation, but TSan didn't work anyway with high entropy ASLR. Moreover, this is currently only a hypothetical case.

bool aslr_on =
(old_personality != -1) && ((old_personality & ADDR_NO_RANDOMIZE) == 0);

if (aslr_on) {
// Disable ASLR if the memory layout was incompatible.
// Alternatively, we could just keep re-execing until we get lucky
Expand Down