Skip to content

Fix native resource leaks in Keychain#184

Merged
rmarinho merged 1 commit into
mainfrom
fix/keychain-native-resource-leaks
Jun 1, 2026
Merged

Fix native resource leaks in Keychain#184
rmarinho merged 1 commit into
mainfrom
fix/keychain-native-resource-leaks

Conversation

@rmarinho

Copy link
Copy Markdown
Member

Summary

Fixes memory leaks in Keychain P/Invoke methods:

  1. GetAllSigningIdentities / GetAllSigningCertificates: The CFDataRef returned by SecCertificateCopyData (which follows Copy semantics = +1 retain count) was never released. Now calls CFRelease(data) after extracting bytes.

  2. FindInternetUserNameAndPassword / FindInternetPassword(string,...): The passwordData allocated by the keychain was never freed, and the item ref was never released. Now calls SecKeychainItemFreeContent and CFRelease(item).

  3. FindInternetPassword(Uri): The passwordData was never freed (item was already released correctly). Now calls SecKeychainItemFreeContent.

These leaks accumulate when enumerating large keychains or repeatedly querying internet passwords.

Testing

No unit tests added — these methods require macOS keychain access with real native interop. Verified the fix compiles correctly and follows the same pattern used in DoesKeychainContainCertificate (line 616).

Copilot AI review requested due to automatic review settings May 29, 2026 15:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes three native resource leaks in Xamarin.MacDev/Keychain.cs by releasing CoreFoundation/Keychain-allocated memory that was previously not freed.

Changes:

  • Release the CFDataRef returned by SecCertificateCopyData in GetAllSigningIdentities and GetAllSigningCertificates.
  • Free passwordData via SecKeychainItemFreeContent and release the item ref via CFRelease in FindInternetUserNameAndPassword and FindInternetPassword(string, ...).
  • Free passwordData in FindInternetPassword(Uri) (the item was already released).

Comment thread Xamarin.MacDev/Keychain.cs Outdated
- GetAllSigningIdentities: CFRelease the CFDataRef returned by
  SecCertificateCopyData (Copy semantics = +1 retain count).
- GetAllSigningCertificates: Same CFDataRef leak fix.
- FindInternetUserNameAndPassword: Free passwordData with
  SecKeychainItemFreeContent and release the item ref.
- FindInternetPassword(string,...): Same leak fix.
- FindInternetPassword(Uri): Free passwordData and align cleanup.

All three FindInternet* methods now use try/finally to guarantee native
resources are freed even if GetUsernameFromKeychainItemRef or marshaling
throws.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rmarinho rmarinho force-pushed the fix/keychain-native-resource-leaks branch from 8e6ffed to 9d01bb0 Compare May 29, 2026 15:57
@rmarinho rmarinho requested a review from rolfbjarne May 29, 2026 21:56
@rmarinho rmarinho merged commit 4bfa9e8 into main Jun 1, 2026
7 checks passed
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.

3 participants