Skip to content

Commit b7ec360

Browse files
authored
[CWS] Fill span context of network events (#38468)
1 parent c6d8fb4 commit b7ec360

File tree

9 files changed

+60
-0
lines changed

9 files changed

+60
-0
lines changed

pkg/security/ebpf/c/include/helpers/network/dns.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ __attribute__((always_inline)) struct dns_event_t *reset_dns_event(struct __sk_b
3030
// process context
3131
fill_network_process_context_from_pkt(&evt->process, pkt);
3232

33+
u64 sched_cls_has_current_pid_tgid_helper = 0;
34+
LOAD_CONSTANT("sched_cls_has_current_pid_tgid_helper", sched_cls_has_current_pid_tgid_helper);
35+
if (sched_cls_has_current_pid_tgid_helper) {
36+
// reset and fill span context
37+
reset_span_context(&evt->span);
38+
fill_span_context(&evt->span);
39+
}
40+
3341
// network context
3442
fill_network_context(&evt->network, skb, pkt);
3543

pkg/security/ebpf/c/include/helpers/network/imds.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ __attribute__((always_inline)) struct imds_event_t *reset_imds_event(struct __sk
2626
// process context
2727
fill_network_process_context_from_pkt(&evt->process, pkt);
2828

29+
u64 sched_cls_has_current_pid_tgid_helper = 0;
30+
LOAD_CONSTANT("sched_cls_has_current_pid_tgid_helper", sched_cls_has_current_pid_tgid_helper);
31+
if (sched_cls_has_current_pid_tgid_helper) {
32+
// reset and fill span context
33+
reset_span_context(&evt->span);
34+
fill_span_context(&evt->span);
35+
}
36+
2937
// network context
3038
fill_network_context(&evt->network, skb, pkt);
3139

pkg/security/ebpf/c/include/helpers/network/stats.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ __attribute__((always_inline)) int flush_network_stats(u32 pid, struct active_fl
4949
// process context
5050
fill_network_process_context(&evt->process, pid, entry->netns);
5151

52+
u64 sched_cls_has_current_pid_tgid_helper = 0;
53+
LOAD_CONSTANT("sched_cls_has_current_pid_tgid_helper", sched_cls_has_current_pid_tgid_helper);
54+
if (sched_cls_has_current_pid_tgid_helper) {
55+
// reset and fill span context
56+
reset_span_context(&evt->span);
57+
fill_span_context(&evt->span);
58+
}
59+
5260
// network context
5361
fill_network_device_context(&evt->device, entry->netns, entry->ifindex);
5462

pkg/security/ebpf/c/include/helpers/span.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ void __attribute__((always_inline)) fill_span_context(struct span_context_t *spa
4949
}
5050
}
5151

52+
void __attribute__((always_inline)) reset_span_context(struct span_context_t *span) {
53+
span->span_id = 0;
54+
span->trace_id[0] = 0;
55+
span->trace_id[1] = 0;
56+
}
57+
5258
void __attribute__((always_inline)) copy_span_context(struct span_context_t *src, struct span_context_t *dst) {
5359
dst->span_id = src->span_id;
5460
dst->trace_id[0] = src->trace_id[0];

pkg/security/ebpf/c/include/hooks/network/dns.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ TAIL_CALL_CLASSIFIER_FNC(dns_response, struct __sk_buff *skb) {
186186
} else {
187187
send_packet_with_context = true;
188188
fill_network_process_context_from_pkt(&map_elem->full_dns_response.process, pkt);
189+
u64 sched_cls_has_current_pid_tgid_helper = 0;
190+
LOAD_CONSTANT("sched_cls_has_current_pid_tgid_helper", sched_cls_has_current_pid_tgid_helper);
191+
if (sched_cls_has_current_pid_tgid_helper) {
192+
// fill span context (that was previously reset by reset_dns_response_event)
193+
fill_span_context(&map_elem->full_dns_response.span);
194+
}
189195
fill_network_context(&map_elem->full_dns_response.network, skb, pkt);
190196
err = bpf_skb_load_bytes(skb, pkt->offset, &map_elem->full_dns_response.header, sizeof(struct dnshdr));
191197
header_id = map_elem->full_dns_response.header.id;

pkg/security/ebpf/c/include/hooks/network/raw.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ TAIL_CALL_CLASSIFIER_FNC(raw_packet_sender, struct __sk_buff *skb) {
2828
// process context
2929
fill_network_process_context_from_pkt(&evt->process, pkt);
3030

31+
u64 sched_cls_has_current_pid_tgid_helper = 0;
32+
LOAD_CONSTANT("sched_cls_has_current_pid_tgid_helper", sched_cls_has_current_pid_tgid_helper);
33+
if (sched_cls_has_current_pid_tgid_helper) {
34+
// reset and fill span context
35+
reset_span_context(&evt->span);
36+
fill_span_context(&evt->span);
37+
}
38+
3139
struct proc_cache_t *entry = get_proc_cache(evt->process.pid);
3240
if (entry == NULL) {
3341
evt->container.container_id[0] = 0;

pkg/security/ebpf/kernel/kernel_bpf.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,9 @@ func (k *Version) SupportCORE() bool {
170170
_, err := btf.LoadKernelSpec()
171171
return err == nil
172172
}
173+
174+
// HasBpfGetCurrentPidTgidForSchedCLS returns true if the kernel supports bpf_get_current_pid_tgid for Sched CLS program type
175+
// https://github.com/torvalds/linux/commit/eb166e522c77699fc19bfa705652327a1e51a117
176+
func (k *Version) HasBpfGetCurrentPidTgidForSchedCLS() bool {
177+
return features.HaveProgramHelper(ebpf.SchedCLS, asm.FnGetCurrentPidTgid) == nil
178+
}

pkg/security/ebpf/kernel/kernel_nobpf.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ func (k *Version) HasSKStorageInTracingPrograms() bool {
7373
func (k *Version) HasTracingHelpersInCgroupSysctlPrograms() bool {
7474
return false
7575
}
76+
77+
// HasBpfGetCurrentPidTgidForSchedCLS returns true if the kernel supports bpf_get_current_pid_tgid for Sched CLS program type
78+
// https://github.com/torvalds/linux/commit/eb166e522c77699fc19bfa705652327a1e51a117
79+
func (k *Version) HasBpfGetCurrentPidTgidForSchedCLS() bool {
80+
return false
81+
}

pkg/security/probe/probe_ebpf.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,10 @@ func (p *EBPFProbe) initManagerOptionsConstants() {
23322332
Name: "raw_packet_filter",
23332333
Value: utils.BoolTouint64(p.config.Probe.NetworkRawPacketFilter != "none"),
23342334
},
2335+
manager.ConstantEditor{
2336+
Name: "sched_cls_has_current_pid_tgid_helper",
2337+
Value: utils.BoolTouint64(p.kernelVersion.HasBpfGetCurrentPidTgidForSchedCLS()),
2338+
},
23352339
)
23362340

23372341
if p.kernelVersion.HavePIDLinkStruct() {

0 commit comments

Comments
 (0)