Fix out-of-bounds read in vsnprintf_async_signal_safe on a trailing '%'#4129
Fix out-of-bounds read in vsnprintf_async_signal_safe on a trailing '%'#4129magic-peach wants to merge 1 commit into
Conversation
When a format string passed to vsnprintf_async_signal_safe ends with a lone
conversion introducer ('%', '%l' or '%ll'), the switch on the conversion
specifier falls through on the terminating NUL and the loop's ++format then
steps past the end of the string, causing an out-of-bounds read. This
function backs serverLogFromHandler (the async-signal-safe logger), so it
should stay robust against malformed formats.
Stop processing when the conversion introducer is immediately followed by the
end of the format string. Add unit tests for vsnprintf_async_signal_safe,
covering ordinary formatting and the trailing-'%' cases; the latter trip
AddressSanitizer without the fix.
Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughFixes ChangesMalformed Format String Fix
Estimated code review effort: 1 (Trivial) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #4129 +/- ##
============================================
- Coverage 76.80% 76.77% -0.03%
============================================
Files 162 162
Lines 81169 81190 +21
============================================
- Hits 62339 62337 -2
- Misses 18830 18853 +23
🚀 New features to boost your workflow:
|
Problem
vsnprintf_async_signal_safe()reads one or more bytes past the end of theformat string when the format ends with a lone conversion introducer —
"%","%l"or"%ll".After skipping
%(and anyl/lllength modifier), the code lands on theterminating NUL. The
switchon the conversion specifier has no case for'\0'and nodefault, so it falls through, and the enclosingfor (; *format; ++format)loop then advancesformatpast the NUL,reading out of bounds on the next
*formattest.This function backs
serverLogFromHandler()(the async-signal-safe logger usedfrom signal handlers), so it should stay robust against malformed format
strings rather than read out of bounds.
Fix
Stop processing when the conversion introducer is immediately followed by the
end of the format string:
Well-formed conversions are unaffected: their specifier character is never the
NUL, so the guard never triggers for them.
Tests
Added
TestSnprintfAsyncSignalSafeinsrc/unit/test_util.cpp(there was noexisting coverage for these functions). It checks ordinary formatting (
%d,%s) plus the trailing-%,%land%llcases.Verified the out-of-bounds read against the current code by extracting the
function into a standalone harness built with
-fsanitize=address:With the fix, all cases pass with no sanitizer error.