Repository navigation
obs-util: Bound counted FFI string reads - #729
Conversation
|
The Rust Tests (ubuntu-26.04) failure is from the new test, not flakiness. Its Other |
|
Updated from master and marked ready. The one failure is Rust Tests (ubuntu), in the The test pulls in obs-util's FFI shims, which call libobs C symbols that the test binary doesn't provide on x86_64-pc-windows-gnu. Have a look at how the other |
The test didn't reference obs_c_oracle, so rust-lld couldn't resolve
blog, bcrash, base_default_log_handler, base_log_literal and
os_utf8_to_wcs_ptr in the rust-test lane.
Local-Gate: v1 tree=547ace1ff5ccf76d2bc5ca96385236eec6e56a0b secs=558 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run
Ci-Attestation: {"at":1792022194,"gate":"rust/all/fmt","host":"linux-x86_64","key":"b9bdc4411bb2df21860fb54ea30f4951b43e6470210694e1c341f49fa1012e91","lane":"lint","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":115,"stamp":"a4013980dcb298139ed4916cf8b5db13","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"b9bdc4411bb2df21860fb54ea30f4951b43e6470210694e1c341f49fa1012e91","lane":"lint","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":115,"stamp":"e4e85566205110ab44b20be2aed4269d","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"60c24219512e61e2c3564d2c9ea696291abd8fabe9fe23e184e16e70391ec2e7","lane":"rust-test","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":129,"stamp":"a348a6851ce120cf4d7be3518cb91dd4","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"61c3a9125f386f868775c5b87d1487d9","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"dbf735f254f26852e5f81de35f37def7","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"40c2974587d4a0093bc2df3e06e89466","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"8abb6fc71ed98e7a894a9f052858a673","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-apple-darwin/clippy","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"30297d94a9c0c41a259829d2deb957ad","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"rust/x86_64-apple-darwin/dylint","host":"linux-x86_64","key":"825a8cd298b0d16a8a5406195077a114a16a877a77107335caa3664c8bf33179","lane":"cross","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":126,"stamp":"a78caf104f4127056b930647e24c3baf","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
Ci-Attestation: {"at":1792022194,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"81ba259fedf206a6bcf4a86db1ca4447603eb77e485d6a97fe55742f9ff2dcdb","lane":"tier2-linux","parents":["cf179b699fad20fdd176a603191110d5ea0eb6ea"],"secs":188,"stamp":"ce35d3b22379f3e4979a5fb82a40cf35","tree":"547ace1ff5ccf76d2bc5ca96385236eec6e56a0b","v":1,"via":"run"}
|
Pushed the link fix to your branch: |
Problem
With
ENABLE_RUST_LIBOBS=ON,astrcmp_nandastrcmpi_nscan past theirn-byte limit before comparing, so a caller that provides exactlynreadable bytes (no NUL) reads out of bounds.cf_literal_to_strhas the same shape for a nonzerocount: it measured a suffix afterliteral + countbefore parsing.The real-world trigger:
os_fread_utf8(libobs/util/platform.c:192) passes its 3-byte stack bufferchar bom[3](platform.c:179) toastrcmp_n(bom, "\xEF\xBB\xBF", 3). We found that withENABLE_RUST_LIBOBS=ONunder ASan every libobs startup aborts there, since the buffer has no NUL. Tests that need libobs then skip silently andcteststill exits 0, so the abort is easy to miss. This PR fixes it.Root cause
rust/obs-util/src/ffi/dstr.rs:149-151(astrcmp_n) and:158-160(astrcmpi_n) onupstream/masterpassed both arguments throughopt_cstr(:112-118), which callsCStr::from_ptrand scans to NUL before the bounded comparison. The C implementation (libobs/util/dstr.c:57,:79) compares at mostnbytes.rust/obs-util/src/ffi/cf_tokenizer.rs:199onupstream/mastermeasured a suffix withCStr::from_ptr(literal.add(count))for every count. The C implementation (libobs/util/cf-tokenizer.c:92) callsstrlen(literal)only whencount == 0; for a nonzero count that initial measurement does not exist.Fix
The dstr FFI now feeds a lazy byte-pair reader to the shared comparator. It reads one pair at a time and stops at the first difference, NUL, or
n; the core still compares throughc_char, preserving the target Ccharordering. Forn == 0it reads nothing, as in C.cf_literal_to_strnow forms a slice of exactlycountbytes when the count is nonzero, and retains the NUL-terminated path forcount == 0.cf_literal_to_strdetails:Fixed: the initial over-read. A nonzero
countno longer triggers a scan pastliteral + count, so a buffer of exactlycountbytes with no trailing NUL is accepted.Output is byte-identical to C. An independent review compared the Rust FFI with the C oracle on 777,777 counted literals (quote + 0 to 5 bytes over
" \ x 1 0 a space f ' 9+ quote, each with several non-NUL tails) and found 0 differences.The one remaining difference is a read, not a result. C's loop tests
*temp_srcbeforetemp_src < end(libobs/util/cf-tokenizer.c:110). Any escape that ends at the closing quote (not only\xand octal; the skip at:69and:76is one case) leavestemp_srcat or pastend, and C then reads one byte outside the counted span. The value is discarded becausetemp_src < endis already false. Rust does not read that byte: the safe core treats any index past the slice as NUL.strtoul(:68,:75) cannot leave the span, because it starts before the closing quote and the quote stops it.Not changed: the C read itself. Making C test
temp_src < endfirst and clamping the skips at:69and:76would change OFF-mode libobs and is a separate follow-up.The safe-core doc comment on
cf_literal_to_strinrust/obs-util/src/cf_tokenizer.rssays this.Option A: fix only the two dstr comparators. Smaller, but leaves the independently reported counted-literal initial over-read.
Option B (implemented): fix both initial over-reads together and pin each with guard-page and cmocka regressions.
Alternative considered: copy counted inputs into temporary NUL-terminated buffers. Rejected because it adds allocation and still fails to honor the caller's exact span directly.
Evidence
Branch
fix/dstr-n-bounded-readon the fork (two commits onupstream/master35045319ee5c6a2afda129a0471850031233ad24):cc99e98e8f4f596a04cdcda02847be6393889235"obs-util: test bounded counted-string reads (RED)".0f3bdf4024fd68035056a76a1cc8d4042d0f16e7"obs-util: bound counted FFI string reads (GREEN)".Tests, all in the RED commit:
astrcmp_n_does_not_scan_past_n,astrcmpi_n_does_not_scan_past_n,cf_literal_to_str_respects_nonzero_count,cf_literal_to_str_escape_at_span_end_stays_in_bounds(the literal"a\"with count 4: the escape ends at the span end, so the core indexes atcount). They fail on RED and pass on GREEN. A read pastnorcountcrashes the test binary.astrcmp_n_compares_the_nth_byteandastrcmpi_n_compares_the_nth_byte("abcd"vs"abce", n = 4, expect -1; catch a shim that compares only n - 1 bytes), andastrcmp_n_ignores_the_byte_after_nandastrcmpi_n_ignores_the_byte_after_n("abcX"vs"abcY", n = 3, expect 0; catch a shim that compares n + 1 bytes).bounded_compare_with_zero_n_reads_nothingchecks thatn == 0dereferences nothing.astrcmp_n_compares_the_nth_byteandastrcmpi_n_compares_the_nth_bytefail (the guard-page tests alone still pass, which is why the fixed-value tests exist). With the bound changed to n + 1,astrcmp_n_ignores_the_byte_after_nandastrcmpi_n_ignores_the_byte_after_nfail, and the guard-page and zero-n tests crash. With thecf_literal_to_strslice changed tocount + 1,cf_literal_to_str_escape_at_span_end_stays_in_boundscrashes with SIGBUS;cf_literal_to_str_respects_nonzero_countalone does not, because a plain literal never indexes atcount, which is why the escape test exists. (Debug profile, which is thecargo testdefault; the optimizer elides the dead load in--release.)astrcmp_n_reads_only_n_bytesintest/cmocka/test_dstr_regressions.cand a counted-literal case inliteral_to_str_testintest/cmocka/test_cf_lexer.c, both on exact, non-terminated allocations.Local runs (macOS arm64, AppleClang, local flags). The Rust results are on RED
cc99e98e8and GREEN0f3bdf402. The cmocka/ASan results are on the earlier RED3106c6cb5and GREENbff884746; the later commits change onlyrust/obs-util/tests/cf_tokenizer_ffi_bounds.rsand a doc comment inrust/obs-util/src/cf_tokenizer.rs, not the C tests or any compiled code path:Rust,
cargo test -p obs-util --locked: RED fails (cf_tokenizer_ffi_boundsanddstr_ffi_boundscrash with SIGBUS/SIGSEGV on the over-read; all other obs-util targets pass); GREEN passes (dstr_ffi_bounds7 passed,cf_tokenizer_ffi_bounds2 passed, the rest unchanged; clippy-D warningsandcargo fmt --checkclean).cmocka
test_dstr_regressionsandtest_cf_lexer, built with the repo'smacos-cipreset plus a local-fsanitize=address -fno-omit-frame-pointer, Debug.ENABLE_RUST_LIBOBS=OFF: pass on RED.ENABLE_RUST_LIBOBS=ONon RED: both abort with an AddressSanitizer heap-buffer-overflow:After GREEN, both tests pass with the Rust port
OFFandON. macOS ASan does not run LeakSanitizer, so leak detection was not exercised here.Bounded-read audit:
astrcmp_nandastrcmpi_nwere the dstr comparison shims that pre-scanned; the other dstrncopy/ncatshims use counted slices.cf_literal_to_strwas the counted-literal pre-scan. The lexer validators already usec_bytes_limited, and UTF-8 shims use sized slices whenever their input size is nonzero. OtherCStrconversions inspected inrust/obs-util/src/ffiaccept NUL-terminated C-string inputs.Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
Compatibility & risk
No C API or ABI changes. Nonzero counts now bound the initial memory read as the C contract specifies; zero-count
cf_literal_to_strcontinues to accept a NUL-terminated string. The Rust comparator retains the existing Ccharcomparison semantics.cf_literal_to_stroutput is unchanged and identical to C; the only difference left is that C reads one discarded byte past the span for escapes that end at the closing quote and Rust does not (see Fix). Tested here on macOS arm64 only (local ASan and the guard-page tests); Linux was not run. Windows was not run, and the guard-page tests arecfg(unix).Status
Status: draft. Opened so upstream PR CI can run it. Branch
fix/dstr-n-bounded-readat0f3bdf4024fd68035056a76a1cc8d4042d0f16e7.How this was tested
astrcmp_n,astrcmpi_nandcf_literal_to_strin the test-only RED commit: the guard-page tests crash on RED and pass on GREEN. The C regression tests use exact 3-byte non-terminated allocations. Local macOS AppleClang builds use themacos-cipreset with an added local-fsanitize=address -fno-omit-frame-pointer(not a CI flag); withENABLE_RUST_LIBOBS=OFFboth C tests pass before and after the fix, and with itONboth fail on RED with the heap-buffer-overflow traces above and pass on GREEN. Mutation checks of the shim bound (n - 1 and n + 1) are described under Evidence.os_fread_utf8was observed in our own ASan runs withENABLE_RUST_LIBOBS=ON; here it is covered by exact-span guard-page and ASan tests of the sameastrcmp_ncall shape.Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.