Skip to content

[win/asan] Don't intercept memset etc. in ntdll #120397

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 2 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions compiler-rt/lib/interception/interception_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,7 @@ static void **InterestingDLLsAvailable() {
"libc++.dll", // libc++
"libunwind.dll", // libunwind
# endif
// NTDLL should go last as it exports some functions that we should
// override in the CRT [presumably only used internally].
// NTDLL must go last as it gets special treatment in OverrideFunction.
"ntdll.dll",
NULL
};
Expand Down Expand Up @@ -1281,9 +1280,22 @@ uptr InternalGetProcAddress(void *module, const char *func_name) {

bool OverrideFunction(
const char *func_name, uptr new_func, uptr *orig_old_func) {
static const char *kNtDllIgnore[] = {
"memcmp", "memcpy", "memmove", "memset"
};

bool hooked = false;
void **DLLs = InterestingDLLsAvailable();
for (size_t i = 0; DLLs[i]; ++i) {
if (DLLs[i + 1] == nullptr) {
// This is the last DLL, i.e. NTDLL. It exports some functions that
// we only want to override in the CRT.
for (const char *ignored : kNtDllIgnore) {
if (strcmp(func_name, ignored) == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there some internal_strcmp function we should be calling instead in order to avoid libc re-entrancy, like hitting the ASan strcmp interceptor, for example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

return hooked;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit, let's just break or continue so the reader doesn't have to think about what hooked is, we just hit the shared return codepath.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's a nested loop, I think returning is the most straight-forward.

}
}

uptr func_addr = InternalGetProcAddress(DLLs[i], func_name);
if (func_addr &&
OverrideFunction(func_addr, new_func, orig_old_func)) {
Expand Down
Loading