2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2015-03-05 22:44:03 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// memzero is used to zero out a byte buffer. This specific format is optimized
|
|
|
|
// by the compiler to use memclr to improve performance. See this code review:
|
|
|
|
// https://codereview.appspot.com/137880043
|
2016-04-05 22:18:19 +03:00
|
|
|
//
|
|
|
|
// Use of memzero is not a guarantee against memory analysis as described in
|
|
|
|
// the Vault threat model:
|
|
|
|
// https://www.vaultproject.io/docs/internals/security.html . Vault does not
|
|
|
|
// provide guarantees against memory analysis or raw memory dumping by
|
|
|
|
// operators, however it does minimize this exposure by zeroing out buffers
|
|
|
|
// that contain secrets as soon as they are no longer used. Starting with Go
|
|
|
|
// 1.5, the garbage collector was changed to become a "generational copying
|
|
|
|
// garbage collector." This change to the garbage collector makes it
|
|
|
|
// impossible for Vault to guarantee a buffer with a secret has not been
|
|
|
|
// copied during a garbage collection. It is therefore possible that secrets
|
|
|
|
// may be exist in memory that have not been wiped despite a pending memzero
|
|
|
|
// call. Over time any copied data with a secret will be reused and the
|
|
|
|
// memory overwritten thereby mitigating some of the risk from this threat
|
|
|
|
// vector.
|
2015-03-05 22:44:03 +03:00
|
|
|
func memzero(b []byte) {
|
2016-05-27 23:47:40 +03:00
|
|
|
if b == nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-05 22:44:03 +03:00
|
|
|
for i := range b {
|
|
|
|
b[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// randbytes is used to create a buffer of size n filled with random bytes
|
|
|
|
func randbytes(n int) []byte {
|
|
|
|
buf := make([]byte, n)
|
|
|
|
if _, err := rand.Read(buf); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to generate %d random bytes: %v", n, err))
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|