Do we need to duplicate descriptors twice?

This commit is contained in:
Frank Denis 2020-06-19 21:42:20 +02:00
parent 539924f85f
commit 55ce158e37
1 changed files with 1 additions and 16 deletions

View File

@ -66,32 +66,17 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) {
if err := unix.Setuid(uid); err != nil {
dlog.Fatalf("Unable to drop user privileges: %s", err)
}
maxfd := uintptr(0)
for _, fd := range fds {
if fd.Fd() > maxfd {
maxfd = fd.Fd()
}
}
fdbase := maxfd + 1
for i, fd := range fds {
if fd.Fd() >= InheritedDescriptorsBase {
dlog.Fatal("Duplicated file descriptors are above base")
}
if err := unix.Dup2(int(fd.Fd()), int(fdbase+uintptr(i))); err != nil {
if err := unix.Dup2(int(fd.Fd()), int(InheritedDescriptorsBase+uintptr(i))); err != nil {
dlog.Fatalf("Unable to clone file descriptor: [%s]", err)
}
if _, err := unix.FcntlInt(fd.Fd(), unix.F_SETFD, unix.FD_CLOEXEC); err != nil {
dlog.Fatalf("Unable to set the close on exec flag: [%s]", err)
}
}
if int(fdbase)+len(fds) >= int(InheritedDescriptorsBase) {
dlog.Fatal("Renumbered file descriptors are above base")
}
for i := range fds {
if err := unix.Dup2(int(fdbase)+i, int(InheritedDescriptorsBase)+i); err != nil {
dlog.Fatalf("Unable to reassign descriptor: [%s]", err)
}
}
err = unix.Exec(path, args, os.Environ())
dlog.Fatalf("Unable to reexecute [%s]: [%s]", path, err)
os.Exit(1)