userns_linux.go 848 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package userns
  2. import (
  3. "sync"
  4. "github.com/opencontainers/runc/libcontainer/user"
  5. )
  6. var (
  7. inUserNS bool
  8. nsOnce sync.Once
  9. )
  10. // runningInUserNS detects whether we are currently running in a user namespace.
  11. // Originally copied from github.com/lxc/lxd/shared/util.go
  12. func runningInUserNS() bool {
  13. nsOnce.Do(func() {
  14. uidmap, err := user.CurrentProcessUIDMap()
  15. if err != nil {
  16. // This kernel-provided file only exists if user namespaces are supported
  17. return
  18. }
  19. inUserNS = uidMapInUserNS(uidmap)
  20. })
  21. return inUserNS
  22. }
  23. func uidMapInUserNS(uidmap []user.IDMap) bool {
  24. /*
  25. * We assume we are in the initial user namespace if we have a full
  26. * range - 4294967295 uids starting at uid 0.
  27. */
  28. if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
  29. return false
  30. }
  31. return true
  32. }