Description
Entry pathnames and link targets returned by libarchive functions such as archive_entry_pathname(), archive_entry_hardlink(), and archive_entry_symlink() come from the archive. Passing them directly to filesystem APIs can select a location outside the intended extraction directory through absolute paths, .. components, or link redirection.
archive_entry_sourcepath() has a different meaning. It returns the local filesystem source path used by archive_read_disk when creating an archive; that value itself is not stored in the archive.
Potential impact
- Files may be read, or files and directories created, replaced, renamed, or deleted with the process's permissions.
- Configuration files, executables, deployment paths, or another user's data may be altered.
Remediation
Choose a fixed, trusted extraction directory. When using libarchive, enable all three path checks together.
ARCHIVE_EXTRACT_SECURE_NODOTDOT: reject paths containing..components.ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS: reject absolute paths.ARCHIVE_EXTRACT_SECURE_SYMLINKS: reject entries whose destination would be redirected by symlinks on disk.
In libarchive 3.8.9 these checks are off by default. Pass the flags to archive_read_extract(). With archive_read_extract2(), use archive_write_disk_set_options() to set them on the archive_write_disk object.
ARCHIVE_EXTRACT_SAFE_WRITES writes to a temporary file and renames it to avoid partial writes. Path confinement and protection against overwriting existing files each require their own controls.
For a custom extractor, check both the entry name and any stored hard-link or symlink target. Reject absolute paths for the target platform and components equal to ... If links are unnecessary, reject link entries. If directories are unnecessary, accept only a single filename with no separators.
Open the extraction directory first. Use an OS API that keeps each lookup inside that directory. Prefix comparisons on a normalized path and basename() alone cannot safely handle links, platform-specific separators, and roots. C++ functions such as std::filesystem::path::filename(), lexically_normal(), canonical(), and weakly_canonical() also leave a gap between checking a path and opening it. Enforce the restriction when opening the file.
Examples
C
Before
#include <archive_entry.h>
#include <stdio.h>
void extract_unsafe(struct archive_entry *entry) {
const char *name = archive_entry_pathname(entry);
FILE *out = fopen(name, "wb");
if (out != NULL) {
fclose(out);
}
}
After
#include <archive.h>
#include <archive_entry.h>
int extract_entry(struct archive *reader, struct archive_entry *entry) {
const int flags = ARCHIVE_EXTRACT_SECURE_NODOTDOT |
ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS |
ARCHIVE_EXTRACT_SECURE_SYMLINKS;
return archive_read_extract(reader, entry, flags);
}
Set the process’s working directory to the trusted extraction root before calling this function. Stop extracting the entry if libarchive returns an error.
If you do not need subdirectories, accept only single-component filenames.
#if defined(__APPLE__)
#define _DARWIN_C_SOURCE
#else
#define _POSIX_C_SOURCE 200809L
#endif
#include <archive_entry.h>
#include <fcntl.h>
#include <string.h>
int open_flat_entry(int output_dir_fd, struct archive_entry *entry) {
const char *name = archive_entry_pathname(entry);
if (name == NULL || name[0] == '\0' ||
strchr(name, '/') != NULL || strchr(name, '\\') != NULL ||
strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
return -1;
}
return openat(output_dir_fd, name,
O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
}
The function rejects path separators, . and ... It uses openat() relative to a trusted, already-open output_dir_fd and refuses existing files or a symlink in the final component.
C++
Before
#include <archive_entry.h>
#include <filesystem>
#include <fstream>
void extract_unsafe_cpp(struct archive_entry *entry) {
const auto leaf = std::filesystem::path(
archive_entry_pathname(entry)).filename();
std::ofstream output{leaf};
}
filename() selects the last path component. The example still lacks a trusted extraction root and a check for existing symlinks when opening the file.
After
#include <archive.h>
#include <archive_entry.h>
int extract_entry_cpp(struct archive *reader, struct archive_entry *entry) {
constexpr int flags = ARCHIVE_EXTRACT_SECURE_NODOTDOT |
ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS |
ARCHIVE_EXTRACT_SECURE_SYMLINKS;
return archive_read_extract(reader, entry, flags);
}
This C++ version uses the same three libarchive checks. Set the working directory to the trusted extraction root and treat a libarchive error as a failed extraction.