When forking, relocate descriptors higher up

Channels used by the `services` module may use descriptors, so we don't
want to overwrite them.

Maybe
fixes #1371
This commit is contained in:
Frank Denis 2020-06-18 23:51:50 +02:00
parent 6dc484c177
commit 6235c11c77
5 changed files with 17 additions and 15 deletions

View File

@ -40,7 +40,11 @@ var (
var (
FileDescriptors = make([]*os.File, 0)
FileDescriptorNum = 0
FileDescriptorNum = uintptr(0)
)
const (
InheritedDescriptorsBase = uintptr(50)
)
func PrefixWithSize(packet []byte) ([]byte, error) {

View File

@ -1,8 +0,0 @@
package main
import (
"os"
)
func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) {
}

View File

@ -81,7 +81,7 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) {
}
}
for i := range fds {
if err := unix.Dup2(int(fdbase+uintptr(i)), int(i)+3); err != nil {
if err := unix.Dup2(int(fdbase)+i, int(InheritedDescriptorsBase)+i); err != nil {
dlog.Fatalf("Unable to reassign descriptor: [%s]", err)
}
}

View File

@ -1,4 +1,4 @@
// +build !windows,!linux,!darwin
// +build !windows,!linux
package main
@ -74,6 +74,9 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) {
}
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 {
dlog.Fatalf("Unable to clone file descriptor: [%s]", err)
}
@ -81,8 +84,11 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) {
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+uintptr(i)), int(i)+3); err != nil {
if err := unix.Dup2(int(fdbase)+i, int(InheritedDescriptorsBase)+i); err != nil {
dlog.Fatalf("Unable to reassign descriptor: [%s]", err)
}
}

View File

@ -148,13 +148,13 @@ func (proxy *Proxy) addDNSListener(listenAddrStr string) {
}
// child
listenerUDP, err := net.FilePacketConn(os.NewFile(uintptr(3+FileDescriptorNum), "listenerUDP"))
listenerUDP, err := net.FilePacketConn(os.NewFile(InheritedDescriptorsBase+FileDescriptorNum, "listenerUDP"))
if err != nil {
dlog.Fatalf("Unable to switch to a different user: %v", err)
}
FileDescriptorNum++
listenerTCP, err := net.FileListener(os.NewFile(uintptr(3+FileDescriptorNum), "listenerTCP"))
listenerTCP, err := net.FileListener(os.NewFile(InheritedDescriptorsBase+FileDescriptorNum, "listenerTCP"))
if err != nil {
dlog.Fatalf("Unable to switch to a different user: %v", err)
}
@ -199,7 +199,7 @@ func (proxy *Proxy) addLocalDoHListener(listenAddrStr string) {
// child
listenerTCP, err := net.FileListener(os.NewFile(uintptr(3+FileDescriptorNum), "listenerTCP"))
listenerTCP, err := net.FileListener(os.NewFile(InheritedDescriptorsBase+FileDescriptorNum, "listenerTCP"))
if err != nil {
dlog.Fatalf("Unable to switch to a different user: %v", err)
}