From 654048bb6cb052a3b5f414508ab4839635865725 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 30 May 2026 21:50:29 +0300 Subject: test(generate): lock in listen(2) handler classification Audit of syscall listen(2): int listen(int sockfd, int backlog). Confirmed the tracing implementation already matches the man page and its socket siblings (bind/connect/accept/getsockname/getpeername): - KindFd, capturing ev->fd = args[0] (sockfd) - FamilyNetwork - exit ret_event UNCLASSIFIED (returns 0/-1, no byte count) listen was already covered by the name-based classify/family/retclassify tests but lacked a dedicated generated-handler lock-in test like its bind/getsockname siblings. Add FormatListen/FormatExitListen tracepoint fixtures and TestGenerateListenHandler asserting the enter captures fd=args[0] (and never backlog at args[1]) and the exit stays UNCLASSIFIED. No classification or generated-code changes; mage generate produces no diff. Co-Authored-By: Claude Opus 4.8 --- internal/generate/codegen_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'internal/generate/codegen_test.go') diff --git a/internal/generate/codegen_test.go b/internal/generate/codegen_test.go index da86b86..818a2d2 100644 --- a/internal/generate/codegen_test.go +++ b/internal/generate/codegen_test.go @@ -123,6 +123,43 @@ func TestGenerateGetsocknameHandler(t *testing.T) { requireNotContains(t, output, "ev->ret_type = TRANSFER_CLASSIFIED;") } +// TestGenerateListenHandler locks in the generated BPF C for listen(2): +// +// int listen(int sockfd, int backlog) +// +// listen marks the socket referred to by sockfd as passive (it will accept +// incoming connections via accept(2)) and returns 0 on success or -1 on error. +// Its sockfd is at args[0], so the enter handler is a KindFd fd_event capturing +// ev->fd = args[0] — matching its socket siblings bind/connect/accept/ +// getsockname/getpeername. The backlog argument (args[1]) is a plain int queue +// length, NOT a second fd, so it must NOT be captured. The exit handler is a +// plain ret_event marked UNCLASSIFIED (0/-1, no byte count), so it must not +// carry a READ/WRITE/TRANSFER classification — guarding against any mistaken +// recvfrom/sendto-style byte-transfer accounting. +func TestGenerateListenHandler(t *testing.T) { + output := generateFromPair(t, FormatListen, FormatExitListen) + + // Enter: KindFd fd_event capturing the sockfd from args[0]. + requireContains(t, output, `SEC("tracepoint/syscalls/sys_enter_listen")`) + requireContains(t, output, "struct fd_event *ev = bpf_ringbuf_reserve(&event_map, sizeof(struct fd_event), 0);") + requireContains(t, output, "ev->event_type = ENTER_FD_EVENT;") + requireContains(t, output, "ev->trace_id = SYS_ENTER_LISTEN;") + requireContains(t, output, "ev->fd = (__s32)ctx->args[0];") + + // Negative guards: the backlog (args[1]) must not be captured as another fd, + // and listen reads no userspace path/buffer. + requireNotContains(t, output, "bpf_probe_read_user_str") + requireNotContains(t, output, "ev->fd = (__s32)ctx->args[1];") + + // Exit: plain ret_event, UNCLASSIFIED (listen returns 0/-1, no byte count). + requireContains(t, output, `SEC("tracepoint/syscalls/sys_exit_listen")`) + requireContains(t, output, "struct ret_event *ev = bpf_ringbuf_reserve(&event_map, sizeof(struct ret_event), 0);") + requireContains(t, output, "ev->ret_type = UNCLASSIFIED;") + requireNotContains(t, output, "ev->ret_type = READ_CLASSIFIED;") + requireNotContains(t, output, "ev->ret_type = WRITE_CLASSIFIED;") + requireNotContains(t, output, "ev->ret_type = TRANSFER_CLASSIFIED;") +} + func TestGeneratePidfdGetfdHandlerUsesPidfdArgument(t *testing.T) { output := generateFromPair(t, FormatPidfdGetfd, FormatExitPidfdGetfd) -- cgit v1.2.3