🔧 C#: Fix an ArrayPool Buffer Returned Still Dirty
ArrayPool<T>.Shared.Return() does not clear the buffer’s contents by default — the clearArray parameter defaults to false specifically because zeroing on every return has a real performance cost, and the pool’s whole purpose is avoiding allocation overhead in hot paths. That means a rented buffer handed back without explicitly requesting a clear still contains whatever data the previous caller wrote into it, and the very next caller to rent a buffer of the same size has a real chance of receiving that exact same backing array — including every byte of data the previous owner never bothered to clear, which is fine for throwaway scratch space, but quietly dangerous when the buffer ever held something like authentication tokens, passwords, or other sensitive data that genuinely shouldn’t outlive its intended use.
🔎 The Problem
byte[] buffer = ArrayPool<byte>.Shared.Rent(256);
try
{
int written = PopulateWithSensitiveToken(buffer); // writes a secret
SendToken(buffer, written);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
// clearArray defaults to false - the secret token's bytes are
// still sitting in this array when it goes back into the pool
}
// Elsewhere, a completely unrelated piece of code rents a
// same-sized buffer for something totally unrelated and non-sensitive:
byte[] scratch = ArrayPool<byte>.Shared.Rent(256);
// there's a real chance this IS the same backing array, still
// holding the previous caller's token bytes until scratch overwrites them
✅ Fix: Explicitly Clear the Buffer When It Ever Held Sensitive Data
- Pass
clearArray: truetoArrayPool<T>.Shared.Return(buffer, clearArray: true)for any buffer that held sensitive data at any point during its use — the small, explicit cost of zeroing is the correct tradeoff for exactly the cases where leaving stale data behind in a shared pool is actually a problem. - Default to NOT pooling (use a freshly allocated, short-lived array instead) for genuinely small, infrequent allocations of sensitive data, and reserve
ArrayPoolspecifically for high-frequency, non-sensitive scratch buffers where its performance benefit is actually worth the bookkeeping required to use it safely. - Consider
CryptographicOperations.ZeroMemory()(or an explicit clearing loop) immediately after the buffer’s sensitive use is finished, rather than relying solely on the return call — this clears the data the moment it’s no longer needed, instead of leaving it sitting in memory for however long the buffer remains checked out before it’s eventually returned.
🚨 Why This Is Dangerous
- A pooled buffer is just a reused chunk of process memory, and unlike disposing an object there’s no garbage collector eventually reclaiming and zeroing it — the leftover sensitive bytes simply stay there, readable by any other code in the same process that happens to rent a same-sized buffer next, for as long as the pool continues reusing that particular array.
ArrayPool gives you back the same memory, not a clean copy of it — and anything you wrote there before returning it is still there for whoever the pool hands that buffer to next, unless you asked it to clear first.





