Skip to content

security(twdata): Zeroize every TWData instance in TWDataDelete, and other TWData improvements#4667

Merged
sergei-boiko-trustwallet merged 12 commits intomasterfrom
security/zeroize-tw-data
Mar 6, 2026
Merged

security(twdata): Zeroize every TWData instance in TWDataDelete, and other TWData improvements#4667
sergei-boiko-trustwallet merged 12 commits intomasterfrom
security/zeroize-tw-data

Conversation

@sergei-boiko-trustwallet
Copy link
Copy Markdown
Contributor

@sergei-boiko-trustwallet sergei-boiko-trustwallet commented Feb 25, 2026

This pull request refactors the TWData interface and related code for improved safety, clarity, and memory handling. The most significant changes include removing the TWDataGet function, updating the semantics and error handling for TWDataCopyBytes and TWDataReplaceBytes, and ensuring sensitive data is securely erased before deletion. Test cases have also been updated to reflect these changes and improve error coverage.

API changes and safety improvements

  • Removed the TWDataGet function from both the header and implementation, encouraging direct use of TWDataBytes for accessing data and simplifying the API.
  • Updated TWDataCopyBytes and TWDataReplaceBytes to return an integer status code (0 for success, -1 for failure) and added bounds checking to prevent invalid memory access. The header documentation and implementation were both updated accordingly.
  • Adjusted usage of these functions throughout the codebase to check return values and handle errors gracefully, including in TWPrivateKeyCreateWithData and TWPrivateKeyIsValid.

Memory handling and security

  • Enhanced deletion functions for TWData and TWString to securely erase memory using memzero before freeing, reducing the risk of sensitive data leakage.
  • Included memzero.h for secure memory erasure.

Test updates and improvements

  • Removed or replaced test cases that used the deprecated TWDataGet function, switching to direct buffer access via TWDataBytes.
  • Updated tests for error handling in TWDataCopyBytes and TWDataReplaceBytes, adding new cases to verify correct behavior for out-of-bounds and zero-size operations.
  • Simplified and clarified tests for various decode functions, removing unnecessary wrappers and directly checking for nullptr results.
    These changes collectively improve the safety, maintainability, and correctness of the codebase, especially around memory management and error handling.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens TWData lifecycle handling in the Trust Wallet Core C interface by attempting to securely wipe TWData contents in TWDataDelete before deallocation.

Changes:

  • Add <TrezorCrypto/memzero.h> include to src/interface/TWData.cpp.
  • Call memzero on the underlying Data buffer in TWDataDelete prior to delete.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 25, 2026

Binary size comparison

➡️ aarch64-apple-ios: 14.34 MB

➡️ aarch64-apple-ios-sim: 14.34 MB

➡️ aarch64-linux-android: 18.77 MB

➡️ armv7-linux-androideabi: 16.20 MB

➡️ wasm32-unknown-emscripten: 13.68 MB

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sergei-boiko-trustwallet sergei-boiko-trustwallet changed the title security(twdata): Zeroize every TWData instance in TWDataDelete security(twdata): Zeroize every TWData instance in TWDataDelete, and other TWData improvements Mar 2, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

tests/interface/TWDataTests.cpp:86

  • Test coverage: since TWDataSet now guards against out-of-range indices, add a unit test that calls TWDataSet(data, TWDataSize(data), ...) (and/or a larger index) and asserts the data remains unchanged. This would lock in the new safety behavior and prevent regressions back to OOB writes.
TEST(TWData, Set) {
    const auto data = WRAPD(TWDataCreateWithHexString(STRING("deadbeef").get()));
    assertHexEqual(data, "deadbeef");
    TWDataSet(data.get(), 1, 0xff);
    assertHexEqual(data, "deffbeef");
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sergei-boiko-trustwallet sergei-boiko-trustwallet merged commit 6d4549a into master Mar 6, 2026
19 checks passed
@sergei-boiko-trustwallet sergei-boiko-trustwallet deleted the security/zeroize-tw-data branch March 6, 2026 11:04
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