Skip to content

Fix buffer overflow in Android LogException via std::string concatenation - #1950

Merged
AustinBenoit merged 3 commits into
mainfrom
fix/android-log-exception-strncat
Oct 27, 2026
Merged

AustinBenoit merged 3 commits into
mainfrom
fix/android-log-exception-strncat

Conversation

@AustinBenoit

Copy link
Copy Markdown
Contributor

Description

Provide details of the change, and generalize the change in the PR title above.

Resolves GitHub Code Scanning security alerts (WS-C-00155):

In firebase::util::LogException (app/src/util_android.cc), when formatting a JNI exception message with a non-null log_fmt, the previous implementation wrote log_fmt into a 512-byte static char buf[512] via vsnprintf and then called strncat(buf, ": ", sizeof(buf) - 1) and strncat(buf, message_str.c_str(), sizeof(buf) - 1). Because the third parameter to strncat is the maximum number of bytes to append rather than the total destination buffer size, a long Java exception message could write past the end of buf.

This change:

  • Replaces both strncat calls with std::string concatenation (message_str = std::string(buf) + ": " + message_str;).
  • Uses a stack-allocated char buf[512] with vsnprintf(buf, sizeof(buf), log_fmt, list) instead of a non-thread-safe static buffer.
  • Adds a release note entry under ### Upcoming in release_build_files/readme.md.

Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.

  • Verified formatting with python3 scripts/format_code.py -git_diff -noformat_file -verbose.
  • Verified copyright headers with bash scripts/gha/check_copyright.sh -g.

Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2786
https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2787


Notes

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.

…tion

Replace the two strncat(buf, ..., sizeof(buf) - 1) calls in firebase::util::LogException with std::string concatenation, and use a stack-local buffer instead of a non-thread-safe static buffer when formatting log_fmt with vsnprintf.

Resolves GitHub Code Scanning security alerts (Rule WS-C-00155):
- https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2786
- https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2787

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the LogException function in app/src/util_android.cc to prevent potential buffer overflows by replacing unsafe strncat operations with std::string concatenation, and updates the release notes. The feedback identifies a valid issue where the local buffer buf is left uninitialized, which could lead to undefined behavior if vsnprintf fails; initializing it to zero is recommended.

Comment thread app/src/util_android.cc Outdated
@AustinBenoit
AustinBenoit merged commit 961decf into main Oct 27, 2026
48 of 49 checks passed
@github-actions github-actions Bot added the tests: in-progress This PR's integration tests are in progress. label Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Integration test with FLAKINESS (succeeded after retry)

Requested by @AustinBenoit on commit 961decf
Last updated: Tue Oct 27 15:51 PDT 2026
View integration test log & download artifacts

Failures Configs
app_check [TEST] [FLAKINESS] [Android] [1/3 os: ubuntu] [1/2 android_device: android_target]
(1 failed tests)  CRASH/TIMEOUT

Add flaky tests to go/fpl-cpp-flake-tracker

@github-actions github-actions Bot added the tests: succeeded This PR's integration tests succeeded. label Oct 27, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests: succeeded This PR's integration tests succeeded.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants