lint fix: fix linter errors after migrating to v2

Signed-off-by: Gunjan Vyas <vyasgun20@gmail.com>
This commit is contained in:
Gunjan Vyas
2025-05-10 14:27:35 +05:30
committed by Christophe Fergeau
parent 6fa6ae02a7
commit 31193c50af
16 changed files with 74 additions and 33 deletions

View File

@@ -17,7 +17,7 @@ type client struct {
func newClient(conn net.Conn, user string, key string) (*client, error) { func newClient(conn net.Conn, user string, key string) (*client, error) {
config, err := newConfig(user, key) config, err := newConfig(user, key)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error getting config for native Go SSH: %s", err) return nil, fmt.Errorf("error getting config for native Go SSH: %s", err)
} }
return &client{ return &client{

View File

@@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"math"
"net" "net"
"net/http" "net/http"
"os" "os"
@@ -175,6 +176,10 @@ func rx(conn net.Conn, tap *water.Interface, errCh chan error, mtu int) {
log.Info(packet.String()) log.Info(packet.String())
} }
if n < 0 || n > math.MaxUint16 {
log.Errorf("invalid frame length")
return
}
binary.LittleEndian.PutUint16(size, uint16(n)) binary.LittleEndian.PutUint16(size, uint16(n))
if _, err := conn.Write(append(size, frame...)); err != nil { if _, err := conn.Write(append(size, frame...)); err != nil {
errCh <- errors.Wrap(err, "cannot write size and packet to socket") errCh <- errors.Wrap(err, "cannot write size and packet to socket")

View File

@@ -3,6 +3,7 @@ package dhcp
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"math"
"net" "net"
"net/http" "net/http"
"time" "time"
@@ -50,7 +51,13 @@ func handler(configuration *types.Configuration, ipPool *tap.IPPool) server4.Han
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionSubnetMask, Value: dhcpv4.IP(parsedSubnet.Mask)}) reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionSubnetMask, Value: dhcpv4.IP(parsedSubnet.Mask)})
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionRouter, Value: dhcpv4.IP(net.ParseIP(configuration.GatewayIP))}) reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionRouter, Value: dhcpv4.IP(net.ParseIP(configuration.GatewayIP))})
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDomainNameServer, Value: dhcpv4.IPs([]net.IP{net.ParseIP(configuration.GatewayIP)})}) reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDomainNameServer, Value: dhcpv4.IPs([]net.IP{net.ParseIP(configuration.GatewayIP)})})
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionInterfaceMTU, Value: dhcpv4.Uint16(configuration.MTU)})
mtu := configuration.MTU
if mtu < 0 || mtu > math.MaxUint16 {
log.Errorf("dhcp: invalid MTU %d", mtu)
} else {
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionInterfaceMTU, Value: dhcpv4.Uint16(mtu)})
}
reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDNSDomainSearchList, Value: &rfc1035label.Labels{ reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDNSDomainSearchList, Value: &rfc1035label.Labels{
Labels: configuration.DNSSearchDomains, Labels: configuration.DNSSearchDomains,
}}) }})
@@ -71,7 +78,7 @@ func handler(configuration *types.Configuration, ipPool *tap.IPPool) server4.Han
} }
} }
func dial(s *stack.Stack, nic int) (*gonet.UDPConn, error) { func dial(s *stack.Stack, nic tcpip.NICID) (*gonet.UDPConn, error) {
var wq waiter.Queue var wq waiter.Queue
ep, err := s.NewEndpoint(udp.ProtocolNumber, ipv4.ProtocolNumber, &wq) ep, err := s.NewEndpoint(udp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
if err != nil { if err != nil {
@@ -98,7 +105,7 @@ type Server struct {
} }
func New(configuration *types.Configuration, stack *stack.Stack, ipPool *tap.IPPool) (*Server, error) { func New(configuration *types.Configuration, stack *stack.Stack, ipPool *tap.IPPool) (*Server, error) {
ln, err := dial(stack, 1) ln, err := dial(stack, tcpip.NICID(1))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -378,7 +378,7 @@ func tcpipAddress(nicID tcpip.NICID, remote string) (address tcpip.FullAddress,
return address, errors.New("invalid remote addr") return address, errors.New("invalid remote addr")
} }
port, err := strconv.Atoi(split[1]) port, err := strconv.ParseUint(split[1], 10, 16)
if err != nil { if err != nil {
return address, err return address, err

View File

@@ -92,7 +92,7 @@ func connectForward(ctx context.Context, bastion *Bastion) (CloseWriteConn, erro
return nil, errors.Wrapf(err, "Couldn't reestablish ssh tunnel on path: %s", bastion.Path) return nil, errors.Wrapf(err, "Couldn't reestablish ssh tunnel on path: %s", bastion.Path)
} }
// Check if ssh connection is still alive // Check if ssh connection is still alive
_, _, err = bastion.Client.Conn.SendRequest("alive@gvproxy", true, nil) _, _, err = bastion.Client.SendRequest("alive@gvproxy", true, nil)
if err != nil { if err != nil {
for bastionRetries := 1; ; bastionRetries++ { for bastionRetries := 1; ; bastionRetries++ {
err = bastion.Reconnect(ctx) err = bastion.Reconnect(ctx)

View File

@@ -2,6 +2,7 @@ package tap
import ( import (
"errors" "errors"
"math"
"net" "net"
"sync" "sync"
@@ -48,9 +49,11 @@ func (p *IPPool) GetOrAssign(mac string) (net.IP, error) {
} }
} }
var i uint64 if p.count > math.MaxInt {
for i = 1; i < p.count; i++ { return nil, errors.New("IP pool exceeds maximum number of IP addresses")
candidate, err := cidr.Host(p.base, int(i)) }
for i := 1; i < int(p.count); i++ {
candidate, err := cidr.Host(p.base, i)
if err != nil { if err != nil {
continue continue
} }

View File

@@ -13,7 +13,7 @@ import (
type LinkEndpoint struct { type LinkEndpoint struct {
debug bool debug bool
mtu int mtu uint32
mac tcpip.LinkAddress mac tcpip.LinkAddress
ip string ip string
virtualIPs map[string]struct{} virtualIPs map[string]struct{}
@@ -22,7 +22,7 @@ type LinkEndpoint struct {
networkSwitch NetworkSwitch networkSwitch NetworkSwitch
} }
func NewLinkEndpoint(debug bool, mtu int, macAddress string, ip string, virtualIPs []string) (*LinkEndpoint, error) { func NewLinkEndpoint(debug bool, mtu uint32, macAddress string, ip string, virtualIPs []string) (*LinkEndpoint, error) {
linkAddr, err := net.ParseMAC(macAddress) linkAddr, err := net.ParseMAC(macAddress)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -82,11 +82,11 @@ func (e *LinkEndpoint) MaxHeaderLength() uint16 {
} }
func (e *LinkEndpoint) MTU() uint32 { func (e *LinkEndpoint) MTU() uint32 {
return uint32(e.mtu) return e.mtu
} }
func (e *LinkEndpoint) SetMTU(mtu uint32) { func (e *LinkEndpoint) SetMTU(mtu uint32) {
e.mtu = int(mtu) e.mtu = mtu
} }
func (e *LinkEndpoint) Wait() {} func (e *LinkEndpoint) Wait() {}

View File

@@ -2,6 +2,9 @@ package tap
import ( import (
"encoding/binary" "encoding/binary"
"math"
log "github.com/sirupsen/logrus"
) )
type protocol interface { type protocol interface {
@@ -27,7 +30,11 @@ func (s *hyperkitProtocol) Buf() []byte {
} }
func (s *hyperkitProtocol) Write(buf []byte, size int) { func (s *hyperkitProtocol) Write(buf []byte, size int) {
binary.LittleEndian.PutUint16(buf, uint16(size)) if size < 0 || size > math.MaxUint16 {
log.Warnf("size out of range. Resetting to %d", math.MaxUint16)
size = math.MaxUint16
}
binary.LittleEndian.PutUint16(buf, uint16(size)) //#nosec: G115
} }
func (s *hyperkitProtocol) Read(buf []byte) int { func (s *hyperkitProtocol) Read(buf []byte) int {
@@ -46,7 +53,11 @@ func (s *qemuProtocol) Buf() []byte {
} }
func (s *qemuProtocol) Write(buf []byte, size int) { func (s *qemuProtocol) Write(buf []byte, size int) {
binary.BigEndian.PutUint32(buf, uint32(size)) if size > math.MaxUint32 {
log.Warnf("size exceeds max limit. Resetting to: %d", math.MaxInt32)
size = math.MaxUint32
}
binary.BigEndian.PutUint32(buf, uint32(size)) //#nosec: G115. Safely checked
} }
func (s *qemuProtocol) Read(buf []byte) int { func (s *qemuProtocol) Read(buf []byte) int {

View File

@@ -3,6 +3,7 @@ package tap
import ( import (
"bufio" "bufio"
"context" "context"
"fmt"
"io" "io"
"net" "net"
"sync" "sync"
@@ -127,6 +128,10 @@ func (e *Switch) txPkt(pkt *stack.PacketBuffer) error {
dst := eth.DestinationAddress() dst := eth.DestinationAddress()
src := eth.SourceAddress() src := eth.SourceAddress()
size := pkt.Size()
if size < 0 {
return fmt.Errorf("packet size out of range")
}
if dst == header.EthernetBroadcastAddress { if dst == header.EthernetBroadcastAddress {
e.camLock.RLock() e.camLock.RLock()
srcID, ok := e.cam[src] srcID, ok := e.cam[src]
@@ -144,7 +149,7 @@ func (e *Switch) txPkt(pkt *stack.PacketBuffer) error {
return err return err
} }
atomic.AddUint64(&e.Sent, uint64(pkt.Size())) atomic.AddUint64(&e.Sent, uint64(size))
} }
} else { } else {
e.camLock.RLock() e.camLock.RLock()
@@ -159,7 +164,7 @@ func (e *Switch) txPkt(pkt *stack.PacketBuffer) error {
if err != nil { if err != nil {
return err return err
} }
atomic.AddUint64(&e.Sent, uint64(pkt.Size())) atomic.AddUint64(&e.Sent, uint64(size))
} }
return nil return nil
} }

View File

@@ -18,11 +18,11 @@ func Dial(endpoint string) (net.Conn, string, error) {
} }
switch parsed.Scheme { switch parsed.Scheme {
case "vsock": case "vsock":
contextID, err := strconv.Atoi(parsed.Hostname()) contextID, err := strconv.ParseUint(parsed.Hostname(), 10, 32)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
port, err := strconv.Atoi(parsed.Port()) port, err := strconv.ParseUint(parsed.Port(), 10, 32)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }

View File

@@ -13,13 +13,13 @@ const DefaultURL = "vsock://:1024"
func listenURL(parsed *url.URL) (net.Listener, error) { func listenURL(parsed *url.URL) (net.Listener, error) {
switch parsed.Scheme { switch parsed.Scheme {
case "vsock": case "vsock":
port, err := strconv.Atoi(parsed.Port()) port, err := strconv.ParseUint(parsed.Port(), 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if parsed.Hostname() != "" { if parsed.Hostname() != "" {
cid, err := strconv.Atoi(parsed.Hostname()) cid, err := strconv.ParseUint(parsed.Hostname(), 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -28,12 +28,11 @@ func (n *VirtualNetwork) DialContextTCP(ctx context.Context, addr string) (net.C
if err != nil { if err != nil {
return nil, err return nil, err
} }
return gonet.DialContextTCP(ctx, n.stack, return gonet.DialContextTCP(ctx, n.stack,
tcpip.FullAddress{ tcpip.FullAddress{
NIC: 1, NIC: 1,
Addr: tcpip.AddrFrom4Slice(ip.To4()), Addr: tcpip.AddrFrom4Slice(ip.To4()),
Port: uint16(port), Port: port,
}, ipv4.ProtocolNumber) }, ipv4.ProtocolNumber)
} }
@@ -45,11 +44,11 @@ func (n *VirtualNetwork) Listen(network, addr string) (net.Listener, error) {
return gonet.ListenTCP(n.stack, tcpip.FullAddress{ return gonet.ListenTCP(n.stack, tcpip.FullAddress{
NIC: 1, NIC: 1,
Addr: tcpip.AddrFrom4Slice(ip.To4()), Addr: tcpip.AddrFrom4Slice(ip.To4()),
Port: uint16(port), Port: port,
}, ipv4.ProtocolNumber) }, ipv4.ProtocolNumber)
} }
func splitIPPort(network string, addr string) (net.IP, uint64, error) { func splitIPPort(network string, addr string) (net.IP, uint16, error) {
if network != "tcp" { if network != "tcp" {
return nil, 0, errors.New("only tcp is supported") return nil, 0, errors.New("only tcp is supported")
} }
@@ -65,5 +64,5 @@ func splitIPPort(network string, addr string) (net.IP, uint64, error) {
if ip == nil { if ip == nil {
return nil, 0, errors.New("invalid address, must be an IP") return nil, 0, errors.New("invalid address, must be an IP")
} }
return ip, port, nil return ip, uint16(port), nil
} }

View File

@@ -33,11 +33,12 @@ func (n *VirtualNetwork) ServicesMux() *http.ServeMux {
http.Error(w, "ip is mandatory", http.StatusInternalServerError) http.Error(w, "ip is mandatory", http.StatusInternalServerError)
return return
} }
port, err := strconv.Atoi(r.URL.Query().Get("port")) port, err := strconv.ParseUint(r.URL.Query().Get("port"), 10, 16)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
port16 := uint16(port)
hj, ok := w.(http.Hijacker) hj, ok := w.(http.Hijacker)
if !ok { if !ok {
@@ -67,7 +68,7 @@ func (n *VirtualNetwork) ServicesMux() *http.ServeMux {
return gonet.DialContextTCP(ctx, n.stack, tcpip.FullAddress{ return gonet.DialContextTCP(ctx, n.stack, tcpip.FullAddress{
NIC: 1, NIC: 1,
Addr: tcpip.AddrFrom4Slice(net.ParseIP(ip).To4()), Addr: tcpip.AddrFrom4Slice(net.ParseIP(ip).To4()),
Port: uint16(port), Port: port16,
}, ipv4.ProtocolNumber) }, ipv4.ProtocolNumber)
}, },
OnDialError: func(_ net.Conn, dstDialErr error) { OnDialError: func(_ net.Conn, dstDialErr error) {

View File

@@ -41,11 +41,15 @@ func New(configuration *types.Configuration) (*VirtualNetwork, error) {
ipPool.Reserve(net.ParseIP(ip), mac) ipPool.Reserve(net.ParseIP(ip), mac)
} }
tapEndpoint, err := tap.NewLinkEndpoint(configuration.Debug, configuration.MTU, configuration.GatewayMacAddress, configuration.GatewayIP, configuration.GatewayVirtualIPs) mtu := configuration.MTU
if mtu < 0 || mtu > math.MaxUint32 {
return nil, errors.New("mtu is out of range")
}
tapEndpoint, err := tap.NewLinkEndpoint(configuration.Debug, uint32(mtu), configuration.GatewayMacAddress, configuration.GatewayIP, configuration.GatewayVirtualIPs)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "cannot create tap endpoint") return nil, errors.Wrap(err, "cannot create tap endpoint")
} }
networkSwitch := tap.NewSwitch(configuration.Debug, configuration.MTU) networkSwitch := tap.NewSwitch(configuration.Debug, mtu)
tapEndpoint.Connect(networkSwitch) tapEndpoint.Connect(networkSwitch)
networkSwitch.Connect(tapEndpoint) networkSwitch.Connect(tapEndpoint)

View File

@@ -4,7 +4,9 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt"
"io" "io"
"math"
"net" "net"
"github.com/containers/gvisor-tap-vsock/pkg/types" "github.com/containers/gvisor-tap-vsock/pkg/types"
@@ -41,6 +43,10 @@ func vpnkitHandshake(conn net.Conn, configuration *types.Configuration) error {
// https://github.com/moby/hyperkit/blob/2f061e447e1435cdf1b9eda364cea6414f2c606b/src/lib/pci_virtio_net_vpnkit.c#L131 // https://github.com/moby/hyperkit/blob/2f061e447e1435cdf1b9eda364cea6414f2c606b/src/lib/pci_virtio_net_vpnkit.c#L131
resp := make([]byte, 258) resp := make([]byte, 258)
resp[0] = 0x01 resp[0] = 0x01
if configuration.MTU < 0 || configuration.MTU > math.MaxUint16 {
return fmt.Errorf("invalid MTU: %d", configuration.MTU)
}
mtu := uint16(configuration.MTU) mtu := uint16(configuration.MTU)
binary.LittleEndian.PutUint16(resp[1:3], mtu) binary.LittleEndian.PutUint16(resp[1:3], mtu)
binary.LittleEndian.PutUint16(resp[3:5], mtu+header.EthernetMinimumSize) binary.LittleEndian.PutUint16(resp[3:5], mtu+header.EthernetMinimumSize)

View File

@@ -62,7 +62,7 @@ func init() {
var _ = ginkgo.BeforeSuite(func() { var _ = ginkgo.BeforeSuite(func() {
// clear the environment before running the tests. It may happen the tests were abruptly stopped earlier leaving a dirty env // clear the environment before running the tests. It may happen the tests were abruptly stopped earlier leaving a dirty env
clear() cleanup()
// check if vfkit version is greater than v0.5 (ignition support is available starting from v0.6) // check if vfkit version is greater than v0.5 (ignition support is available starting from v0.6)
version, err := vfkitVersion() version, err := vfkitVersion()
@@ -207,7 +207,7 @@ func sshCommand(cmd ...string) *exec.Cmd {
return sshCmd return sshCmd
} }
func clear() { func cleanup() {
_ = os.Remove(efiStore) _ = os.Remove(efiStore)
_ = os.Remove(sock) _ = os.Remove(sock)
_ = os.Remove(vfkitSock) _ = os.Remove(vfkitSock)
@@ -249,5 +249,5 @@ var _ = ginkgo.AfterSuite(func() {
log.Error(err) log.Error(err)
} }
} }
clear() cleanup()
}) })