1 /*- 2 * Copyright (c) 2003-2007 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 __FBSDID("$FreeBSD$"); 27 28 DEFINE_TEST(test_read_pax_truncated) 29 { 30 struct archive_entry *ae; 31 struct archive *a; 32 size_t used, i, buff_size = 1000000; 33 size_t filedata_size = 100000; 34 char *buff = malloc(buff_size); 35 char *buff2 = malloc(buff_size); 36 char *filedata = malloc(filedata_size); 37 38 /* Create a new archive in memory. */ 39 assert((a = archive_write_new()) != NULL); 40 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_pax(a)); 41 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 42 assertEqualIntA(a, ARCHIVE_OK, 43 archive_write_open_memory(a, buff, buff_size, &used)); 44 45 /* 46 * Write a file to it. 47 */ 48 assert((ae = archive_entry_new()) != NULL); 49 archive_entry_copy_pathname(ae, "file"); 50 archive_entry_set_mode(ae, S_IFREG | 0755); 51 fill_with_pseudorandom_data(filedata, filedata_size); 52 53 archive_entry_set_atime(ae, 1, 2); 54 archive_entry_set_ctime(ae, 3, 4); 55 archive_entry_set_mtime(ae, 5, 6); 56 archive_entry_set_size(ae, filedata_size); 57 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 58 archive_entry_free(ae); 59 assertEqualIntA(a, (int)filedata_size, 60 (int)archive_write_data(a, filedata, filedata_size)); 61 62 /* Close out the archive. */ 63 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 64 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 65 66 /* Now, read back a truncated version of the archive and 67 * verify that we get an appropriate error. */ 68 for (i = 1; i < used + 100; i += 100) { 69 assert((a = archive_read_new()) != NULL); 70 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 71 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 72 /* If it's truncated very early, the file type detection should fail. */ 73 if (i < 512) { 74 assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory_minimal(a, buff, i, 13)); 75 goto wrap_up; 76 } else { 77 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_minimal(a, buff, i, 13)); 78 } 79 80 /* If it's truncated in a header, the header read should fail. */ 81 if (i < 1536) { 82 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 83 goto wrap_up; 84 } else { 85 failure("Archive truncated to %zu bytes", i); 86 assertEqualIntA(a, 0, archive_read_next_header(a, &ae)); 87 } 88 89 /* If it's truncated in the body, the body read should fail. */ 90 if (i < 1536 + filedata_size) { 91 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, filedata, filedata_size)); 92 goto wrap_up; 93 } else { 94 failure("Archive truncated to %zu bytes", i); 95 assertEqualIntA(a, filedata_size, 96 archive_read_data(a, filedata, filedata_size)); 97 } 98 99 /* Verify the end of the archive. */ 100 /* Archive must be long enough to capture a 512-byte 101 * block of zeroes after the entry. (POSIX requires a 102 * second block of zeros to be written but libarchive 103 * does not return an error if it can't consume 104 * it.) */ 105 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) { 106 failure("i=%zu minsize=%zu", i, 107 1536 + 512*((filedata_size + 511)/512) + 512); 108 assertEqualIntA(a, ARCHIVE_FATAL, 109 archive_read_next_header(a, &ae)); 110 } else { 111 assertEqualIntA(a, ARCHIVE_EOF, 112 archive_read_next_header(a, &ae)); 113 } 114 wrap_up: 115 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 116 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 117 } 118 119 120 121 /* Same as above, except skip the body instead of reading it. */ 122 for (i = 1; i < used + 100; i += 100) { 123 assert((a = archive_read_new()) != NULL); 124 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 125 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 126 /* If it's truncated very early, file type detection should fail. */ 127 if (i < 512) { 128 assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory(a, buff, i, 7)); 129 goto wrap_up2; 130 } else { 131 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, i, 7)); 132 } 133 134 if (i < 1536) { 135 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 136 goto wrap_up2; 137 } else { 138 assertEqualIntA(a, 0, archive_read_next_header(a, &ae)); 139 } 140 141 if (i < 1536 + 512*((filedata_size+511)/512)) { 142 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a)); 143 goto wrap_up2; 144 } else { 145 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a)); 146 } 147 148 /* Verify the end of the archive. */ 149 /* Archive must be long enough to capture a 512-byte 150 * block of zeroes after the entry. (POSIX requires a 151 * second block of zeros to be written but libarchive 152 * does not return an error if it can't consume 153 * it.) */ 154 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) { 155 assertEqualIntA(a, ARCHIVE_FATAL, 156 archive_read_next_header(a, &ae)); 157 } else { 158 assertEqualIntA(a, ARCHIVE_EOF, 159 archive_read_next_header(a, &ae)); 160 } 161 wrap_up2: 162 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 163 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 164 } 165 166 /* Now, damage the archive in various ways and test the responses. */ 167 168 /* Damage the first size field in the pax attributes. */ 169 memcpy(buff2, buff, buff_size); 170 buff2[512] = '9'; 171 buff2[513] = '9'; 172 buff2[514] = 'A'; /* Non-digit in size. */ 173 assert((a = archive_read_new()) != NULL); 174 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 175 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 176 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 177 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 178 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 179 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 180 181 /* Damage the size field in the pax attributes. */ 182 memcpy(buff2, buff, buff_size); 183 buff2[512] = 'A'; /* First character not a digit. */ 184 assert((a = archive_read_new()) != NULL); 185 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 186 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 187 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 188 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 189 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 190 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 191 192 /* Damage the size field in the pax attributes. */ 193 memcpy(buff2, buff, buff_size); 194 for (i = 512; i < 520; i++) /* Size over 999999. */ 195 buff2[i] = '9'; 196 buff2[i] = ' '; 197 assert((a = archive_read_new()) != NULL); 198 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 199 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 200 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 201 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 202 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 203 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 204 205 /* Damage the size field in the pax attributes. */ 206 memcpy(buff2, buff, buff_size); 207 buff2[512] = '9'; /* Valid format, but larger than attribute area. */ 208 buff2[513] = '9'; 209 buff2[514] = '9'; 210 buff2[515] = ' '; 211 assert((a = archive_read_new()) != NULL); 212 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 213 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 214 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 215 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 216 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 217 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 218 219 /* Damage the size field in the pax attributes. */ 220 memcpy(buff2, buff, buff_size); 221 buff2[512] = '1'; /* Too small. */ 222 buff2[513] = ' '; 223 assert((a = archive_read_new()) != NULL); 224 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 225 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 226 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 227 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 228 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 229 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 230 231 /* Damage the size field in the pax attributes. */ 232 memcpy(buff2, buff, buff_size); 233 buff2[512] = ' '; /* No size given. */ 234 assert((a = archive_read_new()) != NULL); 235 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 236 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 237 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 238 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 239 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 240 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 241 242 /* Damage the ustar header. */ 243 memcpy(buff2, buff, buff_size); 244 buff2[1024]++; /* Break the checksum. */ 245 assert((a = archive_read_new()) != NULL); 246 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 247 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 248 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used)); 249 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 250 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 251 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 252 253 /* 254 * TODO: Damage the ustar header in various ways and fixup the 255 * checksum in order to test boundary cases in the innermost 256 * ustar header parsing. 257 */ 258 259 free(buff); 260 free(buff2); 261 free(filedata); 262 } 263