config_linux.go 1.9 KB

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