CVE-2018-1000810: std: Buffer overflow vulnenrability in str::repeat()

Description

The Rust team was recently notified of a security vulnerability affecting the str::repeat function in the standard library. If your code does not use this function, it is not affected.

Overview

This vulnerability is an instance of CWE-680: Integer Overflow to Buffer Overflow1.

The str::repeat function in the standard library allows repeating a string a fixed number of times, returning an owned version of the final string. The capacity of the final string is calculated by multiplying the length of the string being repeated by the number of copies. This calculation can overflow, and this case was not properly checked for.

The rest of the implementation of str::repeat contains unsafe code that relies on a preallocated vector having the capacity calculated earlier. On integer overflow the capacity will be less than required, and which then writes outside of the allocated buffer, leading to buffer overflow.

Affected Versions

While the str::repeat function has been in Rust since 1.16.0, this vulnerability was introduced into the standard library in pull request #48657 2. The pull request was merged on March 6, 2018 and was first part of the 1.26.0 stable released on May 10, 2018.

As such, this vulnerability affects:

Mitigations

This bug can be mitigated manually by auditing for calls to str::repeat and testing if the resulting vector’s capacity will overflow. If it does, then the program should panic.

For Rust 1.29, we’ll be releasing a 1.29.1 on 2018-09-25 with the fix, which consists of checking for overflow and deterministically panicking if it happens. Nightlies and betas produced after 2019-09-21 will also contain a fix for this issue.

We will not be releasing our own fixes for previous versions of Rust. The patch to fix 1.29 should roughly applicable to older versions, although the implementation has seen a few refactorings since it was introduced. The patch for 1.29 is included at the end of this email. If you need assistance patching an older version of Rust on your own, please reach out to our security mailing list, [email protected], and we’ll be happy to help.

The current beta and nightly channels will be updated with a fix for this issue as well.

Timeline of events

Acknowledgements

Thanks to Scott McMurray, who found this bug and reported it to us in accordance with our security policy https://www.rust-lang.org/security.html.

More Info

https://groups.google.com/forum/#!topic/rustlang-security-announcements/CmSuTm-SaU0

Patched Versions

Unaffected Versions