diff options
Diffstat (limited to 'meta-python/recipes-devtools/python/python3-pillow/0001-Restrict-builtins-for-ImageMath.eval.patch')
-rw-r--r-- | meta-python/recipes-devtools/python/python3-pillow/0001-Restrict-builtins-for-ImageMath.eval.patch | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/meta-python/recipes-devtools/python/python3-pillow/0001-Restrict-builtins-for-ImageMath.eval.patch b/meta-python/recipes-devtools/python/python3-pillow/0001-Restrict-builtins-for-ImageMath.eval.patch new file mode 100644 index 000000000..4c266cc41 --- /dev/null +++ b/meta-python/recipes-devtools/python/python3-pillow/0001-Restrict-builtins-for-ImageMath.eval.patch | |||
@@ -0,0 +1,60 @@ | |||
1 | From 8531b01d6cdf0b70f256f93092caa2a5d91afc11 Mon Sep 17 00:00:00 2001 | ||
2 | From: Andrew Murray <radarhere@users.noreply.github.com> | ||
3 | Date: Sun, 2 Jan 2022 17:23:49 +1100 | ||
4 | Subject: [PATCH] Restrict builtins for ImageMath.eval | ||
5 | |||
6 | CVE: CVE-2022-22817 | ||
7 | |||
8 | Upstream-Status: Backport | ||
9 | (https://github.com/python-pillow/Pillow/pull/5923/commits/8531b01d6cdf0b70f256f93092caa2a5d91afc11) | ||
10 | |||
11 | Signed-off-by: Trevor Gamblin <trevor.gamblin@windriver.com> | ||
12 | |||
13 | --- | ||
14 | Tests/test_imagemath.py | 7 +++++++ | ||
15 | src/PIL/ImageMath.py | 7 ++++++- | ||
16 | 2 files changed, 13 insertions(+), 1 deletion(-) | ||
17 | |||
18 | diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py | ||
19 | index e7afd1ab..25811aa8 100644 | ||
20 | --- a/Tests/test_imagemath.py | ||
21 | +++ b/Tests/test_imagemath.py | ||
22 | @@ -1,3 +1,5 @@ | ||
23 | +import pytest | ||
24 | + | ||
25 | from PIL import Image, ImageMath | ||
26 | |||
27 | |||
28 | @@ -50,6 +52,11 @@ def test_ops(): | ||
29 | assert pixel(ImageMath.eval("float(B)**33", images)) == "F 8589934592.0" | ||
30 | |||
31 | |||
32 | +def test_prevent_exec(): | ||
33 | + with pytest.raises(ValueError): | ||
34 | + ImageMath.eval("exec('pass')") | ||
35 | + | ||
36 | + | ||
37 | def test_logical(): | ||
38 | assert pixel(ImageMath.eval("not A", images)) == 0 | ||
39 | assert pixel(ImageMath.eval("A and B", images)) == "L 2" | ||
40 | diff --git a/src/PIL/ImageMath.py b/src/PIL/ImageMath.py | ||
41 | index 7f9c88e1..06bea800 100644 | ||
42 | --- a/src/PIL/ImageMath.py | ||
43 | +++ b/src/PIL/ImageMath.py | ||
44 | @@ -246,7 +246,12 @@ def eval(expression, _dict={}, **kw): | ||
45 | if hasattr(v, "im"): | ||
46 | args[k] = _Operand(v) | ||
47 | |||
48 | - out = builtins.eval(expression, args) | ||
49 | + code = compile(expression, "<string>", "eval") | ||
50 | + for name in code.co_names: | ||
51 | + if name not in args and name != "abs": | ||
52 | + raise ValueError(f"'{name}' not allowed") | ||
53 | + | ||
54 | + out = builtins.eval(expression, {"__builtins": {"abs": abs}}, args) | ||
55 | try: | ||
56 | return out.im | ||
57 | except AttributeError: | ||
58 | -- | ||
59 | 2.33.0 | ||
60 | |||