Skip to content

Commit d1c9cd9

Browse files
committed
libct: fix stdio permission error for userns container
We should let stdio could be accessed in user ns container. Please see opencontainers#4475 Because the default permission of stdio is 0o700, other user can't access them. If we don't change the permission to 0o666, We'll get an error msg if we access stdio in a userns contaienr: ***: /dev/std***: Permission denied. Signed-off-by: lifubang <[email protected]>
1 parent 4ad9f7f commit d1c9cd9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

libcontainer/container_linux.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ func (c *Container) start(process *Process) (retErr error) {
346346
if err := utils.CloseExecFrom(3); err != nil {
347347
return fmt.Errorf("unable to mark non-stdio fds as cloexec: %w", err)
348348
}
349+
350+
// We should let stdio could be accessed in user ns container.
351+
// Please see https://github.com/opencontainers/runc/issues/4475
352+
// Because the default permission of stdio is 0o700, other user can't access
353+
// them. If we don't change the permission to 0o666, We'll get an error msg if
354+
// we access stdio in a userns contaienr: ***: /dev/std***: Permission denied.
355+
if err := c.fixStdioPermission(); err != nil {
356+
return fmt.Errorf("unable to change permission of stdio: %w", err)
357+
}
349358
if err := parent.start(); err != nil {
350359
return fmt.Errorf("unable to start container process: %w", err)
351360
}
@@ -506,6 +515,32 @@ func isDmzBinarySafe(c *configs.Config) bool {
506515
return false
507516
}
508517

518+
func (c *Container) fixStdioPermission() error {
519+
rootuid, err := c.Config().HostRootUID()
520+
if err != nil {
521+
return err
522+
}
523+
rootgid, err := c.Config().HostRootGID()
524+
if err != nil {
525+
return err
526+
}
527+
uid := os.Getuid()
528+
gid := os.Getgid()
529+
530+
if uid != rootuid && gid != rootgid {
531+
if err := os.Stdin.Chmod(0o666); err != nil {
532+
return err
533+
}
534+
if err := os.Stdout.Chmod(0o666); err != nil {
535+
return err
536+
}
537+
if err := os.Stderr.Chmod(0o666); err != nil {
538+
return err
539+
}
540+
}
541+
return nil
542+
}
543+
509544
func (c *Container) newParentProcess(p *Process) (parentProcess, error) {
510545
comm, err := newProcessComm()
511546
if err != nil {

0 commit comments

Comments
 (0)