Skip to content

Fix out-of-bounds write in point query instance stack - #630

Open
stefanatwork wants to merge 1 commit into
masterfrom
fix/pointquery-instance-stack-overflow
Open

stefanatwork wants to merge 1 commit into
masterfrom
fix/pointquery-instance-stack-overflow

Conversation

@stefanatwork

Copy link
Copy Markdown
Collaborator

The point query variant of instance_id_stack::push() guarded the instance stack capacity only with an assert, which is compiled out in release builds. It then unconditionally wrote the instance ID and both 4x4 transform matrices at context->instStackSize and always returned true.

Since rtcPointQuery() recurses into nested instances and all call sites gate recursion on the return value of push(), the missing runtime check meant a scene nested deeper than RTC_MAX_INSTANCE_LEVEL_COUNT drove instStackSize past the fixed-size arrays of the caller provided RTCPointQueryContext, writing instance transform data out of bounds. With the default RTC_MAX_INSTANCE_LEVEL_COUNT of 1, an instance of an instance is enough to corrupt memory past the context; deeper nesting scales the write distance arbitrarily.

Add the same runtime capacity check that the intersect/occluded variant of push() already performs: return false before any write when the stack is full. This makes point queries silently ignore instances nested deeper than RTC_MAX_INSTANCE_LEVEL_COUNT, which is the behaviour already documented for instancing and already implemented for ray queries. The existing call sites handle a false return by skipping the instance without a matching pop(), so the stack stays balanced.

The point query variant of instance_id_stack::push() guarded the
instance stack capacity only with an assert, which is compiled out in
release builds. It then unconditionally wrote the instance ID and both
4x4 transform matrices at context->instStackSize and always returned
true.

Since rtcPointQuery() recurses into nested instances and all call sites
gate recursion on the return value of push(), the missing runtime check
meant a scene nested deeper than RTC_MAX_INSTANCE_LEVEL_COUNT drove
instStackSize past the fixed-size arrays of the caller provided
RTCPointQueryContext, writing instance transform data out of bounds.
With the default RTC_MAX_INSTANCE_LEVEL_COUNT of 1, an instance of an
instance is enough to corrupt memory past the context; deeper nesting
scales the write distance arbitrarily.

Add the same runtime capacity check that the intersect/occluded variant
of push() already performs: return false before any write when the stack
is full. This makes point queries silently ignore instances nested
deeper than RTC_MAX_INSTANCE_LEVEL_COUNT, which is the behaviour already
documented for instancing and already implemented for ray queries. The
existing call sites handle a false return by skipping the instance
without a matching pop(), so the stack stays balanced.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants