Skip to content

ObjectToObjectMetricBase NumberOfParametersType is unsigned int: heap overflow in SyN for dense fields > 2^32 parameters #6949

Open

Description

@gdevenyi

Description

ObjectToObjectMetricBaseTemplate::NumberOfParametersType is unsigned int. TransformBase::NumberOfParametersType is IdentifierType (64-bit). When a metric uses a dense DisplacementFieldTransform with more than 2^32 parameters, GetNumberOfParameters() truncates the count. ImageToImageMetricv4::InitializeForIteration() then makes the derivative array smaller to the truncated size, and the metric threads write past the end of the buffer. The result is heap corruption (segfault or glibc munmap_chunk(): invalid pointer).

In 3D, the limit is 2^32 / 3 ≈ 1,431,655,765 voxels in the virtual domain. This occurs with SyNImageRegistrationMethod (and the BSpline SyN variants) at full resolution on large images.

Downstream report: ANTsX/ANTs#2033 (852 × 1182 × 1965 = 1,978,880,760 voxels, SyN + ANTS neighborhood CC, ITK 5.4.x).

Call chain (ITK v5.4.7, the version that ANTs uses)

  1. Type: itkObjectToObjectMetricBase.h#L173: using NumberOfParametersType = unsigned int;. It is the same on main.
  2. Truncation: itkObjectToObjectMetric.hxx#L127-L133 returns m_MovingTransform->GetNumberOfParameters() (64-bit) through the 32-bit return type. There is no check.
  3. Correct allocation in SyN: itkSyNImageRegistrationMethod.hxx#L595-L597 sizes metricDerivative as GetNumberOfPixels() * ImageDimension (64-bit).
  4. Buffer made smaller: itkImageToImageMetricv4.hxx#L289-L292: const NumberOfParametersType globalDerivativeSize = this->GetNumberOfParameters(); then SetSize(globalDerivativeSize). This frees the correct buffer and allocates a smaller one.
  5. Out-of-bounds writes: itkImageToImageMetricv4GetValueAndDerivativeThreaderBase.hxx#L73 aliases each thread's Derivatives to the smaller buffer. #L315 writes Derivatives[offset + i] with a 64-bit offset from the virtual index.
  6. Out-of-bounds read: itkSyNImageRegistrationMethod.hxx#L633 reads metricDerivative[count++] up to pixels * ImageDimension.

Arithmetic

Virtual domain Voxels Parameters (×3) GetNumberOfParameters()
568 × 788 × 1310 586,335,040 1,759,005,120 correct
852 × 1182 × 1965 1,978,880,760 5,936,642,280 1,641,674,984

Same type at other locations

  • ObjectToObjectMultiMetricv4::GetValueAndDerivative / InitializeForIteration do the same truncating SetSize.
  • The optimizer-weights loop in SyNImageRegistrationMethod (#L604-L613, only when optimizer weights are set) overruns as well when the size is truncated.
  • These are 32-bit too, but they do not overflow at these sizes: ImageVectorOptimizerParametersHelper.hxx#L42 (unsigned int sizeInVectors), Array::GetNumberOfElements() (returns unsigned int), and the vnl_c_vector helpers (unsigned n).

Expected behavior

The metric reports the full 64-bit parameter count, or it gives a clear exception when the count is more than the type can hold.

Possible fix

Change ObjectToObjectMetricBaseTemplate::NumberOfParametersType to IdentifierType (the same as TransformBase), and update the cached copies in the metric threaders. If the API change is too large for 5.4.x, add a check in ObjectToObjectMetric::GetNumberOfParameters() that throws when the transform count is more than NumericTraits<NumberOfParametersType>::max().

Versions

ITK v5.4.x (checked at v5.4.0 and v5.4.7) and current main.

The analysis was done with Claude Code and checked against the source.

Activity

  1. added a commit that references this issue on Nov 19, 2026
    5a11c16
  2. gdevenyi commented on Nov 19, 2026

    @gdevenyi
    ContributorAuthor

    Two follow-ups to the #6991 guard:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions