2 * Copyright (c) 2003-2007,2016 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
31 * Github Issue #744 describes a bug in the sandboxing code that
32 * causes very long pathnames to not get checked for symlinks.
35 DEFINE_TEST(test_write_disk_secure744)
37 #if defined(_WIN32) && !defined(__CYGWIN__)
38 skipping("archive_write_disk security checks not supported on Windows");
41 struct archive_entry *ae;
42 size_t buff_size = 8192;
43 char *buff = malloc(buff_size);
50 /* Start with a known umask. */
53 /* Create an archive_write_disk object. */
54 assert((a = archive_write_disk_new()) != NULL);
55 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
57 while (p + 500 < buff + buff_size) {
62 buff[0] = ((n / 1000) % 10) + '0';
63 buff[1] = ((n / 100) % 10)+ '0';
64 buff[2] = ((n / 10) % 10)+ '0';
65 buff[3] = ((n / 1) % 10)+ '0';
69 /* Create a symlink pointing to the testworkdir */
70 assert((ae = archive_entry_new()) != NULL);
71 archive_entry_copy_pathname(ae, buff);
72 archive_entry_set_mode(ae, S_IFREG | 0777);
73 archive_entry_copy_symlink(ae, testworkdir);
74 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
75 archive_entry_free(ae);
78 sprintf(p, "target%d", n);
80 /* Try to create a file through the symlink, should fail. */
81 assert((ae = archive_entry_new()) != NULL);
82 archive_entry_copy_pathname(ae, buff);
83 archive_entry_set_mode(ae, S_IFDIR | 0777);
85 t = archive_write_header(a, ae);
86 archive_entry_free(ae);
87 failure("Attempt to create target%d via %d-character symlink should have failed", n, (int)strlen(buff));
88 if(!assertEqualInt(ARCHIVE_FAILED, t)) {