aboutsummaryrefslogtreecommitdiffstats
path: root/listener
diff options
context:
space:
mode:
authorWe-unite <3205135446@qq.com>2024-08-15 16:01:48 +0800
committerWe-unite <3205135446@qq.com>2024-08-15 16:01:48 +0800
commit1a361a7a0a7d17eb91d043d9842a13f03c84ed15 (patch)
treee112de2007989a5d479b6ecb60594e6131bbcfc6 /listener
parent61809e72c524294cb07535d0e31c80a283495f80 (diff)
downloadgodo-1a361a7a0a7d17eb91d043d9842a13f03c84ed15.tar.gz
godo-1a361a7a0a7d17eb91d043d9842a13f03c84ed15.zip
Fix rootfs by cgroup, clean file name, etc.
**1. about root fs** The setns is used by a process(for example, process 12345), to enter a namespace of another process(also, process 12000). Process 12345 opens visual file /proc/12000/ns/xxx, gets a fd, then setns(fd, nstype). Here xxx represents for special type of namespace such as mnt/ipc. Param nstype can be found out in manual. In short, switching namespace uses not fileName but file descriptor, which makes it too hard to listen to setns, because the fd info may have been lost on the road, or it's still on road, not in db. This would make significant error! So, in this commit, I check /proc/pid/cgroup. Although it has nothing to do with root filesystem, it contains docker id. Record it, and deal with it in the filter: For each process that has pivot_root, it records its docker id, we remember the map from docker id to rootfs; then check all processes on the tree, if it has docker id, add the corresponding rootfs. **2. Exit time of pids to be zero** Besides, I fix the exit time of pid in this commit. After merging the same processes, sort them in ascending order, so that in each tgid node, the main pid is always the first thread. Then, check other pids' exit time, if is zero, assumpt that exit time is the same as main pid, which means the process exit while the thread is running. **3. Wrong parent** I fix the ppid of threads. For example, process 10 has a child process 20, 20 has threads 20 and 23. When pid 20 is recvd, the ppid and parentTgid in message must be 10. But then, 10 exits, and the parent process of 20 comes to be 1, then 20 makes thread 23. When pid 23 is recvd, the ppid and parentTgid is 1, that's totally wrong! Also, using the sorted process array, we can easily find the main thread, so get the real parent, and check the ppid of other threads. **4. Clean file name** The original file name in database may be complex(such as "/1/2/./../3"). Clean it with go pkg "path" **5. Next step** TODO: Fix the netlink connector, may it usable immediately after powering on. Then the view.
Diffstat (limited to 'listener')
-rw-r--r--listener/deal.go16
-rw-r--r--listener/global.go2
-rw-r--r--listener/godo.go21
3 files changed, 32 insertions, 7 deletions
diff --git a/listener/deal.go b/listener/deal.go
index 8225224..70c2827 100644
--- a/listener/deal.go
+++ b/listener/deal.go
@@ -79,19 +79,19 @@ func deal() {
79 79
80 switch cooked.tag { 80 switch cooked.tag {
81 case NEWPID: 81 case NEWPID:
82 go dealNewPid(cooked) 82 dealNewPid(cooked)
83 case EXECVE: 83 case EXECVE:
84 go dealExecve(cooked) 84 dealExecve(cooked)
85 case PIDEXIT: 85 case PIDEXIT:
86 go deletePid(cooked) 86 deletePid(cooked)
87 case FILEOPEN: 87 case FILEOPEN:
88 go fileOpen(cooked) 88 fileOpen(cooked)
89 case FILEWRITE: 89 case FILEWRITE:
90 go fileWrite(cooked) 90 fileWrite(cooked)
91 case FILECLOSE: 91 case FILECLOSE:
92 go fileClose(cooked) 92 fileClose(cooked)
93 case PIVOTROOT: 93 case PIVOTROOT:
94 go pivotRoot(cooked) 94 pivotRoot(cooked)
95 } 95 }
96 } 96 }
97} 97}
@@ -131,6 +131,7 @@ func dealNewPid(cooked Event) {
131 docRes[0].Cwd = cooked.cwd 131 docRes[0].Cwd = cooked.cwd
132 docRes[0].Comm = cooked.comm 132 docRes[0].Comm = cooked.comm
133 docRes[0].Args = cooked.argv 133 docRes[0].Args = cooked.argv
134 docRes[0].DockerId = cooked.cgroup
134 135
135 err := pidCol.ReplaceOne(bson.M{"pid": cooked.pid}, docRes[0]) 136 err := pidCol.ReplaceOne(bson.M{"pid": cooked.pid}, docRes[0])
136 if err != nil { 137 if err != nil {
@@ -149,6 +150,7 @@ func dealNewPid(cooked Event) {
149 Cwd: cooked.cwd, 150 Cwd: cooked.cwd,
150 Execve: make([]Exec, 0), 151 Execve: make([]Exec, 0),
151 Children: make([]int, 0), 152 Children: make([]int, 0),
153 DockerId: cooked.cgroup,
152 }) 154 })
153 if err != nil { 155 if err != nil {
154 fmt.Fprintf(os.Stderr, "Err inserting: %v\n", err) 156 fmt.Fprintf(os.Stderr, "Err inserting: %v\n", err)
diff --git a/listener/global.go b/listener/global.go
index 11b18bf..b782284 100644
--- a/listener/global.go
+++ b/listener/global.go
@@ -37,6 +37,7 @@ type Event struct {
37 argv []string 37 argv []string
38 comm string 38 comm string
39 cwd string 39 cwd string
40 cgroup string
40 exit_code int 41 exit_code int
41 exit_signal int 42 exit_signal int
42 srcPath string 43 srcPath string
@@ -67,6 +68,7 @@ type Process struct {
67 RootFS string `bson:"rootfs"` 68 RootFS string `bson:"rootfs"`
68 Cwd string `bson:"cwd"` 69 Cwd string `bson:"cwd"`
69 Children []int `bson:"children"` 70 Children []int `bson:"children"`
71 DockerId string `bson:"docker_id"`
70 Execve []Exec `bson:"execve"` 72 Execve []Exec `bson:"execve"`
71 ExitCode int `bson:"exit_code"` 73 ExitCode int `bson:"exit_code"`
72 ExitSignal int `bson:"exit_signal"` 74 ExitSignal int `bson:"exit_signal"`
diff --git a/listener/godo.go b/listener/godo.go
index 87e9446..8d82231 100644
--- a/listener/godo.go
+++ b/listener/godo.go
@@ -8,6 +8,7 @@ import (
8 "netlink" 8 "netlink"
9 "os" 9 "os"
10 "os/exec" 10 "os/exec"
11 "regexp"
11 "strings" 12 "strings"
12 "syscall" 13 "syscall"
13 "time" 14 "time"
@@ -176,4 +177,24 @@ func checkProc(pCooked *Event) {
176 fmt.Fprintf(os.Stderr, "Err: %v\n", err) 177 fmt.Fprintf(os.Stderr, "Err: %v\n", err)
177 pCooked.cwd = "" 178 pCooked.cwd = ""
178 } 179 }
180
181 fd, err = os.Open(fileName + "cgroup")
182 if err != nil {
183 fmt.Fprintf(os.Stderr, "Err: %v\n", err)
184 // cgroup记空,即没赶上
185 return
186 }
187 scanner = bufio.NewScanner(fd)
188 cgroupRegex := regexp.MustCompile(`/docker/([0-9a-f]+)$`)
189 scanner.Split(bufio.ScanLines)
190 for scanner.Scan() {
191 line := scanner.Text()
192 if cgroupRegex.MatchString(line) {
193 match := cgroupRegex.FindStringSubmatch(line)
194 pCooked.cgroup = match[1]
195 return
196 }
197 }
198 fd.Close()
199 pCooked.cgroup = ""
179} 200}