Skip to content

MNT: protect from out-of-bounds data access at the c level #14478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/matplotlib/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def mpl_test_settings(request):
assert len(backend_marker.args) == 1, \
"Marker 'backend' must specify 1 backend."
backend, = backend_marker.args
skip_on_importerror = backend_marker.kwargs.get(
'skip_on_importerror', False)
prev_backend = matplotlib.get_backend()

style = '_classic_test' # Default of cleanup and image_comparison too.
Expand All @@ -45,7 +47,7 @@ def mpl_test_settings(request):
except ImportError as exc:
# Should only occur for the cairo backend tests, if neither
# pycairo nor cairocffi are installed.
if 'cairo' in backend.lower():
if 'cairo' in backend.lower() or skip_on_importerror:
pytest.skip("Failed to switch to backend {} ({})."
.format(backend, exc))
else:
Expand Down
28 changes: 28 additions & 0 deletions lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
import numpy as np
from matplotlib import pyplot as plt


@pytest.mark.backend('TkAgg', skip_on_importerror=True)
def test_blit():
from matplotlib.backends import _tkagg
def evil_blit(photoimage, aggimage, offsets, bboxptr):
data = np.asarray(aggimage)
height, width = data.shape[:2]
dataptr = (height, width, data.ctypes.data)
_tkagg.blit(
photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets,
bboxptr)

fig, ax = plt.subplots()
for bad_boxes in ((-1, 2, 0, 2),
(2, 0, 0, 2),
(1, 6, 0, 2),
(0, 2, -1, 2),
(0, 2, 2, 0),
(0, 2, 1, 6)):
with pytest.raises(ValueError):
evil_blit(fig.canvas._tkphoto,
np.ones((4, 4, 4)),
(0, 1, 2, 3),
bad_boxes)
5 changes: 5 additions & 0 deletions src/_tkagg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ static PyObject *mpl_tk_blit(PyObject *self, PyObject *args)
PyErr_SetString(PyExc_ValueError, "Failed to extract Tk_PhotoHandle");
goto exit;
}
if (0 > y1 || y1 > y2 || y2 > height || 0 > x1 || x1 > x2 || x2 > width) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (0 > y1 || y1 > y2 || y2 > height || 0 > x1 || x1 > x2 || x2 > width) {
if (y1 < 0 || y1 > y2 || y2 > height || x1 < 0 || x1 > x2 || x2 > width) {

I find it easier this way "y1 is smaller than 0 or larger than y2". 0 > y1 sort of ties a knot in my brain. 😄

Copy link
Member Author

@tacaswell tacaswell Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fair, but having all of the > go the same way is also helpful..

Moot now that @efiring merged it..

PyErr_SetString(PyExc_ValueError, "Attempting to draw out of bounds");
goto exit;
}

block.pixelPtr = data_ptr + 4 * ((height - y2) * width + x1);
block.width = x2 - x1;
block.height = y2 - y1;
Expand Down