Clear ArrayPool buffers before returning to pool#1097
Conversation
Pooled byte arrays carrying credentials (passwords, tokens, JWT, nkey signatures) were returned to ArrayPool without clearing, allowing sensitive data to leak into subsequent rentals. Add clearArray: true to all ArrayPool.Return() calls in NatsMemoryOwner, NatsBufferWriter, NatsPooledBufferWriter, and SeqeunceBuilder. Introduce ClearingMemoryPool for the Pipe in CommandWriter so CONNECT message buffers are also cleared.
|
Claude finished @mtmk's task in 2m 26s —— View job PR Review
The fix is directionally correct — clearing pooled buffers prevents credential data from leaking into future rentals. The Issues found: 1. Hot-path clearing in 2. 3. Minor observations (not blocking):
|
Use Interlocked.Exchange in ClearingMemoryOwner.Dispose() for thread-safe disposal. Revert Resize clearArray default to false since the explicit clearArray:true calls in Reset() and Dispose() already cover the important paths without silently broadening the clearing scope to all resize operations.
this is this branch:
...and this is main:
Latency is within noise across all sizes, no measurable throughput regression. |
Avoid per-rent allocations in ClearingMemoryPool by pooling the wrapper objects via the lock-free ObjectPool and renting byte arrays directly from ArrayPool<byte>.Shared instead of going through MemoryPool<byte>.Shared which allocates an ArrayMemoryPoolBuffer on every call.
|
this is after pooling:
on 1 million iteration it performs better than main. we might need to check our pipe usage later, but this means there is no noticeable performance impact. |
Pooled byte arrays carrying credentials (passwords, tokens, JWT, nkey signatures) were returned to
ArrayPoolwithout clearing, allowing sensitive data to leak into subsequent buffer rentals. This addsclearArray: trueto allArrayPool.Return()calls and introduces aClearingMemoryPoolfor thePipeinCommandWritersoCONNECTmessage buffers are also zeroed.