From af1a925b110a9eaf9d091390cdae76a50dda5b28 Mon Sep 17 00:00:00 2001 From: dma Date: Tue, 19 Sep 2017 23:56:03 +0000 Subject: [PATCH] Fix bug where sometimes not all of /proc/net/tcp is read --- sgfw/policy.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/sgfw/policy.go b/sgfw/policy.go index 6df2bda..de4254f 100644 --- a/sgfw/policy.go +++ b/sgfw/policy.go @@ -529,18 +529,33 @@ func readFileDirect(filename string) ([]byte, error) { fd := int(res) data := make([]byte, 65535) + i := 0 + val := 0 + for i = 0; i < 65535; { + val, err = syscall.Read(fd, data[i:]) + i += val + if err != nil && val != 0 { + return nil, err + } + if val == 0 { + break + } + } + + data = data[0:i] +/* val, err := syscall.Read(fd, data) if err != nil { return nil, err } - +*/ syscall.Close(fd) - +/* if val < 65535 { data = data[0:val] } - +*/ return data, nil } @@ -624,7 +639,6 @@ func LookupSandboxProc(srcip net.IP, srcp uint16, dstip net.IP, dstp uint16, pro data = string(bdata) lines := strings.Split(data, "\n") rlines := make([]string, 0) - for l := 0; l < len(lines); l++ { lines[l] = strings.TrimSpace(lines[l]) ssplit := strings.Split(lines[l], ":") @@ -632,10 +646,12 @@ func LookupSandboxProc(srcip net.IP, srcp uint16, dstip net.IP, dstp uint16, pro if len(ssplit) != 6 { continue } - + rlines = append(rlines, strings.Join(ssplit, ":")) } + log.Warningf("Looking for %s:%d => %s:%d \n %s\n******\n", srcip, srcp, dstip, dstp, data) + if proto == "tcp" { res = procsnitch.LookupTCPSocketProcessAll(srcip, srcp, dstip, dstp, rlines) } else if proto == "udp" { @@ -652,7 +668,7 @@ func LookupSandboxProc(srcip net.IP, srcp uint16, dstip net.IP, dstp uint16, pro res.ExePath = GetRealRoot(res.ExePath, OzInitPids[i].Pid) break } else { - fmt.Printf("Couldn't find sandbox name.\n") + log.Warningf("*****\nCouldn't find proc for %s:%d => %s:%d \n %s\n******\n", srcip, srcp, dstip, dstp, data) } }