Skip to content

Commit

Permalink
Adjust SetupUser to Fchown all open file descriptors so they're appro…
Browse files Browse the repository at this point in the history
…priately usable by the forked process
  • Loading branch information
tianon committed Sep 25, 2015
1 parent fb71463 commit 75150ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"io/ioutil"
"strconv"
"syscall"
)

func chownFds(uid, gid int) error {
fdList, err := ioutil.ReadDir("/proc/self/fd")
if err != nil {
return err
}
for _, fi := range fdList {
fd, err := strconv.Atoi(fi.Name())
if err != nil {
// ignore non-numeric file names
continue
}

if err = syscall.Fchown(fd, uid, gid); err != nil {
// "bad file descriptor" probably just means it no longer exists since we did "readdir", so ignore that
if err != syscall.EBADF {
return err
}
}
}
return nil
}
3 changes: 3 additions & 0 deletions setup-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func SetupUser(u string) error {
if err != nil {
return fmt.Errorf("get supplementary groups %s", err)
}
if err := chownFds(execUser.Uid, execUser.Gid); err != nil {
return fmt.Errorf("fchown fds %s", err)
}
if err := syscall.Setgroups(execUser.Sgids); err != nil {
return fmt.Errorf("setgroups %s", err)
}
Expand Down

0 comments on commit 75150ac

Please sign in to comment.