Bound OSC index before reads in wolfSSH_DoOSC#1035
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a Windows-only out-of-bounds read in the terminal escape-sequence parser by adding explicit bounds checks in wolfSSH_DoOSC (OSC / ESC ] handling) and extending the API test suite to cover truncated OSC inputs. This hardens the documented wolfSSH_ConvertConsole API against peer-supplied channel-data buffers that end mid-OSC sequence.
Changes:
- Add an entry guard in
wolfSSH_DoOSCto prevent dereferencingbuf[*idx]when*idx >= bufSz. - Add additional bounds handling in the OSC
'0'title handler (guard the;read and avoidbuf[i]reads wheni == bufSz). - Extend
test_wolfSSH_ConvertConsolewith truncated and well-formed OSC vectors to exercise the new bounds checks.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/wolfterm.c |
Adds bounds checks and safer truncation handling in OSC parsing to prevent OOB reads. |
tests/api.c |
Adds Windows-only API test vectors for truncated and valid OSC sequences to validate the fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1035
Scan targets checked: wolfssh-bugs, wolfssh-src
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Findings are non-blocking.
7e64f6d to
14d1bdc
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1035
Scan targets checked: wolfssh-bugs, wolfssh-src
No new issues found in the changed files. ✅
Fix out-of-bounds read in wolfSSH_DoOSC on truncated OSC sequences
Summary
wolfSSH_DoOSCreadbuf[*idx]without first checking*idx < bufSz. Thecaller
wolfSSH_ConvertConsoleonly guardsi + 1 < bufSzbefore forwardingz = i + 2as*idx, so*idxcan equalbufSz. A peer-suppliedchannel-data buffer whose last two bytes are
ESC ](0x1B 0x5D) driveswolfSSH_DoOSCto dereferencebuf[bufSz]— one byte past the heapallocation — at the very first conditional.
Two adjacent reads inside the OSC
'0'(set icon/window title) handler sharedthe same flaw: the
;check after*idx += 1, and the post-loop BEL test thatread
buf[i]when the scan reachedi == bufSz.This code is compiled only under
USE_WINDOWS_APIand is reached through thedocumented
wolfSSH_ConvertConsoleAPI when an external (Windows) consumerfeeds received channel-data bytes into it.
Changes
src/wolfterm.cwolfSSH_DoOSC: when*idx >= bufSz, drop thesequence (
return WS_SUCCESS) before anybuf[*idx]read.;read in the title handler after*idx += 1.buf[i] != 0x07test withi >= bufSz, and set*idx = bufSzso a truncated title is consumed/dropped rather than emittedas raw console output.
WS_SUCCESS) instead ofreturning
WS_WANT_READ.wolfSSH_DoOSChas no resume path — it never savesstate to
escBufandescStateis never set toWS_ESC_OSC— so aWS_WANT_READwould leave broken parser state (escState == WS_ESC_START,escBufSz == 0) and mis-parse the continuation buffer on re-entry.tests/api.ctest_wolfSSH_ConvertConsolewith OSC vectors covering each newbounds check — buffers ending at
ESC ],ESC ] 0, andESC ] 0 ; title(no BEL) — plus a well-formed
ESC ] 0 ; hi BELpositive control..github/workflows/windows-check.ymlasan-testsjob that builds and runsapi-testandunit-testunderthe MSVC AddressSanitizer. This is the only CI job that exercises the
USE_WINDOWS_APIconsole code (wolfSSH_DoOSC) under a sanitizer: theexisting
sanitizer.ymlruns on Linux, wherewolfterm.cis compiled out,so the OSC path previously had no sanitizer coverage.
pinned v142 does not) and builds the whole job with it.
/p:EnableASAN=true(instrumenting the referencedwolfsshlibrary and
src/wolfterm.c) and/p:WholeProgramOptimization=falsesoobjects/libs link without a cross-version
/GLcode-generation conflict.canary proving ASan detects an overflow on the runner, a
dumpbinassertion that the test binaries import the ASan runtime, and
ASAN_OPTIONS=print_stats=1for in-process evidence.Testing
tests/api.csuite. The new OSC vectors run onUSE_WINDOWS_APIbuilds via the
api-testbinary.ASan + UBSan locally: clean on the PoC and all truncation/well-formed cases.
A negative control with the guard removed reproduced the original
heap-buffer-overflow READ of size 1, 0 bytes after a 2-byte region.asan-testsCI job is green: the positive control reportsAddressSanitizer: stack-buffer-overflowthen continues, the test binariesare confirmed linked against the ASan runtime, and
api-test/unit-testrun clean under ASan.