The vulnerability is a type confusion in the obfstr crate, specifically within the obfstr! macro. The analysis of commit b57034ced6df94329a7595a059d0fdb7332855d3 and the associated issue #60 reveals the following:
- The
obfstr! macro (defined in src/bytes.rs) did not enforce that its input argument was a string slice (&str) before calling the .as_bytes() method on it. This is evident from the patch, which adds ::core::convert::identity::<&str>() to coerce the input to a string slice.
- If a non-string type that still provided an
as_bytes() method was passed to obfstr!, the resulting byte array might not be valid UTF-8.
- This potentially invalid byte array was then passed to the
unsafe_as_str function (defined in src/lib.rs, as per issue #60, and called by obfstr!).
- The
unsafe_as_str function performs a direct, unchecked conversion from bytes to a string slice (core::str::from_utf8_unchecked). If the bytes are not valid UTF-8, this results in undefined behavior.
Therefore, obfstr! is vulnerable because it initiates the type confusion by improperly handling input. unsafe_as_str is the function where the consequence of this confusion (unsafe UTF-8 conversion on invalid bytes) occurs. Both would be involved in a runtime scenario where the vulnerability is triggered. The patch mitigates the vulnerability by ensuring obfstr! correctly prepares its input before it reaches unsafe_as_str.