libc icon indicating copy to clipboard operation
libc copied to clipboard

Request for feedback: Rust-native components as an alternative to extern "C" bindings

Open kernelSHE opened this issue 6 months ago • 4 comments

We’re exploring the development of a crate that would provide alternatives to the extern "C" APIs from this project (e.g., libc), keeping API compatibility and behavior equivalent. The motivation is that some bugs or unsafe assumptions in C libraries can propagate through the FFI boundary, even if Rust wrappers are careful.  We know rewriting mature C libraries is a big effort, so our idea is to provide optional Rust native code that replaces the C library for the selected ABI. We plan to maintain it actively and collaborate with the community, the crate would be fully maintained and actively developed by external contributors, without replacing the original library.  We’d like to understand the community’s stance: would maintainers and users be open to such a Rust-native crate existing and being maintained alongside the current project?

kernelSHE avatar May 12 '26 03:05 kernelSHE

I'm not sure exactly what is being proposed, but there is certainly no harm in other crates providing safer interfaces to the platform. nix and rustix are two such examples.

tgross35 avatar May 12 '26 05:05 tgross35

Thanks for the clarification. That makes sense.

To clarify our proposal: we are not suggesting changing the scope of 'rust-lang/libc', nor are we asking this project to replace or reimplement libc. We understand that crates such as 'nix' and 'rustix' provide safer interfaces to the platform, and our work is closer to an experimental external crate that provides Rust-native implementations for a selected subset of C ABI functions.

We have made some initial progress and implemented Rust-native alternatives for the following functions:

  • 'iconv'
  • 'iconv_close'
  • 'iconv_open'
  • 'strxfrm'
  • 'strftime'
  • 'memcmp'
  • 'unsetenv'
  • 'getifaddrs'
  • 'getgrouplist'
  • 'fnmatch'
  • 'calloc'

Our selection was based on C ABI functions that have historically had disclosed vulnerabilities, security-sensitive behavior, or unsafe assumptions that may propagate across the FFI boundary. For these functions, our goal is to preserve API/ABI compatibility where applicable and verify behavior equivalence through tests against the original libc behavior.

The current prototype repository is here:

https://github.com/kernelSHE/libc-safer-interfaces

At this stage, we would mainly appreciate feedback on the direction and positioning of the project. In particular, does it sound reasonable to maintain this as a separate external crate, clearly documented as an experimental Rust-native alternative for selected libc ABI functions, rather than something that belongs inside 'rust-lang/libc' itself?

Any suggestions on naming, scope boundaries, compatibility expectations, or test strategy would be very helpful.

kernelSHE avatar May 26 '26 07:05 kernelSHE

So the goal is to provide rust-written versions of libc functions? That's fine but you might be better off doing something like forking musl and rewriting functions there. Also note that you have an uphill security battle: there's a world of difference between existing battle-tested libcs and a fresh Rust port.

tgross35 avatar May 26 '26 09:05 tgross35

Closing because there doesn't seem to be any relation to Rust's libc.

tgross35 avatar May 26 '26 09:05 tgross35