1 /*- 2 * Copyright (c) 2003-2007,2016 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #include "test.h" 26 27 #define UMASK 022 28 29 /* 30 * Github Issue #746 describes a problem in which hardlink targets are 31 * not adequately checked and can be used to modify entries outside of 32 * the sandbox. 33 */ 34 35 /* 36 * Verify that ARCHIVE_EXTRACT_SECURE_NODOTDOT disallows '..' in hardlink 37 * targets. 38 */ 39 DEFINE_TEST(test_write_disk_secure746a) 40 { 41 struct archive *a; 42 struct archive_entry *ae; 43 44 /* Start with a known umask. */ 45 assertUmask(UMASK); 46 47 /* The target directory we're going to try to affect. */ 48 assertMakeDir("target", 0700); 49 assertMakeFile("target/foo", 0700, "unmodified"); 50 51 /* The sandbox dir we're going to work within. */ 52 assertMakeDir("sandbox", 0700); 53 assertChdir("sandbox"); 54 55 /* Create an archive_write_disk object. */ 56 assert((a = archive_write_disk_new()) != NULL); 57 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_NODOTDOT); 58 59 /* Attempt to hardlink to the target directory. */ 60 assert((ae = archive_entry_new()) != NULL); 61 archive_entry_copy_pathname(ae, "bar"); 62 archive_entry_set_mode(ae, AE_IFREG | 0777); 63 archive_entry_set_size(ae, 8); 64 archive_entry_copy_hardlink(ae, "../target/foo"); 65 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); 66 assertEqualInt(ARCHIVE_FATAL, archive_write_data(a, "modified", 8)); 67 archive_entry_free(ae); 68 69 /* Verify that target file contents are unchanged. */ 70 assertTextFileContents("unmodified", "../target/foo"); 71 72 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a)); 73 archive_write_free(a); 74 } 75 76 /* 77 * Verify that ARCHIVE_EXTRACT_SECURE_NOSYMLINK disallows symlinks in hardlink 78 * targets. 79 */ 80 DEFINE_TEST(test_write_disk_secure746b) 81 { 82 #if defined(_WIN32) && !defined(__CYGWIN__) 83 skipping("archive_write_disk security checks not supported on Windows"); 84 #else 85 struct archive *a; 86 struct archive_entry *ae; 87 88 /* Start with a known umask. */ 89 assertUmask(UMASK); 90 91 /* The target directory we're going to try to affect. */ 92 assertMakeDir("target", 0700); 93 assertMakeFile("target/foo", 0700, "unmodified"); 94 95 /* The sandbox dir we're going to work within. */ 96 assertMakeDir("sandbox", 0700); 97 assertChdir("sandbox"); 98 99 /* Create an archive_write_disk object. */ 100 assert((a = archive_write_disk_new()) != NULL); 101 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS); 102 103 /* Create a symlink to the target directory. */ 104 assert((ae = archive_entry_new()) != NULL); 105 archive_entry_copy_pathname(ae, "symlink"); 106 archive_entry_set_mode(ae, AE_IFLNK | 0777); 107 archive_entry_copy_symlink(ae, "../target"); 108 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 109 archive_entry_free(ae); 110 111 /* Attempt to hardlink to the target directory via the symlink. */ 112 assert((ae = archive_entry_new()) != NULL); 113 archive_entry_copy_pathname(ae, "bar"); 114 archive_entry_set_mode(ae, AE_IFREG | 0777); 115 archive_entry_set_size(ae, 8); 116 archive_entry_copy_hardlink(ae, "symlink/foo"); 117 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 118 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_data(a, "modified", 8)); 119 archive_entry_free(ae); 120 121 /* Verify that target file contents are unchanged. */ 122 assertTextFileContents("unmodified", "../target/foo"); 123 124 assertEqualIntA(a, ARCHIVE_FATAL, archive_write_close(a)); 125 archive_write_free(a); 126 #endif 127 } 128