Skip to content

Commit

Permalink
fix: validate DNS server IP in SetDNSServer
Browse files Browse the repository at this point in the history
  • Loading branch information
grahambrereton-form3 authored and rekfuki-f3 committed Nov 6, 2023
1 parent 9dcd1ef commit a258d05
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/chaosdaemon/dns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package chaosdaemon
import (
"context"
"fmt"
"net"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
Expand All @@ -36,6 +37,10 @@ func (s *DaemonServer) SetDNSServer(ctx context.Context,
req *pb.SetDNSServerRequest) (*empty.Empty, error) {
log := s.getLoggerFromContext(ctx)

if net.ParseIP(req.DnsServer) == nil {
return nil, fmt.Errorf("invalid DNS server address")
}

log.Info("SetDNSServer", "request", req)
pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
if err != nil {
Expand Down Expand Up @@ -68,6 +73,7 @@ func (s *DaemonServer) SetDNSServer(ctx context.Context,

// add chaos dns server to the first line of /etc/resolv.conf
// Note: can not replace the /etc/resolv.conf like `mv resolv_conf_dnschaos_temp resolv.conf`, will execute with error `Device or resource busy`

processBuilder = bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("cp %s /etc/resolv_conf_dnschaos_temp && sed -i 's/.*nameserver.*/nameserver %s/' /etc/resolv_conf_dnschaos_temp && cat /etc/resolv_conf_dnschaos_temp > %s && rm /etc/resolv_conf_dnschaos_temp", DNSServerConfFile, req.DnsServer, DNSServerConfFile)).SetContext(ctx)
if req.EnterNS {
processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
Expand Down

0 comments on commit a258d05

Please sign in to comment.