Blame libarchive/test/test_write_disk_secure745.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2007,2016 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
#include "test.h"
Packit 08bd4c
__FBSDID("$FreeBSD$");
Packit 08bd4c
Packit 08bd4c
#define UMASK 022
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Github Issue #745 describes a bug in the sandboxing code that
Packit 08bd4c
 * allows one to use a symlink to edit the permissions on a file or
Packit 08bd4c
 * directory outside of the sandbox.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
DEFINE_TEST(test_write_disk_secure745)
Packit 08bd4c
{
Packit 08bd4c
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 08bd4c
	skipping("archive_write_disk security checks not supported on Windows");
Packit 08bd4c
#else
Packit 08bd4c
	struct archive *a;
Packit 08bd4c
	struct archive_entry *ae;
Packit 08bd4c
Packit 08bd4c
	/* Start with a known umask. */
Packit 08bd4c
	assertUmask(UMASK);
Packit 08bd4c
Packit 08bd4c
	/* Create an archive_write_disk object. */
Packit 08bd4c
	assert((a = archive_write_disk_new()) != NULL);
Packit 08bd4c
	archive_write_disk_set_options(a, ARCHIVE_EXTRACT_SECURE_SYMLINKS);
Packit 08bd4c
Packit 08bd4c
	/* The target dir:  The one we're going to try to change permission on */
Packit 08bd4c
	assertMakeDir("target", 0700);
Packit 08bd4c
Packit 08bd4c
	/* The sandbox dir we're going to run inside of. */
Packit 08bd4c
	assertMakeDir("sandbox", 0700);
Packit 08bd4c
	assertChdir("sandbox");
Packit 08bd4c
Packit 08bd4c
	/* Create a symlink pointing to the target directory */
Packit 08bd4c
	assert((ae = archive_entry_new()) != NULL);
Packit 08bd4c
	archive_entry_copy_pathname(ae, "sym");
Packit 08bd4c
	archive_entry_set_mode(ae, AE_IFLNK | 0777);
Packit 08bd4c
	archive_entry_copy_symlink(ae, "../target");
Packit 08bd4c
	assert(0 == archive_write_header(a, ae));
Packit 08bd4c
	archive_entry_free(ae);
Packit 08bd4c
Packit 08bd4c
	/* Try to alter the target dir through the symlink; this should fail. */
Packit 08bd4c
	assert((ae = archive_entry_new()) != NULL);
Packit 08bd4c
	archive_entry_copy_pathname(ae, "sym");
Packit 08bd4c
	archive_entry_set_mode(ae, S_IFDIR | 0777);
Packit 08bd4c
	assert(0 == archive_write_header(a, ae));
Packit 08bd4c
	archive_entry_free(ae);
Packit 08bd4c
Packit 08bd4c
	/* Permission of target dir should not have changed. */
Packit 08bd4c
	assertFileMode("../target", 0700);
Packit 08bd4c
Packit 08bd4c
	assert(0 == archive_write_close(a));
Packit 08bd4c
	archive_write_free(a);
Packit 08bd4c
#endif
Packit 08bd4c
}