-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(linux): prevent infinite re-exec loop under AppArmor #2239
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
Open
anishesg
wants to merge
4
commits into
google:main
Choose a base branch
from
proudhare:fix/ph-issue-2184
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6bde9da
fix(linux): prevent infinite re-exec loop under AppArmor
anishesg 47b38bd
Update src/benchmark.cc
LebedevRI da0a954
address review feedback: fix CI warning, simplify child argv, avoid h…
anishesg fca823e
Apply suggestion from @LebedevRI
LebedevRI 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,13 +36,15 @@ | |
|
|
||
| #ifdef BENCHMARK_OS_LINUX | ||
| #include <sys/personality.h> | ||
| #include <sys/wait.h> | ||
| #endif | ||
|
|
||
| #include <algorithm> | ||
| #include <atomic> | ||
| #include <condition_variable> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <limits> | ||
|
|
@@ -835,11 +837,30 @@ std::make_unsigned_t<T> get_as_unsigned(T v) { | |
|
|
||
| } // end namespace internal | ||
|
|
||
| void MaybeReenterWithoutASLR(int /*argc*/, char** argv) { | ||
| void MaybeReenterWithoutASLR(int argc, char** argv) { | ||
| (void)argc; | ||
| // On e.g. Hexagon simulator, argv may be NULL. | ||
| if (!argv) return; | ||
|
|
||
| #ifdef BENCHMARK_OS_LINUX | ||
| static constexpr const char kTestChildArg[] = "--benchmark_aslr_test_child="; | ||
| static constexpr size_t kTestChildArgLen = sizeof(kTestChildArg) - 1; | ||
|
|
||
| // Check if we are a test child process that should report personality and exit | ||
| for (int i = 1; i < argc; ++i) { | ||
| if (std::strncmp(argv[i], kTestChildArg, kTestChildArgLen) == 0) { | ||
| const int write_fd = std::atoi(argv[i] + kTestChildArgLen); | ||
| const auto test_personality = personality(0xffffffff); | ||
| // Write 1 if ADDR_NO_RANDOMIZE is set, 0 otherwise | ||
| char result = ((test_personality != -1) && | ||
| (internal::get_as_unsigned(test_personality) & ADDR_NO_RANDOMIZE)) | ||
| ? 1 : 0; | ||
| (void)write(write_fd, &result, 1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like GCC wants some better check than cast-to-void here. |
||
| close(write_fd); | ||
| std::exit(0); | ||
| } | ||
| } | ||
|
|
||
| const auto curr_personality = personality(0xffffffff); | ||
|
|
||
| // We should never fail to read-only query the current personality, | ||
|
|
@@ -864,9 +885,55 @@ void MaybeReenterWithoutASLR(int /*argc*/, char** argv) { | |
| if ((internal::get_as_unsigned(new_personality) & ADDR_NO_RANDOMIZE) == 0) | ||
| return; | ||
|
|
||
| execv(argv[0], argv); | ||
| // The exec() functions return only if an error has occurred, | ||
| // in which case we want to just continue as-is. | ||
| // Verify that the personality change survives exec() by testing in a child. | ||
| // Some LSMs (e.g., AppArmor) may reset personality flags during execve(), | ||
| // even though they were successfully set in the parent process. | ||
| // This prevents infinite re-exec loops when the kernel silently resets | ||
| // the personality after each exec. | ||
| int pipefd[2]; | ||
| if (pipe(pipefd) == -1) return; | ||
|
|
||
| pid_t pid = fork(); | ||
| if (pid == -1) { | ||
| close(pipefd[0]); | ||
| close(pipefd[1]); | ||
| return; | ||
| } | ||
|
|
||
| if (pid == 0) { | ||
| // Child: prepare to exec with test argument | ||
| close(pipefd[0]); | ||
|
|
||
| // Build test argument on stack (safe before exec) | ||
| char test_arg[64]; | ||
| std::snprintf(test_arg, sizeof(test_arg), | ||
| "--benchmark_aslr_test_child=%d", pipefd[1]); | ||
|
|
||
| // Simple argv with just the executable and test argument | ||
| char* child_argv[] = {argv[0], test_arg, nullptr}; | ||
|
|
||
| execv(argv[0], child_argv); | ||
| // If exec fails, exit | ||
| _exit(1); | ||
| } | ||
|
|
||
| // Parent: wait for child to report back | ||
| close(pipefd[1]); | ||
|
|
||
| char result = 0; | ||
| ssize_t nread = read(pipefd[0], &result, 1); | ||
| close(pipefd[0]); | ||
|
|
||
| int status; | ||
| waitpid(pid, &status, 0); | ||
|
|
||
| // If we successfully read the result and it indicates ADDR_NO_RANDOMIZE | ||
| // survived exec, proceed with re-exec. Otherwise, skip to avoid infinite loop. | ||
| if (nread == 1 && result == 1) { | ||
| execv(argv[0], argv); | ||
| } | ||
|
|
||
| // Personality doesn't survive exec boundary, skip re-exec. | ||
| #else | ||
| return; | ||
| #endif | ||
|
|
||
Oops, something went wrong.
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.