fitzgen / fitzgen/inlinable_string

Invalid utf8 str could still be created by `from_utf8_unchecked`

Open
#37 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
71
Forks
10
PR merge metrics
No merged PRs in 30d

Description

## Bypass `debug_assert!`
https://github.com/fitzgen/inlinable_string/blob/f7c0a330e1ebc5223925e8c956f5e7075c13b182/src/inline_string.rs#L275-L287
We consider that the protection of `assert_sanity` is not sufficient to check the validity of string due to removed `debug_assert!` in release mode.

## PoC
```rs
fn main() {
let mut s = InlineString::from("A");

{
let bytes: &mut [u8] = s.as_mut();
bytes[0] = 0xFF; // invalid UTF-8 byte
}

// attack vector 1: AsRef::as_ref -> from_utf8_unchecked
let _a: &str = s.as_ref();

// attack vector 2: Index -> from_utf8_unchecked
let _b: &str = &s[..];

// attack vector 3: Deref -> from_utf8_unchecked
let _c: &str = &*s;
}
```
With `cargo run` in nightly version
```sh
thread 'main' panicked at /home/usr/.cargo/registry/src/index.crates.io-6f17d22bba15001f/inlinable_string-0.1.15/src/inline_string.rs:283:9:
inlinable_string: internal error: contents are not valid UTF-8!
```
However, if run with `cargo run --release`, no error message will show. Maybe consider changing it to `assert!` instead? Or change it to [`str::from_utf8`](https://doc.rust-lang.org/std/str/fn.from_utf8.html) to do error handling could be more straightforward.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.