Skip to content

Validate livewire indices and reject images too large for int indexing - #32

Open
argusSecurityBot wants to merge 2 commits into
PyIFT:masterfrom
argusSecurityBot:fix-index-and-size-overflow
Open

argusSecurityBot wants to merge 2 commits into
PyIFT:masterfrom
argusSecurityBot:fix-index-and-size-overflow

Conversation

@argusSecurityBot

Copy link
Copy Markdown

LiveWire.select() bounds-checks only a (y, x) tuple (src/pyift/livewire.py:166); a plain int index goes unchecked to _pyift.livewire_path as src/dst (livewire.py:192).
_livewirePath then writes costs_ptr[src]/preds_ptr[src]/labels_ptr[src] (_livewire.c:63) and reads preds_ptr[dst] (_livewire.c:150), so an index outside the image is an out-of-bounds write or read.
Separately, the grid routines compute the pixel count as int size = shape.x * shape.y * shape.z (_shortestpath.c:48, same pattern at 290, 457, 629, 775, 918, 1063); above INT_MAX it wraps, createHeap(size) (line 65) under-allocates, and the loop indexes it with real pixel indices (line 93).
Fix: select() raises ValueError for an int index outside [0, height*width) (-1 stays a no-op with no source set), and each routine raises ValueError when the array has more than INT_MAX elements.

import numpy as np
from pyift.livewire import LiveWire
lw = LiveWire(np.zeros((4, 4, 1)), sigma=1.0)
lw.select(1000)
lw.confirm()
lw.select((1, 1))

SUMMARY: AddressSanitizer: heap-buffer-overflow _livewire.c:63 (WRITE). Tests: 12 passed before and after.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant