Skip to content

Commit 54ba4db

Browse files
radarherewiredfool
andauthored
Fix OOB Write with invalid tile extents (python-pillow#9427)
Co-authored-by: Eric Soroos <eric-github@soroos.net>
1 parent f78663b commit 54ba4db

File tree

9 files changed

+53
-2
lines changed

9 files changed

+53
-2
lines changed

Tests/images/psd-oob-write-x.psd

1.1 KB
Binary file not shown.

Tests/images/psd-oob-write-y.psd

1.1 KB
Binary file not shown.

Tests/images/psd-oob-write.psd

36.3 KB
Binary file not shown.

Tests/test_file_psd.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,20 @@ def test_layer_crashes(test_file: str) -> None:
182182
assert isinstance(im, PsdImagePlugin.PsdImageFile)
183183
with pytest.raises(SyntaxError):
184184
im.layers
185+
186+
187+
@pytest.mark.parametrize(
188+
"test_file",
189+
[
190+
"Tests/images/psd-oob-write.psd",
191+
"Tests/images/psd-oob-write-x.psd",
192+
"Tests/images/psd-oob-write-y.psd",
193+
],
194+
)
195+
def test_bounds_crash(test_file: str) -> None:
196+
with Image.open(test_file) as im:
197+
assert isinstance(im, PsdImagePlugin.PsdImageFile)
198+
im.seek(im.n_frames)
199+
200+
with pytest.raises(ValueError):
201+
im.load()

Tests/test_imagefile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ def test_negative_offset(self) -> None:
163163
with pytest.raises(ValueError, match="Tile offset cannot be negative"):
164164
im.load()
165165

166+
@pytest.mark.parametrize("xy", ((-1, 0), (0, -1)))
167+
def test_negative_tile_extents(self, xy: tuple[int, int]) -> None:
168+
im = Image.new("1", (1, 1))
169+
fp = BytesIO()
170+
with pytest.raises(SystemError, match="tile cannot extend outside image"):
171+
ImageFile._save(im, fp, [ImageFile._Tile("raw", xy + (1, 1), 0, "1")])
172+
166173
def test_no_format(self) -> None:
167174
buf = BytesIO(b"\x00" * 255)
168175

docs/releasenotes/12.1.1.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
12.1.1
2+
------
3+
4+
Security
5+
========
6+
7+
:cve:`2021-25289`: Fix OOB write with invalid tile extents
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
Check that tile extents do not use negative x or y offsets when decoding or encoding,
11+
and raise an error if they do, rather than allowing an OOB write.
12+
13+
An out-of-bounds write may be triggered when opening a specially crafted PSD image.
14+
This only affects Pillow >= 10.3.0. Reported by
15+
`Yarden Porat <https://github.com/yardenporat353>`__.
16+
17+
Other changes
18+
=============
19+
20+
Patch libavif for svt-av1 4.0 compatibility
21+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
23+
A patch has been added to ``depends/install_libavif.sh``, to allow libavif 1.3.0 to be
24+
compatible with the recently released svt-av1 4.0.0.

docs/releasenotes/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ expected to be backported to earlier versions.
1515
:maxdepth: 2
1616

1717
versioning
18+
12.1.1
1819
12.1.0
1920
12.0.0
2021
11.3.0

src/decode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ _setimage(ImagingDecoderObject *decoder, PyObject *args) {
186186
state->ysize = y1 - y0;
187187
}
188188

189-
if (state->xsize <= 0 || state->xsize + state->xoff > (int)im->xsize ||
189+
if (state->xoff < 0 || state->xsize <= 0 ||
190+
state->xsize + state->xoff > (int)im->xsize || state->yoff < 0 ||
190191
state->ysize <= 0 || state->ysize + state->yoff > (int)im->ysize) {
191192
PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image");
192193
return NULL;

src/encode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ _setimage(ImagingEncoderObject *encoder, PyObject *args) {
254254
state->ysize = y1 - y0;
255255
}
256256

257-
if (state->xsize <= 0 || state->xsize + state->xoff > im->xsize ||
257+
if (state->xoff < 0 || state->xsize <= 0 ||
258+
state->xsize + state->xoff > im->xsize || state->yoff < 0 ||
258259
state->ysize <= 0 || state->ysize + state->yoff > im->ysize) {
259260
PyErr_SetString(PyExc_SystemError, "tile cannot extend outside image");
260261
return NULL;

0 commit comments

Comments
 (0)