Skip to content

Commit 327803b

Browse files
rksharma95daemon1024
authored andcommitted
run presets test suite on bpflsm only
Signed-off-by: rksharma95 <[email protected]>
1 parent eb0a893 commit 327803b

27 files changed

+94
-178
lines changed

KubeArmor/BPF/anonmapexec.bpf.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ int BPF_PROG(enforce_mmap_file, struct file *file, unsigned long reqprot,
109109
event_data->args[2] = flags;
110110
event_data->event_id = ANON_MAP_EXEC;
111111
if (*present == BLOCK) {
112-
event_data->retval = -13;
112+
event_data->retval = -EPERM;
113113
} else {
114114
event_data->retval = 0;
115115
}
116116
bpf_ringbuf_submit(event_data, 0);
117117
// mapping not backed by any file with executable permission, denying mapping
118118
if (*present == BLOCK) {
119-
return -13;
119+
return -EPERM;
120120
} else {
121121
return 0;
122122
}

KubeArmor/BPF/filelessexec.bpf.c

+5-37
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,19 @@ const event *unused __attribute__((unused));
1717
struct preset_map fileless_exec_preset_containers SEC(".maps");
1818

1919
#define MEMFD "memfd:"
20+
#define RUN_SHM "/run/shm/"
21+
#define DEV_SHM "/dev/shm/"
2022

2123
static __always_inline int is_memfd(char *name) {
22-
char memfd[] = MEMFD;
23-
int i = 0;
24-
while (i < sizeof(MEMFD) - 1 && name[i] != '\0' && name[i] == memfd[i]) {
25-
i++;
26-
}
27-
28-
if (i == sizeof(MEMFD) - 1) {
29-
return 1;
30-
}
31-
32-
return 0;
24+
return string_prefix_match(name, MEMFD, sizeof(MEMFD));
3325
}
3426

35-
#define RUN_SHM "/run/shm/"
36-
3727
static __always_inline int is_run_shm(char *name) {
38-
char run_shm[] = RUN_SHM;
39-
int i = 0;
40-
while (i < sizeof(RUN_SHM) - 1 && name[i] != '\0' && name[i] == run_shm[i]) {
41-
i++;
42-
}
43-
44-
if (i == sizeof(RUN_SHM) - 1) {
45-
return 1;
46-
}
47-
48-
return 0;
28+
return string_prefix_match(name, RUN_SHM, sizeof(RUN_SHM));
4929
}
5030

51-
#define DEV_SHM "/dev/shm/"
52-
5331
static __always_inline int is_dev_shm(char *name) {
54-
char dev_shm[] = DEV_SHM;
55-
int i = 0;
56-
while (i < sizeof(DEV_SHM) - 1 && name[i] != '\0' && name[i] == dev_shm[i]) {
57-
i++;
58-
}
59-
60-
if (i == sizeof(DEV_SHM) - 1) {
61-
return 1;
62-
}
63-
64-
return 0;
32+
return string_prefix_match(name, DEV_SHM, sizeof(DEV_SHM));
6533
}
6634

6735
struct pathname {

KubeArmor/BPF/protectenv.bpf.c

+4-26
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,14 @@ struct {
2121
struct preset_map protectenv_preset_containers SEC(".maps");
2222

2323
#define DIR_PROC "/proc/"
24+
#define FILE_ENVIRON "/environ"
2425

2526
static __always_inline int isProcDir(char *path) {
26-
char procDir[] = DIR_PROC;
27-
int i = 0;
28-
while (i < sizeof(DIR_PROC) - 1 && path[i] != '\0' && path[i] == procDir[i]) {
29-
i++;
30-
}
31-
32-
if (i == sizeof(DIR_PROC) - 1) {
33-
return 1;
34-
}
35-
36-
return 0;
27+
return string_prefix_match(path, DIR_PROC, sizeof(DIR_PROC));
3728
}
3829

39-
#define FILE_ENVIRON "/environ"
40-
4130
static __always_inline int isEnviron(char *path) {
42-
char envFile[] = FILE_ENVIRON;
43-
int i = 0;
44-
while (i < sizeof(FILE_ENVIRON) - 1 && path[i] != '\0' &&
45-
path[i] == envFile[i]) {
46-
i++;
47-
}
48-
49-
if (i == sizeof(FILE_ENVIRON) - 1) {
50-
return 1;
51-
}
52-
53-
return 0;
31+
return string_prefix_match(path, FILE_ENVIRON, sizeof(FILE_ENVIRON));
5432
}
5533

5634
SEC("lsm/file_open")
@@ -101,7 +79,7 @@ int BPF_PROG(enforce_file, struct file *file) {
10179
task_info->pid_ns = okey.pid_ns;
10280
task_info->mnt_ns = okey.mnt_ns;
10381
bpf_ringbuf_submit(task_info, 0);
104-
return -13;
82+
return -EPERM;
10583
}
10684

10785
return 0;

KubeArmor/BPF/shared.h

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ struct {
7474
__uint(max_entries, 3);
7575
} bufk SEC(".maps");
7676

77+
// ============
78+
// match prefix
79+
// ============
80+
81+
static __always_inline int string_prefix_match(const char *name, const char *prefix, size_t prefix_len) {
82+
int i = 0;
83+
while (i < prefix_len - 1 && name[i] != '\0' && name[i] == prefix[i]) {
84+
i++;
85+
}
86+
return (i == prefix_len - 1) ? 1 : 0;
87+
}
88+
7789
// ============
7890
// == preset ==
7991
// ============

KubeArmor/core/kubeArmor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (dm *KubeArmorDaemon) InitPresets(logger *fd.Feeder, monitor *mon.SystemMon
318318
// ClosePresets Function
319319
func (dm *KubeArmorDaemon) ClosePresets() bool {
320320
if err := dm.Presets.Destroy(); err != nil {
321-
dm.Logger.Errf("Failed to destry preset (%s)", err.Error())
321+
dm.Logger.Errf("Failed to destroy preset (%s)", err.Error())
322322
return false
323323
}
324324
return true
-147 KB
Binary file not shown.
-147 KB
Binary file not shown.
-48.2 KB
Binary file not shown.
-48.2 KB
Binary file not shown.

KubeArmor/feeder/feeder.go

-17
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,6 @@ func (fd *Feeder) PushLog(log tp.Log) {
534534
in case of enforcer = AppArmor only Default Posture logs will be converted to
535535
container/host log depending upon the defaultPostureLogs flag
536536
*/
537-
presetlog := false
538-
if strings.Contains(log.Enforcer, "PRESET") {
539-
kg.Printf("PRESET log 1: %+v\n", log)
540-
presetlog = true
541-
}
542537

543538
if (cfg.GlobalCfg.EnforcerAlerts && fd.Enforcer == "BPFLSM" && log.Enforcer == "eBPF Monitor") || (fd.Enforcer != "BPFLSM" && !cfg.GlobalCfg.DefaultPostureLogs) {
544539
log = fd.UpdateMatchedPolicy(log)
@@ -567,10 +562,6 @@ func (fd *Feeder) PushLog(log tp.Log) {
567562
fd.Debug("Pushing Telemetry without source")
568563
}
569564

570-
if presetlog {
571-
kg.Printf("PRESET LOG 2: %+v\n", log)
572-
}
573-
574565
// set hostname
575566
log.HostName = cfg.GlobalCfg.Host
576567

@@ -590,10 +581,6 @@ func (fd *Feeder) PushLog(log tp.Log) {
590581
fd.StrToFile(string(arr))
591582
}
592583

593-
if strings.Contains(log.Enforcer, "PRESET") {
594-
kg.Printf("PRESET_LOG: \n%+v\n", &log)
595-
}
596-
597584
// gRPC output
598585
if log.Type == "MatchedPolicy" || log.Type == "MatchedHostPolicy" || log.Type == "SystemEvent" {
599586

@@ -695,10 +682,6 @@ func (fd *Feeder) PushLog(log tp.Log) {
695682
counter := 0
696683
lenAlert := len(fd.EventStructs.AlertStructs)
697684

698-
if strings.Contains(log.Enforcer, "PRESET") {
699-
kg.Printf("PRESET_ALERT: \n%s\n", &pbAlert)
700-
}
701-
702685
for uid := range fd.EventStructs.AlertStructs {
703686
select {
704687
case fd.EventStructs.AlertStructs[uid].Broadcast <- &pbAlert:

KubeArmor/go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ require (
6565
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
6666
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
6767
github.com/felixge/httpsnoop v1.0.4 // indirect
68-
github.com/fsnotify/fsnotify v1.7.0 // indirect
6968
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
7069
github.com/go-logr/logr v1.4.2 // indirect
7170
github.com/go-logr/stdr v1.2.2 // indirect

KubeArmor/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/5GSEC/SentryFlow/protobuf v0.0.0-20240513071927-c6689c164ec8 h1:vOjDsj/1zs1O4V2UG2SINC7/maAx3WEQsE0bz5n0skI=
2-
github.com/5GSEC/SentryFlow/protobuf v0.0.0-20240513071927-c6689c164ec8/go.mod h1:cvmCAKkLBDXx6Rlk97XQQuAtcOhkM/wsWNbxGOC3yfE=
31
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
42
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
53
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
-392 Bytes
Binary file not shown.
-392 Bytes
Binary file not shown.

KubeArmor/presets/anonmapexec/preset.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ContainerVal struct {
4141
}
4242

4343
type AnonMapExecPreset struct {
44-
base.BasePreset
44+
base.Preset
4545

4646
BPFContainerMap *ebpf.Map
4747

@@ -91,10 +91,10 @@ func (p *AnonMapExecPreset) Name() string {
9191
return NAME
9292
}
9393

94-
func (p *AnonMapExecPreset) RegisterPreset(logger *fd.Feeder, monitor *mon.SystemMonitor) (base.BasePresetInterface, error) {
94+
func (p *AnonMapExecPreset) RegisterPreset(logger *fd.Feeder, monitor *mon.SystemMonitor) (base.PresetInterface, error) {
9595

9696
if logger.Enforcer != "BPFLSM" {
97-
// it's based on actibe enforcer, it might possible that node support bpflsm but
97+
// it's based on active enforcer, it might possible that node support bpflsm but
9898
// current enforcer is not bpflsm
9999
return nil, errors.New("AnonExecutionPreset not supported if bpflsm not supported")
100100
}

KubeArmor/presets/base/basePreset.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Authors of KubeArmor
33

4+
// Package base provides interface for presets
45
package base
56

67
import (
@@ -10,33 +11,43 @@ import (
1011
)
1112

1213
const (
14+
// PRESET_ENFORCER prefix for a preset
1315
PRESET_ENFORCER string = "PRESET-"
1416
)
1517

18+
// PresetType represents type of a preset
1619
type PresetType uint8
1720

1821
const (
22+
// FilelessExec preset type
1923
FilelessExec PresetType = 1
20-
AnonMapExec PresetType = 2
24+
// AnonMapExec preset type
25+
AnonMapExec PresetType = 2
2126
)
2227

28+
// PresetAction preset action
2329
type PresetAction uint32
2430

2531
const (
32+
// Audit action
2633
Audit PresetAction = 1
34+
// Block action
2735
Block PresetAction = 2
2836
)
2937

30-
type BasePreset struct {
38+
// Preset type
39+
type Preset struct {
3140
Logger *fd.Feeder
3241
Monitor *mon.SystemMonitor
3342
}
3443

44+
// InnerKey type
3545
type InnerKey struct {
3646
Path [256]byte
3747
Source [256]byte
3848
}
3949

50+
// EventPreset type
4051
type EventPreset struct {
4152
Ts uint64
4253

@@ -59,10 +70,11 @@ type EventPreset struct {
5970
Data InnerKey
6071
}
6172

62-
type BasePresetInterface interface {
73+
// PresetInterface interface
74+
type PresetInterface interface {
6375
Name() string
6476
// Init() error
65-
RegisterPreset(logger *fd.Feeder, monitor *mon.SystemMonitor) (BasePresetInterface, error)
77+
RegisterPreset(logger *fd.Feeder, monitor *mon.SystemMonitor) (PresetInterface, error)
6678
RegisterContainer(containerID string, pidns, mntns uint32)
6779
UnregisterContainer(containerID string)
6880
UpdateSecurityPolicies(endPoint tp.EndPoint)

KubeArmor/presets/base/containers.go

-72
This file was deleted.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)