From 7169c232eda6100cbd50a7292473219cb79e7c71 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Tue, 19 Jul 2022 15:12:45 -0400 Subject: [PATCH 1/3] add lockfile to prevent corruption of /etc/hosts --- netclient/functions/mqhandlers.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/netclient/functions/mqhandlers.go b/netclient/functions/mqhandlers.go index 34ed633e..a7f1b488 100644 --- a/netclient/functions/mqhandlers.go +++ b/netclient/functions/mqhandlers.go @@ -2,6 +2,9 @@ package functions import ( "encoding/json" + "errors" + "fmt" + "os" "runtime" "strings" "time" @@ -243,9 +246,20 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) { func setHostDNS(dns, iface string, windows bool) error { etchosts := "/etc/hosts" + temp := os.TempDir() + lockfile := temp + "netclient-lock" if windows { etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" } + if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) { + return errors.New("/etc/hosts file is locked .... aborting") + } + lock, err := os.Create(lockfile) + if err != nil { + return fmt.Errorf("could not create lock file %w", err) + } + lock.Close() + defer os.Remove(lockfile) dnsdata := strings.NewReader(dns) profile, err := parser.ParseProfile(dnsdata) if err != nil { @@ -268,9 +282,20 @@ func setHostDNS(dns, iface string, windows bool) error { func removeHostDNS(iface string, windows bool) error { etchosts := "/etc/hosts" + temp := os.TempDir() + lockfile := temp + "netclient-lock" if windows { etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" } + if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) { + return errors.New("/etc/hosts file is locked .... aborting") + } + lock, err := os.Create(lockfile) + if err != nil { + return fmt.Errorf("could not create lock file %w", err) + } + lock.Close() + defer os.Remove(lockfile) hosts, err := file.NewFile(etchosts) if err != nil { return err From 0ac12e9eb3afb024faf3868ac1f925c18a97e7da Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Tue, 19 Jul 2022 15:36:53 -0400 Subject: [PATCH 2/3] lockfile path:w --- netclient/functions/mqhandlers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netclient/functions/mqhandlers.go b/netclient/functions/mqhandlers.go index a7f1b488..7f85a12f 100644 --- a/netclient/functions/mqhandlers.go +++ b/netclient/functions/mqhandlers.go @@ -283,9 +283,10 @@ func setHostDNS(dns, iface string, windows bool) error { func removeHostDNS(iface string, windows bool) error { etchosts := "/etc/hosts" temp := os.TempDir() - lockfile := temp + "netclient-lock" + lockfile := temp + "/netclient-lock" if windows { etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" + lockfile = temp + "\\netclient-lock" } if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) { return errors.New("/etc/hosts file is locked .... aborting") From 3a9f75cc83a6b6c5b9d888a3e63073fde6dc541a Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Wed, 20 Jul 2022 12:51:38 -0400 Subject: [PATCH 3/3] lockfile path --- netclient/functions/mqhandlers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netclient/functions/mqhandlers.go b/netclient/functions/mqhandlers.go index 7f85a12f..64470b19 100644 --- a/netclient/functions/mqhandlers.go +++ b/netclient/functions/mqhandlers.go @@ -247,9 +247,10 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) { func setHostDNS(dns, iface string, windows bool) error { etchosts := "/etc/hosts" temp := os.TempDir() - lockfile := temp + "netclient-lock" + lockfile := temp + "/netclient-lock" if windows { etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" + lockfile = temp + "\\netclient-lock" } if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) { return errors.New("/etc/hosts file is locked .... aborting")