Skip to content

fix: return owned CString from acceptfilter() to prevent use-after-free#241

Open
SebTardif wants to merge 1 commit into
mainfrom
fix-f117
Open

fix: return owned CString from acceptfilter() to prevent use-after-free#241
SebTardif wants to merge 1 commit into
mainfrom
fix-f117

Conversation

@SebTardif

Copy link
Copy Markdown
Owner

Summary

Fixes a use-after-free in acceptfilter() on FreeBSD/NetBSD.

Problem

acceptfilter() returns &CStr that references a stack-local accept_filter_arg. The from_raw_parts creates a slice borrowing data on the stack, but the function returns a reference with lifetime tied to &self. When the function returns, the stack frame is freed and the reference becomes dangling.

Fix

Return an owned CString instead of &CStr. Updated trait signatures in freebsd/net.rs and netbsd/net.rs to match.

Behind unstable feature acceptfilter (issue rust-lang#121891).

Fixes #233

acceptfilter() returns &CStr referencing a stack-local
accept_filter_arg struct. The returned reference outlives the stack
frame, causing a use-after-free: any access to the returned CStr
reads freed stack memory.

Return an owned CString instead. This requires updating the trait
signature in both freebsd/net.rs and netbsd/net.rs.

Fixes #233

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif SebTardif added bug Something isn't working I-unsound Undefined behavior or soundness issue P-high High impact: affects correctness on common paths A-libs Standard library O-freebsd FreeBSD / NetBSD labels Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-libs Standard library bug Something isn't working I-unsound Undefined behavior or soundness issue O-freebsd FreeBSD / NetBSD P-high High impact: affects correctness on common paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

acceptfilter() returns dangling &CStr referencing dropped stack memory

1 participant