@@ -609,48 +609,68 @@ static __always_inline int save_context_to_buffer(bufs_t *bufs_p, void *ptr)
609
609
return 0 ;
610
610
}
611
611
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 ) {
615
613
u32 * off = get_buffer_offset (DATA_BUF_TYPE );
616
-
617
- if (off == NULL )
618
- {
614
+ if (off == NULL ) {
619
615
return -1 ;
620
616
}
621
617
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 ;
625
620
}
626
621
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
+ }
629
626
630
- * off += 1 ;
627
+ if (MAX_BUFFER_SIZE - type_pos < (1 + sizeof (int ) + 1 )) {
628
+ return 0 ;
629
+ }
631
630
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 ;
635
635
}
636
636
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
+ }
641
+
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
+ }
644
646
645
- bpf_probe_read (& (bufs_p -> buf [* off ]), sizeof (int ), & sz );
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
+ }
646
652
647
- * off += sz + sizeof (int );
648
- set_buffer_offset (DATA_BUF_TYPE , * off );
653
+ if (read_size < MAX_STRING_SIZE ) {
654
+ return 0 ;
655
+ }
649
656
650
- return sz + sizeof (int );
657
+ int sz = bpf_probe_read_str (& (bufs_p -> buf [str_pos ]), read_size , ptr );
658
+ if (sz <= 0 ) {
659
+ return 0 ;
651
660
}
652
661
653
- return 0 ;
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 );
654
674
}
655
675
656
676
static __always_inline bool prepend_path (struct path * path , bufs_t * string_p , int buf_type )
@@ -1019,7 +1039,7 @@ static __always_inline u32 init_context(sys_context_t *context)
1019
1039
}
1020
1040
}
1021
1041
1022
- #if (defined( BTF_SUPPORTED ))
1042
+ #if LINUX_VERSION_CODE > KERNEL_VERSION ( 5 , 2 , 0 ) // min version that supports 1 million instructions
1023
1043
struct fs_struct * fs ;
1024
1044
fs = READ_KERN (task -> fs );
1025
1045
struct path path = READ_KERN (fs -> pwd );
@@ -1046,6 +1066,13 @@ static __always_inline u32 init_context(sys_context_t *context)
1046
1066
1047
1067
// To check if subsequent alerts should be dropped per container
1048
1068
static __always_inline bool should_drop_alerts_per_container (sys_context_t * context , struct pt_regs * ctx , u32 types , args_t * args ) {
1069
+ #if LINUX_VERSION_CODE > KERNEL_VERSION (5 , 2 , 0 )
1070
+
1071
+ // throttling for host in case of apparmor is handled in userspace
1072
+ if (context -> pid_id == 0 && context -> mnt_id == 0 ) {
1073
+ return false;
1074
+ }
1075
+
1049
1076
u64 current_timestamp = bpf_ktime_get_ns ();
1050
1077
1051
1078
struct outer_key key = {
@@ -1112,6 +1139,7 @@ static __always_inline bool should_drop_alerts_per_container(sys_context_t *cont
1112
1139
}
1113
1140
1114
1141
bpf_map_update_elem (& kubearmor_alert_throttle , & key , state , BPF_ANY );
1142
+ #endif
1115
1143
return false;
1116
1144
}
1117
1145
0 commit comments