Description
Proposal
As explained in #18645, one is currently missing a way to wipe sensitive material from memory in Go.
Recently a new library (memguard) arose which tries to solve that exact problem and I think something like this should be available in the Go 2 standard library.
(memguard
only works with byte slices, this proposal extends that behavior to more (all?) built-in types.)
Reasoning
It's a common practice to erase sensitive data from memory after use and ensure that it is never swapped out to disk. This pretty much sums it up:
RAM was shown to maintain its charge for several seconds after being
unplugged. Freezing it (e.g. with a compressed air can held upside
down) extends this to several minutes, maybe even hours. This is
enough time to plug the memory into another system and dump its
contents to a file, which can then be analyzed.
Examples
Example 1
Variables and constants which are intended to hold sensitive data such as cryptographic keys can be declared / initialized as follows:
var key sensitive []byte // key marked "sensitive" -> memory is mlock'ed etc.
// Similarly, using :=
key sensitive := []byte("Password")
// The same using const
const pass sensitive = "Password"
As soon as key
in the example above goes out of scope it is securely wiped (to be defined) from memory.
Example 2
func main() {
pass sensitive := "Password"
printString(pass)
} // pass is wiped
func printString(s string) {
println(s)
} // s is wiped as it was declared sensitive before being copied-by-value to this function
Example 3
Reassigning variables marked as sensitive
should wipe them first
func main() {
pass sensitive := "Password"
pass = "Something else" // pass is wiped and then assigned its new value
}
Additions
One should be able to wipe vars declared sensitive
before they go out of scope:
wipe(key) // panics if key is not marked "sensitive"
// key is now reset to its zero value: "" for strings, 0 for ints, ..
EDIT:
- Fixed code indentation
- Fix typo:
mmap
->mlock