Xuanwo opened a new pull request, #8949:
URL: https://github.com/apache/opendal/pull/8949

   # Which issue does this PR close?
   
   N/A (maintenance audit fixes for `bindings/c`).
   
   # Rationale for this change
   
   An audit of `bindings/c` identified five safety, error-fidelity, and 
validation bugs across the FFI boundary:
   
   1. **Heap use-after-free on self-aliasing option setters**: 
`opendal_list_options_set_start_after` and `opendal_delete_options_set_version` 
freed the existing `CString` before reading the incoming `const char *`. 
Passing `opts->start_after` / `opts->version` or a substring pointer into it 
caused a heap use-after-free.
   2. **Reader and writer squashing all backend error kinds into 
`OPENDAL_UNEXPECTED`**: `opendal_writer_write_with_cancel` and 
`opendal_writer_close_with_cancel` re-wrapped every `opendal::Error` in 
`ErrorKind::Unexpected`, and `opendal_reader_read_with_cancel` and 
`opendal_reader_seek_with_cancel` similarly mapped every `std::io::Error` to 
`ErrorKind::Unexpected` instead of unwrapping the inner `opendal::Error` (or 
mapping standard I/O error kinds).
   3. **Panic across `extern "C"` on non-ASCII presigned header values**: 
`opendal_presigned_request_inner::new` called `v.to_str().unwrap()`, which 
panicked across the C ABI whenever a presigned request contained non-ASCII 
`obs-text` bytes in a header value (for example UTF-8 `Content-Disposition` or 
user metadata), and also unwrapped `CString::new(...)` on method/URI/header 
names and values.
   4. **Missing NULL guards and `&mut self` reference UB across FFI**: 
`opendal_presigned_request_{method,uri,headers,headers_len}` dereferenced `req` 
and `(*req).inner` without NULL checks. `opendal_operator_options_set`, 
`opendal_operator_layers_add_retry`, and `opendal_operator_layers_add_timeout` 
took `&mut self` directly in `extern "C"` signatures (causing Rust reference UB 
on NULL input), and `opendal_operator_options_set` did not guard NULL 
`key`/`value`.
   5. **Out-of-range conditional timestamps silently dropped**: 
`opendal_stat_options`, `opendal_read_options`, and `opendal_reader_options` 
converted `if_modified_since` / `if_unmodified_since` with 
`Timestamp::from_millisecond(...).ok()`, silently dropping out-of-range 
millisecond values (such as `i64::MAX` or `i64::MIN`) instead of failing the 
conditional operation.
   
   # What changes are included in this PR?
   
   - **Safe replacement order for string option setters** 
(`bindings/c/src/types.rs`): Allocate the new `CString` before releasing the 
previous pointer via `std::mem::replace` in 
`opendal_list_options_set_start_after` and `opendal_delete_options_set_version`.
   - **Preserve error kinds in reader and writer** (`bindings/c/src/writer.rs`, 
`bindings/c/src/reader.rs`):
     - Pass `CancellableError<core::Error>` directly to 
`opendal_error::from_cancellable` in `opendal_writer_write_with_cancel` and 
`opendal_writer_close_with_cancel`.
     - Downcast `std::io::Error` inner `opendal::Error` in 
`opendal_reader_read_with_cancel` and `opendal_reader_seek_with_cancel` (with 
`NotFound` / `PermissionDenied` fallback for plain `std::io::Error`, and 
`Unexpected` otherwise).
   - **Fallible presigned request FFI conversion** 
(`bindings/c/src/presign.rs`): Make `opendal_presigned_request_inner::new` 
return `core::Result<Self>`, using `CString::new(v.as_bytes())` so valid 
non-ASCII `obs-text` header bytes succeed without panicking and interior NUL 
bytes return `OPENDAL_UNEXPECTED` with `*req = NULL`.
   - **FFI NULL guards and raw-pointer signatures** 
(`bindings/c/src/presign.rs`, `bindings/c/src/types.rs`, 
`bindings/c/src/layer.rs`, `bindings/c/include/opendal.h`):
     - Add `req.is_null() || (*req).inner.is_null()` checks to 
`opendal_presigned_request_{method,uri,headers,headers_len}`, returning `NULL` 
or `0`.
     - Change `opendal_operator_options_set`, 
`opendal_operator_layers_add_retry`, and `opendal_operator_layers_add_timeout` 
from `&mut self` to raw `*mut` pointers with NULL guards (including NULL 
`key`/`value` in `opendal_operator_options_set`). The regenerated 
`bindings/c/include/opendal.h` changes only parameter names (`self` -> `opts` / 
`layers`) with identical C ABI signatures.
   - **Reject out-of-range conditional timestamps** (`bindings/c/src/types.rs`, 
`bindings/c/src/operator.rs`, `bindings/c/src/presign.rs`): Switch 
`StatOptions`, `ReadOptions`, and `ReaderOptions` conversions to `TryFrom`, 
returning `OPENDAL_CONFIG_INVALID` (and initializing output parameters) when 
`has_if_modified_since` or `has_if_unmodified_since` is set with an 
out-of-range millisecond timestamp.
   - **Unit and C++ ASan regression tests** (`bindings/c/src/*.rs`, 
`bindings/c/tests/bdd.cpp`, `bindings/c/tests/reader.cpp`, 
`bindings/c/Cargo.toml`): Add Rust unit tests and GTest cases covering 
self-aliasing/substring option updates, reader/writer error-kind preservation, 
non-ASCII presigned header values, NULL pointer guards, and out-of-range 
timestamp rejection.
   
   # Are there any user-facing changes?
   
   - `opendal_reader_read*`, `opendal_reader_seek*`, `opendal_writer_write*`, 
and `opendal_writer_close*` now report the underlying `opendal_code` (such as 
`OPENDAL_NOT_FOUND` or `OPENDAL_CONDITION_NOT_MATCH`) instead of squashing all 
non-cancellation failures to `OPENDAL_UNEXPECTED`.
   - `opendal_operator_stat_with*`, `opendal_operator_read_with*`, 
`opendal_operator_reader_with*`, `opendal_operator_presign_read_with*`, and 
`opendal_operator_presign_stat_with*` now return `OPENDAL_CONFIG_INVALID` when 
`if_modified_since` or `if_unmodified_since` is enabled with an out-of-range 
millisecond timestamp instead of silently ignoring the precondition.
   - Presigned requests containing non-ASCII header values no longer abort the 
process via a panic across FFI.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi
   - Model: Gemini 4 Neon
   - Effort: high
   - Role: Audited `bindings/c`, implemented the five fixes and corresponding 
Rust/C++ regression tests, regenerated `bindings/c/include/opendal.h`, and ran 
local formatting, clippy, Rust unit tests, CMake ASan GTest suite, C behavior 
tests (`memory`), and strict C11/C++14 header compile checks.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to