Skip to content

Fix heap buffer overflow in CLibrary.ioctl(int, long, int[]) (CVE-2026-8484) - #323

Closed
levpachmanov wants to merge 1 commit into
fusesource:masterfrom
levpachmanov:fix-cve-2026-8484-ioctl-heap-overflow
Closed

levpachmanov wants to merge 1 commit into
fusesource:masterfrom
levpachmanov:fix-cve-2026-8484-ioctl-heap-overflow

Conversation

@levpachmanov

Copy link
Copy Markdown

Fixes the heap buffer overflow reported as CVE-2026-8484 (see #319).

The problem

The int[] overload of CLibrary.ioctl (ioctl__IJ_3I in src/main/native/jansi.c) passes the Java array's elements straight to ioctl(). The kernel writes as many bytes as the request defines, not as many as the array holds. For example, on Linux TCGETS writes a 36-byte struct termios on x86-64, so ioctl(fd, TCGETS, new int[1]) writes 32 bytes past the end of the buffer on every call, corrupting the native heap and eventually crashing the JVM.

The fix

Rejecting requests the wrapper cannot size would break callers, so the wrapper keeps accepting every request but no longer hands the array's buffer to the kernel:

  • ioctl() now writes into a zeroed scratch buffer that is at least as large as the array, the argument size encoded in the request (_IOC_SIZE on Linux, IOCPARM_LEN on macOS and FreeBSD), and 4096 bytes. The floor covers legacy terminal requests such as TCGETS that encode no size.
  • Only the array's length is copied back (GetIntArrayRegion / SetIntArrayRegion), so callers that pass a correctly sized array see exactly the same results as before.
  • The WinSize overload, which Jansi itself uses, is unchanged.

Tests

Adds CLibraryIoctlTest, which opens a pty and checks:

  • TIOCGWINSZ into a 1-element array (struct is 8 bytes), in a loop
  • TIOCGWINSZ into an oversized array: the extra elements are left untouched
  • TCGETS into a 1-element array, in a loop (Linux only). Against the current native library this crashes the test JVM.
  • another request (FIONREAD) still works

The test passes locally on macOS. The same change, built with the project's native targets, has been run with the full test suite plus these tests on Linux, macOS and Windows across Java 8, 11 and 17.

Note on binaries

This PR changes the C source only. The prebuilt native libraries under src/main/resources need to be rebuilt (all non-Windows targets; the Windows DLLs don't contain this wrapper) for the fix to take effect in a release.

🤖 Generated with Claude Code

…6-8484)

The int[] overload passed the Java array's elements straight to ioctl().
The kernel writes as many bytes as the request defines (e.g. TCGETS writes
a 36-byte struct termios on Linux x86-64), regardless of the array length,
so a short array overflowed the native heap and could crash the JVM.

The call now goes through a zeroed scratch buffer that is at least as large
as the array, the argument size encoded in the request (_IOC_SIZE /
IOCPARM_LEN), and 4096 bytes for legacy requests that encode no size. Only
the array's length is copied back, so correctly sized callers see the same
results as before and every request is still accepted.

Adds CLibraryIoctlTest covering short arrays, oversized arrays and other
requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant