Skip to content

fix: add safety warning to pickle_memoize cache loading#18925

Merged
tlopex merged 1 commit intoapache:mainfrom
scruge1:fix/pickle-memoize-unsafe-load
Mar 24, 2026
Merged

fix: add safety warning to pickle_memoize cache loading#18925
tlopex merged 1 commit intoapache:mainfrom
scruge1:fix/pickle-memoize-unsafe-load

Conversation

@scruge1
Copy link
Contributor

@scruge1 scruge1 commented Mar 23, 2026

Summary

pickle_memoize loads cached pickle files via pickle.load() without any integrity verification or user warning. If an attacker can write to the cache directory, they can inject malicious pickle payloads that execute arbitrary code on next load.

Fix

Adds a UserWarning when loading pickle cache files to alert users about the security risk.

Related

Huntr security vulnerability report (CWE-502: Deserialization of Untrusted Data)

@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 addresses a security vulnerability (CWE-502: Deserialization of Untrusted Data) in the pickle_memoize utility. It enhances user awareness by adding a UserWarning during the loading of cached pickle files, informing them that pickle files can execute arbitrary code and should only be loaded from trusted sources. This change aims to mitigate risks where malicious payloads could be injected into the cache directory.

Highlights

  • Security Warning Added: A UserWarning has been introduced in pickle_memoize to alert users about the security risks associated with loading cached pickle files, specifically the potential for arbitrary code execution from untrusted sources.

🧠 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.

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.

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 adds a UserWarning to alert users about the security risks of loading cached pickle files. This is a good security measure. I have two suggestions to improve this change: first, to move the import statement to the top of the file to follow Python best practices. Second, to add a test to verify that the warning is triggered as expected. Please see the detailed comments on the code.

if self.path.exists():
with self.path.open("rb") as cache_file:
try:
import warnings
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To adhere to PEP 8 guidelines, import statements should be placed at the top of the file. Please move import warnings to the top of the module.

References
  1. PEP 8, the style guide for Python code, specifies that imports should be at the top of the file. This improves readability and makes dependencies explicit. (link)

Comment on lines +75 to +81
warnings.warn(
f"Loading cached pickle file from {self.path}. "
"Pickle files can execute arbitrary code. "
"Only load cache files you trust.",
UserWarning,
stacklevel=2,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This change introduces a user warning, which is great for security awareness. However, there is no test to verify that this warning is actually triggered when loading from a cached pickle file. Please consider adding a test case to tests/python/contrib/test_memoize.py that uses pytest.warns to assert that the UserWarning is raised.

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 addresses a security concern with pickle.load by adding a UserWarning to inform users of the potential for arbitrary code execution when loading cached files. The change is straightforward and effectively communicates the risk. I have one suggestion regarding the placement of the import warnings statement to improve code style and maintainability.

if self.path.exists():
with self.path.open("rb") as cache_file:
try:
import warnings
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 import warnings statement should be moved to the top of the file. According to PEP 8, imports should be at the top of the file, just after any module comments and docstrings. Placing imports inside functions is generally discouraged as it makes it harder to see the module's dependencies at a glance and can lead to repeated imports.

References
  1. PEP 8 recommends that imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. (link)

pickle_memoize loads cached pickle files via pickle.load() without any
integrity verification. If an attacker can write to the cache directory,
they can inject malicious pickle payloads that execute on next load.

- Add UserWarning when loading pickle cache files
- Move import to module level (PEP 8)
- Add test case to verify warning is raised

Fixes: Unsafe pickle.load() in cache (CWE-502)
Signed-off-by: scruge1 <scruge1@proton.me>
@scruge1 scruge1 force-pushed the fix/pickle-memoize-unsafe-load branch from f1bb09f to 728fed0 Compare March 23, 2026 23:38
Copy link
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

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

LGTM Thanks!

@tlopex tlopex merged commit 7b3fa38 into apache:main Mar 24, 2026
6 checks passed
@tqchen
Copy link
Member

tqchen commented Mar 24, 2026

given pickle is used as part of our code and our security model assumes trusted data for pickle memoize that is only used for trusted data in testcase, we would prefer not having a warning in such case (just like pickle itself won't have warnings).

@scruge1
Copy link
Contributor Author

scruge1 commented Mar 24, 2026

Understood — makes sense that pickle_memoize operates within TVM's trusted-data security model. Closing this PR. Thank you for the review.

@scruge1
Copy link
Contributor Author

scruge1 commented Mar 24, 2026

Understood — the revert in #18926 is appropriate given TVM's trusted-data security model. Thank you for the consideration.

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