Skip to content

Commit af3b7d6

Browse files
committed
Make sure err isn't nil when returning failure
Signed-off-by: Jan Dubois <[email protected]>
1 parent 474eec4 commit af3b7d6

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ jobs:
443443
strategy:
444444
fail-fast: false
445445
matrix:
446-
template:
447-
- default.yaml
448-
- fedora.yaml
446+
include:
447+
- {template: default.yaml, ssh-port-forwarder: true}
448+
- {template: fedora.yaml, ssh-port-forwarder: false}
449449
steps:
450450
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
451451
with:
@@ -467,6 +467,8 @@ jobs:
467467
run: brew uninstall --ignore-dependencies --force qemu
468468
- name: Test
469469
run: ./hack/test-templates.sh templates/${{ matrix.template }}
470+
env:
471+
LIMA_SSH_PORT_FORWARDER: ${{ matrix.ssh-port-forwarder }}
470472
- if: failure()
471473
uses: ./.github/actions/upload_failure_logs_if_exists
472474
with:

hack/test-templates.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,17 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
307307
fi
308308
limactl shell "$NAME" $sudo $CONTAINER_ENGINE info
309309
limactl shell "$NAME" $sudo $CONTAINER_ENGINE pull --quiet ${nginx_image}
310-
limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 8888:80 ${nginx_image}
311310

311+
limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 8888:80 ${nginx_image}
312312
timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://${hostip}:8888; do sleep 3; done"
313+
limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f nginx
314+
315+
if [ "$(uname)" = "Darwin" ]; then
316+
# Only macOS can bind to port 80 without root
317+
limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 127.0.0.1:80:80 ${nginx_image}
318+
timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://localhost:80; do sleep 3; done"
319+
limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f nginx
320+
fi
313321
fi
314322
fi
315323
set +x

pkg/hostagent/port_darwin.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/lima-vm/lima/pkg/bicopy"
13+
"github.com/lima-vm/lima/pkg/portfwd"
1314
"github.com/lima-vm/sshocker/pkg/ssh"
1415
"github.com/sirupsen/logrus"
1516
)
@@ -96,11 +97,12 @@ func newPseudoLoopbackForwarder(localPort int, unixSock string) (*pseudoLoopback
9697
return nil, err
9798
}
9899

99-
lnAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("0.0.0.0:%d", localPort))
100+
// use "tcp" network to listen on both "tcp4" and "tcp6"
101+
lnAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("0.0.0.0:%d", localPort))
100102
if err != nil {
101103
return nil, err
102104
}
103-
ln, err := net.ListenTCP("tcp4", lnAddr)
105+
ln, err := net.ListenTCP("tcp", lnAddr)
104106
if err != nil {
105107
return nil, err
106108
}
@@ -127,7 +129,7 @@ func (plf *pseudoLoopbackForwarder) Serve() error {
127129
ac.Close()
128130
continue
129131
}
130-
if remoteAddrIP != "127.0.0.1" {
132+
if !portfwd.IsLoopback(remoteAddrIP) {
131133
logrus.WithError(err).Debugf("pseudoloopback forwarder: rejecting non-loopback remoteAddr %q", remoteAddr)
132134
ac.Close()
133135
continue

pkg/portfwd/listener.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"strings"
78
"sync"
89

910
guestagentclient "github.com/lima-vm/lima/pkg/guestagent/api/client"
@@ -42,6 +43,7 @@ func (p *ClosableListeners) Forward(ctx context.Context, client *guestagentclien
4243
}
4344

4445
func (p *ClosableListeners) Remove(_ context.Context, protocol, hostAddress, guestAddress string) {
46+
logrus.Debugf("removing listener for hostAddress: %s, guestAddress: %s", hostAddress, guestAddress)
4547
key := key(protocol, hostAddress, guestAddress)
4648
switch protocol {
4749
case "tcp", "tcp6":
@@ -65,7 +67,6 @@ func (p *ClosableListeners) Remove(_ context.Context, protocol, hostAddress, gue
6567

6668
func (p *ClosableListeners) forwardTCP(ctx context.Context, client *guestagentclient.GuestAgentClient, hostAddress, guestAddress string) {
6769
key := key("tcp", hostAddress, guestAddress)
68-
defer p.Remove(ctx, "tcp", hostAddress, guestAddress)
6970

7071
p.listenersRW.Lock()
7172
_, ok := p.listeners[key]
@@ -75,16 +76,21 @@ func (p *ClosableListeners) forwardTCP(ctx context.Context, client *guestagentcl
7576
}
7677
tcpLis, err := Listen(ctx, p.listenConfig, hostAddress)
7778
if err != nil {
78-
logrus.Errorf("failed to accept TCP connection: %v", err)
79+
logrus.Errorf("failed to listen to TCP connection: %v", err)
7980
p.listenersRW.Unlock()
8081
return
8182
}
83+
defer p.Remove(ctx, "tcp", hostAddress, guestAddress)
8284
p.listeners[key] = tcpLis
8385
p.listenersRW.Unlock()
8486
for {
8587
conn, err := tcpLis.Accept()
8688
if err != nil {
8789
logrus.Errorf("failed to accept TCP connection: %v", err)
90+
if strings.Contains(err.Error(), "pseudoloopback") {
91+
// don't stop forwarding because the forwarder has rejected a non-local address
92+
continue
93+
}
8894
return
8995
}
9096
go HandleTCPConnection(ctx, client, conn, guestAddress)

pkg/portfwd/listener_darwin.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ func (p pseudoLoopbackListener) Accept() (net.Conn, error) {
6868
conn.Close()
6969
return nil, err
7070
}
71-
if remoteAddrIP != "127.0.0.1" {
72-
logrus.WithError(err).Debugf("pseudoloopback forwarder: rejecting non-loopback remoteAddr %q", remoteAddr)
71+
if !IsLoopback(remoteAddrIP) {
72+
err := fmt.Errorf("pseudoloopback forwarder: rejecting non-loopback remoteAddr %q", remoteAddr)
73+
logrus.Debug(err)
74+
conn.Close()
7375
return nil, err
7476
}
77+
logrus.Infof("pseudoloopback forwarder: accepting connection from %q", remoteAddr)
7578
return conn, nil
7679
}
7780

@@ -89,7 +92,7 @@ func (pk *pseudoLoopbackPacketConn) ReadFrom(bytes []byte) (n int, addr net.Addr
8992
if err != nil {
9093
return 0, nil, err
9194
}
92-
if remoteAddrIP != "127.0.0.1" {
95+
if !IsLoopback(remoteAddrIP) {
9396
return 0, nil, fmt.Errorf("pseudoloopback forwarder: rejecting non-loopback remoteAddr %q", remoteAddr)
9497
}
9598
return n, remoteAddr, nil
@@ -100,8 +103,12 @@ func (pk *pseudoLoopbackPacketConn) WriteTo(bytes []byte, remoteAddr net.Addr) (
100103
if err != nil {
101104
return 0, err
102105
}
103-
if remoteAddrIP != "127.0.0.1" {
106+
if !IsLoopback(remoteAddrIP) {
104107
return 0, fmt.Errorf("pseudoloopback forwarder: rejecting non-loopback remoteAddr %q", remoteAddr)
105108
}
106109
return pk.PacketConn.WriteTo(bytes, remoteAddr)
107110
}
111+
112+
func IsLoopback(addr string) bool {
113+
return addr == "127.0.0.1" || addr == "::1"
114+
}

0 commit comments

Comments
 (0)