diff --git a/cmd/wireproxy/main.go b/cmd/wireproxy/main.go index e993e880..d60ec6af 100644 --- a/cmd/wireproxy/main.go +++ b/cmd/wireproxy/main.go @@ -13,6 +13,7 @@ import ( "syscall" "github.com/landlock-lsm/go-landlock/landlock" + llsyscall "github.com/landlock-lsm/go-landlock/landlock/syscall" "github.com/akamensky/argparse" "github.com/windtf/wireproxy" @@ -86,6 +87,13 @@ func tlsFilePaths(routines []wireproxy.RoutineSpawner) []string { return paths } +// files the Go resolver reads when it looks up a name on the host +var resolverFiles = []string{ + "/etc/resolv.conf", + "/etc/hosts", + "/etc/nsswitch.conf", +} + func lock(stage string, roFilles ...string) { switch stage { case "boot": @@ -116,7 +124,6 @@ func lock(stage string, roFilles ...string) { // so we can add the provided dynamic rules rules := []landlock.Rule{ - landlock.ROFiles("/etc/resolv.conf").IgnoreIfMissing(), landlock.ROFiles("/dev/fd").IgnoreIfMissing(), landlock.ROFiles("/dev/zero").IgnoreIfMissing(), landlock.ROFiles("/dev/urandom").IgnoreIfMissing(), @@ -136,6 +143,10 @@ func lock(stage string, roFilles ...string) { landlock.RWFiles("/proc/self/fd").IgnoreIfMissing(), } + for _, file := range resolverFiles { + rules = append(rules, landlock.ROFiles(file).IgnoreIfMissing()) + } + if len(roFilles) > 0 { for _, file := range roFilles { rules = append(rules, landlock.ROFiles(file).IgnoreIfMissing()) @@ -163,16 +174,54 @@ func extractPort(addr string) uint16 { return uint16(port) } -func lockNetwork(sections []wireproxy.RoutineSpawner, infoAddr *string) { - var rules []landlock.Rule +// canDialDirect reports whether wireproxy may open TCP connections from the +// host network namespace: split routing dials non-tunnel hosts directly, and +// without a DNS setting names are resolved by the system resolver. +func canDialDirect(sections []wireproxy.RoutineSpawner, systemDNS bool) bool { + if systemDNS { + return true + } + for _, section := range sections { + switch section := section.(type) { + case *wireproxy.HTTPConfig: + if len(section.TunnelDomains) > 0 { + return true + } + case *wireproxy.Socks5Config: + if len(section.TunnelDomains) > 0 { + return true + } + case *wireproxy.SNIConfig: + if len(section.TunnelDomains) > 0 { + return true + } + } + } + return false +} + +// networkRules chooses the Landlock network rules. When connections may go out +// directly the destinations are not known up front, so only bind is restricted +// (restrictConnect is false). Otherwise connect is restricted too, to the +// TCP server tunnel targets and to port 53 for the resolver's TCP fallback +// (used when the endpoint is re-resolved with the system resolver). +func networkRules(sections []wireproxy.RoutineSpawner, infoAddr *string, systemDNS bool) (rules []landlock.Rule, restrictConnect bool) { + restrictConnect = !canDialDirect(sections, systemDNS) + if infoAddr != nil && *infoAddr != "" { rules = append(rules, landlock.BindTCP(extractPort(*infoAddr))) } + if restrictConnect { + rules = append(rules, landlock.ConnectTCP(53)) + } + for _, section := range sections { switch section := section.(type) { case *wireproxy.TCPServerTunnelConfig: - rules = append(rules, landlock.ConnectTCP(extractPort(section.Target))) + if restrictConnect { + rules = append(rules, landlock.ConnectTCP(extractPort(section.Target))) + } case *wireproxy.HTTPConfig: rules = append(rules, landlock.BindTCP(extractPort(section.BindAddress))) case *wireproxy.TCPClientTunnelConfig: @@ -184,7 +233,18 @@ func lockNetwork(sections []wireproxy.RoutineSpawner, infoAddr *string) { } } - panicIfError(landlock.V4.BestEffort().RestrictNet(rules...)) + return rules, restrictConnect +} + +func lockNetwork(sections []wireproxy.RoutineSpawner, infoAddr *string, systemDNS bool) { + rules, restrictConnect := networkRules(sections, infoAddr, systemDNS) + + config := landlock.V4 + if !restrictConnect { + config = landlock.MustConfig(landlock.AccessNetSet(llsyscall.AccessNetBindTCP)) + } + + panicIfError(config.BestEffort().RestrictNet(rules...)) } func main() { @@ -250,7 +310,7 @@ func main() { return } - lockNetwork(conf.Routines, info) + lockNetwork(conf.Routines, info, len(conf.Device.DNS) == 0) if isDaemonProcess { os.Stdout, _ = os.Open(os.DevNull) diff --git a/cmd/wireproxy/main_test.go b/cmd/wireproxy/main_test.go new file mode 100644 index 00000000..59e19fb0 --- /dev/null +++ b/cmd/wireproxy/main_test.go @@ -0,0 +1,125 @@ +package main + +import ( + "fmt" + "net" + "regexp" + "testing" + + "github.com/landlock-lsm/go-landlock/landlock" + "github.com/windtf/wireproxy" +) + +func ruleStrings(rules []landlock.Rule) map[string]bool { + out := map[string]bool{} + for _, r := range rules { + out[fmt.Sprint(r)] = true + } + return out +} + +func TestNetworkRules(t *testing.T) { + domains := []*regexp.Regexp{regexp.MustCompile(`^example\.com$`)} + info := "127.0.0.1:9000" + + tests := []struct { + name string + sections []wireproxy.RoutineSpawner + systemDNS bool + restrictConnect bool + want []string + notWant []string + }{ + { + name: "tunnel only with DNS restricts connect", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.HTTPConfig{BindAddress: "127.0.0.1:8080"}, + &wireproxy.TCPServerTunnelConfig{ListenPort: 1, Target: "10.0.0.1:5432"}, + &wireproxy.TCPClientTunnelConfig{BindAddress: &net.TCPAddr{Port: 2222}}, + }, + restrictConnect: true, + want: []string{ + "ALLOW {bind_tcp} on TCP port 9000", + "ALLOW {bind_tcp} on TCP port 8080", + "ALLOW {bind_tcp} on TCP port 2222", + "ALLOW {connect_tcp} on TCP port 5432", + "ALLOW {connect_tcp} on TCP port 53", + }, + }, + { + name: "split routing HTTP restricts bind only", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.HTTPConfig{BindAddress: "127.0.0.1:8080", TunnelDomains: domains}, + &wireproxy.TCPServerTunnelConfig{ListenPort: 1, Target: "10.0.0.1:5432"}, + }, + want: []string{"ALLOW {bind_tcp} on TCP port 8080", "ALLOW {bind_tcp} on TCP port 9000"}, + notWant: []string{"ALLOW {connect_tcp} on TCP port 5432", "ALLOW {connect_tcp} on TCP port 53"}, + }, + { + name: "split routing SOCKS5 restricts bind only", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.Socks5Config{BindAddress: "127.0.0.1:1080", TunnelDomains: domains}, + }, + want: []string{"ALLOW {bind_tcp} on TCP port 1080"}, + notWant: []string{"ALLOW {connect_tcp} on TCP port 53"}, + }, + { + name: "split routing SNI restricts bind only", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.SNIConfig{BindAddress: "127.0.0.1:8443", TunnelDomains: domains}, + }, + want: []string{"ALLOW {bind_tcp} on TCP port 8443"}, + notWant: []string{"ALLOW {connect_tcp} on TCP port 53"}, + }, + { + name: "system DNS restricts bind only", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.Socks5Config{BindAddress: "127.0.0.1:1080"}, + &wireproxy.TCPServerTunnelConfig{ListenPort: 1, Target: "10.0.0.1:5432"}, + }, + systemDNS: true, + want: []string{"ALLOW {bind_tcp} on TCP port 1080"}, + notWant: []string{"ALLOW {connect_tcp} on TCP port 5432", "ALLOW {connect_tcp} on TCP port 53"}, + }, + { + name: "logging without domains still tunnels everything", + sections: []wireproxy.RoutineSpawner{ + &wireproxy.HTTPConfig{BindAddress: "127.0.0.1:8080", LogDomains: true}, + }, + restrictConnect: true, + want: []string{"ALLOW {connect_tcp} on TCP port 53"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rules, restrictConnect := networkRules(tt.sections, &info, tt.systemDNS) + if restrictConnect != tt.restrictConnect { + t.Fatalf("restrictConnect = %v, want %v", restrictConnect, tt.restrictConnect) + } + got := ruleStrings(rules) + for _, w := range tt.want { + if !got[w] { + t.Errorf("missing rule %q in %v", w, got) + } + } + for _, w := range tt.notWant { + if got[w] { + t.Errorf("unexpected rule %q", w) + } + } + }) + } +} + +func TestResolverFiles(t *testing.T) { + have := map[string]bool{} + for _, f := range resolverFiles { + have[f] = true + } + for _, f := range []string{"/etc/resolv.conf", "/etc/hosts", "/etc/nsswitch.conf"} { + if !have[f] { + t.Errorf("%s is not readable after lockdown", f) + } + } +}