From a776b068a8b7beb39e676e7e31a7b5b4808ea133 Mon Sep 17 00:00:00 2001 From: argusSecurityBot <130381185+argusSecurityBot@users.noreply.github.com> Date: Tue, 20 Oct 2026 17:03:59 +0000 Subject: [PATCH 1/2] Validate livewire indices and reject images too large for int indexing LiveWire.select only checked tuple/list coordinates; an integer index was passed to the C extension unchecked and used to index the cost, predecessor and label arrays. Reject indices outside the image, keeping -1 as a no-op while no source is set, which close() relies on. The grid routines compute the number of pixels as an int product of the image dimensions, so images with more than INT_MAX elements wrap the size and under-allocate the heap that is then indexed with the real pixel coordinates. Raise ValueError for such inputs instead. --- src/pyift/livewire.py | 3 +++ src/pyift_c_ext/_livewire.c | 6 +++++ src/pyift_c_ext/_shortestpath.c | 41 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/pyift/livewire.py b/src/pyift/livewire.py index 1f2f0ed..2ca7a23 100644 --- a/src/pyift/livewire.py +++ b/src/pyift/livewire.py @@ -187,6 +187,9 @@ def select(self, position: Union[Sequence[int], int]) -> None: if not isinstance(position, int): raise TypeError('`position` must be a integer, tuple or list.') + if not (0 <= position < self.size[0] * self.size[1] or position == self.source == -1): + raise ValueError('Index out of image boundary, {}'.format(self.size)) + if self.source != -1: self.cancel() self.current = self._opt_path(self.source, position) diff --git a/src/pyift_c_ext/_livewire.c b/src/pyift_c_ext/_livewire.c index 903b9e1..a1d0885 100644 --- a/src/pyift_c_ext/_livewire.c +++ b/src/pyift_c_ext/_livewire.c @@ -5,6 +5,7 @@ #include "set.h" #include +#include #ifndef M_SQRT2 #define M_SQRT2 1.41421356237309504880 @@ -46,6 +47,11 @@ PyObject *_livewirePath(PyArrayObject *image, PyArrayObject *saliency, PyArrayOb sal_d = PyArray_DIM(saliency, 2); } + if (PyArray_SIZE(image) > INT_MAX || (saliency && PyArray_SIZE(saliency) > INT_MAX)) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + return NULL; + } + Coord shape = {.x = PyArray_DIM(image, 1), .y = PyArray_DIM(image, 0), .z = 1}; diff --git a/src/pyift_c_ext/_shortestpath.c b/src/pyift_c_ext/_shortestpath.c index d701249..19bce78 100644 --- a/src/pyift_c_ext/_shortestpath.c +++ b/src/pyift_c_ext/_shortestpath.c @@ -4,6 +4,7 @@ #include "heap.h" #include +#include static inline double square(double x) { @@ -23,6 +24,11 @@ PyObject *_seedCompetitionGrid(PyArrayObject *_image, PyArrayObject *_seeds) seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -154,6 +160,11 @@ PyObject *_seedCompetitionGraph(PyArrayObject *_weights, PyArrayObject *_indices return NULL; } + if (PyArray_DIM(_seeds, 0) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`seeds` is too large."); + return NULL; + } + PyArrayObject *weights = NULL, *indices = NULL, *indptr = NULL, *seeds = NULL; weights = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _weights, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); indices = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _indices, NPY_LONG, NPY_ARRAY_CARRAY_RO); @@ -266,6 +277,11 @@ PyObject *_dynamicArcWeightGridRoot(PyArrayObject *_image, PyArrayObject *_seeds seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -433,6 +449,11 @@ PyObject *_dynamicArcWeightGridLabel(PyArrayObject *_image, PyArrayObject *_seed seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -605,6 +626,11 @@ PyObject *_dynamicArcWeightGridExpDecay(PyArrayObject *_image, PyArrayObject *_s seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -753,6 +779,11 @@ PyObject *_euclideanDistanceTransformGrid(PyArrayObject *_mask, PyArrayObject *_ scales = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _scales, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); if (!mask || !scales) goto err1; + if (PyArray_SIZE(mask) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`mask` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(mask) == 2) { @@ -896,6 +927,11 @@ PyObject *_watershedFromMinimaGrid(PyArrayObject *_image, PyArrayObject *_mask, scales = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _scales, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); if (!image || !mask || !H_minima || !scales) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(image) == 2) { @@ -1041,6 +1077,11 @@ PyObject *_orientedSeedCompetitionGrid(PyArrayObject *_image, PyArrayObject *_se mask = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _mask, NPY_BOOL, NPY_ARRAY_CARRAY_RO); if (!image || !seeds || !mask) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(image) == 2) { From fa9375b2f3d5fde90092a15796c822b25cc38def Mon Sep 17 00:00:00 2001 From: argusSecurityBot <130381185+argusSecurityBot@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:35:20 +0000 Subject: [PATCH 2/2] Also reject grids whose pixel count exceeds INT_MAX when the channel axis is empty --- src/pyift_c_ext/_shortestpath.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pyift_c_ext/_shortestpath.c b/src/pyift_c_ext/_shortestpath.c index 19bce78..e800f39 100644 --- a/src/pyift_c_ext/_shortestpath.c +++ b/src/pyift_c_ext/_shortestpath.c @@ -24,8 +24,8 @@ PyObject *_seedCompetitionGrid(PyArrayObject *_image, PyArrayObject *_seeds) seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; - if (PyArray_SIZE(image) > INT_MAX) { - PyErr_SetString(PyExc_ValueError, "`image` is too large."); + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); goto err1; } @@ -277,8 +277,8 @@ PyObject *_dynamicArcWeightGridRoot(PyArrayObject *_image, PyArrayObject *_seeds seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; - if (PyArray_SIZE(image) > INT_MAX) { - PyErr_SetString(PyExc_ValueError, "`image` is too large."); + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); goto err1; } @@ -449,8 +449,8 @@ PyObject *_dynamicArcWeightGridLabel(PyArrayObject *_image, PyArrayObject *_seed seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; - if (PyArray_SIZE(image) > INT_MAX) { - PyErr_SetString(PyExc_ValueError, "`image` is too large."); + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); goto err1; } @@ -626,8 +626,8 @@ PyObject *_dynamicArcWeightGridExpDecay(PyArrayObject *_image, PyArrayObject *_s seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; - if (PyArray_SIZE(image) > INT_MAX) { - PyErr_SetString(PyExc_ValueError, "`image` is too large."); + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); goto err1; }