-
Notifications
You must be signed in to change notification settings - Fork 1.4k
runsc: Make identity user mapping work for filesystem #11875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package sandbox | |
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
@@ -1029,6 +1030,13 @@ func (s *Sandbox) createSandboxProcess(conf *config.Config, args *Args, startSyn | |
// configured. | ||
rootlessEUID := unix.Geteuid() != 0 | ||
setUserMappings := false | ||
var gSyncFile *os.File | ||
defer func() { | ||
if gSyncFile != nil { | ||
gSyncFile.Close() | ||
} | ||
}() | ||
|
||
if conf.Network == config.NetworkHost || conf.DirectFS { | ||
if userns, ok := specutils.GetNS(specs.UserNamespace, args.Spec); ok { | ||
log.Infof("Sandbox will be started in container's user namespace: %+v", userns) | ||
|
@@ -1038,7 +1046,7 @@ func (s *Sandbox) createSandboxProcess(conf *config.Config, args *Args, startSyn | |
if err != nil { | ||
return err | ||
} | ||
defer syncFile.Close() | ||
gSyncFile = syncFile | ||
setUserMappings = true | ||
} else { | ||
specutils.SetUIDGIDMappings(cmd, args.Spec) | ||
|
@@ -1283,6 +1291,9 @@ func (s *Sandbox) createSandboxProcess(conf *config.Config, args *Args, startSyn | |
if err := SetUserMappings(args.Spec, cmd.Process.Pid); err != nil { | ||
return err | ||
} | ||
if err := SendIDToSandbox(gSyncFile, args.Spec); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
s.child = true | ||
|
@@ -1292,6 +1303,36 @@ func (s *Sandbox) createSandboxProcess(conf *config.Config, args *Args, startSyn | |
return nil | ||
} | ||
|
||
func SendIDToSandbox(syncFile *os.File, spec *specs.Spec) error { | ||
euid := uint32(os.Geteuid()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand this right, the sandbox will be running under the current user. Strictly speaking, the current user might be unmapped in the container user namespace. I think sandbox/gofer processes should ideally run under the root user if that user is mapped; otherwise, they need to run under the user associated with the init process. |
||
egid := uint32(os.Getegid()) | ||
|
||
var cuid uint32 | ||
var cgid uint32 | ||
|
||
for _, idMap := range spec.Linux.UIDMappings { | ||
if euid >= idMap.HostID && euid < idMap.Size + idMap.HostID { | ||
cuid = euid - idMap.HostID + idMap.ContainerID | ||
break | ||
} | ||
} | ||
|
||
for _, idMap := range spec.Linux.GIDMappings { | ||
if egid >= idMap.HostID && euid < idMap.Size + idMap.HostID { | ||
cgid = egid - idMap.HostID + idMap.ContainerID | ||
break | ||
} | ||
} | ||
buf := make([]byte, 8) | ||
binary.BigEndian.PutUint32(buf[0:4], cuid) | ||
binary.BigEndian.PutUint32(buf[4:8], cgid) | ||
if _, err := syncFile.Write(buf); err != nil { | ||
return fmt.Errorf("write uid&gid to sandbox error: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Wait waits for the containerized process to exit, and returns its WaitStatus. | ||
func (s *Sandbox) Wait(cid string) (unix.WaitStatus, error) { | ||
log.Debugf("Waiting for container %q in sandbox %q", cid, s.ID) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls, write a comment to explain what this function is doing.