netliomax25-code opened PR #14959 from netliomax25-code:wasi-nn-check-tensor-data-length to bytecodealliance:main:
Related: #14532 (the same length check for the ONNX backend) and #14437 (a different wasi-nn host memory disclosure).
The PyTorch backend builds input tensors with
tch::Tensor::from_data_size, passing the guest's dimensions and data straight through. That forwards to libtorch'sat_tensor_of_data, which allocatesnumel(dims)elements andmemcpysnumel * element_sizebytes from the data pointer without looking at the slice length. A guest that sets an fp32 input with dimensions[4096]and only 4 bytes of data, through either WITXset_inputor WITcompute, makes the host copy roughly 16 KiB of adjacent heap memory into the tensor, where it feeds inference and can come back out throughget_output. A standalone program against tch 0.17 / libtorch 2.4.0 callingTensor::from_data_size(&[0; 4], &[4096], Kind::Float)got back 92288 non-zero bytes over 64 iterations.This checks that the data length equals the overflow-checked product of the dimensions and the element size before calling into tch, the same check #14532 added for ONNX. The OpenVINO backend is missing the same check at its two input sites; there
copy_from_slicepanics the host on a mismatch, so those return an error now as well. The underlying unsoundness is arguably in tch's safefrom_data_sizeand may be worth reporting there too, but the check is needed here either way.Tested with
--features pytorchagainst libtorch 2.4.0 on macOS arm64: the new unit test passes, and the existingnn_witx_image_classification_pytorchandnn_wit_image_classification_pytorchtests still pass. I could not run the OpenVINO tests locally since OpenVINO is not installed here; that change is a length comparison ahead of the existingcopy_from_slice.
netliomax25-code removed the review request for wasmtime-wasi-reviewers from PR #14959.
netliomax25-code requested rvolosatovs for a review on PR #14959.
netliomax25-code requested wasmtime-wasi-reviewers for a review on PR #14959.
alexcrichton commented on PR #14959:
cc @jlb6740 @rahulchaphalkar
alexcrichton removed the review request for rvolosatovs from PR #14959.
alexcrichton added the label wasi-nn on PR #14959.
:thumbs_up: rahulchaphalkar submitted PR review:
Thanks, looks good!
:thumbs_up: alexcrichton submitted PR review.
alexcrichton added PR #14959 wasi-nn: check input tensor data length in pytorch and openvino to the merge queue.
:check: alexcrichton merged PR #14959.
alexcrichton removed PR #14959 wasi-nn: check input tensor data length in pytorch and openvino from the merge queue.
Last updated: Oct 31 2026 at 23:03 UTC