From 08099ff669ac919802610f3ce6d5230a5bce6422 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Thu, 2 Jun 2022 10:20:00 -0700 Subject: [PATCH] privval: restrict listeners to TCP and Unix domain sockets (#8670) Front load the protocol type check so we do not wind up creating listeners of types that are not usable for this interface (for example, UDP). Fixes #8647. --- privval/utils.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/privval/utils.go b/privval/utils.go index 1d6681b45..a2cbbf501 100644 --- a/privval/utils.go +++ b/privval/utils.go @@ -27,13 +27,17 @@ func IsConnTimeout(err error) bool { // NewSignerListener creates a new SignerListenerEndpoint using the corresponding listen address func NewSignerListener(listenAddr string, logger log.Logger) (*SignerListenerEndpoint, error) { - var listener net.Listener - protocol, address := tmnet.ProtocolAndAddress(listenAddr) + if protocol != "unix" && protocol != "tcp" { //nolint:goconst + return nil, fmt.Errorf("unsupported address family %q, want unix or tcp", protocol) + } + ln, err := net.Listen(protocol, address) if err != nil { return nil, err } + + var listener net.Listener switch protocol { case "unix": listener = NewUnixListener(ln) @@ -41,13 +45,8 @@ func NewSignerListener(listenAddr string, logger log.Logger) (*SignerListenerEnd // TODO: persist this key so external signer can actually authenticate us listener = NewTCPListener(ln, ed25519.GenPrivKey()) default: - return nil, fmt.Errorf( - "wrong listen address: expected either 'tcp' or 'unix' protocols, got %s", - protocol, - ) + panic("invalid protocol: " + protocol) // semantically unreachable } - pve := NewSignerListenerEndpoint(logger.With("module", "privval"), listener) - - return pve, nil + return NewSignerListenerEndpoint(logger.With("module", "privval"), listener), nil }