sysutils.go 800 B

12345678910111213141516171819202122232425
  1. package common
  2. import (
  3. "fmt"
  4. "golang.org/x/sys/unix"
  5. "os"
  6. "syscall"
  7. )
  8. // LockMemory locks current and future pages in memory to protect secret keys from being swapped out to disk.
  9. // It's possible (and strongly recommended) to deploy Wormhole such that keys are only ever
  10. // stored in memory and never touch the disk. This is a privileged operation and requires CAP_IPC_LOCK.
  11. func LockMemory() {
  12. err := unix.Mlockall(syscall.MCL_CURRENT | syscall.MCL_FUTURE)
  13. if err != nil {
  14. fmt.Printf("Failed to lock memory: %v (CAP_IPC_LOCK missing?)\n", err)
  15. os.Exit(1)
  16. }
  17. }
  18. // SetRestrictiveUmask masks the group and world bits. This ensures that key material
  19. // and sockets we create aren't accidentally group- or world-readable.
  20. func SetRestrictiveUmask() {
  21. syscall.Umask(0077) // cannot fail
  22. }