-
Notifications
You must be signed in to change notification settings - Fork 11
Intercept sigaction to prevent libraries from overwriting signal handlers #420
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| #include <time.h> | ||
| #include <unistd.h> | ||
| #include "common.h" | ||
| #include "counters.h" | ||
| #include "os.h" | ||
|
|
||
| #ifndef __musl__ | ||
|
|
@@ -726,4 +727,88 @@ SigAction OS::replaceSigbusHandler(SigAction action) { | |
| return old_action; | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // sigaction interposition to prevent other libraries from overwriting our | ||
| // SIGSEGV/SIGBUS handlers. This is needed because libraries like wasmtime | ||
| // install broken signal handlers that call malloc() (not async-signal-safe). | ||
| // ============================================================================ | ||
|
|
||
| // Our protected handlers and their chain targets | ||
| static SigAction _protected_segv_handler = nullptr; | ||
| static SigAction _protected_bus_handler = nullptr; | ||
| static volatile SigAction _segv_chain_target = nullptr; | ||
| static volatile SigAction _bus_chain_target = nullptr; | ||
|
|
||
| // Real sigaction function pointer (resolved via dlsym) | ||
| typedef int (*real_sigaction_t)(int, const struct sigaction*, struct sigaction*); | ||
| static real_sigaction_t _real_sigaction = nullptr; | ||
|
|
||
| void OS::protectSignalHandlers(SigAction segvHandler, SigAction busHandler) { | ||
| // Resolve real sigaction BEFORE enabling protection, while we can still use RTLD_DEFAULT | ||
| if (_real_sigaction == nullptr) { | ||
| _real_sigaction = (real_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction"); | ||
| } | ||
| _protected_segv_handler = segvHandler; | ||
| _protected_bus_handler = busHandler; | ||
| } | ||
|
|
||
| SigAction OS::getSegvChainTarget() { | ||
| return __atomic_load_n(&_segv_chain_target, __ATOMIC_ACQUIRE); | ||
| } | ||
|
|
||
| SigAction OS::getBusChainTarget() { | ||
| return __atomic_load_n(&_bus_chain_target, __ATOMIC_ACQUIRE); | ||
| } | ||
|
|
||
| // sigaction hook - called via GOT patching to intercept sigaction calls | ||
|
Contributor
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. what does GOT stand for?
Collaborator
Author
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. Global Offset Table - https://en.wikipedia.org/wiki/Global_Offset_Table |
||
| static int sigaction_hook(int signum, const struct sigaction* act, struct sigaction* oldact) { | ||
| // _real_sigaction must be resolved before any GOT patching happens | ||
| if (_real_sigaction == nullptr) { | ||
| errno = EFAULT; | ||
| return -1; | ||
| } | ||
|
|
||
| // If this is SIGSEGV or SIGBUS and we have protected handlers installed, | ||
| // intercept the call to keep our handler on top | ||
| if (act != nullptr) { | ||
| if (signum == SIGSEGV && _protected_segv_handler != nullptr) { | ||
| // Only intercept SA_SIGINFO handlers (3-arg form) for safe chaining | ||
| if (act->sa_flags & SA_SIGINFO) { | ||
| SigAction new_handler = act->sa_sigaction; | ||
| // Don't intercept if it's our own handler being installed | ||
| if (new_handler != _protected_segv_handler) { | ||
| // Save their handler as our chain target | ||
| __atomic_exchange_n(&_segv_chain_target, new_handler, __ATOMIC_ACQ_REL); | ||
| if (oldact != nullptr) { | ||
| _real_sigaction(signum, nullptr, oldact); | ||
| } | ||
| Counters::increment(SIGACTION_INTERCEPTED); | ||
| // Don't actually install their handler - keep ours on top | ||
| return 0; | ||
| } | ||
| } | ||
| // Let 1-arg handlers (without SA_SIGINFO) pass through - we can't safely chain them | ||
| } else if (signum == SIGBUS && _protected_bus_handler != nullptr) { | ||
| if (act->sa_flags & SA_SIGINFO) { | ||
| SigAction new_handler = act->sa_sigaction; | ||
| if (new_handler != _protected_bus_handler) { | ||
| __atomic_exchange_n(&_bus_chain_target, new_handler, __ATOMIC_ACQ_REL); | ||
| if (oldact != nullptr) { | ||
| _real_sigaction(signum, nullptr, oldact); | ||
| } | ||
| Counters::increment(SIGACTION_INTERCEPTED); | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // For all other cases, pass through to real sigaction | ||
| return _real_sigaction(signum, act, oldact); | ||
| } | ||
|
|
||
| void* OS::getSigactionHook() { | ||
| return (void*)sigaction_hook; | ||
| } | ||
|
|
||
| #endif // __linux__ | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what happens if the array is full? We silently ignore it and don't patch? Isn't this asking for weird failures that would be hard to notice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we would risk those libraries to be able to mess up our safefetch processing. The truth is, vast majority of the libraries are ok.
Also, the whole codecache infra will work only with at most
MAX_NATIVE_LIBSlibraries, so we will work in a weird mode anyway.We can add a warning or a counter to indicate the overflow, though.