Skip to content

Fix b2s wide string garbage extraction in Rust binaries issue #867#1223

Open
svm1048 wants to merge 1 commit intomandiant:masterfrom
svm1048:master
Open

Fix b2s wide string garbage extraction in Rust binaries issue #867#1223
svm1048 wants to merge 1 commit intomandiant:masterfrom
svm1048:master

Conversation

@svm1048
Copy link

@svm1048 svm1048 commented Mar 3, 2026

No description provided.

@google-cla
Copy link

google-cla bot commented Mar 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the string extraction process for Rust binaries to resolve an issue where wide strings were incorrectly parsed as UTF-8, leading to garbage data. By introducing a dedicated module with a strict UTF-8 regex-based extractor, the system now accurately identifies and decodes only valid UTF-8 strings, enhancing the reliability of static string analysis for Rust executables.

Highlights

  • Improved UTF-8 String Extraction: Implemented a new, more robust UTF-8 string extraction mechanism using a strict regex pattern to accurately identify and decode valid UTF-8 sequences, specifically addressing issues with wide string garbage extraction in Rust binaries.
  • Removal of binary2strings Dependency: Eliminated the reliance on the binary2strings library for string extraction within the Rust language module, replacing its functionality with the custom UTF-8 extraction logic.
  • Refactored String Processing Logic: Removed the fix_b2s_wide_strings and filter_and_transform_utf8_strings helper functions, streamlining the string processing pipeline in floss/language/rust/extract.py.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • floss/language/rust/extract.py
    • Updated imports to remove binary2strings and include the new extract_utf8_strings module.
    • Removed the fix_b2s_wide_strings and filter_and_transform_utf8_strings helper functions.
    • Refactored the get_string_blob_strings function to utilize the new extract_utf8_strings for robust UTF-8 string identification.
  • floss/language/rust/strings.py
    • Added a new module strings.py dedicated to strict UTF-8 string extraction.
    • Defined UTF8_PRINTABLE_PATTERN, a regex for matching valid printable ASCII and multi-byte UTF-8 sequences.
    • Implemented the extract_utf8_strings function to scan byte buffers and yield strictly valid UTF-8 strings, ignoring wide strings.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 effectively addresses the issue of garbage wide string extraction in Rust binaries by replacing the binary2strings library with a custom, regex-based UTF-8 string extractor. The new implementation in floss/language/rust/strings.py is a significant improvement, providing more precise control over string extraction. The overall changes are well-structured and simplify the logic in floss/language/rust/extract.py. My review includes a few suggestions to enhance code style and adhere to Python best practices for better maintainability.

Comment on lines +112 to +123
static_strings: List[StaticString] = []
for offset, string_val in extract_utf8_strings(buffer_rdata, min_length=min_length):
# our static algorithm does not extract new lines either
clean_str = string_val.replace("\n", "")

static_strings.append(
StaticString(
string=clean_str,
offset=offset + start_rdata,
encoding=StringEncoding.UTF8
)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The loop for building static_strings can be simplified into a more concise and Pythonic list comprehension. This improves readability and maintainability.

    static_strings: List[StaticString] = [
        StaticString(
            string=string_val.replace("\n", ""),
            offset=offset + start_rdata,
            encoding=StringEncoding.UTF8,
        )
        for offset, string_val in extract_utf8_strings(buffer_rdata, min_length=min_length)
    ]


if __name__ == "__main__":
sys.exit(main())
sys.exit(main()) No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The file is missing a final newline character. It's a standard convention (part of the POSIX standard for text files) to end files with a newline. Some tools may not process the last line correctly if it's not terminated by a newline. Please add a newline at the end of the file for compatibility and consistency.

    sys.exit(main())

Comment on lines +1 to +5
import re
from typing import Iterator, Tuple

'''This regular expression is strictly designed to match printable ASCII
and valid UTF-8 multi-byte sequences.'''
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The multi-line string on lines 4-5 should be a module-level docstring placed at the top of the file, as per PEP 257. This improves code readability and allows documentation-generation tools to correctly parse the file's purpose.

"""This regular expression is strictly designed to match printable ASCII and valid UTF-8 multi-byte sequences."""

import re
from typing import Iterator, Tuple


print("Extracting strings from test buffer...")
for offset, extracted_str in extract_utf8_strings(test_buffer, min_length=4):
print(f"Offset: {hex(offset)} | String: '{extracted_str}'") No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The file is missing a final newline character. It's a standard convention (part of the POSIX standard for text files) to end files with a newline. Some tools may not process the last line correctly if it's not terminated by a newline. Please add a newline at the end of the file for compatibility and consistency.

        print(f"Offset: {hex(offset)} | String: '{extracted_str}'")

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.

1 participant