Skip to content

Commit

Permalink
dap: add sameuser check (go-delve#2494)
Browse files Browse the repository at this point in the history
On linux, delve RPC server allows only connections from the same user
if --only-same-user is set (true, by default). Do the same for DAP
server.

Moved the sameuser check logic to service/internal/sameuser.
Considered importing service/rpccommon from the dap server,
but when we eventually migrate to multiplex rpc and dap from one
port, I am afraid that can cause cyclic imports.
  • Loading branch information
hyangah authored May 19, 2021
1 parent 5dd4b7d commit 669fc2d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/dlv/cmds/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func dapCmd(cmd *cobra.Command, args []string) {
CheckGoVersion: checkGoVersion,
TTY: tty,
},
CheckLocalConnUser: checkLocalConnUser,
})
defer server.Stop()

Expand Down
8 changes: 8 additions & 0 deletions service/dap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
"github.com/go-delve/delve/service/debugger"
"github.com/go-delve/delve/service/internal/sameuser"
"github.com/google/go-dap"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -320,6 +321,13 @@ func (s *Server) Run() {
}
return
}
if s.config.CheckLocalConnUser {
if !sameuser.CanAccept(s.listener.Addr(), conn.RemoteAddr()) {
s.log.Error("Error accepting client connection: Only connections from the same user that started this instance of Delve are allowed to connect. See --only-same-user.")
s.triggerServerStop()
return
}
}
s.mu.Lock()
s.conn = conn // closed in Stop()
s.mu.Unlock()
Expand Down
3 changes: 3 additions & 0 deletions service/internal/sameuser/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package sameuser provides utilities for checking users of a local connection.
// Only works in Linux.
package sameuser
9 changes: 9 additions & 0 deletions service/internal/sameuser/sameuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//+build !linux

package sameuser

import "net"

func CanAccept(_, _ net.Addr) bool {
return true
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//+build linux

package rpccommon
package sameuser

import (
"bytes"
Expand Down Expand Up @@ -96,7 +96,7 @@ func sameUserForRemoteAddr(remoteAddr *net.TCPAddr) (bool, error) {
return sameUserForRemoteAddr4(remoteAddr)
}

func canAccept(listenAddr, remoteAddr net.Addr) bool {
func CanAccept(listenAddr, remoteAddr net.Addr) bool {
laddr, ok := listenAddr.(*net.TCPAddr)
if !ok || !laddr.IP.IsLoopback() {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//+build linux

package rpccommon
package sameuser

import (
"net"
Expand Down
9 changes: 0 additions & 9 deletions service/rpccommon/sameuser.go

This file was deleted.

3 changes: 2 additions & 1 deletion service/rpccommon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
"github.com/go-delve/delve/service/debugger"
"github.com/go-delve/delve/service/internal/sameuser"
"github.com/go-delve/delve/service/rpc1"
"github.com/go-delve/delve/service/rpc2"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (s *ServerImpl) Run() error {
}

if s.config.CheckLocalConnUser {
if !canAccept(s.listener.Addr(), c.RemoteAddr()) {
if !sameuser.CanAccept(s.listener.Addr(), c.RemoteAddr()) {
c.Close()
continue
}
Expand Down

0 comments on commit 669fc2d

Please sign in to comment.