config_unix.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // +build freebsd linux
  2. package configs
  3. import "fmt"
  4. // Gets the root uid for the process on host which could be non-zero
  5. // when user namespaces are enabled.
  6. func (c Config) HostUID() (int, error) {
  7. if c.Namespaces.Contains(NEWUSER) {
  8. if c.UidMappings == nil {
  9. return -1, fmt.Errorf("User namespaces enabled, but no user mappings found.")
  10. }
  11. id, found := c.hostIDFromMapping(0, c.UidMappings)
  12. if !found {
  13. return -1, fmt.Errorf("User namespaces enabled, but no root user mapping found.")
  14. }
  15. return id, nil
  16. }
  17. // Return default root uid 0
  18. return 0, nil
  19. }
  20. // Gets the root gid for the process on host which could be non-zero
  21. // when user namespaces are enabled.
  22. func (c Config) HostGID() (int, error) {
  23. if c.Namespaces.Contains(NEWUSER) {
  24. if c.GidMappings == nil {
  25. return -1, fmt.Errorf("User namespaces enabled, but no gid mappings found.")
  26. }
  27. id, found := c.hostIDFromMapping(0, c.GidMappings)
  28. if !found {
  29. return -1, fmt.Errorf("User namespaces enabled, but no root group mapping found.")
  30. }
  31. return id, nil
  32. }
  33. // Return default root gid 0
  34. return 0, nil
  35. }
  36. // Utility function that gets a host ID for a container ID from user namespace map
  37. // if that ID is present in the map.
  38. func (c Config) hostIDFromMapping(containerID int, uMap []IDMap) (int, bool) {
  39. for _, m := range uMap {
  40. if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
  41. hostID := m.HostID + (containerID - m.ContainerID)
  42. return hostID, true
  43. }
  44. }
  45. return -1, false
  46. }