Skip to content

Commit 9dba761

Browse files
authored
Add Java TLS bpf support (#891)
1 parent ee87ce8 commit 9dba761

File tree

6 files changed

+119
-7
lines changed

6 files changed

+119
-7
lines changed

bpf/common/connection_info.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,7 @@ static __always_inline void populate_ephemeral_info(connection_info_part_t *part
208208
part->type = type;
209209
part->pid = pid;
210210
}
211+
212+
static __always_inline u8 is_empty_connection_info(const connection_info_t *conn) {
213+
return conn->s_port == 0 && conn->d_port == 0;
214+
}

bpf/generictracer/generictracer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
#include "libssl.c"
88
#include "nginx.c"
99
#include "nodejs.c"
10+
#include "java_tls.c"
1011

1112
char __license[] SEC("license") = "Dual MIT/GPL";

bpf/generictracer/java_tls.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:build obi_bpf_ignore
5+
6+
#include <bpfcore/vmlinux.h>
7+
#include <bpfcore/bpf_helpers.h>
8+
#include <bpfcore/bpf_tracing.h>
9+
10+
#include <common/connection_info.h>
11+
#include <common/protocol_defs.h>
12+
13+
#include <generictracer/k_tracer_defs.h>
14+
#include <generictracer/maps/pid_tid_to_conn.h>
15+
16+
#include <logger/bpf_dbg.h>
17+
18+
#include <pid/pid.h>
19+
20+
enum { k_ioctl_magic_id = 0x0b10b1 };
21+
enum {
22+
k_ioctl_java_send = 1,
23+
k_ioctl_java_recv = 2,
24+
};
25+
26+
enum { k_ioctl_invalid_op = 0xff };
27+
28+
static __always_inline u8 cmd_to_op(u8 cmd) {
29+
switch (cmd) {
30+
case k_ioctl_java_send:
31+
return TCP_SEND;
32+
case k_ioctl_java_recv:
33+
return TCP_RECV;
34+
default:
35+
return k_ioctl_invalid_op;
36+
}
37+
}
38+
39+
SEC("kprobe/do_vfs_ioctl")
40+
int BPF_KPROBE(
41+
beyla_kprobe_do_vfs_ioctl, void *filp, unsigned int fd, unsigned int cmd, void *arg) {
42+
(void)ctx;
43+
(void)filp;
44+
45+
u64 id = bpf_get_current_pid_tgid();
46+
47+
if (!valid_pid(id)) {
48+
return 0;
49+
}
50+
51+
bpf_dbg_printk("=== do_vfs_ioctl id=%d ===", id);
52+
53+
// it must be fd == 0 if we are considering this request
54+
if (fd) {
55+
return 0;
56+
}
57+
58+
// some other IOCTL by the app
59+
if (cmd != k_ioctl_magic_id) {
60+
return 0;
61+
}
62+
63+
bpf_dbg_printk("data %llx", arg);
64+
65+
if (!arg) {
66+
return 0;
67+
}
68+
69+
u8 op_cmd = 0;
70+
bpf_probe_read(&op_cmd, sizeof(u8), arg);
71+
72+
u8 op = cmd_to_op(op_cmd);
73+
74+
if (op == k_ioctl_invalid_op) {
75+
bpf_dbg_printk("unknown cmd = %d", op_cmd);
76+
return 0;
77+
}
78+
79+
bpf_dbg_printk("op = %d, cmd = %d", op, op_cmd);
80+
81+
pid_connection_info_t p_conn = {0};
82+
bpf_probe_read(&p_conn.conn, sizeof(connection_info_t), arg + 1);
83+
u16 orig_dport = p_conn.conn.d_port;
84+
sort_connection_info(&p_conn.conn);
85+
p_conn.pid = pid_from_pid_tgid(id);
86+
87+
if (is_empty_connection_info(&p_conn.conn)) {
88+
ssl_pid_connection_info_t *l = bpf_map_lookup_elem(&pid_tid_to_conn, &id);
89+
bpf_dbg_printk("lookup for empty connection info %llx", l);
90+
if (l) {
91+
p_conn = l->p_conn;
92+
}
93+
}
94+
95+
u32 len = 0;
96+
bpf_probe_read(&len, sizeof(u32), arg + 1 + sizeof(connection_info_t));
97+
98+
bpf_dbg_printk("payload len %d", len);
99+
100+
if (len > 0) {
101+
void *buf = arg + 1 + sizeof(connection_info_t) + sizeof(u32);
102+
handle_buf_with_connection(ctx, &p_conn, buf, len, WITH_SSL, op, orig_dport);
103+
}
104+
105+
return 0;
106+
}

internal/test/integration/k8s/manifests/05-uninstrumented-server-client-different-nodes.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ spec:
3131

3232
containers:
3333
- name: httppinger
34-
image: httppinger:dev
34+
image: ghcr.io/open-telemetry/obi-testimg:httppinger-net
35+
imagePullPolicy: IfNotPresent
3536
env:
3637
- name: TARGET_URL
3738
value: "http://otherinstance:8080"
@@ -88,8 +89,8 @@ spec:
8889
claimName: testoutput
8990
containers:
9091
- name: otherinstance
91-
image: testserver:dev
92-
imagePullPolicy: Never # loaded into Kind from localhost
92+
image: ghcr.io/open-telemetry/obi-testimg:gotestserver-net
93+
imagePullPolicy: IfNotPresent
9394
ports:
9495
# exposing hostports to enable operation from tests
9596
- containerPort: 8080

internal/test/integration/k8s/restrict_local_node/restrict_local_node_main_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ var cluster *kube.Kind
3131

3232
func TestMain(m *testing.M) {
3333
if err := docker.Build(os.Stdout, tools.ProjectDir(),
34-
docker.ImageBuild{Tag: "testserver:dev", Dockerfile: k8s.DockerfileTestServer},
35-
docker.ImageBuild{Tag: "httppinger:dev", Dockerfile: k8s.DockerfileHTTPPinger},
3634
docker.ImageBuild{Tag: "obi:dev", Dockerfile: k8s.DockerfileOBI},
3735
docker.ImageBuild{Tag: "quay.io/prometheus/prometheus:v2.55.1"},
3836
docker.ImageBuild{Tag: "otel/opentelemetry-collector-contrib:0.103.0"},
@@ -43,8 +41,6 @@ func TestMain(m *testing.M) {
4341

4442
cluster = kube.NewKind("test-kind-cluster-restrict-local-node",
4543
kube.KindConfig(testpath.Manifests+"/00-kind-multi-node.yml"),
46-
kube.LocalImage("testserver:dev"),
47-
kube.LocalImage("httppinger:dev"),
4844
kube.LocalImage("obi:dev"),
4945
kube.LocalImage("quay.io/prometheus/prometheus:v2.55.1"),
5046
kube.LocalImage("otel/opentelemetry-collector-contrib:0.103.0"),

pkg/internal/ebpf/generictracer/generictracer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ func (p *Tracer) KProbes() map[string]ebpfcommon.ProbeDesc {
324324
Required: true,
325325
Start: p.bpfObjects.ObiKprobeInetCskListenStop,
326326
},
327+
"do_vfs_ioctl": {
328+
Required: true,
329+
Start: p.bpfObjects.BeylaKprobeDoVfsIoctl,
330+
},
327331
}
328332

329333
if p.cfg.EBPF.ContextPropagation != config.ContextPropagationDisabled {

0 commit comments

Comments
 (0)