Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Delphi RTL TZipFile Security Patch

Summary 

Embarcadero is releasing a security update for the Delphi Runtime Library (RTL) System.Zip.TZipFile class, addressing vulnerabilities that could affect applications extracting ZIP archives from untrusted sources. This update is also relevant for customers using C++Builder and RAD Studio, if they use the Delphi RTL in their applications. 

Under specific conditions, a specially crafted ZIP archive could cause an application to create or overwrite files outside the intended extraction directory. 

The scope of these vulnerabilities is limited to applications using the TZipFile class to process untrusted, potentially malicious and specially crafted ZIP archives. Most Delphi and C++Builder applications are not expected to be affected, particularly those that do not use TZipFile, do not extract ZIP files, or exclusively process archives from trusted sources. 

Embarcadero is providing a binary patch for RAD Studio 13 and source code updates for developers using Delphi or C++Builder 11 and 12. We are also in the process of releasing an updated version of RAD Studio 13.2 with the security patch embedded.  

Affected Scenarios 

These security vulnerabilities are relevant only to applications that use the Delphi RTL TZipFile class to extract ZIP archives from untrusted or potentially malicious sources. They do not affect the IDE. 

Examples include applications that: 

Applications that do not use TZipFile for extraction, that exclusively create ZIP archives, or that process ZIP files from trusted and controlled sources, are generally not exposed to these attack scenarios. 

Even when processing untrusted archives, exploitation depends on the application’s extraction workflow, the operating system, and the filesystem permissions available to the application. 

Developers building applications that automatically extract ZIP archives from external sources should review their exposure and apply the relevant update. 

Security Issues Addressed 

The update addresses two categories of security vulnerabilities in the Delphi RTL ZIP extraction implementation. 

1. ZIP Extraction Path Traversal

The ZIP extraction implementation does not adequately validate that extracted file paths remain within the specified destination directory. 

A malicious ZIP archive can contain filenames with relative path traversal sequences (such as ../) or absolute paths, potentially causing files to be written outside the intended extraction location. 

A related issue involves the ZIP Unicode Path extra field (header ID 0x7075), which can specify an alternative filename that differs from the filename recorded in the archive’s central directory. This could allow a malicious archive to bypass applications that inspect and validate ZIP entry filenames before extraction. 

Both issues stem from insufficient validation of archive-controlled filenames and extraction paths.

2. Uncontrolled Symbolic Link Creation

When ZIP entry attributes indicate a symbolic link, TZipFile can automatically create that link using a target specified in the archive. 

A malicious archive could exploit this behavior to create symbolic links pointing outside the extraction directory, potentially redirecting subsequent file extraction operations to unintended locations. 

This scenario is particularly relevant on POSIX platforms, where symbolic link creation generally does not require special privileges. On Windows, symbolic link creation is subject to additional operating system restrictions. 

Potential Impact 

In affected applications, successful exploitation could allow unauthorized file creation or overwriting within the filesystem permissions of the application performing the extraction. 

Depending on the application’s environment, this could result in modification of application data, configuration files, or other resources accessible to the process. 

Remediation 

Embarcadero has updated the Delphi RTL TZipFile extraction implementation to address these vulnerabilities. The changes strengthen validation of ZIP entry filenames and extraction paths, preventing archive-controlled paths from escaping the intended extraction directory. The update also addresses the handling of symbolic links originating from ZIP archive entries. 

We recommend that developers review applications that extract ZIP archives from untrusted sources, apply the relevant update, rebuild these applications, and redeploy them to their customers. 

Because the affected functionality is part of the Delphi RTL, applications incorporating the vulnerable implementation may need to be rebuilt with the updated library to benefit from these corrections. 

Potential Side Effect of the Fix 

There is a possibility that the stronger check on file names and paths changes the behavior of the extraction of legitimate ZIP files, which were leveraging the potentially harmful configuration in correct and controlled scenarios. In these cases, we recommend reviewing the ZIP file content and process. The note below is from Embarcadero R&D. 

For archives written without the UTF-8 flag, FileName, FileNames, IndexOf and GetHeaderFileName now return the Unicode name from the archive’s central directory, the same name Extract uses. Before, they returned the name decoded with Encoding, while Extract used a name from the local header. A Unicode name is ignored when it isn’t valid UTF-8 or isn’t a safe relative path (absolute, drive, .., stream names). The name is then decoded with Encoding; for archives created on Linux or macOS, set Encoding to TEncoding.UTF8. Rename now removes the Unicode name of the renamed entry. GetHeaderFileName gives the extraction name only for headers from FileInfo or FileInfos, not for the header returned by Read or passed to OnProgress. 

Patch for RAD Studio 13 (Binary) 

For RAD Studio 13.x customers, Embarcadero is providing this patch, containing the updated Delphi RTL ZIP implementation, in a new version of the units System.Zip.pas and System.SysUtils.pas. The patch for 13.x versions includes the binary DCU and .O versions of the units for each supported platform and configuration. It also includes matching RTL packages (rtl370.bpl) for each platform that offers runtime packages and the corresponding .lib and .a files for C++Builder. 

The patch allows developers to incorporate the security fixes into their applications without manually modifying the RTL source code or including the modified file in their application. 

