OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
libarchive_extract.cpp
Go to the documentation of this file.
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
16using namespace std::filesystem;
17
18namespace 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 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 std::error_code baseEc;
41 if (!exists(base, baseEc) || baseEc)
42 {
43 create_directories(base, baseEc);
44 if (baseEc)
45 {
46 errorOut = "failed creating destination root [" + base.string() + "]: " + baseEc.message();
47 return false;
48 }
49 }
50
51 std::filesystem::path current = base;
52 for (const auto& component : relativePath)
53 {
54 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 if (name.empty())
59 {
60 continue;
61 }
62 if (name == "." || name == "..")
63 {
64 errorOut = "unsafe component [" + name + "]";
65 return false;
66 }
67
68 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 std::error_code ec;
80 const auto status = symlink_status(current, ec);
81 if (ec && ec != std::errc::no_such_file_or_directory)
82 {
83 errorOut = "failed stat-ing [" + current.string() + "]: " + ec.message();
84 return false;
85 }
86 if (status.type() == std::filesystem::file_type::not_found)
87 {
88 create_directory(current, ec);
89 if (ec)
90 {
91 std::error_code swapEc;
92 if (is_symlink(current, swapEc))
93 {
94 errorOut = "refusing symlink component [" + current.string() + "]";
95 return false;
96 }
97 errorOut = "failed creating [" + current.string() + "]: " + ec.message();
98 return false;
99 }
100 }
101 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 errorOut = "path component is not a directory [" + current.string() + "]";
106 return false;
107 }
108 }
109 return true;
110 }
111 }
112
113 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 a = archive_read_new();
134 archive_read_support_format_all(a);
135 archive_read_support_filter_all(a);
136 r = archive_read_open_filename(a, filename, READ_OPEN_BLOCK_SIZE);
137 if (r != ARCHIVE_OK)
138 {
139 auto errNo = archive_errno(a);
140 fprintf(stderr, "%s (%d)\n", archive_error_string(a), errNo);
141 archive_read_free(a);
142 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 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 r = archive_read_next_header(a, &entry);
199 if (r == ARCHIVE_EOF)
200 break;
201 if (r < ARCHIVE_OK)
202 fprintf(stderr, "%s\n", archive_error_string(a));
203 if (r < ARCHIVE_WARN)
204 {
205 archive_read_free(a);
206 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 std::string entryPathname = archive_entry_pathname(entry);
216 if (entryPathname.empty() || !is_safe_relative_entry_path(entryPathname))
217 {
218 logerror("Refusing unsafe archive entry name: [" + entryPathname + "]");
219 archive_read_free(a);
220 return false;
221 }
222 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 std::filesystem::path entryDestinationPath = destinationDir / relativeEntryPath;
228 entryDestinationPath.make_preferred();
229
230 logdebug("Extracting, destination dir: [" + std::string(destDir) + "]");
231 logdebug("Extracting, entry path name: [" + entryPathname + "]");
232 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 if (archive_entry_hardlink(entry) != nullptr || archive_entry_symlink(entry) != nullptr)
239 {
240 logerror("Refusing link archive entry: [" + entryPathname + "]");
241 archive_read_free(a);
242 return false;
243 }
244 const auto fileType = archive_entry_filetype(entry);
245 if (fileType == AE_IFDIR)
246 {
247 std::string walkError;
248 if (!securely_create_directories(destinationDir, relativeEntryPath, walkError))
249 {
250 logerror("Failed creating extraction directory for [" + entryPathname + "]: " + walkError);
251 archive_read_free(a);
252 return false;
253 }
254 continue;
255 }
256
257 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 logerror("Refusing non-regular archive entry: [" + entryPathname + "]");
262 archive_read_free(a);
263 return false;
264 }
265
266 {
267 std::string walkError;
268 if (!securely_create_directories(destinationDir, relativeEntryPath.parent_path(), walkError))
269 {
270 logerror("Failed creating extraction parent directory for [" + entryPathname + "]: " + walkError);
271 archive_read_free(a);
272 return false;
273 }
274 }
275
276 // a pre-planted symlink AT the final name would make ofstream
277 // clobber its target outside the root
278 std::error_code targetSymlinkEc;
279 if (is_symlink(entryDestinationPath, targetSymlinkEc))
280 {
281 logerror("Refusing symlink extraction target [" + entryDestinationPath.string() + "]");
282 archive_read_free(a);
283 return false;
284 }
285
286 std::ofstream outputStream(entryDestinationPath, std::ios::binary | std::ios::trunc);
287 if (!outputStream)
288 {
289 logerror("Failed opening extraction file [" + entryDestinationPath.string() + "]");
290 archive_read_free(a);
291 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 la_int64_t expectedOffset = 0;
299 for (;;)
300 {
301 const void* buff = nullptr;
302 size_t size = 0;
303 la_int64_t offset = 0;
304
305 r = archive_read_data_block(a, &buff, &size, &offset);
306 if (r == ARCHIVE_EOF)
307 {
308 break;
309 }
310 if (r < ARCHIVE_OK)
311 {
312 fprintf(stderr, "%s\n", archive_error_string(a));
313 archive_read_free(a);
314 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 if (offset != expectedOffset)
325 {
326 logerror("Non-contiguous data extents for archive entry [" + entryPathname + "] (expected offset "
327 + std::to_string(expectedOffset) + ", got " + std::to_string(offset) + ")");
328 archive_read_free(a);
329 return false;
330 }
331
332 outputStream.write(static_cast<const char*>(buff), static_cast<std::streamsize>(size));
333 if (!outputStream)
334 {
335 logerror("Failed writing extraction file [" + entryDestinationPath.string() + "]");
336 archive_read_free(a);
337 return false;
338 }
339 expectedOffset = offset + static_cast<la_int64_t>(size);
340 }
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 if (expectedOffset != archive_entry_size(entry))
346 {
347 logerror("Incomplete archive entry [" + entryPathname + "] (declared "
348 + std::to_string(archive_entry_size(entry)) + " bytes, got "
349 + std::to_string(expectedOffset) + ")");
350 archive_read_free(a);
351 return false;
352 }
353 // the final flush happens at close - check it so ENOSPC/EIO is
354 // not reported as a successful extraction
355 outputStream.close();
356 if (!outputStream)
357 {
358 logerror("Failed flushing extraction file [" + entryDestinationPath.string() + "]");
359 archive_read_free(a);
360 return false;
361 }
362 }
363
364 archive_read_close(a);
365 archive_read_free(a);
366
367 return true;
368 }
369
370
371 constexpr inline static const char* TGZ_EXTENSION = ".tgz";
372 constexpr static const int READ_BUFF_SIZE = 1024;
373
374 bool compress_dir(const char* srcDir, const char* destDir, const char* archiveName, std::string& fileOut, CompressionType type /* = CompressionType::TarGzip*/)
375 {
376 path destArchivePath = path(destDir) / archiveName;
377 switch (type)
378 {
380 destArchivePath.replace_extension(TGZ_EXTENSION);
381 break;
382 }
383
384 struct archive* a = archive_write_new();
385 //if (archive_write_set_format_v7tar(a) != ARCHIVE_OK) return false;
386 if (archive_write_set_format_ustar(a) != ARCHIVE_OK) return false;
387 //if (archive_write_set_format_gnutar(a) != ARCHIVE_OK) return false;
388 if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) return false;
389
390 if (archive_write_open_filename(a, destArchivePath.string().c_str()) != ARCHIVE_OK) return false;
391
393 auto root_entry = archive_entry_new();
394 //auto rootDir = path(archiveName);
395 auto rootDir = path(srcDir).filename();
396 archive_entry_set_pathname(root_entry, rootDir.string().c_str());
397 archive_entry_set_filetype(root_entry, AE_IFDIR);
398 if (archive_write_header(a, root_entry) != ARCHIVE_OK) return false;
399 archive_entry_free(root_entry);
400
401 struct stat st;
402
403 // add files to the archive
404 for (const auto& it : recursive_directory_iterator(srcDir))
405 {
406 if (it.is_directory())
407 {
408 auto relativePath = relative(it.path(), srcDir);
409 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 archive_entry_set_pathname(dir_entry, (rootDir / relativePath).generic_string().c_str());
415 archive_entry_set_filetype(dir_entry, AE_IFDIR);
416 stat(it.path().string().c_str(), &st);
417 archive_entry_copy_stat(dir_entry, &st);
418 if (archive_write_header(a, dir_entry) != ARCHIVE_OK) return false;
419 archive_write_finish_entry(a);
420 archive_entry_free(dir_entry);
421 }
422 else if (it.is_regular_file())
423 {
424 // write header
425 auto relativePath = relative(it.path(), srcDir);
426 auto file_entry = archive_entry_new();
427 archive_entry_set_pathname(file_entry, (rootDir / relativePath).generic_string().c_str());
428 stat(it.path().string().c_str(), &st);
429 archive_entry_set_filetype(file_entry, AE_IFREG);
430 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 archive_entry_copy_stat(file_entry, &st);
434 if (archive_write_header(a, file_entry) != ARCHIVE_OK) return false;
435
436 std::ifstream ifs(it.path(), std::ios::in | std::ios::binary);
437 if (!ifs.is_open()) return false;
438 char szBuffer[READ_BUFF_SIZE]{ 0 };
439 while (true)
440 {
441 ifs.read(szBuffer, sizeof(szBuffer));
442 auto readin = ifs.gcount();
443 if (readin < 1)
444 {
445 break;
446 }
447 if (archive_write_data(a, szBuffer, readin) != readin) return false;
448 }
449 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 archive_write_finish_entry(a);
460 archive_entry_free(file_entry);
461 }
462 }
463
464 // clean up
465 archive_write_close(a);
466 archive_write_free(a);
467
468 fileOut = destArchivePath.string();
469 return true;
470 }
471}
#define logerror(s)
Definition Logger.h:17
#define logwarn(s)
Definition Logger.h:20
#define logdebug(s)
Definition Logger.h:23
bool extract(const char *filename, const char *destDir)
bool compress_dir(const char *srcDir, const char *destDir, const char *archiveName, std::string &fileOut, CompressionType type)
bool is_safe_relative_entry_path(const std::string &name)