mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
Merge pull request #5088 from kolyshkin/pointers
This commit is contained in:
@@ -26,67 +26,67 @@ type LinuxPersonality struct {
|
|||||||
|
|
||||||
// HostUID gets the translated uid for the process on host which could be
|
// HostUID gets the translated uid for the process on host which could be
|
||||||
// different when user namespaces are enabled.
|
// different when user namespaces are enabled.
|
||||||
func (c Config) HostUID(containerId int) (int, error) {
|
func (c *Config) HostUID(containerID int) (int, error) {
|
||||||
if c.Namespaces.Contains(NEWUSER) {
|
if c.Namespaces.Contains(NEWUSER) {
|
||||||
if len(c.UIDMappings) == 0 {
|
if len(c.UIDMappings) == 0 {
|
||||||
return -1, errNoUIDMap
|
return -1, errNoUIDMap
|
||||||
}
|
}
|
||||||
id, found := c.hostIDFromMapping(int64(containerId), c.UIDMappings)
|
id, found := c.hostIDFromMapping(int64(containerID), c.UIDMappings)
|
||||||
if !found {
|
if !found {
|
||||||
return -1, fmt.Errorf("user namespaces enabled, but no mapping found for uid %d", containerId)
|
return -1, fmt.Errorf("user namespaces enabled, but no mapping found for uid %d", containerID)
|
||||||
}
|
}
|
||||||
// If we are a 32-bit binary running on a 64-bit system, it's possible
|
// If we are a 32-bit binary running on a 64-bit system, it's possible
|
||||||
// the mapped user is too large to store in an int, which means we
|
// the mapped user is too large to store in an int, which means we
|
||||||
// cannot do the mapping. We can't just return an int64, because
|
// cannot do the mapping. We can't just return an int64, because
|
||||||
// os.Setuid() takes an int.
|
// os.Setuid() takes an int.
|
||||||
if id > math.MaxInt {
|
if id > math.MaxInt {
|
||||||
return -1, fmt.Errorf("mapping for uid %d (host id %d) is larger than native integer size (%d)", containerId, id, math.MaxInt)
|
return -1, fmt.Errorf("mapping for uid %d (host id %d) is larger than native integer size (%d)", containerID, id, math.MaxInt)
|
||||||
}
|
}
|
||||||
return int(id), nil
|
return int(id), nil
|
||||||
}
|
}
|
||||||
// Return unchanged id.
|
// Return unchanged id.
|
||||||
return containerId, nil
|
return containerID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostRootUID gets the root uid for the process on host which could be non-zero
|
// HostRootUID gets the root uid for the process on host which could be non-zero
|
||||||
// when user namespaces are enabled.
|
// when user namespaces are enabled.
|
||||||
func (c Config) HostRootUID() (int, error) {
|
func (c *Config) HostRootUID() (int, error) {
|
||||||
return c.HostUID(0)
|
return c.HostUID(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostGID gets the translated gid for the process on host which could be
|
// HostGID gets the translated gid for the process on host which could be
|
||||||
// different when user namespaces are enabled.
|
// different when user namespaces are enabled.
|
||||||
func (c Config) HostGID(containerId int) (int, error) {
|
func (c *Config) HostGID(containerID int) (int, error) {
|
||||||
if c.Namespaces.Contains(NEWUSER) {
|
if c.Namespaces.Contains(NEWUSER) {
|
||||||
if len(c.GIDMappings) == 0 {
|
if len(c.GIDMappings) == 0 {
|
||||||
return -1, errNoGIDMap
|
return -1, errNoGIDMap
|
||||||
}
|
}
|
||||||
id, found := c.hostIDFromMapping(int64(containerId), c.GIDMappings)
|
id, found := c.hostIDFromMapping(int64(containerID), c.GIDMappings)
|
||||||
if !found {
|
if !found {
|
||||||
return -1, fmt.Errorf("user namespaces enabled, but no mapping found for gid %d", containerId)
|
return -1, fmt.Errorf("user namespaces enabled, but no mapping found for gid %d", containerID)
|
||||||
}
|
}
|
||||||
// If we are a 32-bit binary running on a 64-bit system, it's possible
|
// If we are a 32-bit binary running on a 64-bit system, it's possible
|
||||||
// the mapped user is too large to store in an int, which means we
|
// the mapped user is too large to store in an int, which means we
|
||||||
// cannot do the mapping. We can't just return an int64, because
|
// cannot do the mapping. We can't just return an int64, because
|
||||||
// os.Setgid() takes an int.
|
// os.Setgid() takes an int.
|
||||||
if id > math.MaxInt {
|
if id > math.MaxInt {
|
||||||
return -1, fmt.Errorf("mapping for gid %d (host id %d) is larger than native integer size (%d)", containerId, id, math.MaxInt)
|
return -1, fmt.Errorf("mapping for gid %d (host id %d) is larger than native integer size (%d)", containerID, id, math.MaxInt)
|
||||||
}
|
}
|
||||||
return int(id), nil
|
return int(id), nil
|
||||||
}
|
}
|
||||||
// Return unchanged id.
|
// Return unchanged id.
|
||||||
return containerId, nil
|
return containerID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostRootGID gets the root gid for the process on host which could be non-zero
|
// HostRootGID gets the root gid for the process on host which could be non-zero
|
||||||
// when user namespaces are enabled.
|
// when user namespaces are enabled.
|
||||||
func (c Config) HostRootGID() (int, error) {
|
func (c *Config) HostRootGID() (int, error) {
|
||||||
return c.HostGID(0)
|
return c.HostGID(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utility function that gets a host ID for a container ID from user namespace map
|
// Utility function that gets a host ID for a container ID from user namespace map
|
||||||
// if that ID is present in the map.
|
// if that ID is present in the map.
|
||||||
func (c Config) hostIDFromMapping(containerID int64, uMap []IDMap) (int64, bool) {
|
func (c *Config) hostIDFromMapping(containerID int64, uMap []IDMap) (int64, bool) {
|
||||||
for _, m := range uMap {
|
for _, m := range uMap {
|
||||||
if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
|
if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
|
||||||
hostID := m.HostID + (containerID - m.ContainerID)
|
hostID := m.HostID + (containerID - m.ContainerID)
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ type Process struct {
|
|||||||
|
|
||||||
// Wait waits for the process to exit.
|
// Wait waits for the process to exit.
|
||||||
// Wait releases any resources associated with the Process
|
// Wait releases any resources associated with the Process
|
||||||
func (p Process) Wait() (*os.ProcessState, error) {
|
func (p *Process) Wait() (*os.ProcessState, error) {
|
||||||
if p.ops == nil {
|
if p.ops == nil {
|
||||||
return nil, errInvalidProcess
|
return nil, errInvalidProcess
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ func (p Process) Wait() (*os.ProcessState, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pid returns the process ID
|
// Pid returns the process ID
|
||||||
func (p Process) Pid() (int, error) {
|
func (p *Process) Pid() (int, error) {
|
||||||
// math.MinInt32 is returned here, because it's invalid value
|
// math.MinInt32 is returned here, because it's invalid value
|
||||||
// for the kill() system call.
|
// for the kill() system call.
|
||||||
if p.ops == nil {
|
if p.ops == nil {
|
||||||
@@ -145,7 +145,7 @@ func (p Process) Pid() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Signal sends a signal to the Process.
|
// Signal sends a signal to the Process.
|
||||||
func (p Process) Signal(sig os.Signal) error {
|
func (p *Process) Signal(sig os.Signal) error {
|
||||||
if p.ops == nil {
|
if p.ops == nil {
|
||||||
return errInvalidProcess
|
return errInvalidProcess
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user