Skip to content

Commit 5935715

Browse files
committed
Add ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS option
This fixes a directory traversal in the cpio tool.
1 parent fc04ba0 commit 5935715

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

Diff for: cpio/bsdcpio.1

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ See above for description.
156156
.It Fl Fl insecure
157157
(i and p mode only)
158158
Disable security checks during extraction or copying.
159-
This allows extraction via symbolic links and path names containing
159+
This allows extraction via symbolic links, absolute paths,
160+
and path names containing
160161
.Sq ..
161162
in the name.
162163
.It Fl J , Fl Fl xz

Diff for: cpio/cpio.c

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ main(int argc, char *argv[])
171171
cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
172172
cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS;
173173
cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;
174+
cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
174175
cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
175176
cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
176177
cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
@@ -256,6 +257,7 @@ main(int argc, char *argv[])
256257
case OPTION_INSECURE:
257258
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
258259
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
260+
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
259261
break;
260262
case 'L': /* GNU cpio */
261263
cpio->option_follow_links = 1;

Diff for: libarchive/archive.h

+2
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ __LA_DECL int archive_read_set_passphrase_callback(struct archive *,
649649
/* Default: Do not use HFS+ compression if it was not compressed. */
650650
/* This has no effect except on Mac OS v10.6 or later. */
651651
#define ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED (0x8000)
652+
/* Default: Do not reject entries with absolute paths */
653+
#define ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS (0x10000)
652654

653655
__LA_DECL int archive_read_extract(struct archive *, struct archive_entry *,
654656
int flags);

Diff for: libarchive/archive_write_disk.3

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ The default is to not refuse such paths.
177177
Note that paths ending in
178178
.Pa ..
179179
always cause an error, regardless of this flag.
180+
.It Cm ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
181+
Refuse to extract an absolute path.
182+
The default is to not refuse such paths.
180183
.It Cm ARCHIVE_EXTRACT_SPARSE
181184
Scan data for blocks of NUL bytes and try to recreate them with holes.
182185
This results in sparse files, independent of whether the archive format

Diff for: libarchive/archive_write_disk_posix.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -2509,8 +2509,9 @@ cleanup_pathname_win(struct archive_write_disk *a)
25092509
/*
25102510
* Canonicalize the pathname. In particular, this strips duplicate
25112511
* '/' characters, '.' elements, and trailing '/'. It also raises an
2512-
* error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
2513-
* set) any '..' in the path.
2512+
* error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is
2513+
* set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
2514+
* is set) if the path is absolute.
25142515
*/
25152516
static int
25162517
cleanup_pathname(struct archive_write_disk *a)
@@ -2529,8 +2530,15 @@ cleanup_pathname(struct archive_write_disk *a)
25292530
cleanup_pathname_win(a);
25302531
#endif
25312532
/* Skip leading '/'. */
2532-
if (*src == '/')
2533+
if (*src == '/') {
2534+
if (a->flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) {
2535+
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2536+
"Path is absolute");
2537+
return (ARCHIVE_FAILED);
2538+
}
2539+
25332540
separator = *src++;
2541+
}
25342542

25352543
/* Scan the pathname one element at a time. */
25362544
for (;;) {

Diff for: libarchive/test/test_write_disk_secure.c

+23
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ DEFINE_TEST(test_write_disk_secure)
178178
assert(S_ISDIR(st.st_mode));
179179
archive_entry_free(ae);
180180

181+
/*
182+
* Without security checks, we should be able to
183+
* extract an absolute path.
184+
*/
185+
assert((ae = archive_entry_new()) != NULL);
186+
archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
187+
archive_entry_set_mode(ae, S_IFREG | 0777);
188+
assert(0 == archive_write_header(a, ae));
189+
assert(0 == archive_write_finish_entry(a));
190+
assertFileExists("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
191+
assert(0 == unlink("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp"));
192+
193+
/* But with security checks enabled, this should fail. */
194+
assert(archive_entry_clear(ae) != NULL);
195+
archive_entry_copy_pathname(ae, "/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
196+
archive_entry_set_mode(ae, S_IFREG | 0777);
197+
archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS);
198+
failure("Extracting an absolute path should fail here.");
199+
assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
200+
archive_entry_free(ae);
201+
assert(0 == archive_write_finish_entry(a));
202+
assertFileNotExists("/tmp/libarchive_test-test_write_disk_secure-absolute_path.tmp");
203+
181204
assertEqualInt(ARCHIVE_OK, archive_write_free(a));
182205

183206
/* Test the entries on disk. */

0 commit comments

Comments
 (0)