Embarcadero is also in the process of releasing an updated version of RAD Studio 13.2 with the security patch embedded.  

Patch availability: GetIt package manager in RAD Studio 13 versions (Tools > GetIt Package Manager) and public download at these links:

Installation Instructions (RAD Studio 13)  

After installing the patch, developers should rebuild affected applications to ensure they include the corrected RTL implementation and inform their customers of the available update. 

Patch for RAD Studio 11 and 12 (Source) 

For customers using Delphi and C++Builder 11 and 12, Embarcadero is also making the relevant RTL source code changes available. 

These changes allow developers to address the vulnerabilities in applications built with earlier Delphi releases, where a patched binary is not available. After the download, follow the steps below to rebuild your application with the patched version of the TZipFile class. 

Source patch availability for RAD Studio 12: Download link  (SHA256: FBD3A0F185DBE647C3A8623F8FD97C77EB7A8D2995FBE3E76AEEFA95E5148C1E)

Source patch availability for RAD Studio 11: Download link (SHA256: 419EE9AA9E1DFECA51127F77ECE98498D35320411388813BCAC2E2F44DA4C18A)

Installation instructions (RAD Studio 11 and 12) for applications not using runtime packages 

These steps apply when your application both uses static linking (it does not use the RTL runtime package) and uses the System.Zip unit. 

1. Check whether your application uses System.Zip
You can inspect the source code for references to System.Zip, but the unit may also be included indirectly through another unit.
If you have a .MAP file from the build, check whether it lists System.Zip. If it does, the application includes that unit.

2. How to Use the updated unit

Choose one of these options: 

Installation instructions (RAD Studio 11 and 12) for applications using runtime packages 

These steps apply if your application is built with runtime packages and directly references rtlxxx.bpl. This is relevant only if your application actually uses the System.Zip unit.  

How to rebuild the RTL package 

Optional Preparation (for Delphi non-Windows platforms) 

The batch file for building the Delphi RTL does the operation for each of the supported target platforms. It assumes the various platform SDKs have been configured. If you don’t have them configured, it’s likely you are not actively using them, so no additional steps are required (however, the RTL build script will fail for those unconfigured platforms). 

 If you want to rebuild the RTL package for all platforms, before you run the steps below, you need to have the various platform SDKs configured in the SDK Manager. You can configure the SDKs as follows: 

Actual Steps 
  1. Open a console (Command Prompt) on the machine where RAD Studio is installed. You can also use the specific RAD Studio Command Prompt installed along with RAD Studio (this automatically includes step 2 below).
  2. Invoke rsvars.bat (available in RAD Studio bin folder) to set up the build environment (this step is not needed if you used the RAD Studio Command Prompt). To verify that the compiler is available to the command prompt, you can run: where dcc32.
  3. Copy the source code to a writable location of your choice

    For example, in a console: [Text Wrapping Break]xcopy /I/S “C:\Program Files (x86)\Embarcadero\Studio\23.0\source\rtl” “c:\work\rtl” 

    (The source folder is under 23.0 for 12.x versions of RAD Studio, 22.0 for 11.x versions, and similar)  
  4. Copy the updated System.Zip.pas and System.SysUtils.pas to the common subfolder of your copy of the rtl source code folder.
  5. Move to the working folder where you copied the RTL source code (“cd c:\work\rtl”)
  6. Invoke buildrtl.bat debug to generate debug version binaries
  7. Invoke buildrtl.bat release to generate release version binaries

Once the process is complete, you can find the new version of the DCU files for the runtime library in the Public Documents folder, for example, under: 

 C:\Users\Public\Documents\Embarcadero\Studio\xx.0\lib\

 The new runtime packages will be in platform specific folders under: 

 C:\Users\Public\Documents\Embarcadero\Studio\[version number]\Bpl\

The compiled package files are called BuildWinRTL290.bpl or BuildWinRTL280.bpl (respectively for RAD Studio 12 and 11) and you need to rename them as RTL290.bpl or RTL280.bpl, place them in the folders of your application executable files, and distribute these new versions along with your application. You can also replace the version in the RAD Studio installation folder. 

Workaround for versions without a patch (RAD Studio XE2 through 10.4) 

If you can’t move to a version covered by the patches, you can protect your application by not calling TZipFile.Extract or TZipFile.ExtractAll on untrusted archives. Instead, read each entry’s contents and write the file yourself, to a path your application computes and validates: 

Checking TZipFile.FileNames and then calling Extract is not sufficient on unpatched versions, because Extract can write to a different name than the one FileNames reports. 

[crayon-6b07b77b0e79b587455497/]

This sample code loads each entry into memory. For large archives, use the stream overload of TZipFile.Read instead. It’s safer to extract into a new, empty directory, because symlinks and junctions that already exist in the destination are followed. With this approach, file times and attributes from the ZIP archive won’t be restored, and setuid bits are not restored either. 

Commitment to Security 

Embarcadero remains committed to maintaining the security and reliability of the Delphi development ecosystem. We encourage developers processing untrusted ZIP archives to review their applications and apply the appropriate security update as soon as possible. 

Exit mobile version