Skip to content

Commit 36866ba

Browse files
authored
fix(utils): restore NormalizePath to fix headless /proc and dot paths (#774)
* fix(utils): restore NormalizePath to fix headless /proc and dot paths Signed-off-by: yugal07 <yashsadhwani544@gmail.com> * fix(utils): handle terminal headless proc paths in NormalizePath Signed-off-by: yugal07 <yashsadhwani544@gmail.com> --------- Signed-off-by: yugal07 <yashsadhwani544@gmail.com>
1 parent a933297 commit 36866ba

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

pkg/utils/datasource_event.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func (e *DatasourceEvent) GetEventType() EventType {
397397

398398
func (e *DatasourceEvent) GetExePath() string {
399399
exepath, _ := e.getFieldAccessor("exepath").String(e.Data)
400-
return exepath
400+
return NormalizePath(exepath)
401401
}
402402

403403
func (e *DatasourceEvent) GetExitCode() uint32 {
@@ -439,7 +439,7 @@ func (e *DatasourceEvent) GetFullPath() string {
439439
if path == "" {
440440
path, _ = e.getFieldAccessor("fname").String(e.Data)
441441
}
442-
return path
442+
return NormalizePath(path)
443443
}
444444

445445
func (e *DatasourceEvent) GetGid() *uint32 {
@@ -490,7 +490,7 @@ func (e *DatasourceEvent) GetNamespace() string {
490490

491491
func (e *DatasourceEvent) GetNewPath() string {
492492
newPath, _ := e.getFieldAccessor("newpath").String(e.Data)
493-
return newPath
493+
return NormalizePath(newPath)
494494
}
495495

496496
func (e *DatasourceEvent) GetNumAnswers() int {
@@ -500,7 +500,7 @@ func (e *DatasourceEvent) GetNumAnswers() int {
500500

501501
func (e *DatasourceEvent) GetOldPath() string {
502502
oldPath, _ := e.getFieldAccessor("oldpath").String(e.Data)
503-
return oldPath
503+
return NormalizePath(oldPath)
504504
}
505505

506506
func (e *DatasourceEvent) GetOpcode() int {
@@ -526,7 +526,7 @@ func (e *DatasourceEvent) GetPath() string {
526526
return e.GetFullPath()
527527
}
528528
path, _ := e.getFieldAccessor("fname").String(e.Data)
529-
return path
529+
return NormalizePath(path)
530530
}
531531

532532
func (e *DatasourceEvent) GetPcomm() string {

pkg/utils/normalize_path_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNormalizePath(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input string
11+
expected string
12+
}{
13+
{
14+
name: "empty path",
15+
input: "",
16+
expected: "",
17+
},
18+
{
19+
name: "dot path",
20+
input: ".",
21+
expected: "/",
22+
},
23+
{
24+
name: "absolute path",
25+
input: "/etc/passwd",
26+
expected: "/etc/passwd",
27+
},
28+
{
29+
name: "headless proc path (task)",
30+
input: "/46/task/46/fd",
31+
expected: "/proc/46/task/46/fd",
32+
},
33+
{
34+
name: "headless proc path (fd)",
35+
input: "/46/fd/3",
36+
expected: "/proc/46/fd/3",
37+
},
38+
{
39+
name: "already absolute proc path",
40+
input: "/proc/46/fd/3",
41+
expected: "/proc/46/fd/3",
42+
},
43+
{
44+
name: "terminal headless proc fd path",
45+
input: "/46/fd",
46+
expected: "/proc/46/fd",
47+
},
48+
{
49+
name: "terminal headless proc task path",
50+
input: "/46/task",
51+
expected: "/proc/46/task",
52+
},
53+
{
54+
name: "relative path (not dot)",
55+
input: "usr/bin/ls",
56+
expected: "/usr/bin/ls",
57+
},
58+
{
59+
name: "relative path with ./",
60+
input: "./config",
61+
expected: "/config",
62+
},
63+
{
64+
name: "path with redundant slashes",
65+
input: "/etc//passwd",
66+
expected: "/etc/passwd",
67+
},
68+
{
69+
name: "path with dot components",
70+
input: "/usr/./bin/../lib",
71+
expected: "/usr/lib",
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
if got := NormalizePath(tt.input); got != tt.expected {
78+
t.Errorf("NormalizePath(%q) = %q, want %q", tt.input, got, tt.expected)
79+
}
80+
})
81+
}
82+
}

pkg/utils/path.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"path"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var headlessProcRegex = regexp.MustCompile(`^/\d+/(task|fd)(/|$)`)
10+
11+
// NormalizePath normalizes a path by:
12+
// 1. Prepending "/proc" to "headless" proc paths (e.g. /46/task/46/fd -> /proc/46/task/46/fd)
13+
// 2. Ensuring it starts with "/" if it's not empty
14+
// 3. Converting "." to "/"
15+
// 4. Cleaning the path (removing redundant slashes, dot-dots, etc.)
16+
func NormalizePath(p string) string {
17+
if p == "" {
18+
return ""
19+
}
20+
21+
if p == "." {
22+
return "/"
23+
}
24+
25+
if headlessProcRegex.MatchString(p) {
26+
p = "/proc" + p
27+
}
28+
29+
if !strings.HasPrefix(p, "/") {
30+
p = "/" + p
31+
}
32+
33+
return path.Clean(p)
34+
}

pkg/utils/struct_event.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (e *StructEvent) GetEventType() EventType {
227227
}
228228

229229
func (e *StructEvent) GetExePath() string {
230-
return e.ExePath
230+
return NormalizePath(e.ExePath)
231231
}
232232

233233
func (e *StructEvent) GetExitCode() uint32 {
@@ -247,7 +247,7 @@ func (e *StructEvent) GetFlagsRaw() uint32 {
247247
}
248248

249249
func (e *StructEvent) GetFullPath() string {
250-
return e.FullPath
250+
return NormalizePath(e.FullPath)
251251
}
252252

253253
func (e *StructEvent) GetGid() *uint32 {
@@ -279,15 +279,15 @@ func (e *StructEvent) GetNamespace() string {
279279
}
280280

281281
func (e *StructEvent) GetNewPath() string {
282-
return e.NewPath
282+
return NormalizePath(e.NewPath)
283283
}
284284

285285
func (e *StructEvent) GetNumAnswers() int {
286286
return e.NumAnswers
287287
}
288288

289289
func (e *StructEvent) GetOldPath() string {
290-
return e.OldPath
290+
return NormalizePath(e.OldPath)
291291
}
292292

293293
func (e *StructEvent) GetOpcode() int {
@@ -311,7 +311,7 @@ func (e *StructEvent) GetPath() string {
311311
if e.FullPathTracing {
312312
return e.GetFullPath()
313313
}
314-
return e.Path
314+
return NormalizePath(e.Path)
315315
}
316316

317317
func (e *StructEvent) GetPcomm() string {

0 commit comments

Comments
 (0)