LCOV - code coverage report
Current view: top level - Utils - libarchive_extract.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 79.6 % 191 152
Test Date: 2026-11-19 04:56:22 Functions: 100.0 % 3 3
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "libarchive_extract.h"
       2              : #include <archive.h>
       3              : #include <archive_entry.h>
       4              : #include <iostream>
       5              : #include <filesystem>
       6              : #include "Logger.h"
       7              : #include "PathSafety.h"
       8              : #include <cstdio>
       9              : #include "FileReader.h"
      10              : #include <sys/stat.h>
      11              : #include <iosfwd>
      12              : 
      13              : 
      14              : // from: https://github.com/libarchive/libarchive/wiki/Examples#user-content-A_Complete_Extractor
      15              : 
      16              : using namespace std::filesystem;
      17              : 
      18              : namespace Utils
      19              : {
      20              :     namespace
      21              :     {
      22              :         const size_t BYTES_PER_KB = 1024UL;
      23              :         const size_t READ_OPEN_BLOCK_SIZE = BYTES_PER_KB * 10UL;
      24              : 
      25              :         // Walk the entry's relative path under destDir one component at a
      26              :         // time, refusing to traverse a symlink at any level (a pre-planted
      27              :         // symlink inside destDir would otherwise redirect directory creation
      28              :         // and ofstream writes outside the extraction root - the guarantee the
      29              :         // old ARCHIVE_EXTRACT_SECURE_SYMLINKS flag provided). create_directory
      30              :         // (singular) is used deliberately: it fails rather than follows when
      31              :         // the name already exists, and a symlink check after a failed create
      32              :         // closes the create-then-swap window as far as the filesystem allows.
      33        66189 :         bool securely_create_directories(const std::filesystem::path& base,
      34              :                                          const std::filesystem::path& relativePath,
      35              :                                          std::string& errorOut)
      36              :         {
      37              :             // the caller-supplied destination root may not exist yet; it is
      38              :             // trusted (operator-configured, canonicalized by ArchiveExtractor
      39              :             // before it reaches us), so create it recursively
      40        66189 :             std::error_code baseEc;
      41        66189 :             if (!exists(base, baseEc) || baseEc)
      42              :             {
      43           87 :                 create_directories(base, baseEc);
      44           87 :                 if (baseEc)
      45              :                 {
      46            0 :                     errorOut = "failed creating destination root [" + base.string() + "]: " + baseEc.message();
      47            0 :                     return false;
      48              :                 }
      49              :             }
      50              : 
      51        66189 :             std::filesystem::path current = base;
      52       430381 :             for (const auto& component : relativePath)
      53              :             {
      54       364193 :                 const auto name = component.string();
      55              :                 // a trailing separator ("step/") iterates as one real
      56              :                 // component plus an empty artifact - skip the artifact; the
      57              :                 // entry-name validation has already applied the real policy
      58       364193 :                 if (name.empty())
      59              :                 {
      60        17801 :                     continue;
      61              :                 }
      62       346392 :                 if (name == "." || name == "..")
      63              :                 {
      64            0 :                     errorOut = "unsafe component [" + name + "]";
      65            0 :                     return false;
      66              :                 }
      67              : 
      68       346392 :                 current /= component;
      69              : 
      70              :                 // Derive existence AND type from symlink_status (lstat - never
      71              :                 // follows links). exists()/is_directory() follow symlinks, so
      72              :                 // a link swapped in after a separate is_symlink probe would
      73              :                 // silently redirect the rest of the walk outside the root.
      74              :                 // One non-following status call closes that traversal window;
      75              :                 // the create-then-swap case stays covered by the post-create
      76              :                 // re-check below. (A swap between this status call and a later
      77              :                 // syscall is inherent to path-based APIs; the per-component
      78              :                 // refusal and the entry-name policy bound what it can reach.)
      79       346392 :                 std::error_code ec;
      80       346392 :                 const auto status = symlink_status(current, ec);
      81       346392 :                 if (ec && ec != std::errc::no_such_file_or_directory)
      82              :                 {
      83            0 :                     errorOut = "failed stat-ing [" + current.string() + "]: " + ec.message();
      84            0 :                     return false;
      85              :                 }
      86       346392 :                 if (status.type() == std::filesystem::file_type::not_found)
      87              :                 {
      88        16574 :                     create_directory(current, ec);
      89        16574 :                     if (ec)
      90              :                     {
      91            0 :                         std::error_code swapEc;
      92            0 :                         if (is_symlink(current, swapEc))
      93              :                         {
      94            0 :                             errorOut = "refusing symlink component [" + current.string() + "]";
      95            0 :                             return false;
      96              :                         }
      97            0 :                         errorOut = "failed creating [" + current.string() + "]: " + ec.message();
      98            0 :                         return false;
      99              :                     }
     100              :                 }
     101       329818 :                 else if (status.type() != std::filesystem::file_type::directory)
     102              :                 {
     103              :                     // symlink, regular file, device... anything that is not a
     104              :                     // real directory component is refused
     105            1 :                     errorOut = "path component is not a directory [" + current.string() + "]";
     106            1 :                     return false;
     107              :                 }
     108       364193 :             }
     109        66188 :             return true;
     110        66189 :         }
     111              :     }
     112              : 
     113          114 :     bool extract(const char* filename, const char* destDir)
     114              :     {
     115              :         // Security-audit F003: this extractor previously handed every entry to
     116              :         // archive_write_disk with PERM/ACL/FFLAGS restoration enabled, so a
     117              :         // crafted archive chose its own file modes, and ARCHIVE_EXTRACT_SECURE_
     118              :         // NOABSOLUTEPATHS could not simply be turned on: the write path
     119              :         // PREPENDS destDir to each entry and writes the joined ABSOLUTE
     120              :         // pathname, which that flag rejects outright (why it was commented
     121              :         // out). Instead of patching flags onto a write path we do not
     122              :         // control, extraction now takes write ownership itself: every entry
     123              :         // name is validated BEFORE any join, regular files are written
     124              :         // through ofstream (umask permissions - archive modes are never
     125              :         // applied), directories are created with default permissions, and
     126              :         // every other entry type (symlink, hardlink, device, fifo) is
     127              :         // refused. ODB++ design archives carry only files and directories,
     128              :         // so nothing legitimate is lost.
     129              :         struct archive* a;
     130              :         struct archive_entry* entry;
     131              :         int r;
     132              : 
     133          114 :         a = archive_read_new();
     134          114 :         archive_read_support_format_all(a);
     135          114 :         archive_read_support_filter_all(a);
     136          114 :         r = archive_read_open_filename(a, filename, READ_OPEN_BLOCK_SIZE);
     137          114 :         if (r != ARCHIVE_OK)
     138              :         {
     139            5 :             auto errNo = archive_errno(a);
     140            5 :             fprintf(stderr, "%s (%d)\n", archive_error_string(a), errNo);
     141            5 :             archive_read_free(a);
     142            5 :             return false;
     143              :         }
     144              : 
     145              :         // Windows long-path safety (security-audit F003 follow-up): libarchive's
     146              :         // write path silently applied \\?\ prefixes on Windows
     147              :         // (__la_win_permissive_name), which lifted the 248-character
     148              :         // CreateDirectoryW directory limit (and the 260-character file limit).
     149              :         // Taking write ownership with std::filesystem + ofstream lost that:
     150              :         // extraction destinations nest the archive stem twice by design
     151              :         // (dest/<stem>/<stem>/... - findRootDir descends one level), and deep
     152              :         // legitimate design trees crossed 248 chars, failing extraction with
     153              :         // ERROR_FILENAME_EXCED_RANGE ("The filename or extension is too
     154              :         // long"). Prefix the absolute destination with the literal wide-path
     155              :         // form so every create/open below can address the full tree. The
     156              :         // entry names joined onto it are validated relative paths (no . or ..
     157              :         // components), and ArchiveExtractor canonicalizes the destination
     158              :         // before it reaches us. No-op on non-Windows.
     159          109 :         std::filesystem::path destinationDir(destDir);
     160              : #if defined(_WIN32)
     161              :         {
     162              :             std::error_code absoluteError;
     163              :             const auto absoluteDestination = std::filesystem::absolute(destinationDir, absoluteError);
     164              :             if (!absoluteError)
     165              :             {
     166              :                 auto wide = absoluteDestination.wstring();
     167              :                 if (wide.rfind(L"\\\\?\\", 0) == 0)
     168              :                 {
     169              :                     destinationDir = std::filesystem::path(std::move(wide));
     170              :                 }
     171              :                 else if (wide.rfind(L"\\\\", 0) == 0)
     172              :                 {
     173              :                     wide.insert(2, L"?\\UNC\\");
     174              :                     destinationDir = std::filesystem::path(std::move(wide));
     175              :                 }
     176              :                 else
     177              :                 {
     178              :                     destinationDir = std::filesystem::path(L"\\\\?\\" + wide);
     179              :                 }
     180              :                 // the \\?\ form is literal: normalize separators on the ROOT
     181              :                 // too. MSVC's absolute() normalizes via GetFullPathNameW, but
     182              :                 // MinGW/libstdc++ preserves input separators for not-yet-
     183              :                 // existing components, so a "C:/designs"-style root would
     184              :                 // otherwise yield \\?\C:/designs and fail every create/open.
     185              :                 destinationDir.make_preferred();
     186              :             }
     187              :             else
     188              :             {
     189              :                 // proceeding unprefixed silently disables the long-path fix;
     190              :                 // make that degradation visible
     191              :                 logwarn("extract: absolute() failed for destination [" + destinationDir.string()
     192              :                     + "]: " + absoluteError.message() + " - proceeding without long-path prefix");
     193              :             }
     194              :         }
     195              : #endif
     196              : 
     197              :         for (;;) {
     198        66296 :             r = archive_read_next_header(a, &entry);
     199        66296 :             if (r == ARCHIVE_EOF)
     200          102 :                 break;
     201        66194 :             if (r < ARCHIVE_OK)
     202            0 :                 fprintf(stderr, "%s\n", archive_error_string(a));
     203        66194 :             if (r < ARCHIVE_WARN)
     204              :             {
     205            0 :                 archive_read_free(a);
     206            0 :                 return false;
     207              :             }
     208              : 
     209              :             // Validate the entry name BEFORE joining. std::filesystem
     210              :             // "base / name" discards the base entirely for a rooted name, and
     211              :             // a ".." component climbs out - reject both (and control
     212              :             // characters, drive forms, and backslash traversal). A hostile
     213              :             // entry fails the whole extraction: silently skipping an entry
     214              :             // would yield a design that parses wrong.
     215        66194 :             std::string entryPathname = archive_entry_pathname(entry);
     216        66194 :             if (entryPathname.empty() || !is_safe_relative_entry_path(entryPathname))
     217              :             {
     218            8 :                 logerror("Refusing unsafe archive entry name: [" + entryPathname + "]");
     219            4 :                 archive_read_free(a);
     220            4 :                 return false;
     221              :             }
     222        66190 :             const std::filesystem::path relativeEntryPath(entryPathname);
     223              :             // A \\?\-prefixed destination is interpreted literally: the
     224              :             // forward slashes carried by tar entry names must be normalized
     225              :             // to native separators before the join, or every create/open
     226              :             // below fails with invalid path syntax on Windows.
     227        66190 :             std::filesystem::path entryDestinationPath = destinationDir / relativeEntryPath;
     228        66190 :             entryDestinationPath.make_preferred();
     229              : 
     230       264760 :             logdebug("Extracting, destination dir: [" + std::string(destDir) + "]");
     231       132380 :             logdebug("Extracting, entry path name: [" + entryPathname + "]");
     232       132380 :             logdebug("Extracting, destination path name: [" + entryDestinationPath.string() + "]");
     233              : 
     234              :             // tar hardlink entries ('1') surface as AE_IFREG with the target
     235              :             // in archive_entry_hardlink() and size 0 - check the link targets
     236              :             // explicitly so a hardlink cannot create an alias/truncation
     237              :             // primitive inside the root
     238        66190 :             if (archive_entry_hardlink(entry) != nullptr || archive_entry_symlink(entry) != nullptr)
     239              :             {
     240            2 :                 logerror("Refusing link archive entry: [" + entryPathname + "]");
     241            1 :                 archive_read_free(a);
     242            1 :                 return false;
     243              :             }
     244        66189 :             const auto fileType = archive_entry_filetype(entry);
     245        66189 :             if (fileType == AE_IFDIR)
     246              :             {
     247        17801 :                 std::string walkError;
     248        17801 :                 if (!securely_create_directories(destinationDir, relativeEntryPath, walkError))
     249              :                 {
     250            0 :                     logerror("Failed creating extraction directory for [" + entryPathname + "]: " + walkError);
     251            0 :                     archive_read_free(a);
     252            0 :                     return false;
     253              :                 }
     254        17801 :                 continue;
     255        17801 :             }
     256              : 
     257        48388 :             if (fileType != AE_IFREG)
     258              :             {
     259              :                 // symlinks/devices/fifos have no place in a design archive
     260              :                 // and are write primitives in every other context
     261            0 :                 logerror("Refusing non-regular archive entry: [" + entryPathname + "]");
     262            0 :                 archive_read_free(a);
     263            0 :                 return false;
     264              :             }
     265              : 
     266              :             {
     267        48388 :                 std::string walkError;
     268        48388 :                 if (!securely_create_directories(destinationDir, relativeEntryPath.parent_path(), walkError))
     269              :                 {
     270            2 :                     logerror("Failed creating extraction parent directory for [" + entryPathname + "]: " + walkError);
     271            1 :                     archive_read_free(a);
     272            1 :                     return false;
     273              :                 }
     274        48388 :             }
     275              : 
     276              :             // a pre-planted symlink AT the final name would make ofstream
     277              :             // clobber its target outside the root
     278        48387 :             std::error_code targetSymlinkEc;
     279        48387 :             if (is_symlink(entryDestinationPath, targetSymlinkEc))
     280              :             {
     281            0 :                 logerror("Refusing symlink extraction target [" + entryDestinationPath.string() + "]");
     282            0 :                 archive_read_free(a);
     283            0 :                 return false;
     284              :             }
     285              : 
     286        48387 :             std::ofstream outputStream(entryDestinationPath, std::ios::binary | std::ios::trunc);
     287        48387 :             if (!outputStream)
     288              :             {
     289            0 :                 logerror("Failed opening extraction file [" + entryDestinationPath.string() + "]");
     290            0 :                 archive_read_free(a);
     291            0 :                 return false;
     292              :             }
     293              : 
     294              :             // stream the entry payload block by block; read_data_block
     295              :             // reports EOF at the end of THIS entry's data. Extents must be
     296              :             // contiguous: any gap or regression fails the extraction (see
     297              :             // the check below).
     298        48387 :             la_int64_t expectedOffset = 0;
     299              :             for (;;)
     300              :             {
     301       138284 :                 const void* buff = nullptr;
     302       138284 :                 size_t size = 0;
     303       138284 :                 la_int64_t offset = 0;
     304              : 
     305       138284 :                 r = archive_read_data_block(a, &buff, &size, &offset);
     306       138284 :                 if (r == ARCHIVE_EOF)
     307              :                 {
     308        48386 :                     break;
     309              :                 }
     310        89898 :                 if (r < ARCHIVE_OK)
     311              :                 {
     312            1 :                     fprintf(stderr, "%s\n", archive_error_string(a));
     313            1 :                     archive_read_free(a);
     314            1 :                     return false;
     315              :                 }
     316              : 
     317              :                 // Design archives are contiguous by construction (CAD tools
     318              :                 // do not emit sparse tars): ANY non-contiguous extent - a
     319              :                 // declared sparse hole (offset > expected, which zero-filling
     320              :                 // would materialize as attacker-declared unbounded zeros: a
     321              :                 // disk-exhaustion DoS) or a backwards/overlapping map
     322              :                 // (offset < expected, which silently shifts/duplicates
     323              :                 // content) - is malformed or hostile, so fail closed.
     324        89897 :                 if (offset != expectedOffset)
     325              :                 {
     326            0 :                     logerror("Non-contiguous data extents for archive entry [" + entryPathname + "] (expected offset "
     327              :                         + std::to_string(expectedOffset) + ", got " + std::to_string(offset) + ")");
     328            0 :                     archive_read_free(a);
     329            0 :                     return false;
     330              :                 }
     331              : 
     332        89897 :                 outputStream.write(static_cast<const char*>(buff), static_cast<std::streamsize>(size));
     333        89897 :                 if (!outputStream)
     334              :                 {
     335            0 :                     logerror("Failed writing extraction file [" + entryDestinationPath.string() + "]");
     336            0 :                     archive_read_free(a);
     337            0 :                     return false;
     338              :                 }
     339        89897 :                 expectedOffset = offset + static_cast<la_int64_t>(size);
     340        89897 :             }
     341              :             // the entry must be COMPLETE: an entry whose declared size
     342              :             // exceeds its actual data (e.g. a sparse record with a trailing
     343              :             // hole) would otherwise write a short file and report success -
     344              :             // inconsistent with the fail-closed extents posture above
     345        48386 :             if (expectedOffset != archive_entry_size(entry))
     346              :             {
     347            0 :                 logerror("Incomplete archive entry [" + entryPathname + "] (declared "
     348              :                     + std::to_string(archive_entry_size(entry)) + " bytes, got "
     349              :                     + std::to_string(expectedOffset) + ")");
     350            0 :                 archive_read_free(a);
     351            0 :                 return false;
     352              :             }
     353              :             // the final flush happens at close - check it so ENOSPC/EIO is
     354              :             // not reported as a successful extraction
     355        48386 :             outputStream.close();
     356        48386 :             if (!outputStream)
     357              :             {
     358            0 :                 logerror("Failed flushing extraction file [" + entryDestinationPath.string() + "]");
     359            0 :                 archive_read_free(a);
     360            0 :                 return false;
     361              :             }
     362       167990 :         }
     363              : 
     364          102 :         archive_read_close(a);
     365          102 :         archive_read_free(a);
     366              : 
     367          102 :         return true;
     368          109 :     }
     369              : 
     370              : 
     371              :     constexpr inline static const char* TGZ_EXTENSION = ".tgz";
     372              :     constexpr static const int READ_BUFF_SIZE = 1024;    
     373              : 
     374            5 :     bool compress_dir(const char* srcDir, const char* destDir, const char* archiveName, std::string& fileOut, CompressionType type /* = CompressionType::TarGzip*/)
     375              :     {
     376            5 :         path destArchivePath = path(destDir) / archiveName;
     377            5 :         switch (type)
     378              :         {
     379            5 :         case CompressionType::TarGzip:
     380            5 :             destArchivePath.replace_extension(TGZ_EXTENSION);
     381            5 :             break;
     382              :         }
     383              : 
     384            5 :         struct archive* a = archive_write_new();
     385              :         //if (archive_write_set_format_v7tar(a) != ARCHIVE_OK) return false;
     386            5 :         if (archive_write_set_format_ustar(a) != ARCHIVE_OK) return false;
     387              :         //if (archive_write_set_format_gnutar(a) != ARCHIVE_OK) return false;
     388            5 :         if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) return false;
     389              : 
     390            5 :         if (archive_write_open_filename(a, destArchivePath.string().c_str()) != ARCHIVE_OK) return false;
     391              : 
     392              :         //// add top-level directory to the archive
     393            5 :         auto root_entry = archive_entry_new();
     394              :         //auto rootDir = path(archiveName);
     395            5 :         auto rootDir = path(srcDir).filename();
     396            5 :         archive_entry_set_pathname(root_entry, rootDir.string().c_str());
     397            5 :         archive_entry_set_filetype(root_entry, AE_IFDIR);
     398            5 :         if (archive_write_header(a, root_entry) != ARCHIVE_OK) return false;
     399            5 :         archive_entry_free(root_entry);
     400              : 
     401              :         struct stat st;
     402              :         
     403              :         // add files to the archive               
     404           33 :         for (const auto& it : recursive_directory_iterator(srcDir))
     405              :                 {
     406           14 :             if (it.is_directory())
     407              :             {                                              
     408           11 :                 auto relativePath = relative(it.path(), srcDir);
     409           11 :                 auto dir_entry = archive_entry_new();
     410              :                 // generic_string(): tar entry names must use '/' on every
     411              :                 // host OS - a Windows-written archive with '\' names extracts
     412              :                 // as literal-backslash filenames on POSIX (and ustar's
     413              :                 // prefix/name split only works at '/' boundaries).
     414           11 :                 archive_entry_set_pathname(dir_entry, (rootDir / relativePath).generic_string().c_str());
     415           11 :                 archive_entry_set_filetype(dir_entry, AE_IFDIR);
     416           11 :                 stat(it.path().string().c_str(), &st);
     417           11 :                 archive_entry_copy_stat(dir_entry, &st);
     418           11 :                 if (archive_write_header(a, dir_entry) != ARCHIVE_OK) return false;
     419           11 :                 archive_write_finish_entry(a);
     420           11 :                 archive_entry_free(dir_entry);
     421           11 :             }
     422            3 :                         else if (it.is_regular_file())
     423              :                         {
     424              :                 // write header                         
     425            3 :                 auto relativePath = relative(it.path(), srcDir);
     426            3 :                 auto file_entry = archive_entry_new();
     427            3 :                 archive_entry_set_pathname(file_entry, (rootDir / relativePath).generic_string().c_str());
     428            3 :                 stat(it.path().string().c_str(), &st);
     429            3 :                 archive_entry_set_filetype(file_entry, AE_IFREG);
     430            3 :                 archive_entry_set_size(file_entry, st.st_size);  // Note 2
     431              :                 //archive_entry_set_mtime(file_entry, st.st_mtime, 0);
     432              :                 //archive_entry_set_perm(file_entry, st.st_mode?);                
     433            3 :                 archive_entry_copy_stat(file_entry, &st);
     434            3 :                 if (archive_write_header(a, file_entry) != ARCHIVE_OK) return false;
     435              :                 
     436            3 :                 std::ifstream ifs(it.path(), std::ios::in | std::ios::binary);
     437            3 :                 if (!ifs.is_open()) return false;
     438            3 :                 char szBuffer[READ_BUFF_SIZE]{ 0 };
     439              :                 while (true)
     440              :                 {
     441            6 :                     ifs.read(szBuffer, sizeof(szBuffer));
     442            6 :                     auto readin = ifs.gcount();
     443            6 :                     if (readin < 1)
     444              :                     {
     445            3 :                         break;
     446              :                     }
     447            3 :                     if (archive_write_data(a, szBuffer, readin) != readin) return false;
     448            3 :                 }
     449            3 :                 ifs.close();
     450              : 
     451              :                 // read file and write to archive
     452              :                 //FileReader fr(it.path());
     453              :                 //auto read = fr.Read();
     454              :                 //if (read > 0)
     455              :                 //{
     456              :                 //    auto& buffer = fr.GetBuffer();
     457              :                 //    if (archive_write_data(a, buffer.data(), read) != read) return false;
     458              :                 //}                               
     459            3 :                 archive_write_finish_entry(a);
     460            3 :                 archive_entry_free(file_entry);
     461            3 :                         }
     462            5 :                 }
     463              : 
     464              :         // clean up        
     465            5 :         archive_write_close(a);
     466            5 :         archive_write_free(a);
     467              : 
     468            5 :         fileOut = destArchivePath.string();
     469            5 :         return true;
     470            5 :     }     
     471              : }
        

Generated by: LCOV version 2.0-1