]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_disk_secure745.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_disk_secure745.c
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 __FBSDID("$FreeBSD$");
27
28 #define UMASK 022
29
30 /*
31  * Github Issue #745 describes a bug in the sandboxing code that
32  * allows one to use a symlink to edit the permissions on a file or
33  * directory outside of the sandbox.
34  */
35
36 DEFINE_TEST(test_write_disk_secure745)
37 {
38 #if defined(_WIN32) && !defined(__CYGWIN__)
39         skipping("archive_write_disk security checks not supported on Windows");
40 #else
41         struct archive *a;
42         struct archive_entry *ae;
43
44         /* Start with a known umask. */
45         assertUmask(UMASK);
46
47         /* Create an archive_write_disk object. */
48         assert((a = archive_write_disk_new()) != NULL);
49         archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
50
51         /* The target dir:  The one we're going to try to change permission on */
52         assertMakeDir("target", 0700);
53
54         /* The sandbox dir we're going to run inside of. */
55         assertMakeDir("sandbox", 0700);
56         assertChdir("sandbox");
57
58         /* Create a symlink pointing to the target directory */
59         assert((ae = archive_entry_new()) != NULL);
60         archive_entry_copy_pathname(ae, "sym");
61         archive_entry_set_mode(ae, AE_IFLNK | 0777);
62         archive_entry_copy_symlink(ae, "../target");
63         assert(0 == archive_write_header(a, ae));
64         archive_entry_free(ae);
65
66         /* Try to alter the target dir through the symlink; this should fail. */
67         assert((ae = archive_entry_new()) != NULL);
68         archive_entry_copy_pathname(ae, "sym");
69         archive_entry_set_mode(ae, S_IFDIR | 0777);
70         assert(0 == archive_write_header(a, ae));
71         archive_entry_free(ae);
72
73         /* Permission of target dir should not have changed. */
74         assertFileMode("../target", 0700);
75
76         assert(0 == archive_write_close(a));
77         archive_write_free(a);
78 #endif
79 }