Forbid Lambda expressions in ImageMath.eval() #5963
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This is an additional fix for CVE-2022-22817 (addressed in #5923). As commented in the original PR, it's still possible to execute arbitrary code via lambda expressions - e.g.:
ImageMath.eval("(lambda: exit())()")
.Original Fix
The original fix checks all
co_names
in the compiledexpression
passed toImageMath.eval()
against builtins names. However, lambda expressions generates anonymous functions and they're not listed in theco_names
structure.Changes in this PR
This PR forbids Lambda expressions being passed to
ImageMath.eval()
by verifying all functions names in the literals section of the bytecode (co_consts
).Trade-offs
Lambda expressions will no longer be supported in
ImageMath.eval()
. If such support is desired, all code objects insideco_consts
can be verified in the same way as the original fix does (iterating throughco_names
and verifying against builtins names). Please let me know in the comments I can rewrite this fix.