-
Notifications
You must be signed in to change notification settings - Fork 1
/
secrets.go
519 lines (425 loc) · 13.7 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Package secrets creates low-level heap-allocated buffers safe for
// the storage of cryptographic secrets.
//
// A Secret is protected from being read from, written to, or executed
// once allocated. They are prevented from being paged out to swap
// (although systems that hibernate will be able to bypass this
// restriction), they have guard pages and a canary to protect against
// buffer overflows and underflows, and their contents are
// automatically zeroed when they become garbage collected.
//
// A Secret attempts to protect memory contents pessimistically. If
// the memory cannot be protected during initialization, no memory
// will be allocated and an error will be returned. If memory
// protections cannot be maintained during the lifespan of an existing
// Secret, the library will panic.
//
// The use of this package should be limited to storing cryptographic
// secrets. In order to provide the promised protections, allocations
// are significantly larger than the amount of memory requested and
// the number of operations during allocation is more than with
// typical allocators like malloc.
//
// Examples
//
// // Create a new Secret 32 bytes in size. The Secret is
// // initialized to zero bytes.
// secret, err := NewSecret(32)
//
// // Allow the Secret to be written to. We defer locking the Secret
// // so we can guarantee that its contents are protected once the
// // function returns.
// secret.Write()
// defer secret.Lock()
//
// // Read (up to) 32 bytes from stdin into the secret
// os.Stdin.Read(secret.Slice())
//
// // This will automatically happen once the Secret is garbage
// // collected, but being explicit allows the memory to be zeroed
// // out earlier.
// secret.Wipe()
//
package secrets
// #cgo pkg-config: libsodium
//
// #include <string.h>
// #include <sys/mman.h>
// #include <unistd.h>
//
// #include <sodium/core.h>
// #include <sodium/randombytes.h>
// #include <sodium/utils.h>
//
// #define _MAP_FAILED (intptr_t)MAP_FAILED
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
var (
// the size of a page of memory
pageSize = C.size_t(C.getpagesize())
// the canary will be filled during init()
canarySize = C.size_t(128)
canary = C.malloc(canarySize)
)
func init() {
if canary == nil {
panic("secrets: couldn't allocate memory for a canary")
}
if int(C.sodium_init()) == -1 {
panic("secrets: libsodium couldn't be initialized")
}
// give the canary a cryptographically random default value
C.randombytes_buf(canary, canarySize)
}
// A Secret contains a protected cryptographic secret. The contents of
// the Secret may only be accessed when explicitly unlocked, and its
// memory is zeroed out before being released back to the operating system.
type Secret struct {
secret *secret
}
// NewSecret creates a new Secret capable of storing len bytes. The
// Secret cannot be read from or written to until unlocked.
//
// If memory allocation fails or memory regions can't be adequately
// protected, an error will be returned.
func NewSecret(
len int,
) (*Secret, error) {
var (
sec Secret
err error
)
sec = Secret{&secret{}}
// empty secrets are valid, but we don't have anything to do
if len == 0 {
return &sec, nil
}
if err = sec.secret.alloc(C.size_t(len)); err != nil {
return nil, err
}
return &sec, nil
}
// NewSecretFromBytes creates a new Secret from a preexisting byte
// slice. The contents of the byte slice are zeroed out after they are
// copied into the Secret.
//
// If memory allocation fails or memory regions can't be adequately
// protected, an error will be returned.
//
// Note that a Secret allocated this way cannot make any security
// guarantees about the original contents of the byte slice. They may
// have been copied by other parts of the program, or silently copied
// by the Go runtime. If you must allocate a Secret from a byte slice,
// it should be done as soon as possible after the byte slice has had
// the secret data written to it.
func NewSecretFromBytes(
data []byte,
) (*Secret, error) {
var (
dataPtr, dataSize = _byteSlicePtrSize(data)
secret, err = NewSecret(len(data))
)
if err != nil {
return nil, err
}
secret.Write()
defer secret.Lock()
C.memcpy(secret.Pointer(), dataPtr, dataSize)
C.sodium_memzero(dataPtr, dataSize)
return secret, nil
}
// Returns the length of the Secret in bytes.
func (s Secret) Len() int { return int(s.Size()) }
// Returns the C size_t length of the Secret in bytes
func (s Secret) Size() C.size_t { return s.secret.size }
// Locks the Secret, preventing any access to its contents.
func (s Secret) Lock() { s.secret.lock() }
// Allows the Secret's contents to be read. Immediately after calling
// this method, always `defer secret.Lock()` to ensure its protection
// is restored.
func (s Secret) Read() { s.secret.unlock(C.PROT_READ) }
// Allows the Secret's contents to be written to. Immediately after
// calling this method, always `defer secret.Lock()` to ensure its
// protection is restored.
func (s Secret) Write() { s.secret.unlock(C.PROT_WRITE) }
// Allows the Secret's contents to be read from and written
// to. Immediately after calling this method, always `defer
// secret.Lock()` to ensure its protection is restored.
func (s Secret) ReadWrite() { s.secret.unlock(C.PROT_READ | C.PROT_WRITE) }
// Returns an unsafe.Pointer pointing to the memory contents of the
// Secret. When accessing memory through this pointer, take care to
// never access more than Len() bytes from this pointer. This pointer
// can only be read from or written to when the Secret itself is
// unlocked.
func (s Secret) Pointer() unsafe.Pointer {
return s.secret.ptr
}
// Returns a byte slice containing the contents of the Secret. This
// slice may only be read from or written to when the Secret itself is
// unlocked. Take care not to create copies of the contents of the
// returned slice.
func (s Secret) Slice() []byte {
sh := reflect.SliceHeader{
Data: uintptr(s.Pointer()),
Len: s.Len(),
Cap: s.Len(),
}
// cast the address of the SliceHeader into a slice pointer,
// then take the value of that pointer to get the data as an
// actual slice
return *(*[]byte)(unsafe.Pointer(&sh))
}
// Copies a Secret's contents into a new Secret. If either allocating
// the new Secret or unlocking the existing Secret fails, returns an
// error.
func (s Secret) Copy() (*Secret, error) {
copy, err := NewSecret(s.Len())
if err != nil {
return nil, err
}
copy.Write()
defer copy.Lock()
s.Read()
defer s.Lock()
C.memcpy(
copy.Pointer(),
s.Pointer(),
s.Size(),
)
return copy, nil
}
// Reduces the size of the Secret to len bytes. The location of the
// overflow canary is adjusted to reflect the new size of the
// Secret. If len is larger than the current length of the secret,
// no operation is performed.
func (s Secret) Trim(len int) error {
// trim only shrinks; otherwise it's a no-op
if len >= s.Len() {
return nil
}
return s.secret.realloc(C.size_t(len))
}
// Splits the Secret into two halves, with the right half beginning at
// the specified offset. The original secret is trimmed to only
// contain the contents of the left half, and the contents of the
// right half are copied into a new Secret which is returned.
func (s Secret) Split(offset int) (*Secret, error) {
var (
right *Secret
err error
)
s.ReadWrite()
defer s.Lock()
if right, err = NewSecretFromBytes(s.Slice()[offset:]); err != nil {
return nil, err
}
s.Trim(offset)
return right, nil
}
// Compares two Secrets for equality in constant time.
func (s Secret) Equal(other *Secret) bool {
if s.Len() != other.Len() {
return false
}
s.Read()
defer s.Lock()
other.Read()
defer other.Lock()
ret := C.sodium_memcmp(
other.Pointer(),
s.Pointer(),
s.Size(),
)
return ret == 0
}
// Immediately zeroes out and releases the Secret's memory. Any
// attempt to reuse a Secret after a call to Wipe() will result in
// undefined behavior.
func (s Secret) Wipe() {
// no need to run the finalizer now; this prevents us from
// accidentally trying to re-free the same memory
runtime.SetFinalizer(s.secret, nil)
// explicitly zero out and free memory
s.secret.free()
s.secret = nil
}
// The actual struct that holds pointers to the underlying data for a
// Secret. This is structured so that the secret has a finalizer which
// cleans up and frees allocated memory once it is garbage collected,
// but a Secret can be copied around (for instance, by passing them as
// values to functions) and garbage collected without invoking the
// finalizer.
type secret struct {
ptr unsafe.Pointer
size C.size_t
}
// Allocates enough memory to contain size bytes, plus room for a
// canary and a guard page before and after the allocation. The pages
// are locked into memory.
//
// The allocated memory is zeroed.
func (s *secret) alloc(size C.size_t) error {
var err error
// calculate the size of the user region, then allocate enough
// guarded pages for that amount
s.size = size
s.ptr, err = guardedAlloc(size)
if err != nil {
return err
}
// ensure we clean up after ourselves, now that we've
// allocated memory
runtime.SetFinalizer(s, func(s *secret) { s.free() })
s.unlock(C.PROT_WRITE)
defer s.lock()
C.sodium_memzero(s.ptr, s.size)
return nil
}
func (s *secret) realloc(size C.size_t) error {
ptr, err := guardedRealloc(s.ptr, s.size, size)
if err != nil {
return err
}
s.ptr = ptr
s.size = size
return nil
}
// Zeroes out the contents of the secret and releases its memory back
// to the system.
func (s *secret) free() {
// free the entire allocated region
guardedFree(s.ptr, s.size)
// don't maintain dangling pointers
s.ptr = nil
s.size = 0
}
// Locks the secret's contents, preventing them from being read,
// written to, or executed.
func (s *secret) lock() {
if ret, err := C.mprotect(s.ptr, s.size, C.PROT_NONE); ret != 0 {
panic(err)
}
}
// Unlocks the secret's contents, giving them the protection level
// specified.
func (s *secret) unlock(prot C.int) {
if ret, err := C.mprotect(s.ptr, s.size, prot); ret != 0 {
panic(err)
}
}
// Calculates the size of an allocation with enough room for two extra
// guard pages.
func guardedAllocSize(size C.size_t) C.size_t {
return 2*pageSize + _pageRound(size)
}
// Allocates the requested amount of memory, plus two guard pages. The
// entire region is protected against any memory access. The pointer
// returned points to a region inbetween the guard pages with enough
// space to contain size bytes. An error is returned if the memory
// can't be allocated or protected.
func guardedAlloc(size C.size_t) (unsafe.Pointer, error) {
var (
userSize = size + canarySize
allocSize = guardedAllocSize(userSize)
)
allocPtr, err := C.mmap(nil, allocSize, C.PROT_NONE, C.MAP_ANON|C.MAP_PRIVATE, -1, 0)
if int(uintptr(allocPtr)) == C._MAP_FAILED {
return nil, err
}
userPtr := _ptrAdd(allocPtr, pageSize)
// we only need to mlock the user region; the guard pages can
// be swapped to disk if the OS wants to
if ret, err := C.sodium_mlock(userPtr, userSize); ret != 0 {
return nil, err
}
canaryWrite(userPtr, size)
// return a pointer to the interior non-guard pages
return userPtr, nil
}
func guardedRealloc(
ptr unsafe.Pointer,
old C.size_t,
new C.size_t,
) (unsafe.Pointer, error) {
if old == new {
return ptr, nil
}
if old > new {
// TODO(stephen):
// - wipe the now-unused part of the secret
canaryVerify(ptr, old)
canaryWrite(ptr, new)
return ptr, nil
}
panic("secrets: guardedRealloc only shrinks allocations")
}
// Frees an earlier allocation of the given number of bytes. Also
// makes sure to free the surrounding pages.
func guardedFree(ptr unsafe.Pointer, size C.size_t) {
var (
allocSize = guardedAllocSize(size)
userSize = size + canarySize
allocPtr = _ptrAdd(ptr, -pageSize)
userPtr = ptr
)
canaryVerify(userPtr, size)
if ret, err := C.mprotect(userPtr, userSize, C.PROT_READ|C.PROT_WRITE); ret != 0 {
panic(err)
}
// wipe the user region (and canary, to avoid it from being leaked)
C.sodium_munlock(userPtr, userSize)
C.munmap(allocPtr, allocSize)
}
func canaryWrite(ptr unsafe.Pointer, size C.size_t) {
var (
canaryPtr = _ptrAdd(ptr, size)
canaryPagePtr = _ptrPageRound(canaryPtr)
)
// allow the user region to be written to, for the canary
if ret, _ := C.mprotect(canaryPagePtr, canarySize, C.PROT_WRITE); ret != 0 {
panic("secrets: couldn't write a canary")
}
// write the canary immediately after the user region
C.memcpy(canaryPtr, canary, canarySize)
// re-lock the user region
if ret, _ := C.mprotect(canaryPagePtr, canarySize, C.PROT_NONE); ret != 0 {
panic("secrets: couldn't write a canary")
}
}
func canaryVerify(ptr unsafe.Pointer, size C.size_t) {
var (
canaryPtr = _ptrAdd(ptr, size)
canaryPagePtr = _ptrPageRound(canaryPtr)
)
// ensure the canary can be read and the user area can be
// wiped clean
if ret, err := C.mprotect(canaryPagePtr, canarySize, C.PROT_READ); ret != 0 {
panic(err)
}
// verify the canary
if C.memcmp(canaryPtr, canary, canarySize) != C.int(0) {
panic("secrets: buffer overflow canary triggered")
}
}
// Rounds the provided pointer to the beginning of its page.
func _ptrPageRound(ptr unsafe.Pointer) unsafe.Pointer {
return _ptrAdd(ptr, -(C.size_t(uintptr(ptr)) % pageSize))
}
// Rounds size to the next highest page boundary.
func _pageRound(size C.size_t) C.size_t {
return (size/pageSize)*pageSize + pageSize
}
// Returns a pointer to the underlying buffer and the size of a byte slice.
func _byteSlicePtrSize(slice []byte) (unsafe.Pointer, C.size_t) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
return unsafe.Pointer(sh.Data), C.size_t(sh.Len)
}
// Performs pointer arithmetic, adding an offset (positive or
// negative) to the provided pointer.
func _ptrAdd(ptr unsafe.Pointer, offset C.size_t) unsafe.Pointer {
return unsafe.Pointer(uintptr(ptr) + uintptr(offset))
}