Skip to content

Fix a use-after-free bug on Win32 when calling lookupSymbol() #62484

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

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Dec 9, 2022

Fix a use-after-free bug on Win32 when calling lookupSymbol(). This function populates a structure SymbolInfo containing a field symbolName that is a std::unique_ptr<char, ...>. Several callers of this function get the underlying C string and pass it beyond the scope of the SymbolInfo instance. Thus, the field can be destructed before the string is used.

On platforms with dladdr(), the string in question is globally owned, so no use-after-free occurs. On Windows, the string is owned by the SymbolInfo structure and is freed when it is destructed. Hence, use-after-free occurs.

This change:

  • Moves SymbolInfo to its own file and provides functions to access its fields, to reinforce that they belong to the symbol info structure and not to the caller.
  • Removes the duplicate implementations of lookupSymbol().
  • Fixes call sites to use the accessor functions.
  • Modifies call sites that use symbolName after the structure dies to ensure they copy the string. The overhead of copying the string should be swamped by the overhead of calling dladdr() or SymFromAddr().

@grynspan
Copy link
Contributor Author

grynspan commented Dec 9, 2022

@swift-ci please test

@grynspan
Copy link
Contributor Author

grynspan commented Dec 9, 2022

@swift-ci please test

3 similar comments
@grynspan
Copy link
Contributor Author

grynspan commented Dec 9, 2022

@swift-ci please test

@grynspan
Copy link
Contributor Author

grynspan commented Dec 9, 2022

@swift-ci please test

@grynspan
Copy link
Contributor Author

grynspan commented Dec 9, 2022

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please clean test linux

@grynspan
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

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

LGTM.

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan requested a review from compnerd December 12, 2022 15:24
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

Don't feel too strongly about the additional symbol, but do think that it would be simpler.

///
/// \note This function will be replaced with \c SymbolInfo::lookup() in a
/// future update.
static inline int lookupSymbol(const void *address, SymbolInfo *outInfo) {
Copy link
Member

Choose a reason for hiding this comment

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

I think that we could out-of-line this function and entirely remove SymbolInfo::lookup, folding the return value handling into the implementation.

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 don't want to get rid of SymbolInfo::lookup(), I want it to be the bottleneck for creating a SymbolInfo instance. The existence of lookupSymbol() at this point could be considered transitional only. Does that make any sense? :)

@grynspan
Copy link
Contributor Author

@swift-ci please test windows

@grynspan
Copy link
Contributor Author

Failures don't look related to this branch. Trying clean builds.

@grynspan
Copy link
Contributor Author

@swift-ci please clean test

@grynspan grynspan force-pushed the jgrynspan/fix-lookupSymbol-use-after-free branch from d44fed0 to 7de79bd Compare December 12, 2022 18:36
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

macOS build failure is on main branch as well. Not a regression. Waiting for it to resolve before rebuilding.

@grynspan
Copy link
Contributor Author

@swift-ci please test macOS

@grynspan grynspan merged commit 26fc627 into swiftlang:main Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants