Skip to content

Commit

Permalink
[CWS] add a bypass if raw packet disabled or no rules (DataDog#31438)
Browse files Browse the repository at this point in the history
  • Loading branch information
safchain authored Nov 26, 2024
1 parent c51e43f commit 5caf3e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pkg/security/ebpf/c/include/hooks/network/tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,18 @@ __attribute__((always_inline)) int prepare_raw_packet_event(struct __sk_buff *sk
return ACT_OK;
}

__attribute__((always_inline)) int is_raw_packet_enabled() {
u32 key = 0;
u32 *enabled = bpf_map_lookup_elem(&raw_packet_enabled, &key);
return enabled && *enabled;
}

SEC("classifier/ingress")
int classifier_raw_packet_ingress(struct __sk_buff *skb) {
if (!is_raw_packet_enabled()) {
return ACT_OK;
}

struct packet_t *pkt = parse_packet(skb, INGRESS);
if (!pkt) {
return ACT_OK;
Expand All @@ -76,6 +85,10 @@ int classifier_raw_packet_ingress(struct __sk_buff *skb) {

SEC("classifier/egress")
int classifier_raw_packet_egress(struct __sk_buff *skb) {
if (!is_raw_packet_enabled()) {
return ACT_OK;
}

struct packet_t *pkt = parse_packet(skb, EGRESS);
if (!pkt) {
return ACT_OK;
Expand Down
1 change: 1 addition & 0 deletions pkg/security/ebpf/c/include/maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ BPF_PERCPU_ARRAY_MAP(selinux_write_buffer, struct selinux_write_buffer_t, 1)
BPF_PERCPU_ARRAY_MAP(is_new_kthread, u32, 1)
BPF_PERCPU_ARRAY_MAP(syscalls_stats, struct syscalls_stats_t, EVENT_MAX)
BPF_PERCPU_ARRAY_MAP(raw_packet_event, struct raw_packet_event_t, 1)
BPF_PERCPU_ARRAY_MAP(raw_packet_enabled, u32, 1)

BPF_PROG_ARRAY(args_envs_progs, 3)
BPF_PROG_ARRAY(dentry_resolver_kprobe_or_fentry_callbacks, EVENT_MAX)
Expand Down
29 changes: 27 additions & 2 deletions pkg/security/probe/probe_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type EBPFProbe struct {
profileManagers *SecurityProfileManagers
fieldHandlers *EBPFFieldHandlers
eventPool *ddsync.TypedPool[model.Event]
numCPU int

ctx context.Context
cancelFnc context.CancelFunc
Expand Down Expand Up @@ -384,6 +385,14 @@ func (p *EBPFProbe) setupRawPacketProgs(rs *rules.RuleSet) error {
return errors.New("unable to find `classifier_router` map")
}

enabledMap, _, err := p.Manager.GetMap("raw_packet_enabled")
if err != nil {
return err
}
if enabledMap == nil {
return errors.New("unable to find `raw_packet_enabled` map")
}

var rawPacketFilters []rawpacket.Filter
for id, rule := range rs.GetRules() {
for _, field := range rule.GetFieldValues("packet.filter") {
Expand All @@ -394,12 +403,28 @@ func (p *EBPFProbe) setupRawPacketProgs(rs *rules.RuleSet) error {
}
}

// enable raw packet or not
enabled := make([]uint32, p.numCPU)
if len(rawPacketFilters) > 0 {
for i := range enabled {
enabled[i] = 1
}
}
if err = enabledMap.Put(uint32(0), enabled); err != nil {
seclog.Errorf("couldn't push raw_packet_enabled entry to kernel space: %s", err)
}

// unload the previews one
if p.rawPacketFilterCollection != nil {
p.rawPacketFilterCollection.Close()
ddebpf.RemoveNameMappingsCollection(p.rawPacketFilterCollection)
}

// not enabled
if enabled[0] == 0 {
return nil
}

// adapt max instruction limits depending of the kernel version
opts := rawpacket.DefaultProgOpts
if p.kernelVersion.Code >= kernel.Kernel5_2 {
Expand Down Expand Up @@ -1894,12 +1919,12 @@ func NewEBPFProbe(probe *Probe, config *config.Config, opts Opts, telemetry tele

p.monitors = NewEBPFMonitors(p)

numCPU, err := utils.NumCPU()
p.numCPU, err = utils.NumCPU()
if err != nil {
return nil, fmt.Errorf("failed to parse CPU count: %w", err)
}

p.managerOptions.MapSpecEditors = probes.AllMapSpecEditors(numCPU, probes.MapSpecEditorOpts{
p.managerOptions.MapSpecEditors = probes.AllMapSpecEditors(p.numCPU, probes.MapSpecEditorOpts{
TracedCgroupSize: config.RuntimeSecurity.ActivityDumpTracedCgroupsCount,
UseRingBuffers: useRingBuffers,
UseMmapableMaps: useMmapableMaps,
Expand Down

0 comments on commit 5caf3e1

Please sign in to comment.