Skip to content

Commit a86f306

Browse files
authored
Merge pull request #1897 from rksharma95/fix-clang18-bug
fix(monitor): system monitor loading issue with clang-llvm 18
2 parents 2cfc2e2 + f8066c7 commit a86f306

File tree

9 files changed

+47
-40
lines changed

9 files changed

+47
-40
lines changed

KubeArmor/BPF/system_monitor.c

+47-27
Original file line numberDiff line numberDiff line change
@@ -609,48 +609,68 @@ static __always_inline int save_context_to_buffer(bufs_t *bufs_p, void *ptr)
609609
return 0;
610610
}
611611

612-
static __always_inline int save_str_to_buffer(bufs_t *bufs_p, void *ptr)
613-
{
614-
612+
static __always_inline int save_str_to_buffer(bufs_t *bufs_p, void *ptr) {
615613
u32 *off = get_buffer_offset(DATA_BUF_TYPE);
616-
617-
if (off == NULL)
618-
{
614+
if (off == NULL) {
619615
return -1;
620616
}
621617

622-
if (*off > MAX_BUFFER_SIZE - MAX_STRING_SIZE - sizeof(int))
623-
{
624-
return 0; // no enough space
618+
if (*off >= MAX_BUFFER_SIZE) {
619+
return 0;
625620
}
626621

627-
u8 type = STR_T;
628-
bpf_probe_read(&(bufs_p->buf[*off & (MAX_BUFFER_SIZE - 1)]), 1, &type);
622+
u32 type_pos = *off;
623+
if (type_pos >= MAX_BUFFER_SIZE || type_pos + 1 > MAX_BUFFER_SIZE) {
624+
return 0;
625+
}
629626

630-
*off += 1;
627+
if (MAX_BUFFER_SIZE - type_pos < (1 + sizeof(int) + 1)) {
628+
return 0;
629+
}
631630

632-
if (*off > MAX_BUFFER_SIZE - MAX_STRING_SIZE - sizeof(int))
633-
{
634-
return 0; // no enough space
631+
u32 size_pos = type_pos + 1;
632+
if (size_pos >= MAX_BUFFER_SIZE ||
633+
size_pos + sizeof(int) > MAX_BUFFER_SIZE) {
634+
return 0;
635635
}
636636

637-
int sz = bpf_probe_read_str(&(bufs_p->buf[*off + sizeof(int)]), MAX_STRING_SIZE, ptr);
638-
if (sz > 0)
639-
{
640-
if (*off > MAX_BUFFER_SIZE - sizeof(int))
641-
{
642-
return 0; // no enough space
643-
}
637+
u8 type_val = STR_T;
638+
if (bpf_probe_read(&(bufs_p->buf[type_pos]), sizeof(u8), &type_val) < 0) {
639+
return 0;
640+
}
644641

645-
bpf_probe_read(&(bufs_p->buf[*off]), sizeof(int), &sz);
642+
u32 str_pos = size_pos + sizeof(int);
643+
if (str_pos >= MAX_BUFFER_SIZE || str_pos + MAX_STRING_SIZE > MAX_BUFFER_SIZE) {
644+
return 0;
645+
}
646646

647-
*off += sz + sizeof(int);
648-
set_buffer_offset(DATA_BUF_TYPE, *off);
647+
u32 remaining_space = MAX_BUFFER_SIZE - str_pos;
648+
u32 read_size = remaining_space;
649+
if (read_size > MAX_STRING_SIZE) {
650+
read_size = MAX_STRING_SIZE;
651+
}
649652

650-
return sz + sizeof(int);
653+
if (read_size < MAX_STRING_SIZE) {
654+
return 0;
651655
}
652656

653-
return 0;
657+
int sz = bpf_probe_read_str(&(bufs_p->buf[str_pos]), read_size, ptr);
658+
if (sz <= 0) {
659+
return 0;
660+
}
661+
662+
if (bpf_probe_read(&(bufs_p->buf[size_pos]), sizeof(int), &sz) < 0) {
663+
return 0;
664+
}
665+
666+
u32 new_off = str_pos + sz;
667+
if (new_off > MAX_BUFFER_SIZE) {
668+
return 0;
669+
}
670+
671+
set_buffer_offset(DATA_BUF_TYPE, new_off);
672+
673+
return sz + sizeof(int);
654674
}
655675

656676
static __always_inline bool prepend_path(struct path *path, bufs_t *string_p, int buf_type)
-3.82 KB
Binary file not shown.
-3.79 KB
Binary file not shown.
4.77 KB
Binary file not shown.
4.78 KB
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

tests/go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ require (
5454
github.com/go-openapi/strfmt v0.23.0 // indirect
5555
github.com/go-openapi/swag v0.23.0 // indirect
5656
github.com/go-openapi/validate v0.24.0 // indirect
57-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
5857
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
5958
github.com/gogo/protobuf v1.3.2 // indirect
6059
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect

tests/go.sum

-12
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT
4848
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
4949
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
5050
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
51-
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
52-
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
5351
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
5452
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
5553
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
@@ -79,8 +77,6 @@ github.com/go-openapi/validate v0.24.0 h1:LdfDKwNbpB6Vn40xhTdNZAnfLECL81w+VX3Bum
7977
github.com/go-openapi/validate v0.24.0/go.mod h1:iyeX1sEufmv3nPbBdX3ieNviWnOZaJ1+zquzJEf2BAQ=
8078
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
8179
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
82-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
83-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
8480
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
8581
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
8682
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@@ -101,8 +97,6 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
10197
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
10298
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
10399
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
104-
github.com/google/pprof v0.0.0-20240319011627-a57c5dfe54fd h1:LjW4RcTwfcqOYGmD7UpFrn1gfBZ9mgu7QN5mSeFkCog=
105-
github.com/google/pprof v0.0.0-20240319011627-a57c5dfe54fd/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
106100
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
107101
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
108102
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
@@ -176,13 +170,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
176170
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
177171
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
178172
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
179-
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
180-
github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY=
181-
github.com/onsi/ginkgo/v2 v2.14.0/go.mod h1:JkUdW7JkN0V6rFvsHcJ478egV3XH9NxpD27Hal/PhZw=
182173
github.com/onsi/ginkgo/v2 v2.19.1 h1:QXgq3Z8Crl5EL1WBAC98A5sEBHARrAJNzAmMxzLcRF0=
183174
github.com/onsi/ginkgo/v2 v2.19.1/go.mod h1:O3DtEWQkPa/F7fBMgmZQKKsluAy8pd3rEQdrjkPb9zA=
184-
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
185-
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
186175
github.com/onsi/gomega v1.34.0 h1:eSSPsPNp6ZpsG8X1OVmOTxig+CblTc4AxpPBykhe2Os=
187176
github.com/onsi/gomega v1.34.0/go.mod h1:MIKI8c+f+QLWk+hxbePD4i0LMJSExPaZOVfkoex4cAo=
188177
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A=
@@ -251,7 +240,6 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
251240
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
252241
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
253242
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
254-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
255243
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
256244
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
257245
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

0 commit comments

Comments
 (0)