Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++News

Buffer Overflow in the C/C++ RTL

We recently identified a potential buffer overflow in the C/C++ runtime library (RTL). Although this is an issue that was introduced to the code in 1997 with no exploitation since its introduction, we are publishing a notification and a fix for safety and openness, out of an abundance of caution.

Issue information:

  • Affects _wgetcurdir, _wsearchstr, plus the “_t” variants _tgetdcwd and _tgetcwd. These methods get the current working directory, and are non-ISO standard functions based on POSIX getcwd, or macros that can resolve to using them, extended for wide characters.
    • Affects Unicode builds
    • Only occurs when these methods are used when passed null as the buffer. In this situation, the method allocates the buffer itself, but for Unicode, only allocates half the space needed. This means some of the current working directory names may be written past the buffer bounds.
  • Affects applications (a) that use these methods and (b) are built with the classic, Win32 Clang, and old Win64 Clang (Clang v5) toolchains for Windows. These all use the affected RTL.
  • It does not affect the STL
  • It does not affect applications that are built with the new, Windows 64-bit Modern C++ toolchain
  • It does not affect C++Builder or RAD Studio itself

Recommended mitigation

Check if your source code uses any of these methods, and if so rebuild the RTL with the below patch applied, or if you use a version without source available or don’t want to / cannot rebuild the RTL, simply pass the method a pre-allocated buffer or replace usage of the method with the Windows API GetCurrentDirectory.

Patch

A patch, with the key lines highlighted (first a comment and second the affected line of code) is:

Apply this to your copy of getdcwd.c, and rebuild the RTL following the instructions in the RTL source directory. For RAD Studio 12.1, you can download a prebuilt version of the RTL on my.embarcadero.com named “C++Builder and RAD Studio 12.1 C++ RTL Replacement”. If you have any issues, we recommend contacting Support.

Affected versions

The issue affects C++Builder 12.1 and earlier, including the Community Edition and Free Compiler. Future versions of C++Builder and RAD Studio will no longer have this issue.

See What's Coming in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

David is an Australian developer, currently living in far-north Europe. He is the senior product manager for C++ at Idera, looking after C++Builder and Visual Assist.

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES