Skip to content

Commit cc7b409

Browse files
authored
usm: native-tls: Fix a leak (#43378)
### What does this PR do? Fixes a memory leak in the native-TLS eBPF code by moving the map cleanup to happen before the null check of the connection tuple. ### Motivation Previously, when `tup_from_ssl_ctx` returned NULL, we would return early without cleaning up the `ssl_sock_by_ctx` map entry. This caused a leak where the SSL context entries would remain in the map even though they were no longer needed. By moving the `bpf_map_delete_elem` call before the null check, we ensure the map entry is always cleaned up regardless of whether the tuple lookup succeeds. ### Describe how you validated your changes ### Additional Notes Co-authored-by: guy.arbitman <guy.arbitman@datadoghq.com>
1 parent d356ebe commit cc7b409

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

pkg/network/ebpf/c/protocols/tls/native-tls.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,10 @@ int BPF_BYPASSABLE_UPROBE(uprobe__SSL_shutdown, void *ssl_ctx) {
361361
u64 pid_tgid = bpf_get_current_pid_tgid();
362362
log_debug("uprobe/SSL_shutdown: pid_tgid=%llx ctx=%p", pid_tgid, ssl_ctx);
363363
conn_tuple_t *t = tup_from_ssl_ctx(ssl_ctx, pid_tgid);
364-
if (t == NULL) {
365-
return 0;
366-
}
367-
368-
// tls_finish can launch a tail call, thus cleanup should be done before.
369364
bpf_map_delete_elem(&ssl_sock_by_ctx, &ssl_ctx);
370-
tls_finish(ctx, t, false);
371-
365+
if (t != NULL) {
366+
tls_finish(ctx, t, false);
367+
}
372368
return 0;
373369
}
374370

@@ -526,13 +522,11 @@ static __always_inline void gnutls_goodbye(struct pt_regs *ctx, void *ssl_sessio
526522
u64 pid_tgid = bpf_get_current_pid_tgid();
527523
log_debug("gnutls_goodbye: pid=%llu ctx=%p", pid_tgid, ssl_session);
528524
conn_tuple_t *t = tup_from_ssl_ctx(ssl_session, pid_tgid);
529-
if (t == NULL) {
530-
return;
531-
}
532-
533525
// tls_finish can launch a tail call, thus cleanup should be done before.
534526
bpf_map_delete_elem(&ssl_sock_by_ctx, &ssl_session);
535-
tls_finish(ctx, t, false);
527+
if (t != NULL) {
528+
tls_finish(ctx, t, false);
529+
}
536530
}
537531

538532
// int gnutls_bye (gnutls_session_t session, gnutls_close_request_t how)

0 commit comments

Comments
 (0)